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/SkBitmap.h"
10 #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"
21 #include "src/core/SkCanvasPriv.h"
22 #include "src/gpu/ganesh/GrCanvas.h"
23 #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"
28 #include "src/gpu/ganesh/effects/GrSkSLFP.h"
29 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
30 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
31 #include "tools/DecodeUtils.h"
32 #include "tools/Resources.h"
33 #include "tools/ToolUtils.h"
34
35 class SampleCoordEffect : public GrFragmentProcessor {
36 public:
37 inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
38
SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)39 SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
40 : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
41 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
42 }
43
name() const44 const char* name() const override { return "SampleCoordEffect"; }
45
clone() const46 std::unique_ptr<GrFragmentProcessor> clone() const override {
47 SkASSERT(false);
48 return nullptr;
49 }
50
onAddToKey(const GrShaderCaps &,skgpu::KeyBuilder *) const51 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
52
onIsEqual(const GrFragmentProcessor &) const53 bool onIsEqual(const GrFragmentProcessor&) const override {
54 SkASSERT(false);
55 return true;
56 }
57
58 private:
59 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
60 using INHERITED = GrFragmentProcessor;
61 };
62
onMakeProgramImpl() const63 std::unique_ptr<GrFragmentProcessor::ProgramImpl> SampleCoordEffect::onMakeProgramImpl() const {
64 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>();
75 }
76
77 DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, rContext, canvas, 512, 512,
78 ToolUtils::color_to_565(0xFF66AA99)) {
79
80 auto sfc = skgpu::ganesh::TopDeviceSurfaceFillContext(canvas);
81 if (!sfc) {
82 return;
83 }
84
85 SkBitmap bmp;
86 ToolUtils::GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
87 auto view = std::get<0>(GrMakeCachedBitmapProxyView(
88 rContext, bmp, /*label=*/"FpCoordinateOverride", skgpu::Mipmapped::kNo));
89 if (!view) {
90 return;
91 }
92 std::unique_ptr<GrFragmentProcessor> imgFP =
93 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
94 auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
95
96 sfc->fillWithFP(std::move(fp));
97 }
98