blob: f368dc8d899c06e02b63128e3db3f9a0b4a1e784 [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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +00009#include "GrOvalRenderer.h"
10
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000011#include "GrEffect.h"
12#include "gl/GrGLEffect.h"
13#include "gl/GrGLSL.h"
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000014#include "gl/GrGLVertexEffect.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000015#include "GrTBackendEffectFactory.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000016
17#include "GrDrawState.h"
18#include "GrDrawTarget.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000019#include "GrGpu.h"
20
21#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000022#include "SkStrokeRec.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000023#include "SkTLazy.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000024
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000025#include "effects/GrVertexEffect.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000026#include "effects/GrRRectEffect.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000027
commit-bot@chromium.org81312832013-03-22 18:34:09 +000028namespace {
29
30struct CircleVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000031 SkPoint fPos;
32 SkPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000033 SkScalar fOuterRadius;
34 SkScalar fInnerRadius;
35};
36
37struct EllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000038 SkPoint fPos;
39 SkPoint fOffset;
40 SkPoint fOuterRadii;
41 SkPoint fInnerRadii;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000042};
43
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000044struct DIEllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000045 SkPoint fPos;
46 SkPoint fOuterOffset;
47 SkPoint fInnerOffset;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000048};
49
commit-bot@chromium.org81312832013-03-22 18:34:09 +000050inline bool circle_stays_circle(const SkMatrix& m) {
51 return m.isSimilarity();
52}
53
54}
55
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000056///////////////////////////////////////////////////////////////////////////////
57
58/**
59 * 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 +000060 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000061 */
62
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000063class CircleEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000064public:
bsalomon83d081a2014-07-08 09:56:10 -070065 static GrEffect* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000066 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
67 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000068
69 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000070 gCircleStrokeEdge->ref();
71 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000072 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000073 gCircleFillEdge->ref();
74 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000075 }
76 }
77
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000078 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000079 uint32_t* validFlags) const SK_OVERRIDE {
80 *validFlags = 0;
81 }
82
83 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
84 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
85 }
86
87 virtual ~CircleEdgeEffect() {}
88
89 static const char* Name() { return "CircleEdge"; }
90
91 inline bool isStroked() const { return fStroke; }
92
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000093 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000094 public:
95 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
96 : INHERITED (factory) {}
97
joshualitt30ba4362014-08-21 20:18:45 -070098 virtual void emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000099 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700100 const GrEffectKey& key,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000101 const char* outputColor,
102 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000103 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000104 const TextureSamplerArray& samplers) SK_OVERRIDE {
105 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
106 const char *vsName, *fsName;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000107 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000108
joshualitt30ba4362014-08-21 20:18:45 -0700109 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
110 const SkString* attr0Name =
111 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
112 vsBuilder->codeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000113
joshualitt30ba4362014-08-21 20:18:45 -0700114 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
115 fsBuilder->codeAppendf("\tfloat d = length(%s.xy);\n", fsName);
116 fsBuilder->codeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000117 if (circleEffect.isStroked()) {
joshualitt30ba4362014-08-21 20:18:45 -0700118 fsBuilder->codeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
119 fsBuilder->codeAppend("\tedgeAlpha *= innerAlpha;\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000120 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000121
joshualitt30ba4362014-08-21 20:18:45 -0700122 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000123 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000124 }
125
bsalomon63e99f72014-07-21 08:03:14 -0700126 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
127 GrEffectKeyBuilder* b) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000128 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700129 b->add32(circleEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000130 }
131
kkinnunen7510b222014-07-30 00:04:16 -0700132 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000133
134 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000135 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000136 };
137
138
139private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000140 CircleEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000141 this->addVertexAttrib(kVec4f_GrSLType);
142 fStroke = stroke;
143 }
144
145 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
146 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
147 return cee.fStroke == fStroke;
148 }
149
150 bool fStroke;
151
152 GR_DECLARE_EFFECT_TEST;
153
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000154 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000155};
156
157GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
158
bsalomon83d081a2014-07-08 09:56:10 -0700159GrEffect* CircleEdgeEffect::TestCreate(SkRandom* random,
160 GrContext* context,
161 const GrDrawTargetCaps&,
162 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000163 return CircleEdgeEffect::Create(random->nextBool());
164}
165
166///////////////////////////////////////////////////////////////////////////////
167
168/**
169 * 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 +0000170 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
171 * in both x and y directions.
172 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000173 * 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 +0000174 */
175
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000176class EllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000177public:
bsalomon83d081a2014-07-08 09:56:10 -0700178 static GrEffect* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000179 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
180 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000181
182 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000183 gEllipseStrokeEdge->ref();
184 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000186 gEllipseFillEdge->ref();
187 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000188 }
189 }
190
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000191 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000192 uint32_t* validFlags) const SK_OVERRIDE {
193 *validFlags = 0;
194 }
195
196 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
197 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
198 }
199
200 virtual ~EllipseEdgeEffect() {}
201
202 static const char* Name() { return "EllipseEdge"; }
203
204 inline bool isStroked() const { return fStroke; }
205
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000206 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000207 public:
208 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
209 : INHERITED (factory) {}
210
joshualitt30ba4362014-08-21 20:18:45 -0700211 virtual void emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000212 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700213 const GrEffectKey& key,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214 const char* outputColor,
215 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000216 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000217 const TextureSamplerArray& samplers) SK_OVERRIDE {
218 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
219
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000220 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000221 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000222
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000223 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
joshualitt30ba4362014-08-21 20:18:45 -0700224
225 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000226 const SkString* attr0Name =
joshualitt30ba4362014-08-21 20:18:45 -0700227 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
228 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000229
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000230 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000231 const SkString* attr1Name =
joshualitt30ba4362014-08-21 20:18:45 -0700232 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
233 vsBuilder->codeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000234
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000235 // for outer curve
joshualitt30ba4362014-08-21 20:18:45 -0700236 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
237 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
238 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
239 fsBuilder->codeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
240 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000241 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700242 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
243 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
244 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000245
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000246 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000247 if (ellipseEffect.isStroked()) {
joshualitt30ba4362014-08-21 20:18:45 -0700248 fsBuilder->codeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
249 fsBuilder->codeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
250 fsBuilder->codeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
251 fsBuilder->codeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
252 fsBuilder->codeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000253 }
254
joshualitt30ba4362014-08-21 20:18:45 -0700255 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000256 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000257 }
258
bsalomon63e99f72014-07-21 08:03:14 -0700259 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
260 GrEffectKeyBuilder* b) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000261 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700262 b->add32(ellipseEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000263 }
264
kkinnunen7510b222014-07-30 00:04:16 -0700265 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000266 }
267
268 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000269 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000270 };
271
272private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000273 EllipseEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000274 this->addVertexAttrib(kVec2f_GrSLType);
275 this->addVertexAttrib(kVec4f_GrSLType);
276 fStroke = stroke;
277 }
278
279 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
280 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
281 return eee.fStroke == fStroke;
282 }
283
284 bool fStroke;
285
286 GR_DECLARE_EFFECT_TEST;
287
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000288 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000289};
290
291GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
292
bsalomon83d081a2014-07-08 09:56:10 -0700293GrEffect* EllipseEdgeEffect::TestCreate(SkRandom* random,
294 GrContext* context,
295 const GrDrawTargetCaps&,
296 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000297 return EllipseEdgeEffect::Create(random->nextBool());
298}
299
300///////////////////////////////////////////////////////////////////////////////
301
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000302/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000303 * 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 +0000304 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
305 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
306 * using differentials.
307 *
308 * The result is device-independent and can be used with any affine matrix.
309 */
310
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000311class DIEllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000312public:
313 enum Mode { kStroke = 0, kHairline, kFill };
314
bsalomon83d081a2014-07-08 09:56:10 -0700315 static GrEffect* Create(Mode mode) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000316 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
317 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
318 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000319
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000320 if (kStroke == mode) {
321 gEllipseStrokeEdge->ref();
322 return gEllipseStrokeEdge;
323 } else if (kHairline == mode) {
324 gEllipseHairlineEdge->ref();
325 return gEllipseHairlineEdge;
326 } else {
327 gEllipseFillEdge->ref();
328 return gEllipseFillEdge;
329 }
330 }
331
332 virtual void getConstantColorComponents(GrColor* color,
333 uint32_t* validFlags) const SK_OVERRIDE {
334 *validFlags = 0;
335 }
336
337 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
338 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
339 }
340
341 virtual ~DIEllipseEdgeEffect() {}
342
343 static const char* Name() { return "DIEllipseEdge"; }
344
345 inline Mode getMode() const { return fMode; }
346
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000347 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000348 public:
349 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
350 : INHERITED (factory) {}
351
joshualitt30ba4362014-08-21 20:18:45 -0700352 virtual void emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000353 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700354 const GrEffectKey& key,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000355 const char* outputColor,
356 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000357 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000358 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000359 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
360
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000361 const char *vsOffsetName0, *fsOffsetName0;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000362 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000363 &vsOffsetName0, &fsOffsetName0);
joshualitt30ba4362014-08-21 20:18:45 -0700364
365 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000366 const SkString* attr0Name =
joshualitt30ba4362014-08-21 20:18:45 -0700367 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
368 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000369 const char *vsOffsetName1, *fsOffsetName1;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000370 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000371 &vsOffsetName1, &fsOffsetName1);
372 const SkString* attr1Name =
joshualitt30ba4362014-08-21 20:18:45 -0700373 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
374 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000375
joshualitt30ba4362014-08-21 20:18:45 -0700376 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
377 SkAssertResult(fsBuilder->enableFeature(
378 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000379 // for outer curve
joshualitt30ba4362014-08-21 20:18:45 -0700380 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
381 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
382 fsBuilder->codeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
383 fsBuilder->codeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
384 fsBuilder->codeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000385 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000386 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000387
joshualitt30ba4362014-08-21 20:18:45 -0700388 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000389 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700390 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
391 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000392 if (kHairline == ellipseEffect.getMode()) {
393 // can probably do this with one step
joshualitt30ba4362014-08-21 20:18:45 -0700394 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
395 fsBuilder->codeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000396 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700397 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000398 }
399
400 // for inner curve
401 if (kStroke == ellipseEffect.getMode()) {
joshualitt30ba4362014-08-21 20:18:45 -0700402 fsBuilder->codeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
403 fsBuilder->codeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
404 fsBuilder->codeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
405 fsBuilder->codeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
406 fsBuilder->codeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000407 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
408 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
joshualitt30ba4362014-08-21 20:18:45 -0700409 fsBuilder->codeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
410 fsBuilder->codeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000411 }
412
joshualitt30ba4362014-08-21 20:18:45 -0700413 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000414 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000415 }
416
bsalomon63e99f72014-07-21 08:03:14 -0700417 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
418 GrEffectKeyBuilder* b) {
419 const DIEllipseEdgeEffect& ellipseEffect =
420 drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000421
bsalomon63e99f72014-07-21 08:03:14 -0700422 b->add32(ellipseEffect.getMode());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000423 }
424
kkinnunen7510b222014-07-30 00:04:16 -0700425 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000426 }
427
428 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000429 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000430 };
431
432private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000433 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000434 this->addVertexAttrib(kVec2f_GrSLType);
435 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000436 fMode = mode;
437 }
438
439 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
440 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
441 return eee.fMode == fMode;
442 }
443
444 Mode fMode;
445
446 GR_DECLARE_EFFECT_TEST;
447
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000448 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000449};
450
451GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
452
bsalomon83d081a2014-07-08 09:56:10 -0700453GrEffect* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
454 GrContext* context,
455 const GrDrawTargetCaps&,
456 GrTexture* textures[]) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000457 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
458}
459
460///////////////////////////////////////////////////////////////////////////////
461
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000462void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000463 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000464}
465
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000466bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000467 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000468{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000469 bool useCoverageAA = useAA &&
470 !target->getDrawState().getRenderTarget()->isMultisampled() &&
471 !target->shouldDisableCoverageAAForBlend();
472
473 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000474 return false;
475 }
476
477 const SkMatrix& vm = context->getMatrix();
478
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000479 // we can draw circles
480 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000481 && circle_stays_circle(vm)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000482 this->drawCircle(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000483 // if we have shader derivative support, render as device-independent
484 } else if (target->caps()->shaderDerivativeSupport()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000485 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000486 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000487 } else if (vm.rectStaysRect()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000488 return this->drawEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000489 } else {
490 return false;
491 }
492
493 return true;
494}
495
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000496///////////////////////////////////////////////////////////////////////////////
497
robertphillips@google.com42903302013-04-20 12:26:07 +0000498// position + edge
499extern const GrVertexAttrib gCircleVertexAttribs[] = {
500 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000501 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000502};
503
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000504void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000505 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000506 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000507 const SkStrokeRec& stroke)
508{
509 GrDrawState* drawState = target->drawState();
510
511 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000512 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000513 vm.mapPoints(&center, 1);
514 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
515 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
516
bsalomon@google.com137f1342013-05-29 21:27:53 +0000517 GrDrawState::AutoViewMatrixRestore avmr;
518 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000519 return;
520 }
521
egdaniel7b3d5ee2014-08-28 05:41:14 -0700522 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
523 sizeof(CircleVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000524
525 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
526 if (!geo.succeeded()) {
527 GrPrintf("Failed to get space for vertices!\n");
528 return;
529 }
530
531 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
532
533 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000534 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
535 SkStrokeRec::kHairline_Style == style;
536 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000537
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000538 SkScalar innerRadius = 0.0f;
539 SkScalar outerRadius = radius;
540 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000541 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000542 if (SkScalarNearlyZero(strokeWidth)) {
543 halfWidth = SK_ScalarHalf;
544 } else {
545 halfWidth = SkScalarHalf(strokeWidth);
546 }
547
548 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000549 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000550 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000551 }
552 }
553
bsalomon83d081a2014-07-08 09:56:10 -0700554 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000555 static const int kCircleEdgeAttrIndex = 1;
556 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
557
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000558 // The radii are outset for two reasons. First, it allows the shader to simply perform
559 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
560 // verts of the bounding box that is rendered and the outset ensures the box will cover all
561 // pixels partially covered by the circle.
562 outerRadius += SK_ScalarHalf;
563 innerRadius -= SK_ScalarHalf;
564
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000565 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000566 center.fX - outerRadius,
567 center.fY - outerRadius,
568 center.fX + outerRadius,
569 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000570 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000571
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000572 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000573 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
574 verts[0].fOuterRadius = outerRadius;
575 verts[0].fInnerRadius = innerRadius;
576
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000577 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000578 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000579 verts[1].fOuterRadius = outerRadius;
580 verts[1].fInnerRadius = innerRadius;
581
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000582 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000583 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
584 verts[2].fOuterRadius = outerRadius;
585 verts[2].fInnerRadius = innerRadius;
586
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000587 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000588 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
589 verts[3].fOuterRadius = outerRadius;
590 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000591
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000592 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000593}
594
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000595///////////////////////////////////////////////////////////////////////////////
596
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000597// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000598extern const GrVertexAttrib gEllipseVertexAttribs[] = {
599 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000600 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
601 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000602};
603
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000604// position + offsets
605extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
606 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000607 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
608 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000609};
610
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000611bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000612 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000613 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000614 const SkStrokeRec& stroke)
615{
616 GrDrawState* drawState = target->drawState();
617#ifdef SK_DEBUG
618 {
619 // we should have checked for this previously
620 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000621 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000622 }
623#endif
624
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000625 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000626 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000627 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000628 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000629 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
630 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000631 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000632 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000633 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000634 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000635
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000636 // do (potentially) anisotropic mapping of stroke
637 SkVector scaledStroke;
638 SkScalar strokeWidth = stroke.getWidth();
639 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
640 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
641
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000642 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000643 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
644 SkStrokeRec::kHairline_Style == style;
645 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000646
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000647 SkScalar innerXRadius = 0;
648 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000649 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000650 if (SkScalarNearlyZero(scaledStroke.length())) {
651 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
652 } else {
653 scaledStroke.scale(SK_ScalarHalf);
654 }
655
656 // we only handle thick strokes for near-circular ellipses
657 if (scaledStroke.length() > SK_ScalarHalf &&
658 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
659 return false;
660 }
661
662 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
663 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
664 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
665 return false;
666 }
667
668 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000669 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000670 innerXRadius = xRadius - scaledStroke.fX;
671 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000672 }
673
674 xRadius += scaledStroke.fX;
675 yRadius += scaledStroke.fY;
676 }
677
bsalomon@google.com137f1342013-05-29 21:27:53 +0000678 GrDrawState::AutoViewMatrixRestore avmr;
679 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000680 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000681 }
682
egdaniel7b3d5ee2014-08-28 05:41:14 -0700683 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
684 sizeof(EllipseVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000685
686 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
687 if (!geo.succeeded()) {
688 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000689 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000690 }
691
692 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
693
bsalomon83d081a2014-07-08 09:56:10 -0700694 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly &&
695 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000696
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000697 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000698 static const int kEllipseEdgeAttrIndex = 2;
699 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000700
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000701 // Compute the reciprocals of the radii here to save time in the shader
702 SkScalar xRadRecip = SkScalarInvert(xRadius);
703 SkScalar yRadRecip = SkScalarInvert(yRadius);
704 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
705 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000706
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000707 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000708 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000709 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000710 xRadius += SK_ScalarHalf;
711 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000712
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000713 SkRect bounds = SkRect::MakeLTRB(
714 center.fX - xRadius,
715 center.fY - yRadius,
716 center.fX + xRadius,
717 center.fY + yRadius
718 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000719
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000720 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000721 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
722 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
723 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000724
725 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000726 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
727 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
728 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000729
730 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000731 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
732 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
733 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000734
735 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000736 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
737 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
738 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000739
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000740 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000741
742 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000743}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000744
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000745bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000746 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000747 const SkRect& ellipse,
748 const SkStrokeRec& stroke)
749{
750 GrDrawState* drawState = target->drawState();
751 const SkMatrix& vm = drawState->getViewMatrix();
752
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000753 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000754 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000755 SkScalar yRadius = SkScalarHalf(ellipse.height());
756
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000757 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000758 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000759 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000760 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000761 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
762
763 SkScalar innerXRadius = 0;
764 SkScalar innerYRadius = 0;
765 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
766 SkScalar strokeWidth = stroke.getWidth();
767
768 if (SkScalarNearlyZero(strokeWidth)) {
769 strokeWidth = SK_ScalarHalf;
770 } else {
771 strokeWidth *= SK_ScalarHalf;
772 }
773
774 // we only handle thick strokes for near-circular ellipses
775 if (strokeWidth > SK_ScalarHalf &&
776 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
777 return false;
778 }
779
780 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
781 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
782 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
783 return false;
784 }
785
786 // set inner radius (if needed)
787 if (SkStrokeRec::kStroke_Style == style) {
788 innerXRadius = xRadius - strokeWidth;
789 innerYRadius = yRadius - strokeWidth;
790 }
791
792 xRadius += strokeWidth;
793 yRadius += strokeWidth;
794 }
795 if (DIEllipseEdgeEffect::kStroke == mode) {
796 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
797 DIEllipseEdgeEffect::kFill;
798 }
799 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
800 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
801
egdaniel7b3d5ee2014-08-28 05:41:14 -0700802 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs),
803 sizeof(DIEllipseVertex));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000804
805 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
806 if (!geo.succeeded()) {
807 GrPrintf("Failed to get space for vertices!\n");
808 return false;
809 }
810
811 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
812
bsalomon83d081a2014-07-08 09:56:10 -0700813 GrEffect* effect = DIEllipseEdgeEffect::Create(mode);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000814
815 static const int kEllipseOuterOffsetAttrIndex = 1;
816 static const int kEllipseInnerOffsetAttrIndex = 2;
817 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
818 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000819
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000820 // This expands the outer rect so that after CTM we end up with a half-pixel border
821 SkScalar a = vm[SkMatrix::kMScaleX];
822 SkScalar b = vm[SkMatrix::kMSkewX];
823 SkScalar c = vm[SkMatrix::kMSkewY];
824 SkScalar d = vm[SkMatrix::kMScaleY];
825 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
826 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
827 // This adjusts the "radius" to include the half-pixel border
828 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
829 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
830
831 SkRect bounds = SkRect::MakeLTRB(
832 center.fX - xRadius - geoDx,
833 center.fY - yRadius - geoDy,
834 center.fX + xRadius + geoDx,
835 center.fY + yRadius + geoDy
836 );
837
838 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
839 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
840 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
841
842 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
843 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
844 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
845
846 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
847 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
848 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
849
850 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
851 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
852 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
853
854 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
855
856 return true;
857}
858
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000859///////////////////////////////////////////////////////////////////////////////
860
861static const uint16_t gRRectIndices[] = {
862 // corners
863 0, 1, 5, 0, 5, 4,
864 2, 3, 7, 2, 7, 6,
865 8, 9, 13, 8, 13, 12,
866 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000867
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000868 // edges
869 1, 2, 6, 1, 6, 5,
870 4, 5, 9, 4, 9, 8,
871 6, 7, 11, 6, 11, 10,
872 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000873
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000874 // center
875 // we place this at the end so that we can ignore these indices when rendering stroke-only
876 5, 6, 10, 5, 10, 9
877};
878
879
880GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
881 if (NULL == fRRectIndexBuffer) {
882 fRRectIndexBuffer =
883 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
884 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000885#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000886 bool updated =
887#endif
888 fRRectIndexBuffer->updateData(gRRectIndices,
889 sizeof(gRRectIndices));
890 GR_DEBUGASSERT(updated);
891 }
892 }
893 return fRRectIndexBuffer;
894}
895
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000896bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA,
bsalomon8af05232014-06-03 06:34:58 -0700897 const SkRRect& origOuter, const SkRRect& origInner) {
898 bool applyAA = useAA &&
899 !target->getDrawState().getRenderTarget()->isMultisampled() &&
900 !target->shouldDisableCoverageAAForBlend();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000901 GrDrawState::AutoRestoreEffects are;
902 if (!origInner.isEmpty()) {
903 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
904 if (!context->getMatrix().isIdentity()) {
905 if (!origInner.transform(context->getMatrix(), inner.writable())) {
906 return false;
907 }
908 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000909 GrEffectEdgeType edgeType = applyAA ? kInverseFillAA_GrEffectEdgeType :
910 kInverseFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700911 GrEffect* effect = GrRRectEffect::Create(edgeType, *inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000912 if (NULL == effect) {
913 return false;
914 }
915 are.set(target->drawState());
916 target->drawState()->addCoverageEffect(effect)->unref();
917 }
918
919 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
bsalomon8af05232014-06-03 06:34:58 -0700920 if (this->drawRRect(target, context, useAA, origOuter, fillRec)) {
921 return true;
922 }
923
924 SkASSERT(!origOuter.isEmpty());
925 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
926 if (!context->getMatrix().isIdentity()) {
927 if (!origOuter.transform(context->getMatrix(), outer.writable())) {
928 return false;
929 }
930 }
931 GrEffectEdgeType edgeType = applyAA ? kFillAA_GrEffectEdgeType :
932 kFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700933 GrEffect* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -0700934 if (NULL == effect) {
935 return false;
936 }
937 if (!are.isSet()) {
938 are.set(target->drawState());
939 }
940 GrDrawState::AutoViewMatrixRestore avmr;
941 if (!avmr.setIdentity(target->drawState())) {
942 return false;
943 }
944 target->drawState()->addCoverageEffect(effect)->unref();
945 SkRect bounds = outer->getBounds();
946 if (applyAA) {
947 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
948 }
bsalomon01c8da12014-08-04 09:21:30 -0700949 target->drawRect(bounds, NULL, NULL);
bsalomon8af05232014-06-03 06:34:58 -0700950 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000951}
952
953bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA,
954 const SkRRect& rrect, const SkStrokeRec& stroke) {
955 if (rrect.isOval()) {
956 return this->drawOval(target, context, useAA, rrect.getBounds(), stroke);
957 }
958
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000959 bool useCoverageAA = useAA &&
960 !target->getDrawState().getRenderTarget()->isMultisampled() &&
961 !target->shouldDisableCoverageAAForBlend();
962
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000963 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000964 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000965 return false;
966 }
967
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000968 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000969
970 if (!vm.rectStaysRect() || !rrect.isSimple()) {
971 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000972 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000973
974 // do any matrix crunching before we reset the draw state for device coords
975 const SkRect& rrectBounds = rrect.getBounds();
976 SkRect bounds;
977 vm.mapRect(&bounds, rrectBounds);
978
979 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000980 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000981 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000982 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000983 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000984
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000985 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000986
987 // do (potentially) anisotropic mapping of stroke
988 SkVector scaledStroke;
989 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000990
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000991 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
992 SkStrokeRec::kHairline_Style == style;
993 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
994
995 if (hasStroke) {
996 if (SkStrokeRec::kHairline_Style == style) {
997 scaledStroke.set(1, 1);
998 } else {
999 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
1000 vm[SkMatrix::kMSkewY]));
1001 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
1002 vm[SkMatrix::kMScaleY]));
1003 }
1004
1005 // if half of strokewidth is greater than radius, we don't handle that right now
1006 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
1007 return false;
1008 }
1009 }
1010
1011 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1012 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1013 // patch will have fractional coverage. This only matters when the interior is actually filled.
1014 // We could consider falling back to rect rendering here, since a tiny radius is
1015 // indistinguishable from a square corner.
1016 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001017 return false;
1018 }
1019
1020 // reset to device coordinates
1021 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001022 GrDrawState::AutoViewMatrixRestore avmr;
1023 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001024 return false;
1025 }
1026
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001027 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
1028 if (NULL == indexBuffer) {
1029 GrPrintf("Failed to create index buffer!\n");
1030 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001031 }
1032
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001033 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001034 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001035 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
1036 sizeof(CircleVertex));
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001037
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001038 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1039 if (!geo.succeeded()) {
1040 GrPrintf("Failed to get space for vertices!\n");
1041 return false;
1042 }
1043 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001044
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001045 SkScalar innerRadius = 0.0f;
1046 SkScalar outerRadius = xRadius;
1047 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001048 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001049 if (SkScalarNearlyZero(scaledStroke.fX)) {
1050 halfWidth = SK_ScalarHalf;
1051 } else {
1052 halfWidth = SkScalarHalf(scaledStroke.fX);
1053 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001054
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001055 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001056 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001057 }
1058 outerRadius += halfWidth;
1059 bounds.outset(halfWidth, halfWidth);
1060 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001061
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001062 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001063
bsalomon83d081a2014-07-08 09:56:10 -07001064 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001065 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001066 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001067
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001068 // The radii are outset for two reasons. First, it allows the shader to simply perform
1069 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1070 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1071 // pixels partially covered by the circle.
1072 outerRadius += SK_ScalarHalf;
1073 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001074
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001075 // Expand the rect so all the pixels will be captured.
1076 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001077
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001078 SkScalar yCoords[4] = {
1079 bounds.fTop,
1080 bounds.fTop + outerRadius,
1081 bounds.fBottom - outerRadius,
1082 bounds.fBottom
1083 };
1084 SkScalar yOuterRadii[4] = {
1085 -outerRadius,
1086 0,
1087 0,
1088 outerRadius
1089 };
1090 for (int i = 0; i < 4; ++i) {
1091 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1092 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1093 verts->fOuterRadius = outerRadius;
1094 verts->fInnerRadius = innerRadius;
1095 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001096
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001097 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1098 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1099 verts->fOuterRadius = outerRadius;
1100 verts->fInnerRadius = innerRadius;
1101 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001102
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001103 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1104 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1105 verts->fOuterRadius = outerRadius;
1106 verts->fInnerRadius = innerRadius;
1107 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001108
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001109 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1110 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1111 verts->fOuterRadius = outerRadius;
1112 verts->fInnerRadius = innerRadius;
1113 verts++;
1114 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001115
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001116 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001117 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1118 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001119 target->setIndexSourceToBuffer(indexBuffer);
1120 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001121
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001122 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001123 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001124 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
1125 sizeof(EllipseVertex));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001126
1127 SkScalar innerXRadius = 0.0f;
1128 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001129 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001130 if (SkScalarNearlyZero(scaledStroke.length())) {
1131 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1132 } else {
1133 scaledStroke.scale(SK_ScalarHalf);
1134 }
1135
1136 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001137 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001138 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1139 return false;
1140 }
1141
1142 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1143 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1144 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1145 return false;
1146 }
1147
1148 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001149 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001150 innerXRadius = xRadius - scaledStroke.fX;
1151 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001152 }
1153
1154 xRadius += scaledStroke.fX;
1155 yRadius += scaledStroke.fY;
1156 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1157 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001158
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001159 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001160
jvanverth@google.come3647412013-05-08 15:31:43 +00001161 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1162 if (!geo.succeeded()) {
1163 GrPrintf("Failed to get space for vertices!\n");
1164 return false;
1165 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001166 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001167
bsalomon83d081a2014-07-08 09:56:10 -07001168 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly);
jvanverth@google.come3647412013-05-08 15:31:43 +00001169 static const int kEllipseOffsetAttrIndex = 1;
1170 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001171 drawState->addCoverageEffect(effect,
1172 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001173
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001174 // Compute the reciprocals of the radii here to save time in the shader
1175 SkScalar xRadRecip = SkScalarInvert(xRadius);
1176 SkScalar yRadRecip = SkScalarInvert(yRadius);
1177 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1178 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001179
1180 // Extend the radii out half a pixel to antialias.
1181 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1182 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001183
1184 // Expand the rect so all the pixels will be captured.
1185 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1186
1187 SkScalar yCoords[4] = {
1188 bounds.fTop,
1189 bounds.fTop + yOuterRadius,
1190 bounds.fBottom - yOuterRadius,
1191 bounds.fBottom
1192 };
1193 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001194 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001195 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1196 SK_ScalarNearlyZero,
1197 yOuterRadius
1198 };
1199
1200 for (int i = 0; i < 4; ++i) {
1201 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001202 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001203 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1204 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001205 verts++;
1206
1207 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1208 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001209 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1210 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001211 verts++;
1212
1213 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1214 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001215 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1216 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001217 verts++;
1218
1219 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1220 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001221 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1222 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001223 verts++;
1224 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001225
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001226 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001227 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1228 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001229 target->setIndexSourceToBuffer(indexBuffer);
1230 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1231 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001232
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001233 return true;
1234}