diff options
author | jvanverth@google.com <jvanverth@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-01-22 13:34:01 +0000 |
---|---|---|
committer | jvanverth@google.com <jvanverth@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-01-22 13:34:01 +0000 |
commit | 46d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24 (patch) | |
tree | 3ed0089389283fd2d9cfa8bea196b9363ee60a9d /src/gpu | |
parent | 9d5f99bc309a7d733e33a149bef295ae3c8b3ac1 (diff) |
Add GPU support for axis-aligned ovals:
- Add drawOval base function to SkDevice, and override in SkGpuDevice
- Move isSimilarityMatrix to SkMatrix (renamed to isSimilarity) and fixed up unit test
- Since both SkGpuDevice::drawOval() and GrContext::drawPath() can try to draw ovals, added GrContext::canDrawOval() and GrContext::internalDrawOval() to avoid duplicate code
- Hooked in axis-aligned oval fill shader
- Enabled GPU stroked circles
- Added stroked circle bench test
Review URL: https://codereview.appspot.com/7137050
git-svn-id: http://skia.googlecode.com/svn/trunk@7304 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/gpu')
-rw-r--r-- | src/gpu/GrContext.cpp | 204 | ||||
-rw-r--r-- | src/gpu/GrDrawState.h | 3 | ||||
-rw-r--r-- | src/gpu/SkGpuDevice.cpp | 31 | ||||
-rw-r--r-- | src/gpu/gl/GrGLProgram.cpp | 7 |
4 files changed, 169 insertions, 76 deletions
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp index 43dda9c75c..97391997fc 100644 --- a/src/gpu/GrContext.cpp +++ b/src/gpu/GrContext.cpp @@ -935,59 +935,62 @@ struct CircleVertex { SkScalar fInnerRadius; }; -/* Returns true if will map a circle to another circle. This can be true - * if the matrix only includes square-scale, rotation, translation. - */ -inline bool isSimilarityTransformation(const SkMatrix& matrix, - SkScalar tol = SK_ScalarNearlyZero) { - if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) { - return true; - } - if (matrix.hasPerspective()) { - return false; - } +inline bool circleStaysCircle(const SkMatrix& m) { + return m.isSimilarity(); +} - SkScalar mx = matrix.get(SkMatrix::kMScaleX); - SkScalar sx = matrix.get(SkMatrix::kMSkewX); - SkScalar my = matrix.get(SkMatrix::kMScaleY); - SkScalar sy = matrix.get(SkMatrix::kMSkewY); +} - if (mx == 0 && sx == 0 && my == 0 && sy == 0) { - return false; - } +void GrContext::drawOval(const GrPaint& paint, + const GrRect& oval, + const SkStrokeRec& stroke) { - // it has scales or skews, but it could also be rotation, check it out. - SkVector vec[2]; - vec[0].set(mx, sx); - vec[1].set(sy, my); + if (!canDrawOval(paint, oval, stroke)) { + SkPath path; + path.addOval(oval); + this->drawPath(paint, path, stroke); + return; + } - return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) && - SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(), - SkScalarSquare(tol)); + internalDrawOval(paint, oval, stroke); } +bool GrContext::canDrawOval(const GrPaint& paint, const GrRect& oval, const SkStrokeRec& stroke) const { + + if (!paint.isAntiAlias()) { + return false; + } + + // we can draw circles in any style + bool isCircle = SkScalarNearlyEqual(oval.width(), oval.height()) + && circleStaysCircle(this->getMatrix()); + // and for now, axis-aligned ellipses only with fill or stroke-and-fill + SkStrokeRec::Style style = stroke.getStyle(); + bool isStroke = (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style); + bool isFilledAxisAlignedEllipse = this->getMatrix().rectStaysRect() && !isStroke; + + return isCircle || isFilledAxisAlignedEllipse; } -// TODO: strokeWidth can't be larger than zero right now. -// It will be fixed when drawPath() can handle strokes. -void GrContext::drawOval(const GrPaint& paint, - const GrRect& rect, - SkScalar strokeWidth) { - GrAssert(strokeWidth <= 0); - if (!isSimilarityTransformation(this->getMatrix()) || - !paint.isAntiAlias() || - rect.height() != rect.width()) { - SkPath path; - path.addOval(rect); - path.setFillType(SkPath::kWinding_FillType); - SkStrokeRec stroke(0 == strokeWidth ? SkStrokeRec::kHairline_InitStyle : - SkStrokeRec::kFill_InitStyle); - if (strokeWidth > 0) { - stroke.setStrokeStyle(strokeWidth, true); - } - this->internalDrawPath(paint, path, stroke); - return; +void GrContext::internalDrawOval(const GrPaint& paint, + const GrRect& oval, + const SkStrokeRec& stroke) { + + SkScalar xRadius = SkScalarHalf(oval.width()); + SkScalar yRadius = SkScalarHalf(oval.height()); + + SkScalar strokeWidth = stroke.getWidth(); + SkStrokeRec::Style style = stroke.getStyle(); + + bool isCircle = SkScalarNearlyEqual(xRadius, yRadius) && circleStaysCircle(this->getMatrix()); +#ifdef SK_DEBUG + { + // we should have checked for this previously + bool isStroke = (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style); + bool isFilledAxisAlignedEllipse = this->getMatrix().rectStaysRect() && !isStroke; + SkASSERT(paint.isAntiAlias() && (isCircle || isFilledAxisAlignedEllipse)); } +#endif GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING); @@ -1008,22 +1011,6 @@ void GrContext::drawOval(const GrPaint& paint, GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit; GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout)); - GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY()); - SkScalar radius = SkScalarHalf(rect.width()); - - vm.mapPoints(¢er, 1); - radius = vm.mapRadius(radius); - - SkScalar outerRadius = radius; - SkScalar innerRadius = 0; - SkScalar halfWidth = 0; - if (strokeWidth == 0) { - halfWidth = SkScalarHalf(SK_Scalar1); - - outerRadius += halfWidth; - innerRadius = SkMaxScalar(0, radius - halfWidth); - } - GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0); if (!geo.succeeded()) { GrPrintf("Failed to get space for vertices!\n"); @@ -1032,26 +1019,93 @@ void GrContext::drawOval(const GrPaint& paint, CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); + GrPoint center = GrPoint::Make(oval.centerX(), oval.centerY()); + vm.mapPoints(¢er, 1); + + SkScalar L; + SkScalar R; + SkScalar T; + SkScalar B; + + if (isCircle) { + drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType); + + xRadius = vm.mapRadius(xRadius); + + SkScalar outerRadius = xRadius; + SkScalar innerRadius = 0; + SkScalar halfWidth = 0; + if (style != SkStrokeRec::kFill_Style) { + strokeWidth = vm.mapRadius(strokeWidth); + if (SkScalarNearlyZero(strokeWidth)) { + halfWidth = SK_ScalarHalf; + } else { + halfWidth = SkScalarHalf(strokeWidth); + } + + outerRadius += halfWidth; + if (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style) { + innerRadius = SkMaxScalar(0, xRadius - halfWidth); + } + } + + for (int i = 0; i < 4; ++i) { + verts[i].fCenter = center; + verts[i].fOuterRadius = outerRadius; + verts[i].fInnerRadius = innerRadius; + } + + L = -outerRadius; + R = +outerRadius; + T = -outerRadius; + B = +outerRadius; + } else { // is axis-aligned ellipse + drawState->setVertexEdgeType(GrDrawState::kEllipse_EdgeType); + + SkRect xformedRect; + vm.mapRect(&xformedRect, oval); + + xRadius = SkScalarHalf(xformedRect.width()); + yRadius = SkScalarHalf(xformedRect.height()); + + if (style == SkStrokeRec::kStrokeAndFill_Style && strokeWidth > 0.0f) { + SkScalar halfWidth = SkScalarHalf(strokeWidth); + // do (potentially) anisotropic mapping + SkVector scaledStroke; + scaledStroke.set(halfWidth, halfWidth); + vm.mapVectors(&scaledStroke, 1); + // this is legit only if scale & translation (which should be the case at the moment) + xRadius += scaledStroke.fX; + yRadius += scaledStroke.fY; + } + + SkScalar ratio = SkScalarDiv(xRadius, yRadius); + + for (int i = 0; i < 4; ++i) { + verts[i].fCenter = center; + verts[i].fOuterRadius = xRadius; + verts[i].fInnerRadius = ratio; + } + + L = -xRadius; + R = +xRadius; + T = -yRadius; + B = +yRadius; + } + // The fragment shader will extend the radius out half a pixel // to antialias. Expand the drawn rect here so all the pixels // will be captured. - SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f); - SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f); - SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f); - SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f); + L += center.fX - SK_ScalarHalf; + R += center.fX + SK_ScalarHalf; + T += center.fY - SK_ScalarHalf; + B += center.fY + SK_ScalarHalf; verts[0].fPos = SkPoint::Make(L, T); verts[1].fPos = SkPoint::Make(R, T); verts[2].fPos = SkPoint::Make(L, B); verts[3].fPos = SkPoint::Make(R, B); - for (int i = 0; i < 4; ++i) { - verts[i].fCenter = center; - verts[i].fOuterRadius = outerRadius; - verts[i].fInnerRadius = innerRadius; - } - - drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType); target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4); } @@ -1065,10 +1119,10 @@ void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrok } SkRect ovalRect; - if ((stroke.isHairlineStyle() || stroke.isFillStyle()) && !path.isInverseFillType() && - path.isOval(&ovalRect)) { - SkScalar width = stroke.isHairlineStyle() ? 0 : -SK_Scalar1; - this->drawOval(paint, ovalRect, width); + bool isOval = path.isOval(&ovalRect); + + if (isOval && !path.isInverseFillType() && this->canDrawOval(paint, ovalRect, stroke)) { + this->drawOval(paint, ovalRect, stroke); return; } diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h index 13ed287803..4a99ecb942 100644 --- a/src/gpu/GrDrawState.h +++ b/src/gpu/GrDrawState.h @@ -681,6 +681,9 @@ public: /* Circle specified as center_x, center_y, outer_radius, inner_radius all in window space (y-down). */ kCircle_EdgeType, + /* Axis-aligned ellipse specified as center_x, center_y, x_radius, x_radius/y_radius + all in window space (y-down). */ + kEllipse_EdgeType, kVertexEdgeTypeCnt }; diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp index 0cf3b35f2a..cedb7d4e8a 100644 --- a/src/gpu/SkGpuDevice.cpp +++ b/src/gpu/SkGpuDevice.cpp @@ -674,6 +674,35 @@ void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, fContext->drawRect(grPaint, rect, doStroke ? width : -1); } +/////////////////////////////////////////////////////////////////////////////// + +void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, + const SkPaint& paint) { + CHECK_FOR_NODRAW_ANNOTATION(paint); + CHECK_SHOULD_DRAW(draw, false); + + bool usePath = false; + // some basic reasons we might need to call drawPath... + if (paint.getMaskFilter() || paint.getPathEffect()) { + usePath = true; + } + + if (usePath) { + SkPath path; + path.addOval(oval); + this->drawPath(draw, path, paint, NULL, true); + return; + } + + GrPaint grPaint; + if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { + return; + } + SkStrokeRec stroke(paint); + + fContext->drawOval(grPaint, oval, stroke); +} + #include "SkMaskFilter.h" #include "SkBounder.h" @@ -912,7 +941,7 @@ void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, return; } - // can we cheat, and threat a thin stroke as a hairline w/ coverage + // can we cheat, and treat a thin stroke as a hairline w/ coverage // if we can, we draw lots faster (raster device does this same test) SkScalar hairlineCoverage; bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage); diff --git a/src/gpu/gl/GrGLProgram.cpp b/src/gpu/gl/GrGLProgram.cpp index 9853af246d..26127e4afd 100644 --- a/src/gpu/gl/GrGLProgram.cpp +++ b/src/gpu/gl/GrGLProgram.cpp @@ -276,6 +276,13 @@ bool GrGLProgram::genEdgeCoverage(SkString* coverageVar, builder->fFSCode.appendf("\tfloat innerAlpha = %s.w == 0.0 ? 1.0 : smoothstep(%s.w - 0.5, %s.w + 0.5, d);\n", fsName, fsName, fsName); builder->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n"); break; + case GrDrawState::kEllipse_EdgeType: + builder->fFSCode.append("\tfloat edgeAlpha;\n"); + builder->fFSCode.appendf("\tvec2 offset = (%s.xy - %s.xy);\n", builder->fragmentPosition(), fsName); + builder->fFSCode.appendf("\toffset.y *= %s.w;\n", fsName); + builder->fFSCode.append("\tfloat d = length(offset);\n"); + builder->fFSCode.appendf("\tedgeAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName); + break; default: GrCrash("Unknown Edge Type!"); break; |