Flutter Engine
The Flutter Engine
rrect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 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 "gm/gm.h"
14#include "include/core/SkRect.h"
16#include "include/core/SkSize.h"
19#include "tools/ToolUtils.h"
20
21typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*);
22
23static void inset0(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
24 SkRect r = src.rect();
25
26 r.inset(dx, dy);
27 if (r.isEmpty()) {
28 dst->setEmpty();
29 return;
30 }
31
32 SkVector radii[4];
33 for (int i = 0; i < 4; ++i) {
34 radii[i] = src.radii((SkRRect::Corner)i);
35 }
36 for (int i = 0; i < 4; ++i) {
37 radii[i].fX -= dx;
38 radii[i].fY -= dy;
39 }
40 dst->setRectRadii(r, radii);
41}
42
43static void inset1(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
44 SkRect r = src.rect();
45
46 r.inset(dx, dy);
47 if (r.isEmpty()) {
48 dst->setEmpty();
49 return;
50 }
51
52 SkVector radii[4];
53 for (int i = 0; i < 4; ++i) {
54 radii[i] = src.radii((SkRRect::Corner)i);
55 }
56 dst->setRectRadii(r, radii);
57}
58
59static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
60 SkRect r = src.rect();
61
62 r.inset(dx, dy);
63 if (r.isEmpty()) {
64 dst->setEmpty();
65 return;
66 }
67
68 SkVector radii[4];
69 for (int i = 0; i < 4; ++i) {
70 radii[i] = src.radii((SkRRect::Corner)i);
71 }
72 for (int i = 0; i < 4; ++i) {
73 if (radii[i].fX) {
74 radii[i].fX -= dx;
75 }
76 if (radii[i].fY) {
77 radii[i].fY -= dy;
78 }
79 }
80 dst->setRectRadii(r, radii);
81}
82
83static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) {
84 return newSize * radius / oldSize;
85}
86
87static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
88 SkRect r = src.rect();
89
90 r.inset(dx, dy);
91 if (r.isEmpty()) {
92 dst->setEmpty();
93 return;
94 }
95
96 SkVector radii[4];
97 for (int i = 0; i < 4; ++i) {
98 radii[i] = src.radii((SkRRect::Corner)i);
99 }
100 for (int i = 0; i < 4; ++i) {
101 radii[i].fX = prop(radii[i].fX, r.width(), src.rect().width());
102 radii[i].fY = prop(radii[i].fY, r.height(), src.rect().height());
103 }
104 dst->setRectRadii(r, radii);
105}
106
107static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
109 paint.setAntiAlias(true);
111
112 if (rrect.isRect()) {
113 paint.setColor(SK_ColorRED);
114 } else if (rrect.isOval()) {
115 paint.setColor(ToolUtils::color_to_565(0xFF008800));
116 } else if (rrect.isSimple()) {
117 paint.setColor(SK_ColorBLUE);
118 } else {
119 paint.setColor(SK_ColorBLACK);
120 }
121 canvas->drawRRect(rrect, paint);
122}
123
124static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) {
125 SkRRect rr;
126 for (SkScalar d = -30; d <= 30; d += 5) {
127 proc(rrect, d, d, &rr);
128 draw_rrect_color(canvas, rr);
129 }
130}
131
132class RRectGM : public skiagm::GM {
133public:
135
136protected:
137 SkString getName() const override { return SkString("rrect"); }
138
139 SkISize getISize() override { return SkISize::Make(820, 710); }
140
141 void onDraw(SkCanvas* canvas) override {
142 constexpr InsetProc insetProcs[] = {
144 };
145
146 SkRRect rrect[4];
147 SkRect r = { 0, 0, 120, 100 };
148 SkVector radii[4] = {
149 { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 }
150 };
151
152 rrect[0].setRect(r);
153 rrect[1].setOval(r);
154 rrect[2].setRectXY(r, 20, 20);
155 rrect[3].setRectRadii(r, radii);
156
157 canvas->translate(50.5f, 50.5f);
158 for (size_t j = 0; j < std::size(insetProcs); ++j) {
159 canvas->save();
160 for (size_t i = 0; i < std::size(rrect); ++i) {
161 drawrr(canvas, rrect[i], insetProcs[j]);
162 canvas->translate(200, 0);
163 }
164 canvas->restore();
165 canvas->translate(0, 170);
166 }
167 }
168
169private:
170 using INHERITED = GM;
171};
172
173DEF_GM( return new RRectGM; )
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
RRectGM()
Definition: rrect.cpp:134
void onDraw(SkCanvas *canvas) override
Definition: rrect.cpp:141
SkString getName() const override
Definition: rrect.cpp:137
SkISize getISize() override
Definition: rrect.cpp:139
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
Definition: SkCanvas.cpp:1705
int save()
Definition: SkCanvas.cpp:447
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
bool isOval() const
Definition: SkRRect.h:85
void setOval(const SkRect &oval)
Definition: SkRRect.cpp:30
bool isRect() const
Definition: SkRRect.h:84
void setRectRadii(const SkRect &rect, const SkVector radii[4])
Definition: SkRRect.cpp:189
void setRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition: SkRRect.cpp:52
bool isSimple() const
Definition: SkRRect.h:86
void setRect(const SkRect &rect)
Definition: SkRRect.h:126
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
SkRRect rrect
Definition: SkRecords.h:232
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
dst
Definition: cp.py:12
static void inset1(const SkRRect &src, SkScalar dx, SkScalar dy, SkRRect *dst)
Definition: rrect.cpp:43
static void inset0(const SkRRect &src, SkScalar dx, SkScalar dy, SkRRect *dst)
Definition: rrect.cpp:23
static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize)
Definition: rrect.cpp:83
static void inset2(const SkRRect &src, SkScalar dx, SkScalar dy, SkRRect *dst)
Definition: rrect.cpp:59
static void drawrr(SkCanvas *canvas, const SkRRect &rrect, InsetProc proc)
Definition: rrect.cpp:124
void(* InsetProc)(const SkRRect &, SkScalar dx, SkScalar dy, SkRRect *)
Definition: rrect.cpp:21
static void draw_rrect_color(SkCanvas *canvas, const SkRRect &rrect)
Definition: rrect.cpp:107
static void inset3(const SkRRect &src, SkScalar dx, SkScalar dy, SkRRect *dst)
Definition: rrect.cpp:87
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
void inset(float dx, float dy)
Definition: SkRect.h:1060
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
bool isEmpty() const
Definition: SkRect.h:693