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" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [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" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 15 | #include "SkClipOpPriv.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 16 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 17 | /* |
| 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 | * |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 21 | * 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! |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 24 | * |
| 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 29 | */ |
| 30 | enum RasterConfigs { |
| 31 | kUnknown_RasterConfig = 0, |
| 32 | kRGB_565_RasterConfig = 1, |
| 33 | kARGB_8888_RasterConfig = 2 |
| 34 | }; |
| 35 | typedef int32_t RasterConfig; |
| 36 | |
| 37 | enum CanvasBackends { |
| 38 | kUnknown_CanvasBackend = 0, |
| 39 | kRaster_CanvasBackend = 1, |
| 40 | kGPU_CanvasBackend = 2, |
| 41 | kPDF_CanvasBackend = 3 |
| 42 | }; |
| 43 | typedef int32_t CanvasBackend; |
| 44 | |
| 45 | struct ClipRect { |
| 46 | int32_t left, top, right, bottom; |
| 47 | }; |
| 48 | |
| 49 | struct SkMCState { |
| 50 | float matrix[9]; |
| 51 | // NOTE: this only works for non-antialiased clips |
| 52 | int32_t clipRectCount; |
| 53 | ClipRect* clipRects; |
| 54 | }; |
| 55 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 56 | // NOTE: If you add more members, create a new subclass of SkCanvasState with a |
| 57 | // new CanvasState::version. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 58 | struct 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. |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 69 | 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] | 70 | void* pixels; // The pixels, all (height * rowBytes) of them. |
| 71 | } raster; |
| 72 | struct { |
| 73 | int32_t textureID; |
| 74 | } gpu; |
| 75 | }; |
| 76 | }; |
| 77 | |
| 78 | class SkCanvasState { |
| 79 | public: |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 80 | SkCanvasState(int32_t version, SkCanvas* canvas) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 81 | SkASSERT(canvas); |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 82 | this->version = version; |
| 83 | width = canvas->getBaseLayerSize().width(); |
| 84 | height = canvas->getBaseLayerSize().height(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 85 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 88 | /** |
| 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 | |
| 100 | class SkCanvasState_v1 : public SkCanvasState { |
| 101 | public: |
| 102 | static const int32_t kVersion = 1; |
| 103 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 104 | SkCanvasState_v1(SkCanvas* canvas) : INHERITED(kVersion, canvas) { |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 105 | layerCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | layers = nullptr; |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 107 | mcState.clipRectCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 108 | mcState.clipRects = nullptr; |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 109 | originalCanvas = canvas; |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | ~SkCanvasState_v1() { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 113 | // 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 120 | } |
| 121 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 122 | SkMCState mcState; |
| 123 | |
| 124 | int32_t layerCount; |
| 125 | SkCanvasLayerState* layers; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 126 | private: |
| 127 | SkCanvas* originalCanvas; |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 128 | typedef SkCanvasState INHERITED; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | //////////////////////////////////////////////////////////////////////////////// |
| 132 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 133 | static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkIRect& clip) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 134 | // initialize the struct |
| 135 | state->clipRectCount = 0; |
| 136 | |
| 137 | // capture the matrix |
| 138 | for (int i = 0; i < 9; i++) { |
| 139 | state->matrix[i] = matrix.get(i); |
| 140 | } |
| 141 | |
| 142 | /* |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 143 | * We only support a single clipRect, so we take the clip's bounds. Clients have long made |
| 144 | * this assumption anyway, so this restriction is fine. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 145 | */ |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 146 | SkSWriter32<sizeof(ClipRect)> clipWriter; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 147 | |
| 148 | if (!clip.isEmpty()) { |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 149 | state->clipRectCount = 1; |
| 150 | state->clipRects = (ClipRect*)sk_malloc_throw(sizeof(ClipRect)); |
| 151 | state->clipRects->left = clip.fLeft; |
| 152 | state->clipRects->top = clip.fTop; |
| 153 | state->clipRects->right = clip.fRight; |
| 154 | state->clipRects->bottom = clip.fBottom; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 155 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) { |
| 161 | SkASSERT(canvas); |
| 162 | |
| 163 | // Check the clip can be decomposed into rectangles (i.e. no soft clips). |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 164 | if (canvas->androidFramework_isClipAA()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 165 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 168 | std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 169 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 170 | setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getDeviceClipBounds()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * decompose the layers |
| 174 | * |
| 175 | * storage is allocated on the stack for the first 3 layers. It is common in |
| 176 | * some view systems (e.g. Android) that a few non-clipped layers are present |
| 177 | * and we will not need to malloc any additional memory in those cases. |
| 178 | */ |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 179 | SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 180 | int layerCount = 0; |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 181 | for (SkCanvas::LayerIter layer(canvas); !layer.done(); layer.next()) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 182 | |
| 183 | // we currently only work for bitmap backed devices |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 184 | SkPixmap pmap; |
| 185 | if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 186 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | SkCanvasLayerState* layerState = |
| 190 | (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState)); |
| 191 | layerState->type = kRaster_CanvasBackend; |
| 192 | layerState->x = layer.x(); |
| 193 | layerState->y = layer.y(); |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 194 | layerState->width = pmap.width(); |
| 195 | layerState->height = pmap.height(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 196 | |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 197 | switch (pmap.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 198 | case kN32_SkColorType: |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 199 | layerState->raster.config = kARGB_8888_RasterConfig; |
| 200 | break; |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 201 | case kRGB_565_SkColorType: |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 202 | layerState->raster.config = kRGB_565_RasterConfig; |
| 203 | break; |
| 204 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 205 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 206 | } |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 207 | layerState->raster.rowBytes = pmap.rowBytes(); |
| 208 | layerState->raster.pixels = pmap.writable_addr(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 209 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 210 | setup_MC_state(&layerState->mcState, layer.matrix(), layer.clipBounds()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 211 | layerCount++; |
| 212 | } |
| 213 | |
| 214 | // 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] | 215 | SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerState)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 216 | canvasState->layerCount = layerCount; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 217 | canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 218 | layerWriter.flatten(canvasState->layers); |
| 219 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 220 | return canvasState.release(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | //////////////////////////////////////////////////////////////////////////////// |
| 224 | |
| 225 | static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { |
| 226 | // reconstruct the matrix |
| 227 | SkMatrix matrix; |
| 228 | for (int i = 0; i < 9; i++) { |
| 229 | matrix.set(i, state.matrix[i]); |
| 230 | } |
| 231 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 232 | // only realy support 1 rect, so if the caller (legacy?) sent us more, we just take the bounds |
| 233 | // of what they sent. |
| 234 | SkIRect bounds = SkIRect::MakeEmpty(); |
| 235 | if (state.clipRectCount > 0) { |
| 236 | bounds.set(state.clipRects[0].left, |
| 237 | state.clipRects[0].top, |
| 238 | state.clipRects[0].right, |
| 239 | state.clipRects[0].bottom); |
| 240 | for (int i = 1; i < state.clipRectCount; ++i) { |
| 241 | bounds.join(state.clipRects[i].left, |
| 242 | state.clipRects[i].top, |
| 243 | state.clipRects[i].right, |
| 244 | state.clipRects[i].bottom); |
| 245 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 248 | canvas->clipRect(SkRect::Make(bounds)); |
| 249 | canvas->concat(matrix); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 252 | static std::unique_ptr<SkCanvas> |
| 253 | make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 254 | SkASSERT(kRaster_CanvasBackend == layerState.type); |
| 255 | |
| 256 | SkBitmap bitmap; |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 257 | SkColorType colorType = |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 258 | layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType : |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 259 | layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType : |
| 260 | kUnknown_SkColorType; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 261 | |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 262 | if (colorType == kUnknown_SkColorType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 263 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 264 | } |
| 265 | |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 266 | bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height, |
| 267 | colorType, kPremul_SkAlphaType), |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 268 | layerState.raster.pixels, (size_t) layerState.raster.rowBytes); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 269 | |
| 270 | SkASSERT(!bitmap.empty()); |
| 271 | SkASSERT(!bitmap.isNull()); |
| 272 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 273 | std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 274 | |
| 275 | // setup the matrix and clip |
| 276 | setup_canvas_from_MC_state(layerState.mcState, canvas.get()); |
| 277 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 278 | return canvas; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 281 | std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 282 | SkASSERT(state); |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 283 | // Currently there is only one possible version. |
| 284 | SkASSERT(SkCanvasState_v1::kVersion == state->version); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 285 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 286 | const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 287 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 288 | if (state_v1->layerCount < 1) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 289 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 292 | std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 293 | |
| 294 | // setup the matrix and clip on the n-way canvas |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 295 | setup_canvas_from_MC_state(state_v1->mcState, canvas.get()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 296 | |
| 297 | // Iterate over the layers and add them to the n-way canvas |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 298 | for (int i = state_v1->layerCount - 1; i >= 0; --i) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 299 | std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 300 | if (!canvasLayer.get()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 301 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 302 | } |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 303 | canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x, |
| 304 | state_v1->layers[i].y)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 307 | return std::move(canvas); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | //////////////////////////////////////////////////////////////////////////////// |
| 311 | |
| 312 | void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 313 | SkASSERT(!state || SkCanvasState_v1::kVersion == state->version); |
| 314 | // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on |
| 315 | // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and |
| 316 | // instead uses the field "version" to determine how to behave. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 317 | delete static_cast<SkCanvasState_v1*>(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 318 | } |