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