blob: aced54f7a906fa32c56ffc5ee25ad8b10fd8fd1f [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"
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000015
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000016#define COUNT(T) + 1
17static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
18#undef COUNT
19
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000020// Tallies the types of commands it sees into a histogram.
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000021class Tally {
22public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000023 Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000024
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000025 template <typename T>
26 void operator()(const T&) { ++fHistogram[T::kType]; }
27
28 template <typename T>
29 int count() const { return fHistogram[T::kType]; }
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000030
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000031 void apply(const SkRecord& record) {
32 for (unsigned i = 0; i < record.count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000033 record.visit<void>(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000034 }
35 }
36
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000037private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000038 int fHistogram[kRecordTypes];
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000039};
40
41DEF_TEST(Recorder, r) {
42 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000043 SkRecorder recorder(&record, 1920, 1080);
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000044
45 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
46
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000047 Tally tally;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000048 tally.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000049 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000050}
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000051
mtklein5f0e8222014-08-22 11:44:26 -070052// All of Skia will work fine without support for comment groups, but
53// Chrome's inspector can break. This serves as a simple regression test.
54DEF_TEST(Recorder_CommentGroups, r) {
55 SkRecord record;
56 SkRecorder recorder(&record, 1920, 1080);
57
58 recorder.beginCommentGroup("test");
59 recorder.addComment("foo", "bar");
60 recorder.addComment("baz", "quux");
61 recorder.endCommentGroup();
62
63 Tally tally;
64 tally.apply(record);
65
66 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::BeginCommentGroup>());
67 REPORTER_ASSERT(r, 2 == tally.count<SkRecords::AddComment>());
68 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::EndCommentGroup>());
69}
70
mtklein29dfaa82014-09-04 14:12:44 -070071// DrawData is similar to comment groups. It doesn't affect drawing, but
72// it's a pass-through we provide to the client. Again, a simple reg. test.
73DEF_TEST(Recorder_DrawData, r) {
74 SkRecord record;
75 SkRecorder recorder(&record, 100, 100);
76
77 const char* data = "This sure is some data, eh?";
78 recorder.drawData(data, strlen(data));
79
80 Tally tally;
81 tally.apply(record);
82 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawData>());
83}
84
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000085// Regression test for leaking refs held by optional arguments.
86DEF_TEST(Recorder_RefLeaking, r) {
87 // We use SaveLayer to test:
88 // - its SkRect argument is optional and SkRect is POD. Just testing that that works.
89 // - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
90
commit-bot@chromium.org12a04122014-04-15 18:00:57 +000091 SkRect bounds = SkRect::MakeWH(320, 240);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000092 SkPaint paint;
reede1085e02014-07-03 07:26:01 -070093 paint.setShader(SkShader::CreateEmptyShader())->unref();
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000094
95 REPORTER_ASSERT(r, paint.getShader()->unique());
96 {
97 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000098 SkRecorder recorder(&record, 1920, 1080);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000099 recorder.saveLayer(&bounds, &paint);
100 REPORTER_ASSERT(r, !paint.getShader()->unique());
101 }
102 REPORTER_ASSERT(r, paint.getShader()->unique());
103}
reed2347b622014-08-07 12:19:50 -0700104
105DEF_TEST(Recorder_RefPictures, r) {
106 SkAutoTUnref<SkPicture> pic;
107
108 {
109 SkPictureRecorder pr;
110 SkCanvas* canvas = pr.beginRecording(100, 100);
111 canvas->drawColor(SK_ColorRED);
112 pic.reset(pr.endRecording());
113 }
114 REPORTER_ASSERT(r, pic->unique());
115
116 {
117 SkRecord record;
118 SkRecorder recorder(&record, 100, 100);
119 recorder.drawPicture(pic);
120 // the recorder should now also be an owner
121 REPORTER_ASSERT(r, !pic->unique());
122 }
123 // the recorder destructor should have released us (back to unique)
124 REPORTER_ASSERT(r, pic->unique());
125}
mtklein29dfaa82014-09-04 14:12:44 -0700126
127DEF_TEST(Recorder_IsDrawingToLayer, r) {
128 SkRecord record;
129 SkRecorder recorder(&record, 100, 100);
130
131 // We'll save, saveLayer, save, and saveLayer, then restore them all,
132 // checking that isDrawingToLayer() is correct at each step.
133
134 REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
135 recorder.save();
136 REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
137 recorder.saveLayer(NULL, NULL);
138 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
139 recorder.save();
140 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
141 recorder.saveLayer(NULL, NULL);
142 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
143 recorder.restore();
144 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
145 recorder.restore();
146 REPORTER_ASSERT(r, recorder.isDrawingToLayer());
147 recorder.restore();
148 REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
149 recorder.restore();
150 REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
151}
152