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 | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
reed | 2347b62 | 2014-08-07 12:19:50 -0700 | [diff] [blame] | 10 | #include "SkPictureRecorder.h" |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 11 | #include "SkRecord.h" |
| 12 | #include "SkRecorder.h" |
| 13 | #include "SkRecords.h" |
reed | e1085e0 | 2014-07-03 07:26:01 -0700 | [diff] [blame] | 14 | #include "SkShader.h" |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 15 | #include "SkSurface.h" |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 16 | |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 17 | #define COUNT(T) + 1 |
| 18 | static const int kRecordTypes = SK_RECORD_TYPES(COUNT); |
| 19 | #undef COUNT |
| 20 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 21 | // Tallies the types of commands it sees into a histogram. |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 22 | class Tally { |
| 23 | public: |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 24 | Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); } |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 25 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 26 | template <typename T> |
| 27 | void operator()(const T&) { ++fHistogram[T::kType]; } |
| 28 | |
| 29 | template <typename T> |
| 30 | int count() const { return fHistogram[T::kType]; } |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 32 | void apply(const SkRecord& record) { |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 33 | for (int i = 0; i < record.count(); i++) { |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 34 | record.visit<void>(i, *this); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 38 | private: |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 39 | int fHistogram[kRecordTypes]; |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | DEF_TEST(Recorder, r) { |
| 43 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 44 | SkRecorder recorder(&record, 1920, 1080); |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 45 | |
| 46 | recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); |
| 47 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 48 | Tally tally; |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 49 | tally.apply(record); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 50 | REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>()); |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 51 | } |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 52 | |
| 53 | // Regression test for leaking refs held by optional arguments. |
| 54 | DEF_TEST(Recorder_RefLeaking, r) { |
| 55 | // We use SaveLayer to test: |
| 56 | // - its SkRect argument is optional and SkRect is POD. Just testing that that works. |
| 57 | // - its SkPaint argument is optional and SkPaint is not POD. The bug was here. |
| 58 | |
commit-bot@chromium.org | 12a0412 | 2014-04-15 18:00:57 +0000 | [diff] [blame] | 59 | SkRect bounds = SkRect::MakeWH(320, 240); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 60 | SkPaint paint; |
reed | e1085e0 | 2014-07-03 07:26:01 -0700 | [diff] [blame] | 61 | paint.setShader(SkShader::CreateEmptyShader())->unref(); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 62 | |
| 63 | REPORTER_ASSERT(r, paint.getShader()->unique()); |
| 64 | { |
| 65 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 66 | SkRecorder recorder(&record, 1920, 1080); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 67 | recorder.saveLayer(&bounds, &paint); |
| 68 | REPORTER_ASSERT(r, !paint.getShader()->unique()); |
| 69 | } |
| 70 | REPORTER_ASSERT(r, paint.getShader()->unique()); |
| 71 | } |
reed | 2347b62 | 2014-08-07 12:19:50 -0700 | [diff] [blame] | 72 | |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 73 | DEF_TEST(Recorder_drawImage_takeReference, reporter) { |
| 74 | |
| 75 | SkAutoTUnref<SkImage> image; |
| 76 | { |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 77 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100)); |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 78 | surface->getCanvas()->clear(SK_ColorGREEN); |
| 79 | image.reset(surface->newImageSnapshot()); |
| 80 | } |
| 81 | { |
| 82 | SkRecord record; |
| 83 | SkRecorder recorder(&record, 100, 100); |
| 84 | |
| 85 | // DrawImage is supposed to take a reference |
| 86 | recorder.drawImage(image.get(), 0, 0); |
| 87 | REPORTER_ASSERT(reporter, !image->unique()); |
| 88 | |
| 89 | Tally tally; |
| 90 | tally.apply(record); |
| 91 | |
| 92 | REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>()); |
| 93 | } |
| 94 | REPORTER_ASSERT(reporter, image->unique()); |
| 95 | |
| 96 | { |
| 97 | SkRecord record; |
| 98 | SkRecorder recorder(&record, 100, 100); |
| 99 | |
| 100 | // DrawImageRect is supposed to take a reference |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 101 | recorder.drawImageRect(image.get(), SkRect::MakeWH(100, 100), nullptr); |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, !image->unique()); |
| 103 | |
| 104 | Tally tally; |
| 105 | tally.apply(record); |
| 106 | |
| 107 | REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>()); |
| 108 | } |
| 109 | REPORTER_ASSERT(reporter, image->unique()); |
| 110 | } |