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 #ifndef SkPictureFlat_DEFINED
8 #define SkPictureFlat_DEFINED
9 
10 #include "SkCanvas.h"
11 #include "SkChecksum.h"
12 #include "SkReadBuffer.h"
13 #include "SkWriteBuffer.h"
14 #include "SkPaint.h"
15 #include "SkPicture.h"
16 #include "SkPtrRecorder.h"
17 #include "SkTDynamicHash.h"
18 
19 /*
20  * Note: While adding new DrawTypes, it is necessary to add to the end of this list
21  *       and update LAST_DRAWTYPE_ENUM to avoid having the code read older skps wrong.
22  *       (which can cause segfaults)
23  *
24  *       Reordering can be done during version updates.
25  */
26 enum DrawType {
27     UNUSED,
28     CLIP_PATH,
29     CLIP_REGION,
30     CLIP_RECT,
31     CLIP_RRECT,
32     CONCAT,
33     DRAW_BITMAP,
34     DRAW_BITMAP_MATRIX, // deprecated, M41 was last Chromium version to write this to an .skp
35     DRAW_BITMAP_NINE,
36     DRAW_BITMAP_RECT,
37     DRAW_CLEAR,
38     DRAW_DATA,
39     DRAW_OVAL,
40     DRAW_PAINT,
41     DRAW_PATH,
42     DRAW_PICTURE,
43     DRAW_POINTS,
44     DRAW_POS_TEXT,
45     DRAW_POS_TEXT_TOP_BOTTOM, // fast variant of DRAW_POS_TEXT
46     DRAW_POS_TEXT_H,
47     DRAW_POS_TEXT_H_TOP_BOTTOM, // fast variant of DRAW_POS_TEXT_H
48     DRAW_RECT,
49     DRAW_RRECT,
50     DRAW_SPRITE,
51     DRAW_TEXT,
52     DRAW_TEXT_ON_PATH,
53     DRAW_TEXT_TOP_BOTTOM,   // fast variant of DRAW_TEXT
54     DRAW_VERTICES_RETIRED_03_2017,
55     RESTORE,
56     ROTATE,
57     SAVE,
58     SAVE_LAYER_SAVEFLAGS_DEPRECATED,
59     SCALE,
60     SET_MATRIX,
61     SKEW,
62     TRANSLATE,
63     NOOP,
64     BEGIN_COMMENT_GROUP, // deprecated (M44)
65     COMMENT,             // deprecated (M44)
66     END_COMMENT_GROUP,   // deprecated (M44)
67 
68     // new ops -- feel free to re-alphabetize on next version bump
69     DRAW_DRRECT,
70     PUSH_CULL,  // deprecated, M41 was last Chromium version to write this to an .skp
71     POP_CULL,   // deprecated, M41 was last Chromium version to write this to an .skp
72 
73     DRAW_PATCH, // could not add in aphabetical order
74     DRAW_PICTURE_MATRIX_PAINT,
75     DRAW_TEXT_BLOB,
76     DRAW_IMAGE,
77     DRAW_IMAGE_RECT_STRICT, // deprecated (M45)
78     DRAW_ATLAS,
79     DRAW_IMAGE_NINE,
80     DRAW_IMAGE_RECT,
81 
82     SAVE_LAYER_SAVELAYERFLAGS_DEPRECATED_JAN_2016,
83     SAVE_LAYER_SAVELAYERREC,
84 
85     DRAW_ANNOTATION,
86     DRAW_DRAWABLE,
87     DRAW_DRAWABLE_MATRIX,
88     DRAW_TEXT_RSXFORM,
89 
90     TRANSLATE_Z,
91 
92     DRAW_SHADOWED_PICTURE_LIGHTS,
93     DRAW_IMAGE_LATTICE,
94     DRAW_ARC,
95     DRAW_REGION,
96     DRAW_VERTICES_OBJECT,
97 
98     LAST_DRAWTYPE_ENUM = DRAW_VERTICES_OBJECT
99 };
100 
101 // In the 'match' method, this constant will match any flavor of DRAW_BITMAP*
102 static const int kDRAW_BITMAP_FLAVOR = LAST_DRAWTYPE_ENUM+1;
103 
104 enum DrawVertexFlags {
105     DRAW_VERTICES_HAS_TEXS    = 0x01,
106     DRAW_VERTICES_HAS_COLORS  = 0x02,
107     DRAW_VERTICES_HAS_INDICES = 0x04,
108     DRAW_VERTICES_HAS_XFER    = 0x08,
109 };
110 
111 enum DrawAtlasFlags {
112     DRAW_ATLAS_HAS_COLORS   = 1 << 0,
113     DRAW_ATLAS_HAS_CULL     = 1 << 1,
114 };
115 
116 enum DrawTextRSXformFlags {
117     DRAW_TEXT_RSXFORM_HAS_CULL  = 1 << 0,
118 };
119 
120 enum SaveLayerRecFlatFlags {
121     SAVELAYERREC_HAS_BOUNDS     = 1 << 0,
122     SAVELAYERREC_HAS_PAINT      = 1 << 1,
123     SAVELAYERREC_HAS_BACKDROP   = 1 << 2,
124     SAVELAYERREC_HAS_FLAGS      = 1 << 3,
125 };
126 
127 ///////////////////////////////////////////////////////////////////////////////
128 // clipparams are packed in 5 bits
129 //  doAA:1 | clipOp:4
130 
ClipParams_pack(SkClipOp op,bool doAA)131 static inline uint32_t ClipParams_pack(SkClipOp op, bool doAA) {
132     unsigned doAABit = doAA ? 1 : 0;
133     return (doAABit << 4) | static_cast<int>(op);
134 }
135 
asValidEnum(SkReadBuffer * buffer,uint32_t candidate)136 template <typename T> T asValidEnum(SkReadBuffer* buffer, uint32_t candidate) {
137 
138     if (buffer->validate(candidate <= static_cast<uint32_t>(T::kMax_EnumValue))) {
139         return static_cast<T>(candidate);
140     }
141 
142     return T::kMax_EnumValue;
143 }
144 
ClipParams_unpackRegionOp(SkReadBuffer * buffer,uint32_t packed)145 static inline SkClipOp ClipParams_unpackRegionOp(SkReadBuffer* buffer, uint32_t packed) {
146     return asValidEnum<SkClipOp>(buffer, packed & 0xF);
147 }
148 
ClipParams_unpackDoAA(uint32_t packed)149 static inline bool ClipParams_unpackDoAA(uint32_t packed) {
150     return SkToBool((packed >> 4) & 1);
151 }
152 
153 ///////////////////////////////////////////////////////////////////////////////
154 
155 class SkTypefacePlayback {
156 public:
157     SkTypefacePlayback();
158     virtual ~SkTypefacePlayback();
159 
count()160     int count() const { return fCount; }
161 
162     void reset(const SkRefCntSet*);
163 
164     void setCount(int count);
165     SkRefCnt* set(int index, SkRefCnt*);
166 
setupBuffer(SkReadBuffer & buffer)167     void setupBuffer(SkReadBuffer& buffer) const {
168         buffer.setTypefaceArray((SkTypeface**)fArray, fCount);
169     }
170 
171 protected:
172     int fCount;
173     SkRefCnt** fArray;
174 };
175 
176 class SkFactoryPlayback {
177 public:
SkFactoryPlayback(int count)178     SkFactoryPlayback(int count) : fCount(count) { fArray = new SkFlattenable::Factory[count]; }
179 
~SkFactoryPlayback()180     ~SkFactoryPlayback() { delete[] fArray; }
181 
base()182     SkFlattenable::Factory* base() const { return fArray; }
183 
setupBuffer(SkReadBuffer & buffer)184     void setupBuffer(SkReadBuffer& buffer) const {
185         buffer.setFactoryPlayback(fArray, fCount);
186     }
187 
188 private:
189     int fCount;
190     SkFlattenable::Factory* fArray;
191 };
192 
193 #endif
194