Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / core / SkRecords.h
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
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkDrawable.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageFilter.h"
16 #include "include/core/SkM44.h"
17 #include "include/core/SkMatrix.h"
18 #include "include/core/SkPath.h"
19 #include "include/core/SkPicture.h"
20 #include "include/core/SkRRect.h"
21 #include "include/core/SkRSXform.h"
22 #include "include/core/SkRect.h"
23 #include "include/core/SkRegion.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkTextBlob.h"
26 #include "include/core/SkVertices.h"
27 #include "src/core/SkDrawShadowInfo.h"
28
29 #if SK_SUPPORT_GPU
30 #include "include/private/chromium/Slug.h"
31 #endif
32
33 namespace SkRecords {
34
35 // A list of all the types of canvas calls we can record.
36 // Each of these is reified into a struct below.
37 //
38 // (We're using the macro-of-macro trick here to do several different things with the same list.)
39 //
40 // We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords
41 // types polymorphically.  (See SkRecord::Record::{visit,mutate} for an example.)
42 //
43 // Order doesn't technically matter here, but the compiler can generally generate better code if
44 // you keep them semantically grouped, especially the Draws.  It's also nice to leave NoOp at 0.
45 #define SK_RECORD_TYPES(M)                                          \
46     M(NoOp)                                                         \
47     M(Flush)                                                        \
48     M(Restore)                                                      \
49     M(Save)                                                         \
50     M(SaveLayer)                                                    \
51     M(SaveBehind)                                                   \
52     M(SetMatrix)                                                    \
53     M(SetM44)                                                       \
54     M(Translate)                                                    \
55     M(Scale)                                                        \
56     M(Concat)                                                       \
57     M(Concat44)                                                     \
58     M(ClipPath)                                                     \
59     M(ClipRRect)                                                    \
60     M(ClipRect)                                                     \
61     M(ClipRegion)                                                   \
62     M(ClipShader)                                                   \
63     M(ResetClip)                                                    \
64     M(DrawArc)                                                      \
65     M(DrawDrawable)                                                 \
66     M(DrawImage)                                                    \
67     M(DrawImageLattice)                                             \
68     M(DrawImageRect)                                                \
69     M(DrawDRRect)                                                   \
70     M(DrawOval)                                                     \
71     M(DrawBehind)                                                   \
72     M(DrawPaint)                                                    \
73     M(DrawPath)                                                     \
74     M(DrawPatch)                                                    \
75     M(DrawPicture)                                                  \
76     M(DrawPoints)                                                   \
77     M(DrawRRect)                                                    \
78     M(DrawRect)                                                     \
79     M(DrawRegion)                                                   \
80     M(DrawTextBlob)                                                 \
81     M(DrawSlug)                                                     \
82     M(DrawAtlas)                                                    \
83     M(DrawVertices)                                                 \
84     M(DrawShadowRec)                                                \
85     M(DrawAnnotation)                                               \
86     M(DrawEdgeAAQuad)                                               \
87     M(DrawEdgeAAImageSet)
88
89
90 // Defines SkRecords::Type, an enum of all record types.
91 #define ENUM(T) T##_Type,
92 enum Type { SK_RECORD_TYPES(ENUM) };
93 #undef ENUM
94
95 #define ACT_AS_PTR(ptr)                 \
96     operator T*() const { return ptr; } \
97     T* operator->() const { return ptr; }
98
99 // An Optional doesn't own the pointer's memory, but may need to destroy non-POD data.
100 template <typename T>
101 class Optional {
102 public:
103     Optional() : fPtr(nullptr) {}
104     Optional(T* ptr) : fPtr(ptr) {}
105     Optional(Optional&& o) : fPtr(o.fPtr) {
106         o.fPtr = nullptr;
107     }
108     ~Optional() { if (fPtr) fPtr->~T(); }
109
110     ACT_AS_PTR(fPtr)
111 private:
112     T* fPtr;
113     Optional(const Optional&) = delete;
114     Optional& operator=(const Optional&) = delete;
115 };
116
117 // PODArray doesn't own the pointer's memory, and we assume the data is POD.
118 template <typename T>
119 class PODArray {
120 public:
121     PODArray() {}
122     PODArray(T* ptr) : fPtr(ptr) {}
123     // Default copy and assign.
124
125     ACT_AS_PTR(fPtr)
126 private:
127     T* fPtr;
128 };
129
130 #undef ACT_AS_PTR
131
132 // SkPath::getBounds() isn't thread safe unless we precache the bounds in a singlethreaded context.
133 // SkPath::cheapComputeDirection() is similar.
134 // Recording is a convenient time to cache these, or we can delay it to between record and playback.
135 struct PreCachedPath : public SkPath {
136     PreCachedPath() {}
137     PreCachedPath(const SkPath& path);
138 };
139
140 // Like SkPath::getBounds(), SkMatrix::getType() isn't thread safe unless we precache it.
141 // This may not cover all SkMatrices used by the picture (e.g. some could be hiding in a shader).
142 struct TypedMatrix : public SkMatrix {
143     TypedMatrix() {}
144     TypedMatrix(const SkMatrix& matrix);
145 };
146
147 enum Tags {
148     kDraw_Tag      = 1,   // May draw something (usually named DrawFoo).
149     kHasImage_Tag  = 2,   // Contains an SkImage or SkBitmap.
150     kHasText_Tag   = 4,   // Contains text.
151     kHasPaint_Tag  = 8,   // May have an SkPaint field, at least optionally.
152
153     kDrawWithPaint_Tag = kDraw_Tag | kHasPaint_Tag,
154 };
155
156 // A macro to make it a little easier to define a struct that can be stored in SkRecord.
157 #define RECORD(T, tags, ...)            \
158 struct T {                              \
159     static const Type kType = T##_Type; \
160     static const int kTags = tags;      \
161     __VA_ARGS__;                        \
162 };
163
164 RECORD(NoOp, 0);
165 RECORD(Flush, 0);
166 RECORD(Restore, 0,
167         TypedMatrix matrix);
168 RECORD(Save, 0);
169
170 RECORD(SaveLayer, kHasPaint_Tag,
171        Optional<SkRect> bounds;
172        Optional<SkPaint> paint;
173        sk_sp<const SkImageFilter> backdrop;
174        SkCanvas::SaveLayerFlags saveLayerFlags;
175        SkScalar backdropScale);
176
177 RECORD(SaveBehind, 0,
178        Optional<SkRect> subset);
179
180 RECORD(SetMatrix, 0,
181         TypedMatrix matrix);
182 RECORD(SetM44, 0,
183         SkM44 matrix);
184 RECORD(Concat, 0,
185         TypedMatrix matrix);
186 RECORD(Concat44, 0,
187        SkM44 matrix);
188
189 RECORD(Translate, 0,
190         SkScalar dx;
191         SkScalar dy);
192
193 RECORD(Scale, 0,
194        SkScalar sx;
195        SkScalar sy);
196
197 struct ClipOpAndAA {
198     ClipOpAndAA() {}
199     ClipOpAndAA(SkClipOp op, bool aa) : fOp(static_cast<unsigned>(op)), fAA(aa) {}
200
201     SkClipOp op() const { return static_cast<SkClipOp>(fOp); }
202     bool aa() const { return fAA != 0; }
203
204 private:
205     unsigned fOp : 31;  // This really only needs to be 3, but there's no win today to do so.
206     unsigned fAA :  1;  // MSVC won't pack an enum with an bool, so we call this an unsigned.
207 };
208 static_assert(sizeof(ClipOpAndAA) == 4, "ClipOpAndAASize");
209
210 RECORD(ClipPath, 0,
211         PreCachedPath path;
212         ClipOpAndAA opAA);
213 RECORD(ClipRRect, 0,
214         SkRRect rrect;
215         ClipOpAndAA opAA);
216 RECORD(ClipRect, 0,
217         SkRect rect;
218         ClipOpAndAA opAA);
219 RECORD(ClipRegion, 0,
220         SkRegion region;
221         SkClipOp op);
222 RECORD(ClipShader, 0,
223         sk_sp<SkShader> shader;
224         SkClipOp op);
225 RECORD(ResetClip, 0);
226
227 // While not strictly required, if you have an SkPaint, it's fastest to put it first.
228 RECORD(DrawArc, kDraw_Tag|kHasPaint_Tag,
229        SkPaint paint;
230        SkRect oval;
231        SkScalar startAngle;
232        SkScalar sweepAngle;
233        unsigned useCenter);
234 RECORD(DrawDRRect, kDraw_Tag|kHasPaint_Tag,
235         SkPaint paint;
236         SkRRect outer;
237         SkRRect inner);
238 RECORD(DrawDrawable, kDraw_Tag,
239         Optional<SkMatrix> matrix;
240         SkRect worstCaseBounds;
241         int32_t index);
242 RECORD(DrawImage, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag,
243         Optional<SkPaint> paint;
244         sk_sp<const SkImage> image;
245         SkScalar left;
246         SkScalar top;
247         SkSamplingOptions sampling);
248 RECORD(DrawImageLattice, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag,
249         Optional<SkPaint> paint;
250         sk_sp<const SkImage> image;
251         int xCount;
252         PODArray<int> xDivs;
253         int yCount;
254         PODArray<int> yDivs;
255         int flagCount;
256         PODArray<SkCanvas::Lattice::RectType> flags;
257         PODArray<SkColor> colors;
258         SkIRect src;
259         SkRect dst;
260         SkFilterMode filter);
261 RECORD(DrawImageRect, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag,
262         Optional<SkPaint> paint;
263         sk_sp<const SkImage> image;
264         SkRect src;
265         SkRect dst;
266         SkSamplingOptions sampling;
267         SkCanvas::SrcRectConstraint constraint);
268 RECORD(DrawOval, kDraw_Tag|kHasPaint_Tag,
269         SkPaint paint;
270         SkRect oval);
271 RECORD(DrawPaint, kDraw_Tag|kHasPaint_Tag,
272         SkPaint paint);
273 RECORD(DrawBehind, kDraw_Tag|kHasPaint_Tag,
274        SkPaint paint);
275 RECORD(DrawPath, kDraw_Tag|kHasPaint_Tag,
276         SkPaint paint;
277         PreCachedPath path);
278 RECORD(DrawPicture, kDraw_Tag|kHasPaint_Tag,
279         Optional<SkPaint> paint;
280         sk_sp<const SkPicture> picture;
281         TypedMatrix matrix);
282 RECORD(DrawPoints, kDraw_Tag|kHasPaint_Tag,
283         SkPaint paint;
284         SkCanvas::PointMode mode;
285         unsigned count;
286         PODArray<SkPoint> pts);
287 RECORD(DrawRRect, kDraw_Tag|kHasPaint_Tag,
288         SkPaint paint;
289         SkRRect rrect);
290 RECORD(DrawRect, kDraw_Tag|kHasPaint_Tag,
291         SkPaint paint;
292         SkRect rect);
293 RECORD(DrawRegion, kDraw_Tag|kHasPaint_Tag,
294         SkPaint paint;
295         SkRegion region);
296 RECORD(DrawTextBlob, kDraw_Tag|kHasText_Tag|kHasPaint_Tag,
297         SkPaint paint;
298         sk_sp<const SkTextBlob> blob;
299         SkScalar x;
300         SkScalar y);
301 #if SK_SUPPORT_GPU
302 RECORD(DrawSlug, kDraw_Tag|kHasText_Tag,
303        sk_sp<const sktext::gpu::Slug> slug);
304 #else
305 RECORD(DrawSlug, 0)
306 #endif
307 RECORD(DrawPatch, kDraw_Tag|kHasPaint_Tag,
308         SkPaint paint;
309         PODArray<SkPoint> cubics;
310         PODArray<SkColor> colors;
311         PODArray<SkPoint> texCoords;
312         SkBlendMode bmode);
313 RECORD(DrawAtlas, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag,
314         Optional<SkPaint> paint;
315         sk_sp<const SkImage> atlas;
316         PODArray<SkRSXform> xforms;
317         PODArray<SkRect> texs;
318         PODArray<SkColor> colors;
319         int count;
320         SkBlendMode mode;
321         SkSamplingOptions sampling;
322         Optional<SkRect> cull);
323 RECORD(DrawVertices, kDraw_Tag|kHasPaint_Tag,
324         SkPaint paint;
325         sk_sp<SkVertices> vertices;
326         SkBlendMode bmode);
327 RECORD(DrawShadowRec, kDraw_Tag,
328        PreCachedPath path;
329        SkDrawShadowRec rec);
330 RECORD(DrawAnnotation, 0,  // TODO: kDraw_Tag, skia:5548
331        SkRect rect;
332        SkString key;
333        sk_sp<SkData> value);
334 RECORD(DrawEdgeAAQuad, kDraw_Tag,
335        SkRect rect;
336        PODArray<SkPoint> clip;
337        SkCanvas::QuadAAFlags aa;
338        SkColor4f color;
339        SkBlendMode mode);
340 RECORD(DrawEdgeAAImageSet, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag,
341        Optional<SkPaint> paint;
342        SkAutoTArray<SkCanvas::ImageSetEntry> set;
343        int count;
344        PODArray<SkPoint> dstClips;
345        PODArray<SkMatrix> preViewMatrices;
346        SkSamplingOptions sampling;
347        SkCanvas::SrcRectConstraint constraint);
348 #undef RECORD
349
350 }  // namespace SkRecords
351
352 #endif//SkRecords_DEFINED