blob: 7aadaa45dc99f46c2cc4c9f49b33a03aff134b7f [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
csmartdaltonc6f411e2016-08-05 22:32:12 -07008#include "GrClipStackClip.h"
csmartdalton28341fa2016-08-17 10:00:21 -07009#include "GrAppliedClip.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070010#include "GrContextPriv.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040011#include "GrDeferredProxyUploader.h"
robertphillips68737822015-10-29 12:12:21 -070012#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "GrFixedClip.h"
bsalomon473addf2015-10-02 07:49:05 -070014#include "GrGpuResourcePriv.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040015#include "GrProxyProvider.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040016#include "GrRenderTargetContextPriv.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040017#include "GrSWMaskHelper.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040018#include "GrShape.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040019#include "GrStencilAttachment.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040020#include "GrStyle.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050021#include "GrTextureProxy.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040022#include "SkClipOpPriv.h"
23#include "SkMakeUnique.h"
24#include "SkTaskGroup.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040025#include "SkTo.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040026#include "SkTraceEvent.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040027#include "effects/GrConvexPolyEffect.h"
28#include "effects/GrRRectEffect.h"
29#include "effects/GrTextureDomain.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).
90bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
91 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
116 GrPathRendererChain::DrawType type =
117 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
118 : GrPathRendererChain::DrawType::kColor;
119
120 GrShape shape(path, GrStyle::SimpleFill());
121 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400122 canDrawArgs.fCaps = context->contextPriv().caps();
Chris Daltonc5348082018-03-30 15:59:38 +0000123 canDrawArgs.fClipConservativeBounds = &scissorRect;
124 canDrawArgs.fViewMatrix = &viewMatrix;
125 canDrawArgs.fShape = &shape;
Brian Salomonc3833b42018-07-09 18:23:58 +0000126 canDrawArgs.fAAType = GrChooseAAType(GrAA(element->isAA()),
Chris Daltonc5348082018-03-30 15:59:38 +0000127 renderTargetContext->fsaaType(),
128 GrAllowMixedSamples::kYes,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400129 *context->contextPriv().caps());
Greg Danielbe7fc462019-01-03 16:40:42 -0500130 SkASSERT(!renderTargetContext->wrapsVkSecondaryCB());
131 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
Chris Daltonc5348082018-03-30 15:59:38 +0000132 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
133
134 // the 'false' parameter disallows use of the SW path renderer
135 GrPathRenderer* pr =
136 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
137 if (prOut) {
138 *prOut = pr;
139 }
140 return SkToBool(!pr);
141 }
142}
143
144/*
145 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
146 * will be used on any element. If so, it returns true to indicate that the
147 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
148 */
149bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
150 bool hasUserStencilSettings,
151 const GrRenderTargetContext* renderTargetContext,
152 const GrReducedClip& reducedClip) {
Chris Daltond22a0232018-08-22 17:25:10 +0000153 // TODO: right now it appears that GPU clip masks are strictly slower than software. We may
154 // want to revisit this assumption once we can test with render target sorting.
155 return true;
156
Chris Daltonc5348082018-03-30 15:59:38 +0000157 // TODO: generalize this function so that when
158 // a clip gets complex enough it can just be done in SW regardless
159 // of whether it would invoke the GrSoftwarePathRenderer.
160
Greg Danielbe7fc462019-01-03 16:40:42 -0500161 // If we're avoiding stencils, always use SW. This includes drawing into a wrapped vulkan
162 // secondary command buffer which can't handle stencils.
163 if (context->contextPriv().caps()->avoidStencilBuffers() ||
164 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000165 return true;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400166 }
Chris Daltonc5348082018-03-30 15:59:38 +0000167
168 // Set the matrix so that rendered clip elements are transformed to mask space from clip
169 // space.
170 SkMatrix translate;
171 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
172
173 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000174 const Element* element = iter.get();
Chris Daltonc5348082018-03-30 15:59:38 +0000175
Brian Salomonc3833b42018-07-09 18:23:58 +0000176 SkClipOp op = element->getOp();
177 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000178 bool needsStencil = invert ||
179 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
180
181 if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings,
182 renderTargetContext, translate, element, nullptr, needsStencil)) {
183 return true;
184 }
185 }
186 return false;
187}
188
189////////////////////////////////////////////////////////////////////////////////
190// sort out what kind of clip mask needs to be created: alpha, stencil,
191// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400192bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400193 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
194 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400195 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
196 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700197 return false;
198 }
199
Brian Salomon510dd422017-03-16 12:15:22 -0400200 if (!fStack || fStack->isWideOpen()) {
201 return true;
202 }
203
Chris Daltona32a3c32017-12-05 10:05:21 -0700204 int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400205 int maxAnalyticFPs = context->contextPriv().caps()->maxClipAnalyticFPs();
Chris Dalton584a79a2017-11-15 13:14:01 -0700206 if (GrFSAAType::kNone != renderTargetContext->fsaaType()) {
207 // With mixed samples (non-msaa color buffer), any coverage info is lost from color once it
208 // hits the color buffer anyway, so we may as well use coverage AA if nothing else in the
209 // pipe is multisampled.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500210 if (renderTargetContext->numColorSamples() > 1 || useHWAA || hasUserStencilSettings) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700211 maxAnalyticFPs = 0;
212 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400213 // We disable MSAA when avoiding stencil.
214 SkASSERT(!context->contextPriv().caps()->avoidStencilBuffers());
Chris Dalton584a79a2017-11-15 13:14:01 -0700215 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700216 auto* ccpr = context->contextPriv().drawingManager()->getCoverageCountingPathRenderer();
Chris Dalton584a79a2017-11-15 13:14:01 -0700217
Ethan Nicholaseace9352018-10-15 20:09:54 +0000218 GrReducedClip reducedClip(*fStack, devBounds, context->contextPriv().caps(),
219 maxWindowRectangles, maxAnalyticFPs, ccpr ? maxAnalyticFPs : 0);
Chris Daltona32a3c32017-12-05 10:05:21 -0700220 if (InitialState::kAllOut == reducedClip.initialState() &&
221 reducedClip.maskElements().isEmpty()) {
222 return false;
223 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224
Chris Dalton79471932017-10-27 01:50:57 -0600225 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700226 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700227 }
cdalton93a379b2016-05-11 13:58:08 -0700228
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700229 if (!reducedClip.windowRectangles().empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700230 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
231 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700232 }
233
Chris Daltona32a3c32017-12-05 10:05:21 -0700234 if (!reducedClip.maskElements().isEmpty()) {
235 if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
236 out)) {
237 return false;
238 }
239 }
240
241 // The opList ID must not be looked up until AFTER producing the clip mask (if any). That step
242 // can cause a flush or otherwise change which opList our draw is going into.
243 uint32_t opListID = renderTargetContext->getOpList()->uniqueID();
244 int rtWidth = renderTargetContext->width(), rtHeight = renderTargetContext->height();
Chris Dalton4c458b12018-06-16 17:22:59 -0600245 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(ccpr, opListID, rtWidth, rtHeight)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700246 out->addCoverageFP(std::move(clipFPs));
247 }
248
Chris Daltona32a3c32017-12-05 10:05:21 -0700249 return true;
250}
csmartdaltond211e782016-08-15 11:17:19 -0700251
Chris Daltona32a3c32017-12-05 10:05:21 -0700252bool GrClipStackClip::applyClipMask(GrContext* context, GrRenderTargetContext* renderTargetContext,
253 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
254 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700255#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600256 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500257 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
258 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600259 const SkIRect& scissor = reducedClip.scissor();
260 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700261#endif
csmartdaltond211e782016-08-15 11:17:19 -0700262
cdaltonede75742015-11-11 15:27:57 -0800263 // If the stencil buffer is multisampled we can use it to do everything.
Chris Dalton79471932017-10-27 01:50:57 -0600264 if ((GrFSAAType::kNone == renderTargetContext->fsaaType() && reducedClip.maskRequiresAA()) ||
Greg Danielbe7fc462019-01-03 16:40:42 -0500265 context->contextPriv().caps()->avoidStencilBuffers() ||
266 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000267 sk_sp<GrTextureProxy> result;
268 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
269 // The clip geometry is complex enough that it will be more efficient to create it
270 // entirely in software
271 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
272 } else {
273 result = this->createAlphaClipMask(context, reducedClip);
274 }
275
276 if (result) {
277 // The mask's top left coord should be pinned to the rounded-out top left corner of
278 // the clip's device space bounds.
279 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000280 return true;
281 }
Eric Karl5c779752017-05-08 12:02:07 -0700282
Chris Daltonc5348082018-03-30 15:59:38 +0000283 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
284 // unless stencils are disallowed.
Greg Danielbe7fc462019-01-03 16:40:42 -0500285 if (context->contextPriv().caps()->avoidStencilBuffers() ||
286 renderTargetContext->wrapsVkSecondaryCB()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700287 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
288 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700289 return false;
290 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000291 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000292
Robert Phillips65048132017-08-10 08:44:49 -0400293 renderTargetContext->setNeedsStencil();
csmartdaltonbde96c62016-08-31 12:54:46 -0700294
Brian Salomonc3833b42018-07-09 18:23:58 +0000295 // This relies on the property that a reduced sub-rect of the last clip will contain all the
296 // relevant window rectangles that were in the last clip. This subtle requirement will go away
297 // after clipping is overhauled.
298 if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(), reducedClip.scissor(),
299 reducedClip.numAnalyticFPs())) {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000300 reducedClip.drawStencilClipMask(context, renderTargetContext);
Brian Salomonc3833b42018-07-09 18:23:58 +0000301 renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor(),
302 reducedClip.numAnalyticFPs());
csmartdaltonbde96c62016-08-31 12:54:46 -0700303 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000304 // GrAppliedClip doesn't need to figure numAnalyticFPs into its key (used by operator==) because
305 // it verifies the FPs are also equal.
306 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000307 return true;
308}
309
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000310////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700311// Create a 8-bit clip mask in alpha
312
Brian Salomonc3833b42018-07-09 18:23:58 +0000313static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
314 GrUniqueKey* key) {
315 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
316 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
317 builder[0] = clipGenID;
318 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
319 // sometimes result in negative coordinates from device space.
320 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
321 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
322 builder[3] = numAnalyticFPs;
323}
324
Robert Phillips427966a2018-12-20 17:20:43 -0500325static void add_invalidate_on_pop_message(GrContext* context,
326 const SkClipStack& stack, uint32_t clipGenID,
327 const GrUniqueKey& clipMaskKey) {
328 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
329
Brian Salomon19f0ed52017-01-06 13:54:58 -0500330 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
331 while (const Element* element = iter.prev()) {
332 if (element->getGenID() == clipGenID) {
Robert Phillips427966a2018-12-20 17:20:43 -0500333 element->addResourceInvalidationMessage(proxyProvider, clipMaskKey);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500334 return;
335 }
336 }
337 SkDEBUGFAIL("Gen ID was not found in stack.");
338}
339
Chris Daltonc5348082018-03-30 15:59:38 +0000340sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrContext* context,
341 const GrReducedClip& reducedClip) const {
342 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomonc3833b42018-07-09 18:23:58 +0000343 GrUniqueKey key;
344 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
345 reducedClip.numAnalyticFPs(), &key);
346
Chris Daltonc5348082018-03-30 15:59:38 +0000347 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Robert Phillips5491e822018-06-27 12:54:44 -0400348 key, kTopLeft_GrSurfaceOrigin));
Chris Daltonc5348082018-03-30 15:59:38 +0000349 if (proxy) {
350 return proxy;
351 }
352
Greg Daniel4065d452018-11-16 15:43:41 -0500353 GrBackendFormat format =
354 context->contextPriv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
Chris Daltonc5348082018-03-30 15:59:38 +0000355 sk_sp<GrRenderTargetContext> rtc(
Robert Phillips5491e822018-06-27 12:54:44 -0400356 context->contextPriv().makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500357 format,
Robert Phillips5491e822018-06-27 12:54:44 -0400358 SkBackingFit::kApprox,
359 reducedClip.width(),
360 reducedClip.height(),
361 kAlpha_8_GrPixelConfig,
362 nullptr, 1,
363 GrMipMapped::kNo,
364 kTopLeft_GrSurfaceOrigin));
Chris Daltonc5348082018-03-30 15:59:38 +0000365 if (!rtc) {
366 return nullptr;
367 }
368
369 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
370 return nullptr;
371 }
372
373 sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
374 if (!result) {
375 return nullptr;
376 }
377
Robert Phillips5491e822018-06-27 12:54:44 -0400378 SkASSERT(result->origin() == kTopLeft_GrSurfaceOrigin);
Chris Daltonc5348082018-03-30 15:59:38 +0000379 proxyProvider->assignUniqueKeyToProxy(key, result.get());
Robert Phillips427966a2018-12-20 17:20:43 -0500380 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Chris Daltonc5348082018-03-30 15:59:38 +0000381
382 return result;
383}
384
Brian Osman5d034742017-09-11 13:38:55 -0400385namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500386
Brian Osman5d034742017-09-11 13:38:55 -0400387/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400388 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400389 * elements, each storing their own AA setting (and already transformed into device space). This
390 * stores all of the information needed by the worker thread to draw all clip elements (see below,
391 * in createSoftwareClipMask).
392 */
393class ClipMaskData {
394public:
395 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600396 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400397 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600398 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400399 fElements.addToTail(*iter.get());
400 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000401 }
402
Chris Dalton79471932017-10-27 01:50:57 -0600403 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400404 InitialState initialState() const { return fInitialState; }
405 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000406
Brian Osman5d034742017-09-11 13:38:55 -0400407private:
Chris Dalton79471932017-10-27 01:50:57 -0600408 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400409 InitialState fInitialState;
410 ElementList fElements;
411};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000412
Brian Osman5d034742017-09-11 13:38:55 -0400413}
414
415static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600416 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400417 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800418 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600419 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800420
Brian Osman5d034742017-09-11 13:38:55 -0400421 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000422
Brian Osman5d034742017-09-11 13:38:55 -0400423 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000424 const Element* element = iter.get();
425 SkClipOp op = element->getOp();
426 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000427
Mike Reedc1f77742016-12-09 09:00:50 -0500428 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000429 // Intersect and reverse difference require modifying pixels outside of the geometry
430 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
431 // but leave the pixels inside the geometry alone. For reverse difference we invert all
432 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500433 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600434 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000435 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400436 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000437 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000438 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000439 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000440 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700441 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400442 helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000443 continue;
444 }
445
446 // The other ops (union, xor, diff) only affect pixels inside
447 // the geometry so they can just be drawn normally
Brian Salomonc3833b42018-07-09 18:23:58 +0000448 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
449 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000450 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000451 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000452 element->asDeviceSpacePath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700453 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400454 helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000455 }
456 }
Brian Osman5d034742017-09-11 13:38:55 -0400457}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000458
Brian Osman5d034742017-09-11 13:38:55 -0400459sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
460 GrContext* context, const GrReducedClip& reducedClip,
461 GrRenderTargetContext* renderTargetContext) const {
Brian Salomonc3833b42018-07-09 18:23:58 +0000462 GrUniqueKey key;
463 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
464 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800465
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500466 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
467
468 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Brian Osman5d034742017-09-11 13:38:55 -0400469 key, kTopLeft_GrSurfaceOrigin));
470 if (proxy) {
471 return proxy;
472 }
473
474 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
475 // left corner of the resulting rect to the top left of the texture.
476 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
477
478 SkTaskGroup* taskGroup = context->contextPriv().getTaskGroup();
479 if (taskGroup && renderTargetContext) {
480 // Create our texture proxy
481 GrSurfaceDesc desc;
Brian Osman5d034742017-09-11 13:38:55 -0400482 desc.fWidth = maskSpaceIBounds.width();
483 desc.fHeight = maskSpaceIBounds.height();
484 desc.fConfig = kAlpha_8_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -0500485
486 GrBackendFormat format =
487 context->contextPriv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
488
Brian Osmand140fe92017-10-03 12:17:26 -0400489 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
490 // to ops), so it can't have any pending IO.
Greg Daniel4065d452018-11-16 15:43:41 -0500491 proxy = proxyProvider->createProxy(format, desc, kTopLeft_GrSurfaceOrigin,
492 SkBackingFit::kApprox, SkBudgeted::kYes,
493 GrInternalSurfaceFlags::kNoPendingIO);
Brian Osman5d034742017-09-11 13:38:55 -0400494
Brian Osman099fa0f2017-10-02 16:38:32 -0400495 auto uploader = skstd::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
496 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400497 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
498 TRACE_EVENT0("skia", "Threaded SW Clip Mask Render");
499 GrSWMaskHelper helper(uploaderRaw->getPixels());
500 if (helper.init(maskSpaceIBounds)) {
501 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
Chris Dalton79471932017-10-27 01:50:57 -0600502 uploaderRaw->data().scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400503 uploaderRaw->data().initialState());
504 } else {
505 SkDEBUGFAIL("Unable to allocate SW clip mask.");
506 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400507 uploaderRaw->signalAndFreeData();
Brian Osman5d034742017-09-11 13:38:55 -0400508 };
509
510 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400511 proxy->texPriv().setDeferredUploader(std::move(uploader));
Brian Osman5d034742017-09-11 13:38:55 -0400512 } else {
513 GrSWMaskHelper helper;
514 if (!helper.init(maskSpaceIBounds)) {
515 return nullptr;
516 }
517
Chris Dalton79471932017-10-27 01:50:57 -0600518 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400519 reducedClip.initialState());
520
521 proxy = helper.toTextureProxy(context, SkBackingFit::kApprox);
522 }
523
524 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500525 proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
Robert Phillips427966a2018-12-20 17:20:43 -0500526 add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
Brian Osman5d034742017-09-11 13:38:55 -0400527 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000528}