blob: a2bc2984beee842d460789c9cc10417e428bec80 [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.com6b70a7b2012-05-11 15:32:48 +000063}
64
robertphillips@google.comfa662942012-05-17 12:20:22 +000065/*
66 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
67 * will be used on any element. If so, it returns true to indicate that the
68 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
69 */
70bool GrClipMaskManager::useSWOnlyPath(GrGpu* gpu, const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000071
72 if (!clipIn.requiresAA()) {
73 // The stencil buffer can handle this case
74 return false;
75 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000076
77 // TODO: generalize this test so that when
78 // a clip gets complex enough it can just be done in SW regardless
79 // of whether it would invoke the GrSoftwarePathRenderer.
80 bool useSW = false;
81
82 for (int i = 0; i < clipIn.getElementCount(); ++i) {
83
84 if (SkRegion::kReplace_Op == clipIn.getOp(i)) {
85 // Everything before a replace op can be ignored so start
86 // afresh w.r.t. determining if any element uses the SW path
87 useSW = false;
88 }
89
90 if (!clipIn.getDoAA(i)) {
91 // non-anti-aliased rects and paths can always be drawn either
92 // directly or by the GrDefaultPathRenderer
93 continue;
94 }
95
96 if (kRect_ClipType == clipIn.getElementType(i)) {
97 // Antialiased rects are converted to paths and then drawn with
98 // kEvenOdd_PathFill.
99 if (!GrAAConvexPathRenderer::staticCanDrawPath(
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000100 true, // always convex
robertphillips@google.comfa662942012-05-17 12:20:22 +0000101 kEvenOdd_PathFill,
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000102 gpu,
103 true)) { // anti-aliased
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 // if the GrAAConvexPathRenderer can't render this rect (due
105 // to lack of derivative support in the shaders) then
106 // the GrSoftwarePathRenderer will be used
107 useSW = true;
108 }
109
110 continue;
111 }
112
113 // only paths need to be considered in the rest of the loop body
114
115 if (GrAAHairLinePathRenderer::staticCanDrawPath(clipIn.getPath(i),
116 clipIn.getPathFill(i),
117 gpu,
118 clipIn.getDoAA(i))) {
119 // the hair line path renderer can handle this one
120 continue;
121 }
122
123 if (GrAAConvexPathRenderer::staticCanDrawPath(
124 clipIn.getPath(i).isConvex(),
125 clipIn.getPathFill(i),
126 gpu,
127 clipIn.getDoAA(i))) {
128 // the convex path renderer can handle this one
129 continue;
130 }
131
132 // otherwise the GrSoftwarePathRenderer is going to be invoked
133 useSW = true;
134 }
135
136 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000137}
138
robertphillips@google.comfa662942012-05-17 12:20:22 +0000139
robertphillips@google.comf294b772012-04-27 14:29:26 +0000140////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000141// sort out what kind of clip mask needs to be created: alpha, stencil,
142// scissor, or entirely software
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000143bool GrClipMaskManager::createClipMask(GrGpu* gpu,
144 const GrClip& clipIn,
145 ScissoringSettings* scissorSettings) {
146
147 GrAssert(scissorSettings);
148
149 scissorSettings->fEnableScissoring = false;
150 fClipMaskInStencil = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000151 fClipMaskInAlpha = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000152
153 GrDrawState* drawState = gpu->drawState();
154 if (!drawState->isClipState()) {
155 return true;
156 }
157
158 GrRenderTarget* rt = drawState->getRenderTarget();
159
160 // GrDrawTarget should have filtered this for us
161 GrAssert(NULL != rt);
162
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000163#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000164 // If MSAA is enabled we can do everything in the stencil buffer.
165 // Otherwise check if we should just create the entire clip mask
166 // in software (this will only happen if the clip mask is anti-aliased
167 // and too complex for the gpu to handle in its entirety)
168 if (0 == rt->numSamples() && useSWOnlyPath(gpu, clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000169 // The clip geometry is complex enough that it will be more
170 // efficient to create it entirely in software
171 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000172 GrIRect bound;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000173 if (this->createSoftwareClipMask(gpu, clipIn, &result, &bound)) {
174 fClipMaskInAlpha = true;
175
176 setup_drawstate_aaclip(gpu, result, bound);
177 return true;
178 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000179
180 // if SW clip mask creation fails fall through to the other
181 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000182 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000183#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000184
robertphillips@google.comf294b772012-04-27 14:29:26 +0000185#if GR_AA_CLIP
186 // If MSAA is enabled use the (faster) stencil path for AA clipping
187 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000188 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000189 // Since we are going to create a destination texture of the correct
190 // size for the mask (rather than being bound by the size of the
191 // render target) we aren't going to use scissoring like the stencil
192 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000193 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000194 GrIRect bound;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000195 if (this->createAlphaClipMask(gpu, clipIn, &result, &bound)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000196 fClipMaskInAlpha = true;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000197
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000198 setup_drawstate_aaclip(gpu, result, bound);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000199 return true;
200 }
201
202 // if alpha clip mask creation fails fall through to the stencil
203 // buffer method
204 }
205#endif // GR_AA_CLIP
206
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000207 // Either a hard (stencil buffer) clip was explicitly requested or
208 // an antialiased clip couldn't be created. In either case, free up
209 // the texture in the antialiased mask cache.
210 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000211 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
212 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000213 // AA cache.
214 fAACache.reset();
215
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000216 GrRect bounds;
217 GrRect rtRect;
218 rtRect.setLTRB(0, 0,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000219 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000220 if (clipIn.hasConservativeBounds()) {
221 bounds = clipIn.getConservativeBounds();
222 if (!bounds.intersect(rtRect)) {
223 bounds.setEmpty();
224 }
225 } else {
226 bounds = rtRect;
227 }
228
229 bounds.roundOut(&scissorSettings->fScissorRect);
230 if (scissorSettings->fScissorRect.isEmpty()) {
231 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
232 // TODO: I think we can do an early exit here - after refactoring try:
233 // set fEnableScissoring to true but leave fClipMaskInStencil false
234 // and return - everything is going to be scissored away anyway!
235 }
236 scissorSettings->fEnableScissoring = true;
237
238 // use the stencil clip if we can't represent the clip as a rectangle.
239 fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
240 !bounds.isEmpty();
241
242 if (fClipMaskInStencil) {
243 return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings);
244 }
245
246 return true;
247}
248
249#define VISUALIZE_COMPLEX_CLIP 0
250
251#if VISUALIZE_COMPLEX_CLIP
252 #include "GrRandom.h"
253 GrRandom gRandom;
254 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
255#else
256 #define SET_RANDOM_COLOR
257#endif
258
259namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000260/**
261 * Does "container" contain "containee"? If either is empty then
262 * no containment is possible.
263 */
264bool contains(const SkRect& container, const SkIRect& containee) {
265 return !containee.isEmpty() && !container.isEmpty() &&
266 container.fLeft <= SkIntToScalar(containee.fLeft) &&
267 container.fTop <= SkIntToScalar(containee.fTop) &&
268 container.fRight >= SkIntToScalar(containee.fRight) &&
269 container.fBottom >= SkIntToScalar(containee.fBottom);
270}
271
272
robertphillips@google.comf294b772012-04-27 14:29:26 +0000273////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000274// determines how many elements at the head of the clip can be skipped and
275// whether the initial clear should be to the inside- or outside-the-clip value,
276// and what op should be used to draw the first element that isn't skipped.
277int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000278 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000279 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000280 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281
282 // logically before the first element of the clip stack is
283 // processed the clip is entirely open. However, depending on the
284 // first set op we may prefer to clear to 0 for performance. We may
285 // also be able to skip the initial clip paths/rects. We loop until
286 // we cannot skip an element.
287 int curr;
288 bool done = false;
289 *clearToInside = true;
290 int count = clip.getElementCount();
291
292 for (curr = 0; curr < count && !done; ++curr) {
293 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000294 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000296 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000297 *clearToInside = false;
298 done = true;
299 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000300 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000301 // if this element contains the entire bounds then we
302 // can skip it.
303 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000304 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305 break;
306 }
307 // if everything is initially clearToInside then intersect is
308 // same as clear to 0 and treat as a replace. Otherwise,
309 // set stays empty.
310 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000311 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 *clearToInside = false;
313 done = true;
314 }
315 break;
316 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000317 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000318 // if everything is initially outside then union is
319 // same as replace. Otherwise, every pixel is still
320 // clearToInside
321 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000322 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000323 done = true;
324 }
325 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000326 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000327 // xor is same as difference or replace both of which
328 // can be 1-pass instead of 2 for xor.
329 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000330 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000332 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333 }
334 done = true;
335 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000336 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000337 // if all pixels are clearToInside then we have to process the
338 // difference, otherwise it has no effect and all pixels
339 // remain outside.
340 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000341 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000342 done = true;
343 }
344 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000345 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000346 // if all pixels are clearToInside then reverse difference
347 // produces empty set. Otherise it is same as replace
348 if (*clearToInside) {
349 *clearToInside = false;
350 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000351 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000352 done = true;
353 }
354 break;
355 default:
356 GrCrash("Unknown set op.");
357 }
358 }
359 return done ? curr-1 : count;
360}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000362}
363
robertphillips@google.comf294b772012-04-27 14:29:26 +0000364
365namespace {
366
367////////////////////////////////////////////////////////////////////////////////
368// set up the OpenGL blend function to perform the specified
369// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000370void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371
372 switch (op) {
373 case SkRegion::kReplace_Op:
374 drawState->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff);
375 break;
376 case SkRegion::kIntersect_Op:
377 drawState->setBlendFunc(kDC_BlendCoeff, kZero_BlendCoeff);
378 break;
379 case SkRegion::kUnion_Op:
380 drawState->setBlendFunc(kOne_BlendCoeff, kISC_BlendCoeff);
381 break;
382 case SkRegion::kXOR_Op:
383 drawState->setBlendFunc(kIDC_BlendCoeff, kISC_BlendCoeff);
384 break;
385 case SkRegion::kDifference_Op:
386 drawState->setBlendFunc(kZero_BlendCoeff, kISC_BlendCoeff);
387 break;
388 case SkRegion::kReverseDifference_Op:
389 drawState->setBlendFunc(kIDC_BlendCoeff, kZero_BlendCoeff);
390 break;
391 default:
392 GrAssert(false);
393 break;
394 }
395}
396
397}
398
399////////////////////////////////////////////////////////////////////////////////
400bool GrClipMaskManager::drawPath(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000401 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402 GrPathFill fill,
403 bool doAA) {
404
405 GrPathRenderer* pr = this->getClipPathRenderer(gpu, path, fill, doAA);
406 if (NULL == pr) {
407 return false;
408 }
409
410 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
411 return true;
412}
413
414////////////////////////////////////////////////////////////////////////////////
415bool GrClipMaskManager::drawClipShape(GrGpu* gpu,
416 GrTexture* target,
417 const GrClip& clipIn,
418 int index) {
419 GrDrawState* drawState = gpu->drawState();
420 GrAssert(NULL != drawState);
421
422 drawState->setRenderTarget(target->asRenderTarget());
423
424 if (kRect_ClipType == clipIn.getElementType(index)) {
425 if (clipIn.getDoAA(index)) {
426 // convert the rect to a path for AA
427 SkPath temp;
428 temp.addRect(clipIn.getRect(index));
429
430 return this->drawPath(gpu, temp,
431 kEvenOdd_PathFill, clipIn.getDoAA(index));
432 } else {
433 gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
434 }
435 } else {
436 return this->drawPath(gpu,
437 clipIn.getPath(index),
438 clipIn.getPathFill(index),
439 clipIn.getDoAA(index));
440 }
441 return true;
442}
443
444void GrClipMaskManager::drawTexture(GrGpu* gpu,
445 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000446 GrTexture* texture) {
447 GrDrawState* drawState = gpu->drawState();
448 GrAssert(NULL != drawState);
449
450 // no AA here since it is encoded in the texture
451 drawState->setRenderTarget(target->asRenderTarget());
452
453 GrMatrix sampleM;
454 sampleM.setIDiv(texture->width(), texture->height());
455 drawState->setTexture(0, texture);
456
457 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
458 GrSamplerState::kNearest_Filter,
459 sampleM);
460
robertphillips@google.comf105b102012-05-14 12:18:26 +0000461 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
462 SkIntToScalar(target->height()));
463
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464 gpu->drawSimpleRect(rect, NULL, 1 << 0);
465
466 drawState->setTexture(0, NULL);
467}
468
469namespace {
470
471void clear(GrGpu* gpu,
472 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473 GrColor color) {
474 GrDrawState* drawState = gpu->drawState();
475 GrAssert(NULL != drawState);
476
477 // zap entire target to specified color
478 drawState->setRenderTarget(target->asRenderTarget());
479 gpu->clear(NULL, color);
480}
481
robertphillips@google.comf105b102012-05-14 12:18:26 +0000482}
483
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000484// get a texture to act as a temporary buffer for AA clip boolean operations
485// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000486void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000487 GrAutoScratchTexture* temp) {
488 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000489 // we've already allocated the temp texture
490 return;
491 }
492
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000493 const GrTextureDesc desc = {
494 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000495 bounds.width(),
496 bounds.height(),
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000497 kAlpha_8_GrPixelConfig,
498 0 // samples
499 };
500
robertphillips@google.comf105b102012-05-14 12:18:26 +0000501 temp->set(fAACache.getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000502}
503
robertphillips@google.comf105b102012-05-14 12:18:26 +0000504
505void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000506 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000507 // Since we are setting up the cache we know the last lookup was a miss
508 // Free up the currently cached mask so it can be reused
509 fAACache.reset();
510
511 const GrTextureDesc desc = {
512 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000513 bounds.width(),
514 bounds.height(),
robertphillips@google.comf105b102012-05-14 12:18:26 +0000515 kAlpha_8_GrPixelConfig,
516 0 // samples
517 };
518
519 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000520}
521
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000522////////////////////////////////////////////////////////////////////////////////
523// Shared preamble between gpu and SW-only AA clip mask creation paths.
524// Handles caching, determination of clip mask bound & allocation (if needed)
525// of the result texture
526// Returns true if there is no more work to be done (i.e., we got a cache hit)
527bool GrClipMaskManager::clipMaskPreamble(GrGpu* gpu,
528 const GrClip& clipIn,
529 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000530 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000531 GrDrawState* origDrawState = gpu->drawState();
532 GrAssert(origDrawState->isClipState());
533
534 GrRenderTarget* rt = origDrawState->getRenderTarget();
535 GrAssert(NULL != rt);
536
537 GrRect rtRect;
538 rtRect.setLTRB(0, 0,
539 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
540
541 // unlike the stencil path the alpha path is not bound to the size of the
542 // render target - determine the minimum size required for the mask
543 GrRect bounds;
544
545 if (clipIn.hasConservativeBounds()) {
546 bounds = clipIn.getConservativeBounds();
547 if (!bounds.intersect(rtRect)) {
548 // the mask will be empty in this case
549 GrAssert(false);
550 bounds.setEmpty();
551 }
552 } else {
553 // still locked to the size of the render target
554 bounds = rtRect;
555 }
556
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000557 GrIRect intBounds;
558 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559
560 // need to outset a pixel since the standard bounding box computation
561 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000562 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000564 // TODO: make sure we don't outset if bounds are still 0,0 @ min
565
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000566 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000567 intBounds.width(),
568 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000569 *result = fAACache.getLastMask();
570 fAACache.getLastBound(resultBounds);
571 return true;
572 }
573
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000574 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000575
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000576 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000577 return false;
578}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000579
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000580////////////////////////////////////////////////////////////////////////////////
581// Create a 8-bit clip mask in alpha
582bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu,
583 const GrClip& clipIn,
584 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000585 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586
robertphillips@google.comf105b102012-05-14 12:18:26 +0000587 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000588 return true;
589 }
590
robertphillips@google.comf105b102012-05-14 12:18:26 +0000591 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000592 if (NULL == accum) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000593 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000594 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000595 return false;
596 }
597
598 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
599 GrDrawState* drawState = gpu->drawState();
600
601 GrDrawTarget::AutoGeometryPush agp(gpu);
602
603 int count = clipIn.getElementCount();
604
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000605 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000606 // if we were able to trim down the size of the mask we need to
607 // offset the paths & rects that will be used to compute it
608 GrMatrix m;
609
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000610 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
611 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000612
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000613 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000614 }
615
616 bool clearToInside;
617 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
618 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000619 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620 &clearToInside,
621 &startOp);
622
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000623 clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000624
robertphillips@google.comf105b102012-05-14 12:18:26 +0000625 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000626
robertphillips@google.comf294b772012-04-27 14:29:26 +0000627 // walk through each clip element and perform its set op
628 for (int c = start; c < count; ++c) {
629
630 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
631
632 if (SkRegion::kReplace_Op == op) {
633 // TODO: replace is actually a lot faster then intersection
634 // for this path - refactor the stencil path so it can handle
635 // replace ops and alter GrClip to allow them through
636
637 // clear the accumulator and draw the new object directly into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000638 clear(gpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000639
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000640 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000641 this->drawClipShape(gpu, accum, clipIn, c);
642
643 } else if (SkRegion::kReverseDifference_Op == op ||
644 SkRegion::kIntersect_Op == op) {
645 // there is no point in intersecting a screen filling rectangle.
646 if (SkRegion::kIntersect_Op == op &&
647 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000648 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000649 continue;
650 }
651
robertphillips@google.comf105b102012-05-14 12:18:26 +0000652 getTemp(*resultBounds, &temp);
653 if (NULL == temp.texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000654 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000655 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000656 return false;
657 }
658
robertphillips@google.comf294b772012-04-27 14:29:26 +0000659 // clear the temp target & draw into it
robertphillips@google.comf105b102012-05-14 12:18:26 +0000660 clear(gpu, temp.texture(), 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000661
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000662 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000663 this->drawClipShape(gpu, temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000664
665 // TODO: rather than adding these two translations here
666 // compute the bounding box needed to render the texture
667 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000668 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000669 GrMatrix m;
670
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000671 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
672 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000673
674 drawState->preConcatViewMatrix(m);
675 }
676
677 // Now draw into the accumulator using the real operation
678 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000679 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000680 this->drawTexture(gpu, accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000681
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000682 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000683 GrMatrix m;
684
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000685 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
686 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000687
688 drawState->preConcatViewMatrix(m);
689 }
690
691 } else {
692 // all the remaining ops can just be directly draw into
693 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000694 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695 this->drawClipShape(gpu, accum, clipIn, c);
696 }
697 }
698
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000699 *result = accum;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000700
robertphillips@google.comf294b772012-04-27 14:29:26 +0000701 return true;
702}
703
704////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000705// Create a 1-bit clip mask in the stencil buffer
706bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
707 const GrClip& clipIn,
708 const GrRect& bounds,
709 ScissoringSettings* scissorSettings) {
710
711 GrAssert(fClipMaskInStencil);
712
713 GrDrawState* drawState = gpu->drawState();
714 GrAssert(drawState->isClipState());
715
716 GrRenderTarget* rt = drawState->getRenderTarget();
717 GrAssert(NULL != rt);
718
719 // TODO: dynamically attach a SB when needed.
720 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
721 if (NULL == stencilBuffer) {
722 return false;
723 }
724
725 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
726
727 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
728
729 // we set the current clip to the bounds so that our recursive
730 // draws are scissored to them. We use the copy of the complex clip
731 // we just stashed on the SB to render from. We set it back after
732 // we finish drawing it into the stencil.
733 const GrClip& clipCopy = stencilBuffer->getLastClip();
734 gpu->setClip(GrClip(bounds));
735
736 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
737 drawState = gpu->drawState();
738 drawState->setRenderTarget(rt);
739 GrDrawTarget::AutoGeometryPush agp(gpu);
740
741 gpu->disableScissor();
742#if !VISUALIZE_COMPLEX_CLIP
743 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
744#endif
745
746 int count = clipCopy.getElementCount();
747 int clipBit = stencilBuffer->bits();
748 SkASSERT((clipBit <= 16) &&
749 "Ganesh only handles 16b or smaller stencil buffers");
750 clipBit = (1 << (clipBit-1));
751
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000752 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753
754 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000755 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756 int start = process_initial_clip_elements(clipCopy,
757 rtRect,
758 &clearToInside,
759 &startOp);
760
761 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
762
763 // walk through each clip element and perform its set op
764 // with the existing clip.
765 for (int c = start; c < count; ++c) {
766 GrPathFill fill;
767 bool fillInverted;
768 // enabled at bottom of loop
769 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
770
771 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000772 // directly to the stencil buffer
773 // with a non-inverted fill rule
774 // without extra passes to
775 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776
robertphillips@google.comf294b772012-04-27 14:29:26 +0000777 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
778
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000780 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 if (kRect_ClipType == clipCopy.getElementType(c)) {
782 canRenderDirectToStencil = true;
783 fill = kEvenOdd_PathFill;
784 fillInverted = false;
785 // there is no point in intersecting a screen filling
786 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000787 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000788 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000789 continue;
790 }
791 } else {
792 fill = clipCopy.getPathFill(c);
793 fillInverted = GrIsFillInverted(fill);
794 fill = GrNonInvertedFill(fill);
795 clipPath = &clipCopy.getPath(c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000796 pr = this->getClipPathRenderer(gpu, *clipPath, fill, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 if (NULL == pr) {
798 fClipMaskInStencil = false;
799 gpu->setClip(clipCopy); // restore to the original
800 return false;
801 }
802 canRenderDirectToStencil =
803 !pr->requiresStencilPass(*clipPath, fill, gpu);
804 }
805
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 int passes;
807 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
808
809 bool canDrawDirectToClip; // Given the renderer, the element,
810 // fill rule, and set operation can
811 // we render the element directly to
812 // stencil bit used for clipping.
813 canDrawDirectToClip =
814 GrStencilSettings::GetClipPasses(op,
815 canRenderDirectToStencil,
816 clipBit,
817 fillInverted,
818 &passes, stencilSettings);
819
820 // draw the element to the client stencil bits if necessary
821 if (!canDrawDirectToClip) {
822 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
823 kIncClamp_StencilOp,
824 kIncClamp_StencilOp,
825 kAlways_StencilFunc,
826 0xffff,
827 0x0000,
828 0xffff);
829 SET_RANDOM_COLOR
830 if (kRect_ClipType == clipCopy.getElementType(c)) {
831 *drawState->stencil() = gDrawToStencil;
832 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
833 } else {
834 if (canRenderDirectToStencil) {
835 *drawState->stencil() = gDrawToStencil;
836 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
837 } else {
838 pr->drawPathToStencil(*clipPath, fill, gpu);
839 }
840 }
841 }
842
843 // now we modify the clip bit by rendering either the clip
844 // element directly or a bounding rect of the entire clip.
845 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
846 for (int p = 0; p < passes; ++p) {
847 *drawState->stencil() = stencilSettings[p];
848 if (canDrawDirectToClip) {
849 if (kRect_ClipType == clipCopy.getElementType(c)) {
850 SET_RANDOM_COLOR
851 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
852 } else {
853 SET_RANDOM_COLOR
854 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
855 }
856 } else {
857 SET_RANDOM_COLOR
858 gpu->drawSimpleRect(bounds, NULL, 0);
859 }
860 }
861 }
862 // restore clip
863 gpu->setClip(clipCopy);
864 // recusive draws would have disabled this since they drew with
865 // the clip bounds as clip.
866 fClipMaskInStencil = true;
867 }
868
869 return true;
870}
871
robertphillips@google.comfa662942012-05-17 12:20:22 +0000872namespace {
873
874GrPathFill invert_fill(GrPathFill fill) {
875 static const GrPathFill gInvertedFillTable[] = {
876 kInverseWinding_PathFill, // kWinding_PathFill
877 kInverseEvenOdd_PathFill, // kEvenOdd_PathFill
878 kWinding_PathFill, // kInverseWinding_PathFill
879 kEvenOdd_PathFill, // kInverseEvenOdd_PathFill
880 kHairLine_PathFill, // kHairLine_PathFill
881 };
882 GR_STATIC_ASSERT(0 == kWinding_PathFill);
883 GR_STATIC_ASSERT(1 == kEvenOdd_PathFill);
884 GR_STATIC_ASSERT(2 == kInverseWinding_PathFill);
885 GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill);
886 GR_STATIC_ASSERT(4 == kHairLine_PathFill);
887 GR_STATIC_ASSERT(5 == kPathFillCount);
888 return gInvertedFillTable[fill];
889}
890
891}
892
robertphillips@google.comf294b772012-04-27 14:29:26 +0000893////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000894bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu,
895 const GrClip& clipIn,
896 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000897 GrIRect *resultBounds) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000898
robertphillips@google.comf105b102012-05-14 12:18:26 +0000899 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000900 return true;
901 }
902
robertphillips@google.comf105b102012-05-14 12:18:26 +0000903 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000904 if (NULL == accum) {
905 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000906 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000907 return false;
908 }
909
robertphillips@google.comfa662942012-05-17 12:20:22 +0000910 GrSWMaskHelper helper(fAACache.getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000911
robertphillips@google.comfa662942012-05-17 12:20:22 +0000912 helper.init(*resultBounds, NULL, false);
913
914 int count = clipIn.getElementCount();
915
916 bool clearToInside;
917 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
918 int start = process_initial_clip_elements(clipIn,
919 *resultBounds,
920 &clearToInside,
921 &startOp);
922
923 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
924
925 for (int i = start; i < count; ++i) {
926
927 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
928
929 if (SkRegion::kIntersect_Op == op ||
930 SkRegion::kReverseDifference_Op == op) {
931 // Intersect and reverse difference require modifying pixels
932 // outside of the geometry that is being "drawn". In both cases
933 // we erase all the pixels outside of the geometry but
934 // leave the pixels inside the geometry alone. For reverse
935 // difference we invert all the pixels before clearing the ones
936 // outside the geometry.
937 if (SkRegion::kReverseDifference_Op == op) {
938 SkRect temp = SkRect::MakeLTRB(
939 SkIntToScalar(resultBounds->left()),
940 SkIntToScalar(resultBounds->top()),
941 SkIntToScalar(resultBounds->right()),
942 SkIntToScalar(resultBounds->bottom()));
943
944 // invert the entire scene
945 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
946 }
947
948 if (kRect_ClipType == clipIn.getElementType(i)) {
949
950 // convert the rect to a path so we can invert the fill
951 SkPath temp;
952 temp.addRect(clipIn.getRect(i));
953
954 helper.draw(temp, SkRegion::kReplace_Op,
955 kInverseEvenOdd_PathFill, clipIn.getDoAA(i),
956 0x00000000);
957 } else {
958 GrAssert(kPath_ClipType == clipIn.getElementType(i));
959
960 helper.draw(clipIn.getPath(i),
961 SkRegion::kReplace_Op,
962 invert_fill(clipIn.getPathFill(i)),
963 clipIn.getDoAA(i),
964 0x00000000);
965 }
966
967 continue;
968 }
969
970 // The other ops (union, xor, diff) only affect pixels inside
971 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000972 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000973
974 helper.draw(clipIn.getRect(i),
975 op,
976 clipIn.getDoAA(i), SK_ColorWHITE);
977
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000978 } else {
979 GrAssert(kPath_ClipType == clipIn.getElementType(i));
980
robertphillips@google.comfa662942012-05-17 12:20:22 +0000981 helper.draw(clipIn.getPath(i),
982 op,
983 clipIn.getPathFill(i),
984 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000985 }
986 }
987
robertphillips@google.comfa662942012-05-17 12:20:22 +0000988 // Because we are using the scratch texture cache, "accum" may be
989 // larger than expected and have some cruft in the areas we aren't using.
990 // Clear it out.
991
992 // TODO: need a simpler way to clear the texture - can we combine
993 // the clear and the writePixels (inside toTexture)
994 GrDrawState* drawState = gpu->drawState();
995 GrAssert(NULL != drawState);
996 GrRenderTarget* temp = drawState->getRenderTarget();
997 clear(gpu, accum, 0x00000000);
998 // can't leave the accum bound as a rendertarget
999 drawState->setRenderTarget(temp);
1000
1001 helper.toTexture(accum);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001002
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001003 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001004
1005 return true;
1006}
1007
1008
1009////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001010GrPathRenderer* GrClipMaskManager::getClipPathRenderer(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001011 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +00001012 GrPathFill fill,
1013 bool antiAlias) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001014 if (NULL == fPathRendererChain) {
1015 fPathRendererChain =
1016 new GrPathRendererChain(gpu->getContext(),
robertphillips@google.comf294b772012-04-27 14:29:26 +00001017 GrPathRendererChain::kNone_UsageFlag);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001018 }
robertphillips@google.comf294b772012-04-27 14:29:26 +00001019 return fPathRendererChain->getPathRenderer(path, fill, gpu, antiAlias);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001020}
1021
robertphillips@google.comf294b772012-04-27 14:29:26 +00001022////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001023void GrClipMaskManager::releaseResources() {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001024 // in case path renderer has any GrResources, start from scratch
1025 GrSafeSetNull(fPathRendererChain);
robertphillips@google.comf105b102012-05-14 12:18:26 +00001026 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001027}