Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ClockSlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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 */
10#include "src/base/SkRandom.h"
11#include "src/pdf/SkPDFUtils.h"
12#include "tools/viewer/Slide.h"
13
14// Implementation in C++ of Mozilla Canvas2D benchmark Canvas Clock Test
15// See https://code.google.com/p/skia/issues/detail?id=1626
16
17#define USE_PATH 1
18
19class ClockSlide : public Slide {
20public:
21 ClockSlide() { fName = "Clock"; }
22
23 void draw(SkCanvas* canvas) override {
24 SkPaint paintFill;
25 SkPaint paintStroke;
26 SkPath path;
27
28 canvas->save();
29 canvas->translate(150, 150);
30 canvas->scale(0.4f, 0.4f);
31 canvas->rotate(-180.f/2.f);
32
33 paintFill.setAntiAlias(true);
34 paintFill.setColor(SK_ColorBLACK);
35 paintStroke.setAntiAlias(true);
37 paintStroke.setColor(SK_ColorBLACK);
38 paintStroke.setStrokeWidth(8);
40
41 // Hour marks
42 SkRect rect;
43#ifndef USE_PATH
44 rect = SkRect::MakeLTRB(200-4, -4, 240+4, 4);
45 SkRRect rrect;
46 SkVector radii[4] = {{4,4}, {4,4}, {4,4}, {4,4}};
47 rrect.setRectRadii(rect, radii);
48#endif
49 canvas->save();
50 for (int i=0;i<12;i++){
51 canvas->rotate(180.f/6.f);
52#ifdef USE_PATH
53 path.reset();
54 path.moveTo(200,0);
55 path.lineTo(240,0);
56 canvas->drawPath(path, paintStroke);
57#else
58 canvas->drawRRect(rrect, paintFill);
59#endif
60 }
61 canvas->restore();
62
63 // Minute marks
64 canvas->save();
65#ifdef USE_PATH
66 paintStroke.setStrokeWidth(5);
67#else
68 rect = SkRect::MakeLTRB(231.5f, -2.5f, 242.5, 2.5f);
69 radii[0] = SkPoint::Make(2.5f,2.5f);
70 radii[1] = SkPoint::Make(2.5f,2.5f);
71 radii[2] = SkPoint::Make(2.5f,2.5f);
72 radii[3] = SkPoint::Make(2.5f,2.5f);
73 rrect.setRectRadii(rect, radii);
74#endif
75 for (int i=0;i<60;i++){
76 if (i%5 == 0) {
77 canvas->rotate(180.f/30.f);
78 continue;
79 }
80#ifdef USE_PATH
81 path.reset();
82 path.moveTo(234,0);
83 path.lineTo(240,0);
84 canvas->drawPath(path, paintStroke);
85#else
86 canvas->drawRRect(rrect, paintFill);
87#endif
88 canvas->rotate(180.f/30.f);
89 }
90 canvas->restore();
91
92 // TODO(herb,kjlubick) Switch to std::chrono::system_clock
93 SkPDF::DateTime time;
95 time.fHour = time.fHour >= 12 ? time.fHour-12 : time.fHour;
96 paintFill.setColor(SK_ColorBLACK);
97
98 // Write hours
99 canvas->save();
100 canvas->rotate(time.fHour*(180.f/6.f) + time.fMinute*(180.f/360.f)
101 + time.fSecond*(180.f/21600.f) );
102#ifdef USE_PATH
103 paintStroke.setStrokeWidth(14);
104 path.reset();
105 path.moveTo(-20,0);
106 path.lineTo(80,0);
107 canvas->drawPath(path, paintStroke);
108#else
109 rect = SkRect::MakeLTRB(-20-7, -7, 80+7, 7);
110 radii[0] = SkPoint::Make(7,7);
111 radii[1] = SkPoint::Make(7,7);
112 radii[2] = SkPoint::Make(7,7);
113 radii[3] = SkPoint::Make(7,7);
114 rrect.setRectRadii(rect, radii);
115 canvas->drawRRect(rrect, paintFill);
116#endif
117 canvas->restore();
118
119 // Write minutes
120 canvas->save();
121 canvas->rotate(time.fMinute*(180.f/30.f)
122 + time.fSecond*(180.f/1800.f) );
123#ifdef USE_PATH
124 paintStroke.setStrokeWidth(10);
125 path.reset();
126 path.moveTo(-56,0);
127 path.lineTo(224,0);
128 canvas->drawPath(path, paintStroke);
129#else
130 rect = SkRect::MakeLTRB(-56-5, -5, 224+5, 5);
131 radii[0] = SkPoint::Make(5,5);
132 radii[1] = SkPoint::Make(5,5);
133 radii[2] = SkPoint::Make(5,5);
134 radii[3] = SkPoint::Make(5,5);
135 rrect.setRectRadii(rect, radii);
136 canvas->drawRRect(rrect, paintFill);
137#endif
138 canvas->restore();
139
140 // Write seconds
141 canvas->save();
142 canvas->rotate(time.fSecond*(180.f/30.f));
143 paintFill.setColor(0xffd40000);
144 paintStroke.setColor(0xffd40000);
145 paintStroke.setStrokeWidth(6);
146#ifdef USE_PATH
147 path.reset();
148 path.moveTo(-60,0);
149 path.lineTo(166,0);
150 canvas->drawPath(path, paintStroke);
151#else
152 rect = SkRect::MakeLTRB(-60-3, -3, 166+3, 3);
153 radii[0] = SkPoint::Make(3,3);
154 radii[1] = SkPoint::Make(3,3);
155 radii[2] = SkPoint::Make(3,3);
156 radii[3] = SkPoint::Make(3,3);
157 rrect.setRectRadii(rect, radii);
158 canvas->drawRRect(rrect, paintFill);
159#endif
160 rect = SkRect::MakeLTRB(-20, -20, 20, 20);
161#ifdef USE_PATH
162 path.reset();
163 path.arcTo(rect, 0, 0, false);
164 path.addOval(rect, SkPathDirection::kCCW);
165 path.arcTo(rect, 360, 0, true);
166 canvas->drawPath(path, paintFill);
167#else
168 canvas->drawOval(rect, paintFill);
169#endif
170 rect = SkRect::MakeLTRB(-20+190, -20, 20+190, 20);
171#ifdef USE_PATH
172 path.reset();
173 path.arcTo(rect, 0, 0, false);
174 path.addOval(rect, SkPathDirection::kCCW);
175 path.arcTo(rect, 360, 0, true);
176 canvas->drawPath(path, paintStroke);
177#else
178 canvas->drawOval(rect, paintStroke);
179#endif
180 paintFill.setColor(0xff505050);
181#ifdef USE_PATH
182 rect = SkRect::MakeLTRB(-6, -6, 6, 6);
183 path.arcTo(rect, 0, 0, false);
184 path.addOval(rect, SkPathDirection::kCCW);
185 path.arcTo(rect, 360, 0, true);
186 canvas->drawPath(path, paintFill);
187#else
188 canvas->drawOval(rect, paintFill);
189 rect = SkRect::MakeLTRB(-6, -6, 6, 6);
190 canvas->drawOval(rect, paintFill);
191#endif
192 canvas->restore();
193
194 paintStroke.setStrokeWidth(18);
195 paintStroke.setColor(0xff325FA2);
196 rect = SkRect::MakeLTRB(-284, -284, 284, 284);
197#ifdef USE_PATH
198 path.reset();
199 path.arcTo(rect, 0, 0, false);
200 path.addOval(rect, SkPathDirection::kCCW);
201 path.arcTo(rect, 360, 0, true);
202 canvas->drawPath(path, paintStroke);
203#else
204 canvas->drawOval(rect, paintStroke);
205#endif
206
207 canvas->restore();
208 }
209
210 bool animate(double /*nanos*/) override { return true; }
211};
212
213DEF_SLIDE( return new ClockSlide(); )
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
#define DEF_SLIDE(code)
Definition Slide.h:25
bool animate(double) override
void draw(SkCanvas *canvas) override
void drawOval(const SkRect &oval, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void rotate(SkScalar degrees)
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
int save()
Definition SkCanvas.cpp:451
void drawPath(const SkPath &path, const SkPaint &paint)
void scale(SkScalar sx, SkScalar sy)
@ kRound_Cap
adds circle
Definition SkPaint.h:335
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition SkPaint.h:170
void setStrokeCap(Cap cap)
Definition SkPaint.cpp:179
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
void setRectRadii(const SkRect &rect, const SkVector radii[4])
Definition SkRRect.cpp:189
Definition Slide.h:29
SkString fName
Definition Slide.h:54
void GetDateTime(SkPDF::DateTime *)
uint8_t fMinute
0..59
uint8_t fSecond
0..59
uint8_t fHour
0..23
static constexpr SkPoint Make(float x, float y)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646