blob: b8d60b5cc94d921a2568ed0a64379dacb62f9fdc [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
csmartdaltonc6f411e2016-08-05 22:32:12 -07002 * Copyright 2016 Google Inc.
robertphillips@google.com1e945b72012-04-16 18:03:03 +00003 *
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 Salomon64227222020-02-26 13:28:42 -05008#include "src/gpu/GrClipStackClip.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/SkTo.h"
11#include "src/core/SkClipOpPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkTaskGroup.h"
13#include "src/core/SkTraceEvent.h"
14#include "src/gpu/GrAppliedClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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 Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrStencilAttachment.h"
25#include "src/gpu/GrStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040026#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/effects/GrConvexPolyEffect.h"
28#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon64227222020-02-26 13:28:42 -050029#include "src/gpu/effects/generated/GrDeviceSpaceEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000030#include "src/gpu/geometry/GrStyledShape.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000031
Brian Salomonc3833b42018-07-09 18:23:58 +000032typedef SkClipStack::Element Element;
33typedef GrReducedClip::InitialState InitialState;
34typedef GrReducedClip::ElementList ElementList;
35
36const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070037
csmartdaltonc6f411e2016-08-05 22:32:12 -070038bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070039 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070040 return true;
41 }
Brian Salomon9a767722017-03-13 17:57:28 -040042 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070043}
44
bsalomon7f0d9f32016-08-15 14:49:10 -070045bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070046 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070047 return true;
48 }
Brian Salomon9a767722017-03-13 17:57:28 -040049 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070050}
51
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050052bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070053 if (!fStack) {
54 return false;
55 }
56 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050057 bool isAA;
58 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
Chris Dalton3b51df12017-11-27 14:33:06 -070059 *aa = GrAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070060 return true;
61 }
62 return false;
63}
64
Michael Ludwigc002d562020-05-13 14:17:57 -040065SkIRect 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);
csmartdaltonc6f411e2016-08-05 22:32:12 -070072 }
csmartdaltonc6f411e2016-08-05 22:32:12 -070073}
74
Chris Daltonc5348082018-03-30 15:59:38 +000075////////////////////////////////////////////////////////////////////////////////
76// set up the draw state to enable the aa clipping mask.
Greg Daniele32506b2020-02-10 16:00:54 -050077static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(GrSurfaceProxyView mask,
Brian Salomon64227222020-02-26 13:28:42 -050078 const SkIRect& devBound,
79 const GrCaps& caps) {
80 GrSamplerState samplerState(GrSamplerState::WrapMode::kClampToBorder,
81 GrSamplerState::Filter::kNearest);
Mike Reed1f607332020-05-21 12:11:27 -040082 auto m = SkMatrix::Translate(-devBound.fLeft, -devBound.fTop);
Brian Salomon64227222020-02-26 13:28:42 -050083 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 Daltonc5348082018-03-30 15:59:38 +000090}
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 Phillips6f0e02f2019-02-13 11:02:28 -050095bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +000096 const SkIRect& scissorRect,
97 bool hasUserStencilSettings,
98 const GrRenderTargetContext* renderTargetContext,
99 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +0000100 const Element* element,
Chris Daltonc5348082018-03-30 15:59:38 +0000101 GrPathRenderer** prOut,
102 bool needsStencil) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000103 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000104 // 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 Salomonc3833b42018-07-09 18:23:58 +0000112 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
Chris Daltonc5348082018-03-30 15:59:38 +0000113
114 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
115 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000116 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000117 if (path.isInverseFillType()) {
118 path.toggleInverseFillType();
119 }
120
Chris Dalton09e56892019-03-13 00:22:01 -0600121 // We only use this method when rendering coverage clip masks.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600122 SkASSERT(renderTargetContext->numSamples() <= 1);
123 auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600124
Chris Daltonc5348082018-03-30 15:59:38 +0000125 GrPathRendererChain::DrawType type =
126 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
127 : GrPathRendererChain::DrawType::kColor;
128
Michael Ludwig2686d692020-04-17 20:21:37 +0000129 GrStyledShape shape(path, GrStyle::SimpleFill());
Chris Daltonc5348082018-03-30 15:59:38 +0000130 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -0500131 canDrawArgs.fCaps = context->priv().caps();
Greg Daniel46e366a2019-12-16 14:38:36 -0500132 canDrawArgs.fProxy = renderTargetContext->asRenderTargetProxy();
Chris Daltonc5348082018-03-30 15:59:38 +0000133 canDrawArgs.fClipConservativeBounds = &scissorRect;
134 canDrawArgs.fViewMatrix = &viewMatrix;
135 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400136 canDrawArgs.fPaint = nullptr;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600137 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -0500138 SkASSERT(!renderTargetContext->wrapsVkSecondaryCB());
139 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
Chris Daltonc5348082018-03-30 15:59:38 +0000140 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
141
142 // the 'false' parameter disallows use of the SW path renderer
143 GrPathRenderer* pr =
Robert Phillips9da87e02019-02-04 13:26:26 -0500144 context->priv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
Chris Daltonc5348082018-03-30 15:59:38 +0000145 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 Phillips6f0e02f2019-02-13 11:02:28 -0500157bool GrClipStackClip::UseSWOnlyPath(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000158 bool hasUserStencilSettings,
159 const GrRenderTargetContext* renderTargetContext,
160 const GrReducedClip& reducedClip) {
Chris Daltond22a0232018-08-22 17:25:10 +0000161 // 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 Daltonc5348082018-03-30 15:59:38 +0000165 // 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 Danielbe7fc462019-01-03 16:40:42 -0500169 // 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 Phillips9da87e02019-02-04 13:26:26 -0500171 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500172 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000173 return true;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400174 }
Chris Daltonc5348082018-03-30 15:59:38 +0000175
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 Salomonc3833b42018-07-09 18:23:58 +0000182 const Element* element = iter.get();
Chris Daltonc5348082018-03-30 15:59:38 +0000183
Brian Salomonc3833b42018-07-09 18:23:58 +0000184 SkClipOp op = element->getOp();
185 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000186 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 Phillips6f0e02f2019-02-13 11:02:28 -0500200bool GrClipStackClip::apply(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400201 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
202 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400203 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
204 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700205 return false;
206 }
207
Brian Salomon510dd422017-03-16 12:15:22 -0400208 if (!fStack || fStack->isWideOpen()) {
209 return true;
210 }
211
Brian Osman5f5680c2019-06-05 10:17:25 -0400212 // 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 Daltona32a3c32017-12-05 10:05:21 -0700220 int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
Brian Osman5f5680c2019-06-05 10:17:25 -0400221 int maxAnalyticFPs = kMaxAnalyticFPs;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600222 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 Salomonc7fe0f72018-05-11 10:14:21 -0400225 // We disable MSAA when avoiding stencil.
Robert Phillips9da87e02019-02-04 13:26:26 -0500226 SkASSERT(!context->priv().caps()->avoidStencilBuffers());
Chris Dalton584a79a2017-11-15 13:14:01 -0700227 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500228 auto* ccpr = context->priv().drawingManager()->getCoverageCountingPathRenderer();
Chris Dalton584a79a2017-11-15 13:14:01 -0700229
Robert Phillips9da87e02019-02-04 13:26:26 -0500230 GrReducedClip reducedClip(*fStack, devBounds, context->priv().caps(),
Ethan Nicholaseace9352018-10-15 20:09:54 +0000231 maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0);
Chris Daltona32a3c32017-12-05 10:05:21 -0700232 if (InitialState::kAllOut == reducedClip.initialState() &&
233 reducedClip.maskElements().isEmpty()) {
234 return false;
235 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000236
Chris Dalton79471932017-10-27 01:50:57 -0600237 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700238 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700239 }
cdalton93a379b2016-05-11 13:58:08 -0700240
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700241 if (!reducedClip.windowRectangles().empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700242 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
243 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700244 }
245
Chris Daltona32a3c32017-12-05 10:05:21 -0700246 if (!reducedClip.maskElements().isEmpty()) {
247 if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
248 out)) {
249 return false;
250 }
251 }
252
Greg Danielf41b2bd2019-08-22 16:19:24 -0400253 // 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();
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400256 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(context, *fMatrixProvider, ccpr,
257 opsTaskID)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700258 out->addCoverageFP(std::move(clipFPs));
259 }
260
Chris Daltona32a3c32017-12-05 10:05:21 -0700261 return true;
262}
csmartdaltond211e782016-08-15 11:17:19 -0700263
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500264bool GrClipStackClip::applyClipMask(GrRecordingContext* context,
265 GrRenderTargetContext* renderTargetContext,
Chris Daltona32a3c32017-12-05 10:05:21 -0700266 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
267 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700268#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600269 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500270 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
271 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600272 const SkIRect& scissor = reducedClip.scissor();
273 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700274#endif
csmartdaltond211e782016-08-15 11:17:19 -0700275
Chris Dalton6ce447a2019-06-23 18:07:38 -0600276 // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping.
277 if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500278 context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500279 renderTargetContext->wrapsVkSecondaryCB()) {
Greg Daniele32506b2020-02-10 16:00:54 -0500280 GrSurfaceProxyView result;
Chris Daltonc5348082018-03-30 15:59:38 +0000281 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
282 // The clip geometry is complex enough that it will be more efficient to create it
283 // entirely in software
284 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
285 } else {
286 result = this->createAlphaClipMask(context, reducedClip);
287 }
288
289 if (result) {
290 // The mask's top left coord should be pinned to the rounded-out top left corner of
291 // the clip's device space bounds.
Brian Salomon64227222020-02-26 13:28:42 -0500292 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor(),
293 *context->priv().caps()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000294 return true;
295 }
Eric Karl5c779752017-05-08 12:02:07 -0700296
Chris Daltonc5348082018-03-30 15:59:38 +0000297 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
298 // unless stencils are disallowed.
Robert Phillips9da87e02019-02-04 13:26:26 -0500299 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500300 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700301 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
302 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700303 return false;
304 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000305 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000306
Michael Ludwig828d3412020-05-12 13:15:35 -0400307 reducedClip.drawStencilClipMask(context, renderTargetContext);
Brian Salomonc3833b42018-07-09 18:23:58 +0000308 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000309 return true;
310}
311
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000312////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700313// Create a 8-bit clip mask in alpha
314
Brian Salomonc3833b42018-07-09 18:23:58 +0000315static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
316 GrUniqueKey* key) {
317 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
318 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
319 builder[0] = clipGenID;
320 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
321 // sometimes result in negative coordinates from device space.
322 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
323 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
324 builder[3] = numAnalyticFPs;
325}
326
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500327static void add_invalidate_on_pop_message(GrRecordingContext* context,
Robert Phillips427966a2018-12-20 17:20:43 -0500328 const SkClipStack& stack, uint32_t clipGenID,
329 const GrUniqueKey& clipMaskKey) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500330 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips427966a2018-12-20 17:20:43 -0500331
Brian Salomon19f0ed52017-01-06 13:54:58 -0500332 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
333 while (const Element* element = iter.prev()) {
334 if (element->getGenID() == clipGenID) {
Robert Phillips427966a2018-12-20 17:20:43 -0500335 element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500336 return;
337 }
338 }
339 SkDEBUGFAIL("Gen ID was not found in stack.");
340}
341
Brian Salomond005b692020-04-01 15:47:05 -0400342static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
343
344static GrSurfaceProxyView find_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
Brian Salomon0029db02020-04-03 10:41:24 -0400345 return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8,
346 1);
Brian Salomond005b692020-04-01 15:47:05 -0400347}
348
Greg Daniele32506b2020-02-10 16:00:54 -0500349GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
350 const GrReducedClip& reducedClip) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500351 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc3833b42018-07-09 18:23:58 +0000352 GrUniqueKey key;
353 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
354 reducedClip.numAnalyticFPs(), &key);
355
Brian Salomond005b692020-04-01 15:47:05 -0400356 if (auto cachedView = find_mask(context->priv().proxyProvider(), key)) {
357 return cachedView;
Chris Daltonc5348082018-03-30 15:59:38 +0000358 }
359
Greg Daniele20fcad2020-01-08 11:52:34 -0500360 auto rtc = GrRenderTargetContext::MakeWithFallback(
361 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox,
Greg Daniele32506b2020-02-10 16:00:54 -0500362 {reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomond005b692020-04-01 15:47:05 -0400363 kMaskOrigin);
Chris Daltonc5348082018-03-30 15:59:38 +0000364 if (!rtc) {
Greg Daniele32506b2020-02-10 16:00:54 -0500365 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000366 }
367
368 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
Greg Daniele32506b2020-02-10 16:00:54 -0500369 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000370 }
371
Greg Daniele32506b2020-02-10 16:00:54 -0500372 GrSurfaceProxyView result = rtc->readSurfaceView();
373 if (!result || !result.asTextureProxy()) {
374 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000375 }
376
Brian Salomond005b692020-04-01 15:47:05 -0400377 SkASSERT(result.origin() == kMaskOrigin);
Greg Daniele32506b2020-02-10 16:00:54 -0500378 proxyProvider->assignUniqueKeyToProxy(key, result.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500379 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Chris Daltonc5348082018-03-30 15:59:38 +0000380
381 return result;
382}
383
Brian Osman5d034742017-09-11 13:38:55 -0400384namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500385
Brian Osman5d034742017-09-11 13:38:55 -0400386/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400387 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400388 * elements, each storing their own AA setting (and already transformed into device space). This
389 * stores all of the information needed by the worker thread to draw all clip elements (see below,
390 * in createSoftwareClipMask).
391 */
392class ClipMaskData {
393public:
394 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600395 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400396 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600397 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400398 fElements.addToTail(*iter.get());
399 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000400 }
401
Chris Dalton79471932017-10-27 01:50:57 -0600402 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400403 InitialState initialState() const { return fInitialState; }
404 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000405
Brian Osman5d034742017-09-11 13:38:55 -0400406private:
Chris Dalton79471932017-10-27 01:50:57 -0600407 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400408 InitialState fInitialState;
409 ElementList fElements;
410};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000411
Brian Osman5d034742017-09-11 13:38:55 -0400412}
413
414static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600415 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400416 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800417 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600418 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800419
Brian Osman5d034742017-09-11 13:38:55 -0400420 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000421
Brian Osman5d034742017-09-11 13:38:55 -0400422 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000423 const Element* element = iter.get();
424 SkClipOp op = element->getOp();
425 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000426
Mike Reedc1f77742016-12-09 09:00:50 -0500427 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 // Intersect and reverse difference require modifying pixels outside of the geometry
429 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
430 // but leave the pixels inside the geometry alone. For reverse difference we invert all
431 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500432 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600433 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000434 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400435 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000436 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000437 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000438 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000439 clipPath.toggleInverseFillType();
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400440 helper.drawShape(GrShape(clipPath), translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000441 continue;
442 }
443
444 // The other ops (union, xor, diff) only affect pixels inside
445 // the geometry so they can just be drawn normally
Brian Salomonc3833b42018-07-09 18:23:58 +0000446 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
447 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400448 } else if (Element::DeviceSpaceType::kRRect == element->getDeviceSpaceType()) {
449 helper.drawRRect(element->getDeviceSpaceRRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000450 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000451 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000452 element->asDeviceSpacePath(&path);
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400453 helper.drawShape(GrShape(path), translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000454 }
455 }
Brian Osman5d034742017-09-11 13:38:55 -0400456}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000457
Greg Daniele32506b2020-02-10 16:00:54 -0500458GrSurfaceProxyView GrClipStackClip::createSoftwareClipMask(
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500459 GrRecordingContext* context, const GrReducedClip& reducedClip,
Brian Osman5d034742017-09-11 13:38:55 -0400460 GrRenderTargetContext* renderTargetContext) const {
Brian Salomonc3833b42018-07-09 18:23:58 +0000461 GrUniqueKey key;
462 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
463 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800464
Robert Phillips9da87e02019-02-04 13:26:26 -0500465 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500466
Brian Salomond005b692020-04-01 15:47:05 -0400467 if (auto cachedView = find_mask(proxyProvider, key)) {
468 return cachedView;
Brian Osman5d034742017-09-11 13:38:55 -0400469 }
470
471 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
472 // left corner of the resulting rect to the top left of the texture.
473 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
474
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500475 SkTaskGroup* taskGroup = nullptr;
476 if (auto direct = context->priv().asDirectContext()) {
477 taskGroup = direct->priv().getTaskGroup();
478 }
479
Greg Daniele32506b2020-02-10 16:00:54 -0500480 GrSurfaceProxyView view;
Brian Osman5d034742017-09-11 13:38:55 -0400481 if (taskGroup && renderTargetContext) {
Brian Salomond005b692020-04-01 15:47:05 -0400482 const GrCaps* caps = context->priv().caps();
Brian Osman5d034742017-09-11 13:38:55 -0400483 // Create our texture proxy
Robert Phillips0a15cc62019-07-30 12:49:10 -0400484 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
485 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500486
Greg Daniel47c20e82020-01-21 14:29:57 -0500487 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kAlpha_8);
488
Brian Osmand140fe92017-10-03 12:17:26 -0400489 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
490 // to ops), so it can't have any pending IO.
Greg Daniele32506b2020-02-10 16:00:54 -0500491 auto proxy = proxyProvider->createProxy(format,
492 maskSpaceIBounds.size(),
Greg Daniele32506b2020-02-10 16:00:54 -0500493 GrRenderable::kNo,
494 1,
Greg Daniele32506b2020-02-10 16:00:54 -0500495 GrMipMapped::kNo,
496 SkBackingFit::kApprox,
497 SkBudgeted::kYes,
498 GrProtected::kNo);
Brian Osman5d034742017-09-11 13:38:55 -0400499
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500500 auto uploader = std::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
Brian Osman099fa0f2017-10-02 16:38:32 -0400501 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400502 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
Brian Salomon5f394272019-07-02 14:07:49 -0400503 TRACE_EVENT0("skia.gpu", "Threaded SW Clip Mask Render");
Brian Osman5d034742017-09-11 13:38:55 -0400504 GrSWMaskHelper helper(uploaderRaw->getPixels());
505 if (helper.init(maskSpaceIBounds)) {
506 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
Chris Dalton79471932017-10-27 01:50:57 -0600507 uploaderRaw->data().scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400508 uploaderRaw->data().initialState());
509 } else {
510 SkDEBUGFAIL("Unable to allocate SW clip mask.");
511 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400512 uploaderRaw->signalAndFreeData();
Brian Osman5d034742017-09-11 13:38:55 -0400513 };
514
515 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400516 proxy->texPriv().setDeferredUploader(std::move(uploader));
Greg Daniele32506b2020-02-10 16:00:54 -0500517
Brian Salomond005b692020-04-01 15:47:05 -0400518 view = {std::move(proxy), kMaskOrigin, swizzle};
Brian Osman5d034742017-09-11 13:38:55 -0400519 } else {
520 GrSWMaskHelper helper;
521 if (!helper.init(maskSpaceIBounds)) {
Greg Daniele32506b2020-02-10 16:00:54 -0500522 return {};
Brian Osman5d034742017-09-11 13:38:55 -0400523 }
524
Chris Dalton79471932017-10-27 01:50:57 -0600525 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400526 reducedClip.initialState());
527
Greg Daniele32506b2020-02-10 16:00:54 -0500528 view = helper.toTextureView(context, SkBackingFit::kApprox);
Brian Osman5d034742017-09-11 13:38:55 -0400529 }
530
Greg Daniele32506b2020-02-10 16:00:54 -0500531 SkASSERT(view);
Brian Salomond005b692020-04-01 15:47:05 -0400532 SkASSERT(view.origin() == kMaskOrigin);
Greg Daniele32506b2020-02-10 16:00:54 -0500533 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500534 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Greg Daniele32506b2020-02-10 16:00:54 -0500535 return view;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000536}