blob: 2955c756419df6bc730af26299a52aa15d9ededd [file] [log] [blame]
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrOvalRenderer.h"
9
10#include "effects/GrCircleEdgeEffect.h"
11#include "effects/GrEllipseEdgeEffect.h"
12
13#include "GrDrawState.h"
14#include "GrDrawTarget.h"
15#include "SkStrokeRec.h"
16
17SK_DEFINE_INST_COUNT(GrOvalRenderer)
18
19namespace {
20
21struct CircleVertex {
22 GrPoint fPos;
23 GrPoint fCenter;
24 SkScalar fOuterRadius;
25 SkScalar fInnerRadius;
26};
27
28struct EllipseVertex {
29 GrPoint fPos;
30 GrPoint fCenter;
31 SkScalar fOuterXRadius;
32 SkScalar fOuterXYRatio;
33 SkScalar fInnerXRadius;
34 SkScalar fInnerXYRatio;
35};
36
37inline bool circle_stays_circle(const SkMatrix& m) {
38 return m.isSimilarity();
39}
40
41}
42
skia.committer@gmail.com7e328512013-03-23 07:01:28 +000043bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, const GrPaint& paint,
commit-bot@chromium.org81312832013-03-22 18:34:09 +000044 const GrRect& oval, const SkStrokeRec& stroke)
45{
46 if (!paint.isAntiAlias()) {
47 return false;
48 }
49
50 const SkMatrix& vm = context->getMatrix();
51
skia.committer@gmail.com7e328512013-03-23 07:01:28 +000052 // we can draw circles
53 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +000054 && circle_stays_circle(vm)) {
55 drawCircle(target, paint, oval, stroke);
56
57 // and axis-aligned ellipses only
58 } else if (vm.rectStaysRect()) {
59 drawEllipse(target, paint, oval, stroke);
60
61 } else {
62 return false;
63 }
64
65 return true;
66}
67
skia.committer@gmail.com7e328512013-03-23 07:01:28 +000068void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org81312832013-03-22 18:34:09 +000069 const GrPaint& paint,
70 const GrRect& circle,
71 const SkStrokeRec& stroke)
72{
73 GrDrawState* drawState = target->drawState();
74
75 const SkMatrix& vm = drawState->getViewMatrix();
76 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
77 vm.mapPoints(&center, 1);
78 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
79 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
80
81 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
82 if (!adcd.succeeded()) {
83 return;
84 }
85
86 // position + edge
87 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com054ae992013-04-01 20:06:51 +000088 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
89 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
commit-bot@chromium.org81312832013-03-22 18:34:09 +000090 };
91 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +000092 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
93
94 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
95 if (!geo.succeeded()) {
96 GrPrintf("Failed to get space for vertices!\n");
97 return;
98 }
99
100 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
101
102 SkStrokeRec::Style style = stroke.getStyle();
103 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
104 enum {
105 // the edge effects share this stage with glyph rendering
106 // (kGlyphMaskStage in GrTextContext) && SW path rendering
107 // (kPathMaskStage in GrSWMaskHelper)
108 kEdgeEffectStage = GrPaint::kTotalStages,
109 };
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000110
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000111 GrEffectRef* effect = GrCircleEdgeEffect::Create(isStroked);
112 static const int kCircleEdgeAttrIndex = 1;
113 drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref();
114
115 SkScalar innerRadius = 0.0f;
116 SkScalar outerRadius = radius;
117 SkScalar halfWidth = 0;
118 if (style != SkStrokeRec::kFill_Style) {
119 if (SkScalarNearlyZero(strokeWidth)) {
120 halfWidth = SK_ScalarHalf;
121 } else {
122 halfWidth = SkScalarHalf(strokeWidth);
123 }
124
125 outerRadius += halfWidth;
126 if (isStroked) {
127 innerRadius = SkMaxScalar(0, radius - halfWidth);
128 }
129 }
130
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000131 // The radii are outset for two reasons. First, it allows the shader to simply perform
132 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
133 // verts of the bounding box that is rendered and the outset ensures the box will cover all
134 // pixels partially covered by the circle.
135 outerRadius += SK_ScalarHalf;
136 innerRadius -= SK_ScalarHalf;
137
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000138 for (int i = 0; i < 4; ++i) {
139 verts[i].fCenter = center;
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000140 verts[i].fOuterRadius = outerRadius;
141 verts[i].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000142 }
143
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000144 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000145 center.fX - outerRadius,
146 center.fY - outerRadius,
147 center.fX + outerRadius,
148 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000149 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000150
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000151 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
152 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
153 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
154 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000155
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000156 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000157}
158
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000159void GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000160 const GrPaint& paint,
161 const GrRect& ellipse,
162 const SkStrokeRec& stroke)
163{
164 GrDrawState* drawState = target->drawState();
165#ifdef SK_DEBUG
166 {
167 // we should have checked for this previously
168 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
169 SkASSERT(paint.isAntiAlias() && isAxisAlignedEllipse);
170 }
171#endif
172
173 const SkMatrix& vm = drawState->getViewMatrix();
174 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
175 vm.mapPoints(&center, 1);
176 SkRect xformedRect;
177 vm.mapRect(&xformedRect, ellipse);
178
179 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
180 if (!adcd.succeeded()) {
181 return;
182 }
183
184 // position + edge
185 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com054ae992013-04-01 20:06:51 +0000186 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
187 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
188 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000189 };
190 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000191 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
192
193 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
194 if (!geo.succeeded()) {
195 GrPrintf("Failed to get space for vertices!\n");
196 return;
197 }
198
199 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
200
201 SkStrokeRec::Style style = stroke.getStyle();
202 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
203 enum {
204 // the edge effects share this stage with glyph rendering
205 // (kGlyphMaskStage in GrTextContext) && SW path rendering
206 // (kPathMaskStage in GrSWMaskHelper)
207 kEdgeEffectStage = GrPaint::kTotalStages,
208 };
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000209
210 GrEffectRef* effect = GrEllipseEdgeEffect::Create(isStroked);
211 static const int kEllipseCenterAttrIndex = 1;
212 static const int kEllipseEdgeAttrIndex = 2;
213 drawState->setEffect(kEdgeEffectStage, effect,
214 kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
215
216 SkScalar xRadius = SkScalarHalf(xformedRect.width());
217 SkScalar yRadius = SkScalarHalf(xformedRect.height());
218 SkScalar innerXRadius = 0.0f;
219 SkScalar innerRatio = 1.0f;
220
221 if (SkStrokeRec::kFill_Style != style) {
222 SkScalar strokeWidth = stroke.getWidth();
223
224 // do (potentially) anisotropic mapping
225 SkVector scaledStroke;
226 scaledStroke.set(strokeWidth, strokeWidth);
227 vm.mapVectors(&scaledStroke, 1);
228
229 if (SkScalarNearlyZero(scaledStroke.length())) {
230 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
231 } else {
232 scaledStroke.scale(0.5f);
233 }
234
235 // this is legit only if scale & translation (which should be the case at the moment)
236 if (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style) {
237 SkScalar innerYRadius = SkMaxScalar(0, yRadius - scaledStroke.fY);
238 if (innerYRadius > SK_ScalarNearlyZero) {
239 innerXRadius = SkMaxScalar(0, xRadius - scaledStroke.fX);
240 innerRatio = innerXRadius/innerYRadius;
241 }
242 }
243 xRadius += scaledStroke.fX;
244 yRadius += scaledStroke.fY;
245 }
246
247 SkScalar outerRatio = SkScalarDiv(xRadius, yRadius);
248
249 for (int i = 0; i < 4; ++i) {
250 verts[i].fCenter = center;
251 verts[i].fOuterXRadius = xRadius + 0.5f;
252 verts[i].fOuterXYRatio = outerRatio;
253 verts[i].fInnerXRadius = innerXRadius - 0.5f;
254 verts[i].fInnerXYRatio = innerRatio;
255 }
256
257 SkScalar L = -xRadius;
258 SkScalar R = +xRadius;
259 SkScalar T = -yRadius;
260 SkScalar B = +yRadius;
261
262 // We've extended the outer x radius out half a pixel to antialias.
263 // Expand the drawn rect here so all the pixels will be captured.
264 L += center.fX - SK_ScalarHalf;
265 R += center.fX + SK_ScalarHalf;
266 T += center.fY - SK_ScalarHalf;
267 B += center.fY + SK_ScalarHalf;
268
269 verts[0].fPos = SkPoint::Make(L, T);
270 verts[1].fPos = SkPoint::Make(R, T);
271 verts[2].fPos = SkPoint::Make(L, B);
272 verts[3].fPos = SkPoint::Make(R, B);
273
274 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
275}