| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2013 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 | #include "GrEllipseEdgeEffect.h" |
| 9 | #include "gl/GrGLEffect.h" |
| 10 | #include "gl/GrGLEffectMatrix.h" |
| 11 | #include "gl/GrGLSL.h" |
| 12 | #include "gl/GrGLTexture.h" |
| 13 | #include "GrTBackendEffectFactory.h" |
| 14 | #include "GrTexture.h" |
| 15 | |
| 16 | class GrGLEllipseEdgeEffect : public GrGLEffect { |
| 17 | public: |
| 18 | GrGLEllipseEdgeEffect(const GrBackendEffectFactory& factory, const GrEffectRef&) |
| 19 | : INHERITED (factory) {} |
| 20 | |
| 21 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 22 | const GrEffectStage& stage, |
| 23 | EffectKey key, |
| 24 | const char* vertexCoords, |
| 25 | const char* outputColor, |
| 26 | const char* inputColor, |
| 27 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 28 | const char *vsName, *fsName; |
| 29 | builder->addVarying(kVec4f_GrSLType, "EllipseEdge", &vsName, &fsName); |
| 30 | |
| 31 | const SkString* attrName = builder->getEffectAttributeName(stage.getVertexAttribIndices()[0]); |
| 32 | builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 33 | |
| 34 | builder->fsCodeAppend("\tfloat edgeAlpha;\n"); |
| 35 | // translate to origin |
| 36 | builder->fsCodeAppendf("\tvec2 offset = (%s.xy - %s.xy);\n", builder->fragmentPosition(), fsName); |
| 37 | // scale y by xRadius/yRadius |
| 38 | builder->fsCodeAppendf("\toffset.y *= %s.w;\n", fsName); |
| 39 | builder->fsCodeAppend("\tfloat d = length(offset);\n"); |
| 40 | // compare length against xRadius |
| 41 | builder->fsCodeAppendf("\tedgeAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName); |
| 42 | SkString modulate; |
| 43 | GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha"); |
| 44 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 45 | } |
| 46 | |
| 47 | static inline EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE { |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | typedef GrGLEffect INHERITED; |
| 56 | }; |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | GrEllipseEdgeEffect::GrEllipseEdgeEffect() : GrEffect() { |
| 61 | this->addVertexAttrib(kVec4f_GrSLType); |
| 62 | } |
| 63 | |
| 64 | void GrEllipseEdgeEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 65 | *validFlags = 0; |
| 66 | } |
| 67 | |
| 68 | const GrBackendEffectFactory& GrEllipseEdgeEffect::getFactory() const { |
| 69 | return GrTBackendEffectFactory<GrEllipseEdgeEffect>::getInstance(); |
| 70 | } |
| 71 | |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | GR_DEFINE_EFFECT_TEST(GrEllipseEdgeEffect); |
| 75 | |
| 76 | GrEffectRef* GrEllipseEdgeEffect::TestCreate(SkMWCRandom* random, |
| 77 | GrContext* context, |
| 78 | GrTexture* textures[]) { |
| 79 | return GrEllipseEdgeEffect::Create(); |
| 80 | } |