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