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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPictureRecorder.h" |
| 11 | #include "include/core/SkShader.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "src/core/SkRecord.h" |
| 14 | #include "src/core/SkRecorder.h" |
| 15 | #include "src/core/SkRecords.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++) { |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 34 | record.visit(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; |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 61 | paint.setShader(SkShaders::Empty()); |
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 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 75 | sk_sp<SkImage> image; |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 76 | { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 77 | auto surface(SkSurface::MakeRasterN32Premul(100, 100)); |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 78 | surface->getCanvas()->clear(SK_ColorGREEN); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 79 | image = surface->makeImageSnapshot(); |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 80 | } |
Mike Reed | 4f23dec | 2020-12-30 14:22:42 +0000 | [diff] [blame] | 81 | |
| 82 | { |
| 83 | SkRecord record; |
| 84 | SkRecorder recorder(&record, 100, 100); |
| 85 | |
| 86 | // DrawImage is supposed to take a reference |
| 87 | recorder.drawImage(image.get(), 0, 0, SkSamplingOptions()); |
| 88 | REPORTER_ASSERT(reporter, !image->unique()); |
| 89 | |
| 90 | Tally tally; |
| 91 | tally.apply(record); |
| 92 | |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 93 | REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>()); |
Mike Reed | 4f23dec | 2020-12-30 14:22:42 +0000 | [diff] [blame] | 94 | } |
| 95 | REPORTER_ASSERT(reporter, image->unique()); |
| 96 | |
| 97 | { |
| 98 | SkRecord record; |
| 99 | SkRecorder recorder(&record, 100, 100); |
| 100 | |
| 101 | // DrawImageRect is supposed to take a reference |
| 102 | recorder.drawImageRect(image.get(), SkRect::MakeWH(100, 100), SkRect::MakeWH(100, 100), |
| 103 | SkSamplingOptions(), nullptr, SkCanvas::kFast_SrcRectConstraint); |
| 104 | REPORTER_ASSERT(reporter, !image->unique()); |
| 105 | |
| 106 | Tally tally; |
| 107 | tally.apply(record); |
| 108 | |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>()); |
Mike Reed | 4f23dec | 2020-12-30 14:22:42 +0000 | [diff] [blame] | 110 | } |
| 111 | REPORTER_ASSERT(reporter, image->unique()); |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 112 | } |
Michael Ludwig | f2efb80 | 2020-11-25 14:29:30 -0500 | [diff] [blame] | 113 | |
| 114 | // skbug.com/10997 |
| 115 | DEF_TEST(Recorder_boundsOverflow, reporter) { |
| 116 | SkRect bigBounds = {SK_ScalarMin, SK_ScalarMin, SK_ScalarMax, SK_ScalarMax}; |
| 117 | |
| 118 | SkRecord record; |
| 119 | SkRecorder recorder(&record, bigBounds); |
| 120 | REPORTER_ASSERT(reporter, recorder.imageInfo().width() > 0 && |
| 121 | recorder.imageInfo().height() > 0); |
| 122 | } |