blob: 8f208d30d09eb917fabe5f56e577a11784db0e79 [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 Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "src/core/SkClipOpPriv.h"
12#include "src/core/SkDevice.h"
13#include "src/core/SkRasterClip.h"
14#include "src/core/SkWriter32.h"
15#include "src/utils/SkCanvasStack.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000016
djsollen@google.com5587ac02013-08-29 20:20:40 +000017/*
18 * WARNING: The structs below are part of a stable ABI and as such we explicitly
19 * use unambigious primitives (e.g. int32_t instead of an enum).
20 *
scroggo352c2182014-07-15 12:34:26 -070021 * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN A NEW
22 * NEW SUBCLASS OF SkCanvasState. SUCH CHANGES SHOULD ONLY BE MADE IF ABSOLUTELY
23 * NECESSARY!
scroggo24519372014-07-22 12:38:55 -070024 *
25 * In order to test changes, run the CanvasState tests. gyp/canvas_state_lib.gyp
26 * describes how to create a library to pass to the CanvasState tests. The tests
27 * should succeed when building the library with your changes and passing that to
28 * the tests running in the unchanged Skia.
djsollen@google.com5587ac02013-08-29 20:20:40 +000029 */
30enum RasterConfigs {
31 kUnknown_RasterConfig = 0,
32 kRGB_565_RasterConfig = 1,
33 kARGB_8888_RasterConfig = 2
34};
35typedef int32_t RasterConfig;
36
37enum CanvasBackends {
38 kUnknown_CanvasBackend = 0,
39 kRaster_CanvasBackend = 1,
40 kGPU_CanvasBackend = 2,
41 kPDF_CanvasBackend = 3
42};
43typedef int32_t CanvasBackend;
44
45struct ClipRect {
46 int32_t left, top, right, bottom;
47};
48
49struct SkMCState {
50 float matrix[9];
51 // NOTE: this only works for non-antialiased clips
52 int32_t clipRectCount;
53 ClipRect* clipRects;
54};
55
scroggo352c2182014-07-15 12:34:26 -070056// NOTE: If you add more members, create a new subclass of SkCanvasState with a
57// new CanvasState::version.
djsollen@google.com5587ac02013-08-29 20:20:40 +000058struct SkCanvasLayerState {
59 CanvasBackend type;
60 int32_t x, y;
61 int32_t width;
62 int32_t height;
63
64 SkMCState mcState;
65
66 union {
67 struct {
68 RasterConfig config; // pixel format: a value from RasterConfigs.
scroggo352c2182014-07-15 12:34:26 -070069 uint64_t rowBytes; // Number of bytes from start of one line to next.
djsollen@google.com5587ac02013-08-29 20:20:40 +000070 void* pixels; // The pixels, all (height * rowBytes) of them.
71 } raster;
72 struct {
73 int32_t textureID;
74 } gpu;
75 };
76};
77
78class SkCanvasState {
79public:
scroggo352c2182014-07-15 12:34:26 -070080 SkCanvasState(int32_t version, SkCanvas* canvas) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000081 SkASSERT(canvas);
scroggo352c2182014-07-15 12:34:26 -070082 this->version = version;
83 width = canvas->getBaseLayerSize().width();
84 height = canvas->getBaseLayerSize().height();
djsollen@google.com5587ac02013-08-29 20:20:40 +000085
djsollen@google.com5587ac02013-08-29 20:20:40 +000086 }
87
scroggo352c2182014-07-15 12:34:26 -070088 /**
89 * The version this struct was built with. This field must always appear
90 * first in the struct so that when the versions don't match (and the
91 * remaining contents and size are potentially different) we can still
92 * compare the version numbers.
93 */
94 int32_t version;
95 int32_t width;
96 int32_t height;
97 int32_t alignmentPadding;
98};
99
100class SkCanvasState_v1 : public SkCanvasState {
101public:
102 static const int32_t kVersion = 1;
103
Mike Reed5df49342016-11-12 08:06:55 -0600104 SkCanvasState_v1(SkCanvas* canvas) : INHERITED(kVersion, canvas) {
scroggo352c2182014-07-15 12:34:26 -0700105 layerCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700106 layers = nullptr;
scroggo352c2182014-07-15 12:34:26 -0700107 mcState.clipRectCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700108 mcState.clipRects = nullptr;
Mike Reed5df49342016-11-12 08:06:55 -0600109 originalCanvas = canvas;
scroggo352c2182014-07-15 12:34:26 -0700110 }
111
112 ~SkCanvasState_v1() {
Michael Ludwig30217952020-12-04 12:58:35 -0500113 // loop through the layers and free the data allocated to the clipRects.
114 // See setup_MC_state, clipRects is only allocated when the clip isn't empty; and empty
115 // is implicitly represented as clipRectCount == 0.
djsollen@google.com5587ac02013-08-29 20:20:40 +0000116 for (int i = 0; i < layerCount; ++i) {
Michael Ludwig30217952020-12-04 12:58:35 -0500117 if (layers[i].mcState.clipRectCount > 0) {
118 sk_free(layers[i].mcState.clipRects);
119 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000120 }
121
Michael Ludwig30217952020-12-04 12:58:35 -0500122 if (mcState.clipRectCount > 0) {
123 sk_free(mcState.clipRects);
124 }
125
126 // layers is always allocated, even if it's with sk_malloc(0), so this is safe.
djsollen@google.com5587ac02013-08-29 20:20:40 +0000127 sk_free(layers);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000128 }
129
djsollen@google.com5587ac02013-08-29 20:20:40 +0000130 SkMCState mcState;
131
132 int32_t layerCount;
133 SkCanvasLayerState* layers;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000134private:
135 SkCanvas* originalCanvas;
John Stiles7571f9e2020-09-02 22:42:33 -0400136 using INHERITED = SkCanvasState;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000137};
138
139////////////////////////////////////////////////////////////////////////////////
140
Mike Reedca37f322018-03-08 13:22:16 -0500141static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkIRect& clip) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000142 // initialize the struct
143 state->clipRectCount = 0;
144
145 // capture the matrix
146 for (int i = 0; i < 9; i++) {
147 state->matrix[i] = matrix.get(i);
148 }
149
150 /*
Mike Reedca37f322018-03-08 13:22:16 -0500151 * We only support a single clipRect, so we take the clip's bounds. Clients have long made
152 * this assumption anyway, so this restriction is fine.
djsollen@google.com5587ac02013-08-29 20:20:40 +0000153 */
Mike Reedca37f322018-03-08 13:22:16 -0500154 SkSWriter32<sizeof(ClipRect)> clipWriter;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000155
156 if (!clip.isEmpty()) {
Mike Reedca37f322018-03-08 13:22:16 -0500157 state->clipRectCount = 1;
158 state->clipRects = (ClipRect*)sk_malloc_throw(sizeof(ClipRect));
159 state->clipRects->left = clip.fLeft;
160 state->clipRects->top = clip.fTop;
161 state->clipRects->right = clip.fRight;
162 state->clipRects->bottom = clip.fBottom;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000163 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000164}
165
djsollen@google.com5587ac02013-08-29 20:20:40 +0000166SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) {
167 SkASSERT(canvas);
168
169 // Check the clip can be decomposed into rectangles (i.e. no soft clips).
Mike Reeda1361362017-03-07 09:37:29 -0500170 if (canvas->androidFramework_isClipAA()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700171 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000172 }
173
Ben Wagner145dbcd2016-11-03 14:40:50 -0400174 std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000175
Mike Reedca37f322018-03-08 13:22:16 -0500176 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getDeviceClipBounds());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000177
Michael Ludwigf326e4f2020-12-04 13:00:43 -0500178 // Historically, the canvas state could report multiple top-level layers because SkCanvas
179 // supported unclipped layers. With that feature removed, there is only ever 0 to 1 "layers",
180 // and all required information is contained by the canvas' top-most device.
181 SkBaseDevice* device = canvas->getTopDevice();
182 if (!device) {
183 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000184 }
185
Michael Ludwigf326e4f2020-12-04 13:00:43 -0500186 SkSWriter32<sizeof(SkCanvasLayerState)> layerWriter;
187 // we currently only work for bitmap backed devices
188 SkPixmap pmap;
189 if (!device->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) {
190 return nullptr;
191 }
192 // and for axis-aligned devices (so not transformed for an image filter)
193 if (!device->isPixelAlignedToGlobal()) {
194 return nullptr;
195 }
196
197 SkIPoint origin = device->getOrigin(); // safe since it's pixel aligned
198
199 SkCanvasLayerState* layerState =
200 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState));
201 layerState->type = kRaster_CanvasBackend;
202 layerState->x = origin.x();
203 layerState->y = origin.y();
204 layerState->width = pmap.width();
205 layerState->height = pmap.height();
206
207 switch (pmap.colorType()) {
208 case kN32_SkColorType:
209 layerState->raster.config = kARGB_8888_RasterConfig;
210 break;
211 case kRGB_565_SkColorType:
212 layerState->raster.config = kRGB_565_RasterConfig;
213 break;
214 default:
215 return nullptr;
216 }
217 layerState->raster.rowBytes = pmap.rowBytes();
218 layerState->raster.pixels = pmap.writable_addr();
219
220 setup_MC_state(&layerState->mcState, device->localToDevice(), device->devClipBounds());
221
djsollen@google.com5587ac02013-08-29 20:20:40 +0000222 // allocate memory for the layers and then and copy them to the struct
Michael Ludwigf326e4f2020-12-04 13:00:43 -0500223 SkASSERT(layerWriter.bytesWritten() == sizeof(SkCanvasLayerState));
224 canvasState->layerCount = 1;
reed@google.com44699382013-10-31 17:28:30 +0000225 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000226 layerWriter.flatten(canvasState->layers);
227
mtklein18300a32016-03-16 13:53:35 -0700228 return canvasState.release();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000229}
230
231////////////////////////////////////////////////////////////////////////////////
232
233static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) {
234 // reconstruct the matrix
235 SkMatrix matrix;
236 for (int i = 0; i < 9; i++) {
237 matrix.set(i, state.matrix[i]);
238 }
239
Mike Reedca37f322018-03-08 13:22:16 -0500240 // only realy support 1 rect, so if the caller (legacy?) sent us more, we just take the bounds
241 // of what they sent.
242 SkIRect bounds = SkIRect::MakeEmpty();
243 if (state.clipRectCount > 0) {
Mike Reed92b33352019-08-24 19:39:13 -0400244 bounds.setLTRB(state.clipRects[0].left,
245 state.clipRects[0].top,
246 state.clipRects[0].right,
247 state.clipRects[0].bottom);
Mike Reedca37f322018-03-08 13:22:16 -0500248 for (int i = 1; i < state.clipRectCount; ++i) {
Mike Reed92b33352019-08-24 19:39:13 -0400249 bounds.join({state.clipRects[i].left,
250 state.clipRects[i].top,
251 state.clipRects[i].right,
252 state.clipRects[i].bottom});
Mike Reedca37f322018-03-08 13:22:16 -0500253 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000254 }
255
Mike Reedca37f322018-03-08 13:22:16 -0500256 canvas->clipRect(SkRect::Make(bounds));
257 canvas->concat(matrix);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000258}
259
Mike Reed5df49342016-11-12 08:06:55 -0600260static std::unique_ptr<SkCanvas>
261make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000262 SkASSERT(kRaster_CanvasBackend == layerState.type);
263
264 SkBitmap bitmap;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000265 SkColorType colorType =
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000266 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType :
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000267 layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType :
268 kUnknown_SkColorType;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000269
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000270 if (colorType == kUnknown_SkColorType) {
halcanary96fcdcc2015-08-27 07:41:13 -0700271 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000272 }
273
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000274 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
275 colorType, kPremul_SkAlphaType),
scroggo352c2182014-07-15 12:34:26 -0700276 layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000277
278 SkASSERT(!bitmap.empty());
279 SkASSERT(!bitmap.isNull());
280
Mike Reed5df49342016-11-12 08:06:55 -0600281 std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000282
283 // setup the matrix and clip
284 setup_canvas_from_MC_state(layerState.mcState, canvas.get());
285
Mike Reed5df49342016-11-12 08:06:55 -0600286 return canvas;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000287}
288
Mike Reed5df49342016-11-12 08:06:55 -0600289std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000290 SkASSERT(state);
scroggo352c2182014-07-15 12:34:26 -0700291 // Currently there is only one possible version.
292 SkASSERT(SkCanvasState_v1::kVersion == state->version);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000293
scroggo352c2182014-07-15 12:34:26 -0700294 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000295
scroggo352c2182014-07-15 12:34:26 -0700296 if (state_v1->layerCount < 1) {
halcanary96fcdcc2015-08-27 07:41:13 -0700297 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000298 }
299
Mike Reed5df49342016-11-12 08:06:55 -0600300 std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000301
302 // setup the matrix and clip on the n-way canvas
Hal Canary67b39de2016-11-07 11:47:44 -0500303 setup_canvas_from_MC_state(state_v1->mcState, canvas.get());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000304
Michael Ludwigf326e4f2020-12-04 13:00:43 -0500305 // Iterate over the layers and add them to the n-way canvas. New clients will only send one
306 // layer since unclipped layers are no longer supported, but old canvas clients may still
307 // create them.
scroggo352c2182014-07-15 12:34:26 -0700308 for (int i = state_v1->layerCount - 1; i >= 0; --i) {
Mike Reed5df49342016-11-12 08:06:55 -0600309 std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]);
John Stilesa008b0f2020-08-16 08:48:02 -0400310 if (!canvasLayer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700311 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000312 }
Mike Reed584ca892016-11-15 11:52:55 -0500313 canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x,
314 state_v1->layers[i].y));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000315 }
316
Mike Kleina9609ea2020-02-18 13:33:23 -0600317 return std::move(canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000318}
319
320////////////////////////////////////////////////////////////////////////////////
321
322void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) {
scroggo352c2182014-07-15 12:34:26 -0700323 SkASSERT(!state || SkCanvasState_v1::kVersion == state->version);
324 // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
325 // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
326 // instead uses the field "version" to determine how to behave.
halcanary385fe4d2015-08-26 13:07:48 -0700327 delete static_cast<SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000328}