Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 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" |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 10 | #include "src/core/SkMatrixProvider.h" |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 11 | #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 | |
| 19 | class SampleMatrixConstantEffect : public GrFragmentProcessor { |
| 20 | public: |
| 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 | |
| 43 | private: |
| 44 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 45 | typedef GrFragmentProcessor INHERITED; |
| 46 | }; |
| 47 | |
| 48 | class 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 | |
| 56 | GrGLSLFragmentProcessor* SampleMatrixConstantEffect::onCreateGLSLInstance() const { |
| 57 | return new GLSLSampleMatrixConstantEffect(); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | DEF_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 Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 89 | SkSimpleMatrixProvider matrixProvider(matrix); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 90 | GrColorInfo colorInfo; |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 91 | GrFPArgs args(ctx, matrixProvider, kHigh_SkFilterQuality, &colorInfo); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 92 | 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 | |