Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 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 "GrConvolutionEffect.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 "GrTBackendEffectFactory.h" |
michael@0 | 13 | |
michael@0 | 14 | // For brevity |
michael@0 | 15 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
michael@0 | 16 | |
michael@0 | 17 | class GrGLConvolutionEffect : public GrGLEffect { |
michael@0 | 18 | public: |
michael@0 | 19 | GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
michael@0 | 20 | |
michael@0 | 21 | virtual void emitCode(GrGLShaderBuilder*, |
michael@0 | 22 | const GrDrawEffect&, |
michael@0 | 23 | EffectKey, |
michael@0 | 24 | const char* outputColor, |
michael@0 | 25 | const char* inputColor, |
michael@0 | 26 | const TransformedCoordsArray&, |
michael@0 | 27 | const TextureSamplerArray&) SK_OVERRIDE; |
michael@0 | 28 | |
michael@0 | 29 | virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK_OVERRIDE; |
michael@0 | 30 | |
michael@0 | 31 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
michael@0 | 32 | |
michael@0 | 33 | private: |
michael@0 | 34 | int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
michael@0 | 35 | bool useBounds() const { return fUseBounds; } |
michael@0 | 36 | Gr1DKernelEffect::Direction direction() const { return fDirection; } |
michael@0 | 37 | |
michael@0 | 38 | int fRadius; |
michael@0 | 39 | bool fUseBounds; |
michael@0 | 40 | Gr1DKernelEffect::Direction fDirection; |
michael@0 | 41 | UniformHandle fKernelUni; |
michael@0 | 42 | UniformHandle fImageIncrementUni; |
michael@0 | 43 | UniformHandle fBoundsUni; |
michael@0 | 44 | |
michael@0 | 45 | typedef GrGLEffect INHERITED; |
michael@0 | 46 | }; |
michael@0 | 47 | |
michael@0 | 48 | GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory, |
michael@0 | 49 | const GrDrawEffect& drawEffect) |
michael@0 | 50 | : INHERITED(factory) { |
michael@0 | 51 | const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>(); |
michael@0 | 52 | fRadius = c.radius(); |
michael@0 | 53 | fUseBounds = c.useBounds(); |
michael@0 | 54 | fDirection = c.direction(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
michael@0 | 58 | const GrDrawEffect&, |
michael@0 | 59 | EffectKey key, |
michael@0 | 60 | const char* outputColor, |
michael@0 | 61 | const char* inputColor, |
michael@0 | 62 | const TransformedCoordsArray& coords, |
michael@0 | 63 | const TextureSamplerArray& samplers) { |
michael@0 | 64 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
michael@0 | 65 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 66 | kVec2f_GrSLType, "ImageIncrement"); |
michael@0 | 67 | if (this->useBounds()) { |
michael@0 | 68 | fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 69 | kVec2f_GrSLType, "Bounds"); |
michael@0 | 70 | } |
michael@0 | 71 | fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 72 | kFloat_GrSLType, "Kernel", this->width()); |
michael@0 | 73 | |
michael@0 | 74 | builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
michael@0 | 75 | |
michael@0 | 76 | int width = this->width(); |
michael@0 | 77 | const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); |
michael@0 | 78 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
michael@0 | 79 | |
michael@0 | 80 | builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc); |
michael@0 | 81 | |
michael@0 | 82 | // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
michael@0 | 83 | for (int i = 0; i < width; i++) { |
michael@0 | 84 | SkString index; |
michael@0 | 85 | SkString kernelIndex; |
michael@0 | 86 | index.appendS32(i); |
michael@0 | 87 | kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
michael@0 | 88 | builder->fsCodeAppendf("\t\t%s += ", outputColor); |
michael@0 | 89 | builder->fsAppendTextureLookup(samplers[0], "coord"); |
michael@0 | 90 | if (this->useBounds()) { |
michael@0 | 91 | const char* bounds = builder->getUniformCStr(fBoundsUni); |
michael@0 | 92 | const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x"; |
michael@0 | 93 | builder->fsCodeAppendf(" * float(coord.%s >= %s.x && coord.%s <= %s.y)", |
michael@0 | 94 | component, bounds, component, bounds); |
michael@0 | 95 | } |
michael@0 | 96 | builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str()); |
michael@0 | 97 | builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | SkString modulate; |
michael@0 | 101 | GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
michael@0 | 102 | builder->fsCodeAppend(modulate.c_str()); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, |
michael@0 | 106 | const GrDrawEffect& drawEffect) { |
michael@0 | 107 | const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>(); |
michael@0 | 108 | GrTexture& texture = *conv.texture(0); |
michael@0 | 109 | // the code we generated was for a specific kernel radius |
michael@0 | 110 | SkASSERT(conv.radius() == fRadius); |
michael@0 | 111 | float imageIncrement[2] = { 0 }; |
michael@0 | 112 | float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
michael@0 | 113 | switch (conv.direction()) { |
michael@0 | 114 | case Gr1DKernelEffect::kX_Direction: |
michael@0 | 115 | imageIncrement[0] = 1.0f / texture.width(); |
michael@0 | 116 | break; |
michael@0 | 117 | case Gr1DKernelEffect::kY_Direction: |
michael@0 | 118 | imageIncrement[1] = ySign / texture.height(); |
michael@0 | 119 | break; |
michael@0 | 120 | default: |
michael@0 | 121 | GrCrash("Unknown filter direction."); |
michael@0 | 122 | } |
michael@0 | 123 | uman.set2fv(fImageIncrementUni, 1, imageIncrement); |
michael@0 | 124 | if (conv.useBounds()) { |
michael@0 | 125 | const float* bounds = conv.bounds(); |
michael@0 | 126 | if (Gr1DKernelEffect::kY_Direction == conv.direction() && |
michael@0 | 127 | texture.origin() != kTopLeft_GrSurfaceOrigin) { |
michael@0 | 128 | uman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); |
michael@0 | 129 | } else { |
michael@0 | 130 | uman.set2f(fBoundsUni, bounds[0], bounds[1]); |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | uman.set1fv(fKernelUni, this->width(), conv.kernel()); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, |
michael@0 | 137 | const GrGLCaps&) { |
michael@0 | 138 | const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>(); |
michael@0 | 139 | EffectKey key = conv.radius(); |
michael@0 | 140 | key <<= 2; |
michael@0 | 141 | if (conv.useBounds()) { |
michael@0 | 142 | key |= 0x2; |
michael@0 | 143 | key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0; |
michael@0 | 144 | } |
michael@0 | 145 | return key; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 149 | |
michael@0 | 150 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
michael@0 | 151 | Direction direction, |
michael@0 | 152 | int radius, |
michael@0 | 153 | const float* kernel, |
michael@0 | 154 | bool useBounds, |
michael@0 | 155 | float bounds[2]) |
michael@0 | 156 | : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) { |
michael@0 | 157 | SkASSERT(radius <= kMaxKernelRadius); |
michael@0 | 158 | SkASSERT(NULL != kernel); |
michael@0 | 159 | int width = this->width(); |
michael@0 | 160 | for (int i = 0; i < width; i++) { |
michael@0 | 161 | fKernel[i] = kernel[i]; |
michael@0 | 162 | } |
michael@0 | 163 | memcpy(fBounds, bounds, sizeof(fBounds)); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
michael@0 | 167 | Direction direction, |
michael@0 | 168 | int radius, |
michael@0 | 169 | float gaussianSigma, |
michael@0 | 170 | bool useBounds, |
michael@0 | 171 | float bounds[2]) |
michael@0 | 172 | : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) { |
michael@0 | 173 | SkASSERT(radius <= kMaxKernelRadius); |
michael@0 | 174 | int width = this->width(); |
michael@0 | 175 | |
michael@0 | 176 | float sum = 0.0f; |
michael@0 | 177 | float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
michael@0 | 178 | for (int i = 0; i < width; ++i) { |
michael@0 | 179 | float x = static_cast<float>(i - this->radius()); |
michael@0 | 180 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
michael@0 | 181 | // is dropped here, since we renormalize the kernel below. |
michael@0 | 182 | fKernel[i] = sk_float_exp(- x * x * denom); |
michael@0 | 183 | sum += fKernel[i]; |
michael@0 | 184 | } |
michael@0 | 185 | // Normalize the kernel |
michael@0 | 186 | float scale = 1.0f / sum; |
michael@0 | 187 | for (int i = 0; i < width; ++i) { |
michael@0 | 188 | fKernel[i] *= scale; |
michael@0 | 189 | } |
michael@0 | 190 | memcpy(fBounds, bounds, sizeof(fBounds)); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | GrConvolutionEffect::~GrConvolutionEffect() { |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const { |
michael@0 | 197 | return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance(); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
michael@0 | 201 | const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase); |
michael@0 | 202 | return (this->texture(0) == s.texture(0) && |
michael@0 | 203 | this->radius() == s.radius() && |
michael@0 | 204 | this->direction() == s.direction() && |
michael@0 | 205 | this->useBounds() == s.useBounds() && |
michael@0 | 206 | 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && |
michael@0 | 207 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 211 | |
michael@0 | 212 | GR_DEFINE_EFFECT_TEST(GrConvolutionEffect); |
michael@0 | 213 | |
michael@0 | 214 | GrEffectRef* GrConvolutionEffect::TestCreate(SkRandom* random, |
michael@0 | 215 | GrContext*, |
michael@0 | 216 | const GrDrawTargetCaps&, |
michael@0 | 217 | GrTexture* textures[]) { |
michael@0 | 218 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
michael@0 | 219 | GrEffectUnitTest::kAlphaTextureIdx; |
michael@0 | 220 | Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
michael@0 | 221 | int radius = random->nextRangeU(1, kMaxKernelRadius); |
michael@0 | 222 | float kernel[kMaxKernelWidth]; |
michael@0 | 223 | for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) { |
michael@0 | 224 | kernel[i] = random->nextSScalar1(); |
michael@0 | 225 | } |
michael@0 | 226 | float bounds[2]; |
michael@0 | 227 | for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) { |
michael@0 | 228 | bounds[i] = random->nextF(); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | bool useBounds = random->nextBool(); |
michael@0 | 232 | return GrConvolutionEffect::Create(textures[texIdx], |
michael@0 | 233 | dir, |
michael@0 | 234 | radius, |
michael@0 | 235 | kernel, |
michael@0 | 236 | useBounds, |
michael@0 | 237 | bounds); |
michael@0 | 238 | } |