| 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 "GrRRectEffect.h" | 8 #include "GrRRectEffect.h" |
| 9 | 9 |
| 10 #include "GrConvexPolyEffect.h" | 10 #include "GrConvexPolyEffect.h" |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 149 |
| 150 void GLCircularRRectEffect::emitCode(EmitArgs& args) { | 150 void GLCircularRRectEffect::emitCode(EmitArgs& args) { |
| 151 const CircularRRectEffect& crre = args.fFp.cast<CircularRRectEffect>(); | 151 const CircularRRectEffect& crre = args.fFp.cast<CircularRRectEffect>(); |
| 152 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 152 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 153 const char *rectName; | 153 const char *rectName; |
| 154 const char *radiusPlusHalfName; | 154 const char *radiusPlusHalfName; |
| 155 // The inner rect is the rrect bounds inset by the radius. Its left, top, ri
ght, and bottom | 155 // The inner rect is the rrect bounds inset by the radius. Its left, top, ri
ght, and bottom |
| 156 // edges correspond to components x, y, z, and w, respectively. When a side
of the rrect has | 156 // edges correspond to components x, y, z, and w, respectively. When a side
of the rrect has |
| 157 // only rectangular corners, that side's value corresponds to the rect edge'
s value outset by | 157 // only rectangular corners, that side's value corresponds to the rect edge'
s value outset by |
| 158 // half a pixel. | 158 // half a pixel. |
| 159 fInnerRectUniform = uniformHandler->addUniform(GrGLSLUniformHandler::kFragme
nt_Visibility, | 159 fInnerRectUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 160 kVec4f_GrSLType, kDefault_GrS
LPrecision, | 160 kVec4f_GrSLType, kDefault_GrS
LPrecision, |
| 161 "innerRect", | 161 "innerRect", |
| 162 &rectName); | 162 &rectName); |
| 163 // x is (r + .5) and y is 1/(r + .5) | 163 // x is (r + .5) and y is 1/(r + .5) |
| 164 fRadiusPlusHalfUniform = uniformHandler->addUniform(GrGLSLUniformHandler::kF
ragment_Visibility, | 164 fRadiusPlusHalfUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 165 kVec2f_GrSLType, kDefaul
t_GrSLPrecision, | 165 kVec2f_GrSLType, kDefaul
t_GrSLPrecision, |
| 166 "radiusPlusHalf", | 166 "radiusPlusHalf", |
| 167 &radiusPlusHalfName); | 167 &radiusPlusHalfName); |
| 168 | 168 |
| 169 // If we're on a device with a "real" mediump then the length calculation co
uld overflow. | 169 // If we're on a device with a "real" mediump then the length calculation co
uld overflow. |
| 170 SkString clampedCircleDistance; | 170 SkString clampedCircleDistance; |
| 171 if (args.fGLSLCaps->floatPrecisionVaries()) { | 171 if (args.fGLSLCaps->floatPrecisionVaries()) { |
| 172 clampedCircleDistance.printf("clamp(%s.x * (1.0 - length(dxy * %s.y)), 0
.0, 1.0);", | 172 clampedCircleDistance.printf("clamp(%s.x * (1.0 - length(dxy * %s.y)), 0
.0, 1.0);", |
| 173 radiusPlusHalfName, radiusPlusHalfName); | 173 radiusPlusHalfName, radiusPlusHalfName); |
| 174 } else { | 174 } else { |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 GrGLSLProgramDataManager::UniformHandle fScaleUniform; | 501 GrGLSLProgramDataManager::UniformHandle fScaleUniform; |
| 502 SkRRect fPrevRRect; | 502 SkRRect fPrevRRect; |
| 503 typedef GrGLSLFragmentProcessor INHERITED; | 503 typedef GrGLSLFragmentProcessor INHERITED; |
| 504 }; | 504 }; |
| 505 | 505 |
| 506 void GLEllipticalRRectEffect::emitCode(EmitArgs& args) { | 506 void GLEllipticalRRectEffect::emitCode(EmitArgs& args) { |
| 507 const EllipticalRRectEffect& erre = args.fFp.cast<EllipticalRRectEffect>(); | 507 const EllipticalRRectEffect& erre = args.fFp.cast<EllipticalRRectEffect>(); |
| 508 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 508 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 509 const char *rectName; | 509 const char *rectName; |
| 510 // The inner rect is the rrect bounds inset by the x/y radii | 510 // The inner rect is the rrect bounds inset by the x/y radii |
| 511 fInnerRectUniform = uniformHandler->addUniform(GrGLSLUniformHandler::kFragme
nt_Visibility, | 511 fInnerRectUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 512 kVec4f_GrSLType, kDefault_GrS
LPrecision, | 512 kVec4f_GrSLType, kDefault_GrS
LPrecision, |
| 513 "innerRect", | 513 "innerRect", |
| 514 &rectName); | 514 &rectName); |
| 515 | 515 |
| 516 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 516 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 517 const char* fragmentPos = fragBuilder->fragmentPosition(); | 517 const char* fragmentPos = fragBuilder->fragmentPosition(); |
| 518 // At each quarter-ellipse corner we compute a vector that is the offset of
the fragment pos | 518 // At each quarter-ellipse corner we compute a vector that is the offset of
the fragment pos |
| 519 // to the ellipse center. The vector is pinned in x and y to be in the quart
er-plane relevant | 519 // to the ellipse center. The vector is pinned in x and y to be in the quart
er-plane relevant |
| 520 // to that corner. This means that points near the interior near the rrect t
op edge will have | 520 // to that corner. This means that points near the interior near the rrect t
op edge will have |
| 521 // a vector that points straight up for both the TL left and TR corners. Com
puting an | 521 // a vector that points straight up for both the TL left and TR corners. Com
puting an |
| 522 // alpha from this vector at either the TR or TL corner will give the correc
t result. Similarly, | 522 // alpha from this vector at either the TR or TL corner will give the correc
t result. Similarly, |
| 523 // fragments near the other three edges will get the correct AA. Fragments i
n the interior of | 523 // fragments near the other three edges will get the correct AA. Fragments i
n the interior of |
| 524 // the rrect will have a (0,0) vector at all four corners. So long as the ra
dii > 0.5 they will | 524 // the rrect will have a (0,0) vector at all four corners. So long as the ra
dii > 0.5 they will |
| 525 // correctly produce an alpha value of 1 at all four corners. We take the mi
n of all the alphas. | 525 // correctly produce an alpha value of 1 at all four corners. We take the mi
n of all the alphas. |
| 526 // | 526 // |
| 527 // The code below is a simplified version of the above that performs maxs on
the vector | 527 // The code below is a simplified version of the above that performs maxs on
the vector |
| 528 // components before computing distances and alpha values so that only one d
istance computation | 528 // components before computing distances and alpha values so that only one d
istance computation |
| 529 // need be computed to determine the min alpha. | 529 // need be computed to determine the min alpha. |
| 530 fragBuilder->codeAppendf("vec2 dxy0 = %s.xy - %s.xy;", rectName, fragmentPos
); | 530 fragBuilder->codeAppendf("vec2 dxy0 = %s.xy - %s.xy;", rectName, fragmentPos
); |
| 531 fragBuilder->codeAppendf("vec2 dxy1 = %s.xy - %s.zw;", fragmentPos, rectName
); | 531 fragBuilder->codeAppendf("vec2 dxy1 = %s.xy - %s.zw;", fragmentPos, rectName
); |
| 532 | 532 |
| 533 // If we're on a device with a "real" mediump then we'll do the distance com
putation in a space | 533 // If we're on a device with a "real" mediump then we'll do the distance com
putation in a space |
| 534 // that is normalized by the largest radius. The scale uniform will be scale
, 1/scale. The | 534 // that is normalized by the largest radius. The scale uniform will be scale
, 1/scale. The |
| 535 // radii uniform values are already in this normalized space. | 535 // radii uniform values are already in this normalized space. |
| 536 const char* scaleName = nullptr; | 536 const char* scaleName = nullptr; |
| 537 if (args.fGLSLCaps->floatPrecisionVaries()) { | 537 if (args.fGLSLCaps->floatPrecisionVaries()) { |
| 538 fScaleUniform = uniformHandler->addUniform(GrGLSLUniformHandler::kFragme
nt_Visibility, | 538 fScaleUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 539 kVec2f_GrSLType, kDefault_GrS
LPrecision, | 539 kVec2f_GrSLType, kDefault_GrS
LPrecision, |
| 540 "scale", &scaleName); | 540 "scale", &scaleName); |
| 541 } | 541 } |
| 542 | 542 |
| 543 // The uniforms with the inv squared radii are highp to prevent underflow. | 543 // The uniforms with the inv squared radii are highp to prevent underflow. |
| 544 switch (erre.getRRect().getType()) { | 544 switch (erre.getRRect().getType()) { |
| 545 case SkRRect::kSimple_Type: { | 545 case SkRRect::kSimple_Type: { |
| 546 const char *invRadiiXYSqdName; | 546 const char *invRadiiXYSqdName; |
| 547 fInvRadiiSqdUniform = uniformHandler->addUniform( | 547 fInvRadiiSqdUniform = uniformHandler->addUniform(kFragment_GrShaderF
lag, |
| 548 GrGLSLUniformHandler::k
Fragment_Visibility, | 548 kVec2f_GrSLType, |
| 549 kVec2f_GrSLType, kDefau
lt_GrSLPrecision, | 549 kDefault_GrSLPrecis
ion, |
| 550 "invRadiiXY", | 550 "invRadiiXY", |
| 551 &invRadiiXYSqdName); | 551 &invRadiiXYSqdName)
; |
| 552 fragBuilder->codeAppend("vec2 dxy = max(max(dxy0, dxy1), 0.0);"); | 552 fragBuilder->codeAppend("vec2 dxy = max(max(dxy0, dxy1), 0.0);"); |
| 553 if (scaleName) { | 553 if (scaleName) { |
| 554 fragBuilder->codeAppendf("dxy *= %s.y;", scaleName); | 554 fragBuilder->codeAppendf("dxy *= %s.y;", scaleName); |
| 555 } | 555 } |
| 556 // Z is the x/y offsets divided by squared radii. | 556 // Z is the x/y offsets divided by squared radii. |
| 557 fragBuilder->codeAppendf("vec2 Z = dxy * %s.xy;", invRadiiXYSqdName)
; | 557 fragBuilder->codeAppendf("vec2 Z = dxy * %s.xy;", invRadiiXYSqdName)
; |
| 558 break; | 558 break; |
| 559 } | 559 } |
| 560 case SkRRect::kNinePatch_Type: { | 560 case SkRRect::kNinePatch_Type: { |
| 561 const char *invRadiiLTRBSqdName; | 561 const char *invRadiiLTRBSqdName; |
| 562 fInvRadiiSqdUniform = uniformHandler->addUniform( | 562 fInvRadiiSqdUniform = uniformHandler->addUniform(kFragment_GrShaderF
lag, |
| 563 GrGLSLUniformHandler::k
Fragment_Visibility, | 563 kVec4f_GrSLType, |
| 564 kVec4f_GrSLType, kDefau
lt_GrSLPrecision, | 564 kDefault_GrSLPrecis
ion, |
| 565 "invRadiiLTRB", | 565 "invRadiiLTRB", |
| 566 &invRadiiLTRBSqdName); | 566 &invRadiiLTRBSqdNam
e); |
| 567 if (scaleName) { | 567 if (scaleName) { |
| 568 fragBuilder->codeAppendf("dxy0 *= %s.y;", scaleName); | 568 fragBuilder->codeAppendf("dxy0 *= %s.y;", scaleName); |
| 569 fragBuilder->codeAppendf("dxy1 *= %s.y;", scaleName); | 569 fragBuilder->codeAppendf("dxy1 *= %s.y;", scaleName); |
| 570 } | 570 } |
| 571 fragBuilder->codeAppend("vec2 dxy = max(max(dxy0, dxy1), 0.0);"); | 571 fragBuilder->codeAppend("vec2 dxy = max(max(dxy0, dxy1), 0.0);"); |
| 572 // Z is the x/y offsets divided by squared radii. We only care about
the (at most) one | 572 // Z is the x/y offsets divided by squared radii. We only care about
the (at most) one |
| 573 // corner where both the x and y offsets are positive, hence the max
es. (The inverse | 573 // corner where both the x and y offsets are positive, hence the max
es. (The inverse |
| 574 // squared radii will always be positive.) | 574 // squared radii will always be positive.) |
| 575 fragBuilder->codeAppendf("vec2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.z
w), 0.0);", | 575 fragBuilder->codeAppendf("vec2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.z
w), 0.0);", |
| 576 invRadiiLTRBSqdName, invRadiiLTRBSqdName); | 576 invRadiiLTRBSqdName, invRadiiLTRBSqdName); |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 if (rrect.isNinePatch()) { | 769 if (rrect.isNinePatch()) { |
| 770 return EllipticalRRectEffect::Create(edgeType, rrect); | 770 return EllipticalRRectEffect::Create(edgeType, rrect); |
| 771 } | 771 } |
| 772 return nullptr; | 772 return nullptr; |
| 773 } | 773 } |
| 774 } | 774 } |
| 775 } | 775 } |
| 776 | 776 |
| 777 return nullptr; | 777 return nullptr; |
| 778 } | 778 } |
| OLD | NEW |