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" |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 22 | #include "SkTLazy.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 23 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 24 | #include "effects/GrVertexEffect.h" |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 25 | #include "effects/GrRRectEffect.h" |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 26 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | struct CircleVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 30 | SkPoint fPos; |
| 31 | SkPoint fOffset; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 32 | SkScalar fOuterRadius; |
| 33 | SkScalar fInnerRadius; |
| 34 | }; |
| 35 | |
| 36 | struct EllipseVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 37 | SkPoint fPos; |
| 38 | SkPoint fOffset; |
| 39 | SkPoint fOuterRadii; |
| 40 | SkPoint fInnerRadii; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 43 | struct DIEllipseVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 44 | SkPoint fPos; |
| 45 | SkPoint fOuterOffset; |
| 46 | SkPoint fInnerOffset; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 49 | inline bool circle_stays_circle(const SkMatrix& m) { |
| 50 | return m.isSimilarity(); |
| 51 | } |
| 52 | |
| 53 | } |
| 54 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | /** |
| 58 | * 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] | 59 | * 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] | 60 | */ |
| 61 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 62 | class CircleEdgeEffect : public GrVertexEffect { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 63 | public: |
| 64 | static GrEffectRef* Create(bool stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 65 | GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true)); |
| 66 | GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 67 | |
| 68 | if (stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 69 | gCircleStrokeEdge->ref(); |
| 70 | return gCircleStrokeEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 71 | } else { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 72 | gCircleFillEdge->ref(); |
| 73 | return gCircleFillEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 77 | virtual void getConstantColorComponents(GrColor* color, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 78 | uint32_t* validFlags) const SK_OVERRIDE { |
| 79 | *validFlags = 0; |
| 80 | } |
| 81 | |
| 82 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 83 | return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance(); |
| 84 | } |
| 85 | |
| 86 | virtual ~CircleEdgeEffect() {} |
| 87 | |
| 88 | static const char* Name() { return "CircleEdge"; } |
| 89 | |
| 90 | inline bool isStroked() const { return fStroke; } |
| 91 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 92 | class GLEffect : public GrGLVertexEffect { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 93 | public: |
| 94 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 95 | : INHERITED (factory) {} |
| 96 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 97 | virtual void emitCode(GrGLFullShaderBuilder* builder, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 98 | const GrDrawEffect& drawEffect, |
| 99 | EffectKey key, |
| 100 | const char* outputColor, |
| 101 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 102 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 103 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 104 | const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>(); |
| 105 | const char *vsName, *fsName; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 106 | builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 107 | |
| 108 | const SkString* attrName = |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 109 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 110 | builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 111 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 112 | builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 113 | builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName); |
| 114 | if (circleEffect.isStroked()) { |
| 115 | builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName); |
| 116 | builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); |
| 117 | } |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 118 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 119 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 120 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 124 | const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>(); |
| 125 | |
| 126 | return circleEffect.isStroked() ? 0x1 : 0x0; |
| 127 | } |
| 128 | |
| 129 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 130 | |
| 131 | private: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 132 | typedef GrGLVertexEffect INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | |
| 136 | private: |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 137 | CircleEdgeEffect(bool stroke) : GrVertexEffect() { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 138 | this->addVertexAttrib(kVec4f_GrSLType); |
| 139 | fStroke = stroke; |
| 140 | } |
| 141 | |
| 142 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 143 | const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other); |
| 144 | return cee.fStroke == fStroke; |
| 145 | } |
| 146 | |
| 147 | bool fStroke; |
| 148 | |
| 149 | GR_DECLARE_EFFECT_TEST; |
| 150 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 151 | typedef GrVertexEffect INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | GR_DEFINE_EFFECT_TEST(CircleEdgeEffect); |
| 155 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 156 | GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 157 | GrContext* context, |
| 158 | const GrDrawTargetCaps&, |
| 159 | GrTexture* textures[]) { |
| 160 | return CircleEdgeEffect::Create(random->nextBool()); |
| 161 | } |
| 162 | |
| 163 | /////////////////////////////////////////////////////////////////////////////// |
| 164 | |
| 165 | /** |
| 166 | * The output of this effect is a modulation of the input color and coverage for an axis-aligned |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 167 | * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii, |
| 168 | * in both x and y directions. |
| 169 | * |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 170 | * 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] | 171 | */ |
| 172 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 173 | class EllipseEdgeEffect : public GrVertexEffect { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 174 | public: |
| 175 | static GrEffectRef* Create(bool stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 176 | GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true)); |
| 177 | GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 178 | |
| 179 | if (stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 180 | gEllipseStrokeEdge->ref(); |
| 181 | return gEllipseStrokeEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 182 | } else { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 183 | gEllipseFillEdge->ref(); |
| 184 | return gEllipseFillEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 188 | virtual void getConstantColorComponents(GrColor* color, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 189 | uint32_t* validFlags) const SK_OVERRIDE { |
| 190 | *validFlags = 0; |
| 191 | } |
| 192 | |
| 193 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 194 | return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance(); |
| 195 | } |
| 196 | |
| 197 | virtual ~EllipseEdgeEffect() {} |
| 198 | |
| 199 | static const char* Name() { return "EllipseEdge"; } |
| 200 | |
| 201 | inline bool isStroked() const { return fStroke; } |
| 202 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 203 | class GLEffect : public GrGLVertexEffect { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 204 | public: |
| 205 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 206 | : INHERITED (factory) {} |
| 207 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 208 | virtual void emitCode(GrGLFullShaderBuilder* builder, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 209 | const GrDrawEffect& drawEffect, |
| 210 | EffectKey key, |
| 211 | const char* outputColor, |
| 212 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 213 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 214 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 215 | const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>(); |
| 216 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 217 | const char *vsOffsetName, *fsOffsetName; |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 218 | const char *vsRadiiName, *fsRadiiName; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 219 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 220 | builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 221 | const SkString* attr0Name = |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 222 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 223 | builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 224 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 225 | builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 226 | const SkString* attr1Name = |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 227 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); |
| 228 | builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 229 | |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 230 | // for outer curve |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 231 | builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName); |
| 232 | builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n"); |
| 233 | builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName); |
jvanverth@google.com | d1b5b14 | 2013-07-02 14:46:03 +0000 | [diff] [blame] | 234 | builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 235 | // avoid calling inversesqrt on zero. |
| 236 | builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
jvanverth@google.com | d1b5b14 | 2013-07-02 14:46:03 +0000 | [diff] [blame] | 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"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 381 | // avoid calling inversesqrt on zero. |
| 382 | 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] | 383 | builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n"); |
| 384 | if (kHairline == ellipseEffect.getMode()) { |
| 385 | // can probably do this with one step |
| 386 | builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n"); |
| 387 | builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n"); |
| 388 | } else { |
| 389 | builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n"); |
| 390 | } |
| 391 | |
| 392 | // for inner curve |
| 393 | if (kStroke == ellipseEffect.getMode()) { |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 394 | builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 395 | builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n"); |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 396 | builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1); |
| 397 | builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1); |
| 398 | builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n" |
| 399 | "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n", |
| 400 | fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 401 | builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n"); |
| 402 | builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n"); |
| 403 | } |
| 404 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 405 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 406 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 410 | const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 411 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 412 | return ellipseEffect.getMode(); |
| 413 | } |
| 414 | |
| 415 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE { |
| 416 | } |
| 417 | |
| 418 | private: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 419 | typedef GrGLVertexEffect INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | private: |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 423 | DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() { |
commit-bot@chromium.org | 96a7a96 | 2013-09-06 19:46:48 +0000 | [diff] [blame] | 424 | this->addVertexAttrib(kVec2f_GrSLType); |
| 425 | this->addVertexAttrib(kVec2f_GrSLType); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 426 | fMode = mode; |
| 427 | } |
| 428 | |
| 429 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 430 | const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other); |
| 431 | return eee.fMode == fMode; |
| 432 | } |
| 433 | |
| 434 | Mode fMode; |
| 435 | |
| 436 | GR_DECLARE_EFFECT_TEST; |
| 437 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 438 | typedef GrVertexEffect INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect); |
| 442 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 443 | GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 444 | GrContext* context, |
| 445 | const GrDrawTargetCaps&, |
| 446 | GrTexture* textures[]) { |
| 447 | return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2))); |
| 448 | } |
| 449 | |
| 450 | /////////////////////////////////////////////////////////////////////////////// |
| 451 | |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 452 | void GrOvalRenderer::reset() { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 453 | SkSafeSetNull(fRRectIndexBuffer); |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 454 | } |
| 455 | |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 456 | bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 457 | const SkRect& oval, const SkStrokeRec& stroke) |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 458 | { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 459 | bool useCoverageAA = useAA && |
| 460 | !target->getDrawState().getRenderTarget()->isMultisampled() && |
| 461 | !target->shouldDisableCoverageAAForBlend(); |
| 462 | |
| 463 | if (!useCoverageAA) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | |
| 467 | const SkMatrix& vm = context->getMatrix(); |
| 468 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 469 | // we can draw circles |
| 470 | if (SkScalarNearlyEqual(oval.width(), oval.height()) |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 471 | && circle_stays_circle(vm)) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 472 | this->drawCircle(target, useCoverageAA, oval, stroke); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 473 | // if we have shader derivative support, render as device-independent |
| 474 | } else if (target->caps()->shaderDerivativeSupport()) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 475 | return this->drawDIEllipse(target, useCoverageAA, oval, stroke); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 476 | // otherwise axis-aligned ellipses only |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 477 | } else if (vm.rectStaysRect()) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 478 | return this->drawEllipse(target, useCoverageAA, oval, stroke); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 479 | } else { |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | return true; |
| 484 | } |
| 485 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 486 | /////////////////////////////////////////////////////////////////////////////// |
| 487 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 488 | // position + edge |
| 489 | extern const GrVertexAttrib gCircleVertexAttribs[] = { |
| 490 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 491 | {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding} |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 492 | }; |
| 493 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 494 | void GrOvalRenderer::drawCircle(GrDrawTarget* target, |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 495 | bool useCoverageAA, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 496 | const SkRect& circle, |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 497 | const SkStrokeRec& stroke) |
| 498 | { |
| 499 | GrDrawState* drawState = target->drawState(); |
| 500 | |
| 501 | const SkMatrix& vm = drawState->getViewMatrix(); |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 502 | SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 503 | vm.mapPoints(¢er, 1); |
| 504 | SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width())); |
| 505 | SkScalar strokeWidth = vm.mapRadius(stroke.getWidth()); |
| 506 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 507 | GrDrawState::AutoViewMatrixRestore avmr; |
| 508 | if (!avmr.setIdentity(drawState)) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 509 | return; |
| 510 | } |
| 511 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 512 | drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 513 | SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 514 | |
| 515 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 516 | if (!geo.succeeded()) { |
| 517 | GrPrintf("Failed to get space for vertices!\n"); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); |
| 522 | |
| 523 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 524 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 525 | SkStrokeRec::kHairline_Style == style; |
| 526 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_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; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 531 | if (hasStroke) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 532 | if (SkScalarNearlyZero(strokeWidth)) { |
| 533 | halfWidth = SK_ScalarHalf; |
| 534 | } else { |
| 535 | halfWidth = SkScalarHalf(strokeWidth); |
| 536 | } |
| 537 | |
| 538 | outerRadius += halfWidth; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 539 | if (isStrokeOnly) { |
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 | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 544 | GrEffectRef* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 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}, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 590 | {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}, |
| 591 | {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding} |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 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 | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 597 | {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}, |
| 598 | {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), 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(); |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 617 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 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(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 633 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 634 | SkStrokeRec::kHairline_Style == style; |
| 635 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 636 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 637 | SkScalar innerXRadius = 0; |
| 638 | SkScalar innerYRadius = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 639 | if (hasStroke) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 640 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 641 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 642 | } else { |
| 643 | scaledStroke.scale(SK_ScalarHalf); |
| 644 | } |
| 645 | |
| 646 | // we only handle thick strokes for near-circular ellipses |
| 647 | if (scaledStroke.length() > SK_ScalarHalf && |
| 648 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 653 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 654 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | // this is legit only if scale & translation (which should be the case at the moment) |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 659 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 660 | innerXRadius = xRadius - scaledStroke.fX; |
| 661 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | xRadius += scaledStroke.fX; |
| 665 | yRadius += scaledStroke.fY; |
| 666 | } |
| 667 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 668 | GrDrawState::AutoViewMatrixRestore avmr; |
| 669 | if (!avmr.setIdentity(drawState)) { |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 670 | return false; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 671 | } |
| 672 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 673 | drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 674 | SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 675 | |
| 676 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 677 | if (!geo.succeeded()) { |
| 678 | GrPrintf("Failed to get space for vertices!\n"); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 679 | return false; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); |
| 683 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 684 | GrEffectRef* effect = EllipseEdgeEffect::Create(isStrokeOnly && |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 685 | innerXRadius > 0 && innerYRadius > 0); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 686 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 687 | static const int kEllipseCenterAttrIndex = 1; |
jvanverth@google.com | 6cc8d44 | 2013-09-10 18:24:37 +0000 | [diff] [blame] | 688 | static const int kEllipseEdgeAttrIndex = 2; |
| 689 | drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref(); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 690 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 691 | // Compute the reciprocals of the radii here to save time in the shader |
| 692 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 693 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
| 694 | SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); |
| 695 | SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 696 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 697 | // 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] | 698 | // 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] | 699 | // TODO: Consider if we should use sqrt(2)/2 instead |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 700 | xRadius += SK_ScalarHalf; |
| 701 | yRadius += SK_ScalarHalf; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 702 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 703 | SkRect bounds = SkRect::MakeLTRB( |
| 704 | center.fX - xRadius, |
| 705 | center.fY - yRadius, |
| 706 | center.fX + xRadius, |
| 707 | center.fY + yRadius |
| 708 | ); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 709 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 710 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 711 | verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius); |
| 712 | verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 713 | verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 714 | |
| 715 | verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 716 | verts[1].fOffset = SkPoint::Make(xRadius, -yRadius); |
| 717 | verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 718 | verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 719 | |
| 720 | verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 721 | verts[2].fOffset = SkPoint::Make(-xRadius, yRadius); |
| 722 | verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 723 | verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 724 | |
| 725 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 726 | verts[3].fOffset = SkPoint::Make(xRadius, yRadius); |
| 727 | verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 728 | verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
skia.committer@gmail.com | 4674676 | 2013-04-12 07:01:51 +0000 | [diff] [blame] | 729 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 730 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 731 | |
| 732 | return true; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 733 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 734 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 735 | bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target, |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 736 | bool useCoverageAA, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 737 | const SkRect& ellipse, |
| 738 | const SkStrokeRec& stroke) |
| 739 | { |
| 740 | GrDrawState* drawState = target->drawState(); |
| 741 | const SkMatrix& vm = drawState->getViewMatrix(); |
| 742 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 743 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 744 | SkScalar xRadius = SkScalarHalf(ellipse.width()); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 745 | SkScalar yRadius = SkScalarHalf(ellipse.height()); |
| 746 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 747 | SkStrokeRec::Style style = stroke.getStyle(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 748 | DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 749 | DIEllipseEdgeEffect::kStroke : |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 750 | (SkStrokeRec::kHairline_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 751 | DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill; |
| 752 | |
| 753 | SkScalar innerXRadius = 0; |
| 754 | SkScalar innerYRadius = 0; |
| 755 | if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) { |
| 756 | SkScalar strokeWidth = stroke.getWidth(); |
| 757 | |
| 758 | if (SkScalarNearlyZero(strokeWidth)) { |
| 759 | strokeWidth = SK_ScalarHalf; |
| 760 | } else { |
| 761 | strokeWidth *= SK_ScalarHalf; |
| 762 | } |
| 763 | |
| 764 | // we only handle thick strokes for near-circular ellipses |
| 765 | if (strokeWidth > SK_ScalarHalf && |
| 766 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 771 | if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius || |
| 772 | strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) { |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | // set inner radius (if needed) |
| 777 | if (SkStrokeRec::kStroke_Style == style) { |
| 778 | innerXRadius = xRadius - strokeWidth; |
| 779 | innerYRadius = yRadius - strokeWidth; |
| 780 | } |
| 781 | |
| 782 | xRadius += strokeWidth; |
| 783 | yRadius += strokeWidth; |
| 784 | } |
| 785 | if (DIEllipseEdgeEffect::kStroke == mode) { |
| 786 | mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke : |
| 787 | DIEllipseEdgeEffect::kFill; |
| 788 | } |
| 789 | SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius); |
| 790 | SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius); |
| 791 | |
| 792 | drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs)); |
| 793 | SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize()); |
| 794 | |
| 795 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 796 | if (!geo.succeeded()) { |
| 797 | GrPrintf("Failed to get space for vertices!\n"); |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices()); |
| 802 | |
| 803 | GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode); |
| 804 | |
| 805 | static const int kEllipseOuterOffsetAttrIndex = 1; |
| 806 | static const int kEllipseInnerOffsetAttrIndex = 2; |
| 807 | drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex, |
| 808 | kEllipseInnerOffsetAttrIndex)->unref(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 809 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 810 | // This expands the outer rect so that after CTM we end up with a half-pixel border |
| 811 | SkScalar a = vm[SkMatrix::kMScaleX]; |
| 812 | SkScalar b = vm[SkMatrix::kMSkewX]; |
| 813 | SkScalar c = vm[SkMatrix::kMSkewY]; |
| 814 | SkScalar d = vm[SkMatrix::kMScaleY]; |
| 815 | SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c)); |
| 816 | SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d)); |
| 817 | // This adjusts the "radius" to include the half-pixel border |
| 818 | SkScalar offsetDx = SkScalarDiv(geoDx, xRadius); |
| 819 | SkScalar offsetDy = SkScalarDiv(geoDy, yRadius); |
| 820 | |
| 821 | SkRect bounds = SkRect::MakeLTRB( |
| 822 | center.fX - xRadius - geoDx, |
| 823 | center.fY - yRadius - geoDy, |
| 824 | center.fX + xRadius + geoDx, |
| 825 | center.fY + yRadius + geoDy |
| 826 | ); |
| 827 | |
| 828 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 829 | verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy); |
| 830 | verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy); |
| 831 | |
| 832 | verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 833 | verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy); |
| 834 | verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy); |
| 835 | |
| 836 | verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 837 | verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy); |
| 838 | verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy); |
| 839 | |
| 840 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 841 | verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy); |
| 842 | verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy); |
| 843 | |
| 844 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); |
| 845 | |
| 846 | return true; |
| 847 | } |
| 848 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 849 | /////////////////////////////////////////////////////////////////////////////// |
| 850 | |
| 851 | static const uint16_t gRRectIndices[] = { |
| 852 | // corners |
| 853 | 0, 1, 5, 0, 5, 4, |
| 854 | 2, 3, 7, 2, 7, 6, |
| 855 | 8, 9, 13, 8, 13, 12, |
| 856 | 10, 11, 15, 10, 15, 14, |
skia.committer@gmail.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 | // edges |
| 859 | 1, 2, 6, 1, 6, 5, |
| 860 | 4, 5, 9, 4, 9, 8, |
| 861 | 6, 7, 11, 6, 11, 10, |
| 862 | 9, 10, 14, 9, 14, 13, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 863 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 864 | // center |
| 865 | // we place this at the end so that we can ignore these indices when rendering stroke-only |
| 866 | 5, 6, 10, 5, 10, 9 |
| 867 | }; |
| 868 | |
| 869 | |
| 870 | GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) { |
| 871 | if (NULL == fRRectIndexBuffer) { |
| 872 | fRRectIndexBuffer = |
| 873 | gpu->createIndexBuffer(sizeof(gRRectIndices), false); |
| 874 | if (NULL != fRRectIndexBuffer) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 875 | #ifdef SK_DEBUG |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 876 | bool updated = |
| 877 | #endif |
| 878 | fRRectIndexBuffer->updateData(gRRectIndices, |
| 879 | sizeof(gRRectIndices)); |
| 880 | GR_DEBUGASSERT(updated); |
| 881 | } |
| 882 | } |
| 883 | return fRRectIndexBuffer; |
| 884 | } |
| 885 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 886 | bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA, |
| 887 | const SkRRect& outer, const SkRRect& origInner) { |
| 888 | GrDrawState::AutoRestoreEffects are; |
| 889 | if (!origInner.isEmpty()) { |
| 890 | SkTCopyOnFirstWrite<SkRRect> inner(origInner); |
| 891 | if (!context->getMatrix().isIdentity()) { |
| 892 | if (!origInner.transform(context->getMatrix(), inner.writable())) { |
| 893 | return false; |
| 894 | } |
| 895 | } |
| 896 | bool applyAA = useAA && |
| 897 | !target->getDrawState().getRenderTarget()->isMultisampled() && |
| 898 | !target->shouldDisableCoverageAAForBlend(); |
| 899 | GrEffectEdgeType edgeType = applyAA ? kInverseFillAA_GrEffectEdgeType : |
| 900 | kInverseFillBW_GrEffectEdgeType; |
| 901 | GrEffectRef* effect = GrRRectEffect::Create(edgeType, *inner); |
| 902 | if (NULL == effect) { |
| 903 | return false; |
| 904 | } |
| 905 | are.set(target->drawState()); |
| 906 | target->drawState()->addCoverageEffect(effect)->unref(); |
| 907 | } |
| 908 | |
| 909 | SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle); |
| 910 | return this->drawRRect(target, context, useAA, outer, fillRec); |
| 911 | } |
| 912 | |
| 913 | bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA, |
| 914 | const SkRRect& rrect, const SkStrokeRec& stroke) { |
| 915 | if (rrect.isOval()) { |
| 916 | return this->drawOval(target, context, useAA, rrect.getBounds(), stroke); |
| 917 | } |
| 918 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 919 | bool useCoverageAA = useAA && |
| 920 | !target->getDrawState().getRenderTarget()->isMultisampled() && |
| 921 | !target->shouldDisableCoverageAAForBlend(); |
| 922 | |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 923 | // only anti-aliased rrects for now |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 924 | if (!useCoverageAA) { |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 925 | return false; |
| 926 | } |
| 927 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 928 | const SkMatrix& vm = context->getMatrix(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 929 | |
| 930 | if (!vm.rectStaysRect() || !rrect.isSimple()) { |
| 931 | return false; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 932 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 933 | |
| 934 | // do any matrix crunching before we reset the draw state for device coords |
| 935 | const SkRect& rrectBounds = rrect.getBounds(); |
| 936 | SkRect bounds; |
| 937 | vm.mapRect(&bounds, rrectBounds); |
| 938 | |
| 939 | SkVector radii = rrect.getSimpleRadii(); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 940 | SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX + |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 941 | vm[SkMatrix::kMSkewY]*radii.fY); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 942 | SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX + |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 943 | vm[SkMatrix::kMScaleY]*radii.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 944 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 945 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 946 | |
| 947 | // do (potentially) anisotropic mapping of stroke |
| 948 | SkVector scaledStroke; |
| 949 | SkScalar strokeWidth = stroke.getWidth(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 950 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 951 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 952 | SkStrokeRec::kHairline_Style == style; |
| 953 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
| 954 | |
| 955 | if (hasStroke) { |
| 956 | if (SkStrokeRec::kHairline_Style == style) { |
| 957 | scaledStroke.set(1, 1); |
| 958 | } else { |
| 959 | scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + |
| 960 | vm[SkMatrix::kMSkewY])); |
| 961 | scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + |
| 962 | vm[SkMatrix::kMScaleY])); |
| 963 | } |
| 964 | |
| 965 | // if half of strokewidth is greater than radius, we don't handle that right now |
| 966 | if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) { |
| 967 | return false; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on |
| 972 | // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine- |
| 973 | // patch will have fractional coverage. This only matters when the interior is actually filled. |
| 974 | // We could consider falling back to rect rendering here, since a tiny radius is |
| 975 | // indistinguishable from a square corner. |
| 976 | if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 977 | return false; |
| 978 | } |
| 979 | |
| 980 | // reset to device coordinates |
| 981 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 982 | GrDrawState::AutoViewMatrixRestore avmr; |
| 983 | if (!avmr.setIdentity(drawState)) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 984 | return false; |
| 985 | } |
| 986 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 987 | GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu()); |
| 988 | if (NULL == indexBuffer) { |
| 989 | GrPrintf("Failed to create index buffer!\n"); |
| 990 | return false; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 991 | } |
| 992 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 993 | // if the corners are circles, use the circle renderer |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 994 | if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 995 | drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 996 | SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 997 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 998 | GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); |
| 999 | if (!geo.succeeded()) { |
| 1000 | GrPrintf("Failed to get space for vertices!\n"); |
| 1001 | return false; |
| 1002 | } |
| 1003 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1004 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1005 | SkScalar innerRadius = 0.0f; |
| 1006 | SkScalar outerRadius = xRadius; |
| 1007 | SkScalar halfWidth = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1008 | if (hasStroke) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1009 | if (SkScalarNearlyZero(scaledStroke.fX)) { |
| 1010 | halfWidth = SK_ScalarHalf; |
| 1011 | } else { |
| 1012 | halfWidth = SkScalarHalf(scaledStroke.fX); |
| 1013 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1014 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1015 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1016 | innerRadius = xRadius - halfWidth; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1017 | } |
| 1018 | outerRadius += halfWidth; |
| 1019 | bounds.outset(halfWidth, halfWidth); |
| 1020 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1021 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1022 | isStrokeOnly = (isStrokeOnly && innerRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 1023 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1024 | GrEffectRef* effect = CircleEdgeEffect::Create(isStrokeOnly); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1025 | static const int kCircleEdgeAttrIndex = 1; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1026 | drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref(); |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 1027 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1028 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
| 1029 | // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the |
| 1030 | // verts of the bounding box that is rendered and the outset ensures the box will cover all |
| 1031 | // pixels partially covered by the circle. |
| 1032 | outerRadius += SK_ScalarHalf; |
| 1033 | innerRadius -= SK_ScalarHalf; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1034 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1035 | // Expand the rect so all the pixels will be captured. |
| 1036 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1037 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1038 | SkScalar yCoords[4] = { |
| 1039 | bounds.fTop, |
| 1040 | bounds.fTop + outerRadius, |
| 1041 | bounds.fBottom - outerRadius, |
| 1042 | bounds.fBottom |
| 1043 | }; |
| 1044 | SkScalar yOuterRadii[4] = { |
| 1045 | -outerRadius, |
| 1046 | 0, |
| 1047 | 0, |
| 1048 | outerRadius |
| 1049 | }; |
| 1050 | for (int i = 0; i < 4; ++i) { |
| 1051 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 1052 | verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]); |
| 1053 | verts->fOuterRadius = outerRadius; |
| 1054 | verts->fInnerRadius = innerRadius; |
| 1055 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1056 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1057 | verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]); |
| 1058 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1059 | verts->fOuterRadius = outerRadius; |
| 1060 | verts->fInnerRadius = innerRadius; |
| 1061 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1062 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1063 | verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]); |
| 1064 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1065 | verts->fOuterRadius = outerRadius; |
| 1066 | verts->fInnerRadius = innerRadius; |
| 1067 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1068 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1069 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1070 | verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]); |
| 1071 | verts->fOuterRadius = outerRadius; |
| 1072 | verts->fInnerRadius = innerRadius; |
| 1073 | verts++; |
| 1074 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1075 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1076 | // drop out the middle quad if we're stroked |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1077 | int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 : |
| 1078 | SK_ARRAY_COUNT(gRRectIndices); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1079 | target->setIndexSourceToBuffer(indexBuffer); |
| 1080 | target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1081 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1082 | // otherwise we use the ellipse renderer |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1083 | } else { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1084 | drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1085 | SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1086 | |
| 1087 | SkScalar innerXRadius = 0.0f; |
| 1088 | SkScalar innerYRadius = 0.0f; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1089 | if (hasStroke) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1090 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 1091 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 1092 | } else { |
| 1093 | scaledStroke.scale(SK_ScalarHalf); |
| 1094 | } |
| 1095 | |
| 1096 | // we only handle thick strokes for near-circular ellipses |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 1097 | if (scaledStroke.length() > SK_ScalarHalf && |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1098 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
| 1102 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1103 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 1104 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | // this is legit only if scale & translation (which should be the case at the moment) |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1109 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1110 | innerXRadius = xRadius - scaledStroke.fX; |
| 1111 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | xRadius += scaledStroke.fX; |
| 1115 | yRadius += scaledStroke.fY; |
| 1116 | bounds.outset(scaledStroke.fX, scaledStroke.fY); |
| 1117 | } |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1118 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1119 | isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 1120 | |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1121 | GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); |
| 1122 | if (!geo.succeeded()) { |
| 1123 | GrPrintf("Failed to get space for vertices!\n"); |
| 1124 | return false; |
| 1125 | } |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1126 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1127 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1128 | GrEffectRef* effect = EllipseEdgeEffect::Create(isStrokeOnly); |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1129 | static const int kEllipseOffsetAttrIndex = 1; |
| 1130 | static const int kEllipseRadiiAttrIndex = 2; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1131 | drawState->addCoverageEffect(effect, |
| 1132 | kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1133 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1134 | // Compute the reciprocals of the radii here to save time in the shader |
| 1135 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 1136 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
| 1137 | SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); |
| 1138 | SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Extend the radii out half a pixel to antialias. |
| 1141 | SkScalar xOuterRadius = xRadius + SK_ScalarHalf; |
| 1142 | SkScalar yOuterRadius = yRadius + SK_ScalarHalf; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1143 | |
| 1144 | // Expand the rect so all the pixels will be captured. |
| 1145 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 1146 | |
| 1147 | SkScalar yCoords[4] = { |
| 1148 | bounds.fTop, |
| 1149 | bounds.fTop + yOuterRadius, |
| 1150 | bounds.fBottom - yOuterRadius, |
| 1151 | bounds.fBottom |
| 1152 | }; |
| 1153 | SkScalar yOuterOffsets[4] = { |
jvanverth@google.com | d1b5b14 | 2013-07-02 14:46:03 +0000 | [diff] [blame] | 1154 | yOuterRadius, |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1155 | SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0 |
| 1156 | SK_ScalarNearlyZero, |
| 1157 | yOuterRadius |
| 1158 | }; |
| 1159 | |
| 1160 | for (int i = 0; i < 4; ++i) { |
| 1161 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
jvanverth@google.com | d1b5b14 | 2013-07-02 14:46:03 +0000 | [diff] [blame] | 1162 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1163 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1164 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1165 | verts++; |
| 1166 | |
| 1167 | verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]); |
| 1168 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1169 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1170 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1171 | verts++; |
| 1172 | |
| 1173 | verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]); |
| 1174 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1175 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1176 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1177 | verts++; |
| 1178 | |
| 1179 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1180 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1181 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1182 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1183 | verts++; |
| 1184 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1185 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1186 | // drop out the middle quad if we're stroked |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame^] | 1187 | int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 : |
| 1188 | SK_ARRAY_COUNT(gRRectIndices); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1189 | target->setIndexSourceToBuffer(indexBuffer); |
| 1190 | target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); |
| 1191 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1192 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1193 | return true; |
| 1194 | } |