blob: b7be58a9259da3444dd4feee619d262fd68b3623 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
csmartdaltonc6f411e2016-08-05 22:32:12 -07002 * Copyright 2016 Google Inc.
robertphillips@google.com1e945b72012-04-16 18:03:03 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkTo.h"
9#include "src/core/SkClipOpPriv.h"
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"
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"
Michael Ludwig663afe52019-06-03 16:46:19 -040030#include "src/gpu/geometry/GrShape.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.
81static 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).
Robert Phillips6f0e02f2019-02-13 11:02:28 -050091bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +000092 const SkIRect& scissorRect,
93 bool hasUserStencilSettings,
94 const GrRenderTargetContext* renderTargetContext,
95 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +000096 const Element* element,
Chris Daltonc5348082018-03-30 15:59:38 +000097 GrPathRenderer** prOut,
98 bool needsStencil) {
Brian Salomonc3833b42018-07-09 18:23:58 +000099 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000100 // rects can always be drawn directly w/o using the software path
101 // TODO: skip rrects once we're drawing them directly.
102 if (prOut) {
103 *prOut = nullptr;
104 }
105 return false;
106 } else {
107 // We shouldn't get here with an empty clip element.
Brian Salomonc3833b42018-07-09 18:23:58 +0000108 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
Chris Daltonc5348082018-03-30 15:59:38 +0000109
110 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
111 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000112 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000113 if (path.isInverseFillType()) {
114 path.toggleInverseFillType();
115 }
116
Chris Dalton09e56892019-03-13 00:22:01 -0600117 // We only use this method when rendering coverage clip masks.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600118 SkASSERT(renderTargetContext->numSamples() <= 1);
119 auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600120
Chris Daltonc5348082018-03-30 15:59:38 +0000121 GrPathRendererChain::DrawType type =
122 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
123 : GrPathRendererChain::DrawType::kColor;
124
125 GrShape shape(path, GrStyle::SimpleFill());
126 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -0500127 canDrawArgs.fCaps = context->priv().caps();
Chris Daltonc5348082018-03-30 15:59:38 +0000128 canDrawArgs.fClipConservativeBounds = &scissorRect;
129 canDrawArgs.fViewMatrix = &viewMatrix;
130 canDrawArgs.fShape = &shape;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600131 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -0500132 SkASSERT(!renderTargetContext->wrapsVkSecondaryCB());
133 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
Chris Daltonc5348082018-03-30 15:59:38 +0000134 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
135
136 // the 'false' parameter disallows use of the SW path renderer
137 GrPathRenderer* pr =
Robert Phillips9da87e02019-02-04 13:26:26 -0500138 context->priv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
Chris Daltonc5348082018-03-30 15:59:38 +0000139 if (prOut) {
140 *prOut = pr;
141 }
142 return SkToBool(!pr);
143 }
144}
145
146/*
147 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
148 * will be used on any element. If so, it returns true to indicate that the
149 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
150 */
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500151bool GrClipStackClip::UseSWOnlyPath(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000152 bool hasUserStencilSettings,
153 const GrRenderTargetContext* renderTargetContext,
154 const GrReducedClip& reducedClip) {
Chris Daltond22a0232018-08-22 17:25:10 +0000155 // TODO: right now it appears that GPU clip masks are strictly slower than software. We may
156 // want to revisit this assumption once we can test with render target sorting.
157 return true;
158
Chris Daltonc5348082018-03-30 15:59:38 +0000159 // TODO: generalize this function so that when
160 // a clip gets complex enough it can just be done in SW regardless
161 // of whether it would invoke the GrSoftwarePathRenderer.
162
Greg Danielbe7fc462019-01-03 16:40:42 -0500163 // If we're avoiding stencils, always use SW. This includes drawing into a wrapped vulkan
164 // secondary command buffer which can't handle stencils.
Robert Phillips9da87e02019-02-04 13:26:26 -0500165 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500166 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000167 return true;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400168 }
Chris Daltonc5348082018-03-30 15:59:38 +0000169
170 // Set the matrix so that rendered clip elements are transformed to mask space from clip
171 // space.
172 SkMatrix translate;
173 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
174
175 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000176 const Element* element = iter.get();
Chris Daltonc5348082018-03-30 15:59:38 +0000177
Brian Salomonc3833b42018-07-09 18:23:58 +0000178 SkClipOp op = element->getOp();
179 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000180 bool needsStencil = invert ||
181 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
182
183 if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings,
184 renderTargetContext, translate, element, nullptr, needsStencil)) {
185 return true;
186 }
187 }
188 return false;
189}
190
191////////////////////////////////////////////////////////////////////////////////
192// sort out what kind of clip mask needs to be created: alpha, stencil,
193// scissor, or entirely software
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500194bool GrClipStackClip::apply(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400195 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
196 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400197 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
198 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700199 return false;
200 }
201
Brian Salomon510dd422017-03-16 12:15:22 -0400202 if (!fStack || fStack->isWideOpen()) {
203 return true;
204 }
205
Brian Osman5f5680c2019-06-05 10:17:25 -0400206 // An default count of 4 was chosen because of the common pattern in Blink of:
207 // isect RR
208 // diff RR
209 // isect convex_poly
210 // isect convex_poly
211 // when drawing rounded div borders.
212 constexpr int kMaxAnalyticFPs = 4;
213
Chris Daltona32a3c32017-12-05 10:05:21 -0700214 int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
Brian Osman5f5680c2019-06-05 10:17:25 -0400215 int maxAnalyticFPs = kMaxAnalyticFPs;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600216 if (renderTargetContext->numSamples() > 1 || useHWAA || hasUserStencilSettings) {
217 // Disable analytic clips when we have MSAA. In MSAA we never conflate coverage and opacity.
218 maxAnalyticFPs = 0;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400219 // We disable MSAA when avoiding stencil.
Robert Phillips9da87e02019-02-04 13:26:26 -0500220 SkASSERT(!context->priv().caps()->avoidStencilBuffers());
Chris Dalton584a79a2017-11-15 13:14:01 -0700221 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500222 auto* ccpr = context->priv().drawingManager()->getCoverageCountingPathRenderer();
Chris Dalton584a79a2017-11-15 13:14:01 -0700223
Robert Phillips9da87e02019-02-04 13:26:26 -0500224 GrReducedClip reducedClip(*fStack, devBounds, context->priv().caps(),
Ethan Nicholaseace9352018-10-15 20:09:54 +0000225 maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0);
Chris Daltona32a3c32017-12-05 10:05:21 -0700226 if (InitialState::kAllOut == reducedClip.initialState() &&
227 reducedClip.maskElements().isEmpty()) {
228 return false;
229 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000230
Chris Dalton79471932017-10-27 01:50:57 -0600231 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700232 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700233 }
cdalton93a379b2016-05-11 13:58:08 -0700234
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700235 if (!reducedClip.windowRectangles().empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700236 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
237 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700238 }
239
Chris Daltona32a3c32017-12-05 10:05:21 -0700240 if (!reducedClip.maskElements().isEmpty()) {
241 if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
242 out)) {
243 return false;
244 }
245 }
246
247 // The opList ID must not be looked up until AFTER producing the clip mask (if any). That step
248 // can cause a flush or otherwise change which opList our draw is going into.
249 uint32_t opListID = renderTargetContext->getOpList()->uniqueID();
250 int rtWidth = renderTargetContext->width(), rtHeight = renderTargetContext->height();
Chris Dalton4c458b12018-06-16 17:22:59 -0600251 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(ccpr, opListID, rtWidth, rtHeight)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700252 out->addCoverageFP(std::move(clipFPs));
253 }
254
Chris Daltona32a3c32017-12-05 10:05:21 -0700255 return true;
256}
csmartdaltond211e782016-08-15 11:17:19 -0700257
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500258bool GrClipStackClip::applyClipMask(GrRecordingContext* context,
259 GrRenderTargetContext* renderTargetContext,
Chris Daltona32a3c32017-12-05 10:05:21 -0700260 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
261 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700262#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600263 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500264 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
265 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600266 const SkIRect& scissor = reducedClip.scissor();
267 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700268#endif
csmartdaltond211e782016-08-15 11:17:19 -0700269
Chris Dalton6ce447a2019-06-23 18:07:38 -0600270 // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping.
271 if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500272 context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500273 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000274 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()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000287 return true;
288 }
Eric Karl5c779752017-05-08 12:02:07 -0700289
Chris Daltonc5348082018-03-30 15:59:38 +0000290 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
291 // unless stencils are disallowed.
Robert Phillips9da87e02019-02-04 13:26:26 -0500292 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500293 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700294 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
295 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700296 return false;
297 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000298 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000299
Robert Phillips65048132017-08-10 08:44:49 -0400300 renderTargetContext->setNeedsStencil();
csmartdaltonbde96c62016-08-31 12:54:46 -0700301
Brian Salomonc3833b42018-07-09 18:23:58 +0000302 // This relies on the property that a reduced sub-rect of the last clip will contain all the
303 // relevant window rectangles that were in the last clip. This subtle requirement will go away
304 // after clipping is overhauled.
305 if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(), reducedClip.scissor(),
306 reducedClip.numAnalyticFPs())) {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000307 reducedClip.drawStencilClipMask(context, renderTargetContext);
Brian Salomonc3833b42018-07-09 18:23:58 +0000308 renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor(),
309 reducedClip.numAnalyticFPs());
csmartdaltonbde96c62016-08-31 12:54:46 -0700310 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000311 // GrAppliedClip doesn't need to figure numAnalyticFPs into its key (used by operator==) because
312 // it verifies the FPs are also equal.
313 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000314 return true;
315}
316
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000317////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700318// Create a 8-bit clip mask in alpha
319
Brian Salomonc3833b42018-07-09 18:23:58 +0000320static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
321 GrUniqueKey* key) {
322 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
323 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
324 builder[0] = clipGenID;
325 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
326 // sometimes result in negative coordinates from device space.
327 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
328 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
329 builder[3] = numAnalyticFPs;
330}
331
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500332static void add_invalidate_on_pop_message(GrRecordingContext* context,
Robert Phillips427966a2018-12-20 17:20:43 -0500333 const SkClipStack& stack, uint32_t clipGenID,
334 const GrUniqueKey& clipMaskKey) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500335 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips427966a2018-12-20 17:20:43 -0500336
Brian Salomon19f0ed52017-01-06 13:54:58 -0500337 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
338 while (const Element* element = iter.prev()) {
339 if (element->getGenID() == clipGenID) {
Robert Phillips427966a2018-12-20 17:20:43 -0500340 element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500341 return;
342 }
343 }
344 SkDEBUGFAIL("Gen ID was not found in stack.");
345}
346
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500347sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000348 const GrReducedClip& reducedClip) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500349 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc3833b42018-07-09 18:23:58 +0000350 GrUniqueKey key;
351 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
352 reducedClip.numAnalyticFPs(), &key);
353
Chris Daltonc5348082018-03-30 15:59:38 +0000354 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Robert Phillips5491e822018-06-27 12:54:44 -0400355 key, kTopLeft_GrSurfaceOrigin));
Chris Daltonc5348082018-03-30 15:59:38 +0000356 if (proxy) {
357 return proxy;
358 }
359
Greg Daniel4065d452018-11-16 15:43:41 -0500360 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500361 context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400362 sk_sp<GrRenderTargetContext> rtc(context->priv().makeDeferredRenderTargetContextWithFallback(
363 format, SkBackingFit::kApprox, reducedClip.width(), reducedClip.height(),
364 kAlpha_8_GrPixelConfig, nullptr, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr,
365 SkBudgeted::kYes, proxy->isProtected() ? GrProtected::kYes : GrProtected::kNo));
Chris Daltonc5348082018-03-30 15:59:38 +0000366 if (!rtc) {
367 return nullptr;
368 }
369
370 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
371 return nullptr;
372 }
373
374 sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
375 if (!result) {
376 return nullptr;
377 }
378
Robert Phillips5491e822018-06-27 12:54:44 -0400379 SkASSERT(result->origin() == kTopLeft_GrSurfaceOrigin);
Chris Daltonc5348082018-03-30 15:59:38 +0000380 proxyProvider->assignUniqueKeyToProxy(key, result.get());
Robert Phillips427966a2018-12-20 17:20:43 -0500381 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Chris Daltonc5348082018-03-30 15:59:38 +0000382
383 return result;
384}
385
Brian Osman5d034742017-09-11 13:38:55 -0400386namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500387
Brian Osman5d034742017-09-11 13:38:55 -0400388/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400389 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400390 * elements, each storing their own AA setting (and already transformed into device space). This
391 * stores all of the information needed by the worker thread to draw all clip elements (see below,
392 * in createSoftwareClipMask).
393 */
394class ClipMaskData {
395public:
396 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600397 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400398 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600399 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400400 fElements.addToTail(*iter.get());
401 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000402 }
403
Chris Dalton79471932017-10-27 01:50:57 -0600404 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400405 InitialState initialState() const { return fInitialState; }
406 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000407
Brian Osman5d034742017-09-11 13:38:55 -0400408private:
Chris Dalton79471932017-10-27 01:50:57 -0600409 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400410 InitialState fInitialState;
411 ElementList fElements;
412};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000413
Brian Osman5d034742017-09-11 13:38:55 -0400414}
415
416static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600417 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400418 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800419 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600420 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800421
Brian Osman5d034742017-09-11 13:38:55 -0400422 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000423
Brian Osman5d034742017-09-11 13:38:55 -0400424 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000425 const Element* element = iter.get();
426 SkClipOp op = element->getOp();
427 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000428
Mike Reedc1f77742016-12-09 09:00:50 -0500429 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430 // Intersect and reverse difference require modifying pixels outside of the geometry
431 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
432 // but leave the pixels inside the geometry alone. For reverse difference we invert all
433 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500434 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600435 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000436 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400437 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000438 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000439 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000440 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000441 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700442 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400443 helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000444 continue;
445 }
446
447 // The other ops (union, xor, diff) only affect pixels inside
448 // the geometry so they can just be drawn normally
Brian Salomonc3833b42018-07-09 18:23:58 +0000449 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
450 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000451 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000452 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000453 element->asDeviceSpacePath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700454 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400455 helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000456 }
457 }
Brian Osman5d034742017-09-11 13:38:55 -0400458}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000459
Brian Osman5d034742017-09-11 13:38:55 -0400460sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500461 GrRecordingContext* context, const GrReducedClip& reducedClip,
Brian Osman5d034742017-09-11 13:38:55 -0400462 GrRenderTargetContext* renderTargetContext) const {
Brian Salomonc3833b42018-07-09 18:23:58 +0000463 GrUniqueKey key;
464 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
465 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800466
Robert Phillips9da87e02019-02-04 13:26:26 -0500467 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500468
469 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Brian Osman5d034742017-09-11 13:38:55 -0400470 key, kTopLeft_GrSurfaceOrigin));
471 if (proxy) {
472 return proxy;
473 }
474
475 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
476 // left corner of the resulting rect to the top left of the texture.
477 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
478
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500479 SkTaskGroup* taskGroup = nullptr;
480 if (auto direct = context->priv().asDirectContext()) {
481 taskGroup = direct->priv().getTaskGroup();
482 }
483
Brian Osman5d034742017-09-11 13:38:55 -0400484 if (taskGroup && renderTargetContext) {
485 // Create our texture proxy
486 GrSurfaceDesc desc;
Brian Osman5d034742017-09-11 13:38:55 -0400487 desc.fWidth = maskSpaceIBounds.width();
488 desc.fHeight = maskSpaceIBounds.height();
489 desc.fConfig = kAlpha_8_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -0500490
491 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500492 context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500493
Brian Osmand140fe92017-10-03 12:17:26 -0400494 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
495 // to ops), so it can't have any pending IO.
Greg Daniel4065d452018-11-16 15:43:41 -0500496 proxy = proxyProvider->createProxy(format, desc, kTopLeft_GrSurfaceOrigin,
Robert Phillips10d17212019-04-24 14:09:10 -0400497 SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Osman5d034742017-09-11 13:38:55 -0400498
Brian Osman099fa0f2017-10-02 16:38:32 -0400499 auto uploader = skstd::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
500 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400501 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
502 TRACE_EVENT0("skia", "Threaded SW Clip Mask Render");
503 GrSWMaskHelper helper(uploaderRaw->getPixels());
504 if (helper.init(maskSpaceIBounds)) {
505 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
Chris Dalton79471932017-10-27 01:50:57 -0600506 uploaderRaw->data().scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400507 uploaderRaw->data().initialState());
508 } else {
509 SkDEBUGFAIL("Unable to allocate SW clip mask.");
510 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400511 uploaderRaw->signalAndFreeData();
Brian Osman5d034742017-09-11 13:38:55 -0400512 };
513
514 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400515 proxy->texPriv().setDeferredUploader(std::move(uploader));
Brian Osman5d034742017-09-11 13:38:55 -0400516 } else {
517 GrSWMaskHelper helper;
518 if (!helper.init(maskSpaceIBounds)) {
519 return nullptr;
520 }
521
Chris Dalton79471932017-10-27 01:50:57 -0600522 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400523 reducedClip.initialState());
524
525 proxy = helper.toTextureProxy(context, SkBackingFit::kApprox);
526 }
527
528 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500529 proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
Robert Phillips427966a2018-12-20 17:20:43 -0500530 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Brian Osman5d034742017-09-11 13:38:55 -0400531 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000532}