blob: 8c3a0f2f1dc162df01bd875a6428a51fc56d389b [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"
29#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomon64227222020-02-26 13:28:42 -050030#include "src/gpu/effects/generated/GrDeviceSpaceEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040031#include "src/gpu/geometry/GrShape.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000032
Brian Salomonc3833b42018-07-09 18:23:58 +000033typedef SkClipStack::Element Element;
34typedef GrReducedClip::InitialState InitialState;
35typedef GrReducedClip::ElementList ElementList;
36
37const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070038
csmartdaltonc6f411e2016-08-05 22:32:12 -070039bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070040 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070041 return true;
42 }
Brian Salomon9a767722017-03-13 17:57:28 -040043 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070044}
45
bsalomon7f0d9f32016-08-15 14:49:10 -070046bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070047 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070048 return true;
49 }
Brian Salomon9a767722017-03-13 17:57:28 -040050 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070051}
52
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050053bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070054 if (!fStack) {
55 return false;
56 }
57 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050058 bool isAA;
59 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
Chris Dalton3b51df12017-11-27 14:33:06 -070060 *aa = GrAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070061 return true;
62 }
63 return false;
64}
65
csmartdaltonc6f411e2016-08-05 22:32:12 -070066void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
67 bool* isIntersectionOfRects) const {
68 if (!fStack) {
69 devResult->setXYWH(0, 0, width, height);
70 if (isIntersectionOfRects) {
71 *isIntersectionOfRects = true;
72 }
73 return;
74 }
75 SkRect devBounds;
Brian Salomon9a767722017-03-13 17:57:28 -040076 fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
csmartdaltonc6f411e2016-08-05 22:32:12 -070077 devBounds.roundOut(devResult);
78}
79
Chris Daltonc5348082018-03-30 15:59:38 +000080////////////////////////////////////////////////////////////////////////////////
81// set up the draw state to enable the aa clipping mask.
Greg Daniele32506b2020-02-10 16:00:54 -050082static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(GrSurfaceProxyView mask,
Brian Salomon64227222020-02-26 13:28:42 -050083 const SkIRect& devBound,
84 const GrCaps& caps) {
85 GrSamplerState samplerState(GrSamplerState::WrapMode::kClampToBorder,
86 GrSamplerState::Filter::kNearest);
87 auto m = SkMatrix::MakeTrans(-devBound.fLeft, -devBound.fTop);
88 auto subset = SkRect::Make(devBound.size());
89 // We scissor to devBounds. The mask's texel centers are aligned to device space
90 // pixel centers. Hence this domain of texture coordinates.
91 auto domain = subset.makeInset(0.5, 0.5);
92 auto fp = GrTextureEffect::MakeSubset(std::move(mask), kPremul_SkAlphaType, m, samplerState,
93 subset, domain, caps);
94 return GrDeviceSpaceEffect::Make(std::move(fp));
Chris Daltonc5348082018-03-30 15:59:38 +000095}
96
97// Does the path in 'element' require SW rendering? If so, return true (and,
98// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
99// 'prOut' to the non-SW path renderer that will do the job).
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500100bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000101 const SkIRect& scissorRect,
102 bool hasUserStencilSettings,
103 const GrRenderTargetContext* renderTargetContext,
104 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +0000105 const Element* element,
Chris Daltonc5348082018-03-30 15:59:38 +0000106 GrPathRenderer** prOut,
107 bool needsStencil) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000108 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000109 // rects can always be drawn directly w/o using the software path
110 // TODO: skip rrects once we're drawing them directly.
111 if (prOut) {
112 *prOut = nullptr;
113 }
114 return false;
115 } else {
116 // We shouldn't get here with an empty clip element.
Brian Salomonc3833b42018-07-09 18:23:58 +0000117 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
Chris Daltonc5348082018-03-30 15:59:38 +0000118
119 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
120 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000121 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000122 if (path.isInverseFillType()) {
123 path.toggleInverseFillType();
124 }
125
Chris Dalton09e56892019-03-13 00:22:01 -0600126 // We only use this method when rendering coverage clip masks.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600127 SkASSERT(renderTargetContext->numSamples() <= 1);
128 auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600129
Chris Daltonc5348082018-03-30 15:59:38 +0000130 GrPathRendererChain::DrawType type =
131 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
132 : GrPathRendererChain::DrawType::kColor;
133
134 GrShape shape(path, GrStyle::SimpleFill());
135 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -0500136 canDrawArgs.fCaps = context->priv().caps();
Greg Daniel46e366a2019-12-16 14:38:36 -0500137 canDrawArgs.fProxy = renderTargetContext->asRenderTargetProxy();
Chris Daltonc5348082018-03-30 15:59:38 +0000138 canDrawArgs.fClipConservativeBounds = &scissorRect;
139 canDrawArgs.fViewMatrix = &viewMatrix;
140 canDrawArgs.fShape = &shape;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600141 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -0500142 SkASSERT(!renderTargetContext->wrapsVkSecondaryCB());
143 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
Chris Daltonc5348082018-03-30 15:59:38 +0000144 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
145
146 // the 'false' parameter disallows use of the SW path renderer
147 GrPathRenderer* pr =
Robert Phillips9da87e02019-02-04 13:26:26 -0500148 context->priv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
Chris Daltonc5348082018-03-30 15:59:38 +0000149 if (prOut) {
150 *prOut = pr;
151 }
152 return SkToBool(!pr);
153 }
154}
155
156/*
157 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
158 * will be used on any element. If so, it returns true to indicate that the
159 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
160 */
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500161bool GrClipStackClip::UseSWOnlyPath(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000162 bool hasUserStencilSettings,
163 const GrRenderTargetContext* renderTargetContext,
164 const GrReducedClip& reducedClip) {
Chris Daltond22a0232018-08-22 17:25:10 +0000165 // TODO: right now it appears that GPU clip masks are strictly slower than software. We may
166 // want to revisit this assumption once we can test with render target sorting.
167 return true;
168
Chris Daltonc5348082018-03-30 15:59:38 +0000169 // TODO: generalize this function so that when
170 // a clip gets complex enough it can just be done in SW regardless
171 // of whether it would invoke the GrSoftwarePathRenderer.
172
Greg Danielbe7fc462019-01-03 16:40:42 -0500173 // If we're avoiding stencils, always use SW. This includes drawing into a wrapped vulkan
174 // secondary command buffer which can't handle stencils.
Robert Phillips9da87e02019-02-04 13:26:26 -0500175 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500176 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000177 return true;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400178 }
Chris Daltonc5348082018-03-30 15:59:38 +0000179
180 // Set the matrix so that rendered clip elements are transformed to mask space from clip
181 // space.
182 SkMatrix translate;
183 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
184
185 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000186 const Element* element = iter.get();
Chris Daltonc5348082018-03-30 15:59:38 +0000187
Brian Salomonc3833b42018-07-09 18:23:58 +0000188 SkClipOp op = element->getOp();
189 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000190 bool needsStencil = invert ||
191 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
192
193 if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings,
194 renderTargetContext, translate, element, nullptr, needsStencil)) {
195 return true;
196 }
197 }
198 return false;
199}
200
201////////////////////////////////////////////////////////////////////////////////
202// sort out what kind of clip mask needs to be created: alpha, stencil,
203// scissor, or entirely software
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500204bool GrClipStackClip::apply(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400205 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
206 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400207 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
208 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700209 return false;
210 }
211
Brian Salomon510dd422017-03-16 12:15:22 -0400212 if (!fStack || fStack->isWideOpen()) {
213 return true;
214 }
215
Brian Osman5f5680c2019-06-05 10:17:25 -0400216 // An default count of 4 was chosen because of the common pattern in Blink of:
217 // isect RR
218 // diff RR
219 // isect convex_poly
220 // isect convex_poly
221 // when drawing rounded div borders.
222 constexpr int kMaxAnalyticFPs = 4;
223
Chris Daltona32a3c32017-12-05 10:05:21 -0700224 int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
Brian Osman5f5680c2019-06-05 10:17:25 -0400225 int maxAnalyticFPs = kMaxAnalyticFPs;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600226 if (renderTargetContext->numSamples() > 1 || useHWAA || hasUserStencilSettings) {
227 // Disable analytic clips when we have MSAA. In MSAA we never conflate coverage and opacity.
228 maxAnalyticFPs = 0;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400229 // We disable MSAA when avoiding stencil.
Robert Phillips9da87e02019-02-04 13:26:26 -0500230 SkASSERT(!context->priv().caps()->avoidStencilBuffers());
Chris Dalton584a79a2017-11-15 13:14:01 -0700231 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500232 auto* ccpr = context->priv().drawingManager()->getCoverageCountingPathRenderer();
Chris Dalton584a79a2017-11-15 13:14:01 -0700233
Robert Phillips9da87e02019-02-04 13:26:26 -0500234 GrReducedClip reducedClip(*fStack, devBounds, context->priv().caps(),
Ethan Nicholaseace9352018-10-15 20:09:54 +0000235 maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0);
Chris Daltona32a3c32017-12-05 10:05:21 -0700236 if (InitialState::kAllOut == reducedClip.initialState() &&
237 reducedClip.maskElements().isEmpty()) {
238 return false;
239 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000240
Chris Dalton79471932017-10-27 01:50:57 -0600241 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700242 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700243 }
cdalton93a379b2016-05-11 13:58:08 -0700244
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700245 if (!reducedClip.windowRectangles().empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700246 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
247 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700248 }
249
Chris Daltona32a3c32017-12-05 10:05:21 -0700250 if (!reducedClip.maskElements().isEmpty()) {
251 if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
252 out)) {
253 return false;
254 }
255 }
256
Greg Danielf41b2bd2019-08-22 16:19:24 -0400257 // The opsTask ID must not be looked up until AFTER producing the clip mask (if any). That step
258 // can cause a flush or otherwise change which opstask our draw is going into.
259 uint32_t opsTaskID = renderTargetContext->getOpsTask()->uniqueID();
260 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(ccpr, opsTaskID)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700261 out->addCoverageFP(std::move(clipFPs));
262 }
263
Chris Daltona32a3c32017-12-05 10:05:21 -0700264 return true;
265}
csmartdaltond211e782016-08-15 11:17:19 -0700266
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500267bool GrClipStackClip::applyClipMask(GrRecordingContext* context,
268 GrRenderTargetContext* renderTargetContext,
Chris Daltona32a3c32017-12-05 10:05:21 -0700269 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
270 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700271#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600272 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500273 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
274 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600275 const SkIRect& scissor = reducedClip.scissor();
276 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700277#endif
csmartdaltond211e782016-08-15 11:17:19 -0700278
Chris Dalton6ce447a2019-06-23 18:07:38 -0600279 // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping.
280 if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500281 context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500282 renderTargetContext->wrapsVkSecondaryCB()) {
Greg Daniele32506b2020-02-10 16:00:54 -0500283 GrSurfaceProxyView result;
Chris Daltonc5348082018-03-30 15:59:38 +0000284 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
285 // The clip geometry is complex enough that it will be more efficient to create it
286 // entirely in software
287 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
288 } else {
289 result = this->createAlphaClipMask(context, reducedClip);
290 }
291
292 if (result) {
293 // The mask's top left coord should be pinned to the rounded-out top left corner of
294 // the clip's device space bounds.
Brian Salomon64227222020-02-26 13:28:42 -0500295 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor(),
296 *context->priv().caps()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000297 return true;
298 }
Eric Karl5c779752017-05-08 12:02:07 -0700299
Chris Daltonc5348082018-03-30 15:59:38 +0000300 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
301 // unless stencils are disallowed.
Robert Phillips9da87e02019-02-04 13:26:26 -0500302 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500303 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700304 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
305 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700306 return false;
307 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000308 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000309
Brian Salomonc3833b42018-07-09 18:23:58 +0000310 // This relies on the property that a reduced sub-rect of the last clip will contain all the
311 // relevant window rectangles that were in the last clip. This subtle requirement will go away
312 // after clipping is overhauled.
313 if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(), reducedClip.scissor(),
314 reducedClip.numAnalyticFPs())) {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000315 reducedClip.drawStencilClipMask(context, renderTargetContext);
Brian Salomonc3833b42018-07-09 18:23:58 +0000316 renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor(),
317 reducedClip.numAnalyticFPs());
csmartdaltonbde96c62016-08-31 12:54:46 -0700318 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000319 // GrAppliedClip doesn't need to figure numAnalyticFPs into its key (used by operator==) because
320 // it verifies the FPs are also equal.
321 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000322 return true;
323}
324
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000325////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700326// Create a 8-bit clip mask in alpha
327
Brian Salomonc3833b42018-07-09 18:23:58 +0000328static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
329 GrUniqueKey* key) {
330 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
331 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
332 builder[0] = clipGenID;
333 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
334 // sometimes result in negative coordinates from device space.
335 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
336 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
337 builder[3] = numAnalyticFPs;
338}
339
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500340static void add_invalidate_on_pop_message(GrRecordingContext* context,
Robert Phillips427966a2018-12-20 17:20:43 -0500341 const SkClipStack& stack, uint32_t clipGenID,
342 const GrUniqueKey& clipMaskKey) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500343 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips427966a2018-12-20 17:20:43 -0500344
Brian Salomon19f0ed52017-01-06 13:54:58 -0500345 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
346 while (const Element* element = iter.prev()) {
347 if (element->getGenID() == clipGenID) {
Robert Phillips427966a2018-12-20 17:20:43 -0500348 element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500349 return;
350 }
351 }
352 SkDEBUGFAIL("Gen ID was not found in stack.");
353}
354
Greg Daniele32506b2020-02-10 16:00:54 -0500355GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
356 const GrReducedClip& reducedClip) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500357 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc3833b42018-07-09 18:23:58 +0000358 GrUniqueKey key;
359 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
360 reducedClip.numAnalyticFPs(), &key);
361
Brian Salomone8d20802020-04-01 09:10:38 -0400362 if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
363 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
364 GrColorType::kAlpha_8);
365 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Chris Daltonc5348082018-03-30 15:59:38 +0000366 }
367
Greg Daniele20fcad2020-01-08 11:52:34 -0500368 auto rtc = GrRenderTargetContext::MakeWithFallback(
369 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox,
Greg Daniele32506b2020-02-10 16:00:54 -0500370 {reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomone8d20802020-04-01 09:10:38 -0400371 kTopLeft_GrSurfaceOrigin);
Chris Daltonc5348082018-03-30 15:59:38 +0000372 if (!rtc) {
Greg Daniele32506b2020-02-10 16:00:54 -0500373 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000374 }
375
376 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
Greg Daniele32506b2020-02-10 16:00:54 -0500377 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000378 }
379
Greg Daniele32506b2020-02-10 16:00:54 -0500380 GrSurfaceProxyView result = rtc->readSurfaceView();
381 if (!result || !result.asTextureProxy()) {
382 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000383 }
384
Brian Salomone8d20802020-04-01 09:10:38 -0400385 SkASSERT(result.origin() == kTopLeft_GrSurfaceOrigin);
Greg Daniele32506b2020-02-10 16:00:54 -0500386 proxyProvider->assignUniqueKeyToProxy(key, result.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500387 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Chris Daltonc5348082018-03-30 15:59:38 +0000388
389 return result;
390}
391
Brian Osman5d034742017-09-11 13:38:55 -0400392namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500393
Brian Osman5d034742017-09-11 13:38:55 -0400394/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400395 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400396 * elements, each storing their own AA setting (and already transformed into device space). This
397 * stores all of the information needed by the worker thread to draw all clip elements (see below,
398 * in createSoftwareClipMask).
399 */
400class ClipMaskData {
401public:
402 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600403 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400404 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600405 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400406 fElements.addToTail(*iter.get());
407 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000408 }
409
Chris Dalton79471932017-10-27 01:50:57 -0600410 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400411 InitialState initialState() const { return fInitialState; }
412 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000413
Brian Osman5d034742017-09-11 13:38:55 -0400414private:
Chris Dalton79471932017-10-27 01:50:57 -0600415 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400416 InitialState fInitialState;
417 ElementList fElements;
418};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000419
Brian Osman5d034742017-09-11 13:38:55 -0400420}
421
422static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600423 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400424 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800425 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600426 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800427
Brian Osman5d034742017-09-11 13:38:55 -0400428 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000429
Brian Osman5d034742017-09-11 13:38:55 -0400430 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000431 const Element* element = iter.get();
432 SkClipOp op = element->getOp();
433 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000434
Mike Reedc1f77742016-12-09 09:00:50 -0500435 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000436 // Intersect and reverse difference require modifying pixels outside of the geometry
437 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
438 // but leave the pixels inside the geometry alone. For reverse difference we invert all
439 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500440 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600441 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000442 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400443 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000444 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000445 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000446 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000447 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700448 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400449 helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000450 continue;
451 }
452
453 // The other ops (union, xor, diff) only affect pixels inside
454 // the geometry so they can just be drawn normally
Brian Salomonc3833b42018-07-09 18:23:58 +0000455 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
456 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000457 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000458 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000459 element->asDeviceSpacePath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700460 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400461 helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000462 }
463 }
Brian Osman5d034742017-09-11 13:38:55 -0400464}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000465
Greg Daniele32506b2020-02-10 16:00:54 -0500466GrSurfaceProxyView GrClipStackClip::createSoftwareClipMask(
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500467 GrRecordingContext* context, const GrReducedClip& reducedClip,
Brian Osman5d034742017-09-11 13:38:55 -0400468 GrRenderTargetContext* renderTargetContext) const {
Brian Salomonc3833b42018-07-09 18:23:58 +0000469 GrUniqueKey key;
470 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
471 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800472
Robert Phillips9da87e02019-02-04 13:26:26 -0500473 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8d20802020-04-01 09:10:38 -0400474 const GrCaps* caps = context->priv().caps();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500475
Brian Salomone8d20802020-04-01 09:10:38 -0400476 if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
477 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
478 GrColorType::kAlpha_8);
479 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Brian Osman5d034742017-09-11 13:38:55 -0400480 }
481
482 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
483 // left corner of the resulting rect to the top left of the texture.
484 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
485
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500486 SkTaskGroup* taskGroup = nullptr;
487 if (auto direct = context->priv().asDirectContext()) {
488 taskGroup = direct->priv().getTaskGroup();
489 }
490
Greg Daniele32506b2020-02-10 16:00:54 -0500491 GrSurfaceProxyView view;
Brian Osman5d034742017-09-11 13:38:55 -0400492 if (taskGroup && renderTargetContext) {
493 // Create our texture proxy
Robert Phillips0a15cc62019-07-30 12:49:10 -0400494 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
495 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500496
Greg Daniel47c20e82020-01-21 14:29:57 -0500497 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kAlpha_8);
498
Brian Osmand140fe92017-10-03 12:17:26 -0400499 // 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.
Greg Daniele32506b2020-02-10 16:00:54 -0500501 auto proxy = proxyProvider->createProxy(format,
502 maskSpaceIBounds.size(),
Greg Daniele32506b2020-02-10 16:00:54 -0500503 GrRenderable::kNo,
504 1,
Greg Daniele32506b2020-02-10 16:00:54 -0500505 GrMipMapped::kNo,
506 SkBackingFit::kApprox,
507 SkBudgeted::kYes,
508 GrProtected::kNo);
Brian Osman5d034742017-09-11 13:38:55 -0400509
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500510 auto uploader = std::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
Brian Osman099fa0f2017-10-02 16:38:32 -0400511 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400512 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
Brian Salomon5f394272019-07-02 14:07:49 -0400513 TRACE_EVENT0("skia.gpu", "Threaded SW Clip Mask Render");
Brian Osman5d034742017-09-11 13:38:55 -0400514 GrSWMaskHelper helper(uploaderRaw->getPixels());
515 if (helper.init(maskSpaceIBounds)) {
516 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
Chris Dalton79471932017-10-27 01:50:57 -0600517 uploaderRaw->data().scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400518 uploaderRaw->data().initialState());
519 } else {
520 SkDEBUGFAIL("Unable to allocate SW clip mask.");
521 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400522 uploaderRaw->signalAndFreeData();
Brian Osman5d034742017-09-11 13:38:55 -0400523 };
524
525 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400526 proxy->texPriv().setDeferredUploader(std::move(uploader));
Greg Daniele32506b2020-02-10 16:00:54 -0500527
Brian Salomone8d20802020-04-01 09:10:38 -0400528 view = {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Brian Osman5d034742017-09-11 13:38:55 -0400529 } else {
530 GrSWMaskHelper helper;
531 if (!helper.init(maskSpaceIBounds)) {
Greg Daniele32506b2020-02-10 16:00:54 -0500532 return {};
Brian Osman5d034742017-09-11 13:38:55 -0400533 }
534
Chris Dalton79471932017-10-27 01:50:57 -0600535 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400536 reducedClip.initialState());
537
Greg Daniele32506b2020-02-10 16:00:54 -0500538 view = helper.toTextureView(context, SkBackingFit::kApprox);
Brian Osman5d034742017-09-11 13:38:55 -0400539 }
540
Greg Daniele32506b2020-02-10 16:00:54 -0500541 SkASSERT(view);
Brian Salomone8d20802020-04-01 09:10:38 -0400542 SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
Greg Daniele32506b2020-02-10 16:00:54 -0500543 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500544 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Greg Daniele32506b2020-02-10 16:00:54 -0500545 return view;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546}