blob: d81bf059ef34e38659910c52538df15c658651de [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
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +00008#include "Test.h"
9
reed2347b622014-08-07 12:19:50 -070010#include "SkPictureRecorder.h"
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000011#include "SkRecord.h"
12#include "SkRecorder.h"
13#include "SkRecords.h"
reede1085e02014-07-03 07:26:01 -070014#include "SkShader.h"
piotaixr65151752014-10-16 11:58:39 -070015#include "SkSurface.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) {
33 for (unsigned i = 0; i < record.count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000034 record.visit<void>(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
mtklein5f0e8222014-08-22 11:44:26 -070053// All of Skia will work fine without support for comment groups, but
54// Chrome's inspector can break. This serves as a simple regression test.
55DEF_TEST(Recorder_CommentGroups, r) {
56 SkRecord record;
57 SkRecorder recorder(&record, 1920, 1080);
58
59 recorder.beginCommentGroup("test");
60 recorder.addComment("foo", "bar");
61 recorder.addComment("baz", "quux");
62 recorder.endCommentGroup();
63
64 Tally tally;
65 tally.apply(record);
66
67 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::BeginCommentGroup>());
68 REPORTER_ASSERT(r, 2 == tally.count<SkRecords::AddComment>());
69 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::EndCommentGroup>());
70}
71
mtklein29dfaa82014-09-04 14:12:44 -070072// DrawData is similar to comment groups. It doesn't affect drawing, but
73// it's a pass-through we provide to the client. Again, a simple reg. test.
74DEF_TEST(Recorder_DrawData, r) {
75 SkRecord record;
76 SkRecorder recorder(&record, 100, 100);
77
78 const char* data = "This sure is some data, eh?";
79 recorder.drawData(data, strlen(data));
80
81 Tally tally;
82 tally.apply(record);
83 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawData>());
84}
85
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000086// Regression test for leaking refs held by optional arguments.
87DEF_TEST(Recorder_RefLeaking, r) {
88 // We use SaveLayer to test:
89 // - its SkRect argument is optional and SkRect is POD. Just testing that that works.
90 // - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
91
commit-bot@chromium.org12a04122014-04-15 18:00:57 +000092 SkRect bounds = SkRect::MakeWH(320, 240);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000093 SkPaint paint;
reede1085e02014-07-03 07:26:01 -070094 paint.setShader(SkShader::CreateEmptyShader())->unref();
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000095
96 REPORTER_ASSERT(r, paint.getShader()->unique());
97 {
98 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000099 SkRecorder recorder(&record, 1920, 1080);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000100 recorder.saveLayer(&bounds, &paint);
101 REPORTER_ASSERT(r, !paint.getShader()->unique());
102 }
103 REPORTER_ASSERT(r, paint.getShader()->unique());
104}
reed2347b622014-08-07 12:19:50 -0700105
106DEF_TEST(Recorder_RefPictures, r) {
107 SkAutoTUnref<SkPicture> pic;
108
109 {
110 SkPictureRecorder pr;
111 SkCanvas* canvas = pr.beginRecording(100, 100);
112 canvas->drawColor(SK_ColorRED);
113 pic.reset(pr.endRecording());
114 }
115 REPORTER_ASSERT(r, pic->unique());
116
117 {
118 SkRecord record;
119 SkRecorder recorder(&record, 100, 100);
120 recorder.drawPicture(pic);
121 // the recorder should now also be an owner
122 REPORTER_ASSERT(r, !pic->unique());
123 }
124 // the recorder destructor should have released us (back to unique)
125 REPORTER_ASSERT(r, pic->unique());
126}
mtklein29dfaa82014-09-04 14:12:44 -0700127
128DEF_TEST(Recorder_IsDrawingToLayer, r) {
129 SkRecord record;
130 SkRecorder recorder(&record, 100, 100);
131
132 // We'll save, saveLayer, save, and saveLayer, then restore them all,
133 // checking that isDrawingToLayer() is correct at each step.
134
135 REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
136 recorder.save();
137 REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
138 recorder.saveLayer(NULL, NULL);
139 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
140 recorder.save();
141 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
142 recorder.saveLayer(NULL, NULL);
143 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
144 recorder.restore();
145 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
146 recorder.restore();
147 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
148 recorder.restore();
149 REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
150 recorder.restore();
151 REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
152}
153
piotaixr65151752014-10-16 11:58:39 -0700154DEF_TEST(Recorder_drawImage_takeReference, reporter) {
155
156 SkAutoTUnref<SkImage> image;
157 {
158 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
159 surface->getCanvas()->clear(SK_ColorGREEN);
160 image.reset(surface->newImageSnapshot());
161 }
162 {
163 SkRecord record;
164 SkRecorder recorder(&record, 100, 100);
165
166 // DrawImage is supposed to take a reference
167 recorder.drawImage(image.get(), 0, 0);
168 REPORTER_ASSERT(reporter, !image->unique());
169
170 Tally tally;
171 tally.apply(record);
172
173 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>());
174 }
175 REPORTER_ASSERT(reporter, image->unique());
176
177 {
178 SkRecord record;
179 SkRecorder recorder(&record, 100, 100);
180
181 // DrawImageRect is supposed to take a reference
182 recorder.drawImageRect(image.get(), 0, SkRect::MakeWH(100, 100));
183 REPORTER_ASSERT(reporter, !image->unique());
184
185 Tally tally;
186 tally.apply(record);
187
188 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>());
189 }
190 REPORTER_ASSERT(reporter, image->unique());
191}