blob: 5f3dbfe07a2e506b98d95b36526e1daa2bdce997 [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"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040020#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040022#include "src/gpu/GrFragmentProcessor.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050023#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomon27c42022021-04-28 12:39:21 -040024#include "src/gpu/SkGr.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040025#include "src/gpu/effects/GrRRectEffect.h"
26#include "src/gpu/effects/GrSkSLFP.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040027#include "src/gpu/effects/GrTextureEffect.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050028#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040029#include "src/gpu/ops/GrFillRectOp.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
32
33class SampleCoordEffect : public GrFragmentProcessor {
34public:
35 static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
36
37 SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
38 : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
Brian Osman1298bc42020-06-30 13:39:35 -040039 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
Ethan Nicholasd4efe682019-08-29 16:10:13 -040040 }
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 onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {
50 }
51
52 bool onIsEqual(const GrFragmentProcessor&) const override {
53 SkASSERT(false);
54 return true;
55 }
56
57private:
Brian Salomon18ab2032021-02-23 10:07:05 -050058 std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override;
John Stiles7571f9e2020-09-02 22:42:33 -040059 using INHERITED = GrFragmentProcessor;
Ethan Nicholasd4efe682019-08-29 16:10:13 -040060};
61
62class GLSLSampleCoordEffect : public GrGLSLFragmentProcessor {
63 void emitCode(EmitArgs& args) override {
64 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osman978693c2020-01-24 14:52:10 -050065 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)");
John Stiles4bdc1212020-12-14 18:04:13 -050067 fragBuilder->codeAppendf("return (%s + %s) / 2;\n", sample1.c_str(), sample2.c_str());
Ethan Nicholasd4efe682019-08-29 16:10:13 -040068 }
69};
70
Brian Salomon18ab2032021-02-23 10:07:05 -050071std::unique_ptr<GrGLSLFragmentProcessor> SampleCoordEffect::onMakeProgramImpl() const {
72 return std::make_unique<GLSLSampleCoordEffect>();
Ethan Nicholasd4efe682019-08-29 16:10:13 -040073}
74
John Stiles0fbc6a32021-06-04 14:40:57 -040075DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, ctx, sdCtx, canvas, 512, 512,
Ethan Nicholasd4efe682019-08-29 16:10:13 -040076 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 Salomon27c42022021-04-28 12:39:21 -040081 auto view = std::get<0>(GrMakeCachedBitmapProxyView(ctx, bmp, GrMipmapped::kNo));
Greg Daniel85da3362020-03-09 15:18:35 -040082 if (!view) {
83 return;
84 }
Brian Salomonfc118442019-11-22 19:09:27 -050085 std::unique_ptr<GrFragmentProcessor> imgFP =
Greg Danield2ccbb52020-02-05 10:45:39 -050086 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
Ethan Nicholasd4efe682019-08-29 16:10:13 -040087 auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
88
89 GrPaint grPaint;
John Stiles41d91b62020-07-21 14:39:40 -040090 grPaint.setCoverageFragmentProcessor(std::move(fp));
Ethan Nicholasd4efe682019-08-29 16:10:13 -040091
John Stiles0fbc6a32021-06-04 14:40:57 -040092 sdCtx->addDrawOp(GrFillRectOp::MakeNonAARect(ctx, std::move(grPaint), SkMatrix::I(), bounds));
Ethan Nicholasd4efe682019-08-29 16:10:13 -040093}