blob: a78f3435a429190cdd816fa761bea8e8bf4753bd [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"
Mike Reed986480a2017-01-13 22:43:16 +000012#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"
Mike Reedebfce6d2016-12-12 10:02:12 -050015#include "SkClipOpPriv.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() {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000113 // loop through the layers and free the data allocated to the clipRects
114 for (int i = 0; i < layerCount; ++i) {
115 sk_free(layers[i].mcState.clipRects);
116 }
117
118 sk_free(mcState.clipRects);
119 sk_free(layers);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000120 }
121
djsollen@google.com5587ac02013-08-29 20:20:40 +0000122 SkMCState mcState;
123
124 int32_t layerCount;
125 SkCanvasLayerState* layers;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000126private:
127 SkCanvas* originalCanvas;
scroggo352c2182014-07-15 12:34:26 -0700128 typedef SkCanvasState INHERITED;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000129};
130
131////////////////////////////////////////////////////////////////////////////////
132
Mike Reedea5e6762017-03-02 20:22:35 +0000133class ClipValidator : public SkCanvas::ClipVisitor {
134public:
135 ClipValidator() : fFailed(false) {}
136 bool failed() { return fFailed; }
137
138 // ClipVisitor
139 void clipRect(const SkRect& rect, SkClipOp op, bool antialias) override {
140 fFailed |= antialias;
141 }
142
143 void clipRRect(const SkRRect& rrect, SkClipOp op, bool antialias) override {
144 fFailed |= antialias;
145 }
146
147 void clipPath(const SkPath&, SkClipOp, bool antialias) override {
148 fFailed |= antialias;
149 }
150
151private:
152 bool fFailed;
153};
154
djsollen@google.com5587ac02013-08-29 20:20:40 +0000155static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkRegion& clip) {
156 // initialize the struct
157 state->clipRectCount = 0;
158
159 // capture the matrix
160 for (int i = 0; i < 9; i++) {
161 state->matrix[i] = matrix.get(i);
162 }
163
164 /*
165 * capture the clip
166 *
167 * storage is allocated on the stack for the first 4 rects. This value was
168 * chosen somewhat arbitrarily, but does allow us to represent simple clips
169 * and some more common complex clips (e.g. a clipRect with a sub-rect
170 * clipped out of its interior) without needing to malloc any additional memory.
171 */
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000172 SkSWriter32<4*sizeof(ClipRect)> clipWriter;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000173
174 if (!clip.isEmpty()) {
175 // only returns the b/w clip so aa clips fail
176 SkRegion::Iterator clip_iterator(clip);
177 for (; !clip_iterator.done(); clip_iterator.next()) {
178 // this assumes the SkIRect is stored in l,t,r,b ordering which
179 // matches the ordering of our ClipRect struct
180 clipWriter.writeIRect(clip_iterator.rect());
181 state->clipRectCount++;
182 }
183 }
184
185 // allocate memory for the clip then and copy them to the struct
reed@google.com44699382013-10-31 17:28:30 +0000186 state->clipRects = (ClipRect*) sk_malloc_throw(clipWriter.bytesWritten());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000187 clipWriter.flatten(state->clipRects);
188}
189
190
191
192SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) {
193 SkASSERT(canvas);
194
195 // Check the clip can be decomposed into rectangles (i.e. no soft clips).
Mike Reedea5e6762017-03-02 20:22:35 +0000196 ClipValidator validator;
197 canvas->replayClips(&validator);
198 if (validator.failed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700199 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000200 }
201
Ben Wagner145dbcd2016-11-03 14:40:50 -0400202 std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000203
204 // decompose the total matrix and clip
Mike Reed3726a4a2017-01-19 11:36:41 -0500205 {
206 SkRegion rgn;
207 canvas->temporary_internal_getRgnClip(&rgn);
208 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), rgn);
209 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000210
211 /*
212 * decompose the layers
213 *
214 * storage is allocated on the stack for the first 3 layers. It is common in
215 * some view systems (e.g. Android) that a few non-clipped layers are present
216 * and we will not need to malloc any additional memory in those cases.
217 */
commit-bot@chromium.org19382422014-01-14 20:51:26 +0000218 SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000219 int layerCount = 0;
reed3aafe112016-08-18 12:45:34 -0700220 for (SkCanvas::LayerIter layer(canvas); !layer.done(); layer.next()) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000221
222 // we currently only work for bitmap backed devices
reed9572a102015-05-26 19:22:17 -0700223 SkPixmap pmap;
224 if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700225 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000226 }
227
228 SkCanvasLayerState* layerState =
229 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState));
230 layerState->type = kRaster_CanvasBackend;
231 layerState->x = layer.x();
232 layerState->y = layer.y();
reed9572a102015-05-26 19:22:17 -0700233 layerState->width = pmap.width();
234 layerState->height = pmap.height();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000235
reed9572a102015-05-26 19:22:17 -0700236 switch (pmap.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000237 case kN32_SkColorType:
djsollen@google.com5587ac02013-08-29 20:20:40 +0000238 layerState->raster.config = kARGB_8888_RasterConfig;
239 break;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000240 case kRGB_565_SkColorType:
djsollen@google.com5587ac02013-08-29 20:20:40 +0000241 layerState->raster.config = kRGB_565_RasterConfig;
242 break;
243 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700244 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000245 }
reed9572a102015-05-26 19:22:17 -0700246 layerState->raster.rowBytes = pmap.rowBytes();
247 layerState->raster.pixels = pmap.writable_addr();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000248
Mike Reedea5e6762017-03-02 20:22:35 +0000249 setup_MC_state(&layerState->mcState, layer.matrix(), layer.clip().bwRgn());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000250 layerCount++;
251 }
252
253 // allocate memory for the layers and then and copy them to the struct
reed@google.com44699382013-10-31 17:28:30 +0000254 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerState));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000255 canvasState->layerCount = layerCount;
reed@google.com44699382013-10-31 17:28:30 +0000256 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000257 layerWriter.flatten(canvasState->layers);
258
mtklein18300a32016-03-16 13:53:35 -0700259 return canvasState.release();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000260}
261
262////////////////////////////////////////////////////////////////////////////////
263
264static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) {
265 // reconstruct the matrix
266 SkMatrix matrix;
267 for (int i = 0; i < 9; i++) {
268 matrix.set(i, state.matrix[i]);
269 }
270
271 // reconstruct the clip
272 SkRegion clip;
273 for (int i = 0; i < state.clipRectCount; ++i) {
274 clip.op(SkIRect::MakeLTRB(state.clipRects[i].left,
275 state.clipRects[i].top,
276 state.clipRects[i].right,
277 state.clipRects[i].bottom),
278 SkRegion::kUnion_Op);
279 }
280
281 canvas->setMatrix(matrix);
Mike Reedc1f77742016-12-09 09:00:50 -0500282 canvas->clipRegion(clip, kReplace_SkClipOp);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000283}
284
Mike Reed5df49342016-11-12 08:06:55 -0600285static std::unique_ptr<SkCanvas>
286make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000287 SkASSERT(kRaster_CanvasBackend == layerState.type);
288
289 SkBitmap bitmap;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000290 SkColorType colorType =
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000291 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType :
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000292 layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType :
293 kUnknown_SkColorType;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000294
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000295 if (colorType == kUnknown_SkColorType) {
halcanary96fcdcc2015-08-27 07:41:13 -0700296 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000297 }
298
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000299 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
300 colorType, kPremul_SkAlphaType),
scroggo352c2182014-07-15 12:34:26 -0700301 layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000302
303 SkASSERT(!bitmap.empty());
304 SkASSERT(!bitmap.isNull());
305
Mike Reed5df49342016-11-12 08:06:55 -0600306 std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000307
308 // setup the matrix and clip
309 setup_canvas_from_MC_state(layerState.mcState, canvas.get());
310
Mike Reed5df49342016-11-12 08:06:55 -0600311 return canvas;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000312}
313
Mike Reed5df49342016-11-12 08:06:55 -0600314std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000315 SkASSERT(state);
scroggo352c2182014-07-15 12:34:26 -0700316 // Currently there is only one possible version.
317 SkASSERT(SkCanvasState_v1::kVersion == state->version);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000318
scroggo352c2182014-07-15 12:34:26 -0700319 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000320
scroggo352c2182014-07-15 12:34:26 -0700321 if (state_v1->layerCount < 1) {
halcanary96fcdcc2015-08-27 07:41:13 -0700322 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000323 }
324
Mike Reed5df49342016-11-12 08:06:55 -0600325 std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000326
327 // setup the matrix and clip on the n-way canvas
Hal Canary67b39de2016-11-07 11:47:44 -0500328 setup_canvas_from_MC_state(state_v1->mcState, canvas.get());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000329
330 // Iterate over the layers and add them to the n-way canvas
scroggo352c2182014-07-15 12:34:26 -0700331 for (int i = state_v1->layerCount - 1; i >= 0; --i) {
Mike Reed5df49342016-11-12 08:06:55 -0600332 std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000333 if (!canvasLayer.get()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700334 return nullptr;
djsollen@google.com5587ac02013-08-29 20:20:40 +0000335 }
Mike Reed584ca892016-11-15 11:52:55 -0500336 canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x,
337 state_v1->layers[i].y));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000338 }
339
Mike Reed5df49342016-11-12 08:06:55 -0600340 return std::move(canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000341}
342
343////////////////////////////////////////////////////////////////////////////////
344
345void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) {
scroggo352c2182014-07-15 12:34:26 -0700346 SkASSERT(!state || SkCanvasState_v1::kVersion == state->version);
347 // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
348 // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
349 // instead uses the field "version" to determine how to behave.
halcanary385fe4d2015-08-26 13:07:48 -0700350 delete static_cast<SkCanvasState_v1*>(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000351}