Bumping manifests a=b2g-bump
[gecko.git] / gfx / 2d / SourceSurfaceSkia.cpp
blob57ab6d11dc90c85498334dc0786a6508edfe3902
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "Logging.h"
8 #include "SourceSurfaceSkia.h"
9 #include "skia/SkBitmap.h"
10 #include "skia/SkDevice.h"
11 #include "HelpersSkia.h"
12 #include "DrawTargetSkia.h"
13 #include "DataSurfaceHelpers.h"
15 namespace mozilla {
16 namespace gfx {
18 SourceSurfaceSkia::SourceSurfaceSkia()
19 : mDrawTarget(nullptr), mLocked(false)
23 SourceSurfaceSkia::~SourceSurfaceSkia()
25 MaybeUnlock();
26 if (mDrawTarget) {
27 mDrawTarget->SnapshotDestroyed();
28 mDrawTarget = nullptr;
32 IntSize
33 SourceSurfaceSkia::GetSize() const
35 return mSize;
38 SurfaceFormat
39 SourceSurfaceSkia::GetFormat() const
41 return mFormat;
44 bool
45 SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas,
46 SurfaceFormat aFormat,
47 DrawTargetSkia* aOwner)
49 SkISize size = aCanvas->getDeviceSize();
51 mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false);
52 mFormat = aFormat;
54 mSize = IntSize(size.fWidth, size.fHeight);
55 mStride = mBitmap.rowBytes();
56 mDrawTarget = aOwner;
58 return true;
61 bool
62 SourceSurfaceSkia::InitFromData(unsigned char* aData,
63 const IntSize &aSize,
64 int32_t aStride,
65 SurfaceFormat aFormat)
67 SkBitmap temp;
68 SkAlphaType alphaType = (aFormat == SurfaceFormat::B8G8R8X8) ?
69 kOpaque_SkAlphaType : kPremul_SkAlphaType;
71 SkImageInfo info = SkImageInfo::Make(aSize.width,
72 aSize.height,
73 GfxFormatToSkiaColorType(aFormat),
74 alphaType);
75 temp.setInfo(info, aStride);
76 temp.setPixels(aData);
78 if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) {
79 return false;
82 if (aFormat == SurfaceFormat::B8G8R8X8) {
83 mBitmap.setAlphaType(kIgnore_SkAlphaType);
86 mSize = aSize;
87 mFormat = aFormat;
88 mStride = mBitmap.rowBytes();
89 return true;
92 unsigned char*
93 SourceSurfaceSkia::GetData()
95 if (!mLocked) {
96 mBitmap.lockPixels();
97 mLocked = true;
100 unsigned char *pixels = (unsigned char *)mBitmap.getPixels();
101 return pixels;
104 void
105 SourceSurfaceSkia::DrawTargetWillChange()
107 if (mDrawTarget) {
108 MaybeUnlock();
110 mDrawTarget = nullptr;
111 SkBitmap temp = mBitmap;
112 mBitmap.reset();
113 temp.copyTo(&mBitmap, temp.colorType());
117 void
118 SourceSurfaceSkia::MaybeUnlock()
120 if (mLocked) {
121 mBitmap.unlockPixels();
122 mLocked = false;