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" |
| 13 | #include "GrTBackendEffectFactory.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 14 | |
| 15 | #include "GrDrawState.h" |
| 16 | #include "GrDrawTarget.h" |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 17 | #include "GrGpu.h" |
| 18 | |
| 19 | #include "SkRRect.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 20 | #include "SkStrokeRec.h" |
| 21 | |
| 22 | SK_DEFINE_INST_COUNT(GrOvalRenderer) |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | struct CircleVertex { |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 27 | GrPoint fPos; |
| 28 | GrPoint fOffset; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 29 | SkScalar fOuterRadius; |
| 30 | SkScalar fInnerRadius; |
| 31 | }; |
| 32 | |
| 33 | struct EllipseVertex { |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 34 | GrPoint fPos; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 35 | GrPoint fOffset; |
| 36 | GrPoint fOuterRadii; |
| 37 | GrPoint fInnerRadii; |
| 38 | }; |
| 39 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 40 | inline bool circle_stays_circle(const SkMatrix& m) { |
| 41 | return m.isSimilarity(); |
| 42 | } |
| 43 | |
| 44 | } |
| 45 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | |
| 48 | /** |
| 49 | * 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] | 50 | * 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] | 51 | */ |
| 52 | |
| 53 | class CircleEdgeEffect : public GrEffect { |
| 54 | public: |
| 55 | static GrEffectRef* Create(bool stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 56 | GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true)); |
| 57 | GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 58 | |
| 59 | if (stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 60 | gCircleStrokeEdge->ref(); |
| 61 | return gCircleStrokeEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 62 | } else { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 63 | gCircleFillEdge->ref(); |
| 64 | return gCircleFillEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 68 | virtual void getConstantColorComponents(GrColor* color, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 69 | uint32_t* validFlags) const SK_OVERRIDE { |
| 70 | *validFlags = 0; |
| 71 | } |
| 72 | |
| 73 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 74 | return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance(); |
| 75 | } |
| 76 | |
| 77 | virtual ~CircleEdgeEffect() {} |
| 78 | |
| 79 | static const char* Name() { return "CircleEdge"; } |
| 80 | |
| 81 | inline bool isStroked() const { return fStroke; } |
| 82 | |
| 83 | class GLEffect : public GrGLEffect { |
| 84 | public: |
| 85 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 86 | : INHERITED (factory) {} |
| 87 | |
| 88 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 89 | const GrDrawEffect& drawEffect, |
| 90 | EffectKey key, |
| 91 | const char* outputColor, |
| 92 | const char* inputColor, |
| 93 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 94 | const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>(); |
| 95 | const char *vsName, *fsName; |
| 96 | builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName); |
| 97 | |
| 98 | const SkString* attrName = |
| 99 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 100 | builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 101 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 102 | builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 103 | builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName); |
| 104 | if (circleEffect.isStroked()) { |
| 105 | builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName); |
| 106 | builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); |
| 107 | } |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 109 | SkString modulate; |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 110 | GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 111 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 112 | } |
| 113 | |
| 114 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 115 | const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>(); |
| 116 | |
| 117 | return circleEffect.isStroked() ? 0x1 : 0x0; |
| 118 | } |
| 119 | |
| 120 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 121 | |
| 122 | private: |
| 123 | typedef GrGLEffect INHERITED; |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | private: |
| 128 | CircleEdgeEffect(bool stroke) : GrEffect() { |
| 129 | this->addVertexAttrib(kVec4f_GrSLType); |
| 130 | fStroke = stroke; |
| 131 | } |
| 132 | |
| 133 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 134 | const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other); |
| 135 | return cee.fStroke == fStroke; |
| 136 | } |
| 137 | |
| 138 | bool fStroke; |
| 139 | |
| 140 | GR_DECLARE_EFFECT_TEST; |
| 141 | |
| 142 | typedef GrEffect INHERITED; |
| 143 | }; |
| 144 | |
| 145 | GR_DEFINE_EFFECT_TEST(CircleEdgeEffect); |
| 146 | |
| 147 | GrEffectRef* CircleEdgeEffect::TestCreate(SkMWCRandom* random, |
| 148 | GrContext* context, |
| 149 | const GrDrawTargetCaps&, |
| 150 | GrTexture* textures[]) { |
| 151 | return CircleEdgeEffect::Create(random->nextBool()); |
| 152 | } |
| 153 | |
| 154 | /////////////////////////////////////////////////////////////////////////////// |
| 155 | |
| 156 | /** |
| 157 | * 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^] | 158 | * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii, |
| 159 | * in both x and y directions. |
| 160 | * |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 161 | * 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] | 162 | */ |
| 163 | |
| 164 | class EllipseEdgeEffect : public GrEffect { |
| 165 | public: |
| 166 | static GrEffectRef* Create(bool stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 167 | GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true)); |
| 168 | GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 169 | |
| 170 | if (stroke) { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 171 | gEllipseStrokeEdge->ref(); |
| 172 | return gEllipseStrokeEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 173 | } else { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 174 | gEllipseFillEdge->ref(); |
| 175 | return gEllipseFillEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 179 | virtual void getConstantColorComponents(GrColor* color, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 180 | uint32_t* validFlags) const SK_OVERRIDE { |
| 181 | *validFlags = 0; |
| 182 | } |
| 183 | |
| 184 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 185 | return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance(); |
| 186 | } |
| 187 | |
| 188 | virtual ~EllipseEdgeEffect() {} |
| 189 | |
| 190 | static const char* Name() { return "EllipseEdge"; } |
| 191 | |
| 192 | inline bool isStroked() const { return fStroke; } |
| 193 | |
| 194 | class GLEffect : public GrGLEffect { |
| 195 | public: |
| 196 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 197 | : INHERITED (factory) {} |
| 198 | |
| 199 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 200 | const GrDrawEffect& drawEffect, |
| 201 | EffectKey key, |
| 202 | const char* outputColor, |
| 203 | const char* inputColor, |
| 204 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 205 | const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>(); |
| 206 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 207 | const char *vsOffsetName, *fsOffsetName; |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 208 | const char *vsRadiiName, *fsRadiiName; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 209 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 210 | builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 211 | const SkString* attr0Name = |
| 212 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 213 | builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 214 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 215 | builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 216 | const SkString* attr1Name = |
| 217 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 218 | builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 219 | |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame^] | 220 | // for outer curve |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 221 | builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName); |
| 222 | builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n"); |
| 223 | builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName); |
| 224 | builder->fsCodeAppend("\tfloat invlen = inversesqrt(dot(grad, grad));\n"); |
| 225 | 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] | 226 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 227 | // for inner curve |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 228 | if (ellipseEffect.isStroked()) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 229 | builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName); |
| 230 | builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n"); |
| 231 | builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName); |
| 232 | builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n"); |
| 233 | 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] | 234 | } |
| 235 | |
| 236 | SkString modulate; |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 237 | GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 238 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 242 | const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>(); |
| 243 | |
| 244 | return ellipseEffect.isStroked() ? 0x1 : 0x0; |
| 245 | } |
| 246 | |
| 247 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE { |
| 248 | } |
| 249 | |
| 250 | private: |
| 251 | typedef GrGLEffect INHERITED; |
| 252 | }; |
| 253 | |
| 254 | private: |
| 255 | EllipseEdgeEffect(bool stroke) : GrEffect() { |
| 256 | this->addVertexAttrib(kVec2f_GrSLType); |
| 257 | this->addVertexAttrib(kVec4f_GrSLType); |
| 258 | fStroke = stroke; |
| 259 | } |
| 260 | |
| 261 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 262 | const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other); |
| 263 | return eee.fStroke == fStroke; |
| 264 | } |
| 265 | |
| 266 | bool fStroke; |
| 267 | |
| 268 | GR_DECLARE_EFFECT_TEST; |
| 269 | |
| 270 | typedef GrEffect INHERITED; |
| 271 | }; |
| 272 | |
| 273 | GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect); |
| 274 | |
| 275 | GrEffectRef* EllipseEdgeEffect::TestCreate(SkMWCRandom* random, |
| 276 | GrContext* context, |
| 277 | const GrDrawTargetCaps&, |
| 278 | GrTexture* textures[]) { |
| 279 | return EllipseEdgeEffect::Create(random->nextBool()); |
| 280 | } |
| 281 | |
| 282 | /////////////////////////////////////////////////////////////////////////////// |
| 283 | |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 284 | bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA, |
| 285 | const GrRect& oval, const SkStrokeRec& stroke) |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 286 | { |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 287 | if (!useAA) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | const SkMatrix& vm = context->getMatrix(); |
| 292 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 293 | // we can draw circles |
| 294 | if (SkScalarNearlyEqual(oval.width(), oval.height()) |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 295 | && circle_stays_circle(vm)) { |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 296 | this->drawCircle(target, useAA, oval, stroke); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 297 | |
| 298 | // and axis-aligned ellipses only |
| 299 | } else if (vm.rectStaysRect()) { |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 300 | return this->drawEllipse(target, useAA, oval, stroke); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 301 | |
| 302 | } else { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | return true; |
| 307 | } |
| 308 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 309 | namespace { |
| 310 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 311 | /////////////////////////////////////////////////////////////////////////////// |
| 312 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 313 | // position + edge |
| 314 | extern const GrVertexAttrib gCircleVertexAttribs[] = { |
| 315 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 316 | {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
| 317 | }; |
| 318 | |
| 319 | }; |
| 320 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 321 | void GrOvalRenderer::drawCircle(GrDrawTarget* target, |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 322 | bool useAA, |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 323 | const GrRect& circle, |
| 324 | const SkStrokeRec& stroke) |
| 325 | { |
| 326 | GrDrawState* drawState = target->drawState(); |
| 327 | |
| 328 | const SkMatrix& vm = drawState->getViewMatrix(); |
| 329 | GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY()); |
| 330 | vm.mapPoints(¢er, 1); |
| 331 | SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width())); |
| 332 | SkScalar strokeWidth = vm.mapRadius(stroke.getWidth()); |
| 333 | |
| 334 | GrDrawState::AutoDeviceCoordDraw adcd(drawState); |
| 335 | if (!adcd.succeeded()) { |
| 336 | return; |
| 337 | } |
| 338 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 339 | drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs)); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 340 | GrAssert(sizeof(CircleVertex) == drawState->getVertexSize()); |
| 341 | |
| 342 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 343 | if (!geo.succeeded()) { |
| 344 | GrPrintf("Failed to get space for vertices!\n"); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); |
| 349 | |
| 350 | SkStrokeRec::Style style = stroke.getStyle(); |
| 351 | bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); |
| 352 | enum { |
| 353 | // the edge effects share this stage with glyph rendering |
| 354 | // (kGlyphMaskStage in GrTextContext) && SW path rendering |
| 355 | // (kPathMaskStage in GrSWMaskHelper) |
| 356 | kEdgeEffectStage = GrPaint::kTotalStages, |
| 357 | }; |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 358 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 359 | GrEffectRef* effect = CircleEdgeEffect::Create(isStroked); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 360 | static const int kCircleEdgeAttrIndex = 1; |
| 361 | drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref(); |
| 362 | |
| 363 | SkScalar innerRadius = 0.0f; |
| 364 | SkScalar outerRadius = radius; |
| 365 | SkScalar halfWidth = 0; |
| 366 | if (style != SkStrokeRec::kFill_Style) { |
| 367 | if (SkScalarNearlyZero(strokeWidth)) { |
| 368 | halfWidth = SK_ScalarHalf; |
| 369 | } else { |
| 370 | halfWidth = SkScalarHalf(strokeWidth); |
| 371 | } |
| 372 | |
| 373 | outerRadius += halfWidth; |
| 374 | if (isStroked) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 375 | innerRadius = radius - halfWidth; |
| 376 | isStroked = (innerRadius > 0); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
bsalomon@google.com | 58e30fe | 2013-04-01 19:01:20 +0000 | [diff] [blame] | 380 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
| 381 | // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the |
| 382 | // verts of the bounding box that is rendered and the outset ensures the box will cover all |
| 383 | // pixels partially covered by the circle. |
| 384 | outerRadius += SK_ScalarHalf; |
| 385 | innerRadius -= SK_ScalarHalf; |
| 386 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 387 | SkRect bounds = SkRect::MakeLTRB( |
bsalomon@google.com | 58e30fe | 2013-04-01 19:01:20 +0000 | [diff] [blame] | 388 | center.fX - outerRadius, |
| 389 | center.fY - outerRadius, |
| 390 | center.fX + outerRadius, |
| 391 | center.fY + outerRadius |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 392 | ); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 393 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 394 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 395 | verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius); |
| 396 | verts[0].fOuterRadius = outerRadius; |
| 397 | verts[0].fInnerRadius = innerRadius; |
| 398 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 399 | verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
skia.committer@gmail.com | 4674676 | 2013-04-12 07:01:51 +0000 | [diff] [blame] | 400 | verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 401 | verts[1].fOuterRadius = outerRadius; |
| 402 | verts[1].fInnerRadius = innerRadius; |
| 403 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 404 | verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 405 | verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius); |
| 406 | verts[2].fOuterRadius = outerRadius; |
| 407 | verts[2].fInnerRadius = innerRadius; |
| 408 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 409 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 410 | verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius); |
| 411 | verts[3].fOuterRadius = outerRadius; |
| 412 | verts[3].fInnerRadius = innerRadius; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 413 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 414 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 415 | } |
| 416 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 417 | /////////////////////////////////////////////////////////////////////////////// |
| 418 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 419 | namespace { |
| 420 | |
| 421 | // position + edge |
| 422 | extern const GrVertexAttrib gEllipseVertexAttribs[] = { |
| 423 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 424 | {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}, |
| 425 | {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
| 426 | }; |
| 427 | |
| 428 | }; |
| 429 | |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 430 | bool GrOvalRenderer::drawEllipse(GrDrawTarget* target, |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 431 | bool useAA, |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 432 | const GrRect& ellipse, |
| 433 | const SkStrokeRec& stroke) |
| 434 | { |
| 435 | GrDrawState* drawState = target->drawState(); |
| 436 | #ifdef SK_DEBUG |
| 437 | { |
| 438 | // we should have checked for this previously |
| 439 | bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect(); |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 440 | SkASSERT(useAA && isAxisAlignedEllipse); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 441 | } |
| 442 | #endif |
| 443 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 444 | // 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] | 445 | const SkMatrix& vm = drawState->getViewMatrix(); |
| 446 | GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY()); |
| 447 | vm.mapPoints(¢er, 1); |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 448 | SkScalar ellipseXRadius = SkScalarHalf(ellipse.width()); |
| 449 | SkScalar ellipseYRadius = SkScalarHalf(ellipse.height()); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 450 | SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius + |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 451 | vm[SkMatrix::kMSkewY]*ellipseYRadius); |
skia.committer@gmail.com | 64b682c | 2013-04-20 07:01:07 +0000 | [diff] [blame] | 452 | SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius + |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 453 | vm[SkMatrix::kMScaleY]*ellipseYRadius); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 454 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 455 | // do (potentially) anisotropic mapping of stroke |
| 456 | SkVector scaledStroke; |
| 457 | SkScalar strokeWidth = stroke.getWidth(); |
| 458 | scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY])); |
| 459 | scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY])); |
| 460 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 461 | SkStrokeRec::Style style = stroke.getStyle(); |
| 462 | bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); |
| 463 | |
| 464 | SkScalar innerXRadius = 0.0f; |
| 465 | SkScalar innerYRadius = 0.0f; |
| 466 | if (SkStrokeRec::kFill_Style != style) { |
| 467 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 468 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 469 | } else { |
| 470 | scaledStroke.scale(SK_ScalarHalf); |
| 471 | } |
| 472 | |
| 473 | // we only handle thick strokes for near-circular ellipses |
| 474 | if (scaledStroke.length() > SK_ScalarHalf && |
| 475 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 480 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 481 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | // this is legit only if scale & translation (which should be the case at the moment) |
| 486 | if (isStroked) { |
| 487 | innerXRadius = xRadius - scaledStroke.fX; |
| 488 | innerYRadius = yRadius - scaledStroke.fY; |
| 489 | isStroked = (innerXRadius > 0 && innerYRadius > 0); |
| 490 | } |
| 491 | |
| 492 | xRadius += scaledStroke.fX; |
| 493 | yRadius += scaledStroke.fY; |
| 494 | } |
| 495 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 496 | GrDrawState::AutoDeviceCoordDraw adcd(drawState); |
| 497 | if (!adcd.succeeded()) { |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 498 | return false; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 499 | } |
| 500 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 501 | drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs)); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 502 | GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize()); |
| 503 | |
| 504 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 505 | if (!geo.succeeded()) { |
| 506 | GrPrintf("Failed to get space for vertices!\n"); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 507 | return false; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); |
| 511 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 512 | enum { |
| 513 | // the edge effects share this stage with glyph rendering |
| 514 | // (kGlyphMaskStage in GrTextContext) && SW path rendering |
| 515 | // (kPathMaskStage in GrSWMaskHelper) |
| 516 | kEdgeEffectStage = GrPaint::kTotalStages, |
| 517 | }; |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 518 | GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 519 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 520 | static const int kEllipseCenterAttrIndex = 1; |
| 521 | static const int kEllipseEdgeAttrIndex = 2; |
| 522 | drawState->setEffect(kEdgeEffectStage, effect, |
| 523 | kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref(); |
| 524 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 525 | // Compute the reciprocals of the radii here to save time in the shader |
| 526 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 527 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
| 528 | SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); |
| 529 | SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 530 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 531 | // 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] | 532 | // 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] | 533 | // TODO: Consider if we should use sqrt(2)/2 instead |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 534 | xRadius += SK_ScalarHalf; |
| 535 | yRadius += SK_ScalarHalf; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 536 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 537 | SkRect bounds = SkRect::MakeLTRB( |
| 538 | center.fX - xRadius, |
| 539 | center.fY - yRadius, |
| 540 | center.fX + xRadius, |
| 541 | center.fY + yRadius |
| 542 | ); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 543 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 544 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 545 | verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius); |
| 546 | verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 547 | verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 548 | |
| 549 | verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 550 | verts[1].fOffset = SkPoint::Make(xRadius, -yRadius); |
| 551 | verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 552 | verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 553 | |
| 554 | verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 555 | verts[2].fOffset = SkPoint::Make(-xRadius, yRadius); |
| 556 | verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 557 | verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 558 | |
| 559 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 560 | verts[3].fOffset = SkPoint::Make(xRadius, yRadius); |
| 561 | verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 562 | verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
skia.committer@gmail.com | 4674676 | 2013-04-12 07:01:51 +0000 | [diff] [blame] | 563 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 564 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 565 | |
| 566 | return true; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 567 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 568 | |
| 569 | /////////////////////////////////////////////////////////////////////////////// |
| 570 | |
| 571 | static const uint16_t gRRectIndices[] = { |
| 572 | // corners |
| 573 | 0, 1, 5, 0, 5, 4, |
| 574 | 2, 3, 7, 2, 7, 6, |
| 575 | 8, 9, 13, 8, 13, 12, |
| 576 | 10, 11, 15, 10, 15, 14, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 577 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 578 | // edges |
| 579 | 1, 2, 6, 1, 6, 5, |
| 580 | 4, 5, 9, 4, 9, 8, |
| 581 | 6, 7, 11, 6, 11, 10, |
| 582 | 9, 10, 14, 9, 14, 13, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 583 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 584 | // center |
| 585 | // we place this at the end so that we can ignore these indices when rendering stroke-only |
| 586 | 5, 6, 10, 5, 10, 9 |
| 587 | }; |
| 588 | |
| 589 | |
| 590 | GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) { |
| 591 | if (NULL == fRRectIndexBuffer) { |
| 592 | fRRectIndexBuffer = |
| 593 | gpu->createIndexBuffer(sizeof(gRRectIndices), false); |
| 594 | if (NULL != fRRectIndexBuffer) { |
| 595 | #if GR_DEBUG |
| 596 | bool updated = |
| 597 | #endif |
| 598 | fRRectIndexBuffer->updateData(gRRectIndices, |
| 599 | sizeof(gRRectIndices)); |
| 600 | GR_DEBUGASSERT(updated); |
| 601 | } |
| 602 | } |
| 603 | return fRRectIndexBuffer; |
| 604 | } |
| 605 | |
skia.committer@gmail.com | 2fd42c4 | 2013-05-03 07:01:00 +0000 | [diff] [blame] | 606 | bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA, |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 607 | const SkRRect& rrect, const SkStrokeRec& stroke) |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 608 | { |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 609 | // only anti-aliased rrects for now |
| 610 | if (!useAA) { |
| 611 | return false; |
| 612 | } |
| 613 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 614 | const SkMatrix& vm = context->getMatrix(); |
| 615 | #ifdef SK_DEBUG |
| 616 | { |
| 617 | // we should have checked for this previously |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 618 | SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple()); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 619 | } |
| 620 | #endif |
| 621 | |
| 622 | // do any matrix crunching before we reset the draw state for device coords |
| 623 | const SkRect& rrectBounds = rrect.getBounds(); |
| 624 | SkRect bounds; |
| 625 | vm.mapRect(&bounds, rrectBounds); |
| 626 | |
| 627 | SkVector radii = rrect.getSimpleRadii(); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 628 | SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX + |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 629 | vm[SkMatrix::kMSkewY]*radii.fY); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 630 | SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX + |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 631 | vm[SkMatrix::kMScaleY]*radii.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 632 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 633 | // if hairline stroke is greater than radius, we don't handle that right now |
| 634 | SkStrokeRec::Style style = stroke.getStyle(); |
| 635 | if (SkStrokeRec::kHairline_Style == style && |
| 636 | (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) { |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | // do (potentially) anisotropic mapping of stroke |
| 641 | SkVector scaledStroke; |
| 642 | SkScalar strokeWidth = stroke.getWidth(); |
| 643 | scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY])); |
| 644 | scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY])); |
| 645 | |
| 646 | // if half of strokewidth is greater than radius, we don't handle that right now |
| 647 | if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) { |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | // reset to device coordinates |
| 652 | GrDrawState* drawState = target->drawState(); |
| 653 | GrDrawState::AutoDeviceCoordDraw adcd(drawState); |
| 654 | if (!adcd.succeeded()) { |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); |
| 659 | |
| 660 | enum { |
| 661 | // the edge effects share this stage with glyph rendering |
| 662 | // (kGlyphMaskStage in GrTextContext) && SW path rendering |
| 663 | // (kPathMaskStage in GrSWMaskHelper) |
| 664 | kEdgeEffectStage = GrPaint::kTotalStages, |
| 665 | }; |
| 666 | |
| 667 | GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu()); |
| 668 | if (NULL == indexBuffer) { |
| 669 | GrPrintf("Failed to create index buffer!\n"); |
| 670 | return false; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 671 | } |
| 672 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 673 | // if the corners are circles, use the circle renderer |
| 674 | if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { |
| 675 | drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs)); |
| 676 | GrAssert(sizeof(CircleVertex) == drawState->getVertexSize()); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 677 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 678 | GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); |
| 679 | if (!geo.succeeded()) { |
| 680 | GrPrintf("Failed to get space for vertices!\n"); |
| 681 | return false; |
| 682 | } |
| 683 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 684 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 685 | SkScalar innerRadius = 0.0f; |
| 686 | SkScalar outerRadius = xRadius; |
| 687 | SkScalar halfWidth = 0; |
| 688 | if (style != SkStrokeRec::kFill_Style) { |
| 689 | if (SkScalarNearlyZero(scaledStroke.fX)) { |
| 690 | halfWidth = SK_ScalarHalf; |
| 691 | } else { |
| 692 | halfWidth = SkScalarHalf(scaledStroke.fX); |
| 693 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 694 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 695 | if (isStroked) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 696 | innerRadius = xRadius - halfWidth; |
| 697 | isStroked = (innerRadius > 0); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 698 | } |
| 699 | outerRadius += halfWidth; |
| 700 | bounds.outset(halfWidth, halfWidth); |
| 701 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 702 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 703 | GrEffectRef* effect = CircleEdgeEffect::Create(isStroked); |
| 704 | static const int kCircleEdgeAttrIndex = 1; |
| 705 | drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref(); |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame^] | 706 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 707 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
| 708 | // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the |
| 709 | // verts of the bounding box that is rendered and the outset ensures the box will cover all |
| 710 | // pixels partially covered by the circle. |
| 711 | outerRadius += SK_ScalarHalf; |
| 712 | innerRadius -= SK_ScalarHalf; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 713 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 714 | // Expand the rect so all the pixels will be captured. |
| 715 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 716 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 717 | SkScalar yCoords[4] = { |
| 718 | bounds.fTop, |
| 719 | bounds.fTop + outerRadius, |
| 720 | bounds.fBottom - outerRadius, |
| 721 | bounds.fBottom |
| 722 | }; |
| 723 | SkScalar yOuterRadii[4] = { |
| 724 | -outerRadius, |
| 725 | 0, |
| 726 | 0, |
| 727 | outerRadius |
| 728 | }; |
| 729 | for (int i = 0; i < 4; ++i) { |
| 730 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 731 | verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]); |
| 732 | verts->fOuterRadius = outerRadius; |
| 733 | verts->fInnerRadius = innerRadius; |
| 734 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 735 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 736 | verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]); |
| 737 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 738 | verts->fOuterRadius = outerRadius; |
| 739 | verts->fInnerRadius = innerRadius; |
| 740 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 741 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 742 | verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]); |
| 743 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 744 | verts->fOuterRadius = outerRadius; |
| 745 | verts->fInnerRadius = innerRadius; |
| 746 | verts++; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 747 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 748 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 749 | verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]); |
| 750 | verts->fOuterRadius = outerRadius; |
| 751 | verts->fInnerRadius = innerRadius; |
| 752 | verts++; |
| 753 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 754 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 755 | // drop out the middle quad if we're stroked |
| 756 | int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices); |
| 757 | target->setIndexSourceToBuffer(indexBuffer); |
| 758 | target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 759 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 760 | // otherwise we use the ellipse renderer |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 761 | } else { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 762 | drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs)); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 763 | GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize()); |
| 764 | |
| 765 | SkScalar innerXRadius = 0.0f; |
| 766 | SkScalar innerYRadius = 0.0f; |
| 767 | if (SkStrokeRec::kFill_Style != style) { |
| 768 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 769 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 770 | } else { |
| 771 | scaledStroke.scale(SK_ScalarHalf); |
| 772 | } |
| 773 | |
| 774 | // we only handle thick strokes for near-circular ellipses |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame^] | 775 | if (scaledStroke.length() > SK_ScalarHalf && |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 776 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 781 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 782 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | // this is legit only if scale & translation (which should be the case at the moment) |
| 787 | if (isStroked) { |
| 788 | innerXRadius = xRadius - scaledStroke.fX; |
| 789 | innerYRadius = yRadius - scaledStroke.fY; |
| 790 | isStroked = (innerXRadius > 0 && innerYRadius > 0); |
| 791 | } |
| 792 | |
| 793 | xRadius += scaledStroke.fX; |
| 794 | yRadius += scaledStroke.fY; |
| 795 | bounds.outset(scaledStroke.fX, scaledStroke.fY); |
| 796 | } |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 797 | |
| 798 | GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); |
| 799 | if (!geo.succeeded()) { |
| 800 | GrPrintf("Failed to get space for vertices!\n"); |
| 801 | return false; |
| 802 | } |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 803 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 804 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 805 | GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked); |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 806 | static const int kEllipseOffsetAttrIndex = 1; |
| 807 | static const int kEllipseRadiiAttrIndex = 2; |
| 808 | drawState->setEffect(kEdgeEffectStage, effect, |
| 809 | kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 810 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 811 | // Compute the reciprocals of the radii here to save time in the shader |
| 812 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 813 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
| 814 | SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); |
| 815 | SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 816 | |
| 817 | // Extend the radii out half a pixel to antialias. |
| 818 | SkScalar xOuterRadius = xRadius + SK_ScalarHalf; |
| 819 | SkScalar yOuterRadius = yRadius + SK_ScalarHalf; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 820 | |
| 821 | // Expand the rect so all the pixels will be captured. |
| 822 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 823 | |
| 824 | SkScalar yCoords[4] = { |
| 825 | bounds.fTop, |
| 826 | bounds.fTop + yOuterRadius, |
| 827 | bounds.fBottom - yOuterRadius, |
| 828 | bounds.fBottom |
| 829 | }; |
| 830 | SkScalar yOuterOffsets[4] = { |
| 831 | -yOuterRadius, |
| 832 | SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0 |
| 833 | SK_ScalarNearlyZero, |
| 834 | yOuterRadius |
| 835 | }; |
| 836 | |
| 837 | for (int i = 0; i < 4; ++i) { |
| 838 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 839 | verts->fOffset = SkPoint::Make(-xOuterRadius, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 840 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 841 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 842 | verts++; |
| 843 | |
| 844 | verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]); |
| 845 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 846 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 847 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 848 | verts++; |
| 849 | |
| 850 | verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]); |
| 851 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 852 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 853 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 854 | verts++; |
| 855 | |
| 856 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 857 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 858 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 859 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 860 | verts++; |
| 861 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 862 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 863 | // drop out the middle quad if we're stroked |
| 864 | int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices); |
| 865 | target->setIndexSourceToBuffer(indexBuffer); |
| 866 | target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); |
| 867 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 868 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 869 | return true; |
| 870 | } |