blob: 829fe3e229d12c69c20957bd4e9dc081558b34d6 [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
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;
reede1085e02014-07-03 07:26:01 -070061 paint.setShader(SkShader::CreateEmptyShader())->unref();
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
75 SkAutoTUnref<SkImage> image;
76 {
reed3054be12014-12-10 07:24:28 -080077 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
piotaixr65151752014-10-16 11:58:39 -070078 surface->getCanvas()->clear(SK_ColorGREEN);
79 image.reset(surface->newImageSnapshot());
80 }
81 {
82 SkRecord record;
83 SkRecorder recorder(&record, 100, 100);
84
85 // DrawImage is supposed to take a reference
86 recorder.drawImage(image.get(), 0, 0);
87 REPORTER_ASSERT(reporter, !image->unique());
88
89 Tally tally;
90 tally.apply(record);
91
92 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>());
93 }
94 REPORTER_ASSERT(reporter, image->unique());
95
96 {
97 SkRecord record;
98 SkRecorder recorder(&record, 100, 100);
99
100 // DrawImageRect is supposed to take a reference
101 recorder.drawImageRect(image.get(), 0, SkRect::MakeWH(100, 100));
102 REPORTER_ASSERT(reporter, !image->unique());
103
104 Tally tally;
105 tally.apply(record);
106
107 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>());
108 }
109 REPORTER_ASSERT(reporter, image->unique());
110}