epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkPictureRecord.h" |
| 9 | #include "SkTSearch.h" |
junov@chromium.org | 4866cc0 | 2012-06-01 21:23:07 +0000 | [diff] [blame] | 10 | #include "SkPixelRef.h" |
rileya@google.com | 9f5898d | 2012-09-11 20:21:44 +0000 | [diff] [blame] | 11 | #include "SkBBoxHierarchy.h" |
| 12 | #include "SkPictureStateTree.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
| 14 | #define MIN_WRITER_SIZE 16384 |
| 15 | #define HEAP_BLOCK_SIZE 4096 |
| 16 | |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 17 | enum { |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 18 | // just need a value that save or getSaveCount would never return |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 19 | kNoInitialSave = -1, |
| 20 | }; |
| 21 | |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 22 | SkPictureRecord::SkPictureRecord(uint32_t flags, SkDevice* device) : |
| 23 | INHERITED(device), |
robertphillips@google.com | 178a267 | 2012-09-13 13:25:30 +0000 | [diff] [blame] | 24 | fBoundingHierarchy(NULL), |
| 25 | fStateTree(NULL), |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 26 | fFlattenableHeap(HEAP_BLOCK_SIZE), |
| 27 | fMatrices(&fFlattenableHeap), |
| 28 | fPaints(&fFlattenableHeap), |
| 29 | fRegions(&fFlattenableHeap), |
djsollen@google.com | d2700ee | 2012-05-30 16:54:13 +0000 | [diff] [blame] | 30 | fWriter(MIN_WRITER_SIZE), |
| 31 | fRecordFlags(flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | #ifdef SK_DEBUG_SIZE |
| 33 | fPointBytes = fRectBytes = fTextBytes = 0; |
| 34 | fPointWrites = fRectWrites = fTextWrites = 0; |
| 35 | #endif |
| 36 | |
| 37 | fRestoreOffsetStack.setReserve(32); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 38 | |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 39 | fBitmapHeap = SkNEW(SkBitmapHeap); |
| 40 | fFlattenableHeap.setBitmapStorage(fBitmapHeap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | fPathHeap = NULL; // lazy allocate |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 42 | fFirstSavedLayerIndex = kNoSavedLayerIndex; |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 43 | |
| 44 | fInitialSaveCount = kNoInitialSave; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | SkPictureRecord::~SkPictureRecord() { |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 48 | SkSafeUnref(fBitmapHeap); |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 49 | SkSafeUnref(fPathHeap); |
rileya@google.com | 9f5898d | 2012-09-11 20:21:44 +0000 | [diff] [blame] | 50 | SkSafeUnref(fBoundingHierarchy); |
| 51 | SkSafeUnref(fStateTree); |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 52 | fFlattenableHeap.setBitmapStorage(NULL); |
| 53 | fPictureRefs.unrefAll(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 58 | SkDevice* SkPictureRecord::setDevice(SkDevice* device) { |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 59 | SkASSERT(!"eeek, don't try to change the device on a recording canvas"); |
| 60 | return this->INHERITED::setDevice(device); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 61 | } |
| 62 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | int SkPictureRecord::save(SaveFlags flags) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 64 | // record the offset to us, making it non-positive to distinguish a save |
| 65 | // from a clip entry. |
| 66 | fRestoreOffsetStack.push(-(int32_t)fWriter.size()); |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | addDraw(SAVE); |
| 69 | addInt(flags); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 70 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | validate(); |
| 72 | return this->INHERITED::save(flags); |
| 73 | } |
| 74 | |
| 75 | int SkPictureRecord::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 76 | SaveFlags flags) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 77 | // record the offset to us, making it non-positive to distinguish a save |
| 78 | // from a clip entry. |
| 79 | fRestoreOffsetStack.push(-(int32_t)fWriter.size()); |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 80 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | addDraw(SAVE_LAYER); |
| 82 | addRectPtr(bounds); |
| 83 | addPaintPtr(paint); |
| 84 | addInt(flags); |
| 85 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 86 | if (kNoSavedLayerIndex == fFirstSavedLayerIndex) { |
| 87 | fFirstSavedLayerIndex = fRestoreOffsetStack.count(); |
| 88 | } |
| 89 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | validate(); |
reed@android.com | 261ae4d | 2009-10-02 16:37:46 +0000 | [diff] [blame] | 91 | /* Don't actually call saveLayer, because that will try to allocate an |
| 92 | offscreen device (potentially very big) which we don't actually need |
| 93 | at this time (and may not be able to afford since during record our |
| 94 | clip starts out the size of the picture, which is often much larger |
| 95 | than the size of the actual device we'll use during playback). |
| 96 | */ |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 97 | int count = this->INHERITED::save(flags); |
| 98 | this->clipRectBounds(bounds, flags, NULL); |
| 99 | return count; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | } |
| 101 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 102 | bool SkPictureRecord::isDrawingToLayer() const { |
| 103 | return fFirstSavedLayerIndex != kNoSavedLayerIndex; |
| 104 | } |
| 105 | |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 106 | // Return the size of the specified drawType's recorded block, or 0 if this verb |
| 107 | // is variable sized, and therefore not known. |
| 108 | static inline uint32_t getSkipableSize(unsigned drawType) { |
| 109 | static const uint8_t gSizes[LAST_DRAWTYPE_ENUM + 1] = { |
| 110 | 0, // UNUSED, |
| 111 | 4, // CLIP_PATH, |
| 112 | 4, // CLIP_REGION, |
| 113 | 7, // CLIP_RECT, |
| 114 | 2, // CONCAT, |
| 115 | 0, // DRAW_BITMAP, |
| 116 | 0, // DRAW_BITMAP_MATRIX, |
| 117 | 0, // DRAW_BITMAP_NINE, |
| 118 | 0, // DRAW_BITMAP_RECT, |
| 119 | 0, // DRAW_CLEAR, |
| 120 | 0, // DRAW_DATA, |
| 121 | 0, // DRAW_PAINT, |
| 122 | 0, // DRAW_PATH, |
| 123 | 0, // DRAW_PICTURE, |
| 124 | 0, // DRAW_POINTS, |
| 125 | 0, // DRAW_POS_TEXT, |
| 126 | 0, // DRAW_POS_TEXT_TOP_BOTTOM, // fast variant of DRAW_POS_TEXT |
| 127 | 0, // DRAW_POS_TEXT_H, |
| 128 | 0, // DRAW_POS_TEXT_H_TOP_BOTTOM, // fast variant of DRAW_POS_TEXT_H |
| 129 | 0, // DRAW_RECT, |
| 130 | 0, // DRAW_SPRITE, |
| 131 | 0, // DRAW_TEXT, |
| 132 | 0, // DRAW_TEXT_ON_PATH, |
| 133 | 0, // DRAW_TEXT_TOP_BOTTOM, // fast variant of DRAW_TEXT |
| 134 | 0, // DRAW_VERTICES, |
| 135 | 0, // RESTORE, |
| 136 | 2, // ROTATE, |
| 137 | 2, // SAVE, |
| 138 | 0, // SAVE_LAYER, |
| 139 | 3, // SCALE, |
| 140 | 2, // SET_MATRIX, |
| 141 | 3, // SKEW, |
| 142 | 3, // TRANSLATE, |
| 143 | }; |
| 144 | |
| 145 | SkASSERT(sizeof(gSizes) == LAST_DRAWTYPE_ENUM + 1); |
| 146 | SkASSERT((unsigned)drawType <= (unsigned)LAST_DRAWTYPE_ENUM); |
| 147 | return gSizes[drawType] * sizeof(uint32_t); |
| 148 | } |
| 149 | |
| 150 | #ifdef TRACK_COLLAPSE_STATS |
| 151 | static int gCollapseCount, gCollapseCalls; |
| 152 | #endif |
| 153 | |
| 154 | /* |
| 155 | * Restore has just been called (but not recoreded), so look back at the |
| 156 | * matching save(), and see if we can eliminate the pair of them, due to no |
| 157 | * intervening matrix/clip calls. |
| 158 | * |
| 159 | * If so, update the writer and return true, in which case we won't even record |
| 160 | * the restore() call. If we still need the restore(), return false. |
| 161 | */ |
| 162 | static bool collapseSaveClipRestore(SkWriter32* writer, int32_t offset) { |
reed@google.com | 6132eb9 | 2012-09-14 19:07:28 +0000 | [diff] [blame] | 163 | // Some unexplained crashes in Chrome may be caused by this. Disabling |
| 164 | // for now to see if it helps. |
| 165 | // crbug.com/147406 |
reed@google.com | a3301b6 | 2012-10-02 15:54:26 +0000 | [diff] [blame] | 166 | #ifdef SK_DISABLE_PICTURE_PEEPHOLE_OPTIMIZATION |
reed@google.com | 6132eb9 | 2012-09-14 19:07:28 +0000 | [diff] [blame] | 167 | return false; |
| 168 | #endif |
| 169 | |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 170 | #ifdef TRACK_COLLAPSE_STATS |
| 171 | gCollapseCalls += 1; |
| 172 | #endif |
| 173 | |
| 174 | int32_t restoreOffset = (int32_t)writer->size(); |
| 175 | |
| 176 | // back up to the save block |
| 177 | while (offset > 0) { |
| 178 | offset = *writer->peek32(offset); |
| 179 | } |
| 180 | |
| 181 | // now offset points to a save |
| 182 | offset = -offset; |
| 183 | if (SAVE_LAYER == *writer->peek32(offset)) { |
| 184 | // not ready to cull these out yet (mrr) |
| 185 | return false; |
| 186 | } |
| 187 | SkASSERT(SAVE == *writer->peek32(offset)); |
| 188 | |
| 189 | // Walk forward until we get back to either a draw-verb (abort) or we hit |
| 190 | // our restore (success). |
| 191 | int32_t saveOffset = offset; |
| 192 | |
| 193 | offset += getSkipableSize(SAVE); |
| 194 | while (offset < restoreOffset) { |
| 195 | uint32_t* block = writer->peek32(offset); |
| 196 | uint32_t op = *block; |
| 197 | uint32_t opSize = getSkipableSize(op); |
| 198 | if (0 == opSize) { |
| 199 | // drawing verb, abort |
| 200 | return false; |
| 201 | } |
| 202 | offset += opSize; |
| 203 | } |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 204 | |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 205 | #ifdef TRACK_COLLAPSE_STATS |
| 206 | gCollapseCount += 1; |
| 207 | SkDebugf("Collapse [%d out of %d] %g%spn", gCollapseCount, gCollapseCalls, |
| 208 | (double)gCollapseCount / gCollapseCalls, "%"); |
| 209 | #endif |
| 210 | |
| 211 | writer->rewindToOffset(saveOffset); |
| 212 | return true; |
| 213 | } |
| 214 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | void SkPictureRecord::restore() { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 216 | // FIXME: SkDeferredCanvas needs to be refactored to respect |
| 217 | // save/restore balancing so that the following test can be |
| 218 | // turned on permanently. |
| 219 | #if 0 |
| 220 | SkASSERT(fRestoreOffsetStack.count() > 1); |
| 221 | #endif |
| 222 | |
reed@android.com | b4e22d6 | 2009-07-09 15:20:25 +0000 | [diff] [blame] | 223 | // check for underflow |
| 224 | if (fRestoreOffsetStack.count() == 0) { |
| 225 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | } |
reed@android.com | b4e22d6 | 2009-07-09 15:20:25 +0000 | [diff] [blame] | 227 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 228 | if (fRestoreOffsetStack.count() == fFirstSavedLayerIndex) { |
| 229 | fFirstSavedLayerIndex = kNoSavedLayerIndex; |
| 230 | } |
| 231 | |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 232 | if (!collapseSaveClipRestore(&fWriter, fRestoreOffsetStack.top())) { |
| 233 | fillRestoreOffsetPlaceholdersForCurrentStackLevel((uint32_t)fWriter.size()); |
| 234 | this->addDraw(RESTORE); |
| 235 | } |
| 236 | |
reed@android.com | b4e22d6 | 2009-07-09 15:20:25 +0000 | [diff] [blame] | 237 | fRestoreOffsetStack.pop(); |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | validate(); |
| 240 | return this->INHERITED::restore(); |
| 241 | } |
| 242 | |
| 243 | bool SkPictureRecord::translate(SkScalar dx, SkScalar dy) { |
| 244 | addDraw(TRANSLATE); |
| 245 | addScalar(dx); |
| 246 | addScalar(dy); |
| 247 | validate(); |
| 248 | return this->INHERITED::translate(dx, dy); |
| 249 | } |
| 250 | |
| 251 | bool SkPictureRecord::scale(SkScalar sx, SkScalar sy) { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 252 | addDraw(SCALE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | addScalar(sx); |
| 254 | addScalar(sy); |
| 255 | validate(); |
| 256 | return this->INHERITED::scale(sx, sy); |
| 257 | } |
| 258 | |
| 259 | bool SkPictureRecord::rotate(SkScalar degrees) { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 260 | addDraw(ROTATE); |
| 261 | addScalar(degrees); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 262 | validate(); |
| 263 | return this->INHERITED::rotate(degrees); |
| 264 | } |
| 265 | |
| 266 | bool SkPictureRecord::skew(SkScalar sx, SkScalar sy) { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 267 | addDraw(SKEW); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | addScalar(sx); |
| 269 | addScalar(sy); |
| 270 | validate(); |
| 271 | return this->INHERITED::skew(sx, sy); |
| 272 | } |
| 273 | |
| 274 | bool SkPictureRecord::concat(const SkMatrix& matrix) { |
| 275 | validate(); |
| 276 | addDraw(CONCAT); |
| 277 | addMatrix(matrix); |
| 278 | validate(); |
| 279 | return this->INHERITED::concat(matrix); |
| 280 | } |
| 281 | |
reed@android.com | 6e073b9 | 2009-01-06 15:03:30 +0000 | [diff] [blame] | 282 | void SkPictureRecord::setMatrix(const SkMatrix& matrix) { |
| 283 | validate(); |
| 284 | addDraw(SET_MATRIX); |
| 285 | addMatrix(matrix); |
| 286 | validate(); |
| 287 | this->INHERITED::setMatrix(matrix); |
| 288 | } |
| 289 | |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 290 | static bool regionOpExpands(SkRegion::Op op) { |
| 291 | switch (op) { |
| 292 | case SkRegion::kUnion_Op: |
| 293 | case SkRegion::kXOR_Op: |
| 294 | case SkRegion::kReverseDifference_Op: |
| 295 | case SkRegion::kReplace_Op: |
| 296 | return true; |
| 297 | case SkRegion::kIntersect_Op: |
| 298 | case SkRegion::kDifference_Op: |
| 299 | return false; |
| 300 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 301 | SkDEBUGFAIL("unknown region op"); |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | } |
| 305 | |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 306 | void SkPictureRecord::fillRestoreOffsetPlaceholdersForCurrentStackLevel( |
| 307 | uint32_t restoreOffset) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 308 | int32_t offset = fRestoreOffsetStack.top(); |
| 309 | while (offset > 0) { |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 310 | uint32_t* peek = fWriter.peek32(offset); |
| 311 | offset = *peek; |
| 312 | *peek = restoreOffset; |
| 313 | } |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 314 | |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 315 | #ifdef SK_DEBUG |
| 316 | // assert that the final offset value points to a save verb |
| 317 | uint32_t drawOp = *fWriter.peek32(-offset); |
| 318 | SkASSERT(SAVE == drawOp || SAVE_LAYER == drawOp); |
| 319 | #endif |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 320 | } |
| 321 | |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 322 | void SkPictureRecord::beginRecording() { |
| 323 | // we have to call this *after* our constructor, to ensure that it gets |
| 324 | // recorded. This is balanced by restoreToCount() call from endRecording, |
| 325 | // which in-turn calls our overridden restore(), so those get recorded too. |
| 326 | fInitialSaveCount = this->save(kMatrixClip_SaveFlag); |
| 327 | } |
| 328 | |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 329 | void SkPictureRecord::endRecording() { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 330 | SkASSERT(kNoInitialSave != fInitialSaveCount); |
| 331 | this->restoreToCount(fInitialSaveCount); |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 332 | } |
| 333 | |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 334 | void SkPictureRecord::recordRestoreOffsetPlaceholder(SkRegion::Op op) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame^] | 335 | if (fRestoreOffsetStack.isEmpty()) { |
| 336 | return; |
| 337 | } |
| 338 | |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 339 | if (regionOpExpands(op)) { |
| 340 | // Run back through any previous clip ops, and mark their offset to |
| 341 | // be 0, disabling their ability to trigger a jump-to-restore, otherwise |
| 342 | // they could hide this clips ability to expand the clip (i.e. go from |
| 343 | // empty to non-empty). |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 344 | fillRestoreOffsetPlaceholdersForCurrentStackLevel(0); |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 345 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 346 | |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 347 | size_t offset = fWriter.size(); |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 348 | // The RestoreOffset field is initially filled with a placeholder |
| 349 | // value that points to the offset of the previous RestoreOffset |
| 350 | // in the current stack level, thus forming a linked list so that |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 351 | // the restore offsets can be filled in when the corresponding |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 352 | // restore command is recorded. |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 353 | addInt(fRestoreOffsetStack.top()); |
| 354 | fRestoreOffsetStack.top() = offset; |
| 355 | } |
| 356 | |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 357 | bool SkPictureRecord::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 358 | addDraw(CLIP_RECT); |
| 359 | addRect(rect); |
reed@google.com | 83ab495 | 2011-11-11 21:34:54 +0000 | [diff] [blame] | 360 | addInt(ClipParams_pack(op, doAA)); |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 361 | recordRestoreOffsetPlaceholder(op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | |
| 363 | validate(); |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 364 | return this->INHERITED::clipRect(rect, op, doAA); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 365 | } |
| 366 | |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 367 | bool SkPictureRecord::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 368 | addDraw(CLIP_PATH); |
| 369 | addPath(path); |
reed@google.com | 83ab495 | 2011-11-11 21:34:54 +0000 | [diff] [blame] | 370 | addInt(ClipParams_pack(op, doAA)); |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 371 | recordRestoreOffsetPlaceholder(op); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 372 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | validate(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 374 | |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 375 | if (fRecordFlags & SkPicture::kUsePathBoundsForClip_RecordingFlag) { |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 376 | return this->INHERITED::clipRect(path.getBounds(), op, doAA); |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 377 | } else { |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 378 | return this->INHERITED::clipPath(path, op, doAA); |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 379 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | bool SkPictureRecord::clipRegion(const SkRegion& region, SkRegion::Op op) { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 383 | addDraw(CLIP_REGION); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 384 | addRegion(region); |
reed@google.com | 83ab495 | 2011-11-11 21:34:54 +0000 | [diff] [blame] | 385 | addInt(ClipParams_pack(op, false)); |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 386 | recordRestoreOffsetPlaceholder(op); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 387 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | validate(); |
| 389 | return this->INHERITED::clipRegion(region, op); |
| 390 | } |
| 391 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 392 | void SkPictureRecord::clear(SkColor color) { |
| 393 | addDraw(DRAW_CLEAR); |
| 394 | addInt(color); |
| 395 | validate(); |
| 396 | } |
| 397 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | void SkPictureRecord::drawPaint(const SkPaint& paint) { |
| 399 | addDraw(DRAW_PAINT); |
| 400 | addPaint(paint); |
| 401 | validate(); |
| 402 | } |
| 403 | |
| 404 | void SkPictureRecord::drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 405 | const SkPaint& paint) { |
| 406 | addDraw(DRAW_POINTS); |
| 407 | addPaint(paint); |
| 408 | addInt(mode); |
| 409 | addInt(count); |
| 410 | fWriter.writeMul4(pts, count * sizeof(SkPoint)); |
| 411 | validate(); |
| 412 | } |
| 413 | |
| 414 | void SkPictureRecord::drawRect(const SkRect& rect, const SkPaint& paint) { |
| 415 | addDraw(DRAW_RECT); |
| 416 | addPaint(paint); |
| 417 | addRect(rect); |
| 418 | validate(); |
| 419 | } |
| 420 | |
| 421 | void SkPictureRecord::drawPath(const SkPath& path, const SkPaint& paint) { |
| 422 | addDraw(DRAW_PATH); |
| 423 | addPaint(paint); |
| 424 | addPath(path); |
| 425 | validate(); |
| 426 | } |
| 427 | |
| 428 | void SkPictureRecord::drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
| 429 | const SkPaint* paint = NULL) { |
| 430 | addDraw(DRAW_BITMAP); |
| 431 | addPaintPtr(paint); |
| 432 | addBitmap(bitmap); |
| 433 | addScalar(left); |
| 434 | addScalar(top); |
| 435 | validate(); |
| 436 | } |
| 437 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 438 | void SkPictureRecord::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 439 | const SkRect& dst, const SkPaint* paint) { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 440 | addDraw(DRAW_BITMAP_RECT_TO_RECT); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 441 | addPaintPtr(paint); |
| 442 | addBitmap(bitmap); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 443 | addRectPtr(src); // may be null |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 444 | addRect(dst); |
| 445 | validate(); |
| 446 | } |
| 447 | |
| 448 | void SkPictureRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix, |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 449 | const SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 450 | addDraw(DRAW_BITMAP_MATRIX); |
| 451 | addPaintPtr(paint); |
| 452 | addBitmap(bitmap); |
| 453 | addMatrix(matrix); |
| 454 | validate(); |
| 455 | } |
| 456 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 457 | void SkPictureRecord::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 458 | const SkRect& dst, const SkPaint* paint) { |
| 459 | addDraw(DRAW_BITMAP_NINE); |
| 460 | addPaintPtr(paint); |
| 461 | addBitmap(bitmap); |
| 462 | addIRect(center); |
| 463 | addRect(dst); |
| 464 | validate(); |
| 465 | } |
| 466 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 467 | void SkPictureRecord::drawSprite(const SkBitmap& bitmap, int left, int top, |
| 468 | const SkPaint* paint = NULL) { |
| 469 | addDraw(DRAW_SPRITE); |
| 470 | addPaintPtr(paint); |
| 471 | addBitmap(bitmap); |
| 472 | addInt(left); |
| 473 | addInt(top); |
| 474 | validate(); |
| 475 | } |
| 476 | |
| 477 | void SkPictureRecord::addFontMetricsTopBottom(const SkPaint& paint, |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 478 | SkScalar minY, SkScalar maxY) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 479 | SkPaint::FontMetrics metrics; |
| 480 | paint.getFontMetrics(&metrics); |
| 481 | SkRect bounds; |
| 482 | // construct a rect so we can see any adjustments from the paint. |
| 483 | // we use 0,1 for left,right, just so the rect isn't empty |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 484 | bounds.set(0, metrics.fTop + minY, |
| 485 | SK_Scalar1, metrics.fBottom + maxY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 486 | (void)paint.computeFastBounds(bounds, &bounds); |
| 487 | // now record the top and bottom |
| 488 | addScalar(bounds.fTop); |
| 489 | addScalar(bounds.fBottom); |
| 490 | } |
| 491 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 492 | void SkPictureRecord::drawText(const void* text, size_t byteLength, SkScalar x, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 493 | SkScalar y, const SkPaint& paint) { |
reed@google.com | 2eb5bb1 | 2012-04-12 14:27:42 +0000 | [diff] [blame] | 494 | bool fast = !paint.isVerticalText() && paint.canComputeFastBounds(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 495 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 496 | addDraw(fast ? DRAW_TEXT_TOP_BOTTOM : DRAW_TEXT); |
| 497 | addPaint(paint); |
| 498 | addText(text, byteLength); |
| 499 | addScalar(x); |
| 500 | addScalar(y); |
| 501 | if (fast) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 502 | addFontMetricsTopBottom(paint, y, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 503 | } |
| 504 | validate(); |
| 505 | } |
| 506 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 507 | void SkPictureRecord::drawPosText(const void* text, size_t byteLength, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 508 | const SkPoint pos[], const SkPaint& paint) { |
| 509 | size_t points = paint.countText(text, byteLength); |
| 510 | if (0 == points) |
| 511 | return; |
| 512 | |
| 513 | bool canUseDrawH = true; |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 514 | SkScalar minY = pos[0].fY; |
| 515 | SkScalar maxY = pos[0].fY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 516 | // check if the caller really should have used drawPosTextH() |
| 517 | { |
| 518 | const SkScalar firstY = pos[0].fY; |
| 519 | for (size_t index = 1; index < points; index++) { |
| 520 | if (pos[index].fY != firstY) { |
| 521 | canUseDrawH = false; |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 522 | if (pos[index].fY < minY) { |
| 523 | minY = pos[index].fY; |
| 524 | } else if (pos[index].fY > maxY) { |
| 525 | maxY = pos[index].fY; |
| 526 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 530 | |
reed@google.com | 2eb5bb1 | 2012-04-12 14:27:42 +0000 | [diff] [blame] | 531 | bool fastBounds = !paint.isVerticalText() && paint.canComputeFastBounds(); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 532 | bool fast = canUseDrawH && fastBounds; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | |
| 534 | if (fast) { |
| 535 | addDraw(DRAW_POS_TEXT_H_TOP_BOTTOM); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 536 | } else if (canUseDrawH) { |
| 537 | addDraw(DRAW_POS_TEXT_H); |
| 538 | } else if (fastBounds) { |
| 539 | addDraw(DRAW_POS_TEXT_TOP_BOTTOM); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 540 | } else { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 541 | addDraw(DRAW_POS_TEXT); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 542 | } |
| 543 | addPaint(paint); |
| 544 | addText(text, byteLength); |
| 545 | addInt(points); |
| 546 | |
| 547 | #ifdef SK_DEBUG_SIZE |
| 548 | size_t start = fWriter.size(); |
| 549 | #endif |
| 550 | if (canUseDrawH) { |
| 551 | if (fast) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 552 | addFontMetricsTopBottom(paint, pos[0].fY, pos[0].fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 553 | } |
| 554 | addScalar(pos[0].fY); |
| 555 | SkScalar* xptr = (SkScalar*)fWriter.reserve(points * sizeof(SkScalar)); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 556 | for (size_t index = 0; index < points; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 557 | *xptr++ = pos[index].fX; |
| 558 | } |
| 559 | else { |
| 560 | fWriter.writeMul4(pos, points * sizeof(SkPoint)); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 561 | if (fastBounds) { |
| 562 | addFontMetricsTopBottom(paint, minY, maxY); |
| 563 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 564 | } |
| 565 | #ifdef SK_DEBUG_SIZE |
| 566 | fPointBytes += fWriter.size() - start; |
| 567 | fPointWrites += points; |
| 568 | #endif |
| 569 | validate(); |
| 570 | } |
| 571 | |
| 572 | void SkPictureRecord::drawPosTextH(const void* text, size_t byteLength, |
| 573 | const SkScalar xpos[], SkScalar constY, |
| 574 | const SkPaint& paint) { |
| 575 | size_t points = paint.countText(text, byteLength); |
| 576 | if (0 == points) |
| 577 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 578 | |
reed@google.com | 2eb5bb1 | 2012-04-12 14:27:42 +0000 | [diff] [blame] | 579 | bool fast = !paint.isVerticalText() && paint.canComputeFastBounds(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 580 | |
| 581 | addDraw(fast ? DRAW_POS_TEXT_H_TOP_BOTTOM : DRAW_POS_TEXT_H); |
| 582 | addPaint(paint); |
| 583 | addText(text, byteLength); |
| 584 | addInt(points); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 585 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 586 | #ifdef SK_DEBUG_SIZE |
| 587 | size_t start = fWriter.size(); |
| 588 | #endif |
| 589 | if (fast) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 590 | addFontMetricsTopBottom(paint, constY, constY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 591 | } |
| 592 | addScalar(constY); |
| 593 | fWriter.writeMul4(xpos, points * sizeof(SkScalar)); |
| 594 | #ifdef SK_DEBUG_SIZE |
| 595 | fPointBytes += fWriter.size() - start; |
| 596 | fPointWrites += points; |
| 597 | #endif |
| 598 | validate(); |
| 599 | } |
| 600 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 601 | void SkPictureRecord::drawTextOnPath(const void* text, size_t byteLength, |
| 602 | const SkPath& path, const SkMatrix* matrix, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 603 | const SkPaint& paint) { |
| 604 | addDraw(DRAW_TEXT_ON_PATH); |
| 605 | addPaint(paint); |
| 606 | addText(text, byteLength); |
| 607 | addPath(path); |
| 608 | addMatrixPtr(matrix); |
| 609 | validate(); |
| 610 | } |
| 611 | |
| 612 | void SkPictureRecord::drawPicture(SkPicture& picture) { |
| 613 | addDraw(DRAW_PICTURE); |
| 614 | addPicture(picture); |
| 615 | validate(); |
| 616 | } |
| 617 | |
| 618 | void SkPictureRecord::drawVertices(VertexMode vmode, int vertexCount, |
| 619 | const SkPoint vertices[], const SkPoint texs[], |
| 620 | const SkColor colors[], SkXfermode*, |
| 621 | const uint16_t indices[], int indexCount, |
| 622 | const SkPaint& paint) { |
| 623 | uint32_t flags = 0; |
| 624 | if (texs) { |
| 625 | flags |= DRAW_VERTICES_HAS_TEXS; |
| 626 | } |
| 627 | if (colors) { |
| 628 | flags |= DRAW_VERTICES_HAS_COLORS; |
| 629 | } |
| 630 | if (indexCount > 0) { |
| 631 | flags |= DRAW_VERTICES_HAS_INDICES; |
| 632 | } |
| 633 | |
| 634 | addDraw(DRAW_VERTICES); |
| 635 | addPaint(paint); |
| 636 | addInt(flags); |
| 637 | addInt(vmode); |
| 638 | addInt(vertexCount); |
| 639 | addPoints(vertices, vertexCount); |
| 640 | if (flags & DRAW_VERTICES_HAS_TEXS) { |
| 641 | addPoints(texs, vertexCount); |
| 642 | } |
| 643 | if (flags & DRAW_VERTICES_HAS_COLORS) { |
| 644 | fWriter.writeMul4(colors, vertexCount * sizeof(SkColor)); |
| 645 | } |
| 646 | if (flags & DRAW_VERTICES_HAS_INDICES) { |
| 647 | addInt(indexCount); |
| 648 | fWriter.writePad(indices, indexCount * sizeof(uint16_t)); |
| 649 | } |
| 650 | } |
| 651 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 652 | void SkPictureRecord::drawData(const void* data, size_t length) { |
| 653 | addDraw(DRAW_DATA); |
| 654 | addInt(length); |
| 655 | fWriter.writePad(data, length); |
| 656 | } |
| 657 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 658 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 659 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 660 | void SkPictureRecord::addBitmap(const SkBitmap& bitmap) { |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 661 | addInt(fBitmapHeap->insert(bitmap)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | void SkPictureRecord::addMatrix(const SkMatrix& matrix) { |
| 665 | addMatrixPtr(&matrix); |
| 666 | } |
| 667 | |
| 668 | void SkPictureRecord::addMatrixPtr(const SkMatrix* matrix) { |
reed@google.com | 83ca337 | 2012-07-12 15:27:54 +0000 | [diff] [blame] | 669 | this->addInt(matrix ? fMatrices.find(*matrix) : 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void SkPictureRecord::addPaint(const SkPaint& paint) { |
| 673 | addPaintPtr(&paint); |
| 674 | } |
| 675 | |
| 676 | void SkPictureRecord::addPaintPtr(const SkPaint* paint) { |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 677 | this->addInt(paint ? fPaints.find(*paint) : 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void SkPictureRecord::addPath(const SkPath& path) { |
| 681 | if (NULL == fPathHeap) { |
| 682 | fPathHeap = SkNEW(SkPathHeap); |
| 683 | } |
| 684 | addInt(fPathHeap->append(path)); |
| 685 | } |
| 686 | |
| 687 | void SkPictureRecord::addPicture(SkPicture& picture) { |
| 688 | int index = fPictureRefs.find(&picture); |
| 689 | if (index < 0) { // not found |
| 690 | index = fPictureRefs.count(); |
| 691 | *fPictureRefs.append() = &picture; |
| 692 | picture.ref(); |
| 693 | } |
| 694 | // follow the convention of recording a 1-based index |
| 695 | addInt(index + 1); |
| 696 | } |
| 697 | |
| 698 | void SkPictureRecord::addPoint(const SkPoint& point) { |
| 699 | #ifdef SK_DEBUG_SIZE |
| 700 | size_t start = fWriter.size(); |
| 701 | #endif |
| 702 | fWriter.writePoint(point); |
| 703 | #ifdef SK_DEBUG_SIZE |
| 704 | fPointBytes += fWriter.size() - start; |
| 705 | fPointWrites++; |
| 706 | #endif |
| 707 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 708 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 709 | void SkPictureRecord::addPoints(const SkPoint pts[], int count) { |
| 710 | fWriter.writeMul4(pts, count * sizeof(SkPoint)); |
| 711 | #ifdef SK_DEBUG_SIZE |
| 712 | fPointBytes += count * sizeof(SkPoint); |
| 713 | fPointWrites++; |
| 714 | #endif |
| 715 | } |
| 716 | |
| 717 | void SkPictureRecord::addRect(const SkRect& rect) { |
| 718 | #ifdef SK_DEBUG_SIZE |
| 719 | size_t start = fWriter.size(); |
| 720 | #endif |
| 721 | fWriter.writeRect(rect); |
| 722 | #ifdef SK_DEBUG_SIZE |
| 723 | fRectBytes += fWriter.size() - start; |
| 724 | fRectWrites++; |
| 725 | #endif |
| 726 | } |
| 727 | |
| 728 | void SkPictureRecord::addRectPtr(const SkRect* rect) { |
| 729 | if (fWriter.writeBool(rect != NULL)) { |
| 730 | fWriter.writeRect(*rect); |
| 731 | } |
| 732 | } |
| 733 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 734 | void SkPictureRecord::addIRect(const SkIRect& rect) { |
| 735 | fWriter.write(&rect, sizeof(rect)); |
| 736 | } |
| 737 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 738 | void SkPictureRecord::addIRectPtr(const SkIRect* rect) { |
| 739 | if (fWriter.writeBool(rect != NULL)) { |
| 740 | *(SkIRect*)fWriter.reserve(sizeof(SkIRect)) = *rect; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | void SkPictureRecord::addRegion(const SkRegion& region) { |
reed@google.com | 83ca337 | 2012-07-12 15:27:54 +0000 | [diff] [blame] | 745 | addInt(fRegions.find(region)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | void SkPictureRecord::addText(const void* text, size_t byteLength) { |
| 749 | #ifdef SK_DEBUG_SIZE |
| 750 | size_t start = fWriter.size(); |
| 751 | #endif |
| 752 | addInt(byteLength); |
| 753 | fWriter.writePad(text, byteLength); |
| 754 | #ifdef SK_DEBUG_SIZE |
| 755 | fTextBytes += fWriter.size() - start; |
| 756 | fTextWrites++; |
| 757 | #endif |
| 758 | } |
| 759 | |
| 760 | /////////////////////////////////////////////////////////////////////////////// |
| 761 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 762 | #ifdef SK_DEBUG_SIZE |
| 763 | size_t SkPictureRecord::size() const { |
| 764 | size_t result = 0; |
| 765 | size_t sizeData; |
| 766 | bitmaps(&sizeData); |
| 767 | result += sizeData; |
| 768 | matrices(&sizeData); |
| 769 | result += sizeData; |
| 770 | paints(&sizeData); |
| 771 | result += sizeData; |
| 772 | paths(&sizeData); |
| 773 | result += sizeData; |
| 774 | pictures(&sizeData); |
| 775 | result += sizeData; |
| 776 | regions(&sizeData); |
| 777 | result += sizeData; |
| 778 | result += streamlen(); |
| 779 | return result; |
| 780 | } |
| 781 | |
| 782 | int SkPictureRecord::bitmaps(size_t* size) const { |
| 783 | size_t result = 0; |
| 784 | int count = fBitmaps.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 785 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 786 | result += sizeof(fBitmaps[index]) + fBitmaps[index]->size(); |
| 787 | *size = result; |
| 788 | return count; |
| 789 | } |
| 790 | |
| 791 | int SkPictureRecord::matrices(size_t* size) const { |
| 792 | int count = fMatrices.count(); |
| 793 | *size = sizeof(fMatrices[0]) * count; |
| 794 | return count; |
| 795 | } |
| 796 | |
| 797 | int SkPictureRecord::paints(size_t* size) const { |
| 798 | size_t result = 0; |
| 799 | int count = fPaints.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 800 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | result += sizeof(fPaints[index]) + fPaints[index]->size(); |
| 802 | *size = result; |
| 803 | return count; |
| 804 | } |
| 805 | |
| 806 | int SkPictureRecord::paths(size_t* size) const { |
| 807 | size_t result = 0; |
| 808 | int count = fPaths.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 809 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 810 | result += sizeof(fPaths[index]) + fPaths[index]->size(); |
| 811 | *size = result; |
| 812 | return count; |
| 813 | } |
| 814 | |
| 815 | int SkPictureRecord::regions(size_t* size) const { |
| 816 | size_t result = 0; |
| 817 | int count = fRegions.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 818 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 819 | result += sizeof(fRegions[index]) + fRegions[index]->size(); |
| 820 | *size = result; |
| 821 | return count; |
| 822 | } |
| 823 | |
| 824 | size_t SkPictureRecord::streamlen() const { |
| 825 | return fWriter.size(); |
| 826 | } |
| 827 | #endif |
| 828 | |
| 829 | #ifdef SK_DEBUG_VALIDATE |
| 830 | void SkPictureRecord::validate() const { |
| 831 | validateBitmaps(); |
| 832 | validateMatrices(); |
| 833 | validatePaints(); |
| 834 | validatePaths(); |
| 835 | validatePictures(); |
| 836 | validateRegions(); |
| 837 | } |
| 838 | |
| 839 | void SkPictureRecord::validateBitmaps() const { |
| 840 | int count = fBitmaps.count(); |
| 841 | SkASSERT((unsigned) count < 0x1000); |
| 842 | for (int index = 0; index < count; index++) { |
| 843 | const SkFlatBitmap* bitPtr = fBitmaps[index]; |
| 844 | SkASSERT(bitPtr); |
| 845 | bitPtr->validate(); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | void SkPictureRecord::validateMatrices() const { |
| 850 | int count = fMatrices.count(); |
| 851 | SkASSERT((unsigned) count < 0x1000); |
| 852 | for (int index = 0; index < count; index++) { |
| 853 | const SkFlatMatrix* matrix = fMatrices[index]; |
| 854 | SkASSERT(matrix); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 855 | matrix->validate(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | void SkPictureRecord::validatePaints() const { |
| 860 | int count = fPaints.count(); |
| 861 | SkASSERT((unsigned) count < 0x1000); |
| 862 | for (int index = 0; index < count; index++) { |
| 863 | const SkFlatPaint* paint = fPaints[index]; |
| 864 | SkASSERT(paint); |
| 865 | // paint->validate(); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | void SkPictureRecord::validatePaths() const { |
| 870 | int count = fPaths.count(); |
| 871 | SkASSERT((unsigned) count < 0x1000); |
| 872 | for (int index = 0; index < count; index++) { |
| 873 | const SkFlatPath* path = fPaths[index]; |
| 874 | SkASSERT(path); |
| 875 | path->validate(); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | void SkPictureRecord::validateRegions() const { |
| 880 | int count = fRegions.count(); |
| 881 | SkASSERT((unsigned) count < 0x1000); |
| 882 | for (int index = 0; index < count; index++) { |
| 883 | const SkFlatRegion* region = fRegions[index]; |
| 884 | SkASSERT(region); |
| 885 | region->validate(); |
| 886 | } |
| 887 | } |
| 888 | #endif |
| 889 | |