Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RecorderTest.cpp
Go to the documentation of this file.
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
13#include "include/core/SkRect.h"
20#include "src/core/SkRecord.h"
21#include "src/core/SkRecorder.h"
22#include "src/core/SkRecords.h"
23#include "tests/Test.h"
24
25#define COUNT(T) + 1
27#undef COUNT
28
29// Tallies the types of commands it sees into a histogram.
30class Tally {
31public:
32 Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
33
34 template <typename T>
35 void operator()(const T&) { ++fHistogram[T::kType]; }
36
37 template <typename T>
38 int count() const { return fHistogram[T::kType]; }
39
40 void apply(const SkRecord& record) {
41 for (int i = 0; i < record.count(); i++) {
42 record.visit(i, *this);
43 }
44 }
45
46private:
47 int fHistogram[kRecordTypes];
48};
49
51 SkRecord record;
52 SkRecorder recorder(&record, 1920, 1080);
53
54 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
55
56 Tally tally;
57 tally.apply(record);
58 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
59}
60
61// Regression test for leaking refs held by optional arguments.
62DEF_TEST(Recorder_RefLeaking, r) {
63 // We use SaveLayer to test:
64 // - its SkRect argument is optional and SkRect is POD. Just testing that that works.
65 // - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
66
67 SkRect bounds = SkRect::MakeWH(320, 240);
69 paint.setShader(SkShaders::Empty());
70
71 REPORTER_ASSERT(r, paint.getShader()->unique());
72 {
73 SkRecord record;
74 SkRecorder recorder(&record, 1920, 1080);
75 recorder.saveLayer(&bounds, &paint);
76 REPORTER_ASSERT(r, !paint.getShader()->unique());
77 }
78 REPORTER_ASSERT(r, paint.getShader()->unique());
79}
80
81DEF_TEST(Recorder_drawImage_takeReference, reporter) {
82
84 {
86 surface->getCanvas()->clear(SK_ColorGREEN);
87 image = surface->makeImageSnapshot();
88 }
89
90 {
91 SkRecord record;
92 SkRecorder recorder(&record, 100, 100);
93
94 // DrawImage is supposed to take a reference
95 recorder.drawImage(image.get(), 0, 0, SkSamplingOptions());
97
98 Tally tally;
99 tally.apply(record);
100
101#if defined(SK_RESOLVE_FILTERS_BEFORE_RESTORE)
102 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>());
103#else
104 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>());
105#endif
106 }
108
109 {
110 SkRecord record;
111 SkRecorder recorder(&record, 100, 100);
112
113 // DrawImageRect is supposed to take a reference
114 recorder.drawImageRect(image.get(), SkRect::MakeWH(100, 100), SkRect::MakeWH(100, 100),
117
118 Tally tally;
119 tally.apply(record);
120
121 REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>());
122 }
124}
125
126// skbug.com/10997
127DEF_TEST(Recorder_boundsOverflow, reporter) {
129
130 SkRecord record;
131 SkRecorder recorder(&record, bigBounds);
132 REPORTER_ASSERT(reporter, recorder.imageInfo().width() > 0 &&
133 recorder.imageInfo().height() > 0);
134}
reporter
static const int kRecordTypes
#define COUNT(T)
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static void sk_bzero(void *buffer, size_t size)
Definition SkMalloc.h:105
#define SK_RECORD_TYPES(M)
Definition SkRecords.h:56
#define SK_ScalarMin
Definition SkScalar.h:25
#define SK_ScalarMax
Definition SkScalar.h:24
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition SkCanvas.h:1543
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
SkImageInfo imageInfo() const
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
auto visit(int i, F &&f) const -> decltype(f(SkRecords::NoOp()))
Definition SkRecord.h:45
int count() const
Definition SkRecord.h:38
bool unique() const
Definition SkRefCnt.h:50
void apply(const SkRecord &record)
void operator()(const T &)
int count() const
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
#define T
static SkImageInfo MakeN32Premul(int width, int height)
int width() const
int height() const
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609