Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkPictureRecord.h
Go to the documentation of this file.
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 SkPictureRecord_DEFINED
9#define SkPictureRecord_DEFINED
10
14#include "include/core/SkData.h"
17#include "include/core/SkM44.h"
19#include "include/core/SkPath.h"
32#include "src/core/SkTHash.h"
33#include "src/core/SkWriter32.h"
34
35#include <cstddef>
36#include <cstdint>
37
38class SkBitmap;
39class SkMatrix;
40class SkPixmap;
41class SkRRect;
42class SkRegion;
43class SkShader;
44class SkSurface;
45class SkSurfaceProps;
46enum class SkBlendMode;
47enum class SkClipOp;
48struct SkDrawShadowRec;
49struct SkIRect;
50struct SkISize;
51struct SkImageInfo;
52struct SkPoint;
53struct SkRSXform;
54struct SkRect;
55
56
57// These macros help with packing and unpacking a single byte value and
58// a 3 byte value into/out of a uint32_t
59#define MASK_24 0x00FFFFFF
60#define UNPACK_8_24(combined, small, large) \
61 small = (combined >> 24) & 0xFF; \
62 large = combined & MASK_24
63#define PACK_8_24(small, large) ((small << 24) | large)
64
65
66class SkPictureRecord : public SkCanvasVirtualEnforcer<SkCanvas> {
67public:
68 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
69
70 SkPictureRecord(const SkIRect& dimensions, uint32_t recordFlags);
71
73 return fPictures;
74 }
75
77 return fDrawables;
78 }
79
81 return fTextBlobs;
82 }
83
85 return fSlugs;
86 }
87
89 return fVertices;
90 }
91
93 return fImages;
94 }
95
97 this->validate(fWriter.bytesWritten(), 0);
98
99 if (fWriter.bytesWritten() == 0) {
100 return SkData::MakeEmpty();
101 }
102 return fWriter.snapshotAsData();
103 }
104
105 void setFlags(uint32_t recordFlags) {
106 fRecordFlags = recordFlags;
107 }
108
109 const SkWriter32& writeStream() const {
110 return fWriter;
111 }
112
113 void beginRecording();
114 void endRecording();
115
116protected:
117 void addNoOp();
118
119private:
120 void handleOptimization(int opt);
121 size_t recordRestoreOffsetPlaceholder();
122 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
123
124 SkTDArray<int32_t> fRestoreOffsetStack;
125
126 SkTDArray<uint32_t> fCullOffsetStack;
127
128 /*
129 * Write the 'drawType' operation and chunk size to the skp. 'size'
130 * can potentially be increased if the chunk size needs its own storage
131 * location (i.e., it overflows 24 bits).
132 * Returns the start offset of the chunk. This is the location at which
133 * the opcode & size are stored.
134 * TODO: since we are handing the size into here we could call reserve
135 * and then return a pointer to the memory storage. This could decrease
136 * allocation overhead but could lead to more wasted space (the tail
137 * end of blocks could go unused). Possibly add a second addDraw that
138 * operates in this manner.
139 */
140 size_t addDraw(DrawType drawType, size_t* size) {
141 size_t offset = fWriter.bytesWritten();
142
143 SkASSERT_RELEASE(this->predrawNotify());
144
145 SkASSERT(0 != *size);
146 SkASSERT(((uint8_t) drawType) == drawType);
147
148 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
149 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
150 *size += 1;
151 fWriter.writeInt(SkToU32(*size));
152 } else {
153 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
154 }
155
156 return offset;
157 }
158
159 void addInt(int value) {
160 fWriter.writeInt(value);
161 }
162 void addScalar(SkScalar scalar) {
163 fWriter.writeScalar(scalar);
164 }
165
166 void addImage(const SkImage*);
167 void addMatrix(const SkMatrix& matrix);
168 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
169 void addPaintPtr(const SkPaint* paint);
170 void addPatch(const SkPoint cubics[12]);
171 void addPath(const SkPath& path);
172 void addPicture(const SkPicture* picture);
173 void addDrawable(SkDrawable* picture);
174 void addPoint(const SkPoint& point);
175 void addPoints(const SkPoint pts[], int count);
176 void addRect(const SkRect& rect);
177 void addRectPtr(const SkRect* rect);
178 void addIRect(const SkIRect& rect);
179 void addIRectPtr(const SkIRect* rect);
180 void addRRect(const SkRRect&);
181 void addRegion(const SkRegion& region);
182 void addSampling(const SkSamplingOptions&);
183 void addText(const void* text, size_t byteLength);
184 void addTextBlob(const SkTextBlob* blob);
185 void addSlug(const sktext::gpu::Slug* slug);
186 void addVertices(const SkVertices*);
187
188 int find(const SkBitmap& bitmap);
189
190protected:
191 void validate(size_t initialOffset, size_t size) const {
192 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
193 }
194
196 bool onPeekPixels(SkPixmap*) override { return false; }
197
198 void willSave() override;
199 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
200 bool onDoSaveBehind(const SkRect*) override;
201 void willRestore() override;
202
203 void didConcat44(const SkM44&) override;
204 void didSetM44(const SkM44&) override;
205 void didScale(SkScalar, SkScalar) override;
206 void didTranslate(SkScalar, SkScalar) override;
207
208 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
209
210 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
211 const SkPaint& paint) override;
212 void onDrawSlug(const sktext::gpu::Slug* slug, const SkPaint& paint) override;
213 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
214 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
215
216 void onDrawPaint(const SkPaint&) override;
217 void onDrawBehind(const SkPaint&) override;
218 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
219 void onDrawRect(const SkRect&, const SkPaint&) override;
220 void onDrawRegion(const SkRegion&, const SkPaint&) override;
221 void onDrawOval(const SkRect&, const SkPaint&) override;
222 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
223 void onDrawRRect(const SkRRect&, const SkPaint&) override;
224 void onDrawPath(const SkPath&, const SkPaint&) override;
225
227 const SkPaint*) override;
228 void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&,
229 const SkPaint*, SrcRectConstraint) override;
230 void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode,
231 const SkPaint*) override;
232 void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
233 SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*) override;
234
235 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
236 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
237
238 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
239 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
240 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
241 void onClipShader(sk_sp<SkShader>, SkClipOp) override;
242 void onClipRegion(const SkRegion&, SkClipOp) override;
243 void onResetClip() override;
244
245 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
246
247 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
248 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
249
250 void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&,
251 SkBlendMode) override;
252 void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[],
253 const SkSamplingOptions&,const SkPaint*, SrcRectConstraint) override;
254
255 int addPathToHeap(const SkPath& path); // does not write to ops stream
256
257 // These entry points allow the writing of matrices, clips, saves &
258 // restores to be deferred (e.g., if the MC state is being collapsed and
259 // only written out as needed).
260 void recordConcat(const SkMatrix& matrix);
261 void recordTranslate(const SkMatrix& matrix);
262 void recordScale(const SkMatrix& matrix);
263 size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA);
264 size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA);
265 size_t recordClipPath(int pathID, SkClipOp op, bool doAA);
266 size_t recordClipRegion(const SkRegion& region, SkClipOp op);
267 void recordSave();
268 void recordSaveLayer(const SaveLayerRec&);
269 void recordRestore(bool fillInSkips = true);
270
271private:
273
274 struct PathHash {
275 uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
276 };
278
279 SkWriter32 fWriter;
280
287
288 uint32_t fRecordFlags;
289 int fInitialSaveCount;
290
291 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
292};
293
294#endif
int count
#define SkASSERT_RELEASE(cond)
Definition SkAssert.h:100
#define SkASSERT(cond)
Definition SkAssert.h:116
SkBlendMode
Definition SkBlendMode.h:38
SkClipOp
Definition SkClipOp.h:13
uint32_t SkColor
Definition SkColor.h:37
#define PACK_8_24(small, large)
#define MASK_24
SkFilterMode
constexpr uint32_t SkToU32(S x)
Definition SkTo.h:26
SrcRectConstraint
Definition SkCanvas.h:1541
SaveLayerStrategy
Definition SkCanvas.h:2263
static sk_sp< SkData > MakeEmpty()
Definition SkData.cpp:94
Definition SkM44.h:150
void onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint) override
void onDrawImage2(const SkImage *, SkScalar, SkScalar, const SkSamplingOptions &, const SkPaint *) override
void willSave() override
const skia_private::TArray< sk_sp< const SkPicture > > & getPictures() const
void onDrawSlug(const sktext::gpu::Slug *slug, const SkPaint &paint) override
void onDrawDrawable(SkDrawable *, const SkMatrix *) override
void onDrawRect(const SkRect &, const SkPaint &) override
void onDrawArc(const SkRect &, SkScalar, SkScalar, bool, const SkPaint &) override
void onDrawBehind(const SkPaint &) override
void onClipRect(const SkRect &, SkClipOp, ClipEdgeStyle) override
void validate(size_t initialOffset, size_t size) const
void onClipRRect(const SkRRect &, SkClipOp, ClipEdgeStyle) override
void recordSaveLayer(const SaveLayerRec &)
void recordScale(const SkMatrix &matrix)
size_t recordClipRRect(const SkRRect &rrect, SkClipOp op, bool doAA)
int addPathToHeap(const SkPath &path)
const skia_private::TArray< sk_sp< const SkTextBlob > > & getTextBlobs() const
void onDrawPaint(const SkPaint &) override
void onDrawPicture(const SkPicture *, const SkMatrix *, const SkPaint *) override
const skia_private::TArray< sk_sp< const SkImage > > & getImages() const
void recordRestore(bool fillInSkips=true)
void willRestore() override
const skia_private::TArray< sk_sp< const SkVertices > > & getVertices() const
void onDrawOval(const SkRect &, const SkPaint &) override
void onDrawAnnotation(const SkRect &, const char[], SkData *) override
void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint &) override
void onDrawEdgeAAQuad(const SkRect &, const SkPoint[4], QuadAAFlags, const SkColor4f &, SkBlendMode) override
sk_sp< SkData > opData() const
SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override
void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode, const SkPaint &paint) override
void didScale(SkScalar, SkScalar) override
void onDrawAtlas2(const SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, const SkSamplingOptions &, const SkRect *, const SkPaint *) override
bool onDoSaveBehind(const SkRect *) override
void onDrawVerticesObject(const SkVertices *, SkBlendMode, const SkPaint &) override
void onDrawRegion(const SkRegion &, const SkPaint &) override
bool onPeekPixels(SkPixmap *) override
void recordTranslate(const SkMatrix &matrix)
const SkWriter32 & writeStream() const
const skia_private::TArray< sk_sp< SkDrawable > > & getDrawables() const
void onClipRegion(const SkRegion &, SkClipOp) override
void recordConcat(const SkMatrix &matrix)
void onDrawShadowRec(const SkPath &, const SkDrawShadowRec &) override
size_t recordClipPath(int pathID, SkClipOp op, bool doAA)
void onDrawPath(const SkPath &, const SkPaint &) override
const skia_private::TArray< sk_sp< const sktext::gpu::Slug > > & getSlugs() const
void didConcat44(const SkM44 &) override
size_t recordClipRect(const SkRect &rect, SkClipOp op, bool doAA)
void onDrawRRect(const SkRRect &, const SkPaint &) override
void onResetClip() override
void onClipPath(const SkPath &, SkClipOp, ClipEdgeStyle) override
size_t recordClipRegion(const SkRegion &region, SkClipOp op)
void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[], const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onDrawDRRect(const SkRRect &, const SkRRect &, const SkPaint &) override
void onDrawImageRect2(const SkImage *, const SkRect &, const SkRect &, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onClipShader(sk_sp< SkShader >, SkClipOp) override
void didTranslate(SkScalar, SkScalar) override
sk_sp< SkSurface > onNewSurface(const SkImageInfo &, const SkSurfaceProps &) override
void didSetM44(const SkM44 &) override
void onDrawImageLattice2(const SkImage *, const Lattice &, const SkRect &, SkFilterMode, const SkPaint *) override
void setFlags(uint32_t recordFlags)
void writeInt(int32_t value)
Definition SkWriter32.h:105
void writeScalar(SkScalar value)
Definition SkWriter32.h:121
sk_sp< SkData > snapshotAsData() const
size_t bytesWritten() const
Definition SkWriter32.h:48
const Paint & paint
float SkScalar
Definition extension.cpp:12
std::u16string text
double y
double x
Point offset