Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2013 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "GrDistanceFieldTextureEffect.h" |
michael@0 | 9 | #include "gl/GrGLEffect.h" |
michael@0 | 10 | #include "gl/GrGLSL.h" |
michael@0 | 11 | #include "gl/GrGLTexture.h" |
michael@0 | 12 | #include "gl/GrGLVertexEffect.h" |
michael@0 | 13 | #include "GrTBackendEffectFactory.h" |
michael@0 | 14 | #include "GrTexture.h" |
michael@0 | 15 | |
michael@0 | 16 | // The distance field is constructed as unsigned char values, so that the zero value is at 128, |
michael@0 | 17 | // and the range is [-4, 4 - 1/255). Hence our multiplier is 8 - 1/32 and zero threshold is 128/255. |
michael@0 | 18 | #define MULTIPLIER "7.96875" |
michael@0 | 19 | #define THRESHOLD "0.50196078431" |
michael@0 | 20 | |
michael@0 | 21 | class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect { |
michael@0 | 22 | public: |
michael@0 | 23 | GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, |
michael@0 | 24 | const GrDrawEffect& drawEffect) |
michael@0 | 25 | : INHERITED (factory) |
michael@0 | 26 | , fTextureSize(SkSize::Make(-1.f,-1.f)) {} |
michael@0 | 27 | |
michael@0 | 28 | virtual void emitCode(GrGLFullShaderBuilder* builder, |
michael@0 | 29 | const GrDrawEffect& drawEffect, |
michael@0 | 30 | EffectKey key, |
michael@0 | 31 | const char* outputColor, |
michael@0 | 32 | const char* inputColor, |
michael@0 | 33 | const TransformedCoordsArray&, |
michael@0 | 34 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
michael@0 | 35 | SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numVertexAttribs()); |
michael@0 | 36 | |
michael@0 | 37 | SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
michael@0 | 38 | |
michael@0 | 39 | SkString fsCoordName; |
michael@0 | 40 | const char* vsCoordName; |
michael@0 | 41 | const char* fsCoordNamePtr; |
michael@0 | 42 | builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsCoordNamePtr); |
michael@0 | 43 | fsCoordName = fsCoordNamePtr; |
michael@0 | 44 | |
michael@0 | 45 | const char* attrName0 = |
michael@0 | 46 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str(); |
michael@0 | 47 | builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); |
michael@0 | 48 | |
michael@0 | 49 | const char* textureSizeUniName = NULL; |
michael@0 | 50 | fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 51 | kVec2f_GrSLType, "TextureSize", |
michael@0 | 52 | &textureSizeUniName); |
michael@0 | 53 | |
michael@0 | 54 | builder->fsCodeAppend("\tvec4 texColor = "); |
michael@0 | 55 | builder->fsAppendTextureLookup(samplers[0], |
michael@0 | 56 | fsCoordName.c_str(), |
michael@0 | 57 | kVec2f_GrSLType); |
michael@0 | 58 | builder->fsCodeAppend(";\n"); |
michael@0 | 59 | builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - " THRESHOLD ");\n"); |
michael@0 | 60 | |
michael@0 | 61 | // we adjust for the effect of the transformation on the distance by using |
michael@0 | 62 | // the length of the gradient of the texture coordinates. We use st coordinates |
michael@0 | 63 | // to ensure we're mapping 1:1 from texel space to pixel space. |
michael@0 | 64 | builder->fsCodeAppendf("\tvec2 st = %s*%s;\n", fsCoordName.c_str(), textureSizeUniName); |
michael@0 | 65 | builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); |
michael@0 | 66 | builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); |
michael@0 | 67 | builder->fsCodeAppend("\tvec2 st_grad = normalize(st);\n"); |
michael@0 | 68 | builder->fsCodeAppend("\tvec2 grad = vec2(st_grad.x*Jdx.x + st_grad.y*Jdy.x,\n"); |
michael@0 | 69 | builder->fsCodeAppend("\t st_grad.x*Jdx.y + st_grad.y*Jdy.y);\n"); |
michael@0 | 70 | |
michael@0 | 71 | // this gives us a smooth step across approximately one fragment |
michael@0 | 72 | // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2) |
michael@0 | 73 | builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(grad);\n"); |
michael@0 | 74 | builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, distance);\n"); |
michael@0 | 75 | |
michael@0 | 76 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
michael@0 | 77 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")).c_str()); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | virtual void setData(const GrGLUniformManager& uman, |
michael@0 | 81 | const GrDrawEffect& drawEffect) SK_OVERRIDE { |
michael@0 | 82 | SkASSERT(fTextureSizeUni.isValid()); |
michael@0 | 83 | const GrDistanceFieldTextureEffect& distanceFieldEffect = |
michael@0 | 84 | drawEffect.castEffect<GrDistanceFieldTextureEffect>(); |
michael@0 | 85 | if (distanceFieldEffect.getSize().width() != fTextureSize.width() || |
michael@0 | 86 | distanceFieldEffect.getSize().height() != fTextureSize.height()) { |
michael@0 | 87 | fTextureSize = distanceFieldEffect.getSize(); |
michael@0 | 88 | uman.set2f(fTextureSizeUni, |
michael@0 | 89 | distanceFieldEffect.getSize().width(), |
michael@0 | 90 | distanceFieldEffect.getSize().height()); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | private: |
michael@0 | 95 | GrGLUniformManager::UniformHandle fTextureSizeUni; |
michael@0 | 96 | SkSize fTextureSize; |
michael@0 | 97 | |
michael@0 | 98 | typedef GrGLVertexEffect INHERITED; |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 102 | |
michael@0 | 103 | GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, |
michael@0 | 104 | const GrTextureParams& params, |
michael@0 | 105 | const SkISize& size) |
michael@0 | 106 | : fTextureAccess(texture, params) |
michael@0 | 107 | , fSize(SkSize::Make(SkIntToScalar(size.width()), SkIntToScalar(size.height()))) { |
michael@0 | 108 | this->addTextureAccess(&fTextureAccess); |
michael@0 | 109 | this->addVertexAttrib(kVec2f_GrSLType); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { |
michael@0 | 113 | const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureEffect>(other); |
michael@0 | 114 | return fTextureAccess == cte.fTextureAccess; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, |
michael@0 | 118 | uint32_t* validFlags) const { |
michael@0 | 119 | if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) && |
michael@0 | 120 | GrPixelConfigIsOpaque(this->texture(0)->config())) { |
michael@0 | 121 | *validFlags = kA_GrColorComponentFlag; |
michael@0 | 122 | } else { |
michael@0 | 123 | *validFlags = 0; |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | const GrBackendEffectFactory& GrDistanceFieldTextureEffect::getFactory() const { |
michael@0 | 128 | return GrTBackendEffectFactory<GrDistanceFieldTextureEffect>::getInstance(); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 132 | |
michael@0 | 133 | GR_DEFINE_EFFECT_TEST(GrDistanceFieldTextureEffect); |
michael@0 | 134 | |
michael@0 | 135 | GrEffectRef* GrDistanceFieldTextureEffect::TestCreate(SkRandom* random, |
michael@0 | 136 | GrContext*, |
michael@0 | 137 | const GrDrawTargetCaps&, |
michael@0 | 138 | GrTexture* textures[]) { |
michael@0 | 139 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
michael@0 | 140 | GrEffectUnitTest::kAlphaTextureIdx; |
michael@0 | 141 | static const SkShader::TileMode kTileModes[] = { |
michael@0 | 142 | SkShader::kClamp_TileMode, |
michael@0 | 143 | SkShader::kRepeat_TileMode, |
michael@0 | 144 | SkShader::kMirror_TileMode, |
michael@0 | 145 | }; |
michael@0 | 146 | SkShader::TileMode tileModes[] = { |
michael@0 | 147 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
michael@0 | 148 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
michael@0 | 149 | }; |
michael@0 | 150 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
michael@0 | 151 | GrTextureParams::kNone_FilterMode); |
michael@0 | 152 | SkISize size = SkISize::Make(1024, 2048); |
michael@0 | 153 | |
michael@0 | 154 | return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, size); |
michael@0 | 155 | } |