blob: 36a3be99364cbc4d6293a8273188d8408bbdc686 [file] [log] [blame]
Ethan Nicholas58430122020-04-14 09:54:02 -04001/*
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"
Brian Osman449b1152020-04-15 16:43:00 -040010#include "src/core/SkMatrixProvider.h"
Ethan Nicholas58430122020-04-14 09:54:02 -040011#include "src/gpu/GrBitmapTextureMaker.h"
12#include "src/gpu/GrClip.h"
13#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrRenderTargetContextPriv.h"
15#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16#include "src/gpu/ops/GrFillRectOp.h"
17#include "tools/Resources.h"
18
19class SampleMatrixConstantEffect : public GrFragmentProcessor {
20public:
21 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 1;
22
23 SampleMatrixConstantEffect(std::unique_ptr<GrFragmentProcessor> child)
24 : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
25 child->setSampleMatrix(SkSL::SampleMatrix(SkSL::SampleMatrix::Kind::kVariable));
26 this->registerChildProcessor(std::move(child));
27 }
28
29 const char* name() const override { return "SampleMatrixConstantEffect"; }
30
31 std::unique_ptr<GrFragmentProcessor> clone() const override {
32 SkASSERT(false);
33 return nullptr;
34 }
35
36 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {
37 }
38
39 bool onIsEqual(const GrFragmentProcessor& that) const override {
40 return this == &that;
41 }
42
43private:
44 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
45 typedef GrFragmentProcessor INHERITED;
46};
47
48class GLSLSampleMatrixConstantEffect : public GrGLSLFragmentProcessor {
49 void emitCode(EmitArgs& args) override {
50 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
51 SkString sample = this->invokeChildWithMatrix(0, args, "float3x3(0.5)");
52 fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, sample.c_str());
53 }
54};
55
56GrGLSLFragmentProcessor* SampleMatrixConstantEffect::onCreateGLSLInstance() const {
57 return new GLSLSampleMatrixConstantEffect();
58}
59
60
61DEF_SIMPLE_GPU_GM(sample_matrix_constant, ctx, rtCtx, canvas, 512, 256) {
62 {
63 SkRect bounds = SkRect::MakeIWH(256, 256);
64 SkBitmap bmp;
65 GetResourceAsBitmap("images/mandrill_256.png", &bmp);
66 GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
67 auto view = maker.view(GrMipMapped::kNo);
68 std::unique_ptr<GrFragmentProcessor> imgFP =
69 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
70 auto fp = std::unique_ptr<GrFragmentProcessor>(
71 new SampleMatrixConstantEffect(std::move(imgFP)));
72
73 GrPaint paint;
74 paint.addCoverageFragmentProcessor(std::move(fp));
75 rtCtx->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), bounds);
76 }
77
78 {
79 GrPaint paint;
80 SkRect bounds = SkRect::MakeLTRB(256, 0, 512, 256);
81 static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff };
82 static constexpr SkScalar pos[] = { 0.0f, 1.0f };
83 const SkPoint pts[] = {{ 256, 0 }, { 512, 0 }};
84
85 auto shader = SkGradientShader::MakeLinear(pts, colors, pos,
86 SK_ARRAY_COUNT(colors),
87 SkTileMode::kRepeat);
88 SkMatrix matrix;
Brian Osman449b1152020-04-15 16:43:00 -040089 SkSimpleMatrixProvider matrixProvider(matrix);
Ethan Nicholas58430122020-04-14 09:54:02 -040090 GrColorInfo colorInfo;
Brian Osman449b1152020-04-15 16:43:00 -040091 GrFPArgs args(ctx, matrixProvider, kHigh_SkFilterQuality, &colorInfo);
Ethan Nicholas58430122020-04-14 09:54:02 -040092 std::unique_ptr<GrFragmentProcessor> gradientFP = as_SB(shader)->asFragmentProcessor(args);
93 auto fp = std::unique_ptr<GrFragmentProcessor>(
94 new SampleMatrixConstantEffect(std::move(gradientFP)));
95 paint.addCoverageFragmentProcessor(std::move(fp));
96 rtCtx->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), bounds);
97 }
98}
99