blob: 3a21a34a6c31534d58f912c47e541d50abf49c17 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +00009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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.org653d5182014-04-15 14:27:14 +000016
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000017#define COUNT(T) + 1
18static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
19#undef COUNT
20
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000021// Tallies the types of commands it sees into a histogram.
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000022class Tally {
23public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000024 Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000025
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000026 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.orgb7369622014-04-08 20:17:26 +000031
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000032 void apply(const SkRecord& record) {
mtkleinc6ad06a2015-08-19 09:51:00 -070033 for (int i = 0; i < record.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -070034 record.visit(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000035 }
36 }
37
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000038private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000039 int fHistogram[kRecordTypes];
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000040};
41
42DEF_TEST(Recorder, r) {
43 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000044 SkRecorder recorder(&record, 1920, 1080);
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000045
46 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
47
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000048 Tally tally;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000049 tally.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000050 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000051}
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000052
53// Regression test for leaking refs held by optional arguments.
54DEF_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.org12a04122014-04-15 18:00:57 +000059 SkRect bounds = SkRect::MakeWH(320, 240);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000060 SkPaint paint;
Mike Reedc8bea7d2019-04-09 13:55:36 -040061 paint.setShader(SkShaders::Empty());
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000062
63 REPORTER_ASSERT(r, paint.getShader()->unique());
64 {
65 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000066 SkRecorder recorder(&record, 1920, 1080);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000067 recorder.saveLayer(&bounds, &paint);
68 REPORTER_ASSERT(r, !paint.getShader()->unique());
69 }
70 REPORTER_ASSERT(r, paint.getShader()->unique());
71}
reed2347b622014-08-07 12:19:50 -070072
piotaixr65151752014-10-16 11:58:39 -070073DEF_TEST(Recorder_drawImage_takeReference, reporter) {
74
reed9ce9d672016-03-17 10:51:11 -070075 sk_sp<SkImage> image;
piotaixr65151752014-10-16 11:58:39 -070076 {
reede8f30622016-03-23 18:59:25 -070077 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
piotaixr65151752014-10-16 11:58:39 -070078 surface->getCanvas()->clear(SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -070079 image = surface->makeImageSnapshot();
piotaixr65151752014-10-16 11:58:39 -070080 }
Mike Reed4f23dec2020-12-30 14:22:42 +000081
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 Reede02d7f82021-01-21 22:25:21 -050093 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>());
Mike Reed4f23dec2020-12-30 14:22:42 +000094 }
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 Reede02d7f82021-01-21 22:25:21 -0500109 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>());
Mike Reed4f23dec2020-12-30 14:22:42 +0000110 }
111 REPORTER_ASSERT(reporter, image->unique());
piotaixr65151752014-10-16 11:58:39 -0700112}
Michael Ludwigf2efb802020-11-25 14:29:30 -0500113
114// skbug.com/10997
115DEF_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}