• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkPictureData_DEFINED
9 #define SkPictureData_DEFINED
10 
11 #include "SkBitmap.h"
12 #include "SkDrawable.h"
13 #include "SkPicture.h"
14 #include "SkPictureFlat.h"
15 
16 class SkData;
17 class SkPictureRecord;
18 class SkReader32;
19 struct SkSerialProcs;
20 class SkStream;
21 class SkWStream;
22 class SkBBoxHierarchy;
23 class SkMatrix;
24 class SkPaint;
25 class SkPath;
26 class SkReadBuffer;
27 class SkTextBlob;
28 
29 struct SkPictInfo {
SkPictInfoSkPictInfo30     SkPictInfo() : fVersion(~0U) {}
31 
getVersionSkPictInfo32     uint32_t getVersion() const {
33         SkASSERT(fVersion != ~0U);
34         return fVersion;
35     }
36 
setVersionSkPictInfo37     void setVersion(uint32_t version) {
38         SkASSERT(version != ~0U);
39         fVersion = version;
40     }
41 
42 public:
43     char        fMagic[8];
44 private:
45     uint32_t    fVersion;
46 public:
47     SkRect      fCullRect;
48 };
49 
50 #define SK_PICT_READER_TAG     SkSetFourByteTag('r', 'e', 'a', 'd')
51 #define SK_PICT_FACTORY_TAG    SkSetFourByteTag('f', 'a', 'c', 't')
52 #define SK_PICT_TYPEFACE_TAG   SkSetFourByteTag('t', 'p', 'f', 'c')
53 #define SK_PICT_PICTURE_TAG    SkSetFourByteTag('p', 'c', 't', 'r')
54 #define SK_PICT_DRAWABLE_TAG   SkSetFourByteTag('d', 'r', 'a', 'w')
55 
56 // This tag specifies the size of the ReadBuffer, needed for the following tags
57 #define SK_PICT_BUFFER_SIZE_TAG     SkSetFourByteTag('a', 'r', 'a', 'y')
58 // these are all inside the ARRAYS tag
59 #define SK_PICT_PAINT_BUFFER_TAG    SkSetFourByteTag('p', 'n', 't', ' ')
60 #define SK_PICT_PATH_BUFFER_TAG     SkSetFourByteTag('p', 't', 'h', ' ')
61 #define SK_PICT_TEXTBLOB_BUFFER_TAG SkSetFourByteTag('b', 'l', 'o', 'b')
62 #define SK_PICT_VERTICES_BUFFER_TAG SkSetFourByteTag('v', 'e', 'r', 't')
63 #define SK_PICT_IMAGE_BUFFER_TAG    SkSetFourByteTag('i', 'm', 'a', 'g')
64 
65 // Always write this guy last (with no length field afterwards)
66 #define SK_PICT_EOF_TAG     SkSetFourByteTag('e', 'o', 'f', ' ')
67 
68 class SkPictureData {
69 public:
70     SkPictureData(const SkPictureRecord& record, const SkPictInfo&);
71     // Does not affect ownership of SkStream.
72     static SkPictureData* CreateFromStream(SkStream*,
73                                            const SkPictInfo&,
74                                            const SkDeserialProcs&,
75                                            SkTypefacePlayback*);
76     static SkPictureData* CreateFromBuffer(SkReadBuffer&, const SkPictInfo&);
77 
78     virtual ~SkPictureData();
79 
80     void serialize(SkWStream*, const SkSerialProcs&, SkRefCntSet*) const;
81     void flatten(SkWriteBuffer&) const;
82 
opData()83     const sk_sp<SkData>& opData() const { return fOpData; }
84 
85 protected:
86     explicit SkPictureData(const SkPictInfo& info);
87 
88     // Does not affect ownership of SkStream.
89     bool parseStream(SkStream*, const SkDeserialProcs&, SkTypefacePlayback*);
90     bool parseBuffer(SkReadBuffer& buffer);
91 
92 public:
getBitmapAsImage(SkReadBuffer * reader)93     const SkImage* getBitmapAsImage(SkReadBuffer* reader) const {
94         const int index = reader->readInt();
95         return reader->validateIndex(index, fBitmapImageCount) ? fBitmapImageRefs[index] : nullptr;
96     }
97 
getImage(SkReadBuffer * reader)98     const SkImage* getImage(SkReadBuffer* reader) const {
99         const int index = reader->readInt();
100         return reader->validateIndex(index, fImageCount) ? fImageRefs[index] : nullptr;
101     }
102 
getPath(SkReadBuffer * reader)103     const SkPath& getPath(SkReadBuffer* reader) const {
104         const int index = reader->readInt() - 1;
105         return reader->validateIndex(index, fPaths.count()) ? fPaths[index] : fEmptyPath;
106     }
107 
getPicture(SkReadBuffer * reader)108     const SkPicture* getPicture(SkReadBuffer* reader) const {
109         const int index = reader->readInt() - 1;
110         return reader->validateIndex(index, fPictureCount) ? fPictureRefs[index] : nullptr;
111     }
112 
getDrawable(SkReadBuffer * reader)113     SkDrawable* getDrawable(SkReadBuffer* reader) const {
114         int index = reader->readInt() - 1;
115         return reader->validateIndex(index, fDrawableCount) ? fDrawableRefs[index] : nullptr;
116     }
117 
getPaint(SkReadBuffer * reader)118     const SkPaint* getPaint(SkReadBuffer* reader) const {
119         const int index = reader->readInt() - 1;
120         if (index == -1) {  // recorder wrote a zero for no paint (likely drawimage)
121             return nullptr;
122         }
123         return reader->validateIndex(index, fPaints.count()) ? &fPaints[index] : nullptr;
124     }
125 
getTextBlob(SkReadBuffer * reader)126     const SkTextBlob* getTextBlob(SkReadBuffer* reader) const {
127         const int index = reader->readInt() - 1;
128         return reader->validateIndex(index, fTextBlobCount) ? fTextBlobRefs[index] : nullptr;
129     }
130 
getVertices(SkReadBuffer * reader)131     const SkVertices* getVertices(SkReadBuffer* reader) const {
132         const int index = reader->readInt() - 1;
133         return reader->validateIndex(index, fVerticesCount) ? fVerticesRefs[index] : nullptr;
134     }
135 
136 private:
137     void init();
138 
139     // these help us with reading/writing
140     // Does not affect ownership of SkStream.
141     bool parseStreamTag(SkStream*, uint32_t tag, uint32_t size,
142                         const SkDeserialProcs&, SkTypefacePlayback*);
143     void parseBufferTag(SkReadBuffer&, uint32_t tag, uint32_t size);
144     void flattenToBuffer(SkWriteBuffer&) const;
145 
146     SkTArray<SkPaint>  fPaints;
147     SkTArray<SkPath>   fPaths;
148 
149     sk_sp<SkData>   fOpData;    // opcodes and parameters
150 
151     const SkPath    fEmptyPath;
152     const SkBitmap  fEmptyBitmap;
153 
154     const SkPicture** fPictureRefs;
155     int fPictureCount;
156     SkDrawable** fDrawableRefs;
157     int fDrawableCount;
158     const SkTextBlob** fTextBlobRefs;
159     int fTextBlobCount;
160     const SkVertices** fVerticesRefs;
161     int fVerticesCount;
162     const SkImage** fImageRefs;
163     int fImageCount;
164     const SkImage** fBitmapImageRefs;
165     int fBitmapImageCount;
166 
167     SkTypefacePlayback fTFPlayback;
168     SkFactoryPlayback* fFactoryPlayback;
169 
170     const SkPictInfo fInfo;
171 
172     static void WriteFactories(SkWStream* stream, const SkFactorySet& rec);
173     static void WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec);
174 
175     void initForPlayback() const;
176 };
177 
178 #endif
179