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 | |
| 11 | #include "SkRecord.h" |
| 12 | #include "SkRecordOpts.h" |
| 13 | #include "SkRecorder.h" |
| 14 | #include "SkRecords.h" |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 15 | #include "SkXfermode.h" |
| 16 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 17 | static const int W = 1920, H = 1080; |
| 18 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 19 | static void draw_pos_text(SkCanvas* canvas, const char* text, bool constantY) { |
| 20 | const size_t len = strlen(text); |
| 21 | SkAutoTMalloc<SkPoint> pos(len); |
| 22 | for (size_t i = 0; i < len; i++) { |
| 23 | pos[i].fX = (SkScalar)i; |
| 24 | pos[i].fY = constantY ? SK_Scalar1 : (SkScalar)i; |
| 25 | } |
| 26 | canvas->drawPosText(text, len, pos, SkPaint()); |
| 27 | } |
| 28 | |
| 29 | DEF_TEST(RecordOpts_StrengthReduction, r) { |
| 30 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 31 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 32 | |
| 33 | // We can convert a drawPosText into a drawPosTextH when all the Ys are the same. |
| 34 | draw_pos_text(&recorder, "This will be reduced to drawPosTextH.", true); |
| 35 | draw_pos_text(&recorder, "This cannot be reduced to drawPosTextH.", false); |
| 36 | |
| 37 | SkRecordReduceDrawPosTextStrength(&record); |
| 38 | |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 39 | assert_type<SkRecords::DrawPosTextH>(r, record, 0); |
| 40 | assert_type<SkRecords::DrawPosText>(r, record, 1); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 41 | } |
| 42 | |
commit-bot@chromium.org | 467705a | 2014-05-07 17:17:48 +0000 | [diff] [blame] | 43 | DEF_TEST(RecordOpts_NoopDrawSaveRestore, r) { |
| 44 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 45 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | 467705a | 2014-05-07 17:17:48 +0000 | [diff] [blame] | 46 | |
| 47 | // The save and restore are pointless if there's only draw commands in the middle. |
| 48 | recorder.save(); |
| 49 | recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint()); |
| 50 | recorder.drawRect(SkRect::MakeWH(300, 300), SkPaint()); |
| 51 | recorder.drawRect(SkRect::MakeWH(100, 100), SkPaint()); |
| 52 | recorder.restore(); |
| 53 | |
| 54 | record.replace<SkRecords::NoOp>(2); // NoOps should be allowed. |
| 55 | |
| 56 | SkRecordNoopSaveRestores(&record); |
| 57 | |
| 58 | assert_type<SkRecords::NoOp>(r, record, 0); |
| 59 | assert_type<SkRecords::DrawRect>(r, record, 1); |
| 60 | assert_type<SkRecords::NoOp>(r, record, 2); |
| 61 | assert_type<SkRecords::DrawRect>(r, record, 3); |
| 62 | assert_type<SkRecords::NoOp>(r, record, 4); |
| 63 | } |
| 64 | |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 65 | DEF_TEST(RecordOpts_SingleNoopSaveRestore, r) { |
| 66 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 67 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 68 | |
| 69 | recorder.save(); |
| 70 | recorder.clipRect(SkRect::MakeWH(200, 200)); |
| 71 | recorder.restore(); |
| 72 | |
| 73 | SkRecordNoopSaveRestores(&record); |
| 74 | for (unsigned i = 0; i < 3; i++) { |
| 75 | assert_type<SkRecords::NoOp>(r, record, i); |
| 76 | } |
| 77 | } |
| 78 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 79 | DEF_TEST(RecordOpts_NoopSaveRestores, r) { |
| 80 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 81 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 82 | |
| 83 | // The second pass will clean up this pair after the first pass noops all the innards. |
| 84 | recorder.save(); |
| 85 | // A simple pointless pair of save/restore. |
| 86 | recorder.save(); |
| 87 | recorder.restore(); |
| 88 | |
| 89 | // As long as we don't draw in there, everything is a noop. |
| 90 | recorder.save(); |
| 91 | recorder.clipRect(SkRect::MakeWH(200, 200)); |
| 92 | recorder.clipRect(SkRect::MakeWH(100, 100)); |
| 93 | recorder.restore(); |
| 94 | recorder.restore(); |
| 95 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 96 | SkRecordNoopSaveRestores(&record); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 97 | for (unsigned index = 0; index < 8; index++) { |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 98 | assert_type<SkRecords::NoOp>(r, record, index); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 99 | } |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 100 | } |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 101 | |
| 102 | static void assert_savelayer_restore(skiatest::Reporter* r, |
| 103 | SkRecord* record, |
| 104 | unsigned i, |
| 105 | bool shouldBeNoOped) { |
| 106 | SkRecordNoopSaveLayerDrawRestores(record); |
| 107 | if (shouldBeNoOped) { |
| 108 | assert_type<SkRecords::NoOp>(r, *record, i); |
| 109 | assert_type<SkRecords::NoOp>(r, *record, i+2); |
| 110 | } else { |
| 111 | assert_type<SkRecords::SaveLayer>(r, *record, i); |
| 112 | assert_type<SkRecords::Restore>(r, *record, i+2); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) { |
| 117 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 118 | SkRecorder recorder(&record, W, H); |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame] | 119 | |
| 120 | SkRect bounds = SkRect::MakeWH(100, 200); |
| 121 | SkRect draw = SkRect::MakeWH(50, 60); |
| 122 | |
| 123 | SkPaint goodLayerPaint, badLayerPaint, worseLayerPaint; |
| 124 | goodLayerPaint.setColor(0x03000000); // Only alpha. |
| 125 | badLayerPaint.setColor( 0x03040506); // Not only alpha. |
| 126 | worseLayerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); // Any effect will do. |
| 127 | |
| 128 | SkPaint goodDrawPaint, badDrawPaint; |
| 129 | goodDrawPaint.setColor(0xFF020202); // Opaque. |
| 130 | badDrawPaint.setColor( 0x0F020202); // Not opaque. |
| 131 | |
| 132 | // No change: optimization can't handle bounds. |
| 133 | recorder.saveLayer(&bounds, NULL); |
| 134 | recorder.drawRect(draw, goodDrawPaint); |
| 135 | recorder.restore(); |
| 136 | assert_savelayer_restore(r, &record, 0, false); |
| 137 | |
| 138 | // SaveLayer/Restore removed: no bounds + no paint = no point. |
| 139 | recorder.saveLayer(NULL, NULL); |
| 140 | recorder.drawRect(draw, goodDrawPaint); |
| 141 | recorder.restore(); |
| 142 | assert_savelayer_restore(r, &record, 3, true); |
| 143 | |
| 144 | // TODO(mtklein): test case with null draw paint |
| 145 | |
| 146 | // No change: layer paint isn't alpha-only. |
| 147 | recorder.saveLayer(NULL, &badLayerPaint); |
| 148 | recorder.drawRect(draw, goodDrawPaint); |
| 149 | recorder.restore(); |
| 150 | assert_savelayer_restore(r, &record, 6, false); |
| 151 | |
| 152 | // No change: layer paint has an effect. |
| 153 | recorder.saveLayer(NULL, &worseLayerPaint); |
| 154 | recorder.drawRect(draw, goodDrawPaint); |
| 155 | recorder.restore(); |
| 156 | assert_savelayer_restore(r, &record, 9, false); |
| 157 | |
| 158 | // No change: draw paint isn't opaque. |
| 159 | recorder.saveLayer(NULL, &goodLayerPaint); |
| 160 | recorder.drawRect(draw, badDrawPaint); |
| 161 | recorder.restore(); |
| 162 | assert_savelayer_restore(r, &record, 12, false); |
| 163 | |
| 164 | // SaveLayer/Restore removed: we can fold in the alpha! |
| 165 | recorder.saveLayer(NULL, &goodLayerPaint); |
| 166 | recorder.drawRect(draw, goodDrawPaint); |
| 167 | recorder.restore(); |
| 168 | assert_savelayer_restore(r, &record, 15, true); |
| 169 | |
| 170 | const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, record, 16); |
| 171 | REPORTER_ASSERT(r, drawRect != NULL); |
| 172 | REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202); |
| 173 | } |