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 #include "RecordTestUtils.h" | 9 #include "RecordTestUtils.h" |
10 | 10 |
11 #include "SkDebugCanvas.h" | 11 #include "SkDebugCanvas.h" |
| 12 #include "SkDrawPictureCallback.h" |
12 #include "SkRecord.h" | 13 #include "SkRecord.h" |
13 #include "SkRecordOpts.h" | 14 #include "SkRecordOpts.h" |
14 #include "SkRecordDraw.h" | 15 #include "SkRecordDraw.h" |
15 #include "SkRecorder.h" | 16 #include "SkRecorder.h" |
16 #include "SkRecords.h" | 17 #include "SkRecords.h" |
17 | 18 |
18 static const int W = 1920, H = 1080; | 19 static const int W = 1920, H = 1080; |
19 | 20 |
20 static void draw_pos_text_h(SkCanvas* canvas, const char* text, SkScalar y) { | 21 static void draw_pos_text_h(SkCanvas* canvas, const char* text, SkScalar y) { |
21 const size_t len = strlen(text); | 22 const size_t len = strlen(text); |
22 SkAutoTMalloc<SkScalar> xpos(len); | 23 SkAutoTMalloc<SkScalar> xpos(len); |
23 for (size_t i = 0; i < len; i++) { | 24 for (size_t i = 0; i < len; i++) { |
24 xpos[i] = (SkScalar)i; | 25 xpos[i] = (SkScalar)i; |
25 } | 26 } |
26 canvas->drawPosTextH(text, len, xpos, y, SkPaint()); | 27 canvas->drawPosTextH(text, len, xpos, y, SkPaint()); |
27 } | 28 } |
28 | 29 |
29 // Rerecord into another SkRecord using full SkCanvas semantics, | 30 class JustOneDraw : public SkDrawPictureCallback { |
30 // tracking clips and allowing SkRecordDraw's quickReject() calls to work. | 31 public: |
| 32 JustOneDraw() : fCalls(0) {} |
| 33 |
| 34 virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; } |
| 35 private: |
| 36 int fCalls; |
| 37 }; |
| 38 |
| 39 DEF_TEST(RecordDraw_Abort, r) { |
| 40 // Record two commands. |
| 41 SkRecord record; |
| 42 SkRecorder recorder(&record, W, H); |
| 43 recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint()); |
| 44 recorder.clipRect(SkRect::MakeWH(100, 200)); |
| 45 |
| 46 SkRecord rerecord; |
| 47 SkRecorder canvas(&rerecord, W, H); |
| 48 |
| 49 JustOneDraw callback; |
| 50 SkRecordDraw(record, &canvas, &callback); |
| 51 |
| 52 REPORTER_ASSERT(r, 3 == rerecord.count()); |
| 53 assert_type<SkRecords::Save> (r, rerecord, 0); |
| 54 assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
| 55 assert_type<SkRecords::Restore> (r, rerecord, 2); |
| 56 } |
| 57 |
| 58 DEF_TEST(RecordDraw_Unbalanced, r) { |
| 59 SkRecord record; |
| 60 SkRecorder recorder(&record, W, H); |
| 61 recorder.save(); // We won't balance this, but SkRecordDraw will for us. |
| 62 |
| 63 SkRecord rerecord; |
| 64 SkRecorder canvas(&rerecord, W, H); |
| 65 SkRecordDraw(record, &canvas); |
| 66 |
| 67 REPORTER_ASSERT(r, 4 == rerecord.count()); |
| 68 assert_type<SkRecords::Save> (r, rerecord, 0); |
| 69 assert_type<SkRecords::Save> (r, rerecord, 1); |
| 70 assert_type<SkRecords::Restore> (r, rerecord, 2); |
| 71 assert_type<SkRecords::Restore> (r, rerecord, 3); |
| 72 } |
| 73 |
| 74 // Rerecord into another SkRecord with a clip. |
31 static void record_clipped(const SkRecord& record, SkRect clip, SkRecord* clippe
d) { | 75 static void record_clipped(const SkRecord& record, SkRect clip, SkRecord* clippe
d) { |
32 SkRecorder recorder(clipped, W, H); | 76 SkRecorder recorder(clipped, W, H); |
33 recorder.clipRect(clip); | 77 recorder.clipRect(clip); |
34 SkRecordDraw(record, &recorder); | 78 SkRecordDraw(record, &recorder); |
35 } | 79 } |
36 | 80 |
37 DEF_TEST(RecordDraw_PosTextHQuickReject, r) { | 81 DEF_TEST(RecordDraw_PosTextHQuickReject, r) { |
38 SkRecord record; | 82 SkRecord record; |
39 SkRecorder recorder(&record, W, H); | 83 SkRecorder recorder(&record, W, H); |
40 | 84 |
41 draw_pos_text_h(&recorder, "This will draw.", 20); | 85 draw_pos_text_h(&recorder, "This will draw.", 20); |
42 draw_pos_text_h(&recorder, "This won't.", 5000); | 86 draw_pos_text_h(&recorder, "This won't.", 5000); |
43 | 87 |
44 SkRecordBoundDrawPosTextH(&record); | 88 SkRecordBoundDrawPosTextH(&record); |
45 | 89 |
46 SkRecord clipped; | 90 SkRecord clipped; |
47 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); | 91 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); |
48 | 92 |
49 // clipRect and the first drawPosTextH. | 93 REPORTER_ASSERT(r, 4 == clipped.count()); |
50 REPORTER_ASSERT(r, 2 == clipped.count()); | 94 assert_type<SkRecords::ClipRect> (r, clipped, 0); |
| 95 assert_type<SkRecords::Save> (r, clipped, 1); |
| 96 assert_type<SkRecords::DrawPosTextH>(r, clipped, 2); |
| 97 assert_type<SkRecords::Restore> (r, clipped, 3); |
51 } | 98 } |
52 | 99 |
53 DEF_TEST(RecordDraw_Culling, r) { | 100 DEF_TEST(RecordDraw_Culling, r) { |
54 // Record these 7 drawing commands verbatim. | 101 // Record these 7 drawing commands verbatim. |
55 SkRecord record; | 102 SkRecord record; |
56 SkRecorder recorder(&record, W, H); | 103 SkRecorder recorder(&record, W, H); |
57 | 104 |
58 recorder.pushCull(SkRect::MakeWH(100, 100)); | 105 recorder.pushCull(SkRect::MakeWH(100, 100)); |
59 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); | 106 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); |
60 recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint()); | 107 recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint()); |
61 recorder.pushCull(SkRect::MakeWH(5, 5)); | 108 recorder.pushCull(SkRect::MakeWH(5, 5)); |
62 recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint()); | 109 recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint()); |
63 recorder.popCull(); | 110 recorder.popCull(); |
64 recorder.popCull(); | 111 recorder.popCull(); |
65 | 112 |
66 // Take a pass over to match up pushCulls and popCulls. | 113 // Take a pass over to match up pushCulls and popCulls. |
67 SkRecordAnnotateCullingPairs(&record); | 114 SkRecordAnnotateCullingPairs(&record); |
68 | 115 |
69 // This clip intersects the outer cull, but allows us to quick reject the in
ner one. | 116 // This clip intersects the outer cull, but allows us to quick reject the in
ner one. |
70 SkRecord clipped; | 117 SkRecord clipped; |
71 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); | 118 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); |
72 | 119 |
73 // We'll keep the clipRect call from above, and the outer two drawRects, and
the push/pop pair. | 120 // If culling weren't working, we'd see 3 more commands recorded here. |
74 // If culling weren't working, we'd see 8 commands recorded here. | 121 REPORTER_ASSERT(r, 7 == clipped.count()); |
75 REPORTER_ASSERT(r, 5 == clipped.count()); | 122 assert_type<SkRecords::ClipRect>(r, clipped, 0); |
| 123 assert_type<SkRecords::Save> (r, clipped, 1); |
| 124 assert_type<SkRecords::PushCull>(r, clipped, 2); |
| 125 assert_type<SkRecords::DrawRect>(r, clipped, 3); |
| 126 assert_type<SkRecords::DrawRect>(r, clipped, 4); |
| 127 assert_type<SkRecords::PopCull> (r, clipped, 5); |
| 128 assert_type<SkRecords::Restore> (r, clipped, 6); |
76 } | 129 } |
77 | 130 |
78 DEF_TEST(RecordDraw_SetMatrixClobber, r) { | 131 DEF_TEST(RecordDraw_SetMatrixClobber, r) { |
79 // Set up an SkRecord that just scales by 2x,3x. | 132 // Set up an SkRecord that just scales by 2x,3x. |
80 SkRecord scaleRecord; | 133 SkRecord scaleRecord; |
81 SkRecorder scaleCanvas(&scaleRecord, W, H); | 134 SkRecorder scaleCanvas(&scaleRecord, W, H); |
82 SkMatrix scale; | 135 SkMatrix scale; |
83 scale.setScale(2, 3); | 136 scale.setScale(2, 3); |
84 scaleCanvas.setMatrix(scale); | 137 scaleCanvas.setMatrix(scale); |
85 | 138 |
86 // Set up an SkRecord with an initial +20, +20 translate. | 139 // Set up an SkRecord with an initial +20, +20 translate. |
87 SkRecord translateRecord; | 140 SkRecord translateRecord; |
88 SkRecorder translateCanvas(&translateRecord, W, H); | 141 SkRecorder translateCanvas(&translateRecord, W, H); |
89 SkMatrix translate; | 142 SkMatrix translate; |
90 translate.setTranslate(20, 20); | 143 translate.setTranslate(20, 20); |
91 translateCanvas.setMatrix(translate); | 144 translateCanvas.setMatrix(translate); |
92 | 145 |
93 SkRecordDraw(scaleRecord, &translateCanvas); | 146 SkRecordDraw(scaleRecord, &translateCanvas); |
| 147 REPORTER_ASSERT(r, 4 == translateRecord.count()); |
| 148 assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); |
| 149 assert_type<SkRecords::Save> (r, translateRecord, 1); |
| 150 assert_type<SkRecords::SetMatrix>(r, translateRecord, 2); |
| 151 assert_type<SkRecords::Restore> (r, translateRecord, 3); |
94 | 152 |
95 // When we look at translateRecord now, it should have its first +20,+20 tra
nslate, | 153 // When we look at translateRecord now, it should have its first +20,+20 tra
nslate, |
96 // then a 2x,3x scale that's been concatted with that +20,+20 translate. | 154 // then a 2x,3x scale that's been concatted with that +20,+20 translate. |
97 const SkRecords::SetMatrix* setMatrix; | 155 const SkRecords::SetMatrix* setMatrix; |
98 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); | 156 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); |
99 REPORTER_ASSERT(r, setMatrix->matrix == translate); | 157 REPORTER_ASSERT(r, setMatrix->matrix == translate); |
100 | 158 |
101 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 1); | 159 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2); |
102 SkMatrix expected = scale; | 160 SkMatrix expected = scale; |
103 expected.postConcat(translate); | 161 expected.postConcat(translate); |
104 REPORTER_ASSERT(r, setMatrix->matrix == expected); | 162 REPORTER_ASSERT(r, setMatrix->matrix == expected); |
105 } | 163 } |
OLD | NEW |