OLD | NEW |
1 #include "Test.h" | 1 #include "Test.h" |
2 | 2 |
3 #include "SkRecord.h" | 3 #include "SkRecord.h" |
4 #include "SkRecords.h" | 4 #include "SkRecords.h" |
5 | 5 |
6 // Adds the area of any DrawRect command it sees into area. | 6 // Sums the area of any DrawRect command it sees. |
7 class AreaSummer { | 7 class AreaSummer { |
8 public: | 8 public: |
9 explicit AreaSummer(int* area) : fArea(area) {} | 9 AreaSummer() : fArea(0) {} |
10 | 10 |
11 template <typename T> void operator()(const T&) { } | 11 template <typename T> void operator()(const T&) { } |
12 | 12 |
| 13 int area() const { return fArea; } |
| 14 |
13 private: | 15 private: |
14 int* fArea; | 16 int fArea; |
15 }; | 17 }; |
16 template <> void AreaSummer::operator()(const SkRecords::DrawRect& record) { | 18 template <> void AreaSummer::operator()(const SkRecords::DrawRect& record) { |
17 *fArea += (int) (record.rect.width() * record.rect.height()); | 19 fArea += (int) (record.rect.width() * record.rect.height()); |
18 } | 20 } |
19 | 21 |
20 // Scales out the bottom-right corner of any DrawRect command it sees by 2x. | 22 // Scales out the bottom-right corner of any DrawRect command it sees by 2x. |
21 struct Stretch { | 23 struct Stretch { |
22 template <typename T> void operator()(T*) {} | 24 template <typename T> void operator()(T*) {} |
23 }; | 25 }; |
24 template <> void Stretch::operator()(SkRecords::DrawRect* record) { | 26 template <> void Stretch::operator()(SkRecords::DrawRect* record) { |
25 record->rect.fRight *= 2; | 27 record->rect.fRight *= 2; |
26 record->rect.fBottom *= 2; | 28 record->rect.fBottom *= 2; |
27 } | 29 } |
28 | 30 |
29 // Basic tests for the low-level SkRecord code. | 31 // Basic tests for the low-level SkRecord code. |
30 DEF_TEST(Record, r) { | 32 DEF_TEST(Record, r) { |
31 SkRecord record; | 33 SkRecord record; |
32 | 34 |
33 // Add a simple DrawRect command. | 35 // Add a simple DrawRect command. |
34 SkRect rect = SkRect::MakeWH(10, 10); | 36 SkRect rect = SkRect::MakeWH(10, 10); |
35 SkPaint paint; | 37 SkPaint paint; |
36 SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRe
ct, (rect, paint)); | 38 SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRe
ct, (rect, paint)); |
37 | 39 |
38 // Its area should be 100. | 40 // Its area should be 100. |
39 int area = 0; | 41 AreaSummer summer; |
40 record.visit(AreaSummer(&area)); | 42 record.visit(summer); |
41 REPORTER_ASSERT(r, area == 100); | 43 REPORTER_ASSERT(r, summer.area() == 100); |
42 | 44 |
43 // Scale 2x. Now it's area should be 400. | 45 // Scale 2x. |
44 record.mutate(Stretch()); | 46 Stretch stretch; |
45 area = 0; | 47 record.mutate(stretch); |
46 record.visit(AreaSummer(&area)); | 48 |
47 REPORTER_ASSERT(r, area == 400); | 49 // Now its area should be 100 + 400. |
| 50 record.visit(summer); |
| 51 REPORTER_ASSERT(r, summer.area() == 500); |
48 } | 52 } |
OLD | NEW |