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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/utils/SkCanvasStateUtils.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 9 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/core/SkDevice.h" |
| 13 | #include "src/core/SkRasterClip.h" |
| 14 | #include "src/core/SkWriter32.h" |
| 15 | #include "src/utils/SkCanvasStack.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() { |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 113 | // loop through the layers and free the data allocated to the clipRects. |
| 114 | // See setup_MC_state, clipRects is only allocated when the clip isn't empty; and empty |
| 115 | // is implicitly represented as clipRectCount == 0. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 116 | for (int i = 0; i < layerCount; ++i) { |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 117 | if (layers[i].mcState.clipRectCount > 0) { |
| 118 | sk_free(layers[i].mcState.clipRects); |
| 119 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 122 | if (mcState.clipRectCount > 0) { |
| 123 | sk_free(mcState.clipRects); |
| 124 | } |
| 125 | |
| 126 | // layers is always allocated, even if it's with sk_malloc(0), so this is safe. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 127 | sk_free(layers); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 128 | } |
| 129 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 130 | SkMCState mcState; |
| 131 | |
| 132 | int32_t layerCount; |
| 133 | SkCanvasLayerState* layers; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 134 | private: |
| 135 | SkCanvas* originalCanvas; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 136 | using INHERITED = SkCanvasState; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | //////////////////////////////////////////////////////////////////////////////// |
| 140 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 141 | 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] | 142 | // initialize the struct |
| 143 | state->clipRectCount = 0; |
| 144 | |
| 145 | // capture the matrix |
| 146 | for (int i = 0; i < 9; i++) { |
| 147 | state->matrix[i] = matrix.get(i); |
| 148 | } |
| 149 | |
| 150 | /* |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 151 | * We only support a single clipRect, so we take the clip's bounds. Clients have long made |
| 152 | * this assumption anyway, so this restriction is fine. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 153 | */ |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 154 | SkSWriter32<sizeof(ClipRect)> clipWriter; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 155 | |
| 156 | if (!clip.isEmpty()) { |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 157 | state->clipRectCount = 1; |
| 158 | state->clipRects = (ClipRect*)sk_malloc_throw(sizeof(ClipRect)); |
| 159 | state->clipRects->left = clip.fLeft; |
| 160 | state->clipRects->top = clip.fTop; |
| 161 | state->clipRects->right = clip.fRight; |
| 162 | state->clipRects->bottom = clip.fBottom; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 163 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 164 | } |
| 165 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 166 | SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) { |
| 167 | SkASSERT(canvas); |
| 168 | |
| 169 | // 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] | 170 | if (canvas->androidFramework_isClipAA()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 171 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 174 | std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 175 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 176 | setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getDeviceClipBounds()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 177 | |
Michael Ludwig | f326e4f | 2020-12-04 13:00:43 -0500 | [diff] [blame] | 178 | // Historically, the canvas state could report multiple top-level layers because SkCanvas |
Mike Reed | ed4108e | 2020-12-17 10:32:24 -0500 | [diff] [blame] | 179 | // supported unclipped layers. With that feature removed, all required information is contained |
| 180 | // by the canvas' top-most device. |
| 181 | SkBaseDevice* device = canvas->topDevice(); |
| 182 | SkASSERT(device); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 183 | |
Michael Ludwig | f326e4f | 2020-12-04 13:00:43 -0500 | [diff] [blame] | 184 | SkSWriter32<sizeof(SkCanvasLayerState)> layerWriter; |
| 185 | // we currently only work for bitmap backed devices |
| 186 | SkPixmap pmap; |
| 187 | if (!device->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) { |
| 188 | return nullptr; |
| 189 | } |
| 190 | // and for axis-aligned devices (so not transformed for an image filter) |
| 191 | if (!device->isPixelAlignedToGlobal()) { |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
| 195 | SkIPoint origin = device->getOrigin(); // safe since it's pixel aligned |
| 196 | |
| 197 | SkCanvasLayerState* layerState = |
| 198 | (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState)); |
| 199 | layerState->type = kRaster_CanvasBackend; |
| 200 | layerState->x = origin.x(); |
| 201 | layerState->y = origin.y(); |
| 202 | layerState->width = pmap.width(); |
| 203 | layerState->height = pmap.height(); |
| 204 | |
| 205 | switch (pmap.colorType()) { |
| 206 | case kN32_SkColorType: |
| 207 | layerState->raster.config = kARGB_8888_RasterConfig; |
| 208 | break; |
| 209 | case kRGB_565_SkColorType: |
| 210 | layerState->raster.config = kRGB_565_RasterConfig; |
| 211 | break; |
| 212 | default: |
| 213 | return nullptr; |
| 214 | } |
| 215 | layerState->raster.rowBytes = pmap.rowBytes(); |
| 216 | layerState->raster.pixels = pmap.writable_addr(); |
| 217 | |
| 218 | setup_MC_state(&layerState->mcState, device->localToDevice(), device->devClipBounds()); |
| 219 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 220 | // allocate memory for the layers and then and copy them to the struct |
Michael Ludwig | f326e4f | 2020-12-04 13:00:43 -0500 | [diff] [blame] | 221 | SkASSERT(layerWriter.bytesWritten() == sizeof(SkCanvasLayerState)); |
| 222 | canvasState->layerCount = 1; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 223 | canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 224 | layerWriter.flatten(canvasState->layers); |
| 225 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 226 | return canvasState.release(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | //////////////////////////////////////////////////////////////////////////////// |
| 230 | |
| 231 | static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { |
| 232 | // reconstruct the matrix |
| 233 | SkMatrix matrix; |
| 234 | for (int i = 0; i < 9; i++) { |
| 235 | matrix.set(i, state.matrix[i]); |
| 236 | } |
| 237 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 238 | // only realy support 1 rect, so if the caller (legacy?) sent us more, we just take the bounds |
| 239 | // of what they sent. |
| 240 | SkIRect bounds = SkIRect::MakeEmpty(); |
| 241 | if (state.clipRectCount > 0) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 242 | bounds.setLTRB(state.clipRects[0].left, |
| 243 | state.clipRects[0].top, |
| 244 | state.clipRects[0].right, |
| 245 | state.clipRects[0].bottom); |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 246 | for (int i = 1; i < state.clipRectCount; ++i) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 247 | bounds.join({state.clipRects[i].left, |
| 248 | state.clipRects[i].top, |
| 249 | state.clipRects[i].right, |
| 250 | state.clipRects[i].bottom}); |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 251 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 254 | canvas->clipRect(SkRect::Make(bounds)); |
| 255 | canvas->concat(matrix); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 258 | static std::unique_ptr<SkCanvas> |
| 259 | make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 260 | SkASSERT(kRaster_CanvasBackend == layerState.type); |
| 261 | |
| 262 | SkBitmap bitmap; |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 263 | SkColorType colorType = |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 264 | layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType : |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 265 | layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType : |
| 266 | kUnknown_SkColorType; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 267 | |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 268 | if (colorType == kUnknown_SkColorType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 269 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 270 | } |
| 271 | |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 272 | bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height, |
| 273 | colorType, kPremul_SkAlphaType), |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 274 | layerState.raster.pixels, (size_t) layerState.raster.rowBytes); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 275 | |
| 276 | SkASSERT(!bitmap.empty()); |
| 277 | SkASSERT(!bitmap.isNull()); |
| 278 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 279 | std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 280 | |
| 281 | // setup the matrix and clip |
| 282 | setup_canvas_from_MC_state(layerState.mcState, canvas.get()); |
| 283 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 284 | return canvas; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 287 | std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 288 | SkASSERT(state); |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 289 | // Currently there is only one possible version. |
| 290 | SkASSERT(SkCanvasState_v1::kVersion == state->version); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 291 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 292 | const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 293 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 294 | if (state_v1->layerCount < 1) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 295 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 298 | std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 299 | |
| 300 | // setup the matrix and clip on the n-way canvas |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 301 | setup_canvas_from_MC_state(state_v1->mcState, canvas.get()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 302 | |
Michael Ludwig | f326e4f | 2020-12-04 13:00:43 -0500 | [diff] [blame] | 303 | // Iterate over the layers and add them to the n-way canvas. New clients will only send one |
| 304 | // layer since unclipped layers are no longer supported, but old canvas clients may still |
| 305 | // create them. |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 306 | for (int i = state_v1->layerCount - 1; i >= 0; --i) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 307 | std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 308 | if (!canvasLayer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 309 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 310 | } |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 311 | canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x, |
| 312 | state_v1->layers[i].y)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Mike Klein | a9609ea | 2020-02-18 13:33:23 -0600 | [diff] [blame] | 315 | return std::move(canvas); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | //////////////////////////////////////////////////////////////////////////////// |
| 319 | |
| 320 | void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 321 | SkASSERT(!state || SkCanvasState_v1::kVersion == state->version); |
| 322 | // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on |
| 323 | // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and |
| 324 | // instead uses the field "version" to determine how to behave. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 325 | delete static_cast<SkCanvasState_v1*>(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 326 | } |