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 #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" 18 class GrGLSLMagnifierEffect : public GrGLSLFragmentProcessor { 19 public: 20 GrGLSLMagnifierEffect() {} 21 void emitCode(EmitArgs& args) override { 22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 23 const GrMagnifierEffect& _outer = args.fFp.cast<GrMagnifierEffect>(); 24 (void)_outer; 25 auto bounds = _outer.bounds(); 26 (void)bounds; 27 auto srcRect = _outer.srcRect(); 28 (void)srcRect; 29 auto xInvZoom = _outer.xInvZoom(); 30 (void)xInvZoom; 31 auto yInvZoom = _outer.yInvZoom(); 32 (void)yInvZoom; 33 auto xInvInset = _outer.xInvInset(); 34 (void)xInvInset; 35 auto yInvInset = _outer.yInvInset(); 36 (void)yInvInset; 37 fBoundsUniformVar = args.fUniformHandler->addUniform( 38 kFragment_GrShaderFlag, kFloat4_GrSLType, "boundsUniform"); 39 fXInvZoomVar = args.fUniformHandler->addUniform( 40 kFragment_GrShaderFlag, kFloat_GrSLType, "xInvZoom"); 41 fYInvZoomVar = args.fUniformHandler->addUniform( 42 kFragment_GrShaderFlag, kFloat_GrSLType, "yInvZoom"); 43 fXInvInsetVar = args.fUniformHandler->addUniform( 44 kFragment_GrShaderFlag, kFloat_GrSLType, "xInvInset"); 45 fYInvInsetVar = args.fUniformHandler->addUniform( 46 kFragment_GrShaderFlag, kFloat_GrSLType, "yInvInset"); 47 fOffsetVar = 48 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, "offset"); 49 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); 50 fragBuilder->codeAppendf( 51 "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", 58 sk_TransformedCoords2D_0.c_str(), 59 args.fUniformHandler->getUniformCStr(fOffsetVar), 60 args.fUniformHandler->getUniformCStr(fXInvZoomVar), 61 args.fUniformHandler->getUniformCStr(fYInvZoomVar), 62 args.fUniformHandler->getUniformCStr(fBoundsUniformVar), 63 args.fUniformHandler->getUniformCStr(fBoundsUniformVar), 64 args.fUniformHandler->getUniformCStr(fXInvInsetVar), 65 args.fUniformHandler->getUniformCStr(fYInvInsetVar)); 66 fragBuilder->codeAppendf( 67 "d.y), 1.0);\n}\n%s = texture(%s, mix(coord, zoom_coord, weight)).%s;\n", 68 args.fOutputColor, 69 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), 70 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str()); 71 } 72 73 private: 74 void onSetData(const GrGLSLProgramDataManager& pdman, 75 const GrFragmentProcessor& _proc) override { 76 const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>(); 77 { 78 pdman.set1f(fXInvZoomVar, (_outer.xInvZoom())); 79 pdman.set1f(fYInvZoomVar, (_outer.yInvZoom())); 80 pdman.set1f(fXInvInsetVar, (_outer.xInvInset())); 81 pdman.set1f(fYInvInsetVar, (_outer.yInvInset())); 82 } 83 GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy(); 84 GrTexture& src = *srcProxy.peekTexture(); 85 (void)src; 86 auto bounds = _outer.bounds(); 87 (void)bounds; 88 UniformHandle& boundsUniform = fBoundsUniformVar; 89 (void)boundsUniform; 90 auto srcRect = _outer.srcRect(); 91 (void)srcRect; 92 UniformHandle& xInvZoom = fXInvZoomVar; 93 (void)xInvZoom; 94 UniformHandle& yInvZoom = fYInvZoomVar; 95 (void)yInvZoom; 96 UniformHandle& xInvInset = fXInvInsetVar; 97 (void)xInvInset; 98 UniformHandle& yInvInset = fYInvInsetVar; 99 (void)yInvInset; 100 UniformHandle& offset = fOffsetVar; 101 (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 } 128 UniformHandle fBoundsUniformVar; 129 UniformHandle fOffsetVar; 130 UniformHandle fXInvZoomVar; 131 UniformHandle fYInvZoomVar; 132 UniformHandle fXInvInsetVar; 133 UniformHandle fYInvInsetVar; 134 }; 135 GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const { 136 return new GrGLSLMagnifierEffect(); 137 } 138 void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, 139 GrProcessorKeyBuilder* b) const {} 140 bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const { 141 const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>(); 142 (void)that; 143 if (fSrc != that.fSrc) return false; 144 if (fBounds != that.fBounds) return false; 145 if (fSrcRect != that.fSrcRect) return false; 146 if (fXInvZoom != that.fXInvZoom) return false; 147 if (fYInvZoom != that.fYInvZoom) return false; 148 if (fXInvInset != that.fXInvInset) return false; 149 if (fYInvInset != that.fYInvInset) return false; 150 return true; 151 } 152 GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src) 153 : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags()) 154 , fSrc(src.fSrc) 155 , fBounds(src.fBounds) 156 , fSrcRect(src.fSrcRect) 157 , fXInvZoom(src.fXInvZoom) 158 , fYInvZoom(src.fYInvZoom) 159 , fXInvInset(src.fXInvInset) 160 , fYInvInset(src.fYInvInset) 161 , fSrcCoordTransform(src.fSrcCoordTransform) { 162 this->setTextureSamplerCnt(1); 163 this->addCoordTransform(&fSrcCoordTransform); 164 } 165 std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const { 166 return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this)); 167 } 168 const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const { 169 return IthTextureSampler(index, fSrc); 170 } 171 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect); 172 #if GR_TEST_UTILS 173 std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) { 174 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); 181 182 SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight)); 183 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); 184 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 196