djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 11 | #include "SkCanvasStack.h" |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 12 | #include "SkDevice.h" |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 13 | #include "SkRasterClip.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 14 | #include "SkWriter32.h" |
| 15 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 16 | /* |
| 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 | * |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 20 | * 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! |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 23 | * |
| 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 28 | */ |
| 29 | enum RasterConfigs { |
| 30 | kUnknown_RasterConfig = 0, |
| 31 | kRGB_565_RasterConfig = 1, |
| 32 | kARGB_8888_RasterConfig = 2 |
| 33 | }; |
| 34 | typedef int32_t RasterConfig; |
| 35 | |
| 36 | enum CanvasBackends { |
| 37 | kUnknown_CanvasBackend = 0, |
| 38 | kRaster_CanvasBackend = 1, |
| 39 | kGPU_CanvasBackend = 2, |
| 40 | kPDF_CanvasBackend = 3 |
| 41 | }; |
| 42 | typedef int32_t CanvasBackend; |
| 43 | |
| 44 | struct ClipRect { |
| 45 | int32_t left, top, right, bottom; |
| 46 | }; |
| 47 | |
| 48 | struct SkMCState { |
| 49 | float matrix[9]; |
| 50 | // NOTE: this only works for non-antialiased clips |
| 51 | int32_t clipRectCount; |
| 52 | ClipRect* clipRects; |
| 53 | }; |
| 54 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 55 | // NOTE: If you add more members, create a new subclass of SkCanvasState with a |
| 56 | // new CanvasState::version. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 57 | struct 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. |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 68 | uint64_t rowBytes; // Number of bytes from start of one line to next. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 69 | void* pixels; // The pixels, all (height * rowBytes) of them. |
| 70 | } raster; |
| 71 | struct { |
| 72 | int32_t textureID; |
| 73 | } gpu; |
| 74 | }; |
| 75 | }; |
| 76 | |
| 77 | class SkCanvasState { |
| 78 | public: |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 79 | SkCanvasState(int32_t version, SkCanvas* canvas) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 80 | SkASSERT(canvas); |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 81 | this->version = version; |
| 82 | width = canvas->getBaseLayerSize().width(); |
| 83 | height = canvas->getBaseLayerSize().height(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 84 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 85 | } |
| 86 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 87 | /** |
| 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 | |
| 99 | class SkCanvasState_v1 : public SkCanvasState { |
| 100 | public: |
| 101 | static const int32_t kVersion = 1; |
| 102 | |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 103 | SkCanvasState_v1(SkCanvas* canvas) |
| 104 | : INHERITED(kVersion, canvas) |
| 105 | { |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 106 | layerCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 107 | layers = nullptr; |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 108 | mcState.clipRectCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 109 | mcState.clipRects = nullptr; |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 110 | originalCanvas = SkRef(canvas); |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | ~SkCanvasState_v1() { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 114 | // loop through the layers and free the data allocated to the clipRects |
| 115 | for (int i = 0; i < layerCount; ++i) { |
| 116 | sk_free(layers[i].mcState.clipRects); |
| 117 | } |
| 118 | |
| 119 | sk_free(mcState.clipRects); |
| 120 | sk_free(layers); |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 121 | |
| 122 | // it is now safe to free the canvas since there should be no remaining |
| 123 | // references to the content that is referenced by this canvas (e.g. pixels) |
| 124 | originalCanvas->unref(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 125 | } |
| 126 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 127 | SkMCState mcState; |
| 128 | |
| 129 | int32_t layerCount; |
| 130 | SkCanvasLayerState* layers; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 131 | private: |
| 132 | SkCanvas* originalCanvas; |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 133 | typedef SkCanvasState INHERITED; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | //////////////////////////////////////////////////////////////////////////////// |
| 137 | |
| 138 | class ClipValidator : public SkCanvas::ClipVisitor { |
| 139 | public: |
| 140 | ClipValidator() : fFailed(false) {} |
| 141 | bool failed() { return fFailed; } |
| 142 | |
| 143 | // ClipVisitor |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 144 | void clipRect(const SkRect& rect, SkCanvas::ClipOp op, bool antialias) override { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 145 | fFailed |= antialias; |
| 146 | } |
| 147 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 148 | void clipRRect(const SkRRect& rrect, SkCanvas::ClipOp op, bool antialias) override { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 149 | fFailed |= antialias; |
| 150 | } |
| 151 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 152 | void clipPath(const SkPath&, SkCanvas::ClipOp, bool antialias) override { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 153 | fFailed |= antialias; |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | bool fFailed; |
| 158 | }; |
| 159 | |
| 160 | static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkRegion& clip) { |
| 161 | // initialize the struct |
| 162 | state->clipRectCount = 0; |
| 163 | |
| 164 | // capture the matrix |
| 165 | for (int i = 0; i < 9; i++) { |
| 166 | state->matrix[i] = matrix.get(i); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * capture the clip |
| 171 | * |
| 172 | * storage is allocated on the stack for the first 4 rects. This value was |
| 173 | * chosen somewhat arbitrarily, but does allow us to represent simple clips |
| 174 | * and some more common complex clips (e.g. a clipRect with a sub-rect |
| 175 | * clipped out of its interior) without needing to malloc any additional memory. |
| 176 | */ |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 177 | SkSWriter32<4*sizeof(ClipRect)> clipWriter; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 178 | |
| 179 | if (!clip.isEmpty()) { |
| 180 | // only returns the b/w clip so aa clips fail |
| 181 | SkRegion::Iterator clip_iterator(clip); |
| 182 | for (; !clip_iterator.done(); clip_iterator.next()) { |
| 183 | // this assumes the SkIRect is stored in l,t,r,b ordering which |
| 184 | // matches the ordering of our ClipRect struct |
| 185 | clipWriter.writeIRect(clip_iterator.rect()); |
| 186 | state->clipRectCount++; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // allocate memory for the clip then and copy them to the struct |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 191 | state->clipRects = (ClipRect*) sk_malloc_throw(clipWriter.bytesWritten()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 192 | clipWriter.flatten(state->clipRects); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
| 197 | SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) { |
| 198 | SkASSERT(canvas); |
| 199 | |
| 200 | // Check the clip can be decomposed into rectangles (i.e. no soft clips). |
| 201 | ClipValidator validator; |
| 202 | canvas->replayClips(&validator); |
| 203 | if (validator.failed()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 204 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 207 | std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 208 | |
| 209 | // decompose the total matrix and clip |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 210 | setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), |
| 211 | canvas->internal_private_getTotalClip()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * decompose the layers |
| 215 | * |
| 216 | * storage is allocated on the stack for the first 3 layers. It is common in |
| 217 | * some view systems (e.g. Android) that a few non-clipped layers are present |
| 218 | * and we will not need to malloc any additional memory in those cases. |
| 219 | */ |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 220 | SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 221 | int layerCount = 0; |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 222 | for (SkCanvas::LayerIter layer(canvas); !layer.done(); layer.next()) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 223 | |
| 224 | // we currently only work for bitmap backed devices |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 225 | SkPixmap pmap; |
| 226 | if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 227 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | SkCanvasLayerState* layerState = |
| 231 | (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState)); |
| 232 | layerState->type = kRaster_CanvasBackend; |
| 233 | layerState->x = layer.x(); |
| 234 | layerState->y = layer.y(); |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 235 | layerState->width = pmap.width(); |
| 236 | layerState->height = pmap.height(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 237 | |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 238 | switch (pmap.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 239 | case kN32_SkColorType: |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 240 | layerState->raster.config = kARGB_8888_RasterConfig; |
| 241 | break; |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 242 | case kRGB_565_SkColorType: |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 243 | layerState->raster.config = kRGB_565_RasterConfig; |
| 244 | break; |
| 245 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 246 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 247 | } |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 248 | layerState->raster.rowBytes = pmap.rowBytes(); |
| 249 | layerState->raster.pixels = pmap.writable_addr(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 250 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 251 | setup_MC_state(&layerState->mcState, layer.matrix(), layer.clip().bwRgn()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 252 | layerCount++; |
| 253 | } |
| 254 | |
| 255 | // allocate memory for the layers and then and copy them to the struct |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 256 | SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerState)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 257 | canvasState->layerCount = layerCount; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 258 | canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 259 | layerWriter.flatten(canvasState->layers); |
| 260 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 261 | return canvasState.release(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | //////////////////////////////////////////////////////////////////////////////// |
| 265 | |
| 266 | static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { |
| 267 | // reconstruct the matrix |
| 268 | SkMatrix matrix; |
| 269 | for (int i = 0; i < 9; i++) { |
| 270 | matrix.set(i, state.matrix[i]); |
| 271 | } |
| 272 | |
| 273 | // reconstruct the clip |
| 274 | SkRegion clip; |
| 275 | for (int i = 0; i < state.clipRectCount; ++i) { |
| 276 | clip.op(SkIRect::MakeLTRB(state.clipRects[i].left, |
| 277 | state.clipRects[i].top, |
| 278 | state.clipRects[i].right, |
| 279 | state.clipRects[i].bottom), |
| 280 | SkRegion::kUnion_Op); |
| 281 | } |
| 282 | |
| 283 | canvas->setMatrix(matrix); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 284 | canvas->clipRegion(clip, SkCanvas::kReplace_Op); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 287 | static SkCanvas* create_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 288 | SkASSERT(kRaster_CanvasBackend == layerState.type); |
| 289 | |
| 290 | SkBitmap bitmap; |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 291 | SkColorType colorType = |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 292 | layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType : |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 293 | layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType : |
| 294 | kUnknown_SkColorType; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 295 | |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 296 | if (colorType == kUnknown_SkColorType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 297 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 298 | } |
| 299 | |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 300 | bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height, |
| 301 | colorType, kPremul_SkAlphaType), |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 302 | layerState.raster.pixels, (size_t) layerState.raster.rowBytes); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 303 | |
| 304 | SkASSERT(!bitmap.empty()); |
| 305 | SkASSERT(!bitmap.isNull()); |
| 306 | |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 307 | sk_sp<SkCanvas> canvas(new SkCanvas(bitmap)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 308 | |
| 309 | // setup the matrix and clip |
| 310 | setup_canvas_from_MC_state(layerState.mcState, canvas.get()); |
| 311 | |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 312 | return canvas.release(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 315 | SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 316 | SkASSERT(state); |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 317 | // Currently there is only one possible version. |
| 318 | SkASSERT(SkCanvasState_v1::kVersion == state->version); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 319 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 320 | const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 321 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 322 | if (state_v1->layerCount < 1) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 323 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 326 | sk_sp<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 327 | |
| 328 | // setup the matrix and clip on the n-way canvas |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 329 | setup_canvas_from_MC_state(state_v1->mcState, canvas.get()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 330 | |
| 331 | // Iterate over the layers and add them to the n-way canvas |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 332 | for (int i = state_v1->layerCount - 1; i >= 0; --i) { |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 333 | sk_sp<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state_v1->layers[i])); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 334 | if (!canvasLayer.get()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 335 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 336 | } |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 337 | canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state_v1->layers[i].x, |
| 338 | state_v1->layers[i].y)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Heather Miller | b613c26 | 2016-11-10 21:25:30 +0000 | [diff] [blame] | 341 | return canvas.release(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | //////////////////////////////////////////////////////////////////////////////// |
| 345 | |
| 346 | void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 347 | SkASSERT(!state || SkCanvasState_v1::kVersion == state->version); |
| 348 | // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on |
| 349 | // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and |
| 350 | // instead uses the field "version" to determine how to behave. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 351 | delete static_cast<SkCanvasState_v1*>(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 352 | } |