blob: 5abcfcb5f3783dc9e109248b127477b35fb76227 [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"
Kevin Lubick677a12f2022-03-09 08:23:14 -05009#include "include/core/SkBitmap.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040010#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkImageInfo.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTileMode.h"
20#include "include/core/SkTypes.h"
Robert Phillips7a0d3c32021-07-21 15:39:51 -040021#include "src/core/SkCanvasPriv.h"
Kevin Lubickacdc1082023-06-09 11:05:24 -040022#include "src/gpu/ganesh/GrCanvas.h"
Greg Daniel719239c2022-04-07 11:20:24 -040023#include "src/gpu/ganesh/GrCaps.h"
24#include "src/gpu/ganesh/GrDirectContextPriv.h"
25#include "src/gpu/ganesh/GrFragmentProcessor.h"
26#include "src/gpu/ganesh/SkGr.h"
27#include "src/gpu/ganesh/SurfaceFillContext.h"
Greg Daniel719239c2022-04-07 11:20:24 -040028#include "src/gpu/ganesh/effects/GrSkSLFP.h"
29#include "src/gpu/ganesh/effects/GrTextureEffect.h"
30#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040031#include "tools/DecodeUtils.h"
Ethan Nicholasd4efe682019-08-29 16:10:13 -040032#include "tools/Resources.h"
33#include "tools/ToolUtils.h"
34
35class SampleCoordEffect : public GrFragmentProcessor {
36public:
Brian Salomon9fa47cc2021-10-08 18:48:26 -040037 inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
Ethan Nicholasd4efe682019-08-29 16:10:13 -040038
39 SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
40 : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
Brian Osman1298bc42020-06-30 13:39:35 -040041 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
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
Greg Danielcdeb0922021-12-20 12:37:56 -050051 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
Ethan Nicholasd4efe682019-08-29 16:10:13 -040052
53 bool onIsEqual(const GrFragmentProcessor&) const override {
54 SkASSERT(false);
55 return true;
56 }
57
58private:
Brian Salomon3176e862021-08-09 11:23:04 -040059 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
John Stiles7571f9e2020-09-02 22:42:33 -040060 using INHERITED = GrFragmentProcessor;
Ethan Nicholasd4efe682019-08-29 16:10:13 -040061};
62
Brian Salomon3176e862021-08-09 11:23:04 -040063std::unique_ptr<GrFragmentProcessor::ProgramImpl> SampleCoordEffect::onMakeProgramImpl() const {
Brian Salomonb25560a2021-08-10 13:56:13 -040064 class Impl : public ProgramImpl {
65 public:
66 void emitCode(EmitArgs& args) override {
67 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
68 SkString s1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)");
69 SkString s2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)");
70 fragBuilder->codeAppendf("return (%s + %s) / 2;\n", s1.c_str(), s2.c_str());
71 }
72 };
73
74 return std::make_unique<Impl>();
Ethan Nicholasd4efe682019-08-29 16:10:13 -040075}
76
Robert Phillips7a0d3c32021-07-21 15:39:51 -040077DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, rContext, canvas, 512, 512,
Ethan Nicholasd4efe682019-08-29 16:10:13 -040078 ToolUtils::color_to_565(0xFF66AA99)) {
Robert Phillips7a0d3c32021-07-21 15:39:51 -040079
Kevin Lubickacdc1082023-06-09 11:05:24 -040080 auto sfc = skgpu::ganesh::TopDeviceSurfaceFillContext(canvas);
John Stilesab7ff172021-07-30 10:59:25 -040081 if (!sfc) {
82 return;
83 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -040084
Ethan Nicholasd4efe682019-08-29 16:10:13 -040085 SkBitmap bmp;
Kevin Lubick8b741882023-10-06 11:41:38 -040086 ToolUtils::GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
Aditya Kushwahfe1bc0a2022-06-22 18:13:00 -070087 auto view = std::get<0>(GrMakeCachedBitmapProxyView(
Kevin Lubickdf73d162023-09-11 11:56:53 -040088 rContext, bmp, /*label=*/"FpCoordinateOverride", skgpu::Mipmapped::kNo));
Greg Daniel85da3362020-03-09 15:18:35 -040089 if (!view) {
90 return;
91 }
Brian Salomonfc118442019-11-22 19:09:27 -050092 std::unique_ptr<GrFragmentProcessor> imgFP =
Greg Danield2ccbb52020-02-05 10:45:39 -050093 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
Ethan Nicholasd4efe682019-08-29 16:10:13 -040094 auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
95
Robert Phillips7a0d3c32021-07-21 15:39:51 -040096 sfc->fillWithFP(std::move(fp));
Ethan Nicholasd4efe682019-08-29 16:10:13 -040097}