Flutter Engine
The Flutter Engine
ArcSlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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
12#include "include/core/SkFont.h"
13#include "include/core/SkPath.h"
24#include "src/base/SkRandom.h"
25#include "src/base/SkUTF.h"
27#include "tools/viewer/Slide.h"
28
29class ArcsSlide : public Slide {
30 class MyDrawable : public SkDrawable {
31 SkRect fR;
32 SkScalar fSweep;
33 public:
34 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
35
36 void setSweep(SkScalar sweep) {
37 if (fSweep != sweep) {
38 fSweep = sweep;
39 this->notifyDrawingChanged();
40 }
41 }
42
43 void onDraw(SkCanvas* canvas) override {
45 paint.setAntiAlias(true);
46 paint.setStrokeWidth(SkIntToScalar(2));
47
49 paint.setColor(0x800000FF);
50 canvas->drawArc(fR, 0, fSweep, true, paint);
51
52 paint.setColor(0x800FF000);
53 canvas->drawArc(fR, 0, fSweep, false, paint);
54
56 paint.setColor(SK_ColorRED);
57 canvas->drawArc(fR, 0, fSweep, true, paint);
58
59 paint.setStrokeWidth(0);
60 paint.setColor(SK_ColorBLUE);
61 canvas->drawArc(fR, 0, fSweep, false, paint);
62 }
63
64 SkRect onGetBounds() override {
65 SkRect r(fR);
66 r.outset(2, 2);
67 return r;
68 }
69 };
70
71 SkRect fRect = {20, 20, 220, 220};
72 sk_sp<MyDrawable> fAnimatingDrawable;
73 sk_sp<SkDrawable> fRootDrawable;
74
75 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
76 canvas->drawRect(r, p);
77 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
78 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
79 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
80 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
81 }
82
83 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
85 SkString str;
87 str.append(", ");
88 str.appendScalar(sweep);
89 SkTextUtils::DrawString(canvas, str.c_str(), rect.centerX(),
90 rect.fBottom + font.getSize() * 5/4, font, SkPaint(),
92 }
93
94 static void DrawArcs(SkCanvas* canvas) {
96 SkRect r;
97 SkScalar w = 75;
98 SkScalar h = 50;
99
100 r.setWH(w, h);
101 paint.setAntiAlias(true);
103
104 canvas->save();
105 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
106
107 paint.setStrokeWidth(SkIntToScalar(1));
108
109 static const SkScalar gAngles[] = {
110 0, 360,
111 0, 45,
112 0, -45,
113 720, 135,
114 -90, 269,
115 -90, 270,
116 -90, 271,
117 -180, -270,
118 225, 90
119 };
120
121 for (size_t i = 0; i < std::size(gAngles); i += 2) {
122 paint.setColor(SK_ColorBLACK);
123 DrawRectWithLines(canvas, r, paint);
124
125 paint.setColor(SK_ColorRED);
126 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
127
128 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
129
130 canvas->translate(w * 8 / 7, 0);
131 }
132
133 canvas->restore();
134 }
135
136 void drawRoot(SkCanvas* canvas) {
138 paint.setAntiAlias(true);
139 paint.setStrokeWidth(SkIntToScalar(2));
141
142 DrawRectWithLines(canvas, fRect, paint);
143
144 canvas->drawDrawable(fAnimatingDrawable.get());
145
146 DrawArcs(canvas);
147 }
148
149public:
150 ArcsSlide() { fName = "Arcs"; }
151
152 void load(SkScalar w, SkScalar h) override {
153 fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
154
155 SkPictureRecorder recorder;
156 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
157 fRootDrawable = recorder.finishRecordingAsDrawable();
158 }
159
160 void draw(SkCanvas* canvas) override {
161 canvas->clear(0xFFDDDDDD);
162 canvas->drawDrawable(fRootDrawable.get());
163 }
164
165 bool animate(double nanos) override {
166 SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360));
167 if (fAnimatingDrawable) {
168 fAnimatingDrawable->setSweep(angle);
169 }
170 return true;
171 }
172};
173
174DEF_SLIDE( return new ArcsSlide(); )
SkRect fRect
Definition: FillRRectOp.cpp:73
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define SkDoubleToScalar(x)
Definition: SkScalar.h:64
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define DEF_SLIDE(code)
Definition: Slide.h:25
bool animate(double nanos) override
Definition: ArcSlide.cpp:165
void draw(SkCanvas *canvas) override
Definition: ArcSlide.cpp:160
void load(SkScalar w, SkScalar h) override
Definition: ArcSlide.cpp:152
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
void drawDrawable(SkDrawable *drawable, const SkMatrix *matrix=nullptr)
Definition: SkCanvas.cpp:2574
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawArc(const SkRect &oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, const SkPaint &paint)
Definition: SkCanvas.cpp:2728
int save()
Definition: SkCanvas.cpp:447
Definition: SkFont.h:35
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkDrawable > finishRecordingAsDrawable()
void append(const char text[])
Definition: SkString.h:203
void appendScalar(SkScalar value)
Definition: SkString.h:213
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
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
T * get() const
Definition: SkRefCnt.h:303
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SkFont DefaultFont()
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.
SkScalar w
SkScalar h
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
void outset(float dx, float dy)
Definition: SkRect.h:1077
void setWH(float width, float height)
Definition: SkRect.h:944
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
constexpr float centerX() const
Definition: SkRect.h:776
constexpr float centerY() const
Definition: SkRect.h:785
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15