Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC. |
| 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 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 8 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 9 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 10 | #include "include/pathops/SkPathOps.h" |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 11 | #include "src/core/SkIPoint16.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 12 | #include "src/core/SkPathPriv.h" |
| 13 | #include "src/gpu/GrClip.h" |
| 14 | #include "src/gpu/GrMemoryPool.h" |
| 15 | #include "src/gpu/GrRecordingContextPriv.h" |
| 16 | #include "src/gpu/GrRenderTargetContext.h" |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 17 | #include "src/gpu/GrSurfaceContextPriv.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 18 | #include "src/gpu/geometry/GrStyledShape.h" |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 19 | #include "src/gpu/ops/GrFillRectOp.h" |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 20 | #include "src/gpu/tessellate/GrDrawAtlasPathOp.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 21 | #include "src/gpu/tessellate/GrTessellatePathOp.h" |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 22 | #include "src/gpu/tessellate/GrTessellateStrokeOp.h" |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 23 | #include "src/gpu/tessellate/GrWangsFormula.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 24 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 25 | constexpr static SkISize kAtlasInitialSize{512, 512}; |
| 26 | constexpr static int kMaxAtlasSize = 2048; |
| 27 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 28 | // The atlas is only used for small-area paths, which means at least one dimension of every path is |
| 29 | // guaranteed to be quite small. So if we transpose tall paths, then every path will have a small |
| 30 | // height, which lends very well to efficient pow2 atlas packing. |
| 31 | constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2; |
| 32 | |
| 33 | // Ensure every path in the atlas falls in or below the 128px high rectanizer band. |
| 34 | constexpr static int kMaxAtlasPathHeight = 128; |
| 35 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 36 | GrTessellationPathRenderer::GrTessellationPathRenderer(const GrCaps& caps) |
| 37 | : fAtlas(GrColorType::kAlpha_8, GrDynamicAtlas::InternalMultisample::kYes, |
| 38 | kAtlasInitialSize, std::min(kMaxAtlasSize, caps.maxPreferredRenderTargetSize()), |
| 39 | caps, kAtlasAlgorithm) { |
| 40 | this->initAtlasFlags(*caps.shaderCaps()); |
| 41 | } |
| 42 | |
| 43 | void GrTessellationPathRenderer::initAtlasFlags(const GrShaderCaps& shaderCaps) { |
| 44 | fStencilAtlasFlags = OpFlags::kStencilOnly | OpFlags::kDisableHWTessellation; |
| 45 | fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2; |
| 46 | // The atlas usually does better with hardware tessellation. If hardware tessellation is |
| 47 | // supported, we choose a max atlas path width that is guaranteed to never require more |
| 48 | // tessellation segments than are supported by the hardware. |
| 49 | if (!shaderCaps.tessellationSupport()) { |
| 50 | return; |
| 51 | } |
| 52 | // Since we limit the area of paths in the atlas to kMaxAtlasPathHeight^2, taller paths can't |
| 53 | // get very wide anyway. Find the tallest path whose width is limited by |
| 54 | // GrWangsFormula::worst_case_cubic() rather than the max area constraint, and use that for our |
| 55 | // max atlas path width. |
| 56 | // |
| 57 | // Solve the following equation for w: |
| 58 | // |
| 59 | // GrWangsFormula::worst_case_cubic(kLinearizationIntolerance, w, kMaxAtlasPathHeight^2 / w) |
| 60 | // == maxTessellationSegments |
| 61 | // |
| 62 | float k = GrWangsFormula::cubic_k(kLinearizationIntolerance); |
| 63 | float h = kMaxAtlasPathHeight; |
| 64 | float s = shaderCaps.maxTessellationSegments(); |
| 65 | // Quadratic formula from Numerical Recipes in C: |
| 66 | // |
| 67 | // q = -1/2 [b + sign(b) sqrt(b*b - 4*a*c)] |
| 68 | // x1 = q/a |
| 69 | // x2 = c/q |
| 70 | // |
| 71 | // float a = 1; // 'a' is always 1 in our specific equation. |
| 72 | float b = -s*s*s*s / (4*k*k); // Always negative. |
| 73 | float c = h*h*h*h; // Always positive. |
| 74 | float det = b*b - 4*1*c; |
| 75 | if (det <= 0) { |
| 76 | // maxTessellationSegments is too small for any path whose area == kMaxAtlasPathHeight^2. |
| 77 | // (This is unexpected because the GL spec mandates a minimum of 64 segments.) |
| 78 | SkDebugf("WARNING: maxTessellationSegments seems too low. (%i)\n", |
| 79 | shaderCaps.maxTessellationSegments()); |
| 80 | return; |
| 81 | } |
| 82 | float q = -.5f * (b - std::sqrt(det)); // Always positive. |
| 83 | // The two roots represent the width^2 and height^2 of the tallest rectangle that is limited by |
| 84 | // GrWangsFormula::worst_case_cubic(). |
| 85 | float r0 = q; // Always positive. |
| 86 | float r1 = c/q; // Always positive. |
| 87 | float worstCaseWidth = std::sqrt(std::max(r0, r1)); |
| 88 | #ifdef SK_DEBUG |
| 89 | float worstCaseHeight = std::sqrt(std::min(r0, r1)); |
| 90 | // Verify the above equation worked as expected. It should have found a width and height whose |
| 91 | // area == kMaxAtlasPathHeight^2. |
| 92 | SkASSERT(SkScalarNearlyEqual(worstCaseHeight * worstCaseWidth, h*h, 1)); |
| 93 | // Verify GrWangsFormula::worst_case_cubic() still works as we expect. The worst case number of |
| 94 | // segments for this bounding box should be maxTessellationSegments. |
| 95 | SkASSERT(SkScalarNearlyEqual(GrWangsFormula::worst_case_cubic( |
| 96 | kLinearizationIntolerance, worstCaseWidth, worstCaseHeight), s, 1)); |
| 97 | #endif |
| 98 | fStencilAtlasFlags &= ~OpFlags::kDisableHWTessellation; |
| 99 | fMaxAtlasPathWidth = std::min(fMaxAtlasPathWidth, (int)worstCaseWidth); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 102 | GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath( |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 103 | const CanDrawPathArgs& args) const { |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 104 | const GrStyledShape& shape = *args.fShape; |
| 105 | if (shape.inverseFilled() || shape.style().hasPathEffect() || |
Chris Dalton | 0f6bb8a | 2020-01-15 09:40:54 -0700 | [diff] [blame] | 106 | args.fViewMatrix->hasPerspective()) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 107 | return CanDrawPath::kNo; |
| 108 | } |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 109 | |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 110 | if (GrAAType::kCoverage == args.fAAType) { |
| 111 | SkASSERT(1 == args.fProxy->numSamples()); |
| 112 | if (!args.fProxy->canUseMixedSamples(*args.fCaps)) { |
| 113 | return CanDrawPath::kNo; |
| 114 | } |
| 115 | } |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 116 | |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 117 | SkPath path; |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 118 | shape.asPath(&path); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 119 | if (SkPathPriv::ConicWeightCnt(path)) { |
| 120 | return CanDrawPath::kNo; |
| 121 | } |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 122 | |
| 123 | if (!shape.style().isSimpleFill()) { |
| 124 | SkPMColor4f constantColor; |
| 125 | // These are only temporary restrictions while we bootstrap tessellated stroking. Every one |
| 126 | // of them will eventually go away. |
| 127 | if (shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style || |
| 128 | !args.fCaps->shaderCaps()->tessellationSupport() || |
| 129 | GrAAType::kCoverage == args.fAAType || !args.fViewMatrix->isSimilarity() || |
| 130 | !args.fPaint->isConstantBlendedColor(&constantColor) || |
| 131 | args.fPaint->numCoverageFragmentProcessors()) { |
| 132 | return CanDrawPath::kNo; |
| 133 | } |
| 134 | } |
| 135 | |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 136 | return CanDrawPath::kYes; |
| 137 | } |
| 138 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 139 | bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 140 | GrRenderTargetContext* renderTargetContext = args.fRenderTargetContext; |
| 141 | GrOpMemoryPool* pool = args.fContext->priv().opMemoryPool(); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 142 | const GrShaderCaps& shaderCaps = *args.fContext->priv().caps()->shaderCaps(); |
| 143 | |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 144 | SkPath path; |
| 145 | args.fShape->asPath(&path); |
| 146 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 147 | SkRect devBounds; |
| 148 | args.fViewMatrix->mapRect(&devBounds, path.getBounds()); |
| 149 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 150 | // See if the path is small and simple enough to atlas instead of drawing directly. |
| 151 | // |
| 152 | // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically |
| 153 | // render the sample mask to an integer texture, but such a scheme would probably require |
| 154 | // GL_EXT_post_depth_coverage, which appears to have low adoption. |
| 155 | SkIRect devIBounds; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 156 | SkIPoint16 locationInAtlas; |
| 157 | bool transposedInAtlas; |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 158 | if (args.fShape->style().isSimpleFill() && |
| 159 | this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, path, devBounds, |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 160 | args.fAAType, &devIBounds, &locationInAtlas, &transposedInAtlas)) { |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 161 | #ifdef SK_DEBUG |
| 162 | // If using hardware tessellation in the atlas, make sure the max number of segments is |
| 163 | // sufficient for this path. fMaxAtlasPathWidth should have been tuned for this to always be |
| 164 | // the case. |
| 165 | if (!(fStencilAtlasFlags & OpFlags::kDisableHWTessellation)) { |
| 166 | int worstCaseNumSegments = GrWangsFormula::worst_case_cubic(kLinearizationIntolerance, |
| 167 | devIBounds.width(), |
| 168 | devIBounds.height()); |
| 169 | SkASSERT(worstCaseNumSegments <= shaderCaps.maxTessellationSegments()); |
| 170 | } |
| 171 | #endif |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 172 | auto op = pool->allocate<GrDrawAtlasPathOp>( |
| 173 | renderTargetContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()), |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 174 | devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 175 | std::move(args.fPaint)); |
| 176 | renderTargetContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 177 | return true; |
| 178 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 179 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 180 | // Find the worst-case log2 number of line segments that a curve in this path might need to be |
| 181 | // divided into. |
| 182 | int worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance, |
| 183 | devBounds.width(), |
| 184 | devBounds.height()); |
| 185 | if (worstCaseResolveLevel > kMaxResolveLevel) { |
| 186 | // The path is too large for our internal indirect draw shaders. Crop it to the viewport. |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 187 | auto viewport = SkRect::MakeIWH(renderTargetContext->width(), |
| 188 | renderTargetContext->height()); |
| 189 | float inflationRadius = 1; |
| 190 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
| 191 | if (stroke.getStyle() == SkStrokeRec::kHairline_Style) { |
| 192 | inflationRadius += SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(), |
| 193 | stroke.getCap(), 1); |
| 194 | } else if (stroke.getStyle() != SkStrokeRec::kFill_Style) { |
| 195 | inflationRadius += stroke.getInflationRadius() * args.fViewMatrix->getMaxScale(); |
| 196 | } |
| 197 | viewport.outset(inflationRadius, inflationRadius); |
| 198 | |
| 199 | SkPath viewportPath; |
| 200 | viewportPath.addRect(viewport); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 201 | // Perform the crop in device space so it's a simple rect-path intersection. |
| 202 | path.transform(*args.fViewMatrix); |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 203 | if (!Op(viewportPath, path, kIntersect_SkPathOp, &path)) { |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 204 | // The crop can fail if the PathOps encounter NaN or infinities. Return true |
| 205 | // because drawing nothing is acceptable behavior for FP overflow. |
| 206 | return true; |
| 207 | } |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 208 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 209 | // Transform the path back to its own local space. |
| 210 | SkMatrix inverse; |
| 211 | if (!args.fViewMatrix->invert(&inverse)) { |
| 212 | return true; // Singular view matrix. Nothing would have drawn anyway. Return true. |
| 213 | } |
| 214 | path.transform(inverse); |
| 215 | path.setIsVolatile(true); |
| 216 | args.fViewMatrix->mapRect(&devBounds, path.getBounds()); |
| 217 | worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance, |
| 218 | devBounds.width(), |
| 219 | devBounds.height()); |
| 220 | // kMaxResolveLevel should be large enough to tessellate paths the size of any screen we |
| 221 | // might encounter. |
| 222 | SkASSERT(worstCaseResolveLevel <= kMaxResolveLevel); |
| 223 | } |
| 224 | |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 225 | if (!args.fShape->style().isSimpleFill()) { |
| 226 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
| 227 | SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style); |
| 228 | auto op = pool->allocate<GrTessellateStrokeOp>(*args.fViewMatrix, path, stroke, |
| 229 | std::move(args.fPaint), args.fAAType); |
| 230 | renderTargetContext->addDrawOp(args.fClip, std::move(op)); |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | auto drawPathFlags = OpFlags::kNone; |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 235 | if ((1 << worstCaseResolveLevel) > shaderCaps.maxTessellationSegments()) { |
| 236 | // The path is too large for hardware tessellation; a curve in this bounding box could |
| 237 | // potentially require more segments than are supported by the hardware. Fall back on |
| 238 | // indirect draws. |
| 239 | drawPathFlags |= OpFlags::kDisableHWTessellation; |
| 240 | } |
| 241 | |
| 242 | auto op = pool->allocate<GrTessellatePathOp>(*args.fViewMatrix, path, std::move(args.fPaint), |
| 243 | args.fAAType, drawPathFlags); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 244 | renderTargetContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 248 | bool GrTessellationPathRenderer::tryAddPathToAtlas( |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 249 | const GrCaps& caps, const SkMatrix& viewMatrix, const SkPath& path, const SkRect& devBounds, |
| 250 | GrAAType aaType, SkIRect* devIBounds, SkIPoint16* locationInAtlas, |
| 251 | bool* transposedInAtlas) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 252 | if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) { |
| 253 | return false; |
| 254 | } |
| 255 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 256 | // Atlas paths require their points to be transformed on the CPU and copied into an "uber path". |
| 257 | // Check if this path has too many points to justify this extra work. |
| 258 | if (path.countPoints() > 200) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 262 | // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this |
| 263 | // guarantees that every atlas entry has a small height, which lends very well to efficient pow2 |
| 264 | // atlas packing. |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 265 | devBounds.roundOut(devIBounds); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 266 | int maxDimenstion = devIBounds->width(); |
| 267 | int minDimension = devIBounds->height(); |
| 268 | *transposedInAtlas = minDimension > maxDimenstion; |
| 269 | if (*transposedInAtlas) { |
| 270 | std::swap(minDimension, maxDimenstion); |
| 271 | } |
| 272 | |
| 273 | // Check if the path is too large for an atlas. Since we use "minDimension" for height in the |
| 274 | // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight. |
| 275 | if (maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight || |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 276 | maxDimenstion > fMaxAtlasPathWidth) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 280 | if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
| 284 | SkMatrix atlasMatrix = viewMatrix; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 285 | if (*transposedInAtlas) { |
| 286 | std::swap(atlasMatrix[0], atlasMatrix[3]); |
| 287 | std::swap(atlasMatrix[1], atlasMatrix[4]); |
| 288 | float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY(); |
| 289 | atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x()); |
| 290 | atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y()); |
| 291 | } else { |
| 292 | atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(), |
| 293 | locationInAtlas->y() - devIBounds->y()); |
| 294 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 295 | |
| 296 | // Concatenate this path onto our uber path that matches its fill and AA types. |
| 297 | SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 298 | uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0). |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 299 | uberPath->addPath(path, atlasMatrix); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 300 | return true; |
| 301 | } |
| 302 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 303 | void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 304 | SkPath path; |
| 305 | args.fShape->asPath(&path); |
| 306 | |
| 307 | GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone; |
| 308 | |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 309 | auto op = args.fContext->priv().opMemoryPool()->allocate<GrTessellatePathOp>( |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 310 | *args.fViewMatrix, path, GrPaint(), aaType, OpFlags::kStencilOnly); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 311 | args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 312 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 313 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 314 | void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP, |
| 315 | const uint32_t* opsTaskIDs, int numOpsTaskIDs) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 316 | if (!fAtlas.drawBounds().isEmpty()) { |
| 317 | this->renderAtlas(onFlushRP); |
| 318 | fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps()); |
| 319 | } |
| 320 | for (SkPath& path : fAtlasUberPaths) { |
| 321 | path.reset(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | constexpr static GrUserStencilSettings kTestStencil( |
| 326 | GrUserStencilSettings::StaticInit< |
| 327 | 0x0000, |
| 328 | GrUserStencilTest::kNotEqual, |
| 329 | 0xffff, |
| 330 | GrUserStencilOp::kKeep, |
| 331 | GrUserStencilOp::kKeep, |
| 332 | 0xffff>()); |
| 333 | |
| 334 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 335 | GrUserStencilSettings::StaticInit< |
| 336 | 0x0000, |
| 337 | GrUserStencilTest::kNotEqual, |
| 338 | 0xffff, |
| 339 | GrUserStencilOp::kZero, |
| 340 | GrUserStencilOp::kKeep, |
| 341 | 0xffff>()); |
| 342 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 343 | void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 344 | auto rtc = fAtlas.instantiate(onFlushRP); |
| 345 | if (!rtc) { |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | // Add ops to stencil the atlas paths. |
| 350 | for (auto antialias : {false, true}) { |
| 351 | for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) { |
| 352 | SkPath* uberPath = this->getAtlasUberPath(fillType, antialias); |
| 353 | if (uberPath->isEmpty()) { |
| 354 | continue; |
| 355 | } |
| 356 | uberPath->setFillType(fillType); |
| 357 | GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone; |
| 358 | auto op = onFlushRP->opMemoryPool()->allocate<GrTessellatePathOp>( |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 359 | SkMatrix::I(), *uberPath, GrPaint(), aaType, fStencilAtlasFlags); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 360 | rtc->addDrawOp(nullptr, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 364 | // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks. |
| 365 | auto fillRectFlags = GrFillRectOp::InputFlags::kNone; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 366 | |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 367 | // This will be the final op in the renderTargetContext. So if Ganesh is planning to discard the |
| 368 | // stencil values anyway, then we might not actually need to reset the stencil values back to 0. |
| 369 | bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass(); |
| 370 | |
| 371 | if (rtc->numSamples() <= 1) { |
| 372 | // We are mixed sampled. We need to enable conservative raster and ensure stencil values get |
| 373 | // reset in order to avoid artifacts along the diagonal of the atlas. |
| 374 | fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster; |
| 375 | mustResetStencil = true; |
| 376 | } |
| 377 | |
| 378 | SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height()); |
| 379 | const GrUserStencilSettings* stencil; |
| 380 | if (mustResetStencil) { |
| 381 | // Outset the cover rect in case there are T-junctions in the path bounds. |
| 382 | coverRect.outset(1, 1); |
| 383 | stencil = &kTestAndResetStencil; |
| 384 | } else { |
| 385 | stencil = &kTestStencil; |
| 386 | } |
| 387 | |
| 388 | GrQuad coverQuad(coverRect); |
| 389 | DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll}; |
| 390 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 391 | GrPaint paint; |
| 392 | paint.setColor4f(SK_PMColor4fWHITE); |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 393 | |
| 394 | auto coverOp = GrFillRectOp::Make(rtc->surfPriv().getContext(), std::move(paint), |
| 395 | GrAAType::kMSAA, &drawQuad, stencil, fillRectFlags); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 396 | rtc->addDrawOp(nullptr, std::move(coverOp)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 397 | |
| 398 | if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) { |
| 399 | onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()), |
| 400 | GrSurfaceProxy::ResolveFlags::kMSAA); |
| 401 | } |
| 402 | } |