Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RecordTest.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
10#include "src/core/SkRecord.h"
11#include "src/core/SkRecords.h"
13#include "tests/Test.h"
14
15#include <cstdint>
16#include <new>
17
18// Sums the area of any DrawRect command it sees.
20public:
21 AreaSummer() : fArea(0) {}
22
23 template <typename T> void operator()(const T&) { }
24
25 void operator()(const SkRecords::DrawRect& draw) {
26 fArea += (int)(draw.rect.width() * draw.rect.height());
27 }
28
29 int area() const { return fArea; }
30
31 void apply(const SkRecord& record) {
32 for (int i = 0; i < record.count(); i++) {
33 record.visit(i, *this);
34 }
35 }
36
37private:
38 int fArea;
39};
40
41// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
42struct Stretch {
43 template <typename T> void operator()(T*) {}
44 void operator()(SkRecords::DrawRect* draw) {
45 draw->rect.fRight *= 2;
46 draw->rect.fBottom *= 2;
47 }
48
49 void apply(SkRecord* record) {
50 for (int i = 0; i < record->count(); i++) {
51 record->mutate(i, *this);
52 }
53 }
54};
55
56#define APPEND(record, type, ...) new (record.append<type>()) type{__VA_ARGS__}
57
58// Basic tests for the low-level SkRecord code.
59DEF_TEST(Record, r) {
60 SkRecord record;
61
62 // Add a simple DrawRect command.
63 SkRect rect = SkRect::MakeWH(10, 10);
65 APPEND(record, SkRecords::DrawRect, paint, rect);
66
67 // Its area should be 100.
68 AreaSummer summer;
69 summer.apply(record);
70 REPORTER_ASSERT(r, summer.area() == 100);
71
72 // Scale 2x.
73 Stretch stretch;
74 stretch.apply(&record);
75
76 // Now its area should be 100 + 400.
77 summer.apply(record);
78 REPORTER_ASSERT(r, summer.area() == 500);
79}
80
81DEF_TEST(Record_defrag, r) {
82 SkRecord record;
83 APPEND(record, SkRecords::Save);
84 APPEND(record, SkRecords::ClipRect);
85 APPEND(record, SkRecords::NoOp);
86 APPEND(record, SkRecords::DrawRect);
87 APPEND(record, SkRecords::NoOp);
88 APPEND(record, SkRecords::NoOp);
89 APPEND(record, SkRecords::Restore);
90 REPORTER_ASSERT(r, record.count() == 7);
91
92 record.defrag();
93 REPORTER_ASSERT(r, record.count() == 4);
94 assert_type<SkRecords::Save >(r, record, 0);
95 assert_type<SkRecords::ClipRect>(r, record, 1);
96 assert_type<SkRecords::DrawRect>(r, record, 2);
97 assert_type<SkRecords::Restore >(r, record, 3);
98}
99
100#undef APPEND
101
102template <typename T>
103static bool is_aligned(const T* p) {
104 return (((uintptr_t)p) & (sizeof(T) - 1)) == 0;
105}
106
107DEF_TEST(Record_Alignment, r) {
108 SkRecord record;
109 REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>()));
110 REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>()));
111 REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>()));
112 REPORTER_ASSERT(r, is_aligned(record.alloc<void*>()));
113
114 // It's not clear if we care that 8-byte values are aligned on 32-bit machines.
115 if (sizeof(void*) == 8) {
116 REPORTER_ASSERT(r, is_aligned(record.alloc<double>()));
117 REPORTER_ASSERT(r, is_aligned(record.alloc<uint64_t>()));
118 }
119}
#define APPEND(record, type,...)
static bool is_aligned(const T *p)
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
Type::kYUV Type::kRGBA() int(0.7 *637)
void operator()(const T &)
void apply(const SkRecord &record)
void operator()(const SkRecords::DrawRect &draw)
int area() const
auto visit(int i, F &&f) const -> decltype(f(SkRecords::NoOp()))
Definition SkRecord.h:45
T * alloc(size_t count=1)
Definition SkRecord.h:61
auto mutate(int i, F &&f) -> decltype(f((SkRecords::NoOp *) nullptr))
Definition SkRecord.h:54
int count() const
Definition SkRecord.h:38
void defrag()
Definition SkRecord.cpp:30
const Paint & paint
#define T
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
void operator()(SkRecords::DrawRect *draw)
void apply(SkRecord *record)
void operator()(T *)