Flutter Engine
The Flutter Engine
SkRecords.h
Go to the documentation of this file.
1/*
2 * Copyright 2014 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 SkRecords_DEFINED
9#define SkRecords_DEFINED
10
14#include "include/core/SkData.h"
17#include "include/core/SkM44.h"
19#include "include/core/SkMesh.h"
21#include "include/core/SkPath.h"
24#include "include/core/SkRect.h"
36
37#include <cstdint>
38
39enum class SkBlendMode;
40enum class SkClipOp;
41struct SkPoint;
42struct SkRSXform;
43
44namespace SkRecords {
45
46// A list of all the types of canvas calls we can record.
47// Each of these is reified into a struct below.
48//
49// (We're using the macro-of-macro trick here to do several different things with the same list.)
50//
51// We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords
52// types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.)
53//
54// Order doesn't technically matter here, but the compiler can generally generate better code if
55// you keep them semantically grouped, especially the Draws. It's also nice to leave NoOp at 0.
56#define SK_RECORD_TYPES(M) \
57 M(NoOp) \
58 M(Restore) \
59 M(Save) \
60 M(SaveLayer) \
61 M(SaveBehind) \
62 M(SetMatrix) \
63 M(SetM44) \
64 M(Translate) \
65 M(Scale) \
66 M(Concat) \
67 M(Concat44) \
68 M(ClipPath) \
69 M(ClipRRect) \
70 M(ClipRect) \
71 M(ClipRegion) \
72 M(ClipShader) \
73 M(ResetClip) \
74 M(DrawArc) \
75 M(DrawDrawable) \
76 M(DrawImage) \
77 M(DrawImageLattice) \
78 M(DrawImageRect) \
79 M(DrawDRRect) \
80 M(DrawOval) \
81 M(DrawBehind) \
82 M(DrawPaint) \
83 M(DrawPath) \
84 M(DrawPatch) \
85 M(DrawPicture) \
86 M(DrawPoints) \
87 M(DrawRRect) \
88 M(DrawRect) \
89 M(DrawRegion) \
90 M(DrawTextBlob) \
91 M(DrawSlug) \
92 M(DrawAtlas) \
93 M(DrawVertices) \
94 M(DrawMesh) \
95 M(DrawShadowRec) \
96 M(DrawAnnotation) \
97 M(DrawEdgeAAQuad) \
98 M(DrawEdgeAAImageSet)
99
100
101// Defines SkRecords::Type, an enum of all record types.
102#define ENUM(T) T##_Type,
104#undef ENUM
105
106#define ACT_AS_PTR(ptr) \
107 operator T*() const { return ptr; } \
108 T* operator->() const { return ptr; }
109
110// An Optional doesn't own the pointer's memory, but may need to destroy non-POD data.
111template <typename T>
112class Optional {
113public:
114 Optional() : fPtr(nullptr) {}
115 Optional(T* ptr) : fPtr(ptr) {}
116 Optional(Optional&& o) : fPtr(o.fPtr) {
117 o.fPtr = nullptr;
118 }
119 ~Optional() { if (fPtr) fPtr->~T(); }
120
121 ACT_AS_PTR(fPtr)
122private:
123 T* fPtr;
124 Optional(const Optional&) = delete;
125 Optional& operator=(const Optional&) = delete;
126};
127
128// PODArray doesn't own the pointer's memory, and we assume the data is POD.
129template <typename T>
130class PODArray {
131public:
133 PODArray(T* ptr) : fPtr(ptr) {}
134 // Default copy and assign.
135
136 ACT_AS_PTR(fPtr)
137private:
138 T* fPtr;
139};
140
141#undef ACT_AS_PTR
142
143// SkPath::getBounds() isn't thread safe unless we precache the bounds in a singlethreaded context.
144// SkPath::cheapComputeDirection() is similar.
145// Recording is a convenient time to cache these, or we can delay it to between record and playback.
146struct PreCachedPath : public SkPath {
148 PreCachedPath(const SkPath& path);
149};
150
151// Like SkPath::getBounds(), SkMatrix::getType() isn't thread safe unless we precache it.
152// This may not cover all SkMatrices used by the picture (e.g. some could be hiding in a shader).
153struct TypedMatrix : public SkMatrix {
156};
157
158enum Tags {
159 kDraw_Tag = 1, // May draw something (usually named DrawFoo).
160 kHasImage_Tag = 2, // Contains an SkImage or SkBitmap.
161 kHasText_Tag = 4, // Contains text.
162 kHasPaint_Tag = 8, // May have an SkPaint field, at least optionally.
163 kMultiDraw_Tag = 16, // Drawing operations that render multiple independent primitives.
164 // These draws are capable of blending with themselves.
165
167};
168
169// A macro to make it a little easier to define a struct that can be stored in SkRecord.
170#define RECORD(T, tags, ...) \
171 struct T { \
172 static const Type kType = T##_Type; \
173 static const int kTags = tags; \
174 __VA_ARGS__; \
175 };
176
177#define RECORD_TRIVIAL(T, tags) \
178 struct T { \
179 static const Type kType = T##_Type; \
180 static const int kTags = tags; \
181 };
182
183RECORD_TRIVIAL(NoOp, 0)
184RECORD(Restore, 0,
186RECORD_TRIVIAL(Save, 0)
187
192 SkCanvas::SaveLayerFlags saveLayerFlags;
194 skia_private::AutoTArray<sk_sp<SkImageFilter>> filters)
195
196RECORD(SaveBehind, 0,
197 Optional<SkRect> subset)
198
199RECORD(SetMatrix, 0,
201RECORD(SetM44, 0,
203RECORD(Concat, 0,
205RECORD(Concat44, 0,
207
208RECORD(Translate, 0,
210 SkScalar dy)
211
213 SkScalar sx;
214 SkScalar sy)
215
216struct ClipOpAndAA {
217 ClipOpAndAA() {}
218 ClipOpAndAA(SkClipOp op, bool aa) : fOp(static_cast<unsigned>(op)), fAA(aa) {}
219
220 SkClipOp op() const { return static_cast<SkClipOp>(fOp); }
221 bool aa() const { return fAA != 0; }
222
223private:
224 unsigned fOp : 31; // This really only needs to be 3, but there's no win today to do so.
225 unsigned fAA : 1; // MSVC won't pack an enum with an bool, so we call this an unsigned.
226};
227static_assert(sizeof(ClipOpAndAA) == 4, "ClipOpAndAASize");
228
229RECORD(ClipPath, 0,
231 ClipOpAndAA opAA)
232RECORD(ClipRRect, 0,
234 ClipOpAndAA opAA)
236 SkRect rect;
237 ClipOpAndAA opAA)
238RECORD(ClipRegion, 0,
240 SkClipOp op)
241RECORD(ClipShader, 0,
242 sk_sp<SkShader> shader;
243 SkClipOp op)
244RECORD_TRIVIAL(ResetClip, 0)
245
246// While not strictly required, if you have an SkPaint, it's fastest to put it first.
252 unsigned useCenter)
255 SkRRect outer;
256 SkRRect inner)
257RECORD(DrawDrawable, kDraw_Tag,
260 int32_t index)
264 SkScalar left;
265 SkScalar top;
279 SkFilterMode filter)
283 SkRect src;
284 SkRect dst;
289 SkRect oval)
292RECORD(DrawBehind, kDraw_Tag|kHasPaint_Tag,
297RECORD(DrawPicture, kDraw_Tag|kHasPaint_Tag,
304 unsigned count;
305 PODArray<SkPoint> pts)
311 SkRect rect)
312RECORD(DrawRegion, kDraw_Tag|kHasPaint_Tag,
317 sk_sp<const SkTextBlob> blob;
318 SkScalar x;
319 SkScalar y)
322 sk_sp<const sktext::gpu::Slug> slug)
325 PODArray<SkPoint> cubics;
327 PODArray<SkPoint> texCoords;
328 SkBlendMode bmode)
335 int count;
338 Optional<SkRect> cull)
341 sk_sp<SkVertices> vertices;
342 SkBlendMode bmode)
346 sk_sp<SkBlender> blender)
347RECORD(DrawShadowRec, kDraw_Tag,
349 SkDrawShadowRec rec)
350RECORD(DrawAnnotation, 0, // TODO: kDraw_Tag, skia:5548
354RECORD(DrawEdgeAAQuad, kDraw_Tag,
355 SkRect rect;
357 SkCanvas::QuadAAFlags aa;
362 skia_private::AutoTArray<SkCanvas::ImageSetEntry> set;
363 int count;
367 SkCanvas::SrcRectConstraint constraint)
368#undef RECORD
369
370} // namespace SkRecords
371
372#endif//SkRecords_DEFINED
SkBlendMode
Definition: SkBlendMode.h:38
SkClipOp
Definition: SkClipOp.h:13
uint32_t SkColor
Definition: SkColor.h:37
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
#define RECORD_TRIVIAL(T, tags)
Definition: SkRecords.h:177
#define ACT_AS_PTR(ptr)
Definition: SkRecords.h:106
#define ENUM(T)
Definition: SkRecords.h:102
SkFilterMode
SrcRectConstraint
Definition: SkCanvas.h:1541
Definition: SkData.h:25
Definition: SkM44.h:150
Definition: SkMesh.h:263
Definition: SkPath.h:59
Optional(Optional &&o)
Definition: SkRecords.h:116
DlColor color
float SkScalar
Definition: extension.cpp:12
uint8_t value
Definition: dart.idl:629
double y
double x
skia_private::AutoTArray< SkCanvas::ImageSetEntry > set
Definition: SkRecords.h:362
RECORD(Restore, 0, TypedMatrix matrix) RECORD(SaveLayer
sk_sp< const sktext::gpu::Slug > slug kDraw_Tag kHasImage_Tag kHasPaint_Tag kMultiDraw_Tag
Definition: SkRecords.h:329
SkBlendMode mode
Definition: SkRecords.h:336
SkCanvas::SaveLayerFlags saveLayerFlags
Definition: SkRecords.h:192
int xCount
Definition: SkRecords.h:270
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
int flagCount
Definition: SkRecords.h:274
SkRect worstCaseBounds
Definition: SkRecords.h:259
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
Optional< SkRect > bounds
Definition: SkRecords.h:189
PODArray< SkRect > texs
Definition: SkRecords.h:333
int yCount
Definition: SkRecords.h:272
PODArray< SkPoint > dstClips
Definition: SkRecords.h:364
SkScalar backdropScale
Definition: SkRecords.h:193
sk_sp< const SkImage > image
Definition: SkRecords.h:269
int count
Definition: SkRecords.h:335
ClipOpAndAA opAA SkRegion region
Definition: SkRecords.h:238
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
sk_sp< const SkImageFilter > backdrop
Definition: SkRecords.h:191
SkRRect rrect
Definition: SkRecords.h:232
SkString key
Definition: SkRecords.h:352
@ kHasImage_Tag
Definition: SkRecords.h:160
@ kDrawWithPaint_Tag
Definition: SkRecords.h:166
@ kHasText_Tag
Definition: SkRecords.h:161
SkRect dst
Definition: SkRecords.h:278
SkRect oval
Definition: SkRecords.h:249
PODArray< SkRSXform > xforms
Definition: SkRecords.h:332
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkMatrix > preViewMatrices
Definition: SkRecords.h:365
SkScalar startAngle
Definition: SkRecords.h:250
PODArray< int > xDivs
Definition: SkRecords.h:271
SkIRect src
Definition: SkRecords.h:277
SkScalar sweepAngle
Definition: SkRecords.h:251
@ SK_RECORD_TYPES
Definition: SkRecords.h:103
SkMesh mesh
Definition: SkRecords.h:345
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
Optional< SkPaint > paint
Definition: SkRecords.h:190
PODArray< SkCanvas::Lattice::RectType > flags
Definition: SkRecords.h:275
unsigned useCenter kDraw_Tag
Definition: SkRecords.h:257
PODArray< int > yDivs
Definition: SkRecords.h:273
void DrawImage(SkCanvas *canvas, const SkImage *image, SkScalar x, SkScalar y, const SkSamplingOptions &sampling={}, const SkPaint *paint=nullptr, SkCanvas::SrcRectConstraint constraint=SkCanvas::kFast_SrcRectConstraint)
SK_API void DrawImageRect(SkCanvas *canvas, const SkImage *image, const SkRect &src, const SkRect &dst, const SkSamplingOptions &sampling={}, const SkPaint *paint=nullptr, SkCanvas::SrcRectConstraint constraint=SkCanvas::kFast_SrcRectConstraint)
static const char * Concat(const char *a, const char *b)
Definition: file_test.cc:86
DlCanvas::PointMode PointMode
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
skgpu::graphite::DrawAtlas DrawAtlas
#define T
Definition: precompiler.cc:65
Definition: SkRect.h:32