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