blob: 283d3a8791ba1534d6937150faa2741dc41e71b8 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkTo.h"
9#include "src/core/SkClipOpPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkTaskGroup.h"
11#include "src/core/SkTraceEvent.h"
12#include "src/gpu/GrAppliedClip.h"
13#include "src/gpu/GrClipStackClip.h"
14#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrDeferredProxyUploader.h"
16#include "src/gpu/GrDrawingManager.h"
17#include "src/gpu/GrFixedClip.h"
18#include "src/gpu/GrGpuResourcePriv.h"
19#include "src/gpu/GrProxyProvider.h"
20#include "src/gpu/GrRecordingContextPriv.h"
21#include "src/gpu/GrRenderTargetContextPriv.h"
22#include "src/gpu/GrSWMaskHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrStencilAttachment.h"
24#include "src/gpu/GrStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/effects/GrConvexPolyEffect.h"
27#include "src/gpu/effects/GrRRectEffect.h"
28#include "src/gpu/effects/GrTextureDomain.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040029#include "src/gpu/geometry/GrShape.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000030
Brian Salomonc3833b42018-07-09 18:23:58 +000031typedef SkClipStack::Element Element;
32typedef GrReducedClip::InitialState InitialState;
33typedef GrReducedClip::ElementList ElementList;
34
35const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070036
csmartdaltonc6f411e2016-08-05 22:32:12 -070037bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070038 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070039 return true;
40 }
Brian Salomon9a767722017-03-13 17:57:28 -040041 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070042}
43
bsalomon7f0d9f32016-08-15 14:49:10 -070044bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070045 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070046 return true;
47 }
Brian Salomon9a767722017-03-13 17:57:28 -040048 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070049}
50
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050051bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070052 if (!fStack) {
53 return false;
54 }
55 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050056 bool isAA;
57 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
Chris Dalton3b51df12017-11-27 14:33:06 -070058 *aa = GrAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070059 return true;
60 }
61 return false;
62}
63
csmartdaltonc6f411e2016-08-05 22:32:12 -070064void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
65 bool* isIntersectionOfRects) const {
66 if (!fStack) {
67 devResult->setXYWH(0, 0, width, height);
68 if (isIntersectionOfRects) {
69 *isIntersectionOfRects = true;
70 }
71 return;
72 }
73 SkRect devBounds;
Brian Salomon9a767722017-03-13 17:57:28 -040074 fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
csmartdaltonc6f411e2016-08-05 22:32:12 -070075 devBounds.roundOut(devResult);
76}
77
Chris Daltonc5348082018-03-30 15:59:38 +000078////////////////////////////////////////////////////////////////////////////////
79// set up the draw state to enable the aa clipping mask.
Greg Daniele32506b2020-02-10 16:00:54 -050080static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(GrSurfaceProxyView mask,
Chris Daltonc5348082018-03-30 15:59:38 +000081 const SkIRect& devBound) {
Greg Daniele32506b2020-02-10 16:00:54 -050082 SkASSERT(mask.asTextureProxy());
Chris Daltonc5348082018-03-30 15:59:38 +000083 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
84 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(mask), domainTexels,
85 {devBound.fLeft, devBound.fTop});
86}
87
88// Does the path in 'element' require SW rendering? If so, return true (and,
89// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
90// 'prOut' to the non-SW path renderer that will do the job).
Robert Phillips6f0e02f2019-02-13 11:02:28 -050091bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +000092 const SkIRect& scissorRect,
93 bool hasUserStencilSettings,
94 const GrRenderTargetContext* renderTargetContext,
95 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +000096 const Element* element,
Chris Daltonc5348082018-03-30 15:59:38 +000097 GrPathRenderer** prOut,
98 bool needsStencil) {
Brian Salomonc3833b42018-07-09 18:23:58 +000099 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000100 // rects can always be drawn directly w/o using the software path
101 // TODO: skip rrects once we're drawing them directly.
102 if (prOut) {
103 *prOut = nullptr;
104 }
105 return false;
106 } else {
107 // We shouldn't get here with an empty clip element.
Brian Salomonc3833b42018-07-09 18:23:58 +0000108 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
Chris Daltonc5348082018-03-30 15:59:38 +0000109
110 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
111 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000112 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000113 if (path.isInverseFillType()) {
114 path.toggleInverseFillType();
115 }
116
Chris Dalton09e56892019-03-13 00:22:01 -0600117 // We only use this method when rendering coverage clip masks.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600118 SkASSERT(renderTargetContext->numSamples() <= 1);
119 auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600120
Chris Daltonc5348082018-03-30 15:59:38 +0000121 GrPathRendererChain::DrawType type =
122 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
123 : GrPathRendererChain::DrawType::kColor;
124
125 GrShape shape(path, GrStyle::SimpleFill());
126 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -0500127 canDrawArgs.fCaps = context->priv().caps();
Greg Daniel46e366a2019-12-16 14:38:36 -0500128 canDrawArgs.fProxy = renderTargetContext->asRenderTargetProxy();
Chris Daltonc5348082018-03-30 15:59:38 +0000129 canDrawArgs.fClipConservativeBounds = &scissorRect;
130 canDrawArgs.fViewMatrix = &viewMatrix;
131 canDrawArgs.fShape = &shape;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600132 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -0500133 SkASSERT(!renderTargetContext->wrapsVkSecondaryCB());
134 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
Chris Daltonc5348082018-03-30 15:59:38 +0000135 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
136
137 // the 'false' parameter disallows use of the SW path renderer
138 GrPathRenderer* pr =
Robert Phillips9da87e02019-02-04 13:26:26 -0500139 context->priv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
Chris Daltonc5348082018-03-30 15:59:38 +0000140 if (prOut) {
141 *prOut = pr;
142 }
143 return SkToBool(!pr);
144 }
145}
146
147/*
148 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
149 * will be used on any element. If so, it returns true to indicate that the
150 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
151 */
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500152bool GrClipStackClip::UseSWOnlyPath(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000153 bool hasUserStencilSettings,
154 const GrRenderTargetContext* renderTargetContext,
155 const GrReducedClip& reducedClip) {
Chris Daltond22a0232018-08-22 17:25:10 +0000156 // TODO: right now it appears that GPU clip masks are strictly slower than software. We may
157 // want to revisit this assumption once we can test with render target sorting.
158 return true;
159
Chris Daltonc5348082018-03-30 15:59:38 +0000160 // TODO: generalize this function so that when
161 // a clip gets complex enough it can just be done in SW regardless
162 // of whether it would invoke the GrSoftwarePathRenderer.
163
Greg Danielbe7fc462019-01-03 16:40:42 -0500164 // If we're avoiding stencils, always use SW. This includes drawing into a wrapped vulkan
165 // secondary command buffer which can't handle stencils.
Robert Phillips9da87e02019-02-04 13:26:26 -0500166 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500167 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000168 return true;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400169 }
Chris Daltonc5348082018-03-30 15:59:38 +0000170
171 // Set the matrix so that rendered clip elements are transformed to mask space from clip
172 // space.
173 SkMatrix translate;
174 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
175
176 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000177 const Element* element = iter.get();
Chris Daltonc5348082018-03-30 15:59:38 +0000178
Brian Salomonc3833b42018-07-09 18:23:58 +0000179 SkClipOp op = element->getOp();
180 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000181 bool needsStencil = invert ||
182 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
183
184 if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings,
185 renderTargetContext, translate, element, nullptr, needsStencil)) {
186 return true;
187 }
188 }
189 return false;
190}
191
192////////////////////////////////////////////////////////////////////////////////
193// sort out what kind of clip mask needs to be created: alpha, stencil,
194// scissor, or entirely software
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500195bool GrClipStackClip::apply(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400196 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
197 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400198 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
199 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700200 return false;
201 }
202
Brian Salomon510dd422017-03-16 12:15:22 -0400203 if (!fStack || fStack->isWideOpen()) {
204 return true;
205 }
206
Brian Osman5f5680c2019-06-05 10:17:25 -0400207 // An default count of 4 was chosen because of the common pattern in Blink of:
208 // isect RR
209 // diff RR
210 // isect convex_poly
211 // isect convex_poly
212 // when drawing rounded div borders.
213 constexpr int kMaxAnalyticFPs = 4;
214
Chris Daltona32a3c32017-12-05 10:05:21 -0700215 int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
Brian Osman5f5680c2019-06-05 10:17:25 -0400216 int maxAnalyticFPs = kMaxAnalyticFPs;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600217 if (renderTargetContext->numSamples() > 1 || useHWAA || hasUserStencilSettings) {
218 // Disable analytic clips when we have MSAA. In MSAA we never conflate coverage and opacity.
219 maxAnalyticFPs = 0;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400220 // We disable MSAA when avoiding stencil.
Robert Phillips9da87e02019-02-04 13:26:26 -0500221 SkASSERT(!context->priv().caps()->avoidStencilBuffers());
Chris Dalton584a79a2017-11-15 13:14:01 -0700222 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500223 auto* ccpr = context->priv().drawingManager()->getCoverageCountingPathRenderer();
Chris Dalton584a79a2017-11-15 13:14:01 -0700224
Robert Phillips9da87e02019-02-04 13:26:26 -0500225 GrReducedClip reducedClip(*fStack, devBounds, context->priv().caps(),
Ethan Nicholaseace9352018-10-15 20:09:54 +0000226 maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0);
Chris Daltona32a3c32017-12-05 10:05:21 -0700227 if (InitialState::kAllOut == reducedClip.initialState() &&
228 reducedClip.maskElements().isEmpty()) {
229 return false;
230 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000231
Chris Dalton79471932017-10-27 01:50:57 -0600232 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700233 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700234 }
cdalton93a379b2016-05-11 13:58:08 -0700235
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700236 if (!reducedClip.windowRectangles().empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700237 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
238 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700239 }
240
Chris Daltona32a3c32017-12-05 10:05:21 -0700241 if (!reducedClip.maskElements().isEmpty()) {
242 if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
243 out)) {
244 return false;
245 }
246 }
247
Greg Danielf41b2bd2019-08-22 16:19:24 -0400248 // The opsTask ID must not be looked up until AFTER producing the clip mask (if any). That step
249 // can cause a flush or otherwise change which opstask our draw is going into.
250 uint32_t opsTaskID = renderTargetContext->getOpsTask()->uniqueID();
251 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(ccpr, opsTaskID)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700252 out->addCoverageFP(std::move(clipFPs));
253 }
254
Chris Daltona32a3c32017-12-05 10:05:21 -0700255 return true;
256}
csmartdaltond211e782016-08-15 11:17:19 -0700257
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500258bool GrClipStackClip::applyClipMask(GrRecordingContext* context,
259 GrRenderTargetContext* renderTargetContext,
Chris Daltona32a3c32017-12-05 10:05:21 -0700260 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
261 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700262#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600263 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500264 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
265 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600266 const SkIRect& scissor = reducedClip.scissor();
267 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700268#endif
csmartdaltond211e782016-08-15 11:17:19 -0700269
Chris Dalton6ce447a2019-06-23 18:07:38 -0600270 // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping.
271 if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500272 context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500273 renderTargetContext->wrapsVkSecondaryCB()) {
Greg Daniele32506b2020-02-10 16:00:54 -0500274 GrSurfaceProxyView result;
Chris Daltonc5348082018-03-30 15:59:38 +0000275 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
276 // The clip geometry is complex enough that it will be more efficient to create it
277 // entirely in software
278 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
279 } else {
280 result = this->createAlphaClipMask(context, reducedClip);
281 }
282
283 if (result) {
284 // The mask's top left coord should be pinned to the rounded-out top left corner of
285 // the clip's device space bounds.
286 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000287 return true;
288 }
Eric Karl5c779752017-05-08 12:02:07 -0700289
Chris Daltonc5348082018-03-30 15:59:38 +0000290 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
291 // unless stencils are disallowed.
Robert Phillips9da87e02019-02-04 13:26:26 -0500292 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500293 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700294 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
295 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700296 return false;
297 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000298 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000299
Brian Salomonc3833b42018-07-09 18:23:58 +0000300 // This relies on the property that a reduced sub-rect of the last clip will contain all the
301 // relevant window rectangles that were in the last clip. This subtle requirement will go away
302 // after clipping is overhauled.
303 if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(), reducedClip.scissor(),
304 reducedClip.numAnalyticFPs())) {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000305 reducedClip.drawStencilClipMask(context, renderTargetContext);
Brian Salomonc3833b42018-07-09 18:23:58 +0000306 renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor(),
307 reducedClip.numAnalyticFPs());
csmartdaltonbde96c62016-08-31 12:54:46 -0700308 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000309 // GrAppliedClip doesn't need to figure numAnalyticFPs into its key (used by operator==) because
310 // it verifies the FPs are also equal.
311 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 return true;
313}
314
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000315////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700316// Create a 8-bit clip mask in alpha
317
Brian Salomonc3833b42018-07-09 18:23:58 +0000318static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
319 GrUniqueKey* key) {
320 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
321 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
322 builder[0] = clipGenID;
323 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
324 // sometimes result in negative coordinates from device space.
325 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
326 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
327 builder[3] = numAnalyticFPs;
328}
329
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500330static void add_invalidate_on_pop_message(GrRecordingContext* context,
Robert Phillips427966a2018-12-20 17:20:43 -0500331 const SkClipStack& stack, uint32_t clipGenID,
332 const GrUniqueKey& clipMaskKey) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500333 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips427966a2018-12-20 17:20:43 -0500334
Brian Salomon19f0ed52017-01-06 13:54:58 -0500335 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
336 while (const Element* element = iter.prev()) {
337 if (element->getGenID() == clipGenID) {
Robert Phillips427966a2018-12-20 17:20:43 -0500338 element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500339 return;
340 }
341 }
342 SkDEBUGFAIL("Gen ID was not found in stack.");
343}
344
Greg Daniele32506b2020-02-10 16:00:54 -0500345GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
346 const GrReducedClip& reducedClip) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500347 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc3833b42018-07-09 18:23:58 +0000348 GrUniqueKey key;
349 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
350 reducedClip.numAnalyticFPs(), &key);
351
Greg Daniele32506b2020-02-10 16:00:54 -0500352 if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(
353 key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin)) {
354 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
355 GrColorType::kAlpha_8);
356 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Chris Daltonc5348082018-03-30 15:59:38 +0000357 }
358
Greg Daniele20fcad2020-01-08 11:52:34 -0500359 auto rtc = GrRenderTargetContext::MakeWithFallback(
360 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox,
Greg Daniele32506b2020-02-10 16:00:54 -0500361 {reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, GrProtected::kNo,
Greg Daniele20fcad2020-01-08 11:52:34 -0500362 kTopLeft_GrSurfaceOrigin);
Chris Daltonc5348082018-03-30 15:59:38 +0000363 if (!rtc) {
Greg Daniele32506b2020-02-10 16:00:54 -0500364 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000365 }
366
367 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
Greg Daniele32506b2020-02-10 16:00:54 -0500368 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000369 }
370
Greg Daniele32506b2020-02-10 16:00:54 -0500371 GrSurfaceProxyView result = rtc->readSurfaceView();
372 if (!result || !result.asTextureProxy()) {
373 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000374 }
375
Greg Daniele32506b2020-02-10 16:00:54 -0500376 SkASSERT(result.origin() == kTopLeft_GrSurfaceOrigin);
377 proxyProvider->assignUniqueKeyToProxy(key, result.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500378 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Chris Daltonc5348082018-03-30 15:59:38 +0000379
380 return result;
381}
382
Brian Osman5d034742017-09-11 13:38:55 -0400383namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500384
Brian Osman5d034742017-09-11 13:38:55 -0400385/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400386 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400387 * 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 */
391class ClipMaskData {
392public:
393 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600394 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400395 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600396 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400397 fElements.addToTail(*iter.get());
398 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000399 }
400
Chris Dalton79471932017-10-27 01:50:57 -0600401 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400402 InitialState initialState() const { return fInitialState; }
403 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000404
Brian Osman5d034742017-09-11 13:38:55 -0400405private:
Chris Dalton79471932017-10-27 01:50:57 -0600406 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400407 InitialState fInitialState;
408 ElementList fElements;
409};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000410
Brian Osman5d034742017-09-11 13:38:55 -0400411}
412
413static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600414 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400415 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800416 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600417 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800418
Brian Osman5d034742017-09-11 13:38:55 -0400419 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000420
Brian Osman5d034742017-09-11 13:38:55 -0400421 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000422 const Element* element = iter.get();
423 SkClipOp op = element->getOp();
424 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000425
Mike Reedc1f77742016-12-09 09:00:50 -0500426 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 // 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 Reedc1f77742016-12-09 09:00:50 -0500431 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600432 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000433 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400434 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000435 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000436 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000437 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000438 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700439 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400440 helper.drawShape(shape, 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);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000448 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000449 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000450 element->asDeviceSpacePath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700451 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400452 helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000453 }
454 }
Brian Osman5d034742017-09-11 13:38:55 -0400455}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000456
Greg Daniele32506b2020-02-10 16:00:54 -0500457GrSurfaceProxyView GrClipStackClip::createSoftwareClipMask(
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500458 GrRecordingContext* context, const GrReducedClip& reducedClip,
Brian Osman5d034742017-09-11 13:38:55 -0400459 GrRenderTargetContext* renderTargetContext) const {
Brian Salomonc3833b42018-07-09 18:23:58 +0000460 GrUniqueKey key;
461 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
462 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800463
Robert Phillips9da87e02019-02-04 13:26:26 -0500464 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400465 const GrCaps* caps = context->priv().caps();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500466
Greg Daniele32506b2020-02-10 16:00:54 -0500467 if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(
468 key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin)) {
469 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
470 GrColorType::kAlpha_8);
471 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Brian Osman5d034742017-09-11 13:38:55 -0400472 }
473
474 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
475 // left corner of the resulting rect to the top left of the texture.
476 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
477
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500478 SkTaskGroup* taskGroup = nullptr;
479 if (auto direct = context->priv().asDirectContext()) {
480 taskGroup = direct->priv().getTaskGroup();
481 }
482
Greg Daniele32506b2020-02-10 16:00:54 -0500483 GrSurfaceProxyView view;
Brian Osman5d034742017-09-11 13:38:55 -0400484 if (taskGroup && renderTargetContext) {
485 // Create our texture proxy
Robert Phillips0a15cc62019-07-30 12:49:10 -0400486 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
487 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500488
Greg Daniel47c20e82020-01-21 14:29:57 -0500489 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kAlpha_8);
490
Brian Osmand140fe92017-10-03 12:17:26 -0400491 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
492 // to ops), so it can't have any pending IO.
Greg Daniele32506b2020-02-10 16:00:54 -0500493 auto proxy = proxyProvider->createProxy(format,
494 maskSpaceIBounds.size(),
495 swizzle,
496 GrRenderable::kNo,
497 1,
498 kTopLeft_GrSurfaceOrigin,
499 GrMipMapped::kNo,
500 SkBackingFit::kApprox,
501 SkBudgeted::kYes,
502 GrProtected::kNo);
Brian Osman5d034742017-09-11 13:38:55 -0400503
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500504 auto uploader = std::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
Brian Osman099fa0f2017-10-02 16:38:32 -0400505 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400506 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
Brian Salomon5f394272019-07-02 14:07:49 -0400507 TRACE_EVENT0("skia.gpu", "Threaded SW Clip Mask Render");
Brian Osman5d034742017-09-11 13:38:55 -0400508 GrSWMaskHelper helper(uploaderRaw->getPixels());
509 if (helper.init(maskSpaceIBounds)) {
510 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
Chris Dalton79471932017-10-27 01:50:57 -0600511 uploaderRaw->data().scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400512 uploaderRaw->data().initialState());
513 } else {
514 SkDEBUGFAIL("Unable to allocate SW clip mask.");
515 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400516 uploaderRaw->signalAndFreeData();
Brian Osman5d034742017-09-11 13:38:55 -0400517 };
518
519 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400520 proxy->texPriv().setDeferredUploader(std::move(uploader));
Greg Daniele32506b2020-02-10 16:00:54 -0500521
522 view = {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Brian Osman5d034742017-09-11 13:38:55 -0400523 } else {
524 GrSWMaskHelper helper;
525 if (!helper.init(maskSpaceIBounds)) {
Greg Daniele32506b2020-02-10 16:00:54 -0500526 return {};
Brian Osman5d034742017-09-11 13:38:55 -0400527 }
528
Chris Dalton79471932017-10-27 01:50:57 -0600529 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400530 reducedClip.initialState());
531
Greg Daniele32506b2020-02-10 16:00:54 -0500532 view = helper.toTextureView(context, SkBackingFit::kApprox);
Brian Osman5d034742017-09-11 13:38:55 -0400533 }
534
Greg Daniele32506b2020-02-10 16:00:54 -0500535 SkASSERT(view);
536 SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
537 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500538 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Greg Daniele32506b2020-02-10 16:00:54 -0500539 return view;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000540}