jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "samplecode/Sample.h" |
jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 8 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkPath.h" |
| 11 | #include "include/core/SkRRect.h" |
| 12 | #include "include/core/SkTime.h" |
| 13 | #include "include/utils/SkRandom.h" |
jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 14 | |
| 15 | // Implementation in C++ of Mozilla Canvas2D benchmark Canvas Clock Test |
| 16 | // See https://code.google.com/p/skia/issues/detail?id=1626 |
| 17 | |
| 18 | #define USE_PATH 1 |
| 19 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 20 | class ClockView : public Sample { |
jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 21 | public: |
| 22 | ClockView() {} |
| 23 | |
| 24 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 25 | SkString name() override { return SkString("Clock"); } |
jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 26 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | void onDrawContent(SkCanvas* canvas) override { |
jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 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.reset(); |
| 58 | path.moveTo(200,0); |
| 59 | path.lineTo(240,0); |
| 60 | canvas->drawPath(path, paintStroke); |
| 61 | #else |
| 62 | canvas->drawRRect(rrect, paintFill); |
| 63 | #endif |
| 64 | } |
| 65 | canvas->restore(); |
| 66 | |
| 67 | // Minute marks |
| 68 | canvas->save(); |
| 69 | #ifdef USE_PATH |
| 70 | paintStroke.setStrokeWidth(5); |
| 71 | #else |
| 72 | rect = SkRect::MakeLTRB(231.5f, -2.5f, 242.5, 2.5f); |
| 73 | radii[0] = SkPoint::Make(2.5f,2.5f); |
| 74 | radii[1] = SkPoint::Make(2.5f,2.5f); |
| 75 | radii[2] = SkPoint::Make(2.5f,2.5f); |
| 76 | radii[3] = SkPoint::Make(2.5f,2.5f); |
| 77 | rrect.setRectRadii(rect, radii); |
| 78 | #endif |
| 79 | for (int i=0;i<60;i++){ |
| 80 | if (i%5 == 0) { |
| 81 | canvas->rotate(180.f/30.f); |
| 82 | continue; |
| 83 | } |
| 84 | #ifdef USE_PATH |
| 85 | path.reset(); |
| 86 | path.moveTo(234,0); |
| 87 | path.lineTo(240,0); |
| 88 | canvas->drawPath(path, paintStroke); |
| 89 | #else |
| 90 | canvas->drawRRect(rrect, paintFill); |
| 91 | #endif |
| 92 | canvas->rotate(180.f/30.f); |
| 93 | } |
| 94 | canvas->restore(); |
| 95 | |
| 96 | SkTime::DateTime time; |
| 97 | SkTime::GetDateTime(&time); |
| 98 | time.fHour = time.fHour >= 12 ? time.fHour-12 : time.fHour; |
| 99 | paintFill.setColor(SK_ColorBLACK); |
| 100 | |
| 101 | // Write hours |
| 102 | canvas->save(); |
| 103 | canvas->rotate(time.fHour*(180.f/6.f) + time.fMinute*(180.f/360.f) |
| 104 | + time.fSecond*(180.f/21600.f) ); |
| 105 | #ifdef USE_PATH |
| 106 | paintStroke.setStrokeWidth(14); |
| 107 | path.reset(); |
| 108 | path.moveTo(-20,0); |
| 109 | path.lineTo(80,0); |
| 110 | canvas->drawPath(path, paintStroke); |
| 111 | #else |
| 112 | rect = SkRect::MakeLTRB(-20-7, -7, 80+7, 7); |
| 113 | radii[0] = SkPoint::Make(7,7); |
| 114 | radii[1] = SkPoint::Make(7,7); |
| 115 | radii[2] = SkPoint::Make(7,7); |
| 116 | radii[3] = SkPoint::Make(7,7); |
| 117 | rrect.setRectRadii(rect, radii); |
| 118 | canvas->drawRRect(rrect, paintFill); |
| 119 | #endif |
| 120 | canvas->restore(); |
| 121 | |
| 122 | // Write minutes |
| 123 | canvas->save(); |
| 124 | canvas->rotate(time.fMinute*(180.f/30.f) |
| 125 | + time.fSecond*(180.f/1800.f) ); |
| 126 | #ifdef USE_PATH |
| 127 | paintStroke.setStrokeWidth(10); |
| 128 | path.reset(); |
| 129 | path.moveTo(-56,0); |
| 130 | path.lineTo(224,0); |
| 131 | canvas->drawPath(path, paintStroke); |
| 132 | #else |
| 133 | rect = SkRect::MakeLTRB(-56-5, -5, 224+5, 5); |
| 134 | radii[0] = SkPoint::Make(5,5); |
| 135 | radii[1] = SkPoint::Make(5,5); |
| 136 | radii[2] = SkPoint::Make(5,5); |
| 137 | radii[3] = SkPoint::Make(5,5); |
| 138 | rrect.setRectRadii(rect, radii); |
| 139 | canvas->drawRRect(rrect, paintFill); |
| 140 | #endif |
| 141 | canvas->restore(); |
| 142 | |
| 143 | // Write seconds |
| 144 | canvas->save(); |
| 145 | canvas->rotate(time.fSecond*(180.f/30.f)); |
| 146 | paintFill.setColor(0xffd40000); |
| 147 | paintStroke.setColor(0xffd40000); |
| 148 | paintStroke.setStrokeWidth(6); |
| 149 | #ifdef USE_PATH |
| 150 | path.reset(); |
| 151 | path.moveTo(-60,0); |
| 152 | path.lineTo(166,0); |
| 153 | canvas->drawPath(path, paintStroke); |
| 154 | #else |
| 155 | rect = SkRect::MakeLTRB(-60-3, -3, 166+3, 3); |
| 156 | radii[0] = SkPoint::Make(3,3); |
| 157 | radii[1] = SkPoint::Make(3,3); |
| 158 | radii[2] = SkPoint::Make(3,3); |
| 159 | radii[3] = SkPoint::Make(3,3); |
| 160 | rrect.setRectRadii(rect, radii); |
| 161 | canvas->drawRRect(rrect, paintFill); |
| 162 | #endif |
| 163 | rect = SkRect::MakeLTRB(-20, -20, 20, 20); |
| 164 | #ifdef USE_PATH |
| 165 | path.reset(); |
| 166 | path.arcTo(rect, 0, 0, false); |
| 167 | path.addOval(rect, SkPath::kCCW_Direction); |
| 168 | path.arcTo(rect, 360, 0, true); |
| 169 | canvas->drawPath(path, paintFill); |
| 170 | #else |
| 171 | canvas->drawOval(rect, paintFill); |
| 172 | #endif |
| 173 | rect = SkRect::MakeLTRB(-20+190, -20, 20+190, 20); |
| 174 | #ifdef USE_PATH |
| 175 | path.reset(); |
| 176 | path.arcTo(rect, 0, 0, false); |
| 177 | path.addOval(rect, SkPath::kCCW_Direction); |
| 178 | path.arcTo(rect, 360, 0, true); |
| 179 | canvas->drawPath(path, paintStroke); |
| 180 | #else |
| 181 | canvas->drawOval(rect, paintStroke); |
| 182 | #endif |
| 183 | paintFill.setColor(0xff505050); |
| 184 | #ifdef USE_PATH |
| 185 | rect = SkRect::MakeLTRB(-6, -6, 6, 6); |
| 186 | path.arcTo(rect, 0, 0, false); |
| 187 | path.addOval(rect, SkPath::kCCW_Direction); |
| 188 | path.arcTo(rect, 360, 0, true); |
| 189 | canvas->drawPath(path, paintFill); |
| 190 | #else |
| 191 | canvas->drawOval(rect, paintFill); |
| 192 | rect = SkRect::MakeLTRB(-6, -6, 6, 6); |
| 193 | canvas->drawOval(rect, paintFill); |
| 194 | #endif |
| 195 | canvas->restore(); |
| 196 | |
| 197 | paintStroke.setStrokeWidth(18); |
| 198 | paintStroke.setColor(0xff325FA2); |
| 199 | rect = SkRect::MakeLTRB(-284, -284, 284, 284); |
| 200 | #ifdef USE_PATH |
| 201 | path.reset(); |
| 202 | path.arcTo(rect, 0, 0, false); |
| 203 | path.addOval(rect, SkPath::kCCW_Direction); |
| 204 | path.arcTo(rect, 360, 0, true); |
| 205 | canvas->drawPath(path, paintStroke); |
| 206 | #else |
| 207 | canvas->drawOval(rect, paintStroke); |
| 208 | #endif |
| 209 | |
| 210 | canvas->restore(); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 211 | } |
jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 212 | |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 213 | bool onAnimate(const AnimTimer&) override { return true; } |
jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 214 | |
| 215 | private: |
| 216 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 217 | typedef Sample INHERITED; |
jvanverth@google.com | dcd36f3 | 2013-09-17 19:52:05 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | ////////////////////////////////////////////////////////////////////////////// |
| 221 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 222 | DEF_SAMPLE( return new ClockView(); ) |