blob: 7da89bd245a2974885db714e2f3118b84b0f0357 [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[] = {
88 {kVec2f_GrVertexAttribType, 0},
89 {kVec4f_GrVertexAttribType, sizeof(GrPoint)}
90 };
91 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
92 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
93 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
94
95 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
96 if (!geo.succeeded()) {
97 GrPrintf("Failed to get space for vertices!\n");
98 return;
99 }
100
101 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
102
103 SkStrokeRec::Style style = stroke.getStyle();
104 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
105 enum {
106 // the edge effects share this stage with glyph rendering
107 // (kGlyphMaskStage in GrTextContext) && SW path rendering
108 // (kPathMaskStage in GrSWMaskHelper)
109 kEdgeEffectStage = GrPaint::kTotalStages,
110 };
111 drawState->setAttribBindings(GrDrawState::kDefault_AttribBindings);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000112
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000113 GrEffectRef* effect = GrCircleEdgeEffect::Create(isStroked);
114 static const int kCircleEdgeAttrIndex = 1;
115 drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref();
116
117 SkScalar innerRadius = 0.0f;
118 SkScalar outerRadius = radius;
119 SkScalar halfWidth = 0;
120 if (style != SkStrokeRec::kFill_Style) {
121 if (SkScalarNearlyZero(strokeWidth)) {
122 halfWidth = SK_ScalarHalf;
123 } else {
124 halfWidth = SkScalarHalf(strokeWidth);
125 }
126
127 outerRadius += halfWidth;
128 if (isStroked) {
129 innerRadius = SkMaxScalar(0, radius - halfWidth);
130 }
131 }
132
133 for (int i = 0; i < 4; ++i) {
134 verts[i].fCenter = center;
135 verts[i].fOuterRadius = outerRadius + 0.5f;
136 verts[i].fInnerRadius = innerRadius - 0.5f;
137 }
138
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000139 // We've extended the outer radius out half a pixel to antialias.
140 // Expand the drawn rect here so all the pixels will be captured.
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000141 SkRect bounds = SkRect::MakeLTRB(
142 center.fX - outerRadius - SK_ScalarHalf,
143 center.fY - outerRadius - SK_ScalarHalf,
144 center.fX + outerRadius + SK_ScalarHalf,
145 center.fY + outerRadius + SK_ScalarHalf
146 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000147
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000148 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
149 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
150 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
151 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000152
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000153 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000154}
155
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000156void GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000157 const GrPaint& paint,
158 const GrRect& ellipse,
159 const SkStrokeRec& stroke)
160{
161 GrDrawState* drawState = target->drawState();
162#ifdef SK_DEBUG
163 {
164 // we should have checked for this previously
165 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
166 SkASSERT(paint.isAntiAlias() && isAxisAlignedEllipse);
167 }
168#endif
169
170 const SkMatrix& vm = drawState->getViewMatrix();
171 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
172 vm.mapPoints(&center, 1);
173 SkRect xformedRect;
174 vm.mapRect(&xformedRect, ellipse);
175
176 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
177 if (!adcd.succeeded()) {
178 return;
179 }
180
181 // position + edge
182 static const GrVertexAttrib kVertexAttribs[] = {
183 {kVec2f_GrVertexAttribType, 0},
184 {kVec2f_GrVertexAttribType, sizeof(GrPoint)},
185 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint)}
186 };
187 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
188 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
189 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
190
191 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
192 if (!geo.succeeded()) {
193 GrPrintf("Failed to get space for vertices!\n");
194 return;
195 }
196
197 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
198
199 SkStrokeRec::Style style = stroke.getStyle();
200 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
201 enum {
202 // the edge effects share this stage with glyph rendering
203 // (kGlyphMaskStage in GrTextContext) && SW path rendering
204 // (kPathMaskStage in GrSWMaskHelper)
205 kEdgeEffectStage = GrPaint::kTotalStages,
206 };
207 drawState->setAttribBindings(GrDrawState::kDefault_AttribBindings);
208
209 GrEffectRef* effect = GrEllipseEdgeEffect::Create(isStroked);
210 static const int kEllipseCenterAttrIndex = 1;
211 static const int kEllipseEdgeAttrIndex = 2;
212 drawState->setEffect(kEdgeEffectStage, effect,
213 kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
214
215 SkScalar xRadius = SkScalarHalf(xformedRect.width());
216 SkScalar yRadius = SkScalarHalf(xformedRect.height());
217 SkScalar innerXRadius = 0.0f;
218 SkScalar innerRatio = 1.0f;
219
220 if (SkStrokeRec::kFill_Style != style) {
221 SkScalar strokeWidth = stroke.getWidth();
222
223 // do (potentially) anisotropic mapping
224 SkVector scaledStroke;
225 scaledStroke.set(strokeWidth, strokeWidth);
226 vm.mapVectors(&scaledStroke, 1);
227
228 if (SkScalarNearlyZero(scaledStroke.length())) {
229 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
230 } else {
231 scaledStroke.scale(0.5f);
232 }
233
234 // this is legit only if scale & translation (which should be the case at the moment)
235 if (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style) {
236 SkScalar innerYRadius = SkMaxScalar(0, yRadius - scaledStroke.fY);
237 if (innerYRadius > SK_ScalarNearlyZero) {
238 innerXRadius = SkMaxScalar(0, xRadius - scaledStroke.fX);
239 innerRatio = innerXRadius/innerYRadius;
240 }
241 }
242 xRadius += scaledStroke.fX;
243 yRadius += scaledStroke.fY;
244 }
245
246 SkScalar outerRatio = SkScalarDiv(xRadius, yRadius);
247
248 for (int i = 0; i < 4; ++i) {
249 verts[i].fCenter = center;
250 verts[i].fOuterXRadius = xRadius + 0.5f;
251 verts[i].fOuterXYRatio = outerRatio;
252 verts[i].fInnerXRadius = innerXRadius - 0.5f;
253 verts[i].fInnerXYRatio = innerRatio;
254 }
255
256 SkScalar L = -xRadius;
257 SkScalar R = +xRadius;
258 SkScalar T = -yRadius;
259 SkScalar B = +yRadius;
260
261 // We've extended the outer x radius out half a pixel to antialias.
262 // Expand the drawn rect here so all the pixels will be captured.
263 L += center.fX - SK_ScalarHalf;
264 R += center.fX + SK_ScalarHalf;
265 T += center.fY - SK_ScalarHalf;
266 B += center.fY + SK_ScalarHalf;
267
268 verts[0].fPos = SkPoint::Make(L, T);
269 verts[1].fPos = SkPoint::Make(R, T);
270 verts[2].fPos = SkPoint::Make(L, B);
271 verts[3].fPos = SkPoint::Make(R, B);
272
273 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
274}