Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCanvasStateUtils.h" | 8 #include "SkCanvasStateUtils.h" |
| 9 | 9 |
| 10 #include "SkBitmapDevice.h" | 10 #include "SkBitmapDevice.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 struct { | 66 struct { |
| 67 int32_t textureID; | 67 int32_t textureID; |
| 68 } gpu; | 68 } gpu; |
| 69 }; | 69 }; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 class SkCanvasState { | 72 class SkCanvasState { |
| 73 public: | 73 public: |
| 74 SkCanvasState(SkCanvas* canvas) { | 74 SkCanvasState(SkCanvas* canvas) { |
| 75 SkASSERT(canvas); | 75 SkASSERT(canvas); |
| 76 version = CANVAS_STATE_VERSION; | 76 version = CANVAS_STATE_VERSION; |
|
mtklein
2014/07/07 22:09:16
Might want to have the derived class pass in versi
scroggo
2014/07/08 18:45:13
Done.
| |
| 77 width = canvas->getDeviceSize().width(); | 77 width = canvas->getBaseLayerSize().width(); |
| 78 height = canvas->getDeviceSize().height(); | 78 height = canvas->getBaseLayerSize().height(); |
| 79 | |
| 80 } | |
| 81 | |
| 82 /** | |
| 83 * The version this struct was built with. This field must always appear | |
| 84 * first in the struct so that when the versions don't match (and the | |
| 85 * remaining contents and size are potentially different) we can still | |
| 86 * compare the version numbers. | |
| 87 */ | |
| 88 int32_t version; | |
| 89 int32_t width; | |
| 90 int32_t height; | |
| 91 int32_t alignmentPadding; | |
| 92 }; | |
| 93 | |
| 94 class SkCanvasState_v1 : public SkCanvasState { | |
| 95 public: | |
| 96 SkCanvasState_v1(SkCanvas* canvas) | |
| 97 : INHERITED(canvas) | |
| 98 { | |
| 99 SkASSERT(this->version == 1); | |
| 79 layerCount = 0; | 100 layerCount = 0; |
| 80 layers = NULL; | 101 layers = NULL; |
| 81 originalCanvas = SkRef(canvas); | |
| 82 | |
| 83 mcState.clipRectCount = 0; | 102 mcState.clipRectCount = 0; |
| 84 mcState.clipRects = NULL; | 103 mcState.clipRects = NULL; |
| 104 originalCanvas = SkRef(canvas); | |
| 85 } | 105 } |
| 86 | 106 |
| 87 ~SkCanvasState() { | 107 ~SkCanvasState_v1() { |
| 88 // loop through the layers and free the data allocated to the clipRects | 108 // loop through the layers and free the data allocated to the clipRects |
| 89 for (int i = 0; i < layerCount; ++i) { | 109 for (int i = 0; i < layerCount; ++i) { |
| 90 sk_free(layers[i].mcState.clipRects); | 110 sk_free(layers[i].mcState.clipRects); |
| 91 } | 111 } |
| 92 | 112 |
| 93 sk_free(mcState.clipRects); | 113 sk_free(mcState.clipRects); |
| 94 sk_free(layers); | 114 sk_free(layers); |
| 95 | 115 |
| 96 // it is now safe to free the canvas since there should be no remaining | 116 // it is now safe to free the canvas since there should be no remaining |
| 97 // references to the content that is referenced by this canvas (e.g. pix els) | 117 // references to the content that is referenced by this canvas (e.g. pix els) |
| 98 originalCanvas->unref(); | 118 originalCanvas->unref(); |
| 99 } | 119 } |
| 100 | 120 |
| 101 /** | |
| 102 * The version this struct was built with. This field must always appear | |
| 103 * first in the struct so that when the versions don't match (and the | |
| 104 * remaining contents and size are potentially different) we can still | |
| 105 * compare the version numbers. | |
| 106 */ | |
| 107 int32_t version; | |
| 108 | |
| 109 int32_t width; | |
| 110 int32_t height; | |
| 111 | |
| 112 SkMCState mcState; | 121 SkMCState mcState; |
| 113 | 122 |
| 114 int32_t layerCount; | 123 int32_t layerCount; |
| 115 SkCanvasLayerState* layers; | 124 SkCanvasLayerState* layers; |
| 116 | |
| 117 private: | 125 private: |
| 118 SkCanvas* originalCanvas; | 126 SkCanvas* originalCanvas; |
| 127 typedef SkCanvasState INHERITED; | |
| 119 }; | 128 }; |
| 120 | 129 |
| 121 //////////////////////////////////////////////////////////////////////////////// | 130 //////////////////////////////////////////////////////////////////////////////// |
| 122 | 131 |
| 123 class ClipValidator : public SkCanvas::ClipVisitor { | 132 class ClipValidator : public SkCanvas::ClipVisitor { |
| 124 public: | 133 public: |
| 125 ClipValidator() : fFailed(false) {} | 134 ClipValidator() : fFailed(false) {} |
| 126 bool failed() { return fFailed; } | 135 bool failed() { return fFailed; } |
| 127 | 136 |
| 128 // ClipVisitor | 137 // ClipVisitor |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 | 193 |
| 185 // Check the clip can be decomposed into rectangles (i.e. no soft clips). | 194 // Check the clip can be decomposed into rectangles (i.e. no soft clips). |
| 186 ClipValidator validator; | 195 ClipValidator validator; |
| 187 canvas->replayClips(&validator); | 196 canvas->replayClips(&validator); |
| 188 if (validator.failed()) { | 197 if (validator.failed()) { |
| 189 SkErrorInternals::SetError(kInvalidOperation_SkError, | 198 SkErrorInternals::SetError(kInvalidOperation_SkError, |
| 190 "CaptureCanvasState does not support canvases with antialiased c lips.\n"); | 199 "CaptureCanvasState does not support canvases with antialiased c lips.\n"); |
| 191 return NULL; | 200 return NULL; |
| 192 } | 201 } |
| 193 | 202 |
| 194 SkAutoTDelete<SkCanvasState> canvasState(SkNEW_ARGS(SkCanvasState, (canvas)) ); | 203 SkAutoTDelete<SkCanvasState_v1> canvasState(SkNEW_ARGS(SkCanvasState_v1, (ca nvas))); |
| 195 | 204 |
| 196 // decompose the total matrix and clip | 205 // decompose the total matrix and clip |
| 197 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), | 206 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), |
| 198 canvas->internal_private_getTotalClip()); | 207 canvas->internal_private_getTotalClip()); |
| 199 | 208 |
| 200 /* | 209 /* |
| 201 * decompose the layers | 210 * decompose the layers |
| 202 * | 211 * |
| 203 * storage is allocated on the stack for the first 3 layers. It is common in | 212 * storage is allocated on the stack for the first 3 layers. It is common in |
| 204 * some view systems (e.g. Android) that a few non-clipped layers are presen t | 213 * some view systems (e.g. Android) that a few non-clipped layers are presen t |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 } | 249 } |
| 241 | 250 |
| 242 // allocate memory for the layers and then and copy them to the struct | 251 // allocate memory for the layers and then and copy them to the struct |
| 243 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerStat e)); | 252 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerStat e)); |
| 244 canvasState->layerCount = layerCount; | 253 canvasState->layerCount = layerCount; |
| 245 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.byte sWritten()); | 254 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.byte sWritten()); |
| 246 layerWriter.flatten(canvasState->layers); | 255 layerWriter.flatten(canvasState->layers); |
| 247 | 256 |
| 248 // for now, just ignore any client supplied DrawFilter. | 257 // for now, just ignore any client supplied DrawFilter. |
| 249 if (canvas->getDrawFilter()) { | 258 if (canvas->getDrawFilter()) { |
| 250 // SkDEBUGF(("CaptureCanvasState will ignore the canvases draw filter.\n" )); | 259 // SkDEBUGF(("CaptureCanvasState will ignore the canvas's draw filter.\n" )); |
| 251 } | 260 } |
| 252 | 261 |
| 253 return canvasState.detach(); | 262 return canvasState.detach(); |
| 254 } | 263 } |
| 255 | 264 |
| 256 //////////////////////////////////////////////////////////////////////////////// | 265 //////////////////////////////////////////////////////////////////////////////// |
| 257 | 266 |
| 258 static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { | 267 static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { |
| 259 // reconstruct the matrix | 268 // reconstruct the matrix |
| 260 SkMatrix matrix; | 269 SkMatrix matrix; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 SkAutoTUnref<SkCanvas> canvas(SkNEW_ARGS(SkCanvas, (bitmap))); | 308 SkAutoTUnref<SkCanvas> canvas(SkNEW_ARGS(SkCanvas, (bitmap))); |
| 300 | 309 |
| 301 // setup the matrix and clip | 310 // setup the matrix and clip |
| 302 setup_canvas_from_MC_state(layerState.mcState, canvas.get()); | 311 setup_canvas_from_MC_state(layerState.mcState, canvas.get()); |
| 303 | 312 |
| 304 return canvas.detach(); | 313 return canvas.detach(); |
| 305 } | 314 } |
| 306 | 315 |
| 307 SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) { | 316 SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) { |
| 308 SkASSERT(state); | 317 SkASSERT(state); |
| 318 SkASSERT(CANVAS_STATE_VERSION == state->version); | |
|
scroggo
2014/07/07 21:44:02
Since CreateFromCanvasState will always be called
mtklein
2014/07/07 22:09:16
It can't hurt to get this function in the state it
| |
| 309 | 319 |
| 310 // check that the versions match | 320 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(stat e); |
| 311 if (CANVAS_STATE_VERSION != state->version) { | |
| 312 SkDebugf("CreateFromCanvasState version does not match the one use to cr eate the input"); | |
| 313 return NULL; | |
| 314 } | |
| 315 | 321 |
| 316 if (state->layerCount < 1) { | 322 if (state_v1->layerCount < 1) { |
| 317 return NULL; | 323 return NULL; |
| 318 } | 324 } |
| 319 | 325 |
| 320 SkAutoTUnref<SkCanvasStack> canvas(SkNEW_ARGS(SkCanvasStack, (state->width, state->height))); | 326 SkAutoTUnref<SkCanvasStack> canvas(SkNEW_ARGS(SkCanvasStack, (state->width, state->height))); |
| 321 | 327 |
| 322 // setup the matrix and clip on the n-way canvas | 328 // setup the matrix and clip on the n-way canvas |
| 323 setup_canvas_from_MC_state(state->mcState, canvas); | 329 setup_canvas_from_MC_state(state_v1->mcState, canvas); |
| 324 | 330 |
| 325 // Iterate over the layers and add them to the n-way canvas | 331 // Iterate over the layers and add them to the n-way canvas |
| 326 for (int i = state->layerCount - 1; i >= 0; --i) { | 332 for (int i = state_v1->layerCount - 1; i >= 0; --i) { |
| 327 SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state ->layers[i])); | 333 SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state _v1->layers[i])); |
| 328 if (!canvasLayer.get()) { | 334 if (!canvasLayer.get()) { |
| 329 return NULL; | 335 return NULL; |
| 330 } | 336 } |
| 331 canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state->layers[i].x, | 337 canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state_v1->layers[i] .x, |
| 332 state->layers[i].y) ); | 338 state_v1->layers[i] .y)); |
| 333 } | 339 } |
| 334 | 340 |
| 335 return canvas.detach(); | 341 return canvas.detach(); |
| 336 } | 342 } |
| 337 | 343 |
| 338 //////////////////////////////////////////////////////////////////////////////// | 344 //////////////////////////////////////////////////////////////////////////////// |
| 339 | 345 |
| 340 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { | 346 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { |
| 341 SkDELETE(state); | 347 SkASSERT(CANVAS_STATE_VERSION == state->version); |
| 348 // Upcast to the correct version of SkCanvasState. This avoids having a virt ual destructor on | |
| 349 // SkCanvasState. That would be strange since SkCanvasState has no other vir tual functions, and | |
| 350 // instead uses the field "version" to determine how to behave. | |
| 351 SkDELETE(static_cast<SkCanvasState_v1*>(state)); | |
| 342 } | 352 } |
| OLD | NEW |