blob: 55a8192c3fb2384c44c843a2107975d0afd76f63 [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"
10#include "GrGpu.h"
11#include "GrRenderTarget.h"
12#include "GrStencilBuffer.h"
13#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000014#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000015#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000016#include "GrAAConvexPathRenderer.h"
17#include "GrAAHairLinePathRenderer.h"
18
19// TODO: move GrSWMaskHelper out of GrSoftwarePathRender.h & remove this include
20#include "GrSoftwarePathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000021
22//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000023//#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000024
robertphillips@google.comf294b772012-04-27 14:29:26 +000025////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +000026void ScissoringSettings::setupScissoring(GrGpu* gpu) {
27 if (!fEnableScissoring) {
28 gpu->disableScissor();
29 return;
30 }
31
32 gpu->enableScissoring(fScissorRect);
33}
34
robertphillips@google.coma72eef32012-05-01 17:22:59 +000035namespace {
36// set up the draw state to enable the aa clipping mask. Besides setting up the
37// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000038void setup_drawstate_aaclip(GrGpu* gpu,
39 GrTexture* result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000040 const GrIRect &bound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041 GrDrawState* drawState = gpu->drawState();
42 GrAssert(drawState);
43
44 static const int maskStage = GrPaint::kTotalStages+1;
45
46 GrMatrix mat;
47 mat.setIDiv(result->width(), result->height());
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000048 mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049 mat.preConcat(drawState->getViewMatrix());
50
51 drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode,
52 GrSamplerState::kNearest_Filter,
53 mat);
54
55 drawState->setTexture(maskStage, result);
56
57 // The AA clipping determination happens long after the geometry has
58 // been set up to draw. Here we directly enable the AA clip mask stage
59 gpu->addToVertexLayout(
60 GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(maskStage));
61}
62
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000063bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000064 GrGpu* gpu,
65 const SkPath& path,
66 GrPathFill fill,
67 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000068 // last (false) parameter disallows use of the SW path renderer
69 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
70}
71
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000072}
73
robertphillips@google.comfa662942012-05-17 12:20:22 +000074/*
75 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
76 * will be used on any element. If so, it returns true to indicate that the
77 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
78 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000079bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000080
81 if (!clipIn.requiresAA()) {
82 // The stencil buffer can handle this case
83 return false;
84 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000085
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000086 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000087 // a clip gets complex enough it can just be done in SW regardless
88 // of whether it would invoke the GrSoftwarePathRenderer.
89 bool useSW = false;
90
91 for (int i = 0; i < clipIn.getElementCount(); ++i) {
92
93 if (SkRegion::kReplace_Op == clipIn.getOp(i)) {
94 // Everything before a replace op can be ignored so start
95 // afresh w.r.t. determining if any element uses the SW path
96 useSW = false;
97 }
98
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000099 // rects can always be drawn directly w/o using the software path
100 // so only paths need to be checked
101 if (kPath_ClipType == clipIn.getElementType(i) &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000102 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000103 clipIn.getPath(i),
104 clipIn.getPathFill(i),
105 clipIn.getDoAA(i))) {
106 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000108 }
109
110 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000111}
112
robertphillips@google.comf294b772012-04-27 14:29:26 +0000113////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000114// sort out what kind of clip mask needs to be created: alpha, stencil,
115// scissor, or entirely software
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000116bool GrClipMaskManager::createClipMask(const GrClip& clipIn,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000117 ScissoringSettings* scissorSettings) {
118
119 GrAssert(scissorSettings);
120
121 scissorSettings->fEnableScissoring = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000122
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000123 fCurrClipMaskType = kNone_ClipMaskType;
124
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000125 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000126 if (!drawState->isClipState()) {
127 return true;
128 }
129
130 GrRenderTarget* rt = drawState->getRenderTarget();
131
132 // GrDrawTarget should have filtered this for us
133 GrAssert(NULL != rt);
134
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000135#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000136 // If MSAA is enabled we can do everything in the stencil buffer.
137 // Otherwise check if we should just create the entire clip mask
138 // in software (this will only happen if the clip mask is anti-aliased
139 // and too complex for the gpu to handle in its entirety)
140 if (0 == rt->numSamples() && useSWOnlyPath(gpu, clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000141 // The clip geometry is complex enough that it will be more
142 // efficient to create it entirely in software
143 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000144 GrIRect bound;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000145 if (this->createSoftwareClipMask(fGpu, clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000146 setup_drawstate_aaclip(fGpu, result, bound);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000147 return true;
148 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000149
150 // if SW clip mask creation fails fall through to the other
151 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000152 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000153#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000154
robertphillips@google.comf294b772012-04-27 14:29:26 +0000155#if GR_AA_CLIP
156 // If MSAA is enabled use the (faster) stencil path for AA clipping
157 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000158 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000159 // Since we are going to create a destination texture of the correct
160 // size for the mask (rather than being bound by the size of the
161 // render target) we aren't going to use scissoring like the stencil
162 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000163 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000164 GrIRect bound;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000165 if (this->createAlphaClipMask(fGpu, clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000166 setup_drawstate_aaclip(fGpu, result, bound);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000167 return true;
168 }
169
170 // if alpha clip mask creation fails fall through to the stencil
171 // buffer method
172 }
173#endif // GR_AA_CLIP
174
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000175 // Either a hard (stencil buffer) clip was explicitly requested or
176 // an antialiased clip couldn't be created. In either case, free up
177 // the texture in the antialiased mask cache.
178 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000179 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
180 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000181 // AA cache.
182 fAACache.reset();
183
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000184 GrRect bounds;
185 GrRect rtRect;
186 rtRect.setLTRB(0, 0,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000187 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000188 if (clipIn.hasConservativeBounds()) {
189 bounds = clipIn.getConservativeBounds();
190 if (!bounds.intersect(rtRect)) {
191 bounds.setEmpty();
192 }
193 } else {
194 bounds = rtRect;
195 }
196
197 bounds.roundOut(&scissorSettings->fScissorRect);
198 if (scissorSettings->fScissorRect.isEmpty()) {
199 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
200 // TODO: I think we can do an early exit here - after refactoring try:
201 // set fEnableScissoring to true but leave fClipMaskInStencil false
202 // and return - everything is going to be scissored away anyway!
203 }
204 scissorSettings->fEnableScissoring = true;
205
206 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000207 bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
208 !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000209
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000210 if (useStencil) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000211 return this->createStencilClipMask(clipIn, bounds, scissorSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000212 }
213
214 return true;
215}
216
217#define VISUALIZE_COMPLEX_CLIP 0
218
219#if VISUALIZE_COMPLEX_CLIP
220 #include "GrRandom.h"
221 GrRandom gRandom;
222 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
223#else
224 #define SET_RANDOM_COLOR
225#endif
226
227namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000228/**
229 * Does "container" contain "containee"? If either is empty then
230 * no containment is possible.
231 */
232bool contains(const SkRect& container, const SkIRect& containee) {
233 return !containee.isEmpty() && !container.isEmpty() &&
234 container.fLeft <= SkIntToScalar(containee.fLeft) &&
235 container.fTop <= SkIntToScalar(containee.fTop) &&
236 container.fRight >= SkIntToScalar(containee.fRight) &&
237 container.fBottom >= SkIntToScalar(containee.fBottom);
238}
239
240
robertphillips@google.comf294b772012-04-27 14:29:26 +0000241////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000242// determines how many elements at the head of the clip can be skipped and
243// whether the initial clear should be to the inside- or outside-the-clip value,
244// and what op should be used to draw the first element that isn't skipped.
245int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000246 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000247 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000248 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000249
250 // logically before the first element of the clip stack is
251 // processed the clip is entirely open. However, depending on the
252 // first set op we may prefer to clear to 0 for performance. We may
253 // also be able to skip the initial clip paths/rects. We loop until
254 // we cannot skip an element.
255 int curr;
256 bool done = false;
257 *clearToInside = true;
258 int count = clip.getElementCount();
259
260 for (curr = 0; curr < count && !done; ++curr) {
261 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000262 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000263 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000264 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000265 *clearToInside = false;
266 done = true;
267 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000268 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000269 // if this element contains the entire bounds then we
270 // can skip it.
271 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000272 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000273 break;
274 }
275 // if everything is initially clearToInside then intersect is
276 // same as clear to 0 and treat as a replace. Otherwise,
277 // set stays empty.
278 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000279 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280 *clearToInside = false;
281 done = true;
282 }
283 break;
284 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000285 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000286 // if everything is initially outside then union is
287 // same as replace. Otherwise, every pixel is still
288 // clearToInside
289 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000290 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 done = true;
292 }
293 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000294 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 // xor is same as difference or replace both of which
296 // can be 1-pass instead of 2 for xor.
297 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000298 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000299 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000300 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000301 }
302 done = true;
303 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000304 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305 // if all pixels are clearToInside then we have to process the
306 // difference, otherwise it has no effect and all pixels
307 // remain outside.
308 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000309 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 done = true;
311 }
312 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000313 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000314 // if all pixels are clearToInside then reverse difference
315 // produces empty set. Otherise it is same as replace
316 if (*clearToInside) {
317 *clearToInside = false;
318 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000319 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320 done = true;
321 }
322 break;
323 default:
324 GrCrash("Unknown set op.");
325 }
326 }
327 return done ? curr-1 : count;
328}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000329
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000330}
331
robertphillips@google.comf294b772012-04-27 14:29:26 +0000332
333namespace {
334
335////////////////////////////////////////////////////////////////////////////////
336// set up the OpenGL blend function to perform the specified
337// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000338void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000339
340 switch (op) {
341 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000342 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343 break;
344 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000345 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000346 break;
347 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000348 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000349 break;
350 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000351 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000352 break;
353 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000354 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355 break;
356 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000357 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000358 break;
359 default:
360 GrAssert(false);
361 break;
362 }
363}
364
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000366bool draw_path(GrContext* context,
367 GrGpu* gpu,
368 const SkPath& path,
369 GrPathFill fill,
370 bool doAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371
robertphillips@google.com72176b22012-05-23 13:19:12 +0000372 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000373 if (NULL == pr) {
374 return false;
375 }
376
377 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
378 return true;
379}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000380
381}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382
383////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000384bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385 const GrClip& clipIn,
386 int index) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000387 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388 GrAssert(NULL != drawState);
389
390 drawState->setRenderTarget(target->asRenderTarget());
391
392 if (kRect_ClipType == clipIn.getElementType(index)) {
393 if (clipIn.getDoAA(index)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000394 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000395 clipIn.getRect(index),
396 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000398 fGpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399 }
400 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000401 return draw_path(this->getContext(), fGpu,
robertphillips@google.com2c756812012-05-22 20:28:23 +0000402 clipIn.getPath(index),
403 clipIn.getPathFill(index),
404 clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000405 }
406 return true;
407}
408
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000409void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000411 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000412 GrAssert(NULL != drawState);
413
414 // no AA here since it is encoded in the texture
415 drawState->setRenderTarget(target->asRenderTarget());
416
417 GrMatrix sampleM;
418 sampleM.setIDiv(texture->width(), texture->height());
419 drawState->setTexture(0, texture);
420
421 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
422 GrSamplerState::kNearest_Filter,
423 sampleM);
424
robertphillips@google.comf105b102012-05-14 12:18:26 +0000425 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
426 SkIntToScalar(target->height()));
427
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000428 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000429
430 drawState->setTexture(0, NULL);
431}
432
433namespace {
434
435void clear(GrGpu* gpu,
436 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437 GrColor color) {
438 GrDrawState* drawState = gpu->drawState();
439 GrAssert(NULL != drawState);
440
441 // zap entire target to specified color
442 drawState->setRenderTarget(target->asRenderTarget());
443 gpu->clear(NULL, color);
444}
445
robertphillips@google.comf105b102012-05-14 12:18:26 +0000446}
447
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000448// get a texture to act as a temporary buffer for AA clip boolean operations
449// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000450void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000451 GrAutoScratchTexture* temp) {
452 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000453 // we've already allocated the temp texture
454 return;
455 }
456
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000457 GrTextureDesc desc;
458 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
459 desc.fWidth = bounds.width();
460 desc.fHeight = bounds.height();
461 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000462
robertphillips@google.com2c756812012-05-22 20:28:23 +0000463 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000464}
465
robertphillips@google.comf105b102012-05-14 12:18:26 +0000466
467void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000468 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000469 // Since we are setting up the cache we know the last lookup was a miss
470 // Free up the currently cached mask so it can be reused
471 fAACache.reset();
472
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000473 GrTextureDesc desc;
474 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
475 desc.fWidth = bounds.width();
476 desc.fHeight = bounds.height();
477 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000478
479 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000480}
481
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000482////////////////////////////////////////////////////////////////////////////////
483// Shared preamble between gpu and SW-only AA clip mask creation paths.
484// Handles caching, determination of clip mask bound & allocation (if needed)
485// of the result texture
486// Returns true if there is no more work to be done (i.e., we got a cache hit)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000487bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000488 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000489 GrIRect *resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000490 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000491 GrAssert(origDrawState->isClipState());
492
493 GrRenderTarget* rt = origDrawState->getRenderTarget();
494 GrAssert(NULL != rt);
495
496 GrRect rtRect;
497 rtRect.setLTRB(0, 0,
498 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
499
500 // unlike the stencil path the alpha path is not bound to the size of the
501 // render target - determine the minimum size required for the mask
502 GrRect bounds;
503
504 if (clipIn.hasConservativeBounds()) {
505 bounds = clipIn.getConservativeBounds();
506 if (!bounds.intersect(rtRect)) {
507 // the mask will be empty in this case
508 GrAssert(false);
509 bounds.setEmpty();
510 }
511 } else {
512 // still locked to the size of the render target
513 bounds = rtRect;
514 }
515
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000516 GrIRect intBounds;
517 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000518
519 // need to outset a pixel since the standard bounding box computation
520 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000521 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000522
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000523 // TODO: make sure we don't outset if bounds are still 0,0 @ min
524
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000525 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000526 intBounds.width(),
527 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000528 *result = fAACache.getLastMask();
529 fAACache.getLastBound(resultBounds);
530 return true;
531 }
532
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000533 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000534
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000535 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000536 return false;
537}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000538
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000539////////////////////////////////////////////////////////////////////////////////
540// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000541bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000542 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000543 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000544
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000545 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
546
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000547 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000548 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000549 return true;
550 }
551
robertphillips@google.comf105b102012-05-14 12:18:26 +0000552 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000553 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000554 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555 return false;
556 }
557
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000558 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
559 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000561 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562
563 int count = clipIn.getElementCount();
564
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000565 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000566 // if we were able to trim down the size of the mask we need to
567 // offset the paths & rects that will be used to compute it
568 GrMatrix m;
569
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000570 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
571 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000572
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000573 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000574 }
575
576 bool clearToInside;
577 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
578 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000579 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000580 &clearToInside,
581 &startOp);
582
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000583 clear(fGpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000584
robertphillips@google.comf105b102012-05-14 12:18:26 +0000585 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000586
robertphillips@google.comf294b772012-04-27 14:29:26 +0000587 // walk through each clip element and perform its set op
588 for (int c = start; c < count; ++c) {
589
590 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
591
592 if (SkRegion::kReplace_Op == op) {
593 // TODO: replace is actually a lot faster then intersection
594 // for this path - refactor the stencil path so it can handle
595 // replace ops and alter GrClip to allow them through
596
597 // clear the accumulator and draw the new object directly into it
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000598 clear(fGpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000599
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000600 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000601 this->drawClipShape(accum, clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000602
603 } else if (SkRegion::kReverseDifference_Op == op ||
604 SkRegion::kIntersect_Op == op) {
605 // there is no point in intersecting a screen filling rectangle.
606 if (SkRegion::kIntersect_Op == op &&
607 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000608 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000609 continue;
610 }
611
robertphillips@google.comf105b102012-05-14 12:18:26 +0000612 getTemp(*resultBounds, &temp);
613 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000614 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000615 return false;
616 }
617
robertphillips@google.comf294b772012-04-27 14:29:26 +0000618 // clear the temp target & draw into it
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000619 clear(fGpu, temp.texture(), 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000621 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000622 this->drawClipShape(temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000623
624 // TODO: rather than adding these two translations here
625 // compute the bounding box needed to render the texture
626 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000627 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628 GrMatrix m;
629
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000630 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
631 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632
633 drawState->preConcatViewMatrix(m);
634 }
635
636 // Now draw into the accumulator using the real operation
637 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000638 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000639 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000641 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000642 GrMatrix m;
643
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000644 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
645 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646
647 drawState->preConcatViewMatrix(m);
648 }
649
650 } else {
651 // all the remaining ops can just be directly draw into
652 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000653 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000654 this->drawClipShape(accum, clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000655 }
656 }
657
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000658 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000659 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000660 return true;
661}
662
663////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000665bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666 const GrRect& bounds,
667 ScissoringSettings* scissorSettings) {
668
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000669 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000670
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000671 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000672 GrAssert(drawState->isClipState());
673
674 GrRenderTarget* rt = drawState->getRenderTarget();
675 GrAssert(NULL != rt);
676
677 // TODO: dynamically attach a SB when needed.
678 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
679 if (NULL == stencilBuffer) {
680 return false;
681 }
682
683 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
684
685 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
686
687 // we set the current clip to the bounds so that our recursive
688 // draws are scissored to them. We use the copy of the complex clip
689 // we just stashed on the SB to render from. We set it back after
690 // we finish drawing it into the stencil.
691 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000692 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000694 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
695 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000697 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000699 fGpu->disableScissor();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000700#if !VISUALIZE_COMPLEX_CLIP
701 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
702#endif
703
704 int count = clipCopy.getElementCount();
705 int clipBit = stencilBuffer->bits();
706 SkASSERT((clipBit <= 16) &&
707 "Ganesh only handles 16b or smaller stencil buffers");
708 clipBit = (1 << (clipBit-1));
709
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000710 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
712 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000713 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 int start = process_initial_clip_elements(clipCopy,
715 rtRect,
716 &clearToInside,
717 &startOp);
718
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000719 fGpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720
721 // walk through each clip element and perform its set op
722 // with the existing clip.
723 for (int c = start; c < count; ++c) {
724 GrPathFill fill;
725 bool fillInverted;
726 // enabled at bottom of loop
727 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
728
729 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000730 // directly to the stencil buffer
731 // with a non-inverted fill rule
732 // without extra passes to
733 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734
robertphillips@google.comf294b772012-04-27 14:29:26 +0000735 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
736
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000738 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739 if (kRect_ClipType == clipCopy.getElementType(c)) {
740 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000741 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 fillInverted = false;
743 // there is no point in intersecting a screen filling
744 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000745 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000746 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747 continue;
748 }
749 } else {
750 fill = clipCopy.getPathFill(c);
751 fillInverted = GrIsFillInverted(fill);
752 fill = GrNonInvertedFill(fill);
753 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000754 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000755 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000756 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000758 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759 return false;
760 }
761 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000762 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000763 }
764
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765 int passes;
766 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
767
768 bool canDrawDirectToClip; // Given the renderer, the element,
769 // fill rule, and set operation can
770 // we render the element directly to
771 // stencil bit used for clipping.
772 canDrawDirectToClip =
773 GrStencilSettings::GetClipPasses(op,
774 canRenderDirectToStencil,
775 clipBit,
776 fillInverted,
777 &passes, stencilSettings);
778
779 // draw the element to the client stencil bits if necessary
780 if (!canDrawDirectToClip) {
781 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
782 kIncClamp_StencilOp,
783 kIncClamp_StencilOp,
784 kAlways_StencilFunc,
785 0xffff,
786 0x0000,
787 0xffff);
788 SET_RANDOM_COLOR
789 if (kRect_ClipType == clipCopy.getElementType(c)) {
790 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000791 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000792 } else {
793 if (canRenderDirectToStencil) {
794 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000795 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000796 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000797 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 }
799 }
800 }
801
802 // now we modify the clip bit by rendering either the clip
803 // element directly or a bounding rect of the entire clip.
804 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
805 for (int p = 0; p < passes; ++p) {
806 *drawState->stencil() = stencilSettings[p];
807 if (canDrawDirectToClip) {
808 if (kRect_ClipType == clipCopy.getElementType(c)) {
809 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000810 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 } else {
812 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000813 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000814 }
815 } else {
816 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000817 fGpu->drawSimpleRect(bounds, NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 }
819 }
820 }
821 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000822 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000823 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000824 // set this last because recursive draws may overwrite it back to kNone.
825 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
826 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000827 return true;
828}
829
bsalomon@google.com411dad02012-06-05 20:24:20 +0000830// mapping of clip-respecting stencil funcs to normal stencil funcs
831// mapping depends on whether stencil-clipping is in effect.
832static const GrStencilFunc
833 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
834 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
835 // In the Clip Funcs
836 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
837 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
838 kLess_StencilFunc, // kLessIfInClip_StencilFunc
839 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
840 // Special in the clip func that forces user's ref to be 0.
841 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
842 // make ref 0 and do normal nequal.
843 },
844 {// Stencil-Clipping is ENABLED
845 // In the Clip Funcs
846 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
847 // eq stencil clip bit, mask
848 // out user bits.
849
850 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
851 // add stencil bit to mask and ref
852
853 kLess_StencilFunc, // kLessIfInClip_StencilFunc
854 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
855 // for both of these we can add
856 // the clip bit to the mask and
857 // ref and compare as normal
858 // Special in the clip func that forces user's ref to be 0.
859 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
860 // make ref have only the clip bit set
861 // and make comparison be less
862 // 10..0 < 1..user_bits..
863 }
864};
865
866GrStencilFunc GrClipMaskManager::adjustStencilParams(GrStencilFunc func,
867 StencilClipMode mode,
868 unsigned int stencilBitCnt,
869 unsigned int* ref,
870 unsigned int* mask,
871 unsigned int* writeMask) {
872 GrAssert(stencilBitCnt > 0);
873 GrAssert((unsigned) func < kStencilFuncCount);
874
875 if (kModifyClip_StencilClipMode == mode) {
876 // We assume that this class is the client/draw-caller of the GrGpu and
877 // has already setup the correct values
878 return func;
879 }
880 unsigned int clipBit = (1 << (stencilBitCnt - 1));
881 unsigned int userBits = clipBit - 1;
882
883 *writeMask &= userBits;
884
885 if (func >= kBasicStencilFuncCount) {
886 int respectClip = kRespectClip_StencilClipMode == mode;
887 if (respectClip) {
888 // The GrGpu class should have checked this
889 GrAssert(this->isClipInStencil());
890 switch (func) {
891 case kAlwaysIfInClip_StencilFunc:
892 *mask = clipBit;
893 *ref = clipBit;
894 break;
895 case kEqualIfInClip_StencilFunc:
896 case kLessIfInClip_StencilFunc:
897 case kLEqualIfInClip_StencilFunc:
898 *mask = (*mask & userBits) | clipBit;
899 *ref = (*ref & userBits) | clipBit;
900 break;
901 case kNonZeroIfInClip_StencilFunc:
902 *mask = (*mask & userBits) | clipBit;
903 *ref = clipBit;
904 break;
905 default:
906 GrCrash("Unknown stencil func");
907 }
908 } else {
909 *mask &= userBits;
910 *ref &= userBits;
911 }
912 const GrStencilFunc* table = gSpecialToBasicStencilFunc[respectClip];
913 func = table[func - kBasicStencilFuncCount];
914 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
915 } else {
916 *mask &= userBits;
917 *ref &= userBits;
918 }
919 return func;
920}
921
922////////////////////////////////////////////////////////////////////////////////
923
robertphillips@google.comfa662942012-05-17 12:20:22 +0000924namespace {
925
926GrPathFill invert_fill(GrPathFill fill) {
927 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000928 kInverseWinding_GrPathFill, // kWinding_GrPathFill
929 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
930 kWinding_GrPathFill, // kInverseWinding_GrPathFill
931 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
932 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +0000933 };
bsalomon@google.com47059542012-06-06 20:51:20 +0000934 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
935 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
936 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
937 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
938 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
939 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000940 return gInvertedFillTable[fill];
941}
942
943}
944
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000945bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000946 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000947 GrIRect *resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000948 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000949
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000950 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000951 return true;
952 }
953
robertphillips@google.comf105b102012-05-14 12:18:26 +0000954 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000955 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000956 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000957 return false;
958 }
959
robertphillips@google.com2c756812012-05-22 20:28:23 +0000960 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000961
robertphillips@google.comfa662942012-05-17 12:20:22 +0000962 helper.init(*resultBounds, NULL, false);
963
964 int count = clipIn.getElementCount();
965
966 bool clearToInside;
967 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
968 int start = process_initial_clip_elements(clipIn,
969 *resultBounds,
970 &clearToInside,
971 &startOp);
972
973 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
974
975 for (int i = start; i < count; ++i) {
976
977 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
978
979 if (SkRegion::kIntersect_Op == op ||
980 SkRegion::kReverseDifference_Op == op) {
981 // Intersect and reverse difference require modifying pixels
982 // outside of the geometry that is being "drawn". In both cases
983 // we erase all the pixels outside of the geometry but
984 // leave the pixels inside the geometry alone. For reverse
985 // difference we invert all the pixels before clearing the ones
986 // outside the geometry.
987 if (SkRegion::kReverseDifference_Op == op) {
988 SkRect temp = SkRect::MakeLTRB(
989 SkIntToScalar(resultBounds->left()),
990 SkIntToScalar(resultBounds->top()),
991 SkIntToScalar(resultBounds->right()),
992 SkIntToScalar(resultBounds->bottom()));
993
994 // invert the entire scene
995 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
996 }
997
998 if (kRect_ClipType == clipIn.getElementType(i)) {
999
1000 // convert the rect to a path so we can invert the fill
1001 SkPath temp;
1002 temp.addRect(clipIn.getRect(i));
1003
1004 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001005 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001006 0x00000000);
1007 } else {
1008 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1009
1010 helper.draw(clipIn.getPath(i),
1011 SkRegion::kReplace_Op,
1012 invert_fill(clipIn.getPathFill(i)),
1013 clipIn.getDoAA(i),
1014 0x00000000);
1015 }
1016
1017 continue;
1018 }
1019
1020 // The other ops (union, xor, diff) only affect pixels inside
1021 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001022 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001023
1024 helper.draw(clipIn.getRect(i),
1025 op,
1026 clipIn.getDoAA(i), SK_ColorWHITE);
1027
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001028 } else {
1029 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1030
robertphillips@google.comfa662942012-05-17 12:20:22 +00001031 helper.draw(clipIn.getPath(i),
1032 op,
1033 clipIn.getPathFill(i),
1034 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001035 }
1036 }
1037
robertphillips@google.comfa662942012-05-17 12:20:22 +00001038 // Because we are using the scratch texture cache, "accum" may be
1039 // larger than expected and have some cruft in the areas we aren't using.
1040 // Clear it out.
1041
1042 // TODO: need a simpler way to clear the texture - can we combine
1043 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001044 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001045 GrAssert(NULL != drawState);
1046 GrRenderTarget* temp = drawState->getRenderTarget();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001047 clear(fGpu, accum, 0x00000000);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001048 // can't leave the accum bound as a rendertarget
1049 drawState->setRenderTarget(temp);
1050
1051 helper.toTexture(accum);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001052
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001053 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001054
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001055 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001056 return true;
1057}
1058
robertphillips@google.comf294b772012-04-27 14:29:26 +00001059////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001060void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001061 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001062}