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