blob: 0a64dd1e49d37be4e1249519126ce26893b06947 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000010#include "effects/GrTextureDomainEffect.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000011#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrStencilBuffer.h"
14#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000015#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000016#include "SkRasterClip.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000017#include "SkStroke.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000018#include "GrAAConvexPathRenderer.h"
19#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000021#include "GrCacheID.h"
22
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000023#include "SkTLazy.h"
24
robertphillips@google.com46a86002012-08-08 10:42:44 +000025GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000026
robertphillips@google.comba998f22012-10-12 11:33:56 +000027#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000028
bsalomon@google.com8182fa02012-12-04 14:06:06 +000029typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000030
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000031using namespace GrReducedClip;
32
bsalomon@google.com51a62862012-11-26 21:19:43 +000033////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000034namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000036// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037void setup_drawstate_aaclip(GrGpu* gpu,
38 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000039 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 GrDrawState* drawState = gpu->drawState();
41 GrAssert(drawState);
42
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000043 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044
bsalomon@google.comb9086a02012-11-01 18:02:54 +000045 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000048 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049 mat.preConcat(drawState->getViewMatrix());
50
bsalomon@google.com08283af2012-10-26 13:01:20 +000051 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052
53 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000054 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000055 drawState->stage(kMaskStage)->setEffect(
56 GrTextureDomainEffect::Create(result,
57 mat,
58 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
59 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000060}
61
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000062bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000063 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000064 const SkPath& origPath,
sugoi@google.com12b4e272012-12-06 20:13:11 +000065 const SkStroke& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000066 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000067 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
68 SkTCopyOnFirstWrite<SkPath> path(origPath);
69 if (path->isInverseFillType()) {
70 path.writable()->toggleInverseFillType();
71 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000072 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000073 GrPathRendererChain::DrawType type = doAA ?
74 GrPathRendererChain::kColorAntiAlias_DrawType :
75 GrPathRendererChain::kColor_DrawType;
76
77 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000078}
79
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000080}
81
robertphillips@google.comfa662942012-05-17 12:20:22 +000082/*
83 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
84 * will be used on any element. If so, it returns true to indicate that the
85 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
86 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000087bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000088
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000089 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000090 // a clip gets complex enough it can just be done in SW regardless
91 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com12b4e272012-12-06 20:13:11 +000092 SkStroke stroke;
93 stroke.setDoFill(true);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000094
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000095 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
96 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000097 // rects can always be drawn directly w/o using the software path
98 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000099 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000100 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000101 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000102 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000103 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000104 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000107 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000108}
109
robertphillips@google.comf294b772012-04-27 14:29:26 +0000110////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000111// sort out what kind of clip mask needs to be created: alpha, stencil,
112// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000113bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000114 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000115
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000116 ElementList elements(16);
117 InitialState initialState;
118 SkIRect clipSpaceIBounds;
119 bool requiresAA;
120 bool isRect = false;
121
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000122 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000123
124 const GrRenderTarget* rt = drawState->getRenderTarget();
125 // GrDrawTarget should have filtered this for us
126 GrAssert(NULL != rt);
127
128 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
129
130 if (!ignoreClip) {
131 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
132 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
133 ReduceClipStack(*clipDataIn->fClipStack,
134 clipSpaceRTIBounds,
135 &elements,
136 &initialState,
137 &clipSpaceIBounds,
138 &requiresAA);
139 if (elements.isEmpty()) {
140 if (kAllIn_InitialState == initialState) {
141 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
142 isRect = true;
143 } else {
144 return false;
145 }
146 }
147 }
148
149 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000150 fGpu->disableScissor();
151 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000152 return true;
153 }
154
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000155#if GR_AA_CLIP
156 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000157
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000158 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000159 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000160 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000161 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000162
163 if (this->useSWOnlyPath(elements)) {
164 // The clip geometry is complex enough that it will be more efficient to create it
165 // entirely in software
166 result = this->createSoftwareClipMask(genID,
167 initialState,
168 elements,
169 clipSpaceIBounds);
170 } else {
171 result = this->createAlphaClipMask(genID,
172 initialState,
173 elements,
174 clipSpaceIBounds);
175 }
176
177 if (NULL != result) {
178 // The mask's top left coord should be pinned to the rounded-out top left corner of
179 // clipSpace bounds. We determine the mask's position WRT to the render target here.
180 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
181 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
182 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000183 fGpu->disableScissor();
184 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000185 return true;
186 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000187 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000188 }
189#endif // GR_AA_CLIP
190
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000191 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
192 // be created. In either case, free up the texture in the anti-aliased mask cache.
193 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
194 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
195 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000196 fAACache.reset();
197
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 // If the clip is a rectangle then just set the scissor. Otherwise, create
199 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000200 if (isRect) {
201 SkIRect clipRect = clipSpaceIBounds;
202 clipRect.offset(-clipDataIn->fOrigin);
203 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000204 this->setGpuStencil();
205 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000206 }
207
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000208 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000209 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
210 this->createStencilClipMask(initialState,
211 elements,
212 clipSpaceIBounds,
213 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000214
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000215 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
216 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
217 // use both stencil and scissor test to the bounds for the final draw.
218 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
219 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
220 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000221 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000222 return true;
223}
224
225#define VISUALIZE_COMPLEX_CLIP 0
226
227#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000228 #include "SkRandom.h"
229 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000230 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
231#else
232 #define SET_RANDOM_COLOR
233#endif
234
235namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000236
237////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000238// set up the OpenGL blend function to perform the specified
239// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000240void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000241
242 switch (op) {
243 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000244 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245 break;
246 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000247 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000248 break;
249 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000250 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000251 break;
252 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000253 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000254 break;
255 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000256 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000257 break;
258 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000259 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000260 break;
261 default:
262 GrAssert(false);
263 break;
264 }
265}
266
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000267////////////////////////////////////////////////////////////////////////////////
268bool draw_path_in_software(GrContext* context,
269 GrGpu* gpu,
270 const SkPath& path,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000271 bool doAA,
272 const GrIRect& resultBounds) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000273 SkStroke stroke;
274 stroke.setDoFill(true);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000275
276 SkAutoTUnref<GrTexture> texture(
277 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000278 stroke,
279 resultBounds,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000280 doAA, NULL));
281 if (NULL == texture) {
282 return false;
283 }
284
285 // The ClipMaskManager accumulates the clip mask in the UL corner
286 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
287
288 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
289
sugoi@google.com12b4e272012-12-06 20:13:11 +0000290 GrAssert(!path.isInverseFillType());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000291 return true;
292}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000293}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000294
295////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000296bool GrClipMaskManager::drawClipShape(GrTexture* target, const SkClipStack::Element* element) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000297 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000298 GrAssert(NULL != drawState);
299
300 drawState->setRenderTarget(target->asRenderTarget());
301
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000302 switch (element->getType()) {
303 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000304 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
305 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000306 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000307 getContext()->getAARectRenderer()->fillAARect(fGpu,
308 fGpu,
309 element->getRect(),
310 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000311 } else {
312 fGpu->drawSimpleRect(element->getRect(), NULL);
313 }
314 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000315 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000316 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
317 if (path->isInverseFillType()) {
318 path.writable()->toggleInverseFillType();
319 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000320 SkStroke stroke;
321 stroke.setDoFill(true);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000322 GrPathRendererChain::DrawType type = element->isAA() ?
323 GrPathRendererChain::kColorAntiAlias_DrawType :
324 GrPathRendererChain::kColor_DrawType;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000325 GrPathRenderer* pr = this->getContext()->getPathRenderer(*path,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 stroke,
327 fGpu,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000328 false,
329 type);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000330 if (NULL == pr) {
331 return false;
332 }
333 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
334 break;
335 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000336 default:
337 // something is wrong if we're trying to draw an empty element.
338 GrCrash("Unexpected element type");
339 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000340 }
341 return true;
342}
343
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000344void GrClipMaskManager::mergeMask(GrTexture* dstMask,
345 GrTexture* srcMask,
346 SkRegion::Op op,
347 const GrIRect& dstBound,
348 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000349 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000350 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000351 SkMatrix oldMatrix = drawState->getViewMatrix();
352 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000354 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000356 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000357
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000358 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000359 sampleM.setIDiv(srcMask->width(), srcMask->height());
360 drawState->stage(0)->setEffect(
361 GrTextureDomainEffect::Create(srcMask,
362 sampleM,
363 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
364 GrTextureDomainEffect::kDecal_WrapMode))->unref();
365 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000366
tomhudson@google.com676e6602012-07-10 17:21:48 +0000367 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000368 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369}
370
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000371// get a texture to act as a temporary buffer for AA clip boolean operations
372// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000373void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000374 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000375 // we've already allocated the temp texture
376 return;
377 }
378
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000379 GrTextureDesc desc;
380 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000381 desc.fWidth = width;
382 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000383 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000384
robertphillips@google.com2c756812012-05-22 20:28:23 +0000385 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000386}
387
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000388////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000389// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
390// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
391// hit)
392bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
393 const SkIRect& clipSpaceIBounds,
394 GrTexture** result) {
395 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
396 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000398 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
399 // Since we are setting up the cache we know the last lookup was a miss. Free up the
400 // currently cached mask so it can be reused.
401 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000403 GrTextureDesc desc;
404 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
405 desc.fWidth = clipSpaceIBounds.width();
406 desc.fHeight = clipSpaceIBounds.height();
407 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000408
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000409 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000410 }
411
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000412 *result = fAACache.getLastMask();
413 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000414}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000416////////////////////////////////////////////////////////////////////////////////
417// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000418GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
419 InitialState initialState,
420 const ElementList& elements,
421 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000422 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
423
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000424 GrTexture* result;
425 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000426 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000428 }
429
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000431 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433 }
434
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000435 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
436 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000438 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000441 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000442 SkIntToScalar(-clipSpaceIBounds.fLeft),
443 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000444 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000445 // The texture may be larger than necessary, this rect represents the part of the texture
446 // we populate with a rasterization of the clip.
447 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
448
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000449 // We're drawing a coverage mask and want coverage to be run through the blend function.
450 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
451
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000452 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000453 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000454
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000455 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
456 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000457 fGpu->clear(&maskSpaceIBounds,
458 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
459 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000460
robertphillips@google.comf105b102012-05-14 12:18:26 +0000461 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000462 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000464 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000465 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000466
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000467 if (SkRegion::kReverseDifference_Op == op || SkRegion::kIntersect_Op == op ||
468 element->isInverseFilled()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000469 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000470 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000471 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000472 return NULL;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000473 }
474
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000475 // This is the bounds of the clip element in the space of the alpha-mask. The temporary
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000476 // mask buffer can be substantially larger than the actually clip stack element. We
477 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000478 // the accumulator
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000479 GrIRect maskSpaceElementIBounds;
480 if (element->isInverseFilled()) {
481 maskSpaceElementIBounds = maskSpaceIBounds;
482 } else {
483 GrRect elementBounds = element->getBounds();
484 elementBounds.offset(clipToMaskOffset);
485 elementBounds.roundOut(&maskSpaceElementIBounds);
486 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000487
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000488 // determines whether we're drawing white-on-black or black-on-white
489 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000490
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000491 // clear the temp target & draw into it
492 fGpu->clear(&maskSpaceElementIBounds,
493 invert ? 0xffffffff : 0x00000000,
494 temp.texture()->asRenderTarget());
495 drawState->setAlpha(invert ? 0x00 : 0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000496 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000497
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000498 if (!this->drawClipShape(temp.texture(), element)) {
499 fAACache.reset();
500 return NULL;
501 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000502
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000503 // Now draw into the accumulator using the real operation and the temp buffer as a
504 // texture
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000505 this->mergeMask(result, temp.texture(), op, maskSpaceIBounds, maskSpaceElementIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000506 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000507 // all the remaining ops can just be directly draw into the accumulation buffer
508 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000509 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000510 this->drawClipShape(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000511 }
512 }
513
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000514 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000515 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000516}
517
518////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000519// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000520// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000521bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
522 const ElementList& elements,
523 const SkIRect& clipSpaceIBounds,
524 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000525
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000526 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000527
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000528 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000529 GrAssert(drawState->isClipState());
530
531 GrRenderTarget* rt = drawState->getRenderTarget();
532 GrAssert(NULL != rt);
533
534 // TODO: dynamically attach a SB when needed.
535 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
536 if (NULL == stencilBuffer) {
537 return false;
538 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000539 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000540
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000541 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000542
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000543 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000544
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000545 // we set the current clip to the bounds so that our recursive draws are scissored to them.
546 // We use the copy of the GrClipData we just stashed on the SB to render from. We set it
547 // back after we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000548 const GrClipData* oldClipData = fGpu->getClip();
549
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000550 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
551 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
552
553 SkClipStack newClipStack(stencilSpaceIBounds);
554 GrClipData newClipData; // origin defaults to (0,0)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000555 newClipData.fClipStack = &newClipStack;
556
557 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000558
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000559 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
560 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000561 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000562 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000563
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000564 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
565 SkVector translate = {
566 SkIntToScalar(clipSpaceToStencilOffset.fX),
567 SkIntToScalar(clipSpaceToStencilOffset.fY)
568 };
569 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000570
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000571#if !VISUALIZE_COMPLEX_CLIP
572 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
573#endif
574
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000575 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000576 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000577 clipBit = (1 << (clipBit-1));
578
robertphillips@google.com7b112892012-07-31 15:18:21 +0000579 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000580
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000581 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000582
583 // walk through each clip element and perform its set op
584 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000585 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
586 const Element* element = iter.get();
sugoi@google.com12b4e272012-12-06 20:13:11 +0000587 SkPath::FillType fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000588 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589 // enabled at bottom of loop
590 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000591 // if the target is MSAA then we want MSAA enabled when the clip is soft
592 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000593 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000594 }
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
sugoi@google.com12b4e272012-12-06 20:13:11 +0000600 SkStroke stroke;
601 stroke.setDoFill(true);
602
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000603 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000604
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000605 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000606 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000607 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000608 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000609 fill = SkPath::kEvenOdd_FillType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000610 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000611 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000612 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000613 clipPath.init(element->getPath());
614 fill = clipPath->getFillType();
615 fillInverted = clipPath->isInverseFillType();
616 if (fillInverted) {
617 clipPath.writable()->toggleInverseFillType();
618 fill = clipPath->getFillType();
619 }
620 pr = this->getContext()->getPathRenderer(*clipPath,
621 stroke,
622 fGpu,
623 false,
624 GrPathRendererChain::kStencilOnly_DrawType,
625 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000626 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000627 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000628 return false;
629 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000630 }
631
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000632 int passes;
633 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
634
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000635 bool canRenderDirectToStencil =
636 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000637 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000638 // fill rule, and set operation can
639 // we render the element directly to
640 // stencil bit used for clipping.
641 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
642 canRenderDirectToStencil,
643 clipBit,
644 fillInverted,
645 &passes,
646 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000647
648 // draw the element to the client stencil bits if necessary
649 if (!canDrawDirectToClip) {
650 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000651 kIncClamp_StencilOp,
652 kIncClamp_StencilOp,
653 kAlways_StencilFunc,
654 0xffff,
655 0x0000,
656 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000657 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000658 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000660 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000661 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000662 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 if (canRenderDirectToStencil) {
664 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000665 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666 } else {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000667 pr->stencilPath(*clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000668 }
669 }
670 }
671
672 // now we modify the clip bit by rendering either the clip
673 // element directly or a bounding rect of the entire clip.
674 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
675 for (int p = 0; p < passes; ++p) {
676 *drawState->stencil() = stencilSettings[p];
677 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000678 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000679 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000680 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000681 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000682 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000683 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000684 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000685 }
686 } else {
687 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000688 // The view matrix is setup to do clip space -> stencil space translation, so
689 // draw rect in clip space.
690 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691 }
692 }
693 }
694 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000695 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000697 // set this last because recursive draws may overwrite it back to kNone.
698 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
699 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000700 return true;
701}
702
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000703
bsalomon@google.com411dad02012-06-05 20:24:20 +0000704// mapping of clip-respecting stencil funcs to normal stencil funcs
705// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000706static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000707 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
708 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
709 // In the Clip Funcs
710 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
711 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
712 kLess_StencilFunc, // kLessIfInClip_StencilFunc
713 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
714 // Special in the clip func that forces user's ref to be 0.
715 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
716 // make ref 0 and do normal nequal.
717 },
718 {// Stencil-Clipping is ENABLED
719 // In the Clip Funcs
720 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
721 // eq stencil clip bit, mask
722 // out user bits.
723
724 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
725 // add stencil bit to mask and ref
726
727 kLess_StencilFunc, // kLessIfInClip_StencilFunc
728 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
729 // for both of these we can add
730 // the clip bit to the mask and
731 // ref and compare as normal
732 // Special in the clip func that forces user's ref to be 0.
733 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
734 // make ref have only the clip bit set
735 // and make comparison be less
736 // 10..0 < 1..user_bits..
737 }
738};
739
bsalomon@google.coma3201942012-06-21 19:58:20 +0000740namespace {
741// Sets the settings to clip against the stencil buffer clip while ignoring the
742// client bits.
743const GrStencilSettings& basic_apply_stencil_clip_settings() {
744 // stencil settings to use when clip is in stencil
745 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
746 kKeep_StencilOp,
747 kKeep_StencilOp,
748 kAlwaysIfInClip_StencilFunc,
749 0x0000,
750 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000751 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000752 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
753}
754}
755
756void GrClipMaskManager::setGpuStencil() {
757 // We make two copies of the StencilSettings here (except in the early
758 // exit scenario. One copy from draw state to the stack var. Then another
759 // from the stack var to the gpu. We could make this class hold a ptr to
760 // GrGpu's fStencilSettings and eliminate the stack copy here.
761
762 const GrDrawState& drawState = fGpu->getDrawState();
763
764 // use stencil for clipping if clipping is enabled and the clip
765 // has been written into the stencil.
766 GrClipMaskManager::StencilClipMode clipMode;
767 if (this->isClipInStencil() && drawState.isClipState()) {
768 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
769 // We can't be modifying the clip and respecting it at the same time.
770 GrAssert(!drawState.isStateFlagEnabled(
771 GrGpu::kModifyStencilClip_StateBit));
772 } else if (drawState.isStateFlagEnabled(
773 GrGpu::kModifyStencilClip_StateBit)) {
774 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
775 } else {
776 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
777 }
778
779 GrStencilSettings settings;
780 // The GrGpu client may not be using the stencil buffer but we may need to
781 // enable it in order to respect a stencil clip.
782 if (drawState.getStencil().isDisabled()) {
783 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
784 settings = basic_apply_stencil_clip_settings();
785 } else {
786 fGpu->disableStencil();
787 return;
788 }
789 } else {
790 settings = drawState.getStencil();
791 }
792
793 // TODO: dynamically attach a stencil buffer
794 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000795 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000796 drawState.getRenderTarget()->getStencilBuffer();
797 if (NULL != stencilBuffer) {
798 stencilBits = stencilBuffer->bits();
799 }
800
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000801 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000802 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000803 this->adjustStencilParams(&settings, clipMode, stencilBits);
804 fGpu->setStencilSettings(settings);
805}
806
807void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
808 StencilClipMode mode,
809 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000810 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000811
812 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000813 // We assume that this clip manager itself is drawing to the GrGpu and
814 // has already setup the correct values.
815 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000816 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000817
bsalomon@google.com411dad02012-06-05 20:24:20 +0000818 unsigned int clipBit = (1 << (stencilBitCnt - 1));
819 unsigned int userBits = clipBit - 1;
820
bsalomon@google.coma3201942012-06-21 19:58:20 +0000821 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000822 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000823
bsalomon@google.coma3201942012-06-21 19:58:20 +0000824 bool finished = false;
825 while (!finished) {
826 GrStencilFunc func = settings->func(face);
827 uint16_t writeMask = settings->writeMask(face);
828 uint16_t funcMask = settings->funcMask(face);
829 uint16_t funcRef = settings->funcRef(face);
830
831 GrAssert((unsigned) func < kStencilFuncCount);
832
833 writeMask &= userBits;
834
835 if (func >= kBasicStencilFuncCount) {
836 int respectClip = kRespectClip_StencilClipMode == mode;
837 if (respectClip) {
838 // The GrGpu class should have checked this
839 GrAssert(this->isClipInStencil());
840 switch (func) {
841 case kAlwaysIfInClip_StencilFunc:
842 funcMask = clipBit;
843 funcRef = clipBit;
844 break;
845 case kEqualIfInClip_StencilFunc:
846 case kLessIfInClip_StencilFunc:
847 case kLEqualIfInClip_StencilFunc:
848 funcMask = (funcMask & userBits) | clipBit;
849 funcRef = (funcRef & userBits) | clipBit;
850 break;
851 case kNonZeroIfInClip_StencilFunc:
852 funcMask = (funcMask & userBits) | clipBit;
853 funcRef = clipBit;
854 break;
855 default:
856 GrCrash("Unknown stencil func");
857 }
858 } else {
859 funcMask &= userBits;
860 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000861 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000862 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000863 gSpecialToBasicStencilFunc[respectClip];
864 func = table[func - kBasicStencilFuncCount];
865 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000866 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000867 funcMask &= userBits;
868 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000869 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000870
871 settings->setFunc(face, func);
872 settings->setWriteMask(face, writeMask);
873 settings->setFuncMask(face, funcMask);
874 settings->setFuncRef(face, funcRef);
875
876 if (GrStencilSettings::kFront_Face == face) {
877 face = GrStencilSettings::kBack_Face;
878 finished = !twoSided;
879 } else {
880 finished = true;
881 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000882 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000883 if (!twoSided) {
884 settings->copyFrontSettingsToBack();
885 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000886}
887
888////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000889GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
890 GrReducedClip::InitialState initialState,
891 const GrReducedClip::ElementList& elements,
892 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000893 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000894
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000895 GrTexture* result;
896 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
897 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000898 }
899
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000900 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000901 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000902 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000903 }
904
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000905 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
906 // the top left corner of the resulting rect to the top left of the texture.
907 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
908
robertphillips@google.com2c756812012-05-22 20:28:23 +0000909 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000910
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000911 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000912 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
913 SkIntToScalar(-clipSpaceIBounds.fTop));
914 helper.init(maskSpaceIBounds, &matrix);
915
916 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000917
sugoi@google.com12b4e272012-12-06 20:13:11 +0000918 SkStroke stroke;
919 stroke.setDoFill(true);
920
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000921 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000922
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000923 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000924 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000925
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000926 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
927 // Intersect and reverse difference require modifying pixels outside of the geometry
928 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
929 // but leave the pixels inside the geometry alone. For reverse difference we invert all
930 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000931 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000932 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000933 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000934 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000935 }
936
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000937 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000938 // convert the rect to a path so we can invert the fill
939 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000940 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000941 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000942
sugoi@google.com12b4e272012-12-06 20:13:11 +0000943 helper.draw(temp, stroke, SkRegion::kReplace_Op,
944 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000945 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000946 } else {
947 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000948 SkPath clipPath = element->getPath();
949 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000950 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000951 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000952 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000953 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000954 }
955
956 continue;
957 }
958
959 // The other ops (union, xor, diff) only affect pixels inside
960 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000961 if (Element::kRect_Type == element->getType()) {
962 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
963 } else {
964 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000965 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000966 }
967 }
968
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000969 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000970
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000971 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000972 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000973}
974
robertphillips@google.comf294b772012-04-27 14:29:26 +0000975////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +0000976void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000977 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000978}