blob: 09e774e5af7e77b1ba17722d05b15d150d9d34dd [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
Yuqian Li05a42962017-03-06 15:47:09 -050011#include "SkBlurImageFilter.h"
kkinnunendc0f4082015-01-26 00:14:26 -080012#include "SkColorFilter.h"
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000013#include "SkRecord.h"
14#include "SkRecordOpts.h"
15#include "SkRecorder.h"
16#include "SkRecords.h"
kkinnunendc0f4082015-01-26 00:14:26 -080017#include "SkPictureRecorder.h"
18#include "SkPictureImageFilter.h"
reed5a3c5282016-09-06 06:54:41 -070019#include "SkSurface.h"
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +000020
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000021static const int W = 1920, H = 1080;
22
reed2ff1fce2014-12-11 07:07:37 -080023DEF_TEST(RecordOpts_NoopDraw, r) {
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000024 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000025 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000026
reed2ff1fce2014-12-11 07:07:37 -080027 recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint());
28 recorder.drawRect(SkRect::MakeWH(300, 300), SkPaint());
29 recorder.drawRect(SkRect::MakeWH(100, 100), SkPaint());
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000030
reed2ff1fce2014-12-11 07:07:37 -080031 record.replace<SkRecords::NoOp>(1); // NoOps should be allowed.
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000032
33 SkRecordNoopSaveRestores(&record);
34
reed2ff1fce2014-12-11 07:07:37 -080035 REPORTER_ASSERT(r, 2 == count_instances_of_type<SkRecords::DrawRect>(record));
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000036}
37
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000038DEF_TEST(RecordOpts_SingleNoopSaveRestore, r) {
39 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000040 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000041
42 recorder.save();
43 recorder.clipRect(SkRect::MakeWH(200, 200));
44 recorder.restore();
45
46 SkRecordNoopSaveRestores(&record);
mtkleinc6ad06a2015-08-19 09:51:00 -070047 for (int i = 0; i < 3; i++) {
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000048 assert_type<SkRecords::NoOp>(r, record, i);
49 }
50}
51
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000052DEF_TEST(RecordOpts_NoopSaveRestores, r) {
53 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000054 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000055
56 // The second pass will clean up this pair after the first pass noops all the innards.
57 recorder.save();
58 // A simple pointless pair of save/restore.
59 recorder.save();
60 recorder.restore();
61
62 // As long as we don't draw in there, everything is a noop.
63 recorder.save();
64 recorder.clipRect(SkRect::MakeWH(200, 200));
65 recorder.clipRect(SkRect::MakeWH(100, 100));
66 recorder.restore();
67 recorder.restore();
68
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000069 SkRecordNoopSaveRestores(&record);
mtkleinc6ad06a2015-08-19 09:51:00 -070070 for (int index = 0; index < record.count(); index++) {
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000071 assert_type<SkRecords::NoOp>(r, record, index);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000072 }
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000073}
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +000074
mtklein99d6a9e2014-09-10 16:08:27 -070075DEF_TEST(RecordOpts_SaveSaveLayerRestoreRestore, r) {
76 SkRecord record;
77 SkRecorder recorder(&record, W, H);
78
79 // A previous bug NoOp'd away the first 3 commands.
80 recorder.save();
halcanary96fcdcc2015-08-27 07:41:13 -070081 recorder.saveLayer(nullptr, nullptr);
mtklein99d6a9e2014-09-10 16:08:27 -070082 recorder.restore();
83 recorder.restore();
84
85 SkRecordNoopSaveRestores(&record);
reed2ff1fce2014-12-11 07:07:37 -080086 switch (record.count()) {
87 case 4:
88 assert_type<SkRecords::Save> (r, record, 0);
89 assert_type<SkRecords::SaveLayer>(r, record, 1);
90 assert_type<SkRecords::Restore> (r, record, 2);
91 assert_type<SkRecords::Restore> (r, record, 3);
92 break;
93 case 2:
94 assert_type<SkRecords::SaveLayer>(r, record, 0);
95 assert_type<SkRecords::Restore> (r, record, 1);
96 break;
97 case 0:
98 break;
99 default:
100 REPORTER_ASSERT(r, false);
101 }
mtklein99d6a9e2014-09-10 16:08:27 -0700102}
103
Yuqian Li05a42962017-03-06 15:47:09 -0500104#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000105static void assert_savelayer_restore(skiatest::Reporter* r,
106 SkRecord* record,
mtkleinc6ad06a2015-08-19 09:51:00 -0700107 int i,
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000108 bool shouldBeNoOped) {
109 SkRecordNoopSaveLayerDrawRestores(record);
110 if (shouldBeNoOped) {
111 assert_type<SkRecords::NoOp>(r, *record, i);
reedbfd5f172016-01-07 11:28:08 -0800112 assert_type<SkRecords::NoOp>(r, *record, i+1);
113 } else {
114 assert_type<SkRecords::SaveLayer>(r, *record, i);
115 assert_type<SkRecords::Restore>(r, *record, i+1);
116 }
117}
118
119static void assert_savelayer_draw_restore(skiatest::Reporter* r,
120 SkRecord* record,
121 int i,
122 bool shouldBeNoOped) {
123 SkRecordNoopSaveLayerDrawRestores(record);
124 if (shouldBeNoOped) {
125 assert_type<SkRecords::NoOp>(r, *record, i);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000126 assert_type<SkRecords::NoOp>(r, *record, i+2);
127 } else {
128 assert_type<SkRecords::SaveLayer>(r, *record, i);
129 assert_type<SkRecords::Restore>(r, *record, i+2);
130 }
131}
132
133DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) {
134 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000135 SkRecorder recorder(&record, W, H);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000136
137 SkRect bounds = SkRect::MakeWH(100, 200);
138 SkRect draw = SkRect::MakeWH(50, 60);
139
kkinnunen678c1b02015-01-16 05:04:36 -0800140 SkPaint alphaOnlyLayerPaint, translucentLayerPaint, xfermodeLayerPaint;
141 alphaOnlyLayerPaint.setColor(0x03000000); // Only alpha.
142 translucentLayerPaint.setColor(0x03040506); // Not only alpha.
reed374772b2016-10-05 17:33:02 -0700143 xfermodeLayerPaint.setBlendMode(SkBlendMode::kDstIn); // Any effect will do.
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000144
kkinnunen678c1b02015-01-16 05:04:36 -0800145 SkPaint opaqueDrawPaint, translucentDrawPaint;
146 opaqueDrawPaint.setColor(0xFF020202); // Opaque.
147 translucentDrawPaint.setColor(0x0F020202); // Not opaque.
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000148
mtklein0c278972015-01-15 08:08:10 -0800149 // SaveLayer/Restore removed: No paint = no point.
halcanary96fcdcc2015-08-27 07:41:13 -0700150 recorder.saveLayer(nullptr, nullptr);
kkinnunen678c1b02015-01-16 05:04:36 -0800151 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000152 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800153 assert_savelayer_draw_restore(r, &record, 0, true);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000154
mtklein0c278972015-01-15 08:08:10 -0800155 // Bounds don't matter.
halcanary96fcdcc2015-08-27 07:41:13 -0700156 recorder.saveLayer(&bounds, nullptr);
kkinnunen678c1b02015-01-16 05:04:36 -0800157 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000158 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800159 assert_savelayer_draw_restore(r, &record, 3, true);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000160
161 // TODO(mtklein): test case with null draw paint
162
163 // No change: layer paint isn't alpha-only.
halcanary96fcdcc2015-08-27 07:41:13 -0700164 recorder.saveLayer(nullptr, &translucentLayerPaint);
kkinnunen678c1b02015-01-16 05:04:36 -0800165 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000166 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800167 assert_savelayer_draw_restore(r, &record, 6, false);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000168
169 // No change: layer paint has an effect.
halcanary96fcdcc2015-08-27 07:41:13 -0700170 recorder.saveLayer(nullptr, &xfermodeLayerPaint);
kkinnunen678c1b02015-01-16 05:04:36 -0800171 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000172 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800173 assert_savelayer_draw_restore(r, &record, 9, false);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000174
kkinnunen678c1b02015-01-16 05:04:36 -0800175 // SaveLayer/Restore removed: we can fold in the alpha!
halcanary96fcdcc2015-08-27 07:41:13 -0700176 recorder.saveLayer(nullptr, &alphaOnlyLayerPaint);
kkinnunen678c1b02015-01-16 05:04:36 -0800177 recorder.drawRect(draw, translucentDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000178 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800179 assert_savelayer_draw_restore(r, &record, 12, true);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000180
181 // SaveLayer/Restore removed: we can fold in the alpha!
halcanary96fcdcc2015-08-27 07:41:13 -0700182 recorder.saveLayer(nullptr, &alphaOnlyLayerPaint);
kkinnunen678c1b02015-01-16 05:04:36 -0800183 recorder.drawRect(draw, opaqueDrawPaint);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000184 recorder.restore();
reedbfd5f172016-01-07 11:28:08 -0800185 assert_savelayer_draw_restore(r, &record, 15, true);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000186
187 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, record, 16);
halcanary96fcdcc2015-08-27 07:41:13 -0700188 REPORTER_ASSERT(r, drawRect != nullptr);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000189 REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202);
reedbfd5f172016-01-07 11:28:08 -0800190
191 // saveLayer w/ backdrop should NOT go away
robertphillips6e7025a2016-04-04 04:31:25 -0700192 sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(3, 3, nullptr));
193 recorder.saveLayer({ nullptr, nullptr, filter.get(), 0});
reedbfd5f172016-01-07 11:28:08 -0800194 recorder.drawRect(draw, opaqueDrawPaint);
195 recorder.restore();
196 assert_savelayer_draw_restore(r, &record, 18, false);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000197}
Yuqian Li05a42962017-03-06 15:47:09 -0500198#endif
kkinnunendc0f4082015-01-26 00:14:26 -0800199
200static void assert_merge_svg_opacity_and_filter_layers(skiatest::Reporter* r,
201 SkRecord* record,
mtkleinc6ad06a2015-08-19 09:51:00 -0700202 int i,
kkinnunendc0f4082015-01-26 00:14:26 -0800203 bool shouldBeNoOped) {
204 SkRecordMergeSvgOpacityAndFilterLayers(record);
205 if (shouldBeNoOped) {
206 assert_type<SkRecords::NoOp>(r, *record, i);
207 assert_type<SkRecords::NoOp>(r, *record, i + 6);
208 } else {
209 assert_type<SkRecords::SaveLayer>(r, *record, i);
210 assert_type<SkRecords::Restore>(r, *record, i + 6);
211 }
212}
213
214DEF_TEST(RecordOpts_MergeSvgOpacityAndFilterLayers, r) {
215 SkRecord record;
216 SkRecorder recorder(&record, W, H);
217
218 SkRect bounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(200));
219 SkRect clip = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(60));
220
221 SkPaint alphaOnlyLayerPaint;
222 alphaOnlyLayerPaint.setColor(0x03000000); // Only alpha.
223 SkPaint translucentLayerPaint;
224 translucentLayerPaint.setColor(0x03040506); // Not only alpha.
225 SkPaint xfermodePaint;
reed374772b2016-10-05 17:33:02 -0700226 xfermodePaint.setBlendMode(SkBlendMode::kDstIn);
kkinnunendc0f4082015-01-26 00:14:26 -0800227 SkPaint colorFilterPaint;
228 colorFilterPaint.setColorFilter(
reed374772b2016-10-05 17:33:02 -0700229 SkColorFilter::MakeModeFilter(SK_ColorLTGRAY, SkBlendMode::kSrcIn));
kkinnunendc0f4082015-01-26 00:14:26 -0800230
231 SkPaint opaqueFilterLayerPaint;
232 opaqueFilterLayerPaint.setColor(0xFF020202); // Opaque.
233 SkPaint translucentFilterLayerPaint;
234 translucentFilterLayerPaint.setColor(0x0F020202); // Not opaque.
reedca2622b2016-03-18 07:25:55 -0700235 sk_sp<SkPicture> shape;
kkinnunendc0f4082015-01-26 00:14:26 -0800236 {
237 SkPictureRecorder recorder;
238 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(100), SkIntToScalar(100));
239 SkPaint shapePaint;
240 shapePaint.setColor(SK_ColorWHITE);
241 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50)), shapePaint);
reedca2622b2016-03-18 07:25:55 -0700242 shape = recorder.finishRecordingAsPicture();
kkinnunendc0f4082015-01-26 00:14:26 -0800243 }
robertphillips5ff17b12016-03-28 13:13:42 -0700244 translucentFilterLayerPaint.setImageFilter(SkPictureImageFilter::Make(shape));
kkinnunendc0f4082015-01-26 00:14:26 -0800245
246 int index = 0;
247
248 {
robertphillips6e7025a2016-04-04 04:31:25 -0700249 sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(3, 3, nullptr));
reedbfd5f172016-01-07 11:28:08 -0800250 // first (null) should be optimized, 2nd should not
251 SkImageFilter* filters[] = { nullptr, filter.get() };
252
kkinnunendc0f4082015-01-26 00:14:26 -0800253 // Any combination of these should cause the pattern to be optimized.
halcanary96fcdcc2015-08-27 07:41:13 -0700254 SkRect* firstBounds[] = { nullptr, &bounds };
255 SkPaint* firstPaints[] = { nullptr, &alphaOnlyLayerPaint };
256 SkRect* secondBounds[] = { nullptr, &bounds };
kkinnunendc0f4082015-01-26 00:14:26 -0800257 SkPaint* secondPaints[] = { &opaqueFilterLayerPaint, &translucentFilterLayerPaint };
258
reedbfd5f172016-01-07 11:28:08 -0800259 for (auto outerF : filters) {
260 bool outerNoOped = !outerF;
261 for (auto innerF : filters) {
262 for (size_t i = 0; i < SK_ARRAY_COUNT(firstBounds); ++ i) {
263 for (size_t j = 0; j < SK_ARRAY_COUNT(firstPaints); ++j) {
264 for (size_t k = 0; k < SK_ARRAY_COUNT(secondBounds); ++k) {
265 for (size_t m = 0; m < SK_ARRAY_COUNT(secondPaints); ++m) {
266 bool innerNoOped = !secondBounds[k] && !secondPaints[m] && !innerF;
267
268 recorder.saveLayer({firstBounds[i], firstPaints[j], outerF, 0});
269 recorder.save();
270 recorder.clipRect(clip);
271 recorder.saveLayer({secondBounds[k], secondPaints[m], innerF, 0});
272 recorder.restore();
273 recorder.restore();
274 recorder.restore();
275 assert_merge_svg_opacity_and_filter_layers(r, &record, index,
276 outerNoOped);
Mike Kleinf3b16d02017-03-06 20:02:32 -0500277 #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
reedbfd5f172016-01-07 11:28:08 -0800278 assert_savelayer_restore(r, &record, index + 3, innerNoOped);
Mike Kleinf3b16d02017-03-06 20:02:32 -0500279 #endif
reedbfd5f172016-01-07 11:28:08 -0800280 index += 7;
281 }
282 }
kkinnunendc0f4082015-01-26 00:14:26 -0800283 }
284 }
285 }
286 }
287 }
288
289 // These should cause the pattern to stay unoptimized:
290 struct {
291 SkPaint* firstPaint;
292 SkPaint* secondPaint;
293 } noChangeTests[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700294 // No change: nullptr filter layer paint not implemented.
295 { &alphaOnlyLayerPaint, nullptr },
kkinnunendc0f4082015-01-26 00:14:26 -0800296 // No change: layer paint is not alpha-only.
297 { &translucentLayerPaint, &opaqueFilterLayerPaint },
298 // No change: layer paint has an xfereffect.
299 { &xfermodePaint, &opaqueFilterLayerPaint },
300 // No change: filter layer paint has an xfereffect.
301 { &alphaOnlyLayerPaint, &xfermodePaint },
302 // No change: layer paint has a color filter.
303 { &colorFilterPaint, &opaqueFilterLayerPaint },
304 // No change: filter layer paint has a color filter (until the optimization accounts for
305 // constant color draws that can filter the color).
306 { &alphaOnlyLayerPaint, &colorFilterPaint }
307 };
308
309 for (size_t i = 0; i < SK_ARRAY_COUNT(noChangeTests); ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700310 recorder.saveLayer(nullptr, noChangeTests[i].firstPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800311 recorder.save();
312 recorder.clipRect(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700313 recorder.saveLayer(nullptr, noChangeTests[i].secondPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800314 recorder.restore();
315 recorder.restore();
316 recorder.restore();
317 assert_merge_svg_opacity_and_filter_layers(r, &record, index, false);
318 index += 7;
319 }
320
321 // Test the folded alpha value.
halcanary96fcdcc2015-08-27 07:41:13 -0700322 recorder.saveLayer(nullptr, &alphaOnlyLayerPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800323 recorder.save();
324 recorder.clipRect(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700325 recorder.saveLayer(nullptr, &opaqueFilterLayerPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800326 recorder.restore();
327 recorder.restore();
328 recorder.restore();
329 assert_merge_svg_opacity_and_filter_layers(r, &record, index, true);
330
331 const SkRecords::SaveLayer* saveLayer = assert_type<SkRecords::SaveLayer>(r, record, index + 3);
halcanary96fcdcc2015-08-27 07:41:13 -0700332 REPORTER_ASSERT(r, saveLayer != nullptr);
kkinnunendc0f4082015-01-26 00:14:26 -0800333 REPORTER_ASSERT(r, saveLayer->paint->getColor() == 0x03020202);
334
335 index += 7;
336
337 // Test that currently we do not fold alphas for patterns without the clip. This is just not
338 // implemented.
halcanary96fcdcc2015-08-27 07:41:13 -0700339 recorder.saveLayer(nullptr, &alphaOnlyLayerPaint);
340 recorder.saveLayer(nullptr, &opaqueFilterLayerPaint);
kkinnunendc0f4082015-01-26 00:14:26 -0800341 recorder.restore();
342 recorder.restore();
343 SkRecordMergeSvgOpacityAndFilterLayers(&record);
344 assert_type<SkRecords::SaveLayer>(r, record, index);
345 assert_type<SkRecords::SaveLayer>(r, record, index + 1);
346 assert_type<SkRecords::Restore>(r, record, index + 2);
347 assert_type<SkRecords::Restore>(r, record, index + 3);
348 index += 4;
349}
reed5a3c5282016-09-06 06:54:41 -0700350
351static void do_draw(SkCanvas* canvas, SkColor color, bool doLayer) {
352 canvas->drawColor(SK_ColorWHITE);
353
354 SkPaint p;
355 p.setColor(color);
356
357 if (doLayer) {
358 canvas->saveLayer(nullptr, nullptr);
reed374772b2016-10-05 17:33:02 -0700359 p.setBlendMode(SkBlendMode::kSrc);
reed5a3c5282016-09-06 06:54:41 -0700360 canvas->drawPaint(p);
361 canvas->restore();
362 } else {
363 canvas->drawPaint(p);
364 }
365}
366
367static bool is_equal(SkSurface* a, SkSurface* b) {
368 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
369 SkPMColor ca, cb;
370 a->readPixels(info, &ca, sizeof(SkPMColor), 0, 0);
371 b->readPixels(info, &cb, sizeof(SkPMColor), 0, 0);
372 return ca == cb;
373}
374
375// Test drawing w/ and w/o a simple layer (no bounds or paint), so see that drawing ops
376// that *should* draw the same in fact do.
377//
378// Perform this test twice : once directly, and once via a picture
379//
380static void do_savelayer_srcmode(skiatest::Reporter* r, SkColor color) {
381 for (int doPicture = 0; doPicture <= 1; ++doPicture) {
382 sk_sp<SkSurface> surf0 = SkSurface::MakeRasterN32Premul(10, 10);
383 sk_sp<SkSurface> surf1 = SkSurface::MakeRasterN32Premul(10, 10);
384 SkCanvas* c0 = surf0->getCanvas();
385 SkCanvas* c1 = surf1->getCanvas();
386
387 SkPictureRecorder rec0, rec1;
388 if (doPicture) {
389 c0 = rec0.beginRecording(10, 10);
390 c1 = rec1.beginRecording(10, 10);
391 }
392
393 do_draw(c0, color, false);
394 do_draw(c1, color, true);
395
396 if (doPicture) {
397 surf0->getCanvas()->drawPicture(rec0.finishRecordingAsPicture());
398 surf1->getCanvas()->drawPicture(rec1.finishRecordingAsPicture());
399 }
400
401 // we replicate the assert so we can see which line is reported if there is a failure
402 if (doPicture) {
403 REPORTER_ASSERT(r, is_equal(surf0.get(), surf1.get()));
404 } else {
405 REPORTER_ASSERT(r, is_equal(surf0.get(), surf1.get()));
406 }
407 }
408}
409
410DEF_TEST(savelayer_srcmode_opaque, r) {
411 do_savelayer_srcmode(r, SK_ColorRED);
412}
413
414DEF_TEST(savelayer_srcmode_alpha, r) {
415 do_savelayer_srcmode(r, 0x80FF0000);
416}
417