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 "SkRecords.h" | 11 #include "SkRecords.h" |
12 | 12 |
13 // Sums the area of any DrawRect command it sees. | 13 // Sums the area of any DrawRect command it sees. |
14 class AreaSummer { | 14 class AreaSummer { |
15 public: | 15 public: |
16 AreaSummer() : fArea(0) {} | 16 AreaSummer() : fArea(0) {} |
17 | 17 |
18 template <typename T> void operator()(const T&) { } | 18 template <typename T> void operator()(const T&) { } |
19 | 19 |
| 20 void operator()(const SkRecords::DrawRect& draw) { |
| 21 fArea += (int)(draw.rect.width() * draw.rect.height()); |
| 22 } |
| 23 |
20 int area() const { return fArea; } | 24 int area() const { return fArea; } |
21 | 25 |
22 void apply(const SkRecord& record) { | 26 void apply(const SkRecord& record) { |
23 for (unsigned i = 0; i < record.count(); i++) { | 27 for (unsigned i = 0; i < record.count(); i++) { |
24 record.visit(i, *this); | 28 record.visit<void>(i, *this); |
25 } | 29 } |
26 } | 30 } |
27 | 31 |
28 private: | 32 private: |
29 int fArea; | 33 int fArea; |
30 }; | 34 }; |
31 template <> void AreaSummer::operator()(const SkRecords::DrawRect& record) { | |
32 fArea += (int) (record.rect.width() * record.rect.height()); | |
33 } | |
34 | 35 |
35 // Scales out the bottom-right corner of any DrawRect command it sees by 2x. | 36 // Scales out the bottom-right corner of any DrawRect command it sees by 2x. |
36 struct Stretch { | 37 struct Stretch { |
37 template <typename T> void operator()(T*) {} | 38 template <typename T> void operator()(T*) {} |
| 39 void operator()(SkRecords::DrawRect* draw) { |
| 40 draw->rect.fRight *= 2; |
| 41 draw->rect.fBottom *= 2; |
| 42 } |
38 | 43 |
39 void apply(SkRecord* record) { | 44 void apply(SkRecord* record) { |
40 for (unsigned i = 0; i < record->count(); i++) { | 45 for (unsigned i = 0; i < record->count(); i++) { |
41 record->mutate(i, *this); | 46 record->mutate<void>(i, *this); |
42 } | 47 } |
43 } | 48 } |
44 }; | 49 }; |
45 template <> void Stretch::operator()(SkRecords::DrawRect* record) { | |
46 record->rect.fRight *= 2; | |
47 record->rect.fBottom *= 2; | |
48 } | |
49 | 50 |
50 // Basic tests for the low-level SkRecord code. | 51 // Basic tests for the low-level SkRecord code. |
51 DEF_TEST(Record, r) { | 52 DEF_TEST(Record, r) { |
52 SkRecord record; | 53 SkRecord record; |
53 | 54 |
54 // Add a simple DrawRect command. | 55 // Add a simple DrawRect command. |
55 SkRect rect = SkRect::MakeWH(10, 10); | 56 SkRect rect = SkRect::MakeWH(10, 10); |
56 SkPaint paint; | 57 SkPaint paint; |
57 SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRe
ct, (rect, paint)); | 58 SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRe
ct, (rect, paint)); |
58 | 59 |
59 // Its area should be 100. | 60 // Its area should be 100. |
60 AreaSummer summer; | 61 AreaSummer summer; |
61 summer.apply(record); | 62 summer.apply(record); |
62 REPORTER_ASSERT(r, summer.area() == 100); | 63 REPORTER_ASSERT(r, summer.area() == 100); |
63 | 64 |
64 // Scale 2x. | 65 // Scale 2x. |
65 Stretch stretch; | 66 Stretch stretch; |
66 stretch.apply(&record); | 67 stretch.apply(&record); |
67 | 68 |
68 // Now its area should be 100 + 400. | 69 // Now its area should be 100 + 400. |
69 summer.apply(record); | 70 summer.apply(record); |
70 REPORTER_ASSERT(r, summer.area() == 500); | 71 REPORTER_ASSERT(r, summer.area() == 500); |
71 } | 72 } |
OLD | NEW |