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