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