 Chromium Code Reviews
 Chromium Code Reviews Issue 23545017:
  Create a semi-stable API for capturing the state of an SkCanvas and reconstructing that state acros…  (Closed) 
  Base URL: https://skia.googlecode.com/svn/trunk
    
  
    Issue 23545017:
  Create a semi-stable API for capturing the state of an SkCanvas and reconstructing that state acros…  (Closed) 
  Base URL: https://skia.googlecode.com/svn/trunk| OLD | NEW | 
|---|---|
| (Empty) | |
| 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 | |
| 8 #include "SkCanvasStateUtils.h" | |
| 9 | |
| 10 #include "SkBitmapDevice.h" | |
| 11 #include "SkCanvas.h" | |
| 12 #include "SkNWayCanvas.h" | |
| 13 #include "SkWriter32.h" | |
| 14 | |
| 15 #define CANVAS_STATE_VERSION 1 | |
| 16 /* | |
| 17 * WARNING: The structs below are part of a stable ABI and as such we explicitly | |
| 18 * use unambigious primitives (e.g. int32_t instead of an enum). | |
| 19 * | |
| 20 * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN AN | |
| 21 * UPDATE OF THE CANVAS_STATE_VERSION. SUCH CHANGES SHOULD ONLY BE MADE IF | |
| 22 * ABSOLUTELY NECESSARY! | |
| 23 */ | |
| 24 enum RasterConfigs { | |
| 25 kUnknown_RasterConfig = 0, | |
| 26 kRGB_565_RasterConfig = 1, | |
| 27 kARGB_8888_RasterConfig = 2 | |
| 28 }; | |
| 29 typedef int32_t RasterConfig; | |
| 30 | |
| 31 enum CanvasBackends { | |
| 32 kUnknown_CanvasBackend = 0, | |
| 33 kRaster_CanvasBackend = 1, | |
| 34 kGPU_CanvasBackend = 2, | |
| 35 kPDF_CanvasBackend = 3 | |
| 36 }; | |
| 37 typedef int32_t CanvasBackend; | |
| 38 | |
| 39 struct ClipRect { | |
| 40 int32_t left, top, right, bottom; | |
| 41 }; | |
| 42 | |
| 43 struct SkMCState { | |
| 44 float matrix[9]; | |
| 45 // NOTE: this only works for non-antialiased clips | |
| 46 int32_t clipRectCount; | |
| 47 ClipRect* clipRects; | |
| 48 }; | |
| 49 | |
| 50 // NOTE: If you add more members, bump CanvasState::version. | |
| 51 struct SkCanvasLayerState { | |
| 52 CanvasBackend type; | |
| 53 int32_t x, y; | |
| 54 int32_t width; | |
| 55 int32_t height; | |
| 56 | |
| 57 SkMCState mcState; | |
| 58 | |
| 59 union { | |
| 60 struct { | |
| 61 RasterConfig config; // pixel format: a value from RasterConfigs. | |
| 62 uint32_t rowBytes; // Number of bytes from start of one line to ne xt. | |
| 63 void* pixels; // The pixels, all (height * rowBytes) of them. | |
| 64 } raster; | |
| 65 struct { | |
| 66 int32_t textureID; | |
| 67 } gpu; | |
| 68 }; | |
| 69 }; | |
| 70 | |
| 71 class SkCanvasState { | |
| 72 public: | |
| 73 SkCanvasState(SkCanvas* canvas) { | |
| 74 SkASSERT(canvas); | |
| 75 version = CANVAS_STATE_VERSION; | |
| 76 width = canvas->getDeviceSize().width(); | |
| 77 height = canvas->getDeviceSize().height(); | |
| 78 layerCount = 0; | |
| 79 layers = NULL; | |
| 80 originalCanvas = SkRef(canvas); | |
| 81 | |
| 82 mcState.clipRectCount = 0; | |
| 83 mcState.clipRects = NULL; | |
| 84 } | |
| 85 | |
| 86 ~SkCanvasState() { | |
| 87 // loop through the layers and free the data allocated to the clipRects | |
| 88 for (int i = 0; i < layerCount; ++i) { | |
| 89 sk_free(layers[i].mcState.clipRects); | |
| 90 } | |
| 91 | |
| 92 sk_free(mcState.clipRects); | |
| 93 sk_free(layers); | |
| 94 | |
| 95 // it is now safe to free the canvas since there should be no remaining | |
| 96 // references to the content that is referenced by this canvas (e.g. pix els) | |
| 97 originalCanvas->unref(); | |
| 98 } | |
| 99 | |
| 100 /** | |
| 101 * The version this struct was built with. This field must always appear | |
| 102 * first in the struct so that when the versions don't match (and the | |
| 103 * remaining contents and size are potentially different) we can still | |
| 104 * compare the version numbers. | |
| 105 */ | |
| 106 int32_t version; | |
| 107 | |
| 108 int32_t width; | |
| 109 int32_t height; | |
| 110 | |
| 111 SkMCState mcState; | |
| 112 | |
| 113 int32_t layerCount; | |
| 114 SkCanvasLayerState* layers; | |
| 115 | |
| 116 private: | |
| 117 SkCanvas* originalCanvas; | |
| 118 }; | |
| 119 | |
| 120 //////////////////////////////////////////////////////////////////////////////// | |
| 121 | |
| 122 class ClipValidator : public SkCanvas::ClipVisitor { | |
| 123 public: | |
| 124 ClipValidator() : fFailed(false) {} | |
| 125 bool failed() { return fFailed; } | |
| 126 | |
| 127 // ClipVisitor | |
| 128 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) S K_OVERRIDE { | |
| 129 fFailed |= antialias; | |
| 130 } | |
| 131 | |
| 132 virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) SK_OVERRI DE { | |
| 133 fFailed |= antialias; | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 bool fFailed; | |
| 138 }; | |
| 139 | |
| 140 static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkReg ion& clip) { | |
| 141 // initialize the struct | |
| 142 state->clipRectCount = 0; | |
| 143 | |
| 144 // capture the matrix | |
| 145 for (int i = 0; i < 9; i++) { | |
| 146 state->matrix[i] = matrix.get(i); | |
| 147 } | |
| 148 | |
| 149 /* | |
| 150 * capture the clip | |
| 151 * | |
| 152 * storage is allocated on the stack for the first 4 rects. This value was | |
| 153 * chosen somewhat arbitrarily, but does allow us to represent simple clips | |
| 154 * and some more common complex clips (e.g. a clipRect with a sub-rect | |
| 155 * clipped out of its interior) without needing to malloc any additional mem ory. | |
| 156 */ | |
| 157 const int clipBufferSize = 4 * sizeof(ClipRect); | |
| 158 char clipBuffer[clipBufferSize]; | |
| 159 SkWriter32 clipWriter(sizeof(ClipRect), clipBuffer, clipBufferSize); | |
| 160 | |
| 161 if (!clip.isEmpty()) { | |
| 162 // only returns the b/w clip so aa clips fail | |
| 163 SkRegion::Iterator clip_iterator(clip); | |
| 164 for (; !clip_iterator.done(); clip_iterator.next()) { | |
| 165 // this assumes the SkIRect is stored in l,t,r,b ordering which | |
| 166 // matches the ordering of our ClipRect struct | |
| 167 clipWriter.writeIRect(clip_iterator.rect()); | |
| 168 state->clipRectCount++; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 // allocate memory for the clip then and copy them to the struct | |
| 173 state->clipRects = (ClipRect*) sk_malloc_throw(clipWriter.size()); | |
| 174 clipWriter.flatten(state->clipRects); | |
| 175 } | |
| 176 | |
| 177 | |
| 178 | |
| 179 SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) { | |
| 180 SkASSERT(canvas); | |
| 181 | |
| 182 // Check the clip can be decomposed into rectangles (i.e. no soft clips). | |
| 183 ClipValidator validator; | |
| 184 canvas->replayClips(&validator); | |
| 185 if (validator.failed()) { | |
| 186 SkDEBUGF(("CaptureCanvasState does not support canvases with antialiased clips.\n")); | |
| 187 return NULL; | |
| 188 } | |
| 189 | |
| 190 SkAutoTDelete<SkCanvasState> canvasState(SkNEW_ARGS(SkCanvasState, (canvas)) ); | |
| 191 | |
| 192 // decompose the total matrix and clip | |
| 193 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getT otalClip()); | |
| 194 | |
| 195 /* | |
| 196 * decompose the layers | |
| 197 * | |
| 198 * storage is allocated on the stack for the first 3 layers. It is common in | |
| 199 * some view systems (e.g. Android) that a few non-clipped layers are presen t | |
| 200 * and we will not need to malloc any additional memory in those cases. | |
| 201 */ | |
| 202 const int layerBufferSize = 3 * sizeof(SkCanvasLayerState); | |
| 203 char layerBuffer[layerBufferSize]; | |
| 204 SkWriter32 layerWriter(sizeof(SkCanvasLayerState), layerBuffer, layerBufferS ize); | |
| 205 int layerCount = 0; | |
| 206 for (SkCanvas::LayerIter layer(canvas, true/*skipEmptyClips*/); !layer.done( ); layer.next()) { | |
| 207 | |
| 208 // we currently only work for bitmap backed devices | |
| 209 const SkBitmap& bitmap = layer.device()->accessBitmap(true/*changePixels */); | |
| 210 if (bitmap.empty() || bitmap.isNull() || !bitmap.lockPixelsAreWritable() ) { | |
| 211 return NULL; | |
| 212 } | |
| 213 | |
| 214 SkCanvasLayerState* layerState = | |
| 215 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerSt ate)); | |
| 216 layerState->type = kRaster_CanvasBackend; | |
| 217 layerState->x = layer.x(); | |
| 218 layerState->y = layer.y(); | |
| 219 layerState->width = bitmap.width(); | |
| 220 layerState->height = bitmap.height(); | |
| 221 | |
| 222 switch (bitmap.config()) { | |
| 223 case SkBitmap::kARGB_8888_Config: | |
| 224 layerState->raster.config = kARGB_8888_RasterConfig; | |
| 225 break; | |
| 226 case SkBitmap::kRGB_565_Config: | |
| 227 layerState->raster.config = kRGB_565_RasterConfig; | |
| 228 break; | |
| 229 default: | |
| 230 return NULL; | |
| 231 } | |
| 232 layerState->raster.rowBytes = bitmap.rowBytes(); | |
| 233 layerState->raster.pixels = bitmap.getPixels(); | |
| 234 | |
| 235 setup_MC_state(&layerState->mcState, layer.matrix(), layer.clip()); | |
| 236 layerCount++; | |
| 237 } | |
| 238 | |
| 239 // allocate memory for the layers and then and copy them to the struct | |
| 240 SkASSERT(layerWriter.size() == layerCount * sizeof(SkCanvasLayerState)); | |
| 241 canvasState->layerCount = layerCount; | |
| 242 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.size ()); | |
| 243 layerWriter.flatten(canvasState->layers); | |
| 244 | |
| 245 // for now, just ignore any client supplied DrawFilter. | |
| 246 if (canvas->getDrawFilter()) { | |
| 247 SkDEBUGF(("CaptureCanvasState will ignore the canvases draw filter.\n")) ; | |
| 248 } | |
| 249 | |
| 250 return canvasState.detach(); | |
| 251 } | |
| 252 | |
| 253 //////////////////////////////////////////////////////////////////////////////// | |
| 254 | |
| 255 static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { | |
| 256 // reconstruct the matrix | |
| 257 SkMatrix matrix; | |
| 258 for (int i = 0; i < 9; i++) { | |
| 259 matrix.set(i, state.matrix[i]); | |
| 260 } | |
| 261 | |
| 262 // reconstruct the clip | |
| 263 SkRegion clip; | |
| 264 for (int i = 0; i < state.clipRectCount; ++i) { | |
| 265 clip.op(SkIRect::MakeLTRB(state.clipRects[i].left, | |
| 266 state.clipRects[i].top, | |
| 267 state.clipRects[i].right, | |
| 268 state.clipRects[i].bottom), | |
| 269 SkRegion::kUnion_Op); | |
| 270 } | |
| 271 | |
| 272 canvas->setMatrix(matrix); | |
| 273 canvas->setClipRegion(clip); | |
| 274 } | |
| 275 | |
| 276 static SkCanvas* create_canvas_from_canvas_layer(const SkCanvasLayerState& layer State) { | |
| 277 SkASSERT(kRaster_CanvasBackend == layerState.type); | |
| 278 | |
| 279 SkBitmap bitmap; | |
| 280 SkBitmap::Config config = | |
| 281 layerState.raster.config == kARGB_8888_RasterConfig ? SkBitmap::kARGB_88 88_Config : | |
| 282 layerState.raster.config == kRGB_565_RasterConfig ? SkBitmap::kRGB_565_C onfig : | |
| 283 SkBitmap::kNo_Config; | |
| 284 | |
| 285 if (config == SkBitmap::kNo_Config) { | |
| 286 return NULL; | |
| 287 } | |
| 288 | |
| 289 bitmap.setConfig(config, layerState.width, layerState.height, | |
| 290 layerState.raster.rowBytes); | |
| 291 bitmap.setPixels(layerState.raster.pixels); | |
| 292 | |
| 293 SkASSERT(!bitmap.empty()); | |
| 294 SkASSERT(!bitmap.isNull()); | |
| 295 | |
| 296 // create a device & canvas | |
| 297 SkAutoTUnref<SkBitmapDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap))); | |
| 298 SkAutoTUnref<SkCanvas> canvas(SkNEW_ARGS(SkCanvas, (device.get()))); | |
| 299 | |
| 300 // setup the matrix and clip | |
| 301 setup_canvas_from_MC_state(layerState.mcState, canvas.get()); | |
| 302 | |
| 303 return canvas.detach(); | |
| 304 } | |
| 305 | |
| 306 SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) { | |
| 307 SkASSERT(state); | |
| 308 | |
| 309 // check that the versions match | |
| 310 if (CANVAS_STATE_VERSION != state->version) { | |
| 311 SkDebugf("CreateFromCanvasState version does not match the one use to cr eate the input"); | |
| 312 return NULL; | |
| 313 } | |
| 314 | |
| 315 if (state->layerCount < 1) { | |
| 316 return NULL; | |
| 317 } | |
| 318 | |
| 319 SkAutoTUnref<SkNWayCanvas> canvas(SkNEW_ARGS(SkNWayCanvas, (state->width, st ate->height))); | |
| 320 | |
| 321 // setup the matrix and clip on the n-way canvas | |
| 322 setup_canvas_from_MC_state(state->mcState, canvas); | |
| 323 | |
| 324 // Iterate over the layers and add them to the n-way canvas | |
| 325 for (int i = 0; i < state->layerCount; ++i) { | |
| 326 SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state ->layers[i])); | |
| 327 if (!canvasLayer.get()) { | |
| 328 return NULL; | |
| 329 } | |
| 330 canvas->addCanvas(canvasLayer.get()); | |
| 
joth
2013/08/30 06:45:57
I think there's a bug here that you never look at
 
joth
2013/08/30 07:05:04
Hmm OK x & y maybe a red-herring.
I looked a bit
 
djsollen
2013/08/30 11:51:19
Yes, the setMatrix will cause some havoc with this
 | |
| 331 } | |
| 332 | |
| 333 return canvas.detach(); | |
| 334 } | |
| 335 | |
| 336 //////////////////////////////////////////////////////////////////////////////// | |
| 337 | |
| 338 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { | |
| 339 SkDELETE(state); | |
| 340 } | |
| OLD | NEW |