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/gpu/GrBitmapTextureMaker.h"
21 #include "src/gpu/GrCaps.h"
22 #include "src/gpu/GrDirectContextPriv.h"
23 #include "src/gpu/GrFragmentProcessor.h"
24 #include "src/gpu/GrRenderTargetContext.h"
25 #include "src/gpu/GrRenderTargetContextPriv.h"
26 #include "src/gpu/effects/GrRRectEffect.h"
27 #include "src/gpu/effects/GrSkSLFP.h"
28 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.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 
SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)37     SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
38         : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
39         this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
40     }
41 
name() const42     const char* name() const override { return "SampleCoordEffect"; }
43 
clone() const44     std::unique_ptr<GrFragmentProcessor> clone() const override {
45         SkASSERT(false);
46         return nullptr;
47     }
48 
onGetGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder *) const49     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {
50     }
51 
onIsEqual(const GrFragmentProcessor &) const52     bool onIsEqual(const GrFragmentProcessor&) const override {
53         SkASSERT(false);
54         return true;
55     }
56 
57 private:
58     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
59     using INHERITED = GrFragmentProcessor;
60 };
61 
62 class GLSLSampleCoordEffect : public GrGLSLFragmentProcessor {
emitCode(EmitArgs & args)63     void emitCode(EmitArgs& args) override {
64         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
65         SkString sample1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)");
66         SkString sample2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)");
67         fragBuilder->codeAppendf("%s = (%s + %s) / 2;\n", args.fOutputColor, sample1.c_str(),
68                                  sample2.c_str());
69     }
70 };
71 
onCreateGLSLInstance() const72 GrGLSLFragmentProcessor* SampleCoordEffect::onCreateGLSLInstance() const {
73     return new GLSLSampleCoordEffect();
74 }
75 
76 DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, ctx, rtCtx, canvas, 512, 512,
77                      ToolUtils::color_to_565(0xFF66AA99)) {
78     SkRect bounds = SkRect::MakeIWH(512, 512);
79 
80     SkBitmap bmp;
81     GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
82     GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
83     auto view = maker.view(GrMipmapped::kNo);
84     if (!view) {
85         return;
86     }
87     std::unique_ptr<GrFragmentProcessor> imgFP =
88             GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
89     auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
90 
91     GrPaint grPaint;
92     grPaint.setCoverageFragmentProcessor(std::move(fp));
93 
94     rtCtx->priv().testingOnly_addDrawOp(GrFillRectOp::MakeNonAARect(ctx,
95                                                                     std::move(grPaint),
96                                                                     SkMatrix::I(),
97                                                                     bounds));
98 }
99