blob: 539701b49c20b8a8efcb54dd78e1ef72848c823a [file] [log] [blame]
djsollen@google.com5587ac02013-08-29 20:20:40 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/utils/SkCanvasStateUtils.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +00009
Mike Reedac9f0c92020-12-23 10:11:33 -050010#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
12#include "src/core/SkClipOpPriv.h"
13#include "src/core/SkDevice.h"
14#include "src/core/SkRasterClip.h"
15#include "src/core/SkWriter32.h"
16#include "src/utils/SkCanvasStack.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000017
djsollen@google.com5587ac02013-08-29 20:20:40 +000018/*
19 * WARNING: The structs below are part of a stable ABI and as such we explicitly
20 * use unambigious primitives (e.g. int32_t instead of an enum).
21 *
scroggo352c2182014-07-15 12:34:26 -070022 * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN A NEW
23 * NEW SUBCLASS OF SkCanvasState. SUCH CHANGES SHOULD ONLY BE MADE IF ABSOLUTELY
24 * NECESSARY!
scroggo24519372014-07-22 12:38:55 -070025 *
26 * In order to test changes, run the CanvasState tests. gyp/canvas_state_lib.gyp
27 * describes how to create a library to pass to the CanvasState tests. The tests
28 * should succeed when building the library with your changes and passing that to
29 * the tests running in the unchanged Skia.
djsollen@google.com5587ac02013-08-29 20:20:40 +000030 */
31enum RasterConfigs {
32 kUnknown_RasterConfig = 0,
33 kRGB_565_RasterConfig = 1,
34 kARGB_8888_RasterConfig = 2
35};
36typedef int32_t RasterConfig;
37
38enum CanvasBackends {
39 kUnknown_CanvasBackend = 0,
40 kRaster_CanvasBackend = 1,
41 kGPU_CanvasBackend = 2,
42 kPDF_CanvasBackend = 3
43};
44typedef int32_t CanvasBackend;
45
46struct ClipRect {
47 int32_t left, top, right, bottom;
48};
49
50struct SkMCState {
51 float matrix[9];
52 // NOTE: this only works for non-antialiased clips
53 int32_t clipRectCount;
54 ClipRect* clipRects;
55};
56
scroggo352c2182014-07-15 12:34:26 -070057// NOTE: If you add more members, create a new subclass of SkCanvasState with a
58// new CanvasState::version.
djsollen@google.com5587ac02013-08-29 20:20:40 +000059struct SkCanvasLayerState {
60 CanvasBackend type;
61 int32_t x, y;
62 int32_t width;
63 int32_t height;
64
65 SkMCState mcState;
66
67 union {
68 struct {
69 RasterConfig config; // pixel format: a value from RasterConfigs.
scroggo352c2182014-07-15 12:34:26 -070070 uint64_t rowBytes; // Number of bytes from start of one line to next.
djsollen@google.com5587ac02013-08-29 20:20:40 +000071 void* pixels; // The pixels, all (height * rowBytes) of them.
72 } raster;
73 struct {
74 int32_t textureID;
75 } gpu;
76 };
77};
78
79class SkCanvasState {
80public:
scroggo352c2182014-07-15 12:34:26 -070081 SkCanvasState(int32_t version, SkCanvas* canvas) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000082 SkASSERT(canvas);
scroggo352c2182014-07-15 12:34:26 -070083 this->version = version;
84 width = canvas->getBaseLayerSize().width();
85 height = canvas->getBaseLayerSize().height();
djsollen@google.com5587ac02013-08-29 20:20:40 +000086
djsollen@google.com5587ac02013-08-29 20:20:40 +000087 }
88
scroggo352c2182014-07-15 12:34:26 -070089 /**
90 * The version this struct was built with. This field must always appear
91 * first in the struct so that when the versions don't match (and the
92 * remaining contents and size are potentially different) we can still
93 * compare the version numbers.
94 */
95 int32_t version;
96 int32_t width;
97 int32_t height;
98 int32_t alignmentPadding;
99};
100
101class SkCanvasState_v1 : public SkCanvasState {
102public:
103 static const int32_t kVersion = 1;
104
Mike Reed5df49342016-11-12 08:06:55 -0600105 SkCanvasState_v1(SkCanvas* canvas) : INHERITED(kVersion, canvas) {
scroggo352c2182014-07-15 12:34:26 -0700106 layerCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700107 layers = nullptr;
scroggo352c2182014-07-15 12:34:26 -0700108 mcState.clipRectCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700109 mcState.clipRects = nullptr;
Mike Reed5df49342016-11-12 08:06:55 -0600110 originalCanvas = canvas;
scroggo352c2182014-07-15 12:34:26 -0700111 }
112
113 ~SkCanvasState_v1() {
Michael Ludwig30217952020-12-04 12:58:35 -0500114 // loop through the layers and free the data allocated to the clipRects.
115 // See setup_MC_state, clipRects is only allocated when the clip isn't empty; and empty
116 // is implicitly represented as clipRectCount == 0.
djsollen@google.com5587ac02013-08-29 20:20:40 +0000117 for (int i = 0; i < layerCount; ++i) {
Michael Ludwig30217952020-12-04 12:58:35 -0500118 if (layers[i].mcState.clipRectCount > 0) {
119 sk_free(layers[i].mcState.clipRects);
120 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000121 }
122
Michael Ludwig30217952020-12-04 12:58:35 -0500123 if (mcState.clipRectCount > 0) {
124 sk_free(mcState.clipRects);
125 }
126
127 // layers is always allocated, even if it's with sk_malloc(0), so this is safe.
djsollen@google.com5587ac02013-08-29 20:20:40 +0000128 sk_free(layers);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000129 }
130
djsollen@google.com5587ac02013-08-29 20:20:40 +0000131 SkMCState mcState;
132
133 int32_t layerCount;
134 SkCanvasLayerState* layers;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000135private:
136 SkCanvas* originalCanvas;
John Stiles7571f9e2020-09-02 22:42:33 -0400137 using INHERITED = SkCanvasState;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000138};
139
140////////////////////////////////////////////////////////////////////////////////
141
Mike Reedca37f322018-03-08 13:22:16 -0500142static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkIRect& clip) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000143 // initialize the struct
144 state->clipRectCount = 0;
145
146 // capture the matrix
147 for (int i = 0; i < 9; i++) {
148 state->matrix[i] = matrix.get(i);
149 }
150
151 /*
Mike Reedca37f322018-03-08 13:22:16 -0500152 * We only support a single clipRect, so we take the clip's bounds. Clients have long made
153 * this assumption anyway, so this restriction is fine.
djsollen@google.com5587ac02013-08-29 20:20:40 +0000154 */
Mike Reedca37f322018-03-08 13:22:16 -0500155 SkSWriter32<sizeof(ClipRect)> clipWriter;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000156
157 if (!clip.isEmpty()) {
Mike Reedca37f322018-03-08 13:22:16 -0500158 state->clipRectCount = 1;
159 state->clipRects = (ClipRect*)sk_malloc_throw(sizeof(ClipRect));
160 state->clipRects->left = clip.fLeft;
161 state->clipRects->top = clip.fTop;
162 state->clipRects->right = clip.fRight;
163 state->clipRects->bottom = clip.fBottom;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000164 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000165}
166
djsollen@google.com5587ac02013-08-29 20:20:40 +0000167SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) {
168 SkASSERT(canvas);
169
170 // Check the clip can be decomposed into rectangles (i.e. no soft clips).
Mike Reeda1361362017-03-07 09:37:29 -0500171 if (canvas->androidFramework_isClipAA()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700172 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000173 }
174
Ben Wagner145dbcd2016-11-03 14:40:50 -0400175 std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000176
Mike Reedca37f322018-03-08 13:22:16 -0500177 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getDeviceClipBounds());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000178
Michael Ludwigf326e4f2020-12-04 13:00:43 -0500179 // Historically, the canvas state could report multiple top-level layers because SkCanvas
Mike Reeded4108e2020-12-17 10:32:24 -0500180 // supported unclipped layers. With that feature removed, all required information is contained
181 // by the canvas' top-most device.
182 SkBaseDevice* device = canvas->topDevice();
183 SkASSERT(device);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000184
Michael Ludwigf326e4f2020-12-04 13:00:43 -0500185 SkSWriter32<sizeof(SkCanvasLayerState)> layerWriter;
186 // we currently only work for bitmap backed devices
187 SkPixmap pmap;
188 if (!device->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) {
189 return nullptr;
190 }
191 // and for axis-aligned devices (so not transformed for an image filter)
192 if (!device->isPixelAlignedToGlobal()) {
193 return nullptr;
194 }
195
196 SkIPoint origin = device->getOrigin(); // safe since it's pixel aligned
197
198 SkCanvasLayerState* layerState =
199 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState));
200 layerState->type = kRaster_CanvasBackend;
201 layerState->x = origin.x();
202 layerState->y = origin.y();
203 layerState->width = pmap.width();
204 layerState->height = pmap.height();
205
206 switch (pmap.colorType()) {
207 case kN32_SkColorType:
208 layerState->raster.config = kARGB_8888_RasterConfig;
209 break;
210 case kRGB_565_SkColorType:
211 layerState->raster.config = kRGB_565_RasterConfig;
212 break;
213 default:
214 return nullptr;
215 }
216 layerState->raster.rowBytes = pmap.rowBytes();
217 layerState->raster.pixels = pmap.writable_addr();
218
219 setup_MC_state(&layerState->mcState, device->localToDevice(), device->devClipBounds());
220
djsollen@google.com5587ac02013-08-29 20:20:40 +0000221 // allocate memory for the layers and then and copy them to the struct
Michael Ludwigf326e4f2020-12-04 13:00:43 -0500222 SkASSERT(layerWriter.bytesWritten() == sizeof(SkCanvasLayerState));
223 canvasState->layerCount = 1;
reed@google.com44699382013-10-31 17:28:30 +0000224 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000225 layerWriter.flatten(canvasState->layers);
226
mtklein18300a32016-03-16 13:53:35 -0700227 return canvasState.release();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000228}
229
230////////////////////////////////////////////////////////////////////////////////
231
232static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) {
233 // reconstruct the matrix
234 SkMatrix matrix;
235 for (int i = 0; i < 9; i++) {
236 matrix.set(i, state.matrix[i]);
237 }
238
Mike Reedca37f322018-03-08 13:22:16 -0500239 // only realy support 1 rect, so if the caller (legacy?) sent us more, we just take the bounds
240 // of what they sent.
241 SkIRect bounds = SkIRect::MakeEmpty();
242 if (state.clipRectCount > 0) {
Mike Reed92b33352019-08-24 19:39:13 -0400243 bounds.setLTRB(state.clipRects[0].left,
244 state.clipRects[0].top,
245 state.clipRects[0].right,
246 state.clipRects[0].bottom);
Mike Reedca37f322018-03-08 13:22:16 -0500247 for (int i = 1; i < state.clipRectCount; ++i) {
Mike Reed92b33352019-08-24 19:39:13 -0400248 bounds.join({state.clipRects[i].left,
249 state.clipRects[i].top,
250 state.clipRects[i].right,
251 state.clipRects[i].bottom});
Mike Reedca37f322018-03-08 13:22:16 -0500252 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000253 }
254
Mike Reedca37f322018-03-08 13:22:16 -0500255 canvas->clipRect(SkRect::Make(bounds));
256 canvas->concat(matrix);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000257}
258
Mike Reed5df49342016-11-12 08:06:55 -0600259static std::unique_ptr<SkCanvas>
260make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000261 SkASSERT(kRaster_CanvasBackend == layerState.type);
262
263 SkBitmap bitmap;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000264 SkColorType colorType =
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000265 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType :
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000266 layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType :
267 kUnknown_SkColorType;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000268
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000269 if (colorType == kUnknown_SkColorType) {
halcanary96fcdcc2015-08-27 07:41:13 -0700270 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000271 }
272
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000273 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
274 colorType, kPremul_SkAlphaType),
scroggo352c2182014-07-15 12:34:26 -0700275 layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000276
277 SkASSERT(!bitmap.empty());
278 SkASSERT(!bitmap.isNull());
279
Mike Reed5df49342016-11-12 08:06:55 -0600280 std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000281
282 // setup the matrix and clip
283 setup_canvas_from_MC_state(layerState.mcState, canvas.get());
284
Mike Reed5df49342016-11-12 08:06:55 -0600285 return canvas;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000286}
287
Mike Reed5df49342016-11-12 08:06:55 -0600288std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000289 SkASSERT(state);
scroggo352c2182014-07-15 12:34:26 -0700290 // Currently there is only one possible version.
291 SkASSERT(SkCanvasState_v1::kVersion == state->version);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000292
scroggo352c2182014-07-15 12:34:26 -0700293 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000294
scroggo352c2182014-07-15 12:34:26 -0700295 if (state_v1->layerCount < 1) {
halcanary96fcdcc2015-08-27 07:41:13 -0700296 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000297 }
298
Mike Reed5df49342016-11-12 08:06:55 -0600299 std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000300
301 // setup the matrix and clip on the n-way canvas
Hal Canary67b39de2016-11-07 11:47:44 -0500302 setup_canvas_from_MC_state(state_v1->mcState, canvas.get());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000303
Michael Ludwigf326e4f2020-12-04 13:00:43 -0500304 // Iterate over the layers and add them to the n-way canvas. New clients will only send one
305 // layer since unclipped layers are no longer supported, but old canvas clients may still
306 // create them.
scroggo352c2182014-07-15 12:34:26 -0700307 for (int i = state_v1->layerCount - 1; i >= 0; --i) {
Mike Reed5df49342016-11-12 08:06:55 -0600308 std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]);
John Stilesa008b0f2020-08-16 08:48:02 -0400309 if (!canvasLayer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700310 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000311 }
Mike Reed584ca892016-11-15 11:52:55 -0500312 canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x,
313 state_v1->layers[i].y));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000314 }
315
Mike Kleina9609ea2020-02-18 13:33:23 -0600316 return std::move(canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000317}
318
319////////////////////////////////////////////////////////////////////////////////
320
321void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) {
scroggo352c2182014-07-15 12:34:26 -0700322 SkASSERT(!state || SkCanvasState_v1::kVersion == state->version);
323 // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
324 // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
325 // instead uses the field "version" to determine how to behave.
halcanary385fe4d2015-08-26 13:07:48 -0700326 delete static_cast<SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000327}