blob: 45564bcce049c8c3cbf457d39da9a1efe3cd702f [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
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000010#include "GrEffect.h"
11#include "gl/GrGLEffect.h"
12#include "gl/GrGLSL.h"
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000013#include "gl/GrGLVertexEffect.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000014#include "GrTBackendEffectFactory.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000015
16#include "GrDrawState.h"
17#include "GrDrawTarget.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000018#include "GrGpu.h"
19
20#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000021#include "SkStrokeRec.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000022#include "SkTLazy.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000023
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000024#include "effects/GrVertexEffect.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000025#include "effects/GrRRectEffect.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000026
commit-bot@chromium.org81312832013-03-22 18:34:09 +000027namespace {
28
29struct CircleVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000030 SkPoint fPos;
31 SkPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000032 SkScalar fOuterRadius;
33 SkScalar fInnerRadius;
34};
35
36struct EllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000037 SkPoint fPos;
38 SkPoint fOffset;
39 SkPoint fOuterRadii;
40 SkPoint fInnerRadii;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000041};
42
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000043struct DIEllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000044 SkPoint fPos;
45 SkPoint fOuterOffset;
46 SkPoint fInnerOffset;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000047};
48
commit-bot@chromium.org81312832013-03-22 18:34:09 +000049inline bool circle_stays_circle(const SkMatrix& m) {
50 return m.isSimilarity();
51}
52
53}
54
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000055///////////////////////////////////////////////////////////////////////////////
56
57/**
58 * The output of this effect is a modulation of the input color and coverage for a circle,
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +000059 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000060 */
61
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000062class CircleEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000063public:
64 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000065 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
66 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000067
68 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000069 gCircleStrokeEdge->ref();
70 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000071 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000072 gCircleFillEdge->ref();
73 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000074 }
75 }
76
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000077 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000078 uint32_t* validFlags) const SK_OVERRIDE {
79 *validFlags = 0;
80 }
81
82 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
83 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
84 }
85
86 virtual ~CircleEdgeEffect() {}
87
88 static const char* Name() { return "CircleEdge"; }
89
90 inline bool isStroked() const { return fStroke; }
91
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000092 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000093 public:
94 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
95 : INHERITED (factory) {}
96
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000097 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000098 const GrDrawEffect& drawEffect,
99 EffectKey key,
100 const char* outputColor,
101 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000102 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000103 const TextureSamplerArray& samplers) SK_OVERRIDE {
104 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
105 const char *vsName, *fsName;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000106 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000107
108 const SkString* attrName =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000109 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
110 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000111
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000112 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000113 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
114 if (circleEffect.isStroked()) {
115 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
116 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
117 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000118
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000119 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000120 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000121 }
122
123 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
124 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
125
126 return circleEffect.isStroked() ? 0x1 : 0x0;
127 }
128
129 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
130
131 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000132 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000133 };
134
135
136private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000137 CircleEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000138 this->addVertexAttrib(kVec4f_GrSLType);
139 fStroke = stroke;
140 }
141
142 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
143 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
144 return cee.fStroke == fStroke;
145 }
146
147 bool fStroke;
148
149 GR_DECLARE_EFFECT_TEST;
150
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000151 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000152};
153
154GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
155
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000156GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000157 GrContext* context,
158 const GrDrawTargetCaps&,
159 GrTexture* textures[]) {
160 return CircleEdgeEffect::Create(random->nextBool());
161}
162
163///////////////////////////////////////////////////////////////////////////////
164
165/**
166 * The output of this effect is a modulation of the input color and coverage for an axis-aligned
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000167 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
168 * in both x and y directions.
169 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000170 * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000171 */
172
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000173class EllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000174public:
175 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000176 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
177 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000178
179 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000180 gEllipseStrokeEdge->ref();
181 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000182 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000183 gEllipseFillEdge->ref();
184 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185 }
186 }
187
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000188 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000189 uint32_t* validFlags) const SK_OVERRIDE {
190 *validFlags = 0;
191 }
192
193 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
194 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
195 }
196
197 virtual ~EllipseEdgeEffect() {}
198
199 static const char* Name() { return "EllipseEdge"; }
200
201 inline bool isStroked() const { return fStroke; }
202
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000203 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000204 public:
205 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
206 : INHERITED (factory) {}
207
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000208 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000209 const GrDrawEffect& drawEffect,
210 EffectKey key,
211 const char* outputColor,
212 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000213 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214 const TextureSamplerArray& samplers) SK_OVERRIDE {
215 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
216
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000217 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000218 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000219
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000220 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000222 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
223 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000224
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000225 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000226 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000227 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
228 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000229
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000230 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000231 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
232 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
233 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000234 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000235 // avoid calling inversesqrt on zero.
236 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000237 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000238 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000239
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000240 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000241 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000242 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
243 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
244 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
245 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
246 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000247 }
248
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000249 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000250 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000251 }
252
253 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
254 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
255
256 return ellipseEffect.isStroked() ? 0x1 : 0x0;
257 }
258
259 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
260 }
261
262 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000263 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000264 };
265
266private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000267 EllipseEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000268 this->addVertexAttrib(kVec2f_GrSLType);
269 this->addVertexAttrib(kVec4f_GrSLType);
270 fStroke = stroke;
271 }
272
273 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
274 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
275 return eee.fStroke == fStroke;
276 }
277
278 bool fStroke;
279
280 GR_DECLARE_EFFECT_TEST;
281
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000282 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000283};
284
285GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
286
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000287GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000288 GrContext* context,
289 const GrDrawTargetCaps&,
290 GrTexture* textures[]) {
291 return EllipseEdgeEffect::Create(random->nextBool());
292}
293
294///////////////////////////////////////////////////////////////////////////////
295
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000296/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000297 * The output of this effect is a modulation of the input color and coverage for an ellipse,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000298 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
299 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
300 * using differentials.
301 *
302 * The result is device-independent and can be used with any affine matrix.
303 */
304
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000305class DIEllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000306public:
307 enum Mode { kStroke = 0, kHairline, kFill };
308
309 static GrEffectRef* Create(Mode mode) {
310 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
311 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
312 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000313
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000314 if (kStroke == mode) {
315 gEllipseStrokeEdge->ref();
316 return gEllipseStrokeEdge;
317 } else if (kHairline == mode) {
318 gEllipseHairlineEdge->ref();
319 return gEllipseHairlineEdge;
320 } else {
321 gEllipseFillEdge->ref();
322 return gEllipseFillEdge;
323 }
324 }
325
326 virtual void getConstantColorComponents(GrColor* color,
327 uint32_t* validFlags) const SK_OVERRIDE {
328 *validFlags = 0;
329 }
330
331 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
332 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
333 }
334
335 virtual ~DIEllipseEdgeEffect() {}
336
337 static const char* Name() { return "DIEllipseEdge"; }
338
339 inline Mode getMode() const { return fMode; }
340
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000341 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000342 public:
343 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
344 : INHERITED (factory) {}
345
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000346 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000347 const GrDrawEffect& drawEffect,
348 EffectKey key,
349 const char* outputColor,
350 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000351 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000352 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000353 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
354
355 SkAssertResult(builder->enableFeature(
356 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
357
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000358 const char *vsOffsetName0, *fsOffsetName0;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000359 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000360 &vsOffsetName0, &fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000361 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000362 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
363 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000364 const char *vsOffsetName1, *fsOffsetName1;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000365 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000366 &vsOffsetName1, &fsOffsetName1);
367 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000368 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
369 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000370
371 // for outer curve
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000372 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000373 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000374 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
375 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000376 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
377 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000378 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000379
380 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000381 // avoid calling inversesqrt on zero.
382 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000383 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
384 if (kHairline == ellipseEffect.getMode()) {
385 // can probably do this with one step
386 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
387 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
388 } else {
389 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
390 }
391
392 // for inner curve
393 if (kStroke == ellipseEffect.getMode()) {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000394 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000395 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000396 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
397 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
398 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
399 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
400 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000401 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
402 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
403 }
404
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000405 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000406 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000407 }
408
409 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
410 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000411
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000412 return ellipseEffect.getMode();
413 }
414
415 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
416 }
417
418 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000419 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000420 };
421
422private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000423 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000424 this->addVertexAttrib(kVec2f_GrSLType);
425 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000426 fMode = mode;
427 }
428
429 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
430 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
431 return eee.fMode == fMode;
432 }
433
434 Mode fMode;
435
436 GR_DECLARE_EFFECT_TEST;
437
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000438 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000439};
440
441GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
442
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000443GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000444 GrContext* context,
445 const GrDrawTargetCaps&,
446 GrTexture* textures[]) {
447 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
448}
449
450///////////////////////////////////////////////////////////////////////////////
451
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000452void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000453 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000454}
455
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000456bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000457 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000458{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000459 bool useCoverageAA = useAA &&
460 !target->getDrawState().getRenderTarget()->isMultisampled() &&
461 !target->shouldDisableCoverageAAForBlend();
462
463 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000464 return false;
465 }
466
467 const SkMatrix& vm = context->getMatrix();
468
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000469 // we can draw circles
470 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000471 && circle_stays_circle(vm)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000472 this->drawCircle(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000473 // if we have shader derivative support, render as device-independent
474 } else if (target->caps()->shaderDerivativeSupport()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000475 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000476 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000477 } else if (vm.rectStaysRect()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000478 return this->drawEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000479 } else {
480 return false;
481 }
482
483 return true;
484}
485
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000486///////////////////////////////////////////////////////////////////////////////
487
robertphillips@google.com42903302013-04-20 12:26:07 +0000488// position + edge
489extern const GrVertexAttrib gCircleVertexAttribs[] = {
490 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000491 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000492};
493
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000494void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000495 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000496 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000497 const SkStrokeRec& stroke)
498{
499 GrDrawState* drawState = target->drawState();
500
501 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000502 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000503 vm.mapPoints(&center, 1);
504 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
505 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
506
bsalomon@google.com137f1342013-05-29 21:27:53 +0000507 GrDrawState::AutoViewMatrixRestore avmr;
508 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000509 return;
510 }
511
robertphillips@google.com42903302013-04-20 12:26:07 +0000512 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000513 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000514
515 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
516 if (!geo.succeeded()) {
517 GrPrintf("Failed to get space for vertices!\n");
518 return;
519 }
520
521 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
522
523 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000524 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
525 SkStrokeRec::kHairline_Style == style;
526 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000527
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000528 SkScalar innerRadius = 0.0f;
529 SkScalar outerRadius = radius;
530 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000531 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000532 if (SkScalarNearlyZero(strokeWidth)) {
533 halfWidth = SK_ScalarHalf;
534 } else {
535 halfWidth = SkScalarHalf(strokeWidth);
536 }
537
538 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000539 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000540 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000541 }
542 }
543
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000544 GrEffectRef* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000545 static const int kCircleEdgeAttrIndex = 1;
546 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
547
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000548 // The radii are outset for two reasons. First, it allows the shader to simply perform
549 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
550 // verts of the bounding box that is rendered and the outset ensures the box will cover all
551 // pixels partially covered by the circle.
552 outerRadius += SK_ScalarHalf;
553 innerRadius -= SK_ScalarHalf;
554
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000555 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000556 center.fX - outerRadius,
557 center.fY - outerRadius,
558 center.fX + outerRadius,
559 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000560 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000561
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000562 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000563 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
564 verts[0].fOuterRadius = outerRadius;
565 verts[0].fInnerRadius = innerRadius;
566
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000567 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000568 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000569 verts[1].fOuterRadius = outerRadius;
570 verts[1].fInnerRadius = innerRadius;
571
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000572 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000573 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
574 verts[2].fOuterRadius = outerRadius;
575 verts[2].fInnerRadius = innerRadius;
576
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000577 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000578 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
579 verts[3].fOuterRadius = outerRadius;
580 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000581
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000582 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000583}
584
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000585///////////////////////////////////////////////////////////////////////////////
586
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000587// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000588extern const GrVertexAttrib gEllipseVertexAttribs[] = {
589 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000590 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
591 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000592};
593
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000594// position + offsets
595extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
596 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000597 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
598 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000599};
600
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000601bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000602 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000603 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000604 const SkStrokeRec& stroke)
605{
606 GrDrawState* drawState = target->drawState();
607#ifdef SK_DEBUG
608 {
609 // we should have checked for this previously
610 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000611 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000612 }
613#endif
614
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000615 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000616 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000617 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000618 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000619 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
620 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000621 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000622 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000623 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000624 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000625
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000626 // do (potentially) anisotropic mapping of stroke
627 SkVector scaledStroke;
628 SkScalar strokeWidth = stroke.getWidth();
629 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
630 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
631
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000632 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000633 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
634 SkStrokeRec::kHairline_Style == style;
635 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000636
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000637 SkScalar innerXRadius = 0;
638 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000639 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000640 if (SkScalarNearlyZero(scaledStroke.length())) {
641 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
642 } else {
643 scaledStroke.scale(SK_ScalarHalf);
644 }
645
646 // we only handle thick strokes for near-circular ellipses
647 if (scaledStroke.length() > SK_ScalarHalf &&
648 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
649 return false;
650 }
651
652 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
653 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
654 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
655 return false;
656 }
657
658 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000659 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000660 innerXRadius = xRadius - scaledStroke.fX;
661 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000662 }
663
664 xRadius += scaledStroke.fX;
665 yRadius += scaledStroke.fY;
666 }
667
bsalomon@google.com137f1342013-05-29 21:27:53 +0000668 GrDrawState::AutoViewMatrixRestore avmr;
669 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000670 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000671 }
672
robertphillips@google.com42903302013-04-20 12:26:07 +0000673 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000674 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000675
676 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
677 if (!geo.succeeded()) {
678 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000679 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000680 }
681
682 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
683
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000684 GrEffectRef* effect = EllipseEdgeEffect::Create(isStrokeOnly &&
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000685 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000686
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000687 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000688 static const int kEllipseEdgeAttrIndex = 2;
689 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000690
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000691 // Compute the reciprocals of the radii here to save time in the shader
692 SkScalar xRadRecip = SkScalarInvert(xRadius);
693 SkScalar yRadRecip = SkScalarInvert(yRadius);
694 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
695 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000696
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000697 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000698 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000699 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000700 xRadius += SK_ScalarHalf;
701 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000702
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000703 SkRect bounds = SkRect::MakeLTRB(
704 center.fX - xRadius,
705 center.fY - yRadius,
706 center.fX + xRadius,
707 center.fY + yRadius
708 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000709
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000710 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000711 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
712 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
713 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000714
715 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000716 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
717 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
718 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000719
720 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000721 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
722 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
723 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000724
725 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000726 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
727 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
728 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000729
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000730 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000731
732 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000733}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000734
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000735bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000736 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000737 const SkRect& ellipse,
738 const SkStrokeRec& stroke)
739{
740 GrDrawState* drawState = target->drawState();
741 const SkMatrix& vm = drawState->getViewMatrix();
742
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000743 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000744 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000745 SkScalar yRadius = SkScalarHalf(ellipse.height());
746
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000747 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000748 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000749 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000750 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000751 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
752
753 SkScalar innerXRadius = 0;
754 SkScalar innerYRadius = 0;
755 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
756 SkScalar strokeWidth = stroke.getWidth();
757
758 if (SkScalarNearlyZero(strokeWidth)) {
759 strokeWidth = SK_ScalarHalf;
760 } else {
761 strokeWidth *= SK_ScalarHalf;
762 }
763
764 // we only handle thick strokes for near-circular ellipses
765 if (strokeWidth > SK_ScalarHalf &&
766 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
767 return false;
768 }
769
770 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
771 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
772 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
773 return false;
774 }
775
776 // set inner radius (if needed)
777 if (SkStrokeRec::kStroke_Style == style) {
778 innerXRadius = xRadius - strokeWidth;
779 innerYRadius = yRadius - strokeWidth;
780 }
781
782 xRadius += strokeWidth;
783 yRadius += strokeWidth;
784 }
785 if (DIEllipseEdgeEffect::kStroke == mode) {
786 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
787 DIEllipseEdgeEffect::kFill;
788 }
789 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
790 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
791
792 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
793 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
794
795 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
796 if (!geo.succeeded()) {
797 GrPrintf("Failed to get space for vertices!\n");
798 return false;
799 }
800
801 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
802
803 GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode);
804
805 static const int kEllipseOuterOffsetAttrIndex = 1;
806 static const int kEllipseInnerOffsetAttrIndex = 2;
807 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
808 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000809
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000810 // This expands the outer rect so that after CTM we end up with a half-pixel border
811 SkScalar a = vm[SkMatrix::kMScaleX];
812 SkScalar b = vm[SkMatrix::kMSkewX];
813 SkScalar c = vm[SkMatrix::kMSkewY];
814 SkScalar d = vm[SkMatrix::kMScaleY];
815 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
816 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
817 // This adjusts the "radius" to include the half-pixel border
818 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
819 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
820
821 SkRect bounds = SkRect::MakeLTRB(
822 center.fX - xRadius - geoDx,
823 center.fY - yRadius - geoDy,
824 center.fX + xRadius + geoDx,
825 center.fY + yRadius + geoDy
826 );
827
828 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
829 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
830 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
831
832 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
833 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
834 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
835
836 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
837 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
838 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
839
840 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
841 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
842 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
843
844 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
845
846 return true;
847}
848
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000849///////////////////////////////////////////////////////////////////////////////
850
851static const uint16_t gRRectIndices[] = {
852 // corners
853 0, 1, 5, 0, 5, 4,
854 2, 3, 7, 2, 7, 6,
855 8, 9, 13, 8, 13, 12,
856 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000857
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000858 // edges
859 1, 2, 6, 1, 6, 5,
860 4, 5, 9, 4, 9, 8,
861 6, 7, 11, 6, 11, 10,
862 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000863
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000864 // center
865 // we place this at the end so that we can ignore these indices when rendering stroke-only
866 5, 6, 10, 5, 10, 9
867};
868
869
870GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
871 if (NULL == fRRectIndexBuffer) {
872 fRRectIndexBuffer =
873 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
874 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000875#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000876 bool updated =
877#endif
878 fRRectIndexBuffer->updateData(gRRectIndices,
879 sizeof(gRRectIndices));
880 GR_DEBUGASSERT(updated);
881 }
882 }
883 return fRRectIndexBuffer;
884}
885
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000886bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA,
887 const SkRRect& outer, const SkRRect& origInner) {
888 GrDrawState::AutoRestoreEffects are;
889 if (!origInner.isEmpty()) {
890 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
891 if (!context->getMatrix().isIdentity()) {
892 if (!origInner.transform(context->getMatrix(), inner.writable())) {
893 return false;
894 }
895 }
896 bool applyAA = useAA &&
897 !target->getDrawState().getRenderTarget()->isMultisampled() &&
898 !target->shouldDisableCoverageAAForBlend();
899 GrEffectEdgeType edgeType = applyAA ? kInverseFillAA_GrEffectEdgeType :
900 kInverseFillBW_GrEffectEdgeType;
901 GrEffectRef* effect = GrRRectEffect::Create(edgeType, *inner);
902 if (NULL == effect) {
903 return false;
904 }
905 are.set(target->drawState());
906 target->drawState()->addCoverageEffect(effect)->unref();
907 }
908
909 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
910 return this->drawRRect(target, context, useAA, outer, fillRec);
911}
912
913bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA,
914 const SkRRect& rrect, const SkStrokeRec& stroke) {
915 if (rrect.isOval()) {
916 return this->drawOval(target, context, useAA, rrect.getBounds(), stroke);
917 }
918
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000919 bool useCoverageAA = useAA &&
920 !target->getDrawState().getRenderTarget()->isMultisampled() &&
921 !target->shouldDisableCoverageAAForBlend();
922
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000923 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000924 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000925 return false;
926 }
927
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000928 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000929
930 if (!vm.rectStaysRect() || !rrect.isSimple()) {
931 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000932 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000933
934 // do any matrix crunching before we reset the draw state for device coords
935 const SkRect& rrectBounds = rrect.getBounds();
936 SkRect bounds;
937 vm.mapRect(&bounds, rrectBounds);
938
939 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000940 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000941 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000942 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000943 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000944
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000945 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000946
947 // do (potentially) anisotropic mapping of stroke
948 SkVector scaledStroke;
949 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000950
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000951 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
952 SkStrokeRec::kHairline_Style == style;
953 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
954
955 if (hasStroke) {
956 if (SkStrokeRec::kHairline_Style == style) {
957 scaledStroke.set(1, 1);
958 } else {
959 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
960 vm[SkMatrix::kMSkewY]));
961 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
962 vm[SkMatrix::kMScaleY]));
963 }
964
965 // if half of strokewidth is greater than radius, we don't handle that right now
966 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
967 return false;
968 }
969 }
970
971 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
972 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
973 // patch will have fractional coverage. This only matters when the interior is actually filled.
974 // We could consider falling back to rect rendering here, since a tiny radius is
975 // indistinguishable from a square corner.
976 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000977 return false;
978 }
979
980 // reset to device coordinates
981 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000982 GrDrawState::AutoViewMatrixRestore avmr;
983 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000984 return false;
985 }
986
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000987 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
988 if (NULL == indexBuffer) {
989 GrPrintf("Failed to create index buffer!\n");
990 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000991 }
992
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000993 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000994 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000995 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000996 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000997
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000998 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
999 if (!geo.succeeded()) {
1000 GrPrintf("Failed to get space for vertices!\n");
1001 return false;
1002 }
1003 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001004
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001005 SkScalar innerRadius = 0.0f;
1006 SkScalar outerRadius = xRadius;
1007 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001008 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001009 if (SkScalarNearlyZero(scaledStroke.fX)) {
1010 halfWidth = SK_ScalarHalf;
1011 } else {
1012 halfWidth = SkScalarHalf(scaledStroke.fX);
1013 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001014
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001015 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001016 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001017 }
1018 outerRadius += halfWidth;
1019 bounds.outset(halfWidth, halfWidth);
1020 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001021
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001022 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001023
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001024 GrEffectRef* effect = CircleEdgeEffect::Create(isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001025 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001026 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001027
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001028 // The radii are outset for two reasons. First, it allows the shader to simply perform
1029 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1030 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1031 // pixels partially covered by the circle.
1032 outerRadius += SK_ScalarHalf;
1033 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001034
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001035 // Expand the rect so all the pixels will be captured.
1036 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001037
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001038 SkScalar yCoords[4] = {
1039 bounds.fTop,
1040 bounds.fTop + outerRadius,
1041 bounds.fBottom - outerRadius,
1042 bounds.fBottom
1043 };
1044 SkScalar yOuterRadii[4] = {
1045 -outerRadius,
1046 0,
1047 0,
1048 outerRadius
1049 };
1050 for (int i = 0; i < 4; ++i) {
1051 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1052 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1053 verts->fOuterRadius = outerRadius;
1054 verts->fInnerRadius = innerRadius;
1055 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001056
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001057 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1058 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1059 verts->fOuterRadius = outerRadius;
1060 verts->fInnerRadius = innerRadius;
1061 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001062
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001063 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1064 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1065 verts->fOuterRadius = outerRadius;
1066 verts->fInnerRadius = innerRadius;
1067 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001068
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001069 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1070 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1071 verts->fOuterRadius = outerRadius;
1072 verts->fInnerRadius = innerRadius;
1073 verts++;
1074 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001075
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001076 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001077 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1078 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001079 target->setIndexSourceToBuffer(indexBuffer);
1080 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001081
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001082 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001083 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001084 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001085 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001086
1087 SkScalar innerXRadius = 0.0f;
1088 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001089 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001090 if (SkScalarNearlyZero(scaledStroke.length())) {
1091 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1092 } else {
1093 scaledStroke.scale(SK_ScalarHalf);
1094 }
1095
1096 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001097 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001098 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1099 return false;
1100 }
1101
1102 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1103 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1104 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1105 return false;
1106 }
1107
1108 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001109 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001110 innerXRadius = xRadius - scaledStroke.fX;
1111 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001112 }
1113
1114 xRadius += scaledStroke.fX;
1115 yRadius += scaledStroke.fY;
1116 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1117 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001118
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001119 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001120
jvanverth@google.come3647412013-05-08 15:31:43 +00001121 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1122 if (!geo.succeeded()) {
1123 GrPrintf("Failed to get space for vertices!\n");
1124 return false;
1125 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001126 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001127
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001128 GrEffectRef* effect = EllipseEdgeEffect::Create(isStrokeOnly);
jvanverth@google.come3647412013-05-08 15:31:43 +00001129 static const int kEllipseOffsetAttrIndex = 1;
1130 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001131 drawState->addCoverageEffect(effect,
1132 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001133
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001134 // Compute the reciprocals of the radii here to save time in the shader
1135 SkScalar xRadRecip = SkScalarInvert(xRadius);
1136 SkScalar yRadRecip = SkScalarInvert(yRadius);
1137 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1138 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001139
1140 // Extend the radii out half a pixel to antialias.
1141 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1142 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001143
1144 // Expand the rect so all the pixels will be captured.
1145 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1146
1147 SkScalar yCoords[4] = {
1148 bounds.fTop,
1149 bounds.fTop + yOuterRadius,
1150 bounds.fBottom - yOuterRadius,
1151 bounds.fBottom
1152 };
1153 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001154 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001155 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1156 SK_ScalarNearlyZero,
1157 yOuterRadius
1158 };
1159
1160 for (int i = 0; i < 4; ++i) {
1161 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001162 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001163 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1164 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001165 verts++;
1166
1167 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1168 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001169 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1170 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001171 verts++;
1172
1173 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1174 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001175 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1176 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001177 verts++;
1178
1179 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1180 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001181 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1182 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001183 verts++;
1184 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001185
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001186 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001187 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1188 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001189 target->setIndexSourceToBuffer(indexBuffer);
1190 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1191 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001192
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001193 return true;
1194}