blob: dc445361fa117d62c51aa2d3aed26e7eaea0bd6f [file] [log] [blame]
Ethan Nicholasd4efe682019-08-29 16:10:13 -04001/*
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"
Greg Daniel6f5441a2020-01-28 17:02:49 -050021#include "src/gpu/GrBitmapTextureMaker.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040022#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrCoordTransform.h"
25#include "src/gpu/GrFragmentProcessor.h"
26#include "src/gpu/GrRenderTargetContext.h"
27#include "src/gpu/GrRenderTargetContextPriv.h"
28#include "src/gpu/effects/GrRRectEffect.h"
29#include "src/gpu/effects/GrSkSLFP.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050030#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040031#include "src/gpu/ops/GrFillRectOp.h"
32#include "tools/Resources.h"
33#include "tools/ToolUtils.h"
34
35class SampleCoordEffect : public GrFragmentProcessor {
36public:
37 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
38
39 SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
40 : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
Michael Ludwig9aba6252020-06-22 14:46:36 -040041 this->registerExplicitlySampledChild(std::move(child));
Ethan Nicholasd4efe682019-08-29 16:10:13 -040042 }
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
51 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {
52 }
53
54 bool onIsEqual(const GrFragmentProcessor&) const override {
55 SkASSERT(false);
56 return true;
57 }
58
59private:
60 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
61 typedef GrFragmentProcessor INHERITED;
62};
63
64class GLSLSampleCoordEffect : public GrGLSLFragmentProcessor {
65 void emitCode(EmitArgs& args) override {
66 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -050067 SkString sample1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)");
68 SkString sample2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)");
Ethan Nicholasd4efe682019-08-29 16:10:13 -040069 fragBuilder->codeAppendf("%s = (%s + %s) / 2;\n", args.fOutputColor, sample1.c_str(),
70 sample2.c_str());
71 }
72};
73
74GrGLSLFragmentProcessor* SampleCoordEffect::onCreateGLSLInstance() const {
75 return new GLSLSampleCoordEffect();
76}
77
78DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, ctx, rtCtx, canvas, 512, 512,
79 ToolUtils::color_to_565(0xFF66AA99)) {
80 SkRect bounds = SkRect::MakeIWH(512, 512);
81
82 SkBitmap bmp;
83 GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
Brian Salomonbc074a62020-03-18 10:06:13 -040084 GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
Brian Salomonecbb0fb2020-02-28 18:07:32 -050085 auto view = maker.view(GrMipMapped::kNo);
Greg Daniel85da3362020-03-09 15:18:35 -040086 if (!view) {
87 return;
88 }
Brian Salomonfc118442019-11-22 19:09:27 -050089 std::unique_ptr<GrFragmentProcessor> imgFP =
Greg Danield2ccbb52020-02-05 10:45:39 -050090 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
Ethan Nicholasd4efe682019-08-29 16:10:13 -040091 auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
92
93 GrPaint grPaint;
94 grPaint.addCoverageFragmentProcessor(std::move(fp));
95
96 rtCtx->priv().testingOnly_addDrawOp(GrFillRectOp::MakeNonAARect(ctx,
97 std::move(grPaint),
98 SkMatrix::I(),
99 bounds));
100}