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