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" |
| 9 | |
| 10 | #include "SkRecord.h" |
| 11 | #include "SkRecordOpts.h" |
| 12 | #include "SkRecorder.h" |
| 13 | #include "SkRecords.h" |
| 14 | |
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 | |
| 19 | // If the command we're reading is a U, set ptr to it, otherwise set it to NULL. |
| 20 | template <typename U> |
| 21 | struct ReadAs { |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 22 | explicit ReadAs(const U** ptr) : ptr(ptr), type(SkRecords::Type(~0)) {} |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 23 | |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 24 | const U** ptr; |
| 25 | SkRecords::Type type; |
| 26 | |
| 27 | void operator()(const U& r) { *ptr = &r; type = U::kType; } |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 28 | |
| 29 | template <typename T> |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 30 | void operator()(const T&) { *ptr = NULL; type = U::kType; } |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | // Assert that the ith command in record is of type T, and return it. |
| 34 | template <typename T> |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 35 | static const T* assert_type(skiatest::Reporter* r, const SkRecord& record, unsigned index) { |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 36 | const T* ptr = NULL; |
| 37 | ReadAs<T> reader(&ptr); |
| 38 | record.visit(index, reader); |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 39 | REPORTER_ASSERT(r, T::kType == reader.type); |
| 40 | REPORTER_ASSERT(r, ptr != NULL); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 41 | return ptr; |
| 42 | } |
| 43 | |
| 44 | DEF_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.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 60 | 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.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static 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 | |
| 74 | DEF_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.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 84 | assert_type<SkRecords::DrawPosTextH>(r, record, 0); |
| 85 | assert_type<SkRecords::DrawPosText>(r, record, 1); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | DEF_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.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 97 | assert_type<SkRecords::DrawPosTextH>(r, record, 0); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 98 | |
| 99 | // This should wrap the original DrawPosTextH with minY and maxY. |
| 100 | SkRecordBoundDrawPosTextH(&record); |
| 101 | |
| 102 | const SkRecords::BoundedDrawPosTextH* bounded = |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 103 | assert_type<SkRecords::BoundedDrawPosTextH>(r, record, 0); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 104 | |
| 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.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 111 | DEF_TEST(RecordOpts_SingleNoopSaveRestore, r) { |
| 112 | SkRecord record; |
| 113 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 114 | |
| 115 | recorder.save(); |
| 116 | recorder.clipRect(SkRect::MakeWH(200, 200)); |
| 117 | recorder.restore(); |
| 118 | |
| 119 | SkRecordNoopSaveRestores(&record); |
| 120 | for (unsigned i = 0; i < 3; i++) { |
| 121 | assert_type<SkRecords::NoOp>(r, record, i); |
| 122 | } |
| 123 | } |
| 124 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 125 | DEF_TEST(RecordOpts_NoopSaveRestores, r) { |
| 126 | SkRecord record; |
| 127 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 128 | |
| 129 | // The second pass will clean up this pair after the first pass noops all the innards. |
| 130 | recorder.save(); |
| 131 | // A simple pointless pair of save/restore. |
| 132 | recorder.save(); |
| 133 | recorder.restore(); |
| 134 | |
| 135 | // As long as we don't draw in there, everything is a noop. |
| 136 | recorder.save(); |
| 137 | recorder.clipRect(SkRect::MakeWH(200, 200)); |
| 138 | recorder.clipRect(SkRect::MakeWH(100, 100)); |
| 139 | recorder.restore(); |
| 140 | recorder.restore(); |
| 141 | |
| 142 | // These will be kept (though some future optimization might noop the save and restore). |
| 143 | recorder.save(); |
| 144 | recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint()); |
| 145 | recorder.restore(); |
| 146 | |
| 147 | SkRecordNoopSaveRestores(&record); |
| 148 | |
| 149 | for (unsigned index = 0; index < 8; index++) { |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 150 | assert_type<SkRecords::NoOp>(r, record, index); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 151 | } |
commit-bot@chromium.org | 7066bf3 | 2014-05-05 17:09:05 +0000 | [diff] [blame] | 152 | assert_type<SkRecords::Save>(r, record, 8); |
| 153 | assert_type<SkRecords::DrawRect>(r, record, 9); |
| 154 | assert_type<SkRecords::Restore>(r, record, 10); |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 155 | } |
commit-bot@chromium.org | f5bf3cf | 2014-05-07 14:47:44 +0000 | [diff] [blame^] | 156 | |
| 157 | static void assert_savelayer_restore(skiatest::Reporter* r, |
| 158 | SkRecord* record, |
| 159 | unsigned i, |
| 160 | bool shouldBeNoOped) { |
| 161 | SkRecordNoopSaveLayerDrawRestores(record); |
| 162 | if (shouldBeNoOped) { |
| 163 | assert_type<SkRecords::NoOp>(r, *record, i); |
| 164 | assert_type<SkRecords::NoOp>(r, *record, i+2); |
| 165 | } else { |
| 166 | assert_type<SkRecords::SaveLayer>(r, *record, i); |
| 167 | assert_type<SkRecords::Restore>(r, *record, i+2); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) { |
| 172 | SkRecord record; |
| 173 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 174 | |
| 175 | SkRect bounds = SkRect::MakeWH(100, 200); |
| 176 | SkRect draw = SkRect::MakeWH(50, 60); |
| 177 | |
| 178 | SkPaint goodLayerPaint, badLayerPaint, worseLayerPaint; |
| 179 | goodLayerPaint.setColor(0x03000000); // Only alpha. |
| 180 | badLayerPaint.setColor( 0x03040506); // Not only alpha. |
| 181 | worseLayerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); // Any effect will do. |
| 182 | |
| 183 | SkPaint goodDrawPaint, badDrawPaint; |
| 184 | goodDrawPaint.setColor(0xFF020202); // Opaque. |
| 185 | badDrawPaint.setColor( 0x0F020202); // Not opaque. |
| 186 | |
| 187 | // No change: optimization can't handle bounds. |
| 188 | recorder.saveLayer(&bounds, NULL); |
| 189 | recorder.drawRect(draw, goodDrawPaint); |
| 190 | recorder.restore(); |
| 191 | assert_savelayer_restore(r, &record, 0, false); |
| 192 | |
| 193 | // SaveLayer/Restore removed: no bounds + no paint = no point. |
| 194 | recorder.saveLayer(NULL, NULL); |
| 195 | recorder.drawRect(draw, goodDrawPaint); |
| 196 | recorder.restore(); |
| 197 | assert_savelayer_restore(r, &record, 3, true); |
| 198 | |
| 199 | // TODO(mtklein): test case with null draw paint |
| 200 | |
| 201 | // No change: layer paint isn't alpha-only. |
| 202 | recorder.saveLayer(NULL, &badLayerPaint); |
| 203 | recorder.drawRect(draw, goodDrawPaint); |
| 204 | recorder.restore(); |
| 205 | assert_savelayer_restore(r, &record, 6, false); |
| 206 | |
| 207 | // No change: layer paint has an effect. |
| 208 | recorder.saveLayer(NULL, &worseLayerPaint); |
| 209 | recorder.drawRect(draw, goodDrawPaint); |
| 210 | recorder.restore(); |
| 211 | assert_savelayer_restore(r, &record, 9, false); |
| 212 | |
| 213 | // No change: draw paint isn't opaque. |
| 214 | recorder.saveLayer(NULL, &goodLayerPaint); |
| 215 | recorder.drawRect(draw, badDrawPaint); |
| 216 | recorder.restore(); |
| 217 | assert_savelayer_restore(r, &record, 12, false); |
| 218 | |
| 219 | // SaveLayer/Restore removed: we can fold in the alpha! |
| 220 | recorder.saveLayer(NULL, &goodLayerPaint); |
| 221 | recorder.drawRect(draw, goodDrawPaint); |
| 222 | recorder.restore(); |
| 223 | assert_savelayer_restore(r, &record, 15, true); |
| 224 | |
| 225 | const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, record, 16); |
| 226 | REPORTER_ASSERT(r, drawRect != NULL); |
| 227 | REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202); |
| 228 | } |