blob: c4004861caa675a9e886c544dc231737be972ba3 [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.comc6b3e482012-12-07 20:43:52 +000073 return NULL == context->getPathRenderer(*path, stroke, gpu, doAA, false);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000074}
75
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000076}
77
robertphillips@google.comfa662942012-05-17 12:20:22 +000078/*
79 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
80 * will be used on any element. If so, it returns true to indicate that the
81 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
82 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000083bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000084
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000085 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000086 // a clip gets complex enough it can just be done in SW regardless
87 // of whether it would invoke the GrSoftwarePathRenderer.
88 bool useSW = false;
sugoi@google.com12b4e272012-12-06 20:13:11 +000089 SkStroke stroke;
90 stroke.setDoFill(true);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000091
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000092 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
93 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000094 // rects can always be drawn directly w/o using the software path
95 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000096 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000098 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +000099 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000100 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000101 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000102 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000103 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000104 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000105}
106
robertphillips@google.comf294b772012-04-27 14:29:26 +0000107////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000108// sort out what kind of clip mask needs to be created: alpha, stencil,
109// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000110bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000111 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000112
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000113 ElementList elements(16);
114 InitialState initialState;
115 SkIRect clipSpaceIBounds;
116 bool requiresAA;
117 bool isRect = false;
118
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000119 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000120
121 const GrRenderTarget* rt = drawState->getRenderTarget();
122 // GrDrawTarget should have filtered this for us
123 GrAssert(NULL != rt);
124
125 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
126
127 if (!ignoreClip) {
128 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
129 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
130 ReduceClipStack(*clipDataIn->fClipStack,
131 clipSpaceRTIBounds,
132 &elements,
133 &initialState,
134 &clipSpaceIBounds,
135 &requiresAA);
136 if (elements.isEmpty()) {
137 if (kAllIn_InitialState == initialState) {
138 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
139 isRect = true;
140 } else {
141 return false;
142 }
143 }
144 }
145
146 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000147 fGpu->disableScissor();
148 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000149 return true;
150 }
151
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000152#if GR_AA_CLIP
153 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000154
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000155 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000156 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000157 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000158 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000159
160 if (this->useSWOnlyPath(elements)) {
161 // The clip geometry is complex enough that it will be more efficient to create it
162 // entirely in software
163 result = this->createSoftwareClipMask(genID,
164 initialState,
165 elements,
166 clipSpaceIBounds);
167 } else {
168 result = this->createAlphaClipMask(genID,
169 initialState,
170 elements,
171 clipSpaceIBounds);
172 }
173
174 if (NULL != result) {
175 // The mask's top left coord should be pinned to the rounded-out top left corner of
176 // clipSpace bounds. We determine the mask's position WRT to the render target here.
177 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
178 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
179 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000180 fGpu->disableScissor();
181 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000182 return true;
183 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000184 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000185 }
186#endif // GR_AA_CLIP
187
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000188 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
189 // be created. In either case, free up the texture in the anti-aliased mask cache.
190 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
191 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
192 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000193 fAACache.reset();
194
bsalomon@google.coma3201942012-06-21 19:58:20 +0000195 // If the clip is a rectangle then just set the scissor. Otherwise, create
196 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000197 if (isRect) {
198 SkIRect clipRect = clipSpaceIBounds;
199 clipRect.offset(-clipDataIn->fOrigin);
200 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000201 this->setGpuStencil();
202 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000203 }
204
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000205 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000206 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
207 this->createStencilClipMask(initialState,
208 elements,
209 clipSpaceIBounds,
210 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000211
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000212 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
213 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
214 // use both stencil and scissor test to the bounds for the final draw.
215 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
216 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
217 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000218 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000219 return true;
220}
221
222#define VISUALIZE_COMPLEX_CLIP 0
223
224#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000225 #include "SkRandom.h"
226 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000227 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
228#else
229 #define SET_RANDOM_COLOR
230#endif
231
232namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000233
234////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000235// set up the OpenGL blend function to perform the specified
236// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000237void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000238
239 switch (op) {
240 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000241 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000242 break;
243 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000244 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245 break;
246 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000247 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000248 break;
249 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000250 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000251 break;
252 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000253 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000254 break;
255 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000256 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000257 break;
258 default:
259 GrAssert(false);
260 break;
261 }
262}
263
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000264////////////////////////////////////////////////////////////////////////////////
265bool draw_path_in_software(GrContext* context,
266 GrGpu* gpu,
267 const SkPath& path,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000268 bool doAA,
269 const GrIRect& resultBounds) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000270 SkStroke stroke;
271 stroke.setDoFill(true);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000272
273 SkAutoTUnref<GrTexture> texture(
274 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000275 stroke,
276 resultBounds,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000277 doAA, NULL));
278 if (NULL == texture) {
279 return false;
280 }
281
282 // The ClipMaskManager accumulates the clip mask in the UL corner
283 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
284
285 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
286
sugoi@google.com12b4e272012-12-06 20:13:11 +0000287 GrAssert(!path.isInverseFillType());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000288 return true;
289}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000290}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000291
292////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000293bool GrClipMaskManager::drawClipShape(GrTexture* target, const SkClipStack::Element* element) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000294 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000295 GrAssert(NULL != drawState);
296
297 drawState->setRenderTarget(target->asRenderTarget());
298
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000299 switch (element->getType()) {
300 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000301 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
302 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000303 if (element->isAA()) {
304 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu, element->getRect(), true);
305 } else {
306 fGpu->drawSimpleRect(element->getRect(), NULL);
307 }
308 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000309 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000310 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
311 if (path->isInverseFillType()) {
312 path.writable()->toggleInverseFillType();
313 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000314 SkStroke stroke;
315 stroke.setDoFill(true);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000316 GrPathRenderer* pr = this->getContext()->getPathRenderer(*path,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317 stroke,
318 fGpu,
319 element->isAA(), false);
320 if (NULL == pr) {
321 return false;
322 }
323 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
324 break;
325 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000326 default:
327 // something is wrong if we're trying to draw an empty element.
328 GrCrash("Unexpected element type");
329 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000330 }
331 return true;
332}
333
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000334void GrClipMaskManager::mergeMask(GrTexture* dstMask,
335 GrTexture* srcMask,
336 SkRegion::Op op,
337 const GrIRect& dstBound,
338 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000339 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000340 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000341 SkMatrix oldMatrix = drawState->getViewMatrix();
342 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000344 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000345
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000346 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000347
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000348 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000349 sampleM.setIDiv(srcMask->width(), srcMask->height());
350 drawState->stage(0)->setEffect(
351 GrTextureDomainEffect::Create(srcMask,
352 sampleM,
353 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
354 GrTextureDomainEffect::kDecal_WrapMode))->unref();
355 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356
tomhudson@google.com676e6602012-07-10 17:21:48 +0000357 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000358 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359}
360
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000361// get a texture to act as a temporary buffer for AA clip boolean operations
362// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000363void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000364 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000365 // we've already allocated the temp texture
366 return;
367 }
368
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000369 GrTextureDesc desc;
370 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000371 desc.fWidth = width;
372 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000373 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000374
robertphillips@google.com2c756812012-05-22 20:28:23 +0000375 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000376}
377
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000378////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000379// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
380// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
381// hit)
382bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
383 const SkIRect& clipSpaceIBounds,
384 GrTexture** result) {
385 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
386 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000388 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
389 // Since we are setting up the cache we know the last lookup was a miss. Free up the
390 // currently cached mask so it can be reused.
391 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000393 GrTextureDesc desc;
394 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
395 desc.fWidth = clipSpaceIBounds.width();
396 desc.fHeight = clipSpaceIBounds.height();
397 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000398
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000399 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000400 }
401
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000402 *result = fAACache.getLastMask();
403 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000404}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000405
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000406////////////////////////////////////////////////////////////////////////////////
407// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000408GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
409 InitialState initialState,
410 const ElementList& elements,
411 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000412 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
413
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000414 GrTexture* result;
415 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000416 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000417 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000418 }
419
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000420 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000421 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000422 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000423 }
424
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000425 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
426 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000427
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000428 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000429
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000431 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432 SkIntToScalar(-clipSpaceIBounds.fLeft),
433 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000434 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000435 // The texture may be larger than necessary, this rect represents the part of the texture
436 // we populate with a rasterization of the clip.
437 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
438
439 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000440 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000442 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
443 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000444 fGpu->clear(&maskSpaceIBounds,
445 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
446 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000447
robertphillips@google.comf105b102012-05-14 12:18:26 +0000448 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000450 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000451 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000452 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000453
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000454 if (SkRegion::kReverseDifference_Op == op || SkRegion::kIntersect_Op == op ||
455 element->isInverseFilled()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000456 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000457 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000458 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 return NULL;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000460 }
461
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000462 // 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 +0000463 // mask buffer can be substantially larger than the actually clip stack element. We
464 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000465 // the accumulator
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000466 GrIRect maskSpaceElementIBounds;
467 if (element->isInverseFilled()) {
468 maskSpaceElementIBounds = maskSpaceIBounds;
469 } else {
470 GrRect elementBounds = element->getBounds();
471 elementBounds.offset(clipToMaskOffset);
472 elementBounds.roundOut(&maskSpaceElementIBounds);
473 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000474
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000475 // determines whether we're drawing white-on-black or black-on-white
476 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000477
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000478 // clear the temp target & draw into it
479 fGpu->clear(&maskSpaceElementIBounds,
480 invert ? 0xffffffff : 0x00000000,
481 temp.texture()->asRenderTarget());
482 drawState->setAlpha(invert ? 0x00 : 0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000483 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000484
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000485 if (!this->drawClipShape(temp.texture(), element)) {
486 fAACache.reset();
487 return NULL;
488 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000489
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000490 // Now draw into the accumulator using the real operation and the temp buffer as a
491 // texture
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000492 this->mergeMask(result, temp.texture(), op, maskSpaceIBounds, maskSpaceElementIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000493 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000494 // all the remaining ops can just be directly draw into the accumulation buffer
495 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000496 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000497 this->drawClipShape(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498 }
499 }
500
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000501 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000502 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000503}
504
505////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000506// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000507// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000508bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
509 const ElementList& elements,
510 const SkIRect& clipSpaceIBounds,
511 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000512
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000513 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000514
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000515 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000516 GrAssert(drawState->isClipState());
517
518 GrRenderTarget* rt = drawState->getRenderTarget();
519 GrAssert(NULL != rt);
520
521 // TODO: dynamically attach a SB when needed.
522 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
523 if (NULL == stencilBuffer) {
524 return false;
525 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000526 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000527
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000528 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000529
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000530 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000531
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000532 // we set the current clip to the bounds so that our recursive draws are scissored to them.
533 // We use the copy of the GrClipData we just stashed on the SB to render from. We set it
534 // back after we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000535 const GrClipData* oldClipData = fGpu->getClip();
536
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000537 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
538 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
539
540 SkClipStack newClipStack(stencilSpaceIBounds);
541 GrClipData newClipData; // origin defaults to (0,0)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000542 newClipData.fClipStack = &newClipStack;
543
544 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000545
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000546 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
547 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000548 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000549 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000550
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000551 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
552 SkVector translate = {
553 SkIntToScalar(clipSpaceToStencilOffset.fX),
554 SkIntToScalar(clipSpaceToStencilOffset.fY)
555 };
556 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000557
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000558#if !VISUALIZE_COMPLEX_CLIP
559 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
560#endif
561
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000562 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000563 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000564 clipBit = (1 << (clipBit-1));
565
robertphillips@google.com7b112892012-07-31 15:18:21 +0000566 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000567
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000568 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000569
570 // walk through each clip element and perform its set op
571 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000572 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
573 const Element* element = iter.get();
sugoi@google.com12b4e272012-12-06 20:13:11 +0000574 SkPath::FillType fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000575 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000576 // enabled at bottom of loop
577 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000578 // if the target is MSAA then we want MSAA enabled when the clip is soft
579 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000580 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000581 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000582
tomhudson@google.com8afae612012-08-14 15:03:35 +0000583 // Can the clip element be drawn directly to the stencil buffer
584 // with a non-inverted fill rule without extra passes to
585 // resolve in/out status?
586 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000587
sugoi@google.com12b4e272012-12-06 20:13:11 +0000588 SkStroke stroke;
589 stroke.setDoFill(true);
590
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000591 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000592
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000593 GrPathRenderer* pr = NULL;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000594 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000595 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000596 canRenderDirectToStencil = true;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000597 fill = SkPath::kEvenOdd_FillType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000598 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000599 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000600 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000601 clipPath = element->getPath();
602 fill = clipPath.getFillType();
603 fillInverted = clipPath.isInverseFillType();
604 fill = SkPath::NonInverseFill(fill);
605 clipPath.setFillType(fill);
606 pr = this->getContext()->getPathRenderer(clipPath, stroke, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000607 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000608 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609 return false;
610 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000611 canRenderDirectToStencil = !pr->requiresStencilPass(clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000612 }
613
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000614 int passes;
615 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
616
617 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000618 // fill rule, and set operation can
619 // we render the element directly to
620 // stencil bit used for clipping.
621 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
622 canRenderDirectToStencil,
623 clipBit,
624 fillInverted,
625 &passes,
626 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627
628 // draw the element to the client stencil bits if necessary
629 if (!canDrawDirectToClip) {
630 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000631 kIncClamp_StencilOp,
632 kIncClamp_StencilOp,
633 kAlways_StencilFunc,
634 0xffff,
635 0x0000,
636 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000637 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000638 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000640 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000642 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000643 if (canRenderDirectToStencil) {
644 *drawState->stencil() = gDrawToStencil;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000645 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000646 } else {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000647 pr->drawPathToStencil(clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648 }
649 }
650 }
651
652 // now we modify the clip bit by rendering either the clip
653 // element directly or a bounding rect of the entire clip.
654 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
655 for (int p = 0; p < passes; ++p) {
656 *drawState->stencil() = stencilSettings[p];
657 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000658 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 SET_RANDOM_COLOR
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 SET_RANDOM_COLOR
sugoi@google.com12b4e272012-12-06 20:13:11 +0000664 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665 }
666 } else {
667 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000668 // The view matrix is setup to do clip space -> stencil space translation, so
669 // draw rect in clip space.
670 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000671 }
672 }
673 }
674 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000675 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000677 // set this last because recursive draws may overwrite it back to kNone.
678 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
679 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000680 return true;
681}
682
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000683
bsalomon@google.com411dad02012-06-05 20:24:20 +0000684// mapping of clip-respecting stencil funcs to normal stencil funcs
685// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000687 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
688 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
689 // In the Clip Funcs
690 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
691 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
692 kLess_StencilFunc, // kLessIfInClip_StencilFunc
693 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
694 // Special in the clip func that forces user's ref to be 0.
695 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
696 // make ref 0 and do normal nequal.
697 },
698 {// Stencil-Clipping is ENABLED
699 // In the Clip Funcs
700 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
701 // eq stencil clip bit, mask
702 // out user bits.
703
704 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
705 // add stencil bit to mask and ref
706
707 kLess_StencilFunc, // kLessIfInClip_StencilFunc
708 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
709 // for both of these we can add
710 // the clip bit to the mask and
711 // ref and compare as normal
712 // Special in the clip func that forces user's ref to be 0.
713 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
714 // make ref have only the clip bit set
715 // and make comparison be less
716 // 10..0 < 1..user_bits..
717 }
718};
719
bsalomon@google.coma3201942012-06-21 19:58:20 +0000720namespace {
721// Sets the settings to clip against the stencil buffer clip while ignoring the
722// client bits.
723const GrStencilSettings& basic_apply_stencil_clip_settings() {
724 // stencil settings to use when clip is in stencil
725 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
726 kKeep_StencilOp,
727 kKeep_StencilOp,
728 kAlwaysIfInClip_StencilFunc,
729 0x0000,
730 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000731 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000732 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
733}
734}
735
736void GrClipMaskManager::setGpuStencil() {
737 // We make two copies of the StencilSettings here (except in the early
738 // exit scenario. One copy from draw state to the stack var. Then another
739 // from the stack var to the gpu. We could make this class hold a ptr to
740 // GrGpu's fStencilSettings and eliminate the stack copy here.
741
742 const GrDrawState& drawState = fGpu->getDrawState();
743
744 // use stencil for clipping if clipping is enabled and the clip
745 // has been written into the stencil.
746 GrClipMaskManager::StencilClipMode clipMode;
747 if (this->isClipInStencil() && drawState.isClipState()) {
748 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
749 // We can't be modifying the clip and respecting it at the same time.
750 GrAssert(!drawState.isStateFlagEnabled(
751 GrGpu::kModifyStencilClip_StateBit));
752 } else if (drawState.isStateFlagEnabled(
753 GrGpu::kModifyStencilClip_StateBit)) {
754 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
755 } else {
756 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
757 }
758
759 GrStencilSettings settings;
760 // The GrGpu client may not be using the stencil buffer but we may need to
761 // enable it in order to respect a stencil clip.
762 if (drawState.getStencil().isDisabled()) {
763 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
764 settings = basic_apply_stencil_clip_settings();
765 } else {
766 fGpu->disableStencil();
767 return;
768 }
769 } else {
770 settings = drawState.getStencil();
771 }
772
773 // TODO: dynamically attach a stencil buffer
774 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000775 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000776 drawState.getRenderTarget()->getStencilBuffer();
777 if (NULL != stencilBuffer) {
778 stencilBits = stencilBuffer->bits();
779 }
780
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000781 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000782 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000783 this->adjustStencilParams(&settings, clipMode, stencilBits);
784 fGpu->setStencilSettings(settings);
785}
786
787void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
788 StencilClipMode mode,
789 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000790 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000791
792 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000793 // We assume that this clip manager itself is drawing to the GrGpu and
794 // has already setup the correct values.
795 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000796 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000797
bsalomon@google.com411dad02012-06-05 20:24:20 +0000798 unsigned int clipBit = (1 << (stencilBitCnt - 1));
799 unsigned int userBits = clipBit - 1;
800
bsalomon@google.coma3201942012-06-21 19:58:20 +0000801 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000802 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000803
bsalomon@google.coma3201942012-06-21 19:58:20 +0000804 bool finished = false;
805 while (!finished) {
806 GrStencilFunc func = settings->func(face);
807 uint16_t writeMask = settings->writeMask(face);
808 uint16_t funcMask = settings->funcMask(face);
809 uint16_t funcRef = settings->funcRef(face);
810
811 GrAssert((unsigned) func < kStencilFuncCount);
812
813 writeMask &= userBits;
814
815 if (func >= kBasicStencilFuncCount) {
816 int respectClip = kRespectClip_StencilClipMode == mode;
817 if (respectClip) {
818 // The GrGpu class should have checked this
819 GrAssert(this->isClipInStencil());
820 switch (func) {
821 case kAlwaysIfInClip_StencilFunc:
822 funcMask = clipBit;
823 funcRef = clipBit;
824 break;
825 case kEqualIfInClip_StencilFunc:
826 case kLessIfInClip_StencilFunc:
827 case kLEqualIfInClip_StencilFunc:
828 funcMask = (funcMask & userBits) | clipBit;
829 funcRef = (funcRef & userBits) | clipBit;
830 break;
831 case kNonZeroIfInClip_StencilFunc:
832 funcMask = (funcMask & userBits) | clipBit;
833 funcRef = clipBit;
834 break;
835 default:
836 GrCrash("Unknown stencil func");
837 }
838 } else {
839 funcMask &= userBits;
840 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000841 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000842 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000843 gSpecialToBasicStencilFunc[respectClip];
844 func = table[func - kBasicStencilFuncCount];
845 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000846 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000847 funcMask &= userBits;
848 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000849 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000850
851 settings->setFunc(face, func);
852 settings->setWriteMask(face, writeMask);
853 settings->setFuncMask(face, funcMask);
854 settings->setFuncRef(face, funcRef);
855
856 if (GrStencilSettings::kFront_Face == face) {
857 face = GrStencilSettings::kBack_Face;
858 finished = !twoSided;
859 } else {
860 finished = true;
861 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000862 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000863 if (!twoSided) {
864 settings->copyFrontSettingsToBack();
865 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000866}
867
868////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000869GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
870 GrReducedClip::InitialState initialState,
871 const GrReducedClip::ElementList& elements,
872 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000873 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000874
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000875 GrTexture* result;
876 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
877 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000878 }
879
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000880 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000881 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000882 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000883 }
884
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000885 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
886 // the top left corner of the resulting rect to the top left of the texture.
887 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
888
robertphillips@google.com2c756812012-05-22 20:28:23 +0000889 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000890
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000891 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000892 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
893 SkIntToScalar(-clipSpaceIBounds.fTop));
894 helper.init(maskSpaceIBounds, &matrix);
895
896 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000897
sugoi@google.com12b4e272012-12-06 20:13:11 +0000898 SkStroke stroke;
899 stroke.setDoFill(true);
900
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000901 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000902
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000903 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000904 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000905
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000906 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
907 // Intersect and reverse difference require modifying pixels outside of the geometry
908 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
909 // but leave the pixels inside the geometry alone. For reverse difference we invert all
910 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000911 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000912 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000913 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000914 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000915 }
916
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000917 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000918 // convert the rect to a path so we can invert the fill
919 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000920 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000921 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000922
sugoi@google.com12b4e272012-12-06 20:13:11 +0000923 helper.draw(temp, stroke, SkRegion::kReplace_Op,
924 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000925 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000926 } else {
927 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000928 SkPath clipPath = element->getPath();
929 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000930 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000931 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000932 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000933 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000934 }
935
936 continue;
937 }
938
939 // The other ops (union, xor, diff) only affect pixels inside
940 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000941 if (Element::kRect_Type == element->getType()) {
942 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
943 } else {
944 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000945 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000946 }
947 }
948
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000949 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000950
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000951 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000952 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000953}
954
robertphillips@google.comf294b772012-04-27 14:29:26 +0000955////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +0000956void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000957 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000958}