blob: 7a1670bbcbfac7f0d83d625c9480beecf2935fcd [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"
Greg Daniel6f5441a2020-01-28 17:02:49 -050020#include "src/gpu/GrBitmapTextureMaker.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040021#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040022#include "src/gpu/GrDirectContextPriv.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040023#include "src/gpu/GrFragmentProcessor.h"
24#include "src/gpu/GrRenderTargetContext.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040025#include "src/gpu/effects/GrRRectEffect.h"
26#include "src/gpu/effects/GrSkSLFP.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050027#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040028#include "src/gpu/ops/GrFillRectOp.h"
29#include "tools/Resources.h"
30#include "tools/ToolUtils.h"
31
32class SampleCoordEffect : public GrFragmentProcessor {
33public:
34 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
35
36 SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
37 : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
Brian Osman1298bc42020-06-30 13:39:35 -040038 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
Ethan Nicholasd4efe682019-08-29 16:10:13 -040039 }
40
41 const char* name() const override { return "SampleCoordEffect"; }
42
43 std::unique_ptr<GrFragmentProcessor> clone() const override {
44 SkASSERT(false);
45 return nullptr;
46 }
47
48 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {
49 }
50
51 bool onIsEqual(const GrFragmentProcessor&) const override {
52 SkASSERT(false);
53 return true;
54 }
55
56private:
57 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
John Stiles7571f9e2020-09-02 22:42:33 -040058 using INHERITED = GrFragmentProcessor;
Ethan Nicholasd4efe682019-08-29 16:10:13 -040059};
60
61class GLSLSampleCoordEffect : public GrGLSLFragmentProcessor {
62 void emitCode(EmitArgs& args) override {
63 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -050064 SkString sample1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)");
65 SkString sample2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)");
Ethan Nicholasd4efe682019-08-29 16:10:13 -040066 fragBuilder->codeAppendf("%s = (%s + %s) / 2;\n", args.fOutputColor, sample1.c_str(),
67 sample2.c_str());
68 }
69};
70
71GrGLSLFragmentProcessor* SampleCoordEffect::onCreateGLSLInstance() const {
72 return new GLSLSampleCoordEffect();
73}
74
75DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, ctx, rtCtx, canvas, 512, 512,
76 ToolUtils::color_to_565(0xFF66AA99)) {
77 SkRect bounds = SkRect::MakeIWH(512, 512);
78
79 SkBitmap bmp;
80 GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
Brian Salomonbc074a62020-03-18 10:06:13 -040081 GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
Brian Salomon7e67dca2020-07-21 09:27:25 -040082 auto view = maker.view(GrMipmapped::kNo);
Greg Daniel85da3362020-03-09 15:18:35 -040083 if (!view) {
84 return;
85 }
Brian Salomonfc118442019-11-22 19:09:27 -050086 std::unique_ptr<GrFragmentProcessor> imgFP =
Greg Danield2ccbb52020-02-05 10:45:39 -050087 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
Ethan Nicholasd4efe682019-08-29 16:10:13 -040088 auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
89
90 GrPaint grPaint;
John Stiles41d91b62020-07-21 14:39:40 -040091 grPaint.setCoverageFragmentProcessor(std::move(fp));
Ethan Nicholasd4efe682019-08-29 16:10:13 -040092
Brian Salomon70fe17e2020-11-30 14:33:58 -050093 rtCtx->addDrawOp(GrFillRectOp::MakeNonAARect(ctx, std::move(grPaint), SkMatrix::I(), bounds));
Ethan Nicholasd4efe682019-08-29 16:10:13 -040094}