Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RecordDrawTest.cpp
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
13#include "include/core/SkM44.h"
17#include "include/core/SkRect.h"
22#include "src/core/SkRecord.h"
24#include "src/core/SkRecorder.h"
25#include "src/core/SkRecords.h"
27#include "tests/Test.h"
28
29using namespace skia_private;
30
31class SkImage;
32
33static const int W = 1920, H = 1080;
34
36public:
37 JustOneDraw() : fCalls(0) {}
38
39 bool abort() override { return fCalls++ > 0; }
40private:
41 int fCalls;
42};
43
44DEF_TEST(RecordDraw_LazySaves, r) {
45 // Record two commands.
46 SkRecord record;
47 SkRecorder recorder(&record, W, H);
48
49 REPORTER_ASSERT(r, 0 == record.count());
50 recorder.save();
51 REPORTER_ASSERT(r, 0 == record.count()); // the save was not recorded (yet)
52 recorder.drawColor(SK_ColorRED);
53 REPORTER_ASSERT(r, 1 == record.count());
54 recorder.scale(2, 2);
55 REPORTER_ASSERT(r, 3 == record.count()); // now we see the save
56 recorder.restore();
57 REPORTER_ASSERT(r, 4 == record.count());
58
59 assert_type<SkRecords::DrawPaint>(r, record, 0);
60 assert_type<SkRecords::Save> (r, record, 1);
61 assert_type<SkRecords::Scale> (r, record, 2);
62 assert_type<SkRecords::Restore> (r, record, 3);
63
64 recorder.save();
65 recorder.save();
66 recorder.restore();
67 recorder.restore();
68 REPORTER_ASSERT(r, 4 == record.count());
69}
70
71DEF_TEST(RecordDraw_Abort, r) {
72 // Record two commands.
73 SkRecord record;
74 SkRecorder recorder(&record, W, H);
75 recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint());
76 recorder.clipRect(SkRect::MakeWH(100, 200));
77
78 SkRecord rerecord;
79 SkRecorder canvas(&rerecord, W, H);
80
82 SkRecordDraw(record, &canvas, nullptr, nullptr, 0, nullptr/*bbh*/, &callback);
83
84 REPORTER_ASSERT(r, 1 == count_instances_of_type<SkRecords::DrawRect>(rerecord));
85 REPORTER_ASSERT(r, 0 == count_instances_of_type<SkRecords::ClipRect>(rerecord));
86}
87
88DEF_TEST(RecordDraw_Unbalanced, r) {
89 SkRecord record;
90 SkRecorder recorder(&record, W, H);
91 recorder.save(); // We won't balance this, but SkRecordDraw will for us.
92 recorder.scale(2, 2);
93
94 SkRecord rerecord;
95 SkRecorder canvas(&rerecord, W, H);
96 SkRecordDraw(record, &canvas, nullptr, nullptr, 0, nullptr/*bbh*/, nullptr/*callback*/);
97
98 int save_count = count_instances_of_type<SkRecords::Save>(rerecord);
99 int restore_count = count_instances_of_type<SkRecords::Save>(rerecord);
100 REPORTER_ASSERT(r, save_count == restore_count);
101}
102
103DEF_TEST(RecordDraw_SetMatrixClobber, r) {
104 // Set up an SkRecord that just scales by 2x,3x.
105 SkRecord scaleRecord;
106 SkRecorder scaleCanvas(&scaleRecord, W, H);
108 scale.setScale(2, 3);
109 scaleCanvas.setMatrix(scale);
110
111 // Set up an SkRecord with an initial +20, +20 translate.
112 SkRecord translateRecord;
113 SkRecorder translateCanvas(&translateRecord, W, H);
114 SkMatrix translate;
115 translate.setTranslate(20, 20);
116 translateCanvas.setMatrix(translate);
117
118 SkRecordDraw(scaleRecord, &translateCanvas, nullptr, nullptr, 0, nullptr/*bbh*/, nullptr/*callback*/);
119 REPORTER_ASSERT(r, 4 == translateRecord.count());
120 assert_type<SkRecords::SetM44>(r, translateRecord, 0);
121 assert_type<SkRecords::Save> (r, translateRecord, 1);
122 assert_type<SkRecords::SetM44>(r, translateRecord, 2);
123 assert_type<SkRecords::Restore> (r, translateRecord, 3);
124
125 // When we look at translateRecord now, it should have its first +20,+20 translate,
126 // then a 2x,3x scale that's been concatted with that +20,+20 translate.
127 const SkRecords::SetM44* setMatrix;
128 setMatrix = assert_type<SkRecords::SetM44>(r, translateRecord, 0);
129 REPORTER_ASSERT(r, setMatrix->matrix == SkM44(translate));
130
131 setMatrix = assert_type<SkRecords::SetM44>(r, translateRecord, 2);
132 SkMatrix expected = scale;
133 expected.postConcat(translate);
134 REPORTER_ASSERT(r, setMatrix->matrix == SkM44(expected));
135}
136
137// Like a==b, with a little slop recognizing that float equality can be weird.
139 SkRect inset(a), outset(a);
140 inset.inset(1, 1);
141 outset.outset(1, 1);
142 return outset.contains(b) && !inset.contains(b);
143}
144
145// TODO This would be nice, but we can't get it right today.
146#if 0
147DEF_TEST(RecordDraw_BasicBounds, r) {
148 SkRecord record;
149 SkRecorder recorder(&record, W, H);
150 recorder.save();
151 recorder.clipRect(SkRect::MakeWH(400, 500));
152 recorder.scale(2, 2);
153 recorder.drawRect(SkRect::MakeWH(320, 240), SkPaint());
154 recorder.restore();
155
158
159 for (int i = 0; i < record.count(); i++) {
160 REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bounds[i]));
161 }
162}
163#endif
164
165// A regression test for crbug.com/415468 and https://bug.skia.org/2957 .
166//
167// This also now serves as a regression test for crbug.com/418417. We used to adjust the
168// bounds for the saveLayer, clip, and restore to be greater than the bounds of the picture.
169// (We were applying the saveLayer paint to the bounds after restore, which makes no sense.)
170DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds, r) {
171 SkRecord record;
172 SkRecorder recorder(&record, 50, 50);
173
174 // We draw a rectangle with a long drop shadow. We used to not update the clip
175 // bounds based on SaveLayer paints, so the drop shadow could be cut off.
177 paint.setImageFilter(SkImageFilters::DropShadow(20, 0, 0, 0, SK_ColorBLACK, nullptr));
178
179 recorder.saveLayer(nullptr, &paint);
180 recorder.clipRect(SkRect::MakeWH(20, 40));
181 recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
182 recorder.restore();
183
184 // Under the original bug, the right edge value of the drawRect would be 20 less than asserted
185 // here because we intersected it with a clip that had not been adjusted for the drop shadow.
186 //
187 // The second bug showed up as adjusting the picture bounds (0,0,50,50) by the drop shadow too.
188 // The saveLayer, clipRect, and restore bounds were incorrectly (0,0,70,50).
189 //
190 // Now, all recorded bounds should be (0,0,40,40), representing the union of the original
191 // draw/clip (0,0,20,40) with the 20px offset drop shadow along the x-axis (20,0,40,40).
192 // The saveLayer and restore match the output bounds of the drop shadow filter, instead of
193 // expanding to fill the entire picture.
194 AutoTArray<SkRect> bounds(record.count());
196 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds.data(), meta);
197 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[0], SkRect::MakeLTRB(0, 0, 40, 40)));
198 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[1], SkRect::MakeLTRB(0, 0, 40, 40)));
199 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[2], SkRect::MakeLTRB(0, 0, 40, 40)));
200 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[3], SkRect::MakeLTRB(0, 0, 40, 40)));
201}
202
203DEF_TEST(RecordDraw_Metadata, r) {
204 SkRecord record;
205 SkRecorder recorder(&record, 50, 50);
206
207 // Just doing some mildly interesting drawing, mostly grabbed from the unit test above.
209 paint.setImageFilter(SkImageFilters::DropShadow(20, 0, 0, 0, SK_ColorBLACK, nullptr));
210
211 recorder.saveLayer(nullptr, &paint);
212 recorder.clipRect(SkRect::MakeWH(20, 40));
213 recorder.save();
214 recorder.translate(10, 10);
215 recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
216 recorder.restore();
217 recorder.restore();
218
219 AutoTArray<SkRect> bounds(record.count());
221 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds.data(), meta);
222
223 REPORTER_ASSERT(r, !meta[0].isDraw); // saveLayer (not a draw, but its restore will be)
224 REPORTER_ASSERT(r, !meta[1].isDraw); // clip
225 REPORTER_ASSERT(r, !meta[2].isDraw); // save
226 REPORTER_ASSERT(r, !meta[3].isDraw); // translate
227 REPORTER_ASSERT(r, meta[4].isDraw); // drawRect
228 REPORTER_ASSERT(r, !meta[5].isDraw); // restore (paired with save, not a draw)
229 REPORTER_ASSERT(r, meta[6].isDraw); // restore (paired with saveLayer, a draw)
230}
231
232// TODO This would be nice, but we can't get it right today.
233#if 0
234// When a saveLayer provides an explicit bound and has a complex paint (e.g., one that
235// affects transparent black), that bound should serve to shrink the area of the required
236// backing store.
237DEF_TEST(RecordDraw_SaveLayerBoundsAffectsClipBounds, r) {
238 SkRecord record;
239 SkRecorder recorder(&record, 50, 50);
240
241 SkPaint p;
242 p.setBlendMode(SkBlendMode::kSrc);
243
244 SkRect layerBounds = SkRect::MakeLTRB(10, 10, 40, 40);
245 recorder.saveLayer(&layerBounds, &p);
246 recorder.drawRect(SkRect::MakeLTRB(20, 20, 30, 30), SkPaint());
247 recorder.restore();
248
250 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds.data());
251 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[0], SkRect::MakeLTRB(10, 10, 40, 40)));
252 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[1], SkRect::MakeLTRB(20, 20, 30, 30)));
253 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[2], SkRect::MakeLTRB(10, 10, 40, 40)));
254}
255#endif
256
257DEF_TEST(RecordDraw_drawImage, r){
258 class SkCanvasMock : public SkCanvas {
259 public:
260 SkCanvasMock(int width, int height) : SkCanvas(width, height) {
261 this->resetTestValues();
262 }
263
264 void resetTestValues() {
265 fDrawImageCalled = fDrawImageRectCalled = false;
266 }
267
268 bool fDrawImageCalled;
269 bool fDrawImageRectCalled;
270 };
271
273 surface->getCanvas()->clear(SK_ColorGREEN);
274 sk_sp<SkImage> image(surface->makeImageSnapshot());
275
276 SkCanvasMock canvas(10, 10);
277}
static const int outset
Definition BlurTest.cpp:58
static bool sloppy_rect_eq(SkRect a, SkRect b)
static const int W
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
void SkRecordFillBounds(const SkRect &cullRect, const SkRecord &record, SkRect bounds[], SkBBoxHierarchy::Metadata meta[])
void SkRecordDraw(const SkRecord &record, SkCanvas *canvas, SkPicture const *const drawablePicts[], SkDrawable *const drawables[], int drawableCount, const SkBBoxHierarchy *bbh, SkPicture::AbortCallback *callback)
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
bool abort() override
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
int save()
Definition SkCanvas.cpp:451
void setMatrix(const SkM44 &matrix)
void scale(SkScalar sx, SkScalar sy)
static sk_sp< SkImageFilter > DropShadow(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor color, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
Definition SkM44.h:150
SkMatrix & postConcat(const SkMatrix &other)
Definition SkMatrix.cpp:683
SkMatrix & setTranslate(SkScalar dx, SkScalar dy)
Definition SkMatrix.cpp:254
SkMatrix & setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition SkMatrix.cpp:296
int count() const
Definition SkRecord.h:38
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
static bool b
struct MyStruct a[10]
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
Optional< SkRect > bounds
Definition SkRecords.h:189
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static SkRect inset(const SkRect &r)
int32_t height
int32_t width
const Scalar scale
Definition SkMD5.cpp:130
static SkImageInfo MakeN32Premul(int width, int height)
void inset(float dx, float dy)
Definition SkRect.h:1060
bool contains(SkScalar x, SkScalar y) const
Definition extension.cpp:19
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646