1 /*
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 
8 #include "Test.h"
9 
10 #include "SkPictureRecorder.h"
11 #include "SkRecord.h"
12 #include "SkRecorder.h"
13 #include "SkRecords.h"
14 #include "SkShader.h"
15 #include "SkSurface.h"
16 
17 #define COUNT(T) + 1
18 static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
19 #undef COUNT
20 
21 // Tallies the types of commands it sees into a histogram.
22 class Tally {
23 public:
Tally()24     Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
25 
26     template <typename T>
operator ()(const T &)27     void operator()(const T&) { ++fHistogram[T::kType]; }
28 
29     template <typename T>
count() const30     int count() const { return fHistogram[T::kType]; }
31 
apply(const SkRecord & record)32     void apply(const SkRecord& record) {
33         for (int i = 0; i < record.count(); i++) {
34             record.visit<void>(i, *this);
35         }
36     }
37 
38 private:
39     int fHistogram[kRecordTypes];
40 };
41 
DEF_TEST(Recorder,r)42 DEF_TEST(Recorder, r) {
43     SkRecord record;
44     SkRecorder recorder(&record, 1920, 1080);
45 
46     recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
47 
48     Tally tally;
49     tally.apply(record);
50     REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
51 }
52 
53 // Regression test for leaking refs held by optional arguments.
DEF_TEST(Recorder_RefLeaking,r)54 DEF_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 
59     SkRect bounds = SkRect::MakeWH(320, 240);
60     SkPaint paint;
61     paint.setShader(SkShader::CreateEmptyShader())->unref();
62 
63     REPORTER_ASSERT(r, paint.getShader()->unique());
64     {
65         SkRecord record;
66         SkRecorder recorder(&record, 1920, 1080);
67         recorder.saveLayer(&bounds, &paint);
68         REPORTER_ASSERT(r, !paint.getShader()->unique());
69     }
70     REPORTER_ASSERT(r, paint.getShader()->unique());
71 }
72 
DEF_TEST(Recorder_drawImage_takeReference,reporter)73 DEF_TEST(Recorder_drawImage_takeReference, reporter) {
74 
75     SkAutoTUnref<SkImage> image;
76     {
77         SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
78         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(), SkRect::MakeWH(100, 100), nullptr);
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 }
111