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