blob: 245f2d0cd85b65cbe309af5282c54bd6f481d138 [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"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContextPriv.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070015#include "GrFixedClip.h"
bsalomon473addf2015-10-02 07:49:05 -070016#include "GrGpuResourcePriv.h"
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040017#include "GrResourceProvider.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
robertphillips976f5f02016-06-03 10:59:20 -070033static const int kMaxAnalyticElements = 4;
Brian Salomon19f0ed52017-01-06 13:54:58 -050034const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070035
csmartdaltonc6f411e2016-08-05 22:32:12 -070036bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070037 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070038 return true;
39 }
Brian Salomon9a767722017-03-13 17:57:28 -040040 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070041}
42
bsalomon7f0d9f32016-08-15 14:49:10 -070043bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070044 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070045 return true;
46 }
Brian Salomon9a767722017-03-13 17:57:28 -040047 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070048}
49
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050050bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070051 if (!fStack) {
52 return false;
53 }
54 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050055 bool isAA;
56 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
57 *aa = GrBoolToAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070058 return true;
59 }
60 return false;
61}
62
csmartdaltonc6f411e2016-08-05 22:32:12 -070063void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
64 bool* isIntersectionOfRects) const {
65 if (!fStack) {
66 devResult->setXYWH(0, 0, width, height);
67 if (isIntersectionOfRects) {
68 *isIntersectionOfRects = true;
69 }
70 return;
71 }
72 SkRect devBounds;
Brian Salomon9a767722017-03-13 17:57:28 -040073 fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
csmartdaltonc6f411e2016-08-05 22:32:12 -070074 devBounds.roundOut(devResult);
75}
76
bsalomon@google.com51a62862012-11-26 21:19:43 +000077////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040078// set up the draw state to enable the aa clipping mask.
Brian Salomonaff329b2017-08-11 09:40:37 -040079static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(sk_sp<GrTextureProxy> mask,
80 const SkIRect& devBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000081 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040082 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(mask), domainTexels,
Brian Salomon2ebd0c82016-10-03 17:15:28 -040083 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000084}
85
robertphillips3f7357f2015-10-27 07:17:33 -070086// Does the path in 'element' require SW rendering? If so, return true (and,
87// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
88// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070089bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
Chris Daltondb91c6e2017-09-08 16:25:08 -060090 const SkIRect& scissorRect,
csmartdaltonc6f411e2016-08-05 22:32:12 -070091 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -040092 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -070093 const SkMatrix& viewMatrix,
94 const Element* element,
95 GrPathRenderer** prOut,
96 bool needsStencil) {
Brian Salomonf3b46e52017-08-30 11:37:57 -040097 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
robertphillips3f7357f2015-10-27 07:17:33 -070098 // rects can always be drawn directly w/o using the software path
99 // TODO: skip rrects once we're drawing them directly.
100 if (prOut) {
101 *prOut = nullptr;
102 }
103 return false;
104 } else {
105 // We shouldn't get here with an empty clip element.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400106 SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700107
robertphillips3f7357f2015-10-27 07:17:33 -0700108 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
109 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400110 element->asDeviceSpacePath(&path);
robertphillips3f7357f2015-10-27 07:17:33 -0700111 if (path.isInverseFillType()) {
112 path.toggleInverseFillType();
113 }
halcanary9d524f22016-03-29 09:03:52 -0700114
Brian Salomon82125e92016-12-10 09:35:48 -0500115 GrPathRendererChain::DrawType type =
116 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
117 : GrPathRendererChain::DrawType::kColor;
halcanary9d524f22016-03-29 09:03:52 -0700118
bsalomon8acedde2016-06-24 10:42:16 -0700119 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700120 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700121 canDrawArgs.fCaps = context->caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600122 canDrawArgs.fClipConservativeBounds = &scissorRect;
robertphillips68737822015-10-29 12:12:21 -0700123 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700124 canDrawArgs.fShape = &shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400125 canDrawArgs.fAAType = GrChooseAAType(GrBoolToAA(element->isAA()),
126 renderTargetContext->fsaaType(),
Brian Salomone225b562017-06-14 13:00:03 -0400127 GrAllowMixedSamples::kYes,
128 *context->caps());
cdalton93a379b2016-05-11 13:58:08 -0700129 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700130
robertphillips3f7357f2015-10-27 07:17:33 -0700131 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700132 GrPathRenderer* pr =
133 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700134 if (prOut) {
135 *prOut = pr;
136 }
137 return SkToBool(!pr);
138 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000139}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000140
robertphillips@google.comfa662942012-05-17 12:20:22 +0000141/*
142 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
143 * will be used on any element. If so, it returns true to indicate that the
144 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
145 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700146bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
147 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400148 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700149 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000150 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000151 // a clip gets complex enough it can just be done in SW regardless
152 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000153
Eric Karl5c779752017-05-08 12:02:07 -0700154 // If we're avoiding stencils, always use SW:
155 if (context->caps()->avoidStencilBuffers())
156 return true;
157
joshualitt8059eb92014-12-29 15:10:07 -0800158 // Set the matrix so that rendered clip elements are transformed to mask space from clip
159 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700160 SkMatrix translate;
161 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800162
Chris Dalton79471932017-10-27 01:50:57 -0600163 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000164 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700165
Mike Reedc1f77742016-12-09 09:00:50 -0500166 SkClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700167 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700168 bool needsStencil = invert ||
Mike Reedc1f77742016-12-09 09:00:50 -0500169 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700170
Chris Dalton79471932017-10-27 01:50:57 -0600171 if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400172 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700173 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000174 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000175 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000176 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000177}
178
Chris Daltond8d15932017-11-01 19:21:24 +0000179static bool get_analytic_clip_processor(const ElementList& elements,
180 bool abortIfAA,
181 const SkRect& drawDevBounds,
182 std::unique_ptr<GrFragmentProcessor>* resultFP) {
183 SkASSERT(elements.count() <= kMaxAnalyticElements);
184 SkSTArray<kMaxAnalyticElements, std::unique_ptr<GrFragmentProcessor>> fps;
185 ElementList::Iter iter(elements);
186 while (iter.get()) {
187 SkClipOp op = iter.get()->getOp();
188 bool invert;
189 bool skip = false;
190 switch (op) {
191 case kReplace_SkClipOp:
192 SkASSERT(iter.get() == elements.head());
193 // Fallthrough, handled same as intersect.
194 case kIntersect_SkClipOp:
195 invert = false;
196 if (iter.get()->contains(drawDevBounds)) {
197 skip = true;
198 }
199 break;
200 case kDifference_SkClipOp:
201 invert = true;
202 // We don't currently have a cheap test for whether a rect is fully outside an
203 // element's primitive, so don't attempt to set skip.
204 break;
205 default:
206 return false;
207 }
208 if (!skip) {
209 GrPrimitiveEdgeType edgeType;
210 if (iter.get()->isAA()) {
211 if (abortIfAA) {
212 return false;
213 }
214 edgeType =
215 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
216 } else {
217 edgeType =
218 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
219 }
220
221 switch (iter.get()->getDeviceSpaceType()) {
222 case SkClipStack::Element::DeviceSpaceType::kPath:
223 fps.emplace_back(
224 GrConvexPolyEffect::Make(edgeType, iter.get()->getDeviceSpacePath()));
225 break;
226 case SkClipStack::Element::DeviceSpaceType::kRRect: {
227 fps.emplace_back(
228 GrRRectEffect::Make(edgeType, iter.get()->getDeviceSpaceRRect()));
229 break;
230 }
231 case SkClipStack::Element::DeviceSpaceType::kRect: {
232 fps.emplace_back(
233 GrConvexPolyEffect::Make(edgeType, iter.get()->getDeviceSpaceRect()));
234 break;
235 }
236 default:
237 break;
238 }
239 if (!fps.back()) {
240 return false;
241 }
242 }
243 iter.next();
244 }
245
246 *resultFP = nullptr;
247 if (fps.count()) {
248 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
249 }
250 return true;
251}
252
robertphillips@google.comf294b772012-04-27 14:29:26 +0000253////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000254// sort out what kind of clip mask needs to be created: alpha, stencil,
255// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400256bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400257 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
258 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400259 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
260 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700261 return false;
262 }
263
Brian Salomon510dd422017-03-16 12:15:22 -0400264 if (!fStack || fStack->isWideOpen()) {
265 return true;
266 }
267
Chris Daltond8d15932017-11-01 19:21:24 +0000268 const GrReducedClip reducedClip(*fStack, devBounds,
269 renderTargetContext->priv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000270
Chris Dalton79471932017-10-27 01:50:57 -0600271 if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) {
Chris Dalton4c92d4a2017-11-06 13:48:04 -0700272 out->hardClip().addScissor(reducedClip.scissor(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700273 }
cdalton93a379b2016-05-11 13:58:08 -0700274
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700275 if (!reducedClip.windowRectangles().empty()) {
Chris Dalton4c92d4a2017-11-06 13:48:04 -0700276 out->hardClip().addWindowRectangles(reducedClip.windowRectangles(),
277 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700278 }
279
Chris Dalton79471932017-10-27 01:50:57 -0600280 if (reducedClip.maskElements().isEmpty()) {
csmartdaltond211e782016-08-15 11:17:19 -0700281 return InitialState::kAllIn == reducedClip.initialState();
282 }
283
csmartdalton3affdc12016-10-28 12:01:10 -0700284#ifdef SK_DEBUG
Chris Dalton79471932017-10-27 01:50:57 -0600285 SkASSERT(reducedClip.hasScissor());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500286 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
287 renderTargetContext->height());
Chris Dalton79471932017-10-27 01:50:57 -0600288 const SkIRect& scissor = reducedClip.scissor();
289 SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT.
csmartdalton3affdc12016-10-28 12:01:10 -0700290#endif
csmartdaltond211e782016-08-15 11:17:19 -0700291
Chris Daltond8d15932017-11-01 19:21:24 +0000292 bool avoidStencilBuffers = context->caps()->avoidStencilBuffers();
293
294 // An element count of 4 was chosen because of the common pattern in Blink of:
295 // isect RR
296 // diff RR
297 // isect convex_poly
298 // isect convex_poly
299 // when drawing rounded div borders. This could probably be tuned based on a
300 // configuration's relative costs of switching RTs to generate a mask vs
301 // longer shaders.
302 if (reducedClip.maskElements().count() <= kMaxAnalyticElements) {
303 // When there are multiple samples we want to do per-sample clipping, not compute a
304 // fractional pixel coverage.
305 bool disallowAnalyticAA =
306 GrFSAAType::kNone != renderTargetContext->fsaaType() && !avoidStencilBuffers;
307 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
308 // With a single color sample, any coverage info is lost from color once it hits the
309 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
310 // is multisampled.
311 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
312 }
313 std::unique_ptr<GrFragmentProcessor> clipFP;
314 if ((reducedClip.maskRequiresAA() || avoidStencilBuffers) &&
315 get_analytic_clip_processor(reducedClip.maskElements(), disallowAnalyticAA, devBounds,
316 &clipFP)) {
317 if (clipFP) {
318 out->addCoverageFP(std::move(clipFP));
319 }
320 return true;
321 }
322 }
323
cdaltonede75742015-11-11 15:27:57 -0800324 // If the stencil buffer is multisampled we can use it to do everything.
Chris Dalton79471932017-10-27 01:50:57 -0600325 if ((GrFSAAType::kNone == renderTargetContext->fsaaType() && reducedClip.maskRequiresAA()) ||
Chris Daltond8d15932017-11-01 19:21:24 +0000326 avoidStencilBuffers) {
Robert Phillips875218e2017-02-24 08:37:13 -0500327 sk_sp<GrTextureProxy> result;
Brian Osman11052242016-10-27 14:47:55 -0400328 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000329 // The clip geometry is complex enough that it will be more efficient to create it
330 // entirely in software
Brian Osman5d034742017-09-11 13:38:55 -0400331 result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000332 } else {
Brian Salomon19f0ed52017-01-06 13:54:58 -0500333 result = this->createAlphaClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000334 }
335
bsalomon49f085d2014-09-05 13:34:00 -0700336 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000337 // The mask's top left coord should be pinned to the rounded-out top left corner of
Brian Salomon9a767722017-03-13 17:57:28 -0400338 // the clip's device space bounds.
Chris Dalton79471932017-10-27 01:50:57 -0600339 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000340 return true;
341 }
Eric Karl5c779752017-05-08 12:02:07 -0700342
343 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
344 // unless stencils are disallowed.
345 if (context->caps()->avoidStencilBuffers()) {
Chris Daltond8d15932017-11-01 19:21:24 +0000346 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. Clip will be ignored.\n");
Eric Karl5c779752017-05-08 12:02:07 -0700347 return false;
348 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000349 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000350
Robert Phillips65048132017-08-10 08:44:49 -0400351 renderTargetContext->setNeedsStencil();
csmartdaltonbde96c62016-08-31 12:54:46 -0700352
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700353 // This relies on the property that a reduced sub-rect of the last clip will contain all the
354 // relevant window rectangles that were in the last clip. This subtle requirement will go away
355 // after clipping is overhauled.
Chris Daltond8d15932017-11-01 19:21:24 +0000356 if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(),
357 reducedClip.scissor())) {
Brian Salomon9a767722017-03-13 17:57:28 -0400358 reducedClip.drawStencilClipMask(context, renderTargetContext);
Chris Daltond8d15932017-11-01 19:21:24 +0000359 renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor());
csmartdaltonbde96c62016-08-31 12:54:46 -0700360 }
Chris Dalton4c92d4a2017-11-06 13:48:04 -0700361 out->hardClip().addStencilClip(reducedClip.maskGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000362 return true;
363}
364
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000365////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700366// Create a 8-bit clip mask in alpha
367
Chris Daltond8d15932017-11-01 19:21:24 +0000368static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
bsalomon473addf2015-10-02 07:49:05 -0700369 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Chris Daltond8d15932017-11-01 19:21:24 +0000370 GrUniqueKey::Builder builder(key, kDomain, 3, GrClipStackClip::kMaskTestTag);
bsalomon473addf2015-10-02 07:49:05 -0700371 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700372 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
Brian Salomon9a767722017-03-13 17:57:28 -0400373 // sometimes result in negative coordinates from device space.
csmartdalton3affdc12016-10-28 12:01:10 -0700374 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
375 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
bsalomon473addf2015-10-02 07:49:05 -0700376}
377
Robert Phillips806be2d2017-06-28 15:23:59 -0400378static void add_invalidate_on_pop_message(const SkClipStack& stack, uint32_t clipGenID,
Brian Salomon19f0ed52017-01-06 13:54:58 -0500379 const GrUniqueKey& clipMaskKey) {
380 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
381 while (const Element* element = iter.prev()) {
382 if (element->getGenID() == clipGenID) {
383 std::unique_ptr<GrUniqueKeyInvalidatedMessage> msg(
384 new GrUniqueKeyInvalidatedMessage(clipMaskKey));
385 element->addResourceInvalidationMessage(std::move(msg));
386 return;
387 }
388 }
389 SkDEBUGFAIL("Gen ID was not found in stack.");
390}
391
Robert Phillips875218e2017-02-24 08:37:13 -0500392sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrContext* context,
393 const GrReducedClip& reducedClip) const {
Brian Osman32342f02017-03-04 08:12:46 -0500394 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700395 GrUniqueKey key;
Chris Daltond8d15932017-11-01 19:21:24 +0000396 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(), &key);
Robert Phillips875218e2017-02-24 08:37:13 -0500397
Greg Danielcd871402017-09-26 12:49:26 -0400398 sk_sp<GrTextureProxy> proxy(resourceProvider->findOrCreateProxyByUniqueKey(
Robert Phillips066f0202017-07-25 10:16:35 -0400399 key, kBottomLeft_GrSurfaceOrigin));
Robert Phillipsd3749482017-03-14 09:17:43 -0400400 if (proxy) {
401 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000402 }
403
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400404 sk_sp<GrRenderTargetContext> rtc(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400405 SkBackingFit::kApprox,
406 reducedClip.width(),
407 reducedClip.height(),
408 kAlpha_8_GrPixelConfig,
409 nullptr));
410 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800411 return nullptr;
412 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000413
Brian Osman11052242016-10-27 14:47:55 -0400414 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700415 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000416 }
417
Robert Phillips875218e2017-02-24 08:37:13 -0500418 sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
419 if (!result) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500420 return nullptr;
421 }
422
Robert Phillips019ff272017-07-24 14:47:57 -0400423 SkASSERT(result->origin() == kBottomLeft_GrSurfaceOrigin);
Robert Phillipsd3749482017-03-14 09:17:43 -0400424 resourceProvider->assignUniqueKeyToProxy(key, result.get());
Chris Dalton79471932017-10-27 01:50:57 -0600425 add_invalidate_on_pop_message(*fStack, reducedClip.maskGenID(), key);
Robert Phillips875218e2017-02-24 08:37:13 -0500426
427 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000428}
429
Brian Osman5d034742017-09-11 13:38:55 -0400430namespace {
Robert Phillips875218e2017-02-24 08:37:13 -0500431
Brian Osman5d034742017-09-11 13:38:55 -0400432/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400433 * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple
Brian Osman5d034742017-09-11 13:38:55 -0400434 * elements, each storing their own AA setting (and already transformed into device space). This
435 * stores all of the information needed by the worker thread to draw all clip elements (see below,
436 * in createSoftwareClipMask).
437 */
438class ClipMaskData {
439public:
440 ClipMaskData(const GrReducedClip& reducedClip)
Chris Dalton79471932017-10-27 01:50:57 -0600441 : fScissor(reducedClip.scissor())
Brian Osman5d034742017-09-11 13:38:55 -0400442 , fInitialState(reducedClip.initialState()) {
Chris Dalton79471932017-10-27 01:50:57 -0600443 for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) {
Brian Osman5d034742017-09-11 13:38:55 -0400444 fElements.addToTail(*iter.get());
445 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000446 }
447
Chris Dalton79471932017-10-27 01:50:57 -0600448 const SkIRect& scissor() const { return fScissor; }
Brian Osman5d034742017-09-11 13:38:55 -0400449 InitialState initialState() const { return fInitialState; }
450 const ElementList& elements() const { return fElements; }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000451
Brian Osman5d034742017-09-11 13:38:55 -0400452private:
Chris Dalton79471932017-10-27 01:50:57 -0600453 SkIRect fScissor;
Brian Osman5d034742017-09-11 13:38:55 -0400454 InitialState fInitialState;
455 ElementList fElements;
456};
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000457
Brian Osman5d034742017-09-11 13:38:55 -0400458}
459
460static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements,
Chris Dalton79471932017-10-27 01:50:57 -0600461 const SkIRect& scissor, InitialState initialState) {
Brian Osman5d034742017-09-11 13:38:55 -0400462 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800463 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600464 translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800465
Brian Osman5d034742017-09-11 13:38:55 -0400466 helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000467
Brian Osman5d034742017-09-11 13:38:55 -0400468 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000469 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500470 SkClipOp op = element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500471 GrAA aa = GrBoolToAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000472
Mike Reedc1f77742016-12-09 09:00:50 -0500473 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000474 // Intersect and reverse difference require modifying pixels outside of the geometry
475 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
476 // but leave the pixels inside the geometry alone. For reverse difference we invert all
477 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500478 if (kReverseDifference_SkClipOp == op) {
Chris Dalton79471932017-10-27 01:50:57 -0600479 SkRect temp = SkRect::Make(scissor);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000480 // invert the entire scene
Brian Salomon74077562017-08-30 13:55:35 -0400481 helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000482 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000483 SkPath clipPath;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400484 element->asDeviceSpacePath(&clipPath);
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000485 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700486 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400487 helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000488 continue;
489 }
490
491 // The other ops (union, xor, diff) only affect pixels inside
492 // the geometry so they can just be drawn normally
Brian Salomonf3b46e52017-08-30 11:37:57 -0400493 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon74077562017-08-30 13:55:35 -0400494 helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000495 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000496 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400497 element->asDeviceSpacePath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700498 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon74077562017-08-30 13:55:35 -0400499 helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000500 }
501 }
Brian Osman5d034742017-09-11 13:38:55 -0400502}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000503
Brian Osman5d034742017-09-11 13:38:55 -0400504sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
505 GrContext* context, const GrReducedClip& reducedClip,
506 GrRenderTargetContext* renderTargetContext) const {
507 GrUniqueKey key;
Chris Daltond8d15932017-11-01 19:21:24 +0000508 create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(), &key);
robertphillips391395d2016-03-02 09:26:36 -0800509
Greg Danielcd871402017-09-26 12:49:26 -0400510 sk_sp<GrTextureProxy> proxy(context->resourceProvider()->findOrCreateProxyByUniqueKey(
Brian Osman5d034742017-09-11 13:38:55 -0400511 key, kTopLeft_GrSurfaceOrigin));
512 if (proxy) {
513 return proxy;
514 }
515
516 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
517 // left corner of the resulting rect to the top left of the texture.
518 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
519
520 SkTaskGroup* taskGroup = context->contextPriv().getTaskGroup();
521 if (taskGroup && renderTargetContext) {
522 // Create our texture proxy
523 GrSurfaceDesc desc;
524 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
525 desc.fWidth = maskSpaceIBounds.width();
526 desc.fHeight = maskSpaceIBounds.height();
527 desc.fConfig = kAlpha_8_GrPixelConfig;
Brian Osmand140fe92017-10-03 12:17:26 -0400528 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
529 // to ops), so it can't have any pending IO.
Brian Osman5d034742017-09-11 13:38:55 -0400530 proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
Brian Osman099fa0f2017-10-02 16:38:32 -0400531 SkBackingFit::kApprox, SkBudgeted::kYes,
532 GrResourceProvider::kNoPendingIO_Flag);
Brian Osman5d034742017-09-11 13:38:55 -0400533
Brian Osman099fa0f2017-10-02 16:38:32 -0400534 auto uploader = skstd::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
535 GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
Brian Osman5d034742017-09-11 13:38:55 -0400536 auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] {
537 TRACE_EVENT0("skia", "Threaded SW Clip Mask Render");
538 GrSWMaskHelper helper(uploaderRaw->getPixels());
539 if (helper.init(maskSpaceIBounds)) {
540 draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(),
Chris Dalton79471932017-10-27 01:50:57 -0600541 uploaderRaw->data().scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400542 uploaderRaw->data().initialState());
543 } else {
544 SkDEBUGFAIL("Unable to allocate SW clip mask.");
545 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400546 uploaderRaw->signalAndFreeData();
Brian Osman5d034742017-09-11 13:38:55 -0400547 };
548
549 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400550 proxy->texPriv().setDeferredUploader(std::move(uploader));
Brian Osman5d034742017-09-11 13:38:55 -0400551 } else {
552 GrSWMaskHelper helper;
553 if (!helper.init(maskSpaceIBounds)) {
554 return nullptr;
555 }
556
Chris Dalton79471932017-10-27 01:50:57 -0600557 draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
Brian Osman5d034742017-09-11 13:38:55 -0400558 reducedClip.initialState());
559
560 proxy = helper.toTextureProxy(context, SkBackingFit::kApprox);
561 }
562
563 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
564 context->resourceProvider()->assignUniqueKeyToProxy(key, proxy.get());
Chris Dalton79471932017-10-27 01:50:57 -0600565 add_invalidate_on_pop_message(*fStack, reducedClip.maskGenID(), key);
Brian Osman5d034742017-09-11 13:38:55 -0400566 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000567}