mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 1 | /* |
| 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" |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 12 | #include "SkLocalMatrixShader.h" |
mike@reedtribe.org | 2c326b7 | 2013-12-30 04:20:38 +0000 | [diff] [blame] | 13 | #include "SkRandom.h" |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 14 | #include "SkVertices.h" |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 15 | |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 16 | static constexpr SkScalar kShaderSize = 40; |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 17 | static sk_sp<SkShader> make_shader1(SkScalar shaderScale) { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 18 | const SkColor colors[] = { |
| 19 | SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, |
| 20 | SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, |
| 21 | }; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 22 | const SkPoint pts[] = {{kShaderSize / 4, 0}, {3 * kShaderSize / 4, kShaderSize}}; |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 23 | const SkMatrix localMatrix = SkMatrix::MakeScale(shaderScale, shaderScale); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 24 | |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 25 | 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.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 35 | } |
| 36 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 37 | static sk_sp<SkShader> make_shader2() { |
| 38 | return SkShader::MakeColorShader(SK_ColorBLUE); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 39 | } |
| 40 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 41 | static sk_sp<SkColorFilter> make_color_filter() { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 42 | return SkColorFilter::MakeModeFilter(0xFFAABBCC, SkBlendMode::kDarken); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 45 | static constexpr SkScalar kMeshSize = 30; |
| 46 | |
| 47 | // start with the center of a 3x3 grid of vertices. |
| 48 | static constexpr uint16_t kMeshFan[] = { |
| 49 | 4, |
| 50 | 0, 1, 2, 5, 8, 7, 6, 3, 0 |
| 51 | }; |
| 52 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 53 | static const int kMeshIndexCnt = (int)SK_ARRAY_COUNT(kMeshFan); |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 54 | static const int kMeshVertexCnt = 9; |
| 55 | |
| 56 | static void fill_mesh(SkPoint pts[kMeshVertexCnt], SkPoint texs[kMeshVertexCnt], |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 57 | SkColor colors[kMeshVertexCnt], SkScalar shaderScale) { |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 58 | 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 Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 68 | const auto shaderSize = kShaderSize * shaderScale; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 69 | texs[0].set(0, 0); |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 70 | 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 Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 78 | |
| 79 | SkRandom rand; |
| 80 | for (size_t i = 0; i < kMeshVertexCnt; ++i) { |
| 81 | colors[i] = rand.nextU() | 0xFF000000; |
| 82 | } |
| 83 | } |
| 84 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 85 | class VerticesGM : public skiagm::GM { |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 86 | SkPoint fPts[kMeshVertexCnt]; |
| 87 | SkPoint fTexs[kMeshVertexCnt]; |
| 88 | SkColor fColors[kMeshVertexCnt]; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 89 | sk_sp<SkShader> fShader1; |
| 90 | sk_sp<SkShader> fShader2; |
| 91 | sk_sp<SkColorFilter> fColorFilter; |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 92 | SkScalar fShaderScale; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 93 | |
| 94 | public: |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 95 | VerticesGM(SkScalar shaderScale) : fShaderScale(shaderScale) {} |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 96 | |
| 97 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 98 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | void onOnceBeforeDraw() override { |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 100 | fill_mesh(fPts, fTexs, fColors, fShaderScale); |
| 101 | fShader1 = make_shader1(fShaderScale); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 102 | fShader2 = make_shader2(); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 103 | fColorFilter = make_color_filter(); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 104 | } |
| 105 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 106 | SkString onShortName() override { |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 107 | SkString name("vertices"); |
Florin Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 108 | if (fShaderScale != 1) { |
| 109 | name.append("_scaled_shader"); |
| 110 | } |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 111 | return name; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 112 | } |
| 113 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 114 | SkISize onISize() override { |
Brian Salomon | 3216180 | 2017-02-10 10:48:15 -0500 | [diff] [blame] | 115 | return SkISize::Make(975, 1175); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 118 | void onDraw(SkCanvas* canvas) override { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 119 | 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.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 149 | }; |
skia.committer@gmail.com | 4c91286 | 2013-12-30 07:01:37 +0000 | [diff] [blame] | 150 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 151 | SkPaint paint; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 152 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 153 | canvas->translate(4, 4); |
| 154 | int x = 0; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 155 | for (auto mode : modes) { |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 156 | canvas->save(); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 157 | 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 Reed | 5fa6645 | 2017-03-16 09:06:34 -0400 | [diff] [blame] | 168 | |
| 169 | const SkColor* colors = attrs.fHasColors ? fColors : nullptr; |
| 170 | const SkPoint* texs = attrs.fHasTexs ? fTexs : nullptr; |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 171 | auto v = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, |
| 172 | kMeshVertexCnt, fPts, texs, colors, |
| 173 | kMeshIndexCnt, kMeshFan); |
| 174 | canvas->drawVertices(v, mode, paint); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 175 | canvas->translate(40, 0); |
| 176 | ++x; |
| 177 | } |
| 178 | } |
| 179 | } |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 180 | } |
| 181 | canvas->restore(); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 182 | canvas->translate(0, 40); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 186 | private: |
| 187 | typedef skiagm::GM INHERITED; |
| 188 | }; |
| 189 | |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 190 | ///////////////////////////////////////////////////////////////////////////////////// |
| 191 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 192 | DEF_GM(return new VerticesGM(1);) |
| 193 | DEF_GM(return new VerticesGM(1 / kShaderSize);) |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 194 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 195 | static void draw_batching(SkCanvas* canvas) { |
Mike Reed | 97eb4fe | 2017-03-14 12:04:16 -0400 | [diff] [blame] | 196 | // Triangle fans can't batch so we convert to regular triangles, |
| 197 | static constexpr int kNumTris = kMeshIndexCnt - 2; |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 198 | SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, kMeshVertexCnt, 3 * kNumTris, |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 199 | SkVertices::kHasColors_BuilderFlag | |
| 200 | SkVertices::kHasTexCoords_BuilderFlag); |
Mike Reed | 97eb4fe | 2017-03-14 12:04:16 -0400 | [diff] [blame] | 201 | |
| 202 | SkPoint* pts = builder.positions(); |
| 203 | SkPoint* texs = builder.texCoords(); |
| 204 | SkColor* colors = builder.colors(); |
| 205 | fill_mesh(pts, texs, colors, 1); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 206 | |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 207 | 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 Malita | f614ba2 | 2017-02-16 22:12:41 -0500 | [diff] [blame] | 215 | auto shader = make_shader1(1); |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 216 | |
Mike Reed | 97eb4fe | 2017-03-14 12:04:16 -0400 | [diff] [blame] | 217 | uint16_t* indices = builder.indices(); |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 218 | 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 Reed | 97eb4fe | 2017-03-14 12:04:16 -0400 | [diff] [blame] | 222 | |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 223 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 224 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 225 | canvas->save(); |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 226 | 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 Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 233 | paint.setShader(useShader ? shader : nullptr); |
Mike Reed | 5fa6645 | 2017-03-16 09:06:34 -0400 | [diff] [blame] | 234 | |
| 235 | const SkPoint* t = useTex ? texs : nullptr; |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 236 | auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, kMeshVertexCnt, |
| 237 | pts, t, colors, kNumTris * 3, indices); |
| 238 | canvas->drawVertices(v, SkBlendMode::kModulate, paint); |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 239 | canvas->restore(); |
| 240 | } |
| 241 | canvas->translate(0, 120); |
| 242 | } |
| 243 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 244 | canvas->restore(); |
| 245 | } |
| 246 | |
| 247 | // This test exists to exercise batching in the gpu backend. |
| 248 | DEF_SIMPLE_GM(vertices_batching, canvas, 100, 500) { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 249 | draw_batching(canvas); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 250 | canvas->translate(50, 0); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 251 | draw_batching(canvas); |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 252 | } |