• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorFilter.h"
13 #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"
23 #include "include/core/SkVertices.h"
24 #include "include/effects/SkGradientShader.h"
25 #include "include/private/SkTDArray.h"
26 #include "include/utils/SkRandom.h"
27 #include "src/shaders/SkLocalMatrixShader.h"
28 
29 #include <initializer_list>
30 #include <utility>
31 
32 static constexpr SkScalar kShaderSize = 40;
make_shader1(SkScalar shaderScale)33 static sk_sp<SkShader> make_shader1(SkScalar shaderScale) {
34     const SkColor colors[] = {
35         SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE,
36         SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW,
37     };
38     const SkPoint pts[] = {{kShaderSize / 4, 0}, {3 * kShaderSize / 4, kShaderSize}};
39     const SkMatrix localMatrix = SkMatrix::MakeScale(shaderScale, shaderScale);
40 
41     sk_sp<SkShader> grad = SkGradientShader::MakeLinear(pts, colors, nullptr,
42                                                         SK_ARRAY_COUNT(colors),
43                                                         SkTileMode::kMirror, 0,
44                                                         &localMatrix);
45     // Throw in a couple of local matrix wrappers for good measure.
46     return shaderScale == 1
47         ? grad
48         : sk_make_sp<SkLocalMatrixShader>(
49               sk_make_sp<SkLocalMatrixShader>(std::move(grad), SkMatrix::MakeTrans(-10, 0)),
50               SkMatrix::MakeTrans(10, 0));
51 }
52 
make_shader2()53 static sk_sp<SkShader> make_shader2() {
54     return SkShaders::Color(SK_ColorBLUE);
55 }
56 
make_color_filter()57 static sk_sp<SkColorFilter> make_color_filter() {
58     return SkColorFilters::Blend(0xFFAABBCC, SkBlendMode::kDarken);
59 }
60 
61 static constexpr SkScalar kMeshSize = 30;
62 
63 // start with the center of a 3x3 grid of vertices.
64 static constexpr uint16_t kMeshFan[] = {
65         4,
66         0, 1, 2, 5, 8, 7, 6, 3, 0
67 };
68 
69 static const int kMeshIndexCnt = (int)SK_ARRAY_COUNT(kMeshFan);
70 static const int kMeshVertexCnt = 9;
71 
fill_mesh(SkPoint pts[kMeshVertexCnt],SkPoint texs[kMeshVertexCnt],SkColor colors[kMeshVertexCnt],SkScalar shaderScale)72 static void fill_mesh(SkPoint pts[kMeshVertexCnt], SkPoint texs[kMeshVertexCnt],
73                       SkColor colors[kMeshVertexCnt], SkScalar shaderScale) {
74     pts[0].set(0, 0);
75     pts[1].set(kMeshSize / 2, 3);
76     pts[2].set(kMeshSize, 0);
77     pts[3].set(3, kMeshSize / 2);
78     pts[4].set(kMeshSize / 2, kMeshSize / 2);
79     pts[5].set(kMeshSize - 3, kMeshSize / 2);
80     pts[6].set(0, kMeshSize);
81     pts[7].set(kMeshSize / 2, kMeshSize - 3);
82     pts[8].set(kMeshSize, kMeshSize);
83 
84     const auto shaderSize = kShaderSize * shaderScale;
85     texs[0].set(0, 0);
86     texs[1].set(shaderSize / 2, 0);
87     texs[2].set(shaderSize, 0);
88     texs[3].set(0, shaderSize / 2);
89     texs[4].set(shaderSize / 2, shaderSize / 2);
90     texs[5].set(shaderSize, shaderSize / 2);
91     texs[6].set(0, shaderSize);
92     texs[7].set(shaderSize / 2, shaderSize);
93     texs[8].set(shaderSize, shaderSize);
94 
95     SkRandom rand;
96     for (size_t i = 0; i < kMeshVertexCnt; ++i) {
97         colors[i] = rand.nextU() | 0xFF000000;
98     }
99 }
100 
101 class VerticesGM : public skiagm::GM {
102     SkPoint                 fPts[kMeshVertexCnt];
103     SkPoint                 fTexs[kMeshVertexCnt];
104     SkColor                 fColors[kMeshVertexCnt];
105     sk_sp<SkShader>         fShader1;
106     sk_sp<SkShader>         fShader2;
107     sk_sp<SkColorFilter>    fColorFilter;
108     SkScalar                fShaderScale;
109 
110 public:
VerticesGM(SkScalar shaderScale)111     VerticesGM(SkScalar shaderScale) : fShaderScale(shaderScale) {}
112 
113 protected:
114 
onOnceBeforeDraw()115     void onOnceBeforeDraw() override {
116         fill_mesh(fPts, fTexs, fColors, fShaderScale);
117         fShader1 = make_shader1(fShaderScale);
118         fShader2 = make_shader2();
119         fColorFilter = make_color_filter();
120     }
121 
onShortName()122     SkString onShortName() override {
123         SkString name("vertices");
124         if (fShaderScale != 1) {
125             name.append("_scaled_shader");
126         }
127         return name;
128     }
129 
onISize()130     SkISize onISize() override {
131         return SkISize::Make(975, 1175);
132     }
133 
onDraw(SkCanvas * canvas)134     void onDraw(SkCanvas* canvas) override {
135         const SkBlendMode modes[] = {
136             SkBlendMode::kClear,
137             SkBlendMode::kSrc,
138             SkBlendMode::kDst,
139             SkBlendMode::kSrcOver,
140             SkBlendMode::kDstOver,
141             SkBlendMode::kSrcIn,
142             SkBlendMode::kDstIn,
143             SkBlendMode::kSrcOut,
144             SkBlendMode::kDstOut,
145             SkBlendMode::kSrcATop,
146             SkBlendMode::kDstATop,
147             SkBlendMode::kXor,
148             SkBlendMode::kPlus,
149             SkBlendMode::kModulate,
150             SkBlendMode::kScreen,
151             SkBlendMode::kOverlay,
152             SkBlendMode::kDarken,
153             SkBlendMode::kLighten,
154             SkBlendMode::kColorDodge,
155             SkBlendMode::kColorBurn,
156             SkBlendMode::kHardLight,
157             SkBlendMode::kSoftLight,
158             SkBlendMode::kDifference,
159             SkBlendMode::kExclusion,
160             SkBlendMode::kMultiply,
161             SkBlendMode::kHue,
162             SkBlendMode::kSaturation,
163             SkBlendMode::kColor,
164             SkBlendMode::kLuminosity,
165         };
166 
167         SkPaint paint;
168 
169         canvas->translate(4, 4);
170         int x = 0;
171         for (auto mode : modes) {
172             canvas->save();
173             for (float alpha : {1.0f, 0.5f}) {
174                 for (const auto& cf : {sk_sp<SkColorFilter>(nullptr), fColorFilter}) {
175                     for (const auto& shader : {fShader1, fShader2}) {
176                         static constexpr struct {
177                             bool fHasColors;
178                             bool fHasTexs;
179                         } kAttrs[] = {{true, false}, {false, true}, {true, true}};
180                         for (auto attrs : kAttrs) {
181                             paint.setShader(shader);
182                             paint.setColorFilter(cf);
183                             paint.setAlphaf(alpha);
184 
185                             const SkColor* colors = attrs.fHasColors ? fColors : nullptr;
186                             const SkPoint* texs = attrs.fHasTexs ? fTexs : nullptr;
187                             auto v = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode,
188                                                           kMeshVertexCnt, fPts, texs, colors,
189                                                           kMeshIndexCnt, kMeshFan);
190                             canvas->drawVertices(v, mode, paint);
191                             canvas->translate(40, 0);
192                             ++x;
193                         }
194                     }
195                 }
196             }
197             canvas->restore();
198             canvas->translate(0, 40);
199         }
200     }
201 
202 private:
203     typedef skiagm::GM INHERITED;
204 };
205 
206 /////////////////////////////////////////////////////////////////////////////////////
207 
208 DEF_GM(return new VerticesGM(1);)
209 DEF_GM(return new VerticesGM(1 / kShaderSize);)
210 
draw_batching(SkCanvas * canvas)211 static void draw_batching(SkCanvas* canvas) {
212     // Triangle fans can't batch so we convert to regular triangles,
213     static constexpr int kNumTris = kMeshIndexCnt - 2;
214     SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, kMeshVertexCnt, 3 * kNumTris,
215                                 SkVertices::kHasColors_BuilderFlag |
216                                 SkVertices::kHasTexCoords_BuilderFlag);
217 
218     SkPoint* pts = builder.positions();
219     SkPoint* texs = builder.texCoords();
220     SkColor* colors = builder.colors();
221     fill_mesh(pts, texs, colors, 1);
222 
223     SkTDArray<SkMatrix> matrices;
224     matrices.push()->reset();
225     matrices.push()->setTranslate(0, 40);
226     SkMatrix* m = matrices.push();
227     m->setRotate(45, kMeshSize / 2, kMeshSize / 2);
228     m->postScale(1.2f, .8f, kMeshSize / 2, kMeshSize / 2);
229     m->postTranslate(0, 80);
230 
231     auto shader = make_shader1(1);
232 
233     uint16_t* indices = builder.indices();
234     for (size_t i = 0; i < kNumTris; ++i) {
235         indices[3 * i] = kMeshFan[0];
236         indices[3 * i + 1] = kMeshFan[i + 1];
237         indices[3 * i + 2] = kMeshFan[i + 2];
238 
239     }
240 
241     canvas->save();
242     canvas->translate(10, 10);
243     for (bool useShader : {false, true}) {
244         for (bool useTex : {false, true}) {
245             for (const auto& m : matrices) {
246                 canvas->save();
247                 canvas->concat(m);
248                 SkPaint paint;
249                 paint.setShader(useShader ? shader : nullptr);
250 
251                 const SkPoint* t = useTex ? texs : nullptr;
252                 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, kMeshVertexCnt,
253                                               pts, t, colors, kNumTris * 3, indices);
254                 canvas->drawVertices(v, SkBlendMode::kModulate, paint);
255                 canvas->restore();
256             }
257             canvas->translate(0, 120);
258         }
259     }
260     canvas->restore();
261 }
262 
263 // This test exists to exercise batching in the gpu backend.
264 DEF_SIMPLE_GM(vertices_batching, canvas, 100, 500) {
265     draw_batching(canvas);
266     canvas->translate(50, 0);
267     draw_batching(canvas);
268 }
269