blob: d31eaa632ca3e1606909983b90494b23976bae14 [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
8#include "SkCanvasStateUtils.h"
9
djsollen@google.com5587ac02013-08-29 20:20:40 +000010#include "SkCanvas.h"
djsollen@google.com339e79f2013-09-04 17:16:00 +000011#include "SkCanvasStack.h"
robertphillips9a53fd72015-06-22 09:46:59 -070012#include "SkDevice.h"
reed1e7f5e72016-04-27 07:49:17 -070013#include "SkRasterClip.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000014#include "SkWriter32.h"
15
djsollen@google.com5587ac02013-08-29 20:20:40 +000016/*
17 * WARNING: The structs below are part of a stable ABI and as such we explicitly
18 * use unambigious primitives (e.g. int32_t instead of an enum).
19 *
scroggo352c2182014-07-15 12:34:26 -070020 * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN A NEW
21 * NEW SUBCLASS OF SkCanvasState. SUCH CHANGES SHOULD ONLY BE MADE IF ABSOLUTELY
22 * NECESSARY!
scroggo24519372014-07-22 12:38:55 -070023 *
24 * In order to test changes, run the CanvasState tests. gyp/canvas_state_lib.gyp
25 * describes how to create a library to pass to the CanvasState tests. The tests
26 * should succeed when building the library with your changes and passing that to
27 * the tests running in the unchanged Skia.
djsollen@google.com5587ac02013-08-29 20:20:40 +000028 */
29enum RasterConfigs {
30 kUnknown_RasterConfig = 0,
31 kRGB_565_RasterConfig = 1,
32 kARGB_8888_RasterConfig = 2
33};
34typedef int32_t RasterConfig;
35
36enum CanvasBackends {
37 kUnknown_CanvasBackend = 0,
38 kRaster_CanvasBackend = 1,
39 kGPU_CanvasBackend = 2,
40 kPDF_CanvasBackend = 3
41};
42typedef int32_t CanvasBackend;
43
44struct ClipRect {
45 int32_t left, top, right, bottom;
46};
47
48struct SkMCState {
49 float matrix[9];
50 // NOTE: this only works for non-antialiased clips
51 int32_t clipRectCount;
52 ClipRect* clipRects;
53};
54
scroggo352c2182014-07-15 12:34:26 -070055// NOTE: If you add more members, create a new subclass of SkCanvasState with a
56// new CanvasState::version.
djsollen@google.com5587ac02013-08-29 20:20:40 +000057struct SkCanvasLayerState {
58 CanvasBackend type;
59 int32_t x, y;
60 int32_t width;
61 int32_t height;
62
63 SkMCState mcState;
64
65 union {
66 struct {
67 RasterConfig config; // pixel format: a value from RasterConfigs.
scroggo352c2182014-07-15 12:34:26 -070068 uint64_t rowBytes; // Number of bytes from start of one line to next.
djsollen@google.com5587ac02013-08-29 20:20:40 +000069 void* pixels; // The pixels, all (height * rowBytes) of them.
70 } raster;
71 struct {
72 int32_t textureID;
73 } gpu;
74 };
75};
76
77class SkCanvasState {
78public:
scroggo352c2182014-07-15 12:34:26 -070079 SkCanvasState(int32_t version, SkCanvas* canvas) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000080 SkASSERT(canvas);
scroggo352c2182014-07-15 12:34:26 -070081 this->version = version;
82 width = canvas->getBaseLayerSize().width();
83 height = canvas->getBaseLayerSize().height();
djsollen@google.com5587ac02013-08-29 20:20:40 +000084
djsollen@google.com5587ac02013-08-29 20:20:40 +000085 }
86
scroggo352c2182014-07-15 12:34:26 -070087 /**
88 * The version this struct was built with. This field must always appear
89 * first in the struct so that when the versions don't match (and the
90 * remaining contents and size are potentially different) we can still
91 * compare the version numbers.
92 */
93 int32_t version;
94 int32_t width;
95 int32_t height;
96 int32_t alignmentPadding;
97};
98
99class SkCanvasState_v1 : public SkCanvasState {
100public:
101 static const int32_t kVersion = 1;
102
Mike Reed5df49342016-11-12 08:06:55 -0600103 SkCanvasState_v1(SkCanvas* canvas) : INHERITED(kVersion, canvas) {
scroggo352c2182014-07-15 12:34:26 -0700104 layerCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700105 layers = nullptr;
scroggo352c2182014-07-15 12:34:26 -0700106 mcState.clipRectCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700107 mcState.clipRects = nullptr;
Mike Reed5df49342016-11-12 08:06:55 -0600108 originalCanvas = canvas;
scroggo352c2182014-07-15 12:34:26 -0700109 }
110
111 ~SkCanvasState_v1() {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000112 // loop through the layers and free the data allocated to the clipRects
113 for (int i = 0; i < layerCount; ++i) {
114 sk_free(layers[i].mcState.clipRects);
115 }
116
117 sk_free(mcState.clipRects);
118 sk_free(layers);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000119 }
120
djsollen@google.com5587ac02013-08-29 20:20:40 +0000121 SkMCState mcState;
122
123 int32_t layerCount;
124 SkCanvasLayerState* layers;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000125private:
126 SkCanvas* originalCanvas;
scroggo352c2182014-07-15 12:34:26 -0700127 typedef SkCanvasState INHERITED;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000128};
129
130////////////////////////////////////////////////////////////////////////////////
131
132class ClipValidator : public SkCanvas::ClipVisitor {
133public:
134 ClipValidator() : fFailed(false) {}
135 bool failed() { return fFailed; }
136
137 // ClipVisitor
Mike Reeda129dfe2016-12-09 13:14:25 +0000138 void clipRect(const SkRect& rect, SkCanvas::ClipOp op, bool antialias) override {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000139 fFailed |= antialias;
140 }
141
Mike Reeda129dfe2016-12-09 13:14:25 +0000142 void clipRRect(const SkRRect& rrect, SkCanvas::ClipOp op, bool antialias) override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000143 fFailed |= antialias;
144 }
145
Mike Reeda129dfe2016-12-09 13:14:25 +0000146 void clipPath(const SkPath&, SkCanvas::ClipOp, bool antialias) override {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000147 fFailed |= antialias;
148 }
149
150private:
151 bool fFailed;
152};
153
154static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkRegion& clip) {
155 // initialize the struct
156 state->clipRectCount = 0;
157
158 // capture the matrix
159 for (int i = 0; i < 9; i++) {
160 state->matrix[i] = matrix.get(i);
161 }
162
163 /*
164 * capture the clip
165 *
166 * storage is allocated on the stack for the first 4 rects. This value was
167 * chosen somewhat arbitrarily, but does allow us to represent simple clips
168 * and some more common complex clips (e.g. a clipRect with a sub-rect
169 * clipped out of its interior) without needing to malloc any additional memory.
170 */
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000171 SkSWriter32<4*sizeof(ClipRect)> clipWriter;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000172
173 if (!clip.isEmpty()) {
174 // only returns the b/w clip so aa clips fail
175 SkRegion::Iterator clip_iterator(clip);
176 for (; !clip_iterator.done(); clip_iterator.next()) {
177 // this assumes the SkIRect is stored in l,t,r,b ordering which
178 // matches the ordering of our ClipRect struct
179 clipWriter.writeIRect(clip_iterator.rect());
180 state->clipRectCount++;
181 }
182 }
183
184 // allocate memory for the clip then and copy them to the struct
reed@google.com44699382013-10-31 17:28:30 +0000185 state->clipRects = (ClipRect*) sk_malloc_throw(clipWriter.bytesWritten());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000186 clipWriter.flatten(state->clipRects);
187}
188
189
190
191SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) {
192 SkASSERT(canvas);
193
194 // Check the clip can be decomposed into rectangles (i.e. no soft clips).
195 ClipValidator validator;
196 canvas->replayClips(&validator);
197 if (validator.failed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700198 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000199 }
200
Ben Wagner145dbcd2016-11-03 14:40:50 -0400201 std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000202
203 // decompose the total matrix and clip
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000204 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(),
205 canvas->internal_private_getTotalClip());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000206
207 /*
208 * decompose the layers
209 *
210 * storage is allocated on the stack for the first 3 layers. It is common in
211 * some view systems (e.g. Android) that a few non-clipped layers are present
212 * and we will not need to malloc any additional memory in those cases.
213 */
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000214 SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000215 int layerCount = 0;
reed3aafe112016-08-18 12:45:34 -0700216 for (SkCanvas::LayerIter layer(canvas); !layer.done(); layer.next()) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000217
218 // we currently only work for bitmap backed devices
reed9572a102015-05-26 19:22:17 -0700219 SkPixmap pmap;
220 if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700221 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000222 }
223
224 SkCanvasLayerState* layerState =
225 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState));
226 layerState->type = kRaster_CanvasBackend;
227 layerState->x = layer.x();
228 layerState->y = layer.y();
reed9572a102015-05-26 19:22:17 -0700229 layerState->width = pmap.width();
230 layerState->height = pmap.height();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000231
reed9572a102015-05-26 19:22:17 -0700232 switch (pmap.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000233 case kN32_SkColorType:
djsollen@google.com5587ac02013-08-29 20:20:40 +0000234 layerState->raster.config = kARGB_8888_RasterConfig;
235 break;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000236 case kRGB_565_SkColorType:
djsollen@google.com5587ac02013-08-29 20:20:40 +0000237 layerState->raster.config = kRGB_565_RasterConfig;
238 break;
239 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700240 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000241 }
reed9572a102015-05-26 19:22:17 -0700242 layerState->raster.rowBytes = pmap.rowBytes();
243 layerState->raster.pixels = pmap.writable_addr();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000244
reed1e7f5e72016-04-27 07:49:17 -0700245 setup_MC_state(&layerState->mcState, layer.matrix(), layer.clip().bwRgn());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000246 layerCount++;
247 }
248
249 // allocate memory for the layers and then and copy them to the struct
reed@google.com44699382013-10-31 17:28:30 +0000250 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerState));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000251 canvasState->layerCount = layerCount;
reed@google.com44699382013-10-31 17:28:30 +0000252 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000253 layerWriter.flatten(canvasState->layers);
254
mtklein18300a32016-03-16 13:53:35 -0700255 return canvasState.release();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000256}
257
258////////////////////////////////////////////////////////////////////////////////
259
260static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) {
261 // reconstruct the matrix
262 SkMatrix matrix;
263 for (int i = 0; i < 9; i++) {
264 matrix.set(i, state.matrix[i]);
265 }
266
267 // reconstruct the clip
268 SkRegion clip;
269 for (int i = 0; i < state.clipRectCount; ++i) {
270 clip.op(SkIRect::MakeLTRB(state.clipRects[i].left,
271 state.clipRects[i].top,
272 state.clipRects[i].right,
273 state.clipRects[i].bottom),
274 SkRegion::kUnion_Op);
275 }
276
277 canvas->setMatrix(matrix);
Mike Reeda129dfe2016-12-09 13:14:25 +0000278 canvas->clipRegion(clip, SkCanvas::kReplace_Op);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000279}
280
Mike Reed5df49342016-11-12 08:06:55 -0600281static std::unique_ptr<SkCanvas>
282make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000283 SkASSERT(kRaster_CanvasBackend == layerState.type);
284
285 SkBitmap bitmap;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000286 SkColorType colorType =
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000287 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType :
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000288 layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType :
289 kUnknown_SkColorType;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000290
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000291 if (colorType == kUnknown_SkColorType) {
halcanary96fcdcc2015-08-27 07:41:13 -0700292 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000293 }
294
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000295 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
296 colorType, kPremul_SkAlphaType),
scroggo352c2182014-07-15 12:34:26 -0700297 layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000298
299 SkASSERT(!bitmap.empty());
300 SkASSERT(!bitmap.isNull());
301
Mike Reed5df49342016-11-12 08:06:55 -0600302 std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000303
304 // setup the matrix and clip
305 setup_canvas_from_MC_state(layerState.mcState, canvas.get());
306
Mike Reed5df49342016-11-12 08:06:55 -0600307 return canvas;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000308}
309
Mike Reed5df49342016-11-12 08:06:55 -0600310std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000311 SkASSERT(state);
scroggo352c2182014-07-15 12:34:26 -0700312 // Currently there is only one possible version.
313 SkASSERT(SkCanvasState_v1::kVersion == state->version);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000314
scroggo352c2182014-07-15 12:34:26 -0700315 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000316
scroggo352c2182014-07-15 12:34:26 -0700317 if (state_v1->layerCount < 1) {
halcanary96fcdcc2015-08-27 07:41:13 -0700318 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000319 }
320
Mike Reed5df49342016-11-12 08:06:55 -0600321 std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000322
323 // setup the matrix and clip on the n-way canvas
Hal Canary67b39de2016-11-07 11:47:44 -0500324 setup_canvas_from_MC_state(state_v1->mcState, canvas.get());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000325
326 // Iterate over the layers and add them to the n-way canvas
scroggo352c2182014-07-15 12:34:26 -0700327 for (int i = state_v1->layerCount - 1; i >= 0; --i) {
Mike Reed5df49342016-11-12 08:06:55 -0600328 std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000329 if (!canvasLayer.get()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700330 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000331 }
Mike Reed584ca892016-11-15 11:52:55 -0500332 canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x,
333 state_v1->layers[i].y));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000334 }
335
Mike Reed5df49342016-11-12 08:06:55 -0600336 return std::move(canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000337}
338
339////////////////////////////////////////////////////////////////////////////////
340
341void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) {
scroggo352c2182014-07-15 12:34:26 -0700342 SkASSERT(!state || SkCanvasState_v1::kVersion == state->version);
343 // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
344 // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
345 // instead uses the field "version" to determine how to behave.
halcanary385fe4d2015-08-26 13:07:48 -0700346 delete static_cast<SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000347}