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" |
| 12 | #include "src/core/SkClipOpPriv.h" |
| 13 | #include "src/core/SkDevice.h" |
| 14 | #include "src/core/SkRasterClip.h" |
| 15 | #include "src/core/SkWriter32.h" |
| 16 | #include "src/utils/SkCanvasStack.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 17 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 18 | /* |
| 19 | * WARNING: The structs below are part of a stable ABI and as such we explicitly |
| 20 | * use unambigious primitives (e.g. int32_t instead of an enum). |
| 21 | * |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 22 | * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN A NEW |
| 23 | * NEW SUBCLASS OF SkCanvasState. SUCH CHANGES SHOULD ONLY BE MADE IF ABSOLUTELY |
| 24 | * NECESSARY! |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 25 | * |
| 26 | * In order to test changes, run the CanvasState tests. gyp/canvas_state_lib.gyp |
| 27 | * describes how to create a library to pass to the CanvasState tests. The tests |
| 28 | * should succeed when building the library with your changes and passing that to |
| 29 | * the tests running in the unchanged Skia. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 30 | */ |
| 31 | enum RasterConfigs { |
| 32 | kUnknown_RasterConfig = 0, |
| 33 | kRGB_565_RasterConfig = 1, |
| 34 | kARGB_8888_RasterConfig = 2 |
| 35 | }; |
| 36 | typedef int32_t RasterConfig; |
| 37 | |
| 38 | enum CanvasBackends { |
| 39 | kUnknown_CanvasBackend = 0, |
| 40 | kRaster_CanvasBackend = 1, |
| 41 | kGPU_CanvasBackend = 2, |
| 42 | kPDF_CanvasBackend = 3 |
| 43 | }; |
| 44 | typedef int32_t CanvasBackend; |
| 45 | |
| 46 | struct ClipRect { |
| 47 | int32_t left, top, right, bottom; |
| 48 | }; |
| 49 | |
| 50 | struct SkMCState { |
| 51 | float matrix[9]; |
| 52 | // NOTE: this only works for non-antialiased clips |
| 53 | int32_t clipRectCount; |
| 54 | ClipRect* clipRects; |
| 55 | }; |
| 56 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 57 | // NOTE: If you add more members, create a new subclass of SkCanvasState with a |
| 58 | // new CanvasState::version. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 59 | struct SkCanvasLayerState { |
| 60 | CanvasBackend type; |
| 61 | int32_t x, y; |
| 62 | int32_t width; |
| 63 | int32_t height; |
| 64 | |
| 65 | SkMCState mcState; |
| 66 | |
| 67 | union { |
| 68 | struct { |
| 69 | RasterConfig config; // pixel format: a value from RasterConfigs. |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 70 | 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] | 71 | void* pixels; // The pixels, all (height * rowBytes) of them. |
| 72 | } raster; |
| 73 | struct { |
| 74 | int32_t textureID; |
| 75 | } gpu; |
| 76 | }; |
| 77 | }; |
| 78 | |
| 79 | class SkCanvasState { |
| 80 | public: |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 81 | SkCanvasState(int32_t version, SkCanvas* canvas) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 82 | SkASSERT(canvas); |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 83 | this->version = version; |
| 84 | width = canvas->getBaseLayerSize().width(); |
| 85 | height = canvas->getBaseLayerSize().height(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 86 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 87 | } |
| 88 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 89 | /** |
| 90 | * The version this struct was built with. This field must always appear |
| 91 | * first in the struct so that when the versions don't match (and the |
| 92 | * remaining contents and size are potentially different) we can still |
| 93 | * compare the version numbers. |
| 94 | */ |
| 95 | int32_t version; |
| 96 | int32_t width; |
| 97 | int32_t height; |
| 98 | int32_t alignmentPadding; |
| 99 | }; |
| 100 | |
| 101 | class SkCanvasState_v1 : public SkCanvasState { |
| 102 | public: |
| 103 | static const int32_t kVersion = 1; |
| 104 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 105 | SkCanvasState_v1(SkCanvas* canvas) : INHERITED(kVersion, canvas) { |
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; |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 110 | originalCanvas = canvas; |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | ~SkCanvasState_v1() { |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 114 | // loop through the layers and free the data allocated to the clipRects. |
| 115 | // See setup_MC_state, clipRects is only allocated when the clip isn't empty; and empty |
| 116 | // is implicitly represented as clipRectCount == 0. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 117 | for (int i = 0; i < layerCount; ++i) { |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 118 | if (layers[i].mcState.clipRectCount > 0) { |
| 119 | sk_free(layers[i].mcState.clipRects); |
| 120 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 123 | if (mcState.clipRectCount > 0) { |
| 124 | sk_free(mcState.clipRects); |
| 125 | } |
| 126 | |
| 127 | // 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] | 128 | sk_free(layers); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 129 | } |
| 130 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 131 | SkMCState mcState; |
| 132 | |
| 133 | int32_t layerCount; |
| 134 | SkCanvasLayerState* layers; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 135 | private: |
| 136 | SkCanvas* originalCanvas; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 137 | using INHERITED = SkCanvasState; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | //////////////////////////////////////////////////////////////////////////////// |
| 141 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 142 | 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] | 143 | // initialize the struct |
| 144 | state->clipRectCount = 0; |
| 145 | |
| 146 | // capture the matrix |
| 147 | for (int i = 0; i < 9; i++) { |
| 148 | state->matrix[i] = matrix.get(i); |
| 149 | } |
| 150 | |
| 151 | /* |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 152 | * We only support a single clipRect, so we take the clip's bounds. Clients have long made |
| 153 | * this assumption anyway, so this restriction is fine. |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 154 | */ |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 155 | SkSWriter32<sizeof(ClipRect)> clipWriter; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 156 | |
| 157 | if (!clip.isEmpty()) { |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 158 | state->clipRectCount = 1; |
| 159 | state->clipRects = (ClipRect*)sk_malloc_throw(sizeof(ClipRect)); |
| 160 | state->clipRects->left = clip.fLeft; |
| 161 | state->clipRects->top = clip.fTop; |
| 162 | state->clipRects->right = clip.fRight; |
| 163 | state->clipRects->bottom = clip.fBottom; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 164 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 165 | } |
| 166 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 167 | SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) { |
| 168 | SkASSERT(canvas); |
| 169 | |
| 170 | // 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] | 171 | if (canvas->androidFramework_isClipAA()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 175 | std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 176 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 177 | setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getDeviceClipBounds()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 178 | |
Michael Ludwig | f326e4f | 2020-12-04 13:00:43 -0500 | [diff] [blame] | 179 | // Historically, the canvas state could report multiple top-level layers because SkCanvas |
Mike Reed | ed4108e | 2020-12-17 10:32:24 -0500 | [diff] [blame] | 180 | // supported unclipped layers. With that feature removed, all required information is contained |
| 181 | // by the canvas' top-most device. |
| 182 | SkBaseDevice* device = canvas->topDevice(); |
| 183 | SkASSERT(device); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 184 | |
Michael Ludwig | f326e4f | 2020-12-04 13:00:43 -0500 | [diff] [blame] | 185 | SkSWriter32<sizeof(SkCanvasLayerState)> layerWriter; |
| 186 | // we currently only work for bitmap backed devices |
| 187 | SkPixmap pmap; |
| 188 | if (!device->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) { |
| 189 | return nullptr; |
| 190 | } |
| 191 | // and for axis-aligned devices (so not transformed for an image filter) |
| 192 | if (!device->isPixelAlignedToGlobal()) { |
| 193 | return nullptr; |
| 194 | } |
| 195 | |
| 196 | SkIPoint origin = device->getOrigin(); // safe since it's pixel aligned |
| 197 | |
| 198 | SkCanvasLayerState* layerState = |
| 199 | (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState)); |
| 200 | layerState->type = kRaster_CanvasBackend; |
| 201 | layerState->x = origin.x(); |
| 202 | layerState->y = origin.y(); |
| 203 | layerState->width = pmap.width(); |
| 204 | layerState->height = pmap.height(); |
| 205 | |
| 206 | switch (pmap.colorType()) { |
| 207 | case kN32_SkColorType: |
| 208 | layerState->raster.config = kARGB_8888_RasterConfig; |
| 209 | break; |
| 210 | case kRGB_565_SkColorType: |
| 211 | layerState->raster.config = kRGB_565_RasterConfig; |
| 212 | break; |
| 213 | default: |
| 214 | return nullptr; |
| 215 | } |
| 216 | layerState->raster.rowBytes = pmap.rowBytes(); |
| 217 | layerState->raster.pixels = pmap.writable_addr(); |
| 218 | |
| 219 | setup_MC_state(&layerState->mcState, device->localToDevice(), device->devClipBounds()); |
| 220 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 221 | // 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] | 222 | SkASSERT(layerWriter.bytesWritten() == sizeof(SkCanvasLayerState)); |
| 223 | canvasState->layerCount = 1; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 224 | canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 225 | layerWriter.flatten(canvasState->layers); |
| 226 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 227 | return canvasState.release(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | //////////////////////////////////////////////////////////////////////////////// |
| 231 | |
| 232 | static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { |
| 233 | // reconstruct the matrix |
| 234 | SkMatrix matrix; |
| 235 | for (int i = 0; i < 9; i++) { |
| 236 | matrix.set(i, state.matrix[i]); |
| 237 | } |
| 238 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 239 | // only realy support 1 rect, so if the caller (legacy?) sent us more, we just take the bounds |
| 240 | // of what they sent. |
| 241 | SkIRect bounds = SkIRect::MakeEmpty(); |
| 242 | if (state.clipRectCount > 0) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 243 | bounds.setLTRB(state.clipRects[0].left, |
| 244 | state.clipRects[0].top, |
| 245 | state.clipRects[0].right, |
| 246 | state.clipRects[0].bottom); |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 247 | for (int i = 1; i < state.clipRectCount; ++i) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 248 | bounds.join({state.clipRects[i].left, |
| 249 | state.clipRects[i].top, |
| 250 | state.clipRects[i].right, |
| 251 | state.clipRects[i].bottom}); |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 252 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 255 | canvas->clipRect(SkRect::Make(bounds)); |
| 256 | canvas->concat(matrix); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 259 | static std::unique_ptr<SkCanvas> |
| 260 | make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 261 | SkASSERT(kRaster_CanvasBackend == layerState.type); |
| 262 | |
| 263 | SkBitmap bitmap; |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 264 | SkColorType colorType = |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 265 | layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType : |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 266 | layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType : |
| 267 | kUnknown_SkColorType; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 268 | |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 269 | if (colorType == kUnknown_SkColorType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 270 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 271 | } |
| 272 | |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 273 | bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height, |
| 274 | colorType, kPremul_SkAlphaType), |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 275 | layerState.raster.pixels, (size_t) layerState.raster.rowBytes); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 276 | |
| 277 | SkASSERT(!bitmap.empty()); |
| 278 | SkASSERT(!bitmap.isNull()); |
| 279 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 280 | std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 281 | |
| 282 | // setup the matrix and clip |
| 283 | setup_canvas_from_MC_state(layerState.mcState, canvas.get()); |
| 284 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 285 | return canvas; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 288 | std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 289 | SkASSERT(state); |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 290 | // Currently there is only one possible version. |
| 291 | SkASSERT(SkCanvasState_v1::kVersion == state->version); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 292 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 293 | const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 294 | |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 295 | if (state_v1->layerCount < 1) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 296 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 299 | std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 300 | |
| 301 | // setup the matrix and clip on the n-way canvas |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 302 | setup_canvas_from_MC_state(state_v1->mcState, canvas.get()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 303 | |
Michael Ludwig | f326e4f | 2020-12-04 13:00:43 -0500 | [diff] [blame] | 304 | // Iterate over the layers and add them to the n-way canvas. New clients will only send one |
| 305 | // layer since unclipped layers are no longer supported, but old canvas clients may still |
| 306 | // create them. |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 307 | for (int i = state_v1->layerCount - 1; i >= 0; --i) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 308 | 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] | 309 | if (!canvasLayer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 310 | return nullptr; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 311 | } |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 312 | canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x, |
| 313 | state_v1->layers[i].y)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Mike Klein | a9609ea | 2020-02-18 13:33:23 -0600 | [diff] [blame] | 316 | return std::move(canvas); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | //////////////////////////////////////////////////////////////////////////////// |
| 320 | |
| 321 | void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { |
scroggo | 352c218 | 2014-07-15 12:34:26 -0700 | [diff] [blame] | 322 | SkASSERT(!state || SkCanvasState_v1::kVersion == state->version); |
| 323 | // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on |
| 324 | // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and |
| 325 | // instead uses the field "version" to determine how to behave. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 326 | delete static_cast<SkCanvasState_v1*>(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 327 | } |