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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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; |
77 width = canvas->getDeviceSize().width(); | 77 width = canvas->getBaseLayerSize().width(); |
78 height = canvas->getDeviceSize().height(); | 78 height = canvas->getBaseLayerSize().height(); |
79 | |
80 } | |
81 | |
82 virtual ~SkCanvasState() {} | |
83 | |
84 /** | |
85 * The version this struct was built with. This field must always appear | |
86 * first in the struct so that when the versions don't match (and the | |
87 * remaining contents and size are potentially different) we can still | |
88 * compare the version numbers. | |
89 */ | |
90 int32_t version; | |
91 int32_t width; | |
92 int32_t height; | |
93 int32_t alignmentPadding; | |
94 }; | |
95 | |
96 class SkCanvasState_v1 : public SkCanvasState { | |
97 public: | |
98 SkCanvasState_v1(SkCanvas* canvas) | |
99 : INHERITED(canvas) | |
100 { | |
101 SkASSERT(this->version == 1); | |
79 layerCount = 0; | 102 layerCount = 0; |
80 layers = NULL; | 103 layers = NULL; |
81 originalCanvas = SkRef(canvas); | |
82 | |
83 mcState.clipRectCount = 0; | 104 mcState.clipRectCount = 0; |
84 mcState.clipRects = NULL; | 105 mcState.clipRects = NULL; |
106 originalCanvas = SkRef(canvas); | |
85 } | 107 } |
86 | 108 |
87 ~SkCanvasState() { | 109 ~SkCanvasState_v1() { |
88 // loop through the layers and free the data allocated to the clipRects | 110 // loop through the layers and free the data allocated to the clipRects |
89 for (int i = 0; i < layerCount; ++i) { | 111 for (int i = 0; i < layerCount; ++i) { |
90 sk_free(layers[i].mcState.clipRects); | 112 sk_free(layers[i].mcState.clipRects); |
91 } | 113 } |
92 | 114 |
93 sk_free(mcState.clipRects); | 115 sk_free(mcState.clipRects); |
94 sk_free(layers); | 116 sk_free(layers); |
95 | 117 |
96 // it is now safe to free the canvas since there should be no remaining | 118 // 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) | 119 // references to the content that is referenced by this canvas (e.g. pix els) |
98 originalCanvas->unref(); | 120 originalCanvas->unref(); |
99 } | 121 } |
100 | 122 |
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; | 123 SkMCState mcState; |
113 | 124 |
114 int32_t layerCount; | 125 int32_t layerCount; |
115 SkCanvasLayerState* layers; | 126 SkCanvasLayerState* layers; |
116 | |
117 private: | 127 private: |
118 SkCanvas* originalCanvas; | 128 SkCanvas* originalCanvas; |
129 typedef SkCanvasState INHERITED; | |
119 }; | 130 }; |
120 | 131 |
121 //////////////////////////////////////////////////////////////////////////////// | 132 //////////////////////////////////////////////////////////////////////////////// |
122 | 133 |
123 class ClipValidator : public SkCanvas::ClipVisitor { | 134 class ClipValidator : public SkCanvas::ClipVisitor { |
124 public: | 135 public: |
125 ClipValidator() : fFailed(false) {} | 136 ClipValidator() : fFailed(false) {} |
126 bool failed() { return fFailed; } | 137 bool failed() { return fFailed; } |
127 | 138 |
128 // ClipVisitor | 139 // ClipVisitor |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 | 195 |
185 // Check the clip can be decomposed into rectangles (i.e. no soft clips). | 196 // Check the clip can be decomposed into rectangles (i.e. no soft clips). |
186 ClipValidator validator; | 197 ClipValidator validator; |
187 canvas->replayClips(&validator); | 198 canvas->replayClips(&validator); |
188 if (validator.failed()) { | 199 if (validator.failed()) { |
189 SkErrorInternals::SetError(kInvalidOperation_SkError, | 200 SkErrorInternals::SetError(kInvalidOperation_SkError, |
190 "CaptureCanvasState does not support canvases with antialiased c lips.\n"); | 201 "CaptureCanvasState does not support canvases with antialiased c lips.\n"); |
191 return NULL; | 202 return NULL; |
192 } | 203 } |
193 | 204 |
194 SkAutoTDelete<SkCanvasState> canvasState(SkNEW_ARGS(SkCanvasState, (canvas)) ); | 205 SkAutoTDelete<SkCanvasState_v1> canvasState(SkNEW_ARGS(SkCanvasState_v1, (ca nvas))); |
195 | 206 |
196 // decompose the total matrix and clip | 207 // decompose the total matrix and clip |
197 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), | 208 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), |
198 canvas->internal_private_getTotalClip()); | 209 canvas->internal_private_getTotalClip()); |
199 | 210 |
200 /* | 211 /* |
201 * decompose the layers | 212 * decompose the layers |
202 * | 213 * |
203 * storage is allocated on the stack for the first 3 layers. It is common in | 214 * 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 | 215 * 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 } | 251 } |
241 | 252 |
242 // allocate memory for the layers and then and copy them to the struct | 253 // allocate memory for the layers and then and copy them to the struct |
243 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerStat e)); | 254 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerStat e)); |
244 canvasState->layerCount = layerCount; | 255 canvasState->layerCount = layerCount; |
245 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.byte sWritten()); | 256 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.byte sWritten()); |
246 layerWriter.flatten(canvasState->layers); | 257 layerWriter.flatten(canvasState->layers); |
247 | 258 |
248 // for now, just ignore any client supplied DrawFilter. | 259 // for now, just ignore any client supplied DrawFilter. |
249 if (canvas->getDrawFilter()) { | 260 if (canvas->getDrawFilter()) { |
250 // SkDEBUGF(("CaptureCanvasState will ignore the canvases draw filter.\n" )); | 261 // SkDEBUGF(("CaptureCanvasState will ignore the canvas's draw filter.\n" )); |
251 } | 262 } |
252 | 263 |
253 return canvasState.detach(); | 264 return canvasState.detach(); |
254 } | 265 } |
255 | 266 |
256 //////////////////////////////////////////////////////////////////////////////// | 267 //////////////////////////////////////////////////////////////////////////////// |
257 | 268 |
258 static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { | 269 static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { |
259 // reconstruct the matrix | 270 // reconstruct the matrix |
260 SkMatrix matrix; | 271 SkMatrix matrix; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 | 311 |
301 // setup the matrix and clip | 312 // setup the matrix and clip |
302 setup_canvas_from_MC_state(layerState.mcState, canvas.get()); | 313 setup_canvas_from_MC_state(layerState.mcState, canvas.get()); |
303 | 314 |
304 return canvas.detach(); | 315 return canvas.detach(); |
305 } | 316 } |
306 | 317 |
307 SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) { | 318 SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) { |
308 SkASSERT(state); | 319 SkASSERT(state); |
309 | 320 |
310 // check that the versions match | 321 if (state->version == 1) { |
311 if (CANVAS_STATE_VERSION != state->version) { | 322 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>( state); |
312 SkDebugf("CreateFromCanvasState version does not match the one use to cr eate the input"); | 323 |
324 if (state_v1->layerCount < 1) { | |
325 return NULL; | |
326 } | |
327 | |
328 SkAutoTUnref<SkCanvasStack> canvas(SkNEW_ARGS(SkCanvasStack, | |
329 (state->width, state->heig ht))); | |
330 | |
331 // setup the matrix and clip on the n-way canvas | |
332 setup_canvas_from_MC_state(state_v1->mcState, canvas); | |
333 | |
334 // Iterate over the layers and add them to the n-way canvas | |
335 for (int i = state_v1->layerCount - 1; i >= 0; --i) { | |
336 SkAutoTUnref<SkCanvas> cLayer(create_canvas_from_canvas_layer(state_ v1->layers[i])); | |
337 if (!cLayer.get()) { | |
338 return NULL; | |
339 } | |
340 canvas->pushCanvas(cLayer.get(), SkIPoint::Make(state_v1->layers[i]. x, | |
341 state_v1->layers[i]. y)); | |
342 } | |
343 | |
344 return canvas.detach(); | |
345 } else { | |
346 SkDebugf("CreateFromCanvasState from version %d is not supported!", stat e->version); | |
313 return NULL; | 347 return NULL; |
314 } | 348 } |
315 | |
316 if (state->layerCount < 1) { | |
317 return NULL; | |
318 } | |
319 | |
320 SkAutoTUnref<SkCanvasStack> canvas(SkNEW_ARGS(SkCanvasStack, (state->width, state->height))); | |
321 | |
322 // setup the matrix and clip on the n-way canvas | |
323 setup_canvas_from_MC_state(state->mcState, canvas); | |
324 | |
325 // Iterate over the layers and add them to the n-way canvas | |
326 for (int i = state->layerCount - 1; i >= 0; --i) { | |
327 SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state ->layers[i])); | |
328 if (!canvasLayer.get()) { | |
329 return NULL; | |
330 } | |
331 canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state->layers[i].x, | |
332 state->layers[i].y) ); | |
333 } | |
334 | |
335 return canvas.detach(); | |
336 } | 349 } |
337 | 350 |
338 //////////////////////////////////////////////////////////////////////////////// | 351 //////////////////////////////////////////////////////////////////////////////// |
339 | 352 |
340 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { | 353 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { |
341 SkDELETE(state); | 354 SkDELETE(state); |
mtklein
2014/07/07 20:32:40
Can't we do the same check-version-and-static-cast
scroggo
2014/07/07 21:44:02
To (try to) sum up our conversation live, you want
| |
342 } | 355 } |
OLD | NEW |