blob: f0f31a667281df2b613242c61d1a7c4433890cb4 [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
Michael Ludwigc002d562020-05-13 14:17:57 -040065SkIRect GrClipStackClip::getConservativeBounds(int width, int height) const {
66 if (fStack) {
67 SkRect devBounds;
68 fStack->getConservativeBounds(0, 0, width, height, &devBounds);
69 return devBounds.roundOut();
70 } else {
71 return this->GrClip::getConservativeBounds(width, height);
csmartdaltonc6f411e2016-08-05 22:32:12 -070072 }
csmartdaltonc6f411e2016-08-05 22:32:12 -070073}
74
Chris Daltonc5348082018-03-30 15:59:38 +000075////////////////////////////////////////////////////////////////////////////////
76// set up the draw state to enable the aa clipping mask.
Greg Daniele32506b2020-02-10 16:00:54 -050077static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(GrSurfaceProxyView mask,
Brian Salomon64227222020-02-26 13:28:42 -050078 const SkIRect& devBound,
79 const GrCaps& caps) {
80 GrSamplerState samplerState(GrSamplerState::WrapMode::kClampToBorder,
81 GrSamplerState::Filter::kNearest);
Mike Reed1f607332020-05-21 12:11:27 -040082 auto m = SkMatrix::Translate(-devBound.fLeft, -devBound.fTop);
Brian Salomon64227222020-02-26 13:28:42 -050083 auto subset = SkRect::Make(devBound.size());
84 // We scissor to devBounds. The mask's texel centers are aligned to device space
85 // pixel centers. Hence this domain of texture coordinates.
86 auto domain = subset.makeInset(0.5, 0.5);
87 auto fp = GrTextureEffect::MakeSubset(std::move(mask), kPremul_SkAlphaType, m, samplerState,
88 subset, domain, caps);
89 return GrDeviceSpaceEffect::Make(std::move(fp));
Chris Daltonc5348082018-03-30 15:59:38 +000090}
91
92// Does the path in 'element' require SW rendering? If so, return true (and,
93// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
94// 'prOut' to the non-SW path renderer that will do the job).
Robert Phillips6f0e02f2019-02-13 11:02:28 -050095bool GrClipStackClip::PathNeedsSWRenderer(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +000096 const SkIRect& scissorRect,
97 bool hasUserStencilSettings,
98 const GrRenderTargetContext* renderTargetContext,
99 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +0000100 const Element* element,
Chris Daltonc5348082018-03-30 15:59:38 +0000101 GrPathRenderer** prOut,
102 bool needsStencil) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000103 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000104 // rects can always be drawn directly w/o using the software path
105 // TODO: skip rrects once we're drawing them directly.
106 if (prOut) {
107 *prOut = nullptr;
108 }
109 return false;
110 } else {
111 // We shouldn't get here with an empty clip element.
Brian Salomonc3833b42018-07-09 18:23:58 +0000112 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
Chris Daltonc5348082018-03-30 15:59:38 +0000113
114 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
115 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000116 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000117 if (path.isInverseFillType()) {
118 path.toggleInverseFillType();
119 }
120
Chris Dalton09e56892019-03-13 00:22:01 -0600121 // We only use this method when rendering coverage clip masks.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600122 SkASSERT(renderTargetContext->numSamples() <= 1);
123 auto aaType = (element->isAA()) ? GrAAType::kCoverage : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600124
Chris Daltonc5348082018-03-30 15:59:38 +0000125 GrPathRendererChain::DrawType type =
126 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
127 : GrPathRendererChain::DrawType::kColor;
128
Michael Ludwig2686d692020-04-17 20:21:37 +0000129 GrStyledShape shape(path, GrStyle::SimpleFill());
Chris Daltonc5348082018-03-30 15:59:38 +0000130 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -0500131 canDrawArgs.fCaps = context->priv().caps();
Greg Daniel46e366a2019-12-16 14:38:36 -0500132 canDrawArgs.fProxy = renderTargetContext->asRenderTargetProxy();
Chris Daltonc5348082018-03-30 15:59:38 +0000133 canDrawArgs.fClipConservativeBounds = &scissorRect;
134 canDrawArgs.fViewMatrix = &viewMatrix;
135 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400136 canDrawArgs.fPaint = nullptr;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600137 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -0500138 SkASSERT(!renderTargetContext->wrapsVkSecondaryCB());
139 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
Chris Daltonc5348082018-03-30 15:59:38 +0000140 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
141
142 // the 'false' parameter disallows use of the SW path renderer
143 GrPathRenderer* pr =
Robert Phillips9da87e02019-02-04 13:26:26 -0500144 context->priv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
Chris Daltonc5348082018-03-30 15:59:38 +0000145 if (prOut) {
146 *prOut = pr;
147 }
148 return SkToBool(!pr);
149 }
150}
151
152/*
153 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
154 * will be used on any element. If so, it returns true to indicate that the
155 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
156 */
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500157bool GrClipStackClip::UseSWOnlyPath(GrRecordingContext* context,
Chris Daltonc5348082018-03-30 15:59:38 +0000158 bool hasUserStencilSettings,
159 const GrRenderTargetContext* renderTargetContext,
160 const GrReducedClip& reducedClip) {
Chris Daltond22a0232018-08-22 17:25:10 +0000161 // TODO: right now it appears that GPU clip masks are strictly slower than software. We may
162 // want to revisit this assumption once we can test with render target sorting.
163 return true;
164
Chris Daltonc5348082018-03-30 15:59:38 +0000165 // TODO: generalize this function so that when
166 // a clip gets complex enough it can just be done in SW regardless
167 // of whether it would invoke the GrSoftwarePathRenderer.
168
Greg Danielbe7fc462019-01-03 16:40:42 -0500169 // If we're avoiding stencils, always use SW. This includes drawing into a wrapped vulkan
170 // secondary command buffer which can't handle stencils.
Robert Phillips9da87e02019-02-04 13:26:26 -0500171 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500172 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000173 return true;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400174 }
Chris Daltonc5348082018-03-30 15:59:38 +0000175
176 // Set the matrix so that rendered clip elements are transformed to mask space from clip
177 // space.
178 SkMatrix translate;
179 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
180
181 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000182 const Element* element = iter.get();
Chris Daltonc5348082018-03-30 15:59:38 +0000183
Brian Salomonc3833b42018-07-09 18:23:58 +0000184 SkClipOp op = element->getOp();
185 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000186 bool needsStencil = invert ||
187 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
188
189 if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings,
190 renderTargetContext, translate, element, nullptr, needsStencil)) {
191 return true;
192 }
193 }
194 return false;
195}
196
197////////////////////////////////////////////////////////////////////////////////
198// sort out what kind of clip mask needs to be created: alpha, stencil,
199// scissor, or entirely software
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500200bool GrClipStackClip::apply(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400201 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
202 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400203 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
204 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700205 return false;
206 }
207
Brian Salomon510dd422017-03-16 12:15:22 -0400208 if (!fStack || fStack->isWideOpen()) {
209 return true;
210 }
211
Brian Osman5f5680c2019-06-05 10:17:25 -0400212 // An default count of 4 was chosen because of the common pattern in Blink of:
213 // isect RR
214 // diff RR
215 // isect convex_poly
216 // isect convex_poly
217 // when drawing rounded div borders.
218 constexpr int kMaxAnalyticFPs = 4;
219
Chris Daltona32a3c32017-12-05 10:05:21 -0700220 int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
Brian Osman5f5680c2019-06-05 10:17:25 -0400221 int maxAnalyticFPs = kMaxAnalyticFPs;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600222 if (renderTargetContext->numSamples() > 1 || useHWAA || hasUserStencilSettings) {
223 // Disable analytic clips when we have MSAA. In MSAA we never conflate coverage and opacity.
224 maxAnalyticFPs = 0;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400225 // We disable MSAA when avoiding stencil.
Robert Phillips9da87e02019-02-04 13:26:26 -0500226 SkASSERT(!context->priv().caps()->avoidStencilBuffers());
Chris Dalton584a79a2017-11-15 13:14:01 -0700227 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500228 auto* ccpr = context->priv().drawingManager()->getCoverageCountingPathRenderer();
Chris Dalton584a79a2017-11-15 13:14:01 -0700229
Robert Phillips9da87e02019-02-04 13:26:26 -0500230 GrReducedClip reducedClip(*fStack, devBounds, context->priv().caps(),
Ethan Nicholaseace9352018-10-15 20:09:54 +0000231 maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0);
Chris Daltona32a3c32017-12-05 10:05:21 -0700232 if (InitialState::kAllOut == reducedClip.initialState() &&
233 reducedClip.maskElements().isEmpty()) {
234 return false;
235 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000236
Chris Dalton79471932017-10-27 01:50:57 -0600237 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700238 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700239 }
cdalton93a379b2016-05-11 13:58:08 -0700240
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700241 if (!reducedClip.windowRectangles().empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700242 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
243 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700244 }
245
Chris Daltona32a3c32017-12-05 10:05:21 -0700246 if (!reducedClip.maskElements().isEmpty()) {
247 if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
248 out)) {
249 return false;
250 }
251 }
252
Greg Danielf41b2bd2019-08-22 16:19:24 -0400253 // The opsTask ID must not be looked up until AFTER producing the clip mask (if any). That step
254 // can cause a flush or otherwise change which opstask our draw is going into.
255 uint32_t opsTaskID = renderTargetContext->getOpsTask()->uniqueID();
256 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(ccpr, opsTaskID)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700257 out->addCoverageFP(std::move(clipFPs));
258 }
259
Chris Daltona32a3c32017-12-05 10:05:21 -0700260 return true;
261}
csmartdaltond211e782016-08-15 11:17:19 -0700262
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500263bool GrClipStackClip::applyClipMask(GrRecordingContext* context,
264 GrRenderTargetContext* renderTargetContext,
Chris Daltona32a3c32017-12-05 10:05:21 -0700265 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
266 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700267#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600268 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500269 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
270 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600271 const SkIRect& scissor = reducedClip.scissor();
272 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700273#endif
csmartdaltond211e782016-08-15 11:17:19 -0700274
Chris Dalton6ce447a2019-06-23 18:07:38 -0600275 // MIXED SAMPLES TODO: We may want to explore using the stencil buffer for AA clipping.
276 if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500277 context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500278 renderTargetContext->wrapsVkSecondaryCB()) {
Greg Daniele32506b2020-02-10 16:00:54 -0500279 GrSurfaceProxyView result;
Chris Daltonc5348082018-03-30 15:59:38 +0000280 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
281 // The clip geometry is complex enough that it will be more efficient to create it
282 // entirely in software
283 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
284 } else {
285 result = this->createAlphaClipMask(context, reducedClip);
286 }
287
288 if (result) {
289 // The mask's top left coord should be pinned to the rounded-out top left corner of
290 // the clip's device space bounds.
Brian Salomon64227222020-02-26 13:28:42 -0500291 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor(),
292 *context->priv().caps()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000293 return true;
294 }
Eric Karl5c779752017-05-08 12:02:07 -0700295
Chris Daltonc5348082018-03-30 15:59:38 +0000296 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
297 // unless stencils are disallowed.
Robert Phillips9da87e02019-02-04 13:26:26 -0500298 if (context->priv().caps()->avoidStencilBuffers() ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500299 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700300 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
301 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700302 return false;
303 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000304 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000305
Michael Ludwig828d3412020-05-12 13:15:35 -0400306 reducedClip.drawStencilClipMask(context, renderTargetContext);
Brian Salomonc3833b42018-07-09 18:23:58 +0000307 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000308 return true;
309}
310
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000311////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700312// Create a 8-bit clip mask in alpha
313
Brian Salomonc3833b42018-07-09 18:23:58 +0000314static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
315 GrUniqueKey* key) {
316 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
317 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
318 builder[0] = clipGenID;
319 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
320 // sometimes result in negative coordinates from device space.
321 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
322 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
323 builder[3] = numAnalyticFPs;
324}
325
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500326static void add_invalidate_on_pop_message(GrRecordingContext* context,
Robert Phillips427966a2018-12-20 17:20:43 -0500327 const SkClipStack& stack, uint32_t clipGenID,
328 const GrUniqueKey& clipMaskKey) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500329 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips427966a2018-12-20 17:20:43 -0500330
Brian Salomon19f0ed52017-01-06 13:54:58 -0500331 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
332 while (const Element* element = iter.prev()) {
333 if (element->getGenID() == clipGenID) {
Robert Phillips427966a2018-12-20 17:20:43 -0500334 element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500335 return;
336 }
337 }
338 SkDEBUGFAIL("Gen ID was not found in stack.");
339}
340
Brian Salomond005b692020-04-01 15:47:05 -0400341static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
342
343static GrSurfaceProxyView find_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
Brian Salomon0029db02020-04-03 10:41:24 -0400344 return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8,
345 1);
Brian Salomond005b692020-04-01 15:47:05 -0400346}
347
Greg Daniele32506b2020-02-10 16:00:54 -0500348GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
349 const GrReducedClip& reducedClip) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500350 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc3833b42018-07-09 18:23:58 +0000351 GrUniqueKey key;
352 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
353 reducedClip.numAnalyticFPs(), &key);
354
Brian Salomond005b692020-04-01 15:47:05 -0400355 if (auto cachedView = find_mask(context->priv().proxyProvider(), key)) {
356 return cachedView;
Chris Daltonc5348082018-03-30 15:59:38 +0000357 }
358
Greg Daniele20fcad2020-01-08 11:52:34 -0500359 auto rtc = GrRenderTargetContext::MakeWithFallback(
360 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox,
Greg Daniele32506b2020-02-10 16:00:54 -0500361 {reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomond005b692020-04-01 15:47:05 -0400362 kMaskOrigin);
Chris Daltonc5348082018-03-30 15:59:38 +0000363 if (!rtc) {
Greg Daniele32506b2020-02-10 16:00:54 -0500364 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000365 }
366
367 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
Greg Daniele32506b2020-02-10 16:00:54 -0500368 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000369 }
370
Greg Daniele32506b2020-02-10 16:00:54 -0500371 GrSurfaceProxyView result = rtc->readSurfaceView();
372 if (!result || !result.asTextureProxy()) {
373 return {};
Chris Daltonc5348082018-03-30 15:59:38 +0000374 }
375
Brian Salomond005b692020-04-01 15:47:05 -0400376 SkASSERT(result.origin() == kMaskOrigin);
Greg Daniele32506b2020-02-10 16:00:54 -0500377 proxyProvider->assignUniqueKeyToProxy(key, result.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500378 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Chris Daltonc5348082018-03-30 15:59:38 +0000379
380 return result;
381}
382
Brian Osman5d034742017-09-11 13:38:55 -0400383namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500384
Brian Osman5d034742017-09-11 13:38:55 -0400385/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400386 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400387 * elements, each storing their own AA setting (and already transformed into device space). This
388 * stores all of the information needed by the worker thread to draw all clip elements (see below,
389 * in createSoftwareClipMask).
390 */
391class ClipMaskData {
392public:
393 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600394 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400395 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600396 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400397 fElements.addToTail(*iter.get());
398 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000399 }
400
Chris Dalton79471932017-10-27 01:50:57 -0600401 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400402 InitialState initialState() const { return fInitialState; }
403 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000404
Brian Osman5d034742017-09-11 13:38:55 -0400405private:
Chris Dalton79471932017-10-27 01:50:57 -0600406 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400407 InitialState fInitialState;
408 ElementList fElements;
409};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000410
Brian Osman5d034742017-09-11 13:38:55 -0400411}
412
413static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600414 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400415 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800416 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600417 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800418
Brian Osman5d034742017-09-11 13:38:55 -0400419 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000420
Brian Osman5d034742017-09-11 13:38:55 -0400421 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000422 const Element* element = iter.get();
423 SkClipOp op = element->getOp();
424 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000425
Mike Reedc1f77742016-12-09 09:00:50 -0500426 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 // Intersect and reverse difference require modifying pixels outside of the geometry
428 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
429 // but leave the pixels inside the geometry alone. For reverse difference we invert all
430 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500431 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600432 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000433 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400434 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000435 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000436 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000437 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000438 clipPath.toggleInverseFillType();
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400439 helper.drawShape(GrShape(clipPath), translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000440 continue;
441 }
442
443 // The other ops (union, xor, diff) only affect pixels inside
444 // the geometry so they can just be drawn normally
Brian Salomonc3833b42018-07-09 18:23:58 +0000445 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
446 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400447 } else if (Element::DeviceSpaceType::kRRect == element->getDeviceSpaceType()) {
448 helper.drawRRect(element->getDeviceSpaceRRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000449 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000450 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000451 element->asDeviceSpacePath(&path);
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400452 helper.drawShape(GrShape(path), translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000453 }
454 }
Brian Osman5d034742017-09-11 13:38:55 -0400455}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000456
Greg Daniele32506b2020-02-10 16:00:54 -0500457GrSurfaceProxyView GrClipStackClip::createSoftwareClipMask(
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500458 GrRecordingContext* context, const GrReducedClip& reducedClip,
Brian Osman5d034742017-09-11 13:38:55 -0400459 GrRenderTargetContext* renderTargetContext) const {
Brian Salomonc3833b42018-07-09 18:23:58 +0000460 GrUniqueKey key;
461 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
462 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800463
Robert Phillips9da87e02019-02-04 13:26:26 -0500464 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500465
Brian Salomond005b692020-04-01 15:47:05 -0400466 if (auto cachedView = find_mask(proxyProvider, key)) {
467 return cachedView;
Brian Osman5d034742017-09-11 13:38:55 -0400468 }
469
470 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
471 // left corner of the resulting rect to the top left of the texture.
472 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
473
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500474 SkTaskGroup* taskGroup = nullptr;
475 if (auto direct = context->priv().asDirectContext()) {
476 taskGroup = direct->priv().getTaskGroup();
477 }
478
Greg Daniele32506b2020-02-10 16:00:54 -0500479 GrSurfaceProxyView view;
Brian Osman5d034742017-09-11 13:38:55 -0400480 if (taskGroup && renderTargetContext) {
Brian Salomond005b692020-04-01 15:47:05 -0400481 const GrCaps* caps = context->priv().caps();
Brian Osman5d034742017-09-11 13:38:55 -0400482 // Create our texture proxy
Robert Phillips0a15cc62019-07-30 12:49:10 -0400483 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
484 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500485
Greg Daniel47c20e82020-01-21 14:29:57 -0500486 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kAlpha_8);
487
Brian Osmand140fe92017-10-03 12:17:26 -0400488 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
489 // to ops), so it can't have any pending IO.
Greg Daniele32506b2020-02-10 16:00:54 -0500490 auto proxy = proxyProvider->createProxy(format,
491 maskSpaceIBounds.size(),
Greg Daniele32506b2020-02-10 16:00:54 -0500492 GrRenderable::kNo,
493 1,
Greg Daniele32506b2020-02-10 16:00:54 -0500494 GrMipMapped::kNo,
495 SkBackingFit::kApprox,
496 SkBudgeted::kYes,
497 GrProtected::kNo);
Brian Osman5d034742017-09-11 13:38:55 -0400498
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500499 auto uploader = std::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
Brian Osman099fa0f2017-10-02 16:38:32 -0400500 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400501 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
Brian Salomon5f394272019-07-02 14:07:49 -0400502 TRACE_EVENT0("skia.gpu", "Threaded SW Clip Mask Render");
Brian Osman5d034742017-09-11 13:38:55 -0400503 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));
Greg Daniele32506b2020-02-10 16:00:54 -0500516
Brian Salomond005b692020-04-01 15:47:05 -0400517 view = {std::move(proxy), kMaskOrigin, swizzle};
Brian Osman5d034742017-09-11 13:38:55 -0400518 } else {
519 GrSWMaskHelper helper;
520 if (!helper.init(maskSpaceIBounds)) {
Greg Daniele32506b2020-02-10 16:00:54 -0500521 return {};
Brian Osman5d034742017-09-11 13:38:55 -0400522 }
523
Chris Dalton79471932017-10-27 01:50:57 -0600524 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400525 reducedClip.initialState());
526
Greg Daniele32506b2020-02-10 16:00:54 -0500527 view = helper.toTextureView(context, SkBackingFit::kApprox);
Brian Osman5d034742017-09-11 13:38:55 -0400528 }
529
Greg Daniele32506b2020-02-10 16:00:54 -0500530 SkASSERT(view);
Brian Salomond005b692020-04-01 15:47:05 -0400531 SkASSERT(view.origin() == kMaskOrigin);
Greg Daniele32506b2020-02-10 16:00:54 -0500532 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
Robert Phillips427966a2018-12-20 17:20:43 -0500533 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Greg Daniele32506b2020-02-10 16:00:54 -0500534 return view;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000535}