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 | |
robertphillips
2014/06/24 15:11:08
Remove this SkDebugf?
mtklein
2014/06/24 15:16:47
Done.
| |
52 SkDebugf("%d\n", rerecord.count()); | |
53 REPORTER_ASSERT(r, 3 == rerecord.count()); | |
54 assert_type<SkRecords::Save> (r, rerecord, 0); | |
55 assert_type<SkRecords::DrawRect>(r, rerecord, 1); | |
56 assert_type<SkRecords::Restore> (r, rerecord, 2); | |
57 } | |
58 | |
59 DEF_TEST(RecordDraw_Unbalanced, r) { | |
60 SkRecord record; | |
61 SkRecorder recorder(&record, W, H); | |
62 recorder.save(); // We won't balance this, but SkRecordDraw will for us. | |
63 | |
64 SkRecord rerecord; | |
65 SkRecorder canvas(&rerecord, W, H); | |
66 SkRecordDraw(record, &canvas); | |
67 | |
68 REPORTER_ASSERT(r, 4 == rerecord.count()); | |
69 assert_type<SkRecords::Save> (r, rerecord, 0); | |
70 assert_type<SkRecords::Save> (r, rerecord, 1); | |
71 assert_type<SkRecords::Restore> (r, rerecord, 2); | |
72 assert_type<SkRecords::Restore> (r, rerecord, 3); | |
73 } | |
74 | |
75 // Rerecord into another SkRecord with a clip. | |
31 static void record_clipped(const SkRecord& record, SkRect clip, SkRecord* clippe d) { | 76 static void record_clipped(const SkRecord& record, SkRect clip, SkRecord* clippe d) { |
32 SkRecorder recorder(clipped, W, H); | 77 SkRecorder recorder(clipped, W, H); |
33 recorder.clipRect(clip); | 78 recorder.clipRect(clip); |
34 SkRecordDraw(record, &recorder); | 79 SkRecordDraw(record, &recorder); |
35 } | 80 } |
36 | 81 |
37 DEF_TEST(RecordDraw_PosTextHQuickReject, r) { | 82 DEF_TEST(RecordDraw_PosTextHQuickReject, r) { |
38 SkRecord record; | 83 SkRecord record; |
39 SkRecorder recorder(&record, W, H); | 84 SkRecorder recorder(&record, W, H); |
40 | 85 |
41 draw_pos_text_h(&recorder, "This will draw.", 20); | 86 draw_pos_text_h(&recorder, "This will draw.", 20); |
42 draw_pos_text_h(&recorder, "This won't.", 5000); | 87 draw_pos_text_h(&recorder, "This won't.", 5000); |
43 | 88 |
44 SkRecordBoundDrawPosTextH(&record); | 89 SkRecordBoundDrawPosTextH(&record); |
45 | 90 |
46 SkRecord clipped; | 91 SkRecord clipped; |
47 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); | 92 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); |
48 | 93 |
49 // clipRect and the first drawPosTextH. | 94 REPORTER_ASSERT(r, 4 == clipped.count()); |
50 REPORTER_ASSERT(r, 2 == clipped.count()); | 95 assert_type<SkRecords::ClipRect> (r, clipped, 0); |
96 assert_type<SkRecords::Save> (r, clipped, 1); | |
97 assert_type<SkRecords::DrawPosTextH>(r, clipped, 2); | |
98 assert_type<SkRecords::Restore> (r, clipped, 3); | |
51 } | 99 } |
52 | 100 |
53 DEF_TEST(RecordDraw_Culling, r) { | 101 DEF_TEST(RecordDraw_Culling, r) { |
54 // Record these 7 drawing commands verbatim. | 102 // Record these 7 drawing commands verbatim. |
55 SkRecord record; | 103 SkRecord record; |
56 SkRecorder recorder(&record, W, H); | 104 SkRecorder recorder(&record, W, H); |
57 | 105 |
58 recorder.pushCull(SkRect::MakeWH(100, 100)); | 106 recorder.pushCull(SkRect::MakeWH(100, 100)); |
59 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); | 107 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); |
60 recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint()); | 108 recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint()); |
61 recorder.pushCull(SkRect::MakeWH(5, 5)); | 109 recorder.pushCull(SkRect::MakeWH(5, 5)); |
62 recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint()); | 110 recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint()); |
63 recorder.popCull(); | 111 recorder.popCull(); |
64 recorder.popCull(); | 112 recorder.popCull(); |
65 | 113 |
66 // Take a pass over to match up pushCulls and popCulls. | 114 // Take a pass over to match up pushCulls and popCulls. |
67 SkRecordAnnotateCullingPairs(&record); | 115 SkRecordAnnotateCullingPairs(&record); |
68 | 116 |
69 // This clip intersects the outer cull, but allows us to quick reject the in ner one. | 117 // This clip intersects the outer cull, but allows us to quick reject the in ner one. |
70 SkRecord clipped; | 118 SkRecord clipped; |
71 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); | 119 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); |
72 | 120 |
73 // We'll keep the clipRect call from above, and the outer two drawRects, and the push/pop pair. | 121 // 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. | 122 REPORTER_ASSERT(r, 7 == clipped.count()); |
75 REPORTER_ASSERT(r, 5 == clipped.count()); | 123 assert_type<SkRecords::ClipRect>(r, clipped, 0); |
124 assert_type<SkRecords::Save> (r, clipped, 1); | |
125 assert_type<SkRecords::PushCull>(r, clipped, 2); | |
126 assert_type<SkRecords::DrawRect>(r, clipped, 3); | |
127 assert_type<SkRecords::DrawRect>(r, clipped, 4); | |
128 assert_type<SkRecords::PopCull> (r, clipped, 5); | |
129 assert_type<SkRecords::Restore> (r, clipped, 6); | |
76 } | 130 } |
77 | 131 |
78 DEF_TEST(RecordDraw_SetMatrixClobber, r) { | 132 DEF_TEST(RecordDraw_SetMatrixClobber, r) { |
79 // Set up an SkRecord that just scales by 2x,3x. | 133 // Set up an SkRecord that just scales by 2x,3x. |
80 SkRecord scaleRecord; | 134 SkRecord scaleRecord; |
81 SkRecorder scaleCanvas(&scaleRecord, W, H); | 135 SkRecorder scaleCanvas(&scaleRecord, W, H); |
82 SkMatrix scale; | 136 SkMatrix scale; |
83 scale.setScale(2, 3); | 137 scale.setScale(2, 3); |
84 scaleCanvas.setMatrix(scale); | 138 scaleCanvas.setMatrix(scale); |
85 | 139 |
86 // Set up an SkRecord with an initial +20, +20 translate. | 140 // Set up an SkRecord with an initial +20, +20 translate. |
87 SkRecord translateRecord; | 141 SkRecord translateRecord; |
88 SkRecorder translateCanvas(&translateRecord, W, H); | 142 SkRecorder translateCanvas(&translateRecord, W, H); |
89 SkMatrix translate; | 143 SkMatrix translate; |
90 translate.setTranslate(20, 20); | 144 translate.setTranslate(20, 20); |
91 translateCanvas.setMatrix(translate); | 145 translateCanvas.setMatrix(translate); |
92 | 146 |
93 SkRecordDraw(scaleRecord, &translateCanvas); | 147 SkRecordDraw(scaleRecord, &translateCanvas); |
148 REPORTER_ASSERT(r, 4 == translateRecord.count()); | |
149 assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); | |
150 assert_type<SkRecords::Save> (r, translateRecord, 1); | |
151 assert_type<SkRecords::SetMatrix>(r, translateRecord, 2); | |
152 assert_type<SkRecords::Restore> (r, translateRecord, 3); | |
94 | 153 |
95 // When we look at translateRecord now, it should have its first +20,+20 tra nslate, | 154 // 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. | 155 // then a 2x,3x scale that's been concatted with that +20,+20 translate. |
97 const SkRecords::SetMatrix* setMatrix; | 156 const SkRecords::SetMatrix* setMatrix; |
98 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); | 157 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); |
99 REPORTER_ASSERT(r, setMatrix->matrix == translate); | 158 REPORTER_ASSERT(r, setMatrix->matrix == translate); |
100 | 159 |
101 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 1); | 160 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2); |
102 SkMatrix expected = scale; | 161 SkMatrix expected = scale; |
103 expected.postConcat(translate); | 162 expected.postConcat(translate); |
104 REPORTER_ASSERT(r, setMatrix->matrix == expected); | 163 REPORTER_ASSERT(r, setMatrix->matrix == expected); |
105 } | 164 } |
OLD | NEW |