| 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 void GLCircleEffect::emitCode(EmitArgs& args) { | 117 void GLCircleEffect::emitCode(EmitArgs& args) { |
| 118 const CircleEffect& ce = args.fFp.cast<CircleEffect>(); | 118 const CircleEffect& ce = args.fFp.cast<CircleEffect>(); |
| 119 const char *circleName; | 119 const char *circleName; |
| 120 // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.
5)) for regular | 120 // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.
5)) for regular |
| 121 // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills. | 121 // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills. |
| 122 fCircleUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_V
isibility, | 122 fCircleUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_V
isibility, |
| 123 kVec4f_GrSLType, kDefault_GrSLPrecision
, | 123 kVec4f_GrSLType, kDefault_GrSLPrecision
, |
| 124 "circle", | 124 "circle", |
| 125 &circleName); | 125 &circleName); |
| 126 | 126 |
| 127 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder()
; | 127 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 128 const char* fragmentPos = fsBuilder->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 fsBuilder->codeAppendf("\t\tfloat d = (length((%s.xy - %s.xy) * %s.w) -
1.0) * %s.z;\n", | 136 fragBuilder->codeAppendf("\t\tfloat d = (length((%s.xy - %s.xy) * %s.w)
- 1.0) * %s.z;\n", |
| 137 circleName, fragmentPos, circleName, circleName)
; | 137 circleName, fragmentPos, circleName, circleName
); |
| 138 } else { | 138 } else { |
| 139 fsBuilder->codeAppendf("\t\tfloat d = (1.0 - length((%s.xy - %s.xy) * %
s.w)) * %s.z;\n", | 139 fragBuilder->codeAppendf("\t\tfloat d = (1.0 - length((%s.xy - %s.xy) *
%s.w)) * %s.z;\n", |
| 140 circleName, fragmentPos, circleName, circleName); | 140 circleName, fragmentPos, circleName, circleName
); |
| 141 } | 141 } |
| 142 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) { | 142 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) { |
| 143 fsBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n"); | 143 fragBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n"); |
| 144 } else { | 144 } else { |
| 145 fsBuilder->codeAppend("\t\td = d > 0.5 ? 1.0 : 0.0;\n"); | 145 fragBuilder->codeAppend("\t\td = d > 0.5 ? 1.0 : 0.0;\n"); |
| 146 } | 146 } |
| 147 | 147 |
| 148 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, | 148 fragBuilder->codeAppendf("\t\t%s = %s;\n", 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, |
| 159 const GrProcessor& processor) { | 159 const GrProcessor& processor) { |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 void GLEllipseEffect::emitCode(EmitArgs& args) { | 289 void GLEllipseEffect::emitCode(EmitArgs& args) { |
| 290 const EllipseEffect& ee = args.fFp.cast<EllipseEffect>(); | 290 const EllipseEffect& ee = args.fFp.cast<EllipseEffect>(); |
| 291 const char *ellipseName; | 291 const char *ellipseName; |
| 292 // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2) | 292 // 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. | 293 // The last two terms can underflow on mediump, so we use highp. |
| 294 fEllipseUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_
Visibility, | 294 fEllipseUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_
Visibility, |
| 295 kVec4f_GrSLType, kHigh_GrSLPrecision, | 295 kVec4f_GrSLType, kHigh_GrSLPrecision, |
| 296 "ellipse", | 296 "ellipse", |
| 297 &ellipseName); | 297 &ellipseName); |
| 298 | 298 |
| 299 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder()
; | 299 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 300 const char* fragmentPos = fsBuilder->fragmentPosition(); | 300 const char* fragmentPos = fragBuilder->fragmentPosition(); |
| 301 | 301 |
| 302 // d is the offset to the ellipse center | 302 // d is the offset to the ellipse center |
| 303 fsBuilder->codeAppendf("\t\tvec2 d = %s.xy - %s.xy;\n", fragmentPos, ellipse
Name); | 303 fragBuilder->codeAppendf("\t\tvec2 d = %s.xy - %s.xy;\n", fragmentPos, ellip
seName); |
| 304 fsBuilder->codeAppendf("\t\tvec2 Z = d * %s.zw;\n", ellipseName); | 304 fragBuilder->codeAppendf("\t\tvec2 Z = d * %s.zw;\n", ellipseName); |
| 305 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1. | 305 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1. |
| 306 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, d) - 1.0;\n"); | 306 fragBuilder->codeAppend("\t\tfloat implicit = dot(Z, d) - 1.0;\n"); |
| 307 // grad_dot is the squared length of the gradient of the implicit. | 307 // grad_dot is the squared length of the gradient of the implicit. |
| 308 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); | 308 fragBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); |
| 309 // avoid calling inversesqrt on zero. | 309 // avoid calling inversesqrt on zero. |
| 310 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); | 310 fragBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
| 311 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_
dot);\n"); | 311 fragBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(gra
d_dot);\n"); |
| 312 | 312 |
| 313 switch (ee.getEdgeType()) { | 313 switch (ee.getEdgeType()) { |
| 314 case kFillAA_GrProcessorEdgeType: | 314 case kFillAA_GrProcessorEdgeType: |
| 315 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.
0, 1.0);\n"); | 315 fragBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist,
0.0, 1.0);\n"); |
| 316 break; | 316 break; |
| 317 case kInverseFillAA_GrProcessorEdgeType: | 317 case kInverseFillAA_GrProcessorEdgeType: |
| 318 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.
0, 1.0);\n"); | 318 fragBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist,
0.0, 1.0);\n"); |
| 319 break; | 319 break; |
| 320 case kFillBW_GrProcessorEdgeType: | 320 case kFillBW_GrProcessorEdgeType: |
| 321 fsBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 0.0 : 1
.0;\n"); | 321 fragBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 0.0 :
1.0;\n"); |
| 322 break; | 322 break; |
| 323 case kInverseFillBW_GrProcessorEdgeType: | 323 case kInverseFillBW_GrProcessorEdgeType: |
| 324 fsBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 1.0 : 0
.0;\n"); | 324 fragBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 1.0 :
0.0;\n"); |
| 325 break; | 325 break; |
| 326 case kHairlineAA_GrProcessorEdgeType: | 326 case kHairlineAA_GrProcessorEdgeType: |
| 327 SkFAIL("Hairline not expected here."); | 327 SkFAIL("Hairline not expected here."); |
| 328 } | 328 } |
| 329 | 329 |
| 330 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, | 330 fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, |
| 331 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")
).c_str()); | 331 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha
")).c_str()); |
| 332 } | 332 } |
| 333 | 333 |
| 334 void GLEllipseEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&, | 334 void GLEllipseEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&, |
| 335 GrProcessorKeyBuilder* b) { | 335 GrProcessorKeyBuilder* b) { |
| 336 const EllipseEffect& ee = effect.cast<EllipseEffect>(); | 336 const EllipseEffect& ee = effect.cast<EllipseEffect>(); |
| 337 b->add32(ee.getEdgeType()); | 337 b->add32(ee.getEdgeType()); |
| 338 } | 338 } |
| 339 | 339 |
| 340 void GLEllipseEffect::onSetData(const GrGLSLProgramDataManager& pdman, | 340 void GLEllipseEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 341 const GrProcessor& effect) { | 341 const GrProcessor& effect) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 372 w /= 2; | 372 w /= 2; |
| 373 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval
.fTop + w), w); | 373 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval
.fTop + w), w); |
| 374 } else { | 374 } else { |
| 375 w /= 2; | 375 w /= 2; |
| 376 h /= 2; | 376 h /= 2; |
| 377 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, ova
l.fTop + h), w, h); | 377 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, ova
l.fTop + h), w, h); |
| 378 } | 378 } |
| 379 | 379 |
| 380 return nullptr; | 380 return nullptr; |
| 381 } | 381 } |
| OLD | NEW |