blob: 462636ead9fb593463991973aee6a0a53a7d943d [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"
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +000013#include "SkErrorInternals.h"
reed1e7f5e72016-04-27 07:49:17 -070014#include "SkRasterClip.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000015#include "SkWriter32.h"
16
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
104 SkCanvasState_v1(SkCanvas* canvas)
105 : INHERITED(kVersion, canvas)
106 {
107 layerCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700108 layers = nullptr;
scroggo352c2182014-07-15 12:34:26 -0700109 mcState.clipRectCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700110 mcState.clipRects = nullptr;
scroggo352c2182014-07-15 12:34:26 -0700111 originalCanvas = SkRef(canvas);
112 }
113
114 ~SkCanvasState_v1() {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000115 // loop through the layers and free the data allocated to the clipRects
116 for (int i = 0; i < layerCount; ++i) {
117 sk_free(layers[i].mcState.clipRects);
118 }
119
120 sk_free(mcState.clipRects);
121 sk_free(layers);
122
123 // it is now safe to free the canvas since there should be no remaining
124 // references to the content that is referenced by this canvas (e.g. pixels)
125 originalCanvas->unref();
126 }
127
djsollen@google.com5587ac02013-08-29 20:20:40 +0000128 SkMCState mcState;
129
130 int32_t layerCount;
131 SkCanvasLayerState* layers;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000132private:
133 SkCanvas* originalCanvas;
scroggo352c2182014-07-15 12:34:26 -0700134 typedef SkCanvasState INHERITED;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000135};
136
137////////////////////////////////////////////////////////////////////////////////
138
139class ClipValidator : public SkCanvas::ClipVisitor {
140public:
141 ClipValidator() : fFailed(false) {}
142 bool failed() { return fFailed; }
143
144 // ClipVisitor
mtklein36352bf2015-03-25 18:17:31 -0700145 void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) override {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000146 fFailed |= antialias;
147 }
148
mtklein36352bf2015-03-25 18:17:31 -0700149 void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000150 fFailed |= antialias;
151 }
152
mtklein36352bf2015-03-25 18:17:31 -0700153 void clipPath(const SkPath&, SkRegion::Op, bool antialias) override {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000154 fFailed |= antialias;
155 }
156
157private:
158 bool fFailed;
159};
160
161static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkRegion& clip) {
162 // initialize the struct
163 state->clipRectCount = 0;
164
165 // capture the matrix
166 for (int i = 0; i < 9; i++) {
167 state->matrix[i] = matrix.get(i);
168 }
169
170 /*
171 * capture the clip
172 *
173 * storage is allocated on the stack for the first 4 rects. This value was
174 * chosen somewhat arbitrarily, but does allow us to represent simple clips
175 * and some more common complex clips (e.g. a clipRect with a sub-rect
176 * clipped out of its interior) without needing to malloc any additional memory.
177 */
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000178 SkSWriter32<4*sizeof(ClipRect)> clipWriter;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000179
180 if (!clip.isEmpty()) {
181 // only returns the b/w clip so aa clips fail
182 SkRegion::Iterator clip_iterator(clip);
183 for (; !clip_iterator.done(); clip_iterator.next()) {
184 // this assumes the SkIRect is stored in l,t,r,b ordering which
185 // matches the ordering of our ClipRect struct
186 clipWriter.writeIRect(clip_iterator.rect());
187 state->clipRectCount++;
188 }
189 }
190
191 // allocate memory for the clip then and copy them to the struct
reed@google.com44699382013-10-31 17:28:30 +0000192 state->clipRects = (ClipRect*) sk_malloc_throw(clipWriter.bytesWritten());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000193 clipWriter.flatten(state->clipRects);
194}
195
196
197
198SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) {
199 SkASSERT(canvas);
200
201 // Check the clip can be decomposed into rectangles (i.e. no soft clips).
202 ClipValidator validator;
203 canvas->replayClips(&validator);
204 if (validator.failed()) {
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +0000205 SkErrorInternals::SetError(kInvalidOperation_SkError,
206 "CaptureCanvasState does not support canvases with antialiased clips.\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700207 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000208 }
209
halcanary385fe4d2015-08-26 13:07:48 -0700210 SkAutoTDelete<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000211
212 // decompose the total matrix and clip
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000213 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(),
214 canvas->internal_private_getTotalClip());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000215
216 /*
217 * decompose the layers
218 *
219 * storage is allocated on the stack for the first 3 layers. It is common in
220 * some view systems (e.g. Android) that a few non-clipped layers are present
221 * and we will not need to malloc any additional memory in those cases.
222 */
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000223 SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000224 int layerCount = 0;
225 for (SkCanvas::LayerIter layer(canvas, true/*skipEmptyClips*/); !layer.done(); layer.next()) {
226
227 // we currently only work for bitmap backed devices
reed9572a102015-05-26 19:22:17 -0700228 SkPixmap pmap;
229 if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700230 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000231 }
232
233 SkCanvasLayerState* layerState =
234 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState));
235 layerState->type = kRaster_CanvasBackend;
236 layerState->x = layer.x();
237 layerState->y = layer.y();
reed9572a102015-05-26 19:22:17 -0700238 layerState->width = pmap.width();
239 layerState->height = pmap.height();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000240
reed9572a102015-05-26 19:22:17 -0700241 switch (pmap.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000242 case kN32_SkColorType:
djsollen@google.com5587ac02013-08-29 20:20:40 +0000243 layerState->raster.config = kARGB_8888_RasterConfig;
244 break;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000245 case kRGB_565_SkColorType:
djsollen@google.com5587ac02013-08-29 20:20:40 +0000246 layerState->raster.config = kRGB_565_RasterConfig;
247 break;
248 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700249 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000250 }
reed9572a102015-05-26 19:22:17 -0700251 layerState->raster.rowBytes = pmap.rowBytes();
252 layerState->raster.pixels = pmap.writable_addr();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000253
reed1e7f5e72016-04-27 07:49:17 -0700254 setup_MC_state(&layerState->mcState, layer.matrix(), layer.clip().bwRgn());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000255 layerCount++;
256 }
257
258 // allocate memory for the layers and then and copy them to the struct
reed@google.com44699382013-10-31 17:28:30 +0000259 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerState));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000260 canvasState->layerCount = layerCount;
reed@google.com44699382013-10-31 17:28:30 +0000261 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000262 layerWriter.flatten(canvasState->layers);
263
mtklein18300a32016-03-16 13:53:35 -0700264 return canvasState.release();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000265}
266
267////////////////////////////////////////////////////////////////////////////////
268
269static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) {
270 // reconstruct the matrix
271 SkMatrix matrix;
272 for (int i = 0; i < 9; i++) {
273 matrix.set(i, state.matrix[i]);
274 }
275
276 // reconstruct the clip
277 SkRegion clip;
278 for (int i = 0; i < state.clipRectCount; ++i) {
279 clip.op(SkIRect::MakeLTRB(state.clipRects[i].left,
280 state.clipRects[i].top,
281 state.clipRects[i].right,
282 state.clipRects[i].bottom),
283 SkRegion::kUnion_Op);
284 }
285
286 canvas->setMatrix(matrix);
287 canvas->setClipRegion(clip);
288}
289
290static SkCanvas* create_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) {
291 SkASSERT(kRaster_CanvasBackend == layerState.type);
292
293 SkBitmap bitmap;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000294 SkColorType colorType =
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000295 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType :
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000296 layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType :
297 kUnknown_SkColorType;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000298
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000299 if (colorType == kUnknown_SkColorType) {
halcanary96fcdcc2015-08-27 07:41:13 -0700300 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000301 }
302
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000303 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
304 colorType, kPremul_SkAlphaType),
scroggo352c2182014-07-15 12:34:26 -0700305 layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000306
307 SkASSERT(!bitmap.empty());
308 SkASSERT(!bitmap.isNull());
309
halcanary385fe4d2015-08-26 13:07:48 -0700310 SkAutoTUnref<SkCanvas> canvas(new SkCanvas(bitmap));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000311
312 // setup the matrix and clip
313 setup_canvas_from_MC_state(layerState.mcState, canvas.get());
314
mtklein18300a32016-03-16 13:53:35 -0700315 return canvas.release();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000316}
317
318SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) {
319 SkASSERT(state);
scroggo352c2182014-07-15 12:34:26 -0700320 // Currently there is only one possible version.
321 SkASSERT(SkCanvasState_v1::kVersion == state->version);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000322
scroggo352c2182014-07-15 12:34:26 -0700323 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000324
scroggo352c2182014-07-15 12:34:26 -0700325 if (state_v1->layerCount < 1) {
halcanary96fcdcc2015-08-27 07:41:13 -0700326 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000327 }
328
halcanary385fe4d2015-08-26 13:07:48 -0700329 SkAutoTUnref<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000330
331 // setup the matrix and clip on the n-way canvas
scroggo352c2182014-07-15 12:34:26 -0700332 setup_canvas_from_MC_state(state_v1->mcState, canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000333
334 // Iterate over the layers and add them to the n-way canvas
scroggo352c2182014-07-15 12:34:26 -0700335 for (int i = state_v1->layerCount - 1; i >= 0; --i) {
336 SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state_v1->layers[i]));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000337 if (!canvasLayer.get()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700338 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000339 }
scroggo352c2182014-07-15 12:34:26 -0700340 canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state_v1->layers[i].x,
341 state_v1->layers[i].y));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000342 }
343
mtklein18300a32016-03-16 13:53:35 -0700344 return canvas.release();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000345}
346
347////////////////////////////////////////////////////////////////////////////////
348
349void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) {
scroggo352c2182014-07-15 12:34:26 -0700350 SkASSERT(!state || SkCanvasState_v1::kVersion == state->version);
351 // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
352 // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
353 // instead uses the field "version" to determine how to behave.
halcanary385fe4d2015-08-26 13:07:48 -0700354 delete static_cast<SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000355}