rects.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/SkBlurTypes.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkDrawLooper.h"
  12. #include "include/core/SkMaskFilter.h"
  13. #include "include/core/SkMatrix.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkPoint.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkShader.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkTileMode.h"
  22. #include "include/core/SkTypes.h"
  23. #include "include/effects/SkBlurDrawLooper.h"
  24. #include "include/effects/SkGradientShader.h"
  25. #include "include/private/SkTArray.h"
  26. #include "src/core/SkBlurMask.h"
  27. namespace skiagm {
  28. class RectsGM : public GM {
  29. sk_sp<SkDrawLooper> fLooper;
  30. enum {
  31. kLooperColorSentinel = 0x01020304
  32. };
  33. public:
  34. RectsGM() {
  35. this->setBGColor(0xFF000000);
  36. this->makePaints();
  37. this->makeMatrices();
  38. this->makeRects();
  39. }
  40. protected:
  41. SkString onShortName() override {
  42. return SkString("rects");
  43. }
  44. SkISize onISize() override {
  45. return SkISize::Make(1200, 900);
  46. }
  47. void makePaints() {
  48. {
  49. // no AA
  50. SkPaint p;
  51. p.setColor(SK_ColorWHITE);
  52. fPaints.push_back(p);
  53. }
  54. {
  55. // AA
  56. SkPaint p;
  57. p.setColor(SK_ColorWHITE);
  58. p.setAntiAlias(true);
  59. fPaints.push_back(p);
  60. }
  61. {
  62. // AA with translucent
  63. SkPaint p;
  64. p.setColor(SK_ColorWHITE);
  65. p.setAntiAlias(true);
  66. p.setAlpha(0x66);
  67. fPaints.push_back(p);
  68. }
  69. {
  70. // AA with mask filter
  71. SkPaint p;
  72. p.setColor(SK_ColorWHITE);
  73. p.setAntiAlias(true);
  74. p.setMaskFilter(SkMaskFilter::MakeBlur(
  75. kNormal_SkBlurStyle,
  76. SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
  77. fPaints.push_back(p);
  78. }
  79. {
  80. // AA with radial shader
  81. SkPaint p;
  82. p.setColor(SK_ColorWHITE);
  83. p.setAntiAlias(true);
  84. SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30));
  85. SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
  86. SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
  87. p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos,
  88. SK_ARRAY_COUNT(colors),
  89. SkTileMode::kClamp));
  90. fPaints.push_back(p);
  91. }
  92. #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
  93. {
  94. // AA with blur
  95. SkPaint p;
  96. p.setColor(SK_ColorWHITE);
  97. p.setAntiAlias(true);
  98. p.setLooper(SkBlurDrawLooper::Make(SK_ColorWHITE,
  99. SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
  100. SkIntToScalar(5), SkIntToScalar(10)));
  101. fPaints.push_back(p);
  102. }
  103. #else
  104. fLooper = SkBlurDrawLooper::Make(SK_ColorWHITE, SkBlurMask::ConvertRadiusToSigma(10),5,10);
  105. {
  106. SkPaint p;
  107. p.setColor(kLooperColorSentinel);
  108. p.setAntiAlias(true);
  109. fPaints.push_back(p);
  110. }
  111. #endif
  112. {
  113. // AA with stroke style
  114. SkPaint p;
  115. p.setColor(SK_ColorWHITE);
  116. p.setAntiAlias(true);
  117. p.setStyle(SkPaint::kStroke_Style);
  118. p.setStrokeWidth(SkIntToScalar(3));
  119. fPaints.push_back(p);
  120. }
  121. {
  122. // AA with bevel-stroke style
  123. SkPaint p;
  124. p.setColor(SK_ColorWHITE);
  125. p.setAntiAlias(true);
  126. p.setStyle(SkPaint::kStroke_Style);
  127. p.setStrokeJoin(SkPaint::kBevel_Join);
  128. p.setStrokeWidth(SkIntToScalar(3));
  129. fPaints.push_back(p);
  130. }
  131. {
  132. // AA with round-stroke style
  133. SkPaint p;
  134. p.setColor(SK_ColorWHITE);
  135. p.setAntiAlias(true);
  136. p.setStyle(SkPaint::kStroke_Style);
  137. p.setStrokeJoin(SkPaint::kRound_Join);
  138. p.setStrokeWidth(SkIntToScalar(3));
  139. fPaints.push_back(p);
  140. }
  141. {
  142. // AA with stroke style, width = 0
  143. SkPaint p;
  144. p.setColor(SK_ColorWHITE);
  145. p.setAntiAlias(true);
  146. p.setStyle(SkPaint::kStroke_Style);
  147. fPaints.push_back(p);
  148. }
  149. {
  150. // AA with stroke style, width wider than rect width and/or height
  151. SkPaint p;
  152. p.setColor(SK_ColorWHITE);
  153. p.setAntiAlias(true);
  154. p.setStyle(SkPaint::kStroke_Style);
  155. p.setStrokeWidth(SkIntToScalar(40));
  156. fPaints.push_back(p);
  157. }
  158. {
  159. // AA with stroke and fill style
  160. SkPaint p;
  161. p.setColor(SK_ColorWHITE);
  162. p.setAntiAlias(true);
  163. p.setStyle(SkPaint::kStrokeAndFill_Style);
  164. p.setStrokeWidth(SkIntToScalar(2));
  165. fPaints.push_back(p);
  166. }
  167. }
  168. void makeMatrices() {
  169. {
  170. // 1x1.5 scale
  171. SkMatrix m;
  172. m.setScale(1, 1.5f);
  173. fMatrices.push_back(m);
  174. }
  175. {
  176. // 1.5x1.5 scale
  177. SkMatrix m;
  178. m.setScale(1.5f, 1.5f);
  179. fMatrices.push_back(m);
  180. }
  181. {
  182. // 1x1.5 skew
  183. SkMatrix m;
  184. m.setSkew(1, 1.5f);
  185. fMatrices.push_back(m);
  186. }
  187. {
  188. // 1.5x1.5 skew
  189. SkMatrix m;
  190. m.setSkew(1.5f, 1.5f);
  191. fMatrices.push_back(m);
  192. }
  193. {
  194. // 30 degree rotation
  195. SkMatrix m;
  196. m.setRotate(SkIntToScalar(30));
  197. fMatrices.push_back(m);
  198. }
  199. {
  200. // 90 degree rotation
  201. SkMatrix m;
  202. m.setRotate(SkIntToScalar(90));
  203. fMatrices.push_back(m);
  204. }
  205. }
  206. void makeRects() {
  207. {
  208. // small square
  209. SkRect r = SkRect::MakeLTRB(0, 0, 30, 30);
  210. fRects.push_back(r);
  211. }
  212. {
  213. // thin vertical
  214. SkRect r = SkRect::MakeLTRB(0, 0, 2, 40);
  215. fRects.push_back(r);
  216. }
  217. {
  218. // thin horizontal
  219. SkRect r = SkRect::MakeLTRB(0, 0, 40, 2);
  220. fRects.push_back(r);
  221. }
  222. {
  223. // very thin
  224. SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10);
  225. fRects.push_back(r);
  226. }
  227. {
  228. // zaftig
  229. SkRect r = SkRect::MakeLTRB(0, 0, 60, 60);
  230. fRects.push_back(r);
  231. }
  232. }
  233. // position the current test on the canvas
  234. static void position(SkCanvas* canvas, int testCount) {
  235. canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4,
  236. SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4);
  237. }
  238. void onDraw(SkCanvas* canvas) override {
  239. canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
  240. int testCount = 0;
  241. for (int i = 0; i < fPaints.count(); ++i) {
  242. for (int j = 0; j < fRects.count(); ++j, ++testCount) {
  243. canvas->save();
  244. this->position(canvas, testCount);
  245. SkPaint p = fPaints[i];
  246. if (p.getColor() == kLooperColorSentinel) {
  247. p.setColor(SK_ColorWHITE);
  248. SkRect r = fRects[j];
  249. fLooper->apply(canvas, p, [r](SkCanvas* c, const SkPaint& p) {
  250. c->drawRect(r, p);
  251. });
  252. } else {
  253. canvas->drawRect(fRects[j], p);
  254. }
  255. canvas->restore();
  256. }
  257. }
  258. SkPaint paint;
  259. paint.setColor(SK_ColorWHITE);
  260. paint.setAntiAlias(true);
  261. for (int i = 0; i < fMatrices.count(); ++i) {
  262. for (int j = 0; j < fRects.count(); ++j, ++testCount) {
  263. canvas->save();
  264. this->position(canvas, testCount);
  265. canvas->concat(fMatrices[i]);
  266. canvas->drawRect(fRects[j], paint);
  267. canvas->restore();
  268. }
  269. }
  270. }
  271. private:
  272. SkTArray<SkPaint> fPaints;
  273. SkTArray<SkMatrix> fMatrices;
  274. SkTArray<SkRect> fRects;
  275. typedef GM INHERITED;
  276. };
  277. //////////////////////////////////////////////////////////////////////////////
  278. DEF_GM( return new RectsGM; )
  279. }