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" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDirectContextPriv.h" |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "src/gpu/ops/GrFillRectOp.h" |
| 15 | #include "tools/Resources.h" |
| 16 | |
| 17 | class SampleMatrixConstantEffect : public GrFragmentProcessor { |
| 18 | public: |
| 19 | static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 1; |
| 20 | |
| 21 | SampleMatrixConstantEffect(std::unique_ptr<GrFragmentProcessor> child) |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 22 | : INHERITED(CLASS_ID, kNone_OptimizationFlags) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 23 | this->registerChild(std::move(child), |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 24 | SkSL::SampleUsage::UniformMatrix( |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 25 | "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 Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 28 | } |
| 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 Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 37 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 38 | bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 42 | using INHERITED = GrFragmentProcessor; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | class GLSLSampleMatrixConstantEffect : public GrGLSLFragmentProcessor { |
| 46 | void emitCode(EmitArgs& args) override { |
| 47 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 48 | SkString sample = this->invokeChildWithMatrix(0, args); |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame] | 49 | fragBuilder->codeAppendf("return %s;\n", sample.c_str()); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
| 53 | GrGLSLFragmentProcessor* SampleMatrixConstantEffect::onCreateGLSLInstance() const { |
| 54 | return new GLSLSampleMatrixConstantEffect(); |
| 55 | } |
| 56 | |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 57 | DEF_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 Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 64 | GrPaint paint; |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 65 | paint.setColorFragmentProcessor(std::move(fp)); |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 66 | rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty), |
| 67 | SkRect::MakeIWH(256, 256)); |
| 68 | }; |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 69 | 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 Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 72 | paint.setColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 73 | rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty), |
| 74 | SkRect::MakeIWH(256, 256)); |
| 75 | }; |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 76 | |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 77 | { |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 78 | SkBitmap bmp; |
| 79 | GetResourceAsBitmap("images/mandrill_256.png", &bmp); |
| 80 | GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 81 | auto view = maker.view(GrMipmapped::kNo); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 82 | std::unique_ptr<GrFragmentProcessor> imgFP = |
| 83 | GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix()); |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 84 | draw(std::move(imgFP), 0, 0); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 85 | view = maker.view(GrMipmapped::kNo); |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 86 | imgFP = |
| 87 | GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix()); |
| 88 | draw2(std::move(imgFP), 256, 0); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | { |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 92 | static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff }; |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 93 | const SkPoint pts[] = {{ 0, 0 }, { 256, 0 }}; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 94 | |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 95 | auto shader = SkGradientShader::MakeLinear(pts, colors, nullptr, |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 96 | SK_ARRAY_COUNT(colors), |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 97 | SkTileMode::kClamp); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 98 | SkMatrix matrix; |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 99 | SkSimpleMatrixProvider matrixProvider(matrix); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 100 | GrColorInfo colorInfo; |
Mike Reed | f3ac2af | 2021-02-05 12:55:38 -0500 | [diff] [blame] | 101 | GrFPArgs args(ctx, matrixProvider, SkSamplingOptions(SkCubicResampler::Mitchell()), |
| 102 | &colorInfo); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 103 | std::unique_ptr<GrFragmentProcessor> gradientFP = as_SB(shader)->asFragmentProcessor(args); |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 104 | draw(std::move(gradientFP), 512, 0); |
| 105 | gradientFP = as_SB(shader)->asFragmentProcessor(args); |
| 106 | draw2(std::move(gradientFP), 768, 0); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 107 | } |
| 108 | } |