xref: /aosp_15_r20/external/skia/src/core/SkPictureFlat.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkFlattenable.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkRegion.h"
13 #include "include/core/SkTypeface.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkTo.h"
16 #include "src/core/SkPicturePriv.h"
17 #include "src/core/SkReadBuffer.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 
23 enum class SkClipOp;
24 
25 /*
26  * Note: While adding new DrawTypes, it is necessary to add to the end of this list
27  *       and update LAST_DRAWTYPE_ENUM to avoid having the code read older skps wrong.
28  *       (which can cause segfaults)
29  *
30  *       Reordering can be done during version updates.
31  */
32 enum DrawType {
33     UNUSED,
34     CLIP_PATH,
35     CLIP_REGION,
36     CLIP_RECT,
37     CLIP_RRECT,
38     CONCAT,
39     DRAW_BITMAP_RETIRED_2016_REMOVED_2018,
40     DRAW_BITMAP_MATRIX_RETIRED_2016_REMOVED_2018,
41     DRAW_BITMAP_NINE_RETIRED_2016_REMOVED_2018,
42     DRAW_BITMAP_RECT_RETIRED_2016_REMOVED_2018,
43     DRAW_CLEAR,
44     DRAW_DATA,
45     DRAW_OVAL,
46     DRAW_PAINT,
47     DRAW_PATH,
48     DRAW_PICTURE,
49     DRAW_POINTS,
50     DRAW_POS_TEXT_REMOVED_1_2019,
51     DRAW_POS_TEXT_TOP_BOTTOM_REMOVED_1_2019,
52     DRAW_POS_TEXT_H_REMOVED_1_2019,
53     DRAW_POS_TEXT_H_TOP_BOTTOM_REMOVED_1_2019,
54     DRAW_RECT,
55     DRAW_RRECT,
56     DRAW_SPRITE_RETIRED_2015_REMOVED_2018,
57     DRAW_TEXT_REMOVED_1_2019,
58     DRAW_TEXT_ON_PATH_RETIRED_08_2018_REMOVED_10_2018,
59     DRAW_TEXT_TOP_BOTTOM_REMOVED_1_2019,
60     DRAW_VERTICES_RETIRED_03_2017_REMOVED_01_2018,
61     RESTORE,
62     ROTATE,
63     SAVE,
64     SAVE_LAYER_SAVEFLAGS_DEPRECATED_2015_REMOVED_12_2020,
65     SCALE,
66     SET_MATRIX,
67     SKEW,
68     TRANSLATE,
69     NOOP,
70     BEGIN_COMMENT_GROUP_obsolete,
71     COMMENT_obsolete,
72     END_COMMENT_GROUP_obsolete,
73 
74     // new ops -- feel free to re-alphabetize on next version bump
75     DRAW_DRRECT,
76     PUSH_CULL,  // deprecated, M41 was last Chromium version to write this to an .skp
77     POP_CULL,   // deprecated, M41 was last Chromium version to write this to an .skp
78 
79     DRAW_PATCH, // could not add in aphabetical order
80     DRAW_PICTURE_MATRIX_PAINT,
81     DRAW_TEXT_BLOB,
82     DRAW_IMAGE,
83     DRAW_IMAGE_RECT_STRICT_obsolete,
84     DRAW_ATLAS,
85     DRAW_IMAGE_NINE,
86     DRAW_IMAGE_RECT,
87 
88     SAVE_LAYER_SAVELAYERFLAGS_DEPRECATED_JAN_2016_REMOVED_01_2018,
89     SAVE_LAYER_SAVELAYERREC,
90 
91     DRAW_ANNOTATION,
92     DRAW_DRAWABLE,
93     DRAW_DRAWABLE_MATRIX,
94     DRAW_TEXT_RSXFORM_DEPRECATED_DEC_2018,
95 
96     TRANSLATE_Z, // deprecated (M60)
97 
98     DRAW_SHADOW_REC,
99     DRAW_IMAGE_LATTICE,
100     DRAW_ARC,
101     DRAW_REGION,
102     DRAW_VERTICES_OBJECT,
103 
104     FLUSH,  // no-op
105 
106     DRAW_EDGEAA_IMAGE_SET,
107 
108     SAVE_BEHIND,
109 
110     DRAW_EDGEAA_QUAD,
111 
112     DRAW_BEHIND_PAINT,
113     CONCAT44,
114     CLIP_SHADER_IN_PAINT,
115     MARK_CTM, // deprecated
116     SET_M44,
117 
118     DRAW_IMAGE2,
119     DRAW_IMAGE_RECT2,
120     DRAW_IMAGE_LATTICE2,
121     DRAW_EDGEAA_IMAGE_SET2,
122 
123     RESET_CLIP,
124 
125     DRAW_SLUG,
126 
127     LAST_DRAWTYPE_ENUM = DRAW_SLUG,
128 };
129 
130 enum DrawVertexFlags {
131     DRAW_VERTICES_HAS_TEXS    = 0x01,
132     DRAW_VERTICES_HAS_COLORS  = 0x02,
133     DRAW_VERTICES_HAS_INDICES = 0x04,
134     DRAW_VERTICES_HAS_XFER    = 0x08,
135 };
136 
137 enum DrawAtlasFlags {
138     DRAW_ATLAS_HAS_COLORS     = 1 << 0,
139     DRAW_ATLAS_HAS_CULL       = 1 << 1,
140     DRAW_ATLAS_HAS_SAMPLING   = 1 << 2,
141 };
142 
143 enum DrawTextRSXformFlags {
144     DRAW_TEXT_RSXFORM_HAS_CULL  = 1 << 0,
145 };
146 
147 enum SaveLayerRecFlatFlags {
148     SAVELAYERREC_HAS_BOUNDS              = 1 << 0,
149     SAVELAYERREC_HAS_PAINT               = 1 << 1,
150     SAVELAYERREC_HAS_BACKDROP            = 1 << 2,
151     SAVELAYERREC_HAS_FLAGS               = 1 << 3,
152     SAVELAYERREC_HAS_CLIPMASK_OBSOLETE   = 1 << 4,  // 6/13/2020
153     SAVELAYERREC_HAS_CLIPMATRIX_OBSOLETE = 1 << 5,  // 6/13/2020
154     SAVELAYERREC_HAS_BACKDROP_SCALE      = 1 << 6,
155     SAVELAYERREC_HAS_MULTIPLE_FILTERS    = 1 << 7,
156     SAVELAYERREC_HAS_BACKDROP_TILEMODE   = 1 << 8,
157 };
158 
159 enum SaveBehindFlatFlags {
160     SAVEBEHIND_HAS_SUBSET = 1 << 0,
161 };
162 
163 ///////////////////////////////////////////////////////////////////////////////
164 // clipparams are packed in 5 bits
165 //  doAA:1 | clipOp:4
166 
167 // Newly serialized pictures will only write kIntersect or kDifference.
ClipParams_pack(SkClipOp op,bool doAA)168 static inline uint32_t ClipParams_pack(SkClipOp op, bool doAA) {
169     unsigned doAABit = doAA ? 1 : 0;
170     return (doAABit << 4) | static_cast<int>(op);
171 }
172 
173 // But old SKPs may have been serialized with the SK_SUPPORT_DEPRECATED_CLIPOP flag, so might
174 // encounter expanding clip ops. Thus, this returns the clip op as the more general Region::Op.
ClipParams_unpackRegionOp(SkReadBuffer * buffer,uint32_t packed)175 static inline SkRegion::Op ClipParams_unpackRegionOp(SkReadBuffer* buffer, uint32_t packed) {
176     uint32_t unpacked = packed & 0xF;
177     if (buffer->validate(unpacked <= SkRegion::kIntersect_Op ||
178                          (unpacked <= SkRegion::kReplace_Op &&
179                                 buffer->isVersionLT(SkPicturePriv::kNoExpandingClipOps)))) {
180         return static_cast<SkRegion::Op>(unpacked);
181     }
182     return SkRegion::kIntersect_Op;
183 }
184 
ClipParams_unpackDoAA(uint32_t packed)185 static inline bool ClipParams_unpackDoAA(uint32_t packed) {
186     return SkToBool((packed >> 4) & 1);
187 }
188 
189 ///////////////////////////////////////////////////////////////////////////////
190 
191 class SkTypefacePlayback {
192 public:
SkTypefacePlayback()193     SkTypefacePlayback() : fCount(0), fArray(nullptr) {}
194     ~SkTypefacePlayback() = default;
195 
196     void setCount(size_t count);
197 
count()198     size_t count() const { return fCount; }
199 
200     sk_sp<SkTypeface>& operator[](size_t index) {
201         SkASSERT(index < fCount);
202         return fArray[index];
203     }
204 
setupBuffer(SkReadBuffer & buffer)205     void setupBuffer(SkReadBuffer& buffer) const {
206         buffer.setTypefaceArray(fArray.get(), fCount);
207     }
208 
209 protected:
210     size_t fCount;
211     std::unique_ptr<sk_sp<SkTypeface>[]> fArray;
212 };
213 
214 class SkFactoryPlayback {
215 public:
SkFactoryPlayback(int count)216     SkFactoryPlayback(int count) : fCount(count) { fArray = new SkFlattenable::Factory[count]; }
217 
~SkFactoryPlayback()218     ~SkFactoryPlayback() { delete[] fArray; }
219 
base()220     SkFlattenable::Factory* base() const { return fArray; }
221 
setupBuffer(SkReadBuffer & buffer)222     void setupBuffer(SkReadBuffer& buffer) const {
223         buffer.setFactoryPlayback(fArray, fCount);
224     }
225 
226 private:
227     int fCount;
228     SkFlattenable::Factory* fArray;
229 };
230 
231 #endif
232