blob: d05f0126e84c13e001be9596181a415d4088646f [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"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Ethan Nicholas58430122020-04-14 09:54:02 -040013#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14#include "src/gpu/ops/GrFillRectOp.h"
15#include "tools/Resources.h"
16
17class SampleMatrixConstantEffect : public GrFragmentProcessor {
18public:
19 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 1;
20
21 SampleMatrixConstantEffect(std::unique_ptr<GrFragmentProcessor> child)
Brian Osman5d7759e2020-05-29 09:25:56 -040022 : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
Michael Ludwig9aba6252020-06-22 14:46:36 -040023 this->registerChild(std::move(child),
Brian Osman1298bc42020-06-30 13:39:35 -040024 SkSL::SampleUsage::UniformMatrix(
Michael Ludwig9aba6252020-06-22 14:46:36 -040025 "float3x3(float3(0.5, 0.0, 0.0), "
26 "float3(0.0, 0.5, 0.0), "
27 "float3(0.0, 0.0, 1.0))"));
Ethan Nicholas58430122020-04-14 09:54:02 -040028 }
29
30 const char* name() const override { return "SampleMatrixConstantEffect"; }
John Stiles4bdc1212020-12-14 18:04:13 -050031 bool usesExplicitReturn() const override { return true; }
Ethan Nicholas58430122020-04-14 09:54:02 -040032
33 std::unique_ptr<GrFragmentProcessor> clone() const override {
34 SkASSERT(false);
35 return nullptr;
36 }
37
Brian Osman5d7759e2020-05-29 09:25:56 -040038 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
39 bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
Ethan Nicholas58430122020-04-14 09:54:02 -040040
41private:
42 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
John Stiles7571f9e2020-09-02 22:42:33 -040043 using INHERITED = GrFragmentProcessor;
Ethan Nicholas58430122020-04-14 09:54:02 -040044};
45
46class GLSLSampleMatrixConstantEffect : public GrGLSLFragmentProcessor {
47 void emitCode(EmitArgs& args) override {
48 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Michael Ludwige88320b2020-06-24 09:04:56 -040049 SkString sample = this->invokeChildWithMatrix(0, args);
John Stiles4bdc1212020-12-14 18:04:13 -050050 fragBuilder->codeAppendf("return %s;\n", sample.c_str());
Ethan Nicholas58430122020-04-14 09:54:02 -040051 }
52};
53
54GrGLSLFragmentProcessor* SampleMatrixConstantEffect::onCreateGLSLInstance() const {
55 return new GLSLSampleMatrixConstantEffect();
56}
57
Brian Salomon25776232020-06-10 12:47:25 -040058DEF_SIMPLE_GPU_GM(sample_matrix_constant, ctx, rtCtx, canvas, 1024, 256) {
59 auto wrap = [](std::unique_ptr<GrFragmentProcessor> baseFP) {
60 return std::unique_ptr<GrFragmentProcessor>(
61 new SampleMatrixConstantEffect(std::move(baseFP)));
62 };
63 auto draw = [rtCtx, &wrap](std::unique_ptr<GrFragmentProcessor> baseFP, int tx, int ty) {
64 auto fp = wrap(std::move(baseFP));
Brian Osman5d7759e2020-05-29 09:25:56 -040065 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -040066 paint.setColorFragmentProcessor(std::move(fp));
Brian Osman5d7759e2020-05-29 09:25:56 -040067 rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty),
68 SkRect::MakeIWH(256, 256));
69 };
Brian Salomon25776232020-06-10 12:47:25 -040070 auto draw2 = [rtCtx, &wrap](std::unique_ptr<GrFragmentProcessor> baseFP, int tx, int ty) {
71 auto fp = wrap(wrap(std::move(baseFP)));
72 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -040073 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon25776232020-06-10 12:47:25 -040074 rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty),
75 SkRect::MakeIWH(256, 256));
76 };
Brian Osman5d7759e2020-05-29 09:25:56 -040077
Ethan Nicholas58430122020-04-14 09:54:02 -040078 {
Ethan Nicholas58430122020-04-14 09:54:02 -040079 SkBitmap bmp;
80 GetResourceAsBitmap("images/mandrill_256.png", &bmp);
81 GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
Brian Salomon7e67dca2020-07-21 09:27:25 -040082 auto view = maker.view(GrMipmapped::kNo);
Ethan Nicholas58430122020-04-14 09:54:02 -040083 std::unique_ptr<GrFragmentProcessor> imgFP =
84 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
Brian Osman5d7759e2020-05-29 09:25:56 -040085 draw(std::move(imgFP), 0, 0);
Brian Salomon7e67dca2020-07-21 09:27:25 -040086 view = maker.view(GrMipmapped::kNo);
Brian Salomon25776232020-06-10 12:47:25 -040087 imgFP =
88 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
89 draw2(std::move(imgFP), 256, 0);
Ethan Nicholas58430122020-04-14 09:54:02 -040090 }
91
92 {
Ethan Nicholas58430122020-04-14 09:54:02 -040093 static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff };
Brian Osman5d7759e2020-05-29 09:25:56 -040094 const SkPoint pts[] = {{ 0, 0 }, { 256, 0 }};
Ethan Nicholas58430122020-04-14 09:54:02 -040095
Brian Osman5d7759e2020-05-29 09:25:56 -040096 auto shader = SkGradientShader::MakeLinear(pts, colors, nullptr,
Ethan Nicholas58430122020-04-14 09:54:02 -040097 SK_ARRAY_COUNT(colors),
Brian Osman5d7759e2020-05-29 09:25:56 -040098 SkTileMode::kClamp);
Ethan Nicholas58430122020-04-14 09:54:02 -040099 SkMatrix matrix;
Brian Osman449b1152020-04-15 16:43:00 -0400100 SkSimpleMatrixProvider matrixProvider(matrix);
Ethan Nicholas58430122020-04-14 09:54:02 -0400101 GrColorInfo colorInfo;
Brian Osman449b1152020-04-15 16:43:00 -0400102 GrFPArgs args(ctx, matrixProvider, kHigh_SkFilterQuality, &colorInfo);
Ethan Nicholas58430122020-04-14 09:54:02 -0400103 std::unique_ptr<GrFragmentProcessor> gradientFP = as_SB(shader)->asFragmentProcessor(args);
Brian Salomon25776232020-06-10 12:47:25 -0400104 draw(std::move(gradientFP), 512, 0);
105 gradientFP = as_SB(shader)->asFragmentProcessor(args);
106 draw2(std::move(gradientFP), 768, 0);
Ethan Nicholas58430122020-04-14 09:54:02 -0400107 }
108}