blob: 33142c8a3d587f50a5419db045431520bf347bd4 [file] [log] [blame]
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +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
8#include "Test.h"
9
10#include "SkRecord.h"
11#include "SkRecordOpts.h"
12#include "SkRecorder.h"
13#include "SkRecords.h"
14
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +000015#include "SkXfermode.h"
16
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000017static const int W = 1920, H = 1080;
18
19// If the command we're reading is a U, set ptr to it, otherwise set it to NULL.
20template <typename U>
21struct ReadAs {
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000022 explicit ReadAs(const U** ptr) : ptr(ptr), type(SkRecords::Type(~0)) {}
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000023
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000024 const U** ptr;
25 SkRecords::Type type;
26
27 void operator()(const U& r) { *ptr = &r; type = U::kType; }
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000028
29 template <typename T>
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000030 void operator()(const T&) { *ptr = NULL; type = U::kType; }
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000031};
32
33// Assert that the ith command in record is of type T, and return it.
34template <typename T>
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000035static const T* assert_type(skiatest::Reporter* r, const SkRecord& record, unsigned index) {
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000036 const T* ptr = NULL;
37 ReadAs<T> reader(&ptr);
38 record.visit(index, reader);
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000039 REPORTER_ASSERT(r, T::kType == reader.type);
40 REPORTER_ASSERT(r, ptr != NULL);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000041 return ptr;
42}
43
44DEF_TEST(RecordOpts_Culling, r) {
45 SkRecord record;
46 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
47
48 recorder.drawRect(SkRect::MakeWH(1000, 10000), SkPaint());
49
50 recorder.pushCull(SkRect::MakeWH(100, 100));
51 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
52 recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint());
53 recorder.pushCull(SkRect::MakeWH(5, 5));
54 recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint());
55 recorder.popCull();
56 recorder.popCull();
57
58 SkRecordAnnotateCullingPairs(&record);
59
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000060 REPORTER_ASSERT(r, 6 == assert_type<SkRecords::PairedPushCull>(r, record, 1)->skip);
61 REPORTER_ASSERT(r, 2 == assert_type<SkRecords::PairedPushCull>(r, record, 4)->skip);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000062}
63
64static void draw_pos_text(SkCanvas* canvas, const char* text, bool constantY) {
65 const size_t len = strlen(text);
66 SkAutoTMalloc<SkPoint> pos(len);
67 for (size_t i = 0; i < len; i++) {
68 pos[i].fX = (SkScalar)i;
69 pos[i].fY = constantY ? SK_Scalar1 : (SkScalar)i;
70 }
71 canvas->drawPosText(text, len, pos, SkPaint());
72}
73
74DEF_TEST(RecordOpts_StrengthReduction, r) {
75 SkRecord record;
76 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
77
78 // We can convert a drawPosText into a drawPosTextH when all the Ys are the same.
79 draw_pos_text(&recorder, "This will be reduced to drawPosTextH.", true);
80 draw_pos_text(&recorder, "This cannot be reduced to drawPosTextH.", false);
81
82 SkRecordReduceDrawPosTextStrength(&record);
83
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000084 assert_type<SkRecords::DrawPosTextH>(r, record, 0);
85 assert_type<SkRecords::DrawPosText>(r, record, 1);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000086}
87
88DEF_TEST(RecordOpts_TextBounding, r) {
89 SkRecord record;
90 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
91
92 // First, get a drawPosTextH. Here's a handy way. Its text size will be the default (12).
93 draw_pos_text(&recorder, "This will be reduced to drawPosTextH.", true);
94 SkRecordReduceDrawPosTextStrength(&record);
95
96 const SkRecords::DrawPosTextH* original =
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000097 assert_type<SkRecords::DrawPosTextH>(r, record, 0);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000098
99 // This should wrap the original DrawPosTextH with minY and maxY.
100 SkRecordBoundDrawPosTextH(&record);
101
102 const SkRecords::BoundedDrawPosTextH* bounded =
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +0000103 assert_type<SkRecords::BoundedDrawPosTextH>(r, record, 0);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +0000104
105 const SkPaint defaults;
106 REPORTER_ASSERT(r, bounded->base == original);
107 REPORTER_ASSERT(r, bounded->minY <= SK_Scalar1 - defaults.getTextSize());
108 REPORTER_ASSERT(r, bounded->maxY >= SK_Scalar1 + defaults.getTextSize());
109}
110
commit-bot@chromium.org467705a2014-05-07 17:17:48 +0000111DEF_TEST(RecordOpts_NoopDrawSaveRestore, r) {
112 SkRecord record;
113 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
114
115 // The save and restore are pointless if there's only draw commands in the middle.
116 recorder.save();
117 recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint());
118 recorder.drawRect(SkRect::MakeWH(300, 300), SkPaint());
119 recorder.drawRect(SkRect::MakeWH(100, 100), SkPaint());
120 recorder.restore();
121
122 record.replace<SkRecords::NoOp>(2); // NoOps should be allowed.
123
124 SkRecordNoopSaveRestores(&record);
125
126 assert_type<SkRecords::NoOp>(r, record, 0);
127 assert_type<SkRecords::DrawRect>(r, record, 1);
128 assert_type<SkRecords::NoOp>(r, record, 2);
129 assert_type<SkRecords::DrawRect>(r, record, 3);
130 assert_type<SkRecords::NoOp>(r, record, 4);
131}
132
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +0000133DEF_TEST(RecordOpts_SingleNoopSaveRestore, r) {
134 SkRecord record;
135 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
136
137 recorder.save();
138 recorder.clipRect(SkRect::MakeWH(200, 200));
139 recorder.restore();
140
141 SkRecordNoopSaveRestores(&record);
142 for (unsigned i = 0; i < 3; i++) {
143 assert_type<SkRecords::NoOp>(r, record, i);
144 }
145}
146
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +0000147DEF_TEST(RecordOpts_NoopSaveRestores, r) {
148 SkRecord record;
149 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
150
151 // The second pass will clean up this pair after the first pass noops all the innards.
152 recorder.save();
153 // A simple pointless pair of save/restore.
154 recorder.save();
155 recorder.restore();
156
157 // As long as we don't draw in there, everything is a noop.
158 recorder.save();
159 recorder.clipRect(SkRect::MakeWH(200, 200));
160 recorder.clipRect(SkRect::MakeWH(100, 100));
161 recorder.restore();
162 recorder.restore();
163
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +0000164 SkRecordNoopSaveRestores(&record);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +0000165 for (unsigned index = 0; index < 8; index++) {
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +0000166 assert_type<SkRecords::NoOp>(r, record, index);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +0000167 }
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +0000168}
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000169
170static void assert_savelayer_restore(skiatest::Reporter* r,
171 SkRecord* record,
172 unsigned i,
173 bool shouldBeNoOped) {
174 SkRecordNoopSaveLayerDrawRestores(record);
175 if (shouldBeNoOped) {
176 assert_type<SkRecords::NoOp>(r, *record, i);
177 assert_type<SkRecords::NoOp>(r, *record, i+2);
178 } else {
179 assert_type<SkRecords::SaveLayer>(r, *record, i);
180 assert_type<SkRecords::Restore>(r, *record, i+2);
181 }
182}
183
184DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) {
185 SkRecord record;
186 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
187
188 SkRect bounds = SkRect::MakeWH(100, 200);
189 SkRect draw = SkRect::MakeWH(50, 60);
190
191 SkPaint goodLayerPaint, badLayerPaint, worseLayerPaint;
192 goodLayerPaint.setColor(0x03000000); // Only alpha.
193 badLayerPaint.setColor( 0x03040506); // Not only alpha.
194 worseLayerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); // Any effect will do.
195
196 SkPaint goodDrawPaint, badDrawPaint;
197 goodDrawPaint.setColor(0xFF020202); // Opaque.
198 badDrawPaint.setColor( 0x0F020202); // Not opaque.
199
200 // No change: optimization can't handle bounds.
201 recorder.saveLayer(&bounds, NULL);
202 recorder.drawRect(draw, goodDrawPaint);
203 recorder.restore();
204 assert_savelayer_restore(r, &record, 0, false);
205
206 // SaveLayer/Restore removed: no bounds + no paint = no point.
207 recorder.saveLayer(NULL, NULL);
208 recorder.drawRect(draw, goodDrawPaint);
209 recorder.restore();
210 assert_savelayer_restore(r, &record, 3, true);
211
212 // TODO(mtklein): test case with null draw paint
213
214 // No change: layer paint isn't alpha-only.
215 recorder.saveLayer(NULL, &badLayerPaint);
216 recorder.drawRect(draw, goodDrawPaint);
217 recorder.restore();
218 assert_savelayer_restore(r, &record, 6, false);
219
220 // No change: layer paint has an effect.
221 recorder.saveLayer(NULL, &worseLayerPaint);
222 recorder.drawRect(draw, goodDrawPaint);
223 recorder.restore();
224 assert_savelayer_restore(r, &record, 9, false);
225
226 // No change: draw paint isn't opaque.
227 recorder.saveLayer(NULL, &goodLayerPaint);
228 recorder.drawRect(draw, badDrawPaint);
229 recorder.restore();
230 assert_savelayer_restore(r, &record, 12, false);
231
232 // SaveLayer/Restore removed: we can fold in the alpha!
233 recorder.saveLayer(NULL, &goodLayerPaint);
234 recorder.drawRect(draw, goodDrawPaint);
235 recorder.restore();
236 assert_savelayer_restore(r, &record, 15, true);
237
238 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, record, 16);
239 REPORTER_ASSERT(r, drawRect != NULL);
240 REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202);
241}