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 | |
| 8 | #include "gm.h" |
| 9 | |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 10 | #include <algorithm> |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
| 12 | #include "SkSurface.h" |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame^] | 13 | #include "ToolUtils.h" |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 14 | |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 15 | // 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] | 16 | static void make_image_tiles(int tileW, int tileH, int m, int n, const SkColor colors[4], |
| 17 | SkCanvas::ImageSetEntry set[]) { |
| 18 | const int w = tileW * m; |
| 19 | const int h = tileH * n; |
| 20 | auto surf = SkSurface::MakeRaster( |
| 21 | SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 22 | surf->getCanvas()->clear(SK_ColorLTGRAY); |
| 23 | |
| 24 | static constexpr SkScalar kStripeW = 10; |
| 25 | static constexpr SkScalar kStripeSpacing = 30; |
| 26 | SkPaint paint; |
| 27 | |
| 28 | SkPoint pts1[] = {{0.f, 0.f}, {(SkScalar)w, (SkScalar)h}}; |
| 29 | auto grad = SkGradientShader::MakeLinear(pts1, colors, nullptr, 2, SkShader::kClamp_TileMode); |
| 30 | paint.setShader(std::move(grad)); |
| 31 | paint.setAntiAlias(true); |
| 32 | paint.setStyle(SkPaint::kStroke_Style); |
| 33 | paint.setStrokeWidth(kStripeW); |
| 34 | SkPoint stripePts[] = {{-w - kStripeW, -kStripeW}, {kStripeW, h + kStripeW}}; |
| 35 | while (stripePts[0].fX <= w) { |
| 36 | surf->getCanvas()->drawPoints(SkCanvas::kLines_PointMode, 2, stripePts, paint); |
| 37 | stripePts[0].fX += kStripeSpacing; |
| 38 | stripePts[1].fX += kStripeSpacing; |
| 39 | } |
| 40 | |
| 41 | SkPoint pts2[] = {{0.f, (SkScalar)h}, {(SkScalar)w, 0.f}}; |
| 42 | grad = SkGradientShader::MakeLinear(pts2, colors + 2, nullptr, 2, SkShader::kClamp_TileMode); |
| 43 | paint.setShader(std::move(grad)); |
| 44 | paint.setBlendMode(SkBlendMode::kMultiply); |
| 45 | stripePts[0] = {-w - kStripeW, h + kStripeW}; |
| 46 | stripePts[1] = {kStripeW, -kStripeW}; |
| 47 | while (stripePts[0].fX <= w) { |
| 48 | surf->getCanvas()->drawPoints(SkCanvas::kLines_PointMode, 2, stripePts, paint); |
| 49 | stripePts[0].fX += kStripeSpacing; |
| 50 | stripePts[1].fX += kStripeSpacing; |
| 51 | } |
| 52 | auto fullImage = surf->makeImageSnapshot(); |
| 53 | for (int y = 0; y < n; ++y) { |
| 54 | for (int x = 0; x < m; ++x) { |
| 55 | // Images will have 1 pixel of overlap at interior seams for filtering continuity. |
| 56 | SkIRect subset = SkIRect::MakeXYWH(x * tileW - 1, y * tileH - 1, tileW + 2, tileH + 2); |
| 57 | set[y * m + x].fAAFlags = SkCanvas::kNone_QuadAAFlags; |
| 58 | if (x == 0) { |
| 59 | subset.fLeft = 0; |
| 60 | set[y * m + x].fAAFlags |= SkCanvas::kLeft_QuadAAFlag; |
| 61 | } |
| 62 | if (x == m - 1) { |
| 63 | subset.fRight = w; |
| 64 | set[y * m + x].fAAFlags |= SkCanvas::kRight_QuadAAFlag; |
| 65 | } |
| 66 | if (y == 0) { |
| 67 | subset.fTop = 0; |
| 68 | set[y * m + x].fAAFlags |= SkCanvas::kTop_QuadAAFlag; |
| 69 | } |
| 70 | if (y == n - 1) { |
| 71 | subset.fBottom = h; |
| 72 | set[y * m + x].fAAFlags |= SkCanvas::kBottom_QuadAAFlag; |
| 73 | } |
| 74 | set[y * m + x].fImage = fullImage->makeSubset(subset); |
| 75 | set[y * m + x].fSrcRect = |
| 76 | SkRect::MakeXYWH(x == 0 ? 0 : 1, y == 0 ? 0 : 1, tileW, tileH); |
| 77 | 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] | 78 | set[y * m + x].fAlpha = 1.f; |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 79 | SkASSERT(set[y * m + x].fImage); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 84 | namespace skiagm { |
| 85 | |
| 86 | class DrawImageSetGM : public GM { |
| 87 | private: |
| 88 | SkString onShortName() final { return SkString("draw_image_set"); } |
| 89 | SkISize onISize() override { return SkISize::Make(1000, 725); } |
| 90 | void onOnceBeforeDraw() override { |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 91 | static constexpr SkColor kColors[] = {SK_ColorCYAN, SK_ColorBLACK, |
| 92 | SK_ColorMAGENTA, SK_ColorBLACK}; |
| 93 | make_image_tiles(kTileW, kTileH, kM, kN, kColors, fSet); |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void onDraw(SkCanvas* canvas) override { |
| 97 | SkScalar d = SkVector{kM * kTileW, kN * kTileH}.length(); |
| 98 | SkMatrix matrices[4]; |
| 99 | // rotation |
| 100 | matrices[0].setRotate(30); |
| 101 | matrices[0].postTranslate(d / 3, 0); |
| 102 | // perespective |
| 103 | SkPoint src[4]; |
| 104 | SkRect::MakeWH(kM * kTileW, kN * kTileH).toQuad(src); |
| 105 | SkPoint dst[4] = {{0, 0}, |
| 106 | {kM * kTileW + 10.f, -5.f}, |
| 107 | {kM * kTileW - 28.f, kN * kTileH + 40.f}, |
| 108 | {45.f, kN * kTileH - 25.f}}; |
| 109 | SkAssertResult(matrices[1].setPolyToPoly(src, dst, 4)); |
| 110 | matrices[1].postTranslate(d, 50.f); |
| 111 | // skew |
| 112 | matrices[2].setRotate(-60.f); |
| 113 | matrices[2].postSkew(0.5f, -1.15f); |
| 114 | matrices[2].postScale(0.6f, 1.05f); |
| 115 | matrices[2].postTranslate(d, 2.6f * d); |
| 116 | // perspective + mirror in x. |
| 117 | dst[1] = {-.25 * kM * kTileW, 0}; |
| 118 | dst[0] = {5.f / 4.f * kM * kTileW, 0}; |
| 119 | dst[3] = {2.f / 3.f * kM * kTileW, 1 / 2.f * kN * kTileH}; |
| 120 | dst[2] = {1.f / 3.f * kM * kTileW, 1 / 2.f * kN * kTileH - 0.1f * kTileH}; |
| 121 | SkAssertResult(matrices[3].setPolyToPoly(src, dst, 4)); |
| 122 | matrices[3].postTranslate(100.f, d); |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 123 | for (auto fm : {kNone_SkFilterQuality, kLow_SkFilterQuality}) { |
| 124 | for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) { |
| 125 | // Draw grid of red lines at interior tile boundaries. |
| 126 | static constexpr SkScalar kLineOutset = 10.f; |
| 127 | SkPaint paint; |
| 128 | paint.setAntiAlias(true); |
| 129 | paint.setColor(SK_ColorRED); |
| 130 | paint.setStyle(SkPaint::kStroke_Style); |
| 131 | paint.setStrokeWidth(0.f); |
| 132 | for (int x = 1; x < kM; ++x) { |
| 133 | SkPoint pts[] = {{x * kTileW, 0}, {x * kTileW, kN * kTileH}}; |
| 134 | matrices[m].mapPoints(pts, 2); |
| 135 | SkVector v = pts[1] - pts[0]; |
| 136 | v.setLength(v.length() + kLineOutset); |
| 137 | canvas->drawLine(pts[1] - v, pts[0] + v, paint); |
| 138 | } |
| 139 | for (int y = 1; y < kN; ++y) { |
| 140 | SkPoint pts[] = {{0, y * kTileH}, {kTileW * kM, y * kTileH}}; |
| 141 | matrices[m].mapPoints(pts, 2); |
| 142 | SkVector v = pts[1] - pts[0]; |
| 143 | v.setLength(v.length() + kLineOutset); |
| 144 | canvas->drawLine(pts[1] - v, pts[0] + v, paint); |
| 145 | } |
| 146 | canvas->save(); |
| 147 | canvas->concat(matrices[m]); |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 148 | canvas->experimental_DrawImageSetV1(fSet, kM * kN, fm, SkBlendMode::kSrcOver); |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 149 | canvas->restore(); |
| 150 | } |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 151 | // A more exotic case with an unusual blend mode, mixed aa flags set, and alpha, |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 152 | // subsets the image |
| 153 | SkCanvas::ImageSetEntry entry; |
| 154 | entry.fSrcRect = SkRect::MakeWH(kTileW, kTileH).makeInset(kTileW / 4.f, kTileH / 4.f); |
| 155 | entry.fDstRect = SkRect::MakeWH(2 * kTileW, 2 * kTileH).makeOffset(d / 4, 2 * d); |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 156 | entry.fImage = fSet[0].fImage; |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 157 | entry.fAlpha = 0.7f; |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 158 | entry.fAAFlags = SkCanvas::kLeft_QuadAAFlag | SkCanvas::kTop_QuadAAFlag; |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 159 | canvas->save(); |
| 160 | canvas->rotate(3.f); |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 161 | canvas->experimental_DrawImageSetV1(&entry, 1, fm, SkBlendMode::kExclusion); |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 162 | canvas->restore(); |
| 163 | canvas->translate(2 * d, 0); |
| 164 | } |
| 165 | } |
| 166 | static constexpr int kM = 4; |
Brian Salomon | 5f4c90e | 2018-10-13 15:13:38 -0400 | [diff] [blame] | 167 | static constexpr int kN = 3; |
| 168 | static constexpr SkScalar kTileW = 30; |
| 169 | static constexpr SkScalar kTileH = 60; |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 170 | SkCanvas::ImageSetEntry fSet[kM * kN]; |
| 171 | }; |
| 172 | |
| 173 | // This GM exercises rect-stays-rect type matrices to test that filtering and antialiasing are not |
| 174 | // incorrectly disabled. |
| 175 | class DrawImageSetRectToRectGM : public GM { |
| 176 | private: |
| 177 | SkString onShortName() final { return SkString("draw_image_set_rect_to_rect"); } |
| 178 | SkISize onISize() override { return SkISize::Make(1250, 850); } |
| 179 | void onOnceBeforeDraw() override { |
| 180 | static constexpr SkColor kColors[] = {SK_ColorBLUE, SK_ColorWHITE, |
| 181 | SK_ColorRED, SK_ColorWHITE}; |
| 182 | make_image_tiles(kTileW, kTileH, kM, kN, kColors, fSet); |
| 183 | } |
| 184 | |
| 185 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame^] | 186 | ToolUtils::draw_checkerboard(canvas, SK_ColorBLACK, SK_ColorWHITE, 50); |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 187 | static constexpr SkScalar kW = kM * kTileW; |
| 188 | static constexpr SkScalar kH = kN * kTileH; |
| 189 | SkMatrix matrices[5]; |
| 190 | // Identity |
| 191 | matrices[0].reset(); |
| 192 | // 90 degree rotation |
| 193 | matrices[1].setRotate(90, kW / 2.f, kH / 2.f); |
| 194 | // Scaling |
| 195 | matrices[2].setScale(2.f, 0.5f); |
| 196 | // Mirror in x and y |
| 197 | matrices[3].setScale(-1.f, -1.f); |
| 198 | matrices[3].postTranslate(kW, kH); |
| 199 | // Mirror in y, rotate, and scale. |
| 200 | matrices[4].setScale(1.f, -1.f); |
| 201 | matrices[4].postTranslate(0, kH); |
| 202 | matrices[4].postRotate(90, kW / 2.f, kH / 2.f); |
| 203 | matrices[4].postScale(2.f, 0.5f); |
| 204 | |
| 205 | static constexpr SkScalar kTranslate = SkTMax(kW, kH) * 2.f + 10.f; |
| 206 | canvas->translate(5.f, 5.f); |
| 207 | canvas->save(); |
| 208 | for (SkScalar frac : {0.f, 0.5f}) { |
| 209 | canvas->save(); |
| 210 | canvas->translate(frac, frac); |
| 211 | for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) { |
| 212 | canvas->save(); |
| 213 | canvas->concat(matrices[m]); |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 214 | canvas->experimental_DrawImageSetV1(fSet, kM * kN, kLow_SkFilterQuality, |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 215 | SkBlendMode::kSrcOver); |
| 216 | canvas->restore(); |
| 217 | canvas->translate(kTranslate, 0); |
| 218 | } |
| 219 | canvas->restore(); |
| 220 | canvas->restore(); |
| 221 | canvas->translate(0, kTranslate); |
| 222 | canvas->save(); |
| 223 | } |
| 224 | for (SkVector scale : {SkVector{2.f, 0.5f}, SkVector{0.5, 2.f}}) { |
| 225 | SkCanvas::ImageSetEntry scaledSet[kM * kN]; |
| 226 | std::copy_n(fSet, kM * kN, scaledSet); |
| 227 | for (int i = 0; i < kM * kN; ++i) { |
| 228 | scaledSet[i].fDstRect.fLeft *= scale.fX; |
| 229 | scaledSet[i].fDstRect.fTop *= scale.fY; |
| 230 | scaledSet[i].fDstRect.fRight *= scale.fX; |
| 231 | scaledSet[i].fDstRect.fBottom *= scale.fY; |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 232 | scaledSet[i].fAlpha = 0 == (i % 3) ? 0.4f : 1.f; |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 233 | } |
| 234 | for (size_t m = 0; m < SK_ARRAY_COUNT(matrices); ++m) { |
| 235 | canvas->save(); |
| 236 | canvas->concat(matrices[m]); |
Brian Salomon | 1da5cad | 2018-11-21 09:21:18 -0500 | [diff] [blame] | 237 | canvas->experimental_DrawImageSetV1(scaledSet, kM * kN, kLow_SkFilterQuality, |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 238 | SkBlendMode::kSrcOver); |
| 239 | canvas->restore(); |
| 240 | canvas->translate(kTranslate, 0); |
| 241 | } |
| 242 | canvas->restore(); |
| 243 | canvas->translate(0, kTranslate); |
| 244 | canvas->save(); |
| 245 | } |
| 246 | } |
| 247 | static constexpr int kM = 2; |
| 248 | static constexpr int kN = 2; |
| 249 | static constexpr int kTileW = 40; |
| 250 | static constexpr int kTileH = 50; |
| 251 | SkCanvas::ImageSetEntry fSet[kM * kN]; |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | DEF_GM(return new DrawImageSetGM();) |
Brian Salomon | 0087c83 | 2018-10-15 14:48:20 -0400 | [diff] [blame] | 255 | DEF_GM(return new DrawImageSetRectToRectGM();) |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 256 | |
| 257 | } // namespace skiagm |