Flutter Engine
The Flutter Engine
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
102 }
104
105 {
106 SkRecord record;
107 SkRecorder recorder(&record, 100, 100);
108
109 // DrawImageRect is supposed to take a reference
110 recorder.drawImageRect(image.get(), SkRect::MakeWH(100, 100), SkRect::MakeWH(100, 100),
113
114 Tally tally;
115 tally.apply(record);
116
118 }
120}
121
122// skbug.com/10997
123DEF_TEST(Recorder_boundsOverflow, reporter) {
125
126 SkRecord record;
127 SkRecorder recorder(&record, bigBounds);
128 REPORTER_ASSERT(reporter, recorder.imageInfo().width() > 0 &&
129 recorder.imageInfo().height() > 0);
130}
sk_bzero(glyphs, sizeof(glyphs))
reporter
Definition: FontMgrTest.cpp:39
static const int kRecordTypes
#define COUNT(T)
DEF_TEST(Recorder, r)
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define SK_RECORD_TYPES(M)
Definition: SkRecords.h:56
#define SK_ScalarMin
Definition: SkScalar.h:25
#define SK_ScalarMax
Definition: SkScalar.h:24
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
@ 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)
Definition: SkCanvas.cpp:2333
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
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
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkShader > Empty()
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API void DrawImageRect(SkCanvas *canvas, const SkImage *image, const SkRect &src, const SkRect &dst, const SkSamplingOptions &sampling={}, const SkPaint *paint=nullptr, SkCanvas::SrcRectConstraint constraint=SkCanvas::kFast_SrcRectConstraint)
#define T
Definition: precompiler.cc:65
static SkImageInfo MakeN32Premul(int width, int height)
int width() const
Definition: SkImageInfo.h:365
int height() const
Definition: SkImageInfo.h:371
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609