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