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 "GrOvalEffect.h" | 8 #include "GrOvalEffect.h" |
9 | 9 |
10 #include "GrFragmentProcessor.h" | 10 #include "GrFragmentProcessor.h" |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 126 |
127 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 127 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
128 const char* fragmentPos = fragBuilder->fragmentPosition(); | 128 const char* fragmentPos = fragBuilder->fragmentPosition(); |
129 | 129 |
130 SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType()); | 130 SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType()); |
131 // TODO: Right now the distance to circle caclulation is performed in a spac
e normalized to the | 131 // TODO: Right now the distance to circle caclulation is performed in a spac
e normalized to the |
132 // radius and then denormalized. This is to prevent overflow on devices that
have a "real" | 132 // radius and then denormalized. This is to prevent overflow on devices that
have a "real" |
133 // mediump. It'd be nice to only to this on mediump devices but we currently
don't have the | 133 // mediump. It'd be nice to only to this on mediump devices but we currently
don't have the |
134 // caps here. | 134 // caps here. |
135 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { | 135 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { |
136 fragBuilder->codeAppendf("\t\tfloat d = (length((%s.xy - %s.xy) * %s.w)
- 1.0) * %s.z;\n", | 136 fragBuilder->codeAppendf("float d = (length((%s.xy - %s.xy) * %s.w) - 1.
0) * %s.z;", |
137 circleName, fragmentPos, circleName, circleName
); | 137 circleName, fragmentPos, circleName, circleName
); |
138 } else { | 138 } else { |
139 fragBuilder->codeAppendf("\t\tfloat d = (1.0 - length((%s.xy - %s.xy) *
%s.w)) * %s.z;\n", | 139 fragBuilder->codeAppendf("float d = (1.0 - length((%s.xy - %s.xy) * %s.
w)) * %s.z;", |
140 circleName, fragmentPos, circleName, circleName
); | 140 circleName, fragmentPos, circleName, circleName
); |
141 } | 141 } |
142 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) { | 142 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) { |
143 fragBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n"); | 143 fragBuilder->codeAppend("d = clamp(d, 0.0, 1.0);"); |
144 } else { | 144 } else { |
145 fragBuilder->codeAppend("\t\td = d > 0.5 ? 1.0 : 0.0;\n"); | 145 fragBuilder->codeAppend("d = d > 0.5 ? 1.0 : 0.0;"); |
146 } | 146 } |
147 | 147 |
148 fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, | 148 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, |
149 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("d")).
c_str()); | 149 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("d")).
c_str()); |
150 } | 150 } |
151 | 151 |
152 void GLCircleEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&, | 152 void GLCircleEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
153 GrProcessorKeyBuilder* b) { | 153 GrProcessorKeyBuilder* b) { |
154 const CircleEffect& ce = processor.cast<CircleEffect>(); | 154 const CircleEffect& ce = processor.cast<CircleEffect>(); |
155 b->add32(ce.getEdgeType()); | 155 b->add32(ce.getEdgeType()); |
156 } | 156 } |
157 | 157 |
158 void GLCircleEffect::onSetData(const GrGLSLProgramDataManager& pdman, | 158 void GLCircleEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 269 |
270 virtual void emitCode(EmitArgs&) override; | 270 virtual void emitCode(EmitArgs&) override; |
271 | 271 |
272 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); | 272 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); |
273 | 273 |
274 protected: | 274 protected: |
275 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override
; | 275 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override
; |
276 | 276 |
277 private: | 277 private: |
278 GrGLSLProgramDataManager::UniformHandle fEllipseUniform; | 278 GrGLSLProgramDataManager::UniformHandle fEllipseUniform; |
| 279 GrGLSLProgramDataManager::UniformHandle fScaleUniform; |
279 SkPoint fPrevCenter; | 280 SkPoint fPrevCenter; |
280 SkVector fPrevRadii; | 281 SkVector fPrevRadii; |
281 | 282 |
282 typedef GrGLSLFragmentProcessor INHERITED; | 283 typedef GrGLSLFragmentProcessor INHERITED; |
283 }; | 284 }; |
284 | 285 |
285 GLEllipseEffect::GLEllipseEffect(const GrProcessor& effect) { | 286 GLEllipseEffect::GLEllipseEffect(const GrProcessor& effect) { |
286 fPrevRadii.fX = -1.f; | 287 fPrevRadii.fX = -1.f; |
287 } | 288 } |
288 | 289 |
289 void GLEllipseEffect::emitCode(EmitArgs& args) { | 290 void GLEllipseEffect::emitCode(EmitArgs& args) { |
290 const EllipseEffect& ee = args.fFp.cast<EllipseEffect>(); | 291 const EllipseEffect& ee = args.fFp.cast<EllipseEffect>(); |
291 const char *ellipseName; | 292 const char *ellipseName; |
292 // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2) | 293 // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2) |
293 // The last two terms can underflow on mediump, so we use highp. | 294 // The last two terms can underflow on mediump, so we use highp. |
294 fEllipseUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFr
agment_Visibility, | 295 fEllipseUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFr
agment_Visibility, |
295 kVec4f_GrSLType, kHigh_Gr
SLPrecision, | 296 kVec4f_GrSLType, kHigh_Gr
SLPrecision, |
296 "ellipse", | 297 "ellipse", |
297 &ellipseName); | 298 &ellipseName); |
| 299 // If we're on a device with a "real" mediump then we'll do the distance com
putation in a space |
| 300 // that is normalized by the larger radius. The scale uniform will be scale,
1/scale. The |
| 301 // inverse squared radii uniform values are already in this normalized space
. The center is |
| 302 // not. |
| 303 const char* scaleName = nullptr; |
| 304 if (args.fGLSLCaps->floatPrecisionVaries()) { |
| 305 fScaleUniform = args.fUniformHandler->addUniform( |
| 306 GrGLSLUniformHandler::kFragment_Visibility, kVec2f_GrSLType, kDefaul
t_GrSLPrecision, |
| 307 "scale", &scaleName); |
| 308 } |
298 | 309 |
299 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 310 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
300 const char* fragmentPos = fragBuilder->fragmentPosition(); | 311 const char* fragmentPos = fragBuilder->fragmentPosition(); |
301 | 312 |
302 // d is the offset to the ellipse center | 313 // d is the offset to the ellipse center |
303 fragBuilder->codeAppendf("\t\tvec2 d = %s.xy - %s.xy;\n", fragmentPos, ellip
seName); | 314 fragBuilder->codeAppendf("vec2 d = %s.xy - %s.xy;", fragmentPos, ellipseName
); |
304 fragBuilder->codeAppendf("\t\tvec2 Z = d * %s.zw;\n", ellipseName); | 315 if (scaleName) { |
| 316 fragBuilder->codeAppendf("d *= %s.y;", scaleName); |
| 317 } |
| 318 fragBuilder->codeAppendf("vec2 Z = d * %s.zw;", ellipseName); |
305 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1. | 319 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1. |
306 fragBuilder->codeAppend("\t\tfloat implicit = dot(Z, d) - 1.0;\n"); | 320 fragBuilder->codeAppend("float implicit = dot(Z, d) - 1.0;"); |
307 // grad_dot is the squared length of the gradient of the implicit. | 321 // grad_dot is the squared length of the gradient of the implicit. |
308 fragBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); | 322 fragBuilder->codeAppendf("float grad_dot = 4.0 * dot(Z, Z);"); |
309 // avoid calling inversesqrt on zero. | 323 // Avoid calling inversesqrt on zero. |
310 fragBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); | 324 fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);"); |
311 fragBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(gra
d_dot);\n"); | 325 fragBuilder->codeAppendf("float approx_dist = implicit * inversesqrt(grad_do
t);"); |
| 326 if (scaleName) { |
| 327 fragBuilder->codeAppendf("approx_dist *= %s.x;", scaleName); |
| 328 } |
312 | 329 |
313 switch (ee.getEdgeType()) { | 330 switch (ee.getEdgeType()) { |
314 case kFillAA_GrProcessorEdgeType: | 331 case kFillAA_GrProcessorEdgeType: |
315 fragBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist,
0.0, 1.0);\n"); | 332 fragBuilder->codeAppend("float alpha = clamp(0.5 - approx_dist, 0.0,
1.0);"); |
316 break; | 333 break; |
317 case kInverseFillAA_GrProcessorEdgeType: | 334 case kInverseFillAA_GrProcessorEdgeType: |
318 fragBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist,
0.0, 1.0);\n"); | 335 fragBuilder->codeAppend("float alpha = clamp(0.5 + approx_dist, 0.0,
1.0);"); |
319 break; | 336 break; |
320 case kFillBW_GrProcessorEdgeType: | 337 case kFillBW_GrProcessorEdgeType: |
321 fragBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 0.0 :
1.0;\n"); | 338 fragBuilder->codeAppend("float alpha = approx_dist > 0.0 ? 0.0 : 1.0
;"); |
322 break; | 339 break; |
323 case kInverseFillBW_GrProcessorEdgeType: | 340 case kInverseFillBW_GrProcessorEdgeType: |
324 fragBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 1.0 :
0.0;\n"); | 341 fragBuilder->codeAppend("float alpha = approx_dist > 0.0 ? 1.0 : 0.0
;"); |
325 break; | 342 break; |
326 case kHairlineAA_GrProcessorEdgeType: | 343 case kHairlineAA_GrProcessorEdgeType: |
327 SkFAIL("Hairline not expected here."); | 344 SkFAIL("Hairline not expected here."); |
328 } | 345 } |
329 | 346 |
330 fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, | 347 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, |
331 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha
")).c_str()); | 348 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha
")).c_str()); |
332 } | 349 } |
333 | 350 |
334 void GLEllipseEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&, | 351 void GLEllipseEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&, |
335 GrProcessorKeyBuilder* b) { | 352 GrProcessorKeyBuilder* b) { |
336 const EllipseEffect& ee = effect.cast<EllipseEffect>(); | 353 const EllipseEffect& ee = effect.cast<EllipseEffect>(); |
337 b->add32(ee.getEdgeType()); | 354 b->add32(ee.getEdgeType()); |
338 } | 355 } |
339 | 356 |
340 void GLEllipseEffect::onSetData(const GrGLSLProgramDataManager& pdman, | 357 void GLEllipseEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
341 const GrProcessor& effect) { | 358 const GrProcessor& effect) { |
342 const EllipseEffect& ee = effect.cast<EllipseEffect>(); | 359 const EllipseEffect& ee = effect.cast<EllipseEffect>(); |
343 if (ee.getRadii() != fPrevRadii || ee.getCenter() != fPrevCenter) { | 360 if (ee.getRadii() != fPrevRadii || ee.getCenter() != fPrevCenter) { |
344 SkScalar invRXSqd = 1.f / (ee.getRadii().fX * ee.getRadii().fX); | 361 float invRXSqd; |
345 SkScalar invRYSqd = 1.f / (ee.getRadii().fY * ee.getRadii().fY); | 362 float invRYSqd; |
| 363 // If we're using a scale factor to work around precision issues, choose
the larger radius |
| 364 // as the scale factor. The inv radii need to be pre-adjusted by the sca
le factor. |
| 365 if (fScaleUniform.isValid()) { |
| 366 if (ee.getRadii().fX > ee.getRadii().fY) { |
| 367 invRXSqd = 1.f; |
| 368 invRYSqd = (ee.getRadii().fX * ee.getRadii().fX) / |
| 369 (ee.getRadii().fY * ee.getRadii().fY); |
| 370 pdman.set2f(fScaleUniform, ee.getRadii().fX, 1.f / ee.getRadii()
.fX); |
| 371 } else { |
| 372 invRXSqd = (ee.getRadii().fY * ee.getRadii().fY) / |
| 373 (ee.getRadii().fX * ee.getRadii().fX); |
| 374 invRYSqd = 1.f; |
| 375 pdman.set2f(fScaleUniform, ee.getRadii().fY, 1.f / ee.getRadii()
.fY); |
| 376 } |
| 377 } else { |
| 378 invRXSqd = 1.f / (ee.getRadii().fX * ee.getRadii().fX); |
| 379 invRYSqd = 1.f / (ee.getRadii().fY * ee.getRadii().fY); |
| 380 } |
346 pdman.set4f(fEllipseUniform, ee.getCenter().fX, ee.getCenter().fY, invRX
Sqd, invRYSqd); | 381 pdman.set4f(fEllipseUniform, ee.getCenter().fX, ee.getCenter().fY, invRX
Sqd, invRYSqd); |
347 fPrevCenter = ee.getCenter(); | 382 fPrevCenter = ee.getCenter(); |
348 fPrevRadii = ee.getRadii(); | 383 fPrevRadii = ee.getRadii(); |
349 } | 384 } |
350 } | 385 } |
351 | 386 |
352 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 387 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
353 | 388 |
354 void EllipseEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, | 389 void EllipseEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
355 GrProcessorKeyBuilder* b) const { | 390 GrProcessorKeyBuilder* b) const { |
(...skipping 16 matching lines...) Expand all Loading... |
372 w /= 2; | 407 w /= 2; |
373 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval
.fTop + w), w); | 408 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval
.fTop + w), w); |
374 } else { | 409 } else { |
375 w /= 2; | 410 w /= 2; |
376 h /= 2; | 411 h /= 2; |
377 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, ova
l.fTop + h), w, h); | 412 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, ova
l.fTop + h), w, h); |
378 } | 413 } |
379 | 414 |
380 return nullptr; | 415 return nullptr; |
381 } | 416 } |
OLD | NEW |