Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 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 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 8 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrMagnifierEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 11 | #include "GrMagnifierEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 19 | class GrGLSLMagnifierEffect : public GrGLSLFragmentProcessor { |
| 20 | public: |
| 21 | GrGLSLMagnifierEffect() {} |
| 22 | void emitCode(EmitArgs& args) override { |
| 23 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 24 | const GrMagnifierEffect& _outer = args.fFp.cast<GrMagnifierEffect>(); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 25 | (void)_outer; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 26 | auto bounds = _outer.bounds; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 27 | (void)bounds; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 28 | auto srcRect = _outer.srcRect; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 29 | (void)srcRect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 30 | auto xInvZoom = _outer.xInvZoom; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 31 | (void)xInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 32 | auto yInvZoom = _outer.yInvZoom; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 33 | (void)yInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 34 | auto xInvInset = _outer.xInvInset; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 35 | (void)xInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 36 | auto yInvInset = _outer.yInvInset; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 37 | (void)yInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 38 | boundsUniformVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 39 | kFragment_GrShaderFlag, kFloat4_GrSLType, "boundsUniform"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 40 | xInvZoomVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 41 | kFragment_GrShaderFlag, kFloat_GrSLType, "xInvZoom"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 42 | yInvZoomVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 43 | kFragment_GrShaderFlag, kFloat_GrSLType, "yInvZoom"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 44 | xInvInsetVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 45 | kFragment_GrShaderFlag, kFloat_GrSLType, "xInvInset"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 46 | yInvInsetVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 47 | kFragment_GrShaderFlag, kFloat_GrSLType, "yInvInset"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 48 | offsetVar = |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 49 | args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, "offset"); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 50 | SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
| 51 | fragBuilder->codeAppendf( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 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", |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 59 | sk_TransformedCoords2D_0.c_str(), |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 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)); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 67 | fragBuilder->codeAppendf( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 68 | "d.y), 1.0);\n}\n%s = texture(%s, mix(coord, zoom_coord, weight)).%s;\n", |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 69 | args.fOutputColor, |
Stephen White | d523a06 | 2019-06-19 13:12:46 -0400 | [diff] [blame^] | 70 | fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]), |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 71 | fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str()); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 76 | const GrFragmentProcessor& _proc) override { |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 77 | const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>(); |
| 78 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 79 | pdman.set1f(xInvZoomVar, (_outer.xInvZoom)); |
| 80 | pdman.set1f(yInvZoomVar, (_outer.yInvZoom)); |
| 81 | pdman.set1f(xInvInsetVar, (_outer.xInvInset)); |
| 82 | pdman.set1f(yInvInsetVar, (_outer.yInvInset)); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 83 | } |
| 84 | GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy(); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 85 | GrTexture& src = *srcProxy.peekTexture(); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 86 | (void)src; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 87 | auto bounds = _outer.bounds; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 88 | (void)bounds; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 89 | UniformHandle& boundsUniform = boundsUniformVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 90 | (void)boundsUniform; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 91 | auto srcRect = _outer.srcRect; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 92 | (void)srcRect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 93 | UniformHandle& xInvZoom = xInvZoomVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 94 | (void)xInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 95 | UniformHandle& yInvZoom = yInvZoomVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 96 | (void)yInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 97 | UniformHandle& xInvInset = xInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 98 | (void)xInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 99 | UniformHandle& yInvInset = yInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 100 | (void)yInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 101 | UniformHandle& offset = offsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 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 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 129 | UniformHandle boundsUniformVar; |
| 130 | UniformHandle offsetVar; |
| 131 | UniformHandle xInvZoomVar; |
| 132 | UniformHandle yInvZoomVar; |
| 133 | UniformHandle xInvInsetVar; |
| 134 | UniformHandle yInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 135 | }; |
| 136 | GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const { |
| 137 | return new GrGLSLMagnifierEffect(); |
| 138 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 139 | void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 140 | GrProcessorKeyBuilder* b) const {} |
| 141 | bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 142 | const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>(); |
| 143 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 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; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src) |
| 154 | : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 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) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 163 | this->setTextureSamplerCnt(1); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 164 | this->addCoordTransform(&srcCoordTransform); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 165 | } |
| 166 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const { |
| 167 | return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this)); |
| 168 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 169 | const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 170 | return IthTextureSampler(index, src); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 171 | } |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 172 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect); |
| 173 | #if GR_TEST_UTILS |
| 174 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 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); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 182 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 183 | SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight)); |
| 184 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 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 |