|
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/. */ |
|
5 |
|
6 |
|
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" |
|
14 |
|
15 namespace mozilla { |
|
16 namespace gfx { |
|
17 |
|
18 SourceSurfaceSkia::SourceSurfaceSkia() |
|
19 : mDrawTarget(nullptr), mLocked(false) |
|
20 { |
|
21 } |
|
22 |
|
23 SourceSurfaceSkia::~SourceSurfaceSkia() |
|
24 { |
|
25 MaybeUnlock(); |
|
26 if (mDrawTarget) { |
|
27 mDrawTarget->SnapshotDestroyed(); |
|
28 mDrawTarget = nullptr; |
|
29 } |
|
30 } |
|
31 |
|
32 IntSize |
|
33 SourceSurfaceSkia::GetSize() const |
|
34 { |
|
35 return mSize; |
|
36 } |
|
37 |
|
38 SurfaceFormat |
|
39 SourceSurfaceSkia::GetFormat() const |
|
40 { |
|
41 return mFormat; |
|
42 } |
|
43 |
|
44 bool |
|
45 SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas, |
|
46 SurfaceFormat aFormat, |
|
47 DrawTargetSkia* aOwner) |
|
48 { |
|
49 SkISize size = aCanvas->getDeviceSize(); |
|
50 |
|
51 mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false); |
|
52 mFormat = aFormat; |
|
53 |
|
54 mSize = IntSize(size.fWidth, size.fHeight); |
|
55 mStride = mBitmap.rowBytes(); |
|
56 mDrawTarget = aOwner; |
|
57 |
|
58 return true; |
|
59 } |
|
60 |
|
61 bool |
|
62 SourceSurfaceSkia::InitFromData(unsigned char* aData, |
|
63 const IntSize &aSize, |
|
64 int32_t aStride, |
|
65 SurfaceFormat aFormat) |
|
66 { |
|
67 SkBitmap temp; |
|
68 temp.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride); |
|
69 temp.setPixels(aData); |
|
70 |
|
71 if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) { |
|
72 return false; |
|
73 } |
|
74 |
|
75 if (aFormat == SurfaceFormat::B8G8R8X8) { |
|
76 mBitmap.setAlphaType(kIgnore_SkAlphaType); |
|
77 } |
|
78 |
|
79 mSize = aSize; |
|
80 mFormat = aFormat; |
|
81 mStride = mBitmap.rowBytes(); |
|
82 return true; |
|
83 } |
|
84 |
|
85 unsigned char* |
|
86 SourceSurfaceSkia::GetData() |
|
87 { |
|
88 if (!mLocked) { |
|
89 mBitmap.lockPixels(); |
|
90 mLocked = true; |
|
91 } |
|
92 |
|
93 unsigned char *pixels = (unsigned char *)mBitmap.getPixels(); |
|
94 return pixels; |
|
95 } |
|
96 |
|
97 void |
|
98 SourceSurfaceSkia::DrawTargetWillChange() |
|
99 { |
|
100 if (mDrawTarget) { |
|
101 MaybeUnlock(); |
|
102 |
|
103 mDrawTarget = nullptr; |
|
104 SkBitmap temp = mBitmap; |
|
105 mBitmap.reset(); |
|
106 temp.copyTo(&mBitmap, temp.colorType()); |
|
107 } |
|
108 } |
|
109 |
|
110 void |
|
111 SourceSurfaceSkia::MaybeUnlock() |
|
112 { |
|
113 if (mLocked) { |
|
114 mBitmap.unlockPixels(); |
|
115 mLocked = false; |
|
116 } |
|
117 } |
|
118 |
|
119 } |
|
120 } |