blob: 14c2dbe8accdb2250315b58a416493798e2bb796 [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
301 SkASSERT(reducedClip.hasIBounds());
302
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000303 // An element count of 4 was chosen because of the common pattern in Blink of:
304 // isect RR
305 // diff RR
306 // isect convex_poly
307 // isect convex_poly
308 // when drawing rounded div borders. This could probably be tuned based on a
309 // configuration's relative costs of switching RTs to generate a mask vs
310 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700311 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800312 // When there are multiple samples we want to do per-sample clipping, not compute a
313 // fractional pixel coverage.
Brian Osman11052242016-10-27 14:47:55 -0400314 bool disallowAnalyticAA = renderTargetContext->isStencilBufferMultisampled();
315 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700316 // With a single color sample, any coverage info is lost from color once it hits the
317 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
318 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700319 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700320 }
bungeman06ca8ec2016-06-09 08:01:03 -0700321 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700322 if (reducedClip.requiresAA() &&
323 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
324 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700325 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000326 return true;
327 }
328 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000329
cdaltonede75742015-11-11 15:27:57 -0800330 // If the stencil buffer is multisampled we can use it to do everything.
Brian Osman11052242016-10-27 14:47:55 -0400331 if (!renderTargetContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700332 sk_sp<GrTexture> result;
Brian Osman11052242016-10-27 14:47:55 -0400333 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000334 // The clip geometry is complex enough that it will be more efficient to create it
335 // entirely in software
csmartdaltonbde96c62016-08-31 12:54:46 -0700336 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000337 } else {
csmartdaltonbde96c62016-08-31 12:54:46 -0700338 result = CreateAlphaClipMask(context, reducedClip);
robertphillips391395d2016-03-02 09:26:36 -0800339 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700340 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000341 }
342
bsalomon49f085d2014-09-05 13:34:00 -0700343 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 // The mask's top left coord should be pinned to the rounded-out top left corner of
345 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700346 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700347 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700348 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000349 return true;
350 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000351 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000352 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000354 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonbde96c62016-08-31 12:54:46 -0700355 // TODO: these need to be swapped over to using a StencilAttachmentProxy
356 GrStencilAttachment* stencilAttachment =
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700357 context->resourceProvider()->attachStencilAttachment(rt);
csmartdaltonbde96c62016-08-31 12:54:46 -0700358 if (nullptr == stencilAttachment) {
359 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
360 return true;
361 }
362
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700363 // This relies on the property that a reduced sub-rect of the last clip will contain all the
364 // relevant window rectangles that were in the last clip. This subtle requirement will go away
365 // after clipping is overhauled.
csmartdaltonbde96c62016-08-31 12:54:46 -0700366 if (stencilAttachment->mustRenderClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
367 fOrigin)) {
Brian Osman11052242016-10-27 14:47:55 -0400368 reducedClip.drawStencilClipMask(context, renderTargetContext, fOrigin);
csmartdaltonbde96c62016-08-31 12:54:46 -0700369 stencilAttachment->setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
370 fOrigin);
371 }
csmartdaltond211e782016-08-15 11:17:19 -0700372 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000373 return true;
374}
375
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000376////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700377// Create a 8-bit clip mask in alpha
378
379static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
380 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
381 GrUniqueKey::Builder builder(key, kDomain, 3);
382 builder[0] = clipGenID;
383 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
384 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
385}
386
csmartdaltonc6f411e2016-08-05 22:32:12 -0700387sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdaltonbde96c62016-08-31 12:54:46 -0700388 const GrReducedClip& reducedClip) {
robertphillips391395d2016-03-02 09:26:36 -0800389 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700390 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700391 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700392 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700393 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000394 }
395
Brian Osman11052242016-10-27 14:47:55 -0400396 sk_sp<GrRenderTargetContext> rtc(context->makeRenderTargetContextWithFallback(
397 SkBackingFit::kApprox,
398 reducedClip.width(),
399 reducedClip.height(),
400 kAlpha_8_GrPixelConfig,
401 nullptr));
402 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800403 return nullptr;
404 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000405
Brian Osman11052242016-10-27 14:47:55 -0400406 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700407 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000408 }
409
Brian Osman11052242016-10-27 14:47:55 -0400410 sk_sp<GrTexture> texture(rtc->asTexture());
robertphillipsc99b8f02016-05-15 07:53:35 -0700411 SkASSERT(texture);
412 texture->resourcePriv().setUniqueKey(key);
413 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000414}
415
csmartdaltonc6f411e2016-08-05 22:32:12 -0700416sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdaltonbde96c62016-08-31 12:54:46 -0700417 const GrReducedClip& reducedClip) {
bsalomon473addf2015-10-02 07:49:05 -0700418 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700419 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700420 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700421 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000422 }
423
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000424 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
425 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700426 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427
robertphillips0152d732016-05-20 06:38:43 -0700428 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000429
joshualitt8059eb92014-12-29 15:10:07 -0800430 // Set the matrix so that rendered clip elements are transformed to mask space from clip
431 // space.
432 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700433 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800434
robertphillips98377402016-05-13 05:47:23 -0700435 helper.init(maskSpaceIBounds, &translate);
csmartdaltond211e782016-08-15 11:17:19 -0700436 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000437
csmartdalton77f2fae2016-08-08 09:55:06 -0700438 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000439 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700440 SkCanvas::ClipOp op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000441
reed73603f32016-09-20 08:42:38 -0700442 if (SkCanvas::kIntersect_Op == op || SkCanvas::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443 // Intersect and reverse difference require modifying pixels outside of the geometry
444 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
445 // but leave the pixels inside the geometry alone. For reverse difference we invert all
446 // the pixels before clearing the ones outside the geometry.
reed73603f32016-09-20 08:42:38 -0700447 if (SkCanvas::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700448 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000449 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700450 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000451 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000452 SkPath clipPath;
453 element->asPath(&clipPath);
454 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700455 GrShape shape(clipPath, GrStyle::SimpleFill());
456 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000457 continue;
458 }
459
460 // The other ops (union, xor, diff) only affect pixels inside
461 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000462 if (Element::kRect_Type == element->getType()) {
reed73603f32016-09-20 08:42:38 -0700463 helper.drawRect(element->getRect(), (SkRegion::Op)op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000464 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000465 SkPath path;
466 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700467 GrShape shape(path, GrStyle::SimpleFill());
reed73603f32016-09-20 08:42:38 -0700468 helper.drawShape(shape, (SkRegion::Op)op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000469 }
470 }
471
krajcevskiad1dc582014-06-10 15:06:47 -0700472 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800473 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700474 desc.fWidth = reducedClip.width();
475 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800476 desc.fConfig = kAlpha_8_GrPixelConfig;
477
robertphillips0152d732016-05-20 06:38:43 -0700478 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800479 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700480 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700481 }
robertphillips391395d2016-03-02 09:26:36 -0800482 result->resourcePriv().setUniqueKey(key);
483
robertphillipsc99b8f02016-05-15 07:53:35 -0700484 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000485
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000486 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000487}