1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/2d/SourceSurfaceSkia.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +#include "Logging.h" 1.11 +#include "SourceSurfaceSkia.h" 1.12 +#include "skia/SkBitmap.h" 1.13 +#include "skia/SkDevice.h" 1.14 +#include "HelpersSkia.h" 1.15 +#include "DrawTargetSkia.h" 1.16 +#include "DataSurfaceHelpers.h" 1.17 + 1.18 +namespace mozilla { 1.19 +namespace gfx { 1.20 + 1.21 +SourceSurfaceSkia::SourceSurfaceSkia() 1.22 + : mDrawTarget(nullptr), mLocked(false) 1.23 +{ 1.24 +} 1.25 + 1.26 +SourceSurfaceSkia::~SourceSurfaceSkia() 1.27 +{ 1.28 + MaybeUnlock(); 1.29 + if (mDrawTarget) { 1.30 + mDrawTarget->SnapshotDestroyed(); 1.31 + mDrawTarget = nullptr; 1.32 + } 1.33 +} 1.34 + 1.35 +IntSize 1.36 +SourceSurfaceSkia::GetSize() const 1.37 +{ 1.38 + return mSize; 1.39 +} 1.40 + 1.41 +SurfaceFormat 1.42 +SourceSurfaceSkia::GetFormat() const 1.43 +{ 1.44 + return mFormat; 1.45 +} 1.46 + 1.47 +bool 1.48 +SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas, 1.49 + SurfaceFormat aFormat, 1.50 + DrawTargetSkia* aOwner) 1.51 +{ 1.52 + SkISize size = aCanvas->getDeviceSize(); 1.53 + 1.54 + mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false); 1.55 + mFormat = aFormat; 1.56 + 1.57 + mSize = IntSize(size.fWidth, size.fHeight); 1.58 + mStride = mBitmap.rowBytes(); 1.59 + mDrawTarget = aOwner; 1.60 + 1.61 + return true; 1.62 +} 1.63 + 1.64 +bool 1.65 +SourceSurfaceSkia::InitFromData(unsigned char* aData, 1.66 + const IntSize &aSize, 1.67 + int32_t aStride, 1.68 + SurfaceFormat aFormat) 1.69 +{ 1.70 + SkBitmap temp; 1.71 + temp.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride); 1.72 + temp.setPixels(aData); 1.73 + 1.74 + if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) { 1.75 + return false; 1.76 + } 1.77 + 1.78 + if (aFormat == SurfaceFormat::B8G8R8X8) { 1.79 + mBitmap.setAlphaType(kIgnore_SkAlphaType); 1.80 + } 1.81 + 1.82 + mSize = aSize; 1.83 + mFormat = aFormat; 1.84 + mStride = mBitmap.rowBytes(); 1.85 + return true; 1.86 +} 1.87 + 1.88 +unsigned char* 1.89 +SourceSurfaceSkia::GetData() 1.90 +{ 1.91 + if (!mLocked) { 1.92 + mBitmap.lockPixels(); 1.93 + mLocked = true; 1.94 + } 1.95 + 1.96 + unsigned char *pixels = (unsigned char *)mBitmap.getPixels(); 1.97 + return pixels; 1.98 +} 1.99 + 1.100 +void 1.101 +SourceSurfaceSkia::DrawTargetWillChange() 1.102 +{ 1.103 + if (mDrawTarget) { 1.104 + MaybeUnlock(); 1.105 + 1.106 + mDrawTarget = nullptr; 1.107 + SkBitmap temp = mBitmap; 1.108 + mBitmap.reset(); 1.109 + temp.copyTo(&mBitmap, temp.colorType()); 1.110 + } 1.111 +} 1.112 + 1.113 +void 1.114 +SourceSurfaceSkia::MaybeUnlock() 1.115 +{ 1.116 + if (mLocked) { 1.117 + mBitmap.unlockPixels(); 1.118 + mLocked = false; 1.119 + } 1.120 +} 1.121 + 1.122 +} 1.123 +}