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 "SkPicture.h"
13 #include "SkPictureContentInfo.h"
14 #include "SkPictureFlat.h"
15 
16 class SkData;
17 class SkPictureRecord;
18 class SkPixelSerializer;
19 class SkReader32;
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 {
30     enum Flags {
31         kCrossProcess_Flag      = 1 << 0,
32         kScalarIsFloat_Flag     = 1 << 1,
33         kPtrIs64Bit_Flag        = 1 << 2,
34     };
35 
36     char        fMagic[8];
37     uint32_t    fVersion;
38     SkRect      fCullRect;
39     uint32_t    fFlags;
40 };
41 
42 #define SK_PICT_READER_TAG     SkSetFourByteTag('r', 'e', 'a', 'd')
43 #define SK_PICT_FACTORY_TAG    SkSetFourByteTag('f', 'a', 'c', 't')
44 #define SK_PICT_TYPEFACE_TAG   SkSetFourByteTag('t', 'p', 'f', 'c')
45 #define SK_PICT_PICTURE_TAG    SkSetFourByteTag('p', 'c', 't', 'r')
46 
47 // This tag specifies the size of the ReadBuffer, needed for the following tags
48 #define SK_PICT_BUFFER_SIZE_TAG     SkSetFourByteTag('a', 'r', 'a', 'y')
49 // these are all inside the ARRAYS tag
50 #define SK_PICT_BITMAP_BUFFER_TAG   SkSetFourByteTag('b', 't', 'm', 'p')
51 #define SK_PICT_PAINT_BUFFER_TAG    SkSetFourByteTag('p', 'n', 't', ' ')
52 #define SK_PICT_PATH_BUFFER_TAG     SkSetFourByteTag('p', 't', 'h', ' ')
53 #define SK_PICT_TEXTBLOB_BUFFER_TAG SkSetFourByteTag('b', 'l', 'o', 'b')
54 #define SK_PICT_IMAGE_BUFFER_TAG    SkSetFourByteTag('i', 'm', 'a', 'g')
55 
56 // Always write this guy last (with no length field afterwards)
57 #define SK_PICT_EOF_TAG     SkSetFourByteTag('e', 'o', 'f', ' ')
58 
59 class SkPictureData {
60 public:
61     SkPictureData(const SkPictureRecord& record, const SkPictInfo&, bool deepCopyOps);
62     // Does not affect ownership of SkStream.
63     static SkPictureData* CreateFromStream(SkStream*,
64                                            const SkPictInfo&,
65                                            SkPicture::InstallPixelRefProc,
66                                            SkTypefacePlayback*);
67     static SkPictureData* CreateFromBuffer(SkReadBuffer&, const SkPictInfo&);
68 
69     virtual ~SkPictureData();
70 
71     void serialize(SkWStream*, SkPixelSerializer*, SkRefCntSet*) const;
72     void flatten(SkWriteBuffer&) const;
73 
74     bool containsBitmaps() const;
75 
hasText()76     bool hasText() const { return fContentInfo.hasText(); }
77 
opCount()78     int opCount() const { return fContentInfo.numOperations(); }
79 
opData()80     const SkData* opData() const { return fOpData; }
81 
82 protected:
83     explicit SkPictureData(const SkPictInfo& info);
84 
85     // Does not affect ownership of SkStream.
86     bool parseStream(SkStream*, SkPicture::InstallPixelRefProc, SkTypefacePlayback*);
87     bool parseBuffer(SkReadBuffer& buffer);
88 
89 public:
getBitmap(SkReader32 * reader)90     const SkBitmap& getBitmap(SkReader32* reader) const {
91         const int index = reader->readInt();
92         return fBitmaps[index];
93     }
94 
getImage(SkReader32 * reader)95     const SkImage* getImage(SkReader32* reader) const {
96         const int index = reader->readInt();
97         return fImageRefs[index];
98     }
99 
getPath(SkReader32 * reader)100     const SkPath& getPath(SkReader32* reader) const {
101         int index = reader->readInt() - 1;
102         return fPaths[index];
103     }
104 
getPicture(SkReader32 * reader)105     const SkPicture* getPicture(SkReader32* reader) const {
106         int index = reader->readInt();
107         SkASSERT(index > 0 && index <= fPictureCount);
108         return fPictureRefs[index - 1];
109     }
110 
getPaint(SkReader32 * reader)111     const SkPaint* getPaint(SkReader32* reader) const {
112         int index = reader->readInt();
113         if (index == 0) {
114             return nullptr;
115         }
116         return &fPaints[index - 1];
117     }
118 
getTextBlob(SkReader32 * reader)119     const SkTextBlob* getTextBlob(SkReader32* reader) const {
120         int index = reader->readInt();
121         SkASSERT(index > 0 && index <= fTextBlobCount);
122         return fTextBlobRefs[index - 1];
123     }
124 
125 #if SK_SUPPORT_GPU
126     /**
127      * sampleCount is the number of samples-per-pixel or zero if non-MSAA.
128      * It is defaulted to be zero.
129      */
130     bool suitableForGpuRasterization(GrContext* context, const char **reason,
131                                      int sampleCount = 0) const;
132 
133     /**
134      * Calls getRecommendedSampleCount with GrPixelConfig and dpi to calculate sampleCount
135      * and then calls the above version of suitableForGpuRasterization
136      */
137     bool suitableForGpuRasterization(GrContext* context, const char **reason,
138                                      GrPixelConfig config, SkScalar dpi) const;
139 
140     bool suitableForLayerOptimization() const;
141 #endif
142 
143 private:
144     void init();
145 
146     // these help us with reading/writing
147     // Does not affect ownership of SkStream.
148     bool parseStreamTag(SkStream*, uint32_t tag, uint32_t size,
149                         SkPicture::InstallPixelRefProc, SkTypefacePlayback*);
150     bool parseBufferTag(SkReadBuffer&, uint32_t tag, uint32_t size);
151     void flattenToBuffer(SkWriteBuffer&) const;
152 
153     // Only used by getBitmap() if the passed in index is SkBitmapHeap::INVALID_SLOT. This empty
154     // bitmap allows playback to draw nothing and move on.
155     SkBitmap fBadBitmap;
156 
157     SkTArray<SkBitmap> fBitmaps;
158     SkTArray<SkPaint>  fPaints;
159     SkTArray<SkPath>   fPaths;
160 
161     SkData* fOpData;    // opcodes and parameters
162 
163     const SkPicture** fPictureRefs;
164     int fPictureCount;
165     const SkTextBlob** fTextBlobRefs;
166     int fTextBlobCount;
167     const SkImage** fImageRefs;
168     int fImageCount;
169 
170     SkPictureContentInfo fContentInfo;
171 
172     SkTypefacePlayback fTFPlayback;
173     SkFactoryPlayback* fFactoryPlayback;
174 
175     const SkPictInfo fInfo;
176 
177     static void WriteFactories(SkWStream* stream, const SkFactorySet& rec);
178     static void WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec);
179 
180     void initForPlayback() const;
181 };
182 
183 #endif
184