Debian Codegraph @

tools/viewer/ClockSlide.cpp

raw
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 */
7 #include "include/core/SkCanvas.h"
8 #include "include/core/SkPath.h"
9 #include "include/core/SkPathBuilder.h"
10 #include "include/core/SkRRect.h"
11 #include "include/private/base/SkAssert.h"
12 #include "src/base/SkRandom.h"
13 #include "tools/viewer/Slide.h"
14
15 #include <chrono>
16 #include <ctime>
17
18 // Implementation in C++ of Mozilla Canvas2D benchmark Canvas Clock Test
19 // See https://code.google.com/p/skia/issues/detail?id=1626
20
21 #define USE_PATH 1
22
23 class ClockSlide : public Slide {
24 public:
25 ClockSlide() { fName = "Clock"; }
26
27 void draw(SkCanvas* canvas) override {
28 SkPaint paintFill;
29 SkPaint paintStroke;
30 SkPath path;
31
32 canvas->save();
33 canvas->translate(150, 150);
34 canvas->scale(0.4f, 0.4f);
35 canvas->rotate(-180.f/2.f);
36
37 paintFill.setAntiAlias(true);
38 paintFill.setColor(SK_ColorBLACK);
39 paintStroke.setAntiAlias(true);
40 paintStroke.setStyle(SkPaint::kStroke_Style);
41 paintStroke.setColor(SK_ColorBLACK);
42 paintStroke.setStrokeWidth(8);
43 paintStroke.setStrokeCap(SkPaint::kRound_Cap);
44
45 // Hour marks
46 SkRect rect;
47 #ifndef USE_PATH
48 rect = SkRect::MakeLTRB(200-4, -4, 240+4, 4);
49 SkRRect rrect;
50 SkVector radii[4] = {{4,4}, {4,4}, {4,4}, {4,4}};
51 rrect.setRectRadii(rect, radii);
52 #endif
53 canvas->save();
54 for (int i=0;i<12;i++){
55 canvas->rotate(180.f/6.f);
56 #ifdef USE_PATH
57 path = SkPath::Line({200,0}, {240,0});
58 canvas->drawPath(path, paintStroke);
59 #else
60 canvas->drawRRect(rrect, paintFill);
61 #endif
62 }
63 canvas->restore();
64
65 // Minute marks
66 canvas->save();
67 #ifdef USE_PATH
68 paintStroke.setStrokeWidth(5);
69 #else
70 rect = SkRect::MakeLTRB(231.5f, -2.5f, 242.5, 2.5f);
71 radii[0] = SkPoint::Make(2.5f,2.5f);
72 radii[1] = SkPoint::Make(2.5f,2.5f);
73 radii[2] = SkPoint::Make(2.5f,2.5f);
74 radii[3] = SkPoint::Make(2.5f,2.5f);
75 rrect.setRectRadii(rect, radii);
76 #endif
77 for (int i=0;i<60;i++){
78 if (i%5 == 0) {
79 canvas->rotate(180.f/30.f);
80 continue;
81 }
82 #ifdef USE_PATH
83 path = SkPath::Line({234,0}, {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 const auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
93 const auto* ltime = std::localtime(&time); // not thread safe, but should be ok for slides
94
95 paintFill.setColor(SK_ColorBLACK);
96
97 // Write hours
98 canvas->save();
99 canvas->rotate(ltime->tm_hour * (180.f / 6) +
100 ltime->tm_min * (180.f / 360) +
101 ltime->tm_sec * (180.f / 21600));
102 #ifdef USE_PATH
103 paintStroke.setStrokeWidth(14);
104 path = SkPath::Line({-20,0}, {80,0});
105 canvas->drawPath(path, paintStroke);
106 #else
107 rect = SkRect::MakeLTRB(-20-7, -7, 80+7, 7);
108 radii[0] = SkPoint::Make(7,7);
109 radii[1] = SkPoint::Make(7,7);
110 radii[2] = SkPoint::Make(7,7);
111 radii[3] = SkPoint::Make(7,7);
112 rrect.setRectRadii(rect, radii);
113 canvas->drawRRect(rrect, paintFill);
114 #endif
115 canvas->restore();
116
117 // Write minutes
118 canvas->save();
119 canvas->rotate(ltime->tm_min * (180.f / 30) +
120 ltime->tm_sec * (180.f / 1800));
121 #ifdef USE_PATH
122 paintStroke.setStrokeWidth(10);
123 path = SkPath::Line({-56,0}, {224,0});
124 canvas->drawPath(path, paintStroke);
125 #else
126 rect = SkRect::MakeLTRB(-56-5, -5, 224+5, 5);
127 radii[0] = SkPoint::Make(5,5);
128 radii[1] = SkPoint::Make(5,5);
129 radii[2] = SkPoint::Make(5,5);
130 radii[3] = SkPoint::Make(5,5);
131 rrect.setRectRadii(rect, radii);
132 canvas->drawRRect(rrect, paintFill);
133 #endif
134 canvas->restore();
135
136 // Write seconds
137 canvas->save();
138 canvas->rotate(ltime->tm_sec * (180.f / 30));
139 paintFill.setColor(0xffd40000);
140 paintStroke.setColor(0xffd40000);
141 paintStroke.setStrokeWidth(6);
142 #ifdef USE_PATH
143 path = SkPath::Line({-60,0}, {166,0});
144 canvas->drawPath(path, paintStroke);
145 #else
146 rect = SkRect::MakeLTRB(-60-3, -3, 166+3, 3);
147 radii[0] = SkPoint::Make(3,3);
148 radii[1] = SkPoint::Make(3,3);
149 radii[2] = SkPoint::Make(3,3);
150 radii[3] = SkPoint::Make(3,3);
151 rrect.setRectRadii(rect, radii);
152 canvas->drawRRect(rrect, paintFill);
153 #endif
154 rect = SkRect::MakeLTRB(-20, -20, 20, 20);
155 #ifdef USE_PATH
156 path = SkPathBuilder()
157 .arcTo(rect, 0, 0, false)
158 .addOval(rect, SkPathDirection::kCCW)
159 .arcTo(rect, 360, 0, true)
160 .detach();
161 canvas->drawPath(path, paintFill);
162 #else
163 canvas->drawOval(rect, paintFill);
164 #endif
165 rect = SkRect::MakeLTRB(-20+190, -20, 20+190, 20);
166 #ifdef USE_PATH
167 path = SkPathBuilder()
168 .arcTo(rect, 0, 0, false)
169 .addOval(rect, SkPathDirection::kCCW)
170 .arcTo(rect, 360, 0, true)
171 .detach();
172 canvas->drawPath(path, paintStroke);
173 #else
174 canvas->drawOval(rect, paintStroke);
175 #endif
176 paintFill.setColor(0xff505050);
177 #ifdef USE_PATH
178 rect = SkRect::MakeLTRB(-6, -6, 6, 6);
179 path = SkPathBuilder(path)
180 .arcTo(rect, 0, 0, false)
181 .addOval(rect, SkPathDirection::kCCW)
182 .arcTo(rect, 360, 0, true)
183 .detach();
184 canvas->drawPath(path, paintFill);
185 #else
186 canvas->drawOval(rect, paintFill);
187 rect = SkRect::MakeLTRB(-6, -6, 6, 6);
188 canvas->drawOval(rect, paintFill);
189 #endif
190 canvas->restore();
191
192 paintStroke.setStrokeWidth(18);
193 paintStroke.setColor(0xff325FA2);
194 rect = SkRect::MakeLTRB(-284, -284, 284, 284);
195 #ifdef USE_PATH
196 path = SkPathBuilder()
197 .arcTo(rect, 0, 0, false)
198 .addOval(rect, SkPathDirection::kCCW)
199 .arcTo(rect, 360, 0, true)
200 .detach();
201 canvas->drawPath(path, paintStroke);
202 #else
203 canvas->drawOval(rect, paintStroke);
204 #endif
205
206 canvas->restore();
207 }
208
209 bool animate(double /*nanos*/) override { return true; }
210 };
211
212 DEF_SLIDE( return new ClockSlide(); )