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 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 8 | #include "GrDashOp.h" |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 9 | #include "GrAppliedClip.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 11 | #include "GrContext.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 13 | #include "GrCoordTransform.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 14 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 15 | #include "GrDrawOpTest.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 16 | #include "GrGeometryProcessor.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 17 | #include "GrMemoryPool.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 18 | #include "GrOpFlushState.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | #include "GrProcessor.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 20 | #include "GrStyle.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 21 | #include "SkGr.h" |
Brian Salomon | fa3783f | 2018-01-05 13:49:07 -0500 | [diff] [blame] | 22 | #include "SkMatrixPriv.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 23 | #include "SkPointPriv.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 24 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 25 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 26 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 27 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 28 | #include "glsl/GrGLSLVarying.h" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 29 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 30 | #include "ops/GrMeshDrawOp.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 31 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 32 | using AAMode = GrDashOp::AAMode; |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 33 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 36 | // Returns whether or not the gpu can fast path the dash line effect. |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 37 | bool GrDashOp::CanDrawDashLine(const SkPoint pts[2], const GrStyle& style, |
| 38 | const SkMatrix& viewMatrix) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 39 | // Pts must be either horizontal or vertical in src space |
| 40 | if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | // May be able to relax this to include skew. As of now cannot do perspective |
| 45 | // because of the non uniform scaling of bloating a rect |
| 46 | if (!viewMatrix.preservesRightAngles()) { |
| 47 | return false; |
| 48 | } |
| 49 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 50 | if (!style.isDashed() || 2 != style.dashIntervalCnt()) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 54 | const SkScalar* intervals = style.dashIntervals(); |
kkinnunen | 261694c | 2015-05-05 08:00:10 -0700 | [diff] [blame] | 55 | if (0 == intervals[0] && 0 == intervals[1]) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 59 | SkPaint::Cap cap = style.strokeRec().getCap(); |
Greg Daniel | 5fb3056 | 2017-06-29 12:27:48 -0400 | [diff] [blame] | 60 | if (SkPaint::kRound_Cap == cap) { |
| 61 | // Current we don't support round caps unless the on interval is zero |
| 62 | if (intervals[0] != 0.f) { |
| 63 | return false; |
| 64 | } |
| 65 | // If the width of the circle caps in greater than the off interval we will pick up unwanted |
| 66 | // segments of circles at the start and end of the dash line. |
| 67 | if (style.strokeRec().getWidth() > intervals[1]) { |
| 68 | return false; |
| 69 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | namespace { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 76 | struct DashLineVertex { |
| 77 | SkPoint fPos; |
| 78 | SkPoint fDashPos; |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 79 | SkScalar fIntervalLength; |
| 80 | SkRect fRect; |
| 81 | }; |
| 82 | struct DashCircleVertex { |
| 83 | SkPoint fPos; |
| 84 | SkPoint fDashPos; |
| 85 | SkScalar fIntervalLength; |
| 86 | SkScalar fRadius; |
| 87 | SkScalar fCenterX; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 88 | }; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 91 | static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale, |
| 92 | const SkMatrix& viewMatrix, const SkPoint pts[2]) { |
| 93 | SkVector vecSrc = pts[1] - pts[0]; |
Greg Daniel | c96f9b5 | 2017-12-07 15:00:06 -0500 | [diff] [blame] | 94 | if (pts[1] == pts[0]) { |
| 95 | vecSrc.set(1.0, 0.0); |
| 96 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 97 | SkScalar magSrc = vecSrc.length(); |
| 98 | SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0; |
| 99 | vecSrc.scale(invSrc); |
| 100 | |
| 101 | SkVector vecSrcPerp; |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 102 | SkPointPriv::RotateCW(vecSrc, &vecSrcPerp); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 103 | viewMatrix.mapVectors(&vecSrc, 1); |
| 104 | viewMatrix.mapVectors(&vecSrcPerp, 1); |
| 105 | |
| 106 | // parallelScale tells how much to scale along the line parallel to the dash line |
| 107 | // perpScale tells how much to scale in the direction perpendicular to the dash line |
| 108 | *parallelScale = vecSrc.length(); |
| 109 | *perpScale = vecSrcPerp.length(); |
| 110 | } |
| 111 | |
| 112 | // calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1] |
| 113 | // Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = nullptr) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 115 | SkVector vec = pts[1] - pts[0]; |
Greg Daniel | c96f9b5 | 2017-12-07 15:00:06 -0500 | [diff] [blame] | 116 | if (pts[1] == pts[0]) { |
| 117 | vec.set(1.0, 0.0); |
| 118 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 119 | SkScalar mag = vec.length(); |
| 120 | SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| 121 | |
| 122 | vec.scale(inv); |
| 123 | rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 124 | if (ptsRot) { |
| 125 | rotMatrix->mapPoints(ptsRot, pts, 2); |
| 126 | // correction for numerical issues if map doesn't make ptsRot exactly horizontal |
| 127 | ptsRot[1].fY = pts[0].fY; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Assumes phase < sum of all intervals |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 132 | static SkScalar calc_start_adjustment(const SkScalar intervals[2], SkScalar phase) { |
| 133 | SkASSERT(phase < intervals[0] + intervals[1]); |
| 134 | if (phase >= intervals[0] && phase != 0) { |
| 135 | SkScalar srcIntervalLen = intervals[0] + intervals[1]; |
| 136 | return srcIntervalLen - phase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 141 | static SkScalar calc_end_adjustment(const SkScalar intervals[2], const SkPoint pts[2], |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 142 | SkScalar phase, SkScalar* endingInt) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 143 | if (pts[1].fX <= pts[0].fX) { |
| 144 | return 0; |
| 145 | } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 146 | SkScalar srcIntervalLen = intervals[0] + intervals[1]; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 147 | SkScalar totalLen = pts[1].fX - pts[0].fX; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 148 | SkScalar temp = totalLen / srcIntervalLen; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 149 | SkScalar numFullIntervals = SkScalarFloorToScalar(temp); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 150 | *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 151 | temp = *endingInt / srcIntervalLen; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 152 | *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen; |
| 153 | if (0 == *endingInt) { |
| 154 | *endingInt = srcIntervalLen; |
| 155 | } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 156 | if (*endingInt > intervals[0]) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 157 | return *endingInt - intervals[0]; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 162 | enum DashCap { |
| 163 | kRound_DashCap, |
| 164 | kNonRound_DashCap, |
| 165 | }; |
| 166 | |
| 167 | static int kDashVertices = 4; |
| 168 | |
| 169 | template <typename T> |
| 170 | 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] | 171 | SkScalar offset, SkScalar bloatX, SkScalar bloatY, SkScalar len, |
| 172 | SkScalar stroke) { |
| 173 | SkScalar startDashX = offset - bloatX; |
| 174 | SkScalar endDashX = offset + len + bloatX; |
| 175 | SkScalar startDashY = -stroke - bloatY; |
| 176 | SkScalar endDashY = stroke + bloatY; |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 177 | vertices[idx].fDashPos = SkPoint::Make(startDashX , startDashY); |
| 178 | vertices[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY); |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 179 | vertices[idx + 2].fDashPos = SkPoint::Make(endDashX, startDashY); |
| 180 | vertices[idx + 3].fDashPos = SkPoint::Make(endDashX, endDashY); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 181 | |
| 182 | vertices[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop); |
| 183 | vertices[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom); |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 184 | vertices[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fTop); |
| 185 | vertices[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fBottom); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 186 | |
Brian Salomon | fa3783f | 2018-01-05 13:49:07 -0500 | [diff] [blame] | 187 | SkMatrixPriv::MapPointsWithStride(matrix, &vertices[idx].fPos, sizeof(T), 4); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void setup_dashed_rect(const SkRect& rect, void* vertices, int idx, |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 191 | const SkMatrix& matrix, SkScalar offset, SkScalar bloatX, |
| 192 | SkScalar bloatY, SkScalar len, SkScalar stroke, |
| 193 | SkScalar startInterval, SkScalar endInterval, SkScalar strokeWidth, |
| 194 | DashCap cap, const size_t vertexStride) { |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 195 | SkScalar intervalLength = startInterval + endInterval; |
| 196 | |
| 197 | if (kRound_DashCap == cap) { |
| 198 | SkASSERT(vertexStride == sizeof(DashCircleVertex)); |
| 199 | DashCircleVertex* verts = reinterpret_cast<DashCircleVertex*>(vertices); |
| 200 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 201 | setup_dashed_rect_common<DashCircleVertex>(rect, matrix, verts, idx, offset, bloatX, |
| 202 | bloatY, len, stroke); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 203 | |
| 204 | SkScalar radius = SkScalarHalf(strokeWidth) - 0.5f; |
| 205 | SkScalar centerX = SkScalarHalf(endInterval); |
| 206 | |
| 207 | for (int i = 0; i < kDashVertices; i++) { |
| 208 | verts[idx + i].fIntervalLength = intervalLength; |
| 209 | verts[idx + i].fRadius = radius; |
| 210 | verts[idx + i].fCenterX = centerX; |
| 211 | } |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 212 | } else { |
| 213 | SkASSERT(kNonRound_DashCap == cap && vertexStride == sizeof(DashLineVertex)); |
| 214 | DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(vertices); |
| 215 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 216 | setup_dashed_rect_common<DashLineVertex>(rect, matrix, verts, idx, offset, bloatX, |
| 217 | bloatY, len, stroke); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 218 | |
| 219 | SkScalar halfOffLen = SkScalarHalf(endInterval); |
| 220 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 221 | SkRect rectParam; |
| 222 | rectParam.set(halfOffLen + 0.5f, -halfStroke + 0.5f, |
| 223 | halfOffLen + startInterval - 0.5f, halfStroke - 0.5f); |
| 224 | for (int i = 0; i < kDashVertices; i++) { |
| 225 | verts[idx + i].fIntervalLength = intervalLength; |
| 226 | verts[idx + i].fRect = rectParam; |
| 227 | } |
| 228 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 229 | } |
| 230 | |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 231 | static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix, |
| 232 | SkPoint* verts) { |
| 233 | verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop); |
| 234 | verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom); |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 235 | verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fTop); |
| 236 | verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fBottom); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 237 | matrix.mapPoints(&verts[idx], 4); |
| 238 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 239 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 240 | |
| 241 | /** |
| 242 | * An GrGeometryProcessor that renders a dashed line. |
| 243 | * This GrGeometryProcessor is meant for dashed lines that only have a single on/off interval pair. |
| 244 | * Bounding geometry is rendered and the effect computes coverage based on the fragment's |
| 245 | * position relative to the dashed line. |
| 246 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 247 | static sk_sp<GrGeometryProcessor> make_dash_gp(GrColor, |
| 248 | AAMode aaMode, |
| 249 | DashCap cap, |
| 250 | const SkMatrix& localMatrix, |
| 251 | bool usesLocalCoords); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 252 | |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 253 | class DashOp final : public GrMeshDrawOp { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 254 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 255 | DEFINE_OP_CLASS_ID |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 256 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 257 | struct LineData { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 258 | SkMatrix fViewMatrix; |
| 259 | SkMatrix fSrcRotInv; |
| 260 | SkPoint fPtsRot[2]; |
| 261 | SkScalar fSrcStrokeWidth; |
| 262 | SkScalar fPhase; |
| 263 | SkScalar fIntervals[2]; |
| 264 | SkScalar fParallelScale; |
| 265 | SkScalar fPerpendicularScale; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 266 | }; |
| 267 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 268 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 269 | GrPaint&& paint, |
| 270 | const LineData& geometry, |
| 271 | SkPaint::Cap cap, |
| 272 | AAMode aaMode, bool fullDash, |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 273 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 274 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 275 | |
| 276 | return pool->allocate<DashOp>(std::move(paint), geometry, cap, |
| 277 | aaMode, fullDash, stencilSettings); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 280 | const char* name() const override { return "DashOp"; } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 281 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 282 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 283 | fProcessorSet.visitProxies(func); |
| 284 | } |
| 285 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 286 | SkString dumpInfo() const override { |
| 287 | SkString string; |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 288 | for (const auto& geo : fLines) { |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 289 | string.appendf("Pt0: [%.2f, %.2f], Pt1: [%.2f, %.2f], Width: %.2f, Ival0: %.2f, " |
| 290 | "Ival1 : %.2f, Phase: %.2f\n", |
| 291 | geo.fPtsRot[0].fX, geo.fPtsRot[0].fY, |
| 292 | geo.fPtsRot[1].fX, geo.fPtsRot[1].fY, |
| 293 | geo.fSrcStrokeWidth, |
| 294 | geo.fIntervals[0], |
| 295 | geo.fIntervals[1], |
| 296 | geo.fPhase); |
| 297 | } |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 298 | string += fProcessorSet.dumpProcessors(); |
| 299 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 300 | return string; |
| 301 | } |
| 302 | |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 303 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 304 | FixedFunctionFlags flags = FixedFunctionFlags::kNone; |
| 305 | if (AAMode::kCoverageWithMSAA == fAAMode) { |
| 306 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 307 | } |
Brian Salomon | 89717fc | 2017-07-13 11:25:18 -0400 | [diff] [blame] | 308 | if (fStencilSettings != &GrUserStencilSettings::kUnused) { |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 309 | flags |= FixedFunctionFlags::kUsesStencil; |
| 310 | } |
| 311 | return flags; |
| 312 | } |
| 313 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 314 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 315 | GrProcessorAnalysisCoverage coverage; |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 316 | if (AAMode::kNone == fAAMode && !clip->numClipCoverageFragmentProcessors()) { |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 317 | coverage = GrProcessorAnalysisCoverage::kNone; |
| 318 | } else { |
| 319 | coverage = GrProcessorAnalysisCoverage::kSingleChannel; |
| 320 | } |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 321 | auto analysis = fProcessorSet.finalize(fColor, coverage, clip, false, caps, &fColor); |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 322 | fDisallowCombineOnTouchOrOverlap = analysis.requiresDstTexture() || |
| 323 | (fProcessorSet.xferProcessor() && |
| 324 | fProcessorSet.xferProcessor()->xferBarrierType(caps)); |
| 325 | fUsesLocalCoords = analysis.usesLocalCoords(); |
| 326 | return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo; |
| 327 | } |
| 328 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 329 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 330 | friend class GrOpMemoryPool; // for ctor |
| 331 | |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 332 | DashOp(GrPaint&& paint, const LineData& geometry, SkPaint::Cap cap, AAMode aaMode, |
| 333 | bool fullDash, const GrUserStencilSettings* stencilSettings) |
| 334 | : INHERITED(ClassID()) |
| 335 | , fColor(paint.getColor()) |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 336 | , fFullDash(fullDash) |
Brian Salomon | 89717fc | 2017-07-13 11:25:18 -0400 | [diff] [blame] | 337 | , fCap(cap) |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 338 | , fAAMode(aaMode) |
| 339 | , fProcessorSet(std::move(paint)) |
| 340 | , fStencilSettings(stencilSettings) { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 341 | fLines.push_back(geometry); |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 342 | |
| 343 | // compute bounds |
| 344 | SkScalar halfStrokeWidth = 0.5f * geometry.fSrcStrokeWidth; |
| 345 | SkScalar xBloat = SkPaint::kButt_Cap == cap ? 0 : halfStrokeWidth; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 346 | SkRect bounds; |
| 347 | bounds.set(geometry.fPtsRot[0], geometry.fPtsRot[1]); |
| 348 | bounds.outset(xBloat, halfStrokeWidth); |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 349 | |
| 350 | // Note, we actually create the combined matrix here, and save the work |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 351 | SkMatrix& combinedMatrix = fLines[0].fSrcRotInv; |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 352 | combinedMatrix.postConcat(geometry.fViewMatrix); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 353 | |
| 354 | IsZeroArea zeroArea = geometry.fSrcStrokeWidth ? IsZeroArea::kNo : IsZeroArea::kYes; |
| 355 | HasAABloat aaBloat = (aaMode == AAMode::kNone) ? HasAABloat ::kNo : HasAABloat::kYes; |
| 356 | this->setTransformedBounds(bounds, combinedMatrix, aaBloat, zeroArea); |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 357 | } |
| 358 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 359 | struct DashDraw { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 360 | DashDraw(const LineData& geo) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 361 | memcpy(fPtsRot, geo.fPtsRot, sizeof(geo.fPtsRot)); |
| 362 | memcpy(fIntervals, geo.fIntervals, sizeof(geo.fIntervals)); |
| 363 | fPhase = geo.fPhase; |
| 364 | } |
| 365 | SkPoint fPtsRot[2]; |
| 366 | SkScalar fIntervals[2]; |
| 367 | SkScalar fPhase; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 368 | SkScalar fStartOffset; |
| 369 | SkScalar fStrokeWidth; |
| 370 | SkScalar fLineLength; |
| 371 | SkScalar fHalfDevStroke; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 372 | SkScalar fDevBloatX; |
| 373 | SkScalar fDevBloatY; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 374 | bool fLineDone; |
| 375 | bool fHasStartRect; |
| 376 | bool fHasEndRect; |
| 377 | }; |
| 378 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 379 | void onPrepareDraws(Target* target) override { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 380 | int instanceCount = fLines.count(); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 381 | SkPaint::Cap cap = this->cap(); |
joshualitt | e494a58 | 2015-08-03 09:32:36 -0700 | [diff] [blame] | 382 | bool isRoundCap = SkPaint::kRound_Cap == cap; |
| 383 | DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 384 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 385 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 386 | if (this->fullDash()) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 387 | gp = make_dash_gp(this->color(), this->aaMode(), capType, this->viewMatrix(), |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 388 | fUsesLocalCoords); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 389 | } else { |
| 390 | // Set up the vertex data for the line and start/end dashes |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 391 | using namespace GrDefaultGeoProcFactory; |
| 392 | Color color(this->color()); |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 393 | LocalCoords::Type localCoordsType = |
| 394 | fUsesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 395 | gp = MakeForDeviceSpace(target->caps().shaderCaps(), |
| 396 | color, |
| 397 | Coverage::kSolid_Type, |
| 398 | localCoordsType, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 399 | this->viewMatrix()); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | if (!gp) { |
| 403 | SkDebugf("Could not create GrGeometryProcessor\n"); |
| 404 | return; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 405 | } |
| 406 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 407 | // useAA here means Edge AA or MSAA |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 408 | bool useAA = this->aaMode() != AAMode::kNone; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 409 | bool fullDash = this->fullDash(); |
| 410 | |
| 411 | // We do two passes over all of the dashes. First we setup the start, end, and bounds, |
| 412 | // rectangles. We preserve all of this work in the rects / draws arrays below. Then we |
| 413 | // iterate again over these decomposed dashes to generate vertices |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 414 | static const int kNumStackDashes = 128; |
| 415 | SkSTArray<kNumStackDashes, SkRect, true> rects; |
| 416 | SkSTArray<kNumStackDashes, DashDraw, true> draws; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 417 | |
| 418 | int totalRectCount = 0; |
joshualitt | d0f5457 | 2015-03-02 12:00:52 -0800 | [diff] [blame] | 419 | int rectOffset = 0; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 420 | rects.push_back_n(3 * instanceCount); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 421 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 422 | const LineData& args = fLines[i]; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 423 | |
| 424 | DashDraw& draw = draws.push_back(args); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 425 | |
Greg Daniel | 95581bb | 2017-06-30 10:36:38 -0400 | [diff] [blame] | 426 | bool hasCap = SkPaint::kButt_Cap != cap; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 427 | |
| 428 | // We always want to at least stroke out half a pixel on each side in device space |
| 429 | // so 0.5f / perpScale gives us this min in src space |
| 430 | SkScalar halfSrcStroke = |
| 431 | SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale); |
| 432 | |
| 433 | SkScalar strokeAdj; |
| 434 | if (!hasCap) { |
| 435 | strokeAdj = 0.f; |
| 436 | } else { |
| 437 | strokeAdj = halfSrcStroke; |
| 438 | } |
| 439 | |
| 440 | SkScalar startAdj = 0; |
| 441 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 442 | bool lineDone = false; |
| 443 | |
| 444 | // Too simplify the algorithm, we always push back rects for start and end rect. |
| 445 | // Otherwise we'd have to track start / end rects for each individual geometry |
joshualitt | d0f5457 | 2015-03-02 12:00:52 -0800 | [diff] [blame] | 446 | SkRect& bounds = rects[rectOffset++]; |
| 447 | SkRect& startRect = rects[rectOffset++]; |
| 448 | SkRect& endRect = rects[rectOffset++]; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 449 | |
| 450 | bool hasStartRect = false; |
| 451 | // If we are using AA, check to see if we are drawing a partial dash at the start. If so |
| 452 | // draw it separately here and adjust our start point accordingly |
| 453 | if (useAA) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 454 | if (draw.fPhase > 0 && draw.fPhase < draw.fIntervals[0]) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 455 | SkPoint startPts[2]; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 456 | startPts[0] = draw.fPtsRot[0]; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 457 | startPts[1].fY = startPts[0].fY; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 458 | startPts[1].fX = SkMinScalar(startPts[0].fX + draw.fIntervals[0] - draw.fPhase, |
| 459 | draw.fPtsRot[1].fX); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 460 | startRect.set(startPts, 2); |
| 461 | startRect.outset(strokeAdj, halfSrcStroke); |
| 462 | |
| 463 | hasStartRect = true; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 464 | startAdj = draw.fIntervals[0] + draw.fIntervals[1] - draw.fPhase; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | // adjustments for start and end of bounding rect so we only draw dash intervals |
| 469 | // contained in the original line segment. |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 470 | startAdj += calc_start_adjustment(draw.fIntervals, draw.fPhase); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 471 | if (startAdj != 0) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 472 | draw.fPtsRot[0].fX += startAdj; |
| 473 | draw.fPhase = 0; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 474 | } |
| 475 | SkScalar endingInterval = 0; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 476 | SkScalar endAdj = calc_end_adjustment(draw.fIntervals, draw.fPtsRot, draw.fPhase, |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 477 | &endingInterval); |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 478 | draw.fPtsRot[1].fX -= endAdj; |
| 479 | if (draw.fPtsRot[0].fX >= draw.fPtsRot[1].fX) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 480 | lineDone = true; |
| 481 | } |
| 482 | |
| 483 | bool hasEndRect = false; |
| 484 | // If we are using AA, check to see if we are drawing a partial dash at then end. If so |
| 485 | // draw it separately here and adjust our end point accordingly |
| 486 | if (useAA && !lineDone) { |
| 487 | // If we adjusted the end then we will not be drawing a partial dash at the end. |
| 488 | // If we didn't adjust the end point then we just need to make sure the ending |
| 489 | // dash isn't a full dash |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 490 | if (0 == endAdj && endingInterval != draw.fIntervals[0]) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 491 | SkPoint endPts[2]; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 492 | endPts[1] = draw.fPtsRot[1]; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 493 | endPts[0].fY = endPts[1].fY; |
| 494 | endPts[0].fX = endPts[1].fX - endingInterval; |
| 495 | |
| 496 | endRect.set(endPts, 2); |
| 497 | endRect.outset(strokeAdj, halfSrcStroke); |
| 498 | |
| 499 | hasEndRect = true; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 500 | endAdj = endingInterval + draw.fIntervals[1]; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 501 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 502 | draw.fPtsRot[1].fX -= endAdj; |
| 503 | if (draw.fPtsRot[0].fX >= draw.fPtsRot[1].fX) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 504 | lineDone = true; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Greg Daniel | c96f9b5 | 2017-12-07 15:00:06 -0500 | [diff] [blame] | 509 | if (draw.fPtsRot[0].fX == draw.fPtsRot[1].fX && |
| 510 | (0 != endAdj || 0 == startAdj) && |
| 511 | hasCap) { |
| 512 | // At this point the fPtsRot[0]/[1] represent the start and end of the inner rect of |
| 513 | // dashes that we want to draw. The only way they can be equal is if the on interval |
| 514 | // is zero (or an edge case if the end of line ends at a full off interval, but this |
| 515 | // is handled as well). Thus if the on interval is zero then we need to draw a cap |
| 516 | // at this position if the stroke has caps. The spec says we only draw this point if |
| 517 | // point lies between [start of line, end of line). Thus we check if we are at the |
| 518 | // end (but not the start), and if so we don't draw the cap. |
| 519 | lineDone = false; |
| 520 | } |
| 521 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 522 | if (startAdj != 0) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 523 | draw.fPhase = 0; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // Change the dashing info from src space into device space |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 527 | SkScalar* devIntervals = draw.fIntervals; |
| 528 | devIntervals[0] = draw.fIntervals[0] * args.fParallelScale; |
| 529 | devIntervals[1] = draw.fIntervals[1] * args.fParallelScale; |
| 530 | SkScalar devPhase = draw.fPhase * args.fParallelScale; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 531 | SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale; |
| 532 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 533 | if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 534 | strokeWidth = 1.f; |
| 535 | } |
| 536 | |
| 537 | SkScalar halfDevStroke = strokeWidth * 0.5f; |
| 538 | |
Greg Daniel | 95581bb | 2017-06-30 10:36:38 -0400 | [diff] [blame] | 539 | if (SkPaint::kSquare_Cap == cap) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 540 | // add cap to on interval and remove from off interval |
| 541 | devIntervals[0] += strokeWidth; |
| 542 | devIntervals[1] -= strokeWidth; |
| 543 | } |
| 544 | SkScalar startOffset = devIntervals[1] * 0.5f + devPhase; |
| 545 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 546 | // For EdgeAA, we bloat in X & Y for both square and round caps. |
| 547 | // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps. |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 548 | SkScalar devBloatX = this->aaMode() == AAMode::kCoverage ? 0.5f : 0.0f; |
| 549 | SkScalar devBloatY; |
| 550 | if (SkPaint::kRound_Cap == cap && this->aaMode() == AAMode::kCoverageWithMSAA) { |
| 551 | devBloatY = 0.5f; |
| 552 | } else { |
| 553 | devBloatY = devBloatX; |
| 554 | } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 555 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 556 | SkScalar bloatX = devBloatX / args.fParallelScale; |
| 557 | SkScalar bloatY = devBloatY / args.fPerpendicularScale; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 558 | |
| 559 | if (devIntervals[1] <= 0.f && useAA) { |
| 560 | // Case when we end up drawing a solid AA rect |
| 561 | // Reset the start rect to draw this single solid rect |
| 562 | // but it requires to upload a new intervals uniform so we can mimic |
| 563 | // one giant dash |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 564 | draw.fPtsRot[0].fX -= hasStartRect ? startAdj : 0; |
| 565 | draw.fPtsRot[1].fX += hasEndRect ? endAdj : 0; |
| 566 | startRect.set(draw.fPtsRot, 2); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 567 | startRect.outset(strokeAdj, halfSrcStroke); |
| 568 | hasStartRect = true; |
| 569 | hasEndRect = false; |
| 570 | lineDone = true; |
| 571 | |
| 572 | SkPoint devicePts[2]; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 573 | args.fViewMatrix.mapPoints(devicePts, draw.fPtsRot, 2); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 574 | SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]); |
| 575 | if (hasCap) { |
| 576 | lineLength += 2.f * halfDevStroke; |
| 577 | } |
| 578 | devIntervals[0] = lineLength; |
| 579 | } |
| 580 | |
| 581 | totalRectCount += !lineDone ? 1 : 0; |
| 582 | totalRectCount += hasStartRect ? 1 : 0; |
| 583 | totalRectCount += hasEndRect ? 1 : 0; |
| 584 | |
| 585 | if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) { |
| 586 | // need to adjust this for round caps to correctly set the dashPos attrib on |
| 587 | // vertices |
| 588 | startOffset -= halfDevStroke; |
| 589 | } |
| 590 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 591 | if (!lineDone) { |
| 592 | SkPoint devicePts[2]; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 593 | args.fViewMatrix.mapPoints(devicePts, draw.fPtsRot, 2); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 594 | draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]); |
| 595 | if (hasCap) { |
| 596 | draw.fLineLength += 2.f * halfDevStroke; |
| 597 | } |
| 598 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 599 | bounds.set(draw.fPtsRot[0].fX, draw.fPtsRot[0].fY, |
| 600 | draw.fPtsRot[1].fX, draw.fPtsRot[1].fY); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 601 | bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke); |
| 602 | } |
| 603 | |
| 604 | if (hasStartRect) { |
| 605 | SkASSERT(useAA); // so that we know bloatX and bloatY have been set |
| 606 | startRect.outset(bloatX, bloatY); |
| 607 | } |
| 608 | |
| 609 | if (hasEndRect) { |
| 610 | SkASSERT(useAA); // so that we know bloatX and bloatY have been set |
| 611 | endRect.outset(bloatX, bloatY); |
| 612 | } |
| 613 | |
| 614 | draw.fStartOffset = startOffset; |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 615 | draw.fDevBloatX = devBloatX; |
| 616 | draw.fDevBloatY = devBloatY; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 617 | draw.fHalfDevStroke = halfDevStroke; |
| 618 | draw.fStrokeWidth = strokeWidth; |
| 619 | draw.fHasStartRect = hasStartRect; |
| 620 | draw.fLineDone = lineDone; |
| 621 | draw.fHasEndRect = hasEndRect; |
| 622 | } |
| 623 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 624 | if (!totalRectCount) { |
| 625 | return; |
| 626 | } |
bsalomon | 8415abe | 2015-05-04 11:41:41 -0700 | [diff] [blame] | 627 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 628 | QuadHelper helper; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 629 | size_t vertexStride; |
| 630 | if (fullDash) { |
| 631 | vertexStride = |
| 632 | SkPaint::kRound_Cap == fCap ? sizeof(DashCircleVertex) : sizeof(DashLineVertex); |
| 633 | } else { |
| 634 | vertexStride = sizeof(SkPoint); |
| 635 | } |
| 636 | SkASSERT(vertexStride == gp->debugOnly_vertexStride()); |
| 637 | void* vertices = helper.init(target, vertexStride, totalRectCount); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 638 | if (!vertices) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 642 | int curVIdx = 0; |
| 643 | int rectIndex = 0; |
| 644 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 645 | const LineData& geom = fLines[i]; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 646 | |
| 647 | if (!draws[i].fLineDone) { |
| 648 | if (fullDash) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 649 | setup_dashed_rect( |
| 650 | rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv, |
| 651 | draws[i].fStartOffset, draws[i].fDevBloatX, draws[i].fDevBloatY, |
| 652 | draws[i].fLineLength, draws[i].fHalfDevStroke, draws[i].fIntervals[0], |
| 653 | draws[i].fIntervals[1], draws[i].fStrokeWidth, capType, vertexStride); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 654 | } else { |
| 655 | SkPoint* verts = reinterpret_cast<SkPoint*>(vertices); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 656 | setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 657 | } |
| 658 | curVIdx += 4; |
| 659 | } |
| 660 | rectIndex++; |
| 661 | |
| 662 | if (draws[i].fHasStartRect) { |
| 663 | if (fullDash) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 664 | setup_dashed_rect( |
| 665 | rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv, |
| 666 | draws[i].fStartOffset, draws[i].fDevBloatX, draws[i].fDevBloatY, |
| 667 | draws[i].fIntervals[0], draws[i].fHalfDevStroke, draws[i].fIntervals[0], |
| 668 | draws[i].fIntervals[1], draws[i].fStrokeWidth, capType, vertexStride); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 669 | } else { |
| 670 | SkPoint* verts = reinterpret_cast<SkPoint*>(vertices); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 671 | setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 672 | } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 673 | curVIdx += 4; |
| 674 | } |
| 675 | rectIndex++; |
| 676 | |
| 677 | if (draws[i].fHasEndRect) { |
| 678 | if (fullDash) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 679 | setup_dashed_rect( |
| 680 | rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv, |
| 681 | draws[i].fStartOffset, draws[i].fDevBloatX, draws[i].fDevBloatY, |
| 682 | draws[i].fIntervals[0], draws[i].fHalfDevStroke, draws[i].fIntervals[0], |
| 683 | draws[i].fIntervals[1], draws[i].fStrokeWidth, capType, vertexStride); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 684 | } else { |
| 685 | SkPoint* verts = reinterpret_cast<SkPoint*>(vertices); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 686 | setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 687 | } |
| 688 | curVIdx += 4; |
| 689 | } |
| 690 | rectIndex++; |
| 691 | } |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 692 | SkASSERT(0 == (curVIdx % 4) && (curVIdx / 4) == totalRectCount); |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 693 | uint32_t pipelineFlags = 0; |
| 694 | if (AAMode::kCoverageWithMSAA == fAAMode) { |
| 695 | pipelineFlags |= GrPipeline::kHWAntialias_Flag; |
| 696 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 697 | auto pipe = target->makePipeline(pipelineFlags, std::move(fProcessorSet), |
| 698 | target->detachAppliedClip()); |
| 699 | helper.recordDraw(target, gp.get(), pipe.fPipeline, pipe.fFixedDynamicState); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 702 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 703 | DashOp* that = t->cast<DashOp>(); |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 704 | if (fProcessorSet != that->fProcessorSet) { |
| 705 | return false; |
| 706 | } |
| 707 | if (fDisallowCombineOnTouchOrOverlap && |
| 708 | GrRectsTouchOrOverlap(this->bounds(), that->bounds())) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 709 | return false; |
| 710 | } |
| 711 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 712 | if (this->aaMode() != that->aaMode()) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 713 | return false; |
| 714 | } |
| 715 | |
| 716 | if (this->fullDash() != that->fullDash()) { |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | if (this->cap() != that->cap()) { |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | // TODO vertex color |
| 725 | if (this->color() != that->color()) { |
| 726 | return false; |
| 727 | } |
| 728 | |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 729 | if (fUsesLocalCoords && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 730 | return false; |
| 731 | } |
| 732 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 733 | fLines.push_back_n(that->fLines.count(), that->fLines.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 734 | this->joinBounds(*that); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 735 | return true; |
| 736 | } |
| 737 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 738 | GrColor color() const { return fColor; } |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 739 | const SkMatrix& viewMatrix() const { return fLines[0].fViewMatrix; } |
| 740 | AAMode aaMode() const { return fAAMode; } |
| 741 | bool fullDash() const { return fFullDash; } |
| 742 | SkPaint::Cap cap() const { return fCap; } |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 743 | |
| 744 | static const int kVertsPerDash = 4; |
| 745 | static const int kIndicesPerDash = 6; |
| 746 | |
Brian Salomon | beae8a9 | 2017-07-12 16:58:54 +0000 | [diff] [blame] | 747 | SkSTArray<1, LineData, true> fLines; |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 748 | GrColor fColor; |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 749 | bool fDisallowCombineOnTouchOrOverlap : 1; |
| 750 | bool fUsesLocalCoords : 1; |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 751 | bool fFullDash : 1; |
Brian Salomon | 89717fc | 2017-07-13 11:25:18 -0400 | [diff] [blame] | 752 | // We use 3 bits for this 3-value enum because MSVS makes the underlying types signed. |
| 753 | SkPaint::Cap fCap : 3; |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 754 | AAMode fAAMode; |
| 755 | GrProcessorSet fProcessorSet; |
| 756 | const GrUserStencilSettings* fStencilSettings; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 757 | |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 758 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 759 | }; |
| 760 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 761 | std::unique_ptr<GrDrawOp> GrDashOp::MakeDashLineOp(GrContext* context, |
| 762 | GrPaint&& paint, |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 763 | const SkMatrix& viewMatrix, |
| 764 | const SkPoint pts[2], |
| 765 | AAMode aaMode, |
| 766 | const GrStyle& style, |
| 767 | const GrUserStencilSettings* stencilSettings) { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 768 | SkASSERT(GrDashOp::CanDrawDashLine(pts, style, viewMatrix)); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 769 | const SkScalar* intervals = style.dashIntervals(); |
| 770 | SkScalar phase = style.dashPhase(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 771 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 772 | SkPaint::Cap cap = style.strokeRec().getCap(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 773 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 774 | DashOp::LineData lineData; |
| 775 | lineData.fSrcStrokeWidth = style.strokeRec().getWidth(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 776 | |
| 777 | // the phase should be normalized to be [0, sum of all intervals) |
kkinnunen | 261694c | 2015-05-05 08:00:10 -0700 | [diff] [blame] | 778 | SkASSERT(phase >= 0 && phase < intervals[0] + intervals[1]); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 779 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 780 | // 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] | 781 | if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 782 | SkMatrix rotMatrix; |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 783 | align_to_x_axis(pts, &rotMatrix, lineData.fPtsRot); |
| 784 | if (!rotMatrix.invert(&lineData.fSrcRotInv)) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 785 | SkDebugf("Failed to create invertible rotation matrix!\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 786 | return nullptr; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 787 | } |
| 788 | } else { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 789 | lineData.fSrcRotInv.reset(); |
| 790 | memcpy(lineData.fPtsRot, pts, 2 * sizeof(SkPoint)); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | // Scale corrections of intervals and stroke from view matrix |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 794 | calc_dash_scaling(&lineData.fParallelScale, &lineData.fPerpendicularScale, viewMatrix, |
| 795 | lineData.fPtsRot); |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 796 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 797 | SkScalar offInterval = intervals[1] * lineData.fParallelScale; |
| 798 | SkScalar strokeWidth = lineData.fSrcStrokeWidth * lineData.fPerpendicularScale; |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 799 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 800 | if (SkPaint::kSquare_Cap == cap && 0 != lineData.fSrcStrokeWidth) { |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 801 | // add cap to on interveal and remove from off interval |
| 802 | offInterval -= strokeWidth; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 803 | } |
| 804 | |
joshualitt | 4f569be | 2015-02-27 11:41:49 -0800 | [diff] [blame] | 805 | // TODO we can do a real rect call if not using fulldash(ie no off interval, not using AA) |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 806 | bool fullDash = offInterval > 0.f || aaMode != AAMode::kNone; |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 807 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 808 | lineData.fViewMatrix = viewMatrix; |
| 809 | lineData.fPhase = phase; |
| 810 | lineData.fIntervals[0] = intervals[0]; |
| 811 | lineData.fIntervals[1] = intervals[1]; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 812 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 813 | return DashOp::Make(context, std::move(paint), lineData, cap, aaMode, fullDash, |
| 814 | stencilSettings); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 815 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 816 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 817 | ////////////////////////////////////////////////////////////////////////////// |
| 818 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 819 | class GLDashingCircleEffect; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 820 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 821 | /* |
| 822 | * This effect will draw a dotted line (defined as a dashed lined with round caps and no on |
| 823 | * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo. |
| 824 | * Both of the previous two parameters are in device space. This effect also requires the setting of |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 825 | * a float2 vertex attribute for the the four corners of the bounding rect. This attribute is the |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 826 | * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we |
| 827 | * transform the line to be horizontal, with the start of line at the origin then shifted to the |
| 828 | * right by half the off interval. The line then goes in the positive x direction. |
| 829 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 830 | class DashingCircleEffect : public GrGeometryProcessor { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 831 | public: |
| 832 | typedef SkPathEffect::DashInfo DashInfo; |
| 833 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 834 | static sk_sp<GrGeometryProcessor> Make(GrColor, |
| 835 | AAMode aaMode, |
| 836 | const SkMatrix& localMatrix, |
| 837 | bool usesLocalCoords); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 838 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 839 | const char* name() const override { return "DashingCircleEffect"; } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 840 | |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 841 | AAMode aaMode() const { return fAAMode; } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 842 | |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 843 | GrColor color() const { return fColor; } |
| 844 | |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 845 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
| 846 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 847 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 848 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 849 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 850 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 851 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 852 | |
| 853 | private: |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 854 | DashingCircleEffect(GrColor, AAMode aaMode, const SkMatrix& localMatrix, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 855 | bool usesLocalCoords); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 856 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 857 | const Attribute& onVertexAttribute(int i) const override { |
| 858 | return IthAttribute(i, kInPosition, kInDashParams, kInCircleParams); |
| 859 | } |
| 860 | |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 861 | GrColor fColor; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 862 | SkMatrix fLocalMatrix; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 863 | bool fUsesLocalCoords; |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 864 | AAMode fAAMode; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 865 | |
| 866 | static constexpr Attribute kInPosition = {"inPosition", kFloat2_GrVertexAttribType}; |
| 867 | static constexpr Attribute kInDashParams = {"inDashParams", kHalf3_GrVertexAttribType}; |
| 868 | static constexpr Attribute kInCircleParams = {"inCircleParams", kHalf2_GrVertexAttribType}; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 869 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 870 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 871 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 872 | friend class GLDashingCircleEffect; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 873 | typedef GrGeometryProcessor INHERITED; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 874 | }; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 875 | constexpr GrPrimitiveProcessor::Attribute DashingCircleEffect::kInPosition; |
| 876 | constexpr GrPrimitiveProcessor::Attribute DashingCircleEffect::kInDashParams; |
| 877 | constexpr GrPrimitiveProcessor::Attribute DashingCircleEffect::kInCircleParams; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 878 | |
| 879 | ////////////////////////////////////////////////////////////////////////////// |
| 880 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 881 | class GLDashingCircleEffect : public GrGLSLGeometryProcessor { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 882 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 883 | GLDashingCircleEffect(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 884 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 885 | void onEmitCode(EmitArgs&, GrGPArgs*) override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 886 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 887 | static inline void GenKey(const GrGeometryProcessor&, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 888 | const GrShaderCaps&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 889 | GrProcessorKeyBuilder*); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 890 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 891 | void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&, |
| 892 | FPCoordTransformIter&& transformIter) override; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 893 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 894 | UniformHandle fParamUniform; |
| 895 | UniformHandle fColorUniform; |
| 896 | GrColor fColor; |
| 897 | SkScalar fPrevRadius; |
| 898 | SkScalar fPrevCenterX; |
| 899 | SkScalar fPrevIntervalLength; |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 900 | typedef GrGLSLGeometryProcessor INHERITED; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 901 | }; |
| 902 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 903 | GLDashingCircleEffect::GLDashingCircleEffect() { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 904 | fColor = GrColor_ILLEGAL; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 905 | fPrevRadius = SK_ScalarMin; |
| 906 | fPrevCenterX = SK_ScalarMin; |
| 907 | fPrevIntervalLength = SK_ScalarMax; |
| 908 | } |
| 909 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 910 | void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 911 | const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 912 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 913 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 914 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 915 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 916 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 917 | varyingHandler->emitAttributes(dce); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 918 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 919 | // XY are dashPos, Z is dashInterval |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 920 | GrGLSLVarying dashParams(kHalf3_GrSLType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 921 | varyingHandler->addVarying("DashParam", &dashParams); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 922 | vertBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.kInDashParams.name()); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 923 | |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 924 | // x refers to circle radius - 0.5, y refers to cicle's center x coord |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 925 | GrGLSLVarying circleParams(kHalf2_GrSLType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 926 | varyingHandler->addVarying("CircleParams", &circleParams); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 927 | vertBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.kInCircleParams.name()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 928 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 929 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 930 | // Setup pass through color |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 931 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 932 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 933 | // Setup position |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 934 | this->writeOutputPosition(vertBuilder, gpArgs, dce.kInPosition.name()); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 935 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 936 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 937 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 938 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 939 | uniformHandler, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 940 | dce.kInPosition.asShaderVar(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 941 | dce.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 942 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 943 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 944 | // transforms all points so that we can compare them to our test circle |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 945 | fragBuilder->codeAppendf("half xShifted = %s.x - floor(%s.x / %s.z) * %s.z;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 946 | dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(), |
| 947 | dashParams.fsIn()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 948 | fragBuilder->codeAppendf("half2 fragPosShifted = half2(xShifted, %s.y);", dashParams.fsIn()); |
| 949 | fragBuilder->codeAppendf("half2 center = half2(%s.y, 0.0);", circleParams.fsIn()); |
| 950 | fragBuilder->codeAppend("half dist = length(center - fragPosShifted);"); |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 951 | if (dce.aaMode() != AAMode::kNone) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 952 | fragBuilder->codeAppendf("half diff = dist - %s.x;", circleParams.fsIn()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 953 | fragBuilder->codeAppend("diff = 1.0 - diff;"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 954 | fragBuilder->codeAppend("half alpha = clamp(diff, 0.0, 1.0);"); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 955 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 956 | fragBuilder->codeAppendf("half alpha = 1.0;"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 957 | fragBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn()); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 958 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 959 | fragBuilder->codeAppendf("%s = half4(alpha);", args.fOutputCoverage); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 960 | } |
| 961 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 962 | void GLDashingCircleEffect::setData(const GrGLSLProgramDataManager& pdman, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 963 | const GrPrimitiveProcessor& processor, |
| 964 | FPCoordTransformIter&& transformIter) { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 965 | const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>(); |
| 966 | if (dce.color() != fColor) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 967 | float c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 968 | GrColorToRGBAFloat(dce.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 969 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 970 | fColor = dce.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 971 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 972 | this->setTransformDataHelper(dce.localMatrix(), pdman, &transformIter); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 973 | } |
| 974 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 975 | void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 976 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 977 | GrProcessorKeyBuilder* b) { |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 978 | const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>(); |
| 979 | uint32_t key = 0; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 980 | key |= dce.usesLocalCoords() && dce.localMatrix().hasPerspective() ? 0x1 : 0x0; |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 981 | key |= static_cast<uint32_t>(dce.aaMode()) << 1; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 982 | b->add32(key); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | ////////////////////////////////////////////////////////////////////////////// |
| 986 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 987 | sk_sp<GrGeometryProcessor> DashingCircleEffect::Make(GrColor color, |
| 988 | AAMode aaMode, |
| 989 | const SkMatrix& localMatrix, |
| 990 | bool usesLocalCoords) { |
| 991 | return sk_sp<GrGeometryProcessor>( |
| 992 | new DashingCircleEffect(color, aaMode, localMatrix, usesLocalCoords)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 995 | void DashingCircleEffect::getGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 996 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 997 | GLDashingCircleEffect::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1000 | GrGLSLPrimitiveProcessor* DashingCircleEffect::createGLSLInstance(const GrShaderCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 1001 | return new GLDashingCircleEffect(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1004 | DashingCircleEffect::DashingCircleEffect(GrColor color, |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 1005 | AAMode aaMode, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1006 | const SkMatrix& localMatrix, |
| 1007 | bool usesLocalCoords) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 1008 | : INHERITED(kDashingCircleEffect_ClassID) |
| 1009 | , fColor(color) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 1010 | , fLocalMatrix(localMatrix) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1011 | , fUsesLocalCoords(usesLocalCoords) |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 1012 | , fAAMode(aaMode) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1013 | this->setVertexAttributeCnt(3); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1016 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1017 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1018 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1019 | sk_sp<GrGeometryProcessor> DashingCircleEffect::TestCreate(GrProcessorTestData* d) { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 1020 | AAMode aaMode = static_cast<AAMode>(d->fRandom->nextULessThan(GrDashOp::kAAModeCnt)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1021 | return DashingCircleEffect::Make(GrRandomColor(d->fRandom), |
| 1022 | aaMode, GrTest::TestMatrix(d->fRandom), |
| 1023 | d->fRandom->nextBool()); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1024 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1025 | #endif |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1026 | |
| 1027 | ////////////////////////////////////////////////////////////////////////////// |
| 1028 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1029 | class GLDashingLineEffect; |
| 1030 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1031 | /* |
| 1032 | * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the |
| 1033 | * length and spacing by the DashInfo. Both of the previous two parameters are in device space. |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1034 | * This effect also requires the setting of a float2 vertex attribute for the the four corners of the |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1035 | * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the |
| 1036 | * vertex coords (in device space) if we transform the line to be horizontal, with the start of |
| 1037 | * line at the origin then shifted to the right by half the off interval. The line then goes in the |
| 1038 | * positive x direction. |
| 1039 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 1040 | class DashingLineEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1041 | public: |
| 1042 | typedef SkPathEffect::DashInfo DashInfo; |
| 1043 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1044 | static sk_sp<GrGeometryProcessor> Make(GrColor, |
| 1045 | AAMode aaMode, |
| 1046 | const SkMatrix& localMatrix, |
| 1047 | bool usesLocalCoords); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1048 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1049 | const char* name() const override { return "DashingEffect"; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1050 | |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 1051 | AAMode aaMode() const { return fAAMode; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1052 | |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 1053 | GrColor color() const { return fColor; } |
| 1054 | |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 1055 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 1056 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1057 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 1058 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1059 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1060 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1061 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1062 | |
| 1063 | private: |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 1064 | DashingLineEffect(GrColor, AAMode aaMode, const SkMatrix& localMatrix, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1065 | bool usesLocalCoords); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1066 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1067 | const Attribute& onVertexAttribute(int i) const override { |
| 1068 | return IthAttribute(i, kInPosition, kInDashParams, kInRectParams); |
| 1069 | } |
| 1070 | |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 1071 | GrColor fColor; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 1072 | SkMatrix fLocalMatrix; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1073 | bool fUsesLocalCoords; |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 1074 | AAMode fAAMode; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1075 | |
| 1076 | static constexpr Attribute kInPosition = {"inPosition", kFloat2_GrVertexAttribType}; |
| 1077 | static constexpr Attribute kInDashParams = {"inDashParams", kHalf3_GrVertexAttribType}; |
| 1078 | static constexpr Attribute kInRectParams = {"inRect", kHalf4_GrVertexAttribType}; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1079 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 1080 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1081 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1082 | friend class GLDashingLineEffect; |
| 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 | }; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1086 | constexpr GrPrimitiveProcessor::Attribute DashingLineEffect::kInPosition; |
| 1087 | constexpr GrPrimitiveProcessor::Attribute DashingLineEffect::kInDashParams; |
| 1088 | constexpr GrPrimitiveProcessor::Attribute DashingLineEffect::kInRectParams; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1089 | |
| 1090 | ////////////////////////////////////////////////////////////////////////////// |
| 1091 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 1092 | class GLDashingLineEffect : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1093 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 1094 | GLDashingLineEffect(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1095 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1096 | void onEmitCode(EmitArgs&, GrGPArgs*) override; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1097 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 1098 | static inline void GenKey(const GrGeometryProcessor&, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1099 | const GrShaderCaps&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 1100 | GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1101 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 1102 | void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&, |
| 1103 | FPCoordTransformIter&& iter) override; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 1104 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1105 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1106 | GrColor fColor; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1107 | UniformHandle fColorUniform; |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 1108 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1109 | }; |
| 1110 | |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 1111 | GLDashingLineEffect::GLDashingLineEffect() : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1112 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 1113 | void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 1114 | const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 1115 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1116 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 1117 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1118 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 1119 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1120 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 1121 | varyingHandler->emitAttributes(de); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1122 | |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1123 | // XY refers to dashPos, Z is the dash interval length |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 1124 | GrGLSLVarying inDashParams(kFloat3_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 1125 | varyingHandler->addVarying("DashParams", &inDashParams); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1126 | vertBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.kInDashParams.name()); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1127 | |
| 1128 | // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5), |
| 1129 | // respectively. |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 1130 | GrGLSLVarying inRectParams(kFloat4_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 1131 | varyingHandler->addVarying("RectParams", &inRectParams); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1132 | vertBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.kInRectParams.name()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 1133 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 1134 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1135 | // Setup pass through color |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 1136 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1137 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1138 | // Setup position |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1139 | this->writeOutputPosition(vertBuilder, gpArgs, de.kInPosition.name()); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 1140 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1141 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1142 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 1143 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1144 | uniformHandler, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1145 | de.kInPosition.asShaderVar(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1146 | de.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 1147 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 1148 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1149 | // transforms all points so that we can compare them to our test rect |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1150 | fragBuilder->codeAppendf("half xShifted = %s.x - floor(%s.x / %s.z) * %s.z;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1151 | inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(), |
| 1152 | inDashParams.fsIn()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1153 | fragBuilder->codeAppendf("half2 fragPosShifted = half2(xShifted, %s.y);", inDashParams.fsIn()); |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 1154 | if (de.aaMode() == AAMode::kCoverage) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1155 | // The amount of coverage removed in x and y by the edges is computed as a pair of negative |
| 1156 | // numbers, xSub and ySub. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1157 | fragBuilder->codeAppend("half xSub, ySub;"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1158 | fragBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn()); |
| 1159 | fragBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn()); |
| 1160 | fragBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn()); |
| 1161 | fragBuilder->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] | 1162 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel |
| 1163 | // covered. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1164 | fragBuilder->codeAppendf( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1165 | "half alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));"); |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 1166 | } else if (de.aaMode() == AAMode::kCoverageWithMSAA) { |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1167 | // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle |
| 1168 | // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1169 | fragBuilder->codeAppend("half xSub;"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1170 | fragBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn()); |
| 1171 | fragBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn()); |
senorblanco | f3c2c46 | 2015-04-20 14:44:26 -0700 | [diff] [blame] | 1172 | // Now compute coverage in x to get the fraction of the pixel covered. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1173 | fragBuilder->codeAppendf("half alpha = (1.0 + max(xSub, -1.0));"); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1174 | } else { |
| 1175 | // Assuming the bounding geometry is tight so no need to check y values |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1176 | fragBuilder->codeAppendf("half alpha = 1.0;"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1177 | fragBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;", |
| 1178 | inRectParams.fsIn()); |
| 1179 | fragBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;", |
| 1180 | inRectParams.fsIn()); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1181 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1182 | fragBuilder->codeAppendf("%s = half4(alpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1185 | void GLDashingLineEffect::setData(const GrGLSLProgramDataManager& pdman, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 1186 | const GrPrimitiveProcessor& processor, |
| 1187 | FPCoordTransformIter&& transformIter) { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1188 | const DashingLineEffect& de = processor.cast<DashingLineEffect>(); |
| 1189 | if (de.color() != fColor) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1190 | float c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1191 | GrColorToRGBAFloat(de.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1192 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1193 | fColor = de.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1194 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 1195 | this->setTransformDataHelper(de.localMatrix(), pdman, &transformIter); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 1198 | void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1199 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1200 | GrProcessorKeyBuilder* b) { |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 1201 | const DashingLineEffect& de = gp.cast<DashingLineEffect>(); |
| 1202 | uint32_t key = 0; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1203 | key |= de.usesLocalCoords() && de.localMatrix().hasPerspective() ? 0x1 : 0x0; |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 1204 | key |= static_cast<int>(de.aaMode()) << 8; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1205 | b->add32(key); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | ////////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 1209 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1210 | sk_sp<GrGeometryProcessor> DashingLineEffect::Make(GrColor color, |
| 1211 | AAMode aaMode, |
| 1212 | const SkMatrix& localMatrix, |
| 1213 | bool usesLocalCoords) { |
| 1214 | return sk_sp<GrGeometryProcessor>( |
| 1215 | new DashingLineEffect(color, aaMode, localMatrix, usesLocalCoords)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1218 | void DashingLineEffect::getGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1219 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 1220 | GLDashingLineEffect::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1223 | GrGLSLPrimitiveProcessor* DashingLineEffect::createGLSLInstance(const GrShaderCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 1224 | return new GLDashingLineEffect(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1227 | DashingLineEffect::DashingLineEffect(GrColor color, |
bsalomon | af18fb4 | 2016-06-07 08:10:46 -0700 | [diff] [blame] | 1228 | AAMode aaMode, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1229 | const SkMatrix& localMatrix, |
| 1230 | bool usesLocalCoords) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 1231 | : INHERITED(kDashingLineEffect_ClassID) |
| 1232 | , fColor(color) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 1233 | , fLocalMatrix(localMatrix) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1234 | , fUsesLocalCoords(usesLocalCoords) |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 1235 | , fAAMode(aaMode) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1236 | this->setVertexAttributeCnt(3); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1239 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1240 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1241 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1242 | sk_sp<GrGeometryProcessor> DashingLineEffect::TestCreate(GrProcessorTestData* d) { |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 1243 | AAMode aaMode = static_cast<AAMode>(d->fRandom->nextULessThan(GrDashOp::kAAModeCnt)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1244 | return DashingLineEffect::Make(GrRandomColor(d->fRandom), |
| 1245 | aaMode, GrTest::TestMatrix(d->fRandom), |
| 1246 | d->fRandom->nextBool()); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1247 | } |
Mike Klein | 5045e50 | 2018-06-19 01:40:57 +0000 | [diff] [blame] | 1248 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1249 | #endif |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1250 | ////////////////////////////////////////////////////////////////////////////// |
| 1251 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1252 | static sk_sp<GrGeometryProcessor> make_dash_gp(GrColor color, |
| 1253 | AAMode aaMode, |
| 1254 | DashCap cap, |
| 1255 | const SkMatrix& viewMatrix, |
| 1256 | bool usesLocalCoords) { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 1257 | SkMatrix invert; |
| 1258 | if (usesLocalCoords && !viewMatrix.invert(&invert)) { |
| 1259 | SkDebugf("Failed to invert\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1260 | return nullptr; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1263 | switch (cap) { |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1264 | case kRound_DashCap: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1265 | return DashingCircleEffect::Make(color, aaMode, invert, usesLocalCoords); |
joshualitt | 5224ba7 | 2015-02-03 15:07:51 -0800 | [diff] [blame] | 1266 | case kNonRound_DashCap: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1267 | return DashingLineEffect::Make(color, aaMode, invert, usesLocalCoords); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1268 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1269 | return nullptr; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1270 | } |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1271 | |
| 1272 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 1273 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1274 | #if GR_TEST_UTILS |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1275 | |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 1276 | GR_DRAW_OP_TEST_DEFINE(DashOp) { |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1277 | SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random); |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 1278 | AAMode aaMode; |
| 1279 | do { |
| 1280 | aaMode = static_cast<AAMode>(random->nextULessThan(GrDashOp::kAAModeCnt)); |
| 1281 | } while (AAMode::kCoverageWithMSAA == aaMode && GrFSAAType::kUnifiedMSAA != fsaaType); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1282 | |
| 1283 | // We can only dash either horizontal or vertical lines |
| 1284 | SkPoint pts[2]; |
| 1285 | if (random->nextBool()) { |
| 1286 | // vertical |
| 1287 | pts[0].fX = 1.f; |
| 1288 | pts[0].fY = random->nextF() * 10.f; |
| 1289 | pts[1].fX = 1.f; |
| 1290 | pts[1].fY = random->nextF() * 10.f; |
| 1291 | } else { |
| 1292 | // horizontal |
| 1293 | pts[0].fX = random->nextF() * 10.f; |
| 1294 | pts[0].fY = 1.f; |
| 1295 | pts[1].fX = random->nextF() * 10.f; |
| 1296 | pts[1].fY = 1.f; |
| 1297 | } |
| 1298 | |
| 1299 | // pick random cap |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 1300 | SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount)); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1301 | |
| 1302 | SkScalar intervals[2]; |
| 1303 | |
| 1304 | // We can only dash with the following intervals |
| 1305 | enum Intervals { |
| 1306 | kOpenOpen_Intervals , |
| 1307 | kOpenClose_Intervals, |
| 1308 | kCloseOpen_Intervals, |
| 1309 | }; |
| 1310 | |
Greg Daniel | 5d00f00 | 2017-06-29 13:44:51 -0400 | [diff] [blame] | 1311 | Intervals intervalType = SkPaint::kRound_Cap == cap ? |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1312 | kOpenClose_Intervals : |
| 1313 | Intervals(random->nextULessThan(kCloseOpen_Intervals + 1)); |
| 1314 | static const SkScalar kIntervalMin = 0.1f; |
Greg Daniel | 5d00f00 | 2017-06-29 13:44:51 -0400 | [diff] [blame] | 1315 | static const SkScalar kIntervalMinCircles = 1.f; // Must be >= to stroke width |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1316 | static const SkScalar kIntervalMax = 10.f; |
| 1317 | switch (intervalType) { |
| 1318 | case kOpenOpen_Intervals: |
| 1319 | intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax); |
| 1320 | intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax); |
| 1321 | break; |
Greg Daniel | 5d00f00 | 2017-06-29 13:44:51 -0400 | [diff] [blame] | 1322 | case kOpenClose_Intervals: { |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1323 | intervals[0] = 0.f; |
Greg Daniel | 5d00f00 | 2017-06-29 13:44:51 -0400 | [diff] [blame] | 1324 | SkScalar min = SkPaint::kRound_Cap == cap ? kIntervalMinCircles : kIntervalMin; |
| 1325 | intervals[1] = random->nextRangeScalar(min, kIntervalMax); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1326 | break; |
Greg Daniel | 5d00f00 | 2017-06-29 13:44:51 -0400 | [diff] [blame] | 1327 | } |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1328 | case kCloseOpen_Intervals: |
| 1329 | intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax); |
| 1330 | intervals[1] = 0.f; |
| 1331 | break; |
| 1332 | |
| 1333 | } |
| 1334 | |
| 1335 | // phase is 0 < sum (i0, i1) |
| 1336 | SkScalar phase = random->nextRangeScalar(0, intervals[0] + intervals[1]); |
| 1337 | |
| 1338 | SkPaint p; |
| 1339 | p.setStyle(SkPaint::kStroke_Style); |
| 1340 | p.setStrokeWidth(SkIntToScalar(1)); |
| 1341 | p.setStrokeCap(cap); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1342 | p.setPathEffect(GrTest::TestDashPathEffect::Make(intervals, 2, phase)); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1343 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1344 | GrStyle style(p); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1345 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1346 | return GrDashOp::MakeDashLineOp(context, std::move(paint), viewMatrix, pts, aaMode, style, |
Brian Salomon | 98222ac | 2017-07-12 15:27:54 -0400 | [diff] [blame] | 1347 | GrGetRandomStencil(random, context)); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | #endif |