blob: 24c813e6540fae03d5a4c529ac42ee76f4adfb55 [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"
9
csmartdalton28341fa2016-08-17 10:00:21 -070010#include "GrAppliedClip.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070011#include "GrContextPriv.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040012#include "GrDeferredProxyUploader.h"
robertphillips68737822015-10-29 12:12:21 -070013#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070014#include "GrFixedClip.h"
bsalomon473addf2015-10-02 07:49:05 -070015#include "GrGpuResourcePriv.h"
Chris Daltona32a3c32017-12-05 10:05:21 -070016#include "GrRenderTargetContextPriv.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050017#include "GrProxyProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050020#include "GrTextureProxy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080021#include "effects/GrConvexPolyEffect.h"
22#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080023#include "effects/GrTextureDomain.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050024#include "SkClipOpPriv.h"
Brian Osman5d034742017-09-11 13:38:55 -040025#include "SkMakeUnique.h"
26#include "SkTaskGroup.h"
27#include "SkTraceEvent.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000028
bsalomon@google.com8182fa02012-12-04 14:06:06 +000029typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070030typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070031typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000032
Brian Salomon19f0ed52017-01-06 13:54:58 -050033const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070034
csmartdaltonc6f411e2016-08-05 22:32:12 -070035bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070036 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070037 return true;
38 }
Brian Salomon9a767722017-03-13 17:57:28 -040039 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070040}
41
bsalomon7f0d9f32016-08-15 14:49:10 -070042bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070043 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070044 return true;
45 }
Brian Salomon9a767722017-03-13 17:57:28 -040046 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070047}
48
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050049bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070050 if (!fStack) {
51 return false;
52 }
53 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050054 bool isAA;
55 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
Chris Dalton3b51df12017-11-27 14:33:06 -070056 *aa = GrAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070057 return true;
58 }
59 return false;
60}
61
csmartdaltonc6f411e2016-08-05 22:32:12 -070062void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
63 bool* isIntersectionOfRects) const {
64 if (!fStack) {
65 devResult->setXYWH(0, 0, width, height);
66 if (isIntersectionOfRects) {
67 *isIntersectionOfRects = true;
68 }
69 return;
70 }
71 SkRect devBounds;
Brian Salomon9a767722017-03-13 17:57:28 -040072 fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
csmartdaltonc6f411e2016-08-05 22:32:12 -070073 devBounds.roundOut(devResult);
74}
75
bsalomon@google.com51a62862012-11-26 21:19:43 +000076////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040077// set up the draw state to enable the aa clipping mask.
Brian Salomonaff329b2017-08-11 09:40:37 -040078static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(sk_sp<GrTextureProxy> mask,
79 const SkIRect& devBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000080 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040081 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(mask), domainTexels,
Brian Salomon2ebd0c82016-10-03 17:15:28 -040082 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000083}
84
robertphillips3f7357f2015-10-27 07:17:33 -070085// Does the path in 'element' require SW rendering? If so, return true (and,
86// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
87// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070088bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
Chris Daltondb91c6e2017-09-08 16:25:08 -060089 const SkIRect& scissorRect,
csmartdaltonc6f411e2016-08-05 22:32:12 -070090 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -040091 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -070092 const SkMatrix& viewMatrix,
93 const Element* element,
94 GrPathRenderer** prOut,
95 bool needsStencil) {
Brian Salomonf3b46e52017-08-30 11:37:57 -040096 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
robertphillips3f7357f2015-10-27 07:17:33 -070097 // rects can always be drawn directly w/o using the software path
98 // TODO: skip rrects once we're drawing them directly.
99 if (prOut) {
100 *prOut = nullptr;
101 }
102 return false;
103 } else {
104 // We shouldn't get here with an empty clip element.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400105 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700106
robertphillips3f7357f2015-10-27 07:17:33 -0700107 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
108 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400109 element->asDeviceSpacePath(&path);
robertphillips3f7357f2015-10-27 07:17:33 -0700110 if (path.isInverseFillType()) {
111 path.toggleInverseFillType();
112 }
halcanary9d524f22016-03-29 09:03:52 -0700113
Brian Salomon82125e92016-12-10 09:35:48 -0500114 GrPathRendererChain::DrawType type =
115 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
116 : GrPathRendererChain::DrawType::kColor;
halcanary9d524f22016-03-29 09:03:52 -0700117
bsalomon8acedde2016-06-24 10:42:16 -0700118 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700119 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700120 canDrawArgs.fCaps = context->caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600121 canDrawArgs.fClipConservativeBounds = &scissorRect;
robertphillips68737822015-10-29 12:12:21 -0700122 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700123 canDrawArgs.fShape = &shape;
Chris Dalton3b51df12017-11-27 14:33:06 -0700124 canDrawArgs.fAAType = GrChooseAAType(GrAA(element->isAA()),
Brian Salomon7c8460e2017-05-12 11:36:10 -0400125 renderTargetContext->fsaaType(),
Brian Salomone225b562017-06-14 13:00:03 -0400126 GrAllowMixedSamples::kYes,
127 *context->caps());
cdalton93a379b2016-05-11 13:58:08 -0700128 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700129
robertphillips3f7357f2015-10-27 07:17:33 -0700130 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700131 GrPathRenderer* pr =
132 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700133 if (prOut) {
134 *prOut = pr;
135 }
136 return SkToBool(!pr);
137 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000138}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000139
robertphillips@google.comfa662942012-05-17 12:20:22 +0000140/*
141 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
142 * will be used on any element. If so, it returns true to indicate that the
143 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
144 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700145bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
146 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400147 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700148 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000149 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000150 // a clip gets complex enough it can just be done in SW regardless
151 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000152
Eric Karl5c779752017-05-08 12:02:07 -0700153 // If we're avoiding stencils, always use SW:
154 if (context->caps()->avoidStencilBuffers())
155 return true;
156
joshualitt8059eb92014-12-29 15:10:07 -0800157 // Set the matrix so that rendered clip elements are transformed to mask space from clip
158 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700159 SkMatrix translate;
160 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800161
Chris Dalton79471932017-10-27 01:50:57 -0600162 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000163 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700164
Mike Reedc1f77742016-12-09 09:00:50 -0500165 SkClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700166 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700167 bool needsStencil = invert ||
Mike Reedc1f77742016-12-09 09:00:50 -0500168 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700169
Chris Dalton79471932017-10-27 01:50:57 -0600170 if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400171 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700172 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000173 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000174 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000175 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000176}
177
robertphillips@google.comf294b772012-04-27 14:29:26 +0000178////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000179// sort out what kind of clip mask needs to be created: alpha, stencil,
180// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400181bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400182 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
183 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400184 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
185 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700186 return false;
187 }
188
Brian Salomon510dd422017-03-16 12:15:22 -0400189 if (!fStack || fStack->isWideOpen()) {
190 return true;
191 }
192
Robert Phillips777707b2018-01-17 11:40:14 -0500193 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Chris Daltona32a3c32017-12-05 10:05:21 -0700194 const auto* caps = context->caps()->shaderCaps();
195 int maxWindowRectangles = renderTargetContext->priv().maxWindowRectangles();
Chris Dalton584a79a2017-11-15 13:14:01 -0700196 int maxAnalyticFPs = context->caps()->maxClipAnalyticFPs();
197 if (GrFSAAType::kNone != renderTargetContext->fsaaType()) {
198 // With mixed samples (non-msaa color buffer), any coverage info is lost from color once it
199 // hits the color buffer anyway, so we may as well use coverage AA if nothing else in the
200 // pipe is multisampled.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500201 if (renderTargetContext->numColorSamples() > 1 || useHWAA || hasUserStencilSettings) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700202 maxAnalyticFPs = 0;
203 }
204 SkASSERT(!context->caps()->avoidStencilBuffers()); // We disable MSAA when avoiding stencil.
205 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700206 auto* ccpr = context->contextPriv().drawingManager()->getCoverageCountingPathRenderer();
Chris Dalton584a79a2017-11-15 13:14:01 -0700207
Chris Daltona32a3c32017-12-05 10:05:21 -0700208 GrReducedClip reducedClip(*fStack, devBounds, caps, maxWindowRectangles, maxAnalyticFPs, ccpr);
209 if (InitialState::kAllOut == reducedClip.initialState() &&
210 reducedClip.maskElements().isEmpty()) {
211 return false;
212 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000213
Chris Dalton79471932017-10-27 01:50:57 -0600214 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700215 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700216 }
cdalton93a379b2016-05-11 13:58:08 -0700217
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700218 if (!reducedClip.windowRectangles().empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700219 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
220 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700221 }
222
Chris Daltona32a3c32017-12-05 10:05:21 -0700223 if (!reducedClip.maskElements().isEmpty()) {
224 if (!this->applyClipMask(context, renderTargetContext, reducedClip, hasUserStencilSettings,
225 out)) {
226 return false;
227 }
228 }
229
230 // The opList ID must not be looked up until AFTER producing the clip mask (if any). That step
231 // can cause a flush or otherwise change which opList our draw is going into.
232 uint32_t opListID = renderTargetContext->getOpList()->uniqueID();
233 int rtWidth = renderTargetContext->width(), rtHeight = renderTargetContext->height();
Robert Phillips777707b2018-01-17 11:40:14 -0500234 if (auto clipFPs = reducedClip.finishAndDetachAnalyticFPs(proxyProvider, opListID,
235 rtWidth, rtHeight)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700236 out->addCoverageFP(std::move(clipFPs));
237 }
238
Chris Daltona32a3c32017-12-05 10:05:21 -0700239 return true;
240}
csmartdaltond211e782016-08-15 11:17:19 -0700241
Chris Daltona32a3c32017-12-05 10:05:21 -0700242bool GrClipStackClip::applyClipMask(GrContext* context, GrRenderTargetContext* renderTargetContext,
243 const GrReducedClip& reducedClip, bool hasUserStencilSettings,
244 GrAppliedClip* out) const {
csmartdalton3affdc12016-10-28 12:01:10 -0700245#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600246 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500247 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
248 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600249 const SkIRect& scissor = reducedClip.scissor();
250 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700251#endif
csmartdaltond211e782016-08-15 11:17:19 -0700252
cdaltonede75742015-11-11 15:27:57 -0800253 // If the stencil buffer is multisampled we can use it to do everything.
Chris Dalton79471932017-10-27 01:50:57 -0600254 if ((GrFSAAType::kNone == renderTargetContext->fsaaType() && reducedClip.maskRequiresAA()) ||
Chris Dalton584a79a2017-11-15 13:14:01 -0700255 context->caps()->avoidStencilBuffers()) {
Robert Phillips875218e2017-02-24 08:37:13 -0500256 sk_sp<GrTextureProxy> result;
Brian Osman11052242016-10-27 14:47:55 -0400257 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000258 // The clip geometry is complex enough that it will be more efficient to create it
259 // entirely in software
Brian Osman5d034742017-09-11 13:38:55 -0400260 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000261 } else {
Brian Salomon19f0ed52017-01-06 13:54:58 -0500262 result = this->createAlphaClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000263 }
264
bsalomon49f085d2014-09-05 13:34:00 -0700265 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000266 // The mask's top left coord should be pinned to the rounded-out top left corner of
Brian Salomon9a767722017-03-13 17:57:28 -0400267 // the clip's device space bounds.
Chris Dalton79471932017-10-27 01:50:57 -0600268 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000269 return true;
270 }
Eric Karl5c779752017-05-08 12:02:07 -0700271
272 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
273 // unless stencils are disallowed.
274 if (context->caps()->avoidStencilBuffers()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700275 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. "
276 "Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700277 return false;
278 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000279 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000280
Robert Phillips65048132017-08-10 08:44:49 -0400281 renderTargetContext->setNeedsStencil();
csmartdaltonbde96c62016-08-31 12:54:46 -0700282
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700283 // This relies on the property that a reduced sub-rect of the last clip will contain all the
284 // relevant window rectangles that were in the last clip. This subtle requirement will go away
285 // after clipping is overhauled.
Chris Dalton584a79a2017-11-15 13:14:01 -0700286 if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(), reducedClip.scissor(),
287 reducedClip.numAnalyticFPs())) {
Brian Salomon9a767722017-03-13 17:57:28 -0400288 reducedClip.drawStencilClipMask(context, renderTargetContext);
Chris Dalton584a79a2017-11-15 13:14:01 -0700289 renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor(),
290 reducedClip.numAnalyticFPs());
csmartdaltonbde96c62016-08-31 12:54:46 -0700291 }
Chris Dalton584a79a2017-11-15 13:14:01 -0700292 // GrAppliedClip doesn't need to figure numAnalyticFPs into its key (used by operator==) because
293 // it verifies the FPs are also equal.
Chris Daltonbbfd5162017-11-07 13:35:22 -0700294 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 return true;
296}
297
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000298////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700299// Create a 8-bit clip mask in alpha
300
Chris Dalton584a79a2017-11-15 13:14:01 -0700301static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, int numAnalyticFPs,
302 GrUniqueKey* key) {
bsalomon473addf2015-10-02 07:49:05 -0700303 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Chris Dalton584a79a2017-11-15 13:14:01 -0700304 GrUniqueKey::Builder builder(key, kDomain, 4, GrClipStackClip::kMaskTestTag);
bsalomon473addf2015-10-02 07:49:05 -0700305 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700306 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
Brian Salomon9a767722017-03-13 17:57:28 -0400307 // sometimes result in negative coordinates from device space.
csmartdalton3affdc12016-10-28 12:01:10 -0700308 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
309 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
Chris Dalton584a79a2017-11-15 13:14:01 -0700310 builder[3] = numAnalyticFPs;
bsalomon473addf2015-10-02 07:49:05 -0700311}
312
Robert Phillips806be2d2017-06-28 15:23:59 -0400313static void add_invalidate_on_pop_message(const SkClipStack& stack, uint32_t clipGenID,
Brian Salomon19f0ed52017-01-06 13:54:58 -0500314 const GrUniqueKey& clipMaskKey) {
315 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
316 while (const Element* element = iter.prev()) {
317 if (element->getGenID() == clipGenID) {
318 std::unique_ptr<GrUniqueKeyInvalidatedMessage> msg(
319 new GrUniqueKeyInvalidatedMessage(clipMaskKey));
320 element->addResourceInvalidationMessage(std::move(msg));
321 return;
322 }
323 }
324 SkDEBUGFAIL("Gen ID was not found in stack.");
325}
326
Robert Phillips875218e2017-02-24 08:37:13 -0500327sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrContext* context,
328 const GrReducedClip& reducedClip) const {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500329 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
bsalomon473addf2015-10-02 07:49:05 -0700330 GrUniqueKey key;
Chris Dalton584a79a2017-11-15 13:14:01 -0700331 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
332 reducedClip.numAnalyticFPs(), &key);
Robert Phillips875218e2017-02-24 08:37:13 -0500333
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500334 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Robert Phillips066f0202017-07-25 10:16:35 -0400335 key, kBottomLeft_GrSurfaceOrigin));
Robert Phillipsd3749482017-03-14 09:17:43 -0400336 if (proxy) {
337 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000338 }
339
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400340 sk_sp<GrRenderTargetContext> rtc(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400341 SkBackingFit::kApprox,
342 reducedClip.width(),
343 reducedClip.height(),
344 kAlpha_8_GrPixelConfig,
345 nullptr));
346 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800347 return nullptr;
348 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000349
Brian Osman11052242016-10-27 14:47:55 -0400350 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700351 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000352 }
353
Robert Phillips875218e2017-02-24 08:37:13 -0500354 sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
355 if (!result) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500356 return nullptr;
357 }
358
Robert Phillips019ff272017-07-24 14:47:57 -0400359 SkASSERT(result->origin() == kBottomLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500360 proxyProvider->assignUniqueKeyToProxy(key, result.get());
Chris Dalton79471932017-10-27 01:50:57 -0600361 add_invalidate_on_pop_message(*fStack, reducedClip.maskGenID(), key);
Robert Phillips875218e2017-02-24 08:37:13 -0500362
363 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000364}
365
Brian Osman5d034742017-09-11 13:38:55 -0400366namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500367
Brian Osman5d034742017-09-11 13:38:55 -0400368/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400369 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400370 * elements, each storing their own AA setting (and already transformed into device space). This
371 * stores all of the information needed by the worker thread to draw all clip elements (see below,
372 * in createSoftwareClipMask).
373 */
374class ClipMaskData {
375public:
376 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600377 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400378 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600379 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400380 fElements.addToTail(*iter.get());
381 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000382 }
383
Chris Dalton79471932017-10-27 01:50:57 -0600384 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400385 InitialState initialState() const { return fInitialState; }
386 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000387
Brian Osman5d034742017-09-11 13:38:55 -0400388private:
Chris Dalton79471932017-10-27 01:50:57 -0600389 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400390 InitialState fInitialState;
391 ElementList fElements;
392};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000393
Brian Osman5d034742017-09-11 13:38:55 -0400394}
395
396static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600397 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400398 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800399 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600400 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800401
Brian Osman5d034742017-09-11 13:38:55 -0400402 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000403
Brian Osman5d034742017-09-11 13:38:55 -0400404 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000405 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500406 SkClipOp op = element->getOp();
Chris Dalton3b51df12017-11-27 14:33:06 -0700407 GrAA aa = GrAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000408
Mike Reedc1f77742016-12-09 09:00:50 -0500409 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000410 // Intersect and reverse difference require modifying pixels outside of the geometry
411 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
412 // but leave the pixels inside the geometry alone. For reverse difference we invert all
413 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500414 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600415 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000416 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400417 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000418 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000419 SkPath clipPath;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400420 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000421 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700422 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400423 helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000424 continue;
425 }
426
427 // The other ops (union, xor, diff) only affect pixels inside
428 // the geometry so they can just be drawn normally
Brian Salomonf3b46e52017-08-30 11:37:57 -0400429 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon74077562017-08-30 13:55:35 -0400430 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000431 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000432 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400433 element->asDeviceSpacePath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700434 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400435 helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000436 }
437 }
Brian Osman5d034742017-09-11 13:38:55 -0400438}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000439
Brian Osman5d034742017-09-11 13:38:55 -0400440sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
441 GrContext* context, const GrReducedClip& reducedClip,
442 GrRenderTargetContext* renderTargetContext) const {
443 GrUniqueKey key;
Chris Dalton584a79a2017-11-15 13:14:01 -0700444 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
445 reducedClip.numAnalyticFPs(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800446
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500447 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
448
449 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Brian Osman5d034742017-09-11 13:38:55 -0400450 key, kTopLeft_GrSurfaceOrigin));
451 if (proxy) {
452 return proxy;
453 }
454
455 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
456 // left corner of the resulting rect to the top left of the texture.
457 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
458
459 SkTaskGroup* taskGroup = context->contextPriv().getTaskGroup();
460 if (taskGroup && renderTargetContext) {
461 // Create our texture proxy
462 GrSurfaceDesc desc;
463 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
464 desc.fWidth = maskSpaceIBounds.width();
465 desc.fHeight = maskSpaceIBounds.height();
466 desc.fConfig = kAlpha_8_GrPixelConfig;
Brian Osmand140fe92017-10-03 12:17:26 -0400467 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
468 // to ops), so it can't have any pending IO.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500469 proxy = proxyProvider->createProxy(desc, SkBackingFit::kApprox, SkBudgeted::kYes,
470 GrResourceProvider::kNoPendingIO_Flag);
Brian Osman5d034742017-09-11 13:38:55 -0400471
Brian Osman099fa0f2017-10-02 16:38:32 -0400472 auto uploader = skstd::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
473 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400474 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
475 TRACE_EVENT0("skia", "Threaded SW Clip Mask Render");
476 GrSWMaskHelper helper(uploaderRaw->getPixels());
477 if (helper.init(maskSpaceIBounds)) {
478 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
Chris Dalton79471932017-10-27 01:50:57 -0600479 uploaderRaw->data().scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400480 uploaderRaw->data().initialState());
481 } else {
482 SkDEBUGFAIL("Unable to allocate SW clip mask.");
483 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400484 uploaderRaw->signalAndFreeData();
Brian Osman5d034742017-09-11 13:38:55 -0400485 };
486
487 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400488 proxy->texPriv().setDeferredUploader(std::move(uploader));
Brian Osman5d034742017-09-11 13:38:55 -0400489 } else {
490 GrSWMaskHelper helper;
491 if (!helper.init(maskSpaceIBounds)) {
492 return nullptr;
493 }
494
Chris Dalton79471932017-10-27 01:50:57 -0600495 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400496 reducedClip.initialState());
497
498 proxy = helper.toTextureProxy(context, SkBackingFit::kApprox);
499 }
500
501 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500502 proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
Chris Dalton79471932017-10-27 01:50:57 -0600503 add_invalidate_on_pop_message(*fStack, reducedClip.maskGenID(), key);
Brian Osman5d034742017-09-11 13:38:55 -0400504 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000505}