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/. */
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"
18 SourceSurfaceSkia::SourceSurfaceSkia()
19 : mDrawTarget(nullptr), mLocked(false)
23 SourceSurfaceSkia::~SourceSurfaceSkia()
27 mDrawTarget
->SnapshotDestroyed();
28 mDrawTarget
= nullptr;
33 SourceSurfaceSkia::GetSize() const
39 SourceSurfaceSkia::GetFormat() const
45 SourceSurfaceSkia::InitFromCanvas(SkCanvas
* aCanvas
,
46 SurfaceFormat aFormat
,
47 DrawTargetSkia
* aOwner
)
49 SkISize size
= aCanvas
->getDeviceSize();
51 mBitmap
= (SkBitmap
)aCanvas
->getDevice()->accessBitmap(false);
54 mSize
= IntSize(size
.fWidth
, size
.fHeight
);
55 mStride
= mBitmap
.rowBytes();
62 SourceSurfaceSkia::InitFromData(unsigned char* aData
,
65 SurfaceFormat aFormat
)
68 SkAlphaType alphaType
= (aFormat
== SurfaceFormat::B8G8R8X8
) ?
69 kOpaque_SkAlphaType
: kPremul_SkAlphaType
;
71 SkImageInfo info
= SkImageInfo::Make(aSize
.width
,
73 GfxFormatToSkiaColorType(aFormat
),
75 temp
.setInfo(info
, aStride
);
76 temp
.setPixels(aData
);
78 if (!temp
.copyTo(&mBitmap
, GfxFormatToSkiaColorType(aFormat
))) {
82 if (aFormat
== SurfaceFormat::B8G8R8X8
) {
83 mBitmap
.setAlphaType(kIgnore_SkAlphaType
);
88 mStride
= mBitmap
.rowBytes();
93 SourceSurfaceSkia::GetData()
100 unsigned char *pixels
= (unsigned char *)mBitmap
.getPixels();
105 SourceSurfaceSkia::DrawTargetWillChange()
110 mDrawTarget
= nullptr;
111 SkBitmap temp
= mBitmap
;
113 temp
.copyTo(&mBitmap
, temp
.colorType());
118 SourceSurfaceSkia::MaybeUnlock()
121 mBitmap
.unlockPixels();