blob: de9d2333cd26b043b4cedd2c6345a7b2a4d40336 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
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
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +00008#include "Test.h"
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +00009#include "RecordTestUtils.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000010
11#include "SkDebugCanvas.h"
Mike Klein271a0302014-09-23 15:28:38 -040012#include "SkDropShadowImageFilter.h"
piotaixr65151752014-10-16 11:58:39 -070013#include "SkImagePriv.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000014#include "SkRecord.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000015#include "SkRecordDraw.h"
Mike Klein271a0302014-09-23 15:28:38 -040016#include "SkRecordOpts.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000017#include "SkRecorder.h"
18#include "SkRecords.h"
piotaixr65151752014-10-16 11:58:39 -070019#include "SkSurface.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000020
21static const int W = 1920, H = 1080;
22
robertphillips783fe162015-01-07 07:28:41 -080023class JustOneDraw : public SkPicture::AbortCallback {
Mike Kleinc11530e2014-06-24 11:29:06 -040024public:
25 JustOneDraw() : fCalls(0) {}
26
mtklein36352bf2015-03-25 18:17:31 -070027 bool abort() override { return fCalls++ > 0; }
Mike Kleinc11530e2014-06-24 11:29:06 -040028private:
29 int fCalls;
30};
31
reed2ff1fce2014-12-11 07:07:37 -080032DEF_TEST(RecordDraw_LazySaves, r) {
33 // Record two commands.
34 SkRecord record;
35 SkRecorder recorder(&record, W, H);
36
37 REPORTER_ASSERT(r, 0 == record.count());
38 recorder.save();
39 REPORTER_ASSERT(r, 0 == record.count()); // the save was not recorded (yet)
40 recorder.drawColor(SK_ColorRED);
41 REPORTER_ASSERT(r, 1 == record.count());
42 recorder.scale(2, 2);
43 REPORTER_ASSERT(r, 3 == record.count()); // now we see the save
44 recorder.restore();
45 REPORTER_ASSERT(r, 4 == record.count());
46
47 assert_type<SkRecords::DrawPaint>(r, record, 0);
48 assert_type<SkRecords::Save> (r, record, 1);
mtkleine9d20522015-11-19 12:08:24 -080049 assert_type<SkRecords::Concat> (r, record, 2);
reed2ff1fce2014-12-11 07:07:37 -080050 assert_type<SkRecords::Restore> (r, record, 3);
51
52 recorder.save();
53 recorder.save();
54 recorder.restore();
55 recorder.restore();
56 REPORTER_ASSERT(r, 4 == record.count());
57}
58
Mike Kleinc11530e2014-06-24 11:29:06 -040059DEF_TEST(RecordDraw_Abort, r) {
60 // Record two commands.
61 SkRecord record;
62 SkRecorder recorder(&record, W, H);
63 recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint());
64 recorder.clipRect(SkRect::MakeWH(100, 200));
65
66 SkRecord rerecord;
67 SkRecorder canvas(&rerecord, W, H);
68
69 JustOneDraw callback;
halcanary96fcdcc2015-08-27 07:41:13 -070070 SkRecordDraw(record, &canvas, nullptr, nullptr, 0, nullptr/*bbh*/, &callback);
Mike Kleinc11530e2014-06-24 11:29:06 -040071
reed2ff1fce2014-12-11 07:07:37 -080072 REPORTER_ASSERT(r, 1 == count_instances_of_type<SkRecords::DrawRect>(rerecord));
73 REPORTER_ASSERT(r, 0 == count_instances_of_type<SkRecords::ClipRect>(rerecord));
Mike Kleinc11530e2014-06-24 11:29:06 -040074}
75
76DEF_TEST(RecordDraw_Unbalanced, r) {
77 SkRecord record;
78 SkRecorder recorder(&record, W, H);
79 recorder.save(); // We won't balance this, but SkRecordDraw will for us.
reed2ff1fce2014-12-11 07:07:37 -080080 recorder.scale(2, 2);
Mike Kleinc11530e2014-06-24 11:29:06 -040081
82 SkRecord rerecord;
83 SkRecorder canvas(&rerecord, W, H);
halcanary96fcdcc2015-08-27 07:41:13 -070084 SkRecordDraw(record, &canvas, nullptr, nullptr, 0, nullptr/*bbh*/, nullptr/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040085
reed2ff1fce2014-12-11 07:07:37 -080086 int save_count = count_instances_of_type<SkRecords::Save>(rerecord);
87 int restore_count = count_instances_of_type<SkRecords::Save>(rerecord);
88 REPORTER_ASSERT(r, save_count == restore_count);
Mike Kleinc11530e2014-06-24 11:29:06 -040089}
90
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000091DEF_TEST(RecordDraw_SetMatrixClobber, r) {
92 // Set up an SkRecord that just scales by 2x,3x.
93 SkRecord scaleRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000094 SkRecorder scaleCanvas(&scaleRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000095 SkMatrix scale;
96 scale.setScale(2, 3);
97 scaleCanvas.setMatrix(scale);
98
99 // Set up an SkRecord with an initial +20, +20 translate.
100 SkRecord translateRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000101 SkRecorder translateCanvas(&translateRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +0000102 SkMatrix translate;
103 translate.setTranslate(20, 20);
104 translateCanvas.setMatrix(translate);
105
halcanary96fcdcc2015-08-27 07:41:13 -0700106 SkRecordDraw(scaleRecord, &translateCanvas, nullptr, nullptr, 0, nullptr/*bbh*/, nullptr/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -0400107 REPORTER_ASSERT(r, 4 == translateRecord.count());
108 assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
109 assert_type<SkRecords::Save> (r, translateRecord, 1);
110 assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
111 assert_type<SkRecords::Restore> (r, translateRecord, 3);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +0000112
113 // When we look at translateRecord now, it should have its first +20,+20 translate,
114 // then a 2x,3x scale that's been concatted with that +20,+20 translate.
115 const SkRecords::SetMatrix* setMatrix;
116 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
117 REPORTER_ASSERT(r, setMatrix->matrix == translate);
118
Mike Kleinc11530e2014-06-24 11:29:06 -0400119 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +0000120 SkMatrix expected = scale;
121 expected.postConcat(translate);
122 REPORTER_ASSERT(r, setMatrix->matrix == expected);
123}
mtkleina723b572014-08-15 11:49:49 -0700124
mtklein937c9c72014-09-02 15:19:48 -0700125// Like a==b, with a little slop recognizing that float equality can be weird.
126static bool sloppy_rect_eq(SkRect a, SkRect b) {
127 SkRect inset(a), outset(a);
128 inset.inset(1, 1);
129 outset.outset(1, 1);
130 return outset.contains(b) && !inset.contains(b);
131}
132
Mike Klein738b80d2018-05-04 13:51:11 -0400133// TODO This would be nice, but we can't get it right today.
134#if 0
mtklein40732b32015-10-24 07:45:47 -0700135DEF_TEST(RecordDraw_BasicBounds, r) {
mtkleina723b572014-08-15 11:49:49 -0700136 SkRecord record;
mtkleina723b572014-08-15 11:49:49 -0700137 SkRecorder recorder(&record, W, H);
138 recorder.save();
139 recorder.clipRect(SkRect::MakeWH(400, 500));
140 recorder.scale(2, 2);
141 recorder.drawRect(SkRect::MakeWH(320, 240), SkPaint());
142 recorder.restore();
143
mtklein40732b32015-10-24 07:45:47 -0700144 SkAutoTMalloc<SkRect> bounds(record.count());
145 SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, bounds);
mtkleina723b572014-08-15 11:49:49 -0700146
mtklein40732b32015-10-24 07:45:47 -0700147 for (int i = 0; i < record.count(); i++) {
148 REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bounds[i]));
mtkleina723b572014-08-15 11:49:49 -0700149 }
150}
Mike Klein738b80d2018-05-04 13:51:11 -0400151#endif
mtklein00f30bd2014-09-02 12:03:31 -0700152
mtklein937c9c72014-09-02 15:19:48 -0700153// A regression test for crbug.com/409110.
154DEF_TEST(RecordDraw_TextBounds, r) {
155 SkRecord record;
156 SkRecorder recorder(&record, W, H);
157
158 // Two Chinese characters in UTF-8.
159 const char text[] = { '\xe6', '\xbc', '\xa2', '\xe5', '\xad', '\x97' };
160 const size_t bytes = SK_ARRAY_COUNT(text);
161
162 const SkScalar xpos[] = { 10, 20 };
163 recorder.drawPosTextH(text, bytes, xpos, 30, SkPaint());
164
165 const SkPoint pos[] = { {40, 50}, {60, 70} };
166 recorder.drawPosText(text, bytes, pos, SkPaint());
167
mtklein40732b32015-10-24 07:45:47 -0700168 SkAutoTMalloc<SkRect> bounds(record.count());
169 SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, bounds);
mtklein937c9c72014-09-02 15:19:48 -0700170
mtklein9a5380d2014-12-16 06:31:01 -0800171 // We can make these next assertions confidently because SkRecordFillBounds
172 // builds its bounds by overestimating font metrics in a platform-independent way.
173 // If that changes, these tests will need to be more flexible.
mtklein40732b32015-10-24 07:45:47 -0700174 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[0], SkRect::MakeLTRB(0, 0, 140, 60)));
175 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[1], SkRect::MakeLTRB(0, 20, 180, 100)));
mtklein937c9c72014-09-02 15:19:48 -0700176}
177
mtklein00f30bd2014-09-02 12:03:31 -0700178// Base test to ensure start/stop range is respected
179DEF_TEST(RecordDraw_PartialStartStop, r) {
180 static const int kWidth = 10, kHeight = 10;
181
182 SkRect r1 = { 0, 0, kWidth, kHeight };
183 SkRect r2 = { 0, 0, kWidth, kHeight/2 };
184 SkRect r3 = { 0, 0, kWidth/2, kHeight };
185 SkPaint p;
186
187 SkRecord record;
188 SkRecorder recorder(&record, kWidth, kHeight);
189 recorder.drawRect(r1, p);
190 recorder.drawRect(r2, p);
191 recorder.drawRect(r3, p);
192
193 SkRecord rerecord;
194 SkRecorder canvas(&rerecord, kWidth, kHeight);
halcanary96fcdcc2015-08-27 07:41:13 -0700195 SkRecordPartialDraw(record, &canvas, nullptr, 0, 1, 2, SkMatrix::I()); // replay just drawRect of r2
mtklein00f30bd2014-09-02 12:03:31 -0700196
reed2ff1fce2014-12-11 07:07:37 -0800197 REPORTER_ASSERT(r, 1 == count_instances_of_type<SkRecords::DrawRect>(rerecord));
198 int index = find_first_instances_of_type<SkRecords::DrawRect>(rerecord);
199 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, index);
mtklein00f30bd2014-09-02 12:03:31 -0700200 REPORTER_ASSERT(r, drawRect->rect == r2);
201}
202
halcanary6950de62015-11-07 05:29:00 -0800203// A regression test for crbug.com/415468 and https://bug.skia.org/2957 .
mtklein8e393bf2014-10-01 12:48:58 -0700204//
205// This also now serves as a regression test for crbug.com/418417. We used to adjust the
206// bounds for the saveLayer, clip, and restore to be greater than the bounds of the picture.
207// (We were applying the saveLayer paint to the bounds after restore, which makes no sense.)
Mike Klein271a0302014-09-23 15:28:38 -0400208DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds, r) {
209 SkRecord record;
210 SkRecorder recorder(&record, 50, 50);
211
212 // We draw a rectangle with a long drop shadow. We used to not update the clip
213 // bounds based on SaveLayer paints, so the drop shadow could be cut off.
214 SkPaint paint;
robertphillipsc4169122016-04-06 08:40:59 -0700215 paint.setImageFilter(SkDropShadowImageFilter::Make(
216 20, 0, 0, 0, SK_ColorBLACK,
217 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
218 nullptr));
Mike Klein271a0302014-09-23 15:28:38 -0400219
halcanary96fcdcc2015-08-27 07:41:13 -0700220 recorder.saveLayer(nullptr, &paint);
Mike Klein271a0302014-09-23 15:28:38 -0400221 recorder.clipRect(SkRect::MakeWH(20, 40));
222 recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
223 recorder.restore();
224
mtklein8e393bf2014-10-01 12:48:58 -0700225 // Under the original bug, the right edge value of the drawRect would be 20 less than asserted
226 // here because we intersected it with a clip that had not been adjusted for the drop shadow.
227 //
228 // The second bug showed up as adjusting the picture bounds (0,0,50,50) by the drop shadow too.
229 // The saveLayer, clipRect, and restore bounds were incorrectly (0,0,70,50).
mtklein40732b32015-10-24 07:45:47 -0700230 SkAutoTMalloc<SkRect> bounds(record.count());
231 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds);
232 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[0], SkRect::MakeLTRB(0, 0, 50, 50)));
233 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[1], SkRect::MakeLTRB(0, 0, 50, 50)));
234 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[2], SkRect::MakeLTRB(0, 0, 40, 40)));
235 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[3], SkRect::MakeLTRB(0, 0, 50, 50)));
Mike Klein271a0302014-09-23 15:28:38 -0400236}
piotaixr65151752014-10-16 11:58:39 -0700237
Mike Klein738b80d2018-05-04 13:51:11 -0400238// TODO This would be nice, but we can't get it right today.
239#if 0
robertphillips4d52afe2014-11-03 08:19:44 -0800240// When a saveLayer provides an explicit bound and has a complex paint (e.g., one that
241// affects transparent black), that bound should serve to shrink the area of the required
242// backing store.
243DEF_TEST(RecordDraw_SaveLayerBoundsAffectsClipBounds, r) {
244 SkRecord record;
245 SkRecorder recorder(&record, 50, 50);
246
247 SkPaint p;
reed374772b2016-10-05 17:33:02 -0700248 p.setBlendMode(SkBlendMode::kSrc);
robertphillips4d52afe2014-11-03 08:19:44 -0800249
mtklein40732b32015-10-24 07:45:47 -0700250 SkRect layerBounds = SkRect::MakeLTRB(10, 10, 40, 40);
251 recorder.saveLayer(&layerBounds, &p);
robertphillips4d52afe2014-11-03 08:19:44 -0800252 recorder.drawRect(SkRect::MakeLTRB(20, 20, 30, 30), SkPaint());
253 recorder.restore();
254
mtklein40732b32015-10-24 07:45:47 -0700255 SkAutoTMalloc<SkRect> bounds(record.count());
256 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds);
Cary Clarke041e312018-03-06 13:00:52 -0500257 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[0], SkRect::MakeLTRB(10, 10, 40, 40)));
258 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[1], SkRect::MakeLTRB(20, 20, 30, 30)));
259 REPORTER_ASSERT(r, sloppy_rect_eq(bounds[2], SkRect::MakeLTRB(10, 10, 40, 40)));
robertphillips4d52afe2014-11-03 08:19:44 -0800260}
Mike Klein738b80d2018-05-04 13:51:11 -0400261#endif
robertphillips4d52afe2014-11-03 08:19:44 -0800262
piotaixr65151752014-10-16 11:58:39 -0700263DEF_TEST(RecordDraw_drawImage, r){
264 class SkCanvasMock : public SkCanvas {
265 public:
mtkleinad8aa1d2014-11-11 18:52:02 -0800266 SkCanvasMock(int width, int height) : SkCanvas(width, height) {
piotaixr65151752014-10-16 11:58:39 -0700267 this->resetTestValues();
268 }
piotaixr65151752014-10-16 11:58:39 -0700269
reed41af9662015-01-05 07:49:08 -0800270 void onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
mtklein36352bf2015-03-25 18:17:31 -0700271 const SkPaint* paint) override {
piotaixr65151752014-10-16 11:58:39 -0700272 fDrawImageCalled = true;
273 }
274
reed41af9662015-01-05 07:49:08 -0800275 void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700276 const SkPaint* paint, SrcRectConstraint) override {
piotaixr65151752014-10-16 11:58:39 -0700277 fDrawImageRectCalled = true;
278 }
279
280 void resetTestValues() {
281 fDrawImageCalled = fDrawImageRectCalled = false;
282 }
283
284 bool fDrawImageCalled;
285 bool fDrawImageRectCalled;
piotaixr65151752014-10-16 11:58:39 -0700286 };
287
reede8f30622016-03-23 18:59:25 -0700288 auto surface(SkSurface::MakeRasterN32Premul(10, 10));
piotaixr65151752014-10-16 11:58:39 -0700289 surface->getCanvas()->clear(SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -0700290 sk_sp<SkImage> image(surface->makeImageSnapshot());
piotaixr65151752014-10-16 11:58:39 -0700291
292 SkCanvasMock canvas(10, 10);
293
294 {
295 SkRecord record;
296 SkRecorder recorder(&record, 10, 10);
297 recorder.drawImage(image, 0, 0);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400298 SkRecordDraw(record, &canvas, nullptr, nullptr, 0, nullptr, nullptr);
piotaixr65151752014-10-16 11:58:39 -0700299 }
300 REPORTER_ASSERT(r, canvas.fDrawImageCalled);
301 canvas.resetTestValues();
302
303 {
304 SkRecord record;
305 SkRecorder recorder(&record, 10, 10);
reede47829b2015-08-06 10:02:53 -0700306 recorder.drawImageRect(image, SkRect::MakeWH(10, 10), nullptr);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400307 SkRecordDraw(record, &canvas, nullptr, nullptr, 0, nullptr, nullptr);
piotaixr65151752014-10-16 11:58:39 -0700308 }
309 REPORTER_ASSERT(r, canvas.fDrawImageRectCalled);
310
311}