blob: ae8d1fb9222b1ad365330cc622095a459e3aa572 [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"; }
31
32 std::unique_ptr<GrFragmentProcessor> clone() const override {
33 SkASSERT(false);
34 return nullptr;
35 }
36
Brian Osman5d7759e2020-05-29 09:25:56 -040037 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
38 bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
Ethan Nicholas58430122020-04-14 09:54:02 -040039
40private:
41 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
John Stiles7571f9e2020-09-02 22:42:33 -040042 using INHERITED = GrFragmentProcessor;
Ethan Nicholas58430122020-04-14 09:54:02 -040043};
44
45class GLSLSampleMatrixConstantEffect : public GrGLSLFragmentProcessor {
46 void emitCode(EmitArgs& args) override {
47 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Michael Ludwige88320b2020-06-24 09:04:56 -040048 SkString sample = this->invokeChildWithMatrix(0, args);
John Stiles4bdc1212020-12-14 18:04:13 -050049 fragBuilder->codeAppendf("return %s;\n", sample.c_str());
Ethan Nicholas58430122020-04-14 09:54:02 -040050 }
51};
52
53GrGLSLFragmentProcessor* SampleMatrixConstantEffect::onCreateGLSLInstance() const {
54 return new GLSLSampleMatrixConstantEffect();
55}
56
Brian Salomon25776232020-06-10 12:47:25 -040057DEF_SIMPLE_GPU_GM(sample_matrix_constant, ctx, rtCtx, canvas, 1024, 256) {
58 auto wrap = [](std::unique_ptr<GrFragmentProcessor> baseFP) {
59 return std::unique_ptr<GrFragmentProcessor>(
60 new SampleMatrixConstantEffect(std::move(baseFP)));
61 };
62 auto draw = [rtCtx, &wrap](std::unique_ptr<GrFragmentProcessor> baseFP, int tx, int ty) {
63 auto fp = wrap(std::move(baseFP));
Brian Osman5d7759e2020-05-29 09:25:56 -040064 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -040065 paint.setColorFragmentProcessor(std::move(fp));
Brian Osman5d7759e2020-05-29 09:25:56 -040066 rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty),
67 SkRect::MakeIWH(256, 256));
68 };
Brian Salomon25776232020-06-10 12:47:25 -040069 auto draw2 = [rtCtx, &wrap](std::unique_ptr<GrFragmentProcessor> baseFP, int tx, int ty) {
70 auto fp = wrap(wrap(std::move(baseFP)));
71 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -040072 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon25776232020-06-10 12:47:25 -040073 rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty),
74 SkRect::MakeIWH(256, 256));
75 };
Brian Osman5d7759e2020-05-29 09:25:56 -040076
Ethan Nicholas58430122020-04-14 09:54:02 -040077 {
Ethan Nicholas58430122020-04-14 09:54:02 -040078 SkBitmap bmp;
79 GetResourceAsBitmap("images/mandrill_256.png", &bmp);
80 GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
Brian Salomon7e67dca2020-07-21 09:27:25 -040081 auto view = maker.view(GrMipmapped::kNo);
Ethan Nicholas58430122020-04-14 09:54:02 -040082 std::unique_ptr<GrFragmentProcessor> imgFP =
83 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
Brian Osman5d7759e2020-05-29 09:25:56 -040084 draw(std::move(imgFP), 0, 0);
Brian Salomon7e67dca2020-07-21 09:27:25 -040085 view = maker.view(GrMipmapped::kNo);
Brian Salomon25776232020-06-10 12:47:25 -040086 imgFP =
87 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
88 draw2(std::move(imgFP), 256, 0);
Ethan Nicholas58430122020-04-14 09:54:02 -040089 }
90
91 {
Ethan Nicholas58430122020-04-14 09:54:02 -040092 static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff };
Brian Osman5d7759e2020-05-29 09:25:56 -040093 const SkPoint pts[] = {{ 0, 0 }, { 256, 0 }};
Ethan Nicholas58430122020-04-14 09:54:02 -040094
Brian Osman5d7759e2020-05-29 09:25:56 -040095 auto shader = SkGradientShader::MakeLinear(pts, colors, nullptr,
Ethan Nicholas58430122020-04-14 09:54:02 -040096 SK_ARRAY_COUNT(colors),
Brian Osman5d7759e2020-05-29 09:25:56 -040097 SkTileMode::kClamp);
Ethan Nicholas58430122020-04-14 09:54:02 -040098 SkMatrix matrix;
Brian Osman449b1152020-04-15 16:43:00 -040099 SkSimpleMatrixProvider matrixProvider(matrix);
Ethan Nicholas58430122020-04-14 09:54:02 -0400100 GrColorInfo colorInfo;
Mike Reedf3ac2af2021-02-05 12:55:38 -0500101 GrFPArgs args(ctx, matrixProvider, SkSamplingOptions(SkCubicResampler::Mitchell()),
102 &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}