blob: 094a49e6d60909d63a14f4f303b4c1fdae260bc1 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
csmartdaltonc6f411e2016-08-05 22:32:12 -07002 * Copyright 2016 Google Inc.
robertphillips@google.com1e945b72012-04-16 18:03:03 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkTo.h"
9#include "src/core/SkClipOpPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkTaskGroup.h"
11#include "src/core/SkTraceEvent.h"
12#include "src/gpu/GrAppliedClip.h"
13#include "src/gpu/GrClipStackClip.h"
14#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrDeferredProxyUploader.h"
16#include "src/gpu/GrDrawingManager.h"
17#include "src/gpu/GrFixedClip.h"
18#include "src/gpu/GrGpuResourcePriv.h"
19#include "src/gpu/GrProxyProvider.h"
20#include "src/gpu/GrRecordingContextPriv.h"
21#include "src/gpu/GrRenderTargetContextPriv.h"
22#include "src/gpu/GrSWMaskHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrStencilAttachment.h"
24#include "src/gpu/GrStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/effects/GrConvexPolyEffect.h"
27#include "src/gpu/effects/GrRRectEffect.h"
28#include "src/gpu/effects/GrTextureDomain.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040029#include "src/gpu/geometry/GrShape.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000030
Brian Salomonc3833b42018-07-09 18:23:58 +000031typedef SkClipStack::Element Element;
32typedef GrReducedClip::InitialState InitialState;
33typedef GrReducedClip::ElementList ElementList;
34
35const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070036
csmartdaltonc6f411e2016-08-05 22:32:12 -070037bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070038 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070039 return true;
40 }
Brian Salomon9a767722017-03-13 17:57:28 -040041 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070042}
43
bsalomon7f0d9f32016-08-15 14:49:10 -070044bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070045 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070046 return true;
47 }
Brian Salomon9a767722017-03-13 17:57:28 -040048 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070049}
50
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050051bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070052 if (!fStack) {
53 return false;
54 }
55 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050056 bool isAA;
57 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
Chris Dalton3b51df12017-11-27 14:33:06 -070058 *aa = GrAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070059 return true;
60 }
61 return false;
62}
63
csmartdaltonc6f411e2016-08-05 22:32:12 -070064void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
65 bool* isIntersectionOfRects) const {
66 if (!fStack) {
67 devResult->setXYWH(0, 0, width, height);
68 if (isIntersectionOfRects) {
69 *isIntersectionOfRects = true;
70 }
71 return;
72 }
73 SkRect devBounds;
Brian Salomon9a767722017-03-13 17:57:28 -040074 fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
csmartdaltonc6f411e2016-08-05 22:32:12 -070075 devBounds.roundOut(devResult);
76}
77
Chris Daltonc5348082018-03-30 15:59:38 +000078////////////////////////////////////////////////////////////////////////////////
79// set up the draw state to enable the aa clipping mask.
80static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(sk_sp<GrTextureProxy> mask,
81 const SkIRect& devBound) {
82 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
83 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(mask), domainTexels,
84 {devBound.fLeft, devBound.fTop});
85}
86
87// Does the path in 'element' require SW rendering? If so, return true (and,
88// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
89// 'prOut' to the non-SW path renderer that will do the job).
Robert Phillips6f0e02f2019-02-13 11:02:28 -050090bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +000091 const SkIRect& scissorRect,
92 bool hasUserStencilSettings,
93 const GrRenderTargetContext* renderTargetContext,
94 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +000095 const Element* element,
Chris Daltonc5348082018-03-30 15:59:38 +000096 GrPathRenderer** prOut,
97 bool needsStencil) {
Brian Salomonc3833b42018-07-09 18:23:58 +000098 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +000099 // rects can always be drawn directly w/o using the software path
100 // TODO: skip rrects once we're drawing them directly.
101 if (prOut) {
102 *prOut = nullptr;
103 }
104 return false;
105 } else {
106 // We shouldn't get here with an empty clip element.
Brian Salomonc3833b42018-07-09 18:23:58 +0000107 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
Chris Daltonc5348082018-03-30 15:59:38 +0000108
109 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
110 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000111 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000112 if (path.isInverseFillType()) {
113 path.toggleInverseFillType();
114 }
115
Chris Dalton09e56892019-03-13 00:22:01 -0600116 // We only use this method when rendering coverage clip masks.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600117 SkASSERT(renderTargetContext->numSamples() <= 1);
118 auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600119
Chris Daltonc5348082018-03-30 15:59:38 +0000120 GrPathRendererChain::DrawType type =
121 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
122 : GrPathRendererChain::DrawType::kColor;
123
124 GrShape shape(path, GrStyle::SimpleFill());
125 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -0500126 canDrawArgs.fCaps = context->priv().caps();
Chris Daltoneffee202019-07-01 22:28:03 -0600127 canDrawArgs.fProxy = renderTargetContext->proxy();
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
Greg Danielf41b2bd2019-08-22 16:19:24 -0400247 // The opsTask 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 opstask our draw is going into.
249 uint32_t opsTaskID = renderTargetContext->getOpsTask()->uniqueID();
250 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(ccpr, opsTaskID)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700251 out->addCoverageFP(std::move(clipFPs));
252 }
253
Chris Daltona32a3c32017-12-05 10:05:21 -0700254 return true;
255}
csmartdaltond211e782016-08-15 11:17:19 -0700256
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500257bool GrClipStackClip::applyClipMask(GrRecordingContext* context,
258 GrRenderTargetContext* renderTargetContext,
Chris Daltona32a3c32017-12-05 10:05:21 -0700259 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
260 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700261#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600262 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500263 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
264 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600265 const SkIRect& scissor = reducedClip.scissor();
266 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700267#endif
csmartdaltond211e782016-08-15 11:17:19 -0700268
Chris Dalton6ce447a2019-06-23 18:07:38 -0600269 // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping.
270 if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500271 context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500272 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000273 sk_sp<GrTextureProxy> result;
274 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
275 // The clip geometry is complex enough that it will be more efficient to create it
276 // entirely in software
277 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
278 } else {
279 result = this->createAlphaClipMask(context, reducedClip);
280 }
281
282 if (result) {
283 // The mask's top left coord should be pinned to the rounded-out top left corner of
284 // the clip's device space bounds.
285 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000286 return true;
287 }
Eric Karl5c779752017-05-08 12:02:07 -0700288
Chris Daltonc5348082018-03-30 15:59:38 +0000289 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
290 // unless stencils are disallowed.
Robert Phillips9da87e02019-02-04 13:26:26 -0500291 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500292 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700293 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
294 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700295 return false;
296 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000297 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000298
Brian Salomonc3833b42018-07-09 18:23:58 +0000299 // This relies on the property that a reduced sub-rect of the last clip will contain all the
300 // relevant window rectangles that were in the last clip. This subtle requirement will go away
301 // after clipping is overhauled.
302 if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(), reducedClip.scissor(),
303 reducedClip.numAnalyticFPs())) {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000304 reducedClip.drawStencilClipMask(context, renderTargetContext);
Brian Salomonc3833b42018-07-09 18:23:58 +0000305 renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor(),
306 reducedClip.numAnalyticFPs());
csmartdaltonbde96c62016-08-31 12:54:46 -0700307 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000308 // GrAppliedClip doesn't need to figure numAnalyticFPs into its key (used by operator==) because
309 // it verifies the FPs are also equal.
310 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000311 return true;
312}
313
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000314////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700315// Create a 8-bit clip mask in alpha
316
Brian Salomonc3833b42018-07-09 18:23:58 +0000317static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
318 GrUniqueKey* key) {
319 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
320 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
321 builder[0] = clipGenID;
322 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
323 // sometimes result in negative coordinates from device space.
324 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
325 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
326 builder[3] = numAnalyticFPs;
327}
328
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500329static void add_invalidate_on_pop_message(GrRecordingContext* context,
Robert Phillips427966a2018-12-20 17:20:43 -0500330 const SkClipStack& stack, uint32_t clipGenID,
331 const GrUniqueKey& clipMaskKey) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500332 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips427966a2018-12-20 17:20:43 -0500333
Brian Salomon19f0ed52017-01-06 13:54:58 -0500334 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
335 while (const Element* element = iter.prev()) {
336 if (element->getGenID() == clipGenID) {
Robert Phillips427966a2018-12-20 17:20:43 -0500337 element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500338 return;
339 }
340 }
341 SkDEBUGFAIL("Gen ID was not found in stack.");
342}
343
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500344sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000345 const GrReducedClip& reducedClip) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500346 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc3833b42018-07-09 18:23:58 +0000347 GrUniqueKey key;
348 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
349 reducedClip.numAnalyticFPs(), &key);
350
Chris Daltonc5348082018-03-30 15:59:38 +0000351 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Brian Salomon2af3e702019-08-11 19:10:31 -0400352 key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin));
Chris Daltonc5348082018-03-30 15:59:38 +0000353 if (proxy) {
354 return proxy;
355 }
356
Brian Salomond6287472019-06-24 15:50:07 -0400357 auto isProtected = proxy->isProtected() ? GrProtected::kYes : GrProtected::kNo;
Brian Salomonbf6b9792019-08-21 09:38:10 -0400358 auto rtc = context->priv().makeDeferredRenderTargetContextWithFallback(SkBackingFit::kApprox,
359 reducedClip.width(),
360 reducedClip.height(),
361 GrColorType::kAlpha_8,
362 nullptr,
363 1,
364 GrMipMapped::kNo,
365 kTopLeft_GrSurfaceOrigin,
366 nullptr,
367 SkBudgeted::kYes,
368 isProtected);
Chris Daltonc5348082018-03-30 15:59:38 +0000369 if (!rtc) {
370 return nullptr;
371 }
372
373 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
374 return nullptr;
375 }
376
377 sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
378 if (!result) {
379 return nullptr;
380 }
381
Robert Phillips5491e822018-06-27 12:54:44 -0400382 SkASSERT(result->origin() == kTopLeft_GrSurfaceOrigin);
Chris Daltonc5348082018-03-30 15:59:38 +0000383 proxyProvider->assignUniqueKeyToProxy(key, result.get());
Robert Phillips427966a2018-12-20 17:20:43 -0500384 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Chris Daltonc5348082018-03-30 15:59:38 +0000385
386 return result;
387}
388
Brian Osman5d034742017-09-11 13:38:55 -0400389namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500390
Brian Osman5d034742017-09-11 13:38:55 -0400391/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400392 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400393 * elements, each storing their own AA setting (and already transformed into device space). This
394 * stores all of the information needed by the worker thread to draw all clip elements (see below,
395 * in createSoftwareClipMask).
396 */
397class ClipMaskData {
398public:
399 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600400 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400401 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600402 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400403 fElements.addToTail(*iter.get());
404 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000405 }
406
Chris Dalton79471932017-10-27 01:50:57 -0600407 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400408 InitialState initialState() const { return fInitialState; }
409 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000410
Brian Osman5d034742017-09-11 13:38:55 -0400411private:
Chris Dalton79471932017-10-27 01:50:57 -0600412 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400413 InitialState fInitialState;
414 ElementList fElements;
415};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000416
Brian Osman5d034742017-09-11 13:38:55 -0400417}
418
419static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600420 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400421 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800422 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600423 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800424
Brian Osman5d034742017-09-11 13:38:55 -0400425 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000426
Brian Osman5d034742017-09-11 13:38:55 -0400427 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000428 const Element* element = iter.get();
429 SkClipOp op = element->getOp();
430 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000431
Mike Reedc1f77742016-12-09 09:00:50 -0500432 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000433 // Intersect and reverse difference require modifying pixels outside of the geometry
434 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
435 // but leave the pixels inside the geometry alone. For reverse difference we invert all
436 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500437 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600438 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000439 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400440 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000441 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000442 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000443 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000444 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700445 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400446 helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000447 continue;
448 }
449
450 // The other ops (union, xor, diff) only affect pixels inside
451 // the geometry so they can just be drawn normally
Brian Salomonc3833b42018-07-09 18:23:58 +0000452 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
453 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000454 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000455 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000456 element->asDeviceSpacePath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700457 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400458 helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000459 }
460 }
Brian Osman5d034742017-09-11 13:38:55 -0400461}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000462
Brian Osman5d034742017-09-11 13:38:55 -0400463sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500464 GrRecordingContext* context, const GrReducedClip& reducedClip,
Brian Osman5d034742017-09-11 13:38:55 -0400465 GrRenderTargetContext* renderTargetContext) const {
Brian Salomonc3833b42018-07-09 18:23:58 +0000466 GrUniqueKey key;
467 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
468 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800469
Robert Phillips9da87e02019-02-04 13:26:26 -0500470 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400471 const GrCaps* caps = context->priv().caps();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500472
473 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Brian Salomon2af3e702019-08-11 19:10:31 -0400474 key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin));
Brian Osman5d034742017-09-11 13:38:55 -0400475 if (proxy) {
476 return proxy;
477 }
478
479 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
480 // left corner of the resulting rect to the top left of the texture.
481 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
482
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500483 SkTaskGroup* taskGroup = nullptr;
484 if (auto direct = context->priv().asDirectContext()) {
485 taskGroup = direct->priv().getTaskGroup();
486 }
487
Brian Osman5d034742017-09-11 13:38:55 -0400488 if (taskGroup && renderTargetContext) {
489 // Create our texture proxy
490 GrSurfaceDesc desc;
Brian Osman5d034742017-09-11 13:38:55 -0400491 desc.fWidth = maskSpaceIBounds.width();
492 desc.fHeight = maskSpaceIBounds.height();
493 desc.fConfig = kAlpha_8_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -0500494
Robert Phillips0a15cc62019-07-30 12:49:10 -0400495 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
496 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500497
Brian Osmand140fe92017-10-03 12:17:26 -0400498 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
499 // to ops), so it can't have any pending IO.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400500 proxy = proxyProvider->createProxy(format,
501 desc,
502 GrRenderable::kNo,
503 1,
504 kTopLeft_GrSurfaceOrigin,
505 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));
Brian Osman5d034742017-09-11 13:38:55 -0400527 } else {
528 GrSWMaskHelper helper;
529 if (!helper.init(maskSpaceIBounds)) {
530 return nullptr;
531 }
532
Chris Dalton79471932017-10-27 01:50:57 -0600533 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400534 reducedClip.initialState());
535
536 proxy = helper.toTextureProxy(context, SkBackingFit::kApprox);
537 }
538
539 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500540 proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
Robert Phillips427966a2018-12-20 17:20:43 -0500541 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Brian Osman5d034742017-09-11 13:38:55 -0400542 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000543}