blob: 4341b870bf4d46a6433461023bba92cabcdda579 [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"
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +00009#include "RecordTestUtils.h"
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000010
kkinnunendc0f4082015-01-26 00:14:26 -080011#include "SkColorFilter.h"
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000012#include "SkRecord.h"
13#include "SkRecordOpts.h"
14#include "SkRecorder.h"
15#include "SkRecords.h"
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +000016#include "SkXfermode.h"
kkinnunendc0f4082015-01-26 00:14:26 -080017#include "SkPictureRecorder.h"
18#include "SkPictureImageFilter.h"
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +000019
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000020static const int W = 1920, H = 1080;
21
reed2ff1fce2014-12-11 07:07:37 -080022DEF_TEST(RecordOpts_NoopDraw, r) {
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000023 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000024 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000025
reed2ff1fce2014-12-11 07:07:37 -080026 recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint());
27 recorder.drawRect(SkRect::MakeWH(300, 300), SkPaint());
28 recorder.drawRect(SkRect::MakeWH(100, 100), SkPaint());
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000029
reed2ff1fce2014-12-11 07:07:37 -080030 record.replace<SkRecords::NoOp>(1); // NoOps should be allowed.
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000031
32 SkRecordNoopSaveRestores(&record);
33
reed2ff1fce2014-12-11 07:07:37 -080034 REPORTER_ASSERT(r, 2 == count_instances_of_type<SkRecords::DrawRect>(record));
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000035}
36
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000037DEF_TEST(RecordOpts_SingleNoopSaveRestore, r) {
38 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000039 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000040
41 recorder.save();
42 recorder.clipRect(SkRect::MakeWH(200, 200));
43 recorder.restore();
44
45 SkRecordNoopSaveRestores(&record);
mtkleinc6ad06a2015-08-19 09:51:00 -070046 for (int i = 0; i < 3; i++) {
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000047 assert_type<SkRecords::NoOp>(r, record, i);
48 }
49}
50
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000051DEF_TEST(RecordOpts_NoopSaveRestores, r) {
52 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000053 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000054
55 // The second pass will clean up this pair after the first pass noops all the innards.
56 recorder.save();
57 // A simple pointless pair of save/restore.
58 recorder.save();
59 recorder.restore();
60
61 // As long as we don't draw in there, everything is a noop.
62 recorder.save();
63 recorder.clipRect(SkRect::MakeWH(200, 200));
64 recorder.clipRect(SkRect::MakeWH(100, 100));
65 recorder.restore();
66 recorder.restore();
67
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000068 SkRecordNoopSaveRestores(&record);
mtkleinc6ad06a2015-08-19 09:51:00 -070069 for (int index = 0; index < record.count(); index++) {
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000070 assert_type<SkRecords::NoOp>(r, record, index);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000071 }
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000072}
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +000073
mtklein99d6a9e2014-09-10 16:08:27 -070074DEF_TEST(RecordOpts_SaveSaveLayerRestoreRestore, r) {
75 SkRecord record;
76 SkRecorder recorder(&record, W, H);
77
78 // A previous bug NoOp'd away the first 3 commands.
79 recorder.save();
halcanary96fcdcc2015-08-27 07:41:13 -070080 recorder.saveLayer(nullptr, nullptr);
mtklein99d6a9e2014-09-10 16:08:27 -070081 recorder.restore();
82 recorder.restore();
83
84 SkRecordNoopSaveRestores(&record);
reed2ff1fce2014-12-11 07:07:37 -080085 switch (record.count()) {
86 case 4:
87 assert_type<SkRecords::Save> (r, record, 0);
88 assert_type<SkRecords::SaveLayer>(r, record, 1);
89 assert_type<SkRecords::Restore> (r, record, 2);
90 assert_type<SkRecords::Restore> (r, record, 3);
91 break;
92 case 2:
93 assert_type<SkRecords::SaveLayer>(r, record, 0);
94 assert_type<SkRecords::Restore> (r, record, 1);
95 break;
96 case 0:
97 break;
98 default:
99 REPORTER_ASSERT(r, false);
100 }
mtklein99d6a9e2014-09-10 16:08:27 -0700101}
102
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000103static void assert_savelayer_restore(skiatest::Reporter* r,
104 SkRecord* record,
mtkleinc6ad06a2015-08-19 09:51:00 -0700105 int i,
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000106 bool shouldBeNoOped) {
107 SkRecordNoopSaveLayerDrawRestores(record);
108 if (shouldBeNoOped) {
109 assert_type<SkRecords::NoOp>(r, *record, i);
reedbfd5f172016-01-07 11:28:08 -0800110 assert_type<SkRecords::NoOp>(r, *record, i+1);
111 } else {
112 assert_type<SkRecords::SaveLayer>(r, *record, i);
113 assert_type<SkRecords::Restore>(r, *record, i+1);
114 }
115}
116
117static void assert_savelayer_draw_restore(skiatest::Reporter* r,
118 SkRecord* record,
119 int i,
120 bool shouldBeNoOped) {
121 SkRecordNoopSaveLayerDrawRestores(record);
122 if (shouldBeNoOped) {
123 assert_type<SkRecords::NoOp>(r, *record, i);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000124 assert_type<SkRecords::NoOp>(r, *record, i+2);
125 } else {
126 assert_type<SkRecords::SaveLayer>(r, *record, i);
127 assert_type<SkRecords::Restore>(r, *record, i+2);
128 }
129}
130
reedbfd5f172016-01-07 11:28:08 -0800131#include "SkBlurImageFilter.h"
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000132DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) {
133 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000134 SkRecorder recorder(&record, W, H);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000135
136 SkRect bounds = SkRect::MakeWH(100, 200);
137 SkRect draw = SkRect::MakeWH(50, 60);
138
kkinnunen678c1b02015-01-16 05:04:36 -0800139 SkPaint alphaOnlyLayerPaint, translucentLayerPaint, xfermodeLayerPaint;
140 alphaOnlyLayerPaint.setColor(0x03000000); // Only alpha.
141 translucentLayerPaint.setColor(0x03040506); // Not only alpha.
142 xfermodeLayerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); // Any effect will do.
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000143
kkinnunen678c1b02015-01-16 05:04:36 -0800144 SkPaint opaqueDrawPaint, translucentDrawPaint;
145 opaqueDrawPaint.setColor(0xFF020202); // Opaque.
146 translucentDrawPaint.setColor(0x0F020202); // Not opaque.
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000147
mtklein0c278972015-01-15 08:08:10 -0800148 // SaveLayer/Restore removed: No paint = no point.
halcanary96fcdcc2015-08-27 07:41:13 -0700149 recorder.saveLayer(nullptr, nullptr);
kkinnunen678c1b02015-01-16 05:04:36 -0800150 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000151 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800152 assert_savelayer_draw_restore(r, &record, 0, true);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000153
mtklein0c278972015-01-15 08:08:10 -0800154 // Bounds don't matter.
halcanary96fcdcc2015-08-27 07:41:13 -0700155 recorder.saveLayer(&bounds, nullptr);
kkinnunen678c1b02015-01-16 05:04:36 -0800156 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000157 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800158 assert_savelayer_draw_restore(r, &record, 3, true);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000159
160 // TODO(mtklein): test case with null draw paint
161
162 // No change: layer paint isn't alpha-only.
halcanary96fcdcc2015-08-27 07:41:13 -0700163 recorder.saveLayer(nullptr, &translucentLayerPaint);
kkinnunen678c1b02015-01-16 05:04:36 -0800164 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000165 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800166 assert_savelayer_draw_restore(r, &record, 6, false);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000167
168 // No change: layer paint has an effect.
halcanary96fcdcc2015-08-27 07:41:13 -0700169 recorder.saveLayer(nullptr, &xfermodeLayerPaint);
kkinnunen678c1b02015-01-16 05:04:36 -0800170 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000171 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800172 assert_savelayer_draw_restore(r, &record, 9, false);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000173
kkinnunen678c1b02015-01-16 05:04:36 -0800174 // SaveLayer/Restore removed: we can fold in the alpha!
halcanary96fcdcc2015-08-27 07:41:13 -0700175 recorder.saveLayer(nullptr, &alphaOnlyLayerPaint);
kkinnunen678c1b02015-01-16 05:04:36 -0800176 recorder.drawRect(draw, translucentDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000177 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800178 assert_savelayer_draw_restore(r, &record, 12, true);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000179
180 // SaveLayer/Restore removed: we can fold in the alpha!
halcanary96fcdcc2015-08-27 07:41:13 -0700181 recorder.saveLayer(nullptr, &alphaOnlyLayerPaint);
kkinnunen678c1b02015-01-16 05:04:36 -0800182 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000183 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800184 assert_savelayer_draw_restore(r, &record, 15, true);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000185
186 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, record, 16);
halcanary96fcdcc2015-08-27 07:41:13 -0700187 REPORTER_ASSERT(r, drawRect != nullptr);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000188 REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202);
reedbfd5f172016-01-07 11:28:08 -0800189
190 // saveLayer w/ backdrop should NOT go away
robertphillips6e7025a2016-04-04 04:31:25 -0700191 sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(3, 3, nullptr));
192 recorder.saveLayer({ nullptr, nullptr, filter.get(), 0});
reedbfd5f172016-01-07 11:28:08 -0800193 recorder.drawRect(draw, opaqueDrawPaint);
194 recorder.restore();
195 assert_savelayer_draw_restore(r, &record, 18, false);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000196}
kkinnunendc0f4082015-01-26 00:14:26 -0800197
198static void assert_merge_svg_opacity_and_filter_layers(skiatest::Reporter* r,
199 SkRecord* record,
mtkleinc6ad06a2015-08-19 09:51:00 -0700200 int i,
kkinnunendc0f4082015-01-26 00:14:26 -0800201 bool shouldBeNoOped) {
202 SkRecordMergeSvgOpacityAndFilterLayers(record);
203 if (shouldBeNoOped) {
204 assert_type<SkRecords::NoOp>(r, *record, i);
205 assert_type<SkRecords::NoOp>(r, *record, i + 6);
206 } else {
207 assert_type<SkRecords::SaveLayer>(r, *record, i);
208 assert_type<SkRecords::Restore>(r, *record, i + 6);
209 }
210}
211
212DEF_TEST(RecordOpts_MergeSvgOpacityAndFilterLayers, r) {
213 SkRecord record;
214 SkRecorder recorder(&record, W, H);
215
216 SkRect bounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(200));
217 SkRect clip = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(60));
218
219 SkPaint alphaOnlyLayerPaint;
220 alphaOnlyLayerPaint.setColor(0x03000000); // Only alpha.
221 SkPaint translucentLayerPaint;
222 translucentLayerPaint.setColor(0x03040506); // Not only alpha.
223 SkPaint xfermodePaint;
224 xfermodePaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
225 SkPaint colorFilterPaint;
226 colorFilterPaint.setColorFilter(
reedd053ce92016-03-22 10:17:23 -0700227 SkColorFilter::MakeModeFilter(SK_ColorLTGRAY, SkXfermode::kSrcIn_Mode));
kkinnunendc0f4082015-01-26 00:14:26 -0800228
229 SkPaint opaqueFilterLayerPaint;
230 opaqueFilterLayerPaint.setColor(0xFF020202); // Opaque.
231 SkPaint translucentFilterLayerPaint;
232 translucentFilterLayerPaint.setColor(0x0F020202); // Not opaque.
reedca2622b2016-03-18 07:25:55 -0700233 sk_sp<SkPicture> shape;
kkinnunendc0f4082015-01-26 00:14:26 -0800234 {
235 SkPictureRecorder recorder;
236 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(100), SkIntToScalar(100));
237 SkPaint shapePaint;
238 shapePaint.setColor(SK_ColorWHITE);
239 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50)), shapePaint);
reedca2622b2016-03-18 07:25:55 -0700240 shape = recorder.finishRecordingAsPicture();
kkinnunendc0f4082015-01-26 00:14:26 -0800241 }
robertphillips5ff17b12016-03-28 13:13:42 -0700242 translucentFilterLayerPaint.setImageFilter(SkPictureImageFilter::Make(shape));
kkinnunendc0f4082015-01-26 00:14:26 -0800243
244 int index = 0;
245
246 {
robertphillips6e7025a2016-04-04 04:31:25 -0700247 sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(3, 3, nullptr));
reedbfd5f172016-01-07 11:28:08 -0800248 // first (null) should be optimized, 2nd should not
249 SkImageFilter* filters[] = { nullptr, filter.get() };
250
kkinnunendc0f4082015-01-26 00:14:26 -0800251 // Any combination of these should cause the pattern to be optimized.
halcanary96fcdcc2015-08-27 07:41:13 -0700252 SkRect* firstBounds[] = { nullptr, &bounds };
253 SkPaint* firstPaints[] = { nullptr, &alphaOnlyLayerPaint };
254 SkRect* secondBounds[] = { nullptr, &bounds };
kkinnunendc0f4082015-01-26 00:14:26 -0800255 SkPaint* secondPaints[] = { &opaqueFilterLayerPaint, &translucentFilterLayerPaint };
256
reedbfd5f172016-01-07 11:28:08 -0800257 for (auto outerF : filters) {
258 bool outerNoOped = !outerF;
259 for (auto innerF : filters) {
260 for (size_t i = 0; i < SK_ARRAY_COUNT(firstBounds); ++ i) {
261 for (size_t j = 0; j < SK_ARRAY_COUNT(firstPaints); ++j) {
262 for (size_t k = 0; k < SK_ARRAY_COUNT(secondBounds); ++k) {
263 for (size_t m = 0; m < SK_ARRAY_COUNT(secondPaints); ++m) {
264 bool innerNoOped = !secondBounds[k] && !secondPaints[m] && !innerF;
265
266 recorder.saveLayer({firstBounds[i], firstPaints[j], outerF, 0});
267 recorder.save();
268 recorder.clipRect(clip);
269 recorder.saveLayer({secondBounds[k], secondPaints[m], innerF, 0});
270 recorder.restore();
271 recorder.restore();
272 recorder.restore();
273 assert_merge_svg_opacity_and_filter_layers(r, &record, index,
274 outerNoOped);
275 assert_savelayer_restore(r, &record, index + 3, innerNoOped);
276 index += 7;
277 }
278 }
kkinnunendc0f4082015-01-26 00:14:26 -0800279 }
280 }
281 }
282 }
283 }
284
285 // These should cause the pattern to stay unoptimized:
286 struct {
287 SkPaint* firstPaint;
288 SkPaint* secondPaint;
289 } noChangeTests[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700290 // No change: nullptr filter layer paint not implemented.
291 { &alphaOnlyLayerPaint, nullptr },
kkinnunendc0f4082015-01-26 00:14:26 -0800292 // No change: layer paint is not alpha-only.
293 { &translucentLayerPaint, &opaqueFilterLayerPaint },
294 // No change: layer paint has an xfereffect.
295 { &xfermodePaint, &opaqueFilterLayerPaint },
296 // No change: filter layer paint has an xfereffect.
297 { &alphaOnlyLayerPaint, &xfermodePaint },
298 // No change: layer paint has a color filter.
299 { &colorFilterPaint, &opaqueFilterLayerPaint },
300 // No change: filter layer paint has a color filter (until the optimization accounts for
301 // constant color draws that can filter the color).
302 { &alphaOnlyLayerPaint, &colorFilterPaint }
303 };
304
305 for (size_t i = 0; i < SK_ARRAY_COUNT(noChangeTests); ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700306 recorder.saveLayer(nullptr, noChangeTests[i].firstPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800307 recorder.save();
308 recorder.clipRect(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700309 recorder.saveLayer(nullptr, noChangeTests[i].secondPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800310 recorder.restore();
311 recorder.restore();
312 recorder.restore();
313 assert_merge_svg_opacity_and_filter_layers(r, &record, index, false);
314 index += 7;
315 }
316
317 // Test the folded alpha value.
halcanary96fcdcc2015-08-27 07:41:13 -0700318 recorder.saveLayer(nullptr, &alphaOnlyLayerPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800319 recorder.save();
320 recorder.clipRect(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700321 recorder.saveLayer(nullptr, &opaqueFilterLayerPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800322 recorder.restore();
323 recorder.restore();
324 recorder.restore();
325 assert_merge_svg_opacity_and_filter_layers(r, &record, index, true);
326
327 const SkRecords::SaveLayer* saveLayer = assert_type<SkRecords::SaveLayer>(r, record, index + 3);
halcanary96fcdcc2015-08-27 07:41:13 -0700328 REPORTER_ASSERT(r, saveLayer != nullptr);
kkinnunendc0f4082015-01-26 00:14:26 -0800329 REPORTER_ASSERT(r, saveLayer->paint->getColor() == 0x03020202);
330
331 index += 7;
332
333 // Test that currently we do not fold alphas for patterns without the clip. This is just not
334 // implemented.
halcanary96fcdcc2015-08-27 07:41:13 -0700335 recorder.saveLayer(nullptr, &alphaOnlyLayerPaint);
336 recorder.saveLayer(nullptr, &opaqueFilterLayerPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800337 recorder.restore();
338 recorder.restore();
339 SkRecordMergeSvgOpacityAndFilterLayers(&record);
340 assert_type<SkRecords::SaveLayer>(r, record, index);
341 assert_type<SkRecords::SaveLayer>(r, record, index + 1);
342 assert_type<SkRecords::Restore>(r, record, index + 2);
343 assert_type<SkRecords::Restore>(r, record, index + 3);
344 index += 4;
345}