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" |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 14 | #include "SkRecordAnalysis.h" |
commit-bot@chromium.org | 066a28d | 2014-04-08 17:31:08 +0000 | [diff] [blame] | 15 | #include "SkRecords.h" |
| 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) { |
| 31 | for (unsigned i = 0; i < record.count(); i++) { |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 32 | record.visit<void>(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) { |
| 49 | for (unsigned i = 0; i < record->count(); i++) { |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 50 | record->mutate<void>(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 | |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 55 | #define APPEND(record, type, ...) SkNEW_PLACEMENT_ARGS(record.append<type>(), type, (__VA_ARGS__)) |
| 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 | |
| 80 | DEF_TEST(RecordAnalysis, r) { |
| 81 | SkRecord record; |
| 82 | |
| 83 | SkRect rect = SkRect::MakeWH(10, 10); |
| 84 | SkPaint paint; |
| 85 | APPEND(record, SkRecords::DrawRect, paint, rect); |
| 86 | REPORTER_ASSERT(r, !SkRecordWillPlaybackBitmaps(record)); |
| 87 | |
| 88 | SkBitmap bitmap; |
| 89 | APPEND(record, SkRecords::DrawBitmap, &paint, bitmap, 0.0f, 0.0f); |
| 90 | REPORTER_ASSERT(r, SkRecordWillPlaybackBitmaps(record)); |
| 91 | |
| 92 | SkNEW_PLACEMENT_ARGS(record.replace<SkRecords::DrawRect>(1), |
| 93 | SkRecords::DrawRect, (paint, rect)); |
| 94 | REPORTER_ASSERT(r, !SkRecordWillPlaybackBitmaps(record)); |
| 95 | |
| 96 | SkPaint paint2; |
| 97 | // CreateBitmapShader is too smart for us; an empty (or 1x1) bitmap shader |
| 98 | // gets optimized into a non-bitmap form, so we create a 2x2 bitmap here. |
| 99 | SkBitmap bitmap2; |
| 100 | bitmap2.allocPixels(SkImageInfo::MakeN32Premul(2, 2)); |
| 101 | bitmap2.eraseColor(SK_ColorBLUE); |
| 102 | *(bitmap2.getAddr32(0, 0)) = SK_ColorGREEN; |
| 103 | SkShader* shader = SkShader::CreateBitmapShader(bitmap2, SkShader::kClamp_TileMode, |
| 104 | SkShader::kClamp_TileMode); |
mtklein | 06d3771 | 2014-07-07 07:00:14 -0700 | [diff] [blame] | 105 | paint2.setShader(shader)->unref(); |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 106 | REPORTER_ASSERT(r, shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType); |
| 107 | |
| 108 | APPEND(record, SkRecords::DrawRect, paint2, rect); |
| 109 | REPORTER_ASSERT(r, SkRecordWillPlaybackBitmaps(record)); |
| 110 | } |
| 111 | |
| 112 | #undef APPEND |
| 113 | |