Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -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/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkImageInfo.h" |
| 12 | #include "include/core/SkMatrix.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkShader.h" |
| 16 | #include "include/core/SkSize.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/core/SkTileMode.h" |
| 19 | #include "include/core/SkTypes.h" |
| 20 | #include "include/gpu/GrContext.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrBitmapTextureMaker.h" |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrCaps.h" |
| 23 | #include "src/gpu/GrContextPriv.h" |
| 24 | #include "src/gpu/GrCoordTransform.h" |
| 25 | #include "src/gpu/GrFragmentProcessor.h" |
| 26 | #include "src/gpu/GrRenderTargetContext.h" |
| 27 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 28 | #include "src/gpu/effects/GrRRectEffect.h" |
| 29 | #include "src/gpu/effects/GrSkSLFP.h" |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 30 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 31 | #include "src/gpu/ops/GrFillRectOp.h" |
| 32 | #include "tools/Resources.h" |
| 33 | #include "tools/ToolUtils.h" |
| 34 | |
| 35 | class SampleCoordEffect : public GrFragmentProcessor { |
| 36 | public: |
| 37 | static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0; |
| 38 | |
| 39 | SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child) |
| 40 | : INHERITED(CLASS_ID, kNone_OptimizationFlags) { |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 41 | child->setSampledWithExplicitCoords(true); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 42 | this->registerChildProcessor(std::move(child)); |
| 43 | } |
| 44 | |
| 45 | const char* name() const override { return "SampleCoordEffect"; } |
| 46 | |
| 47 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 48 | SkASSERT(false); |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override { |
| 53 | } |
| 54 | |
| 55 | bool onIsEqual(const GrFragmentProcessor&) const override { |
| 56 | SkASSERT(false); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 62 | typedef GrFragmentProcessor INHERITED; |
| 63 | }; |
| 64 | |
| 65 | class GLSLSampleCoordEffect : public GrGLSLFragmentProcessor { |
| 66 | void emitCode(EmitArgs& args) override { |
| 67 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 68 | SkString sample1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)"); |
| 69 | SkString sample2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)"); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 70 | fragBuilder->codeAppendf("%s = (%s + %s) / 2;\n", args.fOutputColor, sample1.c_str(), |
| 71 | sample2.c_str()); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | GrGLSLFragmentProcessor* SampleCoordEffect::onCreateGLSLInstance() const { |
| 76 | return new GLSLSampleCoordEffect(); |
| 77 | } |
| 78 | |
| 79 | DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, ctx, rtCtx, canvas, 512, 512, |
| 80 | ToolUtils::color_to_565(0xFF66AA99)) { |
| 81 | SkRect bounds = SkRect::MakeIWH(512, 512); |
| 82 | |
| 83 | SkBitmap bmp; |
| 84 | GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 85 | GrBitmapTextureMaker maker(ctx, bmp); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 86 | auto view = maker.view(GrMipMapped::kNo); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame^] | 87 | if (!view) { |
| 88 | return; |
| 89 | } |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 90 | std::unique_ptr<GrFragmentProcessor> imgFP = |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 91 | GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix()); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 92 | auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP))); |
| 93 | |
| 94 | GrPaint grPaint; |
| 95 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
| 96 | |
| 97 | rtCtx->priv().testingOnly_addDrawOp(GrFillRectOp::MakeNonAARect(ctx, |
| 98 | std::move(grPaint), |
| 99 | SkMatrix::I(), |
| 100 | bounds)); |
| 101 | } |