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