blob: 003b4a5d439bb63782dd1370ea6bc7a0f94b4172 [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"
robertphillips391395d2016-03-02 09:26:36 -080013#include "GrDrawContextPriv.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////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000083// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070084static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
85 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000086 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080087 // We use device coords to compute the texture coordinates. We set our matrix to be a
88 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000089 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000091 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000092
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000093 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070094 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070095 result,
brianosman54f30c12016-07-18 10:53:52 -070096 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070097 mat,
98 GrTextureDomain::MakeTexelDomain(result, domainTexels),
99 GrTextureDomain::kDecal_Mode,
100 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -0700101 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000102}
103
robertphillips3f7357f2015-10-27 07:17:33 -0700104// Does the path in 'element' require SW rendering? If so, return true (and,
105// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
106// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -0700107bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
108 bool hasUserStencilSettings,
109 const GrDrawContext* drawContext,
110 const SkMatrix& viewMatrix,
111 const Element* element,
112 GrPathRenderer** prOut,
113 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700114 if (Element::kRect_Type == element->getType()) {
115 // rects can always be drawn directly w/o using the software path
116 // TODO: skip rrects once we're drawing them directly.
117 if (prOut) {
118 *prOut = nullptr;
119 }
120 return false;
121 } else {
122 // We shouldn't get here with an empty clip element.
123 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700124
robertphillips3f7357f2015-10-27 07:17:33 -0700125 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
126 SkPath path;
127 element->asPath(&path);
128 if (path.isInverseFillType()) {
129 path.toggleInverseFillType();
130 }
halcanary9d524f22016-03-29 09:03:52 -0700131
robertphillips3f7357f2015-10-27 07:17:33 -0700132 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700133
robertphillips423e3372015-10-27 09:23:38 -0700134 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700135 type = element->isAA()
136 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
137 : GrPathRendererChain::kStencilAndColor_DrawType;
138 } else {
139 type = element->isAA()
140 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700141 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700142 }
halcanary9d524f22016-03-29 09:03:52 -0700143
bsalomon8acedde2016-06-24 10:42:16 -0700144 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700145 GrPathRenderer::CanDrawPathArgs canDrawArgs;
146 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
147 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700148 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700149 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700150 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700151 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700152
robertphillips3f7357f2015-10-27 07:17:33 -0700153 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700154 GrPathRenderer* pr =
155 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700156 if (prOut) {
157 *prOut = pr;
158 }
159 return SkToBool(!pr);
160 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000161}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000162
robertphillips@google.comfa662942012-05-17 12:20:22 +0000163/*
164 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
165 * will be used on any element. If so, it returns true to indicate that the
166 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
167 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700168bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
169 bool hasUserStencilSettings,
170 const GrDrawContext* drawContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700171 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000172 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000173 // a clip gets complex enough it can just be done in SW regardless
174 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000175
joshualitt8059eb92014-12-29 15:10:07 -0800176 // Set the matrix so that rendered clip elements are transformed to mask space from clip
177 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700178 SkMatrix translate;
179 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800180
csmartdaltonbde96c62016-08-31 12:54:46 -0700181 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000182 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700183
184 SkRegion::Op op = element->getOp();
185 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700186 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700187 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700188
robertphillips59cf61a2016-07-13 09:18:21 -0700189 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700190 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700191 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000192 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000193 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000194 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000195}
196
csmartdalton77f2fae2016-08-08 09:55:06 -0700197static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700198 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700199 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700200 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700201 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700203 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700204 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700205 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700206 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700207 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 SkRegion::Op op = iter.get()->getOp();
209 bool invert;
210 bool skip = false;
211 switch (op) {
212 case SkRegion::kReplace_Op:
213 SkASSERT(iter.get() == elements.head());
214 // Fallthrough, handled same as intersect.
215 case SkRegion::kIntersect_Op:
216 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700217 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000218 skip = true;
219 }
220 break;
221 case SkRegion::kDifference_Op:
222 invert = true;
223 // We don't currently have a cheap test for whether a rect is fully outside an
224 // element's primitive, so don't attempt to set skip.
225 break;
226 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700227 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000229 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700230 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800231 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700232 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700233 return false;
bsalomona912dde2015-10-14 15:01:50 -0700234 }
joshualittb0a8a372014-09-23 09:50:21 -0700235 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700236 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000237 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700238 edgeType =
239 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 }
bsalomona912dde2015-10-14 15:01:50 -0700241
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000242 switch (iter.get()->getType()) {
243 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700244 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
245 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000246 break;
247 case SkClipStack::Element::kRRect_Type: {
248 SkRRect rrect = iter.get()->getRRect();
249 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700250 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000251 break;
252 }
253 case SkClipStack::Element::kRect_Type: {
254 SkRect rect = iter.get()->getRect();
255 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700256 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000257 break;
258 }
259 default:
260 break;
261 }
bungeman06ca8ec2016-06-09 08:01:03 -0700262 if (!fps.back()) {
263 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000264 }
265 }
mtklein217daa72014-07-02 12:55:21 -0700266 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000267 }
268
bsalomon0b5b6b22015-10-14 08:31:34 -0700269 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700270 if (fps.count()) {
271 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000272 }
bungeman06ca8ec2016-06-09 08:01:03 -0700273 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000274}
275
robertphillips@google.comf294b772012-04-27 14:29:26 +0000276////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000277// sort out what kind of clip mask needs to be created: alpha, stencil,
278// scissor, or entirely software
csmartdaltond211e782016-08-15 11:17:19 -0700279bool GrClipStackClip::apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA,
280 bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700281 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700282 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700283 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000284
csmartdaltoncbecb082016-07-22 08:59:08 -0700285 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700286 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700287 return false;
288 }
289
csmartdaltonc6f411e2016-08-05 22:32:12 -0700290 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
291 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700292
csmartdalton77f2fae2016-08-08 09:55:06 -0700293 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
294 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000295
csmartdaltond211e782016-08-15 11:17:19 -0700296 if (reducedClip.hasIBounds() &&
297 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
298 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
299 scissorSpaceIBounds.offset(-fOrigin);
300 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700301 }
cdalton93a379b2016-05-11 13:58:08 -0700302
csmartdaltond211e782016-08-15 11:17:19 -0700303 if (reducedClip.elements().isEmpty()) {
304 return InitialState::kAllIn == reducedClip.initialState();
305 }
306
307 SkASSERT(reducedClip.hasIBounds());
308
csmartdalton28341fa2016-08-17 10:00:21 -0700309 // Attempt to implement difference clip rects with window rectangles. This will eventually
310 // become more comprehensive.
311 if (drawContext->accessRenderTarget()->renderTargetPriv().supportsWindowRectangles() &&
312 1 == reducedClip.elements().count() && !reducedClip.requiresAA() &&
313 InitialState::kAllIn == reducedClip.initialState()) {
314 const Element* element = reducedClip.elements().head();
315 SkRegion::Op op = element->getOp();
316 if (Element::kRect_Type == element->getType() &&
317 (SkRegion::kDifference_Op == op || SkRegion::kXOR_Op == op)) {
318 SkIRect window;
319 element->getRect().round(&window);
320 window.offset(-fOrigin);
321 out->addWindowRectangle(window);
322 return true;
323 }
324 }
325
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000326 // An element count of 4 was chosen because of the common pattern in Blink of:
327 // isect RR
328 // diff RR
329 // isect convex_poly
330 // isect convex_poly
331 // when drawing rounded div borders. This could probably be tuned based on a
332 // configuration's relative costs of switching RTs to generate a mask vs
333 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700334 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800335 // When there are multiple samples we want to do per-sample clipping, not compute a
336 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700337 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
338 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700339 // With a single color sample, any coverage info is lost from color once it hits the
340 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
341 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700342 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700343 }
bungeman06ca8ec2016-06-09 08:01:03 -0700344 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700345 if (reducedClip.requiresAA() &&
346 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
347 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700348 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000349 return true;
350 }
351 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000352
cdaltonede75742015-11-11 15:27:57 -0800353 // If the stencil buffer is multisampled we can use it to do everything.
csmartdalton77f2fae2016-08-08 09:55:06 -0700354 if (!drawContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700355 sk_sp<GrTexture> result;
csmartdaltonbde96c62016-08-31 12:54:46 -0700356 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000357 // The clip geometry is complex enough that it will be more efficient to create it
358 // entirely in software
csmartdaltonbde96c62016-08-31 12:54:46 -0700359 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000360 } else {
csmartdaltonbde96c62016-08-31 12:54:46 -0700361 result = CreateAlphaClipMask(context, reducedClip);
robertphillips391395d2016-03-02 09:26:36 -0800362 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700363 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000364 }
365
bsalomon49f085d2014-09-05 13:34:00 -0700366 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000367 // The mask's top left coord should be pinned to the rounded-out top left corner of
368 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700369 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700370 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700371 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372 return true;
373 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000374 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000375 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000377 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonbde96c62016-08-31 12:54:46 -0700378 // TODO: these need to be swapped over to using a StencilAttachmentProxy
379 GrStencilAttachment* stencilAttachment =
380 context->resourceProvider()->attachStencilAttachment(drawContext->accessRenderTarget());
381 if (nullptr == stencilAttachment) {
382 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
383 return true;
384 }
385
386 if (stencilAttachment->mustRenderClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
387 fOrigin)) {
388 reducedClip.drawStencilClipMask(context, drawContext, fOrigin);
389 stencilAttachment->setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
390 fOrigin);
391 }
csmartdaltond211e782016-08-15 11:17:19 -0700392 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000393 return true;
394}
395
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000396////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700397// Create a 8-bit clip mask in alpha
398
399static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
400 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
401 GrUniqueKey::Builder builder(key, kDomain, 3);
402 builder[0] = clipGenID;
403 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
404 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
405}
406
csmartdaltonc6f411e2016-08-05 22:32:12 -0700407sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdaltonbde96c62016-08-31 12:54:46 -0700408 const GrReducedClip& reducedClip) {
robertphillips391395d2016-03-02 09:26:36 -0800409 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700410 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700411 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700412 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700413 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000414 }
415
robertphillips544b9aa2015-10-28 11:01:41 -0700416 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700417 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800418 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700419 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800420 }
421
robertphillips6738c702016-07-27 12:13:51 -0700422 sk_sp<GrDrawContext> dc(context->makeDrawContext(SkBackingFit::kApprox,
csmartdalton77f2fae2016-08-08 09:55:06 -0700423 reducedClip.width(),
424 reducedClip.height(),
robertphillips6738c702016-07-27 12:13:51 -0700425 config, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800426 if (!dc) {
427 return nullptr;
428 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000429
csmartdaltonbde96c62016-08-31 12:54:46 -0700430 if (!reducedClip.drawAlphaClipMask(dc.get())) {
431 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000432 }
433
robertphillipsc99b8f02016-05-15 07:53:35 -0700434 sk_sp<GrTexture> texture(dc->asTexture());
435 SkASSERT(texture);
436 texture->resourcePriv().setUniqueKey(key);
437 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438}
439
csmartdaltonc6f411e2016-08-05 22:32:12 -0700440sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdaltonbde96c62016-08-31 12:54:46 -0700441 const GrReducedClip& reducedClip) {
bsalomon473addf2015-10-02 07:49:05 -0700442 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700443 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700444 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700445 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000446 }
447
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000448 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
449 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700450 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000451
robertphillips0152d732016-05-20 06:38:43 -0700452 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000453
joshualitt8059eb92014-12-29 15:10:07 -0800454 // Set the matrix so that rendered clip elements are transformed to mask space from clip
455 // space.
456 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700457 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800458
robertphillips98377402016-05-13 05:47:23 -0700459 helper.init(maskSpaceIBounds, &translate);
csmartdaltond211e782016-08-15 11:17:19 -0700460 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000461
csmartdalton77f2fae2016-08-08 09:55:06 -0700462 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000464 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000465
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000466 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
467 // Intersect and reverse difference require modifying pixels outside of the geometry
468 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
469 // but leave the pixels inside the geometry alone. For reverse difference we invert all
470 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000471 if (SkRegion::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700472 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000473 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700474 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000475 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000476 SkPath clipPath;
477 element->asPath(&clipPath);
478 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700479 GrShape shape(clipPath, GrStyle::SimpleFill());
480 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000481 continue;
482 }
483
484 // The other ops (union, xor, diff) only affect pixels inside
485 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000486 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700487 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000488 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000489 SkPath path;
490 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700491 GrShape shape(path, GrStyle::SimpleFill());
492 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000493 }
494 }
495
krajcevskiad1dc582014-06-10 15:06:47 -0700496 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800497 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700498 desc.fWidth = reducedClip.width();
499 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800500 desc.fConfig = kAlpha_8_GrPixelConfig;
501
robertphillips0152d732016-05-20 06:38:43 -0700502 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800503 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700504 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700505 }
robertphillips391395d2016-03-02 09:26:36 -0800506 result->resourcePriv().setUniqueKey(key);
507
robertphillipsc99b8f02016-05-15 07:53:35 -0700508 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000509
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000510 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000511}