blob: 11b835b0ca0121ba53a931cfd17c118a1d077fcd [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"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000013#include "SkRecord.h"
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000014#include "SkRecordOpts.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000015#include "SkRecordDraw.h"
16#include "SkRecorder.h"
17#include "SkRecords.h"
18
19static const int W = 1920, H = 1080;
20
Mike Kleinc11530e2014-06-24 11:29:06 -040021class JustOneDraw : public SkDrawPictureCallback {
22public:
23 JustOneDraw() : fCalls(0) {}
24
25 virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; }
26private:
27 int fCalls;
28};
29
30DEF_TEST(RecordDraw_Abort, r) {
31 // Record two commands.
32 SkRecord record;
33 SkRecorder recorder(&record, W, H);
34 recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint());
35 recorder.clipRect(SkRect::MakeWH(100, 200));
36
37 SkRecord rerecord;
38 SkRecorder canvas(&rerecord, W, H);
39
40 JustOneDraw callback;
mtklein5ad6ee12014-08-11 08:08:43 -070041 SkRecordDraw(record, &canvas, NULL/*bbh*/, &callback);
Mike Kleinc11530e2014-06-24 11:29:06 -040042
43 REPORTER_ASSERT(r, 3 == rerecord.count());
44 assert_type<SkRecords::Save> (r, rerecord, 0);
45 assert_type<SkRecords::DrawRect>(r, rerecord, 1);
46 assert_type<SkRecords::Restore> (r, rerecord, 2);
47}
48
49DEF_TEST(RecordDraw_Unbalanced, r) {
50 SkRecord record;
51 SkRecorder recorder(&record, W, H);
52 recorder.save(); // We won't balance this, but SkRecordDraw will for us.
53
54 SkRecord rerecord;
55 SkRecorder canvas(&rerecord, W, H);
mtklein5ad6ee12014-08-11 08:08:43 -070056 SkRecordDraw(record, &canvas, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040057
58 REPORTER_ASSERT(r, 4 == rerecord.count());
59 assert_type<SkRecords::Save> (r, rerecord, 0);
60 assert_type<SkRecords::Save> (r, rerecord, 1);
61 assert_type<SkRecords::Restore> (r, rerecord, 2);
62 assert_type<SkRecords::Restore> (r, rerecord, 3);
63}
64
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000065DEF_TEST(RecordDraw_SetMatrixClobber, r) {
66 // Set up an SkRecord that just scales by 2x,3x.
67 SkRecord scaleRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000068 SkRecorder scaleCanvas(&scaleRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000069 SkMatrix scale;
70 scale.setScale(2, 3);
71 scaleCanvas.setMatrix(scale);
72
73 // Set up an SkRecord with an initial +20, +20 translate.
74 SkRecord translateRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000075 SkRecorder translateCanvas(&translateRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000076 SkMatrix translate;
77 translate.setTranslate(20, 20);
78 translateCanvas.setMatrix(translate);
79
mtklein5ad6ee12014-08-11 08:08:43 -070080 SkRecordDraw(scaleRecord, &translateCanvas, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040081 REPORTER_ASSERT(r, 4 == translateRecord.count());
82 assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
83 assert_type<SkRecords::Save> (r, translateRecord, 1);
84 assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
85 assert_type<SkRecords::Restore> (r, translateRecord, 3);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000086
87 // When we look at translateRecord now, it should have its first +20,+20 translate,
88 // then a 2x,3x scale that's been concatted with that +20,+20 translate.
89 const SkRecords::SetMatrix* setMatrix;
90 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
91 REPORTER_ASSERT(r, setMatrix->matrix == translate);
92
Mike Kleinc11530e2014-06-24 11:29:06 -040093 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000094 SkMatrix expected = scale;
95 expected.postConcat(translate);
96 REPORTER_ASSERT(r, setMatrix->matrix == expected);
97}
mtkleina723b572014-08-15 11:49:49 -070098
99struct TestBBH : public SkBBoxHierarchy {
mtklein533eb782014-08-27 10:39:42 -0700100 virtual void insert(void* data, const SkRect& bounds, bool defer) SK_OVERRIDE {
mtkleina723b572014-08-15 11:49:49 -0700101 Entry e = { (uintptr_t)data, bounds };
102 entries.push(e);
103 }
104 virtual int getCount() const SK_OVERRIDE { return entries.count(); }
105
106 virtual void flushDeferredInserts() SK_OVERRIDE {}
107
mtklein533eb782014-08-27 10:39:42 -0700108 virtual void search(const SkRect& query, SkTDArray<void*>* results) const SK_OVERRIDE {}
mtkleina723b572014-08-15 11:49:49 -0700109 virtual void clear() SK_OVERRIDE {}
110 virtual void rewindInserts() SK_OVERRIDE {}
111 virtual int getDepth() const SK_OVERRIDE { return -1; }
112
113 struct Entry {
114 uintptr_t data;
mtklein533eb782014-08-27 10:39:42 -0700115 SkRect bounds;
mtkleina723b572014-08-15 11:49:49 -0700116 };
117 SkTDArray<Entry> entries;
118};
119
120// This test is not meant to make total sense yet. It's testing the status quo
121// of SkRecordFillBounds(), which itself doesn't make total sense yet.
122DEF_TEST(RecordDraw_BBH, r) {
123 TestBBH bbh;
124
125 SkRecord record;
126
127 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
134 SkRecordFillBounds(record, &bbh);
135
136 REPORTER_ASSERT(r, bbh.entries.count() == 5);
137 for (int i = 0; i < bbh.entries.count(); i++) {
138 REPORTER_ASSERT(r, bbh.entries[i].data == (uintptr_t)i);
139
mtklein90e84572014-08-28 05:14:31 -0700140 // We'd like to assert bounds == SkRect::MakeWH(400, 480).
141 // But we allow a little slop in recognition that float equality can be weird.
142 REPORTER_ASSERT(r, SkRect::MakeLTRB(-1, -1, 401, 481).contains(bbh.entries[i].bounds));
143 REPORTER_ASSERT(r, !SkRect::MakeLTRB(+1, +1, 399, 479).contains(bbh.entries[i].bounds));
mtkleina723b572014-08-15 11:49:49 -0700144 }
145}
mtklein00f30bd2014-09-02 12:03:31 -0700146
147// Base test to ensure start/stop range is respected
148DEF_TEST(RecordDraw_PartialStartStop, r) {
149 static const int kWidth = 10, kHeight = 10;
150
151 SkRect r1 = { 0, 0, kWidth, kHeight };
152 SkRect r2 = { 0, 0, kWidth, kHeight/2 };
153 SkRect r3 = { 0, 0, kWidth/2, kHeight };
154 SkPaint p;
155
156 SkRecord record;
157 SkRecorder recorder(&record, kWidth, kHeight);
158 recorder.drawRect(r1, p);
159 recorder.drawRect(r2, p);
160 recorder.drawRect(r3, p);
161
162 SkRecord rerecord;
163 SkRecorder canvas(&rerecord, kWidth, kHeight);
164 SkRecordPartialDraw(record, &canvas, r1, 1, 2); // replay just drawRect of r2
165
166 REPORTER_ASSERT(r, 3 == rerecord.count());
167 assert_type<SkRecords::Save> (r, rerecord, 0);
168 assert_type<SkRecords::DrawRect> (r, rerecord, 1);
169 assert_type<SkRecords::Restore> (r, rerecord, 2);
170
171 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1);
172 REPORTER_ASSERT(r, drawRect->rect == r2);
173}
174
175// Check that clears are converted to drawRects
176DEF_TEST(RecordDraw_PartialClear, r) {
177 static const int kWidth = 10, kHeight = 10;
178
179 SkRect rect = { 0, 0, kWidth, kHeight };
180
181 SkRecord record;
182 SkRecorder recorder(&record, kWidth, kHeight);
183 recorder.clear(SK_ColorRED);
184
185 SkRecord rerecord;
186 SkRecorder canvas(&rerecord, kWidth, kHeight);
187 SkRecordPartialDraw(record, &canvas, rect, 0, 1); // replay just the clear
188
189 REPORTER_ASSERT(r, 3 == rerecord.count());
190 assert_type<SkRecords::Save> (r, rerecord, 0);
191 assert_type<SkRecords::DrawRect>(r, rerecord, 1);
192 assert_type<SkRecords::Restore> (r, rerecord, 2);
193
194 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1);
195 REPORTER_ASSERT(r, drawRect->rect == rect);
196 REPORTER_ASSERT(r, drawRect->paint.getColor() == SK_ColorRED);
197}