commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 0a98d87 | 2014-05-19 15:15:24 +0000 | [diff] [blame] | 9 | #include "RecordTestUtils.h" |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 10 | |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 12 | #include "SkRecord.h" |
| 13 | #include "SkRecordOpts.h" |
| 14 | #include "SkRecorder.h" |
| 15 | #include "SkRecords.h" |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 16 | #include "SkXfermode.h" |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 17 | #include "SkPictureRecorder.h" |
| 18 | #include "SkPictureImageFilter.h" |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 19 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 20 | static const int W = 1920, H = 1080; |
| 21 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 22 | DEF_TEST(RecordOpts_NoopDraw, r) { |
commit-bot@chromium.org | 467705a | 2014-05-07 17:17:48 +0000 | [diff] [blame] | 23 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 24 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | 467705a | 2014-05-07 17:17:48 +0000 | [diff] [blame] | 25 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 26 | 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.org | 467705a | 2014-05-07 17:17:48 +0000 | [diff] [blame] | 29 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 30 | record.replace<SkRecords::NoOp>(1); // NoOps should be allowed. |
commit-bot@chromium.org | 467705a | 2014-05-07 17:17:48 +0000 | [diff] [blame] | 31 | |
| 32 | SkRecordNoopSaveRestores(&record); |
| 33 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 34 | REPORTER_ASSERT(r, 2 == count_instances_of_type<SkRecords::DrawRect>(record)); |
commit-bot@chromium.org | 467705a | 2014-05-07 17:17:48 +0000 | [diff] [blame] | 35 | } |
| 36 | |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 37 | DEF_TEST(RecordOpts_SingleNoopSaveRestore, r) { |
| 38 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 39 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 40 | |
| 41 | recorder.save(); |
| 42 | recorder.clipRect(SkRect::MakeWH(200, 200)); |
| 43 | recorder.restore(); |
| 44 | |
| 45 | SkRecordNoopSaveRestores(&record); |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 46 | for (int i = 0; i < 3; i++) { |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 47 | assert_type<SkRecords::NoOp>(r, record, i); |
| 48 | } |
| 49 | } |
| 50 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 51 | DEF_TEST(RecordOpts_NoopSaveRestores, r) { |
| 52 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 53 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 54 | |
| 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.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 68 | SkRecordNoopSaveRestores(&record); |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 69 | for (int index = 0; index < record.count(); index++) { |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 70 | assert_type<SkRecords::NoOp>(r, record, index); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 71 | } |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 72 | } |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 73 | |
mtklein | 99d6a9e | 2014-09-10 16:08:27 -0700 | [diff] [blame] | 74 | DEF_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(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | recorder.saveLayer(nullptr, nullptr); |
mtklein | 99d6a9e | 2014-09-10 16:08:27 -0700 | [diff] [blame] | 81 | recorder.restore(); |
| 82 | recorder.restore(); |
| 83 | |
| 84 | SkRecordNoopSaveRestores(&record); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 85 | 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 | } |
mtklein | 99d6a9e | 2014-09-10 16:08:27 -0700 | [diff] [blame] | 101 | } |
| 102 | |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 103 | static void assert_savelayer_restore(skiatest::Reporter* r, |
| 104 | SkRecord* record, |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 105 | int i, |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 106 | bool shouldBeNoOped) { |
| 107 | SkRecordNoopSaveLayerDrawRestores(record); |
| 108 | if (shouldBeNoOped) { |
| 109 | assert_type<SkRecords::NoOp>(r, *record, i); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 110 | 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 | |
| 117 | static 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.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 124 | 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 | |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 131 | #include "SkBlurImageFilter.h" |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 132 | DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) { |
| 133 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 134 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 135 | |
| 136 | SkRect bounds = SkRect::MakeWH(100, 200); |
| 137 | SkRect draw = SkRect::MakeWH(50, 60); |
| 138 | |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 139 | 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.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 143 | |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 144 | SkPaint opaqueDrawPaint, translucentDrawPaint; |
| 145 | opaqueDrawPaint.setColor(0xFF020202); // Opaque. |
| 146 | translucentDrawPaint.setColor(0x0F020202); // Not opaque. |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 147 | |
mtklein | 0c27897 | 2015-01-15 08:08:10 -0800 | [diff] [blame] | 148 | // SaveLayer/Restore removed: No paint = no point. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | recorder.saveLayer(nullptr, nullptr); |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 150 | recorder.drawRect(draw, opaqueDrawPaint); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 151 | recorder.restore(); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 152 | assert_savelayer_draw_restore(r, &record, 0, true); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 153 | |
mtklein | 0c27897 | 2015-01-15 08:08:10 -0800 | [diff] [blame] | 154 | // Bounds don't matter. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 155 | recorder.saveLayer(&bounds, nullptr); |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 156 | recorder.drawRect(draw, opaqueDrawPaint); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 157 | recorder.restore(); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 158 | assert_savelayer_draw_restore(r, &record, 3, true); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 159 | |
| 160 | // TODO(mtklein): test case with null draw paint |
| 161 | |
| 162 | // No change: layer paint isn't alpha-only. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | recorder.saveLayer(nullptr, &translucentLayerPaint); |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 164 | recorder.drawRect(draw, opaqueDrawPaint); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 165 | recorder.restore(); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 166 | assert_savelayer_draw_restore(r, &record, 6, false); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 167 | |
| 168 | // No change: layer paint has an effect. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 169 | recorder.saveLayer(nullptr, &xfermodeLayerPaint); |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 170 | recorder.drawRect(draw, opaqueDrawPaint); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 171 | recorder.restore(); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 172 | assert_savelayer_draw_restore(r, &record, 9, false); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 173 | |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 174 | // SaveLayer/Restore removed: we can fold in the alpha! |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 175 | recorder.saveLayer(nullptr, &alphaOnlyLayerPaint); |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 176 | recorder.drawRect(draw, translucentDrawPaint); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 177 | recorder.restore(); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 178 | assert_savelayer_draw_restore(r, &record, 12, true); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 179 | |
| 180 | // SaveLayer/Restore removed: we can fold in the alpha! |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 181 | recorder.saveLayer(nullptr, &alphaOnlyLayerPaint); |
kkinnunen | 678c1b0 | 2015-01-16 05:04:36 -0800 | [diff] [blame] | 182 | recorder.drawRect(draw, opaqueDrawPaint); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 183 | recorder.restore(); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 184 | assert_savelayer_draw_restore(r, &record, 15, true); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 185 | |
| 186 | const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, record, 16); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 187 | REPORTER_ASSERT(r, drawRect != nullptr); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 188 | REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 189 | |
| 190 | // saveLayer w/ backdrop should NOT go away |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 191 | sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(3, 3, nullptr)); |
| 192 | recorder.saveLayer({ nullptr, nullptr, filter.get(), 0}); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 193 | recorder.drawRect(draw, opaqueDrawPaint); |
| 194 | recorder.restore(); |
| 195 | assert_savelayer_draw_restore(r, &record, 18, false); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 196 | } |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 197 | |
| 198 | static void assert_merge_svg_opacity_and_filter_layers(skiatest::Reporter* r, |
| 199 | SkRecord* record, |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 200 | int i, |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 201 | 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 | |
| 212 | DEF_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( |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 227 | SkColorFilter::MakeModeFilter(SK_ColorLTGRAY, SkXfermode::kSrcIn_Mode)); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 228 | |
| 229 | SkPaint opaqueFilterLayerPaint; |
| 230 | opaqueFilterLayerPaint.setColor(0xFF020202); // Opaque. |
| 231 | SkPaint translucentFilterLayerPaint; |
| 232 | translucentFilterLayerPaint.setColor(0x0F020202); // Not opaque. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 233 | sk_sp<SkPicture> shape; |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 234 | { |
| 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); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 240 | shape = recorder.finishRecordingAsPicture(); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 241 | } |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 242 | translucentFilterLayerPaint.setImageFilter(SkPictureImageFilter::Make(shape)); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 243 | |
| 244 | int index = 0; |
| 245 | |
| 246 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 247 | sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(3, 3, nullptr)); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 248 | // first (null) should be optimized, 2nd should not |
| 249 | SkImageFilter* filters[] = { nullptr, filter.get() }; |
| 250 | |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 251 | // Any combination of these should cause the pattern to be optimized. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 252 | SkRect* firstBounds[] = { nullptr, &bounds }; |
| 253 | SkPaint* firstPaints[] = { nullptr, &alphaOnlyLayerPaint }; |
| 254 | SkRect* secondBounds[] = { nullptr, &bounds }; |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 255 | SkPaint* secondPaints[] = { &opaqueFilterLayerPaint, &translucentFilterLayerPaint }; |
| 256 | |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 257 | 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 | } |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // These should cause the pattern to stay unoptimized: |
| 286 | struct { |
| 287 | SkPaint* firstPaint; |
| 288 | SkPaint* secondPaint; |
| 289 | } noChangeTests[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 290 | // No change: nullptr filter layer paint not implemented. |
| 291 | { &alphaOnlyLayerPaint, nullptr }, |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 292 | // 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) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 306 | recorder.saveLayer(nullptr, noChangeTests[i].firstPaint); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 307 | recorder.save(); |
| 308 | recorder.clipRect(clip); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 309 | recorder.saveLayer(nullptr, noChangeTests[i].secondPaint); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 310 | 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 318 | recorder.saveLayer(nullptr, &alphaOnlyLayerPaint); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 319 | recorder.save(); |
| 320 | recorder.clipRect(clip); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 321 | recorder.saveLayer(nullptr, &opaqueFilterLayerPaint); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 322 | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 328 | REPORTER_ASSERT(r, saveLayer != nullptr); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 329 | 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 335 | recorder.saveLayer(nullptr, &alphaOnlyLayerPaint); |
| 336 | recorder.saveLayer(nullptr, &opaqueFilterLayerPaint); |
kkinnunen | dc0f408 | 2015-01-26 00:14:26 -0800 | [diff] [blame] | 337 | 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 | } |