Flutter Engine
The Flutter Engine
Classes | Functions | Variables
RecordDrawTest.cpp File Reference
#include "include/core/SkBBHFactory.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkM44.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPicture.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSurface.h"
#include "include/effects/SkImageFilters.h"
#include "include/private/base/SkTemplates.h"
#include "src/core/SkRecord.h"
#include "src/core/SkRecordDraw.h"
#include "src/core/SkRecorder.h"
#include "src/core/SkRecords.h"
#include "tests/RecordTestUtils.h"
#include "tests/Test.h"

Go to the source code of this file.

Classes

class  JustOneDraw
 

Functions

 DEF_TEST (RecordDraw_LazySaves, r)
 
 DEF_TEST (RecordDraw_Abort, r)
 
 DEF_TEST (RecordDraw_Unbalanced, r)
 
 DEF_TEST (RecordDraw_SetMatrixClobber, r)
 
static bool sloppy_rect_eq (SkRect a, SkRect b)
 
 DEF_TEST (RecordDraw_SaveLayerAffectsClipBounds, r)
 
 DEF_TEST (RecordDraw_Metadata, r)
 
 DEF_TEST (RecordDraw_drawImage, r)
 

Variables

static const int W = 1920
 
static const int H = 1080
 

Function Documentation

◆ DEF_TEST() [1/7]

DEF_TEST ( RecordDraw_Abort  ,
 
)

Definition at line 71 of file RecordDrawTest.cpp.

71 {
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}
static const int W
void SkRecordDraw(const SkRecord &record, SkCanvas *canvas, SkPicture const *const drawablePicts[], SkDrawable *const drawables[], int drawableCount, const SkBBoxHierarchy *bbh, SkPicture::AbortCallback *callback)
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
Definition: SkMD5.cpp:130
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609

◆ DEF_TEST() [2/7]

DEF_TEST ( RecordDraw_drawImage  ,
 
)

Definition at line 257 of file RecordDrawTest.cpp.

257 {
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}
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
VkSurfaceKHR surface
Definition: main.cc:49
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
int32_t height
int32_t width
static SkImageInfo MakeN32Premul(int width, int height)

◆ DEF_TEST() [3/7]

DEF_TEST ( RecordDraw_LazySaves  ,
 
)

Definition at line 44 of file RecordDrawTest.cpp.

44 {
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}
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
int count() const
Definition: SkRecord.h:38

◆ DEF_TEST() [4/7]

DEF_TEST ( RecordDraw_Metadata  ,
 
)

Definition at line 203 of file RecordDrawTest.cpp.

203 {
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
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}
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
void SkRecordFillBounds(const SkRect &cullRect, const SkRecord &record, SkRect bounds[], SkBBoxHierarchy::Metadata meta[])
static sk_sp< SkImageFilter > DropShadow(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor color, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
const Paint & paint
Definition: color_source.cc:38
Optional< SkRect > bounds
Definition: SkRecords.h:189

◆ DEF_TEST() [5/7]

DEF_TEST ( RecordDraw_SaveLayerAffectsClipBounds  ,
 
)

Definition at line 170 of file RecordDrawTest.cpp.

170 {
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.
196 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds.data(), meta);
201}
static bool sloppy_rect_eq(SkRect a, SkRect b)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646

◆ DEF_TEST() [6/7]

DEF_TEST ( RecordDraw_SetMatrixClobber  ,
 
)

Definition at line 103 of file RecordDrawTest.cpp.

103 {
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}
Definition: SkM44.h:150
SkMatrix & postConcat(const SkMatrix &other)
Definition: SkMatrix.cpp:683
SkMatrix & setTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:254
const Scalar scale

◆ DEF_TEST() [7/7]

DEF_TEST ( RecordDraw_Unbalanced  ,
 
)

Definition at line 88 of file RecordDrawTest.cpp.

88 {
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}

◆ sloppy_rect_eq()

static bool sloppy_rect_eq ( SkRect  a,
SkRect  b 
)
static

Definition at line 138 of file RecordDrawTest.cpp.

138 {
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}
static const int outset
Definition: BlurTest.cpp:58
static bool b
struct MyStruct a[10]
static SkRect inset(const SkRect &r)
void inset(float dx, float dy)
Definition: SkRect.h:1060
bool contains(SkScalar x, SkScalar y) const
Definition: extension.cpp:19

Variable Documentation

◆ H

const int H = 1080
static

Definition at line 33 of file RecordDrawTest.cpp.

◆ W

const int W = 1920
static

Definition at line 33 of file RecordDrawTest.cpp.