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 | |
| 15 | static const int W = 1920, H = 1080; |
| 16 | |
| 17 | // If the command we're reading is a U, set ptr to it, otherwise set it to NULL. |
| 18 | template <typename U> |
| 19 | struct ReadAs { |
| 20 | explicit ReadAs(const U** ptr) : ptr(ptr) {} |
| 21 | const U** ptr; |
| 22 | |
| 23 | void operator()(const U& r) { *ptr = &r; } |
| 24 | |
| 25 | template <typename T> |
| 26 | void operator()(const T&) { *ptr = NULL; } |
| 27 | }; |
| 28 | |
| 29 | // Assert that the ith command in record is of type T, and return it. |
| 30 | template <typename T> |
| 31 | static const T* assert_type(const SkRecord& record, unsigned index) { |
| 32 | const T* ptr = NULL; |
| 33 | ReadAs<T> reader(&ptr); |
| 34 | record.visit(index, reader); |
| 35 | SkASSERT(ptr != NULL); |
| 36 | return ptr; |
| 37 | } |
| 38 | |
| 39 | DEF_TEST(RecordOpts_Culling, r) { |
| 40 | SkRecord record; |
| 41 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 42 | |
| 43 | recorder.drawRect(SkRect::MakeWH(1000, 10000), SkPaint()); |
| 44 | |
| 45 | recorder.pushCull(SkRect::MakeWH(100, 100)); |
| 46 | recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); |
| 47 | recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint()); |
| 48 | recorder.pushCull(SkRect::MakeWH(5, 5)); |
| 49 | recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint()); |
| 50 | recorder.popCull(); |
| 51 | recorder.popCull(); |
| 52 | |
| 53 | SkRecordAnnotateCullingPairs(&record); |
| 54 | |
| 55 | REPORTER_ASSERT(r, 6 == assert_type<SkRecords::PairedPushCull>(record, 1)->skip); |
| 56 | REPORTER_ASSERT(r, 2 == assert_type<SkRecords::PairedPushCull>(record, 4)->skip); |
| 57 | } |
| 58 | |
| 59 | static void draw_pos_text(SkCanvas* canvas, const char* text, bool constantY) { |
| 60 | const size_t len = strlen(text); |
| 61 | SkAutoTMalloc<SkPoint> pos(len); |
| 62 | for (size_t i = 0; i < len; i++) { |
| 63 | pos[i].fX = (SkScalar)i; |
| 64 | pos[i].fY = constantY ? SK_Scalar1 : (SkScalar)i; |
| 65 | } |
| 66 | canvas->drawPosText(text, len, pos, SkPaint()); |
| 67 | } |
| 68 | |
| 69 | DEF_TEST(RecordOpts_StrengthReduction, r) { |
| 70 | SkRecord record; |
| 71 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 72 | |
| 73 | // We can convert a drawPosText into a drawPosTextH when all the Ys are the same. |
| 74 | draw_pos_text(&recorder, "This will be reduced to drawPosTextH.", true); |
| 75 | draw_pos_text(&recorder, "This cannot be reduced to drawPosTextH.", false); |
| 76 | |
| 77 | SkRecordReduceDrawPosTextStrength(&record); |
| 78 | |
| 79 | assert_type<SkRecords::DrawPosTextH>(record, 0); |
| 80 | assert_type<SkRecords::DrawPosText>(record, 1); |
| 81 | } |
| 82 | |
| 83 | DEF_TEST(RecordOpts_TextBounding, r) { |
| 84 | SkRecord record; |
| 85 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 86 | |
| 87 | // First, get a drawPosTextH. Here's a handy way. Its text size will be the default (12). |
| 88 | draw_pos_text(&recorder, "This will be reduced to drawPosTextH.", true); |
| 89 | SkRecordReduceDrawPosTextStrength(&record); |
| 90 | |
| 91 | const SkRecords::DrawPosTextH* original = |
| 92 | assert_type<SkRecords::DrawPosTextH>(record, 0); |
| 93 | |
| 94 | // This should wrap the original DrawPosTextH with minY and maxY. |
| 95 | SkRecordBoundDrawPosTextH(&record); |
| 96 | |
| 97 | const SkRecords::BoundedDrawPosTextH* bounded = |
| 98 | assert_type<SkRecords::BoundedDrawPosTextH>(record, 0); |
| 99 | |
| 100 | const SkPaint defaults; |
| 101 | REPORTER_ASSERT(r, bounded->base == original); |
| 102 | REPORTER_ASSERT(r, bounded->minY <= SK_Scalar1 - defaults.getTextSize()); |
| 103 | REPORTER_ASSERT(r, bounded->maxY >= SK_Scalar1 + defaults.getTextSize()); |
| 104 | } |
| 105 | |
| 106 | DEF_TEST(RecordOpts_NoopSaveRestores, r) { |
| 107 | SkRecord record; |
| 108 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 109 | |
| 110 | // The second pass will clean up this pair after the first pass noops all the innards. |
| 111 | recorder.save(); |
| 112 | // A simple pointless pair of save/restore. |
| 113 | recorder.save(); |
| 114 | recorder.restore(); |
| 115 | |
| 116 | // As long as we don't draw in there, everything is a noop. |
| 117 | recorder.save(); |
| 118 | recorder.clipRect(SkRect::MakeWH(200, 200)); |
| 119 | recorder.clipRect(SkRect::MakeWH(100, 100)); |
| 120 | recorder.restore(); |
| 121 | recorder.restore(); |
| 122 | |
| 123 | // These will be kept (though some future optimization might noop the save and restore). |
| 124 | recorder.save(); |
| 125 | recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint()); |
| 126 | recorder.restore(); |
| 127 | |
| 128 | SkRecordNoopSaveRestores(&record); |
| 129 | |
| 130 | for (unsigned index = 0; index < 8; index++) { |
| 131 | assert_type<SkRecords::NoOp>(record, index); |
| 132 | } |
| 133 | assert_type<SkRecords::Save>(record, 8); |
| 134 | assert_type<SkRecords::DrawRect>(record, 9); |
| 135 | assert_type<SkRecords::Restore>(record, 10); |
| 136 | } |