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