OLD | NEW |
| (Empty) |
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 #if SK_SUPPORT_GPU | |
9 | |
10 #include "SkMatrix.h" | |
11 #include "SkString.h" | |
12 #include "GrTRecorder.h" | |
13 #include "Test.h" | |
14 | |
15 //////////////////////////////////////////////////////////////////////////////// | |
16 | |
17 static int activeRecorderItems = 0; | |
18 | |
19 class IntWrapper { | |
20 public: | |
21 IntWrapper() {} | |
22 IntWrapper(int value) : fValue(value) {} | |
23 operator int() { return fValue; } | |
24 private: | |
25 int fValue; | |
26 }; | |
27 | |
28 static void test_empty_back(skiatest::Reporter* reporter) { | |
29 GrTRecorder<IntWrapper, int> recorder(0); | |
30 | |
31 REPORTER_ASSERT(reporter, recorder.empty()); | |
32 | |
33 for (int i = 0; i < 100; ++i) { | |
34 REPORTER_ASSERT(reporter, i == *GrNEW_APPEND_TO_RECORDER(recorder, IntWr
apper, (i))); | |
35 REPORTER_ASSERT(reporter, !recorder.empty()); | |
36 REPORTER_ASSERT(reporter, i == recorder.back()); | |
37 } | |
38 | |
39 REPORTER_ASSERT(reporter, !recorder.empty()); | |
40 | |
41 recorder.reset(); | |
42 | |
43 REPORTER_ASSERT(reporter, recorder.empty()); | |
44 } | |
45 | |
46 struct ExtraData { | |
47 typedef GrTRecorder<ExtraData, int> Recorder; | |
48 | |
49 ExtraData(int i) : fData(i) { | |
50 int* extraData = this->extraData(); | |
51 for (int j = 0; j < i; j++) { | |
52 extraData[j] = i; | |
53 } | |
54 ++activeRecorderItems; | |
55 } | |
56 ~ExtraData() { | |
57 --activeRecorderItems; | |
58 } | |
59 int* extraData() { | |
60 return reinterpret_cast<int*>(Recorder::GetDataForItem(this)); | |
61 } | |
62 int fData; | |
63 }; | |
64 | |
65 static void test_extra_data(skiatest::Reporter* reporter) { | |
66 ExtraData::Recorder recorder(0); | |
67 for (int i = 0; i < 100; ++i) { | |
68 GrNEW_APPEND_WITH_DATA_TO_RECORDER(recorder, ExtraData, (i), i * sizeof(
int)); | |
69 } | |
70 REPORTER_ASSERT(reporter, 100 == activeRecorderItems); | |
71 | |
72 ExtraData::Recorder::Iter iter(recorder); | |
73 for (int i = 0; i < 100; ++i) { | |
74 REPORTER_ASSERT(reporter, iter.next()); | |
75 REPORTER_ASSERT(reporter, i == iter->fData); | |
76 for (int j = 0; j < i; j++) { | |
77 REPORTER_ASSERT(reporter, i == iter->extraData()[j]); | |
78 } | |
79 } | |
80 REPORTER_ASSERT(reporter, !iter.next()); | |
81 | |
82 recorder.reset(); | |
83 REPORTER_ASSERT(reporter, 0 == activeRecorderItems); | |
84 } | |
85 | |
86 enum ClassType { | |
87 kBase_ClassType, | |
88 kSubclass_ClassType, | |
89 kSubSubclass_ClassType, | |
90 kSubclassExtraData_ClassType, | |
91 kSubclassEmpty_ClassType, | |
92 | |
93 kNumClassTypes | |
94 }; | |
95 | |
96 class Base { | |
97 public: | |
98 typedef GrTRecorder<Base, void*> Recorder; | |
99 | |
100 Base() { | |
101 fMatrix.reset(); | |
102 ++activeRecorderItems; | |
103 } | |
104 | |
105 virtual ~Base() { --activeRecorderItems; } | |
106 | |
107 virtual ClassType getType() { return kBase_ClassType; } | |
108 | |
109 virtual void validate(skiatest::Reporter* reporter) const { | |
110 REPORTER_ASSERT(reporter, fMatrix.isIdentity()); | |
111 } | |
112 | |
113 private: | |
114 SkMatrix fMatrix; | |
115 }; | |
116 | |
117 class Subclass : public Base { | |
118 public: | |
119 Subclass() : fString("Lorem ipsum dolor sit amet") {} | |
120 | |
121 virtual ClassType getType() { return kSubclass_ClassType; } | |
122 | |
123 virtual void validate(skiatest::Reporter* reporter) const { | |
124 Base::validate(reporter); | |
125 REPORTER_ASSERT(reporter, !strcmp("Lorem ipsum dolor sit amet", fString.
c_str())); | |
126 } | |
127 | |
128 private: | |
129 SkString fString; | |
130 }; | |
131 | |
132 class SubSubclass : public Subclass { | |
133 public: | |
134 SubSubclass() : fInt(1234), fFloat(1.234f) {} | |
135 | |
136 virtual ClassType getType() { return kSubSubclass_ClassType; } | |
137 | |
138 virtual void validate(skiatest::Reporter* reporter) const { | |
139 Subclass::validate(reporter); | |
140 REPORTER_ASSERT(reporter, 1234 == fInt); | |
141 REPORTER_ASSERT(reporter, 1.234f == fFloat); | |
142 } | |
143 | |
144 private: | |
145 int fInt; | |
146 float fFloat; | |
147 }; | |
148 | |
149 class SubclassExtraData : public Base { | |
150 public: | |
151 SubclassExtraData(int length) : fLength(length) { | |
152 int* data = reinterpret_cast<int*>(Recorder::GetDataForItem(this)); | |
153 for (int i = 0; i < fLength; ++i) { | |
154 data[i] = 123456789 + 987654321 * i; | |
155 } | |
156 } | |
157 | |
158 virtual ClassType getType() { return kSubclassExtraData_ClassType; } | |
159 | |
160 virtual void validate(skiatest::Reporter* reporter) const { | |
161 Base::validate(reporter); | |
162 const int* data = reinterpret_cast<const int*>(Recorder::GetDataForItem(
this)); | |
163 for (int i = 0; i < fLength; ++i) { | |
164 REPORTER_ASSERT(reporter, 123456789 + 987654321 * i == data[i]); | |
165 } | |
166 } | |
167 | |
168 private: | |
169 int fLength; | |
170 }; | |
171 | |
172 class SubclassEmpty : public Base { | |
173 public: | |
174 virtual ClassType getType() { return kSubclassEmpty_ClassType; } | |
175 }; | |
176 | |
177 static void test_subclasses(skiatest::Reporter* reporter) { | |
178 class Order { | |
179 public: | |
180 Order() { this->reset(); } | |
181 void reset() { fCurrent = 0; } | |
182 ClassType next() { | |
183 fCurrent = 1664525 * fCurrent + 1013904223; | |
184 return static_cast<ClassType>(fCurrent % kNumClassTypes); | |
185 } | |
186 private: | |
187 uint32_t fCurrent; | |
188 }; | |
189 | |
190 Base::Recorder recorder(1024); | |
191 | |
192 Order order; | |
193 for (int i = 0; i < 1000; i++) { | |
194 switch (order.next()) { | |
195 case kBase_ClassType: | |
196 GrNEW_APPEND_TO_RECORDER(recorder, Base, ()); | |
197 break; | |
198 | |
199 case kSubclass_ClassType: | |
200 GrNEW_APPEND_TO_RECORDER(recorder, Subclass, ()); | |
201 break; | |
202 | |
203 case kSubSubclass_ClassType: | |
204 GrNEW_APPEND_TO_RECORDER(recorder, SubSubclass, ()); | |
205 break; | |
206 | |
207 case kSubclassExtraData_ClassType: | |
208 GrNEW_APPEND_WITH_DATA_TO_RECORDER(recorder, SubclassExtraData,
(i), sizeof(int) * i); | |
209 break; | |
210 | |
211 case kSubclassEmpty_ClassType: | |
212 GrNEW_APPEND_TO_RECORDER(recorder, SubclassEmpty, ()); | |
213 break; | |
214 | |
215 default: | |
216 reporter->reportFailed(SkString("Invalid class type")); | |
217 break; | |
218 } | |
219 } | |
220 REPORTER_ASSERT(reporter, 1000 == activeRecorderItems); | |
221 | |
222 order.reset(); | |
223 Base::Recorder::Iter iter(recorder); | |
224 for (int i = 0; i < 1000; ++i) { | |
225 REPORTER_ASSERT(reporter, iter.next()); | |
226 REPORTER_ASSERT(reporter, order.next() == iter->getType()); | |
227 iter->validate(reporter); | |
228 } | |
229 REPORTER_ASSERT(reporter, !iter.next()); | |
230 | |
231 // Don't reset the recorder. It should automatically destruct all its items. | |
232 } | |
233 | |
234 DEF_GPUTEST(GrTRecorder, reporter, factory) { | |
235 test_empty_back(reporter); | |
236 | |
237 test_extra_data(reporter); | |
238 REPORTER_ASSERT(reporter, 0 == activeRecorderItems); // test_extra_data shou
ld call reset(). | |
239 | |
240 test_subclasses(reporter); | |
241 REPORTER_ASSERT(reporter, 0 == activeRecorderItems); // Ensure ~GrTRecorder
invokes dtors. | |
242 } | |
243 | |
244 #endif | |
OLD | NEW |