| Index: src/gpu/effects/GrDashingEffect.cpp
|
| diff --git a/src/gpu/effects/GrDashingEffect.cpp b/src/gpu/effects/GrDashingEffect.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..7d28a569b7e57ac6cafcc974c603b2ae03097ca5
|
| --- /dev/null
|
| +++ b/src/gpu/effects/GrDashingEffect.cpp
|
| @@ -0,0 +1,182 @@
|
| +/*
|
| + * Copyright 2014 Google Inc.
|
| + *
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + */
|
| +
|
| +#include "GrDashingEffect.h"
|
| +
|
| +#include "gl/GrGLEffect.h"
|
| +#include "gl/GrGLSL.h"
|
| +#include "GrTBackendEffectFactory.h"
|
| +
|
| +#include "SkPath.h"
|
| +
|
| +//////////////////////////////////////////////////////////////////////////////
|
| +
|
| +class GrGLDashingEffect : public GrGLEffect {
|
| +public:
|
| + GrGLDashingEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
|
| +
|
| + virtual void emitCode(GrGLShaderBuilder* builder,
|
| + const GrDrawEffect& drawEffect,
|
| + EffectKey key,
|
| + const char* outputColor,
|
| + const char* inputColor,
|
| + const TransformedCoordsArray&,
|
| + const TextureSamplerArray&) SK_OVERRIDE;
|
| +
|
| + static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
|
| +
|
| + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
|
| +
|
| +private:
|
| + GrGLUniformManager::UniformHandle fRectUniform;
|
| + GrGLUniformManager::UniformHandle fIntervalUniform;
|
| + SkRect fPrevRect;
|
| + SkScalar fPrevIntervalLength;
|
| + typedef GrGLEffect INHERITED;
|
| +};
|
| +
|
| +GrGLDashingEffect::GrGLDashingEffect(const GrBackendEffectFactory& factory,
|
| + const GrDrawEffect& drawEffect)
|
| + : INHERITED (factory) {
|
| + fPrevRect.fLeft = SK_ScalarNaN;
|
| + fPrevIntervalLength = SK_ScalarMax;
|
| +
|
| +}
|
| +
|
| +void GrGLDashingEffect::emitCode(GrGLShaderBuilder* builder,
|
| + const GrDrawEffect& drawEffect,
|
| + EffectKey key,
|
| + const char* outputColor,
|
| + const char* inputColor,
|
| + const TransformedCoordsArray& coords,
|
| + const TextureSamplerArray& samplers) {
|
| + const GrDashingEffect& de = drawEffect.castEffect<GrDashingEffect>();
|
| + const char *rectName;
|
| + // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
|
| + // respectively.
|
| + fRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
|
| + kVec4f_GrSLType,
|
| + "rect",
|
| + &rectName);
|
| + const char *intervalName;
|
| + // The interval uniform's refers to the total length of the interval (on + off)
|
| + fIntervalUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
|
| + kFloat_GrSLType,
|
| + "interval",
|
| + &intervalName);
|
| + // transforms all points so that we can compare them to our test rect
|
| + builder->fsCodeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
|
| + coords[0].c_str(), coords[0].c_str(), intervalName, intervalName);
|
| + builder->fsCodeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", coords[0].c_str());
|
| + if (GrEffectEdgeTypeIsAA(de.getEdgeType())) {
|
| + // The amount of coverage removed in x and y by the edges is computed as a pair of negative
|
| + // numbers, xSub and ySub.
|
| + builder->fsCodeAppend("\t\tfloat xSub, ySub;\n");
|
| + builder->fsCodeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
|
| + builder->fsCodeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
|
| + builder->fsCodeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
|
| + builder->fsCodeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
|
| + // Now compute coverage in x and y and multiply them to get the fraction of the pixel
|
| + // covered.
|
| + builder->fsCodeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n");
|
| + } else {
|
| + // Assuming the bounding geometry is tight so no need to check y values
|
| + builder->fsCodeAppendf("\t\tfloat alpha = 1.0;\n");
|
| + builder->fsCodeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
|
| + builder->fsCodeAppendf("\t\talpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;\n", rectName);
|
| + }
|
| + builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor,
|
| + (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
|
| +}
|
| +
|
| +void GrGLDashingEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
|
| + const GrDashingEffect& de = drawEffect.castEffect<GrDashingEffect>();
|
| + const SkRect& rect = de.getRect();
|
| + SkScalar intervalLength = de.getIntervalLength();
|
| + if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
|
| + uman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
|
| + rect.fRight - 0.5f, rect.fBottom - 0.5f);
|
| + uman.set1f(fIntervalUniform, intervalLength);
|
| + fPrevRect = rect;
|
| + fPrevIntervalLength = intervalLength;
|
| + }
|
| +}
|
| +
|
| +GrGLEffect::EffectKey GrGLDashingEffect::GenKey(const GrDrawEffect& drawEffect,
|
| + const GrGLCaps&) {
|
| + const GrDashingEffect& cpe = drawEffect.castEffect<GrDashingEffect>();
|
| + return cpe.getEdgeType();
|
| +}
|
| +
|
| +//////////////////////////////////////////////////////////////////////////////
|
| +
|
| +GrEffectRef* GrDashingEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info,
|
| + const SkMatrix& matrix, SkScalar strokeWidth) {
|
| + if (info.fCount != 2) {
|
| + return NULL;
|
| + }
|
| +
|
| + return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrDashingEffect,
|
| + (edgeType, info, matrix, strokeWidth))));
|
| +}
|
| +
|
| +GrDashingEffect::~GrDashingEffect() {}
|
| +
|
| +void GrDashingEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
|
| + *validFlags = 0;
|
| +}
|
| +
|
| +const GrBackendEffectFactory& GrDashingEffect::getFactory() const {
|
| + return GrTBackendEffectFactory<GrDashingEffect>::getInstance();
|
| +}
|
| +
|
| +GrDashingEffect::GrDashingEffect(GrEffectEdgeType edgeType, const DashInfo& info,
|
| + const SkMatrix& matrix, SkScalar strokeWidth)
|
| + : fEdgeType(edgeType)
|
| + , fCoordTransform(kLocal_GrCoordSet, matrix) {
|
| + SkScalar onLen = info.fIntervals[0];
|
| + SkScalar offLen = info.fIntervals[1];
|
| + SkScalar halfOffLen = SkScalarHalf(offLen);
|
| + SkScalar halfStroke = SkScalarHalf(strokeWidth);
|
| + fIntervalLength = onLen + offLen;
|
| + fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
|
| +
|
| + addCoordTransform(&fCoordTransform);
|
| +}
|
| +
|
| +bool GrDashingEffect::onIsEqual(const GrEffect& other) const {
|
| + const GrDashingEffect& de = CastEffect<GrDashingEffect>(other);
|
| + return (fEdgeType == de.fEdgeType &&
|
| + fCoordTransform == de.fCoordTransform &&
|
| + fRect == de.fRect &&
|
| + fIntervalLength == de.fIntervalLength);
|
| +}
|
| +
|
| +GR_DEFINE_EFFECT_TEST(GrDashingEffect);
|
| +
|
| +GrEffectRef* GrDashingEffect::TestCreate(SkRandom* random,
|
| + GrContext*,
|
| + const GrDrawTargetCaps& caps,
|
| + GrTexture*[]) {
|
| + GrEffectRef* effect;
|
| + SkMatrix m;
|
| + m.reset();
|
| + GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan(
|
| + kGrEffectEdgeTypeCnt));
|
| + SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
|
| + DashInfo info;
|
| + info.fCount = 2;
|
| + SkAutoTArray<SkScalar> intervals(info.fCount);
|
| + info.fIntervals = intervals.get();
|
| + info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
|
| + info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
|
| + info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
|
| +
|
| + effect = GrDashingEffect::Create(edgeType, info, m, strokeWidth);
|
| + return effect;
|
| +}
|
| +
|
|
|