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" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 11 | #include "SkRRect.h" |
rileya@google.com | 9f5898d | 2012-09-11 20:21:44 +0000 | [diff] [blame] | 12 | #include "SkBBoxHierarchy.h" |
| 13 | #include "SkPictureStateTree.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
| 15 | #define MIN_WRITER_SIZE 16384 |
| 16 | #define HEAP_BLOCK_SIZE 4096 |
| 17 | |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 18 | enum { |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 19 | // just need a value that save or getSaveCount would never return |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 20 | kNoInitialSave = -1, |
| 21 | }; |
| 22 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 23 | // A lot of basic types get stored as a uint32_t: bools, ints, paint indices, etc. |
| 24 | static int const kUInt32Size = 4; |
| 25 | |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 26 | static const uint32_t kSaveLayerNoBoundsSize = 4 * kUInt32Size; |
| 27 | static const uint32_t kSaveLayerWithBoundsSize = 4 * kUInt32Size + sizeof(SkRect); |
| 28 | |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 29 | SkPictureRecord::SkPictureRecord(uint32_t flags, SkDevice* device) : |
| 30 | INHERITED(device), |
robertphillips@google.com | 178a267 | 2012-09-13 13:25:30 +0000 | [diff] [blame] | 31 | fBoundingHierarchy(NULL), |
| 32 | fStateTree(NULL), |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 33 | fFlattenableHeap(HEAP_BLOCK_SIZE), |
| 34 | fMatrices(&fFlattenableHeap), |
| 35 | fPaints(&fFlattenableHeap), |
| 36 | fRegions(&fFlattenableHeap), |
djsollen@google.com | d2700ee | 2012-05-30 16:54:13 +0000 | [diff] [blame] | 37 | fWriter(MIN_WRITER_SIZE), |
| 38 | fRecordFlags(flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | #ifdef SK_DEBUG_SIZE |
| 40 | fPointBytes = fRectBytes = fTextBytes = 0; |
| 41 | fPointWrites = fRectWrites = fTextWrites = 0; |
| 42 | #endif |
| 43 | |
| 44 | fRestoreOffsetStack.setReserve(32); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 45 | |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 46 | fBitmapHeap = SkNEW(SkBitmapHeap); |
| 47 | fFlattenableHeap.setBitmapStorage(fBitmapHeap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | fPathHeap = NULL; // lazy allocate |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 49 | fFirstSavedLayerIndex = kNoSavedLayerIndex; |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 50 | |
| 51 | fInitialSaveCount = kNoInitialSave; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | SkPictureRecord::~SkPictureRecord() { |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 55 | SkSafeUnref(fBitmapHeap); |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 56 | SkSafeUnref(fPathHeap); |
rileya@google.com | 9f5898d | 2012-09-11 20:21:44 +0000 | [diff] [blame] | 57 | SkSafeUnref(fBoundingHierarchy); |
| 58 | SkSafeUnref(fStateTree); |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 59 | fFlattenableHeap.setBitmapStorage(NULL); |
| 60 | fPictureRefs.unrefAll(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 65 | // Return the offset of the paint inside a given op's byte stream. A zero |
| 66 | // return value means there is no paint (and you really shouldn't be calling |
| 67 | // this method) |
| 68 | static inline uint32_t getPaintOffset(DrawType op, uint32_t opSize) { |
| 69 | // These offsets are where the paint would be if the op size doesn't overflow |
| 70 | static const uint8_t gPaintOffsets[LAST_DRAWTYPE_ENUM + 1] = { |
| 71 | 0, // UNUSED - no paint |
| 72 | 0, // CLIP_PATH - no paint |
| 73 | 0, // CLIP_REGION - no paint |
| 74 | 0, // CLIP_RECT - no paint |
| 75 | 0, // CLIP_RRECT - no paint |
| 76 | 0, // CONCAT - no paint |
| 77 | 1, // DRAW_BITMAP - right after op code |
| 78 | 1, // DRAW_BITMAP_MATRIX - right after op code |
| 79 | 1, // DRAW_BITMAP_NINE - right after op code |
| 80 | 1, // DRAW_BITMAP_RECT_TO_RECT - right after op code |
| 81 | 0, // DRAW_CLEAR - no paint |
| 82 | 0, // DRAW_DATA - no paint |
| 83 | 1, // DRAW_OVAL - right after op code |
| 84 | 1, // DRAW_PAINT - right after op code |
| 85 | 1, // DRAW_PATH - right after op code |
| 86 | 0, // DRAW_PICTURE - no paint |
| 87 | 1, // DRAW_POINTS - right after op code |
| 88 | 1, // DRAW_POS_TEXT - right after op code |
| 89 | 1, // DRAW_POS_TEXT_TOP_BOTTOM - right after op code |
| 90 | 1, // DRAW_POS_TEXT_H - right after op code |
| 91 | 1, // DRAW_POS_TEXT_H_TOP_BOTTOM - right after op code |
| 92 | 1, // DRAW_RECT - right after op code |
| 93 | 1, // DRAW_RRECT - right after op code |
| 94 | 1, // DRAW_SPRITE - right after op code |
| 95 | 1, // DRAW_TEXT - right after op code |
| 96 | 1, // DRAW_TEXT_ON_PATH - right after op code |
| 97 | 1, // DRAW_TEXT_TOP_BOTTOM - right after op code |
| 98 | 1, // DRAW_VERTICES - right after op code |
| 99 | 0, // RESTORE - no paint |
| 100 | 0, // ROTATE - no paint |
| 101 | 0, // SAVE - no paint |
| 102 | 0, // SAVE_LAYER - see below - this paint's location varies |
| 103 | 0, // SCALE - no paint |
| 104 | 0, // SET_MATRIX - no paint |
| 105 | 0, // SKEW - no paint |
| 106 | 0, // TRANSLATE - no paint |
| 107 | 0, // NOOP - no paint |
| 108 | }; |
| 109 | |
| 110 | SkASSERT(sizeof(gPaintOffsets) == LAST_DRAWTYPE_ENUM + 1); |
| 111 | SkASSERT((unsigned)op <= (unsigned)LAST_DRAWTYPE_ENUM); |
| 112 | |
| 113 | int overflow = 0; |
| 114 | if (0 != (opSize & ~MASK_24) || opSize == MASK_24) { |
| 115 | // This op's size overflows so an extra uint32_t will be written |
| 116 | // after the op code |
| 117 | overflow = sizeof(uint32_t); |
| 118 | } |
| 119 | |
| 120 | if (SAVE_LAYER == op) { |
| 121 | static const uint32_t kSaveLayerNoBoundsPaintOffset = 2 * kUInt32Size; |
| 122 | static const uint32_t kSaveLayerWithBoundsPaintOffset = 2 * kUInt32Size + sizeof(SkRect); |
| 123 | |
| 124 | if (kSaveLayerNoBoundsSize == opSize) { |
| 125 | return kSaveLayerNoBoundsPaintOffset + overflow; |
| 126 | } else { |
| 127 | SkASSERT(kSaveLayerWithBoundsSize == opSize); |
| 128 | return kSaveLayerWithBoundsPaintOffset + overflow; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | SkASSERT(0 != gPaintOffsets[op]); // really shouldn't be calling this method |
| 133 | return gPaintOffsets[op] * sizeof(uint32_t) + overflow; |
| 134 | } |
| 135 | |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 136 | SkDevice* SkPictureRecord::setDevice(SkDevice* device) { |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 137 | SkASSERT(!"eeek, don't try to change the device on a recording canvas"); |
| 138 | return this->INHERITED::setDevice(device); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 139 | } |
| 140 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | int SkPictureRecord::save(SaveFlags flags) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 142 | // record the offset to us, making it non-positive to distinguish a save |
| 143 | // from a clip entry. |
| 144 | fRestoreOffsetStack.push(-(int32_t)fWriter.size()); |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 145 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 146 | // op + flags |
| 147 | uint32_t size = 2 * kUInt32Size; |
| 148 | uint32_t initialOffset = this->addDraw(SAVE, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | addInt(flags); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 150 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 151 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | return this->INHERITED::save(flags); |
| 153 | } |
| 154 | |
| 155 | int SkPictureRecord::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 156 | SaveFlags flags) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 157 | // record the offset to us, making it non-positive to distinguish a save |
| 158 | // from a clip entry. |
| 159 | fRestoreOffsetStack.push(-(int32_t)fWriter.size()); |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 160 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 161 | // op + bool for 'bounds' |
skia.committer@gmail.com | ce8343d | 2013-02-16 07:01:31 +0000 | [diff] [blame] | 162 | uint32_t size = 2 * kUInt32Size; |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 163 | if (NULL != bounds) { |
| 164 | size += sizeof(*bounds); // + rect |
| 165 | } |
| 166 | // + paint index + flags |
| 167 | size += 2 * kUInt32Size; |
| 168 | |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 169 | SkASSERT(kSaveLayerNoBoundsSize == size || kSaveLayerWithBoundsSize == size); |
| 170 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 171 | uint32_t initialOffset = this->addDraw(SAVE_LAYER, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | addRectPtr(bounds); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 173 | SkASSERT(initialOffset+getPaintOffset(SAVE_LAYER, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | addPaintPtr(paint); |
| 175 | addInt(flags); |
| 176 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 177 | if (kNoSavedLayerIndex == fFirstSavedLayerIndex) { |
| 178 | fFirstSavedLayerIndex = fRestoreOffsetStack.count(); |
| 179 | } |
| 180 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 181 | validate(initialOffset, size); |
reed@android.com | 261ae4d | 2009-10-02 16:37:46 +0000 | [diff] [blame] | 182 | /* Don't actually call saveLayer, because that will try to allocate an |
| 183 | offscreen device (potentially very big) which we don't actually need |
| 184 | at this time (and may not be able to afford since during record our |
| 185 | clip starts out the size of the picture, which is often much larger |
| 186 | than the size of the actual device we'll use during playback). |
| 187 | */ |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 188 | int count = this->INHERITED::save(flags); |
| 189 | this->clipRectBounds(bounds, flags, NULL); |
| 190 | return count; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | } |
| 192 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 193 | bool SkPictureRecord::isDrawingToLayer() const { |
| 194 | return fFirstSavedLayerIndex != kNoSavedLayerIndex; |
| 195 | } |
| 196 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 197 | /* |
| 198 | * Read the op code from 'offset' in 'writer' and extract the size too. |
| 199 | */ |
| 200 | static DrawType peek_op_and_size(SkWriter32* writer, int32_t offset, uint32_t* size) { |
| 201 | uint32_t* peek = writer->peek32(offset); |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 202 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 203 | uint32_t op; |
| 204 | UNPACK_8_24(*peek, op, *size); |
| 205 | if (MASK_24 == *size) { |
| 206 | // size required its own slot right after the op code |
skia.committer@gmail.com | ce8343d | 2013-02-16 07:01:31 +0000 | [diff] [blame] | 207 | *size = *writer->peek32(offset+kUInt32Size); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 208 | } |
| 209 | return (DrawType) op; |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | #ifdef TRACK_COLLAPSE_STATS |
| 213 | static int gCollapseCount, gCollapseCalls; |
| 214 | #endif |
| 215 | |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 216 | // Is the supplied paint simply a color? |
| 217 | static bool is_simple(const SkPaint& p) { |
| 218 | intptr_t orAccum = (intptr_t)p.getPathEffect() | |
| 219 | (intptr_t)p.getShader() | |
| 220 | (intptr_t)p.getXfermode() | |
| 221 | (intptr_t)p.getMaskFilter() | |
| 222 | (intptr_t)p.getColorFilter() | |
| 223 | (intptr_t)p.getRasterizer() | |
| 224 | (intptr_t)p.getLooper() | |
| 225 | (intptr_t)p.getImageFilter(); |
| 226 | return 0 == orAccum; |
| 227 | } |
| 228 | |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 229 | /* |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 230 | * Restore has just been called (but not recorded), look back at the |
| 231 | * matching save* and see if we are in the configuration: |
| 232 | * SAVE_LAYER |
| 233 | * DRAW_BITMAP|DRAW_BITMAP_MATRIX|DRAW_BITMAP_NINE|DRAW_BITMAP_RECT_TO_RECT |
| 234 | * RESTORE |
| 235 | * where the saveLayer's color can be moved into the drawBitmap*'s paint |
| 236 | */ |
| 237 | static bool remove_save_layer1(SkWriter32* writer, int32_t offset, |
| 238 | SkPaintDictionary* paintDict) { |
| 239 | |
| 240 | int32_t restoreOffset = (int32_t)writer->size(); |
| 241 | |
| 242 | // back up to the save block |
| 243 | // TODO: add a stack to track save*/restore offsets rather than searching backwards |
| 244 | while (offset > 0) { |
| 245 | offset = *writer->peek32(offset); |
| 246 | } |
| 247 | |
| 248 | // now offset points to a save |
| 249 | uint32_t saveLayerOffset = -offset; |
| 250 | uint32_t saveLayerSize; |
| 251 | DrawType op = peek_op_and_size(writer, saveLayerOffset, &saveLayerSize); |
| 252 | if (SAVE_LAYER != op) { |
| 253 | SkASSERT(SAVE == op); |
| 254 | return false; // not a match |
| 255 | } |
| 256 | |
| 257 | if (kSaveLayerWithBoundsSize == saveLayerSize) { |
| 258 | // The saveLayer's bound can offset where the dbm is drawn |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | // step forward one - check if it is a DRAW_BITMAP* |
| 263 | int32_t dbmOffset = saveLayerOffset + saveLayerSize; |
| 264 | if (dbmOffset >= restoreOffset) { |
| 265 | // Just a saveLayer and a restore? Remove it. |
| 266 | writer->rewindToOffset(saveLayerOffset); |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | uint32_t dbmSize; |
| 271 | op = peek_op_and_size(writer, dbmOffset, &dbmSize); |
| 272 | if (DRAW_BITMAP != op && DRAW_BITMAP_MATRIX != op && |
| 273 | DRAW_BITMAP_NINE != op && DRAW_BITMAP_RECT_TO_RECT != op) { |
| 274 | return false; // not a match |
| 275 | } |
| 276 | |
| 277 | offset = dbmOffset + dbmSize; |
| 278 | if (offset < restoreOffset) { |
| 279 | return false; // something else between the dbm* and the restore |
| 280 | } |
| 281 | |
| 282 | uint32_t dbmPaintOffset = getPaintOffset(op, dbmSize); |
| 283 | uint32_t slPaintOffset = getPaintOffset(SAVE_LAYER, saveLayerSize); |
| 284 | |
| 285 | // we have a match, now we need to get the paints involved |
| 286 | int dbmPaintId = *((int32_t*)writer->peek32(dbmOffset+dbmPaintOffset)); |
| 287 | int saveLayerPaintId = *((int32_t*)writer->peek32(saveLayerOffset+slPaintOffset)); |
| 288 | |
| 289 | if (0 == saveLayerPaintId) { |
| 290 | // In this case the saveLayer/restore isn't needed at all - just kill the saveLayer |
| 291 | // and signal the caller (by returning true) to not add the RESTORE op |
| 292 | uint32_t* ptr = writer->peek32(saveLayerOffset); |
| 293 | *ptr = (*ptr & MASK_24) | (NOOP << 24); |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | if (0 == dbmPaintId) { |
| 298 | // In this case just make the DBM* use the saveLayer's paint, kill the saveLayer |
| 299 | // and signal the caller (by returning true) to not add the RESTORE op |
| 300 | uint32_t* ptr = writer->peek32(saveLayerOffset); |
| 301 | *ptr = (*ptr & MASK_24) | (NOOP << 24); |
| 302 | ptr = writer->peek32(dbmOffset+dbmPaintOffset); |
| 303 | SkASSERT(0 == *ptr); |
| 304 | *ptr = saveLayerPaintId; |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | SkAutoTDelete<SkPaint> saveLayerPaint(paintDict->unflatten(saveLayerPaintId)); |
| 309 | if (NULL == saveLayerPaint.get() || !is_simple(*saveLayerPaint)) { |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | // For this optimization we only fold the saveLayer and drawBitmapRect |
| 314 | // together if the saveLayer's draw is simple (i.e., no fancy effects) and |
| 315 | // and the only difference in the colors is that the saveLayer's can have |
| 316 | // an alpha while the drawBitmapRect's is opaque. |
| 317 | // TODO: it should be possible to fold them together even if they both |
| 318 | // have different non-255 alphas |
| 319 | SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque |
| 320 | |
| 321 | SkAutoTDelete<SkPaint> dbmPaint(paintDict->unflatten(dbmPaintId)); |
| 322 | if (NULL == dbmPaint.get() || dbmPaint->getColor() != layerColor) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | SkColor newColor = SkColorSetA(dbmPaint->getColor(), |
| 327 | SkColorGetA(saveLayerPaint->getColor())); |
| 328 | dbmPaint->setColor(newColor); |
| 329 | |
| 330 | const SkFlatData* data = paintDict->findAndReturnFlat(*dbmPaint); |
| 331 | if (NULL == data) { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | // kill the saveLayer and alter the DBMR2R's paint to be the modified one |
| 336 | uint32_t* ptr = writer->peek32(saveLayerOffset); |
| 337 | *ptr = (*ptr & MASK_24) | (NOOP << 24); |
| 338 | ptr = writer->peek32(dbmOffset+dbmPaintOffset); |
| 339 | *ptr = data->index(); |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* |
| 345 | * Restore has just been called (but not recorded), so look back at the |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 346 | * matching save(), and see if we can eliminate the pair of them, due to no |
| 347 | * intervening matrix/clip calls. |
| 348 | * |
| 349 | * If so, update the writer and return true, in which case we won't even record |
| 350 | * the restore() call. If we still need the restore(), return false. |
| 351 | */ |
| 352 | static bool collapseSaveClipRestore(SkWriter32* writer, int32_t offset) { |
| 353 | #ifdef TRACK_COLLAPSE_STATS |
| 354 | gCollapseCalls += 1; |
| 355 | #endif |
| 356 | |
| 357 | int32_t restoreOffset = (int32_t)writer->size(); |
| 358 | |
| 359 | // back up to the save block |
| 360 | while (offset > 0) { |
| 361 | offset = *writer->peek32(offset); |
| 362 | } |
| 363 | |
| 364 | // now offset points to a save |
| 365 | offset = -offset; |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 366 | uint32_t opSize; |
| 367 | DrawType op = peek_op_and_size(writer, offset, &opSize); |
| 368 | if (SAVE_LAYER == op) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 369 | // not ready to cull these out yet (mrr) |
| 370 | return false; |
| 371 | } |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 372 | SkASSERT(SAVE == op); |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 373 | |
| 374 | // Walk forward until we get back to either a draw-verb (abort) or we hit |
| 375 | // our restore (success). |
| 376 | int32_t saveOffset = offset; |
| 377 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 378 | offset += opSize; |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 379 | while (offset < restoreOffset) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 380 | op = peek_op_and_size(writer, offset, &opSize); |
| 381 | if ((op > CONCAT && op < ROTATE) || (SAVE_LAYER == op)) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 382 | // drawing verb, abort |
| 383 | return false; |
| 384 | } |
| 385 | offset += opSize; |
| 386 | } |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 387 | |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 388 | #ifdef TRACK_COLLAPSE_STATS |
| 389 | gCollapseCount += 1; |
| 390 | SkDebugf("Collapse [%d out of %d] %g%spn", gCollapseCount, gCollapseCalls, |
| 391 | (double)gCollapseCount / gCollapseCalls, "%"); |
| 392 | #endif |
| 393 | |
| 394 | writer->rewindToOffset(saveOffset); |
| 395 | return true; |
| 396 | } |
| 397 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | void SkPictureRecord::restore() { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 399 | // FIXME: SkDeferredCanvas needs to be refactored to respect |
| 400 | // save/restore balancing so that the following test can be |
| 401 | // turned on permanently. |
| 402 | #if 0 |
| 403 | SkASSERT(fRestoreOffsetStack.count() > 1); |
| 404 | #endif |
| 405 | |
reed@android.com | b4e22d6 | 2009-07-09 15:20:25 +0000 | [diff] [blame] | 406 | // check for underflow |
| 407 | if (fRestoreOffsetStack.count() == 0) { |
| 408 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 409 | } |
reed@android.com | b4e22d6 | 2009-07-09 15:20:25 +0000 | [diff] [blame] | 410 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 411 | if (fRestoreOffsetStack.count() == fFirstSavedLayerIndex) { |
| 412 | fFirstSavedLayerIndex = kNoSavedLayerIndex; |
| 413 | } |
| 414 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 415 | uint32_t initialOffset, size; |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 416 | if (!collapseSaveClipRestore(&fWriter, fRestoreOffsetStack.top()) && |
| 417 | !remove_save_layer1(&fWriter, fRestoreOffsetStack.top(), &fPaints)) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 418 | fillRestoreOffsetPlaceholdersForCurrentStackLevel((uint32_t)fWriter.size()); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 419 | // op |
| 420 | size = 1 * kUInt32Size; |
| 421 | initialOffset = this->addDraw(RESTORE, &size); |
| 422 | } else { |
| 423 | size = 0; |
| 424 | initialOffset = fWriter.size(); |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 425 | } |
| 426 | |
reed@android.com | b4e22d6 | 2009-07-09 15:20:25 +0000 | [diff] [blame] | 427 | fRestoreOffsetStack.pop(); |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 428 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 429 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | return this->INHERITED::restore(); |
| 431 | } |
| 432 | |
| 433 | bool SkPictureRecord::translate(SkScalar dx, SkScalar dy) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 434 | // op + dx + dy |
| 435 | uint32_t size = 1 * kUInt32Size + 2 * sizeof(SkScalar); |
| 436 | uint32_t initialOffset = this->addDraw(TRANSLATE, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | addScalar(dx); |
| 438 | addScalar(dy); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 439 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | return this->INHERITED::translate(dx, dy); |
| 441 | } |
| 442 | |
| 443 | bool SkPictureRecord::scale(SkScalar sx, SkScalar sy) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 444 | // op + sx + sy |
| 445 | uint32_t size = 1 * kUInt32Size + 2 * sizeof(SkScalar); |
| 446 | uint32_t initialOffset = this->addDraw(SCALE, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 447 | addScalar(sx); |
| 448 | addScalar(sy); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 449 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 450 | return this->INHERITED::scale(sx, sy); |
| 451 | } |
| 452 | |
| 453 | bool SkPictureRecord::rotate(SkScalar degrees) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 454 | // op + degrees |
| 455 | uint32_t size = 1 * kUInt32Size + sizeof(SkScalar); |
| 456 | uint32_t initialOffset = this->addDraw(ROTATE, &size); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 457 | addScalar(degrees); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 458 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 459 | return this->INHERITED::rotate(degrees); |
| 460 | } |
| 461 | |
| 462 | bool SkPictureRecord::skew(SkScalar sx, SkScalar sy) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 463 | // op + sx + sy |
| 464 | uint32_t size = 1 * kUInt32Size + 2 * sizeof(SkScalar); |
| 465 | uint32_t initialOffset = this->addDraw(SKEW, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 466 | addScalar(sx); |
| 467 | addScalar(sy); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 468 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 469 | return this->INHERITED::skew(sx, sy); |
| 470 | } |
| 471 | |
| 472 | bool SkPictureRecord::concat(const SkMatrix& matrix) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 473 | validate(fWriter.size(), 0); |
| 474 | // op + matrix index |
| 475 | uint32_t size = 2 * kUInt32Size; |
| 476 | uint32_t initialOffset = this->addDraw(CONCAT, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 477 | addMatrix(matrix); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 478 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 479 | return this->INHERITED::concat(matrix); |
| 480 | } |
| 481 | |
reed@android.com | 6e073b9 | 2009-01-06 15:03:30 +0000 | [diff] [blame] | 482 | void SkPictureRecord::setMatrix(const SkMatrix& matrix) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 483 | validate(fWriter.size(), 0); |
| 484 | // op + matrix index |
| 485 | uint32_t size = 2 * kUInt32Size; |
| 486 | uint32_t initialOffset = this->addDraw(SET_MATRIX, &size); |
reed@android.com | 6e073b9 | 2009-01-06 15:03:30 +0000 | [diff] [blame] | 487 | addMatrix(matrix); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 488 | validate(initialOffset, size); |
reed@android.com | 6e073b9 | 2009-01-06 15:03:30 +0000 | [diff] [blame] | 489 | this->INHERITED::setMatrix(matrix); |
| 490 | } |
| 491 | |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 492 | static bool regionOpExpands(SkRegion::Op op) { |
| 493 | switch (op) { |
| 494 | case SkRegion::kUnion_Op: |
| 495 | case SkRegion::kXOR_Op: |
| 496 | case SkRegion::kReverseDifference_Op: |
| 497 | case SkRegion::kReplace_Op: |
| 498 | return true; |
| 499 | case SkRegion::kIntersect_Op: |
| 500 | case SkRegion::kDifference_Op: |
| 501 | return false; |
| 502 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 503 | SkDEBUGFAIL("unknown region op"); |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 508 | void SkPictureRecord::fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset) { |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 509 | int32_t offset = fRestoreOffsetStack.top(); |
| 510 | while (offset > 0) { |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 511 | uint32_t* peek = fWriter.peek32(offset); |
| 512 | offset = *peek; |
| 513 | *peek = restoreOffset; |
| 514 | } |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 515 | |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 516 | #ifdef SK_DEBUG |
| 517 | // assert that the final offset value points to a save verb |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 518 | uint32_t opSize; |
| 519 | DrawType drawOp = peek_op_and_size(&fWriter, -offset, &opSize); |
reed@google.com | ffacd3c | 2012-08-30 15:31:23 +0000 | [diff] [blame] | 520 | SkASSERT(SAVE == drawOp || SAVE_LAYER == drawOp); |
| 521 | #endif |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 522 | } |
| 523 | |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 524 | void SkPictureRecord::beginRecording() { |
| 525 | // we have to call this *after* our constructor, to ensure that it gets |
| 526 | // recorded. This is balanced by restoreToCount() call from endRecording, |
| 527 | // which in-turn calls our overridden restore(), so those get recorded too. |
| 528 | fInitialSaveCount = this->save(kMatrixClip_SaveFlag); |
| 529 | } |
| 530 | |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 531 | void SkPictureRecord::endRecording() { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 532 | SkASSERT(kNoInitialSave != fInitialSaveCount); |
| 533 | this->restoreToCount(fInitialSaveCount); |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 534 | } |
| 535 | |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 536 | void SkPictureRecord::recordRestoreOffsetPlaceholder(SkRegion::Op op) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 537 | if (fRestoreOffsetStack.isEmpty()) { |
| 538 | return; |
| 539 | } |
| 540 | |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 541 | if (regionOpExpands(op)) { |
| 542 | // Run back through any previous clip ops, and mark their offset to |
| 543 | // be 0, disabling their ability to trigger a jump-to-restore, otherwise |
| 544 | // they could hide this clips ability to expand the clip (i.e. go from |
| 545 | // empty to non-empty). |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 546 | fillRestoreOffsetPlaceholdersForCurrentStackLevel(0); |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 547 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 548 | |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 549 | size_t offset = fWriter.size(); |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 550 | // The RestoreOffset field is initially filled with a placeholder |
| 551 | // value that points to the offset of the previous RestoreOffset |
| 552 | // 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] | 553 | // the restore offsets can be filled in when the corresponding |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 554 | // restore command is recorded. |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 555 | addInt(fRestoreOffsetStack.top()); |
| 556 | fRestoreOffsetStack.top() = offset; |
| 557 | } |
| 558 | |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 559 | bool SkPictureRecord::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | f929150 | 2013-02-15 15:13:27 +0000 | [diff] [blame] | 560 | // id + rect + clip params |
| 561 | uint32_t size = 1 * kUInt32Size + sizeof(rect) + 1 * kUInt32Size; |
robertphillips@google.com | 4310c66 | 2013-03-01 14:17:58 +0000 | [diff] [blame] | 562 | // recordRestoreOffsetPlaceholder doesn't always write an offset |
robertphillips@google.com | f929150 | 2013-02-15 15:13:27 +0000 | [diff] [blame] | 563 | if (!fRestoreOffsetStack.isEmpty()) { |
| 564 | // + restore offset |
| 565 | size += kUInt32Size; |
| 566 | } |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 567 | uint32_t initialOffset = this->addDraw(CLIP_RECT, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 568 | addRect(rect); |
reed@google.com | 83ab495 | 2011-11-11 21:34:54 +0000 | [diff] [blame] | 569 | addInt(ClipParams_pack(op, doAA)); |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 570 | recordRestoreOffsetPlaceholder(op); |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 571 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 572 | validate(initialOffset, size); |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 573 | return this->INHERITED::clipRect(rect, op, doAA); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 574 | } |
| 575 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 576 | bool SkPictureRecord::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
| 577 | if (rrect.isRect()) { |
| 578 | return this->SkPictureRecord::clipRect(rrect.getBounds(), op, doAA); |
| 579 | } |
| 580 | |
robertphillips@google.com | f929150 | 2013-02-15 15:13:27 +0000 | [diff] [blame] | 581 | // op + rrect + clip params |
| 582 | uint32_t size = 1 * kUInt32Size + SkRRect::kSizeInMemory + 1 * kUInt32Size; |
robertphillips@google.com | 4310c66 | 2013-03-01 14:17:58 +0000 | [diff] [blame] | 583 | // recordRestoreOffsetPlaceholder doesn't always write an offset |
robertphillips@google.com | f929150 | 2013-02-15 15:13:27 +0000 | [diff] [blame] | 584 | if (!fRestoreOffsetStack.isEmpty()) { |
| 585 | // + restore offset |
| 586 | size += kUInt32Size; |
| 587 | } |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 588 | uint32_t initialOffset = this->addDraw(CLIP_RRECT, &size); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 589 | addRRect(rrect); |
| 590 | addInt(ClipParams_pack(op, doAA)); |
| 591 | recordRestoreOffsetPlaceholder(op); |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 592 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 593 | validate(initialOffset, size); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 594 | |
| 595 | if (fRecordFlags & SkPicture::kUsePathBoundsForClip_RecordingFlag) { |
| 596 | return this->INHERITED::clipRect(rrect.getBounds(), op, doAA); |
| 597 | } else { |
| 598 | return this->INHERITED::clipRRect(rrect, op, doAA); |
| 599 | } |
| 600 | } |
| 601 | |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 602 | bool SkPictureRecord::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | 3302783 | 2012-11-07 13:01:25 +0000 | [diff] [blame] | 603 | |
| 604 | SkRect r; |
reed@google.com | 907ef6c | 2012-12-10 15:50:37 +0000 | [diff] [blame] | 605 | if (!path.isInverseFillType() && path.isRect(&r)) { |
robertphillips@google.com | 3302783 | 2012-11-07 13:01:25 +0000 | [diff] [blame] | 606 | return this->clipRect(r, op, doAA); |
| 607 | } |
| 608 | |
robertphillips@google.com | f929150 | 2013-02-15 15:13:27 +0000 | [diff] [blame] | 609 | // op + path index + clip params |
| 610 | uint32_t size = 3 * kUInt32Size; |
robertphillips@google.com | 4310c66 | 2013-03-01 14:17:58 +0000 | [diff] [blame] | 611 | // recordRestoreOffsetPlaceholder doesn't always write an offset |
robertphillips@google.com | f929150 | 2013-02-15 15:13:27 +0000 | [diff] [blame] | 612 | if (!fRestoreOffsetStack.isEmpty()) { |
| 613 | // + restore offset |
| 614 | size += kUInt32Size; |
| 615 | } |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 616 | uint32_t initialOffset = this->addDraw(CLIP_PATH, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 617 | addPath(path); |
reed@google.com | 83ab495 | 2011-11-11 21:34:54 +0000 | [diff] [blame] | 618 | addInt(ClipParams_pack(op, doAA)); |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 619 | recordRestoreOffsetPlaceholder(op); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 620 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 621 | validate(initialOffset, size); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 622 | |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 623 | if (fRecordFlags & SkPicture::kUsePathBoundsForClip_RecordingFlag) { |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 624 | return this->INHERITED::clipRect(path.getBounds(), op, doAA); |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 625 | } else { |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 626 | return this->INHERITED::clipPath(path, op, doAA); |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 627 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | bool SkPictureRecord::clipRegion(const SkRegion& region, SkRegion::Op op) { |
robertphillips@google.com | f929150 | 2013-02-15 15:13:27 +0000 | [diff] [blame] | 631 | // op + region index + clip params |
| 632 | uint32_t size = 3 * kUInt32Size; |
robertphillips@google.com | 4310c66 | 2013-03-01 14:17:58 +0000 | [diff] [blame] | 633 | // recordRestoreOffsetPlaceholder doesn't always write an offset |
robertphillips@google.com | f929150 | 2013-02-15 15:13:27 +0000 | [diff] [blame] | 634 | if (!fRestoreOffsetStack.isEmpty()) { |
| 635 | // + restore offset |
| 636 | size += kUInt32Size; |
| 637 | } |
skia.committer@gmail.com | ce8343d | 2013-02-16 07:01:31 +0000 | [diff] [blame] | 638 | uint32_t initialOffset = this->addDraw(CLIP_REGION, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 639 | addRegion(region); |
reed@google.com | 83ab495 | 2011-11-11 21:34:54 +0000 | [diff] [blame] | 640 | addInt(ClipParams_pack(op, false)); |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame] | 641 | recordRestoreOffsetPlaceholder(op); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 642 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 643 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 644 | return this->INHERITED::clipRegion(region, op); |
| 645 | } |
| 646 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 647 | void SkPictureRecord::clear(SkColor color) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 648 | // op + color |
| 649 | uint32_t size = 2 * kUInt32Size; |
| 650 | uint32_t initialOffset = this->addDraw(DRAW_CLEAR, &size); |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 651 | addInt(color); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 652 | validate(initialOffset, size); |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 653 | } |
| 654 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 655 | void SkPictureRecord::drawPaint(const SkPaint& paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 656 | // op + paint index |
| 657 | uint32_t size = 2 * kUInt32Size; |
| 658 | uint32_t initialOffset = this->addDraw(DRAW_PAINT, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 659 | SkASSERT(initialOffset+getPaintOffset(DRAW_PAINT, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 660 | addPaint(paint); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 661 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | void SkPictureRecord::drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 665 | const SkPaint& paint) { |
| 666 | // op + paint index + mode + count + point data |
| 667 | uint32_t size = 4 * kUInt32Size + count * sizeof(SkPoint); |
| 668 | uint32_t initialOffset = this->addDraw(DRAW_POINTS, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 669 | SkASSERT(initialOffset+getPaintOffset(DRAW_POINTS, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 670 | addPaint(paint); |
| 671 | addInt(mode); |
| 672 | addInt(count); |
| 673 | fWriter.writeMul4(pts, count * sizeof(SkPoint)); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 674 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 675 | } |
| 676 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 677 | void SkPictureRecord::drawOval(const SkRect& oval, const SkPaint& paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 678 | // op + paint index + rect |
| 679 | uint32_t size = 2 * kUInt32Size + sizeof(oval); |
| 680 | uint32_t initialOffset = this->addDraw(DRAW_OVAL, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 681 | SkASSERT(initialOffset+getPaintOffset(DRAW_OVAL, size) == fWriter.size()); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 682 | addPaint(paint); |
| 683 | addRect(oval); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 684 | validate(initialOffset, size); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 685 | } |
| 686 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 687 | void SkPictureRecord::drawRect(const SkRect& rect, const SkPaint& paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 688 | // op + paint index + rect |
| 689 | uint32_t size = 2 * kUInt32Size + sizeof(rect); |
| 690 | uint32_t initialOffset = this->addDraw(DRAW_RECT, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 691 | SkASSERT(initialOffset+getPaintOffset(DRAW_RECT, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 692 | addPaint(paint); |
| 693 | addRect(rect); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 694 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 695 | } |
| 696 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 697 | void SkPictureRecord::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 698 | uint32_t initialOffset, size; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 699 | if (rrect.isRect()) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 700 | // op + paint index + rect |
| 701 | size = 2 * kUInt32Size + sizeof(SkRect); |
| 702 | initialOffset = this->addDraw(DRAW_RECT, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 703 | SkASSERT(initialOffset+getPaintOffset(DRAW_RECT, size) == fWriter.size()); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 704 | addPaint(paint); |
| 705 | addRect(rrect.getBounds()); |
| 706 | } else if (rrect.isOval()) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 707 | // op + paint index + rect |
| 708 | size = 2 * kUInt32Size + sizeof(SkRect); |
| 709 | initialOffset = this->addDraw(DRAW_OVAL, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 710 | SkASSERT(initialOffset+getPaintOffset(DRAW_OVAL, size) == fWriter.size()); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 711 | addPaint(paint); |
| 712 | addRect(rrect.getBounds()); |
| 713 | } else { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 714 | // op + paint index + rrect |
| 715 | size = 2 * kUInt32Size + SkRRect::kSizeInMemory; |
| 716 | initialOffset = this->addDraw(DRAW_RRECT, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 717 | SkASSERT(initialOffset+getPaintOffset(DRAW_RRECT, size) == fWriter.size()); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 718 | addPaint(paint); |
| 719 | addRRect(rrect); |
| 720 | } |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 721 | validate(initialOffset, size); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 722 | } |
| 723 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 724 | void SkPictureRecord::drawPath(const SkPath& path, const SkPaint& paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 725 | // op + paint index + path index |
| 726 | uint32_t size = 3 * kUInt32Size; |
| 727 | uint32_t initialOffset = this->addDraw(DRAW_PATH, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 728 | SkASSERT(initialOffset+getPaintOffset(DRAW_PATH, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 729 | addPaint(paint); |
| 730 | addPath(path); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 731 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | void SkPictureRecord::drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
| 735 | const SkPaint* paint = NULL) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 736 | // op + paint index + bitmap index + left + top |
| 737 | uint32_t size = 3 * kUInt32Size + 2 * sizeof(SkScalar); |
| 738 | uint32_t initialOffset = this->addDraw(DRAW_BITMAP, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 739 | SkASSERT(initialOffset+getPaintOffset(DRAW_BITMAP, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 740 | addPaintPtr(paint); |
| 741 | addBitmap(bitmap); |
| 742 | addScalar(left); |
| 743 | addScalar(top); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 744 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 745 | } |
| 746 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 747 | void SkPictureRecord::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 748 | const SkRect& dst, const SkPaint* paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 749 | // id + paint index + bitmap index + bool for 'src' |
skia.committer@gmail.com | ce8343d | 2013-02-16 07:01:31 +0000 | [diff] [blame] | 750 | uint32_t size = 4 * kUInt32Size; |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 751 | if (NULL != src) { |
| 752 | size += sizeof(*src); // + rect |
| 753 | } |
| 754 | size += sizeof(dst); // + rect |
| 755 | |
| 756 | uint32_t initialOffset = this->addDraw(DRAW_BITMAP_RECT_TO_RECT, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 757 | SkASSERT(initialOffset+getPaintOffset(DRAW_BITMAP_RECT_TO_RECT, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 758 | addPaintPtr(paint); |
| 759 | addBitmap(bitmap); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 760 | addRectPtr(src); // may be null |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 761 | addRect(dst); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 762 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void SkPictureRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix, |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 766 | const SkPaint* paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 767 | // id + paint index + bitmap index + matrix index |
| 768 | uint32_t size = 4 * kUInt32Size; |
| 769 | uint32_t initialOffset = this->addDraw(DRAW_BITMAP_MATRIX, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 770 | SkASSERT(initialOffset+getPaintOffset(DRAW_BITMAP_MATRIX, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 771 | addPaintPtr(paint); |
| 772 | addBitmap(bitmap); |
| 773 | addMatrix(matrix); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 774 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 775 | } |
| 776 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 777 | void SkPictureRecord::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 778 | const SkRect& dst, const SkPaint* paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 779 | // op + paint index + bitmap id + center + dst rect |
skia.committer@gmail.com | ce8343d | 2013-02-16 07:01:31 +0000 | [diff] [blame] | 780 | uint32_t size = 3 * kUInt32Size + sizeof(center) + sizeof(dst); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 781 | uint32_t initialOffset = this->addDraw(DRAW_BITMAP_NINE, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 782 | SkASSERT(initialOffset+getPaintOffset(DRAW_BITMAP_NINE, size) == fWriter.size()); |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 783 | addPaintPtr(paint); |
| 784 | addBitmap(bitmap); |
| 785 | addIRect(center); |
| 786 | addRect(dst); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 787 | validate(initialOffset, size); |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 788 | } |
| 789 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 790 | void SkPictureRecord::drawSprite(const SkBitmap& bitmap, int left, int top, |
| 791 | const SkPaint* paint = NULL) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 792 | // op + paint index + bitmap index + left + top |
| 793 | uint32_t size = 5 * kUInt32Size; |
| 794 | uint32_t initialOffset = this->addDraw(DRAW_SPRITE, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 795 | SkASSERT(initialOffset+getPaintOffset(DRAW_SPRITE, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 796 | addPaintPtr(paint); |
| 797 | addBitmap(bitmap); |
| 798 | addInt(left); |
| 799 | addInt(top); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 800 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | } |
| 802 | |
reed@google.com | 4595426 | 2012-12-07 17:14:40 +0000 | [diff] [blame] | 803 | // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been |
| 804 | // tweaked by paint.computeFastBounds(). |
| 805 | // |
| 806 | static void computeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 807 | SkPaint::FontMetrics metrics; |
| 808 | paint.getFontMetrics(&metrics); |
| 809 | SkRect bounds; |
| 810 | // construct a rect so we can see any adjustments from the paint. |
| 811 | // we use 0,1 for left,right, just so the rect isn't empty |
reed@google.com | 4595426 | 2012-12-07 17:14:40 +0000 | [diff] [blame] | 812 | bounds.set(0, metrics.fTop, SK_Scalar1, metrics.fBottom); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 813 | (void)paint.computeFastBounds(bounds, &bounds); |
reed@google.com | 4595426 | 2012-12-07 17:14:40 +0000 | [diff] [blame] | 814 | topbot[0] = bounds.fTop; |
| 815 | topbot[1] = bounds.fBottom; |
| 816 | } |
| 817 | |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 818 | void SkPictureRecord::addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData& flat, |
reed@google.com | 4595426 | 2012-12-07 17:14:40 +0000 | [diff] [blame] | 819 | SkScalar minY, SkScalar maxY) { |
junov@chromium.org | 3f5ecd6 | 2013-01-22 18:01:26 +0000 | [diff] [blame] | 820 | if (!flat.isTopBotWritten()) { |
| 821 | computeFontMetricsTopBottom(paint, flat.writableTopBot()); |
| 822 | SkASSERT(flat.isTopBotWritten()); |
reed@google.com | 4595426 | 2012-12-07 17:14:40 +0000 | [diff] [blame] | 823 | } |
junov@chromium.org | 3f5ecd6 | 2013-01-22 18:01:26 +0000 | [diff] [blame] | 824 | addScalar(flat.topBot()[0] + minY); |
| 825 | addScalar(flat.topBot()[1] + maxY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 826 | } |
| 827 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 828 | void SkPictureRecord::drawText(const void* text, size_t byteLength, SkScalar x, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 829 | SkScalar y, const SkPaint& paint) { |
reed@google.com | 2eb5bb1 | 2012-04-12 14:27:42 +0000 | [diff] [blame] | 830 | bool fast = !paint.isVerticalText() && paint.canComputeFastBounds(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 831 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 832 | // op + paint index + length + 'length' worth of chars + x + y |
| 833 | uint32_t size = 3 * kUInt32Size + SkAlign4(byteLength) + 2 * sizeof(SkScalar); |
| 834 | if (fast) { |
| 835 | size += 2 * sizeof(SkScalar); // + top & bottom |
| 836 | } |
| 837 | |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 838 | DrawType op = fast ? DRAW_TEXT_TOP_BOTTOM : DRAW_TEXT; |
| 839 | uint32_t initialOffset = this->addDraw(op, &size); |
| 840 | SkASSERT(initialOffset+getPaintOffset(op, size) == fWriter.size()); |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 841 | const SkFlatData* flatPaintData = addPaint(paint); |
| 842 | SkASSERT(flatPaintData); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 843 | addText(text, byteLength); |
| 844 | addScalar(x); |
| 845 | addScalar(y); |
| 846 | if (fast) { |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 847 | addFontMetricsTopBottom(paint, *flatPaintData, y, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 848 | } |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 849 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 850 | } |
| 851 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 852 | void SkPictureRecord::drawPosText(const void* text, size_t byteLength, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 853 | const SkPoint pos[], const SkPaint& paint) { |
| 854 | size_t points = paint.countText(text, byteLength); |
| 855 | if (0 == points) |
| 856 | return; |
| 857 | |
| 858 | bool canUseDrawH = true; |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 859 | SkScalar minY = pos[0].fY; |
| 860 | SkScalar maxY = pos[0].fY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 861 | // check if the caller really should have used drawPosTextH() |
| 862 | { |
| 863 | const SkScalar firstY = pos[0].fY; |
| 864 | for (size_t index = 1; index < points; index++) { |
| 865 | if (pos[index].fY != firstY) { |
| 866 | canUseDrawH = false; |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 867 | if (pos[index].fY < minY) { |
| 868 | minY = pos[index].fY; |
| 869 | } else if (pos[index].fY > maxY) { |
| 870 | maxY = pos[index].fY; |
| 871 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 875 | |
reed@google.com | 2eb5bb1 | 2012-04-12 14:27:42 +0000 | [diff] [blame] | 876 | bool fastBounds = !paint.isVerticalText() && paint.canComputeFastBounds(); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 877 | bool fast = canUseDrawH && fastBounds; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 878 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 879 | // op + paint index + length + 'length' worth of data + num points |
| 880 | uint32_t size = 3 * kUInt32Size + SkAlign4(byteLength) + 1 * kUInt32Size; |
| 881 | if (canUseDrawH) { |
| 882 | if (fast) { |
| 883 | size += 2 * sizeof(SkScalar); // + top & bottom |
| 884 | } |
| 885 | // + y-pos + actual x-point data |
| 886 | size += sizeof(SkScalar) + points * sizeof(SkScalar); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 887 | } else { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 888 | // + x&y point data |
| 889 | size += points * sizeof(SkPoint); |
| 890 | if (fastBounds) { |
| 891 | size += 2 * sizeof(SkScalar); // + top & bottom |
| 892 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 893 | } |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 894 | |
| 895 | DrawType op; |
| 896 | if (fast) { |
| 897 | op = DRAW_POS_TEXT_H_TOP_BOTTOM; |
| 898 | } else if (canUseDrawH) { |
| 899 | op = DRAW_POS_TEXT_H; |
| 900 | } else if (fastBounds) { |
| 901 | op = DRAW_POS_TEXT_TOP_BOTTOM; |
| 902 | } else { |
| 903 | op = DRAW_POS_TEXT; |
| 904 | } |
| 905 | uint32_t initialOffset = this->addDraw(op, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 906 | SkASSERT(initialOffset+getPaintOffset(op, size) == fWriter.size()); |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 907 | const SkFlatData* flatPaintData = addPaint(paint); |
| 908 | SkASSERT(flatPaintData); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 909 | addText(text, byteLength); |
| 910 | addInt(points); |
| 911 | |
| 912 | #ifdef SK_DEBUG_SIZE |
| 913 | size_t start = fWriter.size(); |
| 914 | #endif |
| 915 | if (canUseDrawH) { |
| 916 | if (fast) { |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 917 | addFontMetricsTopBottom(paint, *flatPaintData, pos[0].fY, pos[0].fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 918 | } |
| 919 | addScalar(pos[0].fY); |
| 920 | SkScalar* xptr = (SkScalar*)fWriter.reserve(points * sizeof(SkScalar)); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 921 | for (size_t index = 0; index < points; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 922 | *xptr++ = pos[index].fX; |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 923 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 924 | fWriter.writeMul4(pos, points * sizeof(SkPoint)); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 925 | if (fastBounds) { |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 926 | addFontMetricsTopBottom(paint, *flatPaintData, minY, maxY); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 927 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 928 | } |
| 929 | #ifdef SK_DEBUG_SIZE |
| 930 | fPointBytes += fWriter.size() - start; |
| 931 | fPointWrites += points; |
| 932 | #endif |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 933 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | void SkPictureRecord::drawPosTextH(const void* text, size_t byteLength, |
| 937 | const SkScalar xpos[], SkScalar constY, |
| 938 | const SkPaint& paint) { |
| 939 | size_t points = paint.countText(text, byteLength); |
| 940 | if (0 == points) |
| 941 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 942 | |
reed@google.com | 2eb5bb1 | 2012-04-12 14:27:42 +0000 | [diff] [blame] | 943 | bool fast = !paint.isVerticalText() && paint.canComputeFastBounds(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 944 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 945 | // op + paint index + length + 'length' worth of data + num points |
| 946 | uint32_t size = 3 * kUInt32Size + SkAlign4(byteLength) + 1 * kUInt32Size; |
| 947 | if (fast) { |
| 948 | size += 2 * sizeof(SkScalar); // + top & bottom |
| 949 | } |
| 950 | // + y + the actual points |
| 951 | size += 1 * kUInt32Size + points * sizeof(SkScalar); |
| 952 | |
skia.committer@gmail.com | ce8343d | 2013-02-16 07:01:31 +0000 | [diff] [blame] | 953 | uint32_t initialOffset = this->addDraw(fast ? DRAW_POS_TEXT_H_TOP_BOTTOM : DRAW_POS_TEXT_H, |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 954 | &size); |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 955 | const SkFlatData* flatPaintData = addPaint(paint); |
| 956 | SkASSERT(flatPaintData); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 957 | addText(text, byteLength); |
| 958 | addInt(points); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 959 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 960 | #ifdef SK_DEBUG_SIZE |
| 961 | size_t start = fWriter.size(); |
| 962 | #endif |
| 963 | if (fast) { |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 964 | addFontMetricsTopBottom(paint, *flatPaintData, constY, constY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 965 | } |
| 966 | addScalar(constY); |
| 967 | fWriter.writeMul4(xpos, points * sizeof(SkScalar)); |
| 968 | #ifdef SK_DEBUG_SIZE |
| 969 | fPointBytes += fWriter.size() - start; |
| 970 | fPointWrites += points; |
| 971 | #endif |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 972 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 973 | } |
| 974 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 975 | void SkPictureRecord::drawTextOnPath(const void* text, size_t byteLength, |
| 976 | const SkPath& path, const SkMatrix* matrix, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 977 | const SkPaint& paint) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 978 | // op + paint index + length + 'length' worth of data + path index + matrix index |
| 979 | uint32_t size = 3 * kUInt32Size + SkAlign4(byteLength) + 2 * kUInt32Size; |
| 980 | uint32_t initialOffset = this->addDraw(DRAW_TEXT_ON_PATH, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 981 | SkASSERT(initialOffset+getPaintOffset(DRAW_TEXT_ON_PATH, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 982 | addPaint(paint); |
| 983 | addText(text, byteLength); |
| 984 | addPath(path); |
| 985 | addMatrixPtr(matrix); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 986 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | void SkPictureRecord::drawPicture(SkPicture& picture) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 990 | // op + picture index |
| 991 | uint32_t size = 2 * kUInt32Size; |
| 992 | uint32_t initialOffset = this->addDraw(DRAW_PICTURE, &size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 993 | addPicture(picture); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 994 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | void SkPictureRecord::drawVertices(VertexMode vmode, int vertexCount, |
| 998 | const SkPoint vertices[], const SkPoint texs[], |
| 999 | const SkColor colors[], SkXfermode*, |
| 1000 | const uint16_t indices[], int indexCount, |
| 1001 | const SkPaint& paint) { |
| 1002 | uint32_t flags = 0; |
| 1003 | if (texs) { |
| 1004 | flags |= DRAW_VERTICES_HAS_TEXS; |
| 1005 | } |
| 1006 | if (colors) { |
| 1007 | flags |= DRAW_VERTICES_HAS_COLORS; |
| 1008 | } |
| 1009 | if (indexCount > 0) { |
| 1010 | flags |= DRAW_VERTICES_HAS_INDICES; |
| 1011 | } |
| 1012 | |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1013 | // op + paint index + flags + vmode + vCount + vertices |
| 1014 | uint32_t size = 5 * kUInt32Size + vertexCount * sizeof(SkPoint); |
| 1015 | if (flags & DRAW_VERTICES_HAS_TEXS) { |
| 1016 | size += vertexCount * sizeof(SkPoint); // + uvs |
| 1017 | } |
| 1018 | if (flags & DRAW_VERTICES_HAS_COLORS) { |
| 1019 | size += vertexCount * sizeof(SkColor); // + vert colors |
| 1020 | } |
| 1021 | if (flags & DRAW_VERTICES_HAS_INDICES) { |
| 1022 | // + num indices + indices |
| 1023 | size += 1 * kUInt32Size + SkAlign4(indexCount * sizeof(uint16_t)); |
| 1024 | } |
| 1025 | |
| 1026 | uint32_t initialOffset = this->addDraw(DRAW_VERTICES, &size); |
robertphillips@google.com | e37ad35 | 2013-03-01 19:44:30 +0000 | [diff] [blame^] | 1027 | SkASSERT(initialOffset+getPaintOffset(DRAW_VERTICES, size) == fWriter.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1028 | addPaint(paint); |
| 1029 | addInt(flags); |
| 1030 | addInt(vmode); |
| 1031 | addInt(vertexCount); |
| 1032 | addPoints(vertices, vertexCount); |
| 1033 | if (flags & DRAW_VERTICES_HAS_TEXS) { |
| 1034 | addPoints(texs, vertexCount); |
| 1035 | } |
| 1036 | if (flags & DRAW_VERTICES_HAS_COLORS) { |
| 1037 | fWriter.writeMul4(colors, vertexCount * sizeof(SkColor)); |
| 1038 | } |
| 1039 | if (flags & DRAW_VERTICES_HAS_INDICES) { |
| 1040 | addInt(indexCount); |
| 1041 | fWriter.writePad(indices, indexCount * sizeof(uint16_t)); |
| 1042 | } |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1043 | validate(initialOffset, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 1046 | void SkPictureRecord::drawData(const void* data, size_t length) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1047 | // op + length + 'length' worth of data |
| 1048 | uint32_t size = 2 * kUInt32Size + SkAlign4(length); |
skia.committer@gmail.com | ce8343d | 2013-02-16 07:01:31 +0000 | [diff] [blame] | 1049 | uint32_t initialOffset = this->addDraw(DRAW_DATA, &size); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 1050 | addInt(length); |
| 1051 | fWriter.writePad(data, length); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1052 | validate(initialOffset, size); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1055 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1056 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1057 | void SkPictureRecord::addBitmap(const SkBitmap& bitmap) { |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1058 | const int index = fBitmapHeap->insert(bitmap); |
| 1059 | // In debug builds, a bad return value from insert() will crash, allowing for debugging. In |
| 1060 | // release builds, the invalid value will be recorded so that the reader will know that there |
| 1061 | // was a problem. |
| 1062 | SkASSERT(index != SkBitmapHeap::INVALID_SLOT); |
| 1063 | addInt(index); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | void SkPictureRecord::addMatrix(const SkMatrix& matrix) { |
| 1067 | addMatrixPtr(&matrix); |
| 1068 | } |
| 1069 | |
| 1070 | void SkPictureRecord::addMatrixPtr(const SkMatrix* matrix) { |
reed@google.com | 83ca337 | 2012-07-12 15:27:54 +0000 | [diff] [blame] | 1071 | this->addInt(matrix ? fMatrices.find(*matrix) : 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 1074 | const SkFlatData* SkPictureRecord::addPaintPtr(const SkPaint* paint) { |
| 1075 | const SkFlatData* data = paint ? fPaints.findAndReturnFlat(*paint) : NULL; |
| 1076 | int index = data ? data->index() : 0; |
reed@google.com | 4595426 | 2012-12-07 17:14:40 +0000 | [diff] [blame] | 1077 | this->addInt(index); |
junov@chromium.org | f3b1223 | 2013-01-22 17:50:47 +0000 | [diff] [blame] | 1078 | return data; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | void SkPictureRecord::addPath(const SkPath& path) { |
| 1082 | if (NULL == fPathHeap) { |
| 1083 | fPathHeap = SkNEW(SkPathHeap); |
| 1084 | } |
| 1085 | addInt(fPathHeap->append(path)); |
| 1086 | } |
| 1087 | |
| 1088 | void SkPictureRecord::addPicture(SkPicture& picture) { |
| 1089 | int index = fPictureRefs.find(&picture); |
| 1090 | if (index < 0) { // not found |
| 1091 | index = fPictureRefs.count(); |
| 1092 | *fPictureRefs.append() = &picture; |
| 1093 | picture.ref(); |
| 1094 | } |
| 1095 | // follow the convention of recording a 1-based index |
| 1096 | addInt(index + 1); |
| 1097 | } |
| 1098 | |
| 1099 | void SkPictureRecord::addPoint(const SkPoint& point) { |
| 1100 | #ifdef SK_DEBUG_SIZE |
| 1101 | size_t start = fWriter.size(); |
| 1102 | #endif |
| 1103 | fWriter.writePoint(point); |
| 1104 | #ifdef SK_DEBUG_SIZE |
| 1105 | fPointBytes += fWriter.size() - start; |
| 1106 | fPointWrites++; |
| 1107 | #endif |
| 1108 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1109 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1110 | void SkPictureRecord::addPoints(const SkPoint pts[], int count) { |
| 1111 | fWriter.writeMul4(pts, count * sizeof(SkPoint)); |
| 1112 | #ifdef SK_DEBUG_SIZE |
| 1113 | fPointBytes += count * sizeof(SkPoint); |
| 1114 | fPointWrites++; |
| 1115 | #endif |
| 1116 | } |
| 1117 | |
| 1118 | void SkPictureRecord::addRect(const SkRect& rect) { |
| 1119 | #ifdef SK_DEBUG_SIZE |
| 1120 | size_t start = fWriter.size(); |
| 1121 | #endif |
| 1122 | fWriter.writeRect(rect); |
| 1123 | #ifdef SK_DEBUG_SIZE |
| 1124 | fRectBytes += fWriter.size() - start; |
| 1125 | fRectWrites++; |
| 1126 | #endif |
| 1127 | } |
| 1128 | |
| 1129 | void SkPictureRecord::addRectPtr(const SkRect* rect) { |
| 1130 | if (fWriter.writeBool(rect != NULL)) { |
| 1131 | fWriter.writeRect(*rect); |
| 1132 | } |
| 1133 | } |
| 1134 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 1135 | void SkPictureRecord::addIRect(const SkIRect& rect) { |
| 1136 | fWriter.write(&rect, sizeof(rect)); |
| 1137 | } |
| 1138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1139 | void SkPictureRecord::addIRectPtr(const SkIRect* rect) { |
| 1140 | if (fWriter.writeBool(rect != NULL)) { |
| 1141 | *(SkIRect*)fWriter.reserve(sizeof(SkIRect)) = *rect; |
| 1142 | } |
| 1143 | } |
| 1144 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1145 | void SkPictureRecord::addRRect(const SkRRect& rrect) { |
| 1146 | fWriter.writeRRect(rrect); |
| 1147 | } |
| 1148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1149 | void SkPictureRecord::addRegion(const SkRegion& region) { |
reed@google.com | 83ca337 | 2012-07-12 15:27:54 +0000 | [diff] [blame] | 1150 | addInt(fRegions.find(region)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | void SkPictureRecord::addText(const void* text, size_t byteLength) { |
| 1154 | #ifdef SK_DEBUG_SIZE |
| 1155 | size_t start = fWriter.size(); |
| 1156 | #endif |
| 1157 | addInt(byteLength); |
| 1158 | fWriter.writePad(text, byteLength); |
| 1159 | #ifdef SK_DEBUG_SIZE |
| 1160 | fTextBytes += fWriter.size() - start; |
| 1161 | fTextWrites++; |
| 1162 | #endif |
| 1163 | } |
| 1164 | |
| 1165 | /////////////////////////////////////////////////////////////////////////////// |
| 1166 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1167 | #ifdef SK_DEBUG_SIZE |
| 1168 | size_t SkPictureRecord::size() const { |
| 1169 | size_t result = 0; |
| 1170 | size_t sizeData; |
| 1171 | bitmaps(&sizeData); |
| 1172 | result += sizeData; |
| 1173 | matrices(&sizeData); |
| 1174 | result += sizeData; |
| 1175 | paints(&sizeData); |
| 1176 | result += sizeData; |
| 1177 | paths(&sizeData); |
| 1178 | result += sizeData; |
| 1179 | pictures(&sizeData); |
| 1180 | result += sizeData; |
| 1181 | regions(&sizeData); |
| 1182 | result += sizeData; |
| 1183 | result += streamlen(); |
| 1184 | return result; |
| 1185 | } |
| 1186 | |
| 1187 | int SkPictureRecord::bitmaps(size_t* size) const { |
| 1188 | size_t result = 0; |
| 1189 | int count = fBitmaps.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1190 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1191 | result += sizeof(fBitmaps[index]) + fBitmaps[index]->size(); |
| 1192 | *size = result; |
| 1193 | return count; |
| 1194 | } |
| 1195 | |
| 1196 | int SkPictureRecord::matrices(size_t* size) const { |
| 1197 | int count = fMatrices.count(); |
| 1198 | *size = sizeof(fMatrices[0]) * count; |
| 1199 | return count; |
| 1200 | } |
| 1201 | |
| 1202 | int SkPictureRecord::paints(size_t* size) const { |
| 1203 | size_t result = 0; |
| 1204 | int count = fPaints.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1205 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1206 | result += sizeof(fPaints[index]) + fPaints[index]->size(); |
| 1207 | *size = result; |
| 1208 | return count; |
| 1209 | } |
| 1210 | |
| 1211 | int SkPictureRecord::paths(size_t* size) const { |
| 1212 | size_t result = 0; |
| 1213 | int count = fPaths.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1214 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1215 | result += sizeof(fPaths[index]) + fPaths[index]->size(); |
| 1216 | *size = result; |
| 1217 | return count; |
| 1218 | } |
| 1219 | |
| 1220 | int SkPictureRecord::regions(size_t* size) const { |
| 1221 | size_t result = 0; |
| 1222 | int count = fRegions.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1223 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1224 | result += sizeof(fRegions[index]) + fRegions[index]->size(); |
| 1225 | *size = result; |
| 1226 | return count; |
| 1227 | } |
| 1228 | |
| 1229 | size_t SkPictureRecord::streamlen() const { |
| 1230 | return fWriter.size(); |
| 1231 | } |
| 1232 | #endif |
| 1233 | |
| 1234 | #ifdef SK_DEBUG_VALIDATE |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1235 | void SkPictureRecord::validate(uint32_t initialOffset, uint32_t size) const { |
| 1236 | SkASSERT(fWriter.size() == initialOffset + size); |
| 1237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1238 | validateBitmaps(); |
| 1239 | validateMatrices(); |
| 1240 | validatePaints(); |
| 1241 | validatePaths(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1242 | validateRegions(); |
| 1243 | } |
| 1244 | |
| 1245 | void SkPictureRecord::validateBitmaps() const { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1246 | int count = fBitmapHeap->count(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1247 | SkASSERT((unsigned) count < 0x1000); |
| 1248 | for (int index = 0; index < count; index++) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1249 | const SkBitmap* bitPtr = fBitmapHeap->getBitmap(index); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1250 | SkASSERT(bitPtr); |
| 1251 | bitPtr->validate(); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | void SkPictureRecord::validateMatrices() const { |
| 1256 | int count = fMatrices.count(); |
| 1257 | SkASSERT((unsigned) count < 0x1000); |
| 1258 | for (int index = 0; index < count; index++) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1259 | const SkFlatData* matrix = fMatrices[index]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1260 | SkASSERT(matrix); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1261 | // matrix->validate(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | void SkPictureRecord::validatePaints() const { |
| 1266 | int count = fPaints.count(); |
| 1267 | SkASSERT((unsigned) count < 0x1000); |
| 1268 | for (int index = 0; index < count; index++) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1269 | const SkFlatData* paint = fPaints[index]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1270 | SkASSERT(paint); |
| 1271 | // paint->validate(); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | void SkPictureRecord::validatePaths() const { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1276 | if (NULL == fPathHeap) { |
| 1277 | return; |
| 1278 | } |
| 1279 | |
| 1280 | int count = fPathHeap->count(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1281 | SkASSERT((unsigned) count < 0x1000); |
| 1282 | for (int index = 0; index < count; index++) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1283 | const SkPath& path = (*fPathHeap)[index]; |
| 1284 | path.validate(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | void SkPictureRecord::validateRegions() const { |
| 1289 | int count = fRegions.count(); |
| 1290 | SkASSERT((unsigned) count < 0x1000); |
| 1291 | for (int index = 0; index < count; index++) { |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1292 | const SkFlatData* region = fRegions[index]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1293 | SkASSERT(region); |
robertphillips@google.com | 2ca1aaa | 2013-02-15 13:47:37 +0000 | [diff] [blame] | 1294 | // region->validate(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | #endif |