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 "src/core/SkCanvasPriv.h"
21#include "src/gpu/GrCaps.h"
22#include "src/gpu/GrDirectContextPriv.h"
23#include "src/gpu/GrFragmentProcessor.h"
24#include "src/gpu/SkGr.h"
25#include "src/gpu/SurfaceFillContext.h"
26#include "src/gpu/effects/GrRRectEffect.h"
27#include "src/gpu/effects/GrSkSLFP.h"
28#include "src/gpu/effects/GrTextureEffect.h"
29#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
32
33class SampleCoordEffect : public GrFragmentProcessor {
34public:
35    inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
36
37    SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
38        : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
39        this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
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
49    void onAddToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
50
51    bool onIsEqual(const GrFragmentProcessor&) const override {
52        SkASSERT(false);
53        return true;
54    }
55
56private:
57    std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
58    using INHERITED = GrFragmentProcessor;
59};
60
61std::unique_ptr<GrFragmentProcessor::ProgramImpl> SampleCoordEffect::onMakeProgramImpl() const {
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>();
73}
74
75DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, rContext, canvas, 512, 512,
76                     ToolUtils::color_to_565(0xFF66AA99)) {
77
78    auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas);
79    if (!sfc) {
80        return;
81    }
82
83    SkBitmap bmp;
84    GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
85    auto view = std::get<0>(GrMakeCachedBitmapProxyView(rContext, bmp, GrMipmapped::kNo));
86    if (!view) {
87        return;
88    }
89    std::unique_ptr<GrFragmentProcessor> imgFP =
90            GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
91    auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
92
93    sfc->fillWithFP(std::move(fp));
94}
95