drawquadset.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright 2018 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/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkMatrix.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkShader.h"
  18. #include "include/core/SkSize.h"
  19. #include "include/core/SkString.h"
  20. #include "include/core/SkTileMode.h"
  21. #include "include/core/SkTypeface.h"
  22. #include "include/core/SkTypes.h"
  23. #include "include/effects/SkGradientShader.h"
  24. #include "include/gpu/GrContext.h"
  25. #include "include/private/GrTypesPriv.h"
  26. #include "src/gpu/GrClip.h"
  27. #include "src/gpu/GrPaint.h"
  28. #include "src/gpu/GrRenderTargetContext.h"
  29. #include "src/gpu/SkGr.h"
  30. #include <utility>
  31. static constexpr SkScalar kTileWidth = 40;
  32. static constexpr SkScalar kTileHeight = 30;
  33. static constexpr int kRowCount = 4;
  34. static constexpr int kColCount = 3;
  35. static void draw_text(SkCanvas* canvas, const char* text) {
  36. canvas->drawString(text, 0, 0, SkFont(nullptr, 12), SkPaint());
  37. }
  38. static void draw_gradient_tiles(SkCanvas* canvas, bool alignGradients) {
  39. // Always draw the same gradient
  40. static constexpr SkPoint pts[] = { {0.f, 0.f}, {0.25f * kTileWidth, 0.25f * kTileHeight} };
  41. static constexpr SkColor colors[] = { SK_ColorBLUE, SK_ColorWHITE };
  42. GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
  43. GrContext* context = canvas->getGrContext();
  44. auto gradient = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror);
  45. SkPaint paint;
  46. paint.setShader(gradient);
  47. for (int i = 0; i < kRowCount; ++i) {
  48. for (int j = 0; j < kColCount; ++j) {
  49. SkRect tile = SkRect::MakeWH(kTileWidth, kTileHeight);
  50. if (alignGradients) {
  51. tile.offset(j * kTileWidth, i * kTileHeight);
  52. } else {
  53. canvas->save();
  54. canvas->translate(j * kTileWidth, i * kTileHeight);
  55. }
  56. unsigned aa = SkCanvas::kNone_QuadAAFlags;
  57. if (i == 0) {
  58. aa |= SkCanvas::kTop_QuadAAFlag;
  59. }
  60. if (i == kRowCount - 1) {
  61. aa |= SkCanvas::kBottom_QuadAAFlag;
  62. }
  63. if (j == 0) {
  64. aa |= SkCanvas::kLeft_QuadAAFlag;
  65. }
  66. if (j == kColCount - 1) {
  67. aa |= SkCanvas::kRight_QuadAAFlag;
  68. }
  69. if (rtc) {
  70. // Use non-public API to leverage general GrPaint capabilities
  71. SkMatrix view = canvas->getTotalMatrix();
  72. GrPaint grPaint;
  73. SkPaintToGrPaint(context, rtc->colorSpaceInfo(), paint, view, &grPaint);
  74. rtc->fillRectWithEdgeAA(GrNoClip(), std::move(grPaint), GrAA::kYes,
  75. static_cast<GrQuadAAFlags>(aa), view, tile);
  76. } else {
  77. // Fallback to solid color on raster backend since the public API only has color
  78. SkColor color = alignGradients ? SK_ColorBLUE
  79. : (i * kColCount + j) % 2 == 0 ? SK_ColorBLUE
  80. : SK_ColorWHITE;
  81. canvas->experimental_DrawEdgeAAQuad(
  82. tile, nullptr, static_cast<SkCanvas::QuadAAFlags>(aa), color,
  83. SkBlendMode::kSrcOver);
  84. }
  85. if (!alignGradients) {
  86. // Pop off the matrix translation when drawing unaligned
  87. canvas->restore();
  88. }
  89. }
  90. }
  91. }
  92. static void draw_color_tiles(SkCanvas* canvas, bool multicolor) {
  93. for (int i = 0; i < kRowCount; ++i) {
  94. for (int j = 0; j < kColCount; ++j) {
  95. SkRect tile = SkRect::MakeXYWH(j * kTileWidth, i * kTileHeight, kTileWidth, kTileHeight);
  96. SkColor4f color;
  97. if (multicolor) {
  98. color = {(i + 1.f) / kRowCount, (j + 1.f) / kColCount, .4f, 1.f};
  99. } else {
  100. color = {.2f, .8f, .3f, 1.f};
  101. }
  102. unsigned aa = SkCanvas::kNone_QuadAAFlags;
  103. if (i == 0) {
  104. aa |= SkCanvas::kTop_QuadAAFlag;
  105. }
  106. if (i == kRowCount - 1) {
  107. aa |= SkCanvas::kBottom_QuadAAFlag;
  108. }
  109. if (j == 0) {
  110. aa |= SkCanvas::kLeft_QuadAAFlag;
  111. }
  112. if (j == kColCount - 1) {
  113. aa |= SkCanvas::kRight_QuadAAFlag;
  114. }
  115. canvas->experimental_DrawEdgeAAQuad(
  116. tile, nullptr, static_cast<SkCanvas::QuadAAFlags>(aa), color.toSkColor(),
  117. SkBlendMode::kSrcOver);
  118. }
  119. }
  120. }
  121. static void draw_tile_boundaries(SkCanvas* canvas, const SkMatrix& local) {
  122. // Draw grid of red lines at interior tile boundaries.
  123. static constexpr SkScalar kLineOutset = 10.f;
  124. SkPaint paint;
  125. paint.setAntiAlias(true);
  126. paint.setColor(SK_ColorRED);
  127. paint.setStyle(SkPaint::kStroke_Style);
  128. paint.setStrokeWidth(0.f);
  129. for (int x = 1; x < kColCount; ++x) {
  130. SkPoint pts[] = {{x * kTileWidth, 0}, {x * kTileWidth, kRowCount * kTileHeight}};
  131. local.mapPoints(pts, 2);
  132. SkVector v = pts[1] - pts[0];
  133. v.setLength(v.length() + kLineOutset);
  134. canvas->drawLine(pts[1] - v, pts[0] + v, paint);
  135. }
  136. for (int y = 1; y < kRowCount; ++y) {
  137. SkPoint pts[] = {{0, y * kTileHeight}, {kTileWidth * kColCount, y * kTileHeight}};
  138. local.mapPoints(pts, 2);
  139. SkVector v = pts[1] - pts[0];
  140. v.setLength(v.length() + kLineOutset);
  141. canvas->drawLine(pts[1] - v, pts[0] + v, paint);
  142. }
  143. }
  144. // Tile renderers (column variation)
  145. typedef void (*TileRenderer)(SkCanvas*);
  146. static TileRenderer kTileSets[] = {
  147. [](SkCanvas* canvas) { draw_gradient_tiles(canvas, /* aligned */ false); },
  148. [](SkCanvas* canvas) { draw_gradient_tiles(canvas, /* aligned */ true); },
  149. [](SkCanvas* canvas) { draw_color_tiles(canvas, /* multicolor */ false); },
  150. [](SkCanvas* canvas) { draw_color_tiles(canvas, /* multicolor */true); },
  151. };
  152. static const char* kTileSetNames[] = { "Local", "Aligned", "Green", "Multicolor" };
  153. static_assert(SK_ARRAY_COUNT(kTileSets) == SK_ARRAY_COUNT(kTileSetNames), "Count mismatch");
  154. namespace skiagm {
  155. class DrawQuadSetGM : public GM {
  156. private:
  157. SkString onShortName() override { return SkString("draw_quad_set"); }
  158. SkISize onISize() override { return SkISize::Make(800, 800); }
  159. void onDraw(SkCanvas* canvas) override {
  160. SkMatrix rowMatrices[5];
  161. // Identity
  162. rowMatrices[0].setIdentity();
  163. // Translate/scale
  164. rowMatrices[1].setTranslate(5.5f, 20.25f);
  165. rowMatrices[1].postScale(.9f, .7f);
  166. // Rotation
  167. rowMatrices[2].setRotate(20.0f);
  168. rowMatrices[2].preTranslate(15.f, -20.f);
  169. // Skew
  170. rowMatrices[3].setSkew(.5f, .25f);
  171. rowMatrices[3].preTranslate(-30.f, 0.f);
  172. // Perspective
  173. SkPoint src[4];
  174. SkRect::MakeWH(kColCount * kTileWidth, kRowCount * kTileHeight).toQuad(src);
  175. SkPoint dst[4] = {{0, 0},
  176. {kColCount * kTileWidth + 10.f, 15.f},
  177. {kColCount * kTileWidth - 28.f, kRowCount * kTileHeight + 40.f},
  178. {25.f, kRowCount * kTileHeight - 15.f}};
  179. SkAssertResult(rowMatrices[4].setPolyToPoly(src, dst, 4));
  180. rowMatrices[4].preTranslate(0.f, +10.f);
  181. static const char* matrixNames[] = { "Identity", "T+S", "Rotate", "Skew", "Perspective" };
  182. static_assert(SK_ARRAY_COUNT(matrixNames) == SK_ARRAY_COUNT(rowMatrices), "Count mismatch");
  183. // Print a column header
  184. canvas->save();
  185. canvas->translate(110.f, 20.f);
  186. for (size_t j = 0; j < SK_ARRAY_COUNT(kTileSetNames); ++j) {
  187. draw_text(canvas, kTileSetNames[j]);
  188. canvas->translate(kColCount * kTileWidth + 30.f, 0.f);
  189. }
  190. canvas->restore();
  191. canvas->translate(0.f, 40.f);
  192. // Render all tile variations
  193. for (size_t i = 0; i < SK_ARRAY_COUNT(rowMatrices); ++i) {
  194. canvas->save();
  195. canvas->translate(10.f, 0.5f * kRowCount * kTileHeight);
  196. draw_text(canvas, matrixNames[i]);
  197. canvas->translate(100.f, -0.5f * kRowCount * kTileHeight);
  198. for (size_t j = 0; j < SK_ARRAY_COUNT(kTileSets); ++j) {
  199. canvas->save();
  200. draw_tile_boundaries(canvas, rowMatrices[i]);
  201. canvas->concat(rowMatrices[i]);
  202. kTileSets[j](canvas);
  203. // Undo the local transformation
  204. canvas->restore();
  205. // And advance to the next column
  206. canvas->translate(kColCount * kTileWidth + 30.f, 0.f);
  207. }
  208. // Reset back to the left edge
  209. canvas->restore();
  210. // And advance to the next row
  211. canvas->translate(0.f, kRowCount * kTileHeight + 20.f);
  212. }
  213. }
  214. };
  215. DEF_GM(return new DrawQuadSetGM();)
  216. } // namespace skiagm