blob: 67c6f6eac30a547f3acc5967293792e622f5fdd8 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
robertphillips68737822015-10-29 12:12:21 -070010#include "GrDrawingManager.h"
robertphillips391395d2016-03-02 09:26:36 -080011#include "GrDrawContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070012#include "GrGpuResourcePriv.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070017#include "GrResourceProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070020#include "SkRasterClip.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070021#include "SkTLazy.h"
joshualitta8b84992016-01-13 13:35:35 -080022#include "batches/GrRectBatchFactory.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080023#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080025#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080026#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000027
bsalomon@google.com8182fa02012-12-04 14:06:06 +000028typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070029typedef GrReducedClip::InitialState InitialState;
bsalomon@google.com51a62862012-11-26 21:19:43 +000030
robertphillips976f5f02016-06-03 10:59:20 -070031static const int kMaxAnalyticElements = 4;
32
bsalomon@google.com51a62862012-11-26 21:19:43 +000033////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000035// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070036static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
37 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000038 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080039 // We use device coords to compute the texture coordinates. We set our matrix to be a
40 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000043 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000045 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070046 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070047 result,
brianosman54f30c12016-07-18 10:53:52 -070048 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070049 mat,
50 GrTextureDomain::MakeTexelDomain(result, domainTexels),
51 GrTextureDomain::kDecal_Mode,
52 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070053 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000054}
55
robertphillips3f7357f2015-10-27 07:17:33 -070056// Does the path in 'element' require SW rendering? If so, return true (and,
57// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
58// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070059bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070060 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070061 const GrDrawContext* drawContext,
robertphillips68737822015-10-29 12:12:21 -070062 const SkMatrix& viewMatrix,
63 const Element* element,
64 GrPathRenderer** prOut,
65 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070066 if (Element::kRect_Type == element->getType()) {
67 // rects can always be drawn directly w/o using the software path
68 // TODO: skip rrects once we're drawing them directly.
69 if (prOut) {
70 *prOut = nullptr;
71 }
72 return false;
73 } else {
74 // We shouldn't get here with an empty clip element.
75 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070076
robertphillips3f7357f2015-10-27 07:17:33 -070077 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
78 SkPath path;
79 element->asPath(&path);
80 if (path.isInverseFillType()) {
81 path.toggleInverseFillType();
82 }
halcanary9d524f22016-03-29 09:03:52 -070083
robertphillips3f7357f2015-10-27 07:17:33 -070084 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070085
robertphillips423e3372015-10-27 09:23:38 -070086 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070087 type = element->isAA()
88 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
89 : GrPathRendererChain::kStencilAndColor_DrawType;
90 } else {
91 type = element->isAA()
92 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -070093 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -070094 }
halcanary9d524f22016-03-29 09:03:52 -070095
bsalomon8acedde2016-06-24 10:42:16 -070096 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -070097 GrPathRenderer::CanDrawPathArgs canDrawArgs;
98 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
99 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700100 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700101 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700102 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700103 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700104
robertphillips3f7357f2015-10-27 07:17:33 -0700105 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700106 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700107 if (prOut) {
108 *prOut = pr;
109 }
110 return SkToBool(!pr);
111 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000112}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000113
robertphillips@google.comfa662942012-05-17 12:20:22 +0000114/*
115 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
116 * will be used on any element. If so, it returns true to indicate that the
117 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
118 */
robertphillips391395d2016-03-02 09:26:36 -0800119bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
robertphillips59cf61a2016-07-13 09:18:21 -0700120 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700121 const GrDrawContext* drawContext,
joshualitt8059eb92014-12-29 15:10:07 -0800122 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800123 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000124 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000125 // a clip gets complex enough it can just be done in SW regardless
126 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000127
joshualitt8059eb92014-12-29 15:10:07 -0800128 // Set the matrix so that rendered clip elements are transformed to mask space from clip
129 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700130 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800131
tfarinabf54e492014-10-23 17:47:18 -0700132 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000133 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700134
135 SkRegion::Op op = element->getOp();
136 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700137 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700138 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700139
robertphillips59cf61a2016-07-13 09:18:21 -0700140 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700141 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700142 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000143 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000144 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000145 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000146}
147
robertphillips976f5f02016-06-03 10:59:20 -0700148static bool get_analytic_clip_processor(const GrReducedClip::ElementList& elements,
149 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700150 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700151 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700152 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000153 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700154 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700155 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700156 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
tfarinabf54e492014-10-23 17:47:18 -0700157 GrReducedClip::ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700158 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000159 SkRegion::Op op = iter.get()->getOp();
160 bool invert;
161 bool skip = false;
162 switch (op) {
163 case SkRegion::kReplace_Op:
164 SkASSERT(iter.get() == elements.head());
165 // Fallthrough, handled same as intersect.
166 case SkRegion::kIntersect_Op:
167 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700168 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 skip = true;
170 }
171 break;
172 case SkRegion::kDifference_Op:
173 invert = true;
174 // We don't currently have a cheap test for whether a rect is fully outside an
175 // element's primitive, so don't attempt to set skip.
176 break;
177 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700178 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000179 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000180 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700181 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800182 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700183 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700184 return false;
bsalomona912dde2015-10-14 15:01:50 -0700185 }
joshualittb0a8a372014-09-23 09:50:21 -0700186 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700187 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000188 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700189 edgeType =
190 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 }
bsalomona912dde2015-10-14 15:01:50 -0700192
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000193 switch (iter.get()->getType()) {
194 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700195 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
196 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000197 break;
198 case SkClipStack::Element::kRRect_Type: {
199 SkRRect rrect = iter.get()->getRRect();
200 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700201 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 break;
203 }
204 case SkClipStack::Element::kRect_Type: {
205 SkRect rect = iter.get()->getRect();
206 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700207 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 break;
209 }
210 default:
211 break;
212 }
bungeman06ca8ec2016-06-09 08:01:03 -0700213 if (!fps.back()) {
214 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000215 }
216 }
mtklein217daa72014-07-02 12:55:21 -0700217 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000218 }
219
bsalomon0b5b6b22015-10-14 08:31:34 -0700220 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700221 if (fps.count()) {
222 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000223 }
bungeman06ca8ec2016-06-09 08:01:03 -0700224 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000225}
226
robertphillips@google.comf294b772012-04-27 14:29:26 +0000227////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000228// sort out what kind of clip mask needs to be created: alpha, stencil,
229// scissor, or entirely software
robertphillips976f5f02016-06-03 10:59:20 -0700230bool GrClipMaskManager::SetupClipping(GrContext* context,
robertphillips976f5f02016-06-03 10:59:20 -0700231 GrDrawContext* drawContext,
cdalton846c0512016-05-13 10:25:00 -0700232 const GrClipStackClip& clip,
bsalomonbd2bbe42016-07-08 07:36:42 -0700233 const SkRect* origDevBounds,
robertphillips59cf61a2016-07-13 09:18:21 -0700234 bool useHWAA,
235 bool hasUserStencilSettings,
bsalomon0ba8c242015-10-07 09:20:28 -0700236 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700237 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
238 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700239 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000240
csmartdaltoncbecb082016-07-22 08:59:08 -0700241 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
242 if (origDevBounds && !devBounds.intersect(*origDevBounds)) {
243 return false;
244 }
245
246 const SkScalar clipX = SkIntToScalar(clip.origin().x()),
247 clipY = SkIntToScalar(clip.origin().y());
248
bsalomonf045d602015-11-18 19:01:12 -0800249 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800250 int32_t genID = 0;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000251 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800252 bool requiresAA = false;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000253
csmartdaltoncbecb082016-07-22 08:59:08 -0700254 InitialState initialState = GrReducedClip::ReduceClipStack(*clip.clipStack(),
255 devBounds.makeOffset(clipX, clipY),
256 &elements,
257 &genID,
258 &clipSpaceIBounds,
259 &requiresAA);
cdalton846c0512016-05-13 10:25:00 -0700260 if (elements.isEmpty()) {
bsalomonbd2bbe42016-07-08 07:36:42 -0700261 if (GrReducedClip::kAllOut_InitialState == initialState) {
cdalton846c0512016-05-13 10:25:00 -0700262 return false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700263 } else {
264 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
265 scissorSpaceIBounds.offset(-clip.origin());
csmartdaltoncbecb082016-07-22 08:59:08 -0700266 if (!GrClip::IsInsideClip(scissorSpaceIBounds, devBounds)) {
bsalomonbd2bbe42016-07-08 07:36:42 -0700267 out->makeScissored(scissorSpaceIBounds);
268 }
269 return true;
cdalton846c0512016-05-13 10:25:00 -0700270 }
271 }
cdalton93a379b2016-05-11 13:58:08 -0700272
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000273 // An element count of 4 was chosen because of the common pattern in Blink of:
274 // isect RR
275 // diff RR
276 // isect convex_poly
277 // isect convex_poly
278 // when drawing rounded div borders. This could probably be tuned based on a
279 // configuration's relative costs of switching RTs to generate a mask vs
280 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700281 if (elements.count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800282 // When there are multiple samples we want to do per-sample clipping, not compute a
283 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700284 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
285 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700286 // With a single color sample, any coverage info is lost from color once it hits the
287 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
288 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700289 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700290 }
bungeman06ca8ec2016-06-09 08:01:03 -0700291 sk_sp<GrFragmentProcessor> clipFP;
bsalomonbd2bbe42016-07-08 07:36:42 -0700292 if (requiresAA &&
csmartdaltoncbecb082016-07-22 08:59:08 -0700293 get_analytic_clip_processor(elements, disallowAnalyticAA, {-clipX, -clipY}, devBounds,
bsalomonbd2bbe42016-07-08 07:36:42 -0700294 &clipFP)) {
mtklein217daa72014-07-02 12:55:21 -0700295 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800296 scissorSpaceIBounds.offset(-clip.origin());
csmartdaltoncbecb082016-07-22 08:59:08 -0700297 if (GrClip::IsInsideClip(scissorSpaceIBounds, devBounds)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700298 out->makeFPBased(std::move(clipFP), SkRect::Make(scissorSpaceIBounds));
299 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700300 out->makeScissoredFPBased(std::move(clipFP), scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000301 }
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000302 return true;
303 }
304 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000305
cdaltonede75742015-11-11 15:27:57 -0800306 // If the stencil buffer is multisampled we can use it to do everything.
robertphillips976f5f02016-06-03 10:59:20 -0700307 if (!drawContext->isStencilBufferMultisampled() && requiresAA) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700308 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000309
joshualitt8059eb92014-12-29 15:10:07 -0800310 // The top-left of the mask corresponds to the top-left corner of the bounds.
311 SkVector clipToMaskOffset = {
312 SkIntToScalar(-clipSpaceIBounds.fLeft),
313 SkIntToScalar(-clipSpaceIBounds.fTop)
314 };
315
robertphillips59cf61a2016-07-13 09:18:21 -0700316 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
robertphillips976f5f02016-06-03 10:59:20 -0700317 clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000318 // The clip geometry is complex enough that it will be more efficient to create it
319 // entirely in software
robertphillips976f5f02016-06-03 10:59:20 -0700320 result = CreateSoftwareClipMask(context->textureProvider(),
robertphillipsc99b8f02016-05-15 07:53:35 -0700321 genID,
322 initialState,
323 elements,
324 clipToMaskOffset,
325 clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700327 result = CreateAlphaClipMask(context,
robertphillipsc99b8f02016-05-15 07:53:35 -0700328 genID,
329 initialState,
330 elements,
331 clipToMaskOffset,
332 clipSpaceIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800333 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700334 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000335 }
336
bsalomon49f085d2014-09-05 13:34:00 -0700337 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000338 // The mask's top left coord should be pinned to the rounded-out top left corner of
339 // clipSpace bounds. We determine the mask's position WRT to the render target here.
340 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800341 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon6cc90062016-07-08 11:31:22 -0700342 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds),
343 SkRect::Make(rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000344 return true;
345 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000346 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000347 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000349 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800350 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
robertphillips976f5f02016-06-03 10:59:20 -0700351 CreateStencilClipMask(context,
352 drawContext,
353 genID,
354 initialState,
355 elements,
356 clipSpaceIBounds,
357 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000358
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000359 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
360 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
361 // use both stencil and scissor test to the bounds for the final draw.
362 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
363 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
csmartdaltoncbecb082016-07-22 08:59:08 -0700364 if (GrClip::IsInsideClip(scissorSpaceIBounds, devBounds)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700365 out->makeStencil(true, devBounds);
366 } else {
367 out->makeScissoredStencil(scissorSpaceIBounds);
368 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000369 return true;
370}
371
robertphillips391395d2016-03-02 09:26:36 -0800372static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700373 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700374 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800375 const SkMatrix& viewMatrix,
376 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800377
378 // TODO: Draw rrects directly here.
379 switch (element->getType()) {
380 case Element::kEmpty_Type:
381 SkDEBUGFAIL("Should never get here with an empty element.");
382 break;
robertphillips391395d2016-03-02 09:26:36 -0800383 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700384 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800385 element->getOp(),
386 element->isInverseFilled(),
387 element->isAA(),
388 viewMatrix, element->getRect());
389 break;
robertphillips86c60752016-03-02 08:43:13 -0800390 default: {
391 SkPath path;
392 element->asPath(&path);
393 if (path.isInverseFillType()) {
394 path.toggleInverseFillType();
395 }
396
cdalton862cff32016-05-12 15:09:48 -0700397 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800398 element->getOp(),
399 element->isInverseFilled(),
400 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800401 break;
402 }
403 }
robertphillips391395d2016-03-02 09:26:36 -0800404
405 return false;
406}
407
408static void draw_element(GrDrawContext* dc,
409 const GrClip& clip, // TODO: can this just always be WideOpen?
410 const GrPaint &paint,
411 const SkMatrix& viewMatrix,
412 const SkClipStack::Element* element) {
413
414 // TODO: Draw rrects directly here.
415 switch (element->getType()) {
416 case Element::kEmpty_Type:
417 SkDEBUGFAIL("Should never get here with an empty element.");
418 break;
419 case Element::kRect_Type:
420 dc->drawRect(clip, paint, viewMatrix, element->getRect());
421 break;
422 default: {
423 SkPath path;
424 element->asPath(&path);
425 if (path.isInverseFillType()) {
426 path.toggleInverseFillType();
427 }
428
bsalomon6663acf2016-05-10 09:14:17 -0700429 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800430 break;
431 }
432 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433}
434
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000435////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700436// Create a 8-bit clip mask in alpha
437
438static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
439 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
440 GrUniqueKey::Builder builder(key, kDomain, 3);
441 builder[0] = clipGenID;
442 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
443 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
444}
445
robertphillipsc99b8f02016-05-15 07:53:35 -0700446sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
447 int32_t elementsGenID,
448 GrReducedClip::InitialState initialState,
449 const GrReducedClip::ElementList& elements,
450 const SkVector& clipToMaskOffset,
451 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800452 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700453 GrUniqueKey key;
454 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
455 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700456 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000457 }
458
robertphillips544b9aa2015-10-28 11:01:41 -0700459 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700460 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800461 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700462 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800463 }
464
robertphillipsc99b8f02016-05-15 07:53:35 -0700465 sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox,
466 clipSpaceIBounds.width(),
467 clipSpaceIBounds.height(),
brianosmandfe4f2e2016-07-21 13:28:36 -0700468 config, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800469 if (!dc) {
470 return nullptr;
471 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700472
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000473 // The texture may be larger than necessary, this rect represents the part of the texture
474 // we populate with a rasterization of the clip.
475 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
476
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000477 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
478 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800479 dc->clear(&maskSpaceIBounds,
480 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
481 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000482
robertphillips391395d2016-03-02 09:26:36 -0800483 // Set the matrix so that rendered clip elements are transformed to mask space from clip
484 // space.
485 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
486
487 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000488 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
489 // pass must not set values outside of this bounds or stencil values outside the rect won't be
490 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800491
robertphillips@google.comf294b772012-04-27 14:29:26 +0000492 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700493 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000494 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000495 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000496 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000497 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700498 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700499
robertphillips391395d2016-03-02 09:26:36 -0800500 // draw directly into the result with the stencil set to make the pixels affected
501 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700502 static constexpr GrUserStencilSettings kStencilInElement(
503 GrUserStencilSettings::StaticInit<
504 0xffff,
505 GrUserStencilTest::kAlways,
506 0xffff,
507 GrUserStencilOp::kReplace,
508 GrUserStencilOp::kReplace,
509 0xffff>()
510 );
cdalton862cff32016-05-12 15:09:48 -0700511 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800512 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800513 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000514 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000515
robertphillips391395d2016-03-02 09:26:36 -0800516 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700517 static constexpr GrUserStencilSettings kDrawOutsideElement(
518 GrUserStencilSettings::StaticInit<
519 0x0000,
520 GrUserStencilTest::kEqual,
521 0xffff,
522 GrUserStencilOp::kZero,
523 GrUserStencilOp::kZero,
524 0xffff>()
525 );
cdalton862cff32016-05-12 15:09:48 -0700526 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800527 op, !invert, false,
528 translate,
529 SkRect::Make(clipSpaceIBounds))) {
robertphillips391395d2016-03-02 09:26:36 -0800530 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000531 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000532 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800533 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800534 GrPaint paint;
535 paint.setAntiAlias(element->isAA());
536 paint.setCoverageSetOpXPFactory(op, false);
537
cdalton846c0512016-05-13 10:25:00 -0700538 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000539 }
540 }
541
robertphillipsc99b8f02016-05-15 07:53:35 -0700542 sk_sp<GrTexture> texture(dc->asTexture());
543 SkASSERT(texture);
544 texture->resourcePriv().setUniqueKey(key);
545 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000546}
547
548////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000549// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000550// (as opposed to canvas) coordinates
robertphillips976f5f02016-06-03 10:59:20 -0700551bool GrClipMaskManager::CreateStencilClipMask(GrContext* context,
552 GrDrawContext* drawContext,
joshualitt9853cce2014-11-17 14:22:48 -0800553 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700554 GrReducedClip::InitialState initialState,
555 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000556 const SkIRect& clipSpaceIBounds,
557 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700558 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000559
robertphillips976f5f02016-06-03 10:59:20 -0700560 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
561 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700562 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000563 return false;
564 }
565
robertphillips976f5f02016-06-03 10:59:20 -0700566 // TODO: these need to be swapped over to using a StencilAttachmentProxy
egdaniel8dc7c3a2015-04-16 11:22:42 -0700567 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
568 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000569 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
570 SkVector translate = {
571 SkIntToScalar(clipSpaceToStencilOffset.fX),
572 SkIntToScalar(clipSpaceToStencilOffset.fY)
573 };
joshualitt8059eb92014-12-29 15:10:07 -0800574 SkMatrix viewMatrix;
575 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000576
bsalomon@google.com9f131742012-12-13 20:43:56 +0000577 // We set the current clip to the bounds so that our recursive draws are scissored to them.
578 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
579 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700580 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000581
robertphillips976f5f02016-06-03 10:59:20 -0700582 drawContext->drawContextPriv().clearStencilClip(
583 stencilSpaceIBounds,
584 GrReducedClip::kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000585
586 // walk through each clip element and perform its set op
587 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700588 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000589 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700590 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800591
tomhudson@google.com8afae612012-08-14 15:03:35 +0000592 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000593 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700594 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000595
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000596 // This will be used to determine whether the clip shape can be rendered into the
597 // stencil with arbitrary stencil settings.
598 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000599
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000600 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000601
halcanary96fcdcc2015-08-27 07:41:13 -0700602 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000603 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000604 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000605 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000606 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000607 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000608 element->asPath(&clipPath);
609 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000610 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000611 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000612 }
robertphillips68737822015-10-29 12:12:21 -0700613
bsalomon8acedde2016-06-24 10:42:16 -0700614 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700615 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700616 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700617 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700618 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700619 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700620 canDrawArgs.fHasUserStencilSettings = false;
621 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700622
robertphillips976f5f02016-06-03 10:59:20 -0700623 pr = context->drawingManager()->getPathRenderer(canDrawArgs, false,
624 GrPathRendererChain::kStencilOnly_DrawType,
625 &stencilSupport);
626 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627 return false;
628 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000629 }
630
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000631 bool canRenderDirectToStencil =
632 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700633 bool drawDirectToClip; // Given the renderer, the element,
634 // fill rule, and set operation should
635 // we render the element directly to
636 // stencil bit used for clipping.
637 GrUserStencilSettings const* const* stencilPasses =
638 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
639 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000640
641 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700642 if (!drawDirectToClip) {
643 static constexpr GrUserStencilSettings kDrawToStencil(
644 GrUserStencilSettings::StaticInit<
645 0x0000,
646 GrUserStencilTest::kAlways,
647 0xffff,
648 GrUserStencilOp::kIncMaybeClamp,
649 GrUserStencilOp::kIncMaybeClamp,
650 0xffff>()
651 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000652 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700653 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
654 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000655 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000656 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700657 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000658 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700659 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700660 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700661 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700662
663 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700664 args.fResourceProvider = context->resourceProvider();
665 args.fPaint = &paint;
666 args.fUserStencilSettings = &kDrawToStencil;
667 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700668 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700669 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700670 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700671 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700672 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700673 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000674 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700675 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700676 args.fResourceProvider = context->resourceProvider();
677 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700678 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700679 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700680 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700681 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700682 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000683 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000684 }
685 }
686 }
687
688 // now we modify the clip bit by rendering either the clip
689 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700690 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
joshualitt9853cce2014-11-17 14:22:48 -0800691
cdalton93a379b2016-05-11 13:58:08 -0700692 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000693 if (Element::kRect_Type == element->getType()) {
bsalomon6cc90062016-07-08 11:31:22 -0700694 clip.enableStencilClip(element->getRect().makeOffset(translate.fX,
695 translate.fY));
csmartdalton656dbe42016-06-10 12:32:57 -0700696 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
697 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700699 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700700 GrPaint paint;
bsalomon6cc90062016-07-08 11:31:22 -0700701 SkRect bounds = clipPath.getBounds();
702 bounds.offset(translate.fX, translate.fY);
703 clip.enableStencilClip(bounds);
bungeman06ca8ec2016-06-09 08:01:03 -0700704 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700705 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700706 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700707 args.fResourceProvider = context->resourceProvider();
708 args.fPaint = &paint;
709 args.fUserStencilSettings = *pass;
710 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700711 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700712 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700713 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700714 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700715 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700716 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717 }
718 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000719 // The view matrix is setup to do clip space -> stencil space translation, so
720 // draw rect in clip space.
bsalomon6cc90062016-07-08 11:31:22 -0700721 SkRect bounds = SkRect::Make(clipSpaceIBounds);
722 bounds.offset(translate.fX, translate.fY);
723 clip.enableStencilClip(bounds);
csmartdalton656dbe42016-06-10 12:32:57 -0700724 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
725 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 }
727 }
728 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730 return true;
731}
732
bsalomon@google.com411dad02012-06-05 20:24:20 +0000733////////////////////////////////////////////////////////////////////////////////
robertphillipsc99b8f02016-05-15 07:53:35 -0700734sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
robertphillips0152d732016-05-20 06:38:43 -0700735 GrTextureProvider* texProvider,
robertphillipsc99b8f02016-05-15 07:53:35 -0700736 int32_t elementsGenID,
737 GrReducedClip::InitialState initialState,
738 const GrReducedClip::ElementList& elements,
739 const SkVector& clipToMaskOffset,
740 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700741 GrUniqueKey key;
742 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips0152d732016-05-20 06:38:43 -0700743 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700744 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000745 }
746
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000747 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
748 // the top left corner of the resulting rect to the top left of the texture.
749 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
750
robertphillips0152d732016-05-20 06:38:43 -0700751 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000752
joshualitt8059eb92014-12-29 15:10:07 -0800753 // Set the matrix so that rendered clip elements are transformed to mask space from clip
754 // space.
755 SkMatrix translate;
756 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800757
robertphillips98377402016-05-13 05:47:23 -0700758 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700759 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000760
tfarinabf54e492014-10-23 17:47:18 -0700761 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000762 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000763 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000764
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000765 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
766 // Intersect and reverse difference require modifying pixels outside of the geometry
767 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
768 // but leave the pixels inside the geometry alone. For reverse difference we invert all
769 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000770 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000771 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000772 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700773 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000774 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000775 SkPath clipPath;
776 element->asPath(&clipPath);
777 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700778 GrShape shape(clipPath, GrStyle::SimpleFill());
779 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000780 continue;
781 }
782
783 // The other ops (union, xor, diff) only affect pixels inside
784 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000785 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700786 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000787 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000788 SkPath path;
789 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700790 GrShape shape(path, GrStyle::SimpleFill());
791 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000792 }
793 }
794
krajcevskiad1dc582014-06-10 15:06:47 -0700795 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800796 GrSurfaceDesc desc;
797 desc.fWidth = clipSpaceIBounds.width();
798 desc.fHeight = clipSpaceIBounds.height();
799 desc.fConfig = kAlpha_8_GrPixelConfig;
800
robertphillips0152d732016-05-20 06:38:43 -0700801 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800802 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700803 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700804 }
robertphillips391395d2016-03-02 09:26:36 -0800805 result->resourcePriv().setUniqueKey(key);
806
robertphillipsc99b8f02016-05-15 07:53:35 -0700807 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000808
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000809 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000810}