blob: 7d57f0b02c45acd51aa3b53a14066850771922f2 [file] [log] [blame]
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +00001/*
2 * Copyright 2013 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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkColorFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
16#include "include/core/SkRefCnt.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/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkVertices.h"
24#include "include/effects/SkGradientShader.h"
Brian Osman3c358422020-03-23 10:44:12 -040025#include "include/effects/SkRuntimeEffect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/private/SkTDArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/utils/SkRandom.h"
Brian Osman3c358422020-03-23 10:44:12 -040028#include "src/core/SkVerticesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/shaders/SkLocalMatrixShader.h"
Brian Osman3c358422020-03-23 10:44:12 -040030#include "src/utils/SkPatchUtils.h"
31#include "tools/Resources.h"
Brian Osmane41fa2d2020-03-20 15:45:49 -040032#include "tools/ToolUtils.h"
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000033
Ben Wagner7fde8e12019-05-01 17:28:53 -040034#include <initializer_list>
35#include <utility>
36
Brian Salomon3f363692017-02-02 21:05:19 -050037static constexpr SkScalar kShaderSize = 40;
Florin Malitaf614ba22017-02-16 22:12:41 -050038static sk_sp<SkShader> make_shader1(SkScalar shaderScale) {
reed@google.com85e143c2013-12-30 15:51:25 +000039 const SkColor colors[] = {
40 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE,
41 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW,
42 };
Brian Salomon3f363692017-02-02 21:05:19 -050043 const SkPoint pts[] = {{kShaderSize / 4, 0}, {3 * kShaderSize / 4, kShaderSize}};
Mike Reed1f607332020-05-21 12:11:27 -040044 const SkMatrix localMatrix = SkMatrix::Scale(shaderScale, shaderScale);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000045
Florin Malitaf614ba22017-02-16 22:12:41 -050046 sk_sp<SkShader> grad = SkGradientShader::MakeLinear(pts, colors, nullptr,
47 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040048 SkTileMode::kMirror, 0,
Florin Malitaf614ba22017-02-16 22:12:41 -050049 &localMatrix);
50 // Throw in a couple of local matrix wrappers for good measure.
51 return shaderScale == 1
52 ? grad
53 : sk_make_sp<SkLocalMatrixShader>(
Mike Reed1f607332020-05-21 12:11:27 -040054 sk_make_sp<SkLocalMatrixShader>(std::move(grad), SkMatrix::Translate(-10, 0)),
55 SkMatrix::Translate(10, 0));
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000056}
57
reed1a9b9642016-03-13 14:13:58 -070058static sk_sp<SkShader> make_shader2() {
Mike Reedc8bea7d2019-04-09 13:55:36 -040059 return SkShaders::Color(SK_ColorBLUE);
bsalomon093779c2016-03-08 11:53:31 -080060}
61
reedd053ce92016-03-22 10:17:23 -070062static sk_sp<SkColorFilter> make_color_filter() {
Mike Reedb286bc22019-04-08 16:23:20 -040063 return SkColorFilters::Blend(0xFFAABBCC, SkBlendMode::kDarken);
bsalomon093779c2016-03-08 11:53:31 -080064}
65
Brian Salomon3f363692017-02-02 21:05:19 -050066static constexpr SkScalar kMeshSize = 30;
67
68// start with the center of a 3x3 grid of vertices.
69static constexpr uint16_t kMeshFan[] = {
70 4,
71 0, 1, 2, 5, 8, 7, 6, 3, 0
72};
73
Brian Salomon199fb872017-02-06 09:41:10 -050074static const int kMeshIndexCnt = (int)SK_ARRAY_COUNT(kMeshFan);
Brian Salomon3f363692017-02-02 21:05:19 -050075static const int kMeshVertexCnt = 9;
76
77static void fill_mesh(SkPoint pts[kMeshVertexCnt], SkPoint texs[kMeshVertexCnt],
Florin Malitaf614ba22017-02-16 22:12:41 -050078 SkColor colors[kMeshVertexCnt], SkScalar shaderScale) {
Brian Salomon3f363692017-02-02 21:05:19 -050079 pts[0].set(0, 0);
80 pts[1].set(kMeshSize / 2, 3);
81 pts[2].set(kMeshSize, 0);
82 pts[3].set(3, kMeshSize / 2);
83 pts[4].set(kMeshSize / 2, kMeshSize / 2);
84 pts[5].set(kMeshSize - 3, kMeshSize / 2);
85 pts[6].set(0, kMeshSize);
86 pts[7].set(kMeshSize / 2, kMeshSize - 3);
87 pts[8].set(kMeshSize, kMeshSize);
88
Florin Malitaf614ba22017-02-16 22:12:41 -050089 const auto shaderSize = kShaderSize * shaderScale;
Brian Salomon3f363692017-02-02 21:05:19 -050090 texs[0].set(0, 0);
Florin Malitaf614ba22017-02-16 22:12:41 -050091 texs[1].set(shaderSize / 2, 0);
92 texs[2].set(shaderSize, 0);
93 texs[3].set(0, shaderSize / 2);
94 texs[4].set(shaderSize / 2, shaderSize / 2);
95 texs[5].set(shaderSize, shaderSize / 2);
96 texs[6].set(0, shaderSize);
97 texs[7].set(shaderSize / 2, shaderSize);
98 texs[8].set(shaderSize, shaderSize);
Brian Salomon3f363692017-02-02 21:05:19 -050099
100 SkRandom rand;
101 for (size_t i = 0; i < kMeshVertexCnt; ++i) {
102 colors[i] = rand.nextU() | 0xFF000000;
103 }
104}
105
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000106class VerticesGM : public skiagm::GM {
Brian Salomon3f363692017-02-02 21:05:19 -0500107 SkPoint fPts[kMeshVertexCnt];
108 SkPoint fTexs[kMeshVertexCnt];
109 SkColor fColors[kMeshVertexCnt];
reedd053ce92016-03-22 10:17:23 -0700110 sk_sp<SkShader> fShader1;
111 sk_sp<SkShader> fShader2;
112 sk_sp<SkColorFilter> fColorFilter;
Florin Malitaf614ba22017-02-16 22:12:41 -0500113 SkScalar fShaderScale;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000114
115public:
Mike Reed887cdf12017-04-03 11:11:09 -0400116 VerticesGM(SkScalar shaderScale) : fShaderScale(shaderScale) {}
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000117
118protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000119
mtklein36352bf2015-03-25 18:17:31 -0700120 void onOnceBeforeDraw() override {
Florin Malitaf614ba22017-02-16 22:12:41 -0500121 fill_mesh(fPts, fTexs, fColors, fShaderScale);
122 fShader1 = make_shader1(fShaderScale);
reed1a9b9642016-03-13 14:13:58 -0700123 fShader2 = make_shader2();
reedd053ce92016-03-22 10:17:23 -0700124 fColorFilter = make_color_filter();
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000125 }
126
mtklein36352bf2015-03-25 18:17:31 -0700127 SkString onShortName() override {
reed@google.com60da8f32014-05-05 20:41:21 +0000128 SkString name("vertices");
Florin Malitaf614ba22017-02-16 22:12:41 -0500129 if (fShaderScale != 1) {
130 name.append("_scaled_shader");
131 }
reed@google.com60da8f32014-05-05 20:41:21 +0000132 return name;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000133 }
134
mtklein36352bf2015-03-25 18:17:31 -0700135 SkISize onISize() override {
Brian Salomon32161802017-02-10 10:48:15 -0500136 return SkISize::Make(975, 1175);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000137 }
138
mtklein36352bf2015-03-25 18:17:31 -0700139 void onDraw(SkCanvas* canvas) override {
Mike Reed7d954ad2016-10-28 15:42:34 -0400140 const SkBlendMode modes[] = {
141 SkBlendMode::kClear,
142 SkBlendMode::kSrc,
143 SkBlendMode::kDst,
144 SkBlendMode::kSrcOver,
145 SkBlendMode::kDstOver,
146 SkBlendMode::kSrcIn,
147 SkBlendMode::kDstIn,
148 SkBlendMode::kSrcOut,
149 SkBlendMode::kDstOut,
150 SkBlendMode::kSrcATop,
151 SkBlendMode::kDstATop,
152 SkBlendMode::kXor,
153 SkBlendMode::kPlus,
154 SkBlendMode::kModulate,
155 SkBlendMode::kScreen,
156 SkBlendMode::kOverlay,
157 SkBlendMode::kDarken,
158 SkBlendMode::kLighten,
159 SkBlendMode::kColorDodge,
160 SkBlendMode::kColorBurn,
161 SkBlendMode::kHardLight,
162 SkBlendMode::kSoftLight,
163 SkBlendMode::kDifference,
164 SkBlendMode::kExclusion,
165 SkBlendMode::kMultiply,
166 SkBlendMode::kHue,
167 SkBlendMode::kSaturation,
168 SkBlendMode::kColor,
169 SkBlendMode::kLuminosity,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000170 };
skia.committer@gmail.com4c912862013-12-30 07:01:37 +0000171
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000172 SkPaint paint;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000173
bsalomon093779c2016-03-08 11:53:31 -0800174 canvas->translate(4, 4);
175 int x = 0;
Brian Salomon199fb872017-02-06 09:41:10 -0500176 for (auto mode : modes) {
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000177 canvas->save();
Mike Reed9407e242019-02-15 16:13:57 -0500178 for (float alpha : {1.0f, 0.5f}) {
Brian Salomon199fb872017-02-06 09:41:10 -0500179 for (const auto& cf : {sk_sp<SkColorFilter>(nullptr), fColorFilter}) {
180 for (const auto& shader : {fShader1, fShader2}) {
181 static constexpr struct {
182 bool fHasColors;
183 bool fHasTexs;
184 } kAttrs[] = {{true, false}, {false, true}, {true, true}};
185 for (auto attrs : kAttrs) {
186 paint.setShader(shader);
187 paint.setColorFilter(cf);
Mike Reed9407e242019-02-15 16:13:57 -0500188 paint.setAlphaf(alpha);
Mike Reed5fa66452017-03-16 09:06:34 -0400189
190 const SkColor* colors = attrs.fHasColors ? fColors : nullptr;
191 const SkPoint* texs = attrs.fHasTexs ? fTexs : nullptr;
Mike Reed887cdf12017-04-03 11:11:09 -0400192 auto v = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode,
193 kMeshVertexCnt, fPts, texs, colors,
194 kMeshIndexCnt, kMeshFan);
195 canvas->drawVertices(v, mode, paint);
Brian Salomon199fb872017-02-06 09:41:10 -0500196 canvas->translate(40, 0);
197 ++x;
198 }
199 }
200 }
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000201 }
202 canvas->restore();
bsalomon093779c2016-03-08 11:53:31 -0800203 canvas->translate(0, 40);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000204 }
205 }
206
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000207private:
John Stiles7571f9e2020-09-02 22:42:33 -0400208 using INHERITED = skiagm::GM;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000209};
210
reed@google.com60da8f32014-05-05 20:41:21 +0000211/////////////////////////////////////////////////////////////////////////////////////
212
Mike Reed887cdf12017-04-03 11:11:09 -0400213DEF_GM(return new VerticesGM(1);)
214DEF_GM(return new VerticesGM(1 / kShaderSize);)
Brian Salomon3f363692017-02-02 21:05:19 -0500215
Mike Reed887cdf12017-04-03 11:11:09 -0400216static void draw_batching(SkCanvas* canvas) {
Mike Reed97eb4fe2017-03-14 12:04:16 -0400217 // Triangle fans can't batch so we convert to regular triangles,
218 static constexpr int kNumTris = kMeshIndexCnt - 2;
Mike Reed887cdf12017-04-03 11:11:09 -0400219 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, kMeshVertexCnt, 3 * kNumTris,
Mike Reedaa9e3322017-03-16 14:38:48 -0400220 SkVertices::kHasColors_BuilderFlag |
221 SkVertices::kHasTexCoords_BuilderFlag);
Mike Reed97eb4fe2017-03-14 12:04:16 -0400222
223 SkPoint* pts = builder.positions();
224 SkPoint* texs = builder.texCoords();
225 SkColor* colors = builder.colors();
226 fill_mesh(pts, texs, colors, 1);
Brian Salomon199fb872017-02-06 09:41:10 -0500227
Brian Salomon3f363692017-02-02 21:05:19 -0500228 SkTDArray<SkMatrix> matrices;
229 matrices.push()->reset();
230 matrices.push()->setTranslate(0, 40);
John Stilesa212b952021-08-02 13:26:38 -0400231 matrices.push()
232 ->setRotate(45, kMeshSize / 2, kMeshSize / 2)
233 .postScale(1.2f, .8f, kMeshSize / 2, kMeshSize / 2)
234 .postTranslate(0, 80);
Brian Salomon3f363692017-02-02 21:05:19 -0500235
Florin Malitaf614ba22017-02-16 22:12:41 -0500236 auto shader = make_shader1(1);
Brian Salomon3f363692017-02-02 21:05:19 -0500237
Mike Reed97eb4fe2017-03-14 12:04:16 -0400238 uint16_t* indices = builder.indices();
Brian Salomon3f363692017-02-02 21:05:19 -0500239 for (size_t i = 0; i < kNumTris; ++i) {
240 indices[3 * i] = kMeshFan[0];
241 indices[3 * i + 1] = kMeshFan[i + 1];
242 indices[3 * i + 2] = kMeshFan[i + 2];
Mike Reed97eb4fe2017-03-14 12:04:16 -0400243
Brian Salomon3f363692017-02-02 21:05:19 -0500244 }
Brian Salomon199fb872017-02-06 09:41:10 -0500245
Brian Salomon199fb872017-02-06 09:41:10 -0500246 canvas->save();
Brian Salomon3f363692017-02-02 21:05:19 -0500247 canvas->translate(10, 10);
248 for (bool useShader : {false, true}) {
249 for (bool useTex : {false, true}) {
250 for (const auto& m : matrices) {
251 canvas->save();
252 canvas->concat(m);
253 SkPaint paint;
Brian Salomon3f363692017-02-02 21:05:19 -0500254 paint.setShader(useShader ? shader : nullptr);
Mike Reed5fa66452017-03-16 09:06:34 -0400255
256 const SkPoint* t = useTex ? texs : nullptr;
Mike Reed887cdf12017-04-03 11:11:09 -0400257 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, kMeshVertexCnt,
258 pts, t, colors, kNumTris * 3, indices);
259 canvas->drawVertices(v, SkBlendMode::kModulate, paint);
Brian Salomon3f363692017-02-02 21:05:19 -0500260 canvas->restore();
261 }
262 canvas->translate(0, 120);
263 }
264 }
Brian Salomon199fb872017-02-06 09:41:10 -0500265 canvas->restore();
266}
267
268// This test exists to exercise batching in the gpu backend.
269DEF_SIMPLE_GM(vertices_batching, canvas, 100, 500) {
Mike Reed887cdf12017-04-03 11:11:09 -0400270 draw_batching(canvas);
Brian Salomon199fb872017-02-06 09:41:10 -0500271 canvas->translate(50, 0);
Mike Reed887cdf12017-04-03 11:11:09 -0400272 draw_batching(canvas);
Brian Salomon3f363692017-02-02 21:05:19 -0500273}
Mike Reedd32bdaf2020-03-06 12:52:52 -0500274
Brian Osmane41fa2d2020-03-20 15:45:49 -0400275// Test case for skbug.com/10069. We need to draw the vertices twice (with different matrices) to
276// trigger the bug.
277DEF_SIMPLE_GM(vertices_perspective, canvas, 256, 256) {
278 SkPaint paint;
279 paint.setShader(ToolUtils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 32));
280
281 SkRect r = SkRect::MakeWH(128, 128);
282
283 SkPoint pos[4];
284 r.toQuad(pos);
285 auto verts = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pos, pos, nullptr);
286
287 SkMatrix persp;
288 persp.setPerspY(SK_Scalar1 / 100);
289
290 canvas->save();
291 canvas->concat(persp);
292 canvas->drawRect(r, paint);
293 canvas->restore();
294
295 canvas->save();
296 canvas->translate(r.width(), 0);
297 canvas->concat(persp);
298 canvas->drawRect(r, paint);
299 canvas->restore();
300
301 canvas->save();
302 canvas->translate(0, r.height());
303 canvas->concat(persp);
304 canvas->drawVertices(verts, paint);
305 canvas->restore();
306
307 canvas->save();
308 canvas->translate(r.width(), r.height());
309 canvas->concat(persp);
310 canvas->drawVertices(verts, paint);
311 canvas->restore();
312}