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