commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrOvalRenderer.h" |
| 9 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 10 | #include "GrEffect.h" |
| 11 | #include "gl/GrGLEffect.h" |
| 12 | #include "gl/GrGLSL.h" |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 13 | #include "gl/GrGLVertexEffect.h" |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 14 | #include "GrTBackendEffectFactory.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 15 | |
| 16 | #include "GrDrawState.h" |
| 17 | #include "GrDrawTarget.h" |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 18 | #include "GrGpu.h" |
| 19 | |
| 20 | #include "SkRRect.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 21 | #include "SkStrokeRec.h" |
| 22 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 23 | #include "effects/GrVertexEffect.h" |
| 24 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | struct CircleVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 28 | SkPoint fPos; |
| 29 | SkPoint fOffset; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 30 | SkScalar fOuterRadius; |
| 31 | SkScalar fInnerRadius; |
| 32 | }; |
| 33 | |
| 34 | struct EllipseVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 35 | SkPoint fPos; |
| 36 | SkPoint fOffset; |
| 37 | SkPoint fOuterRadii; |
| 38 | SkPoint fInnerRadii; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 41 | struct DIEllipseVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 42 | SkPoint fPos; |
| 43 | SkPoint fOuterOffset; |
| 44 | SkPoint fInnerOffset; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 47 | inline bool circle_stays_circle(const SkMatrix& m) { |
| 48 | return m.isSimilarity(); |
| 49 | } |
| 50 | |
| 51 | } |
| 52 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 53 | /////////////////////////////////////////////////////////////////////////////// |
| 54 | |
| 55 | /** |
| 56 | * The output of this effect is a modulation of the input color and coverage for a circle, |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 57 | * specified as offset_x, offset_y (both from center point), outer radius and inner radius. |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 58 | */ |
| 59 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 60 | class CircleEdgeEffect : public GrVertexEffect { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 61 | public: |
| 62 | static GrEffectRef* Create(bool stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 63 | GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true)); |
| 64 | GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 65 | |
| 66 | if (stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 67 | gCircleStrokeEdge->ref(); |
| 68 | return gCircleStrokeEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 69 | } else { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 70 | gCircleFillEdge->ref(); |
| 71 | return gCircleFillEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 75 | virtual void getConstantColorComponents(GrColor* color, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 76 | uint32_t* validFlags) const SK_OVERRIDE { |
| 77 | *validFlags = 0; |
| 78 | } |
| 79 | |
| 80 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 81 | return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance(); |
| 82 | } |
| 83 | |
| 84 | virtual ~CircleEdgeEffect() {} |
| 85 | |
| 86 | static const char* Name() { return "CircleEdge"; } |
| 87 | |
| 88 | inline bool isStroked() const { return fStroke; } |
| 89 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 90 | class GLEffect : public GrGLVertexEffect { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 91 | public: |
| 92 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 93 | : INHERITED (factory) {} |
| 94 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 95 | virtual void emitCode(GrGLFullShaderBuilder* builder, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 96 | const GrDrawEffect& drawEffect, |
| 97 | EffectKey key, |
| 98 | const char* outputColor, |
| 99 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 100 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 101 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 102 | const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>(); |
| 103 | const char *vsName, *fsName; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 104 | builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 105 | |
| 106 | const SkString* attrName = |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 107 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 108 | builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 109 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 110 | builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 111 | builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName); |
| 112 | if (circleEffect.isStroked()) { |
| 113 | builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName); |
| 114 | builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); |
| 115 | } |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 116 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 117 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 118 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 122 | const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>(); |
| 123 | |
| 124 | return circleEffect.isStroked() ? 0x1 : 0x0; |
| 125 | } |
| 126 | |
| 127 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 128 | |
| 129 | private: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 130 | typedef GrGLVertexEffect INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | |
| 134 | private: |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 135 | CircleEdgeEffect(bool stroke) : GrVertexEffect() { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 136 | this->addVertexAttrib(kVec4f_GrSLType); |
| 137 | fStroke = stroke; |
| 138 | } |
| 139 | |
| 140 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 141 | const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other); |
| 142 | return cee.fStroke == fStroke; |
| 143 | } |
| 144 | |
| 145 | bool fStroke; |
| 146 | |
| 147 | GR_DECLARE_EFFECT_TEST; |
| 148 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 149 | typedef GrVertexEffect INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | GR_DEFINE_EFFECT_TEST(CircleEdgeEffect); |
| 153 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 154 | GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 155 | GrContext* context, |
| 156 | const GrDrawTargetCaps&, |
| 157 | GrTexture* textures[]) { |
| 158 | return CircleEdgeEffect::Create(random->nextBool()); |
| 159 | } |
| 160 | |
| 161 | /////////////////////////////////////////////////////////////////////////////// |
| 162 | |
| 163 | /** |
| 164 | * The output of this effect is a modulation of the input color and coverage for an axis-aligned |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 165 | * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii, |
| 166 | * in both x and y directions. |
| 167 | * |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 168 | * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0. |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 169 | */ |
| 170 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 171 | class EllipseEdgeEffect : public GrVertexEffect { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 172 | public: |
| 173 | static GrEffectRef* Create(bool stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 174 | GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true)); |
| 175 | GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 176 | |
| 177 | if (stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 178 | gEllipseStrokeEdge->ref(); |
| 179 | return gEllipseStrokeEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 180 | } else { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 181 | gEllipseFillEdge->ref(); |
| 182 | return gEllipseFillEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 186 | virtual void getConstantColorComponents(GrColor* color, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 187 | uint32_t* validFlags) const SK_OVERRIDE { |
| 188 | *validFlags = 0; |
| 189 | } |
| 190 | |
| 191 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 192 | return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance(); |
| 193 | } |
| 194 | |
| 195 | virtual ~EllipseEdgeEffect() {} |
| 196 | |
| 197 | static const char* Name() { return "EllipseEdge"; } |
| 198 | |
| 199 | inline bool isStroked() const { return fStroke; } |
| 200 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 201 | class GLEffect : public GrGLVertexEffect { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 202 | public: |
| 203 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 204 | : INHERITED (factory) {} |
| 205 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 206 | virtual void emitCode(GrGLFullShaderBuilder* builder, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 207 | const GrDrawEffect& drawEffect, |
| 208 | EffectKey key, |
| 209 | const char* outputColor, |
| 210 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 211 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 212 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 213 | const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>(); |
| 214 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 215 | const char *vsOffsetName, *fsOffsetName; |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 216 | const char *vsRadiiName, *fsRadiiName; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 217 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 218 | builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 219 | const SkString* attr0Name = |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 220 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 221 | builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 222 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 223 | builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 224 | const SkString* attr1Name = |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 225 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); |
| 226 | builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 227 | |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 228 | // for outer curve |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 229 | builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName); |
| 230 | builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n"); |
| 231 | builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName); |
jvanverth@google.com | d1b5b14 | 2013-07-02 14:46:03 +0000 | [diff] [blame] | 232 | builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame^] | 233 | // avoid calling inversesqrt on zero. |
| 234 | builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
jvanverth@google.com | d1b5b14 | 2013-07-02 14:46:03 +0000 | [diff] [blame] | 235 | builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n"); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 236 | builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 237 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 238 | // for inner curve |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 239 | if (ellipseEffect.isStroked()) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 240 | builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName); |
| 241 | builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n"); |
| 242 | builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName); |
| 243 | builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n"); |
| 244 | builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 245 | } |
| 246 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 247 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 248 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 252 | const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>(); |
| 253 | |
| 254 | return ellipseEffect.isStroked() ? 0x1 : 0x0; |
| 255 | } |
| 256 | |
| 257 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE { |
| 258 | } |
| 259 | |
| 260 | private: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 261 | typedef GrGLVertexEffect INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | private: |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 265 | EllipseEdgeEffect(bool stroke) : GrVertexEffect() { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 266 | this->addVertexAttrib(kVec2f_GrSLType); |
| 267 | this->addVertexAttrib(kVec4f_GrSLType); |
| 268 | fStroke = stroke; |
| 269 | } |
| 270 | |
| 271 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 272 | const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other); |
| 273 | return eee.fStroke == fStroke; |
| 274 | } |
| 275 | |
| 276 | bool fStroke; |
| 277 | |
| 278 | GR_DECLARE_EFFECT_TEST; |
| 279 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 280 | typedef GrVertexEffect INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect); |
| 284 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 285 | GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 286 | GrContext* context, |
| 287 | const GrDrawTargetCaps&, |
| 288 | GrTexture* textures[]) { |
| 289 | return EllipseEdgeEffect::Create(random->nextBool()); |
| 290 | } |
| 291 | |
| 292 | /////////////////////////////////////////////////////////////////////////////// |
| 293 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 294 | /** |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 295 | * The output of this effect is a modulation of the input color and coverage for an ellipse, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 296 | * specified as a 2D offset from center for both the outer and inner paths (if stroked). The |
| 297 | * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by |
| 298 | * using differentials. |
| 299 | * |
| 300 | * The result is device-independent and can be used with any affine matrix. |
| 301 | */ |
| 302 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 303 | class DIEllipseEdgeEffect : public GrVertexEffect { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 304 | public: |
| 305 | enum Mode { kStroke = 0, kHairline, kFill }; |
| 306 | |
| 307 | static GrEffectRef* Create(Mode mode) { |
| 308 | GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke)); |
| 309 | GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline)); |
| 310 | GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill)); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 311 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 312 | if (kStroke == mode) { |
| 313 | gEllipseStrokeEdge->ref(); |
| 314 | return gEllipseStrokeEdge; |
| 315 | } else if (kHairline == mode) { |
| 316 | gEllipseHairlineEdge->ref(); |
| 317 | return gEllipseHairlineEdge; |
| 318 | } else { |
| 319 | gEllipseFillEdge->ref(); |
| 320 | return gEllipseFillEdge; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | virtual void getConstantColorComponents(GrColor* color, |
| 325 | uint32_t* validFlags) const SK_OVERRIDE { |
| 326 | *validFlags = 0; |
| 327 | } |
| 328 | |
| 329 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 330 | return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance(); |
| 331 | } |
| 332 | |
| 333 | virtual ~DIEllipseEdgeEffect() {} |
| 334 | |
| 335 | static const char* Name() { return "DIEllipseEdge"; } |
| 336 | |
| 337 | inline Mode getMode() const { return fMode; } |
| 338 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 339 | class GLEffect : public GrGLVertexEffect { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 340 | public: |
| 341 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 342 | : INHERITED (factory) {} |
| 343 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 344 | virtual void emitCode(GrGLFullShaderBuilder* builder, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 345 | const GrDrawEffect& drawEffect, |
| 346 | EffectKey key, |
| 347 | const char* outputColor, |
| 348 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 349 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 350 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 351 | const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>(); |
| 352 | |
| 353 | SkAssertResult(builder->enableFeature( |
| 354 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 355 | |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 356 | const char *vsOffsetName0, *fsOffsetName0; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 357 | builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0", |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 358 | &vsOffsetName0, &fsOffsetName0); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 359 | const SkString* attr0Name = |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 360 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 361 | builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str()); |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 362 | const char *vsOffsetName1, *fsOffsetName1; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 363 | builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1", |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 364 | &vsOffsetName1, &fsOffsetName1); |
| 365 | const SkString* attr1Name = |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 366 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); |
| 367 | builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 368 | |
| 369 | // for outer curve |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 370 | builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 371 | builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n"); |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 372 | builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0); |
| 373 | builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 374 | builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n" |
| 375 | "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n", |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 376 | fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 377 | |
| 378 | builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame^] | 379 | // avoid calling inversesqrt on zero. |
| 380 | builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 381 | builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n"); |
| 382 | if (kHairline == ellipseEffect.getMode()) { |
| 383 | // can probably do this with one step |
| 384 | builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n"); |
| 385 | builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n"); |
| 386 | } else { |
| 387 | builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n"); |
| 388 | } |
| 389 | |
| 390 | // for inner curve |
| 391 | if (kStroke == ellipseEffect.getMode()) { |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 392 | builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 393 | builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n"); |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 394 | builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1); |
| 395 | builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1); |
| 396 | builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n" |
| 397 | "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n", |
| 398 | fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 399 | builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n"); |
| 400 | builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n"); |
| 401 | } |
| 402 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 403 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 404 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 408 | const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 409 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 410 | return ellipseEffect.getMode(); |
| 411 | } |
| 412 | |
| 413 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE { |
| 414 | } |
| 415 | |
| 416 | private: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 417 | typedef GrGLVertexEffect INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 418 | }; |
| 419 | |
| 420 | private: |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 421 | DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() { |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 422 | this->addVertexAttrib(kVec2f_GrSLType); |
| 423 | this->addVertexAttrib(kVec2f_GrSLType); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 424 | fMode = mode; |
| 425 | } |
| 426 | |
| 427 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 428 | const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other); |
| 429 | return eee.fMode == fMode; |
| 430 | } |
| 431 | |
| 432 | Mode fMode; |
| 433 | |
| 434 | GR_DECLARE_EFFECT_TEST; |
| 435 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 436 | typedef GrVertexEffect INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect); |
| 440 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 441 | GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 442 | GrContext* context, |
| 443 | const GrDrawTargetCaps&, |
| 444 | GrTexture* textures[]) { |
| 445 | return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2))); |
| 446 | } |
| 447 | |
| 448 | /////////////////////////////////////////////////////////////////////////////// |
| 449 | |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 450 | void GrOvalRenderer::reset() { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 451 | SkSafeSetNull(fRRectIndexBuffer); |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 452 | } |
| 453 | |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 454 | bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 455 | const SkRect& oval, const SkStrokeRec& stroke) |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 456 | { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 457 | bool useCoverageAA = useAA && |
| 458 | !target->getDrawState().getRenderTarget()->isMultisampled() && |
| 459 | !target->shouldDisableCoverageAAForBlend(); |
| 460 | |
| 461 | if (!useCoverageAA) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 462 | return false; |
| 463 | } |
| 464 | |
| 465 | const SkMatrix& vm = context->getMatrix(); |
| 466 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 467 | // we can draw circles |
| 468 | if (SkScalarNearlyEqual(oval.width(), oval.height()) |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 469 | && circle_stays_circle(vm)) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 470 | this->drawCircle(target, useCoverageAA, oval, stroke); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 471 | // if we have shader derivative support, render as device-independent |
| 472 | } else if (target->caps()->shaderDerivativeSupport()) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 473 | return this->drawDIEllipse(target, useCoverageAA, oval, stroke); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 474 | // otherwise axis-aligned ellipses only |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 475 | } else if (vm.rectStaysRect()) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 476 | return this->drawEllipse(target, useCoverageAA, oval, stroke); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 477 | } else { |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | return true; |
| 482 | } |
| 483 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 484 | /////////////////////////////////////////////////////////////////////////////// |
| 485 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 486 | // position + edge |
| 487 | extern const GrVertexAttrib gCircleVertexAttribs[] = { |
| 488 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 489 | {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding} |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 490 | }; |
| 491 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 492 | void GrOvalRenderer::drawCircle(GrDrawTarget* target, |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 493 | bool useCoverageAA, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 494 | const SkRect& circle, |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 495 | const SkStrokeRec& stroke) |
| 496 | { |
| 497 | GrDrawState* drawState = target->drawState(); |
| 498 | |
| 499 | const SkMatrix& vm = drawState->getViewMatrix(); |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 500 | SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 501 | vm.mapPoints(¢er, 1); |
| 502 | SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width())); |
| 503 | SkScalar strokeWidth = vm.mapRadius(stroke.getWidth()); |
| 504 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 505 | GrDrawState::AutoViewMatrixRestore avmr; |
| 506 | if (!avmr.setIdentity(drawState)) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 510 | drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 511 | SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 512 | |
| 513 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 514 | if (!geo.succeeded()) { |
| 515 | GrPrintf("Failed to get space for vertices!\n"); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); |
| 520 | |
| 521 | SkStrokeRec::Style style = stroke.getStyle(); |
| 522 | bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 523 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 524 | SkScalar innerRadius = 0.0f; |
| 525 | SkScalar outerRadius = radius; |
| 526 | SkScalar halfWidth = 0; |
| 527 | if (style != SkStrokeRec::kFill_Style) { |
| 528 | if (SkScalarNearlyZero(strokeWidth)) { |
| 529 | halfWidth = SK_ScalarHalf; |
| 530 | } else { |
| 531 | halfWidth = SkScalarHalf(strokeWidth); |
| 532 | } |
| 533 | |
| 534 | outerRadius += halfWidth; |
| 535 | if (isStroked) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 536 | innerRadius = radius - halfWidth; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 540 | GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0); |
| 541 | static const int kCircleEdgeAttrIndex = 1; |
| 542 | drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref(); |
| 543 | |
bsalomon@google.com | 58e30fe | 2013-04-01 19:01:20 +0000 | [diff] [blame] | 544 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
| 545 | // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the |
| 546 | // verts of the bounding box that is rendered and the outset ensures the box will cover all |
| 547 | // pixels partially covered by the circle. |
| 548 | outerRadius += SK_ScalarHalf; |
| 549 | innerRadius -= SK_ScalarHalf; |
| 550 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 551 | SkRect bounds = SkRect::MakeLTRB( |
bsalomon@google.com | 58e30fe | 2013-04-01 19:01:20 +0000 | [diff] [blame] | 552 | center.fX - outerRadius, |
| 553 | center.fY - outerRadius, |
| 554 | center.fX + outerRadius, |
| 555 | center.fY + outerRadius |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 556 | ); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 557 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 558 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 559 | verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius); |
| 560 | verts[0].fOuterRadius = outerRadius; |
| 561 | verts[0].fInnerRadius = innerRadius; |
| 562 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 563 | verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
skia.committer@gmail.com | 4674676 | 2013-04-12 07:01:51 +0000 | [diff] [blame] | 564 | verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 565 | verts[1].fOuterRadius = outerRadius; |
| 566 | verts[1].fInnerRadius = innerRadius; |
| 567 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 568 | verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 569 | verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius); |
| 570 | verts[2].fOuterRadius = outerRadius; |
| 571 | verts[2].fInnerRadius = innerRadius; |
| 572 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 573 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 574 | verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius); |
| 575 | verts[3].fOuterRadius = outerRadius; |
| 576 | verts[3].fInnerRadius = innerRadius; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 577 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 578 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 579 | } |
| 580 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 581 | /////////////////////////////////////////////////////////////////////////////// |
| 582 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 583 | // position + offset + 1/radii |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 584 | extern const GrVertexAttrib gEllipseVertexAttribs[] = { |
| 585 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 586 | {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}, |
| 587 | {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding} |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 588 | }; |
| 589 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 590 | // position + offsets |
| 591 | extern const GrVertexAttrib gDIEllipseVertexAttribs[] = { |
| 592 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 593 | {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}, |
| 594 | {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding}, |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 597 | bool GrOvalRenderer::drawEllipse(GrDrawTarget* target, |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 598 | bool useCoverageAA, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 599 | const SkRect& ellipse, |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 600 | const SkStrokeRec& stroke) |
| 601 | { |
| 602 | GrDrawState* drawState = target->drawState(); |
| 603 | #ifdef SK_DEBUG |
| 604 | { |
| 605 | // we should have checked for this previously |
| 606 | bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 607 | SkASSERT(useCoverageAA && isAxisAlignedEllipse); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 608 | } |
| 609 | #endif |
| 610 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 611 | // do any matrix crunching before we reset the draw state for device coords |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 612 | const SkMatrix& vm = drawState->getViewMatrix(); |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 613 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 614 | vm.mapPoints(¢er, 1); |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 615 | SkScalar ellipseXRadius = SkScalarHalf(ellipse.width()); |
| 616 | SkScalar ellipseYRadius = SkScalarHalf(ellipse.height()); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 617 | SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius + |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 618 | vm[SkMatrix::kMSkewY]*ellipseYRadius); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 619 | SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius + |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 620 | vm[SkMatrix::kMScaleY]*ellipseYRadius); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 621 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 622 | // do (potentially) anisotropic mapping of stroke |
| 623 | SkVector scaledStroke; |
| 624 | SkScalar strokeWidth = stroke.getWidth(); |
| 625 | scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY])); |
| 626 | scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY])); |
| 627 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 628 | SkStrokeRec::Style style = stroke.getStyle(); |
| 629 | bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); |
| 630 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 631 | SkScalar innerXRadius = 0; |
| 632 | SkScalar innerYRadius = 0; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 633 | if (SkStrokeRec::kFill_Style != style) { |
| 634 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 635 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 636 | } else { |
| 637 | scaledStroke.scale(SK_ScalarHalf); |
| 638 | } |
| 639 | |
| 640 | // we only handle thick strokes for near-circular ellipses |
| 641 | if (scaledStroke.length() > SK_ScalarHalf && |
| 642 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 647 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 648 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | // this is legit only if scale & translation (which should be the case at the moment) |
| 653 | if (isStroked) { |
| 654 | innerXRadius = xRadius - scaledStroke.fX; |
| 655 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | xRadius += scaledStroke.fX; |
| 659 | yRadius += scaledStroke.fY; |
| 660 | } |
| 661 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 662 | GrDrawState::AutoViewMatrixRestore avmr; |
| 663 | if (!avmr.setIdentity(drawState)) { |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 664 | return false; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 665 | } |
| 666 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 667 | drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 668 | SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 669 | |
| 670 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 671 | if (!geo.succeeded()) { |
| 672 | GrPrintf("Failed to get space for vertices!\n"); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 673 | return false; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); |
| 677 | |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 678 | GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked && |
| 679 | innerXRadius > 0 && innerYRadius > 0); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 680 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 681 | static const int kEllipseCenterAttrIndex = 1; |
jvanverth@google.com | 6cc8d44 | 2013-09-10 18:24:37 +0000 | [diff] [blame] | 682 | static const int kEllipseEdgeAttrIndex = 2; |
| 683 | drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref(); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 684 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 685 | // Compute the reciprocals of the radii here to save time in the shader |
| 686 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 687 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
| 688 | SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); |
| 689 | SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 690 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 691 | // We've extended the outer x radius out half a pixel to antialias. |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 692 | // This will also expand the rect so all the pixels will be captured. |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 693 | // TODO: Consider if we should use sqrt(2)/2 instead |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 694 | xRadius += SK_ScalarHalf; |
| 695 | yRadius += SK_ScalarHalf; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 696 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 697 | SkRect bounds = SkRect::MakeLTRB( |
| 698 | center.fX - xRadius, |
| 699 | center.fY - yRadius, |
| 700 | center.fX + xRadius, |
| 701 | center.fY + yRadius |
| 702 | ); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 703 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 704 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 705 | verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius); |
| 706 | verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 707 | verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 708 | |
| 709 | verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 710 | verts[1].fOffset = SkPoint::Make(xRadius, -yRadius); |
| 711 | verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 712 | verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 713 | |
| 714 | verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 715 | verts[2].fOffset = SkPoint::Make(-xRadius, yRadius); |
| 716 | verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 717 | verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 718 | |
| 719 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 720 | verts[3].fOffset = SkPoint::Make(xRadius, yRadius); |
| 721 | verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 722 | verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
skia.committer@gmail.com | 4674676 | 2013-04-12 07:01:51 +0000 | [diff] [blame] | 723 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 724 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 725 | |
| 726 | return true; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 727 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 728 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 729 | bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target, |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 730 | bool useCoverageAA, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 731 | const SkRect& ellipse, |
| 732 | const SkStrokeRec& stroke) |
| 733 | { |
| 734 | GrDrawState* drawState = target->drawState(); |
| 735 | const SkMatrix& vm = drawState->getViewMatrix(); |
| 736 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 737 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 738 | SkScalar xRadius = SkScalarHalf(ellipse.width()); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 739 | SkScalar yRadius = SkScalarHalf(ellipse.height()); |
| 740 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 741 | SkStrokeRec::Style style = stroke.getStyle(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 742 | DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 743 | DIEllipseEdgeEffect::kStroke : |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 744 | (SkStrokeRec::kHairline_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 745 | DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill; |
| 746 | |
| 747 | SkScalar innerXRadius = 0; |
| 748 | SkScalar innerYRadius = 0; |
| 749 | if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) { |
| 750 | SkScalar strokeWidth = stroke.getWidth(); |
| 751 | |
| 752 | if (SkScalarNearlyZero(strokeWidth)) { |
| 753 | strokeWidth = SK_ScalarHalf; |
| 754 | } else { |
| 755 | strokeWidth *= SK_ScalarHalf; |
| 756 | } |
| 757 | |
| 758 | // we only handle thick strokes for near-circular ellipses |
| 759 | if (strokeWidth > SK_ScalarHalf && |
| 760 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 765 | if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius || |
| 766 | strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) { |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | // set inner radius (if needed) |
| 771 | if (SkStrokeRec::kStroke_Style == style) { |
| 772 | innerXRadius = xRadius - strokeWidth; |
| 773 | innerYRadius = yRadius - strokeWidth; |
| 774 | } |
| 775 | |
| 776 | xRadius += strokeWidth; |
| 777 | yRadius += strokeWidth; |
| 778 | } |
| 779 | if (DIEllipseEdgeEffect::kStroke == mode) { |
| 780 | mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke : |
| 781 | DIEllipseEdgeEffect::kFill; |
| 782 | } |
| 783 | SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius); |
| 784 | SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius); |
| 785 | |
| 786 | drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs)); |
| 787 | SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize()); |
| 788 | |
| 789 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 790 | if (!geo.succeeded()) { |
| 791 | GrPrintf("Failed to get space for vertices!\n"); |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices()); |
| 796 | |
| 797 | GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode); |
| 798 | |
| 799 | static const int kEllipseOuterOffsetAttrIndex = 1; |
| 800 | static const int kEllipseInnerOffsetAttrIndex = 2; |
| 801 | drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex, |
| 802 | kEllipseInnerOffsetAttrIndex)->unref(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 803 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 804 | // This expands the outer rect so that after CTM we end up with a half-pixel border |
| 805 | SkScalar a = vm[SkMatrix::kMScaleX]; |
| 806 | SkScalar b = vm[SkMatrix::kMSkewX]; |
| 807 | SkScalar c = vm[SkMatrix::kMSkewY]; |
| 808 | SkScalar d = vm[SkMatrix::kMScaleY]; |
| 809 | SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c)); |
| 810 | SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d)); |
| 811 | // This adjusts the "radius" to include the half-pixel border |
| 812 | SkScalar offsetDx = SkScalarDiv(geoDx, xRadius); |
| 813 | SkScalar offsetDy = SkScalarDiv(geoDy, yRadius); |
| 814 | |
| 815 | SkRect bounds = SkRect::MakeLTRB( |
| 816 | center.fX - xRadius - geoDx, |
| 817 | center.fY - yRadius - geoDy, |
| 818 | center.fX + xRadius + geoDx, |
| 819 | center.fY + yRadius + geoDy |
| 820 | ); |
| 821 | |
| 822 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 823 | verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy); |
| 824 | verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy); |
| 825 | |
| 826 | verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 827 | verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy); |
| 828 | verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy); |
| 829 | |
| 830 | verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 831 | verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy); |
| 832 | verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy); |
| 833 | |
| 834 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 835 | verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy); |
| 836 | verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy); |
| 837 | |
| 838 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); |
| 839 | |
| 840 | return true; |
| 841 | } |
| 842 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 843 | /////////////////////////////////////////////////////////////////////////////// |
| 844 | |
| 845 | static const uint16_t gRRectIndices[] = { |
| 846 | // corners |
| 847 | 0, 1, 5, 0, 5, 4, |
| 848 | 2, 3, 7, 2, 7, 6, |
| 849 | 8, 9, 13, 8, 13, 12, |
| 850 | 10, 11, 15, 10, 15, 14, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 851 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 852 | // edges |
| 853 | 1, 2, 6, 1, 6, 5, |
| 854 | 4, 5, 9, 4, 9, 8, |
| 855 | 6, 7, 11, 6, 11, 10, |
| 856 | 9, 10, 14, 9, 14, 13, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 857 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 858 | // center |
| 859 | // we place this at the end so that we can ignore these indices when rendering stroke-only |
| 860 | 5, 6, 10, 5, 10, 9 |
| 861 | }; |
| 862 | |
| 863 | |
| 864 | GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) { |
| 865 | if (NULL == fRRectIndexBuffer) { |
| 866 | fRRectIndexBuffer = |
| 867 | gpu->createIndexBuffer(sizeof(gRRectIndices), false); |
| 868 | if (NULL != fRRectIndexBuffer) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 869 | #ifdef SK_DEBUG |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 870 | bool updated = |
| 871 | #endif |
| 872 | fRRectIndexBuffer->updateData(gRRectIndices, |
| 873 | sizeof(gRRectIndices)); |
| 874 | GR_DEBUGASSERT(updated); |
| 875 | } |
| 876 | } |
| 877 | return fRRectIndexBuffer; |
| 878 | } |
| 879 | |
skia.committer@gmail.com | 2fd42c4 | 2013-05-03 07:01:00 +0000 | [diff] [blame] | 880 | bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA, |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 881 | const SkRRect& rrect, const SkStrokeRec& stroke) |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 882 | { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 883 | bool useCoverageAA = useAA && |
| 884 | !target->getDrawState().getRenderTarget()->isMultisampled() && |
| 885 | !target->shouldDisableCoverageAAForBlend(); |
| 886 | |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 887 | // only anti-aliased rrects for now |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 888 | if (!useCoverageAA) { |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 889 | return false; |
| 890 | } |
| 891 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 892 | const SkMatrix& vm = context->getMatrix(); |
| 893 | #ifdef SK_DEBUG |
| 894 | { |
| 895 | // we should have checked for this previously |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 896 | SkASSERT(useCoverageAA && vm.rectStaysRect() && rrect.isSimple()); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 897 | } |
| 898 | #endif |
| 899 | |
| 900 | // do any matrix crunching before we reset the draw state for device coords |
| 901 | const SkRect& rrectBounds = rrect.getBounds(); |
| 902 | SkRect bounds; |
| 903 | vm.mapRect(&bounds, rrectBounds); |
| 904 | |
| 905 | SkVector radii = rrect.getSimpleRadii(); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 906 | SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX + |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 907 | vm[SkMatrix::kMSkewY]*radii.fY); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 908 | SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX + |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 909 | vm[SkMatrix::kMScaleY]*radii.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 910 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 911 | // if hairline stroke is greater than radius, we don't handle that right now |
| 912 | SkStrokeRec::Style style = stroke.getStyle(); |
| 913 | if (SkStrokeRec::kHairline_Style == style && |
commit-bot@chromium.org | eacc489 | 2013-11-07 16:34:53 +0000 | [diff] [blame] | 914 | (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 915 | return false; |
| 916 | } |
| 917 | |
| 918 | // do (potentially) anisotropic mapping of stroke |
| 919 | SkVector scaledStroke; |
| 920 | SkScalar strokeWidth = stroke.getWidth(); |
| 921 | scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY])); |
| 922 | scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY])); |
| 923 | |
| 924 | // if half of strokewidth is greater than radius, we don't handle that right now |
commit-bot@chromium.org | eacc489 | 2013-11-07 16:34:53 +0000 | [diff] [blame] | 925 | if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 926 | return false; |
| 927 | } |
| 928 | |
| 929 | // reset to device coordinates |
| 930 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 931 | GrDrawState::AutoViewMatrixRestore avmr; |
| 932 | if (!avmr.setIdentity(drawState)) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 933 | return false; |
| 934 | } |
| 935 | |
| 936 | bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); |
| 937 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 938 | GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu()); |
| 939 | if (NULL == indexBuffer) { |
| 940 | GrPrintf("Failed to create index buffer!\n"); |
| 941 | return false; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 942 | } |
| 943 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 944 | // if the corners are circles, use the circle renderer |
| 945 | if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { |
| 946 | drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 947 | SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 948 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 949 | GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); |
| 950 | if (!geo.succeeded()) { |
| 951 | GrPrintf("Failed to get space for vertices!\n"); |
| 952 | return false; |
| 953 | } |
| 954 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 955 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 956 | SkScalar innerRadius = 0.0f; |
| 957 | SkScalar outerRadius = xRadius; |
| 958 | SkScalar halfWidth = 0; |
| 959 | if (style != SkStrokeRec::kFill_Style) { |
| 960 | if (SkScalarNearlyZero(scaledStroke.fX)) { |
| 961 | halfWidth = SK_ScalarHalf; |
| 962 | } else { |
| 963 | halfWidth = SkScalarHalf(scaledStroke.fX); |
| 964 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 965 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 966 | if (isStroked) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 967 | innerRadius = xRadius - halfWidth; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 968 | } |
| 969 | outerRadius += halfWidth; |
| 970 | bounds.outset(halfWidth, halfWidth); |
| 971 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 972 | |
commit-bot@chromium.org | eacc489 | 2013-11-07 16:34:53 +0000 | [diff] [blame] | 973 | isStroked = (isStroked && innerRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 974 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 975 | GrEffectRef* effect = CircleEdgeEffect::Create(isStroked); |
| 976 | static const int kCircleEdgeAttrIndex = 1; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 977 | drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref(); |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 978 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 979 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
| 980 | // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the |
| 981 | // verts of the bounding box that is rendered and the outset ensures the box will cover all |
| 982 | // pixels partially covered by the circle. |
| 983 | outerRadius += SK_ScalarHalf; |
| 984 | innerRadius -= SK_ScalarHalf; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 985 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 986 | // Expand the rect so all the pixels will be captured. |
| 987 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 988 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 989 | SkScalar yCoords[4] = { |
| 990 | bounds.fTop, |
| 991 | bounds.fTop + outerRadius, |
| 992 | bounds.fBottom - outerRadius, |
| 993 | bounds.fBottom |
| 994 | }; |
| 995 | SkScalar yOuterRadii[4] = { |
| 996 | -outerRadius, |
| 997 | 0, |
| 998 | 0, |
| 999 | outerRadius |
| 1000 | }; |
| 1001 | for (int i = 0; i < 4; ++i) { |
| 1002 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 1003 | verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]); |
| 1004 | verts->fOuterRadius = outerRadius; |
| 1005 | verts->fInnerRadius = innerRadius; |
| 1006 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1007 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1008 | verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]); |
| 1009 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1010 | verts->fOuterRadius = outerRadius; |
| 1011 | verts->fInnerRadius = innerRadius; |
| 1012 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1013 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1014 | verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]); |
| 1015 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1016 | verts->fOuterRadius = outerRadius; |
| 1017 | verts->fInnerRadius = innerRadius; |
| 1018 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1019 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1020 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1021 | verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]); |
| 1022 | verts->fOuterRadius = outerRadius; |
| 1023 | verts->fInnerRadius = innerRadius; |
| 1024 | verts++; |
| 1025 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1026 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1027 | // drop out the middle quad if we're stroked |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1028 | int indexCnt = isStroked ? SK_ARRAY_COUNT(gRRectIndices)-6 : SK_ARRAY_COUNT(gRRectIndices); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1029 | target->setIndexSourceToBuffer(indexBuffer); |
| 1030 | target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1031 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1032 | // otherwise we use the ellipse renderer |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1033 | } else { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1034 | drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1035 | SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1036 | |
| 1037 | SkScalar innerXRadius = 0.0f; |
| 1038 | SkScalar innerYRadius = 0.0f; |
| 1039 | if (SkStrokeRec::kFill_Style != style) { |
| 1040 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 1041 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 1042 | } else { |
| 1043 | scaledStroke.scale(SK_ScalarHalf); |
| 1044 | } |
| 1045 | |
| 1046 | // we only handle thick strokes for near-circular ellipses |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 1047 | if (scaledStroke.length() > SK_ScalarHalf && |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1048 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
| 1052 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1053 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 1054 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
| 1055 | return false; |
| 1056 | } |
| 1057 | |
| 1058 | // this is legit only if scale & translation (which should be the case at the moment) |
| 1059 | if (isStroked) { |
| 1060 | innerXRadius = xRadius - scaledStroke.fX; |
| 1061 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | xRadius += scaledStroke.fX; |
| 1065 | yRadius += scaledStroke.fY; |
| 1066 | bounds.outset(scaledStroke.fX, scaledStroke.fY); |
| 1067 | } |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1068 | |
commit-bot@chromium.org | eacc489 | 2013-11-07 16:34:53 +0000 | [diff] [blame] | 1069 | isStroked = (isStroked && innerXRadius >= 0 && innerYRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 1070 | |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1071 | GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); |
| 1072 | if (!geo.succeeded()) { |
| 1073 | GrPrintf("Failed to get space for vertices!\n"); |
| 1074 | return false; |
| 1075 | } |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1076 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1077 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1078 | GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked); |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1079 | static const int kEllipseOffsetAttrIndex = 1; |
| 1080 | static const int kEllipseRadiiAttrIndex = 2; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1081 | drawState->addCoverageEffect(effect, |
| 1082 | kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1083 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1084 | // Compute the reciprocals of the radii here to save time in the shader |
| 1085 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 1086 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
| 1087 | SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); |
| 1088 | SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1089 | |
| 1090 | // Extend the radii out half a pixel to antialias. |
| 1091 | SkScalar xOuterRadius = xRadius + SK_ScalarHalf; |
| 1092 | SkScalar yOuterRadius = yRadius + SK_ScalarHalf; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1093 | |
| 1094 | // Expand the rect so all the pixels will be captured. |
| 1095 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 1096 | |
| 1097 | SkScalar yCoords[4] = { |
| 1098 | bounds.fTop, |
| 1099 | bounds.fTop + yOuterRadius, |
| 1100 | bounds.fBottom - yOuterRadius, |
| 1101 | bounds.fBottom |
| 1102 | }; |
| 1103 | SkScalar yOuterOffsets[4] = { |
jvanverth@google.com | d1b5b14 | 2013-07-02 14:46:03 +0000 | [diff] [blame] | 1104 | yOuterRadius, |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1105 | SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0 |
| 1106 | SK_ScalarNearlyZero, |
| 1107 | yOuterRadius |
| 1108 | }; |
| 1109 | |
| 1110 | for (int i = 0; i < 4; ++i) { |
| 1111 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
jvanverth@google.com | d1b5b14 | 2013-07-02 14:46:03 +0000 | [diff] [blame] | 1112 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1113 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1114 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1115 | verts++; |
| 1116 | |
| 1117 | verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]); |
| 1118 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1119 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1120 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1121 | verts++; |
| 1122 | |
| 1123 | verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]); |
| 1124 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1125 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1126 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1127 | verts++; |
| 1128 | |
| 1129 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1130 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1131 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1132 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1133 | verts++; |
| 1134 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1135 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1136 | // drop out the middle quad if we're stroked |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1137 | int indexCnt = isStroked ? SK_ARRAY_COUNT(gRRectIndices)-6 : SK_ARRAY_COUNT(gRRectIndices); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1138 | target->setIndexSourceToBuffer(indexBuffer); |
| 1139 | target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); |
| 1140 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1141 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1142 | return true; |
| 1143 | } |