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