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