| 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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 const SkBitmap& bitmap = layer.device()->accessBitmap(true/*changePixels
*/); | 227 SkPixmap pmap; |
| 228 if (bitmap.empty() || bitmap.isNull() || !bitmap.lockPixelsAreWritable()
) { | 228 if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pm
ap.height()) { |
| 229 return NULL; | 229 return NULL; |
| 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 = bitmap.width(); | 237 layerState->width = pmap.width(); |
| 238 layerState->height = bitmap.height(); | 238 layerState->height = pmap.height(); |
| 239 | 239 |
| 240 switch (bitmap.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 NULL; |
| 249 } | 249 } |
| 250 layerState->raster.rowBytes = bitmap.rowBytes(); | 250 layerState->raster.rowBytes = pmap.rowBytes(); |
| 251 layerState->raster.pixels = bitmap.getPixels(); | 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)); |
| 259 canvasState->layerCount = layerCount; | 259 canvasState->layerCount = layerCount; |
| 260 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.byte
sWritten()); | 260 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.byte
sWritten()); |
| 261 layerWriter.flatten(canvasState->layers); | 261 layerWriter.flatten(canvasState->layers); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 SkDELETE(static_cast<SkCanvasState_v1*>(state)); | 358 SkDELETE(static_cast<SkCanvasState_v1*>(state)); |
| 359 } | 359 } |
| OLD | NEW |