blob: 2e9a4b9bd4463d709885f001cf1aa5eff5051eb4 [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.coma72eef32012-05-01 17:22:59 +000016
17//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000018//#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000019
robertphillips@google.comf294b772012-04-27 14:29:26 +000020////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +000021void ScissoringSettings::setupScissoring(GrGpu* gpu) {
22 if (!fEnableScissoring) {
23 gpu->disableScissor();
24 return;
25 }
26
27 gpu->enableScissoring(fScissorRect);
28}
29
robertphillips@google.coma72eef32012-05-01 17:22:59 +000030namespace {
31// set up the draw state to enable the aa clipping mask. Besides setting up the
32// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000033void setup_drawstate_aaclip(GrGpu* gpu,
34 GrTexture* result,
35 const GrRect &bound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036 GrDrawState* drawState = gpu->drawState();
37 GrAssert(drawState);
38
39 static const int maskStage = GrPaint::kTotalStages+1;
40
41 GrMatrix mat;
42 mat.setIDiv(result->width(), result->height());
43 mat.preTranslate(-bound.fLeft, -bound.fTop);
44 mat.preConcat(drawState->getViewMatrix());
45
46 drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode,
47 GrSamplerState::kNearest_Filter,
48 mat);
49
50 drawState->setTexture(maskStage, result);
51
52 // The AA clipping determination happens long after the geometry has
53 // been set up to draw. Here we directly enable the AA clip mask stage
54 gpu->addToVertexLayout(
55 GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(maskStage));
56}
57
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000058bool create_mask_in_sw() {
59 return false;
60}
61
robertphillips@google.coma72eef32012-05-01 17:22:59 +000062}
63
robertphillips@google.comf294b772012-04-27 14:29:26 +000064////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000065// sort out what kind of clip mask needs to be created: alpha, stencil,
66// scissor, or entirely software
robertphillips@google.com1e945b72012-04-16 18:03:03 +000067bool GrClipMaskManager::createClipMask(GrGpu* gpu,
68 const GrClip& clipIn,
69 ScissoringSettings* scissorSettings) {
70
71 GrAssert(scissorSettings);
72
73 scissorSettings->fEnableScissoring = false;
74 fClipMaskInStencil = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +000075 fClipMaskInAlpha = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000076
77 GrDrawState* drawState = gpu->drawState();
78 if (!drawState->isClipState()) {
79 return true;
80 }
81
82 GrRenderTarget* rt = drawState->getRenderTarget();
83
84 // GrDrawTarget should have filtered this for us
85 GrAssert(NULL != rt);
86
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000087#if GR_SW_CLIP
88 if (create_mask_in_sw()) {
89 // The clip geometry is complex enough that it will be more
90 // efficient to create it entirely in software
91 GrTexture* result = NULL;
92 GrRect bound;
93 if (this->createSoftwareClipMask(gpu, clipIn, &result, &bound)) {
94 fClipMaskInAlpha = true;
95
96 setup_drawstate_aaclip(gpu, result, bound);
97 return true;
98 }
99 }
100#endif
101
robertphillips@google.comf294b772012-04-27 14:29:26 +0000102#if GR_AA_CLIP
103 // If MSAA is enabled use the (faster) stencil path for AA clipping
104 // otherwise the alpha clip mask is our only option
105 if (clipIn.requiresAA() && 0 == rt->numSamples()) {
106 // Since we are going to create a destination texture of the correct
107 // size for the mask (rather than being bound by the size of the
108 // render target) we aren't going to use scissoring like the stencil
109 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000110 GrTexture* result = NULL;
111 GrRect bound;
112 if (this->createAlphaClipMask(gpu, clipIn, &result, &bound)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000113 fClipMaskInAlpha = true;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000114
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000115 setup_drawstate_aaclip(gpu, result, bound);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000116 return true;
117 }
118
119 // if alpha clip mask creation fails fall through to the stencil
120 // buffer method
121 }
122#endif // GR_AA_CLIP
123
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000124 GrRect bounds;
125 GrRect rtRect;
126 rtRect.setLTRB(0, 0,
127 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
128 if (clipIn.hasConservativeBounds()) {
129 bounds = clipIn.getConservativeBounds();
130 if (!bounds.intersect(rtRect)) {
131 bounds.setEmpty();
132 }
133 } else {
134 bounds = rtRect;
135 }
136
137 bounds.roundOut(&scissorSettings->fScissorRect);
138 if (scissorSettings->fScissorRect.isEmpty()) {
139 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
140 // TODO: I think we can do an early exit here - after refactoring try:
141 // set fEnableScissoring to true but leave fClipMaskInStencil false
142 // and return - everything is going to be scissored away anyway!
143 }
144 scissorSettings->fEnableScissoring = true;
145
146 // use the stencil clip if we can't represent the clip as a rectangle.
147 fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
148 !bounds.isEmpty();
149
150 if (fClipMaskInStencil) {
151 return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings);
152 }
153
154 return true;
155}
156
157#define VISUALIZE_COMPLEX_CLIP 0
158
159#if VISUALIZE_COMPLEX_CLIP
160 #include "GrRandom.h"
161 GrRandom gRandom;
162 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
163#else
164 #define SET_RANDOM_COLOR
165#endif
166
167namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000168////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000169// determines how many elements at the head of the clip can be skipped and
170// whether the initial clear should be to the inside- or outside-the-clip value,
171// and what op should be used to draw the first element that isn't skipped.
172int process_initial_clip_elements(const GrClip& clip,
173 const GrRect& bounds,
174 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000175 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000176
177 // logically before the first element of the clip stack is
178 // processed the clip is entirely open. However, depending on the
179 // first set op we may prefer to clear to 0 for performance. We may
180 // also be able to skip the initial clip paths/rects. We loop until
181 // we cannot skip an element.
182 int curr;
183 bool done = false;
184 *clearToInside = true;
185 int count = clip.getElementCount();
186
187 for (curr = 0; curr < count && !done; ++curr) {
188 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000189 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000190 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000191 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000192 *clearToInside = false;
193 done = true;
194 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000195 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000196 // if this element contains the entire bounds then we
197 // can skip it.
198 if (kRect_ClipType == clip.getElementType(curr)
199 && clip.getRect(curr).contains(bounds)) {
200 break;
201 }
202 // if everything is initially clearToInside then intersect is
203 // same as clear to 0 and treat as a replace. Otherwise,
204 // set stays empty.
205 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000206 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000207 *clearToInside = false;
208 done = true;
209 }
210 break;
211 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000212 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000213 // if everything is initially outside then union is
214 // same as replace. Otherwise, every pixel is still
215 // clearToInside
216 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000217 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000218 done = true;
219 }
220 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000221 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000222 // xor is same as difference or replace both of which
223 // can be 1-pass instead of 2 for xor.
224 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000225 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000226 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000227 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000228 }
229 done = true;
230 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000231 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232 // if all pixels are clearToInside then we have to process the
233 // difference, otherwise it has no effect and all pixels
234 // remain outside.
235 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000236 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000237 done = true;
238 }
239 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000240 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000241 // if all pixels are clearToInside then reverse difference
242 // produces empty set. Otherise it is same as replace
243 if (*clearToInside) {
244 *clearToInside = false;
245 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000246 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000247 done = true;
248 }
249 break;
250 default:
251 GrCrash("Unknown set op.");
252 }
253 }
254 return done ? curr-1 : count;
255}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000256
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000257}
258
robertphillips@google.comf294b772012-04-27 14:29:26 +0000259
260namespace {
261
262////////////////////////////////////////////////////////////////////////////////
263// set up the OpenGL blend function to perform the specified
264// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000265void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000266
267 switch (op) {
268 case SkRegion::kReplace_Op:
269 drawState->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff);
270 break;
271 case SkRegion::kIntersect_Op:
272 drawState->setBlendFunc(kDC_BlendCoeff, kZero_BlendCoeff);
273 break;
274 case SkRegion::kUnion_Op:
275 drawState->setBlendFunc(kOne_BlendCoeff, kISC_BlendCoeff);
276 break;
277 case SkRegion::kXOR_Op:
278 drawState->setBlendFunc(kIDC_BlendCoeff, kISC_BlendCoeff);
279 break;
280 case SkRegion::kDifference_Op:
281 drawState->setBlendFunc(kZero_BlendCoeff, kISC_BlendCoeff);
282 break;
283 case SkRegion::kReverseDifference_Op:
284 drawState->setBlendFunc(kIDC_BlendCoeff, kZero_BlendCoeff);
285 break;
286 default:
287 GrAssert(false);
288 break;
289 }
290}
291
292}
293
294////////////////////////////////////////////////////////////////////////////////
295bool GrClipMaskManager::drawPath(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000296 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000297 GrPathFill fill,
298 bool doAA) {
299
300 GrPathRenderer* pr = this->getClipPathRenderer(gpu, path, fill, doAA);
301 if (NULL == pr) {
302 return false;
303 }
304
305 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
306 return true;
307}
308
309////////////////////////////////////////////////////////////////////////////////
310bool GrClipMaskManager::drawClipShape(GrGpu* gpu,
311 GrTexture* target,
312 const GrClip& clipIn,
313 int index) {
314 GrDrawState* drawState = gpu->drawState();
315 GrAssert(NULL != drawState);
316
317 drawState->setRenderTarget(target->asRenderTarget());
318
319 if (kRect_ClipType == clipIn.getElementType(index)) {
320 if (clipIn.getDoAA(index)) {
321 // convert the rect to a path for AA
322 SkPath temp;
323 temp.addRect(clipIn.getRect(index));
324
325 return this->drawPath(gpu, temp,
326 kEvenOdd_PathFill, clipIn.getDoAA(index));
327 } else {
328 gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
329 }
330 } else {
331 return this->drawPath(gpu,
332 clipIn.getPath(index),
333 clipIn.getPathFill(index),
334 clipIn.getDoAA(index));
335 }
336 return true;
337}
338
339void GrClipMaskManager::drawTexture(GrGpu* gpu,
340 GrTexture* target,
341 const GrRect& rect,
342 GrTexture* texture) {
343 GrDrawState* drawState = gpu->drawState();
344 GrAssert(NULL != drawState);
345
346 // no AA here since it is encoded in the texture
347 drawState->setRenderTarget(target->asRenderTarget());
348
349 GrMatrix sampleM;
350 sampleM.setIDiv(texture->width(), texture->height());
351 drawState->setTexture(0, texture);
352
353 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
354 GrSamplerState::kNearest_Filter,
355 sampleM);
356
357 gpu->drawSimpleRect(rect, NULL, 1 << 0);
358
359 drawState->setTexture(0, NULL);
360}
361
362namespace {
363
364void clear(GrGpu* gpu,
365 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000366 GrColor color) {
367 GrDrawState* drawState = gpu->drawState();
368 GrAssert(NULL != drawState);
369
370 // zap entire target to specified color
371 drawState->setRenderTarget(target->asRenderTarget());
372 gpu->clear(NULL, color);
373}
374
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000375// get a texture to act as a temporary buffer for AA clip boolean operations
376// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000377void get_temp(GrGpu *gpu, const GrRect& bounds, GrTexture** temp) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000378 if (NULL != *temp) {
379 // we've already allocated the temp texture
380 return;
381 }
382
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000383 const GrTextureDesc desc = {
384 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
385 SkScalarCeilToInt(bounds.width()),
386 SkScalarCeilToInt(bounds.height()),
387 kAlpha_8_GrPixelConfig,
388 0 // samples
389 };
390
391 *temp = gpu->createTexture(desc, NULL, 0);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000392}
393
394}
395
396void GrClipMaskManager::getAccum(GrGpu* gpu,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000397 const GrRect& bounds,
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000398 GrTexture** accum) {
399 GrAssert(NULL == *accum);
400
401 // since we are getting an accumulator we know our cache is shot. See
402 // if we can reuse the texture stored in the cache
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000403 if (fAACache.getLastMaskWidth() >= bounds.width() &&
404 fAACache.getLastMaskHeight() >= bounds.height()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000405 // we can just reuse the existing texture
406 *accum = fAACache.detachLastMask();
407 fAACache.reset();
408 } else {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000409 const GrTextureDesc desc = {
410 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
411 SkScalarCeilToInt(bounds.width()),
412 SkScalarCeilToInt(bounds.height()),
413 kAlpha_8_GrPixelConfig,
414 0 // samples
415 };
416
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000417 *accum = gpu->createTexture(desc, NULL, 0);
418 }
419
420 GrAssert(1 == (*accum)->getRefCnt());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000421}
422
robertphillips@google.comf294b772012-04-27 14:29:26 +0000423
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000424////////////////////////////////////////////////////////////////////////////////
425// Shared preamble between gpu and SW-only AA clip mask creation paths.
426// Handles caching, determination of clip mask bound & allocation (if needed)
427// of the result texture
428// Returns true if there is no more work to be done (i.e., we got a cache hit)
429bool GrClipMaskManager::clipMaskPreamble(GrGpu* gpu,
430 const GrClip& clipIn,
431 GrTexture** result,
432 GrRect *resultBounds,
433 GrTexture** maskStorage) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000434 GrDrawState* origDrawState = gpu->drawState();
435 GrAssert(origDrawState->isClipState());
436
437 GrRenderTarget* rt = origDrawState->getRenderTarget();
438 GrAssert(NULL != rt);
439
440 GrRect rtRect;
441 rtRect.setLTRB(0, 0,
442 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
443
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000444
robertphillips@google.comf294b772012-04-27 14:29:26 +0000445 // unlike the stencil path the alpha path is not bound to the size of the
446 // render target - determine the minimum size required for the mask
447 GrRect bounds;
448
449 if (clipIn.hasConservativeBounds()) {
450 bounds = clipIn.getConservativeBounds();
451 if (!bounds.intersect(rtRect)) {
452 // the mask will be empty in this case
453 GrAssert(false);
454 bounds.setEmpty();
455 }
456 } else {
457 // still locked to the size of the render target
458 bounds = rtRect;
459 }
460
461 bounds.roundOut();
462
463 // need to outset a pixel since the standard bounding box computation
464 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
465 bounds.outset(SkIntToScalar(1), SkIntToScalar(1));
466
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000467 // TODO: make sure we don't outset if bounds are still 0,0 @ min
468
robertphillips@google.comf294b772012-04-27 14:29:26 +0000469 GrAssert(SkScalarIsInt(bounds.width()));
470 GrAssert(SkScalarIsInt(bounds.height()));
471
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000472 if (fAACache.canReuse(clipIn,
473 SkScalarCeilToInt(bounds.width()),
474 SkScalarCeilToInt(bounds.height()))) {
475 *result = fAACache.getLastMask();
476 fAACache.getLastBound(resultBounds);
477 return true;
478 }
479
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000480 this->getAccum(gpu, bounds, maskStorage);
481 *resultBounds = bounds;
482 return false;
483}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000484
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000485////////////////////////////////////////////////////////////////////////////////
486// Create a 8-bit clip mask in alpha
487bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu,
488 const GrClip& clipIn,
489 GrTexture** result,
490 GrRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000491
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000492 GrTexture* accum = NULL;
493 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds, &accum)) {
494 return true;
495 }
496
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000497 if (NULL == accum) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498 fClipMaskInAlpha = false;
499 return false;
500 }
501
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000502 GrRect newRTBounds;
503 newRTBounds.setLTRB(0, 0, resultBounds->width(), resultBounds->height());
504
robertphillips@google.comf294b772012-04-27 14:29:26 +0000505 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
506 GrDrawState* drawState = gpu->drawState();
507
508 GrDrawTarget::AutoGeometryPush agp(gpu);
509
510 int count = clipIn.getElementCount();
511
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000512 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000513 // if we were able to trim down the size of the mask we need to
514 // offset the paths & rects that will be used to compute it
515 GrMatrix m;
516
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000517 m.setTranslate(-resultBounds->fLeft, -resultBounds->fTop);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000518
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000519 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000520 }
521
522 bool clearToInside;
523 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
524 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000525 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000526 &clearToInside,
527 &startOp);
528
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000529 clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000530
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000531 GrTexture* temp = NULL;
532
robertphillips@google.comf294b772012-04-27 14:29:26 +0000533 // walk through each clip element and perform its set op
534 for (int c = start; c < count; ++c) {
535
536 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
537
538 if (SkRegion::kReplace_Op == op) {
539 // TODO: replace is actually a lot faster then intersection
540 // for this path - refactor the stencil path so it can handle
541 // replace ops and alter GrClip to allow them through
542
543 // clear the accumulator and draw the new object directly into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000544 clear(gpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000545
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000547 this->drawClipShape(gpu, accum, clipIn, c);
548
549 } else if (SkRegion::kReverseDifference_Op == op ||
550 SkRegion::kIntersect_Op == op) {
551 // there is no point in intersecting a screen filling rectangle.
552 if (SkRegion::kIntersect_Op == op &&
553 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000554 clipIn.getRect(c).contains(*resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555 continue;
556 }
557
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000558 get_temp(gpu, *resultBounds, &temp);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000559 if (NULL == temp) {
560 fClipMaskInAlpha = false;
561 SkSafeUnref(accum);
562 return false;
563 }
564
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565 // clear the temp target & draw into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000566 clear(gpu, temp, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000567
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000568 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000569 this->drawClipShape(gpu, temp, clipIn, c);
570
571 // TODO: rather than adding these two translations here
572 // compute the bounding box needed to render the texture
573 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000574 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000575 GrMatrix m;
576
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000577 m.setTranslate(resultBounds->fLeft, resultBounds->fTop);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000578
579 drawState->preConcatViewMatrix(m);
580 }
581
582 // Now draw into the accumulator using the real operation
583 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000584 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000585 this->drawTexture(gpu, accum, newRTBounds, temp);
586
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000587 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000588 GrMatrix m;
589
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000590 m.setTranslate(-resultBounds->fLeft, -resultBounds->fTop);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591
592 drawState->preConcatViewMatrix(m);
593 }
594
595 } else {
596 // all the remaining ops can just be directly draw into
597 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000598 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000599 this->drawClipShape(gpu, accum, clipIn, c);
600 }
601 }
602
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000603 fAACache.set(clipIn, accum, *resultBounds);
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000604 *result = accum;
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000605 SkSafeUnref(accum); // fAACache still has a ref to accum
606 SkSafeUnref(temp);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000607
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608 return true;
609}
610
611////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000612// Create a 1-bit clip mask in the stencil buffer
613bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
614 const GrClip& clipIn,
615 const GrRect& bounds,
616 ScissoringSettings* scissorSettings) {
617
618 GrAssert(fClipMaskInStencil);
619
620 GrDrawState* drawState = gpu->drawState();
621 GrAssert(drawState->isClipState());
622
623 GrRenderTarget* rt = drawState->getRenderTarget();
624 GrAssert(NULL != rt);
625
626 // TODO: dynamically attach a SB when needed.
627 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
628 if (NULL == stencilBuffer) {
629 return false;
630 }
631
632 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
633
634 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
635
636 // we set the current clip to the bounds so that our recursive
637 // draws are scissored to them. We use the copy of the complex clip
638 // we just stashed on the SB to render from. We set it back after
639 // we finish drawing it into the stencil.
640 const GrClip& clipCopy = stencilBuffer->getLastClip();
641 gpu->setClip(GrClip(bounds));
642
643 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
644 drawState = gpu->drawState();
645 drawState->setRenderTarget(rt);
646 GrDrawTarget::AutoGeometryPush agp(gpu);
647
648 gpu->disableScissor();
649#if !VISUALIZE_COMPLEX_CLIP
650 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
651#endif
652
653 int count = clipCopy.getElementCount();
654 int clipBit = stencilBuffer->bits();
655 SkASSERT((clipBit <= 16) &&
656 "Ganesh only handles 16b or smaller stencil buffers");
657 clipBit = (1 << (clipBit-1));
658
659 GrRect rtRect;
660 rtRect.setLTRB(0, 0,
661 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
662
663 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000664 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665 int start = process_initial_clip_elements(clipCopy,
666 rtRect,
667 &clearToInside,
668 &startOp);
669
670 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
671
672 // walk through each clip element and perform its set op
673 // with the existing clip.
674 for (int c = start; c < count; ++c) {
675 GrPathFill fill;
676 bool fillInverted;
677 // enabled at bottom of loop
678 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
679
680 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000681 // directly to the stencil buffer
682 // with a non-inverted fill rule
683 // without extra passes to
684 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000685
robertphillips@google.comf294b772012-04-27 14:29:26 +0000686 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
687
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000689 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 if (kRect_ClipType == clipCopy.getElementType(c)) {
691 canRenderDirectToStencil = true;
692 fill = kEvenOdd_PathFill;
693 fillInverted = false;
694 // there is no point in intersecting a screen filling
695 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000696 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697 clipCopy.getRect(c).contains(rtRect)) {
698 continue;
699 }
700 } else {
701 fill = clipCopy.getPathFill(c);
702 fillInverted = GrIsFillInverted(fill);
703 fill = GrNonInvertedFill(fill);
704 clipPath = &clipCopy.getPath(c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000705 pr = this->getClipPathRenderer(gpu, *clipPath, fill, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706 if (NULL == pr) {
707 fClipMaskInStencil = false;
708 gpu->setClip(clipCopy); // restore to the original
709 return false;
710 }
711 canRenderDirectToStencil =
712 !pr->requiresStencilPass(*clipPath, fill, gpu);
713 }
714
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 int passes;
716 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
717
718 bool canDrawDirectToClip; // Given the renderer, the element,
719 // fill rule, and set operation can
720 // we render the element directly to
721 // stencil bit used for clipping.
722 canDrawDirectToClip =
723 GrStencilSettings::GetClipPasses(op,
724 canRenderDirectToStencil,
725 clipBit,
726 fillInverted,
727 &passes, stencilSettings);
728
729 // draw the element to the client stencil bits if necessary
730 if (!canDrawDirectToClip) {
731 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
732 kIncClamp_StencilOp,
733 kIncClamp_StencilOp,
734 kAlways_StencilFunc,
735 0xffff,
736 0x0000,
737 0xffff);
738 SET_RANDOM_COLOR
739 if (kRect_ClipType == clipCopy.getElementType(c)) {
740 *drawState->stencil() = gDrawToStencil;
741 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
742 } else {
743 if (canRenderDirectToStencil) {
744 *drawState->stencil() = gDrawToStencil;
745 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
746 } else {
747 pr->drawPathToStencil(*clipPath, fill, gpu);
748 }
749 }
750 }
751
752 // now we modify the clip bit by rendering either the clip
753 // element directly or a bounding rect of the entire clip.
754 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
755 for (int p = 0; p < passes; ++p) {
756 *drawState->stencil() = stencilSettings[p];
757 if (canDrawDirectToClip) {
758 if (kRect_ClipType == clipCopy.getElementType(c)) {
759 SET_RANDOM_COLOR
760 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
761 } else {
762 SET_RANDOM_COLOR
763 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
764 }
765 } else {
766 SET_RANDOM_COLOR
767 gpu->drawSimpleRect(bounds, NULL, 0);
768 }
769 }
770 }
771 // restore clip
772 gpu->setClip(clipCopy);
773 // recusive draws would have disabled this since they drew with
774 // the clip bounds as clip.
775 fClipMaskInStencil = true;
776 }
777
778 return true;
779}
780
robertphillips@google.comf294b772012-04-27 14:29:26 +0000781////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000782bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu,
783 const GrClip& clipIn,
784 GrTexture** result,
785 GrRect *resultBounds) {
786
787 GrTexture* accum = NULL;
788 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds, &accum)) {
789 return true;
790 }
791
792 if (NULL == accum) {
793 fClipMaskInAlpha = false;
794 return false;
795 }
796
797#if 0
798 SkRasterClip rasterClip;
799
800 // TODO: refactor GrClip out of existance and use SkCanvas's ClipVisitor
801 // - may have to move it to SkClipStack
802 for (int i = 0; i < clipIn.getElementCount(); ++i) {
803 if (kRect_ClipType == clipIn.getElementType(i)) {
804 rasterClip.op(clipIn.getRect(i), clipIn.getOp(i), clipIn.getDoAA(i));
805 } else {
806 GrAssert(kPath_ClipType == clipIn.getElementType(i));
807
808 SkIPoint deviceSize = SkIPoint::Make(resultBounds->width(),
809 resultBounds->height());
810
811 SkRasterClip::clipPathHelper(&rasterClip,
812 clipIn.getPath(i),
813 clipIn.getOp(i),
814 clipIn.getDoAA(i),
815 deviceSize);
816 }
817 }
818
819 // TODO: need to get pixels out of SkRasterClip & into the texture!
820#endif
821
822 fAACache.set(clipIn, accum, *resultBounds);
823 *result = accum;
824 SkSafeUnref(accum); // fAACache still has a ref to accum
825
826 return true;
827}
828
829
830////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831GrPathRenderer* GrClipMaskManager::getClipPathRenderer(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000832 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000833 GrPathFill fill,
834 bool antiAlias) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000835 if (NULL == fPathRendererChain) {
836 fPathRendererChain =
837 new GrPathRendererChain(gpu->getContext(),
robertphillips@google.comf294b772012-04-27 14:29:26 +0000838 GrPathRendererChain::kNone_UsageFlag);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000839 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000840 return fPathRendererChain->getPathRenderer(path, fill, gpu, antiAlias);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000841}
842
robertphillips@google.comf294b772012-04-27 14:29:26 +0000843////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844void GrClipMaskManager::freeResources() {
845 // in case path renderer has any GrResources, start from scratch
846 GrSafeSetNull(fPathRendererChain);
847}