Flutter Engine
The Flutter Engine
XferSlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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 "include/core/SkFont.h"
11#include "include/core/SkPath.h"
18#include "src/base/SkRandom.h"
19#include "tools/DecodeUtils.h"
22
33};
35
37
38struct ModeButton {
42
43public:
44 void init(const char label[], const SkRect& rect) {
45 fLabel = label;
46 fRect = rect;
47 fColor = (gRand.nextU() & 0x7F7F7F7F) | SkColorSetARGB(0xFF, 0, 0, 0x80);
48 }
49
50 void draw(SkCanvas* canvas) {
52 paint.setAntiAlias(true);
53 paint.setColor(fColor);
54 canvas->drawRoundRect(fRect, 8, 8, paint);
55
56 paint.setColor(0xFFFFFFFF);
58 font.setSize(16);
62 }
63
65 return fRect.intersects({x - 1, y - 1, x + 1, y + 1});
66 }
67};
68
69class ModeDrawable : public SkDrawable {
70public:
72
75
77 SkRect target = SkRect::MakeXYWH(x - fLoc.x() - 1, y - fLoc.y() - 1, 3, 3);
78 return this->getBounds().intersects(target);
79 }
80};
81
82class CircDrawable : public ModeDrawable {
83 SkPaint fPaint;
84 SkRect fBounds;
85
86public:
88 const SkColor colors[] = { 0, c };
90 colors, nullptr, 2,
93 }
94
95protected:
96 SkRect onGetBounds() override {
97 return fBounds;
98 }
99
100 void onDraw(SkCanvas* canvas) override {
101 fPaint.setBlendMode(fMode);
102 canvas->save();
103 canvas->translate(fLoc.x(), fLoc.y());
104 canvas->drawOval(fBounds, fPaint);
105 canvas->restore();
106 }
107};
108
110public:
113 for (int i = 0; i < N; ++i) {
114 fDrs[i].reset(new CircDrawable(200, colors[i]));
115 fDrs[i]->fLoc.set(100.f + i * 100, 100.f + i * 100);
117 }
118 fSelected = nullptr;
119
120 this->addButtons();
121 fName = "XferDemo";
122 }
123
124 void draw(SkCanvas* canvas) override {
125 for (int i = 0; i < N_Modes; ++i) {
126 fModeButtons[i].draw(canvas);
127 }
128
130 if (fSelected) {
131 for (int i = 0; i < N_Modes; ++i) {
132 if (fSelected->fMode == gModes[i]) {
133 canvas->drawRect(fModeRect[i], paint);
134 break;
135 }
136 }
137 }
138
139 canvas->saveLayer(nullptr, nullptr);
140 for (int i = 0; i < N; ++i) {
141 fDrs[i]->draw(canvas);
142 }
143 canvas->restore();
144 }
145
146protected:
148 // Check mode buttons first
149 for (int i = 0; i < N_Modes; ++i) {
150 if (fModeButtons[i].hitTest(x, y)) {
151 Click* click = new Click();
152 click->fMeta.setS32("mode", i);
153 return click;
154 }
155 }
156 fSelected = nullptr;
157 for (int i = N - 1; i >= 0; --i) {
158 if (fDrs[i]->hitTest(x, y)) {
159 fSelected = fDrs[i].get();
160 break;
161 }
162 }
163 return fSelected ? new Click() : nullptr;
164 }
165
166 bool onClick(Click* click) override {
167 int32_t mode;
168 if (click->fMeta.findS32("mode", &mode)) {
169 if (fSelected && skui::InputState::kUp == click->fState) {
170 fSelected->fMode = gModes[mode];
171 }
172 } else {
173 fSelected->fLoc.fX += click->fCurr.fX - click->fPrev.fX;
174 fSelected->fLoc.fY += click->fCurr.fY - click->fPrev.fY;
175 }
176 return true;
177 }
178
179private:
180 enum {
181 N = 4
182 };
183
184 SkRect fModeRect[N_Modes];
185 ModeButton fModeButtons[N_Modes];
186 sk_sp<CircDrawable> fDrs[N];
187 CircDrawable* fSelected;
188
189 void addButtons() {
190 SkScalar x = 10;
191 SkScalar y = 10;
192 for (int i = 0; i < N_Modes; ++i) {
193 fModeButtons[i].init(SkBlendMode_Name(gModes[i]), SkRect::MakeXYWH(x, y, 70, 25));
194 fModeRect[i] = SkRect::MakeXYWH(x, y + 28, 70, 2);
195 x += 80;
196 }
197 }
198};
199
200DEF_SLIDE( return new XferSlide; )
201
202//////////////////////////////////////////////////////////////////////////////
203
204#include "tools/Resources.h"
205
207public:
209 fName = "CubicResampler";
210 }
211
212protected:
213 void load(SkScalar, SkScalar) override {
214 SkRect r = {10, 10, 200, 200};
215 for (const char* name : {"images/mandrill_128.png",
216 "images/rle.bmp",
217 "images/example_4.png"}) {
218 fRecs.push_back({ToolUtils::GetResourceAsImage(name), r});
219 r.offset(0, r.height() + 10);
220 }
221 fDomain.setXYWH(r.fLeft + 3 * r.width() + 40, 50, 200, 200);
222 fCubic = {.3f, .5f};
223 }
224
225 void draw(SkCanvas* canvas) override {
226 for (const auto& rec : fRecs) {
227 rec.draw(canvas, fCubic);
228 }
229
231 paint.setAntiAlias(true);
232 paint.setStroke(true);
233 canvas->drawRect(fDomain, paint);
234
235 paint.setColor(SK_ColorRED);
236 paint.setStroke(false);
237 SkPoint loc = SkMatrix::RectToRect({0,0,1,1}, fDomain).mapXY(fCubic.B, fCubic.C);
238 canvas->drawCircle(loc.fX, loc.fY, 8, paint);
239
240 SkString str;
241 str.printf("B=%4.2f C=%4.2f", fCubic.B, fCubic.C);
243 font.setSize(25);
245 paint.setColor(SK_ColorBLACK);
246 canvas->drawSimpleText(str.c_str(), str.size(), SkTextEncoding::kUTF8,
247 fDomain.fLeft + 10, fDomain.fBottom + 40, font, paint);
248 }
249
250 static float pin_unitize(float min, float max, float value) {
251 return (std::min(std::max(value, min), max) - min) / (max - min);
252 }
253 static SkPoint pin_unitize(const SkRect& r, SkPoint p) {
254 return {
255 pin_unitize(r.fLeft, r.fRight, p.fX),
256 pin_unitize(r.fTop, r.fBottom, p.fY),
257 };
258 }
259
260protected:
262 if (fDomain.contains(x, y)) {
263 return new Click([this](Click* click) {
264 auto [B, C] = pin_unitize(fDomain, click->fCurr);
265 fCubic = {B, C};
266 return true;
267 });
268 }
269 return nullptr;
270 }
271
272 bool onClick(ClickHandlerSlide::Click *) override { return false; }
273
274private:
275 struct Rec {
276 sk_sp<SkImage> fImage;
278
279 void draw(SkCanvas* canvas, SkCubicResampler cubic) const {
280 SkRect r = fBounds;
282
283 SkMatrix lm = SkMatrix::Translate(r.x(), r.y())
284 * SkMatrix::Scale(10, 10);
285 paint.setShader(fImage->makeShader(SkSamplingOptions(), lm));
286 canvas->drawRect(r, paint);
287
288 r.offset(r.width() + 10, 0);
289 lm.postTranslate(r.width() + 10, 0);
290
292 canvas->drawRect(r, paint);
293
294 r.offset(r.width() + 10, 0);
295 lm.postTranslate(r.width() + 10, 0);
296
298 SkSamplingOptions(cubic), &lm));
299 canvas->drawRect(r, paint);
300 }
301 };
302
303 std::vector<Rec> fRecs;
304 SkRect fDomain;
305 SkCubicResampler fCubic;
306};
307
308DEF_SLIDE( return new CubicResamplerSlide; )
const char * fName
const SkRect fBounds
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition: SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ kDstIn
r = d * sa
@ kSrcOver
r = s + (1-sa)*d
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kDstOut
r = d * (1-sa)
@ kSrcIn
r = s * da
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define DEF_SLIDE(code)
Definition: Slide.h:25
static SkRandom gRand
Definition: XferSlide.cpp:36
const SkBlendMode gModes[]
Definition: XferSlide.cpp:23
const int N_Modes
Definition: XferSlide.cpp:34
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void onDraw(SkCanvas *canvas) override
Definition: XferSlide.cpp:100
CircDrawable(SkScalar size, SkColor c)
Definition: XferSlide.cpp:87
SkRect onGetBounds() override
Definition: XferSlide.cpp:96
bool onClick(ClickHandlerSlide::Click *) override
Definition: XferSlide.cpp:272
static SkPoint pin_unitize(const SkRect &r, SkPoint p)
Definition: XferSlide.cpp:253
Click * onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override
Definition: XferSlide.cpp:261
void draw(SkCanvas *canvas) override
Definition: XferSlide.cpp:225
static float pin_unitize(float min, float max, float value)
Definition: XferSlide.cpp:250
void load(SkScalar, SkScalar) override
Definition: XferSlide.cpp:213
SkBlendMode fMode
Definition: XferSlide.cpp:73
SkPoint fLoc
Definition: XferSlide.cpp:74
bool hitTest(SkScalar x, SkScalar y)
Definition: XferSlide.cpp:76
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
void restore()
Definition: SkCanvas.cpp:461
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, const SkPaint &paint)
Definition: SkCanvas.cpp:2717
int save()
Definition: SkCanvas.cpp:447
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
SkRect getBounds()
Definition: SkDrawable.cpp:71
void draw(SkCanvas *, const SkMatrix *=nullptr)
Definition: SkDrawable.cpp:43
Definition: SkFont.h:35
@ kAntiAlias
may have transparent pixels on glyph edges
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
SkMatrix & postTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:281
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition: SkMatrix.h:157
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
void setS32(const char name[], int32_t value)
Definition: SkMetaData.cpp:24
bool findS32(const char name[], int32_t *value=nullptr) const
Definition: SkMetaData.cpp:96
void setShader(sk_sp< SkShader > shader)
void setBlendMode(SkBlendMode mode)
Definition: SkPaint.cpp:151
uint32_t nextU()
Definition: SkRandom.h:42
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
size_t size() const
Definition: SkString.h:131
const char * c_str() const
Definition: SkString.h:133
static void DrawString(SkCanvas *canvas, const char text[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint, Align align=kLeft_Align)
Definition: SkTextUtils.h:34
SkString fName
Definition: Slide.h:54
bool onClick(Click *click) override
Definition: XferSlide.cpp:166
void draw(SkCanvas *canvas) override
Definition: XferSlide.cpp:124
Click * onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override
Definition: XferSlide.cpp:147
T * get() const
Definition: SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
const Paint & paint
Definition: color_source.cc:38
#define C(TEST_CATEGORY)
Definition: colrv1.cpp:248
float SkScalar
Definition: extension.cpp:12
uint8_t value
uint32_t * target
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
#define B
double y
double x
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
SkFont DefaultFont()
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
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 mode
Definition: switches.h:228
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
font
Font Metadata and Metrics.
AI float cubic(float precision, const SkPoint pts[], const VectorXform &vectorXform=VectorXform())
Definition: WangsFormula.h:195
ModifierKey
Definition: ModifierKey.h:9
void init(const char label[], const SkRect &rect)
Definition: XferSlide.cpp:44
SkColor fColor
Definition: XferSlide.cpp:40
SkRect fRect
Definition: XferSlide.cpp:41
bool hitTest(SkScalar x, SkScalar y)
Definition: XferSlide.cpp:64
SkString fLabel
Definition: XferSlide.cpp:39
void draw(SkCanvas *canvas)
Definition: XferSlide.cpp:50
float fX
x-axis value
Definition: SkPoint_impl.h:164
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
void set(float x, float y)
Definition: SkPoint_impl.h:200
float fY
y-axis value
Definition: SkPoint_impl.h:165
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
constexpr float x() const
Definition: SkRect.h:720
constexpr float y() const
Definition: SkRect.h:727
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
bool intersects(const SkRect &r) const
Definition: SkRect.h:1121
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
constexpr float centerX() const
Definition: SkRect.h:776
void offset(float dx, float dy)
Definition: SkRect.h:1016
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15