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