OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "GrOvalRenderer.h" | 8 #include "GrOvalRenderer.h" |
9 | 9 |
10 #include "GrEffect.h" | 10 #include "GrEffect.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 81 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
82 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance(); | 82 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance(); |
83 } | 83 } |
84 | 84 |
85 virtual ~CircleEdgeEffect() {} | 85 virtual ~CircleEdgeEffect() {} |
86 | 86 |
87 static const char* Name() { return "CircleEdge"; } | 87 static const char* Name() { return "CircleEdge"; } |
88 | 88 |
89 inline bool isStroked() const { return fStroke; } | 89 inline bool isStroked() const { return fStroke; } |
90 | 90 |
91 class GLEffect : public GrGLEffect { | 91 class GLEffect : public GrGLVertexEffect { |
92 public: | 92 public: |
93 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) | 93 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
94 : INHERITED (factory) {} | 94 : INHERITED (factory) {} |
95 | 95 |
96 virtual void emitCode(GrGLShaderBuilder* builder, | 96 virtual void emitCode(GrGLFullShaderBuilder* builder, |
97 const GrDrawEffect& drawEffect, | 97 const GrDrawEffect& drawEffect, |
98 EffectKey key, | 98 EffectKey key, |
99 const char* outputColor, | 99 const char* outputColor, |
100 const char* inputColor, | 100 const char* inputColor, |
101 const TransformedCoordsArray&, | 101 const TransformedCoordsArray&, |
102 const TextureSamplerArray& samplers) SK_OVERRIDE { | 102 const TextureSamplerArray& samplers) SK_OVERRIDE { |
103 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertex
Builder(); | |
104 SkASSERT(NULL != vertexBuilder); | |
105 | |
106 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleE
dgeEffect>(); | 103 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleE
dgeEffect>(); |
107 const char *vsName, *fsName; | 104 const char *vsName, *fsName; |
108 vertexBuilder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &f
sName); | 105 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName)
; |
109 | 106 |
110 const SkString* attrName = | 107 const SkString* attrName = |
111 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttrib
Indices()[0]); | 108 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[0]); |
112 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str
()); | 109 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
113 | 110 |
114 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName); | 111 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName); |
115 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0
);\n", fsName); | 112 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0
);\n", fsName); |
116 if (circleEffect.isStroked()) { | 113 if (circleEffect.isStroked()) { |
117 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0
, 1.0);\n", fsName); | 114 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0
, 1.0);\n", fsName); |
118 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); | 115 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); |
119 } | 116 } |
120 | 117 |
121 SkString modulate; | 118 SkString modulate; |
122 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); | 119 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
123 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()
); | 120 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()
); |
124 } | 121 } |
125 | 122 |
126 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrG
LCaps&) { | 123 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrG
LCaps&) { |
127 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleE
dgeEffect>(); | 124 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleE
dgeEffect>(); |
128 | 125 |
129 return circleEffect.isStroked() ? 0x1 : 0x0; | 126 return circleEffect.isStroked() ? 0x1 : 0x0; |
130 } | 127 } |
131 | 128 |
132 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_
OVERRIDE {} | 129 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_
OVERRIDE {} |
133 | 130 |
134 private: | 131 private: |
135 typedef GrGLEffect INHERITED; | 132 typedef GrGLVertexEffect INHERITED; |
136 }; | 133 }; |
137 | 134 |
138 | 135 |
139 private: | 136 private: |
140 CircleEdgeEffect(bool stroke) : GrVertexEffect() { | 137 CircleEdgeEffect(bool stroke) : GrVertexEffect() { |
141 this->addVertexAttrib(kVec4f_GrSLType); | 138 this->addVertexAttrib(kVec4f_GrSLType); |
142 fStroke = stroke; | 139 fStroke = stroke; |
143 } | 140 } |
144 | 141 |
145 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { | 142 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 193 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
197 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance(); | 194 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance(); |
198 } | 195 } |
199 | 196 |
200 virtual ~EllipseEdgeEffect() {} | 197 virtual ~EllipseEdgeEffect() {} |
201 | 198 |
202 static const char* Name() { return "EllipseEdge"; } | 199 static const char* Name() { return "EllipseEdge"; } |
203 | 200 |
204 inline bool isStroked() const { return fStroke; } | 201 inline bool isStroked() const { return fStroke; } |
205 | 202 |
206 class GLEffect : public GrGLEffect { | 203 class GLEffect : public GrGLVertexEffect { |
207 public: | 204 public: |
208 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) | 205 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
209 : INHERITED (factory) {} | 206 : INHERITED (factory) {} |
210 | 207 |
211 virtual void emitCode(GrGLShaderBuilder* builder, | 208 virtual void emitCode(GrGLFullShaderBuilder* builder, |
212 const GrDrawEffect& drawEffect, | 209 const GrDrawEffect& drawEffect, |
213 EffectKey key, | 210 EffectKey key, |
214 const char* outputColor, | 211 const char* outputColor, |
215 const char* inputColor, | 212 const char* inputColor, |
216 const TransformedCoordsArray&, | 213 const TransformedCoordsArray&, |
217 const TextureSamplerArray& samplers) SK_OVERRIDE { | 214 const TextureSamplerArray& samplers) SK_OVERRIDE { |
218 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertex
Builder(); | |
219 SkASSERT(NULL != vertexBuilder); | |
220 | |
221 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<Ellip
seEdgeEffect>(); | 215 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<Ellip
seEdgeEffect>(); |
222 | 216 |
223 const char *vsOffsetName, *fsOffsetName; | 217 const char *vsOffsetName, *fsOffsetName; |
224 const char *vsRadiiName, *fsRadiiName; | 218 const char *vsRadiiName, *fsRadiiName; |
225 | 219 |
226 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffs
etName, &fsOffsetName); | 220 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName
, &fsOffsetName); |
227 const SkString* attr0Name = | 221 const SkString* attr0Name = |
228 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttrib
Indices()[0]); | 222 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[0]); |
229 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name
->c_str()); | 223 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_st
r()); |
230 | 224 |
231 vertexBuilder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiN
ame, &fsRadiiName); | 225 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &
fsRadiiName); |
232 const SkString* attr1Name = | 226 const SkString* attr1Name = |
233 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttrib
Indices()[1]); | 227 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[1]); |
234 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name-
>c_str()); | 228 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str
()); |
235 | 229 |
236 // for outer curve | 230 // for outer curve |
237 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffset
Name, fsRadiiName); | 231 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffset
Name, fsRadiiName); |
238 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset
) - 1.0;\n"); | 232 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset
) - 1.0;\n"); |
239 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fs
RadiiName); | 233 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fs
RadiiName); |
240 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); | 234 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); |
241 // we need to clamp the length^2 of the gradiant vector to a non-zer
o value, because | 235 // we need to clamp the length^2 of the gradiant vector to a non-zer
o value, because |
242 // on the Nexus 4 the undefined result of inversesqrt(0) drops out a
n entire tile | 236 // on the Nexus 4 the undefined result of inversesqrt(0) drops out a
n entire tile |
243 // TODO: restrict this to Adreno-only | 237 // TODO: restrict this to Adreno-only |
244 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); | 238 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
(...skipping 17 matching lines...) Expand all Loading... |
262 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrG
LCaps&) { | 256 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrG
LCaps&) { |
263 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<Ellip
seEdgeEffect>(); | 257 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<Ellip
seEdgeEffect>(); |
264 | 258 |
265 return ellipseEffect.isStroked() ? 0x1 : 0x0; | 259 return ellipseEffect.isStroked() ? 0x1 : 0x0; |
266 } | 260 } |
267 | 261 |
268 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_
OVERRIDE { | 262 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_
OVERRIDE { |
269 } | 263 } |
270 | 264 |
271 private: | 265 private: |
272 typedef GrGLEffect INHERITED; | 266 typedef GrGLVertexEffect INHERITED; |
273 }; | 267 }; |
274 | 268 |
275 private: | 269 private: |
276 EllipseEdgeEffect(bool stroke) : GrVertexEffect() { | 270 EllipseEdgeEffect(bool stroke) : GrVertexEffect() { |
277 this->addVertexAttrib(kVec2f_GrSLType); | 271 this->addVertexAttrib(kVec2f_GrSLType); |
278 this->addVertexAttrib(kVec4f_GrSLType); | 272 this->addVertexAttrib(kVec4f_GrSLType); |
279 fStroke = stroke; | 273 fStroke = stroke; |
280 } | 274 } |
281 | 275 |
282 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { | 276 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 334 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
341 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance(); | 335 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance(); |
342 } | 336 } |
343 | 337 |
344 virtual ~DIEllipseEdgeEffect() {} | 338 virtual ~DIEllipseEdgeEffect() {} |
345 | 339 |
346 static const char* Name() { return "DIEllipseEdge"; } | 340 static const char* Name() { return "DIEllipseEdge"; } |
347 | 341 |
348 inline Mode getMode() const { return fMode; } | 342 inline Mode getMode() const { return fMode; } |
349 | 343 |
350 class GLEffect : public GrGLEffect { | 344 class GLEffect : public GrGLVertexEffect { |
351 public: | 345 public: |
352 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) | 346 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
353 : INHERITED (factory) {} | 347 : INHERITED (factory) {} |
354 | 348 |
355 virtual void emitCode(GrGLShaderBuilder* builder, | 349 virtual void emitCode(GrGLFullShaderBuilder* builder, |
356 const GrDrawEffect& drawEffect, | 350 const GrDrawEffect& drawEffect, |
357 EffectKey key, | 351 EffectKey key, |
358 const char* outputColor, | 352 const char* outputColor, |
359 const char* inputColor, | 353 const char* inputColor, |
360 const TransformedCoordsArray&, | 354 const TransformedCoordsArray&, |
361 const TextureSamplerArray& samplers) SK_OVERRIDE { | 355 const TextureSamplerArray& samplers) SK_OVERRIDE { |
362 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertex
Builder(); | |
363 SkASSERT(NULL != vertexBuilder); | |
364 | |
365 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIE
llipseEdgeEffect>(); | 356 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIE
llipseEdgeEffect>(); |
366 | 357 |
367 SkAssertResult(builder->enableFeature( | 358 SkAssertResult(builder->enableFeature( |
368 GrGLShaderBuilder::kStandardDeriva
tives_GLSLFeature)); | 359 GrGLShaderBuilder::kStandardDeriva
tives_GLSLFeature)); |
369 | 360 |
370 const char *vsOffsetName0, *fsOffsetName0; | 361 const char *vsOffsetName0, *fsOffsetName0; |
371 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets0", | 362 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0", |
372 &vsOffsetName0, &fsOffsetName0); | 363 &vsOffsetName0, &fsOffsetName0); |
373 const SkString* attr0Name = | 364 const SkString* attr0Name = |
374 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttrib
Indices()[0]); | 365 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[0]); |
375 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Nam
e->c_str()); | 366 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_s
tr()); |
376 const char *vsOffsetName1, *fsOffsetName1; | 367 const char *vsOffsetName1, *fsOffsetName1; |
377 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets1", | 368 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1", |
378 &vsOffsetName1, &fsOffsetName1); | 369 &vsOffsetName1, &fsOffsetName1); |
379 const SkString* attr1Name = | 370 const SkString* attr1Name = |
380 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttrib
Indices()[1]); | 371 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[1]); |
381 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Nam
e->c_str()); | 372 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_s
tr()); |
382 | 373 |
383 // for outer curve | 374 // for outer curve |
384 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetNam
e0); | 375 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetNam
e0); |
385 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset
) - 1.0;\n"); | 376 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset
) - 1.0;\n"); |
386 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0); | 377 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0); |
387 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0); | 378 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0); |
388 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s
.y*duvdx.y,\n" | 379 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s
.y*duvdx.y,\n" |
389 "\t 2.0*%s.x*duvdy.x + 2.0*%s
.y*duvdy.y);\n", | 380 "\t 2.0*%s.x*duvdy.x + 2.0*%s
.y*duvdy.y);\n", |
390 fsOffsetName0, fsOffsetName0, fsOffsetName0,
fsOffsetName0); | 381 fsOffsetName0, fsOffsetName0, fsOffsetName0,
fsOffsetName0); |
391 | 382 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrG
LCaps&) { | 415 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrG
LCaps&) { |
425 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIE
llipseEdgeEffect>(); | 416 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIE
llipseEdgeEffect>(); |
426 | 417 |
427 return ellipseEffect.getMode(); | 418 return ellipseEffect.getMode(); |
428 } | 419 } |
429 | 420 |
430 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_
OVERRIDE { | 421 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_
OVERRIDE { |
431 } | 422 } |
432 | 423 |
433 private: | 424 private: |
434 typedef GrGLEffect INHERITED; | 425 typedef GrGLVertexEffect INHERITED; |
435 }; | 426 }; |
436 | 427 |
437 private: | 428 private: |
438 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() { | 429 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() { |
439 this->addVertexAttrib(kVec2f_GrSLType); | 430 this->addVertexAttrib(kVec2f_GrSLType); |
440 this->addVertexAttrib(kVec2f_GrSLType); | 431 this->addVertexAttrib(kVec2f_GrSLType); |
441 fMode = mode; | 432 fMode = mode; |
442 } | 433 } |
443 | 434 |
444 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { | 435 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1143 } | 1134 } |
1144 | 1135 |
1145 // drop out the middle quad if we're stroked | 1136 // drop out the middle quad if we're stroked |
1146 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_CO
UNT(gRRectIndices); | 1137 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_CO
UNT(gRRectIndices); |
1147 target->setIndexSourceToBuffer(indexBuffer); | 1138 target->setIndexSourceToBuffer(indexBuffer); |
1148 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bou
nds); | 1139 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bou
nds); |
1149 } | 1140 } |
1150 | 1141 |
1151 return true; | 1142 return true; |
1152 } | 1143 } |
OLD | NEW |