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 | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 10 | #include "GrBatch.h" |
| 11 | #include "GrBatchTarget.h" |
| 12 | #include "GrBufferAllocPool.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 13 | #include "GrDrawTarget.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 14 | #include "GrGeometryProcessor.h" |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 15 | #include "GrGpu.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 16 | #include "GrInvariantOutput.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 17 | #include "GrPipelineBuilder.h" |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 18 | #include "GrProcessor.h" |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 19 | #include "SkRRect.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 20 | #include "SkStrokeRec.h" |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 21 | #include "SkTLazy.h" |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 22 | #include "effects/GrRRectEffect.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 23 | #include "gl/GrGLProcessor.h" |
| 24 | #include "gl/GrGLSL.h" |
| 25 | #include "gl/GrGLGeometryProcessor.h" |
| 26 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 27 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 28 | // TODO(joshualitt) - Break this file up during GrBatch post implementation cleanup |
| 29 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 30 | namespace { |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 31 | // TODO(joshualitt) add per vertex colors |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 32 | struct CircleVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 33 | SkPoint fPos; |
| 34 | SkPoint fOffset; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 35 | SkScalar fOuterRadius; |
| 36 | SkScalar fInnerRadius; |
| 37 | }; |
| 38 | |
| 39 | struct EllipseVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 40 | SkPoint fPos; |
| 41 | SkPoint fOffset; |
| 42 | SkPoint fOuterRadii; |
| 43 | SkPoint fInnerRadii; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 46 | struct DIEllipseVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 47 | SkPoint fPos; |
| 48 | SkPoint fOuterOffset; |
| 49 | SkPoint fInnerOffset; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 52 | inline bool circle_stays_circle(const SkMatrix& m) { |
| 53 | return m.isSimilarity(); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | /** |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 61 | * The output of this effect is a modulation of the input color and coverage for a circle. It |
| 62 | * operates in a space normalized by the circle radius (outer radius in the case of a stroke) |
| 63 | * with origin at the circle center. Two vertex attributes are used: |
| 64 | * vec2f : position in device space of the bounding geometry vertices |
| 65 | * vec4f : (p.xy, outerRad, innerRad) |
| 66 | * p is the position in the normalized space. |
| 67 | * outerRad is the outerRadius in device space. |
| 68 | * 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] | 69 | */ |
| 70 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 71 | class CircleEdgeEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 72 | public: |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 73 | static GrGeometryProcessor* Create(GrColor color, bool stroke, const SkMatrix& localMatrix) { |
| 74 | return SkNEW_ARGS(CircleEdgeEffect, (color, stroke, localMatrix)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 75 | } |
| 76 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 77 | const Attribute* inPosition() const { return fInPosition; } |
| 78 | const Attribute* inCircleEdge() const { return fInCircleEdge; } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 79 | virtual ~CircleEdgeEffect() {} |
| 80 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 81 | const char* name() const SK_OVERRIDE { return "CircleEdge"; } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 82 | |
| 83 | inline bool isStroked() const { return fStroke; } |
| 84 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | class GLProcessor : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 86 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 87 | GLProcessor(const GrGeometryProcessor&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 88 | const GrBatchTracker&) |
| 89 | : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 90 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 91 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 92 | const CircleEdgeEffect& ce = args.fGP.cast<CircleEdgeEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 93 | GrGLGPBuilder* pb = args.fPB; |
| 94 | const BatchTracker& local = args.fBT.cast<BatchTracker>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 95 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 96 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 97 | // emit attributes |
| 98 | vsBuilder->emitAttributes(ce); |
| 99 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 100 | GrGLVertToFrag v(kVec4f_GrSLType); |
| 101 | args.fPB->addVarying("CircleEdge", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 102 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), ce.inCircleEdge()->fName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 103 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 104 | // Setup pass through color |
| 105 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, |
| 106 | &fColorUniform); |
| 107 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 108 | // setup uniform viewMatrix |
| 109 | this->addUniformViewMatrix(pb); |
| 110 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 111 | // Setup position |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 112 | SetupPosition(vsBuilder, gpArgs, ce.inPosition()->fName, |
| 113 | ce.viewMatrix(), this->uViewM()); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 114 | |
| 115 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 116 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, ce.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 117 | ce.localMatrix(), args.fTransformsIn, args.fTransformsOut);; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 118 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 119 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 120 | fsBuilder->codeAppendf("float d = length(%s.xy);", v.fsIn()); |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 121 | 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] | 122 | if (ce.isStroked()) { |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 123 | fsBuilder->codeAppendf("float innerAlpha = clamp(%s.z * (d - %s.w), 0.0, 1.0);", |
| 124 | v.fsIn(), v.fsIn()); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 125 | fsBuilder->codeAppend("edgeAlpha *= innerAlpha;"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 126 | } |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 127 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 128 | fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 129 | } |
| 130 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 131 | static void GenKey(const GrGeometryProcessor& gp, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 132 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 133 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 134 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 135 | const BatchTracker& local = bt.cast<BatchTracker>(); |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 136 | const CircleEdgeEffect& circleEffect = gp.cast<CircleEdgeEffect>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 137 | uint16_t key = circleEffect.isStroked() ? 0x1 : 0x0; |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 138 | key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x2 : 0x0; |
| 139 | key |= ComputePosKey(gp.viewMatrix()) << 2; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 140 | b->add32(key << 16 | local.fInputColorType); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 141 | } |
| 142 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 143 | virtual void setData(const GrGLProgramDataManager& pdman, |
| 144 | const GrPrimitiveProcessor& gp, |
| 145 | const GrBatchTracker& bt) SK_OVERRIDE { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 146 | this->setUniformViewMatrix(pdman, gp.viewMatrix()); |
| 147 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 148 | const BatchTracker& local = bt.cast<BatchTracker>(); |
| 149 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 150 | GrGLfloat c[4]; |
| 151 | GrColorToRGBAFloat(local.fColor, c); |
| 152 | pdman.set4fv(fColorUniform, 1, c); |
| 153 | fColor = local.fColor; |
| 154 | } |
| 155 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 156 | |
| 157 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 158 | GrColor fColor; |
| 159 | UniformHandle fColorUniform; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 160 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 163 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
| 164 | const GrGLCaps& caps, |
| 165 | GrProcessorKeyBuilder* b) const SK_OVERRIDE { |
| 166 | GLProcessor::GenKey(*this, bt, caps, b); |
| 167 | } |
| 168 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 169 | virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt, |
| 170 | const GrGLCaps&) const SK_OVERRIDE { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 171 | return SkNEW_ARGS(GLProcessor, (*this, bt)); |
| 172 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 173 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 174 | void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const SK_OVERRIDE { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 175 | BatchTracker* local = bt->cast<BatchTracker>(); |
| 176 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 177 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 178 | } |
| 179 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 180 | bool onCanMakeEqual(const GrBatchTracker& m, |
| 181 | const GrGeometryProcessor& that, |
| 182 | const GrBatchTracker& t) const SK_OVERRIDE { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 183 | const BatchTracker& mine = m.cast<BatchTracker>(); |
| 184 | const BatchTracker& theirs = t.cast<BatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 185 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 186 | that, theirs.fUsesLocalCoords) && |
| 187 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 188 | theirs.fInputColorType, theirs.fColor); |
| 189 | } |
| 190 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 191 | private: |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 192 | CircleEdgeEffect(GrColor color, bool stroke, const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 193 | : INHERITED(color, SkMatrix::I(), localMatrix) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 194 | this->initClassID<CircleEdgeEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 195 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
| 196 | fInCircleEdge = &this->addVertexAttrib(Attribute("inCircleEdge", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 197 | kVec4f_GrVertexAttribType)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 198 | fStroke = stroke; |
| 199 | } |
| 200 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 201 | bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 202 | const CircleEdgeEffect& cee = other.cast<CircleEdgeEffect>(); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 203 | return cee.fStroke == fStroke; |
| 204 | } |
| 205 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 206 | void onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 207 | out->setUnknownSingleComponent(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 208 | } |
| 209 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 210 | struct BatchTracker { |
| 211 | GrGPInput fInputColorType; |
| 212 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 213 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 216 | const Attribute* fInPosition; |
| 217 | const Attribute* fInCircleEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 218 | bool fStroke; |
| 219 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 220 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 221 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 222 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 225 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleEdgeEffect); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 226 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 227 | GrGeometryProcessor* CircleEdgeEffect::TestCreate(SkRandom* random, |
| 228 | GrContext* context, |
| 229 | const GrDrawTargetCaps&, |
| 230 | GrTexture* textures[]) { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 231 | return CircleEdgeEffect::Create(GrRandomColor(random), |
| 232 | random->nextBool(), |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 233 | GrProcessorUnitTest::TestMatrix(random)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /////////////////////////////////////////////////////////////////////////////// |
| 237 | |
| 238 | /** |
| 239 | * 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] | 240 | * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii, |
| 241 | * in both x and y directions. |
| 242 | * |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 243 | * 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] | 244 | */ |
| 245 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 246 | class EllipseEdgeEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 247 | public: |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 248 | static GrGeometryProcessor* Create(GrColor color, bool stroke, const SkMatrix& localMatrix) { |
| 249 | return SkNEW_ARGS(EllipseEdgeEffect, (color, stroke, localMatrix)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 250 | } |
| 251 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 252 | virtual ~EllipseEdgeEffect() {} |
| 253 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 254 | const char* name() const SK_OVERRIDE { return "EllipseEdge"; } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 255 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 256 | const Attribute* inPosition() const { return fInPosition; } |
| 257 | const Attribute* inEllipseOffset() const { return fInEllipseOffset; } |
| 258 | const Attribute* inEllipseRadii() const { return fInEllipseRadii; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 259 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 260 | inline bool isStroked() const { return fStroke; } |
| 261 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 262 | class GLProcessor : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 263 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 264 | GLProcessor(const GrGeometryProcessor&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 265 | const GrBatchTracker&) |
| 266 | : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 267 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 268 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 269 | const EllipseEdgeEffect& ee = args.fGP.cast<EllipseEdgeEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 270 | GrGLGPBuilder* pb = args.fPB; |
| 271 | const BatchTracker& local = args.fBT.cast<BatchTracker>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 272 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 273 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 274 | // emit attributes |
| 275 | vsBuilder->emitAttributes(ee); |
| 276 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 277 | GrGLVertToFrag ellipseOffsets(kVec2f_GrSLType); |
| 278 | args.fPB->addVarying("EllipseOffsets", &ellipseOffsets); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 279 | vsBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 280 | ee.inEllipseOffset()->fName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 281 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 282 | GrGLVertToFrag ellipseRadii(kVec4f_GrSLType); |
| 283 | args.fPB->addVarying("EllipseRadii", &ellipseRadii); |
| 284 | vsBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 285 | ee.inEllipseRadii()->fName); |
| 286 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 287 | // Setup pass through color |
| 288 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, |
| 289 | &fColorUniform); |
| 290 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 291 | // setup uniform viewMatrix |
| 292 | this->addUniformViewMatrix(pb); |
| 293 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 294 | // Setup position |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 295 | SetupPosition(vsBuilder, gpArgs, ee.inPosition()->fName, |
| 296 | ee.viewMatrix(), this->uViewM()); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 297 | |
| 298 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 299 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, ee.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 300 | ee.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 301 | |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 302 | // for outer curve |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 303 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 304 | fsBuilder->codeAppendf("vec2 scaledOffset = %s*%s.xy;", ellipseOffsets.fsIn(), |
| 305 | ellipseRadii.fsIn()); |
| 306 | fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 307 | fsBuilder->codeAppendf("vec2 grad = 2.0*scaledOffset*%s.xy;", ellipseRadii.fsIn()); |
| 308 | fsBuilder->codeAppend("float grad_dot = dot(grad, grad);"); |
| 309 | |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 310 | // avoid calling inversesqrt on zero. |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 311 | fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);"); |
| 312 | fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);"); |
| 313 | 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] | 314 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 315 | // for inner curve |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 316 | if (ee.isStroked()) { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 317 | fsBuilder->codeAppendf("scaledOffset = %s*%s.zw;", |
| 318 | ellipseOffsets.fsIn(), ellipseRadii.fsIn()); |
| 319 | fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 320 | fsBuilder->codeAppendf("grad = 2.0*scaledOffset*%s.zw;", |
| 321 | ellipseRadii.fsIn()); |
| 322 | fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));"); |
| 323 | 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] | 324 | } |
| 325 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 326 | fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 327 | } |
| 328 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 329 | static void GenKey(const GrGeometryProcessor& gp, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 330 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 331 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 332 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 333 | const BatchTracker& local = bt.cast<BatchTracker>(); |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 334 | const EllipseEdgeEffect& ellipseEffect = gp.cast<EllipseEdgeEffect>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 335 | uint16_t key = ellipseEffect.isStroked() ? 0x1 : 0x0; |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 336 | key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x2 : 0x0; |
| 337 | key |= ComputePosKey(gp.viewMatrix()) << 2; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 338 | b->add32(key << 16 | local.fInputColorType); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 339 | } |
| 340 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 341 | virtual void setData(const GrGLProgramDataManager& pdman, |
| 342 | const GrPrimitiveProcessor& gp, |
| 343 | const GrBatchTracker& bt) SK_OVERRIDE { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 344 | this->setUniformViewMatrix(pdman, gp.viewMatrix()); |
| 345 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 346 | const BatchTracker& local = bt.cast<BatchTracker>(); |
| 347 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 348 | GrGLfloat c[4]; |
| 349 | GrColorToRGBAFloat(local.fColor, c); |
| 350 | pdman.set4fv(fColorUniform, 1, c); |
| 351 | fColor = local.fColor; |
| 352 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 356 | GrColor fColor; |
| 357 | UniformHandle fColorUniform; |
| 358 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 359 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 360 | }; |
| 361 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 362 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
| 363 | const GrGLCaps& caps, |
| 364 | GrProcessorKeyBuilder* b) const SK_OVERRIDE { |
| 365 | GLProcessor::GenKey(*this, bt, caps, b); |
| 366 | } |
| 367 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 368 | virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt, |
| 369 | const GrGLCaps&) const SK_OVERRIDE { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 370 | return SkNEW_ARGS(GLProcessor, (*this, bt)); |
| 371 | } |
| 372 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 373 | void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const SK_OVERRIDE { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 374 | BatchTracker* local = bt->cast<BatchTracker>(); |
| 375 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 376 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 377 | } |
| 378 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 379 | bool onCanMakeEqual(const GrBatchTracker& m, |
| 380 | const GrGeometryProcessor& that, |
| 381 | const GrBatchTracker& t) const SK_OVERRIDE { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 382 | const BatchTracker& mine = m.cast<BatchTracker>(); |
| 383 | const BatchTracker& theirs = t.cast<BatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 384 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 385 | that, theirs.fUsesLocalCoords) && |
| 386 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 387 | theirs.fInputColorType, theirs.fColor); |
| 388 | } |
| 389 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 390 | private: |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 391 | EllipseEdgeEffect(GrColor color, bool stroke, const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 392 | : INHERITED(color, SkMatrix::I(), localMatrix) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 393 | this->initClassID<EllipseEdgeEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 394 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
| 395 | fInEllipseOffset = &this->addVertexAttrib(Attribute("inEllipseOffset", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 396 | kVec2f_GrVertexAttribType)); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 397 | fInEllipseRadii = &this->addVertexAttrib(Attribute("inEllipseRadii", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 398 | kVec4f_GrVertexAttribType)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 399 | fStroke = stroke; |
| 400 | } |
| 401 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 402 | bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 403 | const EllipseEdgeEffect& eee = other.cast<EllipseEdgeEffect>(); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 404 | return eee.fStroke == fStroke; |
| 405 | } |
| 406 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 407 | void onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 408 | out->setUnknownSingleComponent(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 409 | } |
| 410 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 411 | struct BatchTracker { |
| 412 | GrGPInput fInputColorType; |
| 413 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 414 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 415 | }; |
| 416 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 417 | const Attribute* fInPosition; |
| 418 | const Attribute* fInEllipseOffset; |
| 419 | const Attribute* fInEllipseRadii; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 420 | bool fStroke; |
| 421 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 422 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 423 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 424 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 425 | }; |
| 426 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 427 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseEdgeEffect); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 428 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 429 | GrGeometryProcessor* EllipseEdgeEffect::TestCreate(SkRandom* random, |
| 430 | GrContext* context, |
| 431 | const GrDrawTargetCaps&, |
| 432 | GrTexture* textures[]) { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 433 | return EllipseEdgeEffect::Create(GrRandomColor(random), |
| 434 | random->nextBool(), |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 435 | GrProcessorUnitTest::TestMatrix(random)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /////////////////////////////////////////////////////////////////////////////// |
| 439 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 440 | /** |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 441 | * 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] | 442 | * specified as a 2D offset from center for both the outer and inner paths (if stroked). The |
| 443 | * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by |
| 444 | * using differentials. |
| 445 | * |
| 446 | * The result is device-independent and can be used with any affine matrix. |
| 447 | */ |
| 448 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 449 | class DIEllipseEdgeEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 450 | public: |
| 451 | enum Mode { kStroke = 0, kHairline, kFill }; |
| 452 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 453 | static GrGeometryProcessor* Create(GrColor color, const SkMatrix& viewMatrix, Mode mode) { |
| 454 | return SkNEW_ARGS(DIEllipseEdgeEffect, (color, viewMatrix, mode)); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 455 | } |
| 456 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 457 | virtual ~DIEllipseEdgeEffect() {} |
| 458 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 459 | const char* name() const SK_OVERRIDE { return "DIEllipseEdge"; } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 460 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 461 | const Attribute* inPosition() const { return fInPosition; } |
| 462 | const Attribute* inEllipseOffsets0() const { return fInEllipseOffsets0; } |
| 463 | const Attribute* inEllipseOffsets1() const { return fInEllipseOffsets1; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 464 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 465 | inline Mode getMode() const { return fMode; } |
| 466 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 467 | class GLProcessor : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 468 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 469 | GLProcessor(const GrGeometryProcessor&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 470 | const GrBatchTracker&) |
| 471 | : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 472 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 473 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 474 | const DIEllipseEdgeEffect& ee = args.fGP.cast<DIEllipseEdgeEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 475 | GrGLGPBuilder* pb = args.fPB; |
| 476 | const BatchTracker& local = args.fBT.cast<BatchTracker>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 477 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 478 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 479 | // emit attributes |
| 480 | vsBuilder->emitAttributes(ee); |
| 481 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 482 | GrGLVertToFrag offsets0(kVec2f_GrSLType); |
| 483 | args.fPB->addVarying("EllipseOffsets0", &offsets0); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 484 | vsBuilder->codeAppendf("%s = %s;", offsets0.vsOut(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 485 | ee.inEllipseOffsets0()->fName); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 486 | |
| 487 | GrGLVertToFrag offsets1(kVec2f_GrSLType); |
| 488 | args.fPB->addVarying("EllipseOffsets1", &offsets1); |
| 489 | vsBuilder->codeAppendf("%s = %s;", offsets1.vsOut(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 490 | ee.inEllipseOffsets1()->fName); |
| 491 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 492 | // Setup pass through color |
| 493 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, |
| 494 | &fColorUniform); |
| 495 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 496 | // setup uniform viewMatrix |
| 497 | this->addUniformViewMatrix(pb); |
| 498 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 499 | // Setup position |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 500 | SetupPosition(vsBuilder, gpArgs, ee.inPosition()->fName, |
| 501 | ee.viewMatrix(), this->uViewM()); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 502 | |
| 503 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 504 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, ee.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 505 | ee.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 506 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 507 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 508 | SkAssertResult(fsBuilder->enableFeature( |
| 509 | GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 510 | // for outer curve |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 511 | fsBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn()); |
| 512 | fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 513 | fsBuilder->codeAppendf("vec2 duvdx = dFdx(%s);", offsets0.fsIn()); |
| 514 | fsBuilder->codeAppendf("vec2 duvdy = dFdy(%s);", offsets0.fsIn()); |
| 515 | fsBuilder->codeAppendf("vec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y," |
| 516 | " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);", |
| 517 | offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 518 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 519 | fsBuilder->codeAppend("float grad_dot = dot(grad, grad);"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 520 | // avoid calling inversesqrt on zero. |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 521 | fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);"); |
| 522 | fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);"); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 523 | if (kHairline == ee.getMode()) { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 524 | // can probably do this with one step |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 525 | fsBuilder->codeAppend("float edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);"); |
| 526 | 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] | 527 | } else { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 528 | 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] | 529 | } |
| 530 | |
| 531 | // for inner curve |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 532 | if (kStroke == ee.getMode()) { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 533 | fsBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn()); |
| 534 | fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 535 | fsBuilder->codeAppendf("duvdx = dFdx(%s);", offsets1.fsIn()); |
| 536 | fsBuilder->codeAppendf("duvdy = dFdy(%s);", offsets1.fsIn()); |
| 537 | fsBuilder->codeAppendf("grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y," |
| 538 | " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);", |
| 539 | offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(), |
| 540 | offsets1.fsIn()); |
| 541 | fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));"); |
| 542 | 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] | 543 | } |
| 544 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 545 | fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 546 | } |
| 547 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 548 | static void GenKey(const GrGeometryProcessor& gp, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 549 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 550 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 551 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 552 | const BatchTracker& local = bt.cast<BatchTracker>(); |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 553 | const DIEllipseEdgeEffect& ellipseEffect = gp.cast<DIEllipseEdgeEffect>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 554 | uint16_t key = ellipseEffect.getMode(); |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 555 | key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 << 8 : 0x0; |
| 556 | key |= ComputePosKey(gp.viewMatrix()) << 9; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 557 | b->add32(key << 16 | local.fInputColorType); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 558 | } |
| 559 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 560 | virtual void setData(const GrGLProgramDataManager& pdman, |
| 561 | const GrPrimitiveProcessor& gp, |
| 562 | const GrBatchTracker& bt) SK_OVERRIDE { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 563 | this->setUniformViewMatrix(pdman, gp.viewMatrix()); |
| 564 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 565 | const BatchTracker& local = bt.cast<BatchTracker>(); |
| 566 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 567 | GrGLfloat c[4]; |
| 568 | GrColorToRGBAFloat(local.fColor, c); |
| 569 | pdman.set4fv(fColorUniform, 1, c); |
| 570 | fColor = local.fColor; |
| 571 | } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 575 | GrColor fColor; |
| 576 | UniformHandle fColorUniform; |
| 577 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 578 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 579 | }; |
| 580 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 581 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
| 582 | const GrGLCaps& caps, |
| 583 | GrProcessorKeyBuilder* b) const SK_OVERRIDE { |
| 584 | GLProcessor::GenKey(*this, bt, caps, b); |
| 585 | } |
| 586 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 587 | virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt, |
| 588 | const GrGLCaps&) const SK_OVERRIDE { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 589 | return SkNEW_ARGS(GLProcessor, (*this, bt)); |
| 590 | } |
| 591 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 592 | void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const SK_OVERRIDE { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 593 | BatchTracker* local = bt->cast<BatchTracker>(); |
| 594 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 595 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 596 | } |
| 597 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 598 | bool onCanMakeEqual(const GrBatchTracker& m, |
| 599 | const GrGeometryProcessor& that, |
| 600 | const GrBatchTracker& t) const SK_OVERRIDE { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 601 | const BatchTracker& mine = m.cast<BatchTracker>(); |
| 602 | const BatchTracker& theirs = t.cast<BatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 603 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 604 | that, theirs.fUsesLocalCoords) && |
| 605 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 606 | theirs.fInputColorType, theirs.fColor); |
| 607 | } |
| 608 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 609 | private: |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 610 | DIEllipseEdgeEffect(GrColor color, const SkMatrix& viewMatrix, Mode mode) |
| 611 | : INHERITED(color, viewMatrix) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 612 | this->initClassID<DIEllipseEdgeEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 613 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
| 614 | fInEllipseOffsets0 = &this->addVertexAttrib(Attribute("inEllipseOffsets0", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 615 | kVec2f_GrVertexAttribType)); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 616 | fInEllipseOffsets1 = &this->addVertexAttrib(Attribute("inEllipseOffsets1", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 617 | kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 618 | fMode = mode; |
| 619 | } |
| 620 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 621 | bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 622 | const DIEllipseEdgeEffect& eee = other.cast<DIEllipseEdgeEffect>(); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 623 | return eee.fMode == fMode; |
| 624 | } |
| 625 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 626 | void onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 627 | out->setUnknownSingleComponent(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 628 | } |
| 629 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 630 | struct BatchTracker { |
| 631 | GrGPInput fInputColorType; |
| 632 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 633 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 634 | }; |
| 635 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 636 | const Attribute* fInPosition; |
| 637 | const Attribute* fInEllipseOffsets0; |
| 638 | const Attribute* fInEllipseOffsets1; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 639 | Mode fMode; |
| 640 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 641 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 642 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 643 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 644 | }; |
| 645 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 646 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseEdgeEffect); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 647 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 648 | GrGeometryProcessor* DIEllipseEdgeEffect::TestCreate(SkRandom* random, |
| 649 | GrContext* context, |
| 650 | const GrDrawTargetCaps&, |
| 651 | GrTexture* textures[]) { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 652 | return DIEllipseEdgeEffect::Create(GrRandomColor(random), |
| 653 | GrProcessorUnitTest::TestMatrix(random), |
| 654 | (Mode)(random->nextRangeU(0,2))); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /////////////////////////////////////////////////////////////////////////////// |
| 658 | |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 659 | void GrOvalRenderer::reset() { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 660 | SkSafeSetNull(fRRectIndexBuffer); |
joshualitt | 58a6544 | 2014-10-22 20:53:24 -0700 | [diff] [blame] | 661 | SkSafeSetNull(fStrokeRRectIndexBuffer); |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 662 | } |
| 663 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 664 | bool GrOvalRenderer::drawOval(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 665 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 666 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 667 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 668 | bool useAA, |
| 669 | const SkRect& oval, |
| 670 | const SkStrokeRec& stroke) |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 671 | { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 672 | bool useCoverageAA = useAA && |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 673 | !pipelineBuilder->getRenderTarget()->isMultisampled() && |
| 674 | pipelineBuilder->canUseFracCoveragePrimProc(color, *target->caps()); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 675 | |
| 676 | if (!useCoverageAA) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 677 | return false; |
| 678 | } |
| 679 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 680 | // we can draw circles |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 681 | if (SkScalarNearlyEqual(oval.width(), oval.height()) && circle_stays_circle(viewMatrix)) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 682 | this->drawCircle(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval, stroke); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 683 | // if we have shader derivative support, render as device-independent |
| 684 | } else if (target->caps()->shaderDerivativeSupport()) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 685 | return this->drawDIEllipse(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 686 | stroke); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 687 | // otherwise axis-aligned ellipses only |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 688 | } else if (viewMatrix.rectStaysRect()) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 689 | return this->drawEllipse(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval, |
| 690 | stroke); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 691 | } else { |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | return true; |
| 696 | } |
| 697 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 698 | /////////////////////////////////////////////////////////////////////////////// |
| 699 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 700 | class CircleBatch : public GrBatch { |
| 701 | public: |
| 702 | struct Geometry { |
| 703 | GrColor fColor; |
| 704 | SkMatrix fViewMatrix; |
| 705 | SkScalar fInnerRadius; |
| 706 | SkScalar fOuterRadius; |
| 707 | bool fStroke; |
| 708 | SkRect fDevBounds; |
| 709 | }; |
| 710 | |
| 711 | static GrBatch* Create(const Geometry& geometry) { |
| 712 | return SkNEW_ARGS(CircleBatch, (geometry)); |
| 713 | } |
| 714 | |
| 715 | const char* name() const SK_OVERRIDE { return "CircleBatch"; } |
| 716 | |
| 717 | void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 718 | // When this is called on a batch, there is only one geometry bundle |
| 719 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 720 | } |
| 721 | |
| 722 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 723 | out->setUnknownSingleComponent(); |
| 724 | } |
| 725 | |
| 726 | void initBatchOpt(const GrBatchOpt& batchOpt) { |
| 727 | fBatchOpt = batchOpt; |
| 728 | } |
| 729 | |
| 730 | void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE { |
| 731 | // Handle any color overrides |
| 732 | if (init.fColorIgnored) { |
| 733 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 734 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 735 | fGeoData[0].fColor = init.fOverrideColor; |
| 736 | } |
| 737 | |
| 738 | // setup batch properties |
| 739 | fBatch.fColorIgnored = init.fColorIgnored; |
| 740 | fBatch.fColor = fGeoData[0].fColor; |
| 741 | fBatch.fStroke = fGeoData[0].fStroke; |
| 742 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 743 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 744 | } |
| 745 | |
| 746 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE { |
| 747 | SkMatrix invert; |
| 748 | if (!this->viewMatrix().invert(&invert)) { |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | // Setup geometry processor |
| 753 | SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(), |
| 754 | this->stroke(), |
| 755 | invert)); |
| 756 | |
| 757 | batchTarget->initDraw(gp, pipeline); |
| 758 | |
| 759 | // TODO this is hacky, but the only way we have to initialize the GP is to use the |
| 760 | // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch |
| 761 | // everywhere we can remove this nastiness |
| 762 | GrPipelineInfo init; |
| 763 | init.fColorIgnored = fBatch.fColorIgnored; |
| 764 | init.fOverrideColor = GrColor_ILLEGAL; |
| 765 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 766 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 767 | gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 768 | |
| 769 | int instanceCount = fGeoData.count(); |
| 770 | int vertexCount = kVertsPerCircle * instanceCount; |
| 771 | size_t vertexStride = gp->getVertexStride(); |
| 772 | SkASSERT(vertexStride == sizeof(CircleVertex)); |
| 773 | |
| 774 | const GrVertexBuffer* vertexBuffer; |
| 775 | int firstVertex; |
| 776 | |
| 777 | void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 778 | vertexCount, |
| 779 | &vertexBuffer, |
| 780 | &firstVertex); |
| 781 | |
| 782 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(vertices); |
| 783 | |
| 784 | for (int i = 0; i < instanceCount; i++) { |
| 785 | Geometry& args = fGeoData[i]; |
| 786 | |
| 787 | SkScalar innerRadius = args.fInnerRadius; |
| 788 | SkScalar outerRadius = args.fOuterRadius; |
| 789 | |
| 790 | const SkRect& bounds = args.fDevBounds; |
| 791 | |
| 792 | // The inner radius in the vertex data must be specified in normalized space. |
| 793 | innerRadius = innerRadius / outerRadius; |
| 794 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 795 | verts[0].fOffset = SkPoint::Make(-1, -1); |
| 796 | verts[0].fOuterRadius = outerRadius; |
| 797 | verts[0].fInnerRadius = innerRadius; |
| 798 | |
| 799 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 800 | verts[1].fOffset = SkPoint::Make(-1, 1); |
| 801 | verts[1].fOuterRadius = outerRadius; |
| 802 | verts[1].fInnerRadius = innerRadius; |
| 803 | |
| 804 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 805 | verts[2].fOffset = SkPoint::Make(1, 1); |
| 806 | verts[2].fOuterRadius = outerRadius; |
| 807 | verts[2].fInnerRadius = innerRadius; |
| 808 | |
| 809 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 810 | verts[3].fOffset = SkPoint::Make(1, -1); |
| 811 | verts[3].fOuterRadius = outerRadius; |
| 812 | verts[3].fInnerRadius = innerRadius; |
| 813 | |
| 814 | verts += kVertsPerCircle; |
| 815 | } |
| 816 | |
| 817 | const GrIndexBuffer* quadIndexBuffer = batchTarget->quadIndexBuffer(); |
| 818 | |
| 819 | GrDrawTarget::DrawInfo drawInfo; |
| 820 | drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); |
| 821 | drawInfo.setStartVertex(0); |
| 822 | drawInfo.setStartIndex(0); |
| 823 | drawInfo.setVerticesPerInstance(kVertsPerCircle); |
| 824 | drawInfo.setIndicesPerInstance(kIndicesPerCircle); |
| 825 | drawInfo.adjustStartVertex(firstVertex); |
| 826 | drawInfo.setVertexBuffer(vertexBuffer); |
| 827 | drawInfo.setIndexBuffer(quadIndexBuffer); |
| 828 | |
| 829 | int maxInstancesPerDraw = quadIndexBuffer->maxQuads(); |
| 830 | |
| 831 | while (instanceCount) { |
| 832 | drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw)); |
| 833 | drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance()); |
| 834 | drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance()); |
| 835 | |
| 836 | batchTarget->draw(drawInfo); |
| 837 | |
| 838 | drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount()); |
| 839 | instanceCount -= drawInfo.instanceCount(); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 844 | |
| 845 | private: |
| 846 | CircleBatch(const Geometry& geometry) { |
| 847 | this->initClassID<CircleBatch>(); |
| 848 | fGeoData.push_back(geometry); |
| 849 | } |
| 850 | |
| 851 | bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { |
| 852 | CircleBatch* that = t->cast<CircleBatch>(); |
| 853 | |
| 854 | // TODO use vertex color to avoid breaking batches |
| 855 | if (this->color() != that->color()) { |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | if (this->stroke() != that->stroke()) { |
| 860 | return false; |
| 861 | } |
| 862 | |
| 863 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 864 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | GrColor color() const { return fBatch.fColor; } |
| 873 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 874 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 875 | bool stroke() const { return fBatch.fStroke; } |
| 876 | |
| 877 | struct BatchTracker { |
| 878 | GrColor fColor; |
| 879 | bool fStroke; |
| 880 | bool fUsesLocalCoords; |
| 881 | bool fColorIgnored; |
| 882 | bool fCoverageIgnored; |
| 883 | }; |
| 884 | |
| 885 | static const int kVertsPerCircle = 4; |
| 886 | static const int kIndicesPerCircle = 6; |
| 887 | |
| 888 | GrBatchOpt fBatchOpt; |
| 889 | BatchTracker fBatch; |
| 890 | SkSTArray<1, Geometry, true> fGeoData; |
| 891 | }; |
| 892 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 893 | void GrOvalRenderer::drawCircle(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 894 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 895 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 896 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 897 | bool useCoverageAA, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 898 | const SkRect& circle, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 899 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 900 | SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 901 | viewMatrix.mapPoints(¢er, 1); |
| 902 | SkScalar radius = viewMatrix.mapRadius(SkScalarHalf(circle.width())); |
| 903 | SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 904 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 905 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 906 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 907 | SkStrokeRec::kHairline_Style == style; |
| 908 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 909 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 910 | SkScalar innerRadius = 0.0f; |
| 911 | SkScalar outerRadius = radius; |
| 912 | SkScalar halfWidth = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 913 | if (hasStroke) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 914 | if (SkScalarNearlyZero(strokeWidth)) { |
| 915 | halfWidth = SK_ScalarHalf; |
| 916 | } else { |
| 917 | halfWidth = SkScalarHalf(strokeWidth); |
| 918 | } |
| 919 | |
| 920 | outerRadius += halfWidth; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 921 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 922 | innerRadius = radius - halfWidth; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 926 | // The radii are outset for two reasons. First, it allows the shader to simply perform simpler |
| 927 | // computation because the computed alpha is zero, rather than 50%, at the radius. |
| 928 | // Second, the outer radius is used to compute the verts of the bounding box that is rendered |
| 929 | // 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] | 930 | outerRadius += SK_ScalarHalf; |
| 931 | innerRadius -= SK_ScalarHalf; |
| 932 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 933 | SkRect bounds = SkRect::MakeLTRB( |
bsalomon@google.com | 58e30fe | 2013-04-01 19:01:20 +0000 | [diff] [blame] | 934 | center.fX - outerRadius, |
| 935 | center.fY - outerRadius, |
| 936 | center.fX + outerRadius, |
| 937 | center.fY + outerRadius |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 938 | ); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 939 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 940 | CircleBatch::Geometry geometry; |
| 941 | geometry.fViewMatrix = viewMatrix; |
| 942 | geometry.fColor = color; |
| 943 | geometry.fInnerRadius = innerRadius; |
| 944 | geometry.fOuterRadius = outerRadius; |
| 945 | geometry.fStroke = isStrokeOnly && innerRadius > 0; |
| 946 | geometry.fDevBounds = bounds; |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 947 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 948 | SkAutoTUnref<GrBatch> batch(CircleBatch::Create(geometry)); |
| 949 | target->drawBatch(pipelineBuilder, batch, &bounds); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 950 | } |
| 951 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 952 | /////////////////////////////////////////////////////////////////////////////// |
| 953 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 954 | class EllipseBatch : public GrBatch { |
| 955 | public: |
| 956 | struct Geometry { |
| 957 | GrColor fColor; |
| 958 | SkMatrix fViewMatrix; |
| 959 | SkScalar fXRadius; |
| 960 | SkScalar fYRadius; |
| 961 | SkScalar fInnerXRadius; |
| 962 | SkScalar fInnerYRadius; |
| 963 | bool fStroke; |
| 964 | SkRect fDevBounds; |
| 965 | }; |
| 966 | |
| 967 | static GrBatch* Create(const Geometry& geometry) { |
| 968 | return SkNEW_ARGS(EllipseBatch, (geometry)); |
| 969 | } |
| 970 | |
| 971 | const char* name() const SK_OVERRIDE { return "EllipseBatch"; } |
| 972 | |
| 973 | void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 974 | // When this is called on a batch, there is only one geometry bundle |
| 975 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 976 | } |
| 977 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 978 | out->setUnknownSingleComponent(); |
| 979 | } |
| 980 | |
| 981 | void initBatchOpt(const GrBatchOpt& batchOpt) { |
| 982 | fBatchOpt = batchOpt; |
| 983 | } |
| 984 | |
| 985 | void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE { |
| 986 | // Handle any color overrides |
| 987 | if (init.fColorIgnored) { |
| 988 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 989 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 990 | fGeoData[0].fColor = init.fOverrideColor; |
| 991 | } |
| 992 | |
| 993 | // setup batch properties |
| 994 | fBatch.fColorIgnored = init.fColorIgnored; |
| 995 | fBatch.fColor = fGeoData[0].fColor; |
| 996 | fBatch.fStroke = fGeoData[0].fStroke; |
| 997 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 998 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 999 | } |
| 1000 | |
| 1001 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE { |
| 1002 | SkMatrix invert; |
| 1003 | if (!this->viewMatrix().invert(&invert)) { |
| 1004 | return; |
| 1005 | } |
| 1006 | |
| 1007 | // Setup geometry processor |
| 1008 | SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(), |
| 1009 | this->stroke(), |
| 1010 | invert)); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1011 | |
| 1012 | batchTarget->initDraw(gp, pipeline); |
| 1013 | |
| 1014 | // TODO this is hacky, but the only way we have to initialize the GP is to use the |
| 1015 | // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch |
| 1016 | // everywhere we can remove this nastiness |
| 1017 | GrPipelineInfo init; |
| 1018 | init.fColorIgnored = fBatch.fColorIgnored; |
| 1019 | init.fOverrideColor = GrColor_ILLEGAL; |
| 1020 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 1021 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 1022 | gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 1023 | |
| 1024 | int instanceCount = fGeoData.count(); |
| 1025 | int vertexCount = kVertsPerEllipse * instanceCount; |
| 1026 | size_t vertexStride = gp->getVertexStride(); |
joshualitt | 19e0058 | 2015-02-11 17:36:30 -0800 | [diff] [blame^] | 1027 | SkASSERT(vertexStride == sizeof(EllipseVertex)); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1028 | |
| 1029 | const GrVertexBuffer* vertexBuffer; |
| 1030 | int firstVertex; |
| 1031 | |
| 1032 | void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 1033 | vertexCount, |
| 1034 | &vertexBuffer, |
| 1035 | &firstVertex); |
| 1036 | |
| 1037 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(vertices); |
| 1038 | |
| 1039 | for (int i = 0; i < instanceCount; i++) { |
| 1040 | Geometry& args = fGeoData[i]; |
| 1041 | |
| 1042 | SkScalar xRadius = args.fXRadius; |
| 1043 | SkScalar yRadius = args.fYRadius; |
| 1044 | |
| 1045 | // Compute the reciprocals of the radii here to save time in the shader |
| 1046 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 1047 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
| 1048 | SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius); |
| 1049 | SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius); |
| 1050 | |
| 1051 | const SkRect& bounds = args.fDevBounds; |
| 1052 | |
| 1053 | // The inner radius in the vertex data must be specified in normalized space. |
| 1054 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 1055 | verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius); |
| 1056 | verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1057 | verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1058 | |
| 1059 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 1060 | verts[1].fOffset = SkPoint::Make(-xRadius, yRadius); |
| 1061 | verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1062 | verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1063 | |
| 1064 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 1065 | verts[2].fOffset = SkPoint::Make(xRadius, yRadius); |
| 1066 | verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1067 | verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1068 | |
| 1069 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 1070 | verts[3].fOffset = SkPoint::Make(xRadius, -yRadius); |
| 1071 | verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1072 | verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1073 | |
| 1074 | verts += kVertsPerEllipse; |
| 1075 | } |
| 1076 | |
| 1077 | const GrIndexBuffer* quadIndexBuffer = batchTarget->quadIndexBuffer(); |
| 1078 | |
| 1079 | GrDrawTarget::DrawInfo drawInfo; |
| 1080 | drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); |
| 1081 | drawInfo.setStartVertex(0); |
| 1082 | drawInfo.setStartIndex(0); |
| 1083 | drawInfo.setVerticesPerInstance(kVertsPerEllipse); |
| 1084 | drawInfo.setIndicesPerInstance(kIndicesPerEllipse); |
| 1085 | drawInfo.adjustStartVertex(firstVertex); |
| 1086 | drawInfo.setVertexBuffer(vertexBuffer); |
| 1087 | drawInfo.setIndexBuffer(quadIndexBuffer); |
| 1088 | |
| 1089 | int maxInstancesPerDraw = quadIndexBuffer->maxQuads(); |
| 1090 | |
| 1091 | while (instanceCount) { |
| 1092 | drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw)); |
| 1093 | drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance()); |
| 1094 | drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance()); |
| 1095 | |
| 1096 | batchTarget->draw(drawInfo); |
| 1097 | |
| 1098 | drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount()); |
| 1099 | instanceCount -= drawInfo.instanceCount(); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1104 | |
| 1105 | private: |
| 1106 | EllipseBatch(const Geometry& geometry) { |
| 1107 | this->initClassID<EllipseBatch>(); |
| 1108 | fGeoData.push_back(geometry); |
| 1109 | } |
| 1110 | |
| 1111 | bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { |
| 1112 | EllipseBatch* that = t->cast<EllipseBatch>(); |
| 1113 | |
| 1114 | // TODO use vertex color to avoid breaking batches |
| 1115 | if (this->color() != that->color()) { |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
| 1119 | if (this->stroke() != that->stroke()) { |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
| 1123 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 1124 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
| 1128 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 1129 | return true; |
| 1130 | } |
| 1131 | |
| 1132 | GrColor color() const { return fBatch.fColor; } |
| 1133 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1134 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1135 | bool stroke() const { return fBatch.fStroke; } |
| 1136 | |
| 1137 | struct BatchTracker { |
| 1138 | GrColor fColor; |
| 1139 | bool fStroke; |
| 1140 | bool fUsesLocalCoords; |
| 1141 | bool fColorIgnored; |
| 1142 | bool fCoverageIgnored; |
| 1143 | }; |
| 1144 | |
| 1145 | static const int kVertsPerEllipse = 4; |
| 1146 | static const int kIndicesPerEllipse = 6; |
| 1147 | |
| 1148 | GrBatchOpt fBatchOpt; |
| 1149 | BatchTracker fBatch; |
| 1150 | SkSTArray<1, Geometry, true> fGeoData; |
| 1151 | }; |
| 1152 | |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 1153 | bool GrOvalRenderer::drawEllipse(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1154 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1155 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1156 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1157 | bool useCoverageAA, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 1158 | const SkRect& ellipse, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1159 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1160 | #ifdef SK_DEBUG |
| 1161 | { |
| 1162 | // we should have checked for this previously |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1163 | bool isAxisAlignedEllipse = viewMatrix.rectStaysRect(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1164 | SkASSERT(useCoverageAA && isAxisAlignedEllipse); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1165 | } |
| 1166 | #endif |
| 1167 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1168 | // 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] | 1169 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1170 | viewMatrix.mapPoints(¢er, 1); |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1171 | SkScalar ellipseXRadius = SkScalarHalf(ellipse.width()); |
| 1172 | SkScalar ellipseYRadius = SkScalarHalf(ellipse.height()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1173 | SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius + |
| 1174 | viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius); |
| 1175 | SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius + |
| 1176 | viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1177 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1178 | // do (potentially) anisotropic mapping of stroke |
| 1179 | SkVector scaledStroke; |
| 1180 | SkScalar strokeWidth = stroke.getWidth(); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1181 | scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] + |
| 1182 | viewMatrix[SkMatrix::kMSkewY])); |
| 1183 | scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] + |
| 1184 | viewMatrix[SkMatrix::kMScaleY])); |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1185 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1186 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1187 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 1188 | SkStrokeRec::kHairline_Style == style; |
| 1189 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1190 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1191 | SkScalar innerXRadius = 0; |
| 1192 | SkScalar innerYRadius = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1193 | if (hasStroke) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1194 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 1195 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 1196 | } else { |
| 1197 | scaledStroke.scale(SK_ScalarHalf); |
| 1198 | } |
| 1199 | |
| 1200 | // we only handle thick strokes for near-circular ellipses |
| 1201 | if (scaledStroke.length() > SK_ScalarHalf && |
| 1202 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1207 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 1208 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | // 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] | 1213 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1214 | innerXRadius = xRadius - scaledStroke.fX; |
| 1215 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | xRadius += scaledStroke.fX; |
| 1219 | yRadius += scaledStroke.fY; |
| 1220 | } |
| 1221 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1222 | // 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] | 1223 | // 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] | 1224 | // TODO: Consider if we should use sqrt(2)/2 instead |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 1225 | xRadius += SK_ScalarHalf; |
| 1226 | yRadius += SK_ScalarHalf; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1227 | |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 1228 | SkRect bounds = SkRect::MakeLTRB( |
| 1229 | center.fX - xRadius, |
| 1230 | center.fY - yRadius, |
| 1231 | center.fX + xRadius, |
| 1232 | center.fY + yRadius |
| 1233 | ); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1234 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1235 | EllipseBatch::Geometry geometry; |
| 1236 | geometry.fViewMatrix = viewMatrix; |
| 1237 | geometry.fColor = color; |
| 1238 | geometry.fXRadius = xRadius; |
| 1239 | geometry.fYRadius = yRadius; |
| 1240 | geometry.fInnerXRadius = innerXRadius; |
| 1241 | geometry.fInnerYRadius = innerYRadius; |
| 1242 | geometry.fStroke = isStrokeOnly && innerXRadius > 0 && innerYRadius > 0; |
| 1243 | geometry.fDevBounds = bounds; |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 1244 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1245 | SkAutoTUnref<GrBatch> batch(EllipseBatch::Create(geometry)); |
| 1246 | target->drawBatch(pipelineBuilder, batch, &bounds); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 1247 | |
| 1248 | return true; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1249 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1250 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1251 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 1252 | |
| 1253 | class DIEllipseBatch : public GrBatch { |
| 1254 | public: |
| 1255 | struct Geometry { |
| 1256 | GrColor fColor; |
| 1257 | SkMatrix fViewMatrix; |
| 1258 | SkScalar fXRadius; |
| 1259 | SkScalar fYRadius; |
| 1260 | SkScalar fInnerXRadius; |
| 1261 | SkScalar fInnerYRadius; |
| 1262 | SkScalar fGeoDx; |
| 1263 | SkScalar fGeoDy; |
| 1264 | DIEllipseEdgeEffect::Mode fMode; |
| 1265 | SkRect fDevBounds; |
| 1266 | }; |
| 1267 | |
| 1268 | static GrBatch* Create(const Geometry& geometry) { |
| 1269 | return SkNEW_ARGS(DIEllipseBatch, (geometry)); |
| 1270 | } |
| 1271 | |
| 1272 | const char* name() const SK_OVERRIDE { return "DIEllipseBatch"; } |
| 1273 | |
| 1274 | void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 1275 | // When this is called on a batch, there is only one geometry bundle |
| 1276 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1277 | } |
| 1278 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 1279 | out->setUnknownSingleComponent(); |
| 1280 | } |
| 1281 | |
| 1282 | void initBatchOpt(const GrBatchOpt& batchOpt) { |
| 1283 | fBatchOpt = batchOpt; |
| 1284 | } |
| 1285 | |
| 1286 | void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE { |
| 1287 | // Handle any color overrides |
| 1288 | if (init.fColorIgnored) { |
| 1289 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 1290 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 1291 | fGeoData[0].fColor = init.fOverrideColor; |
| 1292 | } |
| 1293 | |
| 1294 | // setup batch properties |
| 1295 | fBatch.fColorIgnored = init.fColorIgnored; |
| 1296 | fBatch.fColor = fGeoData[0].fColor; |
| 1297 | fBatch.fMode = fGeoData[0].fMode; |
| 1298 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 1299 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 1300 | } |
| 1301 | |
| 1302 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE { |
| 1303 | // Setup geometry processor |
| 1304 | SkAutoTUnref<GrGeometryProcessor> gp(DIEllipseEdgeEffect::Create(this->color(), |
| 1305 | this->viewMatrix(), |
| 1306 | this->mode())); |
| 1307 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1308 | batchTarget->initDraw(gp, pipeline); |
| 1309 | |
| 1310 | // TODO this is hacky, but the only way we have to initialize the GP is to use the |
| 1311 | // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch |
| 1312 | // everywhere we can remove this nastiness |
| 1313 | GrPipelineInfo init; |
| 1314 | init.fColorIgnored = fBatch.fColorIgnored; |
| 1315 | init.fOverrideColor = GrColor_ILLEGAL; |
| 1316 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 1317 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 1318 | gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 1319 | |
| 1320 | int instanceCount = fGeoData.count(); |
| 1321 | int vertexCount = kVertsPerEllipse * instanceCount; |
| 1322 | size_t vertexStride = gp->getVertexStride(); |
joshualitt | 19e0058 | 2015-02-11 17:36:30 -0800 | [diff] [blame^] | 1323 | SkASSERT(vertexStride == sizeof(DIEllipseVertex)); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1324 | |
| 1325 | const GrVertexBuffer* vertexBuffer; |
| 1326 | int firstVertex; |
| 1327 | |
| 1328 | void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 1329 | vertexCount, |
| 1330 | &vertexBuffer, |
| 1331 | &firstVertex); |
| 1332 | |
| 1333 | DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(vertices); |
| 1334 | |
| 1335 | for (int i = 0; i < instanceCount; i++) { |
| 1336 | Geometry& args = fGeoData[i]; |
| 1337 | |
| 1338 | SkScalar xRadius = args.fXRadius; |
| 1339 | SkScalar yRadius = args.fYRadius; |
| 1340 | |
| 1341 | const SkRect& bounds = args.fDevBounds; |
| 1342 | |
| 1343 | // This adjusts the "radius" to include the half-pixel border |
| 1344 | SkScalar offsetDx = SkScalarDiv(args.fGeoDx, xRadius); |
| 1345 | SkScalar offsetDy = SkScalarDiv(args.fGeoDy, yRadius); |
| 1346 | |
| 1347 | SkScalar innerRatioX = SkScalarDiv(xRadius, args.fInnerXRadius); |
| 1348 | SkScalar innerRatioY = SkScalarDiv(yRadius, args.fInnerYRadius); |
| 1349 | |
| 1350 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 1351 | verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy); |
| 1352 | verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy); |
| 1353 | |
| 1354 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 1355 | verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy); |
| 1356 | verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy); |
| 1357 | |
| 1358 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 1359 | verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy); |
| 1360 | verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy); |
| 1361 | |
| 1362 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 1363 | verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy); |
| 1364 | verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy); |
| 1365 | |
| 1366 | verts += kVertsPerEllipse; |
| 1367 | } |
| 1368 | |
| 1369 | const GrIndexBuffer* quadIndexBuffer = batchTarget->quadIndexBuffer(); |
| 1370 | |
| 1371 | GrDrawTarget::DrawInfo drawInfo; |
| 1372 | drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); |
| 1373 | drawInfo.setStartVertex(0); |
| 1374 | drawInfo.setStartIndex(0); |
| 1375 | drawInfo.setVerticesPerInstance(kVertsPerEllipse); |
| 1376 | drawInfo.setIndicesPerInstance(kIndicesPerEllipse); |
| 1377 | drawInfo.adjustStartVertex(firstVertex); |
| 1378 | drawInfo.setVertexBuffer(vertexBuffer); |
| 1379 | drawInfo.setIndexBuffer(quadIndexBuffer); |
| 1380 | |
| 1381 | int maxInstancesPerDraw = quadIndexBuffer->maxQuads(); |
| 1382 | |
| 1383 | while (instanceCount) { |
| 1384 | drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw)); |
| 1385 | drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance()); |
| 1386 | drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance()); |
| 1387 | |
| 1388 | batchTarget->draw(drawInfo); |
| 1389 | |
| 1390 | drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount()); |
| 1391 | instanceCount -= drawInfo.instanceCount(); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1396 | |
| 1397 | private: |
| 1398 | DIEllipseBatch(const Geometry& geometry) { |
| 1399 | this->initClassID<DIEllipseBatch>(); |
| 1400 | fGeoData.push_back(geometry); |
| 1401 | } |
| 1402 | |
| 1403 | bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { |
| 1404 | DIEllipseBatch* that = t->cast<DIEllipseBatch>(); |
| 1405 | |
| 1406 | // TODO use vertex color to avoid breaking batches |
| 1407 | if (this->color() != that->color()) { |
| 1408 | return false; |
| 1409 | } |
| 1410 | |
| 1411 | if (this->mode() != that->mode()) { |
| 1412 | return false; |
| 1413 | } |
| 1414 | |
| 1415 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 1416 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 1421 | return true; |
| 1422 | } |
| 1423 | |
| 1424 | GrColor color() const { return fBatch.fColor; } |
| 1425 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1426 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1427 | DIEllipseEdgeEffect::Mode mode() const { return fBatch.fMode; } |
| 1428 | |
| 1429 | struct BatchTracker { |
| 1430 | GrColor fColor; |
| 1431 | DIEllipseEdgeEffect::Mode fMode; |
| 1432 | bool fUsesLocalCoords; |
| 1433 | bool fColorIgnored; |
| 1434 | bool fCoverageIgnored; |
| 1435 | }; |
| 1436 | |
| 1437 | static const int kVertsPerEllipse = 4; |
| 1438 | static const int kIndicesPerEllipse = 6; |
| 1439 | |
| 1440 | GrBatchOpt fBatchOpt; |
| 1441 | BatchTracker fBatch; |
| 1442 | SkSTArray<1, Geometry, true> fGeoData; |
| 1443 | }; |
| 1444 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1445 | bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1446 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1447 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1448 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1449 | bool useCoverageAA, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1450 | const SkRect& ellipse, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1451 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1452 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1453 | SkScalar xRadius = SkScalarHalf(ellipse.width()); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1454 | SkScalar yRadius = SkScalarHalf(ellipse.height()); |
| 1455 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1456 | SkStrokeRec::Style style = stroke.getStyle(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1457 | DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1458 | DIEllipseEdgeEffect::kStroke : |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1459 | (SkStrokeRec::kHairline_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1460 | DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill; |
| 1461 | |
| 1462 | SkScalar innerXRadius = 0; |
| 1463 | SkScalar innerYRadius = 0; |
| 1464 | if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) { |
| 1465 | SkScalar strokeWidth = stroke.getWidth(); |
| 1466 | |
| 1467 | if (SkScalarNearlyZero(strokeWidth)) { |
| 1468 | strokeWidth = SK_ScalarHalf; |
| 1469 | } else { |
| 1470 | strokeWidth *= SK_ScalarHalf; |
| 1471 | } |
| 1472 | |
| 1473 | // we only handle thick strokes for near-circular ellipses |
| 1474 | if (strokeWidth > SK_ScalarHalf && |
| 1475 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
| 1479 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1480 | if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius || |
| 1481 | strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) { |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
| 1485 | // set inner radius (if needed) |
| 1486 | if (SkStrokeRec::kStroke_Style == style) { |
| 1487 | innerXRadius = xRadius - strokeWidth; |
| 1488 | innerYRadius = yRadius - strokeWidth; |
| 1489 | } |
| 1490 | |
| 1491 | xRadius += strokeWidth; |
| 1492 | yRadius += strokeWidth; |
| 1493 | } |
| 1494 | if (DIEllipseEdgeEffect::kStroke == mode) { |
| 1495 | mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke : |
| 1496 | DIEllipseEdgeEffect::kFill; |
| 1497 | } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1498 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1499 | // 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] | 1500 | SkScalar a = viewMatrix[SkMatrix::kMScaleX]; |
| 1501 | SkScalar b = viewMatrix[SkMatrix::kMSkewX]; |
| 1502 | SkScalar c = viewMatrix[SkMatrix::kMSkewY]; |
| 1503 | SkScalar d = viewMatrix[SkMatrix::kMScaleY]; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1504 | SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c)); |
| 1505 | SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d)); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1506 | |
| 1507 | SkRect bounds = SkRect::MakeLTRB( |
| 1508 | center.fX - xRadius - geoDx, |
| 1509 | center.fY - yRadius - geoDy, |
| 1510 | center.fX + xRadius + geoDx, |
| 1511 | center.fY + yRadius + geoDy |
| 1512 | ); |
| 1513 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1514 | DIEllipseBatch::Geometry geometry; |
| 1515 | geometry.fViewMatrix = viewMatrix; |
| 1516 | geometry.fColor = color; |
| 1517 | geometry.fXRadius = xRadius; |
| 1518 | geometry.fYRadius = yRadius; |
| 1519 | geometry.fInnerXRadius = innerXRadius; |
| 1520 | geometry.fInnerYRadius = innerYRadius; |
| 1521 | geometry.fGeoDx = geoDx; |
| 1522 | geometry.fGeoDy = geoDy; |
| 1523 | geometry.fMode = mode; |
| 1524 | geometry.fDevBounds = bounds; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1525 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1526 | SkAutoTUnref<GrBatch> batch(DIEllipseBatch::Create(geometry)); |
| 1527 | target->drawBatch(pipelineBuilder, batch, &bounds); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1528 | |
| 1529 | return true; |
| 1530 | } |
| 1531 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1532 | /////////////////////////////////////////////////////////////////////////////// |
| 1533 | |
| 1534 | static const uint16_t gRRectIndices[] = { |
| 1535 | // corners |
| 1536 | 0, 1, 5, 0, 5, 4, |
| 1537 | 2, 3, 7, 2, 7, 6, |
| 1538 | 8, 9, 13, 8, 13, 12, |
| 1539 | 10, 11, 15, 10, 15, 14, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1540 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1541 | // edges |
| 1542 | 1, 2, 6, 1, 6, 5, |
| 1543 | 4, 5, 9, 4, 9, 8, |
| 1544 | 6, 7, 11, 6, 11, 10, |
| 1545 | 9, 10, 14, 9, 14, 13, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1546 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1547 | // center |
| 1548 | // we place this at the end so that we can ignore these indices when rendering stroke-only |
| 1549 | 5, 6, 10, 5, 10, 9 |
| 1550 | }; |
| 1551 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 1552 | static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6; |
| 1553 | static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices); |
| 1554 | static const int kVertsPerRRect = 16; |
| 1555 | static const int kNumRRectsInIndexBuffer = 256; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1556 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1557 | GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(bool isStrokeOnly) { |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 1558 | if (isStrokeOnly) { |
| 1559 | if (NULL == fStrokeRRectIndexBuffer) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1560 | fStrokeRRectIndexBuffer = fGpu->createInstancedIndexBuffer(gRRectIndices, |
| 1561 | kIndicesPerStrokeRRect, |
| 1562 | kNumRRectsInIndexBuffer, |
| 1563 | kVertsPerRRect); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1564 | } |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 1565 | return fStrokeRRectIndexBuffer; |
| 1566 | } else { |
| 1567 | if (NULL == fRRectIndexBuffer) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1568 | fRRectIndexBuffer = fGpu->createInstancedIndexBuffer(gRRectIndices, |
| 1569 | kIndicesPerRRect, |
| 1570 | kNumRRectsInIndexBuffer, |
| 1571 | kVertsPerRRect); |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 1572 | } |
| 1573 | return fRRectIndexBuffer; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1574 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1577 | bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1578 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1579 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1580 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1581 | bool useAA, |
| 1582 | const SkRRect& origOuter, |
| 1583 | const SkRRect& origInner) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1584 | bool applyAA = useAA && |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1585 | !pipelineBuilder->getRenderTarget()->isMultisampled() && |
| 1586 | pipelineBuilder->canUseFracCoveragePrimProc(color, *target->caps()); |
| 1587 | GrPipelineBuilder::AutoRestoreEffects are; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1588 | if (!origInner.isEmpty()) { |
| 1589 | SkTCopyOnFirstWrite<SkRRect> inner(origInner); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1590 | if (!viewMatrix.isIdentity()) { |
| 1591 | if (!origInner.transform(viewMatrix, inner.writable())) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1592 | return false; |
| 1593 | } |
| 1594 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1595 | GrPrimitiveEdgeType edgeType = applyAA ? |
| 1596 | kInverseFillAA_GrProcessorEdgeType : |
| 1597 | kInverseFillBW_GrProcessorEdgeType; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1598 | // TODO this needs to be a geometry processor |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1599 | GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner); |
| 1600 | if (NULL == fp) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1601 | return false; |
| 1602 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1603 | are.set(pipelineBuilder); |
| 1604 | pipelineBuilder->addCoverageProcessor(fp)->unref(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1608 | if (this->drawRRect(target, pipelineBuilder, color, viewMatrix, useAA, origOuter, fillRec)) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1609 | return true; |
| 1610 | } |
| 1611 | |
| 1612 | SkASSERT(!origOuter.isEmpty()); |
| 1613 | SkTCopyOnFirstWrite<SkRRect> outer(origOuter); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1614 | if (!viewMatrix.isIdentity()) { |
| 1615 | if (!origOuter.transform(viewMatrix, outer.writable())) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1616 | return false; |
| 1617 | } |
| 1618 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1619 | GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType : |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1620 | kFillBW_GrProcessorEdgeType; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1621 | GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1622 | if (NULL == effect) { |
| 1623 | return false; |
| 1624 | } |
| 1625 | if (!are.isSet()) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1626 | are.set(pipelineBuilder); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1627 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1628 | |
| 1629 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1630 | if (!viewMatrix.invert(&invert)) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1631 | return false; |
| 1632 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1633 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1634 | pipelineBuilder->addCoverageProcessor(effect)->unref(); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1635 | SkRect bounds = outer->getBounds(); |
| 1636 | if (applyAA) { |
| 1637 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 1638 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1639 | target->drawRect(pipelineBuilder, color, SkMatrix::I(), bounds, NULL, &invert); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1640 | return true; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1643 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1644 | |
| 1645 | class RRectCircleRendererBatch : public GrBatch { |
| 1646 | public: |
| 1647 | struct Geometry { |
| 1648 | GrColor fColor; |
| 1649 | SkMatrix fViewMatrix; |
| 1650 | SkScalar fInnerRadius; |
| 1651 | SkScalar fOuterRadius; |
| 1652 | bool fStroke; |
| 1653 | SkRect fDevBounds; |
| 1654 | }; |
| 1655 | |
| 1656 | static GrBatch* Create(const Geometry& geometry, const GrIndexBuffer* indexBuffer) { |
| 1657 | return SkNEW_ARGS(RRectCircleRendererBatch, (geometry, indexBuffer)); |
| 1658 | } |
| 1659 | |
| 1660 | const char* name() const SK_OVERRIDE { return "RRectCircleBatch"; } |
| 1661 | |
| 1662 | void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 1663 | // When this is called on a batch, there is only one geometry bundle |
| 1664 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1665 | } |
| 1666 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 1667 | out->setUnknownSingleComponent(); |
| 1668 | } |
| 1669 | |
| 1670 | void initBatchOpt(const GrBatchOpt& batchOpt) { |
| 1671 | fBatchOpt = batchOpt; |
| 1672 | } |
| 1673 | |
| 1674 | void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE { |
| 1675 | // Handle any color overrides |
| 1676 | if (init.fColorIgnored) { |
| 1677 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 1678 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 1679 | fGeoData[0].fColor = init.fOverrideColor; |
| 1680 | } |
| 1681 | |
| 1682 | // setup batch properties |
| 1683 | fBatch.fColorIgnored = init.fColorIgnored; |
| 1684 | fBatch.fColor = fGeoData[0].fColor; |
| 1685 | fBatch.fStroke = fGeoData[0].fStroke; |
| 1686 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 1687 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 1688 | } |
| 1689 | |
| 1690 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE { |
| 1691 | // reset to device coordinates |
| 1692 | SkMatrix invert; |
| 1693 | if (!this->viewMatrix().invert(&invert)) { |
| 1694 | SkDebugf("Failed to invert\n"); |
| 1695 | return; |
| 1696 | } |
| 1697 | |
| 1698 | // Setup geometry processor |
| 1699 | SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(), |
| 1700 | this->stroke(), |
| 1701 | invert)); |
| 1702 | |
| 1703 | batchTarget->initDraw(gp, pipeline); |
| 1704 | |
| 1705 | // TODO this is hacky, but the only way we have to initialize the GP is to use the |
| 1706 | // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch |
| 1707 | // everywhere we can remove this nastiness |
| 1708 | GrPipelineInfo init; |
| 1709 | init.fColorIgnored = fBatch.fColorIgnored; |
| 1710 | init.fOverrideColor = GrColor_ILLEGAL; |
| 1711 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 1712 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 1713 | gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 1714 | |
| 1715 | int instanceCount = fGeoData.count(); |
| 1716 | int vertexCount = kVertsPerRRect * instanceCount; |
| 1717 | size_t vertexStride = gp->getVertexStride(); |
| 1718 | SkASSERT(vertexStride == sizeof(CircleVertex)); |
| 1719 | |
| 1720 | const GrVertexBuffer* vertexBuffer; |
| 1721 | int firstVertex; |
| 1722 | |
| 1723 | void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 1724 | vertexCount, |
| 1725 | &vertexBuffer, |
| 1726 | &firstVertex); |
| 1727 | |
| 1728 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(vertices); |
| 1729 | |
| 1730 | for (int i = 0; i < instanceCount; i++) { |
| 1731 | Geometry& args = fGeoData[i]; |
| 1732 | |
| 1733 | SkScalar outerRadius = args.fOuterRadius; |
| 1734 | |
| 1735 | const SkRect& bounds = args.fDevBounds; |
| 1736 | |
| 1737 | SkScalar yCoords[4] = { |
| 1738 | bounds.fTop, |
| 1739 | bounds.fTop + outerRadius, |
| 1740 | bounds.fBottom - outerRadius, |
| 1741 | bounds.fBottom |
| 1742 | }; |
| 1743 | |
| 1744 | SkScalar yOuterRadii[4] = {-1, 0, 0, 1 }; |
| 1745 | // The inner radius in the vertex data must be specified in normalized space. |
| 1746 | SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius; |
| 1747 | for (int i = 0; i < 4; ++i) { |
| 1748 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 1749 | verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]); |
| 1750 | verts->fOuterRadius = outerRadius; |
| 1751 | verts->fInnerRadius = innerRadius; |
| 1752 | verts++; |
| 1753 | |
| 1754 | verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]); |
| 1755 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1756 | verts->fOuterRadius = outerRadius; |
| 1757 | verts->fInnerRadius = innerRadius; |
| 1758 | verts++; |
| 1759 | |
| 1760 | verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]); |
| 1761 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1762 | verts->fOuterRadius = outerRadius; |
| 1763 | verts->fInnerRadius = innerRadius; |
| 1764 | verts++; |
| 1765 | |
| 1766 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1767 | verts->fOffset = SkPoint::Make(1, yOuterRadii[i]); |
| 1768 | verts->fOuterRadius = outerRadius; |
| 1769 | verts->fInnerRadius = innerRadius; |
| 1770 | verts++; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | // drop out the middle quad if we're stroked |
| 1775 | int indexCnt = this->stroke() ? SK_ARRAY_COUNT(gRRectIndices) - 6 : |
| 1776 | SK_ARRAY_COUNT(gRRectIndices); |
| 1777 | |
| 1778 | |
| 1779 | GrDrawTarget::DrawInfo drawInfo; |
| 1780 | drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); |
| 1781 | drawInfo.setStartVertex(0); |
| 1782 | drawInfo.setStartIndex(0); |
| 1783 | drawInfo.setVerticesPerInstance(kVertsPerRRect); |
| 1784 | drawInfo.setIndicesPerInstance(indexCnt); |
| 1785 | drawInfo.adjustStartVertex(firstVertex); |
| 1786 | drawInfo.setVertexBuffer(vertexBuffer); |
| 1787 | drawInfo.setIndexBuffer(fIndexBuffer); |
| 1788 | |
| 1789 | int maxInstancesPerDraw = kNumRRectsInIndexBuffer; |
| 1790 | |
| 1791 | while (instanceCount) { |
| 1792 | drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw)); |
| 1793 | drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance()); |
| 1794 | drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance()); |
| 1795 | |
| 1796 | batchTarget->draw(drawInfo); |
| 1797 | |
| 1798 | drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount()); |
| 1799 | instanceCount -= drawInfo.instanceCount(); |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1804 | |
| 1805 | private: |
| 1806 | RRectCircleRendererBatch(const Geometry& geometry, const GrIndexBuffer* indexBuffer) |
| 1807 | : fIndexBuffer(indexBuffer) { |
| 1808 | this->initClassID<RRectCircleRendererBatch>(); |
| 1809 | fGeoData.push_back(geometry); |
| 1810 | } |
| 1811 | |
| 1812 | bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { |
| 1813 | RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>(); |
| 1814 | |
| 1815 | // TODO use vertex color to avoid breaking batches |
| 1816 | if (this->color() != that->color()) { |
| 1817 | return false; |
| 1818 | } |
| 1819 | |
| 1820 | if (this->stroke() != that->stroke()) { |
| 1821 | return false; |
| 1822 | } |
| 1823 | |
| 1824 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 1825 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1826 | return false; |
| 1827 | } |
| 1828 | |
| 1829 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 1830 | return true; |
| 1831 | } |
| 1832 | |
| 1833 | GrColor color() const { return fBatch.fColor; } |
| 1834 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1835 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1836 | bool stroke() const { return fBatch.fStroke; } |
| 1837 | |
| 1838 | struct BatchTracker { |
| 1839 | GrColor fColor; |
| 1840 | bool fStroke; |
| 1841 | bool fUsesLocalCoords; |
| 1842 | bool fColorIgnored; |
| 1843 | bool fCoverageIgnored; |
| 1844 | }; |
| 1845 | |
| 1846 | GrBatchOpt fBatchOpt; |
| 1847 | BatchTracker fBatch; |
| 1848 | SkSTArray<1, Geometry, true> fGeoData; |
| 1849 | const GrIndexBuffer* fIndexBuffer; |
| 1850 | }; |
| 1851 | |
| 1852 | class RRectEllipseRendererBatch : public GrBatch { |
| 1853 | public: |
| 1854 | struct Geometry { |
| 1855 | GrColor fColor; |
| 1856 | SkMatrix fViewMatrix; |
| 1857 | SkScalar fXRadius; |
| 1858 | SkScalar fYRadius; |
| 1859 | SkScalar fInnerXRadius; |
| 1860 | SkScalar fInnerYRadius; |
| 1861 | bool fStroke; |
| 1862 | SkRect fDevBounds; |
| 1863 | }; |
| 1864 | |
| 1865 | static GrBatch* Create(const Geometry& geometry, const GrIndexBuffer* indexBuffer) { |
| 1866 | return SkNEW_ARGS(RRectEllipseRendererBatch, (geometry, indexBuffer)); |
| 1867 | } |
| 1868 | |
| 1869 | const char* name() const SK_OVERRIDE { return "RRectEllipseRendererBatch"; } |
| 1870 | |
| 1871 | void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 1872 | // When this is called on a batch, there is only one geometry bundle |
| 1873 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1874 | } |
| 1875 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { |
| 1876 | out->setUnknownSingleComponent(); |
| 1877 | } |
| 1878 | |
| 1879 | void initBatchOpt(const GrBatchOpt& batchOpt) { |
| 1880 | fBatchOpt = batchOpt; |
| 1881 | } |
| 1882 | |
| 1883 | void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE { |
| 1884 | // Handle any color overrides |
| 1885 | if (init.fColorIgnored) { |
| 1886 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 1887 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 1888 | fGeoData[0].fColor = init.fOverrideColor; |
| 1889 | } |
| 1890 | |
| 1891 | // setup batch properties |
| 1892 | fBatch.fColorIgnored = init.fColorIgnored; |
| 1893 | fBatch.fColor = fGeoData[0].fColor; |
| 1894 | fBatch.fStroke = fGeoData[0].fStroke; |
| 1895 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 1896 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 1897 | } |
| 1898 | |
| 1899 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE { |
| 1900 | // reset to device coordinates |
| 1901 | SkMatrix invert; |
| 1902 | if (!this->viewMatrix().invert(&invert)) { |
| 1903 | SkDebugf("Failed to invert\n"); |
| 1904 | return; |
| 1905 | } |
| 1906 | |
| 1907 | // Setup geometry processor |
| 1908 | SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(), |
| 1909 | this->stroke(), |
| 1910 | invert)); |
| 1911 | |
| 1912 | batchTarget->initDraw(gp, pipeline); |
| 1913 | |
| 1914 | // TODO this is hacky, but the only way we have to initialize the GP is to use the |
| 1915 | // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch |
| 1916 | // everywhere we can remove this nastiness |
| 1917 | GrPipelineInfo init; |
| 1918 | init.fColorIgnored = fBatch.fColorIgnored; |
| 1919 | init.fOverrideColor = GrColor_ILLEGAL; |
| 1920 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 1921 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 1922 | gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 1923 | |
| 1924 | int instanceCount = fGeoData.count(); |
| 1925 | int vertexCount = kVertsPerRRect * instanceCount; |
| 1926 | size_t vertexStride = gp->getVertexStride(); |
| 1927 | SkASSERT(vertexStride == sizeof(EllipseVertex)); |
| 1928 | |
| 1929 | const GrVertexBuffer* vertexBuffer; |
| 1930 | int firstVertex; |
| 1931 | |
| 1932 | void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 1933 | vertexCount, |
| 1934 | &vertexBuffer, |
| 1935 | &firstVertex); |
| 1936 | |
| 1937 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(vertices); |
| 1938 | |
| 1939 | for (int i = 0; i < instanceCount; i++) { |
| 1940 | Geometry& args = fGeoData[i]; |
| 1941 | |
| 1942 | // Compute the reciprocals of the radii here to save time in the shader |
| 1943 | SkScalar xRadRecip = SkScalarInvert(args.fXRadius); |
| 1944 | SkScalar yRadRecip = SkScalarInvert(args.fYRadius); |
| 1945 | SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius); |
| 1946 | SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius); |
| 1947 | |
| 1948 | // Extend the radii out half a pixel to antialias. |
| 1949 | SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf; |
| 1950 | SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf; |
| 1951 | |
| 1952 | const SkRect& bounds = args.fDevBounds; |
| 1953 | |
| 1954 | SkScalar yCoords[4] = { |
| 1955 | bounds.fTop, |
| 1956 | bounds.fTop + yOuterRadius, |
| 1957 | bounds.fBottom - yOuterRadius, |
| 1958 | bounds.fBottom |
| 1959 | }; |
| 1960 | SkScalar yOuterOffsets[4] = { |
| 1961 | yOuterRadius, |
| 1962 | SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0 |
| 1963 | SK_ScalarNearlyZero, |
| 1964 | yOuterRadius |
| 1965 | }; |
| 1966 | |
| 1967 | for (int i = 0; i < 4; ++i) { |
| 1968 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 1969 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
| 1970 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1971 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1972 | verts++; |
| 1973 | |
| 1974 | verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]); |
| 1975 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
| 1976 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1977 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1978 | verts++; |
| 1979 | |
| 1980 | verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]); |
| 1981 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
| 1982 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1983 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1984 | verts++; |
| 1985 | |
| 1986 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1987 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
| 1988 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1989 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1990 | verts++; |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | // drop out the middle quad if we're stroked |
| 1995 | int indexCnt = this->stroke() ? SK_ARRAY_COUNT(gRRectIndices) - 6 : |
| 1996 | SK_ARRAY_COUNT(gRRectIndices); |
| 1997 | |
| 1998 | GrDrawTarget::DrawInfo drawInfo; |
| 1999 | drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); |
| 2000 | drawInfo.setStartVertex(0); |
| 2001 | drawInfo.setStartIndex(0); |
| 2002 | drawInfo.setVerticesPerInstance(kVertsPerRRect); |
| 2003 | drawInfo.setIndicesPerInstance(indexCnt); |
| 2004 | drawInfo.adjustStartVertex(firstVertex); |
| 2005 | drawInfo.setVertexBuffer(vertexBuffer); |
| 2006 | drawInfo.setIndexBuffer(fIndexBuffer); |
| 2007 | |
| 2008 | int maxInstancesPerDraw = kNumRRectsInIndexBuffer; |
| 2009 | |
| 2010 | while (instanceCount) { |
| 2011 | drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw)); |
| 2012 | drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance()); |
| 2013 | drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance()); |
| 2014 | |
| 2015 | batchTarget->draw(drawInfo); |
| 2016 | |
| 2017 | drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount()); |
| 2018 | instanceCount -= drawInfo.instanceCount(); |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 2023 | |
| 2024 | private: |
| 2025 | RRectEllipseRendererBatch(const Geometry& geometry, const GrIndexBuffer* indexBuffer) |
| 2026 | : fIndexBuffer(indexBuffer) { |
| 2027 | this->initClassID<RRectEllipseRendererBatch>(); |
| 2028 | fGeoData.push_back(geometry); |
| 2029 | } |
| 2030 | |
| 2031 | bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { |
| 2032 | RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>(); |
| 2033 | |
| 2034 | // TODO use vertex color to avoid breaking batches |
| 2035 | if (this->color() != that->color()) { |
| 2036 | return false; |
| 2037 | } |
| 2038 | |
| 2039 | if (this->stroke() != that->stroke()) { |
| 2040 | return false; |
| 2041 | } |
| 2042 | |
| 2043 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 2044 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 2045 | return false; |
| 2046 | } |
| 2047 | |
| 2048 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 2049 | return true; |
| 2050 | } |
| 2051 | |
| 2052 | GrColor color() const { return fBatch.fColor; } |
| 2053 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 2054 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 2055 | bool stroke() const { return fBatch.fStroke; } |
| 2056 | |
| 2057 | struct BatchTracker { |
| 2058 | GrColor fColor; |
| 2059 | bool fStroke; |
| 2060 | bool fUsesLocalCoords; |
| 2061 | bool fColorIgnored; |
| 2062 | bool fCoverageIgnored; |
| 2063 | }; |
| 2064 | |
| 2065 | GrBatchOpt fBatchOpt; |
| 2066 | BatchTracker fBatch; |
| 2067 | SkSTArray<1, Geometry, true> fGeoData; |
| 2068 | const GrIndexBuffer* fIndexBuffer; |
| 2069 | }; |
| 2070 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 2071 | bool GrOvalRenderer::drawRRect(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2072 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 2073 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 2074 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 2075 | bool useAA, |
| 2076 | const SkRRect& rrect, |
| 2077 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2078 | if (rrect.isOval()) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2079 | return this->drawOval(target, pipelineBuilder, color, viewMatrix, useAA, rrect.getBounds(), |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 2080 | stroke); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 2083 | bool useCoverageAA = useAA && |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2084 | !pipelineBuilder->getRenderTarget()->isMultisampled() && |
| 2085 | pipelineBuilder->canUseFracCoveragePrimProc(color, *target->caps()); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 2086 | |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 2087 | // only anti-aliased rrects for now |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 2088 | if (!useCoverageAA) { |
commit-bot@chromium.org | 37d883d | 2013-05-02 13:11:22 +0000 | [diff] [blame] | 2089 | return false; |
| 2090 | } |
| 2091 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 2092 | if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2093 | return false; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2094 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2095 | |
| 2096 | // do any matrix crunching before we reset the draw state for device coords |
| 2097 | const SkRect& rrectBounds = rrect.getBounds(); |
| 2098 | SkRect bounds; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 2099 | viewMatrix.mapRect(&bounds, rrectBounds); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2100 | |
| 2101 | SkVector radii = rrect.getSimpleRadii(); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 2102 | SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX + |
| 2103 | viewMatrix[SkMatrix::kMSkewY]*radii.fY); |
| 2104 | SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX + |
| 2105 | viewMatrix[SkMatrix::kMScaleY]*radii.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2106 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2107 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2108 | |
| 2109 | // do (potentially) anisotropic mapping of stroke |
| 2110 | SkVector scaledStroke; |
| 2111 | SkScalar strokeWidth = stroke.getWidth(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2112 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2113 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 2114 | SkStrokeRec::kHairline_Style == style; |
| 2115 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
| 2116 | |
| 2117 | if (hasStroke) { |
| 2118 | if (SkStrokeRec::kHairline_Style == style) { |
| 2119 | scaledStroke.set(1, 1); |
| 2120 | } else { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 2121 | scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] + |
| 2122 | viewMatrix[SkMatrix::kMSkewY])); |
| 2123 | scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] + |
| 2124 | viewMatrix[SkMatrix::kMScaleY])); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | // if half of strokewidth is greater than radius, we don't handle that right now |
| 2128 | if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) { |
| 2129 | return false; |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on |
| 2134 | // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine- |
| 2135 | // patch will have fractional coverage. This only matters when the interior is actually filled. |
| 2136 | // We could consider falling back to rect rendering here, since a tiny radius is |
| 2137 | // indistinguishable from a square corner. |
| 2138 | if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2139 | return false; |
| 2140 | } |
| 2141 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 2142 | GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(isStrokeOnly); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2143 | if (NULL == indexBuffer) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 2144 | SkDebugf("Failed to create index buffer!\n"); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2145 | return false; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2148 | // if the corners are circles, use the circle renderer |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2149 | if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2150 | SkScalar innerRadius = 0.0f; |
| 2151 | SkScalar outerRadius = xRadius; |
| 2152 | SkScalar halfWidth = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2153 | if (hasStroke) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2154 | if (SkScalarNearlyZero(scaledStroke.fX)) { |
| 2155 | halfWidth = SK_ScalarHalf; |
| 2156 | } else { |
| 2157 | halfWidth = SkScalarHalf(scaledStroke.fX); |
| 2158 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2159 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2160 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2161 | innerRadius = xRadius - halfWidth; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2162 | } |
| 2163 | outerRadius += halfWidth; |
| 2164 | bounds.outset(halfWidth, halfWidth); |
| 2165 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2166 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2167 | isStrokeOnly = (isStrokeOnly && innerRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 2168 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2169 | // 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] | 2170 | // simpler computation because the computed alpha is zero, rather than 50%, at the radius. |
| 2171 | // Second, the outer radius is used to compute the verts of the bounding box that is |
| 2172 | // rendered and the outset ensures the box will cover all partially covered by the rrect |
| 2173 | // corners. |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2174 | outerRadius += SK_ScalarHalf; |
| 2175 | innerRadius -= SK_ScalarHalf; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2176 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2177 | // Expand the rect so all the pixels will be captured. |
| 2178 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2179 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2180 | RRectCircleRendererBatch::Geometry geometry; |
| 2181 | geometry.fViewMatrix = viewMatrix; |
| 2182 | geometry.fColor = color; |
| 2183 | geometry.fInnerRadius = innerRadius; |
| 2184 | geometry.fOuterRadius = outerRadius; |
| 2185 | geometry.fStroke = isStrokeOnly; |
| 2186 | geometry.fDevBounds = bounds; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2187 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2188 | SkAutoTUnref<GrBatch> batch(RRectCircleRendererBatch::Create(geometry, indexBuffer)); |
| 2189 | target->drawBatch(pipelineBuilder, batch, &bounds); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2190 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2191 | // otherwise we use the ellipse renderer |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2192 | } else { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2193 | SkScalar innerXRadius = 0.0f; |
| 2194 | SkScalar innerYRadius = 0.0f; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2195 | if (hasStroke) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2196 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 2197 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 2198 | } else { |
| 2199 | scaledStroke.scale(SK_ScalarHalf); |
| 2200 | } |
| 2201 | |
| 2202 | // we only handle thick strokes for near-circular ellipses |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 2203 | if (scaledStroke.length() > SK_ScalarHalf && |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2204 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
| 2205 | return false; |
| 2206 | } |
| 2207 | |
| 2208 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 2209 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 2210 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
| 2211 | return false; |
| 2212 | } |
| 2213 | |
| 2214 | // 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] | 2215 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2216 | innerXRadius = xRadius - scaledStroke.fX; |
| 2217 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | xRadius += scaledStroke.fX; |
| 2221 | yRadius += scaledStroke.fY; |
| 2222 | bounds.outset(scaledStroke.fX, scaledStroke.fY); |
| 2223 | } |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 2224 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2225 | isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 2226 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2227 | // Expand the rect so all the pixels will be captured. |
| 2228 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 2229 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2230 | RRectEllipseRendererBatch::Geometry geometry; |
| 2231 | geometry.fViewMatrix = viewMatrix; |
| 2232 | geometry.fColor = color; |
| 2233 | geometry.fXRadius = xRadius; |
| 2234 | geometry.fYRadius = yRadius; |
| 2235 | geometry.fInnerXRadius = innerXRadius; |
| 2236 | geometry.fInnerYRadius = innerYRadius; |
| 2237 | geometry.fStroke = isStrokeOnly; |
| 2238 | geometry.fDevBounds = bounds; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2239 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2240 | SkAutoTUnref<GrBatch> batch(RRectEllipseRendererBatch::Create(geometry, indexBuffer)); |
| 2241 | target->drawBatch(pipelineBuilder, batch, &bounds); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2242 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2243 | return true; |
| 2244 | } |