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