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