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 "include/core/SkCanvas.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPicture.h"
13 #include "include/private/SkChecksum.h"
14 #include "src/core/SkPtrRecorder.h"
15 #include "src/core/SkReadBuffer.h"
16 #include "src/core/SkTDynamicHash.h"
17 #include "src/core/SkWriteBuffer.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_RETIRED_2016_REMOVED_2018,
34 DRAW_BITMAP_MATRIX_RETIRED_2016_REMOVED_2018,
35 DRAW_BITMAP_NINE_RETIRED_2016_REMOVED_2018,
36 DRAW_BITMAP_RECT_RETIRED_2016_REMOVED_2018,
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_REMOVED_1_2019,
45 DRAW_POS_TEXT_TOP_BOTTOM_REMOVED_1_2019,
46 DRAW_POS_TEXT_H_REMOVED_1_2019,
47 DRAW_POS_TEXT_H_TOP_BOTTOM_REMOVED_1_2019,
48 DRAW_RECT,
49 DRAW_RRECT,
50 DRAW_SPRITE_RETIRED_2015_REMOVED_2018,
51 DRAW_TEXT_REMOVED_1_2019,
52 DRAW_TEXT_ON_PATH_RETIRED_08_2018_REMOVED_10_2018,
53 DRAW_TEXT_TOP_BOTTOM_REMOVED_1_2019,
54 DRAW_VERTICES_RETIRED_03_2017_REMOVED_01_2018,
55 RESTORE,
56 ROTATE,
57 SAVE,
58 SAVE_LAYER_SAVEFLAGS_DEPRECATED_2015_REMOVED_12_2020,
59 SCALE,
60 SET_MATRIX,
61 SKEW,
62 TRANSLATE,
63 NOOP,
64 BEGIN_COMMENT_GROUP_obsolete,
65 COMMENT_obsolete,
66 END_COMMENT_GROUP_obsolete,
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_obsolete,
78 DRAW_ATLAS,
79 DRAW_IMAGE_NINE,
80 DRAW_IMAGE_RECT,
81
82 SAVE_LAYER_SAVELAYERFLAGS_DEPRECATED_JAN_2016_REMOVED_01_2018,
83 SAVE_LAYER_SAVELAYERREC,
84
85 DRAW_ANNOTATION,
86 DRAW_DRAWABLE,
87 DRAW_DRAWABLE_MATRIX,
88 DRAW_TEXT_RSXFORM_DEPRECATED_DEC_2018,
89
90 TRANSLATE_Z, // deprecated (M60)
91
92 DRAW_SHADOW_REC,
93 DRAW_IMAGE_LATTICE,
94 DRAW_ARC,
95 DRAW_REGION,
96 DRAW_VERTICES_OBJECT,
97
98 FLUSH,
99
100 DRAW_EDGEAA_IMAGE_SET,
101
102 SAVE_BEHIND,
103
104 DRAW_EDGEAA_QUAD,
105
106 DRAW_BEHIND_PAINT,
107 CONCAT44,
108 CLIP_SHADER_IN_PAINT,
109 MARK_CTM, // deprecated
110 SET_M44,
111
112 DRAW_IMAGE2,
113 DRAW_IMAGE_RECT2,
114 DRAW_IMAGE_LATTICE2,
115 DRAW_EDGEAA_IMAGE_SET2,
116
117 RESET_CLIP,
118
119 DRAW_SLUG,
120
121 LAST_DRAWTYPE_ENUM = DRAW_SLUG,
122 };
123
124 enum DrawVertexFlags {
125 DRAW_VERTICES_HAS_TEXS = 0x01,
126 DRAW_VERTICES_HAS_COLORS = 0x02,
127 DRAW_VERTICES_HAS_INDICES = 0x04,
128 DRAW_VERTICES_HAS_XFER = 0x08,
129 };
130
131 enum DrawAtlasFlags {
132 DRAW_ATLAS_HAS_COLORS = 1 << 0,
133 DRAW_ATLAS_HAS_CULL = 1 << 1,
134 DRAW_ATLAS_HAS_SAMPLING = 1 << 2,
135 };
136
137 enum DrawTextRSXformFlags {
138 DRAW_TEXT_RSXFORM_HAS_CULL = 1 << 0,
139 };
140
141 enum SaveLayerRecFlatFlags {
142 SAVELAYERREC_HAS_BOUNDS = 1 << 0,
143 SAVELAYERREC_HAS_PAINT = 1 << 1,
144 SAVELAYERREC_HAS_BACKDROP = 1 << 2,
145 SAVELAYERREC_HAS_FLAGS = 1 << 3,
146 SAVELAYERREC_HAS_CLIPMASK_OBSOLETE = 1 << 4, // 6/13/2020
147 SAVELAYERREC_HAS_CLIPMATRIX_OBSOLETE = 1 << 5, // 6/13/2020
148 SAVELAYERREC_HAS_BACKDROP_SCALE = 1 << 6
149 };
150
151 enum SaveBehindFlatFlags {
152 SAVEBEHIND_HAS_SUBSET = 1 << 0,
153 };
154
155 ///////////////////////////////////////////////////////////////////////////////
156 // clipparams are packed in 5 bits
157 // doAA:1 | clipOp:4
158
159 // Newly serialized pictures will only write kIntersect or kDifference.
ClipParams_pack(SkClipOp op,bool doAA)160 static inline uint32_t ClipParams_pack(SkClipOp op, bool doAA) {
161 unsigned doAABit = doAA ? 1 : 0;
162 return (doAABit << 4) | static_cast<int>(op);
163 }
164
165 // But old SKPs may have been serialized with the SK_SUPPORT_DEPRECATED_CLIPOP flag, so might
166 // encounter expanding clip ops. Thus, this returns the clip op as the more general Region::Op.
ClipParams_unpackRegionOp(SkReadBuffer * buffer,uint32_t packed)167 static inline SkRegion::Op ClipParams_unpackRegionOp(SkReadBuffer* buffer, uint32_t packed) {
168 uint32_t unpacked = packed & 0xF;
169 if (buffer->validate(unpacked <= SkRegion::kIntersect_Op ||
170 (unpacked <= SkRegion::kReplace_Op &&
171 buffer->isVersionLT(SkPicturePriv::kNoExpandingClipOps)))) {
172 return static_cast<SkRegion::Op>(unpacked);
173 }
174 return SkRegion::kIntersect_Op;
175 }
176
ClipParams_unpackDoAA(uint32_t packed)177 static inline bool ClipParams_unpackDoAA(uint32_t packed) {
178 return SkToBool((packed >> 4) & 1);
179 }
180
181 ///////////////////////////////////////////////////////////////////////////////
182
183 class SkTypefacePlayback {
184 public:
SkTypefacePlayback()185 SkTypefacePlayback() : fCount(0), fArray(nullptr) {}
186 ~SkTypefacePlayback() = default;
187
188 void setCount(size_t count);
189
count()190 size_t count() const { return fCount; }
191
192 sk_sp<SkTypeface>& operator[](size_t index) {
193 SkASSERT(index < fCount);
194 return fArray[index];
195 }
196
setupBuffer(SkReadBuffer & buffer)197 void setupBuffer(SkReadBuffer& buffer) const {
198 buffer.setTypefaceArray(fArray.get(), fCount);
199 }
200
201 protected:
202 size_t fCount;
203 std::unique_ptr<sk_sp<SkTypeface>[]> fArray;
204 };
205
206 class SkFactoryPlayback {
207 public:
SkFactoryPlayback(int count)208 SkFactoryPlayback(int count) : fCount(count) { fArray = new SkFlattenable::Factory[count]; }
209
~SkFactoryPlayback()210 ~SkFactoryPlayback() { delete[] fArray; }
211
base()212 SkFlattenable::Factory* base() const { return fArray; }
213
setupBuffer(SkReadBuffer & buffer)214 void setupBuffer(SkReadBuffer& buffer) const {
215 buffer.setFactoryPlayback(fArray, fCount);
216 }
217
218 private:
219 int fCount;
220 SkFlattenable::Factory* fArray;
221 };
222
223 #endif
224