1 /*
2 * Copyright 2019 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 "gm/gm.h"
9 #include "include/effects/SkGradientShader.h"
10 #include "src/core/SkMatrixProvider.h"
11 #include "src/gpu/GrDirectContextPriv.h"
12 #include "src/gpu/SkGr.h"
13 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "src/gpu/ops/GrFillRectOp.h"
15 #include "tools/Resources.h"
16
17 class SampleMatrixConstantEffect : public GrFragmentProcessor {
18 public:
19 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 1;
20
SampleMatrixConstantEffect(std::unique_ptr<GrFragmentProcessor> child)21 SampleMatrixConstantEffect(std::unique_ptr<GrFragmentProcessor> child)
22 : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
23 this->registerChild(std::move(child),
24 SkSL::SampleUsage::UniformMatrix(
25 "float3x3(float3(0.5, 0.0, 0.0), "
26 "float3(0.0, 0.5, 0.0), "
27 "float3(0.0, 0.0, 1.0))"));
28 }
29
name() const30 const char* name() const override { return "SampleMatrixConstantEffect"; }
31
clone() const32 std::unique_ptr<GrFragmentProcessor> clone() const override {
33 SkASSERT(false);
34 return nullptr;
35 }
36
onGetGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder *) const37 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
onIsEqual(const GrFragmentProcessor & that) const38 bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
39
40 private:
41 std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override;
42 using INHERITED = GrFragmentProcessor;
43 };
44
45 class GLSLSampleMatrixConstantEffect : public GrGLSLFragmentProcessor {
emitCode(EmitArgs & args)46 void emitCode(EmitArgs& args) override {
47 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
48 SkString sample = this->invokeChildWithMatrix(0, args);
49 fragBuilder->codeAppendf("return %s;\n", sample.c_str());
50 }
51 };
52
onMakeProgramImpl() const53 std::unique_ptr<GrGLSLFragmentProcessor> SampleMatrixConstantEffect::onMakeProgramImpl() const {
54 return std::make_unique<GLSLSampleMatrixConstantEffect>();
55 }
56
57 DEF_SIMPLE_GPU_GM(sample_matrix_constant, ctx, rtCtx, canvas, 1024, 256) {
__anon6e58b17b0102(std::unique_ptr<GrFragmentProcessor> baseFP) 58 auto wrap = [](std::unique_ptr<GrFragmentProcessor> baseFP) {
59 return std::unique_ptr<GrFragmentProcessor>(
60 new SampleMatrixConstantEffect(std::move(baseFP)));
61 };
__anon6e58b17b0202(std::unique_ptr<GrFragmentProcessor> baseFP, int tx, int ty) 62 auto draw = [rtCtx, &wrap](std::unique_ptr<GrFragmentProcessor> baseFP, int tx, int ty) {
63 auto fp = wrap(std::move(baseFP));
64 GrPaint paint;
65 paint.setColorFragmentProcessor(std::move(fp));
66 rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty),
67 SkRect::MakeIWH(256, 256));
68 };
__anon6e58b17b0302(std::unique_ptr<GrFragmentProcessor> baseFP, int tx, int ty) 69 auto draw2 = [rtCtx, &wrap](std::unique_ptr<GrFragmentProcessor> baseFP, int tx, int ty) {
70 auto fp = wrap(wrap(std::move(baseFP)));
71 GrPaint paint;
72 paint.setColorFragmentProcessor(std::move(fp));
73 rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty),
74 SkRect::MakeIWH(256, 256));
75 };
76
77 {
78 SkBitmap bmp;
79 GetResourceAsBitmap("images/mandrill_256.png", &bmp);
80 auto [view, ct] = GrMakeCachedBitmapProxyView(ctx, bmp, GrMipmapped::kNo);
81 std::unique_ptr<GrFragmentProcessor> imgFP = GrTextureEffect::Make(view,
82 bmp.alphaType(),
83 SkMatrix());
84 draw(std::move(imgFP), 0, 0);
85 imgFP = GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
86 draw2(std::move(imgFP), 256, 0);
87 }
88
89 {
90 static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff };
91 const SkPoint pts[] = {{ 0, 0 }, { 256, 0 }};
92
93 auto shader = SkGradientShader::MakeLinear(pts, colors, nullptr,
94 SK_ARRAY_COUNT(colors),
95 SkTileMode::kClamp);
96 SkMatrix matrix;
97 SkSimpleMatrixProvider matrixProvider(matrix);
98 GrColorInfo colorInfo;
99 GrFPArgs args(ctx, matrixProvider, &colorInfo);
100 std::unique_ptr<GrFragmentProcessor> gradientFP = as_SB(shader)->asFragmentProcessor(args);
101 draw(std::move(gradientFP), 512, 0);
102 gradientFP = as_SB(shader)->asFragmentProcessor(args);
103 draw2(std::move(gradientFP), 768, 0);
104 }
105 }
106