blob: 7c7c011d46a9ad0464df66f18c9839435ed261d5 [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
8#include "gm.h"
9#include "SkCanvas.h"
bsalomon093779c2016-03-08 11:53:31 -080010#include "SkColorFilter.h"
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000011#include "SkGradientShader.h"
Florin Malitaf614ba22017-02-16 22:12:41 -050012#include "SkLocalMatrixShader.h"
mike@reedtribe.org2c326b72013-12-30 04:20:38 +000013#include "SkRandom.h"
Brian Salomon199fb872017-02-06 09:41:10 -050014#include "SkVertices.h"
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000015
Brian Salomon3f363692017-02-02 21:05:19 -050016static constexpr SkScalar kShaderSize = 40;
Florin Malitaf614ba22017-02-16 22:12:41 -050017static sk_sp<SkShader> make_shader1(SkScalar shaderScale) {
reed@google.com85e143c2013-12-30 15:51:25 +000018 const SkColor colors[] = {
19 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE,
20 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW,
21 };
Brian Salomon3f363692017-02-02 21:05:19 -050022 const SkPoint pts[] = {{kShaderSize / 4, 0}, {3 * kShaderSize / 4, kShaderSize}};
Florin Malitaf614ba22017-02-16 22:12:41 -050023 const SkMatrix localMatrix = SkMatrix::MakeScale(shaderScale, shaderScale);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000024
Florin Malitaf614ba22017-02-16 22:12:41 -050025 sk_sp<SkShader> grad = SkGradientShader::MakeLinear(pts, colors, nullptr,
26 SK_ARRAY_COUNT(colors),
27 SkShader::kMirror_TileMode, 0,
28 &localMatrix);
29 // Throw in a couple of local matrix wrappers for good measure.
30 return shaderScale == 1
31 ? grad
32 : sk_make_sp<SkLocalMatrixShader>(
33 sk_make_sp<SkLocalMatrixShader>(std::move(grad), SkMatrix::MakeTrans(-10, 0)),
34 SkMatrix::MakeTrans(10, 0));
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000035}
36
reed1a9b9642016-03-13 14:13:58 -070037static sk_sp<SkShader> make_shader2() {
38 return SkShader::MakeColorShader(SK_ColorBLUE);
bsalomon093779c2016-03-08 11:53:31 -080039}
40
reedd053ce92016-03-22 10:17:23 -070041static sk_sp<SkColorFilter> make_color_filter() {
Mike Reed7d954ad2016-10-28 15:42:34 -040042 return SkColorFilter::MakeModeFilter(0xFFAABBCC, SkBlendMode::kDarken);
bsalomon093779c2016-03-08 11:53:31 -080043}
44
Brian Salomon3f363692017-02-02 21:05:19 -050045static constexpr SkScalar kMeshSize = 30;
46
47// start with the center of a 3x3 grid of vertices.
48static constexpr uint16_t kMeshFan[] = {
49 4,
50 0, 1, 2, 5, 8, 7, 6, 3, 0
51};
52
Brian Salomon199fb872017-02-06 09:41:10 -050053static const int kMeshIndexCnt = (int)SK_ARRAY_COUNT(kMeshFan);
Brian Salomon3f363692017-02-02 21:05:19 -050054static const int kMeshVertexCnt = 9;
55
56static void fill_mesh(SkPoint pts[kMeshVertexCnt], SkPoint texs[kMeshVertexCnt],
Florin Malitaf614ba22017-02-16 22:12:41 -050057 SkColor colors[kMeshVertexCnt], SkScalar shaderScale) {
Brian Salomon3f363692017-02-02 21:05:19 -050058 pts[0].set(0, 0);
59 pts[1].set(kMeshSize / 2, 3);
60 pts[2].set(kMeshSize, 0);
61 pts[3].set(3, kMeshSize / 2);
62 pts[4].set(kMeshSize / 2, kMeshSize / 2);
63 pts[5].set(kMeshSize - 3, kMeshSize / 2);
64 pts[6].set(0, kMeshSize);
65 pts[7].set(kMeshSize / 2, kMeshSize - 3);
66 pts[8].set(kMeshSize, kMeshSize);
67
Florin Malitaf614ba22017-02-16 22:12:41 -050068 const auto shaderSize = kShaderSize * shaderScale;
Brian Salomon3f363692017-02-02 21:05:19 -050069 texs[0].set(0, 0);
Florin Malitaf614ba22017-02-16 22:12:41 -050070 texs[1].set(shaderSize / 2, 0);
71 texs[2].set(shaderSize, 0);
72 texs[3].set(0, shaderSize / 2);
73 texs[4].set(shaderSize / 2, shaderSize / 2);
74 texs[5].set(shaderSize, shaderSize / 2);
75 texs[6].set(0, shaderSize);
76 texs[7].set(shaderSize / 2, shaderSize);
77 texs[8].set(shaderSize, shaderSize);
Brian Salomon3f363692017-02-02 21:05:19 -050078
79 SkRandom rand;
80 for (size_t i = 0; i < kMeshVertexCnt; ++i) {
81 colors[i] = rand.nextU() | 0xFF000000;
82 }
83}
84
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000085class VerticesGM : public skiagm::GM {
Brian Salomon3f363692017-02-02 21:05:19 -050086 SkPoint fPts[kMeshVertexCnt];
87 SkPoint fTexs[kMeshVertexCnt];
88 SkColor fColors[kMeshVertexCnt];
reedd053ce92016-03-22 10:17:23 -070089 sk_sp<SkShader> fShader1;
90 sk_sp<SkShader> fShader2;
91 sk_sp<SkColorFilter> fColorFilter;
Florin Malitaf614ba22017-02-16 22:12:41 -050092 SkScalar fShaderScale;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000093
94public:
Mike Reed887cdf12017-04-03 11:11:09 -040095 VerticesGM(SkScalar shaderScale) : fShaderScale(shaderScale) {}
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000096
97protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000098
mtklein36352bf2015-03-25 18:17:31 -070099 void onOnceBeforeDraw() override {
Florin Malitaf614ba22017-02-16 22:12:41 -0500100 fill_mesh(fPts, fTexs, fColors, fShaderScale);
101 fShader1 = make_shader1(fShaderScale);
reed1a9b9642016-03-13 14:13:58 -0700102 fShader2 = make_shader2();
reedd053ce92016-03-22 10:17:23 -0700103 fColorFilter = make_color_filter();
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000104 }
105
mtklein36352bf2015-03-25 18:17:31 -0700106 SkString onShortName() override {
reed@google.com60da8f32014-05-05 20:41:21 +0000107 SkString name("vertices");
Florin Malitaf614ba22017-02-16 22:12:41 -0500108 if (fShaderScale != 1) {
109 name.append("_scaled_shader");
110 }
reed@google.com60da8f32014-05-05 20:41:21 +0000111 return name;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000112 }
113
mtklein36352bf2015-03-25 18:17:31 -0700114 SkISize onISize() override {
Brian Salomon32161802017-02-10 10:48:15 -0500115 return SkISize::Make(975, 1175);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000116 }
117
mtklein36352bf2015-03-25 18:17:31 -0700118 void onDraw(SkCanvas* canvas) override {
Mike Reed7d954ad2016-10-28 15:42:34 -0400119 const SkBlendMode modes[] = {
120 SkBlendMode::kClear,
121 SkBlendMode::kSrc,
122 SkBlendMode::kDst,
123 SkBlendMode::kSrcOver,
124 SkBlendMode::kDstOver,
125 SkBlendMode::kSrcIn,
126 SkBlendMode::kDstIn,
127 SkBlendMode::kSrcOut,
128 SkBlendMode::kDstOut,
129 SkBlendMode::kSrcATop,
130 SkBlendMode::kDstATop,
131 SkBlendMode::kXor,
132 SkBlendMode::kPlus,
133 SkBlendMode::kModulate,
134 SkBlendMode::kScreen,
135 SkBlendMode::kOverlay,
136 SkBlendMode::kDarken,
137 SkBlendMode::kLighten,
138 SkBlendMode::kColorDodge,
139 SkBlendMode::kColorBurn,
140 SkBlendMode::kHardLight,
141 SkBlendMode::kSoftLight,
142 SkBlendMode::kDifference,
143 SkBlendMode::kExclusion,
144 SkBlendMode::kMultiply,
145 SkBlendMode::kHue,
146 SkBlendMode::kSaturation,
147 SkBlendMode::kColor,
148 SkBlendMode::kLuminosity,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000149 };
skia.committer@gmail.com4c912862013-12-30 07:01:37 +0000150
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000151 SkPaint paint;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000152
bsalomon093779c2016-03-08 11:53:31 -0800153 canvas->translate(4, 4);
154 int x = 0;
Brian Salomon199fb872017-02-06 09:41:10 -0500155 for (auto mode : modes) {
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000156 canvas->save();
Brian Salomon199fb872017-02-06 09:41:10 -0500157 for (uint8_t alpha : {0xFF, 0x80}) {
158 for (const auto& cf : {sk_sp<SkColorFilter>(nullptr), fColorFilter}) {
159 for (const auto& shader : {fShader1, fShader2}) {
160 static constexpr struct {
161 bool fHasColors;
162 bool fHasTexs;
163 } kAttrs[] = {{true, false}, {false, true}, {true, true}};
164 for (auto attrs : kAttrs) {
165 paint.setShader(shader);
166 paint.setColorFilter(cf);
167 paint.setAlpha(alpha);
Mike Reed5fa66452017-03-16 09:06:34 -0400168
169 const SkColor* colors = attrs.fHasColors ? fColors : nullptr;
170 const SkPoint* texs = attrs.fHasTexs ? fTexs : nullptr;
Mike Reed887cdf12017-04-03 11:11:09 -0400171 auto v = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode,
172 kMeshVertexCnt, fPts, texs, colors,
173 kMeshIndexCnt, kMeshFan);
174 canvas->drawVertices(v, mode, paint);
Brian Salomon199fb872017-02-06 09:41:10 -0500175 canvas->translate(40, 0);
176 ++x;
177 }
178 }
179 }
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000180 }
181 canvas->restore();
bsalomon093779c2016-03-08 11:53:31 -0800182 canvas->translate(0, 40);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000183 }
184 }
185
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000186private:
187 typedef skiagm::GM INHERITED;
188};
189
reed@google.com60da8f32014-05-05 20:41:21 +0000190/////////////////////////////////////////////////////////////////////////////////////
191
Mike Reed887cdf12017-04-03 11:11:09 -0400192DEF_GM(return new VerticesGM(1);)
193DEF_GM(return new VerticesGM(1 / kShaderSize);)
Brian Salomon3f363692017-02-02 21:05:19 -0500194
Mike Reed887cdf12017-04-03 11:11:09 -0400195static void draw_batching(SkCanvas* canvas) {
Mike Reed97eb4fe2017-03-14 12:04:16 -0400196 // Triangle fans can't batch so we convert to regular triangles,
197 static constexpr int kNumTris = kMeshIndexCnt - 2;
Mike Reed887cdf12017-04-03 11:11:09 -0400198 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, kMeshVertexCnt, 3 * kNumTris,
Mike Reedaa9e3322017-03-16 14:38:48 -0400199 SkVertices::kHasColors_BuilderFlag |
200 SkVertices::kHasTexCoords_BuilderFlag);
Mike Reed97eb4fe2017-03-14 12:04:16 -0400201
202 SkPoint* pts = builder.positions();
203 SkPoint* texs = builder.texCoords();
204 SkColor* colors = builder.colors();
205 fill_mesh(pts, texs, colors, 1);
Brian Salomon199fb872017-02-06 09:41:10 -0500206
Brian Salomon3f363692017-02-02 21:05:19 -0500207 SkTDArray<SkMatrix> matrices;
208 matrices.push()->reset();
209 matrices.push()->setTranslate(0, 40);
210 SkMatrix* m = matrices.push();
211 m->setRotate(45, kMeshSize / 2, kMeshSize / 2);
212 m->postScale(1.2f, .8f, kMeshSize / 2, kMeshSize / 2);
213 m->postTranslate(0, 80);
214
Florin Malitaf614ba22017-02-16 22:12:41 -0500215 auto shader = make_shader1(1);
Brian Salomon3f363692017-02-02 21:05:19 -0500216
Mike Reed97eb4fe2017-03-14 12:04:16 -0400217 uint16_t* indices = builder.indices();
Brian Salomon3f363692017-02-02 21:05:19 -0500218 for (size_t i = 0; i < kNumTris; ++i) {
219 indices[3 * i] = kMeshFan[0];
220 indices[3 * i + 1] = kMeshFan[i + 1];
221 indices[3 * i + 2] = kMeshFan[i + 2];
Mike Reed97eb4fe2017-03-14 12:04:16 -0400222
Brian Salomon3f363692017-02-02 21:05:19 -0500223 }
Brian Salomon199fb872017-02-06 09:41:10 -0500224
Brian Salomon199fb872017-02-06 09:41:10 -0500225 canvas->save();
Brian Salomon3f363692017-02-02 21:05:19 -0500226 canvas->translate(10, 10);
227 for (bool useShader : {false, true}) {
228 for (bool useTex : {false, true}) {
229 for (const auto& m : matrices) {
230 canvas->save();
231 canvas->concat(m);
232 SkPaint paint;
Brian Salomon3f363692017-02-02 21:05:19 -0500233 paint.setShader(useShader ? shader : nullptr);
Mike Reed5fa66452017-03-16 09:06:34 -0400234
235 const SkPoint* t = useTex ? texs : nullptr;
Mike Reed887cdf12017-04-03 11:11:09 -0400236 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, kMeshVertexCnt,
237 pts, t, colors, kNumTris * 3, indices);
238 canvas->drawVertices(v, SkBlendMode::kModulate, paint);
Brian Salomon3f363692017-02-02 21:05:19 -0500239 canvas->restore();
240 }
241 canvas->translate(0, 120);
242 }
243 }
Brian Salomon199fb872017-02-06 09:41:10 -0500244 canvas->restore();
245}
246
247// This test exists to exercise batching in the gpu backend.
248DEF_SIMPLE_GM(vertices_batching, canvas, 100, 500) {
Mike Reed887cdf12017-04-03 11:11:09 -0400249 draw_batching(canvas);
Brian Salomon199fb872017-02-06 09:41:10 -0500250 canvas->translate(50, 0);
Mike Reed887cdf12017-04-03 11:11:09 -0400251 draw_batching(canvas);
Brian Salomon3f363692017-02-02 21:05:19 -0500252}