| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gl/builders/GrGLProgramBuilder.h" | 8 #include "gl/builders/GrGLProgramBuilder.h" |
| 9 #include "GrOvalEffect.h" | 9 #include "GrOvalEffect.h" |
| 10 | 10 |
| 11 #include "gl/GrGLEffect.h" | 11 #include "gl/GrGLProcessor.h" |
| 12 #include "gl/GrGLSL.h" | 12 #include "gl/GrGLSL.h" |
| 13 #include "GrTBackendEffectFactory.h" | 13 #include "GrTBackendProcessorFactory.h" |
| 14 | 14 |
| 15 #include "SkRect.h" | 15 #include "SkRect.h" |
| 16 | 16 |
| 17 ////////////////////////////////////////////////////////////////////////////// | 17 ////////////////////////////////////////////////////////////////////////////// |
| 18 | 18 |
| 19 class GLCircleEffect; | 19 class GLCircleEffect; |
| 20 | 20 |
| 21 class CircleEffect : public GrEffect { | 21 class CircleEffect : public GrFragmentProcessor { |
| 22 public: | 22 public: |
| 23 static GrEffect* Create(GrEffectEdgeType, const SkPoint& center, SkScalar ra
dius); | 23 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPoint& cente
r, SkScalar radius); |
| 24 | 24 |
| 25 virtual ~CircleEffect() {}; | 25 virtual ~CircleEffect() {}; |
| 26 static const char* Name() { return "Circle"; } | 26 static const char* Name() { return "Circle"; } |
| 27 | 27 |
| 28 const SkPoint& getCenter() const { return fCenter; } | 28 const SkPoint& getCenter() const { return fCenter; } |
| 29 SkScalar getRadius() const { return fRadius; } | 29 SkScalar getRadius() const { return fRadius; } |
| 30 | 30 |
| 31 GrEffectEdgeType getEdgeType() const { return fEdgeType; } | 31 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
| 32 | 32 |
| 33 typedef GLCircleEffect GLEffect; | 33 typedef GLCircleEffect GLProcessor; |
| 34 | 34 |
| 35 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; | 35 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 36 | 36 |
| 37 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 37 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE; |
| 38 | 38 |
| 39 private: | 39 private: |
| 40 CircleEffect(GrEffectEdgeType, const SkPoint& center, SkScalar radius); | 40 CircleEffect(GrPrimitiveEdgeType, const SkPoint& center, SkScalar radius); |
| 41 | 41 |
| 42 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 42 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE; |
| 43 | 43 |
| 44 SkPoint fCenter; | 44 SkPoint fCenter; |
| 45 SkScalar fRadius; | 45 SkScalar fRadius; |
| 46 GrEffectEdgeType fEdgeType; | 46 GrPrimitiveEdgeType fEdgeType; |
| 47 | 47 |
| 48 GR_DECLARE_EFFECT_TEST; | 48 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 49 | 49 |
| 50 typedef GrEffect INHERITED; | 50 typedef GrFragmentProcessor INHERITED; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 GrEffect* CircleEffect::Create(GrEffectEdgeType edgeType, const SkPoint& center,
SkScalar radius) { | 53 GrFragmentProcessor* CircleEffect::Create(GrPrimitiveEdgeType edgeType, const Sk
Point& center, |
| 54 SkScalar radius) { |
| 54 SkASSERT(radius >= 0); | 55 SkASSERT(radius >= 0); |
| 55 return SkNEW_ARGS(CircleEffect, (edgeType, center, radius)); | 56 return SkNEW_ARGS(CircleEffect, (edgeType, center, radius)); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void CircleEffect::getConstantColorComponents(GrColor* color, uint32_t* validFla
gs) const { | 59 void CircleEffect::getConstantColorComponents(GrColor* color, uint32_t* validFla
gs) const { |
| 59 *validFlags = 0; | 60 *validFlags = 0; |
| 60 } | 61 } |
| 61 | 62 |
| 62 const GrBackendEffectFactory& CircleEffect::getFactory() const { | 63 const GrBackendFragmentProcessorFactory& CircleEffect::getFactory() const { |
| 63 return GrTBackendEffectFactory<CircleEffect>::getInstance(); | 64 return GrTBackendFragmentProcessorFactory<CircleEffect>::getInstance(); |
| 64 } | 65 } |
| 65 | 66 |
| 66 CircleEffect::CircleEffect(GrEffectEdgeType edgeType, const SkPoint& c, SkScalar
r) | 67 CircleEffect::CircleEffect(GrPrimitiveEdgeType edgeType, const SkPoint& c, SkSca
lar r) |
| 67 : fCenter(c) | 68 : fCenter(c) |
| 68 , fRadius(r) | 69 , fRadius(r) |
| 69 , fEdgeType(edgeType) { | 70 , fEdgeType(edgeType) { |
| 70 this->setWillReadFragmentPosition(); | 71 this->setWillReadFragmentPosition(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 bool CircleEffect::onIsEqual(const GrEffect& other) const { | 74 bool CircleEffect::onIsEqual(const GrProcessor& other) const { |
| 74 const CircleEffect& ce = other.cast<CircleEffect>(); | 75 const CircleEffect& ce = other.cast<CircleEffect>(); |
| 75 return fEdgeType == ce.fEdgeType && fCenter == ce.fCenter && fRadius == ce.f
Radius; | 76 return fEdgeType == ce.fEdgeType && fCenter == ce.fCenter && fRadius == ce.f
Radius; |
| 76 } | 77 } |
| 77 | 78 |
| 78 ////////////////////////////////////////////////////////////////////////////// | 79 ////////////////////////////////////////////////////////////////////////////// |
| 79 | 80 |
| 80 GR_DEFINE_EFFECT_TEST(CircleEffect); | 81 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleEffect); |
| 81 | 82 |
| 82 GrEffect* CircleEffect::TestCreate(SkRandom* random, | 83 GrFragmentProcessor* CircleEffect::TestCreate(SkRandom* random, |
| 83 GrContext*, | 84 GrContext*, |
| 84 const GrDrawTargetCaps& caps, | 85 const GrDrawTargetCaps& caps, |
| 85 GrTexture*[]) { | 86 GrTexture*[]) { |
| 86 SkPoint center; | 87 SkPoint center; |
| 87 center.fX = random->nextRangeScalar(0.f, 1000.f); | 88 center.fX = random->nextRangeScalar(0.f, 1000.f); |
| 88 center.fY = random->nextRangeScalar(0.f, 1000.f); | 89 center.fY = random->nextRangeScalar(0.f, 1000.f); |
| 89 SkScalar radius = random->nextRangeF(0.f, 1000.f); | 90 SkScalar radius = random->nextRangeF(0.f, 1000.f); |
| 90 GrEffectEdgeType et; | 91 GrPrimitiveEdgeType et; |
| 91 do { | 92 do { |
| 92 et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt); | 93 et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt)
; |
| 93 } while (kHairlineAA_GrEffectEdgeType == et); | 94 } while (kHairlineAA_GrProcessorEdgeType == et); |
| 94 return CircleEffect::Create(et, center, radius); | 95 return CircleEffect::Create(et, center, radius); |
| 95 } | 96 } |
| 96 | 97 |
| 97 ////////////////////////////////////////////////////////////////////////////// | 98 ////////////////////////////////////////////////////////////////////////////// |
| 98 | 99 |
| 99 class GLCircleEffect : public GrGLEffect { | 100 class GLCircleEffect : public GrGLFragmentProcessor { |
| 100 public: | 101 public: |
| 101 GLCircleEffect(const GrBackendEffectFactory&, const GrEffect&); | 102 GLCircleEffect(const GrBackendProcessorFactory&, const GrProcessor&); |
| 102 | 103 |
| 103 virtual void emitCode(GrGLProgramBuilder* builder, | 104 virtual void emitCode(GrGLProgramBuilder* builder, |
| 104 const GrEffect& effect, | 105 const GrFragmentProcessor& fp, |
| 105 const GrEffectKey& key, | 106 const GrProcessorKey& key, |
| 106 const char* outputColor, | 107 const char* outputColor, |
| 107 const char* inputColor, | 108 const char* inputColor, |
| 108 const TransformedCoordsArray&, | 109 const TransformedCoordsArray&, |
| 109 const TextureSamplerArray&) SK_OVERRIDE; | 110 const TextureSamplerArray&) SK_OVERRIDE; |
| 110 | 111 |
| 111 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuild
er*); | 112 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKe
yBuilder*); |
| 112 | 113 |
| 113 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER
RIDE; | 114 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE; |
| 114 | 115 |
| 115 private: | 116 private: |
| 116 GrGLProgramDataManager::UniformHandle fCircleUniform; | 117 GrGLProgramDataManager::UniformHandle fCircleUniform; |
| 117 SkPoint fPrevCenter; | 118 SkPoint fPrevCenter; |
| 118 SkScalar fPrevRadius; | 119 SkScalar fPrevRadius; |
| 119 | 120 |
| 120 typedef GrGLEffect INHERITED; | 121 typedef GrGLFragmentProcessor INHERITED; |
| 121 }; | 122 }; |
| 122 | 123 |
| 123 GLCircleEffect::GLCircleEffect(const GrBackendEffectFactory& factory, | 124 GLCircleEffect::GLCircleEffect(const GrBackendProcessorFactory& factory, |
| 124 const GrEffect& effect) | 125 const GrProcessor&) |
| 125 : INHERITED (factory) { | 126 : INHERITED (factory) { |
| 126 fPrevRadius = -1.f; | 127 fPrevRadius = -1.f; |
| 127 } | 128 } |
| 128 | 129 |
| 129 void GLCircleEffect::emitCode(GrGLProgramBuilder* builder, | 130 void GLCircleEffect::emitCode(GrGLProgramBuilder* builder, |
| 130 const GrEffect& effect, | 131 const GrFragmentProcessor& fp, |
| 131 const GrEffectKey& key, | 132 const GrProcessorKey& key, |
| 132 const char* outputColor, | 133 const char* outputColor, |
| 133 const char* inputColor, | 134 const char* inputColor, |
| 134 const TransformedCoordsArray&, | 135 const TransformedCoordsArray&, |
| 135 const TextureSamplerArray& samplers) { | 136 const TextureSamplerArray& samplers) { |
| 136 const CircleEffect& ce = effect.cast<CircleEffect>(); | 137 const CircleEffect& ce = fp.cast<CircleEffect>(); |
| 137 const char *circleName; | 138 const char *circleName; |
| 138 // The circle uniform is (center.x, center.y, radius + 0.5) for regular fill
s and | 139 // The circle uniform is (center.x, center.y, radius + 0.5) for regular fill
s and |
| 139 // (... ,radius - 0.5) for inverse fills. | 140 // (... ,radius - 0.5) for inverse fills. |
| 140 fCircleUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, | 141 fCircleUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, |
| 141 kVec3f_GrSLType, | 142 kVec3f_GrSLType, |
| 142 "circle", | 143 "circle", |
| 143 &circleName); | 144 &circleName); |
| 144 | 145 |
| 145 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 146 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 146 const char* fragmentPos = fsBuilder->fragmentPosition(); | 147 const char* fragmentPos = fsBuilder->fragmentPosition(); |
| 147 | 148 |
| 148 SkASSERT(kHairlineAA_GrEffectEdgeType != ce.getEdgeType()); | 149 SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType()); |
| 149 if (GrEffectEdgeTypeIsInverseFill(ce.getEdgeType())) { | 150 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { |
| 150 fsBuilder->codeAppendf("\t\tfloat d = length(%s.xy - %s.xy) - %s.z;\n", | 151 fsBuilder->codeAppendf("\t\tfloat d = length(%s.xy - %s.xy) - %s.z;\n", |
| 151 circleName, fragmentPos, circleName); | 152 circleName, fragmentPos, circleName); |
| 152 } else { | 153 } else { |
| 153 fsBuilder->codeAppendf("\t\tfloat d = %s.z - length(%s.xy - %s.xy);\n", | 154 fsBuilder->codeAppendf("\t\tfloat d = %s.z - length(%s.xy - %s.xy);\n", |
| 154 circleName, fragmentPos, circleName); | 155 circleName, fragmentPos, circleName); |
| 155 } | 156 } |
| 156 if (GrEffectEdgeTypeIsAA(ce.getEdgeType())) { | 157 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) { |
| 157 fsBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n"); | 158 fsBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n"); |
| 158 } else { | 159 } else { |
| 159 fsBuilder->codeAppend("\t\td = d > 0.5 ? 1.0 : 0.0;\n"); | 160 fsBuilder->codeAppend("\t\td = d > 0.5 ? 1.0 : 0.0;\n"); |
| 160 } | 161 } |
| 161 | 162 |
| 162 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, | 163 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, |
| 163 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("d")).c_str())
; | 164 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("d")).c_str())
; |
| 164 } | 165 } |
| 165 | 166 |
| 166 void GLCircleEffect::GenKey(const GrEffect& effect, const GrGLCaps&, | 167 void GLCircleEffect::GenKey(const GrProcessor& processor, const GrGLCaps&, |
| 167 GrEffectKeyBuilder* b) { | 168 GrProcessorKeyBuilder* b) { |
| 168 const CircleEffect& ce = effect.cast<CircleEffect>(); | 169 const CircleEffect& ce = processor.cast<CircleEffect>(); |
| 169 b->add32(ce.getEdgeType()); | 170 b->add32(ce.getEdgeType()); |
| 170 } | 171 } |
| 171 | 172 |
| 172 void GLCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrEffect
& effect) { | 173 void GLCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrProces
sor& processor) { |
| 173 const CircleEffect& ce = effect.cast<CircleEffect>(); | 174 const CircleEffect& ce = processor.cast<CircleEffect>(); |
| 174 if (ce.getRadius() != fPrevRadius || ce.getCenter() != fPrevCenter) { | 175 if (ce.getRadius() != fPrevRadius || ce.getCenter() != fPrevCenter) { |
| 175 SkScalar radius = ce.getRadius(); | 176 SkScalar radius = ce.getRadius(); |
| 176 if (GrEffectEdgeTypeIsInverseFill(ce.getEdgeType())) { | 177 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { |
| 177 radius -= 0.5f; | 178 radius -= 0.5f; |
| 178 } else { | 179 } else { |
| 179 radius += 0.5f; | 180 radius += 0.5f; |
| 180 } | 181 } |
| 181 pdman.set3f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius
); | 182 pdman.set3f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius
); |
| 182 fPrevCenter = ce.getCenter(); | 183 fPrevCenter = ce.getCenter(); |
| 183 fPrevRadius = ce.getRadius(); | 184 fPrevRadius = ce.getRadius(); |
| 184 } | 185 } |
| 185 } | 186 } |
| 186 | 187 |
| 187 ////////////////////////////////////////////////////////////////////////////// | 188 ////////////////////////////////////////////////////////////////////////////// |
| 188 | 189 |
| 189 class GLEllipseEffect; | 190 class GLEllipseEffect; |
| 190 | 191 |
| 191 class EllipseEffect : public GrEffect { | 192 class EllipseEffect : public GrFragmentProcessor { |
| 192 public: | 193 public: |
| 193 static GrEffect* Create(GrEffectEdgeType, const SkPoint& center, SkScalar rx
, SkScalar ry); | 194 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPoint& cente
r, SkScalar rx, |
| 195 SkScalar ry); |
| 194 | 196 |
| 195 virtual ~EllipseEffect() {}; | 197 virtual ~EllipseEffect() {}; |
| 196 static const char* Name() { return "Ellipse"; } | 198 static const char* Name() { return "Ellipse"; } |
| 197 | 199 |
| 198 const SkPoint& getCenter() const { return fCenter; } | 200 const SkPoint& getCenter() const { return fCenter; } |
| 199 SkVector getRadii() const { return fRadii; } | 201 SkVector getRadii() const { return fRadii; } |
| 200 | 202 |
| 201 GrEffectEdgeType getEdgeType() const { return fEdgeType; } | 203 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
| 202 | 204 |
| 203 typedef GLEllipseEffect GLEffect; | 205 typedef GLEllipseEffect GLProcessor; |
| 204 | 206 |
| 205 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; | 207 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 206 | 208 |
| 207 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 209 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE; |
| 208 | 210 |
| 209 private: | 211 private: |
| 210 EllipseEffect(GrEffectEdgeType, const SkPoint& center, SkScalar rx, SkScalar
ry); | 212 EllipseEffect(GrPrimitiveEdgeType, const SkPoint& center, SkScalar rx, SkSca
lar ry); |
| 211 | 213 |
| 212 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 214 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE; |
| 213 | 215 |
| 214 SkPoint fCenter; | 216 SkPoint fCenter; |
| 215 SkVector fRadii; | 217 SkVector fRadii; |
| 216 GrEffectEdgeType fEdgeType; | 218 GrPrimitiveEdgeType fEdgeType; |
| 217 | 219 |
| 218 GR_DECLARE_EFFECT_TEST; | 220 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 219 | 221 |
| 220 typedef GrEffect INHERITED; | 222 typedef GrFragmentProcessor INHERITED; |
| 221 }; | 223 }; |
| 222 | 224 |
| 223 GrEffect* EllipseEffect::Create(GrEffectEdgeType edgeType, | 225 GrFragmentProcessor* EllipseEffect::Create(GrPrimitiveEdgeType edgeType, |
| 224 const SkPoint& center, | 226 const SkPoint& center, |
| 225 SkScalar rx, | 227 SkScalar rx, |
| 226 SkScalar ry) { | 228 SkScalar ry) { |
| 227 SkASSERT(rx >= 0 && ry >= 0); | 229 SkASSERT(rx >= 0 && ry >= 0); |
| 228 return SkNEW_ARGS(EllipseEffect, (edgeType, center, rx, ry)); | 230 return SkNEW_ARGS(EllipseEffect, (edgeType, center, rx, ry)); |
| 229 } | 231 } |
| 230 | 232 |
| 231 void EllipseEffect::getConstantColorComponents(GrColor* color, uint32_t* validFl
ags) const { | 233 void EllipseEffect::getConstantColorComponents(GrColor* color, uint32_t* validFl
ags) const { |
| 232 *validFlags = 0; | 234 *validFlags = 0; |
| 233 } | 235 } |
| 234 | 236 |
| 235 const GrBackendEffectFactory& EllipseEffect::getFactory() const { | 237 const GrBackendFragmentProcessorFactory& EllipseEffect::getFactory() const { |
| 236 return GrTBackendEffectFactory<EllipseEffect>::getInstance(); | 238 return GrTBackendFragmentProcessorFactory<EllipseEffect>::getInstance(); |
| 237 } | 239 } |
| 238 | 240 |
| 239 EllipseEffect::EllipseEffect(GrEffectEdgeType edgeType, const SkPoint& c, SkScal
ar rx, SkScalar ry) | 241 EllipseEffect::EllipseEffect(GrPrimitiveEdgeType edgeType, const SkPoint& c, SkS
calar rx, SkScalar ry) |
| 240 : fCenter(c) | 242 : fCenter(c) |
| 241 , fRadii(SkVector::Make(rx, ry)) | 243 , fRadii(SkVector::Make(rx, ry)) |
| 242 , fEdgeType(edgeType) { | 244 , fEdgeType(edgeType) { |
| 243 this->setWillReadFragmentPosition(); | 245 this->setWillReadFragmentPosition(); |
| 244 } | 246 } |
| 245 | 247 |
| 246 bool EllipseEffect::onIsEqual(const GrEffect& other) const { | 248 bool EllipseEffect::onIsEqual(const GrProcessor& other) const { |
| 247 const EllipseEffect& ee = other.cast<EllipseEffect>(); | 249 const EllipseEffect& ee = other.cast<EllipseEffect>(); |
| 248 return fEdgeType == ee.fEdgeType && fCenter == ee.fCenter && fRadii == ee.fR
adii; | 250 return fEdgeType == ee.fEdgeType && fCenter == ee.fCenter && fRadii == ee.fR
adii; |
| 249 } | 251 } |
| 250 | 252 |
| 251 ////////////////////////////////////////////////////////////////////////////// | 253 ////////////////////////////////////////////////////////////////////////////// |
| 252 | 254 |
| 253 GR_DEFINE_EFFECT_TEST(EllipseEffect); | 255 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipseEffect); |
| 254 | 256 |
| 255 GrEffect* EllipseEffect::TestCreate(SkRandom* random, | 257 GrFragmentProcessor* EllipseEffect::TestCreate(SkRandom* random, |
| 256 GrContext*, | 258 GrContext*, |
| 257 const GrDrawTargetCaps& caps, | 259 const GrDrawTargetCaps& caps, |
| 258 GrTexture*[]) { | 260 GrTexture*[]) { |
| 259 SkPoint center; | 261 SkPoint center; |
| 260 center.fX = random->nextRangeScalar(0.f, 1000.f); | 262 center.fX = random->nextRangeScalar(0.f, 1000.f); |
| 261 center.fY = random->nextRangeScalar(0.f, 1000.f); | 263 center.fY = random->nextRangeScalar(0.f, 1000.f); |
| 262 SkScalar rx = random->nextRangeF(0.f, 1000.f); | 264 SkScalar rx = random->nextRangeF(0.f, 1000.f); |
| 263 SkScalar ry = random->nextRangeF(0.f, 1000.f); | 265 SkScalar ry = random->nextRangeF(0.f, 1000.f); |
| 264 GrEffectEdgeType et; | 266 GrPrimitiveEdgeType et; |
| 265 do { | 267 do { |
| 266 et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt); | 268 et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt)
; |
| 267 } while (kHairlineAA_GrEffectEdgeType == et); | 269 } while (kHairlineAA_GrProcessorEdgeType == et); |
| 268 return EllipseEffect::Create(et, center, rx, ry); | 270 return EllipseEffect::Create(et, center, rx, ry); |
| 269 } | 271 } |
| 270 | 272 |
| 271 ////////////////////////////////////////////////////////////////////////////// | 273 ////////////////////////////////////////////////////////////////////////////// |
| 272 | 274 |
| 273 class GLEllipseEffect : public GrGLEffect { | 275 class GLEllipseEffect : public GrGLFragmentProcessor { |
| 274 public: | 276 public: |
| 275 GLEllipseEffect(const GrBackendEffectFactory&, const GrEffect&); | 277 GLEllipseEffect(const GrBackendProcessorFactory&, const GrProcessor&); |
| 276 | 278 |
| 277 virtual void emitCode(GrGLProgramBuilder* builder, | 279 virtual void emitCode(GrGLProgramBuilder* builder, |
| 278 const GrEffect& effect, | 280 const GrFragmentProcessor& fp, |
| 279 const GrEffectKey& key, | 281 const GrProcessorKey& key, |
| 280 const char* outputColor, | 282 const char* outputColor, |
| 281 const char* inputColor, | 283 const char* inputColor, |
| 282 const TransformedCoordsArray&, | 284 const TransformedCoordsArray&, |
| 283 const TextureSamplerArray&) SK_OVERRIDE; | 285 const TextureSamplerArray&) SK_OVERRIDE; |
| 284 | 286 |
| 285 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuild
er*); | 287 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKe
yBuilder*); |
| 286 | 288 |
| 287 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER
RIDE; | 289 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE; |
| 288 | 290 |
| 289 private: | 291 private: |
| 290 GrGLProgramDataManager::UniformHandle fEllipseUniform; | 292 GrGLProgramDataManager::UniformHandle fEllipseUniform; |
| 291 SkPoint fPrevCenter; | 293 SkPoint fPrevCenter; |
| 292 SkVector fPrevRadii; | 294 SkVector fPrevRadii; |
| 293 | 295 |
| 294 typedef GrGLEffect INHERITED; | 296 typedef GrGLFragmentProcessor INHERITED; |
| 295 }; | 297 }; |
| 296 | 298 |
| 297 GLEllipseEffect::GLEllipseEffect(const GrBackendEffectFactory& factory, | 299 GLEllipseEffect::GLEllipseEffect(const GrBackendProcessorFactory& factory, |
| 298 const GrEffect& effect) | 300 const GrProcessor& effect) |
| 299 : INHERITED (factory) { | 301 : INHERITED (factory) { |
| 300 fPrevRadii.fX = -1.f; | 302 fPrevRadii.fX = -1.f; |
| 301 } | 303 } |
| 302 | 304 |
| 303 void GLEllipseEffect::emitCode(GrGLProgramBuilder* builder, | 305 void GLEllipseEffect::emitCode(GrGLProgramBuilder* builder, |
| 304 const GrEffect& effect, | 306 const GrFragmentProcessor& fp, |
| 305 const GrEffectKey& key, | 307 const GrProcessorKey& key, |
| 306 const char* outputColor, | 308 const char* outputColor, |
| 307 const char* inputColor, | 309 const char* inputColor, |
| 308 const TransformedCoordsArray&, | 310 const TransformedCoordsArray&, |
| 309 const TextureSamplerArray& samplers) { | 311 const TextureSamplerArray& samplers) { |
| 310 const EllipseEffect& ee = effect.cast<EllipseEffect>(); | 312 const EllipseEffect& ee = fp.cast<EllipseEffect>(); |
| 311 const char *ellipseName; | 313 const char *ellipseName; |
| 312 // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2) | 314 // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2) |
| 313 fEllipseUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, | 315 fEllipseUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, |
| 314 kVec4f_GrSLType, | 316 kVec4f_GrSLType, |
| 315 "ellipse", | 317 "ellipse", |
| 316 &ellipseName); | 318 &ellipseName); |
| 317 | 319 |
| 318 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 320 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 319 const char* fragmentPos = fsBuilder->fragmentPosition(); | 321 const char* fragmentPos = fsBuilder->fragmentPosition(); |
| 320 | 322 |
| 321 // d is the offset to the ellipse center | 323 // d is the offset to the ellipse center |
| 322 fsBuilder->codeAppendf("\t\tvec2 d = %s.xy - %s.xy;\n", fragmentPos, ellipse
Name); | 324 fsBuilder->codeAppendf("\t\tvec2 d = %s.xy - %s.xy;\n", fragmentPos, ellipse
Name); |
| 323 fsBuilder->codeAppendf("\t\tvec2 Z = d * %s.zw;\n", ellipseName); | 325 fsBuilder->codeAppendf("\t\tvec2 Z = d * %s.zw;\n", ellipseName); |
| 324 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1. | 326 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1. |
| 325 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, d) - 1.0;\n"); | 327 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, d) - 1.0;\n"); |
| 326 // grad_dot is the squared length of the gradient of the implicit. | 328 // grad_dot is the squared length of the gradient of the implicit. |
| 327 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); | 329 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); |
| 328 // avoid calling inversesqrt on zero. | 330 // avoid calling inversesqrt on zero. |
| 329 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); | 331 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
| 330 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_
dot);\n"); | 332 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_
dot);\n"); |
| 331 | 333 |
| 332 switch (ee.getEdgeType()) { | 334 switch (ee.getEdgeType()) { |
| 333 case kFillAA_GrEffectEdgeType: | 335 case kFillAA_GrProcessorEdgeType: |
| 334 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.
0, 1.0);\n"); | 336 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.
0, 1.0);\n"); |
| 335 break; | 337 break; |
| 336 case kInverseFillAA_GrEffectEdgeType: | 338 case kInverseFillAA_GrProcessorEdgeType: |
| 337 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.
0, 1.0);\n"); | 339 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.
0, 1.0);\n"); |
| 338 break; | 340 break; |
| 339 case kFillBW_GrEffectEdgeType: | 341 case kFillBW_GrProcessorEdgeType: |
| 340 fsBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 0.0 : 1
.0;\n"); | 342 fsBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 0.0 : 1
.0;\n"); |
| 341 break; | 343 break; |
| 342 case kInverseFillBW_GrEffectEdgeType: | 344 case kInverseFillBW_GrProcessorEdgeType: |
| 343 fsBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 1.0 : 0
.0;\n"); | 345 fsBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 1.0 : 0
.0;\n"); |
| 344 break; | 346 break; |
| 345 case kHairlineAA_GrEffectEdgeType: | 347 case kHairlineAA_GrProcessorEdgeType: |
| 346 SkFAIL("Hairline not expected here."); | 348 SkFAIL("Hairline not expected here."); |
| 347 } | 349 } |
| 348 | 350 |
| 349 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, | 351 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, |
| 350 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st
r()); | 352 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st
r()); |
| 351 } | 353 } |
| 352 | 354 |
| 353 void GLEllipseEffect::GenKey(const GrEffect& effect, const GrGLCaps&, | 355 void GLEllipseEffect::GenKey(const GrProcessor& effect, const GrGLCaps&, |
| 354 GrEffectKeyBuilder* b) { | 356 GrProcessorKeyBuilder* b) { |
| 355 const EllipseEffect& ee = effect.cast<EllipseEffect>(); | 357 const EllipseEffect& ee = effect.cast<EllipseEffect>(); |
| 356 b->add32(ee.getEdgeType()); | 358 b->add32(ee.getEdgeType()); |
| 357 } | 359 } |
| 358 | 360 |
| 359 void GLEllipseEffect::setData(const GrGLProgramDataManager& pdman, const GrEffec
t& effect) { | 361 void GLEllipseEffect::setData(const GrGLProgramDataManager& pdman, const GrProce
ssor& effect) { |
| 360 const EllipseEffect& ee = effect.cast<EllipseEffect>(); | 362 const EllipseEffect& ee = effect.cast<EllipseEffect>(); |
| 361 if (ee.getRadii() != fPrevRadii || ee.getCenter() != fPrevCenter) { | 363 if (ee.getRadii() != fPrevRadii || ee.getCenter() != fPrevCenter) { |
| 362 SkScalar invRXSqd = 1.f / (ee.getRadii().fX * ee.getRadii().fX); | 364 SkScalar invRXSqd = 1.f / (ee.getRadii().fX * ee.getRadii().fX); |
| 363 SkScalar invRYSqd = 1.f / (ee.getRadii().fY * ee.getRadii().fY); | 365 SkScalar invRYSqd = 1.f / (ee.getRadii().fY * ee.getRadii().fY); |
| 364 pdman.set4f(fEllipseUniform, ee.getCenter().fX, ee.getCenter().fY, invRX
Sqd, invRYSqd); | 366 pdman.set4f(fEllipseUniform, ee.getCenter().fX, ee.getCenter().fY, invRX
Sqd, invRYSqd); |
| 365 fPrevCenter = ee.getCenter(); | 367 fPrevCenter = ee.getCenter(); |
| 366 fPrevRadii = ee.getRadii(); | 368 fPrevRadii = ee.getRadii(); |
| 367 } | 369 } |
| 368 } | 370 } |
| 369 | 371 |
| 370 ////////////////////////////////////////////////////////////////////////////// | 372 ////////////////////////////////////////////////////////////////////////////// |
| 371 | 373 |
| 372 GrEffect* GrOvalEffect::Create(GrEffectEdgeType edgeType, const SkRect& oval) { | 374 GrFragmentProcessor* GrOvalEffect::Create(GrPrimitiveEdgeType edgeType, const Sk
Rect& oval) { |
| 373 if (kHairlineAA_GrEffectEdgeType == edgeType) { | 375 if (kHairlineAA_GrProcessorEdgeType == edgeType) { |
| 374 return NULL; | 376 return NULL; |
| 375 } | 377 } |
| 376 SkScalar w = oval.width(); | 378 SkScalar w = oval.width(); |
| 377 SkScalar h = oval.height(); | 379 SkScalar h = oval.height(); |
| 378 if (SkScalarNearlyEqual(w, h)) { | 380 if (SkScalarNearlyEqual(w, h)) { |
| 379 w /= 2; | 381 w /= 2; |
| 380 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval
.fTop + w), w); | 382 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval
.fTop + w), w); |
| 381 } else { | 383 } else { |
| 382 w /= 2; | 384 w /= 2; |
| 383 h /= 2; | 385 h /= 2; |
| 384 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, ova
l.fTop + h), w, h); | 386 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, ova
l.fTop + h), w, h); |
| 385 } | 387 } |
| 386 | 388 |
| 387 return NULL; | 389 return NULL; |
| 388 } | 390 } |
| OLD | NEW |