blob: 21cdbaeb8b032f12ccba3ff7f4dbfff36238b9c5 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
csmartdaltonc6f411e2016-08-05 22:32:12 -07002 * Copyright 2016 Google Inc.
robertphillips@google.com1e945b72012-04-16 18:03:03 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomon64227222020-02-26 13:28:42 -05008#include "src/gpu/GrClipStackClip.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/SkTo.h"
11#include "src/core/SkClipOpPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkTaskGroup.h"
13#include "src/core/SkTraceEvent.h"
14#include "src/gpu/GrAppliedClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrDeferredProxyUploader.h"
17#include "src/gpu/GrDrawingManager.h"
18#include "src/gpu/GrFixedClip.h"
19#include "src/gpu/GrGpuResourcePriv.h"
20#include "src/gpu/GrProxyProvider.h"
21#include "src/gpu/GrRecordingContextPriv.h"
22#include "src/gpu/GrRenderTargetContextPriv.h"
23#include "src/gpu/GrSWMaskHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrStencilAttachment.h"
25#include "src/gpu/GrStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040026#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/effects/GrConvexPolyEffect.h"
28#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon64227222020-02-26 13:28:42 -050029#include "src/gpu/effects/generated/GrDeviceSpaceEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000030#include "src/gpu/geometry/GrStyledShape.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000031
Brian Salomonc3833b42018-07-09 18:23:58 +000032typedef SkClipStack::Element Element;
33typedef GrReducedClip::InitialState InitialState;
34typedef GrReducedClip::ElementList ElementList;
35
36const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070037
csmartdaltonc6f411e2016-08-05 22:32:12 -070038bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070039 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070040 return true;
41 }
Brian Salomon9a767722017-03-13 17:57:28 -040042 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070043}
44
bsalomon7f0d9f32016-08-15 14:49:10 -070045bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070046 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070047 return true;
48 }
Brian Salomon9a767722017-03-13 17:57:28 -040049 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070050}
51
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050052bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070053 if (!fStack) {
54 return false;
55 }
56 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050057 bool isAA;
58 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
Chris Dalton3b51df12017-11-27 14:33:06 -070059 *aa = GrAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070060 return true;
61 }
62 return false;
63}
64
csmartdaltonc6f411e2016-08-05 22:32:12 -070065void 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;
Brian Salomon9a767722017-03-13 17:57:28 -040075 fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
csmartdaltonc6f411e2016-08-05 22:32:12 -070076 devBounds.roundOut(devResult);
77}
78
Chris Daltonc5348082018-03-30 15:59:38 +000079////////////////////////////////////////////////////////////////////////////////
80// set up the draw state to enable the aa clipping mask.
Greg Daniele32506b2020-02-10 16:00:54 -050081static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(GrSurfaceProxyView mask,
Brian Salomon64227222020-02-26 13:28:42 -050082 const SkIRect& devBound,
83 const GrCaps& caps) {
84 GrSamplerState samplerState(GrSamplerState::WrapMode::kClampToBorder,
85 GrSamplerState::Filter::kNearest);
86 auto m = SkMatrix::MakeTrans(-devBound.fLeft, -devBound.fTop);
87 auto subset = SkRect::Make(devBound.size());
88 // We scissor to devBounds. The mask's texel centers are aligned to device space
89 // pixel centers. Hence this domain of texture coordinates.
90 auto domain = subset.makeInset(0.5, 0.5);
91 auto fp = GrTextureEffect::MakeSubset(std::move(mask), kPremul_SkAlphaType, m, samplerState,
92 subset, domain, caps);
93 return GrDeviceSpaceEffect::Make(std::move(fp));
Chris Daltonc5348082018-03-30 15:59:38 +000094}
95
96// Does the path in 'element' require SW rendering? If so, return true (and,
97// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
98// 'prOut' to the non-SW path renderer that will do the job).
Robert Phillips6f0e02f2019-02-13 11:02:28 -050099bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000100 const SkIRect& scissorRect,
101 bool hasUserStencilSettings,
102 const GrRenderTargetContext* renderTargetContext,
103 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +0000104 const Element* element,
Chris Daltonc5348082018-03-30 15:59:38 +0000105 GrPathRenderer** prOut,
106 bool needsStencil) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000107 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000108 // rects can always be drawn directly w/o using the software path
109 // TODO: skip rrects once we're drawing them directly.
110 if (prOut) {
111 *prOut = nullptr;
112 }
113 return false;
114 } else {
115 // We shouldn't get here with an empty clip element.
Brian Salomonc3833b42018-07-09 18:23:58 +0000116 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
Chris Daltonc5348082018-03-30 15:59:38 +0000117
118 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
119 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000120 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000121 if (path.isInverseFillType()) {
122 path.toggleInverseFillType();
123 }
124
Chris Dalton09e56892019-03-13 00:22:01 -0600125 // We only use this method when rendering coverage clip masks.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600126 SkASSERT(renderTargetContext->numSamples() <= 1);
127 auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600128
Chris Daltonc5348082018-03-30 15:59:38 +0000129 GrPathRendererChain::DrawType type =
130 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
131 : GrPathRendererChain::DrawType::kColor;
132
Michael Ludwig2686d692020-04-17 20:21:37 +0000133 GrStyledShape shape(path, GrStyle::SimpleFill());
Chris Daltonc5348082018-03-30 15:59:38 +0000134 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -0500135 canDrawArgs.fCaps = context->priv().caps();
Greg Daniel46e366a2019-12-16 14:38:36 -0500136 canDrawArgs.fProxy = renderTargetContext->asRenderTargetProxy();
Chris Daltonc5348082018-03-30 15:59:38 +0000137 canDrawArgs.fClipConservativeBounds = &scissorRect;
138 canDrawArgs.fViewMatrix = &viewMatrix;
139 canDrawArgs.fShape = &shape;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600140 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -0500141 SkASSERT(!renderTargetContext->wrapsVkSecondaryCB());
142 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
Chris Daltonc5348082018-03-30 15:59:38 +0000143 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
144
145 // the 'false' parameter disallows use of the SW path renderer
146 GrPathRenderer* pr =
Robert Phillips9da87e02019-02-04 13:26:26 -0500147 context->priv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
Chris Daltonc5348082018-03-30 15:59:38 +0000148 if (prOut) {
149 *prOut = pr;
150 }
151 return SkToBool(!pr);
152 }
153}
154
155/*
156 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
157 * will be used on any element. If so, it returns true to indicate that the
158 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
159 */
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500160bool GrClipStackClip::UseSWOnlyPath(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000161 bool hasUserStencilSettings,
162 const GrRenderTargetContext* renderTargetContext,
163 const GrReducedClip& reducedClip) {
Chris Daltond22a0232018-08-22 17:25:10 +0000164 // TODO: right now it appears that GPU clip masks are strictly slower than software. We may
165 // want to revisit this assumption once we can test with render target sorting.
166 return true;
167
Chris Daltonc5348082018-03-30 15:59:38 +0000168 // TODO: generalize this function so that when
169 // a clip gets complex enough it can just be done in SW regardless
170 // of whether it would invoke the GrSoftwarePathRenderer.
171
Greg Danielbe7fc462019-01-03 16:40:42 -0500172 // If we're avoiding stencils, always use SW. This includes drawing into a wrapped vulkan
173 // secondary command buffer which can't handle stencils.
Robert Phillips9da87e02019-02-04 13:26:26 -0500174 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500175 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000176 return true;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400177 }
Chris Daltonc5348082018-03-30 15:59:38 +0000178
179 // Set the matrix so that rendered clip elements are transformed to mask space from clip
180 // space.
181 SkMatrix translate;
182 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
183
184 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000185 const Element* element = iter.get();
Chris Daltonc5348082018-03-30 15:59:38 +0000186
Brian Salomonc3833b42018-07-09 18:23:58 +0000187 SkClipOp op = element->getOp();
188 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000189 bool needsStencil = invert ||
190 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
191
192 if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings,
193 renderTargetContext, translate, element, nullptr, needsStencil)) {
194 return true;
195 }
196 }
197 return false;
198}
199
200////////////////////////////////////////////////////////////////////////////////
201// sort out what kind of clip mask needs to be created: alpha, stencil,
202// scissor, or entirely software
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500203bool GrClipStackClip::apply(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400204 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
205 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400206 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
207 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700208 return false;
209 }
210
Brian Salomon510dd422017-03-16 12:15:22 -0400211 if (!fStack || fStack->isWideOpen()) {
212 return true;
213 }
214
Brian Osman5f5680c2019-06-05 10:17:25 -0400215 // An default count of 4 was chosen because of the common pattern in Blink of:
216 // isect RR
217 // diff RR
218 // isect convex_poly
219 // isect convex_poly
220 // when drawing rounded div borders.
221 constexpr int kMaxAnalyticFPs = 4;
222
Chris Daltona32a3c32017-12-05 10:05:21 -0700223 int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
Brian Osman5f5680c2019-06-05 10:17:25 -0400224 int maxAnalyticFPs = kMaxAnalyticFPs;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600225 if (renderTargetContext->numSamples() > 1 || useHWAA || hasUserStencilSettings) {
226 // Disable analytic clips when we have MSAA. In MSAA we never conflate coverage and opacity.
227 maxAnalyticFPs = 0;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400228 // We disable MSAA when avoiding stencil.
Robert Phillips9da87e02019-02-04 13:26:26 -0500229 SkASSERT(!context->priv().caps()->avoidStencilBuffers());
Chris Dalton584a79a2017-11-15 13:14:01 -0700230 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500231 auto* ccpr = context->priv().drawingManager()->getCoverageCountingPathRenderer();
Chris Dalton584a79a2017-11-15 13:14:01 -0700232
Robert Phillips9da87e02019-02-04 13:26:26 -0500233 GrReducedClip reducedClip(*fStack, devBounds, context->priv().caps(),
Ethan Nicholaseace9352018-10-15 20:09:54 +0000234 maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0);
Chris Daltona32a3c32017-12-05 10:05:21 -0700235 if (InitialState::kAllOut == reducedClip.initialState() &&
236 reducedClip.maskElements().isEmpty()) {
237 return false;
238 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000239
Chris Dalton79471932017-10-27 01:50:57 -0600240 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700241 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700242 }
cdalton93a379b2016-05-11 13:58:08 -0700243
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700244 if (!reducedClip.windowRectangles().empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700245 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
246 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700247 }
248
Chris Daltona32a3c32017-12-05 10:05:21 -0700249 if (!reducedClip.maskElements().isEmpty()) {
250 if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
251 out)) {
252 return false;
253 }
254 }
255
Greg Danielf41b2bd2019-08-22 16:19:24 -0400256 // The opsTask ID must not be looked up until AFTER producing the clip mask (if any). That step
257 // can cause a flush or otherwise change which opstask our draw is going into.
258 uint32_t opsTaskID = renderTargetContext->getOpsTask()->uniqueID();
259 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(ccpr, opsTaskID)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700260 out->addCoverageFP(std::move(clipFPs));
261 }
262
Chris Daltona32a3c32017-12-05 10:05:21 -0700263 return true;
264}
csmartdaltond211e782016-08-15 11:17:19 -0700265
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500266bool GrClipStackClip::applyClipMask(GrRecordingContext* context,
267 GrRenderTargetContext* renderTargetContext,
Chris Daltona32a3c32017-12-05 10:05:21 -0700268 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
269 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700270#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600271 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500272 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
273 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600274 const SkIRect& scissor = reducedClip.scissor();
275 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700276#endif
csmartdaltond211e782016-08-15 11:17:19 -0700277
Chris Dalton6ce447a2019-06-23 18:07:38 -0600278 // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping.
279 if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500280 context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500281 renderTargetContext->wrapsVkSecondaryCB()) {
Greg Daniele32506b2020-02-10 16:00:54 -0500282 GrSurfaceProxyView result;
Chris Daltonc5348082018-03-30 15:59:38 +0000283 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
284 // The clip geometry is complex enough that it will be more efficient to create it
285 // entirely in software
286 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
287 } else {
288 result = this->createAlphaClipMask(context, reducedClip);
289 }
290
291 if (result) {
292 // The mask's top left coord should be pinned to the rounded-out top left corner of
293 // the clip's device space bounds.
Brian Salomon64227222020-02-26 13:28:42 -0500294 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor(),
295 *context->priv().caps()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000296 return true;
297 }
Eric Karl5c779752017-05-08 12:02:07 -0700298
Chris Daltonc5348082018-03-30 15:59:38 +0000299 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
300 // unless stencils are disallowed.
Robert Phillips9da87e02019-02-04 13:26:26 -0500301 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500302 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700303 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
304 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700305 return false;
306 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000307 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000308
Brian Salomonc3833b42018-07-09 18:23:58 +0000309 // This relies on the property that a reduced sub-rect of the last clip will contain all the
310 // relevant window rectangles that were in the last clip. This subtle requirement will go away
311 // after clipping is overhauled.
312 if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(), reducedClip.scissor(),
313 reducedClip.numAnalyticFPs())) {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000314 reducedClip.drawStencilClipMask(context, renderTargetContext);
Brian Salomonc3833b42018-07-09 18:23:58 +0000315 renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor(),
316 reducedClip.numAnalyticFPs());
csmartdaltonbde96c62016-08-31 12:54:46 -0700317 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000318 // GrAppliedClip doesn't need to figure numAnalyticFPs into its key (used by operator==) because
319 // it verifies the FPs are also equal.
320 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000321 return true;
322}
323
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000324////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700325// Create a 8-bit clip mask in alpha
326
Brian Salomonc3833b42018-07-09 18:23:58 +0000327static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
328 GrUniqueKey* key) {
329 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
330 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
331 builder[0] = clipGenID;
332 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
333 // sometimes result in negative coordinates from device space.
334 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
335 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
336 builder[3] = numAnalyticFPs;
337}
338
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500339static void add_invalidate_on_pop_message(GrRecordingContext* context,
Robert Phillips427966a2018-12-20 17:20:43 -0500340 const SkClipStack& stack, uint32_t clipGenID,
341 const GrUniqueKey& clipMaskKey) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500342 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips427966a2018-12-20 17:20:43 -0500343
Brian Salomon19f0ed52017-01-06 13:54:58 -0500344 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
345 while (const Element* element = iter.prev()) {
346 if (element->getGenID() == clipGenID) {
Robert Phillips427966a2018-12-20 17:20:43 -0500347 element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500348 return;
349 }
350 }
351 SkDEBUGFAIL("Gen ID was not found in stack.");
352}
353
Brian Salomond005b692020-04-01 15:47:05 -0400354static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
355
356static GrSurfaceProxyView find_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
Brian Salomon0029db02020-04-03 10:41:24 -0400357 return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8,
358 1);
Brian Salomond005b692020-04-01 15:47:05 -0400359}
360
Greg Daniele32506b2020-02-10 16:00:54 -0500361GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
362 const GrReducedClip& reducedClip) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500363 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc3833b42018-07-09 18:23:58 +0000364 GrUniqueKey key;
365 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
366 reducedClip.numAnalyticFPs(), &key);
367
Brian Salomond005b692020-04-01 15:47:05 -0400368 if (auto cachedView = find_mask(context->priv().proxyProvider(), key)) {
369 return cachedView;
Chris Daltonc5348082018-03-30 15:59:38 +0000370 }
371
Greg Daniele20fcad2020-01-08 11:52:34 -0500372 auto rtc = GrRenderTargetContext::MakeWithFallback(
373 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox,
Greg Daniele32506b2020-02-10 16:00:54 -0500374 {reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomond005b692020-04-01 15:47:05 -0400375 kMaskOrigin);
Chris Daltonc5348082018-03-30 15:59:38 +0000376 if (!rtc) {
Greg Daniele32506b2020-02-10 16:00:54 -0500377 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000378 }
379
380 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
Greg Daniele32506b2020-02-10 16:00:54 -0500381 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000382 }
383
Greg Daniele32506b2020-02-10 16:00:54 -0500384 GrSurfaceProxyView result = rtc->readSurfaceView();
385 if (!result || !result.asTextureProxy()) {
386 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000387 }
388
Brian Salomond005b692020-04-01 15:47:05 -0400389 SkASSERT(result.origin() == kMaskOrigin);
Greg Daniele32506b2020-02-10 16:00:54 -0500390 proxyProvider->assignUniqueKeyToProxy(key, result.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500391 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Chris Daltonc5348082018-03-30 15:59:38 +0000392
393 return result;
394}
395
Brian Osman5d034742017-09-11 13:38:55 -0400396namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500397
Brian Osman5d034742017-09-11 13:38:55 -0400398/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400399 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400400 * elements, each storing their own AA setting (and already transformed into device space). This
401 * stores all of the information needed by the worker thread to draw all clip elements (see below,
402 * in createSoftwareClipMask).
403 */
404class ClipMaskData {
405public:
406 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600407 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400408 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600409 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400410 fElements.addToTail(*iter.get());
411 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000412 }
413
Chris Dalton79471932017-10-27 01:50:57 -0600414 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400415 InitialState initialState() const { return fInitialState; }
416 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000417
Brian Osman5d034742017-09-11 13:38:55 -0400418private:
Chris Dalton79471932017-10-27 01:50:57 -0600419 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400420 InitialState fInitialState;
421 ElementList fElements;
422};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000423
Brian Osman5d034742017-09-11 13:38:55 -0400424}
425
426static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600427 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400428 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800429 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600430 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800431
Brian Osman5d034742017-09-11 13:38:55 -0400432 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000433
Brian Osman5d034742017-09-11 13:38:55 -0400434 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000435 const Element* element = iter.get();
436 SkClipOp op = element->getOp();
437 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000438
Mike Reedc1f77742016-12-09 09:00:50 -0500439 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 // Intersect and reverse difference require modifying pixels outside of the geometry
441 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
442 // but leave the pixels inside the geometry alone. For reverse difference we invert all
443 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500444 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600445 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000446 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400447 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000448 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000449 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000450 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000451 clipPath.toggleInverseFillType();
Michael Ludwig2686d692020-04-17 20:21:37 +0000452 GrStyledShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400453 helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000454 continue;
455 }
456
457 // The other ops (union, xor, diff) only affect pixels inside
458 // the geometry so they can just be drawn normally
Brian Salomonc3833b42018-07-09 18:23:58 +0000459 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
460 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000461 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000462 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000463 element->asDeviceSpacePath(&path);
Michael Ludwig2686d692020-04-17 20:21:37 +0000464 GrStyledShape shape(path, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400465 helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000466 }
467 }
Brian Osman5d034742017-09-11 13:38:55 -0400468}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000469
Greg Daniele32506b2020-02-10 16:00:54 -0500470GrSurfaceProxyView GrClipStackClip::createSoftwareClipMask(
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500471 GrRecordingContext* context, const GrReducedClip& reducedClip,
Brian Osman5d034742017-09-11 13:38:55 -0400472 GrRenderTargetContext* renderTargetContext) const {
Brian Salomonc3833b42018-07-09 18:23:58 +0000473 GrUniqueKey key;
474 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
475 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800476
Robert Phillips9da87e02019-02-04 13:26:26 -0500477 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500478
Brian Salomond005b692020-04-01 15:47:05 -0400479 if (auto cachedView = find_mask(proxyProvider, key)) {
480 return cachedView;
Brian Osman5d034742017-09-11 13:38:55 -0400481 }
482
483 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
484 // left corner of the resulting rect to the top left of the texture.
485 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
486
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500487 SkTaskGroup* taskGroup = nullptr;
488 if (auto direct = context->priv().asDirectContext()) {
489 taskGroup = direct->priv().getTaskGroup();
490 }
491
Greg Daniele32506b2020-02-10 16:00:54 -0500492 GrSurfaceProxyView view;
Brian Osman5d034742017-09-11 13:38:55 -0400493 if (taskGroup && renderTargetContext) {
Brian Salomond005b692020-04-01 15:47:05 -0400494 const GrCaps* caps = context->priv().caps();
Brian Osman5d034742017-09-11 13:38:55 -0400495 // Create our texture proxy
Robert Phillips0a15cc62019-07-30 12:49:10 -0400496 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
497 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500498
Greg Daniel47c20e82020-01-21 14:29:57 -0500499 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kAlpha_8);
500
Brian Osmand140fe92017-10-03 12:17:26 -0400501 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
502 // to ops), so it can't have any pending IO.
Greg Daniele32506b2020-02-10 16:00:54 -0500503 auto proxy = proxyProvider->createProxy(format,
504 maskSpaceIBounds.size(),
Greg Daniele32506b2020-02-10 16:00:54 -0500505 GrRenderable::kNo,
506 1,
Greg Daniele32506b2020-02-10 16:00:54 -0500507 GrMipMapped::kNo,
508 SkBackingFit::kApprox,
509 SkBudgeted::kYes,
510 GrProtected::kNo);
Brian Osman5d034742017-09-11 13:38:55 -0400511
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500512 auto uploader = std::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
Brian Osman099fa0f2017-10-02 16:38:32 -0400513 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400514 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
Brian Salomon5f394272019-07-02 14:07:49 -0400515 TRACE_EVENT0("skia.gpu", "Threaded SW Clip Mask Render");
Brian Osman5d034742017-09-11 13:38:55 -0400516 GrSWMaskHelper helper(uploaderRaw->getPixels());
517 if (helper.init(maskSpaceIBounds)) {
518 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
Chris Dalton79471932017-10-27 01:50:57 -0600519 uploaderRaw->data().scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400520 uploaderRaw->data().initialState());
521 } else {
522 SkDEBUGFAIL("Unable to allocate SW clip mask.");
523 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400524 uploaderRaw->signalAndFreeData();
Brian Osman5d034742017-09-11 13:38:55 -0400525 };
526
527 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400528 proxy->texPriv().setDeferredUploader(std::move(uploader));
Greg Daniele32506b2020-02-10 16:00:54 -0500529
Brian Salomond005b692020-04-01 15:47:05 -0400530 view = {std::move(proxy), kMaskOrigin, swizzle};
Brian Osman5d034742017-09-11 13:38:55 -0400531 } else {
532 GrSWMaskHelper helper;
533 if (!helper.init(maskSpaceIBounds)) {
Greg Daniele32506b2020-02-10 16:00:54 -0500534 return {};
Brian Osman5d034742017-09-11 13:38:55 -0400535 }
536
Chris Dalton79471932017-10-27 01:50:57 -0600537 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400538 reducedClip.initialState());
539
Greg Daniele32506b2020-02-10 16:00:54 -0500540 view = helper.toTextureView(context, SkBackingFit::kApprox);
Brian Osman5d034742017-09-11 13:38:55 -0400541 }
542
Greg Daniele32506b2020-02-10 16:00:54 -0500543 SkASSERT(view);
Brian Salomond005b692020-04-01 15:47:05 -0400544 SkASSERT(view.origin() == kMaskOrigin);
Greg Daniele32506b2020-02-10 16:00:54 -0500545 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500546 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Greg Daniele32506b2020-02-10 16:00:54 -0500547 return view;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000548}