blob: 6fdcdf5f2669d49789a1c22ab93d2e098a02cd3e [file] [log] [blame]
Michael Ludwig69858532018-11-28 15:34:34 -05001/*
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"
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 Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkGradientShader.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040025#include "include/gpu/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/private/GrTypesPriv.h"
Brian Osman449b1152020-04-15 16:43:00 -040027#include "src/core/SkMatrixProvider.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "src/gpu/GrPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/SkGr.h"
Robert Phillips3d311a92020-05-05 09:18:50 -040031#include "tools/ToolUtils.h"
Michael Ludwig69858532018-11-28 15:34:34 -050032
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include <utility>
34
Michael Ludwig69858532018-11-28 15:34:34 -050035static constexpr SkScalar kTileWidth = 40;
36static constexpr SkScalar kTileHeight = 30;
37
38static constexpr int kRowCount = 4;
39static constexpr int kColCount = 3;
40
41static void draw_text(SkCanvas* canvas, const char* text) {
Robert Phillips3d311a92020-05-05 09:18:50 -040042 SkFont font(ToolUtils::create_portable_typeface(), 12);
43 canvas->drawString(text, 0, 0, font, SkPaint());
Michael Ludwig69858532018-11-28 15:34:34 -050044}
45
Michael Ludwig75451902019-01-23 11:14:29 -050046static void draw_gradient_tiles(SkCanvas* canvas, bool alignGradients) {
47 // Always draw the same gradient
Michael Ludwig69858532018-11-28 15:34:34 -050048 static constexpr SkPoint pts[] = { {0.f, 0.f}, {0.25f * kTileWidth, 0.25f * kTileHeight} };
49 static constexpr SkColor colors[] = { SK_ColorBLUE, SK_ColorWHITE };
50
Michael Ludwig75451902019-01-23 11:14:29 -050051 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
52
Robert Phillips95c250c2020-06-29 15:36:12 -040053 auto context = canvas->recordingContext();
Robert Phillips6a6de562019-02-15 15:19:15 -050054
Mike Reedfae8fce2019-04-03 10:27:45 -040055 auto gradient = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror);
Michael Ludwig69858532018-11-28 15:34:34 -050056 SkPaint paint;
57 paint.setShader(gradient);
Michael Ludwig75451902019-01-23 11:14:29 -050058
59 for (int i = 0; i < kRowCount; ++i) {
60 for (int j = 0; j < kColCount; ++j) {
61 SkRect tile = SkRect::MakeWH(kTileWidth, kTileHeight);
62 if (alignGradients) {
63 tile.offset(j * kTileWidth, i * kTileHeight);
64 } else {
65 canvas->save();
66 canvas->translate(j * kTileWidth, i * kTileHeight);
67 }
68
69 unsigned aa = SkCanvas::kNone_QuadAAFlags;
70 if (i == 0) {
71 aa |= SkCanvas::kTop_QuadAAFlag;
72 }
73 if (i == kRowCount - 1) {
74 aa |= SkCanvas::kBottom_QuadAAFlag;
75 }
76 if (j == 0) {
77 aa |= SkCanvas::kLeft_QuadAAFlag;
78 }
79 if (j == kColCount - 1) {
80 aa |= SkCanvas::kRight_QuadAAFlag;
81 }
82
83 if (rtc) {
84 // Use non-public API to leverage general GrPaint capabilities
85 SkMatrix view = canvas->getTotalMatrix();
Brian Osman449b1152020-04-15 16:43:00 -040086 SkSimpleMatrixProvider matrixProvider(view);
Michael Ludwig75451902019-01-23 11:14:29 -050087 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -040088 SkPaintToGrPaint(context, rtc->colorInfo(), paint, matrixProvider, &grPaint);
Michael Ludwig7c12e282020-05-29 09:54:07 -040089 rtc->fillRectWithEdgeAA(nullptr, std::move(grPaint), GrAA::kYes,
Michael Ludwig75451902019-01-23 11:14:29 -050090 static_cast<GrQuadAAFlags>(aa), view, tile);
91 } else {
92 // Fallback to solid color on raster backend since the public API only has color
93 SkColor color = alignGradients ? SK_ColorBLUE
94 : (i * kColCount + j) % 2 == 0 ? SK_ColorBLUE
95 : SK_ColorWHITE;
Michael Ludwig926fb892019-03-22 16:37:53 -040096 canvas->experimental_DrawEdgeAAQuad(
97 tile, nullptr, static_cast<SkCanvas::QuadAAFlags>(aa), color,
98 SkBlendMode::kSrcOver);
Michael Ludwig75451902019-01-23 11:14:29 -050099 }
100
101 if (!alignGradients) {
102 // Pop off the matrix translation when drawing unaligned
103 canvas->restore();
104 }
105 }
106 }
Michael Ludwig69858532018-11-28 15:34:34 -0500107}
108
Michael Ludwig75451902019-01-23 11:14:29 -0500109static void draw_color_tiles(SkCanvas* canvas, bool multicolor) {
Michael Ludwig69858532018-11-28 15:34:34 -0500110 for (int i = 0; i < kRowCount; ++i) {
111 for (int j = 0; j < kColCount; ++j) {
112 SkRect tile = SkRect::MakeXYWH(j * kTileWidth, i * kTileHeight, kTileWidth, kTileHeight);
113
Michael Ludwig75451902019-01-23 11:14:29 -0500114 SkColor4f color;
Michael Ludwig69858532018-11-28 15:34:34 -0500115 if (multicolor) {
Michael Ludwig75451902019-01-23 11:14:29 -0500116 color = {(i + 1.f) / kRowCount, (j + 1.f) / kColCount, .4f, 1.f};
Michael Ludwig69858532018-11-28 15:34:34 -0500117 } else {
Michael Ludwig75451902019-01-23 11:14:29 -0500118 color = {.2f, .8f, .3f, 1.f};
Michael Ludwig69858532018-11-28 15:34:34 -0500119 }
120
Michael Ludwig75451902019-01-23 11:14:29 -0500121 unsigned aa = SkCanvas::kNone_QuadAAFlags;
Michael Ludwig69858532018-11-28 15:34:34 -0500122 if (i == 0) {
Michael Ludwig75451902019-01-23 11:14:29 -0500123 aa |= SkCanvas::kTop_QuadAAFlag;
Michael Ludwig69858532018-11-28 15:34:34 -0500124 }
125 if (i == kRowCount - 1) {
Michael Ludwig75451902019-01-23 11:14:29 -0500126 aa |= SkCanvas::kBottom_QuadAAFlag;
Michael Ludwig69858532018-11-28 15:34:34 -0500127 }
128 if (j == 0) {
Michael Ludwig75451902019-01-23 11:14:29 -0500129 aa |= SkCanvas::kLeft_QuadAAFlag;
Michael Ludwig69858532018-11-28 15:34:34 -0500130 }
131 if (j == kColCount - 1) {
Michael Ludwig75451902019-01-23 11:14:29 -0500132 aa |= SkCanvas::kRight_QuadAAFlag;
Michael Ludwig69858532018-11-28 15:34:34 -0500133 }
Michael Ludwig75451902019-01-23 11:14:29 -0500134
Michael Ludwig926fb892019-03-22 16:37:53 -0400135 canvas->experimental_DrawEdgeAAQuad(
136 tile, nullptr, static_cast<SkCanvas::QuadAAFlags>(aa), color.toSkColor(),
Michael Ludwig75451902019-01-23 11:14:29 -0500137 SkBlendMode::kSrcOver);
Michael Ludwig69858532018-11-28 15:34:34 -0500138 }
139 }
Michael Ludwig69858532018-11-28 15:34:34 -0500140}
141
142static void draw_tile_boundaries(SkCanvas* canvas, const SkMatrix& local) {
143 // Draw grid of red lines at interior tile boundaries.
144 static constexpr SkScalar kLineOutset = 10.f;
145 SkPaint paint;
146 paint.setAntiAlias(true);
147 paint.setColor(SK_ColorRED);
148 paint.setStyle(SkPaint::kStroke_Style);
149 paint.setStrokeWidth(0.f);
150 for (int x = 1; x < kColCount; ++x) {
151 SkPoint pts[] = {{x * kTileWidth, 0}, {x * kTileWidth, kRowCount * kTileHeight}};
152 local.mapPoints(pts, 2);
153 SkVector v = pts[1] - pts[0];
154 v.setLength(v.length() + kLineOutset);
155 canvas->drawLine(pts[1] - v, pts[0] + v, paint);
156 }
157 for (int y = 1; y < kRowCount; ++y) {
158 SkPoint pts[] = {{0, y * kTileHeight}, {kTileWidth * kColCount, y * kTileHeight}};
159 local.mapPoints(pts, 2);
160 SkVector v = pts[1] - pts[0];
161 v.setLength(v.length() + kLineOutset);
162 canvas->drawLine(pts[1] - v, pts[0] + v, paint);
163 }
164}
165
166// Tile renderers (column variation)
Michael Ludwig75451902019-01-23 11:14:29 -0500167typedef void (*TileRenderer)(SkCanvas*);
Michael Ludwig69858532018-11-28 15:34:34 -0500168static TileRenderer kTileSets[] = {
Michael Ludwig75451902019-01-23 11:14:29 -0500169 [](SkCanvas* canvas) { draw_gradient_tiles(canvas, /* aligned */ false); },
170 [](SkCanvas* canvas) { draw_gradient_tiles(canvas, /* aligned */ true); },
171 [](SkCanvas* canvas) { draw_color_tiles(canvas, /* multicolor */ false); },
172 [](SkCanvas* canvas) { draw_color_tiles(canvas, /* multicolor */true); },
Michael Ludwig69858532018-11-28 15:34:34 -0500173};
174static const char* kTileSetNames[] = { "Local", "Aligned", "Green", "Multicolor" };
175static_assert(SK_ARRAY_COUNT(kTileSets) == SK_ARRAY_COUNT(kTileSetNames), "Count mismatch");
176
177namespace skiagm {
178
179class DrawQuadSetGM : public GM {
180private:
Hal Canaryfa3305a2019-07-18 12:36:54 -0400181 SkString onShortName() override { return SkString("draw_quad_set"); }
Michael Ludwig69858532018-11-28 15:34:34 -0500182 SkISize onISize() override { return SkISize::Make(800, 800); }
183
184 void onDraw(SkCanvas* canvas) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500185 SkMatrix rowMatrices[5];
186 // Identity
187 rowMatrices[0].setIdentity();
188 // Translate/scale
189 rowMatrices[1].setTranslate(5.5f, 20.25f);
190 rowMatrices[1].postScale(.9f, .7f);
191 // Rotation
192 rowMatrices[2].setRotate(20.0f);
193 rowMatrices[2].preTranslate(15.f, -20.f);
194 // Skew
195 rowMatrices[3].setSkew(.5f, .25f);
196 rowMatrices[3].preTranslate(-30.f, 0.f);
197 // Perspective
198 SkPoint src[4];
199 SkRect::MakeWH(kColCount * kTileWidth, kRowCount * kTileHeight).toQuad(src);
200 SkPoint dst[4] = {{0, 0},
201 {kColCount * kTileWidth + 10.f, 15.f},
202 {kColCount * kTileWidth - 28.f, kRowCount * kTileHeight + 40.f},
203 {25.f, kRowCount * kTileHeight - 15.f}};
204 SkAssertResult(rowMatrices[4].setPolyToPoly(src, dst, 4));
205 rowMatrices[4].preTranslate(0.f, +10.f);
206 static const char* matrixNames[] = { "Identity", "T+S", "Rotate", "Skew", "Perspective" };
207 static_assert(SK_ARRAY_COUNT(matrixNames) == SK_ARRAY_COUNT(rowMatrices), "Count mismatch");
208
209 // Print a column header
210 canvas->save();
211 canvas->translate(110.f, 20.f);
212 for (size_t j = 0; j < SK_ARRAY_COUNT(kTileSetNames); ++j) {
213 draw_text(canvas, kTileSetNames[j]);
214 canvas->translate(kColCount * kTileWidth + 30.f, 0.f);
215 }
216 canvas->restore();
217 canvas->translate(0.f, 40.f);
218
219 // Render all tile variations
220 for (size_t i = 0; i < SK_ARRAY_COUNT(rowMatrices); ++i) {
221 canvas->save();
222 canvas->translate(10.f, 0.5f * kRowCount * kTileHeight);
223 draw_text(canvas, matrixNames[i]);
224
225 canvas->translate(100.f, -0.5f * kRowCount * kTileHeight);
226 for (size_t j = 0; j < SK_ARRAY_COUNT(kTileSets); ++j) {
227 canvas->save();
228 draw_tile_boundaries(canvas, rowMatrices[i]);
229
230 canvas->concat(rowMatrices[i]);
Michael Ludwig75451902019-01-23 11:14:29 -0500231 kTileSets[j](canvas);
Michael Ludwig69858532018-11-28 15:34:34 -0500232 // Undo the local transformation
233 canvas->restore();
234 // And advance to the next column
235 canvas->translate(kColCount * kTileWidth + 30.f, 0.f);
236 }
237 // Reset back to the left edge
238 canvas->restore();
239 // And advance to the next row
240 canvas->translate(0.f, kRowCount * kTileHeight + 20.f);
241 }
242 }
243};
244
245DEF_GM(return new DrawQuadSetGM();)
246
247} // namespace skiagm