michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #include "Logging.h" michael@0: #include "SourceSurfaceSkia.h" michael@0: #include "skia/SkBitmap.h" michael@0: #include "skia/SkDevice.h" michael@0: #include "HelpersSkia.h" michael@0: #include "DrawTargetSkia.h" michael@0: #include "DataSurfaceHelpers.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: SourceSurfaceSkia::SourceSurfaceSkia() michael@0: : mDrawTarget(nullptr), mLocked(false) michael@0: { michael@0: } michael@0: michael@0: SourceSurfaceSkia::~SourceSurfaceSkia() michael@0: { michael@0: MaybeUnlock(); michael@0: if (mDrawTarget) { michael@0: mDrawTarget->SnapshotDestroyed(); michael@0: mDrawTarget = nullptr; michael@0: } michael@0: } michael@0: michael@0: IntSize michael@0: SourceSurfaceSkia::GetSize() const michael@0: { michael@0: return mSize; michael@0: } michael@0: michael@0: SurfaceFormat michael@0: SourceSurfaceSkia::GetFormat() const michael@0: { michael@0: return mFormat; michael@0: } michael@0: michael@0: bool michael@0: SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas, michael@0: SurfaceFormat aFormat, michael@0: DrawTargetSkia* aOwner) michael@0: { michael@0: SkISize size = aCanvas->getDeviceSize(); michael@0: michael@0: mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false); michael@0: mFormat = aFormat; michael@0: michael@0: mSize = IntSize(size.fWidth, size.fHeight); michael@0: mStride = mBitmap.rowBytes(); michael@0: mDrawTarget = aOwner; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: SourceSurfaceSkia::InitFromData(unsigned char* aData, michael@0: const IntSize &aSize, michael@0: int32_t aStride, michael@0: SurfaceFormat aFormat) michael@0: { michael@0: SkBitmap temp; michael@0: temp.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride); michael@0: temp.setPixels(aData); michael@0: michael@0: if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) { michael@0: return false; michael@0: } michael@0: michael@0: if (aFormat == SurfaceFormat::B8G8R8X8) { michael@0: mBitmap.setAlphaType(kIgnore_SkAlphaType); michael@0: } michael@0: michael@0: mSize = aSize; michael@0: mFormat = aFormat; michael@0: mStride = mBitmap.rowBytes(); michael@0: return true; michael@0: } michael@0: michael@0: unsigned char* michael@0: SourceSurfaceSkia::GetData() michael@0: { michael@0: if (!mLocked) { michael@0: mBitmap.lockPixels(); michael@0: mLocked = true; michael@0: } michael@0: michael@0: unsigned char *pixels = (unsigned char *)mBitmap.getPixels(); michael@0: return pixels; michael@0: } michael@0: michael@0: void michael@0: SourceSurfaceSkia::DrawTargetWillChange() michael@0: { michael@0: if (mDrawTarget) { michael@0: MaybeUnlock(); michael@0: michael@0: mDrawTarget = nullptr; michael@0: SkBitmap temp = mBitmap; michael@0: mBitmap.reset(); michael@0: temp.copyTo(&mBitmap, temp.colorType()); michael@0: } michael@0: } michael@0: michael@0: void michael@0: SourceSurfaceSkia::MaybeUnlock() michael@0: { michael@0: if (mLocked) { michael@0: mBitmap.unlockPixels(); michael@0: mLocked = false; michael@0: } michael@0: } michael@0: michael@0: } michael@0: }