blob: 3e34da75a147b9e7e990f4f74cfdf542bf166114 [file] [log] [blame]
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05008/**************************************************************************************************
9 *** This file was autogenerated from GrMagnifierEffect.fp; do not modify.
10 **************************************************************************************************/
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050011#include "GrMagnifierEffect.h"
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050012#include "glsl/GrGLSLFragmentProcessor.h"
13#include "glsl/GrGLSLFragmentShaderBuilder.h"
14#include "glsl/GrGLSLProgramBuilder.h"
15#include "GrTexture.h"
16#include "SkSLCPP.h"
17#include "SkSLUtil.h"
18class GrGLSLMagnifierEffect : public GrGLSLFragmentProcessor {
19public:
20 GrGLSLMagnifierEffect() {}
21 void emitCode(EmitArgs& args) override {
22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Mike Kleind6ab77a2019-03-21 08:18:24 -050023 const GrMagnifierEffect& _outer = args.fFp.cast<GrMagnifierEffect>();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050024 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040025 auto bounds = _outer.bounds;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050026 (void)bounds;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040027 auto srcRect = _outer.srcRect;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050028 (void)srcRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040029 auto xInvZoom = _outer.xInvZoom;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050030 (void)xInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040031 auto yInvZoom = _outer.yInvZoom;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050032 (void)yInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040033 auto xInvInset = _outer.xInvInset;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050034 (void)xInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040035 auto yInvInset = _outer.yInvInset;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050036 (void)yInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040037 boundsUniformVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050038 kFragment_GrShaderFlag, kFloat4_GrSLType, "boundsUniform");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040039 xInvZoomVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050040 kFragment_GrShaderFlag, kFloat_GrSLType, "xInvZoom");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040041 yInvZoomVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050042 kFragment_GrShaderFlag, kFloat_GrSLType, "yInvZoom");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040043 xInvInsetVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050044 kFragment_GrShaderFlag, kFloat_GrSLType, "xInvInset");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040045 yInvInsetVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050046 kFragment_GrShaderFlag, kFloat_GrSLType, "yInvInset");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040047 offsetVar =
Ethan Nicholas858fecc2019-03-07 13:19:18 -050048 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, "offset");
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050049 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
50 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050051 "float2 coord = %s;\nfloat2 zoom_coord = float2(%s) + coord * float2(%s, "
52 "%s);\nfloat2 delta = (coord - %s.xy) * %s.zw;\ndelta = min(delta, "
53 "float2(half2(1.0, 1.0)) - delta);\ndelta *= float2(%s, %s);\nfloat weight = "
54 "0.0;\nif (delta.x < 2.0 && delta.y < 2.0) {\n delta = float2(half2(2.0, 2.0)) "
55 "- delta;\n float dist = length(delta);\n dist = max(2.0 - dist, 0.0);\n "
56 "weight = min(dist * dist, 1.0);\n} else {\n float2 delta_squared = delta * "
57 "delta;\n weight = min(min(delta_squared.x, delta_square",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050058 sk_TransformedCoords2D_0.c_str(),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040059 args.fUniformHandler->getUniformCStr(offsetVar),
60 args.fUniformHandler->getUniformCStr(xInvZoomVar),
61 args.fUniformHandler->getUniformCStr(yInvZoomVar),
62 args.fUniformHandler->getUniformCStr(boundsUniformVar),
63 args.fUniformHandler->getUniformCStr(boundsUniformVar),
64 args.fUniformHandler->getUniformCStr(xInvInsetVar),
65 args.fUniformHandler->getUniformCStr(yInvInsetVar));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050066 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050067 "d.y), 1.0);\n}\n%s = texture(%s, mix(coord, zoom_coord, weight)).%s;\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050068 args.fOutputColor,
69 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
70 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str());
71 }
72
73private:
74 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050075 const GrFragmentProcessor& _proc) override {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050076 const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>();
77 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040078 pdman.set1f(xInvZoomVar, (_outer.xInvZoom));
79 pdman.set1f(yInvZoomVar, (_outer.yInvZoom));
80 pdman.set1f(xInvInsetVar, (_outer.xInvInset));
81 pdman.set1f(yInvInsetVar, (_outer.yInvInset));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050082 }
83 GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy();
Mike Kleind6ab77a2019-03-21 08:18:24 -050084 GrTexture& src = *srcProxy.peekTexture();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050085 (void)src;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040086 auto bounds = _outer.bounds;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050087 (void)bounds;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040088 UniformHandle& boundsUniform = boundsUniformVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050089 (void)boundsUniform;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040090 auto srcRect = _outer.srcRect;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050091 (void)srcRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040092 UniformHandle& xInvZoom = xInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050093 (void)xInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040094 UniformHandle& yInvZoom = yInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050095 (void)yInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040096 UniformHandle& xInvInset = xInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050097 (void)xInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040098 UniformHandle& yInvInset = yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050099 (void)yInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400100 UniformHandle& offset = offsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500101 (void)offset;
102
103 SkScalar invW = 1.0f / src.width();
104 SkScalar invH = 1.0f / src.height();
105
106 {
107 SkScalar y = srcRect.y() * invH;
108 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
109 y = 1.0f - (srcRect.height() / bounds.height()) - y;
110 }
111
112 pdman.set2f(offset, srcRect.x() * invW, y);
113 }
114
115 {
116 SkScalar y = bounds.y() * invH;
117 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
118 y = 1.0f - bounds.height() * invH;
119 }
120
121 pdman.set4f(boundsUniform,
122 bounds.x() * invW,
123 y,
124 SkIntToScalar(src.width()) / bounds.width(),
125 SkIntToScalar(src.height()) / bounds.height());
126 }
127 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400128 UniformHandle boundsUniformVar;
129 UniformHandle offsetVar;
130 UniformHandle xInvZoomVar;
131 UniformHandle yInvZoomVar;
132 UniformHandle xInvInsetVar;
133 UniformHandle yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500134};
135GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const {
136 return new GrGLSLMagnifierEffect();
137}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500138void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500139 GrProcessorKeyBuilder* b) const {}
140bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const {
141 const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>();
142 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400143 if (src != that.src) return false;
144 if (bounds != that.bounds) return false;
145 if (srcRect != that.srcRect) return false;
146 if (xInvZoom != that.xInvZoom) return false;
147 if (yInvZoom != that.yInvZoom) return false;
148 if (xInvInset != that.xInvInset) return false;
149 if (yInvInset != that.yInvInset) return false;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500150 return true;
151}
152GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src)
153 : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400154 , srcCoordTransform(src.srcCoordTransform)
155 , src(src.src)
156 , bounds(src.bounds)
157 , srcRect(src.srcRect)
158 , xInvZoom(src.xInvZoom)
159 , yInvZoom(src.yInvZoom)
160 , xInvInset(src.xInvInset)
161 , yInvInset(src.yInvInset) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400162 this->setTextureSamplerCnt(1);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400163 this->addCoordTransform(&srcCoordTransform);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500164}
165std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const {
166 return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this));
167}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400168const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400169 return IthTextureSampler(index, src);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400170}
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500171GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect);
172#if GR_TEST_UTILS
173std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500174 sk_sp<GrTextureProxy> proxy = d->textureProxy(0);
175 const int kMaxWidth = 200;
176 const int kMaxHeight = 200;
177 const SkScalar kMaxInset = 20.0f;
178 uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
179 uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
180 SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500181
Mike Kleind6ab77a2019-03-21 08:18:24 -0500182 SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
183 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500184
185 auto effect = GrMagnifierEffect::Make(std::move(proxy),
186 bounds,
187 srcRect,
188 srcRect.width() / bounds.width(),
189 srcRect.height() / bounds.height(),
190 bounds.width() / inset,
191 bounds.height() / inset);
192 SkASSERT(effect);
193 return effect;
194}
195#endif