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" |
| 21 | #include "src/gpu/GrCaps.h" |
| 22 | #include "src/gpu/GrContextPriv.h" |
| 23 | #include "src/gpu/GrCoordTransform.h" |
| 24 | #include "src/gpu/GrFragmentProcessor.h" |
| 25 | #include "src/gpu/GrRenderTargetContext.h" |
| 26 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 27 | #include "src/gpu/effects/GrRRectEffect.h" |
| 28 | #include "src/gpu/effects/GrSkSLFP.h" |
| 29 | #include "src/gpu/ops/GrFillRectOp.h" |
| 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) { |
| 39 | child->setComputeLocalCoordsInVertexShader(false); |
| 40 | this->registerChildProcessor(std::move(child)); |
| 41 | } |
| 42 | |
| 43 | const char* name() const override { return "SampleCoordEffect"; } |
| 44 | |
| 45 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 46 | SkASSERT(false); |
| 47 | return nullptr; |
| 48 | } |
| 49 | |
| 50 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override { |
| 51 | } |
| 52 | |
| 53 | bool onIsEqual(const GrFragmentProcessor&) const override { |
| 54 | SkASSERT(false); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 60 | typedef GrFragmentProcessor INHERITED; |
| 61 | }; |
| 62 | |
| 63 | class GLSLSampleCoordEffect : public GrGLSLFragmentProcessor { |
| 64 | void emitCode(EmitArgs& args) override { |
| 65 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 66 | SkString sample1("sample1"); |
| 67 | SkString sample2("sample2"); |
| 68 | this->invokeChild(0, &sample1, args, "float2(sk_FragCoord.x, sk_FragCoord.y)"); |
| 69 | this->invokeChild(0, &sample2, args, "float2(sk_FragCoord.x, 512 - sk_FragCoord.y)"); |
| 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); |
| 85 | GrProxyProvider* proxyProvider = ctx->priv().proxyProvider(); |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 86 | GrColorType srcColorType = SkColorTypeToGrColorType(bmp.colorType()); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 87 | sk_sp<GrTextureProxy> texture = proxyProvider->createProxyFromBitmap(bmp, GrMipMapped::kNo); |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 88 | std::unique_ptr<GrFragmentProcessor> imgFP = GrSimpleTextureEffect::Make(texture, srcColorType, |
| 89 | SkMatrix()); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 90 | auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP))); |
| 91 | |
| 92 | GrPaint grPaint; |
| 93 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
| 94 | |
| 95 | rtCtx->priv().testingOnly_addDrawOp(GrFillRectOp::MakeNonAARect(ctx, |
| 96 | std::move(grPaint), |
| 97 | SkMatrix::I(), |
| 98 | bounds)); |
| 99 | } |