commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "GrDashingEffect.h" |
| 9 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 10 | #include "GrBatch.h" |
| 11 | #include "GrBatchTarget.h" |
| 12 | #include "GrBufferAllocPool.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 13 | #include "GrGeometryProcessor.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 14 | #include "GrContext.h" |
| 15 | #include "GrCoordTransform.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 16 | #include "GrDefaultGeoProcFactory.h" |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 17 | #include "GrDrawTarget.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 18 | #include "GrDrawTargetCaps.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 19 | #include "GrInvariantOutput.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 20 | #include "GrProcessor.h" |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 21 | #include "GrStrokeInfo.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 22 | #include "SkGr.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 23 | #include "gl/GrGLGeometryProcessor.h" |
| 24 | #include "gl/GrGLProcessor.h" |
| 25 | #include "gl/GrGLSL.h" |
| 26 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 30 | // Returns whether or not the gpu can fast path the dash line effect. |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 31 | bool GrDashingEffect::CanDrawDashLine(const SkPoint pts[2], const GrStrokeInfo& strokeInfo, |
| 32 | const SkMatrix& viewMatrix) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 33 | // Pts must be either horizontal or vertical in src space |
| 34 | if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | // May be able to relax this to include skew. As of now cannot do perspective |
| 39 | // because of the non uniform scaling of bloating a rect |
| 40 | if (!viewMatrix.preservesRightAngles()) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo(); |
| 49 | if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap(); |
| 54 | // Current we do don't handle Round or Square cap dashes |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 55 | if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | namespace { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 63 | struct DashLineVertex { |
| 64 | SkPoint fPos; |
| 65 | SkPoint fDashPos; |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 66 | SkScalar fIntervalLength; |
| 67 | SkRect fRect; |
| 68 | }; |
| 69 | struct DashCircleVertex { |
| 70 | SkPoint fPos; |
| 71 | SkPoint fDashPos; |
| 72 | SkScalar fIntervalLength; |
| 73 | SkScalar fRadius; |
| 74 | SkScalar fCenterX; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 75 | }; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 76 | |
| 77 | enum DashAAMode { |
| 78 | kBW_DashAAMode, |
| 79 | kEdgeAA_DashAAMode, |
| 80 | kMSAA_DashAAMode, |
| 81 | |
| 82 | kDashAAModeCount, |
| 83 | }; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 86 | static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale, |
| 87 | const SkMatrix& viewMatrix, const SkPoint pts[2]) { |
| 88 | SkVector vecSrc = pts[1] - pts[0]; |
| 89 | SkScalar magSrc = vecSrc.length(); |
| 90 | SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0; |
| 91 | vecSrc.scale(invSrc); |
| 92 | |
| 93 | SkVector vecSrcPerp; |
| 94 | vecSrc.rotateCW(&vecSrcPerp); |
| 95 | viewMatrix.mapVectors(&vecSrc, 1); |
| 96 | viewMatrix.mapVectors(&vecSrcPerp, 1); |
| 97 | |
| 98 | // parallelScale tells how much to scale along the line parallel to the dash line |
| 99 | // perpScale tells how much to scale in the direction perpendicular to the dash line |
| 100 | *parallelScale = vecSrc.length(); |
| 101 | *perpScale = vecSrcPerp.length(); |
| 102 | } |
| 103 | |
| 104 | // calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1] |
| 105 | // Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot |
| 106 | static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) { |
| 107 | SkVector vec = pts[1] - pts[0]; |
| 108 | SkScalar mag = vec.length(); |
| 109 | SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| 110 | |
| 111 | vec.scale(inv); |
| 112 | rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 113 | if (ptsRot) { |
| 114 | rotMatrix->mapPoints(ptsRot, pts, 2); |
| 115 | // correction for numerical issues if map doesn't make ptsRot exactly horizontal |
| 116 | ptsRot[1].fY = pts[0].fY; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Assumes phase < sum of all intervals |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 121 | static SkScalar calc_start_adjustment(const SkScalar intervals[2], SkScalar phase) { |
| 122 | SkASSERT(phase < intervals[0] + intervals[1]); |
| 123 | if (phase >= intervals[0] && phase != 0) { |
| 124 | SkScalar srcIntervalLen = intervals[0] + intervals[1]; |
| 125 | return srcIntervalLen - phase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 130 | static SkScalar calc_end_adjustment(const SkScalar intervals[2], const SkPoint pts[2], |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 131 | SkScalar phase, SkScalar* endingInt) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 132 | if (pts[1].fX <= pts[0].fX) { |
| 133 | return 0; |
| 134 | } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 135 | SkScalar srcIntervalLen = intervals[0] + intervals[1]; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 136 | SkScalar totalLen = pts[1].fX - pts[0].fX; |
| 137 | SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen); |
| 138 | SkScalar numFullIntervals = SkScalarFloorToScalar(temp); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 139 | *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 140 | temp = SkScalarDiv(*endingInt, srcIntervalLen); |
| 141 | *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen; |
| 142 | if (0 == *endingInt) { |
| 143 | *endingInt = srcIntervalLen; |
| 144 | } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 145 | if (*endingInt > intervals[0]) { |
| 146 | if (0 == intervals[0]) { |
commit-bot@chromium.org | ad88340 | 2014-05-19 14:43:45 +0000 | [diff] [blame] | 147 | *endingInt -= 0.01f; // make sure we capture the last zero size pnt (used if has caps) |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 148 | } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 149 | return *endingInt - intervals[0]; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 154 | enum DashCap { |
| 155 | kRound_DashCap, |
| 156 | kNonRound_DashCap, |
| 157 | }; |
| 158 | |
| 159 | static int kDashVertices = 4; |
| 160 | |
| 161 | template <typename T> |
| 162 | void setup_dashed_rect_common(const SkRect& rect, const SkMatrix& matrix, T* vertices, int idx, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 163 | SkScalar offset, SkScalar bloatX, SkScalar bloatY, SkScalar len, |
| 164 | SkScalar stroke) { |
| 165 | SkScalar startDashX = offset - bloatX; |
| 166 | SkScalar endDashX = offset + len + bloatX; |
| 167 | SkScalar startDashY = -stroke - bloatY; |
| 168 | SkScalar endDashY = stroke + bloatY; |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 169 | vertices[idx].fDashPos = SkPoint::Make(startDashX , startDashY); |
| 170 | vertices[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY); |
| 171 | vertices[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY); |
| 172 | vertices[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY); |
| 173 | |
| 174 | vertices[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop); |
| 175 | vertices[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom); |
| 176 | vertices[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom); |
| 177 | vertices[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop); |
| 178 | |
| 179 | matrix.mapPointsWithStride(&vertices[idx].fPos, sizeof(T), 4); |
| 180 | } |
| 181 | |
| 182 | static void setup_dashed_rect(const SkRect& rect, void* vertices, int idx, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 183 | const SkMatrix& matrix, SkScalar offset, SkScalar bloatX, |
| 184 | SkScalar bloatY, SkScalar len, SkScalar stroke, |
| 185 | SkScalar startInterval, SkScalar endInterval, SkScalar strokeWidth, |
| 186 | DashCap cap, const size_t vertexStride) { |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 187 | SkScalar intervalLength = startInterval + endInterval; |
| 188 | |
| 189 | if (kRound_DashCap == cap) { |
| 190 | SkASSERT(vertexStride == sizeof(DashCircleVertex)); |
| 191 | DashCircleVertex* verts = reinterpret_cast<DashCircleVertex*>(vertices); |
| 192 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 193 | setup_dashed_rect_common<DashCircleVertex>(rect, matrix, verts, idx, offset, bloatX, |
| 194 | bloatY, len, stroke); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 195 | |
| 196 | SkScalar radius = SkScalarHalf(strokeWidth) - 0.5f; |
| 197 | SkScalar centerX = SkScalarHalf(endInterval); |
| 198 | |
| 199 | for (int i = 0; i < kDashVertices; i++) { |
| 200 | verts[idx + i].fIntervalLength = intervalLength; |
| 201 | verts[idx + i].fRadius = radius; |
| 202 | verts[idx + i].fCenterX = centerX; |
| 203 | } |
| 204 | |
| 205 | } else { |
| 206 | SkASSERT(kNonRound_DashCap == cap && vertexStride == sizeof(DashLineVertex)); |
| 207 | DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(vertices); |
| 208 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 209 | setup_dashed_rect_common<DashLineVertex>(rect, matrix, verts, idx, offset, bloatX, |
| 210 | bloatY, len, stroke); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 211 | |
| 212 | SkScalar halfOffLen = SkScalarHalf(endInterval); |
| 213 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 214 | SkRect rectParam; |
| 215 | rectParam.set(halfOffLen + 0.5f, -halfStroke + 0.5f, |
| 216 | halfOffLen + startInterval - 0.5f, halfStroke - 0.5f); |
| 217 | for (int i = 0; i < kDashVertices; i++) { |
| 218 | verts[idx + i].fIntervalLength = intervalLength; |
| 219 | verts[idx + i].fRect = rectParam; |
| 220 | } |
| 221 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 222 | } |
| 223 | |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 224 | static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix, |
| 225 | SkPoint* verts) { |
| 226 | verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop); |
| 227 | verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom); |
| 228 | verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom); |
| 229 | verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop); |
| 230 | matrix.mapPoints(&verts[idx], 4); |
| 231 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 232 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * An GrGeometryProcessor that renders a dashed line. |
| 236 | * This GrGeometryProcessor is meant for dashed lines that only have a single on/off interval pair. |
| 237 | * Bounding geometry is rendered and the effect computes coverage based on the fragment's |
| 238 | * position relative to the dashed line. |
| 239 | */ |
| 240 | static GrGeometryProcessor* create_dash_gp(GrColor, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 241 | DashAAMode aaMode, |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 242 | DashCap cap, |
| 243 | const SkMatrix& localMatrix); |
| 244 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 245 | class DashBatch : public GrBatch { |
| 246 | public: |
| 247 | struct Geometry { |
| 248 | GrColor fColor; |
| 249 | SkMatrix fViewMatrix; |
| 250 | SkMatrix fSrcRotInv; |
| 251 | SkPoint fPtsRot[2]; |
| 252 | SkScalar fSrcStrokeWidth; |
| 253 | SkScalar fPhase; |
| 254 | SkScalar fIntervals[2]; |
| 255 | SkScalar fParallelScale; |
| 256 | SkScalar fPerpendicularScale; |
| 257 | SkDEBUGCODE(SkRect fDevBounds;) |
| 258 | }; |
| 259 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 260 | static GrBatch* Create(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) { |
| 261 | return SkNEW_ARGS(DashBatch, (geometry, cap, aaMode, fullDash)); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 262 | } |
| 263 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 264 | const char* name() const override { return "DashBatch"; } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 265 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 266 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 267 | // When this is called on a batch, there is only one geometry bundle |
| 268 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 269 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 270 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 271 | out->setUnknownSingleComponent(); |
| 272 | } |
| 273 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 274 | void initBatchTracker(const GrPipelineInfo& init) override { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 275 | // Handle any color overrides |
| 276 | if (init.fColorIgnored) { |
| 277 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 278 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 279 | fGeoData[0].fColor = init.fOverrideColor; |
| 280 | } |
| 281 | |
| 282 | // setup batch properties |
| 283 | fBatch.fColorIgnored = init.fColorIgnored; |
| 284 | fBatch.fColor = fGeoData[0].fColor; |
| 285 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 286 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 287 | } |
| 288 | |
| 289 | struct DashDraw { |
| 290 | SkScalar fStartOffset; |
| 291 | SkScalar fStrokeWidth; |
| 292 | SkScalar fLineLength; |
| 293 | SkScalar fHalfDevStroke; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 294 | SkScalar fDevBloatX; |
| 295 | SkScalar fDevBloatY; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 296 | bool fLineDone; |
| 297 | bool fHasStartRect; |
| 298 | bool fHasEndRect; |
| 299 | }; |
| 300 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 301 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 302 | int instanceCount = fGeoData.count(); |
| 303 | |
| 304 | SkMatrix invert; |
| 305 | if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) { |
| 306 | SkDebugf("Failed to invert\n"); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | SkPaint::Cap cap = this->cap(); |
| 311 | |
| 312 | SkAutoTUnref<const GrGeometryProcessor> gp; |
| 313 | |
| 314 | bool isRoundCap = SkPaint::kRound_Cap == cap; |
| 315 | DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap; |
| 316 | if (this->fullDash()) { |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 317 | gp.reset(create_dash_gp(this->color(), this->aaMode(), capType, invert)); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 318 | } else { |
| 319 | // Set up the vertex data for the line and start/end dashes |
| 320 | gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType, |
| 321 | this->color(), |
| 322 | SkMatrix::I(), |
| 323 | invert)); |
| 324 | } |
| 325 | |
| 326 | batchTarget->initDraw(gp, pipeline); |
| 327 | |
| 328 | // TODO remove this when batch is everywhere |
| 329 | GrPipelineInfo init; |
| 330 | init.fColorIgnored = fBatch.fColorIgnored; |
| 331 | init.fOverrideColor = GrColor_ILLEGAL; |
| 332 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 333 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 334 | gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 335 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 336 | // useAA here means Edge AA or MSAA |
| 337 | bool useAA = this->aaMode() != kBW_DashAAMode; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 338 | bool fullDash = this->fullDash(); |
| 339 | |
| 340 | // We do two passes over all of the dashes. First we setup the start, end, and bounds, |
| 341 | // rectangles. We preserve all of this work in the rects / draws arrays below. Then we |
| 342 | // iterate again over these decomposed dashes to generate vertices |
| 343 | SkSTArray<128, SkRect, true> rects; |
| 344 | SkSTArray<128, DashDraw, true> draws; |
| 345 | |
| 346 | int totalRectCount = 0; |
joshualitt | d0f5457 | 2015-03-02 12:00:52 -0800 | [diff] [blame] | 347 | int rectOffset = 0; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 348 | for (int i = 0; i < instanceCount; i++) { |
| 349 | Geometry& args = fGeoData[i]; |
| 350 | |
| 351 | bool hasCap = SkPaint::kButt_Cap != cap && 0 != args.fSrcStrokeWidth; |
| 352 | |
| 353 | // We always want to at least stroke out half a pixel on each side in device space |
| 354 | // so 0.5f / perpScale gives us this min in src space |
| 355 | SkScalar halfSrcStroke = |
| 356 | SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale); |
| 357 | |
| 358 | SkScalar strokeAdj; |
| 359 | if (!hasCap) { |
| 360 | strokeAdj = 0.f; |
| 361 | } else { |
| 362 | strokeAdj = halfSrcStroke; |
| 363 | } |
| 364 | |
| 365 | SkScalar startAdj = 0; |
| 366 | |
| 367 | SkMatrix& combinedMatrix = args.fSrcRotInv; |
| 368 | combinedMatrix.postConcat(args.fViewMatrix); |
| 369 | |
| 370 | bool lineDone = false; |
| 371 | |
| 372 | // Too simplify the algorithm, we always push back rects for start and end rect. |
| 373 | // Otherwise we'd have to track start / end rects for each individual geometry |
joshualitt | d0f5457 | 2015-03-02 12:00:52 -0800 | [diff] [blame] | 374 | rects.push_back(); |
| 375 | rects.push_back(); |
| 376 | rects.push_back(); |
| 377 | SkRect& bounds = rects[rectOffset++]; |
| 378 | SkRect& startRect = rects[rectOffset++]; |
| 379 | SkRect& endRect = rects[rectOffset++]; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 380 | |
| 381 | bool hasStartRect = false; |
| 382 | // If we are using AA, check to see if we are drawing a partial dash at the start. If so |
| 383 | // draw it separately here and adjust our start point accordingly |
| 384 | if (useAA) { |
| 385 | if (args.fPhase > 0 && args.fPhase < args.fIntervals[0]) { |
| 386 | SkPoint startPts[2]; |
| 387 | startPts[0] = args.fPtsRot[0]; |
| 388 | startPts[1].fY = startPts[0].fY; |
| 389 | startPts[1].fX = SkMinScalar(startPts[0].fX + args.fIntervals[0] - args.fPhase, |
| 390 | args.fPtsRot[1].fX); |
| 391 | startRect.set(startPts, 2); |
| 392 | startRect.outset(strokeAdj, halfSrcStroke); |
| 393 | |
| 394 | hasStartRect = true; |
| 395 | startAdj = args.fIntervals[0] + args.fIntervals[1] - args.fPhase; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // adjustments for start and end of bounding rect so we only draw dash intervals |
| 400 | // contained in the original line segment. |
| 401 | startAdj += calc_start_adjustment(args.fIntervals, args.fPhase); |
| 402 | if (startAdj != 0) { |
| 403 | args.fPtsRot[0].fX += startAdj; |
| 404 | args.fPhase = 0; |
| 405 | } |
| 406 | SkScalar endingInterval = 0; |
| 407 | SkScalar endAdj = calc_end_adjustment(args.fIntervals, args.fPtsRot, args.fPhase, |
| 408 | &endingInterval); |
| 409 | args.fPtsRot[1].fX -= endAdj; |
| 410 | if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) { |
| 411 | lineDone = true; |
| 412 | } |
| 413 | |
| 414 | bool hasEndRect = false; |
| 415 | // If we are using AA, check to see if we are drawing a partial dash at then end. If so |
| 416 | // draw it separately here and adjust our end point accordingly |
| 417 | if (useAA && !lineDone) { |
| 418 | // If we adjusted the end then we will not be drawing a partial dash at the end. |
| 419 | // If we didn't adjust the end point then we just need to make sure the ending |
| 420 | // dash isn't a full dash |
| 421 | if (0 == endAdj && endingInterval != args.fIntervals[0]) { |
| 422 | SkPoint endPts[2]; |
| 423 | endPts[1] = args.fPtsRot[1]; |
| 424 | endPts[0].fY = endPts[1].fY; |
| 425 | endPts[0].fX = endPts[1].fX - endingInterval; |
| 426 | |
| 427 | endRect.set(endPts, 2); |
| 428 | endRect.outset(strokeAdj, halfSrcStroke); |
| 429 | |
| 430 | hasEndRect = true; |
| 431 | endAdj = endingInterval + args.fIntervals[1]; |
| 432 | |
| 433 | args.fPtsRot[1].fX -= endAdj; |
| 434 | if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) { |
| 435 | lineDone = true; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (startAdj != 0) { |
| 441 | args.fPhase = 0; |
| 442 | } |
| 443 | |
| 444 | // Change the dashing info from src space into device space |
| 445 | SkScalar* devIntervals = args.fIntervals; |
| 446 | devIntervals[0] = args.fIntervals[0] * args.fParallelScale; |
| 447 | devIntervals[1] = args.fIntervals[1] * args.fParallelScale; |
| 448 | SkScalar devPhase = args.fPhase * args.fParallelScale; |
| 449 | SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale; |
| 450 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 451 | if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 452 | strokeWidth = 1.f; |
| 453 | } |
| 454 | |
| 455 | SkScalar halfDevStroke = strokeWidth * 0.5f; |
| 456 | |
| 457 | if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) { |
| 458 | // add cap to on interval and remove from off interval |
| 459 | devIntervals[0] += strokeWidth; |
| 460 | devIntervals[1] -= strokeWidth; |
| 461 | } |
| 462 | SkScalar startOffset = devIntervals[1] * 0.5f + devPhase; |
| 463 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 464 | // For EdgeAA, we bloat in X & Y for both square and round caps. |
| 465 | // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps. |
| 466 | SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f; |
| 467 | SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode) |
| 468 | ? 0.5f : devBloatX; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 469 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 470 | SkScalar bloatX = devBloatX / args.fParallelScale; |
| 471 | SkScalar bloatY = devBloatY / args.fPerpendicularScale; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 472 | |
| 473 | if (devIntervals[1] <= 0.f && useAA) { |
| 474 | // Case when we end up drawing a solid AA rect |
| 475 | // Reset the start rect to draw this single solid rect |
| 476 | // but it requires to upload a new intervals uniform so we can mimic |
| 477 | // one giant dash |
| 478 | args.fPtsRot[0].fX -= hasStartRect ? startAdj : 0; |
| 479 | args.fPtsRot[1].fX += hasEndRect ? endAdj : 0; |
| 480 | startRect.set(args.fPtsRot, 2); |
| 481 | startRect.outset(strokeAdj, halfSrcStroke); |
| 482 | hasStartRect = true; |
| 483 | hasEndRect = false; |
| 484 | lineDone = true; |
| 485 | |
| 486 | SkPoint devicePts[2]; |
| 487 | args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2); |
| 488 | SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]); |
| 489 | if (hasCap) { |
| 490 | lineLength += 2.f * halfDevStroke; |
| 491 | } |
| 492 | devIntervals[0] = lineLength; |
| 493 | } |
| 494 | |
| 495 | totalRectCount += !lineDone ? 1 : 0; |
| 496 | totalRectCount += hasStartRect ? 1 : 0; |
| 497 | totalRectCount += hasEndRect ? 1 : 0; |
| 498 | |
| 499 | if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) { |
| 500 | // need to adjust this for round caps to correctly set the dashPos attrib on |
| 501 | // vertices |
| 502 | startOffset -= halfDevStroke; |
| 503 | } |
| 504 | |
| 505 | DashDraw& draw = draws.push_back(); |
| 506 | if (!lineDone) { |
| 507 | SkPoint devicePts[2]; |
| 508 | args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2); |
| 509 | draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]); |
| 510 | if (hasCap) { |
| 511 | draw.fLineLength += 2.f * halfDevStroke; |
| 512 | } |
| 513 | |
| 514 | bounds.set(args.fPtsRot[0].fX, args.fPtsRot[0].fY, |
| 515 | args.fPtsRot[1].fX, args.fPtsRot[1].fY); |
| 516 | bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke); |
| 517 | } |
| 518 | |
| 519 | if (hasStartRect) { |
| 520 | SkASSERT(useAA); // so that we know bloatX and bloatY have been set |
| 521 | startRect.outset(bloatX, bloatY); |
| 522 | } |
| 523 | |
| 524 | if (hasEndRect) { |
| 525 | SkASSERT(useAA); // so that we know bloatX and bloatY have been set |
| 526 | endRect.outset(bloatX, bloatY); |
| 527 | } |
| 528 | |
| 529 | draw.fStartOffset = startOffset; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 530 | draw.fDevBloatX = devBloatX; |
| 531 | draw.fDevBloatY = devBloatY; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 532 | draw.fHalfDevStroke = halfDevStroke; |
| 533 | draw.fStrokeWidth = strokeWidth; |
| 534 | draw.fHasStartRect = hasStartRect; |
| 535 | draw.fLineDone = lineDone; |
| 536 | draw.fHasEndRect = hasEndRect; |
| 537 | } |
| 538 | |
| 539 | const GrVertexBuffer* vertexBuffer; |
| 540 | int firstVertex; |
| 541 | |
| 542 | size_t vertexStride = gp->getVertexStride(); |
| 543 | void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 544 | totalRectCount * kVertsPerDash, |
| 545 | &vertexBuffer, |
| 546 | &firstVertex); |
| 547 | |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 548 | if (!vertices || !batchTarget->quadIndexBuffer()) { |
| 549 | SkDebugf("Could not allocate buffers\n"); |
| 550 | return; |
| 551 | } |
| 552 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 553 | int curVIdx = 0; |
| 554 | int rectIndex = 0; |
| 555 | for (int i = 0; i < instanceCount; i++) { |
| 556 | Geometry& args = fGeoData[i]; |
| 557 | |
| 558 | if (!draws[i].fLineDone) { |
| 559 | if (fullDash) { |
| 560 | setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 561 | draws[i].fStartOffset, draws[i].fDevBloatX, |
| 562 | draws[i].fDevBloatY, draws[i].fLineLength, |
| 563 | draws[i].fHalfDevStroke, args.fIntervals[0], |
| 564 | args.fIntervals[1], draws[i].fStrokeWidth, |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 565 | capType, gp->getVertexStride()); |
| 566 | } else { |
| 567 | SkPoint* verts = reinterpret_cast<SkPoint*>(vertices); |
| 568 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
| 569 | setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts); |
| 570 | } |
| 571 | curVIdx += 4; |
| 572 | } |
| 573 | rectIndex++; |
| 574 | |
| 575 | if (draws[i].fHasStartRect) { |
| 576 | if (fullDash) { |
| 577 | setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 578 | draws[i].fStartOffset, draws[i].fDevBloatX, |
| 579 | draws[i].fDevBloatY, args.fIntervals[0], |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 580 | draws[i].fHalfDevStroke, args.fIntervals[0], |
| 581 | args.fIntervals[1], draws[i].fStrokeWidth, capType, |
| 582 | gp->getVertexStride()); |
| 583 | } else { |
| 584 | SkPoint* verts = reinterpret_cast<SkPoint*>(vertices); |
| 585 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
| 586 | setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts); |
| 587 | } |
| 588 | |
| 589 | curVIdx += 4; |
| 590 | } |
| 591 | rectIndex++; |
| 592 | |
| 593 | if (draws[i].fHasEndRect) { |
| 594 | if (fullDash) { |
| 595 | setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 596 | draws[i].fStartOffset, draws[i].fDevBloatX, |
| 597 | draws[i].fDevBloatY, args.fIntervals[0], |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 598 | draws[i].fHalfDevStroke, args.fIntervals[0], |
| 599 | args.fIntervals[1], draws[i].fStrokeWidth, capType, |
| 600 | gp->getVertexStride()); |
| 601 | } else { |
| 602 | SkPoint* verts = reinterpret_cast<SkPoint*>(vertices); |
| 603 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
| 604 | setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts); |
| 605 | } |
| 606 | curVIdx += 4; |
| 607 | } |
| 608 | rectIndex++; |
| 609 | } |
| 610 | |
| 611 | const GrIndexBuffer* dashIndexBuffer = batchTarget->quadIndexBuffer(); |
| 612 | |
| 613 | GrDrawTarget::DrawInfo drawInfo; |
| 614 | drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); |
| 615 | drawInfo.setStartVertex(0); |
| 616 | drawInfo.setStartIndex(0); |
| 617 | drawInfo.setVerticesPerInstance(kVertsPerDash); |
| 618 | drawInfo.setIndicesPerInstance(kIndicesPerDash); |
| 619 | drawInfo.adjustStartVertex(firstVertex); |
| 620 | drawInfo.setVertexBuffer(vertexBuffer); |
| 621 | drawInfo.setIndexBuffer(dashIndexBuffer); |
| 622 | |
| 623 | int maxInstancesPerDraw = dashIndexBuffer->maxQuads(); |
| 624 | while (totalRectCount) { |
| 625 | drawInfo.setInstanceCount(SkTMin(totalRectCount, maxInstancesPerDraw)); |
| 626 | drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance()); |
| 627 | drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance()); |
| 628 | |
| 629 | batchTarget->draw(drawInfo); |
| 630 | |
| 631 | drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount()); |
| 632 | totalRectCount -= drawInfo.instanceCount(); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 637 | |
| 638 | private: |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 639 | DashBatch(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 640 | this->initClassID<DashBatch>(); |
| 641 | fGeoData.push_back(geometry); |
| 642 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 643 | fBatch.fAAMode = aaMode; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 644 | fBatch.fCap = cap; |
| 645 | fBatch.fFullDash = fullDash; |
| 646 | } |
| 647 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 648 | bool onCombineIfPossible(GrBatch* t) override { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 649 | DashBatch* that = t->cast<DashBatch>(); |
| 650 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 651 | if (this->aaMode() != that->aaMode()) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 652 | return false; |
| 653 | } |
| 654 | |
| 655 | if (this->fullDash() != that->fullDash()) { |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | if (this->cap() != that->cap()) { |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | // TODO vertex color |
| 664 | if (this->color() != that->color()) { |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 669 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | GrColor color() const { return fBatch.fColor; } |
| 678 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 679 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 680 | DashAAMode aaMode() const { return fBatch.fAAMode; } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 681 | bool fullDash() const { return fBatch.fFullDash; } |
| 682 | SkPaint::Cap cap() const { return fBatch.fCap; } |
| 683 | |
| 684 | struct BatchTracker { |
| 685 | GrColor fColor; |
| 686 | bool fUsesLocalCoords; |
| 687 | bool fColorIgnored; |
| 688 | bool fCoverageIgnored; |
| 689 | SkPaint::Cap fCap; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 690 | DashAAMode fAAMode; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 691 | bool fFullDash; |
| 692 | }; |
| 693 | |
| 694 | static const int kVertsPerDash = 4; |
| 695 | static const int kIndicesPerDash = 6; |
| 696 | |
| 697 | BatchTracker fBatch; |
| 698 | SkSTArray<1, Geometry, true> fGeoData; |
| 699 | }; |
| 700 | |
| 701 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 702 | bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target, |
| 703 | GrPipelineBuilder* pipelineBuilder, GrColor color, |
| 704 | const SkMatrix& viewMatrix, const SkPoint pts[2], |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 705 | bool useAA, const GrStrokeInfo& strokeInfo) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 706 | const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 707 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 708 | SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 709 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 710 | DashBatch::Geometry geometry; |
| 711 | geometry.fSrcStrokeWidth = strokeInfo.getStrokeRec().getWidth(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 712 | |
| 713 | // the phase should be normalized to be [0, sum of all intervals) |
| 714 | SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]); |
| 715 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 716 | // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 717 | if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 718 | SkMatrix rotMatrix; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 719 | align_to_x_axis(pts, &rotMatrix, geometry.fPtsRot); |
| 720 | if(!rotMatrix.invert(&geometry.fSrcRotInv)) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 721 | SkDebugf("Failed to create invertible rotation matrix!\n"); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 722 | return false; |
| 723 | } |
| 724 | } else { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 725 | geometry.fSrcRotInv.reset(); |
| 726 | memcpy(geometry.fPtsRot, pts, 2 * sizeof(SkPoint)); |
| 727 | } |
| 728 | |
| 729 | // Scale corrections of intervals and stroke from view matrix |
| 730 | calc_dash_scaling(&geometry.fParallelScale, &geometry.fPerpendicularScale, viewMatrix, |
| 731 | geometry.fPtsRot); |
| 732 | |
| 733 | SkScalar offInterval = info.fIntervals[1] * geometry.fParallelScale; |
| 734 | SkScalar strokeWidth = geometry.fSrcStrokeWidth * geometry.fPerpendicularScale; |
| 735 | |
| 736 | if (SkPaint::kSquare_Cap == cap && 0 != geometry.fSrcStrokeWidth) { |
| 737 | // add cap to on interveal and remove from off interval |
| 738 | offInterval -= strokeWidth; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 739 | } |
| 740 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 741 | DashAAMode aaMode = pipelineBuilder->getRenderTarget()->isMultisampled() ? kMSAA_DashAAMode : |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 742 | useAA ? kEdgeAA_DashAAMode : kBW_DashAAMode; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 743 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 744 | // TODO we can do a real rect call if not using fulldash(ie no off interval, not using AA) |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 745 | bool fullDash = offInterval > 0.f || aaMode != kBW_DashAAMode; |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 746 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 747 | geometry.fColor = color; |
| 748 | geometry.fViewMatrix = viewMatrix; |
| 749 | geometry.fPhase = info.fPhase; |
| 750 | geometry.fIntervals[0] = info.fIntervals[0]; |
| 751 | geometry.fIntervals[1] = info.fIntervals[1]; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 752 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 753 | SkAutoTUnref<GrBatch> batch(DashBatch::Create(geometry, cap, aaMode, fullDash)); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 754 | target->drawBatch(pipelineBuilder, batch); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 755 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 756 | return true; |
| 757 | } |
| 758 | |
| 759 | ////////////////////////////////////////////////////////////////////////////// |
| 760 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 761 | class GLDashingCircleEffect; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 762 | |
| 763 | struct DashingCircleBatchTracker { |
| 764 | GrGPInput fInputColorType; |
| 765 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 766 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 767 | }; |
| 768 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 769 | /* |
| 770 | * This effect will draw a dotted line (defined as a dashed lined with round caps and no on |
| 771 | * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo. |
| 772 | * Both of the previous two parameters are in device space. This effect also requires the setting of |
| 773 | * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the |
| 774 | * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we |
| 775 | * transform the line to be horizontal, with the start of line at the origin then shifted to the |
| 776 | * right by half the off interval. The line then goes in the positive x direction. |
| 777 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 778 | class DashingCircleEffect : public GrGeometryProcessor { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 779 | public: |
| 780 | typedef SkPathEffect::DashInfo DashInfo; |
| 781 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 782 | static GrGeometryProcessor* Create(GrColor, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 783 | DashAAMode aaMode, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 784 | const SkMatrix& localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 785 | |
| 786 | virtual ~DashingCircleEffect(); |
| 787 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 788 | const char* name() const override { return "DashingCircleEffect"; } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 789 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 790 | const Attribute* inPosition() const { return fInPosition; } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 791 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 792 | const Attribute* inDashParams() const { return fInDashParams; } |
| 793 | |
| 794 | const Attribute* inCircleParams() const { return fInCircleParams; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 795 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 796 | DashAAMode aaMode() const { return fAAMode; } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 797 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 798 | virtual void getGLProcessorKey(const GrBatchTracker&, |
| 799 | const GrGLCaps&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 800 | GrProcessorKeyBuilder* b) const override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 801 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 802 | virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 803 | const GrGLCaps&) const override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 804 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 805 | void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 806 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 807 | bool onCanMakeEqual(const GrBatchTracker&, |
| 808 | const GrGeometryProcessor&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 809 | const GrBatchTracker&) const override; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 810 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 811 | private: |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 812 | DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 813 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 814 | bool onIsEqual(const GrGeometryProcessor& other) const override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 815 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 816 | void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 817 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 818 | DashAAMode fAAMode; |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 819 | const Attribute* fInPosition; |
| 820 | const Attribute* fInDashParams; |
| 821 | const Attribute* fInCircleParams; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 822 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 823 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 824 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 825 | typedef GrGeometryProcessor INHERITED; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | ////////////////////////////////////////////////////////////////////////////// |
| 829 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 830 | class GLDashingCircleEffect : public GrGLGeometryProcessor { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 831 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 832 | GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 833 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 834 | void onEmitCode(EmitArgs&, GrGPArgs*) override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 835 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 836 | static inline void GenKey(const GrGeometryProcessor&, |
| 837 | const GrBatchTracker&, |
| 838 | const GrGLCaps&, |
| 839 | GrProcessorKeyBuilder*); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 840 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 841 | virtual void setData(const GrGLProgramDataManager&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 842 | const GrPrimitiveProcessor&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 843 | const GrBatchTracker&) override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 844 | |
| 845 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 846 | UniformHandle fParamUniform; |
| 847 | UniformHandle fColorUniform; |
| 848 | GrColor fColor; |
| 849 | SkScalar fPrevRadius; |
| 850 | SkScalar fPrevCenterX; |
| 851 | SkScalar fPrevIntervalLength; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 852 | typedef GrGLGeometryProcessor INHERITED; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 853 | }; |
| 854 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 855 | GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&, |
| 856 | const GrBatchTracker&) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 857 | fColor = GrColor_ILLEGAL; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 858 | fPrevRadius = SK_ScalarMin; |
| 859 | fPrevCenterX = SK_ScalarMin; |
| 860 | fPrevIntervalLength = SK_ScalarMax; |
| 861 | } |
| 862 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 863 | void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 864 | const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 865 | const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>(); |
| 866 | GrGLGPBuilder* pb = args.fPB; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 867 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 868 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 869 | // emit attributes |
| 870 | vsBuilder->emitAttributes(dce); |
| 871 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 872 | // XY are dashPos, Z is dashInterval |
| 873 | GrGLVertToFrag dashParams(kVec3f_GrSLType); |
| 874 | args.fPB->addVarying("DashParam", &dashParams); |
| 875 | vsBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->fName); |
| 876 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 877 | // x refers to circle radius - 0.5, y refers to cicle's center x coord |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 878 | GrGLVertToFrag circleParams(kVec2f_GrSLType); |
| 879 | args.fPB->addVarying("CircleParams", &circleParams); |
| 880 | vsBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->fName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 881 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 882 | // Setup pass through color |
| 883 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform); |
| 884 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 885 | // Setup position |
joshualitt | dd21987 | 2015-02-12 14:48:42 -0800 | [diff] [blame] | 886 | this->setupPosition(pb, gpArgs, dce.inPosition()->fName, dce.viewMatrix()); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 887 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 888 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 889 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, dce.inPosition()->fName, dce.localMatrix(), |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 890 | args.fTransformsIn, args.fTransformsOut); |
| 891 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 892 | // transforms all points so that we can compare them to our test circle |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 893 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 894 | fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;", |
| 895 | dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(), |
| 896 | dashParams.fsIn()); |
| 897 | fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn()); |
| 898 | fsBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn()); |
| 899 | fsBuilder->codeAppend("float dist = length(center - fragPosShifted);"); |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 900 | if (dce.aaMode() != kBW_DashAAMode) { |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 901 | fsBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn()); |
| 902 | fsBuilder->codeAppend("diff = 1.0 - diff;"); |
| 903 | fsBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);"); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 904 | } else { |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 905 | fsBuilder->codeAppendf("float alpha = 1.0;"); |
| 906 | fsBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn()); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 907 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 908 | fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 909 | } |
| 910 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 911 | void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 912 | const GrPrimitiveProcessor& processor, |
| 913 | const GrBatchTracker& bt) { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 914 | this->setUniformViewMatrix(pdman, processor.viewMatrix()); |
| 915 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 916 | const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>(); |
| 917 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 918 | GrGLfloat c[4]; |
| 919 | GrColorToRGBAFloat(local.fColor, c); |
| 920 | pdman.set4fv(fColorUniform, 1, c); |
| 921 | fColor = local.fColor; |
| 922 | } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 923 | } |
| 924 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 925 | void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 926 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 927 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 928 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 929 | const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>(); |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 930 | const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>(); |
| 931 | uint32_t key = 0; |
| 932 | key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0; |
| 933 | key |= ComputePosKey(gp.viewMatrix()) << 1; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 934 | key |= dce.aaMode() << 8; |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 935 | b->add32(key << 16 | local.fInputColorType); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | ////////////////////////////////////////////////////////////////////////////// |
| 939 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 940 | GrGeometryProcessor* DashingCircleEffect::Create(GrColor color, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 941 | DashAAMode aaMode, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 942 | const SkMatrix& localMatrix) { |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 943 | return SkNEW_ARGS(DashingCircleEffect, (color, aaMode, localMatrix)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | DashingCircleEffect::~DashingCircleEffect() {} |
| 947 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 948 | void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 949 | out->setUnknownSingleComponent(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 950 | } |
| 951 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 952 | void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 953 | const GrGLCaps& caps, |
| 954 | GrProcessorKeyBuilder* b) const { |
| 955 | GLDashingCircleEffect::GenKey(*this, bt, caps, b); |
| 956 | } |
| 957 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 958 | GrGLPrimitiveProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt, |
| 959 | const GrGLCaps&) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 960 | return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 961 | } |
| 962 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 963 | DashingCircleEffect::DashingCircleEffect(GrColor color, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 964 | DashAAMode aaMode, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 965 | const SkMatrix& localMatrix) |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 966 | : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 967 | this->initClassID<DashingCircleEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 968 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 969 | fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType)); |
| 970 | fInCircleParams = &this->addVertexAttrib(Attribute("inCircleParams", |
| 971 | kVec2f_GrVertexAttribType)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 972 | } |
| 973 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 974 | bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 975 | const DashingCircleEffect& dce = other.cast<DashingCircleEffect>(); |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 976 | return fAAMode == dce.fAAMode; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 977 | } |
| 978 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 979 | void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 980 | DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>(); |
| 981 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 982 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 983 | } |
| 984 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 985 | bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m, |
| 986 | const GrGeometryProcessor& that, |
| 987 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 988 | const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>(); |
| 989 | const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 990 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 991 | that, theirs.fUsesLocalCoords) && |
| 992 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 993 | theirs.fInputColorType, theirs.fColor); |
| 994 | } |
| 995 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 996 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 997 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 998 | GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random, |
| 999 | GrContext*, |
| 1000 | const GrDrawTargetCaps& caps, |
| 1001 | GrTexture*[]) { |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1002 | DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount)); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1003 | return DashingCircleEffect::Create(GrRandomColor(random), |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1004 | aaMode, GrProcessorUnitTest::TestMatrix(random)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | ////////////////////////////////////////////////////////////////////////////// |
| 1008 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1009 | class GLDashingLineEffect; |
| 1010 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1011 | struct DashingLineBatchTracker { |
| 1012 | GrGPInput fInputColorType; |
| 1013 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1014 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1015 | }; |
| 1016 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1017 | /* |
| 1018 | * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the |
| 1019 | * length and spacing by the DashInfo. Both of the previous two parameters are in device space. |
| 1020 | * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the |
| 1021 | * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the |
| 1022 | * vertex coords (in device space) if we transform the line to be horizontal, with the start of |
| 1023 | * line at the origin then shifted to the right by half the off interval. The line then goes in the |
| 1024 | * positive x direction. |
| 1025 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 1026 | class DashingLineEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1027 | public: |
| 1028 | typedef SkPathEffect::DashInfo DashInfo; |
| 1029 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1030 | static GrGeometryProcessor* Create(GrColor, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1031 | DashAAMode aaMode, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1032 | const SkMatrix& localMatrix); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1033 | |
| 1034 | virtual ~DashingLineEffect(); |
| 1035 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1036 | const char* name() const override { return "DashingEffect"; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1037 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 1038 | const Attribute* inPosition() const { return fInPosition; } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 1039 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1040 | const Attribute* inDashParams() const { return fInDashParams; } |
| 1041 | |
| 1042 | const Attribute* inRectParams() const { return fInRectParams; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 1043 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1044 | DashAAMode aaMode() const { return fAAMode; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1045 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1046 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
| 1047 | const GrGLCaps& caps, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1048 | GrProcessorKeyBuilder* b) const override; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1049 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1050 | virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1051 | const GrGLCaps&) const override; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1052 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1053 | void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1054 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1055 | bool onCanMakeEqual(const GrBatchTracker&, |
| 1056 | const GrGeometryProcessor&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1057 | const GrBatchTracker&) const override; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1058 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1059 | private: |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1060 | DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1061 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1062 | bool onIsEqual(const GrGeometryProcessor& other) const override; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1063 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1064 | void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 1065 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1066 | DashAAMode fAAMode; |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1067 | const Attribute* fInPosition; |
| 1068 | const Attribute* fInDashParams; |
| 1069 | const Attribute* fInRectParams; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1070 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1071 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1072 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 1073 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1074 | }; |
| 1075 | |
| 1076 | ////////////////////////////////////////////////////////////////////////////// |
| 1077 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 1078 | class GLDashingLineEffect : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1079 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1080 | GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1081 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1082 | void onEmitCode(EmitArgs&, GrGPArgs*) override; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1083 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 1084 | static inline void GenKey(const GrGeometryProcessor&, |
| 1085 | const GrBatchTracker&, |
| 1086 | const GrGLCaps&, |
| 1087 | GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1088 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 1089 | virtual void setData(const GrGLProgramDataManager&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1090 | const GrPrimitiveProcessor&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1091 | const GrBatchTracker&) override; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1092 | |
| 1093 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1094 | GrColor fColor; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1095 | UniformHandle fColorUniform; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 1096 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1097 | }; |
| 1098 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1099 | GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&, |
| 1100 | const GrBatchTracker&) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1101 | fColor = GrColor_ILLEGAL; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 1104 | void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 1105 | const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1106 | const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>(); |
| 1107 | GrGLGPBuilder* pb = args.fPB; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 1108 | |
| 1109 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 1110 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1111 | // emit attributes |
| 1112 | vsBuilder->emitAttributes(de); |
| 1113 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1114 | // XY refers to dashPos, Z is the dash interval length |
| 1115 | GrGLVertToFrag inDashParams(kVec3f_GrSLType); |
| 1116 | args.fPB->addVarying("DashParams", &inDashParams); |
| 1117 | vsBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->fName); |
| 1118 | |
| 1119 | // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5), |
| 1120 | // respectively. |
| 1121 | GrGLVertToFrag inRectParams(kVec4f_GrSLType); |
| 1122 | args.fPB->addVarying("RectParams", &inRectParams); |
| 1123 | vsBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->fName); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 1124 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1125 | // Setup pass through color |
| 1126 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform); |
| 1127 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1128 | // Setup position |
joshualitt | dd21987 | 2015-02-12 14:48:42 -0800 | [diff] [blame] | 1129 | this->setupPosition(pb, gpArgs, de.inPosition()->fName, de.viewMatrix()); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 1130 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1131 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 1132 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, de.inPosition()->fName, de.localMatrix(), |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1133 | args.fTransformsIn, args.fTransformsOut); |
| 1134 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1135 | // transforms all points so that we can compare them to our test rect |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 1136 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1137 | fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;", |
| 1138 | inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(), |
| 1139 | inDashParams.fsIn()); |
| 1140 | fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn()); |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1141 | if (de.aaMode() == kEdgeAA_DashAAMode) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1142 | // The amount of coverage removed in x and y by the edges is computed as a pair of negative |
| 1143 | // numbers, xSub and ySub. |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1144 | fsBuilder->codeAppend("float xSub, ySub;"); |
| 1145 | fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn()); |
| 1146 | fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn()); |
| 1147 | fsBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn()); |
| 1148 | fsBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn()); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1149 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel |
| 1150 | // covered. |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1151 | fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));"); |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1152 | } else if (de.aaMode() == kMSAA_DashAAMode) { |
| 1153 | // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle |
| 1154 | // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha. |
| 1155 | fsBuilder->codeAppend("float xSub;"); |
| 1156 | fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn()); |
| 1157 | fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn()); |
| 1158 | // Now compute coverage in x to get the fraction of the pixel covered. |
| 1159 | fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0));"); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1160 | } else { |
| 1161 | // Assuming the bounding geometry is tight so no need to check y values |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1162 | fsBuilder->codeAppendf("float alpha = 1.0;"); |
| 1163 | fsBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;", |
| 1164 | inRectParams.fsIn()); |
| 1165 | fsBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;", |
| 1166 | inRectParams.fsIn()); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1167 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 1168 | fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1171 | void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1172 | const GrPrimitiveProcessor& processor, |
| 1173 | const GrBatchTracker& bt) { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 1174 | this->setUniformViewMatrix(pdman, processor.viewMatrix()); |
| 1175 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1176 | const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>(); |
| 1177 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 1178 | GrGLfloat c[4]; |
| 1179 | GrColorToRGBAFloat(local.fColor, c); |
| 1180 | pdman.set4fv(fColorUniform, 1, c); |
| 1181 | fColor = local.fColor; |
| 1182 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 1185 | void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1186 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 1187 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1188 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1189 | const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>(); |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 1190 | const DashingLineEffect& de = gp.cast<DashingLineEffect>(); |
| 1191 | uint32_t key = 0; |
| 1192 | key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0; |
| 1193 | key |= ComputePosKey(gp.viewMatrix()) << 1; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1194 | key |= de.aaMode() << 8; |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 1195 | b->add32(key << 16 | local.fInputColorType); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | ////////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 1199 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1200 | GrGeometryProcessor* DashingLineEffect::Create(GrColor color, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1201 | DashAAMode aaMode, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1202 | const SkMatrix& localMatrix) { |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1203 | return SkNEW_ARGS(DashingLineEffect, (color, aaMode, localMatrix)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | DashingLineEffect::~DashingLineEffect() {} |
| 1207 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 1208 | void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 1209 | out->setUnknownSingleComponent(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1212 | void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 1213 | const GrGLCaps& caps, |
| 1214 | GrProcessorKeyBuilder* b) const { |
| 1215 | GLDashingLineEffect::GenKey(*this, bt, caps, b); |
| 1216 | } |
| 1217 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1218 | GrGLPrimitiveProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt, |
| 1219 | const GrGLCaps&) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1220 | return SkNEW_ARGS(GLDashingLineEffect, (*this, bt)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1223 | DashingLineEffect::DashingLineEffect(GrColor color, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1224 | DashAAMode aaMode, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1225 | const SkMatrix& localMatrix) |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1226 | : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1227 | this->initClassID<DashingLineEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 1228 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1229 | fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType)); |
| 1230 | fInRectParams = &this->addVertexAttrib(Attribute("inRect", kVec4f_GrVertexAttribType)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 1233 | bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 1234 | const DashingLineEffect& de = other.cast<DashingLineEffect>(); |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1235 | return fAAMode == de.fAAMode; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 1238 | void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1239 | DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>(); |
| 1240 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1241 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1244 | bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m, |
| 1245 | const GrGeometryProcessor& that, |
| 1246 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1247 | const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>(); |
| 1248 | const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1249 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 1250 | that, theirs.fUsesLocalCoords) && |
| 1251 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1252 | theirs.fInputColorType, theirs.fColor); |
| 1253 | } |
| 1254 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1255 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1256 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1257 | GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random, |
| 1258 | GrContext*, |
| 1259 | const GrDrawTargetCaps& caps, |
| 1260 | GrTexture*[]) { |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1261 | DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount)); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1262 | return DashingLineEffect::Create(GrRandomColor(random), |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1263 | aaMode, GrProcessorUnitTest::TestMatrix(random)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | ////////////////////////////////////////////////////////////////////////////// |
| 1267 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1268 | static GrGeometryProcessor* create_dash_gp(GrColor color, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1269 | DashAAMode dashAAMode, |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1270 | DashCap cap, |
| 1271 | const SkMatrix& localMatrix) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1272 | switch (cap) { |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1273 | case kRound_DashCap: |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1274 | return DashingCircleEffect::Create(color, dashAAMode, localMatrix); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1275 | case kNonRound_DashCap: |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1276 | return DashingLineEffect::Create(color, dashAAMode, localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1277 | default: |
| 1278 | SkFAIL("Unexpected dashed cap."); |
| 1279 | } |
| 1280 | return NULL; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1281 | } |