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