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 | |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkImageInfo.h" |
| 12 | #include "SkShader.h" |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 13 | #include "SkRecord.h" |
| 14 | #include "SkRecords.h" |
| 15 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 16 | // Sums the area of any DrawRect command it sees. |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 17 | class AreaSummer { |
| 18 | public: |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 19 | AreaSummer() : fArea(0) {} |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 20 | |
| 21 | template <typename T> void operator()(const T&) { } |
| 22 | |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 23 | void operator()(const SkRecords::DrawRect& draw) { |
| 24 | fArea += (int)(draw.rect.width() * draw.rect.height()); |
| 25 | } |
| 26 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 27 | int area() const { return fArea; } |
| 28 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 29 | void apply(const SkRecord& record) { |
| 30 | for (unsigned i = 0; i < record.count(); i++) { |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 31 | record.visit<void>(i, *this); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 35 | private: |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 36 | int fArea; |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 37 | }; |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 38 | |
| 39 | // Scales out the bottom-right corner of any DrawRect command it sees by 2x. |
| 40 | struct Stretch { |
| 41 | template <typename T> void operator()(T*) {} |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 42 | void operator()(SkRecords::DrawRect* draw) { |
| 43 | draw->rect.fRight *= 2; |
| 44 | draw->rect.fBottom *= 2; |
| 45 | } |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 46 | |
| 47 | void apply(SkRecord* record) { |
| 48 | for (unsigned i = 0; i < record->count(); i++) { |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 49 | record->mutate<void>(i, *this); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 52 | }; |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 53 | |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 54 | #define APPEND(record, type, ...) SkNEW_PLACEMENT_ARGS(record.append<type>(), type, (__VA_ARGS__)) |
| 55 | |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 56 | // Basic tests for the low-level SkRecord code. |
| 57 | DEF_TEST(Record, r) { |
| 58 | SkRecord record; |
| 59 | |
| 60 | // Add a simple DrawRect command. |
| 61 | SkRect rect = SkRect::MakeWH(10, 10); |
| 62 | SkPaint paint; |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 63 | APPEND(record, SkRecords::DrawRect, paint, rect); |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 64 | |
| 65 | // Its area should be 100. |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 66 | AreaSummer summer; |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 67 | summer.apply(record); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 68 | REPORTER_ASSERT(r, summer.area() == 100); |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 69 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 70 | // Scale 2x. |
| 71 | Stretch stretch; |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 72 | stretch.apply(&record); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 73 | |
| 74 | // Now its area should be 100 + 400. |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 75 | summer.apply(record); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 76 | REPORTER_ASSERT(r, summer.area() == 500); |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 77 | } |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 78 | |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 79 | #undef APPEND |
| 80 | |
mtklein | 0209e95 | 2014-08-28 14:10:05 -0700 | [diff] [blame] | 81 | template <typename T> |
| 82 | static bool is_aligned(const T* p) { |
| 83 | return (((uintptr_t)p) & (sizeof(T) - 1)) == 0; |
| 84 | } |
| 85 | |
| 86 | DEF_TEST(Record_Alignment, r) { |
| 87 | SkRecord record; |
mtklein | 0209e95 | 2014-08-28 14:10:05 -0700 | [diff] [blame] | 88 | REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>())); |
mtklein | 0209e95 | 2014-08-28 14:10:05 -0700 | [diff] [blame] | 89 | REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>())); |
| 90 | REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>())); |
mtklein | 0209e95 | 2014-08-28 14:10:05 -0700 | [diff] [blame] | 91 | REPORTER_ASSERT(r, is_aligned(record.alloc<void*>())); |
| 92 | |
mtklein | f2950b1 | 2014-11-13 12:41:14 -0800 | [diff] [blame] | 93 | // It's not clear if we care that 8-byte values are aligned on 32-bit machines. |
| 94 | if (sizeof(void*) == 8) { |
| 95 | REPORTER_ASSERT(r, is_aligned(record.alloc<double>())); |
| 96 | REPORTER_ASSERT(r, is_aligned(record.alloc<uint64_t>())); |
| 97 | } |
mtklein | 0209e95 | 2014-08-28 14:10:05 -0700 | [diff] [blame] | 98 | } |
| 99 | |