• 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/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorFilter.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkGradientShader.h"
28 #include "tools/ToolUtils.h"
29 
30 #include <algorithm>
31 #include <initializer_list>
32 #include <utility>
33 
34 // Makes a set of m x n tiled images to be drawn with SkCanvas::experimental_drawImageSetV1().
make_image_tiles(int tileW,int tileH,int m,int n,const SkColor colors[4],SkCanvas::ImageSetEntry set[],const SkColor bgColor=SK_ColorLTGRAY)35 static void make_image_tiles(int tileW, int tileH, int m, int n, const SkColor colors[4],
36                              SkCanvas::ImageSetEntry set[], const SkColor bgColor=SK_ColorLTGRAY) {
37     const int w = tileW * m;
38     const int h = tileH * n;
39     auto surf = SkSurface::MakeRaster(
40             SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
41     surf->getCanvas()->clear(bgColor);
42 
43     static constexpr SkScalar kStripeW = 10;
44     static constexpr SkScalar kStripeSpacing = 30;
45     SkPaint paint;
46 
47     SkPoint pts1[] = {{0.f, 0.f}, {(SkScalar)w, (SkScalar)h}};
48     auto grad = SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkTileMode::kClamp);
49     paint.setShader(std::move(grad));
50     paint.setAntiAlias(true);
51     paint.setStyle(SkPaint::kStroke_Style);
52     paint.setStrokeWidth(kStripeW);
53     SkPoint stripePts[] = {{-w - kStripeW, -kStripeW}, {kStripeW, h + kStripeW}};
54     while (stripePts[0].fX <= w) {
55         surf->getCanvas()->drawPoints(SkCanvas::kLines_PointMode, 2, stripePts, paint);
56         stripePts[0].fX += kStripeSpacing;
57         stripePts[1].fX += kStripeSpacing;
58     }
59 
60     SkPoint pts2[] = {{0.f, (SkScalar)h}, {(SkScalar)w, 0.f}};
61     grad = SkGradientShader::MakeLinear(pts2, colors + 2, nullptr, 2, SkTileMode::kClamp);
62     paint.setShader(std::move(grad));
63     paint.setBlendMode(SkBlendMode::kMultiply);
64     stripePts[0] = {-w - kStripeW, h + kStripeW};
65     stripePts[1] = {kStripeW, -kStripeW};
66     while (stripePts[0].fX <= w) {
67         surf->getCanvas()->drawPoints(SkCanvas::kLines_PointMode, 2, stripePts, paint);
68         stripePts[0].fX += kStripeSpacing;
69         stripePts[1].fX += kStripeSpacing;
70     }
71     auto fullImage = surf->makeImageSnapshot();
72     for (int y = 0; y < n; ++y) {
73         for (int x = 0; x < m; ++x) {
74             // Images will have 1 pixel of overlap at interior seams for filtering continuity.
75             SkIRect subset = SkIRect::MakeXYWH(x * tileW - 1, y * tileH - 1, tileW + 2, tileH + 2);
76             set[y * m + x].fAAFlags = SkCanvas::kNone_QuadAAFlags;
77             if (x == 0) {
78                 subset.fLeft = 0;
79                 set[y * m + x].fAAFlags |= SkCanvas::kLeft_QuadAAFlag;
80             }
81             if (x == m - 1) {
82                 subset.fRight = w;
83                 set[y * m + x].fAAFlags |= SkCanvas::kRight_QuadAAFlag;
84             }
85             if (y == 0) {
86                 subset.fTop = 0;
87                 set[y * m + x].fAAFlags |= SkCanvas::kTop_QuadAAFlag;
88             }
89             if (y == n - 1) {
90                 subset.fBottom = h;
91                 set[y * m + x].fAAFlags |= SkCanvas::kBottom_QuadAAFlag;
92             }
93             set[y * m + x].fImage = fullImage->makeSubset(subset);
94             set[y * m + x].fSrcRect =
95                     SkRect::MakeXYWH(x == 0 ? 0 : 1, y == 0 ? 0 : 1, tileW, tileH);
96             set[y * m + x].fDstRect = SkRect::MakeXYWH(x * tileW, y * tileH, tileW, tileH);
97             set[y * m + x].fAlpha = 1.f;
98             SkASSERT(set[y * m + x].fImage);
99         }
100     }
101 }
102 
103 namespace skiagm {
104 
105 class DrawImageSetGM : public GM {
106 private:
onShortName()107     SkString onShortName() override { return SkString("draw_image_set"); }
onISize()108     SkISize onISize() override { return {1000, 725}; }
onOnceBeforeDraw()109     void onOnceBeforeDraw() override {
110         static constexpr SkColor kColors[] = {SK_ColorCYAN,    SK_ColorBLACK,
111                                               SK_ColorMAGENTA, SK_ColorBLACK};
112         make_image_tiles(kTileW, kTileH, kM, kN, kColors, fSet);
113     }
114 
onDraw(SkCanvas * canvas)115     void onDraw(SkCanvas* canvas) override {
116         SkScalar d = SkVector{kM * kTileW, kN * kTileH}.length();
117         SkMatrix matrices[4];
118         // rotation
119         matrices[0].setRotate(30);
120         matrices[0].postTranslate(d / 3, 0);
121         // perespective
122         SkPoint src[4];
123         SkRect::MakeWH(kM * kTileW, kN * kTileH).toQuad(src);
124         SkPoint dst[4] = {{0, 0},
125                           {kM * kTileW + 10.f, -5.f},
126                           {kM * kTileW - 28.f, kN * kTileH + 40.f},
127                           {45.f, kN * kTileH - 25.f}};
128         SkAssertResult(matrices[1].setPolyToPoly(src, dst, 4));
129         matrices[1].postTranslate(d, 50.f);
130         // skew
131         matrices[2].setRotate(-60.f);
132         matrices[2].postSkew(0.5f, -1.15f);
133         matrices[2].postScale(0.6f, 1.05f);
134         matrices[2].postTranslate(d, 2.6f * d);
135         // perspective + mirror in x.
136         dst[1] = {-.25 * kM * kTileW, 0};
137         dst[0] = {5.f / 4.f * kM * kTileW, 0};
138         dst[3] = {2.f / 3.f * kM * kTileW, 1 / 2.f * kN * kTileH};
139         dst[2] = {1.f / 3.f * kM * kTileW, 1 / 2.f * kN * kTileH - 0.1f * kTileH};
140         SkAssertResult(matrices[3].setPolyToPoly(src, dst, 4));
141         matrices[3].postTranslate(100.f, d);
142         for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) {
143             SkPaint setPaint;
144             setPaint.setBlendMode(SkBlendMode::kSrcOver);
145             SkSamplingOptions sampling(fm);
146 
147             for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) {
148                 // Draw grid of red lines at interior tile boundaries.
149                 static constexpr SkScalar kLineOutset = 10.f;
150                 SkPaint paint;
151                 paint.setAntiAlias(true);
152                 paint.setColor(SK_ColorRED);
153                 paint.setStyle(SkPaint::kStroke_Style);
154                 paint.setStrokeWidth(0.f);
155                 for (int x = 1; x < kM; ++x) {
156                     SkPoint pts[] = {{x * kTileW, 0}, {x * kTileW, kN * kTileH}};
157                     matrices[m].mapPoints(pts, 2);
158                     SkVector v = pts[1] - pts[0];
159                     v.setLength(v.length() + kLineOutset);
160                     canvas->drawLine(pts[1] - v, pts[0] + v, paint);
161                 }
162                 for (int y = 1; y < kN; ++y) {
163                     SkPoint pts[] = {{0, y * kTileH}, {kTileW * kM, y * kTileH}};
164                     matrices[m].mapPoints(pts, 2);
165                     SkVector v = pts[1] - pts[0];
166                     v.setLength(v.length() + kLineOutset);
167                     canvas->drawLine(pts[1] - v, pts[0] + v, paint);
168                 }
169                 canvas->save();
170                 canvas->concat(matrices[m]);
171                 canvas->experimental_DrawEdgeAAImageSet(fSet, kM * kN, nullptr, nullptr, sampling,
172                                                         &setPaint,
173                                                         SkCanvas::kFast_SrcRectConstraint);
174                 canvas->restore();
175             }
176             // A more exotic case with an unusual blend mode, mixed aa flags set, and alpha,
177             // subsets the image. And another with all the above plus a color filter.
178             SkCanvas::ImageSetEntry entry;
179             entry.fSrcRect = SkRect::MakeWH(kTileW, kTileH).makeInset(kTileW / 4.f, kTileH / 4.f);
180             entry.fDstRect = SkRect::MakeWH(1.5 * kTileW, 1.5 * kTileH).makeOffset(d / 4, 2 * d);
181             entry.fImage = fSet[0].fImage;
182             entry.fAlpha = 0.7f;
183             entry.fAAFlags = SkCanvas::kLeft_QuadAAFlag | SkCanvas::kTop_QuadAAFlag;
184             canvas->save();
185             canvas->rotate(3.f);
186 
187             setPaint.setBlendMode(SkBlendMode::kExclusion);
188             canvas->experimental_DrawEdgeAAImageSet(&entry, 1, nullptr, nullptr, sampling,
189                                                     &setPaint, SkCanvas::kFast_SrcRectConstraint);
190             canvas->translate(entry.fDstRect.width() + 8.f, 0);
191             SkPaint cfPaint = setPaint;
192             cfPaint.setColorFilter(SkColorFilters::LinearToSRGBGamma());
193             canvas->experimental_DrawEdgeAAImageSet(&entry, 1, nullptr, nullptr, sampling,
194                                                     &cfPaint, SkCanvas::kFast_SrcRectConstraint);
195             canvas->restore();
196             canvas->translate(2 * d, 0);
197         }
198     }
199     inline static constexpr int kM = 4;
200     inline static constexpr int kN = 3;
201     inline static constexpr SkScalar kTileW = 30;
202     inline static constexpr SkScalar kTileH = 60;
203     SkCanvas::ImageSetEntry fSet[kM * kN];
204 };
205 
206 // This GM exercises rect-stays-rect type matrices to test that filtering and antialiasing are not
207 // incorrectly disabled.
208 class DrawImageSetRectToRectGM : public GM {
209 private:
onShortName()210     SkString onShortName() override { return SkString("draw_image_set_rect_to_rect"); }
onISize()211     SkISize onISize() override { return {1250, 850}; }
onOnceBeforeDraw()212     void onOnceBeforeDraw() override {
213         static constexpr SkColor kColors[] = {SK_ColorBLUE, SK_ColorWHITE,
214                                               SK_ColorRED,  SK_ColorWHITE};
215         make_image_tiles(kTileW, kTileH, kM, kN, kColors, fSet);
216     }
217 
onDraw(SkCanvas * canvas)218     void onDraw(SkCanvas* canvas) override {
219         ToolUtils::draw_checkerboard(canvas, SK_ColorBLACK, SK_ColorWHITE, 50);
220         static constexpr SkScalar kW = kM * kTileW;
221         static constexpr SkScalar kH = kN * kTileH;
222         SkMatrix matrices[5];
223         // Identity
224         matrices[0].reset();
225         // 90 degree rotation
226         matrices[1].setRotate(90, kW / 2.f, kH / 2.f);
227         // Scaling
228         matrices[2].setScale(2.f, 0.5f);
229         // Mirror in x and y
230         matrices[3].setScale(-1.f, -1.f);
231         matrices[3].postTranslate(kW, kH);
232         // Mirror in y, rotate, and scale.
233         matrices[4].setScale(1.f, -1.f);
234         matrices[4].postTranslate(0, kH);
235         matrices[4].postRotate(90, kW / 2.f, kH / 2.f);
236         matrices[4].postScale(2.f, 0.5f);
237 
238         SkPaint paint;
239         paint.setBlendMode(SkBlendMode::kSrcOver);
240 
241         static constexpr SkScalar kTranslate = std::max(kW, kH) * 2.f + 10.f;
242         canvas->translate(5.f, 5.f);
243         canvas->save();
244         for (SkScalar frac : {0.f, 0.5f}) {
245             canvas->save();
246             canvas->translate(frac, frac);
247             for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) {
248                 canvas->save();
249                 canvas->concat(matrices[m]);
250                 canvas->experimental_DrawEdgeAAImageSet(fSet, kM * kN, nullptr, nullptr,
251                                                         SkSamplingOptions(SkFilterMode::kLinear),
252                                                         &paint, SkCanvas::kFast_SrcRectConstraint);
253                 canvas->restore();
254                 canvas->translate(kTranslate, 0);
255             }
256             canvas->restore();
257             canvas->restore();
258             canvas->translate(0, kTranslate);
259             canvas->save();
260         }
261         for (SkVector scale : {SkVector{2.f, 0.5f}, SkVector{0.5, 2.f}}) {
262             SkCanvas::ImageSetEntry scaledSet[kM * kN];
263             std::copy_n(fSet, kM * kN, scaledSet);
264             for (int i = 0; i < kM * kN; ++i) {
265                 scaledSet[i].fDstRect.fLeft *= scale.fX;
266                 scaledSet[i].fDstRect.fTop *= scale.fY;
267                 scaledSet[i].fDstRect.fRight *= scale.fX;
268                 scaledSet[i].fDstRect.fBottom *= scale.fY;
269                 scaledSet[i].fAlpha = 0 == (i % 3) ? 0.4f : 1.f;
270             }
271             for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) {
272                 canvas->save();
273                 canvas->concat(matrices[m]);
274                 canvas->experimental_DrawEdgeAAImageSet(scaledSet, kM * kN, nullptr, nullptr,
275                                                         SkSamplingOptions(SkFilterMode::kLinear),
276                                                         &paint, SkCanvas::kFast_SrcRectConstraint);
277                 canvas->restore();
278                 canvas->translate(kTranslate, 0);
279             }
280             canvas->restore();
281             canvas->translate(0, kTranslate);
282             canvas->save();
283         }
284     }
285     inline static constexpr int kM = 2;
286     inline static constexpr int kN = 2;
287     inline static constexpr int kTileW = 40;
288     inline static constexpr int kTileH = 50;
289     SkCanvas::ImageSetEntry fSet[kM * kN];
290 };
291 
292 // This GM exercises alpha-only and color textures being combined correctly with the paint's color.
293 class DrawImageSetAlphaOnlyGM : public GM {
294 private:
onShortName()295     SkString onShortName() override { return SkString("draw_image_set_alpha_only"); }
onISize()296     SkISize onISize() override { return {kM*kTileW, 2*kN*kTileH}; }
297 
onGpuSetup(GrDirectContext * direct,SkString *)298     DrawResult onGpuSetup(GrDirectContext* direct, SkString*) override {
299         static constexpr SkColor kColors[] = {SK_ColorBLUE, SK_ColorTRANSPARENT,
300                                               SK_ColorRED,  SK_ColorTRANSPARENT};
301         static constexpr SkColor kBGColor = SkColorSetARGB(128, 128, 128, 128);
302         make_image_tiles(kTileW, kTileH, kM, kN, kColors, fSet, kBGColor);
303 
304         // Modify the alpha of the entries, decreasing by column, and convert even rows to
305         // alpha-only textures.
306         sk_sp<SkColorSpace> alphaSpace = SkColorSpace::MakeSRGB();
307         for (int y = 0; y < kN; ++y) {
308             for (int x = 0; x < kM; ++x) {
309                 int i = y * kM + x;
310                 fSet[i].fAlpha = (kM - x) / (float) kM;
311                 if (y % 2 == 0) {
312                     fSet[i].fImage = fSet[i].fImage->makeColorTypeAndColorSpace(
313                             kAlpha_8_SkColorType, alphaSpace, direct);
314                 }
315             }
316         }
317         return skiagm::DrawResult::kOk;
318     }
319 
onDraw(SkCanvas * canvas)320     void onDraw(SkCanvas* canvas) override {
321         ToolUtils::draw_checkerboard(canvas, SK_ColorGRAY, SK_ColorDKGRAY, 25);
322 
323         SkPaint paint;
324         paint.setBlendMode(SkBlendMode::kSrcOver);
325         paint.setColor4f({0.2f, 0.8f, 0.4f, 1.f}); // colorizes even rows, no effect on odd rows
326 
327         // Top rows use experimental edge set API
328         canvas->experimental_DrawEdgeAAImageSet(fSet, kM * kN, nullptr, nullptr,
329                                                 SkSamplingOptions(SkFilterMode::kLinear), &paint,
330                                                 SkCanvas::kFast_SrcRectConstraint);
331 
332         canvas->translate(0.f, kN * kTileH);
333 
334         // Bottom rows draw each image from the set using the regular API
335         for (int y = 0; y < kN; ++y) {
336             for (int x = 0; x < kM; ++x) {
337                 int i = y * kM + x;
338                 SkPaint entryPaint = paint;
339                 entryPaint.setAlphaf(fSet[i].fAlpha * paint.getAlphaf());
340                 canvas->drawImageRect(fSet[i].fImage.get(), fSet[i].fSrcRect, fSet[i].fDstRect,
341                                       SkSamplingOptions(), &entryPaint,
342                                       SkCanvas::kFast_SrcRectConstraint);
343             }
344         }
345     }
346 
347     inline static constexpr int kM = 4;
348     inline static constexpr int kN = 4;
349     inline static constexpr int kTileW = 50;
350     inline static constexpr int kTileH = 50;
351     SkCanvas::ImageSetEntry fSet[kM * kN];
352 };
353 
354 DEF_GM(return new DrawImageSetGM();)
355 DEF_GM(return new DrawImageSetRectToRectGM();)
356 DEF_GM(return new DrawImageSetAlphaOnlyGM();)
357 
358 }  // namespace skiagm
359