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 "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 }; | 97 }; |
98 | 98 |
99 class SkCanvasState_v1 : public SkCanvasState { | 99 class SkCanvasState_v1 : public SkCanvasState { |
100 public: | 100 public: |
101 static const int32_t kVersion = 1; | 101 static const int32_t kVersion = 1; |
102 | 102 |
103 SkCanvasState_v1(SkCanvas* canvas) | 103 SkCanvasState_v1(SkCanvas* canvas) |
104 : INHERITED(kVersion, canvas) | 104 : INHERITED(kVersion, canvas) |
105 { | 105 { |
106 layerCount = 0; | 106 layerCount = 0; |
107 layers = NULL; | 107 layers = nullptr; |
108 mcState.clipRectCount = 0; | 108 mcState.clipRectCount = 0; |
109 mcState.clipRects = NULL; | 109 mcState.clipRects = nullptr; |
110 originalCanvas = SkRef(canvas); | 110 originalCanvas = SkRef(canvas); |
111 } | 111 } |
112 | 112 |
113 ~SkCanvasState_v1() { | 113 ~SkCanvasState_v1() { |
114 // loop through the layers and free the data allocated to the clipRects | 114 // loop through the layers and free the data allocated to the clipRects |
115 for (int i = 0; i < layerCount; ++i) { | 115 for (int i = 0; i < layerCount; ++i) { |
116 sk_free(layers[i].mcState.clipRects); | 116 sk_free(layers[i].mcState.clipRects); |
117 } | 117 } |
118 | 118 |
119 sk_free(mcState.clipRects); | 119 sk_free(mcState.clipRects); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 196 |
197 SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) { | 197 SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) { |
198 SkASSERT(canvas); | 198 SkASSERT(canvas); |
199 | 199 |
200 // Check the clip can be decomposed into rectangles (i.e. no soft clips). | 200 // Check the clip can be decomposed into rectangles (i.e. no soft clips). |
201 ClipValidator validator; | 201 ClipValidator validator; |
202 canvas->replayClips(&validator); | 202 canvas->replayClips(&validator); |
203 if (validator.failed()) { | 203 if (validator.failed()) { |
204 SkErrorInternals::SetError(kInvalidOperation_SkError, | 204 SkErrorInternals::SetError(kInvalidOperation_SkError, |
205 "CaptureCanvasState does not support canvases with antialiased c
lips.\n"); | 205 "CaptureCanvasState does not support canvases with antialiased c
lips.\n"); |
206 return NULL; | 206 return nullptr; |
207 } | 207 } |
208 | 208 |
209 SkAutoTDelete<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas)); | 209 SkAutoTDelete<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas)); |
210 | 210 |
211 // decompose the total matrix and clip | 211 // decompose the total matrix and clip |
212 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), | 212 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), |
213 canvas->internal_private_getTotalClip()); | 213 canvas->internal_private_getTotalClip()); |
214 | 214 |
215 /* | 215 /* |
216 * decompose the layers | 216 * decompose the layers |
217 * | 217 * |
218 * storage is allocated on the stack for the first 3 layers. It is common in | 218 * storage is allocated on the stack for the first 3 layers. It is common in |
219 * some view systems (e.g. Android) that a few non-clipped layers are presen
t | 219 * some view systems (e.g. Android) that a few non-clipped layers are presen
t |
220 * and we will not need to malloc any additional memory in those cases. | 220 * and we will not need to malloc any additional memory in those cases. |
221 */ | 221 */ |
222 SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter; | 222 SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter; |
223 int layerCount = 0; | 223 int layerCount = 0; |
224 for (SkCanvas::LayerIter layer(canvas, true/*skipEmptyClips*/); !layer.done(
); layer.next()) { | 224 for (SkCanvas::LayerIter layer(canvas, true/*skipEmptyClips*/); !layer.done(
); layer.next()) { |
225 | 225 |
226 // we currently only work for bitmap backed devices | 226 // we currently only work for bitmap backed devices |
227 SkPixmap pmap; | 227 SkPixmap pmap; |
228 if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pm
ap.height()) { | 228 if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pm
ap.height()) { |
229 return NULL; | 229 return nullptr; |
230 } | 230 } |
231 | 231 |
232 SkCanvasLayerState* layerState = | 232 SkCanvasLayerState* layerState = |
233 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerSt
ate)); | 233 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerSt
ate)); |
234 layerState->type = kRaster_CanvasBackend; | 234 layerState->type = kRaster_CanvasBackend; |
235 layerState->x = layer.x(); | 235 layerState->x = layer.x(); |
236 layerState->y = layer.y(); | 236 layerState->y = layer.y(); |
237 layerState->width = pmap.width(); | 237 layerState->width = pmap.width(); |
238 layerState->height = pmap.height(); | 238 layerState->height = pmap.height(); |
239 | 239 |
240 switch (pmap.colorType()) { | 240 switch (pmap.colorType()) { |
241 case kN32_SkColorType: | 241 case kN32_SkColorType: |
242 layerState->raster.config = kARGB_8888_RasterConfig; | 242 layerState->raster.config = kARGB_8888_RasterConfig; |
243 break; | 243 break; |
244 case kRGB_565_SkColorType: | 244 case kRGB_565_SkColorType: |
245 layerState->raster.config = kRGB_565_RasterConfig; | 245 layerState->raster.config = kRGB_565_RasterConfig; |
246 break; | 246 break; |
247 default: | 247 default: |
248 return NULL; | 248 return nullptr; |
249 } | 249 } |
250 layerState->raster.rowBytes = pmap.rowBytes(); | 250 layerState->raster.rowBytes = pmap.rowBytes(); |
251 layerState->raster.pixels = pmap.writable_addr(); | 251 layerState->raster.pixels = pmap.writable_addr(); |
252 | 252 |
253 setup_MC_state(&layerState->mcState, layer.matrix(), layer.clip()); | 253 setup_MC_state(&layerState->mcState, layer.matrix(), layer.clip()); |
254 layerCount++; | 254 layerCount++; |
255 } | 255 } |
256 | 256 |
257 // allocate memory for the layers and then and copy them to the struct | 257 // allocate memory for the layers and then and copy them to the struct |
258 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerStat
e)); | 258 SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerStat
e)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 static SkCanvas* create_canvas_from_canvas_layer(const SkCanvasLayerState& layer
State) { | 294 static SkCanvas* create_canvas_from_canvas_layer(const SkCanvasLayerState& layer
State) { |
295 SkASSERT(kRaster_CanvasBackend == layerState.type); | 295 SkASSERT(kRaster_CanvasBackend == layerState.type); |
296 | 296 |
297 SkBitmap bitmap; | 297 SkBitmap bitmap; |
298 SkColorType colorType = | 298 SkColorType colorType = |
299 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType : | 299 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType : |
300 layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType
: | 300 layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType
: |
301 kUnknown_SkColorType; | 301 kUnknown_SkColorType; |
302 | 302 |
303 if (colorType == kUnknown_SkColorType) { | 303 if (colorType == kUnknown_SkColorType) { |
304 return NULL; | 304 return nullptr; |
305 } | 305 } |
306 | 306 |
307 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height, | 307 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height, |
308 colorType, kPremul_SkAlphaType), | 308 colorType, kPremul_SkAlphaType), |
309 layerState.raster.pixels, (size_t) layerState.raster.ro
wBytes); | 309 layerState.raster.pixels, (size_t) layerState.raster.ro
wBytes); |
310 | 310 |
311 SkASSERT(!bitmap.empty()); | 311 SkASSERT(!bitmap.empty()); |
312 SkASSERT(!bitmap.isNull()); | 312 SkASSERT(!bitmap.isNull()); |
313 | 313 |
314 SkAutoTUnref<SkCanvas> canvas(new SkCanvas(bitmap)); | 314 SkAutoTUnref<SkCanvas> canvas(new SkCanvas(bitmap)); |
315 | 315 |
316 // setup the matrix and clip | 316 // setup the matrix and clip |
317 setup_canvas_from_MC_state(layerState.mcState, canvas.get()); | 317 setup_canvas_from_MC_state(layerState.mcState, canvas.get()); |
318 | 318 |
319 return canvas.detach(); | 319 return canvas.detach(); |
320 } | 320 } |
321 | 321 |
322 SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state)
{ | 322 SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state)
{ |
323 SkASSERT(state); | 323 SkASSERT(state); |
324 // Currently there is only one possible version. | 324 // Currently there is only one possible version. |
325 SkASSERT(SkCanvasState_v1::kVersion == state->version); | 325 SkASSERT(SkCanvasState_v1::kVersion == state->version); |
326 | 326 |
327 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(stat
e); | 327 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(stat
e); |
328 | 328 |
329 if (state_v1->layerCount < 1) { | 329 if (state_v1->layerCount < 1) { |
330 return NULL; | 330 return nullptr; |
331 } | 331 } |
332 | 332 |
333 SkAutoTUnref<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->he
ight)); | 333 SkAutoTUnref<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->he
ight)); |
334 | 334 |
335 // setup the matrix and clip on the n-way canvas | 335 // setup the matrix and clip on the n-way canvas |
336 setup_canvas_from_MC_state(state_v1->mcState, canvas); | 336 setup_canvas_from_MC_state(state_v1->mcState, canvas); |
337 | 337 |
338 // Iterate over the layers and add them to the n-way canvas | 338 // Iterate over the layers and add them to the n-way canvas |
339 for (int i = state_v1->layerCount - 1; i >= 0; --i) { | 339 for (int i = state_v1->layerCount - 1; i >= 0; --i) { |
340 SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state
_v1->layers[i])); | 340 SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state
_v1->layers[i])); |
341 if (!canvasLayer.get()) { | 341 if (!canvasLayer.get()) { |
342 return NULL; | 342 return nullptr; |
343 } | 343 } |
344 canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state_v1->layers[i]
.x, | 344 canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state_v1->layers[i]
.x, |
345 state_v1->layers[i]
.y)); | 345 state_v1->layers[i]
.y)); |
346 } | 346 } |
347 | 347 |
348 return canvas.detach(); | 348 return canvas.detach(); |
349 } | 349 } |
350 | 350 |
351 //////////////////////////////////////////////////////////////////////////////// | 351 //////////////////////////////////////////////////////////////////////////////// |
352 | 352 |
353 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { | 353 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { |
354 SkASSERT(!state || SkCanvasState_v1::kVersion == state->version); | 354 SkASSERT(!state || SkCanvasState_v1::kVersion == state->version); |
355 // Upcast to the correct version of SkCanvasState. This avoids having a virt
ual destructor on | 355 // Upcast to the correct version of SkCanvasState. This avoids having a virt
ual destructor on |
356 // SkCanvasState. That would be strange since SkCanvasState has no other vir
tual functions, and | 356 // SkCanvasState. That would be strange since SkCanvasState has no other vir
tual functions, and |
357 // instead uses the field "version" to determine how to behave. | 357 // instead uses the field "version" to determine how to behave. |
358 delete static_cast<SkCanvasState_v1*>(state); | 358 delete static_cast<SkCanvasState_v1*>(state); |
359 } | 359 } |
OLD | NEW |