robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | /* |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 2 | * Copyright 2016 Google Inc. |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 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 | 6422722 | 2020-02-26 13:28:42 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrClipStackClip.h" |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/SkTo.h" |
| 11 | #include "src/core/SkClipOpPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/core/SkTaskGroup.h" |
| 13 | #include "src/core/SkTraceEvent.h" |
| 14 | #include "src/gpu/GrAppliedClip.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrContextPriv.h" |
| 16 | #include "src/gpu/GrDeferredProxyUploader.h" |
| 17 | #include "src/gpu/GrDrawingManager.h" |
| 18 | #include "src/gpu/GrFixedClip.h" |
| 19 | #include "src/gpu/GrGpuResourcePriv.h" |
| 20 | #include "src/gpu/GrProxyProvider.h" |
| 21 | #include "src/gpu/GrRecordingContextPriv.h" |
| 22 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 23 | #include "src/gpu/GrSWMaskHelper.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrStencilAttachment.h" |
| 25 | #include "src/gpu/GrStyle.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 26 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/effects/GrConvexPolyEffect.h" |
| 28 | #include "src/gpu/effects/GrRRectEffect.h" |
Brian Salomon | 6422722 | 2020-02-26 13:28:42 -0500 | [diff] [blame] | 29 | #include "src/gpu/effects/generated/GrDeviceSpaceEffect.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 30 | #include "src/gpu/geometry/GrStyledShape.h" |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 31 | |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 32 | typedef SkClipStack::Element Element; |
| 33 | typedef GrReducedClip::InitialState InitialState; |
| 34 | typedef GrReducedClip::ElementList ElementList; |
| 35 | |
| 36 | const char GrClipStackClip::kMaskTestTag[] = "clip_mask"; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 37 | |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 38 | bool GrClipStackClip::quickContains(const SkRect& rect) const { |
reed | 4d2cce4 | 2016-08-22 13:03:47 -0700 | [diff] [blame] | 39 | if (!fStack || fStack->isWideOpen()) { |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 40 | return true; |
| 41 | } |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 42 | return fStack->quickContains(rect); |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 43 | } |
| 44 | |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 45 | bool GrClipStackClip::quickContains(const SkRRect& rrect) const { |
reed | 4d2cce4 | 2016-08-22 13:03:47 -0700 | [diff] [blame] | 46 | if (!fStack || fStack->isWideOpen()) { |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 47 | return true; |
| 48 | } |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 49 | return fStack->quickContains(rrect); |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 52 | bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const { |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 53 | if (!fStack) { |
| 54 | return false; |
| 55 | } |
| 56 | const SkRect* rtBounds = &origRTBounds; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 57 | bool isAA; |
| 58 | if (fStack->isRRect(*rtBounds, rr, &isAA)) { |
Chris Dalton | 3b51df1 | 2017-11-27 14:33:06 -0700 | [diff] [blame] | 59 | *aa = GrAA(isAA); |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
Michael Ludwig | c002d56 | 2020-05-13 14:17:57 -0400 | [diff] [blame] | 65 | SkIRect GrClipStackClip::getConservativeBounds(int width, int height) const { |
| 66 | if (fStack) { |
| 67 | SkRect devBounds; |
| 68 | fStack->getConservativeBounds(0, 0, width, height, &devBounds); |
| 69 | return devBounds.roundOut(); |
| 70 | } else { |
| 71 | return this->GrClip::getConservativeBounds(width, height); |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 72 | } |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 75 | //////////////////////////////////////////////////////////////////////////////// |
| 76 | // set up the draw state to enable the aa clipping mask. |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 77 | static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(GrSurfaceProxyView mask, |
Brian Salomon | 6422722 | 2020-02-26 13:28:42 -0500 | [diff] [blame] | 78 | const SkIRect& devBound, |
| 79 | const GrCaps& caps) { |
| 80 | GrSamplerState samplerState(GrSamplerState::WrapMode::kClampToBorder, |
| 81 | GrSamplerState::Filter::kNearest); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 82 | auto m = SkMatrix::Translate(-devBound.fLeft, -devBound.fTop); |
Brian Salomon | 6422722 | 2020-02-26 13:28:42 -0500 | [diff] [blame] | 83 | auto subset = SkRect::Make(devBound.size()); |
| 84 | // We scissor to devBounds. The mask's texel centers are aligned to device space |
| 85 | // pixel centers. Hence this domain of texture coordinates. |
| 86 | auto domain = subset.makeInset(0.5, 0.5); |
| 87 | auto fp = GrTextureEffect::MakeSubset(std::move(mask), kPremul_SkAlphaType, m, samplerState, |
| 88 | subset, domain, caps); |
| 89 | return GrDeviceSpaceEffect::Make(std::move(fp)); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Does the path in 'element' require SW rendering? If so, return true (and, |
| 93 | // optionally, set 'prOut' to NULL. If not, return false (and, optionally, set |
| 94 | // 'prOut' to the non-SW path renderer that will do the job). |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 95 | bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context, |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 96 | const SkIRect& scissorRect, |
| 97 | bool hasUserStencilSettings, |
| 98 | const GrRenderTargetContext* renderTargetContext, |
| 99 | const SkMatrix& viewMatrix, |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 100 | const Element* element, |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 101 | GrPathRenderer** prOut, |
| 102 | bool needsStencil) { |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 103 | if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) { |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 104 | // rects can always be drawn directly w/o using the software path |
| 105 | // TODO: skip rrects once we're drawing them directly. |
| 106 | if (prOut) { |
| 107 | *prOut = nullptr; |
| 108 | } |
| 109 | return false; |
| 110 | } else { |
| 111 | // We shouldn't get here with an empty clip element. |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 112 | SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType()); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 113 | |
| 114 | // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer |
| 115 | SkPath path; |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 116 | element->asDeviceSpacePath(&path); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 117 | if (path.isInverseFillType()) { |
| 118 | path.toggleInverseFillType(); |
| 119 | } |
| 120 | |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 121 | // We only use this method when rendering coverage clip masks. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 122 | SkASSERT(renderTargetContext->numSamples() <= 1); |
| 123 | auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone; |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 124 | |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 125 | GrPathRendererChain::DrawType type = |
| 126 | needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor |
| 127 | : GrPathRendererChain::DrawType::kColor; |
| 128 | |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 129 | GrStyledShape shape(path, GrStyle::SimpleFill()); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 130 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 131 | canDrawArgs.fCaps = context->priv().caps(); |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 132 | canDrawArgs.fProxy = renderTargetContext->asRenderTargetProxy(); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 133 | canDrawArgs.fClipConservativeBounds = &scissorRect; |
| 134 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 135 | canDrawArgs.fShape = &shape; |
Ethan Nicholas | afe2c90 | 2020-04-28 13:55:02 -0400 | [diff] [blame] | 136 | canDrawArgs.fPaint = nullptr; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 137 | canDrawArgs.fAAType = aaType; |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 138 | SkASSERT(!renderTargetContext->wrapsVkSecondaryCB()); |
| 139 | canDrawArgs.fTargetIsWrappedVkSecondaryCB = false; |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 140 | canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings; |
| 141 | |
| 142 | // the 'false' parameter disallows use of the SW path renderer |
| 143 | GrPathRenderer* pr = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 144 | context->priv().drawingManager()->getPathRenderer(canDrawArgs, false, type); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 145 | if (prOut) { |
| 146 | *prOut = pr; |
| 147 | } |
| 148 | return SkToBool(!pr); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * This method traverses the clip stack to see if the GrSoftwarePathRenderer |
| 154 | * will be used on any element. If so, it returns true to indicate that the |
| 155 | * entire clip should be rendered in SW and then uploaded en masse to the gpu. |
| 156 | */ |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 157 | bool GrClipStackClip::UseSWOnlyPath(GrRecordingContext* context, |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 158 | bool hasUserStencilSettings, |
| 159 | const GrRenderTargetContext* renderTargetContext, |
| 160 | const GrReducedClip& reducedClip) { |
Chris Dalton | d22a023 | 2018-08-22 17:25:10 +0000 | [diff] [blame] | 161 | // TODO: right now it appears that GPU clip masks are strictly slower than software. We may |
| 162 | // want to revisit this assumption once we can test with render target sorting. |
| 163 | return true; |
| 164 | |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 165 | // TODO: generalize this function so that when |
| 166 | // a clip gets complex enough it can just be done in SW regardless |
| 167 | // of whether it would invoke the GrSoftwarePathRenderer. |
| 168 | |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 169 | // If we're avoiding stencils, always use SW. This includes drawing into a wrapped vulkan |
| 170 | // secondary command buffer which can't handle stencils. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 171 | if (context->priv().caps()->avoidStencilBuffers() || |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 172 | renderTargetContext->wrapsVkSecondaryCB()) { |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 173 | return true; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 174 | } |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 175 | |
| 176 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 177 | // space. |
| 178 | SkMatrix translate; |
| 179 | translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top())); |
| 180 | |
| 181 | for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) { |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 182 | const Element* element = iter.get(); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 183 | |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 184 | SkClipOp op = element->getOp(); |
| 185 | bool invert = element->isInverseFilled(); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 186 | bool needsStencil = invert || |
| 187 | kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op; |
| 188 | |
| 189 | if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings, |
| 190 | renderTargetContext, translate, element, nullptr, needsStencil)) { |
| 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | //////////////////////////////////////////////////////////////////////////////// |
| 198 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 199 | // scissor, or entirely software |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 200 | bool GrClipStackClip::apply(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 97180af | 2017-03-14 13:42:58 -0400 | [diff] [blame] | 201 | bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out, |
| 202 | SkRect* bounds) const { |
Brian Salomon | 97180af | 2017-03-14 13:42:58 -0400 | [diff] [blame] | 203 | SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height()); |
| 204 | if (!devBounds.intersect(*bounds)) { |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 208 | if (!fStack || fStack->isWideOpen()) { |
| 209 | return true; |
| 210 | } |
| 211 | |
Brian Osman | 5f5680c | 2019-06-05 10:17:25 -0400 | [diff] [blame] | 212 | // An default count of 4 was chosen because of the common pattern in Blink of: |
| 213 | // isect RR |
| 214 | // diff RR |
| 215 | // isect convex_poly |
| 216 | // isect convex_poly |
| 217 | // when drawing rounded div borders. |
| 218 | constexpr int kMaxAnalyticFPs = 4; |
| 219 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 220 | int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles(); |
Brian Osman | 5f5680c | 2019-06-05 10:17:25 -0400 | [diff] [blame] | 221 | int maxAnalyticFPs = kMaxAnalyticFPs; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 222 | if (renderTargetContext->numSamples() > 1 || useHWAA || hasUserStencilSettings) { |
| 223 | // Disable analytic clips when we have MSAA. In MSAA we never conflate coverage and opacity. |
| 224 | maxAnalyticFPs = 0; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 225 | // We disable MSAA when avoiding stencil. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 226 | SkASSERT(!context->priv().caps()->avoidStencilBuffers()); |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 227 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 228 | auto* ccpr = context->priv().drawingManager()->getCoverageCountingPathRenderer(); |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 229 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 230 | GrReducedClip reducedClip(*fStack, devBounds, context->priv().caps(), |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 231 | maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 232 | if (InitialState::kAllOut == reducedClip.initialState() && |
| 233 | reducedClip.maskElements().isEmpty()) { |
| 234 | return false; |
| 235 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 236 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 237 | if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 238 | out->hardClip().addScissor(reducedClip.scissor(), bounds); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 239 | } |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 240 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 241 | if (!reducedClip.windowRectangles().empty()) { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 242 | out->hardClip().addWindowRectangles(reducedClip.windowRectangles(), |
| 243 | GrWindowRectsState::Mode::kExclusive); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 246 | if (!reducedClip.maskElements().isEmpty()) { |
| 247 | if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings, |
| 248 | out)) { |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 253 | // The opsTask ID must not be looked up until AFTER producing the clip mask (if any). That step |
| 254 | // can cause a flush or otherwise change which opstask our draw is going into. |
| 255 | uint32_t opsTaskID = renderTargetContext->getOpsTask()->uniqueID(); |
| 256 | if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(ccpr, opsTaskID)) { |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 257 | out->addCoverageFP(std::move(clipFPs)); |
| 258 | } |
| 259 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 260 | return true; |
| 261 | } |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 262 | |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 263 | bool GrClipStackClip::applyClipMask(GrRecordingContext* context, |
| 264 | GrRenderTargetContext* renderTargetContext, |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 265 | const GrReducedClip& reducedClip, bool hasUserStencilSettings, |
| 266 | GrAppliedClip* out) const { |
csmartdalton | 3affdc1 | 2016-10-28 12:01:10 -0700 | [diff] [blame] | 267 | #ifdef SK_DEBUG |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 268 | SkASSERT(reducedClip.hasScissor()); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 269 | SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(), |
| 270 | renderTargetContext->height()); |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 271 | const SkIRect& scissor = reducedClip.scissor(); |
| 272 | SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT. |
csmartdalton | 3affdc1 | 2016-10-28 12:01:10 -0700 | [diff] [blame] | 273 | #endif |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 274 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 275 | // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping. |
| 276 | if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) || |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 277 | context->priv().caps()->avoidStencilBuffers() || |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 278 | renderTargetContext->wrapsVkSecondaryCB()) { |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 279 | GrSurfaceProxyView result; |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 280 | if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) { |
| 281 | // The clip geometry is complex enough that it will be more efficient to create it |
| 282 | // entirely in software |
| 283 | result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext); |
| 284 | } else { |
| 285 | result = this->createAlphaClipMask(context, reducedClip); |
| 286 | } |
| 287 | |
| 288 | if (result) { |
| 289 | // The mask's top left coord should be pinned to the rounded-out top left corner of |
| 290 | // the clip's device space bounds. |
Brian Salomon | 6422722 | 2020-02-26 13:28:42 -0500 | [diff] [blame] | 291 | out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor(), |
| 292 | *context->priv().caps())); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 293 | return true; |
| 294 | } |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 295 | |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 296 | // If alpha or software clip mask creation fails, fall through to the stencil code paths, |
| 297 | // unless stencils are disallowed. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 298 | if (context->priv().caps()->avoidStencilBuffers() || |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 299 | renderTargetContext->wrapsVkSecondaryCB()) { |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 300 | SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. " |
| 301 | "Clip will be ignored.\n"); |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 304 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 305 | |
Michael Ludwig | 828d341 | 2020-05-12 13:15:35 -0400 | [diff] [blame] | 306 | reducedClip.drawStencilClipMask(context, renderTargetContext); |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 307 | out->hardClip().addStencilClip(reducedClip.maskGenID()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 311 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 312 | // Create a 8-bit clip mask in alpha |
| 313 | |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 314 | static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs, |
| 315 | GrUniqueKey* key) { |
| 316 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 317 | GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag); |
| 318 | builder[0] = clipGenID; |
| 319 | // SkToS16 because image filters outset layers to a size indicated by the filter, which can |
| 320 | // sometimes result in negative coordinates from device space. |
| 321 | builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16); |
| 322 | builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16); |
| 323 | builder[3] = numAnalyticFPs; |
| 324 | } |
| 325 | |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 326 | static void add_invalidate_on_pop_message(GrRecordingContext* context, |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 327 | const SkClipStack& stack, uint32_t clipGenID, |
| 328 | const GrUniqueKey& clipMaskKey) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 329 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 330 | |
Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 331 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart); |
| 332 | while (const Element* element = iter.prev()) { |
| 333 | if (element->getGenID() == clipGenID) { |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 334 | element->addResourceInvalidationMessage(proxyProvider, clipMaskKey); |
Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | } |
| 338 | SkDEBUGFAIL("Gen ID was not found in stack."); |
| 339 | } |
| 340 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 341 | static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin; |
| 342 | |
| 343 | static GrSurfaceProxyView find_mask(GrProxyProvider* provider, const GrUniqueKey& key) { |
Brian Salomon | 0029db0 | 2020-04-03 10:41:24 -0400 | [diff] [blame] | 344 | return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8, |
| 345 | 1); |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 346 | } |
| 347 | |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 348 | GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context, |
| 349 | const GrReducedClip& reducedClip) const { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 350 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 351 | GrUniqueKey key; |
| 352 | create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(), |
| 353 | reducedClip.numAnalyticFPs(), &key); |
| 354 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 355 | if (auto cachedView = find_mask(context->priv().proxyProvider(), key)) { |
| 356 | return cachedView; |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 359 | auto rtc = GrRenderTargetContext::MakeWithFallback( |
| 360 | context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox, |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 361 | {reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, GrProtected::kNo, |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 362 | kMaskOrigin); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 363 | if (!rtc) { |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 364 | return {}; |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | if (!reducedClip.drawAlphaClipMask(rtc.get())) { |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 368 | return {}; |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 371 | GrSurfaceProxyView result = rtc->readSurfaceView(); |
| 372 | if (!result || !result.asTextureProxy()) { |
| 373 | return {}; |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 376 | SkASSERT(result.origin() == kMaskOrigin); |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 377 | proxyProvider->assignUniqueKeyToProxy(key, result.asTextureProxy()); |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 378 | add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key); |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 379 | |
| 380 | return result; |
| 381 | } |
| 382 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 383 | namespace { |
Robert Phillips | 875218e | 2017-02-24 08:37:13 -0500 | [diff] [blame] | 384 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 385 | /** |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 386 | * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 387 | * elements, each storing their own AA setting (and already transformed into device space). This |
| 388 | * stores all of the information needed by the worker thread to draw all clip elements (see below, |
| 389 | * in createSoftwareClipMask). |
| 390 | */ |
| 391 | class ClipMaskData { |
| 392 | public: |
| 393 | ClipMaskData(const GrReducedClip& reducedClip) |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 394 | : fScissor(reducedClip.scissor()) |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 395 | , fInitialState(reducedClip.initialState()) { |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 396 | for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) { |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 397 | fElements.addToTail(*iter.get()); |
| 398 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 401 | const SkIRect& scissor() const { return fScissor; } |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 402 | InitialState initialState() const { return fInitialState; } |
| 403 | const ElementList& elements() const { return fElements; } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 404 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 405 | private: |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 406 | SkIRect fScissor; |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 407 | InitialState fInitialState; |
| 408 | ElementList fElements; |
| 409 | }; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 410 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements, |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 414 | const SkIRect& scissor, InitialState initialState) { |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 415 | // Set the matrix so that rendered clip elements are transformed to mask space from clip space. |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 416 | SkMatrix translate; |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 417 | translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top())); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 418 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 419 | helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 420 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 421 | for (ElementList::Iter iter(elements); iter.get(); iter.next()) { |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 422 | const Element* element = iter.get(); |
| 423 | SkClipOp op = element->getOp(); |
| 424 | GrAA aa = GrAA(element->isAA()); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 425 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 426 | if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 427 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 428 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 429 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 430 | // the pixels before clearing the ones outside the geometry. |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 431 | if (kReverseDifference_SkClipOp == op) { |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 432 | SkRect temp = SkRect::Make(scissor); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 433 | // invert the entire scene |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 434 | helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 435 | } |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 436 | SkPath clipPath; |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 437 | element->asDeviceSpacePath(&clipPath); |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 438 | clipPath.toggleInverseFillType(); |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 439 | helper.drawShape(GrShape(clipPath), translate, SkRegion::kReplace_Op, aa, 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 440 | continue; |
| 441 | } |
| 442 | |
| 443 | // The other ops (union, xor, diff) only affect pixels inside |
| 444 | // the geometry so they can just be drawn normally |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 445 | if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) { |
| 446 | helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF); |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 447 | } else if (Element::DeviceSpaceType::kRRect == element->getDeviceSpaceType()) { |
| 448 | helper.drawRRect(element->getDeviceSpaceRRect(), translate, (SkRegion::Op)op, aa, 0xFF); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 449 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 450 | SkPath path; |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 451 | element->asDeviceSpacePath(&path); |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 452 | helper.drawShape(GrShape(path), translate, (SkRegion::Op)op, aa, 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 455 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 456 | |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 457 | GrSurfaceProxyView GrClipStackClip::createSoftwareClipMask( |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 458 | GrRecordingContext* context, const GrReducedClip& reducedClip, |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 459 | GrRenderTargetContext* renderTargetContext) const { |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 460 | GrUniqueKey key; |
| 461 | create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(), |
| 462 | reducedClip.numAnalyticFPs(), &key); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 463 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 464 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 465 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 466 | if (auto cachedView = find_mask(proxyProvider, key)) { |
| 467 | return cachedView; |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // The mask texture may be larger than necessary. We round out the clip bounds and pin the top |
| 471 | // left corner of the resulting rect to the top left of the texture. |
| 472 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height()); |
| 473 | |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 474 | SkTaskGroup* taskGroup = nullptr; |
| 475 | if (auto direct = context->priv().asDirectContext()) { |
| 476 | taskGroup = direct->priv().getTaskGroup(); |
| 477 | } |
| 478 | |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 479 | GrSurfaceProxyView view; |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 480 | if (taskGroup && renderTargetContext) { |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 481 | const GrCaps* caps = context->priv().caps(); |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 482 | // Create our texture proxy |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 483 | GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8, |
| 484 | GrRenderable::kNo); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 485 | |
Greg Daniel | 47c20e8 | 2020-01-21 14:29:57 -0500 | [diff] [blame] | 486 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kAlpha_8); |
| 487 | |
Brian Osman | d140fe9 | 2017-10-03 12:17:26 -0400 | [diff] [blame] | 488 | // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt |
| 489 | // to ops), so it can't have any pending IO. |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 490 | auto proxy = proxyProvider->createProxy(format, |
| 491 | maskSpaceIBounds.size(), |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 492 | GrRenderable::kNo, |
| 493 | 1, |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 494 | GrMipMapped::kNo, |
| 495 | SkBackingFit::kApprox, |
| 496 | SkBudgeted::kYes, |
| 497 | GrProtected::kNo); |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 498 | |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 499 | auto uploader = std::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip); |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 500 | GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get(); |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 501 | auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] { |
Brian Salomon | 5f39427 | 2019-07-02 14:07:49 -0400 | [diff] [blame] | 502 | TRACE_EVENT0("skia.gpu", "Threaded SW Clip Mask Render"); |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 503 | GrSWMaskHelper helper(uploaderRaw->getPixels()); |
| 504 | if (helper.init(maskSpaceIBounds)) { |
| 505 | draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(), |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 506 | uploaderRaw->data().scissor(), |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 507 | uploaderRaw->data().initialState()); |
| 508 | } else { |
| 509 | SkDEBUGFAIL("Unable to allocate SW clip mask."); |
| 510 | } |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 511 | uploaderRaw->signalAndFreeData(); |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | taskGroup->add(std::move(drawAndUploadMask)); |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 515 | proxy->texPriv().setDeferredUploader(std::move(uploader)); |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 516 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 517 | view = {std::move(proxy), kMaskOrigin, swizzle}; |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 518 | } else { |
| 519 | GrSWMaskHelper helper; |
| 520 | if (!helper.init(maskSpaceIBounds)) { |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 521 | return {}; |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 524 | draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(), |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 525 | reducedClip.initialState()); |
| 526 | |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 527 | view = helper.toTextureView(context, SkBackingFit::kApprox); |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 528 | } |
| 529 | |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 530 | SkASSERT(view); |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 531 | SkASSERT(view.origin() == kMaskOrigin); |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 532 | proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy()); |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 533 | add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key); |
Greg Daniel | e32506b | 2020-02-10 16:00:54 -0500 | [diff] [blame] | 534 | return view; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 535 | } |