Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
Brian Salomon | db151e0 | 2019-09-17 12:11:16 -0400 | [diff] [blame] | 12 | #include "include/core/SkColorFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include "include/core/SkTileMode.h" |
| 27 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "include/effects/SkGradientShader.h" |
| 29 | #include "tools/ToolUtils.h" |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 30 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include <algorithm> |
| 32 | #include <initializer_list> |
| 33 | #include <utility> |
| 34 | |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 35 | // Makes a set of m x n tiled images to be drawn with SkCanvas::experimental_drawImageSetV1(). |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 36 | static 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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 49 | auto grad = SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkTileMode::kClamp); |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 50 | 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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 62 | grad = SkGradientShader::MakeLinear(pts2, colors + 2, nullptr, 2, SkTileMode::kClamp); |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 63 | 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 Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 98 | set[y * m + x].fAlpha = 1.f; |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 99 | SkASSERT(set[y * m + x].fImage); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 104 | namespace skiagm { |
| 105 | |
| 106 | class DrawImageSetGM : public GM { |
| 107 | private: |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 108 | SkString onShortName() override { return SkString("draw_image_set"); } |
| 109 | SkISize onISize() override { return {1000, 725}; } |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 110 | void onOnceBeforeDraw() override { |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 111 | static constexpr SkColor kColors[] = {SK_ColorCYAN, SK_ColorBLACK, |
| 112 | SK_ColorMAGENTA, SK_ColorBLACK}; |
| 113 | make_image_tiles(kTileW, kTileH, kM, kN, kColors, fSet); |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 114 | } |
| 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 Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 143 | for (auto fm : {kNone_SkFilterQuality, kLow_SkFilterQuality}) { |
Michael Ludwig | cf67778 | 2019-03-26 16:41:49 -0400 | [diff] [blame] | 144 | SkPaint setPaint; |
| 145 | setPaint.setFilterQuality(fm); |
| 146 | setPaint.setBlendMode(SkBlendMode::kSrcOver); |
Michael Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 147 | |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 148 | 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 Ludwig | cf67778 | 2019-03-26 16:41:49 -0400 | [diff] [blame] | 172 | canvas->experimental_DrawEdgeAAImageSet(fSet, kM * kN, nullptr, nullptr, &setPaint, |
Michael Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 173 | SkCanvas::kFast_SrcRectConstraint); |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 174 | canvas->restore(); |
| 175 | } |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 176 | // A more exotic case with an unusual blend mode, mixed aa flags set, and alpha, |
Brian Salomon | db151e0 | 2019-09-17 12:11:16 -0400 | [diff] [blame] | 177 | // subsets the image. And another with all the above plus a color filter. |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 178 | SkCanvas::ImageSetEntry entry; |
| 179 | entry.fSrcRect = SkRect::MakeWH(kTileW, kTileH).makeInset(kTileW / 4.f, kTileH / 4.f); |
Brian Salomon | db151e0 | 2019-09-17 12:11:16 -0400 | [diff] [blame] | 180 | entry.fDstRect = SkRect::MakeWH(1.5 * kTileW, 1.5 * kTileH).makeOffset(d / 4, 2 * d); |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 181 | entry.fImage = fSet[0].fImage; |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 182 | entry.fAlpha = 0.7f; |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 183 | entry.fAAFlags = SkCanvas::kLeft_QuadAAFlag | SkCanvas::kTop_QuadAAFlag; |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 184 | canvas->save(); |
| 185 | canvas->rotate(3.f); |
Michael Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 186 | |
Michael Ludwig | cf67778 | 2019-03-26 16:41:49 -0400 | [diff] [blame] | 187 | setPaint.setBlendMode(SkBlendMode::kExclusion); |
| 188 | canvas->experimental_DrawEdgeAAImageSet(&entry, 1, nullptr, nullptr, &setPaint, |
Michael Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 189 | SkCanvas::kFast_SrcRectConstraint); |
Brian Salomon | db151e0 | 2019-09-17 12:11:16 -0400 | [diff] [blame] | 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, &cfPaint, |
| 194 | SkCanvas::kFast_SrcRectConstraint); |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 195 | canvas->restore(); |
| 196 | canvas->translate(2 * d, 0); |
| 197 | } |
| 198 | } |
| 199 | static constexpr int kM = 4; |
Brian Salomon | 5f4c90e | 2018-10-13 15:13:38 -0400 | [diff] [blame] | 200 | static constexpr int kN = 3; |
| 201 | static constexpr SkScalar kTileW = 30; |
| 202 | static constexpr SkScalar kTileH = 60; |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 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: |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 210 | SkString onShortName() override { return SkString("draw_image_set_rect_to_rect"); } |
| 211 | SkISize onISize() override { return {1250, 850}; } |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 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 | |
| 218 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 219 | ToolUtils::draw_checkerboard(canvas, SK_ColorBLACK, SK_ColorWHITE, 50); |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 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 | |
Michael Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 238 | SkPaint paint; |
| 239 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 240 | paint.setBlendMode(SkBlendMode::kSrcOver); |
| 241 | |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 242 | 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 Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 251 | canvas->experimental_DrawEdgeAAImageSet(fSet, kM * kN, nullptr, nullptr, &paint, |
| 252 | SkCanvas::kFast_SrcRectConstraint); |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 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; |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 269 | scaledSet[i].fAlpha = 0 == (i % 3) ? 0.4f : 1.f; |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 270 | } |
| 271 | for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) { |
| 272 | canvas->save(); |
| 273 | canvas->concat(matrices[m]); |
Michael Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 274 | canvas->experimental_DrawEdgeAAImageSet(scaledSet, kM * kN, nullptr, nullptr, |
| 275 | &paint, SkCanvas::kFast_SrcRectConstraint); |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 276 | 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 Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | DEF_GM(return new DrawImageSetGM();) |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 292 | DEF_GM(return new DrawImageSetRectToRectGM();) |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 293 | |
| 294 | } // namespace skiagm |