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" |
Kevin Lubick | 677a12f | 2022-03-09 08:23:14 -0500 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkImageInfo.h" |
| 13 | #include "include/core/SkMatrix.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkShader.h" |
| 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
| 19 | #include "include/core/SkTileMode.h" |
| 20 | #include "include/core/SkTypes.h" |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 21 | #include "src/core/SkCanvasPriv.h" |
Kevin Lubick | acdc108 | 2023-06-09 11:05:24 -0400 | [diff] [blame] | 22 | #include "src/gpu/ganesh/GrCanvas.h" |
Greg Daniel | 719239c | 2022-04-07 11:20:24 -0400 | [diff] [blame] | 23 | #include "src/gpu/ganesh/GrCaps.h" |
| 24 | #include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 25 | #include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 26 | #include "src/gpu/ganesh/SkGr.h" |
| 27 | #include "src/gpu/ganesh/SurfaceFillContext.h" |
Greg Daniel | 719239c | 2022-04-07 11:20:24 -0400 | [diff] [blame] | 28 | #include "src/gpu/ganesh/effects/GrSkSLFP.h" |
| 29 | #include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 30 | #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
Kevin Lubick | 8b74188 | 2023-10-06 11:41:38 -0400 | [diff] [blame] | 31 | #include "tools/DecodeUtils.h" |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 32 | #include "tools/Resources.h" |
| 33 | #include "tools/ToolUtils.h" |
| 34 | |
| 35 | class SampleCoordEffect : public GrFragmentProcessor { |
| 36 | public: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 37 | inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0; |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 38 | |
| 39 | SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child) |
| 40 | : INHERITED(CLASS_ID, kNone_OptimizationFlags) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 41 | this->registerChild(std::move(child), SkSL::SampleUsage::Explicit()); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const char* name() const override { return "SampleCoordEffect"; } |
| 45 | |
| 46 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 47 | SkASSERT(false); |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
Greg Daniel | cdeb092 | 2021-12-20 12:37:56 -0500 | [diff] [blame] | 51 | void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {} |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 52 | |
| 53 | bool onIsEqual(const GrFragmentProcessor&) const override { |
| 54 | SkASSERT(false); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | private: |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 59 | std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 60 | using INHERITED = GrFragmentProcessor; |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 61 | }; |
| 62 | |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 63 | std::unique_ptr<GrFragmentProcessor::ProgramImpl> SampleCoordEffect::onMakeProgramImpl() const { |
Brian Salomon | b25560a | 2021-08-10 13:56:13 -0400 | [diff] [blame] | 64 | class Impl : public ProgramImpl { |
| 65 | public: |
| 66 | void emitCode(EmitArgs& args) override { |
| 67 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 68 | SkString s1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)"); |
| 69 | SkString s2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)"); |
| 70 | fragBuilder->codeAppendf("return (%s + %s) / 2;\n", s1.c_str(), s2.c_str()); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | return std::make_unique<Impl>(); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 77 | DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, rContext, canvas, 512, 512, |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 78 | ToolUtils::color_to_565(0xFF66AA99)) { |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 79 | |
Kevin Lubick | acdc108 | 2023-06-09 11:05:24 -0400 | [diff] [blame] | 80 | auto sfc = skgpu::ganesh::TopDeviceSurfaceFillContext(canvas); |
John Stiles | ab7ff17 | 2021-07-30 10:59:25 -0400 | [diff] [blame] | 81 | if (!sfc) { |
| 82 | return; |
| 83 | } |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 84 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 85 | SkBitmap bmp; |
Kevin Lubick | 8b74188 | 2023-10-06 11:41:38 -0400 | [diff] [blame] | 86 | ToolUtils::GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp); |
Aditya Kushwah | fe1bc0a | 2022-06-22 18:13:00 -0700 | [diff] [blame] | 87 | auto view = std::get<0>(GrMakeCachedBitmapProxyView( |
Kevin Lubick | df73d16 | 2023-09-11 11:56:53 -0400 | [diff] [blame] | 88 | rContext, bmp, /*label=*/"FpCoordinateOverride", skgpu::Mipmapped::kNo)); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 89 | if (!view) { |
| 90 | return; |
| 91 | } |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 92 | std::unique_ptr<GrFragmentProcessor> imgFP = |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 93 | GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix()); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 94 | auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP))); |
| 95 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 96 | sfc->fillWithFP(std::move(fp)); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 97 | } |