1 /*
2 * Copyright 2022 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 "include/core/SkCanvas.h"
9 #include "include/core/SkSurface.h"
10 #include "include/effects/SkGradientShader.h"
11 #include "src/core/SkPaintParamsKey.h"
12 #include "src/core/SkPaintPriv.h"
13 #include "src/core/SkShaderCodeDictionary.h"
14 #include "tests/Test.h"
15
16 namespace {
17
make_image_shader(int imageWidth,int imageHeight,SkTileMode xTileMode,SkTileMode yTileMode,SkColor color)18 sk_sp<SkShader> make_image_shader(int imageWidth, int imageHeight,
19 SkTileMode xTileMode, SkTileMode yTileMode,
20 SkColor color) {
21 auto surface = SkSurface::MakeRasterN32Premul(imageWidth, imageHeight);
22 SkCanvas *canvas = surface->getCanvas();
23 canvas->clear(color);
24 return surface->makeImageSnapshot()->makeShader(xTileMode, yTileMode, SkSamplingOptions());
25 }
26
make_linear_gradient_shader(SkTileMode tileMode)27 sk_sp<SkShader> make_linear_gradient_shader(SkTileMode tileMode) {
28 SkPoint pts[2];
29 SkColor colors[2] = {SK_ColorRED, SK_ColorBLUE};
30
31 pts[0].set(0, 0);
32 pts[1].set(SkIntToScalar(100), 0);
33 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, tileMode);
34 }
35
make_blend_shader(sk_sp<SkShader> shaderA,sk_sp<SkShader> shaderB,SkBlendMode mode)36 sk_sp<SkShader> make_blend_shader(sk_sp<SkShader> shaderA,
37 sk_sp<SkShader> shaderB,
38 SkBlendMode mode) {
39 return SkShaders::Blend(mode, std::move(shaderA), std::move(shaderB));
40 }
41
dump_keys(SkShaderCodeDictionary * dict,const SkPaint & paint)42 void dump_keys(SkShaderCodeDictionary *dict, const SkPaint &paint) {
43 #ifdef SK_DEBUG
44 auto keys = SkPaintPriv::ToKeys(paint, dict, SkBackend::kGraphite);
45
46 for (const auto& k : keys) {
47 // TODO: we need a better way to assess that key creation succeeded
48 k->dump();
49 }
50 #endif
51 }
52
53 } // anonymous namespace
54
DEF_GRAPHITE_TEST(ComboTest,r)55 DEF_GRAPHITE_TEST(ComboTest, r) {
56 SkShaderCodeDictionary dict;
57
58 {
59 SkPaint paint;
60 paint.setBlendMode(SkBlendMode::kLighten);
61 dump_keys(&dict, paint);
62 }
63
64 {
65 SkPaint paint;
66 paint.setShader(make_image_shader(16, 16, SkTileMode::kClamp,
67 SkTileMode::kRepeat, SK_ColorRED));
68 dump_keys(&dict, paint);
69 }
70
71 {
72 SkPaint paint;
73 paint.setShader(make_linear_gradient_shader(SkTileMode::kClamp));
74 dump_keys(&dict, paint);
75 }
76
77 {
78 SkPaint paint;
79 auto shaderA = make_image_shader(16, 16, SkTileMode::kDecal,
80 SkTileMode::kRepeat, SK_ColorBLUE);
81 auto shaderB = make_linear_gradient_shader(SkTileMode::kClamp);
82 paint.setShader(make_blend_shader(std::move(shaderA), std::move(shaderB),
83 SkBlendMode::kDstIn));
84 dump_keys(&dict, paint);
85 }
86 }
87