gfx/skia/trunk/src/gpu/effects/GrConvexPolyEffect.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 2014 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 "GrConvexPolyEffect.h"
michael@0 9
michael@0 10 #include "gl/GrGLEffect.h"
michael@0 11 #include "gl/GrGLSL.h"
michael@0 12 #include "GrTBackendEffectFactory.h"
michael@0 13
michael@0 14 #include "SkPath.h"
michael@0 15
michael@0 16 //////////////////////////////////////////////////////////////////////////////
michael@0 17 class GLAARectEffect;
michael@0 18
michael@0 19 class AARectEffect : public GrEffect {
michael@0 20 public:
michael@0 21 typedef GLAARectEffect GLEffect;
michael@0 22
michael@0 23 const SkRect& getRect() const { return fRect; }
michael@0 24
michael@0 25 static const char* Name() { return "AARect"; }
michael@0 26
michael@0 27 static GrEffectRef* Create(GrEffectEdgeType edgeType, const SkRect& rect) {
michael@0 28 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(AARectEffect, (edgeType, rect))));
michael@0 29 }
michael@0 30
michael@0 31 virtual void getConstantColorComponents(GrColor* color,
michael@0 32 uint32_t* validFlags) const SK_OVERRIDE {
michael@0 33 if (fRect.isEmpty()) {
michael@0 34 // An empty rect will have no coverage anywhere.
michael@0 35 *color = 0x00000000;
michael@0 36 *validFlags = kRGBA_GrColorComponentFlags;
michael@0 37 } else {
michael@0 38 *validFlags = 0;
michael@0 39 }
michael@0 40 }
michael@0 41
michael@0 42 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
michael@0 43
michael@0 44 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
michael@0 45
michael@0 46 private:
michael@0 47 AARectEffect(GrEffectEdgeType edgeType, const SkRect& rect) : fRect(rect), fEdgeType(edgeType) {
michael@0 48 this->setWillReadFragmentPosition();
michael@0 49 }
michael@0 50
michael@0 51 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
michael@0 52 const AARectEffect& aare = CastEffect<AARectEffect>(other);
michael@0 53 return fRect == aare.fRect;
michael@0 54 }
michael@0 55
michael@0 56 SkRect fRect;
michael@0 57 GrEffectEdgeType fEdgeType;
michael@0 58
michael@0 59 typedef GrEffect INHERITED;
michael@0 60
michael@0 61 GR_DECLARE_EFFECT_TEST;
michael@0 62
michael@0 63 };
michael@0 64
michael@0 65 GR_DEFINE_EFFECT_TEST(AARectEffect);
michael@0 66
michael@0 67 GrEffectRef* AARectEffect::TestCreate(SkRandom* random,
michael@0 68 GrContext*,
michael@0 69 const GrDrawTargetCaps& caps,
michael@0 70 GrTexture*[]) {
michael@0 71 SkRect rect = SkRect::MakeLTRB(random->nextSScalar1(),
michael@0 72 random->nextSScalar1(),
michael@0 73 random->nextSScalar1(),
michael@0 74 random->nextSScalar1());
michael@0 75 GrEffectRef* effect;
michael@0 76 do {
michael@0 77 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan(
michael@0 78 kGrEffectEdgeTypeCnt));
michael@0 79
michael@0 80 effect = AARectEffect::Create(edgeType, rect);
michael@0 81 } while (NULL == effect);
michael@0 82 return effect;
michael@0 83 }
michael@0 84
michael@0 85 //////////////////////////////////////////////////////////////////////////////
michael@0 86
michael@0 87 class GLAARectEffect : public GrGLEffect {
michael@0 88 public:
michael@0 89 GLAARectEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
michael@0 90
michael@0 91 virtual void emitCode(GrGLShaderBuilder* builder,
michael@0 92 const GrDrawEffect& drawEffect,
michael@0 93 EffectKey key,
michael@0 94 const char* outputColor,
michael@0 95 const char* inputColor,
michael@0 96 const TransformedCoordsArray&,
michael@0 97 const TextureSamplerArray&) SK_OVERRIDE;
michael@0 98
michael@0 99 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
michael@0 100
michael@0 101 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
michael@0 102
michael@0 103 private:
michael@0 104 GrGLUniformManager::UniformHandle fRectUniform;
michael@0 105 SkRect fPrevRect;
michael@0 106 typedef GrGLEffect INHERITED;
michael@0 107 };
michael@0 108
michael@0 109 GLAARectEffect::GLAARectEffect(const GrBackendEffectFactory& factory,
michael@0 110 const GrDrawEffect& drawEffect)
michael@0 111 : INHERITED (factory) {
michael@0 112 fPrevRect.fLeft = SK_ScalarNaN;
michael@0 113 }
michael@0 114
michael@0 115 void GLAARectEffect::emitCode(GrGLShaderBuilder* builder,
michael@0 116 const GrDrawEffect& drawEffect,
michael@0 117 EffectKey key,
michael@0 118 const char* outputColor,
michael@0 119 const char* inputColor,
michael@0 120 const TransformedCoordsArray&,
michael@0 121 const TextureSamplerArray& samplers) {
michael@0 122 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>();
michael@0 123 const char *rectName;
michael@0 124 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
michael@0 125 // respectively.
michael@0 126 fRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
michael@0 127 kVec4f_GrSLType,
michael@0 128 "rect",
michael@0 129 &rectName);
michael@0 130 const char* fragmentPos = builder->fragmentPosition();
michael@0 131 if (GrEffectEdgeTypeIsAA(aare.getEdgeType())) {
michael@0 132 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
michael@0 133 // numbers, xSub and ySub.
michael@0 134 builder->fsCodeAppend("\t\tfloat xSub, ySub;\n");
michael@0 135 builder->fsCodeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
michael@0 136 builder->fsCodeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
michael@0 137 builder->fsCodeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
michael@0 138 builder->fsCodeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
michael@0 139 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
michael@0 140 // covered.
michael@0 141 builder->fsCodeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n");
michael@0 142 } else {
michael@0 143 builder->fsCodeAppendf("\t\tfloat alpha = 1.0;\n");
michael@0 144 builder->fsCodeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
michael@0 145 builder->fsCodeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
michael@0 146 builder->fsCodeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
michael@0 147 builder->fsCodeAppendf("\t\talpha *= (%s.w - %s.y) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
michael@0 148 }
michael@0 149
michael@0 150 if (GrEffectEdgeTypeIsInverseFill(aare.getEdgeType())) {
michael@0 151 builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n");
michael@0 152 }
michael@0 153 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor,
michael@0 154 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
michael@0 155 }
michael@0 156
michael@0 157 void GLAARectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
michael@0 158 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>();
michael@0 159 const SkRect& rect = aare.getRect();
michael@0 160 if (rect != fPrevRect) {
michael@0 161 uman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
michael@0 162 rect.fRight - 0.5f, rect.fBottom - 0.5f);
michael@0 163 fPrevRect = rect;
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 GrGLEffect::EffectKey GLAARectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
michael@0 168 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>();
michael@0 169 return aare.getEdgeType();
michael@0 170 }
michael@0 171
michael@0 172 const GrBackendEffectFactory& AARectEffect::getFactory() const {
michael@0 173 return GrTBackendEffectFactory<AARectEffect>::getInstance();
michael@0 174 }
michael@0 175
michael@0 176 //////////////////////////////////////////////////////////////////////////////
michael@0 177
michael@0 178 class GrGLConvexPolyEffect : public GrGLEffect {
michael@0 179 public:
michael@0 180 GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
michael@0 181
michael@0 182 virtual void emitCode(GrGLShaderBuilder* builder,
michael@0 183 const GrDrawEffect& drawEffect,
michael@0 184 EffectKey key,
michael@0 185 const char* outputColor,
michael@0 186 const char* inputColor,
michael@0 187 const TransformedCoordsArray&,
michael@0 188 const TextureSamplerArray&) SK_OVERRIDE;
michael@0 189
michael@0 190 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
michael@0 191
michael@0 192 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
michael@0 193
michael@0 194 private:
michael@0 195 GrGLUniformManager::UniformHandle fEdgeUniform;
michael@0 196 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
michael@0 197 typedef GrGLEffect INHERITED;
michael@0 198 };
michael@0 199
michael@0 200 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrBackendEffectFactory& factory,
michael@0 201 const GrDrawEffect& drawEffect)
michael@0 202 : INHERITED (factory) {
michael@0 203 fPrevEdges[0] = SK_ScalarNaN;
michael@0 204 }
michael@0 205
michael@0 206 void GrGLConvexPolyEffect::emitCode(GrGLShaderBuilder* builder,
michael@0 207 const GrDrawEffect& drawEffect,
michael@0 208 EffectKey key,
michael@0 209 const char* outputColor,
michael@0 210 const char* inputColor,
michael@0 211 const TransformedCoordsArray&,
michael@0 212 const TextureSamplerArray& samplers) {
michael@0 213 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
michael@0 214
michael@0 215 const char *edgeArrayName;
michael@0 216 fEdgeUniform = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility,
michael@0 217 kVec3f_GrSLType,
michael@0 218 "edges",
michael@0 219 cpe.getEdgeCount(),
michael@0 220 &edgeArrayName);
michael@0 221 builder->fsCodeAppend("\t\tfloat alpha = 1.0;\n");
michael@0 222 builder->fsCodeAppend("\t\tfloat edge;\n");
michael@0 223 const char* fragmentPos = builder->fragmentPosition();
michael@0 224 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
michael@0 225 builder->fsCodeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
michael@0 226 edgeArrayName, i, fragmentPos, fragmentPos);
michael@0 227 if (GrEffectEdgeTypeIsAA(cpe.getEdgeType())) {
michael@0 228 builder->fsCodeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
michael@0 229 } else {
michael@0 230 builder->fsCodeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
michael@0 231 }
michael@0 232 builder->fsCodeAppend("\t\talpha *= edge;\n");
michael@0 233 }
michael@0 234
michael@0 235 // Woe is me. See skbug.com/2149.
michael@0 236 if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) {
michael@0 237 builder->fsCodeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n");
michael@0 238 }
michael@0 239
michael@0 240 if (GrEffectEdgeTypeIsInverseFill(cpe.getEdgeType())) {
michael@0 241 builder->fsCodeAppend("\talpha = 1.0 - alpha;\n");
michael@0 242 }
michael@0 243 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
michael@0 244 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
michael@0 245 }
michael@0 246
michael@0 247 void GrGLConvexPolyEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
michael@0 248 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
michael@0 249 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
michael@0 250 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
michael@0 251 uman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
michael@0 252 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
michael@0 253 }
michael@0 254 }
michael@0 255
michael@0 256 GrGLEffect::EffectKey GrGLConvexPolyEffect::GenKey(const GrDrawEffect& drawEffect,
michael@0 257 const GrGLCaps&) {
michael@0 258 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
michael@0 259 GR_STATIC_ASSERT(kGrEffectEdgeTypeCnt <= 8);
michael@0 260 return (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
michael@0 261 }
michael@0 262
michael@0 263 //////////////////////////////////////////////////////////////////////////////
michael@0 264
michael@0 265 GrEffectRef* GrConvexPolyEffect::Create(GrEffectEdgeType type, const SkPath& path, const SkVector* offset) {
michael@0 266 if (kHairlineAA_GrEffectEdgeType == type) {
michael@0 267 return NULL;
michael@0 268 }
michael@0 269 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
michael@0 270 !path.isConvex()) {
michael@0 271 return NULL;
michael@0 272 }
michael@0 273
michael@0 274 if (path.countPoints() > kMaxEdges) {
michael@0 275 return NULL;
michael@0 276 }
michael@0 277
michael@0 278 SkPoint pts[kMaxEdges];
michael@0 279 SkScalar edges[3 * kMaxEdges];
michael@0 280
michael@0 281 SkPath::Direction dir;
michael@0 282 SkAssertResult(path.cheapComputeDirection(&dir));
michael@0 283
michael@0 284 SkVector t;
michael@0 285 if (NULL == offset) {
michael@0 286 t.set(0, 0);
michael@0 287 } else {
michael@0 288 t = *offset;
michael@0 289 }
michael@0 290
michael@0 291 int count = path.getPoints(pts, kMaxEdges);
michael@0 292 int n = 0;
michael@0 293 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
michael@0 294 if (pts[lastPt] != pts[i]) {
michael@0 295 SkVector v = pts[i] - pts[lastPt];
michael@0 296 v.normalize();
michael@0 297 if (SkPath::kCCW_Direction == dir) {
michael@0 298 edges[3 * n] = v.fY;
michael@0 299 edges[3 * n + 1] = -v.fX;
michael@0 300 } else {
michael@0 301 edges[3 * n] = -v.fY;
michael@0 302 edges[3 * n + 1] = v.fX;
michael@0 303 }
michael@0 304 SkPoint p = pts[i] + t;
michael@0 305 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
michael@0 306 ++n;
michael@0 307 }
michael@0 308 }
michael@0 309 if (path.isInverseFillType()) {
michael@0 310 type = GrInvertEffectEdgeType(type);
michael@0 311 }
michael@0 312 return Create(type, n, edges);
michael@0 313 }
michael@0 314
michael@0 315 GrEffectRef* GrConvexPolyEffect::Create(GrEffectEdgeType edgeType, const SkRect& rect) {
michael@0 316 if (kHairlineAA_GrEffectEdgeType == edgeType){
michael@0 317 return NULL;
michael@0 318 }
michael@0 319 return AARectEffect::Create(edgeType, rect);
michael@0 320 }
michael@0 321
michael@0 322 GrConvexPolyEffect::~GrConvexPolyEffect() {}
michael@0 323
michael@0 324 void GrConvexPolyEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
michael@0 325 *validFlags = 0;
michael@0 326 }
michael@0 327
michael@0 328 const GrBackendEffectFactory& GrConvexPolyEffect::getFactory() const {
michael@0 329 return GrTBackendEffectFactory<GrConvexPolyEffect>::getInstance();
michael@0 330 }
michael@0 331
michael@0 332 GrConvexPolyEffect::GrConvexPolyEffect(GrEffectEdgeType edgeType, int n, const SkScalar edges[])
michael@0 333 : fEdgeType(edgeType)
michael@0 334 , fEdgeCount(n) {
michael@0 335 // Factory function should have already ensured this.
michael@0 336 SkASSERT(n <= kMaxEdges);
michael@0 337 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
michael@0 338 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
michael@0 339 // and 100% covered in the non-AA case.
michael@0 340 for (int i = 0; i < n; ++i) {
michael@0 341 fEdges[3 * i + 2] += SK_ScalarHalf;
michael@0 342 }
michael@0 343 this->setWillReadFragmentPosition();
michael@0 344 }
michael@0 345
michael@0 346 bool GrConvexPolyEffect::onIsEqual(const GrEffect& other) const {
michael@0 347 const GrConvexPolyEffect& cpe = CastEffect<GrConvexPolyEffect>(other);
michael@0 348 // ignore the fact that 0 == -0 and just use memcmp.
michael@0 349 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
michael@0 350 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
michael@0 351 }
michael@0 352
michael@0 353 //////////////////////////////////////////////////////////////////////////////
michael@0 354
michael@0 355 GR_DEFINE_EFFECT_TEST(GrConvexPolyEffect);
michael@0 356
michael@0 357 GrEffectRef* GrConvexPolyEffect::TestCreate(SkRandom* random,
michael@0 358 GrContext*,
michael@0 359 const GrDrawTargetCaps& caps,
michael@0 360 GrTexture*[]) {
michael@0 361 int count = random->nextULessThan(kMaxEdges) + 1;
michael@0 362 SkScalar edges[kMaxEdges * 3];
michael@0 363 for (int i = 0; i < 3 * count; ++i) {
michael@0 364 edges[i] = random->nextSScalar1();
michael@0 365 }
michael@0 366
michael@0 367 GrEffectRef* effect;
michael@0 368 do {
michael@0 369 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(
michael@0 370 random->nextULessThan(kGrEffectEdgeTypeCnt));
michael@0 371 effect = GrConvexPolyEffect::Create(edgeType, count, edges);
michael@0 372 } while (NULL == effect);
michael@0 373 return effect;
michael@0 374 }

mercurial