• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
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 "tests/Test.h"
9 
10 #include "experimental/graphite/src/ContextUtils.h"
11 #include "experimental/graphite/src/UniformCache.h"
12 #include "include/core/SkPaint.h"
13 #include "include/effects/SkGradientShader.h"
14 
15 namespace {
16 
create_paint(skgpu::Combination combo)17 std::tuple<SkPaint, int> create_paint(skgpu::Combination combo) {
18     SkPoint pts[2] = {{-100, -100},
19                       {100,  100}};
20     SkColor colors[2] = {SK_ColorRED, SK_ColorGREEN};
21     SkScalar offsets[2] = {0.0f, 1.0f};
22 
23     sk_sp<SkShader> s;
24     int numUniforms = 0;
25     switch (combo.fShaderType) {
26         case skgpu::ShaderCombo::ShaderType::kNone:
27             numUniforms += 1;
28             break;
29         case skgpu::ShaderCombo::ShaderType::kLinearGradient:
30             s = SkGradientShader::MakeLinear(pts, colors, offsets, 2, combo.fTileMode);
31             numUniforms += 6;
32             break;
33         case skgpu::ShaderCombo::ShaderType::kRadialGradient:
34             s = SkGradientShader::MakeRadial({0, 0}, 100, colors, offsets, 2, combo.fTileMode);
35             numUniforms += 6;
36             break;
37         case skgpu::ShaderCombo::ShaderType::kSweepGradient:
38             s = SkGradientShader::MakeSweep(0, 0, colors, offsets, 2, combo.fTileMode,
39                                             0, 359, 0, nullptr);
40             numUniforms += 6;
41             break;
42         case skgpu::ShaderCombo::ShaderType::kConicalGradient:
43             s = SkGradientShader::MakeTwoPointConical({100, 100}, 100,
44                                                       {-100, -100}, 100,
45                                                       colors, offsets, 2, combo.fTileMode);
46             numUniforms += 6;
47             break;
48     }
49     SkPaint p;
50     p.setColor(SK_ColorRED);
51     p.setShader(std::move(s));
52     p.setBlendMode(combo.fBlendMode);
53     return { p, numUniforms };
54 }
55 
56 } // anonymous namespace
57 
DEF_GRAPHITE_TEST(UniformTest,reporter)58 DEF_GRAPHITE_TEST(UniformTest, reporter) {
59     using namespace skgpu;
60 
61     UniformCache cache;
62 
63     for (auto s : { ShaderCombo::ShaderType::kNone,
64                     ShaderCombo::ShaderType::kLinearGradient,
65                     ShaderCombo::ShaderType::kRadialGradient,
66                     ShaderCombo::ShaderType::kSweepGradient,
67                     ShaderCombo::ShaderType::kConicalGradient }) {
68         for (auto tm: { SkTileMode::kClamp,
69                         SkTileMode::kRepeat,
70                         SkTileMode::kMirror,
71                         SkTileMode::kDecal }) {
72             if (s == ShaderCombo::ShaderType::kNone) {
73                 tm = SkTileMode::kRepeat;  // the TileMode doesn't matter for this case
74             }
75 
76             for (auto bm : { SkBlendMode::kSrc, SkBlendMode::kSrcOver }) {
77                 Combination expected;
78 
79                 expected.fShaderType = s;
80                 expected.fTileMode = tm;
81                 expected.fBlendMode = bm;
82 
83                 auto [ p, expectedNumUniforms ] = create_paint(expected);
84                 auto [ actual, ud] = ExtractCombo(&cache, p);
85                 REPORTER_ASSERT(reporter, expected == actual);
86                 REPORTER_ASSERT(reporter, expectedNumUniforms == ud->count());
87                 for (int i = 0; i < ud->count(); ++i) {
88                     REPORTER_ASSERT(reporter, ud->offset(i) >= 0 && ud->offset(i) < ud->dataSize());
89                 }
90                 REPORTER_ASSERT(reporter, ud->id() != UniformData::kInvalidUniformID);
91             }
92         }
93     }
94 }
95