1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/private/SkTo.h"
9 #include "src/core/SkClipOpPriv.h"
10 #include "src/core/SkMakeUnique.h"
11 #include "src/core/SkTaskGroup.h"
12 #include "src/core/SkTraceEvent.h"
13 #include "src/gpu/GrAppliedClip.h"
14 #include "src/gpu/GrClipStackClip.h"
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"
24 #include "src/gpu/GrStencilAttachment.h"
25 #include "src/gpu/GrStyle.h"
26 #include "src/gpu/GrTextureProxy.h"
27 #include "src/gpu/effects/GrConvexPolyEffect.h"
28 #include "src/gpu/effects/GrRRectEffect.h"
29 #include "src/gpu/effects/GrTextureDomain.h"
30 #include "src/gpu/geometry/GrShape.h"
31 
32 typedef SkClipStack::Element Element;
33 typedef GrReducedClip::InitialState InitialState;
34 typedef GrReducedClip::ElementList ElementList;
35 
36 const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
37 
quickContains(const SkRect & rect) const38 bool GrClipStackClip::quickContains(const SkRect& rect) const {
39     if (!fStack || fStack->isWideOpen()) {
40         return true;
41     }
42     return fStack->quickContains(rect);
43 }
44 
quickContains(const SkRRect & rrect) const45 bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
46     if (!fStack || fStack->isWideOpen()) {
47         return true;
48     }
49     return fStack->quickContains(rrect);
50 }
51 
isRRect(const SkRect & origRTBounds,SkRRect * rr,GrAA * aa) const52 bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
53     if (!fStack) {
54         return false;
55     }
56     const SkRect* rtBounds = &origRTBounds;
57     bool isAA;
58     if (fStack->isRRect(*rtBounds, rr, &isAA)) {
59         *aa = GrAA(isAA);
60         return true;
61     }
62     return false;
63 }
64 
getConservativeBounds(int width,int height,SkIRect * devResult,bool * isIntersectionOfRects) const65 void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
66                                             bool* isIntersectionOfRects) const {
67     if (!fStack) {
68         devResult->setXYWH(0, 0, width, height);
69         if (isIntersectionOfRects) {
70             *isIntersectionOfRects = true;
71         }
72         return;
73     }
74     SkRect devBounds;
75     fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
76     devBounds.roundOut(devResult);
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 // set up the draw state to enable the aa clipping mask.
create_fp_for_mask(sk_sp<GrTextureProxy> mask,const SkIRect & devBound)81 static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(sk_sp<GrTextureProxy> mask,
82                                                                const SkIRect& devBound) {
83     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).
PathNeedsSWRenderer(GrRecordingContext * context,const SkIRect & scissorRect,bool hasUserStencilSettings,const GrRenderTargetContext * renderTargetContext,const SkMatrix & viewMatrix,const Element * element,GrPathRenderer ** prOut,bool needsStencil)91 bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context,
92                                           const SkIRect& scissorRect,
93                                           bool hasUserStencilSettings,
94                                           const GrRenderTargetContext* renderTargetContext,
95                                           const SkMatrix& viewMatrix,
96                                           const Element* element,
97                                           GrPathRenderer** prOut,
98                                           bool needsStencil) {
99     if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
100         // 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.
108         SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
109 
110         // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
111         SkPath path;
112         element->asDeviceSpacePath(&path);
113         if (path.isInverseFillType()) {
114             path.toggleInverseFillType();
115         }
116 
117         // We only use this method when rendering coverage clip masks.
118         SkASSERT(renderTargetContext->numSamples() <= 1);
119         auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone;
120 
121         GrPathRendererChain::DrawType type =
122                 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
123                              : GrPathRendererChain::DrawType::kColor;
124 
125         GrShape shape(path, GrStyle::SimpleFill());
126         GrPathRenderer::CanDrawPathArgs canDrawArgs;
127         canDrawArgs.fCaps = context->priv().caps();
128         canDrawArgs.fProxy = renderTargetContext->proxy();
129         canDrawArgs.fClipConservativeBounds = &scissorRect;
130         canDrawArgs.fViewMatrix = &viewMatrix;
131         canDrawArgs.fShape = &shape;
132         canDrawArgs.fAAType = aaType;
133         SkASSERT(!renderTargetContext->wrapsVkSecondaryCB());
134         canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
135         canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
136 
137         // the 'false' parameter disallows use of the SW path renderer
138         GrPathRenderer* pr =
139             context->priv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
140         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  */
UseSWOnlyPath(GrRecordingContext * context,bool hasUserStencilSettings,const GrRenderTargetContext * renderTargetContext,const GrReducedClip & reducedClip)152 bool GrClipStackClip::UseSWOnlyPath(GrRecordingContext* context,
153                                     bool hasUserStencilSettings,
154                                     const GrRenderTargetContext* renderTargetContext,
155                                     const GrReducedClip& reducedClip) {
156     // 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 
160     // 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 
164     // If we're avoiding stencils, always use SW. This includes drawing into a wrapped vulkan
165     // secondary command buffer which can't handle stencils.
166     if (context->priv().caps()->avoidStencilBuffers() ||
167         renderTargetContext->wrapsVkSecondaryCB()) {
168         return true;
169     }
170 
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()) {
177         const Element* element = iter.get();
178 
179         SkClipOp op = element->getOp();
180         bool invert = element->isInverseFilled();
181         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
apply(GrRecordingContext * context,GrRenderTargetContext * renderTargetContext,bool useHWAA,bool hasUserStencilSettings,GrAppliedClip * out,SkRect * bounds) const195 bool GrClipStackClip::apply(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
196                             bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
197                             SkRect* bounds) const {
198     SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
199     if (!devBounds.intersect(*bounds)) {
200         return false;
201     }
202 
203     if (!fStack || fStack->isWideOpen()) {
204         return true;
205     }
206 
207     // 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 
215     int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
216     int maxAnalyticFPs = kMaxAnalyticFPs;
217     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;
220         // We disable MSAA when avoiding stencil.
221         SkASSERT(!context->priv().caps()->avoidStencilBuffers());
222     }
223     auto* ccpr = context->priv().drawingManager()->getCoverageCountingPathRenderer();
224 
225     GrReducedClip reducedClip(*fStack, devBounds, context->priv().caps(),
226                               maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0);
227     if (InitialState::kAllOut == reducedClip.initialState() &&
228         reducedClip.maskElements().isEmpty()) {
229         return false;
230     }
231 
232     if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
233         out->hardClip().addScissor(reducedClip.scissor(), bounds);
234     }
235 
236     if (!reducedClip.windowRectangles().empty()) {
237         out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
238                                             GrWindowRectsState::Mode::kExclusive);
239     }
240 
241     if (!reducedClip.maskElements().isEmpty()) {
242         if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
243                                  out)) {
244             return false;
245         }
246     }
247 
248     // 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)) {
252         out->addCoverageFP(std::move(clipFPs));
253     }
254 
255     return true;
256 }
257 
applyClipMask(GrRecordingContext * context,GrRenderTargetContext * renderTargetContext,const GrReducedClip & reducedClip,bool hasUserStencilSettings,GrAppliedClip * out) const258 bool GrClipStackClip::applyClipMask(GrRecordingContext* context,
259                                     GrRenderTargetContext* renderTargetContext,
260                                     const GrReducedClip& reducedClip, bool hasUserStencilSettings,
261                                     GrAppliedClip* out) const {
262 #ifdef SK_DEBUG
263     SkASSERT(reducedClip.hasScissor());
264     SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
265                                         renderTargetContext->height());
266     const SkIRect& scissor = reducedClip.scissor();
267     SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
268 #endif
269 
270     // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping.
271     if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
272         context->priv().caps()->avoidStencilBuffers() ||
273         renderTargetContext->wrapsVkSecondaryCB()) {
274         sk_sp<GrTextureProxy> result;
275         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()));
287             return true;
288         }
289 
290         // If alpha or software clip mask creation fails, fall through to the stencil code paths,
291         // unless stencils are disallowed.
292         if (context->priv().caps()->avoidStencilBuffers() ||
293             renderTargetContext->wrapsVkSecondaryCB()) {
294             SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
295                      "Clip will be ignored.\n");
296             return false;
297         }
298     }
299 
300     // 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())) {
305         reducedClip.drawStencilClipMask(context, renderTargetContext);
306         renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor(),
307                                                 reducedClip.numAnalyticFPs());
308     }
309     // 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());
312     return true;
313 }
314 
315 ////////////////////////////////////////////////////////////////////////////////
316 // Create a 8-bit clip mask in alpha
317 
create_clip_mask_key(uint32_t clipGenID,const SkIRect & bounds,int numAnalyticFPs,GrUniqueKey * key)318 static 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 
add_invalidate_on_pop_message(GrRecordingContext * context,const SkClipStack & stack,uint32_t clipGenID,const GrUniqueKey & clipMaskKey)330 static void add_invalidate_on_pop_message(GrRecordingContext* context,
331                                           const SkClipStack& stack, uint32_t clipGenID,
332                                           const GrUniqueKey& clipMaskKey) {
333     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
334 
335     SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
336     while (const Element* element = iter.prev()) {
337         if (element->getGenID() == clipGenID) {
338             element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
339             return;
340         }
341     }
342     SkDEBUGFAIL("Gen ID was not found in stack.");
343 }
344 
createAlphaClipMask(GrRecordingContext * context,const GrReducedClip & reducedClip) const345 sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
346                                                            const GrReducedClip& reducedClip) const {
347     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
348     GrUniqueKey key;
349     create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
350                          reducedClip.numAnalyticFPs(), &key);
351 
352     sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
353             key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin));
354     if (proxy) {
355         return proxy;
356     }
357 
358     auto isProtected = proxy->isProtected() ? GrProtected::kYes : GrProtected::kNo;
359     auto rtc = context->priv().makeDeferredRenderTargetContextWithFallback(SkBackingFit::kApprox,
360                                                                            reducedClip.width(),
361                                                                            reducedClip.height(),
362                                                                            GrColorType::kAlpha_8,
363                                                                            nullptr,
364                                                                            1,
365                                                                            GrMipMapped::kNo,
366                                                                            kTopLeft_GrSurfaceOrigin,
367                                                                            nullptr,
368                                                                            SkBudgeted::kYes,
369                                                                            isProtected);
370     if (!rtc) {
371         return nullptr;
372     }
373 
374     if (!reducedClip.drawAlphaClipMask(rtc.get())) {
375         return nullptr;
376     }
377 
378     sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
379     if (!result) {
380         return nullptr;
381     }
382 
383     SkASSERT(result->origin() == kTopLeft_GrSurfaceOrigin);
384     proxyProvider->assignUniqueKeyToProxy(key, result.get());
385     add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
386 
387     return result;
388 }
389 
390 namespace {
391 
392 /**
393  * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
394  * elements, each storing their own AA setting (and already transformed into device space). This
395  * stores all of the information needed by the worker thread to draw all clip elements (see below,
396  * in createSoftwareClipMask).
397  */
398 class ClipMaskData {
399 public:
ClipMaskData(const GrReducedClip & reducedClip)400     ClipMaskData(const GrReducedClip& reducedClip)
401             : fScissor(reducedClip.scissor())
402             , fInitialState(reducedClip.initialState()) {
403         for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
404             fElements.addToTail(*iter.get());
405         }
406     }
407 
scissor() const408     const SkIRect& scissor() const { return fScissor; }
initialState() const409     InitialState initialState() const { return fInitialState; }
elements() const410     const ElementList& elements() const { return fElements; }
411 
412 private:
413     SkIRect fScissor;
414     InitialState fInitialState;
415     ElementList fElements;
416 };
417 
418 }
419 
draw_clip_elements_to_mask_helper(GrSWMaskHelper & helper,const ElementList & elements,const SkIRect & scissor,InitialState initialState)420 static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
421                                               const SkIRect& scissor, InitialState initialState) {
422     // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
423     SkMatrix translate;
424     translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
425 
426     helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
427 
428     for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
429         const Element* element = iter.get();
430         SkClipOp op = element->getOp();
431         GrAA aa = GrAA(element->isAA());
432 
433         if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
434             // Intersect and reverse difference require modifying pixels outside of the geometry
435             // that is being "drawn". In both cases we erase all the pixels outside of the geometry
436             // but leave the pixels inside the geometry alone. For reverse difference we invert all
437             // the pixels before clearing the ones outside the geometry.
438             if (kReverseDifference_SkClipOp == op) {
439                 SkRect temp = SkRect::Make(scissor);
440                 // invert the entire scene
441                 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
442             }
443             SkPath clipPath;
444             element->asDeviceSpacePath(&clipPath);
445             clipPath.toggleInverseFillType();
446             GrShape shape(clipPath, GrStyle::SimpleFill());
447             helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
448             continue;
449         }
450 
451         // The other ops (union, xor, diff) only affect pixels inside
452         // the geometry so they can just be drawn normally
453         if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
454             helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
455         } else {
456             SkPath path;
457             element->asDeviceSpacePath(&path);
458             GrShape shape(path, GrStyle::SimpleFill());
459             helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
460         }
461     }
462 }
463 
createSoftwareClipMask(GrRecordingContext * context,const GrReducedClip & reducedClip,GrRenderTargetContext * renderTargetContext) const464 sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
465         GrRecordingContext* context, const GrReducedClip& reducedClip,
466         GrRenderTargetContext* renderTargetContext) const {
467     GrUniqueKey key;
468     create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
469                          reducedClip.numAnalyticFPs(), &key);
470 
471     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
472     const GrCaps* caps = context->priv().caps();
473 
474     sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
475             key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin));
476     if (proxy) {
477         return proxy;
478     }
479 
480     // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
481     // left corner of the resulting rect to the top left of the texture.
482     SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
483 
484     SkTaskGroup* taskGroup = nullptr;
485     if (auto direct = context->priv().asDirectContext()) {
486         taskGroup = direct->priv().getTaskGroup();
487     }
488 
489     if (taskGroup && renderTargetContext) {
490         // Create our texture proxy
491         GrSurfaceDesc desc;
492         desc.fWidth = maskSpaceIBounds.width();
493         desc.fHeight = maskSpaceIBounds.height();
494         desc.fConfig = kAlpha_8_GrPixelConfig;
495 
496         GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
497                                                                GrRenderable::kNo);
498 
499         // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
500         // to ops), so it can't have any pending IO.
501         proxy = proxyProvider->createProxy(format,
502                                            desc,
503                                            GrRenderable::kNo,
504                                            1,
505                                            kTopLeft_GrSurfaceOrigin,
506                                            GrMipMapped::kNo,
507                                            SkBackingFit::kApprox,
508                                            SkBudgeted::kYes,
509                                            GrProtected::kNo);
510 
511         auto uploader = skstd::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
512         GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
513         auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
514             TRACE_EVENT0("skia.gpu", "Threaded SW Clip Mask Render");
515             GrSWMaskHelper helper(uploaderRaw->getPixels());
516             if (helper.init(maskSpaceIBounds)) {
517                 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
518                                                   uploaderRaw->data().scissor(),
519                                                   uploaderRaw->data().initialState());
520             } else {
521                 SkDEBUGFAIL("Unable to allocate SW clip mask.");
522             }
523             uploaderRaw->signalAndFreeData();
524         };
525 
526         taskGroup->add(std::move(drawAndUploadMask));
527         proxy->texPriv().setDeferredUploader(std::move(uploader));
528     } else {
529         GrSWMaskHelper helper;
530         if (!helper.init(maskSpaceIBounds)) {
531             return nullptr;
532         }
533 
534         draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
535                                           reducedClip.initialState());
536 
537         proxy = helper.toTextureProxy(context, SkBackingFit::kApprox);
538     }
539 
540     SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
541     proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
542     add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
543     return proxy;
544 }
545