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