1/*
2 * Copyright 2018 Google Inc.
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/**************************************************************************************************
9 *** This file was autogenerated from GrMagnifierEffect.fp; do not modify.
10 **************************************************************************************************/
11#include "GrMagnifierEffect.h"
12
13#include "src/gpu/GrTexture.h"
14#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
15#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
17#include "src/sksl/SkSLCPP.h"
18#include "src/sksl/SkSLUtil.h"
19class GrGLSLMagnifierEffect : public GrGLSLFragmentProcessor {
20public:
21 GrGLSLMagnifierEffect() {}
22 void emitCode(EmitArgs& args) override {
23 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24 const GrMagnifierEffect& _outer = args.fFp.cast<GrMagnifierEffect>();
25 (void)_outer;
26 auto bounds = _outer.bounds;
27 (void)bounds;
28 auto srcRect = _outer.srcRect;
29 (void)srcRect;
30 auto xInvZoom = _outer.xInvZoom;
31 (void)xInvZoom;
32 auto yInvZoom = _outer.yInvZoom;
33 (void)yInvZoom;
34 auto xInvInset = _outer.xInvInset;
35 (void)xInvInset;
36 auto yInvInset = _outer.yInvInset;
37 (void)yInvInset;
38 boundsUniformVar = args.fUniformHandler->addUniform(
39 &_outer, kFragment_GrShaderFlag, kFloat4_GrSLType, "boundsUniform");
40 xInvZoomVar = args.fUniformHandler->addUniform(
41 &_outer, kFragment_GrShaderFlag, kFloat_GrSLType, "xInvZoom");
42 yInvZoomVar = args.fUniformHandler->addUniform(
43 &_outer, kFragment_GrShaderFlag, kFloat_GrSLType, "yInvZoom");
44 xInvInsetVar = args.fUniformHandler->addUniform(
45 &_outer, kFragment_GrShaderFlag, kFloat_GrSLType, "xInvInset");
46 yInvInsetVar = args.fUniformHandler->addUniform(
47 &_outer, kFragment_GrShaderFlag, kFloat_GrSLType, "yInvInset");
48 offsetVar = args.fUniformHandler->addUniform(
49 &_outer, kFragment_GrShaderFlag, kHalf2_GrSLType, "offset");
50 SkString sk_TransformedCoords2D_0 =
51 fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
52 fragBuilder->codeAppendf(
53 "float2 coord = %s;\nfloat2 zoom_coord = float2(%s) + coord * float2(%s, "
54 "%s);\nfloat2 delta = (coord - %s.xy) * %s.zw;\ndelta = min(delta, "
55 "float2(half2(1.0, 1.0)) - delta);\ndelta *= float2(%s, %s);\nfloat weight = "
56 "0.0;\nif (delta.x < 2.0 && delta.y < 2.0) {\n delta = float2(half2(2.0, 2.0)) "
57 "- delta;\n float dist = length(delta);\n dist = max(2.0 - dist, 0.0);\n "
58 "weight = min(dist * dist, 1.0);\n} else {\n float2 delta_squared = delta * "
59 "delta;\n weight = min(min(delta_squared.x, delta_square",
60 sk_TransformedCoords2D_0.c_str(),
61 args.fUniformHandler->getUniformCStr(offsetVar),
62 args.fUniformHandler->getUniformCStr(xInvZoomVar),
63 args.fUniformHandler->getUniformCStr(yInvZoomVar),
64 args.fUniformHandler->getUniformCStr(boundsUniformVar),
65 args.fUniformHandler->getUniformCStr(boundsUniformVar),
66 args.fUniformHandler->getUniformCStr(xInvInsetVar),
67 args.fUniformHandler->getUniformCStr(yInvInsetVar));
68 fragBuilder->codeAppendf(
69 "d.y), 1.0);\n}\n%s = sample(%s, mix(coord, zoom_coord, weight)).%s;\n",
70 args.fOutputColor,
71 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
72 fragBuilder->getProgramBuilder()
73 ->samplerSwizzle(args.fTexSamplers[0])
74 .asString()
75 .c_str());
76 }
77
78private:
79 void onSetData(const GrGLSLProgramDataManager& pdman,
80 const GrFragmentProcessor& _proc) override {
81 const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>();
82 {
83 pdman.set1f(xInvZoomVar, (_outer.xInvZoom));
84 pdman.set1f(yInvZoomVar, (_outer.yInvZoom));
85 pdman.set1f(xInvInsetVar, (_outer.xInvInset));
86 pdman.set1f(yInvInsetVar, (_outer.yInvInset));
87 }
88 const GrSurfaceProxyView& srcView = _outer.textureSampler(0).view();
89 GrTexture& src = *srcView.proxy()->peekTexture();
90 (void)src;
91 auto bounds = _outer.bounds;
92 (void)bounds;
93 UniformHandle& boundsUniform = boundsUniformVar;
94 (void)boundsUniform;
95 auto srcRect = _outer.srcRect;
96 (void)srcRect;
97 UniformHandle& xInvZoom = xInvZoomVar;
98 (void)xInvZoom;
99 UniformHandle& yInvZoom = yInvZoomVar;
100 (void)yInvZoom;
101 UniformHandle& xInvInset = xInvInsetVar;
102 (void)xInvInset;
103 UniformHandle& yInvInset = yInvInsetVar;
104 (void)yInvInset;
105 UniformHandle& offset = offsetVar;
106 (void)offset;
107
108 SkScalar invW = 1.0f / src.width();
109 SkScalar invH = 1.0f / src.height();
110
111 {
112 SkScalar y = srcRect.y() * invH;
113 if (srcView.origin() != kTopLeft_GrSurfaceOrigin) {
114 y = 1.0f - (srcRect.height() / bounds.height()) - y;
115 }
116
117 pdman.set2f(offset, srcRect.x() * invW, y);
118 }
119
120 {
121 SkScalar y = bounds.y() * invH;
122 SkScalar hSign = 1.f;
123 if (srcView.origin() != kTopLeft_GrSurfaceOrigin) {
124 y = 1.0f - bounds.y() * invH;
125 hSign = -1.f;
126 }
127
128 pdman.set4f(boundsUniform,
129 bounds.x() * invW,
130 y,
131 SkIntToScalar(src.width()) / bounds.width(),
132 hSign * SkIntToScalar(src.height()) / bounds.height());
133 }
134 }
135 UniformHandle boundsUniformVar;
136 UniformHandle offsetVar;
137 UniformHandle xInvZoomVar;
138 UniformHandle yInvZoomVar;
139 UniformHandle xInvInsetVar;
140 UniformHandle yInvInsetVar;
141};
142GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const {
143 return new GrGLSLMagnifierEffect();
144}
145void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
146 GrProcessorKeyBuilder* b) const {}
147bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const {
148 const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>();
149 (void)that;
150 if (src != that.src) return false;
151 if (bounds != that.bounds) return false;
152 if (srcRect != that.srcRect) return false;
153 if (xInvZoom != that.xInvZoom) return false;
154 if (yInvZoom != that.yInvZoom) return false;
155 if (xInvInset != that.xInvInset) return false;
156 if (yInvInset != that.yInvInset) return false;
157 return true;
158}
159GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src)
160 : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags())
161 , srcCoordTransform(src.srcCoordTransform)
162 , src(src.src)
163 , bounds(src.bounds)
164 , srcRect(src.srcRect)
165 , xInvZoom(src.xInvZoom)
166 , yInvZoom(src.yInvZoom)
167 , xInvInset(src.xInvInset)
168 , yInvInset(src.yInvInset) {
169 this->setTextureSamplerCnt(1);
170 this->addCoordTransform(&srcCoordTransform);
171}
172std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const {
173 return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this));
174}
175const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const {
176 return IthTextureSampler(index, src);
177}
178GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect);
179#if GR_TEST_UTILS
180std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) {
181 auto[view, ct, at] = d->randomView();
182 const int kMaxWidth = 200;
183 const int kMaxHeight = 200;
184 const SkScalar kMaxInset = 20.0f;
185 uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
186 uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
187 SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);
188
189 SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
190 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
191
192 auto effect = GrMagnifierEffect::Make(std::move(view),
193 bounds,
194 srcRect,
195 srcRect.width() / bounds.width(),
196 srcRect.height() / bounds.height(),
197 bounds.width() / inset,
198 bounds.height() / inset);
199 SkASSERT(effect);
200 return effect;
201}
202#endif
203