Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [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" |
| 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkMatrix.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkShader.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTileMode.h" |
| 22 | #include "include/core/SkTypeface.h" |
| 23 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/effects/SkGradientShader.h" |
| 25 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/GrClip.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 28 | #include "src/gpu/GrPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/gpu/SkGr.h" |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 31 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 32 | #include <utility> |
| 33 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 34 | static constexpr SkScalar kTileWidth = 40; |
| 35 | static constexpr SkScalar kTileHeight = 30; |
| 36 | |
| 37 | static constexpr int kRowCount = 4; |
| 38 | static constexpr int kColCount = 3; |
| 39 | |
| 40 | static void draw_text(SkCanvas* canvas, const char* text) { |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 41 | canvas->drawString(text, 0, 0, SkFont(nullptr, 12), SkPaint()); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 44 | static void draw_gradient_tiles(SkCanvas* canvas, bool alignGradients) { |
| 45 | // Always draw the same gradient |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 46 | static constexpr SkPoint pts[] = { {0.f, 0.f}, {0.25f * kTileWidth, 0.25f * kTileHeight} }; |
| 47 | static constexpr SkColor colors[] = { SK_ColorBLUE, SK_ColorWHITE }; |
| 48 | |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 49 | GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 50 | |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 51 | GrContext* context = canvas->getGrContext(); |
| 52 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 53 | auto gradient = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 54 | SkPaint paint; |
| 55 | paint.setShader(gradient); |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 56 | |
| 57 | for (int i = 0; i < kRowCount; ++i) { |
| 58 | for (int j = 0; j < kColCount; ++j) { |
| 59 | SkRect tile = SkRect::MakeWH(kTileWidth, kTileHeight); |
| 60 | if (alignGradients) { |
| 61 | tile.offset(j * kTileWidth, i * kTileHeight); |
| 62 | } else { |
| 63 | canvas->save(); |
| 64 | canvas->translate(j * kTileWidth, i * kTileHeight); |
| 65 | } |
| 66 | |
| 67 | unsigned aa = SkCanvas::kNone_QuadAAFlags; |
| 68 | if (i == 0) { |
| 69 | aa |= SkCanvas::kTop_QuadAAFlag; |
| 70 | } |
| 71 | if (i == kRowCount - 1) { |
| 72 | aa |= SkCanvas::kBottom_QuadAAFlag; |
| 73 | } |
| 74 | if (j == 0) { |
| 75 | aa |= SkCanvas::kLeft_QuadAAFlag; |
| 76 | } |
| 77 | if (j == kColCount - 1) { |
| 78 | aa |= SkCanvas::kRight_QuadAAFlag; |
| 79 | } |
| 80 | |
| 81 | if (rtc) { |
| 82 | // Use non-public API to leverage general GrPaint capabilities |
| 83 | SkMatrix view = canvas->getTotalMatrix(); |
| 84 | GrPaint grPaint; |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 85 | SkPaintToGrPaint(context, rtc->colorInfo(), paint, view, &grPaint); |
Michael Ludwig | 136f45a | 2019-02-19 11:44:41 -0500 | [diff] [blame] | 86 | rtc->fillRectWithEdgeAA(GrNoClip(), std::move(grPaint), GrAA::kYes, |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 87 | static_cast<GrQuadAAFlags>(aa), view, tile); |
| 88 | } else { |
| 89 | // Fallback to solid color on raster backend since the public API only has color |
| 90 | SkColor color = alignGradients ? SK_ColorBLUE |
| 91 | : (i * kColCount + j) % 2 == 0 ? SK_ColorBLUE |
| 92 | : SK_ColorWHITE; |
Michael Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 93 | canvas->experimental_DrawEdgeAAQuad( |
| 94 | tile, nullptr, static_cast<SkCanvas::QuadAAFlags>(aa), color, |
| 95 | SkBlendMode::kSrcOver); |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (!alignGradients) { |
| 99 | // Pop off the matrix translation when drawing unaligned |
| 100 | canvas->restore(); |
| 101 | } |
| 102 | } |
| 103 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 106 | static void draw_color_tiles(SkCanvas* canvas, bool multicolor) { |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 107 | for (int i = 0; i < kRowCount; ++i) { |
| 108 | for (int j = 0; j < kColCount; ++j) { |
| 109 | SkRect tile = SkRect::MakeXYWH(j * kTileWidth, i * kTileHeight, kTileWidth, kTileHeight); |
| 110 | |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 111 | SkColor4f color; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 112 | if (multicolor) { |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 113 | color = {(i + 1.f) / kRowCount, (j + 1.f) / kColCount, .4f, 1.f}; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 114 | } else { |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 115 | color = {.2f, .8f, .3f, 1.f}; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 118 | unsigned aa = SkCanvas::kNone_QuadAAFlags; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 119 | if (i == 0) { |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 120 | aa |= SkCanvas::kTop_QuadAAFlag; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 121 | } |
| 122 | if (i == kRowCount - 1) { |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 123 | aa |= SkCanvas::kBottom_QuadAAFlag; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 124 | } |
| 125 | if (j == 0) { |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 126 | aa |= SkCanvas::kLeft_QuadAAFlag; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 127 | } |
| 128 | if (j == kColCount - 1) { |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 129 | aa |= SkCanvas::kRight_QuadAAFlag; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 130 | } |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 131 | |
Michael Ludwig | 926fb89 | 2019-03-22 16:37:53 -0400 | [diff] [blame] | 132 | canvas->experimental_DrawEdgeAAQuad( |
| 133 | tile, nullptr, static_cast<SkCanvas::QuadAAFlags>(aa), color.toSkColor(), |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 134 | SkBlendMode::kSrcOver); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 135 | } |
| 136 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void draw_tile_boundaries(SkCanvas* canvas, const SkMatrix& local) { |
| 140 | // Draw grid of red lines at interior tile boundaries. |
| 141 | static constexpr SkScalar kLineOutset = 10.f; |
| 142 | SkPaint paint; |
| 143 | paint.setAntiAlias(true); |
| 144 | paint.setColor(SK_ColorRED); |
| 145 | paint.setStyle(SkPaint::kStroke_Style); |
| 146 | paint.setStrokeWidth(0.f); |
| 147 | for (int x = 1; x < kColCount; ++x) { |
| 148 | SkPoint pts[] = {{x * kTileWidth, 0}, {x * kTileWidth, kRowCount * kTileHeight}}; |
| 149 | local.mapPoints(pts, 2); |
| 150 | SkVector v = pts[1] - pts[0]; |
| 151 | v.setLength(v.length() + kLineOutset); |
| 152 | canvas->drawLine(pts[1] - v, pts[0] + v, paint); |
| 153 | } |
| 154 | for (int y = 1; y < kRowCount; ++y) { |
| 155 | SkPoint pts[] = {{0, y * kTileHeight}, {kTileWidth * kColCount, y * kTileHeight}}; |
| 156 | local.mapPoints(pts, 2); |
| 157 | SkVector v = pts[1] - pts[0]; |
| 158 | v.setLength(v.length() + kLineOutset); |
| 159 | canvas->drawLine(pts[1] - v, pts[0] + v, paint); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Tile renderers (column variation) |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 164 | typedef void (*TileRenderer)(SkCanvas*); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 165 | static TileRenderer kTileSets[] = { |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 166 | [](SkCanvas* canvas) { draw_gradient_tiles(canvas, /* aligned */ false); }, |
| 167 | [](SkCanvas* canvas) { draw_gradient_tiles(canvas, /* aligned */ true); }, |
| 168 | [](SkCanvas* canvas) { draw_color_tiles(canvas, /* multicolor */ false); }, |
| 169 | [](SkCanvas* canvas) { draw_color_tiles(canvas, /* multicolor */true); }, |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 170 | }; |
| 171 | static const char* kTileSetNames[] = { "Local", "Aligned", "Green", "Multicolor" }; |
| 172 | static_assert(SK_ARRAY_COUNT(kTileSets) == SK_ARRAY_COUNT(kTileSetNames), "Count mismatch"); |
| 173 | |
| 174 | namespace skiagm { |
| 175 | |
| 176 | class DrawQuadSetGM : public GM { |
| 177 | private: |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 178 | SkString onShortName() override { return SkString("draw_quad_set"); } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 179 | SkISize onISize() override { return SkISize::Make(800, 800); } |
| 180 | |
| 181 | void onDraw(SkCanvas* canvas) override { |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 182 | SkMatrix rowMatrices[5]; |
| 183 | // Identity |
| 184 | rowMatrices[0].setIdentity(); |
| 185 | // Translate/scale |
| 186 | rowMatrices[1].setTranslate(5.5f, 20.25f); |
| 187 | rowMatrices[1].postScale(.9f, .7f); |
| 188 | // Rotation |
| 189 | rowMatrices[2].setRotate(20.0f); |
| 190 | rowMatrices[2].preTranslate(15.f, -20.f); |
| 191 | // Skew |
| 192 | rowMatrices[3].setSkew(.5f, .25f); |
| 193 | rowMatrices[3].preTranslate(-30.f, 0.f); |
| 194 | // Perspective |
| 195 | SkPoint src[4]; |
| 196 | SkRect::MakeWH(kColCount * kTileWidth, kRowCount * kTileHeight).toQuad(src); |
| 197 | SkPoint dst[4] = {{0, 0}, |
| 198 | {kColCount * kTileWidth + 10.f, 15.f}, |
| 199 | {kColCount * kTileWidth - 28.f, kRowCount * kTileHeight + 40.f}, |
| 200 | {25.f, kRowCount * kTileHeight - 15.f}}; |
| 201 | SkAssertResult(rowMatrices[4].setPolyToPoly(src, dst, 4)); |
| 202 | rowMatrices[4].preTranslate(0.f, +10.f); |
| 203 | static const char* matrixNames[] = { "Identity", "T+S", "Rotate", "Skew", "Perspective" }; |
| 204 | static_assert(SK_ARRAY_COUNT(matrixNames) == SK_ARRAY_COUNT(rowMatrices), "Count mismatch"); |
| 205 | |
| 206 | // Print a column header |
| 207 | canvas->save(); |
| 208 | canvas->translate(110.f, 20.f); |
| 209 | for (size_t j = 0; j < SK_ARRAY_COUNT(kTileSetNames); ++j) { |
| 210 | draw_text(canvas, kTileSetNames[j]); |
| 211 | canvas->translate(kColCount * kTileWidth + 30.f, 0.f); |
| 212 | } |
| 213 | canvas->restore(); |
| 214 | canvas->translate(0.f, 40.f); |
| 215 | |
| 216 | // Render all tile variations |
| 217 | for (size_t i = 0; i < SK_ARRAY_COUNT(rowMatrices); ++i) { |
| 218 | canvas->save(); |
| 219 | canvas->translate(10.f, 0.5f * kRowCount * kTileHeight); |
| 220 | draw_text(canvas, matrixNames[i]); |
| 221 | |
| 222 | canvas->translate(100.f, -0.5f * kRowCount * kTileHeight); |
| 223 | for (size_t j = 0; j < SK_ARRAY_COUNT(kTileSets); ++j) { |
| 224 | canvas->save(); |
| 225 | draw_tile_boundaries(canvas, rowMatrices[i]); |
| 226 | |
| 227 | canvas->concat(rowMatrices[i]); |
Michael Ludwig | 7545190 | 2019-01-23 11:14:29 -0500 | [diff] [blame] | 228 | kTileSets[j](canvas); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 229 | // Undo the local transformation |
| 230 | canvas->restore(); |
| 231 | // And advance to the next column |
| 232 | canvas->translate(kColCount * kTileWidth + 30.f, 0.f); |
| 233 | } |
| 234 | // Reset back to the left edge |
| 235 | canvas->restore(); |
| 236 | // And advance to the next row |
| 237 | canvas->translate(0.f, kRowCount * kTileHeight + 20.f); |
| 238 | } |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | DEF_GM(return new DrawQuadSetGM();) |
| 243 | |
| 244 | } // namespace skiagm |