3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #ifndef SkPicturePlayback_DEFINED
9 #define SkPicturePlayback_DEFINED
11 #include "SkPicture.h"
12 #include "SkReader32.h"
17 #include "SkOrderedReadBuffer.h"
20 #include "SkPathHeap.h"
23 #include "SkPictureFlat.h"
25 #ifdef SK_BUILD_FOR_ANDROID
29 class SkPictureRecord;
32 class SkBBoxHierarchy;
33 class SkPictureStateTree;
37 kCrossProcess_Flag = 1 << 0,
38 kScalarIsFloat_Flag = 1 << 1,
39 kPtrIs64Bit_Flag = 1 << 2,
49 * Container for data that is needed to deep copy a SkPicture. The container
50 * enables the data to be generated once and reused for subsequent copies.
52 struct SkPictCopyInfo {
53 SkPictCopyInfo() : initialized(false), controller(1024) {}
56 SkChunkFlatController controller;
57 SkTDArray<SkFlatData*> paintData;
60 class SkPicturePlayback {
63 SkPicturePlayback(const SkPicturePlayback& src, SkPictCopyInfo* deepCopyInfo = NULL);
64 explicit SkPicturePlayback(const SkPictureRecord& record, bool deepCopy = false);
65 static SkPicturePlayback* CreateFromStream(SkStream*, const SkPictInfo&,
66 SkPicture::InstallPixelRefProc);
68 virtual ~SkPicturePlayback();
70 void draw(SkCanvas& canvas, SkDrawPictureCallback*);
72 void serialize(SkWStream*, SkPicture::EncodeBitmap) const;
74 void dumpSize() const;
76 bool containsBitmaps() const;
78 #ifdef SK_BUILD_FOR_ANDROID
79 // Can be called in the middle of playback (the draw() call). WIll abort the
80 // drawing and return from draw() after the "current" op code is done
81 void abort() { fAbortCurrentPlayback = true; }
85 bool parseStream(SkStream*, const SkPictInfo&,
86 SkPicture::InstallPixelRefProc);
88 virtual bool preDraw(int opIndex, int type);
89 virtual void postDraw(int opIndex);
95 size_t length() { return fByteLength; }
96 const void* text() { return (const void*) fText; }
101 const SkBitmap& getBitmap(SkReader32& reader) {
102 const int index = reader.readInt();
103 if (SkBitmapHeap::INVALID_SLOT == index) {
105 SkDebugf("An invalid bitmap was recorded!\n");
109 return (*fBitmaps)[index];
112 void getMatrix(SkReader32& reader, SkMatrix* matrix) {
113 reader.readMatrix(matrix);
116 const SkPath& getPath(SkReader32& reader) {
117 return (*fPathHeap)[reader.readInt() - 1];
120 SkPicture& getPicture(SkReader32& reader) {
121 int index = reader.readInt();
122 SkASSERT(index > 0 && index <= fPictureCount);
123 return *fPictureRefs[index - 1];
126 const SkPaint* getPaint(SkReader32& reader) {
127 int index = reader.readInt();
131 return &(*fPaints)[index - 1];
134 const SkRect* getRectPtr(SkReader32& reader) {
135 if (reader.readBool()) {
136 return &reader.skipT<SkRect>();
142 const SkIRect* getIRectPtr(SkReader32& reader) {
143 if (reader.readBool()) {
144 return &reader.skipT<SkIRect>();
150 void getRegion(SkReader32& reader, SkRegion* region) {
151 reader.readRegion(region);
154 void getText(SkReader32& reader, TextContainer* text) {
155 size_t length = text->fByteLength = reader.readInt();
156 text->fText = (const char*)reader.skip(length);
163 int size(size_t* sizePtr);
164 int bitmaps(size_t* size);
165 int paints(size_t* size);
166 int paths(size_t* size);
171 void dumpBitmap(const SkBitmap& bitmap) const;
172 void dumpMatrix(const SkMatrix& matrix) const;
173 void dumpPaint(const SkPaint& paint) const;
174 void dumpPath(const SkPath& path) const;
175 void dumpPicture(const SkPicture& picture) const;
176 void dumpRegion(const SkRegion& region) const;
177 int dumpDrawType(char* bufferPtr, char* buffer, DrawType drawType);
178 int dumpInt(char* bufferPtr, char* buffer, char* name);
179 int dumpRect(char* bufferPtr, char* buffer, char* name);
180 int dumpPoint(char* bufferPtr, char* buffer, char* name);
181 void dumpPointArray(char** bufferPtrPtr, char* buffer, int count);
182 int dumpPtr(char* bufferPtr, char* buffer, char* name, void* ptr);
183 int dumpRectPtr(char* bufferPtr, char* buffer, char* name);
184 int dumpScalar(char* bufferPtr, char* buffer, char* name);
185 void dumpText(char** bufferPtrPtr, char* buffer);
192 private: // these help us with reading/writing
193 bool parseStreamTag(SkStream*, const SkPictInfo&, uint32_t tag, size_t size,
194 SkPicture::InstallPixelRefProc);
195 bool parseBufferTag(SkOrderedReadBuffer&, uint32_t tag, size_t size);
196 void flattenToBuffer(SkOrderedWriteBuffer&) const;
199 // Only used by getBitmap() if the passed in index is SkBitmapHeap::INVALID_SLOT. This empty
200 // bitmap allows playback to draw nothing and move on.
203 SkAutoTUnref<SkBitmapHeap> fBitmapHeap;
204 SkAutoTUnref<SkPathHeap> fPathHeap;
206 SkTRefArray<SkBitmap>* fBitmaps;
207 SkTRefArray<SkPaint>* fPaints;
209 SkData* fOpData; // opcodes and parameters
211 SkPicture** fPictureRefs;
214 SkBBoxHierarchy* fBoundingHierarchy;
215 SkPictureStateTree* fStateTree;
217 SkTypefacePlayback fTFPlayback;
218 SkFactoryPlayback* fFactoryPlayback;
219 #ifdef SK_BUILD_FOR_ANDROID
221 bool fAbortCurrentPlayback;