| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "GrAAConvexPathRenderer.h" | 9 #include "GrAAConvexPathRenderer.h" |
| 10 | 10 |
| 11 #include "GrContext.h" | 11 #include "GrContext.h" |
| 12 #include "GrDrawState.h" | 12 #include "GrDrawState.h" |
| 13 #include "GrDrawTargetCaps.h" | 13 #include "GrDrawTargetCaps.h" |
| 14 #include "GrEffect.h" | 14 #include "GrProcessor.h" |
| 15 #include "GrPathUtils.h" | 15 #include "GrPathUtils.h" |
| 16 #include "GrTBackendEffectFactory.h" | 16 #include "GrTBackendProcessorFactory.h" |
| 17 #include "SkString.h" | 17 #include "SkString.h" |
| 18 #include "SkStrokeRec.h" | 18 #include "SkStrokeRec.h" |
| 19 #include "SkTraceEvent.h" | 19 #include "SkTraceEvent.h" |
| 20 | 20 |
| 21 #include "gl/builders/GrGLFullProgramBuilder.h" | 21 #include "gl/builders/GrGLFullProgramBuilder.h" |
| 22 #include "gl/GrGLEffect.h" | 22 #include "gl/GrGLProcessor.h" |
| 23 #include "gl/GrGLSL.h" | 23 #include "gl/GrGLSL.h" |
| 24 #include "gl/GrGLGeometryProcessor.h" | 24 #include "gl/GrGLGeometryProcessor.h" |
| 25 | 25 |
| 26 #include "effects/GrGeometryProcessor.h" | 26 #include "GrGeometryProcessor.h" |
| 27 | 27 |
| 28 GrAAConvexPathRenderer::GrAAConvexPathRenderer() { | 28 GrAAConvexPathRenderer::GrAAConvexPathRenderer() { |
| 29 } | 29 } |
| 30 | 30 |
| 31 struct Segment { | 31 struct Segment { |
| 32 enum { | 32 enum { |
| 33 // These enum values are assumed in member functions below. | 33 // These enum values are assumed in member functions below. |
| 34 kLine = 0, | 34 kLine = 0, |
| 35 kQuad = 1, | 35 kQuad = 1, |
| 36 } fType; | 36 } fType; |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 * distance with negative being inside, positive outside. The edge is specified
in | 500 * distance with negative being inside, positive outside. The edge is specified
in |
| 501 * window space (y-down). If either the third or fourth component of the interpo
lated | 501 * window space (y-down). If either the third or fourth component of the interpo
lated |
| 502 * vertex coord is > 0 then the pixel is considered outside the edge. This is us
ed to | 502 * vertex coord is > 0 then the pixel is considered outside the edge. This is us
ed to |
| 503 * attempt to trim to a portion of the infinite quad. | 503 * attempt to trim to a portion of the infinite quad. |
| 504 * Requires shader derivative instruction support. | 504 * Requires shader derivative instruction support. |
| 505 */ | 505 */ |
| 506 | 506 |
| 507 class QuadEdgeEffect : public GrGeometryProcessor { | 507 class QuadEdgeEffect : public GrGeometryProcessor { |
| 508 public: | 508 public: |
| 509 | 509 |
| 510 static GrEffect* Create() { | 510 static GrGeometryProcessor* Create() { |
| 511 GR_CREATE_STATIC_EFFECT(gQuadEdgeEffect, QuadEdgeEffect, ()); | 511 GR_CREATE_STATIC_GEOMETRY_PROCESSOR(gQuadEdgeEffect, QuadEdgeEffect, ())
; |
| 512 gQuadEdgeEffect->ref(); | 512 gQuadEdgeEffect->ref(); |
| 513 return gQuadEdgeEffect; | 513 return gQuadEdgeEffect; |
| 514 } | 514 } |
| 515 | 515 |
| 516 virtual ~QuadEdgeEffect() {} | 516 virtual ~QuadEdgeEffect() {} |
| 517 | 517 |
| 518 static const char* Name() { return "QuadEdge"; } | 518 static const char* Name() { return "QuadEdge"; } |
| 519 | 519 |
| 520 virtual void getConstantColorComponents(GrColor* color, | 520 virtual void getConstantColorComponents(GrColor* color, |
| 521 uint32_t* validFlags) const SK_OVERR
IDE { | 521 uint32_t* validFlags) const SK_OVERR
IDE { |
| 522 *validFlags = 0; | 522 *validFlags = 0; |
| 523 } | 523 } |
| 524 | 524 |
| 525 const GrShaderVar& inQuadEdge() const { return fInQuadEdge; } | 525 const GrShaderVar& inQuadEdge() const { return fInQuadEdge; } |
| 526 | 526 |
| 527 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 527 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERR
IDE { |
| 528 return GrTBackendEffectFactory<QuadEdgeEffect>::getInstance(); | 528 return GrTBackendGeometryProcessorFactory<QuadEdgeEffect>::getInstance()
; |
| 529 } | 529 } |
| 530 | 530 |
| 531 class GLEffect : public GrGLGeometryProcessor { | 531 class GLProcessor : public GrGLGeometryProcessor { |
| 532 public: | 532 public: |
| 533 GLEffect(const GrBackendEffectFactory& factory, const GrEffect&) | 533 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&
) |
| 534 : INHERITED (factory) {} | 534 : INHERITED (factory) {} |
| 535 | 535 |
| 536 virtual void emitCode(GrGLFullProgramBuilder* builder, | 536 virtual void emitCode(GrGLFullProgramBuilder* builder, |
| 537 const GrEffect& effect, | 537 const GrGeometryProcessor& geometryProcessor, |
| 538 const GrEffectKey& key, | 538 const GrProcessorKey& key, |
| 539 const char* outputColor, | 539 const char* outputColor, |
| 540 const char* inputColor, | 540 const char* inputColor, |
| 541 const TransformedCoordsArray&, | 541 const TransformedCoordsArray&, |
| 542 const TextureSamplerArray& samplers) SK_OVERRIDE { | 542 const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 543 const char *vsName, *fsName; | 543 const char *vsName, *fsName; |
| 544 builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName); | 544 builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName); |
| 545 | 545 |
| 546 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui
lder(); | 546 GrGLProcessorFragmentShaderBuilder* fsBuilder = builder->getFragment
ShaderBuilder(); |
| 547 | 547 |
| 548 SkAssertResult(fsBuilder->enableFeature( | 548 SkAssertResult(fsBuilder->enableFeature( |
| 549 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)
); | 549 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)
); |
| 550 fsBuilder->codeAppendf("\t\tfloat edgeAlpha;\n"); | 550 fsBuilder->codeAppendf("\t\tfloat edgeAlpha;\n"); |
| 551 | 551 |
| 552 // keep the derivative instructions outside the conditional | 552 // keep the derivative instructions outside the conditional |
| 553 fsBuilder->codeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); | 553 fsBuilder->codeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 554 fsBuilder->codeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); | 554 fsBuilder->codeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 555 fsBuilder->codeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsNa
me, fsName); | 555 fsBuilder->codeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsNa
me, fsName); |
| 556 // today we know z and w are in device space. We could use derivativ
es | 556 // today we know z and w are in device space. We could use derivativ
es |
| 557 fsBuilder->codeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5,
1.0);\n", fsName, | 557 fsBuilder->codeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5,
1.0);\n", fsName, |
| 558 fsName); | 558 fsName); |
| 559 fsBuilder->codeAppendf ("\t\t} else {\n"); | 559 fsBuilder->codeAppendf ("\t\t} else {\n"); |
| 560 fsBuilder->codeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvd
x.y,\n" | 560 fsBuilder->codeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvd
x.y,\n" |
| 561 "\t\t\t 2.0*%s.x*duvdy.x - duvd
y.y);\n", | 561 "\t\t\t 2.0*%s.x*duvdy.x - duvd
y.y);\n", |
| 562 fsName, fsName); | 562 fsName, fsName); |
| 563 fsBuilder->codeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fs
Name, fsName, | 563 fsBuilder->codeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fs
Name, fsName, |
| 564 fsName); | 564 fsName); |
| 565 fsBuilder->codeAppendf("\t\t\tedgeAlpha = " | 565 fsBuilder->codeAppendf("\t\t\tedgeAlpha = " |
| 566 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0
);\n\t\t}\n"); | 566 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0
);\n\t\t}\n"); |
| 567 | 567 |
| 568 | 568 |
| 569 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, | 569 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, |
| 570 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeA
lpha")).c_str()); | 570 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeA
lpha")).c_str()); |
| 571 | 571 |
| 572 const GrShaderVar& inQuadEdge = effect.cast<QuadEdgeEffect>().inQuad
Edge(); | 572 const GrShaderVar& inQuadEdge = geometryProcessor.cast<QuadEdgeEffec
t>().inQuadEdge(); |
| 573 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder
(); | 573 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder
(); |
| 574 vsBuilder->codeAppendf("\t%s = %s;\n", vsName, inQuadEdge.c_str()); | 574 vsBuilder->codeAppendf("\t%s = %s;\n", vsName, inQuadEdge.c_str()); |
| 575 } | 575 } |
| 576 | 576 |
| 577 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyB
uilder*) {} | 577 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcess
orKeyBuilder*) {} |
| 578 | 578 |
| 579 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_
OVERRIDE {} | 579 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&)
SK_OVERRIDE {} |
| 580 | 580 |
| 581 private: | 581 private: |
| 582 typedef GrGLGeometryProcessor INHERITED; | 582 typedef GrGLGeometryProcessor INHERITED; |
| 583 }; | 583 }; |
| 584 | 584 |
| 585 private: | 585 private: |
| 586 QuadEdgeEffect() | 586 QuadEdgeEffect() |
| 587 : fInQuadEdge(this->addVertexAttrib(GrShaderVar("inQuadEdge", | 587 : fInQuadEdge(this->addVertexAttrib(GrShaderVar("inQuadEdge", |
| 588 kVec4f_GrSLType, | 588 kVec4f_GrSLType, |
| 589 GrShaderVar::kAttribute_
TypeModifier))) { | 589 GrShaderVar::kAttribute_
TypeModifier))) { |
| 590 } | 590 } |
| 591 | 591 |
| 592 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { | 592 virtual bool onIsEqual(const GrProcessor& other) const SK_OVERRIDE { |
| 593 return true; | 593 return true; |
| 594 } | 594 } |
| 595 | 595 |
| 596 const GrShaderVar& fInQuadEdge; | 596 const GrShaderVar& fInQuadEdge; |
| 597 | 597 |
| 598 GR_DECLARE_EFFECT_TEST; | 598 GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
| 599 | 599 |
| 600 typedef GrEffect INHERITED; | 600 typedef GrFragmentProcessor INHERITED; |
| 601 }; | 601 }; |
| 602 | 602 |
| 603 GR_DEFINE_EFFECT_TEST(QuadEdgeEffect); | 603 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(QuadEdgeEffect); |
| 604 | 604 |
| 605 GrEffect* QuadEdgeEffect::TestCreate(SkRandom* random, | 605 GrGeometryProcessor* QuadEdgeEffect::TestCreate(SkRandom* random, |
| 606 GrContext*, | 606 GrContext*, |
| 607 const GrDrawTargetCaps& caps, | 607 const GrDrawTargetCaps& caps, |
| 608 GrTexture*[]) { | 608 GrTexture*[]) { |
| 609 // Doesn't work without derivative instructions. | 609 // Doesn't work without derivative instructions. |
| 610 return caps.shaderDerivativeSupport() ? QuadEdgeEffect::Create() : NULL; | 610 return caps.shaderDerivativeSupport() ? QuadEdgeEffect::Create() : NULL; |
| 611 } | 611 } |
| 612 | 612 |
| 613 /////////////////////////////////////////////////////////////////////////////// | 613 /////////////////////////////////////////////////////////////////////////////// |
| 614 | 614 |
| 615 bool GrAAConvexPathRenderer::canDrawPath(const SkPath& path, | 615 bool GrAAConvexPathRenderer::canDrawPath(const SkPath& path, |
| 616 const SkStrokeRec& stroke, | 616 const SkStrokeRec& stroke, |
| 617 const GrDrawTarget* target, | 617 const GrDrawTarget* target, |
| 618 bool antiAlias) const { | 618 bool antiAlias) const { |
| 619 return (target->caps()->shaderDerivativeSupport() && antiAlias && | 619 return (target->caps()->shaderDerivativeSupport() && antiAlias && |
| 620 stroke.isFillStyle() && !path.isInverseFillType() && path.isConvex()
); | 620 stroke.isFillStyle() && !path.isInverseFillType() && path.isConvex()
); |
| 621 } | 621 } |
| 622 | 622 |
| 623 namespace { | 623 namespace { |
| 624 | 624 |
| 625 // position + edge | 625 // position + edge |
| 626 extern const GrVertexAttrib gPathAttribs[] = { | 626 extern const GrVertexAttrib gPathAttribs[] = { |
| 627 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, | 627 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, |
| 628 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding} | 628 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttr
ibBinding} |
| 629 }; | 629 }; |
| 630 | 630 |
| 631 }; | 631 }; |
| 632 | 632 |
| 633 bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath, | 633 bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath, |
| 634 const SkStrokeRec&, | 634 const SkStrokeRec&, |
| 635 GrDrawTarget* target, | 635 GrDrawTarget* target, |
| 636 bool antiAlias) { | 636 bool antiAlias) { |
| 637 | 637 |
| 638 const SkPath* path = &origPath; | 638 const SkPath* path = &origPath; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 SkRect devBounds; | 674 SkRect devBounds; |
| 675 if (!get_segments(*path, viewMatrix, &segments, &fanPt, &vCount, &iCount, &d
evBounds)) { | 675 if (!get_segments(*path, viewMatrix, &segments, &fanPt, &vCount, &iCount, &d
evBounds)) { |
| 676 return false; | 676 return false; |
| 677 } | 677 } |
| 678 | 678 |
| 679 // Our computed verts should all be within one pixel of the segment control
points. | 679 // Our computed verts should all be within one pixel of the segment control
points. |
| 680 devBounds.outset(SK_Scalar1, SK_Scalar1); | 680 devBounds.outset(SK_Scalar1, SK_Scalar1); |
| 681 | 681 |
| 682 drawState->setVertexAttribs<gPathAttribs>(SK_ARRAY_COUNT(gPathAttribs), size
of(QuadVertex)); | 682 drawState->setVertexAttribs<gPathAttribs>(SK_ARRAY_COUNT(gPathAttribs), size
of(QuadVertex)); |
| 683 | 683 |
| 684 GrEffect* quadEffect = QuadEdgeEffect::Create(); | 684 GrGeometryProcessor* quadProcessor = QuadEdgeEffect::Create(); |
| 685 drawState->setGeometryProcessor(quadEffect)->unref(); | 685 drawState->setGeometryProcessor(quadProcessor)->unref(); |
| 686 | 686 |
| 687 GrDrawTarget::AutoReleaseGeometry arg(target, vCount, iCount); | 687 GrDrawTarget::AutoReleaseGeometry arg(target, vCount, iCount); |
| 688 if (!arg.succeeded()) { | 688 if (!arg.succeeded()) { |
| 689 return false; | 689 return false; |
| 690 } | 690 } |
| 691 verts = reinterpret_cast<QuadVertex*>(arg.vertices()); | 691 verts = reinterpret_cast<QuadVertex*>(arg.vertices()); |
| 692 idxs = reinterpret_cast<uint16_t*>(arg.indices()); | 692 idxs = reinterpret_cast<uint16_t*>(arg.indices()); |
| 693 | 693 |
| 694 SkSTArray<kPreallocDrawCnt, Draw, true> draws; | 694 SkSTArray<kPreallocDrawCnt, Draw, true> draws; |
| 695 create_vertices(segments, fanPt, &draws, verts, idxs); | 695 create_vertices(segments, fanPt, &draws, verts, idxs); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 713 vOffset, // start vertex | 713 vOffset, // start vertex |
| 714 0, // start index | 714 0, // start index |
| 715 draw.fVertexCnt, | 715 draw.fVertexCnt, |
| 716 draw.fIndexCnt, | 716 draw.fIndexCnt, |
| 717 &devBounds); | 717 &devBounds); |
| 718 vOffset += draw.fVertexCnt; | 718 vOffset += draw.fVertexCnt; |
| 719 } | 719 } |
| 720 | 720 |
| 721 return true; | 721 return true; |
| 722 } | 722 } |
| OLD | NEW |