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"; } |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame^] | 31 | bool usesExplicitReturn() const override { return true; } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 32 | |
| 33 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 34 | SkASSERT(false); |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 38 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 39 | bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 40 | |
| 41 | private: |
| 42 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 43 | using INHERITED = GrFragmentProcessor; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | class GLSLSampleMatrixConstantEffect : public GrGLSLFragmentProcessor { |
| 47 | void emitCode(EmitArgs& args) override { |
| 48 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 49 | SkString sample = this->invokeChildWithMatrix(0, args); |
John Stiles | 4bdc121 | 2020-12-14 18:04:13 -0500 | [diff] [blame^] | 50 | fragBuilder->codeAppendf("return %s;\n", sample.c_str()); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 51 | } |
| 52 | }; |
| 53 | |
| 54 | GrGLSLFragmentProcessor* SampleMatrixConstantEffect::onCreateGLSLInstance() const { |
| 55 | return new GLSLSampleMatrixConstantEffect(); |
| 56 | } |
| 57 | |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 58 | DEF_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 Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 65 | GrPaint paint; |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 66 | paint.setColorFragmentProcessor(std::move(fp)); |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 67 | rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty), |
| 68 | SkRect::MakeIWH(256, 256)); |
| 69 | }; |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 70 | 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 Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 73 | paint.setColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 74 | rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty), |
| 75 | SkRect::MakeIWH(256, 256)); |
| 76 | }; |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 77 | |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 78 | { |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 79 | SkBitmap bmp; |
| 80 | GetResourceAsBitmap("images/mandrill_256.png", &bmp); |
| 81 | GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 82 | auto view = maker.view(GrMipmapped::kNo); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 83 | std::unique_ptr<GrFragmentProcessor> imgFP = |
| 84 | GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix()); |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 85 | draw(std::move(imgFP), 0, 0); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 86 | view = maker.view(GrMipmapped::kNo); |
Brian Salomon | 2577623 | 2020-06-10 12:47:25 -0400 | [diff] [blame] | 87 | imgFP = |
| 88 | GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix()); |
| 89 | draw2(std::move(imgFP), 256, 0); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | { |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 93 | static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff }; |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 94 | const SkPoint pts[] = {{ 0, 0 }, { 256, 0 }}; |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 95 | |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 96 | auto shader = SkGradientShader::MakeLinear(pts, colors, nullptr, |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 97 | SK_ARRAY_COUNT(colors), |
Brian Osman | 5d7759e | 2020-05-29 09:25:56 -0400 | [diff] [blame] | 98 | SkTileMode::kClamp); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 99 | SkMatrix matrix; |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 100 | SkSimpleMatrixProvider matrixProvider(matrix); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 101 | GrColorInfo colorInfo; |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 102 | GrFPArgs args(ctx, matrixProvider, kHigh_SkFilterQuality, &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 | } |