Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | #include "Logging.h" |
michael@0 | 8 | #include "SourceSurfaceSkia.h" |
michael@0 | 9 | #include "skia/SkBitmap.h" |
michael@0 | 10 | #include "skia/SkDevice.h" |
michael@0 | 11 | #include "HelpersSkia.h" |
michael@0 | 12 | #include "DrawTargetSkia.h" |
michael@0 | 13 | #include "DataSurfaceHelpers.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | namespace gfx { |
michael@0 | 17 | |
michael@0 | 18 | SourceSurfaceSkia::SourceSurfaceSkia() |
michael@0 | 19 | : mDrawTarget(nullptr), mLocked(false) |
michael@0 | 20 | { |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | SourceSurfaceSkia::~SourceSurfaceSkia() |
michael@0 | 24 | { |
michael@0 | 25 | MaybeUnlock(); |
michael@0 | 26 | if (mDrawTarget) { |
michael@0 | 27 | mDrawTarget->SnapshotDestroyed(); |
michael@0 | 28 | mDrawTarget = nullptr; |
michael@0 | 29 | } |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | IntSize |
michael@0 | 33 | SourceSurfaceSkia::GetSize() const |
michael@0 | 34 | { |
michael@0 | 35 | return mSize; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | SurfaceFormat |
michael@0 | 39 | SourceSurfaceSkia::GetFormat() const |
michael@0 | 40 | { |
michael@0 | 41 | return mFormat; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | bool |
michael@0 | 45 | SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas, |
michael@0 | 46 | SurfaceFormat aFormat, |
michael@0 | 47 | DrawTargetSkia* aOwner) |
michael@0 | 48 | { |
michael@0 | 49 | SkISize size = aCanvas->getDeviceSize(); |
michael@0 | 50 | |
michael@0 | 51 | mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false); |
michael@0 | 52 | mFormat = aFormat; |
michael@0 | 53 | |
michael@0 | 54 | mSize = IntSize(size.fWidth, size.fHeight); |
michael@0 | 55 | mStride = mBitmap.rowBytes(); |
michael@0 | 56 | mDrawTarget = aOwner; |
michael@0 | 57 | |
michael@0 | 58 | return true; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | bool |
michael@0 | 62 | SourceSurfaceSkia::InitFromData(unsigned char* aData, |
michael@0 | 63 | const IntSize &aSize, |
michael@0 | 64 | int32_t aStride, |
michael@0 | 65 | SurfaceFormat aFormat) |
michael@0 | 66 | { |
michael@0 | 67 | SkBitmap temp; |
michael@0 | 68 | temp.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride); |
michael@0 | 69 | temp.setPixels(aData); |
michael@0 | 70 | |
michael@0 | 71 | if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) { |
michael@0 | 72 | return false; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | if (aFormat == SurfaceFormat::B8G8R8X8) { |
michael@0 | 76 | mBitmap.setAlphaType(kIgnore_SkAlphaType); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | mSize = aSize; |
michael@0 | 80 | mFormat = aFormat; |
michael@0 | 81 | mStride = mBitmap.rowBytes(); |
michael@0 | 82 | return true; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | unsigned char* |
michael@0 | 86 | SourceSurfaceSkia::GetData() |
michael@0 | 87 | { |
michael@0 | 88 | if (!mLocked) { |
michael@0 | 89 | mBitmap.lockPixels(); |
michael@0 | 90 | mLocked = true; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | unsigned char *pixels = (unsigned char *)mBitmap.getPixels(); |
michael@0 | 94 | return pixels; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | void |
michael@0 | 98 | SourceSurfaceSkia::DrawTargetWillChange() |
michael@0 | 99 | { |
michael@0 | 100 | if (mDrawTarget) { |
michael@0 | 101 | MaybeUnlock(); |
michael@0 | 102 | |
michael@0 | 103 | mDrawTarget = nullptr; |
michael@0 | 104 | SkBitmap temp = mBitmap; |
michael@0 | 105 | mBitmap.reset(); |
michael@0 | 106 | temp.copyTo(&mBitmap, temp.colorType()); |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | void |
michael@0 | 111 | SourceSurfaceSkia::MaybeUnlock() |
michael@0 | 112 | { |
michael@0 | 113 | if (mLocked) { |
michael@0 | 114 | mBitmap.unlockPixels(); |
michael@0 | 115 | mLocked = false; |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | } |
michael@0 | 120 | } |