blob: d99c3c3a9ec53925de4eb1309a5c2c9ef7bcd819 [file] [log] [blame]
Brian Salomond7065e72018-10-12 11:42:02 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Brian Salomondb151e02019-09-17 12:11:16 -040012#include "include/core/SkColorFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFilterQuality.h"
14#include "include/core/SkImage.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkMatrix.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkPoint.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
22#include "include/core/SkShader.h"
23#include "include/core/SkSize.h"
24#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/core/SkTileMode.h"
27#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/effects/SkGradientShader.h"
29#include "tools/ToolUtils.h"
Brian Salomond7065e72018-10-12 11:42:02 -040030
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include <algorithm>
32#include <initializer_list>
33#include <utility>
34
Brian Salomon1da5cad2018-11-21 09:21:18 -050035// Makes a set of m x n tiled images to be drawn with SkCanvas::experimental_drawImageSetV1().
Brian Salomon0087c832018-10-15 14:48:20 -040036static void make_image_tiles(int tileW, int tileH, int m, int n, const SkColor colors[4],
37 SkCanvas::ImageSetEntry set[]) {
38 const int w = tileW * m;
39 const int h = tileH * n;
40 auto surf = SkSurface::MakeRaster(
41 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
42 surf->getCanvas()->clear(SK_ColorLTGRAY);
43
44 static constexpr SkScalar kStripeW = 10;
45 static constexpr SkScalar kStripeSpacing = 30;
46 SkPaint paint;
47
48 SkPoint pts1[] = {{0.f, 0.f}, {(SkScalar)w, (SkScalar)h}};
Mike Reedfae8fce2019-04-03 10:27:45 -040049 auto grad = SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkTileMode::kClamp);
Brian Salomon0087c832018-10-15 14:48:20 -040050 paint.setShader(std::move(grad));
51 paint.setAntiAlias(true);
52 paint.setStyle(SkPaint::kStroke_Style);
53 paint.setStrokeWidth(kStripeW);
54 SkPoint stripePts[] = {{-w - kStripeW, -kStripeW}, {kStripeW, h + kStripeW}};
55 while (stripePts[0].fX <= w) {
56 surf->getCanvas()->drawPoints(SkCanvas::kLines_PointMode, 2, stripePts, paint);
57 stripePts[0].fX += kStripeSpacing;
58 stripePts[1].fX += kStripeSpacing;
59 }
60
61 SkPoint pts2[] = {{0.f, (SkScalar)h}, {(SkScalar)w, 0.f}};
Mike Reedfae8fce2019-04-03 10:27:45 -040062 grad = SkGradientShader::MakeLinear(pts2, colors + 2, nullptr, 2, SkTileMode::kClamp);
Brian Salomon0087c832018-10-15 14:48:20 -040063 paint.setShader(std::move(grad));
64 paint.setBlendMode(SkBlendMode::kMultiply);
65 stripePts[0] = {-w - kStripeW, h + kStripeW};
66 stripePts[1] = {kStripeW, -kStripeW};
67 while (stripePts[0].fX <= w) {
68 surf->getCanvas()->drawPoints(SkCanvas::kLines_PointMode, 2, stripePts, paint);
69 stripePts[0].fX += kStripeSpacing;
70 stripePts[1].fX += kStripeSpacing;
71 }
72 auto fullImage = surf->makeImageSnapshot();
73 for (int y = 0; y < n; ++y) {
74 for (int x = 0; x < m; ++x) {
75 // Images will have 1 pixel of overlap at interior seams for filtering continuity.
76 SkIRect subset = SkIRect::MakeXYWH(x * tileW - 1, y * tileH - 1, tileW + 2, tileH + 2);
77 set[y * m + x].fAAFlags = SkCanvas::kNone_QuadAAFlags;
78 if (x == 0) {
79 subset.fLeft = 0;
80 set[y * m + x].fAAFlags |= SkCanvas::kLeft_QuadAAFlag;
81 }
82 if (x == m - 1) {
83 subset.fRight = w;
84 set[y * m + x].fAAFlags |= SkCanvas::kRight_QuadAAFlag;
85 }
86 if (y == 0) {
87 subset.fTop = 0;
88 set[y * m + x].fAAFlags |= SkCanvas::kTop_QuadAAFlag;
89 }
90 if (y == n - 1) {
91 subset.fBottom = h;
92 set[y * m + x].fAAFlags |= SkCanvas::kBottom_QuadAAFlag;
93 }
94 set[y * m + x].fImage = fullImage->makeSubset(subset);
95 set[y * m + x].fSrcRect =
96 SkRect::MakeXYWH(x == 0 ? 0 : 1, y == 0 ? 0 : 1, tileW, tileH);
97 set[y * m + x].fDstRect = SkRect::MakeXYWH(x * tileW, y * tileH, tileW, tileH);
Brian Salomon1da5cad2018-11-21 09:21:18 -050098 set[y * m + x].fAlpha = 1.f;
Brian Salomon0087c832018-10-15 14:48:20 -040099 SkASSERT(set[y * m + x].fImage);
100 }
101 }
102}
103
Brian Salomond7065e72018-10-12 11:42:02 -0400104namespace skiagm {
105
106class DrawImageSetGM : public GM {
107private:
Hal Canaryfa3305a2019-07-18 12:36:54 -0400108 SkString onShortName() override { return SkString("draw_image_set"); }
109 SkISize onISize() override { return {1000, 725}; }
Brian Salomond7065e72018-10-12 11:42:02 -0400110 void onOnceBeforeDraw() override {
Brian Salomon0087c832018-10-15 14:48:20 -0400111 static constexpr SkColor kColors[] = {SK_ColorCYAN, SK_ColorBLACK,
112 SK_ColorMAGENTA, SK_ColorBLACK};
113 make_image_tiles(kTileW, kTileH, kM, kN, kColors, fSet);
Brian Salomond7065e72018-10-12 11:42:02 -0400114 }
115
116 void onDraw(SkCanvas* canvas) override {
117 SkScalar d = SkVector{kM * kTileW, kN * kTileH}.length();
118 SkMatrix matrices[4];
119 // rotation
120 matrices[0].setRotate(30);
121 matrices[0].postTranslate(d / 3, 0);
122 // perespective
123 SkPoint src[4];
124 SkRect::MakeWH(kM * kTileW, kN * kTileH).toQuad(src);
125 SkPoint dst[4] = {{0, 0},
126 {kM * kTileW + 10.f, -5.f},
127 {kM * kTileW - 28.f, kN * kTileH + 40.f},
128 {45.f, kN * kTileH - 25.f}};
129 SkAssertResult(matrices[1].setPolyToPoly(src, dst, 4));
130 matrices[1].postTranslate(d, 50.f);
131 // skew
132 matrices[2].setRotate(-60.f);
133 matrices[2].postSkew(0.5f, -1.15f);
134 matrices[2].postScale(0.6f, 1.05f);
135 matrices[2].postTranslate(d, 2.6f * d);
136 // perspective + mirror in x.
137 dst[1] = {-.25 * kM * kTileW, 0};
138 dst[0] = {5.f / 4.f * kM * kTileW, 0};
139 dst[3] = {2.f / 3.f * kM * kTileW, 1 / 2.f * kN * kTileH};
140 dst[2] = {1.f / 3.f * kM * kTileW, 1 / 2.f * kN * kTileH - 0.1f * kTileH};
141 SkAssertResult(matrices[3].setPolyToPoly(src, dst, 4));
142 matrices[3].postTranslate(100.f, d);
Brian Salomond7065e72018-10-12 11:42:02 -0400143 for (auto fm : {kNone_SkFilterQuality, kLow_SkFilterQuality}) {
Michael Ludwigcf677782019-03-26 16:41:49 -0400144 SkPaint setPaint;
145 setPaint.setFilterQuality(fm);
146 setPaint.setBlendMode(SkBlendMode::kSrcOver);
Michael Ludwig926fb892019-03-22 16:37:53 -0400147
Brian Salomond7065e72018-10-12 11:42:02 -0400148 for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) {
149 // Draw grid of red lines at interior tile boundaries.
150 static constexpr SkScalar kLineOutset = 10.f;
151 SkPaint paint;
152 paint.setAntiAlias(true);
153 paint.setColor(SK_ColorRED);
154 paint.setStyle(SkPaint::kStroke_Style);
155 paint.setStrokeWidth(0.f);
156 for (int x = 1; x < kM; ++x) {
157 SkPoint pts[] = {{x * kTileW, 0}, {x * kTileW, kN * kTileH}};
158 matrices[m].mapPoints(pts, 2);
159 SkVector v = pts[1] - pts[0];
160 v.setLength(v.length() + kLineOutset);
161 canvas->drawLine(pts[1] - v, pts[0] + v, paint);
162 }
163 for (int y = 1; y < kN; ++y) {
164 SkPoint pts[] = {{0, y * kTileH}, {kTileW * kM, y * kTileH}};
165 matrices[m].mapPoints(pts, 2);
166 SkVector v = pts[1] - pts[0];
167 v.setLength(v.length() + kLineOutset);
168 canvas->drawLine(pts[1] - v, pts[0] + v, paint);
169 }
170 canvas->save();
171 canvas->concat(matrices[m]);
Michael Ludwigcf677782019-03-26 16:41:49 -0400172 canvas->experimental_DrawEdgeAAImageSet(fSet, kM * kN, nullptr, nullptr, &setPaint,
Michael Ludwig926fb892019-03-22 16:37:53 -0400173 SkCanvas::kFast_SrcRectConstraint);
Brian Salomond7065e72018-10-12 11:42:02 -0400174 canvas->restore();
175 }
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500176 // A more exotic case with an unusual blend mode, mixed aa flags set, and alpha,
Brian Salomondb151e02019-09-17 12:11:16 -0400177 // subsets the image. And another with all the above plus a color filter.
Brian Salomond7065e72018-10-12 11:42:02 -0400178 SkCanvas::ImageSetEntry entry;
179 entry.fSrcRect = SkRect::MakeWH(kTileW, kTileH).makeInset(kTileW / 4.f, kTileH / 4.f);
Brian Salomondb151e02019-09-17 12:11:16 -0400180 entry.fDstRect = SkRect::MakeWH(1.5 * kTileW, 1.5 * kTileH).makeOffset(d / 4, 2 * d);
Brian Salomon0087c832018-10-15 14:48:20 -0400181 entry.fImage = fSet[0].fImage;
Brian Salomon1da5cad2018-11-21 09:21:18 -0500182 entry.fAlpha = 0.7f;
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500183 entry.fAAFlags = SkCanvas::kLeft_QuadAAFlag | SkCanvas::kTop_QuadAAFlag;
Brian Salomond7065e72018-10-12 11:42:02 -0400184 canvas->save();
185 canvas->rotate(3.f);
Michael Ludwig926fb892019-03-22 16:37:53 -0400186
Michael Ludwigcf677782019-03-26 16:41:49 -0400187 setPaint.setBlendMode(SkBlendMode::kExclusion);
188 canvas->experimental_DrawEdgeAAImageSet(&entry, 1, nullptr, nullptr, &setPaint,
Michael Ludwig926fb892019-03-22 16:37:53 -0400189 SkCanvas::kFast_SrcRectConstraint);
Brian Salomondb151e02019-09-17 12:11:16 -0400190 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, &cfPaint,
194 SkCanvas::kFast_SrcRectConstraint);
Brian Salomond7065e72018-10-12 11:42:02 -0400195 canvas->restore();
196 canvas->translate(2 * d, 0);
197 }
198 }
199 static constexpr int kM = 4;
Brian Salomon5f4c90e2018-10-13 15:13:38 -0400200 static constexpr int kN = 3;
201 static constexpr SkScalar kTileW = 30;
202 static constexpr SkScalar kTileH = 60;
Brian Salomon0087c832018-10-15 14:48:20 -0400203 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.
208class DrawImageSetRectToRectGM : public GM {
209private:
Hal Canaryfa3305a2019-07-18 12:36:54 -0400210 SkString onShortName() override { return SkString("draw_image_set_rect_to_rect"); }
211 SkISize onISize() override { return {1250, 850}; }
Brian Salomon0087c832018-10-15 14:48:20 -0400212 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
218 void onDraw(SkCanvas* canvas) override {
Mike Kleinea3f0142019-03-20 11:12:10 -0500219 ToolUtils::draw_checkerboard(canvas, SK_ColorBLACK, SK_ColorWHITE, 50);
Brian Salomon0087c832018-10-15 14:48:20 -0400220 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
Michael Ludwig926fb892019-03-22 16:37:53 -0400238 SkPaint paint;
239 paint.setFilterQuality(kLow_SkFilterQuality);
240 paint.setBlendMode(SkBlendMode::kSrcOver);
241
Brian Salomon0087c832018-10-15 14:48:20 -0400242 static constexpr SkScalar kTranslate = SkTMax(kW, kH) * 2.f + 10.f;
243 canvas->translate(5.f, 5.f);
244 canvas->save();
245 for (SkScalar frac : {0.f, 0.5f}) {
246 canvas->save();
247 canvas->translate(frac, frac);
248 for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) {
249 canvas->save();
250 canvas->concat(matrices[m]);
Michael Ludwig926fb892019-03-22 16:37:53 -0400251 canvas->experimental_DrawEdgeAAImageSet(fSet, kM * kN, nullptr, nullptr, &paint,
252 SkCanvas::kFast_SrcRectConstraint);
Brian Salomon0087c832018-10-15 14:48:20 -0400253 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;
Brian Salomon1da5cad2018-11-21 09:21:18 -0500269 scaledSet[i].fAlpha = 0 == (i % 3) ? 0.4f : 1.f;
Brian Salomon0087c832018-10-15 14:48:20 -0400270 }
271 for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) {
272 canvas->save();
273 canvas->concat(matrices[m]);
Michael Ludwig926fb892019-03-22 16:37:53 -0400274 canvas->experimental_DrawEdgeAAImageSet(scaledSet, kM * kN, nullptr, nullptr,
275 &paint, SkCanvas::kFast_SrcRectConstraint);
Brian Salomon0087c832018-10-15 14:48:20 -0400276 canvas->restore();
277 canvas->translate(kTranslate, 0);
278 }
279 canvas->restore();
280 canvas->translate(0, kTranslate);
281 canvas->save();
282 }
283 }
284 static constexpr int kM = 2;
285 static constexpr int kN = 2;
286 static constexpr int kTileW = 40;
287 static constexpr int kTileH = 50;
288 SkCanvas::ImageSetEntry fSet[kM * kN];
Brian Salomond7065e72018-10-12 11:42:02 -0400289};
290
291DEF_GM(return new DrawImageSetGM();)
Brian Salomon0087c832018-10-15 14:48:20 -0400292DEF_GM(return new DrawImageSetRectToRectGM();)
Brian Salomond7065e72018-10-12 11:42:02 -0400293
294} // namespace skiagm