1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/utils/SkCanvasStateUtils.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkSize.h"
20 #include "include/private/base/SkMalloc.h"
21 #include "src/core/SkDevice.h"
22 #include "src/core/SkWriter32.h"
23 #include "src/utils/SkCanvasStack.h"
24
25 #include <cstddef>
26 #include <cstdint>
27 #include <utility>
28
29 /*
30 * WARNING: The structs below are part of a stable ABI and as such we explicitly
31 * use unambigious primitives (e.g. int32_t instead of an enum).
32 *
33 * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN A NEW
34 * NEW SUBCLASS OF SkCanvasState. SUCH CHANGES SHOULD ONLY BE MADE IF ABSOLUTELY
35 * NECESSARY!
36 *
37 * In order to test changes, run the CanvasState tests. gyp/canvas_state_lib.gyp
38 * describes how to create a library to pass to the CanvasState tests. The tests
39 * should succeed when building the library with your changes and passing that to
40 * the tests running in the unchanged Skia.
41 */
42 enum RasterConfigs {
43 kUnknown_RasterConfig = 0,
44 kRGB_565_RasterConfig = 1,
45 kARGB_8888_RasterConfig = 2
46 };
47 typedef int32_t RasterConfig;
48
49 enum CanvasBackends {
50 kUnknown_CanvasBackend = 0,
51 kRaster_CanvasBackend = 1,
52 kGPU_CanvasBackend = 2,
53 kPDF_CanvasBackend = 3
54 };
55 typedef int32_t CanvasBackend;
56
57 struct ClipRect {
58 int32_t left, top, right, bottom;
59 };
60
61 struct SkMCState {
62 float matrix[9];
63 // NOTE: this only works for non-antialiased clips
64 int32_t clipRectCount;
65 ClipRect* clipRects;
66 };
67
68 // NOTE: If you add more members, create a new subclass of SkCanvasState with a
69 // new CanvasState::version.
70 struct SkCanvasLayerState {
71 CanvasBackend type;
72 int32_t x, y;
73 int32_t width;
74 int32_t height;
75
76 SkMCState mcState;
77
78 union {
79 struct {
80 RasterConfig config; // pixel format: a value from RasterConfigs.
81 uint64_t rowBytes; // Number of bytes from start of one line to next.
82 void* pixels; // The pixels, all (height * rowBytes) of them.
83 } raster;
84 struct {
85 int32_t textureID;
86 } gpu;
87 };
88 };
89
90 class SkCanvasState {
91 public:
SkCanvasState(int32_t version,SkCanvas * canvas)92 SkCanvasState(int32_t version, SkCanvas* canvas) {
93 SkASSERT(canvas);
94 this->version = version;
95 width = canvas->getBaseLayerSize().width();
96 height = canvas->getBaseLayerSize().height();
97
98 }
99
100 /**
101 * The version this struct was built with. This field must always appear
102 * first in the struct so that when the versions don't match (and the
103 * remaining contents and size are potentially different) we can still
104 * compare the version numbers.
105 */
106 int32_t version;
107 int32_t width;
108 int32_t height;
109 int32_t alignmentPadding;
110 };
111
112 class SkCanvasState_v1 : public SkCanvasState {
113 public:
114 static const int32_t kVersion = 1;
115
SkCanvasState_v1(SkCanvas * canvas)116 SkCanvasState_v1(SkCanvas* canvas) : INHERITED(kVersion, canvas) {
117 layerCount = 0;
118 layers = nullptr;
119 mcState.clipRectCount = 0;
120 mcState.clipRects = nullptr;
121 originalCanvas = canvas;
122 }
123
~SkCanvasState_v1()124 ~SkCanvasState_v1() {
125 // loop through the layers and free the data allocated to the clipRects.
126 // See setup_MC_state, clipRects is only allocated when the clip isn't empty; and empty
127 // is implicitly represented as clipRectCount == 0.
128 for (int i = 0; i < layerCount; ++i) {
129 if (layers[i].mcState.clipRectCount > 0) {
130 sk_free(layers[i].mcState.clipRects);
131 }
132 }
133
134 if (mcState.clipRectCount > 0) {
135 sk_free(mcState.clipRects);
136 }
137
138 // layers is always allocated, even if it's with sk_malloc(0), so this is safe.
139 sk_free(layers);
140 }
141
142 SkMCState mcState;
143
144 int32_t layerCount;
145 SkCanvasLayerState* layers;
146 private:
147 SkCanvas* originalCanvas;
148 using INHERITED = SkCanvasState;
149 };
150
151 ////////////////////////////////////////////////////////////////////////////////
152
setup_MC_state(SkMCState * state,const SkMatrix & matrix,const SkIRect & clip)153 static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkIRect& clip) {
154 // initialize the struct
155 state->clipRectCount = 0;
156
157 // capture the matrix
158 for (int i = 0; i < 9; i++) {
159 state->matrix[i] = matrix.get(i);
160 }
161
162 /*
163 * We only support a single clipRect, so we take the clip's bounds. Clients have long made
164 * this assumption anyway, so this restriction is fine.
165 */
166 SkSWriter32<sizeof(ClipRect)> clipWriter;
167
168 if (!clip.isEmpty()) {
169 state->clipRectCount = 1;
170 state->clipRects = (ClipRect*)sk_malloc_throw(sizeof(ClipRect));
171 state->clipRects->left = clip.fLeft;
172 state->clipRects->top = clip.fTop;
173 state->clipRects->right = clip.fRight;
174 state->clipRects->bottom = clip.fBottom;
175 }
176 }
177
CaptureCanvasState(SkCanvas * canvas)178 SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) {
179 SkASSERT(canvas);
180
181 // Check the clip can be decomposed into rectangles (i.e. no soft clips).
182 if (canvas->androidFramework_isClipAA()) {
183 return nullptr;
184 }
185
186 std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas));
187
188 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getDeviceClipBounds());
189
190 // Historically, the canvas state could report multiple top-level layers because SkCanvas
191 // supported unclipped layers. With that feature removed, all required information is contained
192 // by the canvas' top-most device.
193 SkDevice* device = canvas->topDevice();
194 SkASSERT(device);
195
196 SkSWriter32<sizeof(SkCanvasLayerState)> layerWriter;
197 // we currently only work for bitmap backed devices
198 SkPixmap pmap;
199 if (!device->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) {
200 return nullptr;
201 }
202 // and for axis-aligned devices (so not transformed for an image filter)
203 if (!device->isPixelAlignedToGlobal()) {
204 return nullptr;
205 }
206
207 SkIPoint origin = device->getOrigin(); // safe since it's pixel aligned
208
209 SkCanvasLayerState* layerState =
210 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState));
211 layerState->type = kRaster_CanvasBackend;
212 layerState->x = origin.x();
213 layerState->y = origin.y();
214 layerState->width = pmap.width();
215 layerState->height = pmap.height();
216
217 switch (pmap.colorType()) {
218 case kN32_SkColorType:
219 layerState->raster.config = kARGB_8888_RasterConfig;
220 break;
221 case kRGB_565_SkColorType:
222 layerState->raster.config = kRGB_565_RasterConfig;
223 break;
224 default:
225 return nullptr;
226 }
227 layerState->raster.rowBytes = pmap.rowBytes();
228 layerState->raster.pixels = pmap.writable_addr();
229
230 setup_MC_state(&layerState->mcState, device->localToDevice(), device->devClipBounds());
231
232 // allocate memory for the layers and then and copy them to the struct
233 SkASSERT(layerWriter.bytesWritten() == sizeof(SkCanvasLayerState));
234 canvasState->layerCount = 1;
235 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten());
236 layerWriter.flatten(canvasState->layers);
237
238 return canvasState.release();
239 }
240
241 ////////////////////////////////////////////////////////////////////////////////
242
setup_canvas_from_MC_state(const SkMCState & state,SkCanvas * canvas)243 static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) {
244 // reconstruct the matrix
245 SkMatrix matrix;
246 for (int i = 0; i < 9; i++) {
247 matrix.set(i, state.matrix[i]);
248 }
249
250 // only realy support 1 rect, so if the caller (legacy?) sent us more, we just take the bounds
251 // of what they sent.
252 SkIRect bounds = SkIRect::MakeEmpty();
253 if (state.clipRectCount > 0) {
254 bounds.setLTRB(state.clipRects[0].left,
255 state.clipRects[0].top,
256 state.clipRects[0].right,
257 state.clipRects[0].bottom);
258 for (int i = 1; i < state.clipRectCount; ++i) {
259 bounds.join({state.clipRects[i].left,
260 state.clipRects[i].top,
261 state.clipRects[i].right,
262 state.clipRects[i].bottom});
263 }
264 }
265
266 canvas->clipRect(SkRect::Make(bounds));
267 canvas->concat(matrix);
268 }
269
270 static std::unique_ptr<SkCanvas>
make_canvas_from_canvas_layer(const SkCanvasLayerState & layerState)271 make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) {
272 SkASSERT(kRaster_CanvasBackend == layerState.type);
273
274 SkBitmap bitmap;
275 SkColorType colorType =
276 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType :
277 layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType :
278 kUnknown_SkColorType;
279
280 if (colorType == kUnknown_SkColorType) {
281 return nullptr;
282 }
283
284 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
285 colorType, kPremul_SkAlphaType),
286 layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
287
288 SkASSERT(!bitmap.empty());
289 SkASSERT(!bitmap.isNull());
290
291 std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap));
292
293 // setup the matrix and clip
294 setup_canvas_from_MC_state(layerState.mcState, canvas.get());
295
296 return canvas;
297 }
298
MakeFromCanvasState(const SkCanvasState * state)299 std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) {
300 SkASSERT(state);
301 // Currently there is only one possible version.
302 SkASSERT(SkCanvasState_v1::kVersion == state->version);
303
304 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
305
306 if (state_v1->layerCount < 1) {
307 return nullptr;
308 }
309
310 std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height));
311
312 // setup the matrix and clip on the n-way canvas
313 setup_canvas_from_MC_state(state_v1->mcState, canvas.get());
314
315 // Iterate over the layers and add them to the n-way canvas. New clients will only send one
316 // layer since unclipped layers are no longer supported, but old canvas clients may still
317 // create them.
318 for (int i = state_v1->layerCount - 1; i >= 0; --i) {
319 std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]);
320 if (!canvasLayer) {
321 return nullptr;
322 }
323 canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x,
324 state_v1->layers[i].y));
325 }
326
327 return canvas;
328 }
329
330 ////////////////////////////////////////////////////////////////////////////////
331
ReleaseCanvasState(SkCanvasState * state)332 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) {
333 SkASSERT(!state || SkCanvasState_v1::kVersion == state->version);
334 // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
335 // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
336 // instead uses the field "version" to determine how to behave.
337 delete static_cast<SkCanvasState_v1*>(state);
338 }
339