blob: 21a897ca8b1a5e3b10ec759f758cf7dae8381833 [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
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000071// Regression test for leaking refs held by optional arguments.
72DEF_TEST(Recorder_RefLeaking, r) {
73 // We use SaveLayer to test:
74 // - its SkRect argument is optional and SkRect is POD. Just testing that that works.
75 // - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
76
commit-bot@chromium.org12a04122014-04-15 18:00:57 +000077 SkRect bounds = SkRect::MakeWH(320, 240);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000078 SkPaint paint;
reede1085e02014-07-03 07:26:01 -070079 paint.setShader(SkShader::CreateEmptyShader())->unref();
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000080
81 REPORTER_ASSERT(r, paint.getShader()->unique());
82 {
83 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000084 SkRecorder recorder(&record, 1920, 1080);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000085 recorder.saveLayer(&bounds, &paint);
86 REPORTER_ASSERT(r, !paint.getShader()->unique());
87 }
88 REPORTER_ASSERT(r, paint.getShader()->unique());
89}
reed2347b622014-08-07 12:19:50 -070090
91DEF_TEST(Recorder_RefPictures, r) {
92 SkAutoTUnref<SkPicture> pic;
93
94 {
95 SkPictureRecorder pr;
96 SkCanvas* canvas = pr.beginRecording(100, 100);
97 canvas->drawColor(SK_ColorRED);
98 pic.reset(pr.endRecording());
99 }
100 REPORTER_ASSERT(r, pic->unique());
101
102 {
103 SkRecord record;
104 SkRecorder recorder(&record, 100, 100);
105 recorder.drawPicture(pic);
106 // the recorder should now also be an owner
107 REPORTER_ASSERT(r, !pic->unique());
108 }
109 // the recorder destructor should have released us (back to unique)
110 REPORTER_ASSERT(r, pic->unique());
111}