blob: ae992ec4fb824c0334e5c987b264a0de9caae2e9 [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
joshualitt408d6122014-09-17 07:00:35 -070010#include "gl/builders/GrGLFullProgramBuilder.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000011#include "gl/GrGLEffect.h"
12#include "gl/GrGLSL.h"
joshualitt249af152014-09-15 11:41:13 -070013#include "gl/GrGLGeometryProcessor.h"
joshualitt408d6122014-09-17 07:00:35 -070014#include "GrEffect.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
joshualitt249af152014-09-15 11:41:13 -070025#include "effects/GrGeometryProcessor.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
joshualitt249af152014-09-15 11:41:13 -070063class CircleEdgeEffect : public GrGeometryProcessor {
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
joshualitt249af152014-09-15 11:41:13 -070083 const GrShaderVar& inCircleEdge() const { return fInCircleEdge; }
84
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000085 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
86 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
87 }
88
89 virtual ~CircleEdgeEffect() {}
90
91 static const char* Name() { return "CircleEdge"; }
92
93 inline bool isStroked() const { return fStroke; }
94
joshualitt249af152014-09-15 11:41:13 -070095 class GLEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000096 public:
joshualitt49586be2014-09-16 08:21:41 -070097 GLEffect(const GrBackendEffectFactory& factory, const GrEffect&)
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000098 : INHERITED (factory) {}
99
joshualitt30ba4362014-08-21 20:18:45 -0700100 virtual void emitCode(GrGLFullProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700101 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700102 const GrEffectKey& key,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000103 const char* outputColor,
104 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000105 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000106 const TextureSamplerArray& samplers) SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700107 const CircleEdgeEffect& circleEffect = effect.cast<CircleEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000108 const char *vsName, *fsName;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000109 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000110
joshualitt249af152014-09-15 11:41:13 -0700111 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();;
112 vsBuilder->codeAppendf("\t%s = %s;\n", vsName, circleEffect.inCircleEdge().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
joshualitt49586be2014-09-16 08:21:41 -0700126 static void GenKey(const GrEffect& effect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -0700127 GrEffectKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700128 const CircleEdgeEffect& circleEffect = effect.cast<CircleEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700129 b->add32(circleEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000130 }
131
joshualitt49586be2014-09-16 08:21:41 -0700132 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVERRIDE {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000133
134 private:
joshualitt249af152014-09-15 11:41:13 -0700135 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000136 };
137
138
139private:
joshualitt249af152014-09-15 11:41:13 -0700140 CircleEdgeEffect(bool stroke)
141 : fInCircleEdge(this->addVertexAttrib(
142 GrShaderVar("inCircleEdge",
143 kVec4f_GrSLType,
144 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000145 fStroke = stroke;
146 }
147
148 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700149 const CircleEdgeEffect& cee = other.cast<CircleEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000150 return cee.fStroke == fStroke;
151 }
152
joshualitt249af152014-09-15 11:41:13 -0700153 const GrShaderVar& fInCircleEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000154 bool fStroke;
155
156 GR_DECLARE_EFFECT_TEST;
157
joshualitt249af152014-09-15 11:41:13 -0700158 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000159};
160
161GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
162
bsalomon83d081a2014-07-08 09:56:10 -0700163GrEffect* CircleEdgeEffect::TestCreate(SkRandom* random,
164 GrContext* context,
165 const GrDrawTargetCaps&,
166 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000167 return CircleEdgeEffect::Create(random->nextBool());
168}
169
170///////////////////////////////////////////////////////////////////////////////
171
172/**
173 * 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 +0000174 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
175 * in both x and y directions.
176 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000177 * 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 +0000178 */
179
joshualitt249af152014-09-15 11:41:13 -0700180class EllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000181public:
bsalomon83d081a2014-07-08 09:56:10 -0700182 static GrEffect* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000183 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
184 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185
186 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000187 gEllipseStrokeEdge->ref();
188 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000189 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000190 gEllipseFillEdge->ref();
191 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000192 }
193 }
194
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000195 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000196 uint32_t* validFlags) const SK_OVERRIDE {
197 *validFlags = 0;
198 }
199
200 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
201 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
202 }
203
204 virtual ~EllipseEdgeEffect() {}
205
206 static const char* Name() { return "EllipseEdge"; }
207
joshualitt249af152014-09-15 11:41:13 -0700208 const GrShaderVar& inEllipseOffset() const { return fInEllipseOffset; }
209 const GrShaderVar& inEllipseRadii() const { return fInEllipseRadii; }
210
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000211 inline bool isStroked() const { return fStroke; }
212
joshualitt249af152014-09-15 11:41:13 -0700213 class GLEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214 public:
joshualitt49586be2014-09-16 08:21:41 -0700215 GLEffect(const GrBackendEffectFactory& factory, const GrEffect&)
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000216 : INHERITED (factory) {}
217
joshualitt30ba4362014-08-21 20:18:45 -0700218 virtual void emitCode(GrGLFullProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700219 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700220 const GrEffectKey& key,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221 const char* outputColor,
222 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000223 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000224 const TextureSamplerArray& samplers) SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700225 const EllipseEdgeEffect& ellipseEffect = effect.cast<EllipseEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000226
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000227 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000228 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000229
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000230 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
joshualitt30ba4362014-08-21 20:18:45 -0700231
232 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
joshualitt249af152014-09-15 11:41:13 -0700233 vsBuilder->codeAppendf("%s = %s;", vsOffsetName,
234 ellipseEffect.inEllipseOffset().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000235
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000236 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
joshualitt249af152014-09-15 11:41:13 -0700237 vsBuilder->codeAppendf("%s = %s;", vsRadiiName, ellipseEffect.inEllipseRadii().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000238
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000239 // for outer curve
joshualitt30ba4362014-08-21 20:18:45 -0700240 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
241 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
242 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
243 fsBuilder->codeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
244 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000245 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700246 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
247 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
248 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000249
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000250 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000251 if (ellipseEffect.isStroked()) {
joshualitt30ba4362014-08-21 20:18:45 -0700252 fsBuilder->codeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
253 fsBuilder->codeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
254 fsBuilder->codeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
255 fsBuilder->codeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
256 fsBuilder->codeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000257 }
258
joshualitt30ba4362014-08-21 20:18:45 -0700259 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000260 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000261 }
262
joshualitt49586be2014-09-16 08:21:41 -0700263 static void GenKey(const GrEffect& effect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -0700264 GrEffectKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700265 const EllipseEdgeEffect& ellipseEffect = effect.cast<EllipseEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700266 b->add32(ellipseEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000267 }
268
joshualitt49586be2014-09-16 08:21:41 -0700269 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVERRIDE {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000270 }
271
272 private:
joshualitt249af152014-09-15 11:41:13 -0700273 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000274 };
275
276private:
joshualitt249af152014-09-15 11:41:13 -0700277 EllipseEdgeEffect(bool stroke)
278 : fInEllipseOffset(this->addVertexAttrib(
279 GrShaderVar("inEllipseOffset",
280 kVec2f_GrSLType,
281 GrShaderVar::kAttribute_TypeModifier)))
282 , fInEllipseRadii(this->addVertexAttrib(
283 GrShaderVar("inEllipseRadii",
284 kVec4f_GrSLType,
285 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000286 fStroke = stroke;
287 }
288
289 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700290 const EllipseEdgeEffect& eee = other.cast<EllipseEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000291 return eee.fStroke == fStroke;
292 }
293
joshualitt249af152014-09-15 11:41:13 -0700294 const GrShaderVar& fInEllipseOffset;
295 const GrShaderVar& fInEllipseRadii;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000296 bool fStroke;
297
298 GR_DECLARE_EFFECT_TEST;
299
joshualitt249af152014-09-15 11:41:13 -0700300 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000301};
302
303GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
304
bsalomon83d081a2014-07-08 09:56:10 -0700305GrEffect* EllipseEdgeEffect::TestCreate(SkRandom* random,
306 GrContext* context,
307 const GrDrawTargetCaps&,
308 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000309 return EllipseEdgeEffect::Create(random->nextBool());
310}
311
312///////////////////////////////////////////////////////////////////////////////
313
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000314/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000315 * 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 +0000316 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
317 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
318 * using differentials.
319 *
320 * The result is device-independent and can be used with any affine matrix.
321 */
322
joshualitt249af152014-09-15 11:41:13 -0700323class DIEllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000324public:
325 enum Mode { kStroke = 0, kHairline, kFill };
326
bsalomon83d081a2014-07-08 09:56:10 -0700327 static GrEffect* Create(Mode mode) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000328 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
329 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
330 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000331
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000332 if (kStroke == mode) {
333 gEllipseStrokeEdge->ref();
334 return gEllipseStrokeEdge;
335 } else if (kHairline == mode) {
336 gEllipseHairlineEdge->ref();
337 return gEllipseHairlineEdge;
338 } else {
339 gEllipseFillEdge->ref();
340 return gEllipseFillEdge;
341 }
342 }
343
344 virtual void getConstantColorComponents(GrColor* color,
345 uint32_t* validFlags) const SK_OVERRIDE {
346 *validFlags = 0;
347 }
348
349 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
350 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
351 }
352
353 virtual ~DIEllipseEdgeEffect() {}
354
355 static const char* Name() { return "DIEllipseEdge"; }
356
joshualitt249af152014-09-15 11:41:13 -0700357 const GrShaderVar& inEllipseOffsets0() const { return fInEllipseOffsets0; }
358 const GrShaderVar& inEllipseOffsets1() const { return fInEllipseOffsets1; }
359
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000360 inline Mode getMode() const { return fMode; }
361
joshualitt249af152014-09-15 11:41:13 -0700362 class GLEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000363 public:
joshualitt49586be2014-09-16 08:21:41 -0700364 GLEffect(const GrBackendEffectFactory& factory, const GrEffect&)
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000365 : INHERITED (factory) {}
366
joshualitt30ba4362014-08-21 20:18:45 -0700367 virtual void emitCode(GrGLFullProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700368 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700369 const GrEffectKey& key,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000370 const char* outputColor,
371 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000372 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000373 const TextureSamplerArray& samplers) SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700374 const DIEllipseEdgeEffect& ellipseEffect = effect.cast<DIEllipseEdgeEffect>();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000375
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000376 const char *vsOffsetName0, *fsOffsetName0;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000377 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000378 &vsOffsetName0, &fsOffsetName0);
joshualitt30ba4362014-08-21 20:18:45 -0700379
380 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
joshualitt249af152014-09-15 11:41:13 -0700381 vsBuilder->codeAppendf("%s = %s;", vsOffsetName0,
382 ellipseEffect.inEllipseOffsets0().c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000383 const char *vsOffsetName1, *fsOffsetName1;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000384 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000385 &vsOffsetName1, &fsOffsetName1);
joshualitt249af152014-09-15 11:41:13 -0700386 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName1,
387 ellipseEffect.inEllipseOffsets1().c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000388
joshualitt30ba4362014-08-21 20:18:45 -0700389 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
390 SkAssertResult(fsBuilder->enableFeature(
391 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000392 // for outer curve
joshualitt30ba4362014-08-21 20:18:45 -0700393 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
394 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
395 fsBuilder->codeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
396 fsBuilder->codeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
397 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 +0000398 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000399 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000400
joshualitt30ba4362014-08-21 20:18:45 -0700401 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000402 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700403 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
404 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000405 if (kHairline == ellipseEffect.getMode()) {
406 // can probably do this with one step
joshualitt30ba4362014-08-21 20:18:45 -0700407 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
408 fsBuilder->codeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000409 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700410 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000411 }
412
413 // for inner curve
414 if (kStroke == ellipseEffect.getMode()) {
joshualitt30ba4362014-08-21 20:18:45 -0700415 fsBuilder->codeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
416 fsBuilder->codeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
417 fsBuilder->codeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
418 fsBuilder->codeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
419 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 +0000420 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
421 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
joshualitt30ba4362014-08-21 20:18:45 -0700422 fsBuilder->codeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
423 fsBuilder->codeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000424 }
425
joshualitt30ba4362014-08-21 20:18:45 -0700426 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000427 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000428 }
429
joshualitt49586be2014-09-16 08:21:41 -0700430 static void GenKey(const GrEffect& effect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -0700431 GrEffectKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700432 const DIEllipseEdgeEffect& ellipseEffect = effect.cast<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000433
bsalomon63e99f72014-07-21 08:03:14 -0700434 b->add32(ellipseEffect.getMode());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000435 }
436
joshualitt49586be2014-09-16 08:21:41 -0700437 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000438 }
439
440 private:
joshualitt249af152014-09-15 11:41:13 -0700441 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000442 };
443
444private:
joshualitt249af152014-09-15 11:41:13 -0700445 DIEllipseEdgeEffect(Mode mode)
446 : fInEllipseOffsets0(this->addVertexAttrib(
447 GrShaderVar("inEllipseOffsets0",
448 kVec2f_GrSLType,
449 GrShaderVar::kAttribute_TypeModifier)))
450 , fInEllipseOffsets1(this->addVertexAttrib(
451 GrShaderVar("inEllipseOffsets1",
452 kVec2f_GrSLType,
453 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000454 fMode = mode;
455 }
456
457 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700458 const DIEllipseEdgeEffect& eee = other.cast<DIEllipseEdgeEffect>();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000459 return eee.fMode == fMode;
460 }
461
joshualitt249af152014-09-15 11:41:13 -0700462 const GrShaderVar& fInEllipseOffsets0;
463 const GrShaderVar& fInEllipseOffsets1;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000464 Mode fMode;
465
466 GR_DECLARE_EFFECT_TEST;
467
joshualitt249af152014-09-15 11:41:13 -0700468 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000469};
470
471GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
472
bsalomon83d081a2014-07-08 09:56:10 -0700473GrEffect* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
474 GrContext* context,
475 const GrDrawTargetCaps&,
476 GrTexture* textures[]) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000477 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
478}
479
480///////////////////////////////////////////////////////////////////////////////
481
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000482void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000483 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000484}
485
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000486bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000487 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000488{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000489 bool useCoverageAA = useAA &&
490 !target->getDrawState().getRenderTarget()->isMultisampled() &&
491 !target->shouldDisableCoverageAAForBlend();
492
493 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000494 return false;
495 }
496
497 const SkMatrix& vm = context->getMatrix();
498
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000499 // we can draw circles
500 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000501 && circle_stays_circle(vm)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000502 this->drawCircle(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000503 // if we have shader derivative support, render as device-independent
504 } else if (target->caps()->shaderDerivativeSupport()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000505 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000506 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000507 } else if (vm.rectStaysRect()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000508 return this->drawEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000509 } else {
510 return false;
511 }
512
513 return true;
514}
515
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000516///////////////////////////////////////////////////////////////////////////////
517
robertphillips@google.com42903302013-04-20 12:26:07 +0000518// position + edge
519extern const GrVertexAttrib gCircleVertexAttribs[] = {
520 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000521 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000522};
523
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000524void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000525 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000526 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000527 const SkStrokeRec& stroke)
528{
529 GrDrawState* drawState = target->drawState();
530
531 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000532 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000533 vm.mapPoints(&center, 1);
534 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
535 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
536
bsalomon@google.com137f1342013-05-29 21:27:53 +0000537 GrDrawState::AutoViewMatrixRestore avmr;
538 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000539 return;
540 }
541
egdaniel7b3d5ee2014-08-28 05:41:14 -0700542 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
543 sizeof(CircleVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000544
545 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
546 if (!geo.succeeded()) {
547 GrPrintf("Failed to get space for vertices!\n");
548 return;
549 }
550
551 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
552
553 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000554 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
555 SkStrokeRec::kHairline_Style == style;
556 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000557
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000558 SkScalar innerRadius = 0.0f;
559 SkScalar outerRadius = radius;
560 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000561 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000562 if (SkScalarNearlyZero(strokeWidth)) {
563 halfWidth = SK_ScalarHalf;
564 } else {
565 halfWidth = SkScalarHalf(strokeWidth);
566 }
567
568 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000569 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000570 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000571 }
572 }
573
bsalomon83d081a2014-07-08 09:56:10 -0700574 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
joshualitt249af152014-09-15 11:41:13 -0700575 drawState->setGeometryProcessor(effect)->unref();
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000576
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000577 // The radii are outset for two reasons. First, it allows the shader to simply perform
578 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
579 // verts of the bounding box that is rendered and the outset ensures the box will cover all
580 // pixels partially covered by the circle.
581 outerRadius += SK_ScalarHalf;
582 innerRadius -= SK_ScalarHalf;
583
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000584 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000585 center.fX - outerRadius,
586 center.fY - outerRadius,
587 center.fX + outerRadius,
588 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000589 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000590
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000591 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000592 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
593 verts[0].fOuterRadius = outerRadius;
594 verts[0].fInnerRadius = innerRadius;
595
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000596 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000597 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000598 verts[1].fOuterRadius = outerRadius;
599 verts[1].fInnerRadius = innerRadius;
600
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000601 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000602 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
603 verts[2].fOuterRadius = outerRadius;
604 verts[2].fInnerRadius = innerRadius;
605
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000606 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000607 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
608 verts[3].fOuterRadius = outerRadius;
609 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000610
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000611 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000612}
613
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000614///////////////////////////////////////////////////////////////////////////////
615
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000616// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000617extern const GrVertexAttrib gEllipseVertexAttribs[] = {
618 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000619 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
620 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000621};
622
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000623// position + offsets
624extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
625 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000626 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
627 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000628};
629
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000630bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000631 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000632 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000633 const SkStrokeRec& stroke)
634{
635 GrDrawState* drawState = target->drawState();
636#ifdef SK_DEBUG
637 {
638 // we should have checked for this previously
639 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000640 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000641 }
642#endif
643
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000644 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000645 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000646 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000647 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000648 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
649 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000650 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000651 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000652 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000653 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000654
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000655 // do (potentially) anisotropic mapping of stroke
656 SkVector scaledStroke;
657 SkScalar strokeWidth = stroke.getWidth();
658 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
659 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
660
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000661 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000662 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
663 SkStrokeRec::kHairline_Style == style;
664 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000665
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000666 SkScalar innerXRadius = 0;
667 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000668 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000669 if (SkScalarNearlyZero(scaledStroke.length())) {
670 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
671 } else {
672 scaledStroke.scale(SK_ScalarHalf);
673 }
674
675 // we only handle thick strokes for near-circular ellipses
676 if (scaledStroke.length() > SK_ScalarHalf &&
677 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
678 return false;
679 }
680
681 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
682 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
683 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
684 return false;
685 }
686
687 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000688 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000689 innerXRadius = xRadius - scaledStroke.fX;
690 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000691 }
692
693 xRadius += scaledStroke.fX;
694 yRadius += scaledStroke.fY;
695 }
696
bsalomon@google.com137f1342013-05-29 21:27:53 +0000697 GrDrawState::AutoViewMatrixRestore avmr;
698 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000699 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000700 }
701
egdaniel7b3d5ee2014-08-28 05:41:14 -0700702 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
703 sizeof(EllipseVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000704
705 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
706 if (!geo.succeeded()) {
707 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000708 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000709 }
710
711 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
712
bsalomon83d081a2014-07-08 09:56:10 -0700713 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly &&
714 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000715
joshualitt249af152014-09-15 11:41:13 -0700716 drawState->setGeometryProcessor(effect)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000717
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000718 // Compute the reciprocals of the radii here to save time in the shader
719 SkScalar xRadRecip = SkScalarInvert(xRadius);
720 SkScalar yRadRecip = SkScalarInvert(yRadius);
721 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
722 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000723
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000724 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000725 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000726 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000727 xRadius += SK_ScalarHalf;
728 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000729
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000730 SkRect bounds = SkRect::MakeLTRB(
731 center.fX - xRadius,
732 center.fY - yRadius,
733 center.fX + xRadius,
734 center.fY + yRadius
735 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000736
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000737 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000738 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
739 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
740 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000741
742 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000743 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
744 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
745 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000746
747 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000748 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
749 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
750 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000751
752 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000753 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
754 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
755 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000756
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000757 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000758
759 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000760}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000761
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000762bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000763 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000764 const SkRect& ellipse,
765 const SkStrokeRec& stroke)
766{
767 GrDrawState* drawState = target->drawState();
768 const SkMatrix& vm = drawState->getViewMatrix();
769
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000770 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000771 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000772 SkScalar yRadius = SkScalarHalf(ellipse.height());
773
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000774 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000775 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000776 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000777 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000778 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
779
780 SkScalar innerXRadius = 0;
781 SkScalar innerYRadius = 0;
782 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
783 SkScalar strokeWidth = stroke.getWidth();
784
785 if (SkScalarNearlyZero(strokeWidth)) {
786 strokeWidth = SK_ScalarHalf;
787 } else {
788 strokeWidth *= SK_ScalarHalf;
789 }
790
791 // we only handle thick strokes for near-circular ellipses
792 if (strokeWidth > SK_ScalarHalf &&
793 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
794 return false;
795 }
796
797 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
798 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
799 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
800 return false;
801 }
802
803 // set inner radius (if needed)
804 if (SkStrokeRec::kStroke_Style == style) {
805 innerXRadius = xRadius - strokeWidth;
806 innerYRadius = yRadius - strokeWidth;
807 }
808
809 xRadius += strokeWidth;
810 yRadius += strokeWidth;
811 }
812 if (DIEllipseEdgeEffect::kStroke == mode) {
813 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
814 DIEllipseEdgeEffect::kFill;
815 }
816 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
817 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
818
egdaniel7b3d5ee2014-08-28 05:41:14 -0700819 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs),
820 sizeof(DIEllipseVertex));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000821
822 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
823 if (!geo.succeeded()) {
824 GrPrintf("Failed to get space for vertices!\n");
825 return false;
826 }
827
828 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
829
bsalomon83d081a2014-07-08 09:56:10 -0700830 GrEffect* effect = DIEllipseEdgeEffect::Create(mode);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000831
joshualitt249af152014-09-15 11:41:13 -0700832 drawState->setGeometryProcessor(effect)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000833
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000834 // This expands the outer rect so that after CTM we end up with a half-pixel border
835 SkScalar a = vm[SkMatrix::kMScaleX];
836 SkScalar b = vm[SkMatrix::kMSkewX];
837 SkScalar c = vm[SkMatrix::kMSkewY];
838 SkScalar d = vm[SkMatrix::kMScaleY];
839 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
840 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
841 // This adjusts the "radius" to include the half-pixel border
842 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
843 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
844
845 SkRect bounds = SkRect::MakeLTRB(
846 center.fX - xRadius - geoDx,
847 center.fY - yRadius - geoDy,
848 center.fX + xRadius + geoDx,
849 center.fY + yRadius + geoDy
850 );
851
852 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
853 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
854 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
855
856 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
857 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
858 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
859
860 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
861 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
862 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
863
864 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
865 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
866 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
867
868 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
869
870 return true;
871}
872
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000873///////////////////////////////////////////////////////////////////////////////
874
875static const uint16_t gRRectIndices[] = {
876 // corners
877 0, 1, 5, 0, 5, 4,
878 2, 3, 7, 2, 7, 6,
879 8, 9, 13, 8, 13, 12,
880 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000881
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000882 // edges
883 1, 2, 6, 1, 6, 5,
884 4, 5, 9, 4, 9, 8,
885 6, 7, 11, 6, 11, 10,
886 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000887
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000888 // center
889 // we place this at the end so that we can ignore these indices when rendering stroke-only
890 5, 6, 10, 5, 10, 9
891};
892
893
894GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
895 if (NULL == fRRectIndexBuffer) {
896 fRRectIndexBuffer =
897 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
bsalomon49f085d2014-09-05 13:34:00 -0700898 if (fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000899#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000900 bool updated =
901#endif
902 fRRectIndexBuffer->updateData(gRRectIndices,
903 sizeof(gRRectIndices));
904 GR_DEBUGASSERT(updated);
905 }
906 }
907 return fRRectIndexBuffer;
908}
909
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000910bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA,
bsalomon8af05232014-06-03 06:34:58 -0700911 const SkRRect& origOuter, const SkRRect& origInner) {
912 bool applyAA = useAA &&
913 !target->getDrawState().getRenderTarget()->isMultisampled() &&
914 !target->shouldDisableCoverageAAForBlend();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000915 GrDrawState::AutoRestoreEffects are;
916 if (!origInner.isEmpty()) {
917 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
918 if (!context->getMatrix().isIdentity()) {
919 if (!origInner.transform(context->getMatrix(), inner.writable())) {
920 return false;
921 }
922 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000923 GrEffectEdgeType edgeType = applyAA ? kInverseFillAA_GrEffectEdgeType :
924 kInverseFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700925 GrEffect* effect = GrRRectEffect::Create(edgeType, *inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000926 if (NULL == effect) {
927 return false;
928 }
929 are.set(target->drawState());
930 target->drawState()->addCoverageEffect(effect)->unref();
931 }
932
933 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
bsalomon8af05232014-06-03 06:34:58 -0700934 if (this->drawRRect(target, context, useAA, origOuter, fillRec)) {
935 return true;
936 }
937
938 SkASSERT(!origOuter.isEmpty());
939 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
940 if (!context->getMatrix().isIdentity()) {
941 if (!origOuter.transform(context->getMatrix(), outer.writable())) {
942 return false;
943 }
944 }
945 GrEffectEdgeType edgeType = applyAA ? kFillAA_GrEffectEdgeType :
946 kFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700947 GrEffect* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -0700948 if (NULL == effect) {
949 return false;
950 }
951 if (!are.isSet()) {
952 are.set(target->drawState());
953 }
954 GrDrawState::AutoViewMatrixRestore avmr;
955 if (!avmr.setIdentity(target->drawState())) {
956 return false;
957 }
958 target->drawState()->addCoverageEffect(effect)->unref();
959 SkRect bounds = outer->getBounds();
960 if (applyAA) {
961 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
962 }
bsalomon01c8da12014-08-04 09:21:30 -0700963 target->drawRect(bounds, NULL, NULL);
bsalomon8af05232014-06-03 06:34:58 -0700964 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000965}
966
967bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA,
968 const SkRRect& rrect, const SkStrokeRec& stroke) {
969 if (rrect.isOval()) {
970 return this->drawOval(target, context, useAA, rrect.getBounds(), stroke);
971 }
972
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000973 bool useCoverageAA = useAA &&
974 !target->getDrawState().getRenderTarget()->isMultisampled() &&
975 !target->shouldDisableCoverageAAForBlend();
976
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000977 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000978 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000979 return false;
980 }
981
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000982 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000983
984 if (!vm.rectStaysRect() || !rrect.isSimple()) {
985 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000986 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000987
988 // do any matrix crunching before we reset the draw state for device coords
989 const SkRect& rrectBounds = rrect.getBounds();
990 SkRect bounds;
991 vm.mapRect(&bounds, rrectBounds);
992
993 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000994 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000995 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000996 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000997 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000998
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000999 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001000
1001 // do (potentially) anisotropic mapping of stroke
1002 SkVector scaledStroke;
1003 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001004
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001005 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1006 SkStrokeRec::kHairline_Style == style;
1007 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1008
1009 if (hasStroke) {
1010 if (SkStrokeRec::kHairline_Style == style) {
1011 scaledStroke.set(1, 1);
1012 } else {
1013 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
1014 vm[SkMatrix::kMSkewY]));
1015 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
1016 vm[SkMatrix::kMScaleY]));
1017 }
1018
1019 // if half of strokewidth is greater than radius, we don't handle that right now
1020 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
1021 return false;
1022 }
1023 }
1024
1025 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1026 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1027 // patch will have fractional coverage. This only matters when the interior is actually filled.
1028 // We could consider falling back to rect rendering here, since a tiny radius is
1029 // indistinguishable from a square corner.
1030 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001031 return false;
1032 }
1033
1034 // reset to device coordinates
1035 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001036 GrDrawState::AutoViewMatrixRestore avmr;
1037 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001038 return false;
1039 }
1040
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001041 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
1042 if (NULL == indexBuffer) {
1043 GrPrintf("Failed to create index buffer!\n");
1044 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001045 }
1046
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001047 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001048 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001049 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
1050 sizeof(CircleVertex));
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001051
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001052 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1053 if (!geo.succeeded()) {
1054 GrPrintf("Failed to get space for vertices!\n");
1055 return false;
1056 }
1057 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001058
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001059 SkScalar innerRadius = 0.0f;
1060 SkScalar outerRadius = xRadius;
1061 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001062 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001063 if (SkScalarNearlyZero(scaledStroke.fX)) {
1064 halfWidth = SK_ScalarHalf;
1065 } else {
1066 halfWidth = SkScalarHalf(scaledStroke.fX);
1067 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001068
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001069 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001070 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001071 }
1072 outerRadius += halfWidth;
1073 bounds.outset(halfWidth, halfWidth);
1074 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001075
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001076 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001077
bsalomon83d081a2014-07-08 09:56:10 -07001078 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001079 drawState->setGeometryProcessor(effect)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001080
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001081 // The radii are outset for two reasons. First, it allows the shader to simply perform
1082 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1083 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1084 // pixels partially covered by the circle.
1085 outerRadius += SK_ScalarHalf;
1086 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001087
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001088 // Expand the rect so all the pixels will be captured.
1089 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001090
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001091 SkScalar yCoords[4] = {
1092 bounds.fTop,
1093 bounds.fTop + outerRadius,
1094 bounds.fBottom - outerRadius,
1095 bounds.fBottom
1096 };
1097 SkScalar yOuterRadii[4] = {
1098 -outerRadius,
1099 0,
1100 0,
1101 outerRadius
1102 };
1103 for (int i = 0; i < 4; ++i) {
1104 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1105 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1106 verts->fOuterRadius = outerRadius;
1107 verts->fInnerRadius = innerRadius;
1108 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001109
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001110 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1111 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1112 verts->fOuterRadius = outerRadius;
1113 verts->fInnerRadius = innerRadius;
1114 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001115
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001116 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1117 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1118 verts->fOuterRadius = outerRadius;
1119 verts->fInnerRadius = innerRadius;
1120 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001121
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001122 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1123 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1124 verts->fOuterRadius = outerRadius;
1125 verts->fInnerRadius = innerRadius;
1126 verts++;
1127 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001128
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001129 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001130 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1131 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001132 target->setIndexSourceToBuffer(indexBuffer);
1133 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001134
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001135 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001136 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001137 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
1138 sizeof(EllipseVertex));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001139
1140 SkScalar innerXRadius = 0.0f;
1141 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001142 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001143 if (SkScalarNearlyZero(scaledStroke.length())) {
1144 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1145 } else {
1146 scaledStroke.scale(SK_ScalarHalf);
1147 }
1148
1149 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001150 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001151 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1152 return false;
1153 }
1154
1155 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1156 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1157 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1158 return false;
1159 }
1160
1161 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001162 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001163 innerXRadius = xRadius - scaledStroke.fX;
1164 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001165 }
1166
1167 xRadius += scaledStroke.fX;
1168 yRadius += scaledStroke.fY;
1169 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1170 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001171
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001172 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001173
jvanverth@google.come3647412013-05-08 15:31:43 +00001174 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1175 if (!geo.succeeded()) {
1176 GrPrintf("Failed to get space for vertices!\n");
1177 return false;
1178 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001179 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001180
bsalomon83d081a2014-07-08 09:56:10 -07001181 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001182 drawState->setGeometryProcessor(effect)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001183
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001184 // Compute the reciprocals of the radii here to save time in the shader
1185 SkScalar xRadRecip = SkScalarInvert(xRadius);
1186 SkScalar yRadRecip = SkScalarInvert(yRadius);
1187 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1188 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001189
1190 // Extend the radii out half a pixel to antialias.
1191 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1192 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001193
1194 // Expand the rect so all the pixels will be captured.
1195 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1196
1197 SkScalar yCoords[4] = {
1198 bounds.fTop,
1199 bounds.fTop + yOuterRadius,
1200 bounds.fBottom - yOuterRadius,
1201 bounds.fBottom
1202 };
1203 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001204 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001205 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1206 SK_ScalarNearlyZero,
1207 yOuterRadius
1208 };
1209
1210 for (int i = 0; i < 4; ++i) {
1211 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001212 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001213 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1214 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001215 verts++;
1216
1217 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1218 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001219 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1220 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001221 verts++;
1222
1223 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1224 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001225 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1226 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001227 verts++;
1228
1229 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1230 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001231 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1232 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001233 verts++;
1234 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001235
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001236 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001237 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1238 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001239 target->setIndexSourceToBuffer(indexBuffer);
1240 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1241 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001242
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001243 return true;
1244}