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/core/SkUtils.h" |
14 | #include "src/gpu/GrTexture.h" |
15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
17 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
18 | #include "src/sksl/SkSLCPP.h" |
19 | #include "src/sksl/SkSLUtil.h" |
20 | class GrGLSLMagnifierEffect : public GrGLSLFragmentProcessor { |
21 | public: |
22 | GrGLSLMagnifierEffect() {} |
23 | void emitCode(EmitArgs& args) override { |
24 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
25 | const GrMagnifierEffect& _outer = args.fFp.cast<GrMagnifierEffect>(); |
26 | (void)_outer; |
27 | auto bounds = _outer.bounds; |
28 | (void)bounds; |
29 | auto srcRect = _outer.srcRect; |
30 | (void)srcRect; |
31 | auto xInvZoom = _outer.xInvZoom; |
32 | (void)xInvZoom; |
33 | auto yInvZoom = _outer.yInvZoom; |
34 | (void)yInvZoom; |
35 | auto xInvInset = _outer.xInvInset; |
36 | (void)xInvInset; |
37 | auto yInvInset = _outer.yInvInset; |
38 | (void)yInvInset; |
39 | boundsUniformVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
40 | kFloat4_GrSLType, "boundsUniform" ); |
41 | xInvZoomVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
42 | kFloat_GrSLType, "xInvZoom" ); |
43 | yInvZoomVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
44 | kFloat_GrSLType, "yInvZoom" ); |
45 | xInvInsetVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
46 | kFloat_GrSLType, "xInvInset" ); |
47 | yInvInsetVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
48 | kFloat_GrSLType, "yInvInset" ); |
49 | offsetVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
50 | kHalf2_GrSLType, "offset" ); |
51 | fragBuilder->codeAppendf( |
52 | R"SkSL(float2 zoom_coord = float2(%s) + %s * float2(%s, %s); |
53 | float2 delta = (%s - %s.xy) * %s.zw; |
54 | delta = min(delta, float2(half2(1.0, 1.0)) - delta); |
55 | delta *= float2(%s, %s); |
56 | float weight = 0.0; |
57 | if (delta.x < 2.0 && delta.y < 2.0) { |
58 | delta = float2(half2(2.0, 2.0)) - delta; |
59 | float dist = length(delta); |
60 | dist = max(2.0 - dist, 0.0); |
61 | weight = min(dist * dist, 1.0); |
62 | } else { |
63 | float2 delta_squared = delta * delta; |
64 | weight = min(min(delta_squared.x, delta_squared.y), 1.0); |
65 | })SkSL" , |
66 | args.fUniformHandler->getUniformCStr(offsetVar), args.fSampleCoord, |
67 | args.fUniformHandler->getUniformCStr(xInvZoomVar), |
68 | args.fUniformHandler->getUniformCStr(yInvZoomVar), args.fSampleCoord, |
69 | args.fUniformHandler->getUniformCStr(boundsUniformVar), |
70 | args.fUniformHandler->getUniformCStr(boundsUniformVar), |
71 | args.fUniformHandler->getUniformCStr(xInvInsetVar), |
72 | args.fUniformHandler->getUniformCStr(yInvInsetVar)); |
73 | SkString _coords1043 = SkStringPrintf("mix(%s, zoom_coord, weight)" , args.fSampleCoord); |
74 | SkString _sample1043 = this->invokeChild(0, args, _coords1043.c_str()); |
75 | fragBuilder->codeAppendf( |
76 | R"SkSL( |
77 | %s = %s; |
78 | )SkSL" , |
79 | args.fOutputColor, _sample1043.c_str()); |
80 | } |
81 | |
82 | private: |
83 | void onSetData(const GrGLSLProgramDataManager& pdman, |
84 | const GrFragmentProcessor& _proc) override { |
85 | const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>(); |
86 | { |
87 | pdman.set1f(xInvZoomVar, (_outer.xInvZoom)); |
88 | pdman.set1f(yInvZoomVar, (_outer.yInvZoom)); |
89 | pdman.set1f(xInvInsetVar, (_outer.xInvInset)); |
90 | pdman.set1f(yInvInsetVar, (_outer.yInvInset)); |
91 | } |
92 | auto bounds = _outer.bounds; |
93 | (void)bounds; |
94 | UniformHandle& boundsUniform = boundsUniformVar; |
95 | (void)boundsUniform; |
96 | auto srcRect = _outer.srcRect; |
97 | (void)srcRect; |
98 | UniformHandle& xInvZoom = xInvZoomVar; |
99 | (void)xInvZoom; |
100 | UniformHandle& yInvZoom = yInvZoomVar; |
101 | (void)yInvZoom; |
102 | UniformHandle& xInvInset = xInvInsetVar; |
103 | (void)xInvInset; |
104 | UniformHandle& yInvInset = yInvInsetVar; |
105 | (void)yInvInset; |
106 | UniformHandle& offset = offsetVar; |
107 | (void)offset; |
108 | |
109 | pdman.set2f(offset, srcRect.x(), srcRect.y()); |
110 | pdman.set4f(boundsUniform, bounds.x(), bounds.y(), 1.f / bounds.width(), |
111 | 1.f / bounds.height()); |
112 | } |
113 | UniformHandle boundsUniformVar; |
114 | UniformHandle offsetVar; |
115 | UniformHandle xInvZoomVar; |
116 | UniformHandle yInvZoomVar; |
117 | UniformHandle xInvInsetVar; |
118 | UniformHandle yInvInsetVar; |
119 | }; |
120 | GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const { |
121 | return new GrGLSLMagnifierEffect(); |
122 | } |
123 | void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
124 | GrProcessorKeyBuilder* b) const {} |
125 | bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const { |
126 | const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>(); |
127 | (void)that; |
128 | if (bounds != that.bounds) return false; |
129 | if (srcRect != that.srcRect) return false; |
130 | if (xInvZoom != that.xInvZoom) return false; |
131 | if (yInvZoom != that.yInvZoom) return false; |
132 | if (xInvInset != that.xInvInset) return false; |
133 | if (yInvInset != that.yInvInset) return false; |
134 | return true; |
135 | } |
136 | GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src) |
137 | : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags()) |
138 | , bounds(src.bounds) |
139 | , srcRect(src.srcRect) |
140 | , xInvZoom(src.xInvZoom) |
141 | , yInvZoom(src.yInvZoom) |
142 | , xInvInset(src.xInvInset) |
143 | , yInvInset(src.yInvInset) { |
144 | this->cloneAndRegisterAllChildProcessors(src); |
145 | this->setUsesSampleCoordsDirectly(); |
146 | } |
147 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const { |
148 | return std::make_unique<GrMagnifierEffect>(*this); |
149 | } |
150 | #if GR_TEST_UTILS |
151 | SkString GrMagnifierEffect::onDumpInfo() const { |
152 | return SkStringPrintf( |
153 | "(bounds=int4(%d, %d, %d, %d), srcRect=float4(%f, %f, %f, %f), xInvZoom=%f, " |
154 | "yInvZoom=%f, xInvInset=%f, yInvInset=%f)" , |
155 | bounds.left(), bounds.top(), bounds.right(), bounds.bottom(), srcRect.left(), |
156 | srcRect.top(), srcRect.right(), srcRect.bottom(), xInvZoom, yInvZoom, xInvInset, |
157 | yInvInset); |
158 | } |
159 | #endif |
160 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect); |
161 | #if GR_TEST_UTILS |
162 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) { |
163 | const int kMaxWidth = 200; |
164 | const int kMaxHeight = 200; |
165 | const SkScalar kMaxInset = 20.0f; |
166 | uint32_t width = d->fRandom->nextULessThan(kMaxWidth); |
167 | uint32_t height = d->fRandom->nextULessThan(kMaxHeight); |
168 | SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset); |
169 | |
170 | SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight)); |
171 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
172 | |
173 | auto src = GrProcessorUnitTest::MakeChildFP(d); |
174 | auto effect = GrMagnifierEffect::Make(std::move(src), |
175 | bounds, |
176 | srcRect, |
177 | srcRect.width() / bounds.width(), |
178 | srcRect.height() / bounds.height(), |
179 | bounds.width() / inset, |
180 | bounds.height() / inset); |
181 | SkASSERT(effect); |
182 | return effect; |
183 | } |
184 | #endif |
185 | |