Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkPicturePlayback.h
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  */
8 #ifndef SkPicturePlayback_DEFINED
9 #define SkPicturePlayback_DEFINED
10
11 #include "SkPicture.h"
12 #include "SkReader32.h"
13
14 #include "SkBitmap.h"
15 #include "SkData.h"
16 #include "SkMatrix.h"
17 #include "SkOrderedReadBuffer.h"
18 #include "SkPaint.h"
19 #include "SkPath.h"
20 #include "SkPathHeap.h"
21 #include "SkRegion.h"
22 #include "SkRRect.h"
23 #include "SkPictureFlat.h"
24
25 #ifdef SK_BUILD_FOR_ANDROID
26 #include "SkThread.h"
27 #endif
28
29 class SkPictureRecord;
30 class SkStream;
31 class SkWStream;
32 class SkBBoxHierarchy;
33 class SkPictureStateTree;
34
35 struct SkPictInfo {
36     enum Flags {
37         kCrossProcess_Flag      = 1 << 0,
38         kScalarIsFloat_Flag     = 1 << 1,
39         kPtrIs64Bit_Flag        = 1 << 2,
40     };
41
42     uint32_t    fVersion;
43     uint32_t    fWidth;
44     uint32_t    fHeight;
45     uint32_t    fFlags;
46 };
47
48 /**
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.
51  */
52 struct SkPictCopyInfo {
53     SkPictCopyInfo() : initialized(false), controller(1024) {}
54
55     bool initialized;
56     SkChunkFlatController controller;
57     SkTDArray<SkFlatData*> paintData;
58 };
59
60 class SkPicturePlayback {
61 public:
62     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);
67
68     virtual ~SkPicturePlayback();
69
70     void draw(SkCanvas& canvas, SkDrawPictureCallback*);
71
72     void serialize(SkWStream*, SkPicture::EncodeBitmap) const;
73
74     void dumpSize() const;
75
76     bool containsBitmaps() const;
77
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; }
82 #endif
83
84 protected:
85     bool parseStream(SkStream*, const SkPictInfo&,
86                      SkPicture::InstallPixelRefProc);
87 #ifdef SK_DEVELOPER
88     virtual bool preDraw(int opIndex, int type);
89     virtual void postDraw(int opIndex);
90 #endif
91
92 private:
93     class TextContainer {
94     public:
95         size_t length() { return fByteLength; }
96         const void* text() { return (const void*) fText; }
97         size_t fByteLength;
98         const char* fText;
99     };
100
101     const SkBitmap& getBitmap(SkReader32& reader) {
102         const int index = reader.readInt();
103         if (SkBitmapHeap::INVALID_SLOT == index) {
104 #ifdef SK_DEBUG
105             SkDebugf("An invalid bitmap was recorded!\n");
106 #endif
107             return fBadBitmap;
108         }
109         return (*fBitmaps)[index];
110     }
111
112     void getMatrix(SkReader32& reader, SkMatrix* matrix) {
113         reader.readMatrix(matrix);
114     }
115
116     const SkPath& getPath(SkReader32& reader) {
117         return (*fPathHeap)[reader.readInt() - 1];
118     }
119
120     SkPicture& getPicture(SkReader32& reader) {
121         int index = reader.readInt();
122         SkASSERT(index > 0 && index <= fPictureCount);
123         return *fPictureRefs[index - 1];
124     }
125
126     const SkPaint* getPaint(SkReader32& reader) {
127         int index = reader.readInt();
128         if (index == 0) {
129             return NULL;
130         }
131         return &(*fPaints)[index - 1];
132     }
133
134     const SkRect* getRectPtr(SkReader32& reader) {
135         if (reader.readBool()) {
136             return &reader.skipT<SkRect>();
137         } else {
138             return NULL;
139         }
140     }
141
142     const SkIRect* getIRectPtr(SkReader32& reader) {
143         if (reader.readBool()) {
144             return &reader.skipT<SkIRect>();
145         } else {
146             return NULL;
147         }
148     }
149
150     void getRegion(SkReader32& reader, SkRegion* region) {
151         reader.readRegion(region);
152     }
153
154     void getText(SkReader32& reader, TextContainer* text) {
155         size_t length = text->fByteLength = reader.readInt();
156         text->fText = (const char*)reader.skip(length);
157     }
158
159     void init();
160
161 #ifdef SK_DEBUG_SIZE
162 public:
163     int size(size_t* sizePtr);
164     int bitmaps(size_t* size);
165     int paints(size_t* size);
166     int paths(size_t* size);
167 #endif
168
169 #ifdef SK_DEBUG_DUMP
170 private:
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);
186     void dumpStream();
187
188 public:
189     void dump() const;
190 #endif
191
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;
197
198 private:
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.
201     SkBitmap fBadBitmap;
202
203     SkAutoTUnref<SkBitmapHeap> fBitmapHeap;
204     SkAutoTUnref<SkPathHeap> fPathHeap;
205
206     SkTRefArray<SkBitmap>* fBitmaps;
207     SkTRefArray<SkPaint>* fPaints;
208
209     SkData* fOpData;    // opcodes and parameters
210
211     SkPicture** fPictureRefs;
212     int fPictureCount;
213
214     SkBBoxHierarchy* fBoundingHierarchy;
215     SkPictureStateTree* fStateTree;
216
217     SkTypefacePlayback fTFPlayback;
218     SkFactoryPlayback* fFactoryPlayback;
219 #ifdef SK_BUILD_FOR_ANDROID
220     SkMutex fDrawMutex;
221     bool fAbortCurrentPlayback;
222 #endif
223 };
224
225 #endif