blob: 669a9c23a6cd4811010b4be3e17edd9e679b6424 [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"
robertphillips68737822015-10-29 12:12:21 -070012#include "GrDrawingManager.h"
Brian Osman11052242016-10-27 14:47:55 -040013#include "GrRenderTargetContextPriv.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070014#include "GrFixedClip.h"
bsalomon473addf2015-10-02 07:49:05 -070015#include "GrGpuResourcePriv.h"
csmartdalton28341fa2016-08-17 10:00:21 -070016#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070017#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080019#include "effects/GrConvexPolyEffect.h"
20#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080021#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000022
bsalomon@google.com8182fa02012-12-04 14:06:06 +000023typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070024typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070025typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000026
robertphillips976f5f02016-06-03 10:59:20 -070027static const int kMaxAnalyticElements = 4;
28
csmartdaltonc6f411e2016-08-05 22:32:12 -070029bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070030 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070031 return true;
32 }
33 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
34 SkIntToScalar(fOrigin.y())));
35}
36
bsalomon7f0d9f32016-08-15 14:49:10 -070037bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070038 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070039 return true;
40 }
41 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
42 SkIntToScalar(fOrigin.fY)));
43}
44
bsalomoncb31e512016-08-26 10:48:19 -070045bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, bool* aa) const {
46 if (!fStack) {
47 return false;
48 }
49 const SkRect* rtBounds = &origRTBounds;
50 SkRect tempRTBounds;
51 bool origin = fOrigin.fX || fOrigin.fY;
52 if (origin) {
53 tempRTBounds = origRTBounds;
54 tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY));
55 rtBounds = &tempRTBounds;
56 }
57 if (fStack->isRRect(*rtBounds, rr, aa)) {
58 if (origin) {
59 rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
60 }
61 return true;
62 }
63 return false;
64}
65
csmartdaltonc6f411e2016-08-05 22:32:12 -070066void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
67 bool* isIntersectionOfRects) const {
68 if (!fStack) {
69 devResult->setXYWH(0, 0, width, height);
70 if (isIntersectionOfRects) {
71 *isIntersectionOfRects = true;
72 }
73 return;
74 }
75 SkRect devBounds;
76 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
77 isIntersectionOfRects);
78 devBounds.roundOut(devResult);
79}
80
bsalomon@google.com51a62862012-11-26 21:19:43 +000081////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040082// set up the draw state to enable the aa clipping mask.
bungeman06ca8ec2016-06-09 08:01:03 -070083static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
84 const SkIRect &devBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000085 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
Brian Salomon2ebd0c82016-10-03 17:15:28 -040086 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(result, domainTexels,
87 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000088}
89
robertphillips3f7357f2015-10-27 07:17:33 -070090// Does the path in 'element' require SW rendering? If so, return true (and,
91// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
92// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070093bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
94 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -040095 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -070096 const SkMatrix& viewMatrix,
97 const Element* element,
98 GrPathRenderer** prOut,
99 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700100 if (Element::kRect_Type == element->getType()) {
101 // rects can always be drawn directly w/o using the software path
102 // TODO: skip rrects once we're drawing them directly.
103 if (prOut) {
104 *prOut = nullptr;
105 }
106 return false;
107 } else {
108 // We shouldn't get here with an empty clip element.
109 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700110
robertphillips3f7357f2015-10-27 07:17:33 -0700111 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
112 SkPath path;
113 element->asPath(&path);
114 if (path.isInverseFillType()) {
115 path.toggleInverseFillType();
116 }
halcanary9d524f22016-03-29 09:03:52 -0700117
robertphillips3f7357f2015-10-27 07:17:33 -0700118 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700119
robertphillips423e3372015-10-27 09:23:38 -0700120 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700121 type = element->isAA()
122 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
123 : GrPathRendererChain::kStencilAndColor_DrawType;
124 } else {
125 type = element->isAA()
126 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700127 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700128 }
halcanary9d524f22016-03-29 09:03:52 -0700129
bsalomon8acedde2016-06-24 10:42:16 -0700130 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700131 GrPathRenderer::CanDrawPathArgs canDrawArgs;
132 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
133 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700134 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700135 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700136 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
Brian Osman11052242016-10-27 14:47:55 -0400137 canDrawArgs.fIsStencilBufferMSAA = renderTargetContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700138
robertphillips3f7357f2015-10-27 07:17:33 -0700139 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700140 GrPathRenderer* pr =
141 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700142 if (prOut) {
143 *prOut = pr;
144 }
145 return SkToBool(!pr);
146 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000147}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000148
robertphillips@google.comfa662942012-05-17 12:20:22 +0000149/*
150 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
151 * will be used on any element. If so, it returns true to indicate that the
152 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
153 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700154bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
155 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400156 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700157 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000158 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000159 // a clip gets complex enough it can just be done in SW regardless
160 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000161
joshualitt8059eb92014-12-29 15:10:07 -0800162 // Set the matrix so that rendered clip elements are transformed to mask space from clip
163 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700164 SkMatrix translate;
165 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800166
csmartdaltonbde96c62016-08-31 12:54:46 -0700167 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000168 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700169
reed73603f32016-09-20 08:42:38 -0700170 SkCanvas::ClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700171 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700172 bool needsStencil = invert ||
reed73603f32016-09-20 08:42:38 -0700173 SkCanvas::kIntersect_Op == op || SkCanvas::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700174
robertphillips59cf61a2016-07-13 09:18:21 -0700175 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400176 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700177 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000178 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000179 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000180 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000181}
182
csmartdalton77f2fae2016-08-08 09:55:06 -0700183static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700184 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700185 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700186 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700187 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000188 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700189 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700190 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700191 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700192 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700193 while (iter.get()) {
reed73603f32016-09-20 08:42:38 -0700194 SkCanvas::ClipOp op = iter.get()->getOp();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 bool invert;
196 bool skip = false;
197 switch (op) {
198 case SkRegion::kReplace_Op:
199 SkASSERT(iter.get() == elements.head());
200 // Fallthrough, handled same as intersect.
201 case SkRegion::kIntersect_Op:
202 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700203 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000204 skip = true;
205 }
206 break;
207 case SkRegion::kDifference_Op:
208 invert = true;
209 // We don't currently have a cheap test for whether a rect is fully outside an
210 // element's primitive, so don't attempt to set skip.
211 break;
212 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700213 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000215 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700216 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800217 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700218 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700219 return false;
bsalomona912dde2015-10-14 15:01:50 -0700220 }
joshualittb0a8a372014-09-23 09:50:21 -0700221 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700222 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000223 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700224 edgeType =
225 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000226 }
bsalomona912dde2015-10-14 15:01:50 -0700227
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 switch (iter.get()->getType()) {
229 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700230 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
231 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000232 break;
233 case SkClipStack::Element::kRRect_Type: {
234 SkRRect rrect = iter.get()->getRRect();
235 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700236 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000237 break;
238 }
239 case SkClipStack::Element::kRect_Type: {
240 SkRect rect = iter.get()->getRect();
241 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700242 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000243 break;
244 }
245 default:
246 break;
247 }
bungeman06ca8ec2016-06-09 08:01:03 -0700248 if (!fps.back()) {
249 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000250 }
251 }
mtklein217daa72014-07-02 12:55:21 -0700252 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000253 }
254
bsalomon0b5b6b22015-10-14 08:31:34 -0700255 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700256 if (fps.count()) {
257 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000258 }
bungeman06ca8ec2016-06-09 08:01:03 -0700259 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000260}
261
robertphillips@google.comf294b772012-04-27 14:29:26 +0000262////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000263// sort out what kind of clip mask needs to be created: alpha, stencil,
264// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400265bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
266 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700267 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700268 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700269 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000270
Brian Osman11052242016-10-27 14:47:55 -0400271 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700272 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700273 return false;
274 }
275
Brian Osman11052242016-10-27 14:47:55 -0400276 GrRenderTarget* rt = renderTargetContext->accessRenderTarget();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700277
csmartdaltonc6f411e2016-08-05 22:32:12 -0700278 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
279 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700280
csmartdalton77f2fae2016-08-08 09:55:06 -0700281 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700282 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds,
283 rt->renderTargetPriv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000284
csmartdaltond211e782016-08-15 11:17:19 -0700285 if (reducedClip.hasIBounds() &&
286 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
287 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
288 scissorSpaceIBounds.offset(-fOrigin);
289 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700290 }
cdalton93a379b2016-05-11 13:58:08 -0700291
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700292 if (!reducedClip.windowRectangles().empty()) {
293 out->addWindowRectangles(reducedClip.windowRectangles(), fOrigin,
294 GrWindowRectsState::Mode::kExclusive);
295 }
296
csmartdaltond211e782016-08-15 11:17:19 -0700297 if (reducedClip.elements().isEmpty()) {
298 return InitialState::kAllIn == reducedClip.initialState();
299 }
300
csmartdalton3affdc12016-10-28 12:01:10 -0700301#ifdef SK_DEBUG
csmartdaltond211e782016-08-15 11:17:19 -0700302 SkASSERT(reducedClip.hasIBounds());
csmartdalton3affdc12016-10-28 12:01:10 -0700303 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
304 renderTargetContext->height());
305 SkIRect clipIBounds = reducedClip.ibounds().makeOffset(-fOrigin.x(), -fOrigin.y());
306 SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT.
307#endif
csmartdaltond211e782016-08-15 11:17:19 -0700308
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000309 // An element count of 4 was chosen because of the common pattern in Blink of:
310 // isect RR
311 // diff RR
312 // isect convex_poly
313 // isect convex_poly
314 // when drawing rounded div borders. This could probably be tuned based on a
315 // configuration's relative costs of switching RTs to generate a mask vs
316 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700317 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800318 // When there are multiple samples we want to do per-sample clipping, not compute a
319 // fractional pixel coverage.
Brian Osman11052242016-10-27 14:47:55 -0400320 bool disallowAnalyticAA = renderTargetContext->isStencilBufferMultisampled();
321 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700322 // With a single color sample, any coverage info is lost from color once it hits the
323 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
324 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700325 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700326 }
bungeman06ca8ec2016-06-09 08:01:03 -0700327 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700328 if (reducedClip.requiresAA() &&
329 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
330 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700331 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000332 return true;
333 }
334 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000335
cdaltonede75742015-11-11 15:27:57 -0800336 // If the stencil buffer is multisampled we can use it to do everything.
Brian Osman11052242016-10-27 14:47:55 -0400337 if (!renderTargetContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700338 sk_sp<GrTexture> result;
Brian Osman11052242016-10-27 14:47:55 -0400339 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000340 // The clip geometry is complex enough that it will be more efficient to create it
341 // entirely in software
csmartdaltonbde96c62016-08-31 12:54:46 -0700342 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000343 } else {
csmartdaltonbde96c62016-08-31 12:54:46 -0700344 result = CreateAlphaClipMask(context, reducedClip);
robertphillips391395d2016-03-02 09:26:36 -0800345 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700346 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000347 }
348
bsalomon49f085d2014-09-05 13:34:00 -0700349 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000350 // The mask's top left coord should be pinned to the rounded-out top left corner of
351 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700352 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700353 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700354 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355 return true;
356 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000357 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000358 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000360 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdalton7cdda992016-11-01 07:03:03 -0700361 if (!context->resourceProvider()->attachStencilAttachment(rt)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700362 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
363 return true;
364 }
365
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700366 // This relies on the property that a reduced sub-rect of the last clip will contain all the
367 // relevant window rectangles that were in the last clip. This subtle requirement will go away
368 // after clipping is overhauled.
csmartdalton7cdda992016-11-01 07:03:03 -0700369 if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(),
370 reducedClip.ibounds(), fOrigin)) {
Brian Osman11052242016-10-27 14:47:55 -0400371 reducedClip.drawStencilClipMask(context, renderTargetContext, fOrigin);
csmartdalton7cdda992016-11-01 07:03:03 -0700372 renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
373 fOrigin);
csmartdaltonbde96c62016-08-31 12:54:46 -0700374 }
csmartdaltond211e782016-08-15 11:17:19 -0700375 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000376 return true;
377}
378
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000379////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700380// Create a 8-bit clip mask in alpha
381
382static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
383 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
384 GrUniqueKey::Builder builder(key, kDomain, 3);
385 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700386 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
387 // sometimes result in negative coordinates from clip space.
388 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
389 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
bsalomon473addf2015-10-02 07:49:05 -0700390}
391
csmartdaltonc6f411e2016-08-05 22:32:12 -0700392sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdaltonbde96c62016-08-31 12:54:46 -0700393 const GrReducedClip& reducedClip) {
robertphillips391395d2016-03-02 09:26:36 -0800394 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700395 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700396 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700397 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700398 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000399 }
400
Brian Osman11052242016-10-27 14:47:55 -0400401 sk_sp<GrRenderTargetContext> rtc(context->makeRenderTargetContextWithFallback(
402 SkBackingFit::kApprox,
403 reducedClip.width(),
404 reducedClip.height(),
405 kAlpha_8_GrPixelConfig,
406 nullptr));
407 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800408 return nullptr;
409 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000410
Brian Osman11052242016-10-27 14:47:55 -0400411 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700412 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413 }
414
Brian Osman11052242016-10-27 14:47:55 -0400415 sk_sp<GrTexture> texture(rtc->asTexture());
robertphillipsc99b8f02016-05-15 07:53:35 -0700416 SkASSERT(texture);
417 texture->resourcePriv().setUniqueKey(key);
418 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000419}
420
csmartdaltonc6f411e2016-08-05 22:32:12 -0700421sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdaltonbde96c62016-08-31 12:54:46 -0700422 const GrReducedClip& reducedClip) {
bsalomon473addf2015-10-02 07:49:05 -0700423 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700424 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700425 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700426 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000427 }
428
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000429 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
430 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700431 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432
robertphillips0152d732016-05-20 06:38:43 -0700433 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000434
joshualitt8059eb92014-12-29 15:10:07 -0800435 // Set the matrix so that rendered clip elements are transformed to mask space from clip
436 // space.
437 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700438 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800439
csmartdalton3affdc12016-10-28 12:01:10 -0700440 if (!helper.init(maskSpaceIBounds, &translate)) {
441 return nullptr;
442 }
csmartdaltond211e782016-08-15 11:17:19 -0700443 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000444
csmartdalton77f2fae2016-08-08 09:55:06 -0700445 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000446 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700447 SkCanvas::ClipOp op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000448
reed73603f32016-09-20 08:42:38 -0700449 if (SkCanvas::kIntersect_Op == op || SkCanvas::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000450 // Intersect and reverse difference require modifying pixels outside of the geometry
451 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
452 // but leave the pixels inside the geometry alone. For reverse difference we invert all
453 // the pixels before clearing the ones outside the geometry.
reed73603f32016-09-20 08:42:38 -0700454 if (SkCanvas::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700455 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000456 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700457 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000458 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000459 SkPath clipPath;
460 element->asPath(&clipPath);
461 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700462 GrShape shape(clipPath, GrStyle::SimpleFill());
463 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000464 continue;
465 }
466
467 // The other ops (union, xor, diff) only affect pixels inside
468 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000469 if (Element::kRect_Type == element->getType()) {
reed73603f32016-09-20 08:42:38 -0700470 helper.drawRect(element->getRect(), (SkRegion::Op)op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000471 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000472 SkPath path;
473 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700474 GrShape shape(path, GrStyle::SimpleFill());
reed73603f32016-09-20 08:42:38 -0700475 helper.drawShape(shape, (SkRegion::Op)op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000476 }
477 }
478
krajcevskiad1dc582014-06-10 15:06:47 -0700479 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800480 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700481 desc.fWidth = reducedClip.width();
482 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800483 desc.fConfig = kAlpha_8_GrPixelConfig;
484
robertphillips0152d732016-05-20 06:38:43 -0700485 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800486 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700487 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700488 }
robertphillips391395d2016-03-02 09:26:36 -0800489 result->resourcePriv().setUniqueKey(key);
490
robertphillipsc99b8f02016-05-15 07:53:35 -0700491 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000492
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000493 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000494}