ovals.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkMatrix.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkScalar.h"
  15. #include "include/core/SkShader.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTileMode.h"
  19. #include "include/core/SkTypes.h"
  20. #include "include/effects/SkGradientShader.h"
  21. #include "include/private/SkTArray.h"
  22. #include "include/utils/SkRandom.h"
  23. #include "tools/ToolUtils.h"
  24. namespace skiagm {
  25. class OvalGM : public GM {
  26. public:
  27. OvalGM() {
  28. this->setBGColor(0xFF000000);
  29. this->makePaints();
  30. this->makeMatrices();
  31. }
  32. protected:
  33. SkString onShortName() override {
  34. return SkString("ovals");
  35. }
  36. SkISize onISize() override {
  37. return SkISize::Make(1200, 900);
  38. }
  39. void makePaints() {
  40. {
  41. // no AA
  42. SkPaint p;
  43. fPaints.push_back(p);
  44. }
  45. {
  46. // AA
  47. SkPaint p;
  48. p.setAntiAlias(true);
  49. fPaints.push_back(p);
  50. }
  51. {
  52. // AA with stroke style
  53. SkPaint p;
  54. p.setAntiAlias(true);
  55. p.setStyle(SkPaint::kStroke_Style);
  56. p.setStrokeWidth(SkIntToScalar(5));
  57. fPaints.push_back(p);
  58. }
  59. {
  60. // AA with stroke style, width = 0
  61. SkPaint p;
  62. p.setAntiAlias(true);
  63. p.setStyle(SkPaint::kStroke_Style);
  64. fPaints.push_back(p);
  65. }
  66. {
  67. // AA with stroke and fill style
  68. SkPaint p;
  69. p.setAntiAlias(true);
  70. p.setStyle(SkPaint::kStrokeAndFill_Style);
  71. p.setStrokeWidth(SkIntToScalar(3));
  72. fPaints.push_back(p);
  73. }
  74. }
  75. void makeMatrices() {
  76. {
  77. SkMatrix m;
  78. m.setIdentity();
  79. fMatrices.push_back(m);
  80. }
  81. {
  82. SkMatrix m;
  83. m.setScale(SkIntToScalar(3), SkIntToScalar(2));
  84. fMatrices.push_back(m);
  85. }
  86. {
  87. SkMatrix m;
  88. m.setScale(SkIntToScalar(2), SkIntToScalar(2));
  89. fMatrices.push_back(m);
  90. }
  91. {
  92. SkMatrix m;
  93. m.setScale(SkIntToScalar(1), SkIntToScalar(2));
  94. fMatrices.push_back(m);
  95. }
  96. {
  97. SkMatrix m;
  98. m.setScale(SkIntToScalar(4), SkIntToScalar(1));
  99. fMatrices.push_back(m);
  100. }
  101. {
  102. SkMatrix m;
  103. m.setRotate(SkIntToScalar(90));
  104. fMatrices.push_back(m);
  105. }
  106. {
  107. SkMatrix m;
  108. m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
  109. fMatrices.push_back(m);
  110. }
  111. {
  112. SkMatrix m;
  113. m.setRotate(SkIntToScalar(60));
  114. fMatrices.push_back(m);
  115. }
  116. }
  117. SkColor genColor(SkRandom* rand) {
  118. SkScalar hsv[3];
  119. hsv[0] = rand->nextRangeF(0.0f, 360.0f);
  120. hsv[1] = rand->nextRangeF(0.75f, 1.0f);
  121. hsv[2] = rand->nextRangeF(0.75f, 1.0f);
  122. return ToolUtils::color_to_565(SkHSVToColor(hsv));
  123. }
  124. void onDraw(SkCanvas* canvas) override {
  125. SkRandom rand(1);
  126. canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
  127. SkRect oval = SkRect::MakeLTRB(-20, -30, 20, 30);
  128. const SkScalar kXStart = 60.0f;
  129. const SkScalar kYStart = 80.0f;
  130. const int kXStep = 150;
  131. const int kYStep = 160;
  132. int maxX = fMatrices.count();
  133. SkPaint rectPaint;
  134. rectPaint.setAntiAlias(true);
  135. rectPaint.setStyle(SkPaint::kStroke_Style);
  136. rectPaint.setStrokeWidth(SkIntToScalar(0));
  137. rectPaint.setColor(SK_ColorLTGRAY);
  138. int testCount = 0;
  139. for (int i = 0; i < fPaints.count(); ++i) {
  140. for (int j = 0; j < fMatrices.count(); ++j) {
  141. canvas->save();
  142. SkMatrix mat = fMatrices[j];
  143. // position the oval, and make it at off-integer coords.
  144. mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
  145. SK_Scalar1 / 4,
  146. kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
  147. 3 * SK_Scalar1 / 4);
  148. canvas->concat(mat);
  149. SkColor color = genColor(&rand);
  150. fPaints[i].setColor(color);
  151. canvas->drawRect(oval, rectPaint);
  152. canvas->drawOval(oval, fPaints[i]);
  153. canvas->restore();
  154. ++testCount;
  155. }
  156. }
  157. // special cases
  158. // non-scaled tall and skinny oval
  159. for (int i = 0; i < fPaints.count(); ++i) {
  160. SkRect oval = SkRect::MakeLTRB(-20, -60, 20, 60);
  161. canvas->save();
  162. // position the oval, and make it at off-integer coords.
  163. canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
  164. kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
  165. SkColor color = genColor(&rand);
  166. fPaints[i].setColor(color);
  167. canvas->drawRect(oval, rectPaint);
  168. canvas->drawOval(oval, fPaints[i]);
  169. canvas->restore();
  170. }
  171. // non-scaled wide and short oval
  172. for (int i = 0; i < fPaints.count(); ++i) {
  173. SkRect oval = SkRect::MakeLTRB(-80, -30, 80, 30);
  174. canvas->save();
  175. // position the oval, and make it at off-integer coords.
  176. canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
  177. kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
  178. SK_ScalarHalf * kYStep);
  179. SkColor color = genColor(&rand);
  180. fPaints[i].setColor(color);
  181. canvas->drawRect(oval, rectPaint);
  182. canvas->drawOval(oval, fPaints[i]);
  183. canvas->restore();
  184. }
  185. // super skinny oval
  186. for (int i = 0; i < fPaints.count(); ++i) {
  187. SkRect oval = SkRect::MakeLTRB(0, -60, 1, 60);
  188. canvas->save();
  189. // position the oval, and make it at off-integer coords.
  190. canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
  191. kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
  192. SkColor color = genColor(&rand);
  193. fPaints[i].setColor(color);
  194. canvas->drawOval(oval, fPaints[i]);
  195. canvas->restore();
  196. }
  197. // super short oval
  198. for (int i = 0; i < fPaints.count(); ++i) {
  199. SkRect oval = SkRect::MakeLTRB(-80, -1, 80, 0);
  200. canvas->save();
  201. // position the oval, and make it at off-integer coords.
  202. canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
  203. kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
  204. SK_ScalarHalf * kYStep);
  205. SkColor color = genColor(&rand);
  206. fPaints[i].setColor(color);
  207. canvas->drawOval(oval, fPaints[i]);
  208. canvas->restore();
  209. }
  210. // radial gradient
  211. SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
  212. SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
  213. SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
  214. auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
  215. SkTileMode::kClamp);
  216. for (int i = 0; i < fPaints.count(); ++i) {
  217. canvas->save();
  218. // position the path, and make it at off-integer coords.
  219. canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
  220. kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
  221. SK_ScalarHalf * kYStep);
  222. SkColor color = genColor(&rand);
  223. fPaints[i].setColor(color);
  224. fPaints[i].setShader(shader);
  225. canvas->drawRect(oval, rectPaint);
  226. canvas->drawOval(oval, fPaints[i]);
  227. fPaints[i].setShader(nullptr);
  228. canvas->restore();
  229. }
  230. // reflected oval
  231. for (int i = 0; i < fPaints.count(); ++i) {
  232. SkRect oval = SkRect::MakeLTRB(-30, -30, 30, 30);
  233. canvas->save();
  234. // position the oval, and make it at off-integer coords.
  235. canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
  236. kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
  237. SK_ScalarHalf * kYStep);
  238. canvas->rotate(90);
  239. canvas->scale(1, -1);
  240. canvas->scale(1, 0.66f);
  241. SkColor color = genColor(&rand);
  242. fPaints[i].setColor(color);
  243. canvas->drawRect(oval, rectPaint);
  244. canvas->drawOval(oval, fPaints[i]);
  245. canvas->restore();
  246. }
  247. }
  248. private:
  249. SkTArray<SkPaint> fPaints;
  250. SkTArray<SkMatrix> fMatrices;
  251. typedef GM INHERITED;
  252. };
  253. //////////////////////////////////////////////////////////////////////////////
  254. DEF_GM( return new OvalGM; )
  255. }