SkPicturePlayback.h revision 6f6dfb4e8c0e71ff75c63b27c1a3ca71d1e36bbb
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 "SkPictureFlat.h"
23#include "SkSerializationHelpers.h"
24
25#ifdef SK_BUILD_FOR_ANDROID
26#include "SkThread.h"
27#endif
28
29class SkPictureRecord;
30class SkStream;
31class SkWStream;
32class SkBBoxHierarchy;
33class SkPictureStateTree;
34
35struct 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 */
52struct SkPictCopyInfo {
53    SkPictCopyInfo() : initialized(false), controller(1024) {}
54
55    bool initialized;
56    SkChunkFlatController controller;
57    SkTDArray<SkFlatData*> paintData;
58};
59
60class SkPicturePlayback {
61public:
62    SkPicturePlayback();
63    SkPicturePlayback(const SkPicturePlayback& src, SkPictCopyInfo* deepCopyInfo = NULL);
64    explicit SkPicturePlayback(const SkPictureRecord& record, bool deepCopy = false);
65    SkPicturePlayback(SkStream*, const SkPictInfo&, bool* isValid,
66                      SkSerializationHelpers::DecodeBitmap decoder);
67
68    virtual ~SkPicturePlayback();
69
70    void draw(SkCanvas& canvas);
71
72    void serialize(SkWStream*, SkSerializationHelpers::EncodeBitmap) const;
73
74    void dumpSize() const;
75
76    // Can be called in the middle of playback (the draw() call). WIll abort the
77    // drawing and return from draw() after the "current" op code is done
78    void abort();
79
80protected:
81#ifdef SK_PICTURE_PROFILING_STUBS
82    virtual void preDraw(size_t offset, int type);
83    virtual void postDraw(size_t offset);
84#endif
85
86private:
87    class TextContainer {
88    public:
89        size_t length() { return fByteLength; }
90        const void* text() { return (const void*) fText; }
91        size_t fByteLength;
92        const char* fText;
93    };
94
95    const SkBitmap& getBitmap(SkReader32& reader) {
96        int index = reader.readInt();
97        return (*fBitmaps)[index];
98    }
99
100    const SkMatrix* getMatrix(SkReader32& reader) {
101        int index = reader.readInt();
102        if (index == 0) {
103            return NULL;
104        }
105        return &(*fMatrices)[index - 1];
106    }
107
108    const SkPath& getPath(SkReader32& reader) {
109        return (*fPathHeap)[reader.readInt() - 1];
110    }
111
112    SkPicture& getPicture(SkReader32& reader) {
113        int index = reader.readInt();
114        SkASSERT(index > 0 && index <= fPictureCount);
115        return *fPictureRefs[index - 1];
116    }
117
118    const SkPaint* getPaint(SkReader32& reader) {
119        int index = reader.readInt();
120        if (index == 0) {
121            return NULL;
122        }
123        return &(*fPaints)[index - 1];
124    }
125
126    const SkRect* getRectPtr(SkReader32& reader) {
127        if (reader.readBool()) {
128            return &reader.skipT<SkRect>();
129        } else {
130            return NULL;
131        }
132    }
133
134    const SkIRect* getIRectPtr(SkReader32& reader) {
135        if (reader.readBool()) {
136            return &reader.skipT<SkIRect>();
137        } else {
138            return NULL;
139        }
140    }
141
142    const SkRegion& getRegion(SkReader32& reader) {
143        int index = reader.readInt();
144        return (*fRegions)[index - 1];
145    }
146
147    void getText(SkReader32& reader, TextContainer* text) {
148        size_t length = text->fByteLength = reader.readInt();
149        text->fText = (const char*)reader.skip(length);
150    }
151
152    void init();
153
154#ifdef SK_DEBUG_SIZE
155public:
156    int size(size_t* sizePtr);
157    int bitmaps(size_t* size);
158    int paints(size_t* size);
159    int paths(size_t* size);
160    int regions(size_t* size);
161#endif
162
163#ifdef SK_DEBUG_DUMP
164private:
165    void dumpBitmap(const SkBitmap& bitmap) const;
166    void dumpMatrix(const SkMatrix& matrix) const;
167    void dumpPaint(const SkPaint& paint) const;
168    void dumpPath(const SkPath& path) const;
169    void dumpPicture(const SkPicture& picture) const;
170    void dumpRegion(const SkRegion& region) const;
171    int dumpDrawType(char* bufferPtr, char* buffer, DrawType drawType);
172    int dumpInt(char* bufferPtr, char* buffer, char* name);
173    int dumpRect(char* bufferPtr, char* buffer, char* name);
174    int dumpPoint(char* bufferPtr, char* buffer, char* name);
175    void dumpPointArray(char** bufferPtrPtr, char* buffer, int count);
176    int dumpPtr(char* bufferPtr, char* buffer, char* name, void* ptr);
177    int dumpRectPtr(char* bufferPtr, char* buffer, char* name);
178    int dumpScalar(char* bufferPtr, char* buffer, char* name);
179    void dumpText(char** bufferPtrPtr, char* buffer);
180    void dumpStream();
181
182public:
183    void dump() const;
184#endif
185
186private:    // these help us with reading/writing
187    bool parseStreamTag(SkStream*, const SkPictInfo&, uint32_t tag, size_t size,
188                        SkSerializationHelpers::DecodeBitmap decoder);
189    bool parseBufferTag(SkOrderedReadBuffer&, uint32_t tag, size_t size);
190    void flattenToBuffer(SkOrderedWriteBuffer&) const;
191
192private:
193    SkAutoTUnref<SkBitmapHeap> fBitmapHeap;
194    SkAutoTUnref<SkPathHeap> fPathHeap;
195
196    SkTRefArray<SkBitmap>* fBitmaps;
197    SkTRefArray<SkMatrix>* fMatrices;
198    SkTRefArray<SkPaint>* fPaints;
199    SkTRefArray<SkRegion>* fRegions;
200
201    SkData* fOpData;    // opcodes and parameters
202
203    SkPicture** fPictureRefs;
204    int fPictureCount;
205
206    SkBBoxHierarchy* fBoundingHierarchy;
207    SkPictureStateTree* fStateTree;
208
209    SkTypefacePlayback fTFPlayback;
210    SkFactoryPlayback* fFactoryPlayback;
211#ifdef SK_BUILD_FOR_ANDROID
212    SkMutex fDrawMutex;
213#endif
214};
215
216#endif
217