[go: nahoru, domu]

blob: 05e579101e15914b07da3b5974e9be3220b3de3e [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
125SkPicture::SkPicture() {
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000126 this->needsNewGenID();
reed@android.combcd4d5a2008-12-17 15:59:43 +0000127 fRecord = NULL;
128 fPlayback = NULL;
129 fWidth = fHeight = 0;
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000130 fAccelData = NULL;
reed@android.combcd4d5a2008-12-17 15:59:43 +0000131}
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()
135 , fAccelData(NULL) {
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000136 this->needsNewGenID();
reed@android.combcd4d5a2008-12-17 15:59:43 +0000137 fWidth = src.fWidth;
138 fHeight = src.fHeight;
139 fRecord = NULL;
140
141 /* We want to copy the src's playback. However, if that hasn't been built
142 yet, we need to fake a call to endRecording() without actually calling
143 it (since it is destructive, and we don't want to change src).
144 */
145 if (src.fPlayback) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000146 fPlayback = SkNEW_ARGS(SkPicturePlayback, (this, *src.fPlayback));
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000147 SkASSERT(NULL == src.fRecord);
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000148 fUniqueID = src.uniqueID(); // need to call method to ensure != 0
reed@android.combcd4d5a2008-12-17 15:59:43 +0000149 } else if (src.fRecord) {
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000150 SkPictInfo info;
151 this->createHeader(&info);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000152 // here we do a fake src.endRecording()
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000153 fPlayback = SkNEW_ARGS(SkPicturePlayback, (this, *src.fRecord, info));
reed@android.combcd4d5a2008-12-17 15:59:43 +0000154 } else {
155 fPlayback = NULL;
156 }
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000157
158 fPathHeap.reset(SkSafeRef(src.fPathHeap.get()));
159}
160
161const SkPath& SkPicture::getPath(int index) const {
162 return (*fPathHeap.get())[index];
163}
164
165int SkPicture::addPathToHeap(const SkPath& path) {
166 if (NULL == fPathHeap) {
167 fPathHeap.reset(SkNEW(SkPathHeap));
168 }
169#ifdef SK_DEDUP_PICTURE_PATHS
170 return fPathHeap->insert(path);
171#else
172 return fPathHeap->append(path);
173#endif
174}
175
176void SkPicture::initForPlayback() const {
177 // ensure that the paths bounds are pre-computed
178 if (NULL != fPathHeap.get()) {
179 for (int i = 0; i < fPathHeap->count(); i++) {
180 (*fPathHeap.get())[i].updateBoundsCache();
181 }
182 }
183}
184
185void SkPicture::dumpSize() const {
186 SkDebugf("--- picture size: paths=%d\n",
187 SafeCount(fPathHeap.get()));
reed@android.combcd4d5a2008-12-17 15:59:43 +0000188}
189
190SkPicture::~SkPicture() {
reed@google.com91911482011-02-07 15:30:46 +0000191 SkSafeUnref(fRecord);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000192 SkDELETE(fPlayback);
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000193 SkSafeUnref(fAccelData);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000194}
195
commit-bot@chromium.orge6e785e2014-03-04 19:08:57 +0000196void SkPicture::internalOnly_EnableOpts(bool enableOpts) {
197 if (NULL != fRecord) {
198 fRecord->internalOnly_EnableOpts(enableOpts);
199 }
200}
201
reed@android.combcd4d5a2008-12-17 15:59:43 +0000202void SkPicture::swap(SkPicture& other) {
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000203 SkTSwap(fUniqueID, other.fUniqueID);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000204 SkTSwap(fRecord, other.fRecord);
205 SkTSwap(fPlayback, other.fPlayback);
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000206 SkTSwap(fAccelData, other.fAccelData);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000207 SkTSwap(fWidth, other.fWidth);
208 SkTSwap(fHeight, other.fHeight);
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000209 fPathHeap.swap(&other.fPathHeap);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000210}
211
djsollen@google.com3855f872012-08-29 18:52:07 +0000212SkPicture* SkPicture::clone() const {
213 SkPicture* clonedPicture = SkNEW(SkPicture);
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000214 this->clone(clonedPicture, 1);
djsollen@google.com3855f872012-08-29 18:52:07 +0000215 return clonedPicture;
216}
217
218void SkPicture::clone(SkPicture* pictures, int count) const {
219 SkPictCopyInfo copyInfo;
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000220 SkPictInfo info;
221 this->createHeader(&info);
djsollen@google.com3855f872012-08-29 18:52:07 +0000222
223 for (int i = 0; i < count; i++) {
224 SkPicture* clone = &pictures[i];
225
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000226 clone->needsNewGenID();
djsollen@google.com3855f872012-08-29 18:52:07 +0000227 clone->fWidth = fWidth;
228 clone->fHeight = fHeight;
commit-bot@chromium.orgefea6992014-02-14 17:27:10 +0000229 SkSafeSetNull(clone->fRecord);
scroggo@google.coma0dd4332012-11-02 20:51:19 +0000230 SkDELETE(clone->fPlayback);
231
djsollen@google.com3855f872012-08-29 18:52:07 +0000232 /* We want to copy the src's playback. However, if that hasn't been built
233 yet, we need to fake a call to endRecording() without actually calling
234 it (since it is destructive, and we don't want to change src).
235 */
236 if (fPlayback) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000237 clone->fPlayback = SkNEW_ARGS(SkPicturePlayback, (clone, *fPlayback, &copyInfo));
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000238 SkASSERT(NULL == fRecord);
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000239 clone->fUniqueID = this->uniqueID(); // need to call method to ensure != 0
djsollen@google.com3855f872012-08-29 18:52:07 +0000240 } else if (fRecord) {
241 // here we do a fake src.endRecording()
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000242 clone->fPlayback = SkNEW_ARGS(SkPicturePlayback, (clone, *fRecord, info, true));
djsollen@google.com3855f872012-08-29 18:52:07 +0000243 } else {
244 clone->fPlayback = NULL;
245 }
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000246
247 clone->fPathHeap.reset(SkSafeRef(fPathHeap.get()));
djsollen@google.com3855f872012-08-29 18:52:07 +0000248 }
249}
250
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000251SkPicture::AccelData::Domain SkPicture::AccelData::GenerateDomain() {
252 static int32_t gNextID = 0;
253
254 int32_t id = sk_atomic_inc(&gNextID);
255 if (id >= 1 << (8 * sizeof(Domain))) {
256 SK_CRASH();
257 }
258
259 return static_cast<Domain>(id);
260}
261
reed@android.combcd4d5a2008-12-17 15:59:43 +0000262///////////////////////////////////////////////////////////////////////////////
263
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000264#ifdef SK_SUPPORT_LEGACY_DERIVED_PICTURE_CLASSES
265
reed@android.comc4a55d72009-02-13 14:56:09 +0000266SkCanvas* SkPicture::beginRecording(int width, int height,
267 uint32_t recordingFlags) {
reed@android.combcd4d5a2008-12-17 15:59:43 +0000268 if (fPlayback) {
269 SkDELETE(fPlayback);
270 fPlayback = NULL;
271 }
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000272 SkSafeUnref(fAccelData);
commit-bot@chromium.orgefea6992014-02-14 17:27:10 +0000273 SkSafeSetNull(fRecord);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000274
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000275 this->needsNewGenID();
276
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000277 // Must be set before calling createBBoxHierarchy
278 fWidth = width;
279 fHeight = height;
280
commit-bot@chromium.orgca101d62014-02-17 15:28:00 +0000281 const SkISize size = SkISize::Make(width, height);
282
rileya@google.com7fb3d2f2012-09-13 21:41:51 +0000283 if (recordingFlags & kOptimizeForClippedPlayback_RecordingFlag) {
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000284 SkBBoxHierarchy* tree = this->createBBoxHierarchy();
rileya@google.com7fb3d2f2012-09-13 21:41:51 +0000285 SkASSERT(NULL != tree);
commit-bot@chromium.orgca101d62014-02-17 15:28:00 +0000286 fRecord = SkNEW_ARGS(SkBBoxHierarchyRecord, (size, recordingFlags, tree));
rileya@google.com7fb3d2f2012-09-13 21:41:51 +0000287 tree->unref();
288 } else {
commit-bot@chromium.orgca101d62014-02-17 15:28:00 +0000289 fRecord = SkNEW_ARGS(SkPictureRecord, (size, recordingFlags));
rileya@google.com7fb3d2f2012-09-13 21:41:51 +0000290 }
reed@google.com118a6d02012-09-27 20:31:31 +0000291 fRecord->beginRecording();
reed@android.combcd4d5a2008-12-17 15:59:43 +0000292
reed@android.combcd4d5a2008-12-17 15:59:43 +0000293 return fRecord;
294}
295
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000296#endif
297
skia.committer@gmail.com0f1b1d62014-04-18 03:03:54 +0000298SkCanvas* SkPicture::beginRecording(int width, int height,
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000299 SkBBHFactory* bbhFactory,
300 uint32_t recordingFlags) {
301 if (fPlayback) {
302 SkDELETE(fPlayback);
303 fPlayback = NULL;
304 }
305 SkSafeUnref(fAccelData);
306 SkSafeSetNull(fRecord);
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000307 SkASSERT(NULL == fPathHeap);
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000308
309 this->needsNewGenID();
310
311 fWidth = width;
312 fHeight = height;
313
314 const SkISize size = SkISize::Make(width, height);
315
316 if (NULL != bbhFactory) {
317 SkAutoTUnref<SkBBoxHierarchy> tree((*bbhFactory)(width, height));
318 SkASSERT(NULL != tree);
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000319 fRecord = SkNEW_ARGS(SkBBoxHierarchyRecord, (this, size,
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000320 recordingFlags|
321 kOptimizeForClippedPlayback_RecordingFlag,
322 tree.get()));
323 } else {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000324 fRecord = SkNEW_ARGS(SkPictureRecord, (this, size, recordingFlags));
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000325 }
326 fRecord->beginRecording();
327
328 return fRecord;
329}
330
331
332#ifdef SK_SUPPORT_LEGACY_DERIVED_PICTURE_CLASSES
333
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000334SkBBoxHierarchy* SkPicture::createBBoxHierarchy() const {
commit-bot@chromium.orgfd73c592014-04-16 16:02:10 +0000335 // TODO: this code is now replicated in SkRTreePicture. Once all external
336 // clients have been weaned off of kOptimizeForClippedPlayback_RecordingFlag,
337 // this code can be removed.
338
skia.committer@gmail.com8d230dd2012-11-02 02:01:24 +0000339 // These values were empirically determined to produce reasonable
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000340 // performance in most cases.
341 static const int kRTreeMinChildren = 6;
342 static const int kRTreeMaxChildren = 11;
343
344 SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth),
345 SkIntToScalar(fHeight));
sglez@google.com9baab6f2013-08-30 17:27:47 +0000346 bool sortDraws = false; // Do not sort draw calls when bulk loading.
347
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000348 return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren,
sglez@google.com9baab6f2013-08-30 17:27:47 +0000349 aspectRatio, sortDraws);
junov@chromium.orgbb78c442012-11-01 17:10:32 +0000350}
351
commit-bot@chromium.org48da8612014-04-17 23:35:06 +0000352#endif
353
reed@android.combcd4d5a2008-12-17 15:59:43 +0000354SkCanvas* SkPicture::getRecordingCanvas() const {
355 // will be null if we are not recording
356 return fRecord;
357}
358
359void SkPicture::endRecording() {
360 if (NULL == fPlayback) {
361 if (NULL != fRecord) {
junov@chromium.org9aa18db2012-07-12 17:47:34 +0000362 fRecord->endRecording();
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000363 SkPictInfo info;
364 this->createHeader(&info);
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000365 fPlayback = SkNEW_ARGS(SkPicturePlayback, (this, *fRecord, info));
commit-bot@chromium.orgefea6992014-02-14 17:27:10 +0000366 SkSafeSetNull(fRecord);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000367 }
368 }
369 SkASSERT(NULL == fRecord);
370}
371
commit-bot@chromium.org4b918972014-03-18 17:45:32 +0000372const SkPicture::OperationList& SkPicture::OperationList::InvalidList() {
373 static OperationList gInvalid;
374 return gInvalid;
375}
376
377const SkPicture::OperationList& SkPicture::EXPERIMENTAL_getActiveOps(const SkIRect& queryRect) {
robertphillips@google.com6dd381b2014-04-13 19:09:42 +0000378 this->endRecording(); // TODO: remove eventually
commit-bot@chromium.org4b918972014-03-18 17:45:32 +0000379 if (NULL != fPlayback) {
380 return fPlayback->getActiveOps(queryRect);
381 }
382 return OperationList::InvalidList();
383}
384
commit-bot@chromium.org0ea71132014-03-24 19:40:49 +0000385size_t SkPicture::EXPERIMENTAL_curOpID() const {
386 if (NULL != fPlayback) {
387 return fPlayback->curOpID();
388 }
389 return 0;
390}
391
reed@google.com2b3a5082013-05-20 17:02:41 +0000392void SkPicture::draw(SkCanvas* surface, SkDrawPictureCallback* callback) {
robertphillips@google.com6dd381b2014-04-13 19:09:42 +0000393 this->endRecording(); // TODO: remove eventually
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000394 if (NULL != fPlayback) {
reed@google.com2b3a5082013-05-20 17:02:41 +0000395 fPlayback->draw(*surface, callback);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000396 }
397}
398
399///////////////////////////////////////////////////////////////////////////////
400
401#include "SkStream.h"
402
rmistry@google.com8793ad72013-12-02 13:50:38 +0000403static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' };
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000404
405bool SkPicture::IsValidPictInfo(const SkPictInfo& info) {
406 if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) {
407 return false;
408 }
409
410 if (info.fVersion < MIN_PICTURE_VERSION ||
411 info.fVersion > CURRENT_PICTURE_VERSION) {
412 return false;
413 }
414
415 return true;
416}
rmistry@google.com8793ad72013-12-02 13:50:38 +0000417
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000418bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
scroggo@google.com3b990302013-06-28 21:32:00 +0000419 if (NULL == stream) {
420 return false;
borenet@google.com30dda202012-09-17 18:26:06 +0000421 }
reed@google.com4bca7f62012-06-22 15:38:39 +0000422
rmistry@google.com8793ad72013-12-02 13:50:38 +0000423 // Check magic bytes.
reed@google.com4bca7f62012-06-22 15:38:39 +0000424 SkPictInfo info;
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000425 SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
426 if (!stream->read(&info, sizeof(info)) || !IsValidPictInfo(info)) {
scroggo@google.com3b990302013-06-28 21:32:00 +0000427 return false;
reed@google.com4bca7f62012-06-22 15:38:39 +0000428 }
reed@android.combcd4d5a2008-12-17 15:59:43 +0000429
scroggo@google.com3b990302013-06-28 21:32:00 +0000430 if (pInfo != NULL) {
431 *pInfo = info;
432 }
433 return true;
434}
435
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000436bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo) {
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000437 // Check magic bytes.
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000438 SkPictInfo info;
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000439 SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
440 if (!buffer.readByteArray(&info, sizeof(info)) || !IsValidPictInfo(info)) {
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000441 return false;
442 }
443
444 if (pInfo != NULL) {
445 *pInfo = info;
446 }
447 return true;
448}
449
scroggo@google.com3b990302013-06-28 21:32:00 +0000450SkPicture::SkPicture(SkPicturePlayback* playback, int width, int height)
451 : fPlayback(playback)
452 , fRecord(NULL)
453 , fWidth(width)
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000454 , fHeight(height)
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000455 , fAccelData(NULL) {
456 this->needsNewGenID();
457}
scroggo@google.com3b990302013-06-28 21:32:00 +0000458
459SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) {
460 SkPictInfo info;
461
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000462 if (!InternalOnly_StreamIsSKP(stream, &info)) {
scroggo@google.com3b990302013-06-28 21:32:00 +0000463 return NULL;
464 }
465
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000466 SkPicture* newPict = SkNEW_ARGS(SkPicture, (NULL, info.fWidth, info.fHeight));
467
scroggo@google.com3b990302013-06-28 21:32:00 +0000468 // Check to see if there is a playback to recreate.
reed@android.combcd4d5a2008-12-17 15:59:43 +0000469 if (stream->readBool()) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000470 SkPicturePlayback* playback = SkPicturePlayback::CreateFromStream(newPict, stream,
471 info, proc);
scroggo@google.com3b7431c2013-10-01 15:30:46 +0000472 if (NULL == playback) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000473 SkDELETE(newPict);
scroggo@google.com3b7431c2013-10-01 15:30:46 +0000474 return NULL;
475 }
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000476 newPict->fPlayback = playback;
reed@android.combcd4d5a2008-12-17 15:59:43 +0000477 }
reed@google.com4bca7f62012-06-22 15:38:39 +0000478
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000479 return newPict;
reed@android.combcd4d5a2008-12-17 15:59:43 +0000480}
481
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000482SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) {
483 SkPictInfo info;
484
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000485 if (!InternalOnly_BufferIsSKP(buffer, &info)) {
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000486 return NULL;
487 }
488
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000489 SkPicture* newPict = SkNEW_ARGS(SkPicture, (NULL, info.fWidth, info.fHeight));
490
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000491 // Check to see if there is a playback to recreate.
492 if (buffer.readBool()) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000493 SkPicturePlayback* playback = SkPicturePlayback::CreateFromBuffer(newPict, buffer, info);
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000494 if (NULL == playback) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000495 SkDELETE(newPict);
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000496 return NULL;
497 }
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000498 newPict->fPlayback = playback;
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000499 }
500
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000501 return newPict;
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000502}
503
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000504void SkPicture::createHeader(SkPictInfo* info) const {
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000505 // Copy magic bytes at the beginning of the header
506 SkASSERT(sizeof(kMagic) == 8);
commit-bot@chromium.org060e3a52014-03-12 14:46:41 +0000507 SkASSERT(sizeof(kMagic) == sizeof(info->fMagic));
508 memcpy(info->fMagic, kMagic, sizeof(kMagic));
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000509
commit-bot@chromium.orgabad6d62014-03-03 19:18:39 +0000510 // Set picture info after magic bytes in the header
commit-bot@chromium.org338284f2014-02-25 02:16:10 +0000511 info->fVersion = CURRENT_PICTURE_VERSION;
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000512 info->fWidth = fWidth;
513 info->fHeight = fHeight;
514 info->fFlags = SkPictInfo::kCrossProcess_Flag;
515 // TODO: remove this flag, since we're always float (now)
516 info->fFlags |= SkPictInfo::kScalarIsFloat_Flag;
517
518 if (8 == sizeof(void*)) {
519 info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag;
520 }
521}
522
scroggo@google.com2e727312013-02-22 22:04:19 +0000523void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const {
reed@android.combcd4d5a2008-12-17 15:59:43 +0000524 SkPicturePlayback* playback = fPlayback;
reed@google.com91911482011-02-07 15:30:46 +0000525
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000526 SkPictInfo info;
527 this->createHeader(&info);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000528 if (NULL == playback && fRecord) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000529 playback = SkNEW_ARGS(SkPicturePlayback, (this, *fRecord, info));
reed@android.combcd4d5a2008-12-17 15:59:43 +0000530 }
531
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000532 stream->write(&info, sizeof(info));
reed@android.combcd4d5a2008-12-17 15:59:43 +0000533 if (playback) {
534 stream->writeBool(true);
scroggo@google.com6ff73172012-10-04 21:46:08 +0000535 playback->serialize(stream, encoder);
reed@android.combcd4d5a2008-12-17 15:59:43 +0000536 // delete playback if it is a local version (i.e. cons'd up just now)
537 if (playback != fPlayback) {
538 SkDELETE(playback);
539 }
540 } else {
541 stream->writeBool(false);
542 }
543}
544
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000545void SkPicture::WriteTagSize(SkWriteBuffer& buffer, uint32_t tag, size_t size) {
546 buffer.writeUInt(tag);
547 buffer.writeUInt(SkToU32(size));
548}
549
550void SkPicture::WriteTagSize(SkWStream* stream, uint32_t tag, size_t size) {
551 stream->write32(tag);
552 stream->write32(SkToU32(size));
553}
554
555bool SkPicture::parseBufferTag(SkReadBuffer& buffer,
556 uint32_t tag,
557 uint32_t size) {
558 switch (tag) {
559 case SK_PICT_PATH_BUFFER_TAG:
560 if (size > 0) {
561 fPathHeap.reset(SkNEW_ARGS(SkPathHeap, (buffer)));
562 }
563 break;
564 default:
565 // The tag was invalid.
566 return false;
567 }
568
569 return true; // success
570}
571
572void SkPicture::flattenToBuffer(SkWriteBuffer& buffer) const {
573 int n;
574
575 if ((n = SafeCount(fPathHeap.get())) > 0) {
576 WriteTagSize(buffer, SK_PICT_PATH_BUFFER_TAG, n);
577 fPathHeap->flatten(buffer);
578 }
579}
580
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000581void SkPicture::flatten(SkWriteBuffer& buffer) const {
582 SkPicturePlayback* playback = fPlayback;
583
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000584 SkPictInfo info;
585 this->createHeader(&info);
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000586 if (NULL == playback && fRecord) {
commit-bot@chromium.orgfc486cd2014-04-23 22:35:42 +0000587 playback = SkNEW_ARGS(SkPicturePlayback, (this, *fRecord, info));
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000588 }
589
commit-bot@chromium.org206dc582014-03-28 18:05:47 +0000590 buffer.writeByteArray(&info, sizeof(info));
commit-bot@chromium.org7183ff62014-02-07 12:20:04 +0000591 if (playback) {
592 buffer.writeBool(true);
593 playback->flatten(buffer);
594 // delete playback if it is a local version (i.e. cons'd up just now)
595 if (playback != fPlayback) {
596 SkDELETE(playback);
597 }
598 } else {
599 buffer.writeBool(false);
600 }
601}
602
commit-bot@chromium.org05937b42014-03-19 22:54:40 +0000603#if SK_SUPPORT_GPU
commit-bot@chromium.org95cdc162014-03-19 21:24:25 +0000604bool SkPicture::suitableForGpuRasterization(GrContext* context) const {
605 // Stub for now; never veto GPu rasterization.
606 return true;
607}
commit-bot@chromium.org05937b42014-03-19 22:54:40 +0000608#endif
commit-bot@chromium.org95cdc162014-03-19 21:24:25 +0000609
tomhudson@google.comc7941702013-10-24 11:12:47 +0000610bool SkPicture::willPlayBackBitmaps() const {
commit-bot@chromium.org73857a32014-03-16 19:46:36 +0000611 if (!fPlayback) {
612 return false;
613 }
tomhudson@google.comc7941702013-10-24 11:12:47 +0000614 return fPlayback->containsBitmaps();
615}
616
djsollen@google.com6789a342013-02-01 16:18:09 +0000617#ifdef SK_BUILD_FOR_ANDROID
reed@android.combcd4d5a2008-12-17 15:59:43 +0000618void SkPicture::abortPlayback() {
619 if (NULL == fPlayback) {
620 return;
621 }
622 fPlayback->abort();
623}
djsollen@google.com6789a342013-02-01 16:18:09 +0000624#endif
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000625
626static int32_t next_picture_generation_id() {
627 static int32_t gPictureGenerationID = 0;
628 // do a loop in case our global wraps around, as we never want to
629 // return a 0
630 int32_t genID;
631 do {
632 genID = sk_atomic_inc(&gPictureGenerationID) + 1;
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000633 } while (SK_InvalidGenID == genID);
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000634 return genID;
635}
636
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000637uint32_t SkPicture::uniqueID() const {
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000638 if (NULL != fRecord) {
639 SkASSERT(NULL == fPlayback);
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000640 return SK_InvalidGenID;
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000641 }
642
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000643 if (SK_InvalidGenID == fUniqueID) {
644 fUniqueID = next_picture_generation_id();
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000645 }
commit-bot@chromium.orgb50bcb92014-04-07 18:26:22 +0000646 return fUniqueID;
robertphillips@google.com647fe7f2014-04-02 23:51:13 +0000647}