blob: f1c3a8b644328493e532c85d46eea192f58d7f38 [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 Kleinc11530e2014-06-24 11:29:06 -040012#include "SkDrawPictureCallback.h"
Mike Klein271a0302014-09-23 15:28:38 -040013#include "SkDropShadowImageFilter.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"
19
20static const int W = 1920, H = 1080;
21
Mike Kleinc11530e2014-06-24 11:29:06 -040022class JustOneDraw : public SkDrawPictureCallback {
23public:
24 JustOneDraw() : fCalls(0) {}
25
26 virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; }
27private:
28 int fCalls;
29};
30
31DEF_TEST(RecordDraw_Abort, r) {
32 // Record two commands.
33 SkRecord record;
34 SkRecorder recorder(&record, W, H);
35 recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint());
36 recorder.clipRect(SkRect::MakeWH(100, 200));
37
38 SkRecord rerecord;
39 SkRecorder canvas(&rerecord, W, H);
40
41 JustOneDraw callback;
mtklein5ad6ee12014-08-11 08:08:43 -070042 SkRecordDraw(record, &canvas, NULL/*bbh*/, &callback);
Mike Kleinc11530e2014-06-24 11:29:06 -040043
44 REPORTER_ASSERT(r, 3 == rerecord.count());
45 assert_type<SkRecords::Save> (r, rerecord, 0);
46 assert_type<SkRecords::DrawRect>(r, rerecord, 1);
47 assert_type<SkRecords::Restore> (r, rerecord, 2);
48}
49
50DEF_TEST(RecordDraw_Unbalanced, r) {
51 SkRecord record;
52 SkRecorder recorder(&record, W, H);
53 recorder.save(); // We won't balance this, but SkRecordDraw will for us.
54
55 SkRecord rerecord;
56 SkRecorder canvas(&rerecord, W, H);
mtklein5ad6ee12014-08-11 08:08:43 -070057 SkRecordDraw(record, &canvas, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040058
59 REPORTER_ASSERT(r, 4 == rerecord.count());
60 assert_type<SkRecords::Save> (r, rerecord, 0);
61 assert_type<SkRecords::Save> (r, rerecord, 1);
62 assert_type<SkRecords::Restore> (r, rerecord, 2);
63 assert_type<SkRecords::Restore> (r, rerecord, 3);
64}
65
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000066DEF_TEST(RecordDraw_SetMatrixClobber, r) {
67 // Set up an SkRecord that just scales by 2x,3x.
68 SkRecord scaleRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000069 SkRecorder scaleCanvas(&scaleRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000070 SkMatrix scale;
71 scale.setScale(2, 3);
72 scaleCanvas.setMatrix(scale);
73
74 // Set up an SkRecord with an initial +20, +20 translate.
75 SkRecord translateRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000076 SkRecorder translateCanvas(&translateRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000077 SkMatrix translate;
78 translate.setTranslate(20, 20);
79 translateCanvas.setMatrix(translate);
80
mtklein5ad6ee12014-08-11 08:08:43 -070081 SkRecordDraw(scaleRecord, &translateCanvas, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040082 REPORTER_ASSERT(r, 4 == translateRecord.count());
83 assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
84 assert_type<SkRecords::Save> (r, translateRecord, 1);
85 assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
86 assert_type<SkRecords::Restore> (r, translateRecord, 3);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000087
88 // When we look at translateRecord now, it should have its first +20,+20 translate,
89 // then a 2x,3x scale that's been concatted with that +20,+20 translate.
90 const SkRecords::SetMatrix* setMatrix;
91 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
92 REPORTER_ASSERT(r, setMatrix->matrix == translate);
93
Mike Kleinc11530e2014-06-24 11:29:06 -040094 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000095 SkMatrix expected = scale;
96 expected.postConcat(translate);
97 REPORTER_ASSERT(r, setMatrix->matrix == expected);
98}
mtkleina723b572014-08-15 11:49:49 -070099
100struct TestBBH : public SkBBoxHierarchy {
mtklein6bd41962014-10-02 07:41:56 -0700101 virtual void insert(unsigned opIndex, const SkRect& bounds, bool defer) SK_OVERRIDE {
102 Entry e = { opIndex, bounds };
103 fEntries.push(e);
mtkleina723b572014-08-15 11:49:49 -0700104 }
mtkleina723b572014-08-15 11:49:49 -0700105
mtklein6bd41962014-10-02 07:41:56 -0700106 virtual void search(const SkRect& query, SkTDArray<unsigned>* results) const SK_OVERRIDE {}
mtkleina723b572014-08-15 11:49:49 -0700107
108 struct Entry {
mtklein6bd41962014-10-02 07:41:56 -0700109 unsigned opIndex;
mtklein533eb782014-08-27 10:39:42 -0700110 SkRect bounds;
mtkleina723b572014-08-15 11:49:49 -0700111 };
mtklein6bd41962014-10-02 07:41:56 -0700112 SkTDArray<Entry> fEntries;
mtkleina723b572014-08-15 11:49:49 -0700113};
114
mtklein937c9c72014-09-02 15:19:48 -0700115// Like a==b, with a little slop recognizing that float equality can be weird.
116static bool sloppy_rect_eq(SkRect a, SkRect b) {
117 SkRect inset(a), outset(a);
118 inset.inset(1, 1);
119 outset.outset(1, 1);
120 return outset.contains(b) && !inset.contains(b);
121}
122
mtkleina723b572014-08-15 11:49:49 -0700123// This test is not meant to make total sense yet. It's testing the status quo
124// of SkRecordFillBounds(), which itself doesn't make total sense yet.
125DEF_TEST(RecordDraw_BBH, r) {
mtkleina723b572014-08-15 11:49:49 -0700126 SkRecord record;
mtkleina723b572014-08-15 11:49:49 -0700127 SkRecorder recorder(&record, W, H);
128 recorder.save();
129 recorder.clipRect(SkRect::MakeWH(400, 500));
130 recorder.scale(2, 2);
131 recorder.drawRect(SkRect::MakeWH(320, 240), SkPaint());
132 recorder.restore();
133
mtklein937c9c72014-09-02 15:19:48 -0700134 TestBBH bbh;
mtkleina723b572014-08-15 11:49:49 -0700135 SkRecordFillBounds(record, &bbh);
136
mtklein6bd41962014-10-02 07:41:56 -0700137 REPORTER_ASSERT(r, bbh.fEntries.count() == 5);
138 for (int i = 0; i < bbh.fEntries.count(); i++) {
139 REPORTER_ASSERT(r, bbh.fEntries[i].opIndex == (unsigned)i);
mtkleina723b572014-08-15 11:49:49 -0700140
mtklein6bd41962014-10-02 07:41:56 -0700141 REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bbh.fEntries[i].bounds));
mtkleina723b572014-08-15 11:49:49 -0700142 }
143}
mtklein00f30bd2014-09-02 12:03:31 -0700144
mtklein937c9c72014-09-02 15:19:48 -0700145// A regression test for crbug.com/409110.
146DEF_TEST(RecordDraw_TextBounds, r) {
147 SkRecord record;
148 SkRecorder recorder(&record, W, H);
149
150 // Two Chinese characters in UTF-8.
151 const char text[] = { '\xe6', '\xbc', '\xa2', '\xe5', '\xad', '\x97' };
152 const size_t bytes = SK_ARRAY_COUNT(text);
153
154 const SkScalar xpos[] = { 10, 20 };
155 recorder.drawPosTextH(text, bytes, xpos, 30, SkPaint());
156
157 const SkPoint pos[] = { {40, 50}, {60, 70} };
158 recorder.drawPosText(text, bytes, pos, SkPaint());
159
160 TestBBH bbh;
161 SkRecordFillBounds(record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700162 REPORTER_ASSERT(r, bbh.fEntries.count() == 2);
mtklein937c9c72014-09-02 15:19:48 -0700163
164 // We can make these next assertions confidently because SkRecordFillBounds
165 // builds its bounds by overestimating font metrics in a platform-independent way.
166 // If that changes, these tests will need to be more flexible.
mtklein6bd41962014-10-02 07:41:56 -0700167 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(-86, 6, 116, 54)));
168 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(-56, 26, 156, 94)));
mtklein937c9c72014-09-02 15:19:48 -0700169}
170
mtklein00f30bd2014-09-02 12:03:31 -0700171// Base test to ensure start/stop range is respected
172DEF_TEST(RecordDraw_PartialStartStop, r) {
173 static const int kWidth = 10, kHeight = 10;
174
175 SkRect r1 = { 0, 0, kWidth, kHeight };
176 SkRect r2 = { 0, 0, kWidth, kHeight/2 };
177 SkRect r3 = { 0, 0, kWidth/2, kHeight };
178 SkPaint p;
179
180 SkRecord record;
181 SkRecorder recorder(&record, kWidth, kHeight);
182 recorder.drawRect(r1, p);
183 recorder.drawRect(r2, p);
184 recorder.drawRect(r3, p);
185
186 SkRecord rerecord;
187 SkRecorder canvas(&rerecord, kWidth, kHeight);
robertphillips4815fe52014-09-16 10:32:43 -0700188 SkRecordPartialDraw(record, &canvas, r1, 1, 2, SkMatrix::I()); // replay just drawRect of r2
mtklein00f30bd2014-09-02 12:03:31 -0700189
190 REPORTER_ASSERT(r, 3 == rerecord.count());
191 assert_type<SkRecords::Save> (r, rerecord, 0);
192 assert_type<SkRecords::DrawRect> (r, rerecord, 1);
193 assert_type<SkRecords::Restore> (r, rerecord, 2);
194
195 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1);
196 REPORTER_ASSERT(r, drawRect->rect == r2);
197}
198
199// Check that clears are converted to drawRects
200DEF_TEST(RecordDraw_PartialClear, r) {
201 static const int kWidth = 10, kHeight = 10;
202
203 SkRect rect = { 0, 0, kWidth, kHeight };
204
205 SkRecord record;
206 SkRecorder recorder(&record, kWidth, kHeight);
207 recorder.clear(SK_ColorRED);
208
209 SkRecord rerecord;
210 SkRecorder canvas(&rerecord, kWidth, kHeight);
robertphillips4815fe52014-09-16 10:32:43 -0700211 SkRecordPartialDraw(record, &canvas, rect, 0, 1, SkMatrix::I()); // replay just the clear
mtklein00f30bd2014-09-02 12:03:31 -0700212
213 REPORTER_ASSERT(r, 3 == rerecord.count());
214 assert_type<SkRecords::Save> (r, rerecord, 0);
215 assert_type<SkRecords::DrawRect>(r, rerecord, 1);
216 assert_type<SkRecords::Restore> (r, rerecord, 2);
217
218 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1);
219 REPORTER_ASSERT(r, drawRect->rect == rect);
220 REPORTER_ASSERT(r, drawRect->paint.getColor() == SK_ColorRED);
221}
Mike Klein271a0302014-09-23 15:28:38 -0400222
223// A regression test for crbug.com/415468 and skbug.com/2957.
mtklein8e393bf2014-10-01 12:48:58 -0700224//
225// This also now serves as a regression test for crbug.com/418417. We used to adjust the
226// bounds for the saveLayer, clip, and restore to be greater than the bounds of the picture.
227// (We were applying the saveLayer paint to the bounds after restore, which makes no sense.)
Mike Klein271a0302014-09-23 15:28:38 -0400228DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds, r) {
229 SkRecord record;
230 SkRecorder recorder(&record, 50, 50);
231
232 // We draw a rectangle with a long drop shadow. We used to not update the clip
233 // bounds based on SaveLayer paints, so the drop shadow could be cut off.
234 SkPaint paint;
235 paint.setImageFilter(SkDropShadowImageFilter::Create(20, 0, 0, 0, SK_ColorBLACK))->unref();
236
237 recorder.saveLayer(NULL, &paint);
238 recorder.clipRect(SkRect::MakeWH(20, 40));
239 recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
240 recorder.restore();
241
mtklein8e393bf2014-10-01 12:48:58 -0700242 // Under the original bug, the right edge value of the drawRect would be 20 less than asserted
243 // here because we intersected it with a clip that had not been adjusted for the drop shadow.
244 //
245 // The second bug showed up as adjusting the picture bounds (0,0,50,50) by the drop shadow too.
246 // The saveLayer, clipRect, and restore bounds were incorrectly (0,0,70,50).
Mike Klein271a0302014-09-23 15:28:38 -0400247 TestBBH bbh;
248 SkRecordFillBounds(record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700249 REPORTER_ASSERT(r, bbh.fEntries.count() == 4);
250 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
251 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
252 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(0, 0, 40, 40)));
253 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[3].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
Mike Klein271a0302014-09-23 15:28:38 -0400254}