[go: nahoru, domu]

blob: 65f723e53b210bf8131e807d1debfff9fab36897 [file] [log] [blame]
epoger@google.com685cfc02011-07-28 14:26:00 +00001
reed@android.combcd4d5a2008-12-17 15:59:43 +00002/*
epoger@google.com685cfc02011-07-28 14:26:00 +00003 * Copyright 2007 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
scroggo@google.com8b854502013-05-07 14:45:40 +00009
reed@android.combcd4d5a2008-12-17 15:59:43 +000010#include "SkPictureFlat.h"
11#include "SkPicturePlayback.h"
12#include "SkPictureRecord.h"
13
robertphillips@google.com7aefc822014-04-18 18:04:41 +000014#include "SkBBHFactory.h"
robertphillips@google.com2fd23902013-08-29 11:54:56 +000015#include "SkBitmapDevice.h"
reed@android.combcd4d5a2008-12-17 15:59:43 +000016#include "SkCanvas.h"
17#include "SkChunkAlloc.h"
18#include "SkPicture.h"
19#include "SkRegion.h"
20#include "SkStream.h"
21#include "SkTDArray.h"
22#include "SkTSearch.h"
23#include "SkTime.h"
24
25#include "SkReader32.h"
26#include "SkWriter32.h"
rileya@google.com7fb3d2f2012-09-13 21:41:51 +000027#include "SkRTree.h"
28#include "SkBBoxHierarchyRecord.h"
reed@android.combcd4d5a2008-12-17 15:59:43 +000029
commit-bot@chromium.org95cdc162014-03-19 21:24:25 +000030#if SK_SUPPORT_GPU
31#include "GrContext.h"
32#endif
33
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +000034template <typename T> int SafeCount(const T* obj) {
35 return obj ? obj->count() : 0;
36}
37
reed@android.combcd4d5a2008-12-17 15:59:43 +000038#define DUMP_BUFFER_SIZE 65536
39
40//#define ENABLE_TIME_DRAW // dumps milliseconds for each draw
41
42
43#ifdef SK_DEBUG
reed@google.com91911482011-02-07 15:30:46 +000044// enable SK_DEBUG_TRACE to trace DrawType elements when
reed@android.combcd4d5a2008-12-17 15:59:43 +000045// recorded and played back
46// #define SK_DEBUG_TRACE
47// enable SK_DEBUG_SIZE to see the size of picture components
48// #define SK_DEBUG_SIZE
49// enable SK_DEBUG_DUMP to see the contents of recorded elements
50// #define SK_DEBUG_DUMP
51// enable SK_DEBUG_VALIDATE to check internal structures for consistency
52// #define SK_DEBUG_VALIDATE
53#endif
54
55#if defined SK_DEBUG_TRACE || defined SK_DEBUG_DUMP
56const char* DrawTypeToString(DrawType drawType) {
57 switch (drawType) {
58 case UNUSED: SkDebugf("DrawType UNUSED\n"); SkASSERT(0); break;
59 case CLIP_PATH: return "CLIP_PATH";
60 case CLIP_REGION: return "CLIP_REGION";
61 case CLIP_RECT: return "CLIP_RECT";
robertphillips@google.come7189be2013-02-15 17:19:15 +000062 case CLIP_RRECT: return "CLIP_RRECT";
reed@android.combcd4d5a2008-12-17 15:59:43 +000063 case CONCAT: return "CONCAT";
64 case DRAW_BITMAP: return "DRAW_BITMAP";
65 case DRAW_BITMAP_MATRIX: return "DRAW_BITMAP_MATRIX";
robertphillips@google.come7189be2013-02-15 17:19:15 +000066 case DRAW_BITMAP_NINE: return "DRAW_BITMAP_NINE";
67 case DRAW_BITMAP_RECT_TO_RECT: return "DRAW_BITMAP_RECT_TO_RECT";
68 case DRAW_CLEAR: return "DRAW_CLEAR";
69 case DRAW_DATA: return "DRAW_DATA";
70 case DRAW_OVAL: return "DRAW_OVAL";
reed@android.combcd4d5a2008-12-17 15:59:43 +000071 case DRAW_PAINT: return "DRAW_PAINT";
72 case DRAW_PATH: return "DRAW_PATH";
73 case DRAW_PICTURE: return "DRAW_PICTURE";
74 case DRAW_POINTS: return "DRAW_POINTS";
75 case DRAW_POS_TEXT: return "DRAW_POS_TEXT";
robertphillips@google.come7189be2013-02-15 17:19:15 +000076 case DRAW_POS_TEXT_TOP_BOTTOM: return "DRAW_POS_TEXT_TOP_BOTTOM";
reed@android.combcd4d5a2008-12-17 15:59:43 +000077 case DRAW_POS_TEXT_H: return "DRAW_POS_TEXT_H";
robertphillips@google.come7189be2013-02-15 17:19:15 +000078 case DRAW_POS_TEXT_H_TOP_BOTTOM: return "DRAW_POS_TEXT_H_TOP_BOTTOM";
79 case DRAW_RECT: return "DRAW_RECT";
80 case DRAW_RRECT: return "DRAW_RRECT";
reed@android.combcd4d5a2008-12-17 15:59:43 +000081 case DRAW_SPRITE: return "DRAW_SPRITE";
82 case DRAW_TEXT: return "DRAW_TEXT";
83 case DRAW_TEXT_ON_PATH: return "DRAW_TEXT_ON_PATH";
robertphillips@google.come7189be2013-02-15 17:19:15 +000084 case DRAW_TEXT_TOP_BOTTOM: return "DRAW_TEXT_TOP_BOTTOM";
85 case DRAW_VERTICES: return "DRAW_VERTICES";
reed@android.combcd4d5a2008-12-17 15:59:43 +000086 case RESTORE: return "RESTORE";
87 case ROTATE: return "ROTATE";
88 case SAVE: return "SAVE";
89 case SAVE_LAYER: return "SAVE_LAYER";
90 case SCALE: return "SCALE";
robertphillips@google.come7189be2013-02-15 17:19:15 +000091 case SET_MATRIX: return "SET_MATRIX";
reed@android.combcd4d5a2008-12-17 15:59:43 +000092 case SKEW: return "SKEW";
93 case TRANSLATE: return "TRANSLATE";
robertphillips@google.come7189be2013-02-15 17:19:15 +000094 case NOOP: return "NOOP";
reed@google.com91911482011-02-07 15:30:46 +000095 default:
96 SkDebugf("DrawType error 0x%08x\n", drawType);
97 SkASSERT(0);
reed@android.combcd4d5a2008-12-17 15:59:43 +000098 break;
99 }
reed@google.com91911482011-02-07 15:30:46 +0000100 SkASSERT(0);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000101 return NULL;
102}
103#endif
104
105#ifdef SK_DEBUG_VALIDATE
106static void validateMatrix(const SkMatrix* matrix) {
107 SkScalar scaleX = matrix->getScaleX();
108 SkScalar scaleY = matrix->getScaleY();
109 SkScalar skewX = matrix->getSkewX();
110 SkScalar skewY = matrix->getSkewY();
111 SkScalar perspX = matrix->getPerspX();
112 SkScalar perspY = matrix->getPerspY();
113 if (scaleX != 0 && skewX != 0)
114 SkDebugf("scaleX != 0 && skewX != 0\n");
115 SkASSERT(scaleX == 0 || skewX == 0);
116 SkASSERT(scaleY == 0 || skewY == 0);
117 SkASSERT(perspX == 0);
118 SkASSERT(perspY == 0);
119}
120#endif
121
122
123///////////////////////////////////////////////////////////////////////////////
124
commit-bot@chromium.org7a1832f2014-04-24 21:53:13 +0000125SkPicture::SkPicture()
126 : fAccelData(NULL) {
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000127 this->needsNewGenID();
reed@android.combcd4d5a2008-12-17 15:59:43 +0000128 fRecord = NULL;
129 fPlayback = NULL;
130 fWidth = fHeight = 0;
131}
132
skia.committer@gmail.com8b5598b2014-03-17 03:02:17 +0000133SkPicture::SkPicture(const SkPicture& src)
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000134 : INHERITED()
commit-bot@chromium.org7a1832f2014-04-24 21:53:13 +0000135 , fAccelData(NULL)
136 , fContentInfo(src.fContentInfo) {
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000137 this->needsNewGenID();
reed@android.combcd4d5a2008-12-17 15:59:43 +0000138 fWidth = src.fWidth;
139 fHeight = src.fHeight;
140 fRecord = NULL;
141
142 /* We want to copy the src's playback. However, if that hasn't been built
143 yet, we need to fake a call to endRecording() without actually calling
144 it (since it is destructive, and we don't want to change src).
145 */
146 if (src.fPlayback) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000147 fPlayback = SkNEW_ARGS(SkPicturePlayback, (this, *src.fPlayback));
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000148 SkASSERT(NULL == src.fRecord);
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000149 fUniqueID = src.uniqueID(); // need to call method to ensure != 0
reed@android.combcd4d5a2008-12-17 15:59:43 +0000150 } else if (src.fRecord) {
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000151 SkPictInfo info;
152 this->createHeader(&info);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000153 // here we do a fake src.endRecording()
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000154 fPlayback = SkNEW_ARGS(SkPicturePlayback, (this, *src.fRecord, info));
reed@android.combcd4d5a2008-12-17 15:59:43 +0000155 } else {
156 fPlayback = NULL;
157 }
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000158
159 fPathHeap.reset(SkSafeRef(src.fPathHeap.get()));
160}
161
162const SkPath& SkPicture::getPath(int index) const {
163 return (*fPathHeap.get())[index];
164}
165
166int SkPicture::addPathToHeap(const SkPath& path) {
167 if (NULL == fPathHeap) {
168 fPathHeap.reset(SkNEW(SkPathHeap));
169 }
170#ifdef SK_DEDUP_PICTURE_PATHS
171 return fPathHeap->insert(path);
172#else
173 return fPathHeap->append(path);
174#endif
175}
176
177void SkPicture::initForPlayback() const {
178 // ensure that the paths bounds are pre-computed
179 if (NULL != fPathHeap.get()) {
180 for (int i = 0; i < fPathHeap->count(); i++) {
181 (*fPathHeap.get())[i].updateBoundsCache();
182 }
183 }
184}
185
186void SkPicture::dumpSize() const {
187 SkDebugf("--- picture size: paths=%d\n",
188 SafeCount(fPathHeap.get()));
reed@android.combcd4d5a2008-12-17 15:59:43 +0000189}
190
191SkPicture::~SkPicture() {
reed@google.com91911482011-02-07 15:30:46 +0000192 SkSafeUnref(fRecord);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000193 SkDELETE(fPlayback);
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000194 SkSafeUnref(fAccelData);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000195}
196
commit-bot@chromium.orge6e785e2014-03-04 19:08:57 +0000197void SkPicture::internalOnly_EnableOpts(bool enableOpts) {
198 if (NULL != fRecord) {
199 fRecord->internalOnly_EnableOpts(enableOpts);
200 }
201}
202
reed@android.combcd4d5a2008-12-17 15:59:43 +0000203void SkPicture::swap(SkPicture& other) {
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000204 SkTSwap(fUniqueID, other.fUniqueID);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000205 SkTSwap(fRecord, other.fRecord);
206 SkTSwap(fPlayback, other.fPlayback);
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000207 SkTSwap(fAccelData, other.fAccelData);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000208 SkTSwap(fWidth, other.fWidth);
209 SkTSwap(fHeight, other.fHeight);
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000210 fPathHeap.swap(&other.fPathHeap);
commit-bot@chromium.org7a1832f2014-04-24 21:53:13 +0000211 fContentInfo.swap(&other.fContentInfo);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000212}
213
djsollen@google.com3855f872012-08-29 18:52:07 +0000214SkPicture* SkPicture::clone() const {
215 SkPicture* clonedPicture = SkNEW(SkPicture);
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000216 this->clone(clonedPicture, 1);
djsollen@google.com3855f872012-08-29 18:52:07 +0000217 return clonedPicture;
218}
219
220void SkPicture::clone(SkPicture* pictures, int count) const {
221 SkPictCopyInfo copyInfo;
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000222 SkPictInfo info;
223 this->createHeader(&info);
djsollen@google.com3855f872012-08-29 18:52:07 +0000224
225 for (int i = 0; i < count; i++) {
226 SkPicture* clone = &pictures[i];
227
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000228 clone->needsNewGenID();
djsollen@google.com3855f872012-08-29 18:52:07 +0000229 clone->fWidth = fWidth;
230 clone->fHeight = fHeight;
commit-bot@chromium.orgefea6992014-02-14 17:27:10 +0000231 SkSafeSetNull(clone->fRecord);
scroggo@google.coma0dd4332012-11-02 20:51:19 +0000232 SkDELETE(clone->fPlayback);
commit-bot@chromium.org7a1832f2014-04-24 21:53:13 +0000233 clone->fContentInfo.set(fContentInfo);
scroggo@google.coma0dd4332012-11-02 20:51:19 +0000234
djsollen@google.com3855f872012-08-29 18:52:07 +0000235 /* We want to copy the src's playback. However, if that hasn't been built
236 yet, we need to fake a call to endRecording() without actually calling
237 it (since it is destructive, and we don't want to change src).
238 */
239 if (fPlayback) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000240 clone->fPlayback = SkNEW_ARGS(SkPicturePlayback, (clone, *fPlayback, &copyInfo));
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000241 SkASSERT(NULL == fRecord);
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000242 clone->fUniqueID = this->uniqueID(); // need to call method to ensure != 0
djsollen@google.com3855f872012-08-29 18:52:07 +0000243 } else if (fRecord) {
244 // here we do a fake src.endRecording()
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000245 clone->fPlayback = SkNEW_ARGS(SkPicturePlayback, (clone, *fRecord, info, true));
djsollen@google.com3855f872012-08-29 18:52:07 +0000246 } else {
247 clone->fPlayback = NULL;
248 }
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000249
250 clone->fPathHeap.reset(SkSafeRef(fPathHeap.get()));
djsollen@google.com3855f872012-08-29 18:52:07 +0000251 }
252}
253
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000254SkPicture::AccelData::Domain SkPicture::AccelData::GenerateDomain() {
255 static int32_t gNextID = 0;
256
257 int32_t id = sk_atomic_inc(&gNextID);
258 if (id >= 1 << (8 * sizeof(Domain))) {
259 SK_CRASH();
260 }
261
262 return static_cast<Domain>(id);
263}
264
reed@android.combcd4d5a2008-12-17 15:59:43 +0000265///////////////////////////////////////////////////////////////////////////////
266
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000267#ifdef SK_SUPPORT_LEGACY_DERIVED_PICTURE_CLASSES
268
reed@android.comc4a55d72009-02-13 14:56:09 +0000269SkCanvas* SkPicture::beginRecording(int width, int height,
270 uint32_t recordingFlags) {
reed@android.combcd4d5a2008-12-17 15:59:43 +0000271 if (fPlayback) {
272 SkDELETE(fPlayback);
273 fPlayback = NULL;
274 }
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000275 SkSafeUnref(fAccelData);
commit-bot@chromium.orgefea6992014-02-14 17:27:10 +0000276 SkSafeSetNull(fRecord);
commit-bot@chromium.org7a1832f2014-04-24 21:53:13 +0000277 fContentInfo.reset();
reed@android.combcd4d5a2008-12-17 15:59:43 +0000278
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000279 this->needsNewGenID();
280
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000281 // Must be set before calling createBBoxHierarchy
282 fWidth = width;
283 fHeight = height;
284
commit-bot@chromium.orgca101d62014-02-17 15:28:00 +0000285 const SkISize size = SkISize::Make(width, height);
286
rileya@google.com7fb3d2f2012-09-13 21:41:51 +0000287 if (recordingFlags & kOptimizeForClippedPlayback_RecordingFlag) {
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000288 SkBBoxHierarchy* tree = this->createBBoxHierarchy();
rileya@google.com7fb3d2f2012-09-13 21:41:51 +0000289 SkASSERT(NULL != tree);
robertphillips@google.com7c64bc92014-04-24 00:42:23 +0000290 fRecord = SkNEW_ARGS(SkBBoxHierarchyRecord, (this, size, recordingFlags, tree));
rileya@google.com7fb3d2f2012-09-13 21:41:51 +0000291 tree->unref();
292 } else {
robertphillips@google.com7c64bc92014-04-24 00:42:23 +0000293 fRecord = SkNEW_ARGS(SkPictureRecord, (this, size, recordingFlags));
rileya@google.com7fb3d2f2012-09-13 21:41:51 +0000294 }
reed@google.com118a6d02012-09-27 20:31:31 +0000295 fRecord->beginRecording();
reed@android.combcd4d5a2008-12-17 15:59:43 +0000296
reed@android.combcd4d5a2008-12-17 15:59:43 +0000297 return fRecord;
298}
299
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000300#endif
301
skia.committer@gmail.com0f1b1d62014-04-18 03:03:54 +0000302SkCanvas* SkPicture::beginRecording(int width, int height,
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000303 SkBBHFactory* bbhFactory,
304 uint32_t recordingFlags) {
305 if (fPlayback) {
306 SkDELETE(fPlayback);
307 fPlayback = NULL;
308 }
309 SkSafeUnref(fAccelData);
310 SkSafeSetNull(fRecord);
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000311 SkASSERT(NULL == fPathHeap);
commit-bot@chromium.org7a1832f2014-04-24 21:53:13 +0000312 fContentInfo.reset();
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000313
314 this->needsNewGenID();
315
316 fWidth = width;
317 fHeight = height;
318
319 const SkISize size = SkISize::Make(width, height);
320
321 if (NULL != bbhFactory) {
322 SkAutoTUnref<SkBBoxHierarchy> tree((*bbhFactory)(width, height));
323 SkASSERT(NULL != tree);
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000324 fRecord = SkNEW_ARGS(SkBBoxHierarchyRecord, (this, size,
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000325 recordingFlags|
326 kOptimizeForClippedPlayback_RecordingFlag,
327 tree.get()));
328 } else {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000329 fRecord = SkNEW_ARGS(SkPictureRecord, (this, size, recordingFlags));
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000330 }
331 fRecord->beginRecording();
332
333 return fRecord;
334}
335
336
337#ifdef SK_SUPPORT_LEGACY_DERIVED_PICTURE_CLASSES
338
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000339SkBBoxHierarchy* SkPicture::createBBoxHierarchy() const {
commit-bot@chromium.orgfd73c592014-04-16 16:02:10 +0000340 // TODO: this code is now replicated in SkRTreePicture. Once all external
341 // clients have been weaned off of kOptimizeForClippedPlayback_RecordingFlag,
342 // this code can be removed.
343
skia.committer@gmail.com8d230dd2012-11-02 02:01:24 +0000344 // These values were empirically determined to produce reasonable
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000345 // performance in most cases.
346 static const int kRTreeMinChildren = 6;
347 static const int kRTreeMaxChildren = 11;
348
349 SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth),
350 SkIntToScalar(fHeight));
sglez@google.com9baab6f2013-08-30 17:27:47 +0000351 bool sortDraws = false; // Do not sort draw calls when bulk loading.
352
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000353 return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren,
sglez@google.com9baab6f2013-08-30 17:27:47 +0000354 aspectRatio, sortDraws);
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000355}
356
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000357#endif
358
reed@android.combcd4d5a2008-12-17 15:59:43 +0000359SkCanvas* SkPicture::getRecordingCanvas() const {
360 // will be null if we are not recording
361 return fRecord;
362}
363
364void SkPicture::endRecording() {
365 if (NULL == fPlayback) {
366 if (NULL != fRecord) {
junov@chromium.org9aa18db2012-07-12 17:47:34 +0000367 fRecord->endRecording();
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000368 SkPictInfo info;
369 this->createHeader(&info);
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000370 fPlayback = SkNEW_ARGS(SkPicturePlayback, (this, *fRecord, info));
commit-bot@chromium.orgefea6992014-02-14 17:27:10 +0000371 SkSafeSetNull(fRecord);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000372 }
373 }
374 SkASSERT(NULL == fRecord);
375}
376
commit-bot@chromium.org4b918972014-03-18 17:45:32 +0000377const SkPicture::OperationList& SkPicture::OperationList::InvalidList() {
378 static OperationList gInvalid;
379 return gInvalid;
380}
381
382const SkPicture::OperationList& SkPicture::EXPERIMENTAL_getActiveOps(const SkIRect& queryRect) {
robertphillips@google.com6dd381b2014-04-13 19:09:42 +0000383 this->endRecording(); // TODO: remove eventually
commit-bot@chromium.org4b918972014-03-18 17:45:32 +0000384 if (NULL != fPlayback) {
385 return fPlayback->getActiveOps(queryRect);
386 }
387 return OperationList::InvalidList();
388}
389
commit-bot@chromium.org0ea71132014-03-24 19:40:49 +0000390size_t SkPicture::EXPERIMENTAL_curOpID() const {
391 if (NULL != fPlayback) {
392 return fPlayback->curOpID();
393 }
394 return 0;
395}
396
reed@google.com2b3a5082013-05-20 17:02:41 +0000397void SkPicture::draw(SkCanvas* surface, SkDrawPictureCallback* callback) {
robertphillips@google.com6dd381b2014-04-13 19:09:42 +0000398 this->endRecording(); // TODO: remove eventually
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000399 if (NULL != fPlayback) {
reed@google.com2b3a5082013-05-20 17:02:41 +0000400 fPlayback->draw(*surface, callback);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000401 }
402}
403
404///////////////////////////////////////////////////////////////////////////////
405
406#include "SkStream.h"
407
rmistry@google.com8793ad72013-12-02 13:50:38 +0000408static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' };
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000409
410bool SkPicture::IsValidPictInfo(const SkPictInfo& info) {
411 if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) {
412 return false;
413 }
414
415 if (info.fVersion < MIN_PICTURE_VERSION ||
416 info.fVersion > CURRENT_PICTURE_VERSION) {
417 return false;
418 }
419
420 return true;
421}
rmistry@google.com8793ad72013-12-02 13:50:38 +0000422
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000423bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
scroggo@google.com3b990302013-06-28 21:32:00 +0000424 if (NULL == stream) {
425 return false;
borenet@google.com30dda202012-09-17 18:26:06 +0000426 }
reed@google.com4bca7f62012-06-22 15:38:39 +0000427
rmistry@google.com8793ad72013-12-02 13:50:38 +0000428 // Check magic bytes.
reed@google.com4bca7f62012-06-22 15:38:39 +0000429 SkPictInfo info;
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000430 SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
431 if (!stream->read(&info, sizeof(info)) || !IsValidPictInfo(info)) {
scroggo@google.com3b990302013-06-28 21:32:00 +0000432 return false;
reed@google.com4bca7f62012-06-22 15:38:39 +0000433 }
reed@android.combcd4d5a2008-12-17 15:59:43 +0000434
scroggo@google.com3b990302013-06-28 21:32:00 +0000435 if (pInfo != NULL) {
436 *pInfo = info;
437 }
438 return true;
439}
440
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000441bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo) {
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000442 // Check magic bytes.
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000443 SkPictInfo info;
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000444 SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
445 if (!buffer.readByteArray(&info, sizeof(info)) || !IsValidPictInfo(info)) {
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000446 return false;
447 }
448
449 if (pInfo != NULL) {
450 *pInfo = info;
451 }
452 return true;
453}
454
scroggo@google.com3b990302013-06-28 21:32:00 +0000455SkPicture::SkPicture(SkPicturePlayback* playback, int width, int height)
456 : fPlayback(playback)
457 , fRecord(NULL)
458 , fWidth(width)
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000459 , fHeight(height)
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000460 , fAccelData(NULL) {
461 this->needsNewGenID();
462}
scroggo@google.com3b990302013-06-28 21:32:00 +0000463
464SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) {
465 SkPictInfo info;
466
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000467 if (!InternalOnly_StreamIsSKP(stream, &info)) {
scroggo@google.com3b990302013-06-28 21:32:00 +0000468 return NULL;
469 }
470
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000471 SkPicture* newPict = SkNEW_ARGS(SkPicture, (NULL, info.fWidth, info.fHeight));
472
scroggo@google.com3b990302013-06-28 21:32:00 +0000473 // Check to see if there is a playback to recreate.
reed@android.combcd4d5a2008-12-17 15:59:43 +0000474 if (stream->readBool()) {
skia.committer@gmail.com88c3a5d2014-04-24 03:05:07 +0000475 SkPicturePlayback* playback = SkPicturePlayback::CreateFromStream(newPict, stream,
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000476 info, proc);
scroggo@google.com3b7431c2013-10-01 15:30:46 +0000477 if (NULL == playback) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000478 SkDELETE(newPict);
scroggo@google.com3b7431c2013-10-01 15:30:46 +0000479 return NULL;
480 }
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000481 newPict->fPlayback = playback;
reed@android.combcd4d5a2008-12-17 15:59:43 +0000482 }
reed@google.com4bca7f62012-06-22 15:38:39 +0000483
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000484 return newPict;
reed@android.combcd4d5a2008-12-17 15:59:43 +0000485}
486
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000487SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) {
488 SkPictInfo info;
489
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000490 if (!InternalOnly_BufferIsSKP(buffer, &info)) {
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000491 return NULL;
492 }
493
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000494 SkPicture* newPict = SkNEW_ARGS(SkPicture, (NULL, info.fWidth, info.fHeight));
495
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000496 // Check to see if there is a playback to recreate.
497 if (buffer.readBool()) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000498 SkPicturePlayback* playback = SkPicturePlayback::CreateFromBuffer(newPict, buffer, info);
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000499 if (NULL == playback) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000500 SkDELETE(newPict);
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000501 return NULL;
502 }
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000503 newPict->fPlayback = playback;
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000504 }
505
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000506 return newPict;
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000507}
508
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000509void SkPicture::createHeader(SkPictInfo* info) const {
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000510 // Copy magic bytes at the beginning of the header
511 SkASSERT(sizeof(kMagic) == 8);
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000512 SkASSERT(sizeof(kMagic) == sizeof(info->fMagic));
513 memcpy(info->fMagic, kMagic, sizeof(kMagic));
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000514
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000515 // Set picture info after magic bytes in the header
commit-bot@chromium.org338284f2014-02-25 02:16:10 +0000516 info->fVersion = CURRENT_PICTURE_VERSION;
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000517 info->fWidth = fWidth;
518 info->fHeight = fHeight;
519 info->fFlags = SkPictInfo::kCrossProcess_Flag;
520 // TODO: remove this flag, since we're always float (now)
521 info->fFlags |= SkPictInfo::kScalarIsFloat_Flag;
522
523 if (8 == sizeof(void*)) {
524 info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag;
525 }
526}
527
scroggo@google.com2e727312013-02-22 22:04:19 +0000528void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const {
reed@android.combcd4d5a2008-12-17 15:59:43 +0000529 SkPicturePlayback* playback = fPlayback;
reed@google.com91911482011-02-07 15:30:46 +0000530
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000531 SkPictInfo info;
532 this->createHeader(&info);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000533 if (NULL == playback && fRecord) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000534 playback = SkNEW_ARGS(SkPicturePlayback, (this, *fRecord, info));
reed@android.combcd4d5a2008-12-17 15:59:43 +0000535 }
536
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000537 stream->write(&info, sizeof(info));
reed@android.combcd4d5a2008-12-17 15:59:43 +0000538 if (playback) {
539 stream->writeBool(true);
scroggo@google.com6ff73172012-10-04 21:46:08 +0000540 playback->serialize(stream, encoder);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000541 // delete playback if it is a local version (i.e. cons'd up just now)
542 if (playback != fPlayback) {
543 SkDELETE(playback);
544 }
545 } else {
546 stream->writeBool(false);
547 }
548}
549
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000550void SkPicture::WriteTagSize(SkWriteBuffer& buffer, uint32_t tag, size_t size) {
551 buffer.writeUInt(tag);
552 buffer.writeUInt(SkToU32(size));
553}
554
555void SkPicture::WriteTagSize(SkWStream* stream, uint32_t tag, size_t size) {
556 stream->write32(tag);
557 stream->write32(SkToU32(size));
558}
559
560bool SkPicture::parseBufferTag(SkReadBuffer& buffer,
skia.committer@gmail.com88c3a5d2014-04-24 03:05:07 +0000561 uint32_t tag,
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000562 uint32_t size) {
563 switch (tag) {
564 case SK_PICT_PATH_BUFFER_TAG:
565 if (size > 0) {
566 fPathHeap.reset(SkNEW_ARGS(SkPathHeap, (buffer)));
567 }
568 break;
569 default:
570 // The tag was invalid.
571 return false;
572 }
573
574 return true; // success
575}
576
577void SkPicture::flattenToBuffer(SkWriteBuffer& buffer) const {
578 int n;
579
580 if ((n = SafeCount(fPathHeap.get())) > 0) {
581 WriteTagSize(buffer, SK_PICT_PATH_BUFFER_TAG, n);
582 fPathHeap->flatten(buffer);
583 }
584}
585
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000586void SkPicture::flatten(SkWriteBuffer& buffer) const {
587 SkPicturePlayback* playback = fPlayback;
588
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000589 SkPictInfo info;
590 this->createHeader(&info);
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000591 if (NULL == playback && fRecord) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000592 playback = SkNEW_ARGS(SkPicturePlayback, (this, *fRecord, info));
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000593 }
594
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000595 buffer.writeByteArray(&info, sizeof(info));
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000596 if (playback) {
597 buffer.writeBool(true);
598 playback->flatten(buffer);
599 // delete playback if it is a local version (i.e. cons'd up just now)
600 if (playback != fPlayback) {
601 SkDELETE(playback);
602 }
603 } else {
604 buffer.writeBool(false);
605 }
606}
607
commit-bot@chromium.org05937b42014-03-19 22:54:40 +0000608#if SK_SUPPORT_GPU
commit-bot@chromium.org95cdc162014-03-19 21:24:25 +0000609bool SkPicture::suitableForGpuRasterization(GrContext* context) const {
commit-bot@chromium.org7a1832f2014-04-24 21:53:13 +0000610 // TODO: the heuristic used here needs to be refined
611 static const int kNumPaintWithPathEffectUsesTol = 1;
612 static const int kNumAAConcavePaths = 5;
613
614 SkASSERT(this->numAAHairlineConcavePaths() <= this->numAAConcavePaths());
615
616 return this->numPaintWithPathEffectUses() < kNumPaintWithPathEffectUsesTol &&
617 (this->numAAConcavePaths()-this->numAAHairlineConcavePaths()) < kNumAAConcavePaths;
commit-bot@chromium.org95cdc162014-03-19 21:24:25 +0000618}
commit-bot@chromium.org05937b42014-03-19 22:54:40 +0000619#endif
commit-bot@chromium.org95cdc162014-03-19 21:24:25 +0000620
tomhudson@google.comc7941702013-10-24 11:12:47 +0000621bool SkPicture::willPlayBackBitmaps() const {
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000622 if (!fPlayback) {
623 return false;
624 }
tomhudson@google.comc7941702013-10-24 11:12:47 +0000625 return fPlayback->containsBitmaps();
626}
627
djsollen@google.com6789a342013-02-01 16:18:09 +0000628#ifdef SK_BUILD_FOR_ANDROID
reed@android.combcd4d5a2008-12-17 15:59:43 +0000629void SkPicture::abortPlayback() {
630 if (NULL == fPlayback) {
631 return;
632 }
633 fPlayback->abort();
634}
djsollen@google.com6789a342013-02-01 16:18:09 +0000635#endif
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000636
637static int32_t next_picture_generation_id() {
638 static int32_t gPictureGenerationID = 0;
639 // do a loop in case our global wraps around, as we never want to
640 // return a 0
641 int32_t genID;
642 do {
643 genID = sk_atomic_inc(&gPictureGenerationID) + 1;
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000644 } while (SK_InvalidGenID == genID);
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000645 return genID;
646}
647
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000648uint32_t SkPicture::uniqueID() const {
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000649 if (NULL != fRecord) {
650 SkASSERT(NULL == fPlayback);
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000651 return SK_InvalidGenID;
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000652 }
653
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000654 if (SK_InvalidGenID == fUniqueID) {
655 fUniqueID = next_picture_generation_id();
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000656 }
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000657 return fUniqueID;
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000658}