SampleClock.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "samplecode/Sample.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkPath.h"
  10. #include "include/core/SkRRect.h"
  11. #include "include/core/SkTime.h"
  12. #include "include/utils/SkRandom.h"
  13. // Implementation in C++ of Mozilla Canvas2D benchmark Canvas Clock Test
  14. // See https://code.google.com/p/skia/issues/detail?id=1626
  15. #define USE_PATH 1
  16. class ClockView : public Sample {
  17. SkString name() override { return SkString("Clock"); }
  18. void onDrawContent(SkCanvas* canvas) override {
  19. SkPaint paintFill;
  20. SkPaint paintStroke;
  21. SkPath path;
  22. canvas->save();
  23. canvas->translate(150, 150);
  24. canvas->scale(0.4f, 0.4f);
  25. canvas->rotate(-180.f/2.f);
  26. paintFill.setAntiAlias(true);
  27. paintFill.setColor(SK_ColorBLACK);
  28. paintStroke.setAntiAlias(true);
  29. paintStroke.setStyle(SkPaint::kStroke_Style);
  30. paintStroke.setColor(SK_ColorBLACK);
  31. paintStroke.setStrokeWidth(8);
  32. paintStroke.setStrokeCap(SkPaint::kRound_Cap);
  33. // Hour marks
  34. SkRect rect;
  35. #ifndef USE_PATH
  36. rect = SkRect::MakeLTRB(200-4, -4, 240+4, 4);
  37. SkRRect rrect;
  38. SkVector radii[4] = {{4,4}, {4,4}, {4,4}, {4,4}};
  39. rrect.setRectRadii(rect, radii);
  40. #endif
  41. canvas->save();
  42. for (int i=0;i<12;i++){
  43. canvas->rotate(180.f/6.f);
  44. #ifdef USE_PATH
  45. path.reset();
  46. path.moveTo(200,0);
  47. path.lineTo(240,0);
  48. canvas->drawPath(path, paintStroke);
  49. #else
  50. canvas->drawRRect(rrect, paintFill);
  51. #endif
  52. }
  53. canvas->restore();
  54. // Minute marks
  55. canvas->save();
  56. #ifdef USE_PATH
  57. paintStroke.setStrokeWidth(5);
  58. #else
  59. rect = SkRect::MakeLTRB(231.5f, -2.5f, 242.5, 2.5f);
  60. radii[0] = SkPoint::Make(2.5f,2.5f);
  61. radii[1] = SkPoint::Make(2.5f,2.5f);
  62. radii[2] = SkPoint::Make(2.5f,2.5f);
  63. radii[3] = SkPoint::Make(2.5f,2.5f);
  64. rrect.setRectRadii(rect, radii);
  65. #endif
  66. for (int i=0;i<60;i++){
  67. if (i%5 == 0) {
  68. canvas->rotate(180.f/30.f);
  69. continue;
  70. }
  71. #ifdef USE_PATH
  72. path.reset();
  73. path.moveTo(234,0);
  74. path.lineTo(240,0);
  75. canvas->drawPath(path, paintStroke);
  76. #else
  77. canvas->drawRRect(rrect, paintFill);
  78. #endif
  79. canvas->rotate(180.f/30.f);
  80. }
  81. canvas->restore();
  82. SkTime::DateTime time;
  83. SkTime::GetDateTime(&time);
  84. time.fHour = time.fHour >= 12 ? time.fHour-12 : time.fHour;
  85. paintFill.setColor(SK_ColorBLACK);
  86. // Write hours
  87. canvas->save();
  88. canvas->rotate(time.fHour*(180.f/6.f) + time.fMinute*(180.f/360.f)
  89. + time.fSecond*(180.f/21600.f) );
  90. #ifdef USE_PATH
  91. paintStroke.setStrokeWidth(14);
  92. path.reset();
  93. path.moveTo(-20,0);
  94. path.lineTo(80,0);
  95. canvas->drawPath(path, paintStroke);
  96. #else
  97. rect = SkRect::MakeLTRB(-20-7, -7, 80+7, 7);
  98. radii[0] = SkPoint::Make(7,7);
  99. radii[1] = SkPoint::Make(7,7);
  100. radii[2] = SkPoint::Make(7,7);
  101. radii[3] = SkPoint::Make(7,7);
  102. rrect.setRectRadii(rect, radii);
  103. canvas->drawRRect(rrect, paintFill);
  104. #endif
  105. canvas->restore();
  106. // Write minutes
  107. canvas->save();
  108. canvas->rotate(time.fMinute*(180.f/30.f)
  109. + time.fSecond*(180.f/1800.f) );
  110. #ifdef USE_PATH
  111. paintStroke.setStrokeWidth(10);
  112. path.reset();
  113. path.moveTo(-56,0);
  114. path.lineTo(224,0);
  115. canvas->drawPath(path, paintStroke);
  116. #else
  117. rect = SkRect::MakeLTRB(-56-5, -5, 224+5, 5);
  118. radii[0] = SkPoint::Make(5,5);
  119. radii[1] = SkPoint::Make(5,5);
  120. radii[2] = SkPoint::Make(5,5);
  121. radii[3] = SkPoint::Make(5,5);
  122. rrect.setRectRadii(rect, radii);
  123. canvas->drawRRect(rrect, paintFill);
  124. #endif
  125. canvas->restore();
  126. // Write seconds
  127. canvas->save();
  128. canvas->rotate(time.fSecond*(180.f/30.f));
  129. paintFill.setColor(0xffd40000);
  130. paintStroke.setColor(0xffd40000);
  131. paintStroke.setStrokeWidth(6);
  132. #ifdef USE_PATH
  133. path.reset();
  134. path.moveTo(-60,0);
  135. path.lineTo(166,0);
  136. canvas->drawPath(path, paintStroke);
  137. #else
  138. rect = SkRect::MakeLTRB(-60-3, -3, 166+3, 3);
  139. radii[0] = SkPoint::Make(3,3);
  140. radii[1] = SkPoint::Make(3,3);
  141. radii[2] = SkPoint::Make(3,3);
  142. radii[3] = SkPoint::Make(3,3);
  143. rrect.setRectRadii(rect, radii);
  144. canvas->drawRRect(rrect, paintFill);
  145. #endif
  146. rect = SkRect::MakeLTRB(-20, -20, 20, 20);
  147. #ifdef USE_PATH
  148. path.reset();
  149. path.arcTo(rect, 0, 0, false);
  150. path.addOval(rect, SkPath::kCCW_Direction);
  151. path.arcTo(rect, 360, 0, true);
  152. canvas->drawPath(path, paintFill);
  153. #else
  154. canvas->drawOval(rect, paintFill);
  155. #endif
  156. rect = SkRect::MakeLTRB(-20+190, -20, 20+190, 20);
  157. #ifdef USE_PATH
  158. path.reset();
  159. path.arcTo(rect, 0, 0, false);
  160. path.addOval(rect, SkPath::kCCW_Direction);
  161. path.arcTo(rect, 360, 0, true);
  162. canvas->drawPath(path, paintStroke);
  163. #else
  164. canvas->drawOval(rect, paintStroke);
  165. #endif
  166. paintFill.setColor(0xff505050);
  167. #ifdef USE_PATH
  168. rect = SkRect::MakeLTRB(-6, -6, 6, 6);
  169. path.arcTo(rect, 0, 0, false);
  170. path.addOval(rect, SkPath::kCCW_Direction);
  171. path.arcTo(rect, 360, 0, true);
  172. canvas->drawPath(path, paintFill);
  173. #else
  174. canvas->drawOval(rect, paintFill);
  175. rect = SkRect::MakeLTRB(-6, -6, 6, 6);
  176. canvas->drawOval(rect, paintFill);
  177. #endif
  178. canvas->restore();
  179. paintStroke.setStrokeWidth(18);
  180. paintStroke.setColor(0xff325FA2);
  181. rect = SkRect::MakeLTRB(-284, -284, 284, 284);
  182. #ifdef USE_PATH
  183. path.reset();
  184. path.arcTo(rect, 0, 0, false);
  185. path.addOval(rect, SkPath::kCCW_Direction);
  186. path.arcTo(rect, 360, 0, true);
  187. canvas->drawPath(path, paintStroke);
  188. #else
  189. canvas->drawOval(rect, paintStroke);
  190. #endif
  191. canvas->restore();
  192. }
  193. bool onAnimate(double /*nanos*/) override { return true; }
  194. };
  195. DEF_SAMPLE( return new ClockView(); )