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