blob: d930c06b37bf0b7554053561b67876c7cb2a7701 [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.comf294b772012-04-27 14:29:26 +0000139////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000140// sort out what kind of clip mask needs to be created: alpha, stencil,
141// scissor, or entirely software
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000142bool GrClipMaskManager::createClipMask(GrGpu* gpu,
143 const GrClip& clipIn,
144 ScissoringSettings* scissorSettings) {
145
146 GrAssert(scissorSettings);
147
148 scissorSettings->fEnableScissoring = false;
149 fClipMaskInStencil = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000150 fClipMaskInAlpha = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000151
152 GrDrawState* drawState = gpu->drawState();
153 if (!drawState->isClipState()) {
154 return true;
155 }
156
157 GrRenderTarget* rt = drawState->getRenderTarget();
158
159 // GrDrawTarget should have filtered this for us
160 GrAssert(NULL != rt);
161
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000162#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000163 // If MSAA is enabled we can do everything in the stencil buffer.
164 // Otherwise check if we should just create the entire clip mask
165 // in software (this will only happen if the clip mask is anti-aliased
166 // and too complex for the gpu to handle in its entirety)
167 if (0 == rt->numSamples() && useSWOnlyPath(gpu, clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000168 // The clip geometry is complex enough that it will be more
169 // efficient to create it entirely in software
170 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000171 GrIRect bound;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000172 if (this->createSoftwareClipMask(gpu, clipIn, &result, &bound)) {
173 fClipMaskInAlpha = true;
174
175 setup_drawstate_aaclip(gpu, result, bound);
176 return true;
177 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000178
179 // if SW clip mask creation fails fall through to the other
180 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000181 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000182#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000183
robertphillips@google.comf294b772012-04-27 14:29:26 +0000184#if GR_AA_CLIP
185 // If MSAA is enabled use the (faster) stencil path for AA clipping
186 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000187 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000188 // Since we are going to create a destination texture of the correct
189 // size for the mask (rather than being bound by the size of the
190 // render target) we aren't going to use scissoring like the stencil
191 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000192 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000193 GrIRect bound;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000194 if (this->createAlphaClipMask(gpu, clipIn, &result, &bound)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000195 fClipMaskInAlpha = true;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000196
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000197 setup_drawstate_aaclip(gpu, result, bound);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000198 return true;
199 }
200
201 // if alpha clip mask creation fails fall through to the stencil
202 // buffer method
203 }
204#endif // GR_AA_CLIP
205
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000206 // Either a hard (stencil buffer) clip was explicitly requested or
207 // an antialiased clip couldn't be created. In either case, free up
208 // the texture in the antialiased mask cache.
209 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000210 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
211 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000212 // AA cache.
213 fAACache.reset();
214
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000215 GrRect bounds;
216 GrRect rtRect;
217 rtRect.setLTRB(0, 0,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000218 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000219 if (clipIn.hasConservativeBounds()) {
220 bounds = clipIn.getConservativeBounds();
221 if (!bounds.intersect(rtRect)) {
222 bounds.setEmpty();
223 }
224 } else {
225 bounds = rtRect;
226 }
227
228 bounds.roundOut(&scissorSettings->fScissorRect);
229 if (scissorSettings->fScissorRect.isEmpty()) {
230 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
231 // TODO: I think we can do an early exit here - after refactoring try:
232 // set fEnableScissoring to true but leave fClipMaskInStencil false
233 // and return - everything is going to be scissored away anyway!
234 }
235 scissorSettings->fEnableScissoring = true;
236
237 // use the stencil clip if we can't represent the clip as a rectangle.
238 fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
239 !bounds.isEmpty();
240
241 if (fClipMaskInStencil) {
242 return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings);
243 }
244
245 return true;
246}
247
248#define VISUALIZE_COMPLEX_CLIP 0
249
250#if VISUALIZE_COMPLEX_CLIP
251 #include "GrRandom.h"
252 GrRandom gRandom;
253 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
254#else
255 #define SET_RANDOM_COLOR
256#endif
257
258namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000259/**
260 * Does "container" contain "containee"? If either is empty then
261 * no containment is possible.
262 */
263bool contains(const SkRect& container, const SkIRect& containee) {
264 return !containee.isEmpty() && !container.isEmpty() &&
265 container.fLeft <= SkIntToScalar(containee.fLeft) &&
266 container.fTop <= SkIntToScalar(containee.fTop) &&
267 container.fRight >= SkIntToScalar(containee.fRight) &&
268 container.fBottom >= SkIntToScalar(containee.fBottom);
269}
270
271
robertphillips@google.comf294b772012-04-27 14:29:26 +0000272////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000273// determines how many elements at the head of the clip can be skipped and
274// whether the initial clear should be to the inside- or outside-the-clip value,
275// and what op should be used to draw the first element that isn't skipped.
276int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000277 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000278 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000279 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280
281 // logically before the first element of the clip stack is
282 // processed the clip is entirely open. However, depending on the
283 // first set op we may prefer to clear to 0 for performance. We may
284 // also be able to skip the initial clip paths/rects. We loop until
285 // we cannot skip an element.
286 int curr;
287 bool done = false;
288 *clearToInside = true;
289 int count = clip.getElementCount();
290
291 for (curr = 0; curr < count && !done; ++curr) {
292 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000293 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000294 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000295 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000296 *clearToInside = false;
297 done = true;
298 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000299 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 // if this element contains the entire bounds then we
301 // can skip it.
302 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000303 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000304 break;
305 }
306 // if everything is initially clearToInside then intersect is
307 // same as clear to 0 and treat as a replace. Otherwise,
308 // set stays empty.
309 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000310 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000311 *clearToInside = false;
312 done = true;
313 }
314 break;
315 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000316 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000317 // if everything is initially outside then union is
318 // same as replace. Otherwise, every pixel is still
319 // clearToInside
320 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000321 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000322 done = true;
323 }
324 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000325 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000326 // xor is same as difference or replace both of which
327 // can be 1-pass instead of 2 for xor.
328 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000329 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000330 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000331 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000332 }
333 done = true;
334 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000335 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000336 // if all pixels are clearToInside then we have to process the
337 // difference, otherwise it has no effect and all pixels
338 // remain outside.
339 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000340 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000341 done = true;
342 }
343 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000344 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 // if all pixels are clearToInside then reverse difference
346 // produces empty set. Otherise it is same as replace
347 if (*clearToInside) {
348 *clearToInside = false;
349 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000350 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000351 done = true;
352 }
353 break;
354 default:
355 GrCrash("Unknown set op.");
356 }
357 }
358 return done ? curr-1 : count;
359}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000361}
362
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363
364namespace {
365
366////////////////////////////////////////////////////////////////////////////////
367// set up the OpenGL blend function to perform the specified
368// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000369void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370
371 switch (op) {
372 case SkRegion::kReplace_Op:
373 drawState->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff);
374 break;
375 case SkRegion::kIntersect_Op:
376 drawState->setBlendFunc(kDC_BlendCoeff, kZero_BlendCoeff);
377 break;
378 case SkRegion::kUnion_Op:
379 drawState->setBlendFunc(kOne_BlendCoeff, kISC_BlendCoeff);
380 break;
381 case SkRegion::kXOR_Op:
382 drawState->setBlendFunc(kIDC_BlendCoeff, kISC_BlendCoeff);
383 break;
384 case SkRegion::kDifference_Op:
385 drawState->setBlendFunc(kZero_BlendCoeff, kISC_BlendCoeff);
386 break;
387 case SkRegion::kReverseDifference_Op:
388 drawState->setBlendFunc(kIDC_BlendCoeff, kZero_BlendCoeff);
389 break;
390 default:
391 GrAssert(false);
392 break;
393 }
394}
395
396}
397
robertphillips@google.com2c756812012-05-22 20:28:23 +0000398namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000400bool draw_path(GrContext* context,
401 GrGpu* gpu,
402 const SkPath& path,
403 GrPathFill fill,
404 bool doAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000405
robertphillips@google.com2c756812012-05-22 20:28:23 +0000406 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000407 if (NULL == pr) {
408 return false;
409 }
410
411 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
412 return true;
413}
robertphillips@google.com2c756812012-05-22 20:28:23 +0000414};
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415
416////////////////////////////////////////////////////////////////////////////////
417bool GrClipMaskManager::drawClipShape(GrGpu* gpu,
418 GrTexture* target,
419 const GrClip& clipIn,
420 int index) {
421 GrDrawState* drawState = gpu->drawState();
422 GrAssert(NULL != drawState);
423
424 drawState->setRenderTarget(target->asRenderTarget());
425
426 if (kRect_ClipType == clipIn.getElementType(index)) {
427 if (clipIn.getDoAA(index)) {
428 // convert the rect to a path for AA
429 SkPath temp;
430 temp.addRect(clipIn.getRect(index));
431
robertphillips@google.com2c756812012-05-22 20:28:23 +0000432 return draw_path(this->getContext(), gpu, temp,
433 kEvenOdd_PathFill, clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000434 } else {
435 gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
436 }
437 } else {
robertphillips@google.com2c756812012-05-22 20:28:23 +0000438 return draw_path(this->getContext(), gpu,
439 clipIn.getPath(index),
440 clipIn.getPathFill(index),
441 clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000442 }
443 return true;
444}
445
446void GrClipMaskManager::drawTexture(GrGpu* gpu,
447 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000448 GrTexture* texture) {
449 GrDrawState* drawState = gpu->drawState();
450 GrAssert(NULL != drawState);
451
452 // no AA here since it is encoded in the texture
453 drawState->setRenderTarget(target->asRenderTarget());
454
455 GrMatrix sampleM;
456 sampleM.setIDiv(texture->width(), texture->height());
457 drawState->setTexture(0, texture);
458
459 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
460 GrSamplerState::kNearest_Filter,
461 sampleM);
462
robertphillips@google.comf105b102012-05-14 12:18:26 +0000463 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
464 SkIntToScalar(target->height()));
465
robertphillips@google.comf294b772012-04-27 14:29:26 +0000466 gpu->drawSimpleRect(rect, NULL, 1 << 0);
467
468 drawState->setTexture(0, NULL);
469}
470
471namespace {
472
473void clear(GrGpu* gpu,
474 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000475 GrColor color) {
476 GrDrawState* drawState = gpu->drawState();
477 GrAssert(NULL != drawState);
478
479 // zap entire target to specified color
480 drawState->setRenderTarget(target->asRenderTarget());
481 gpu->clear(NULL, color);
482}
483
robertphillips@google.comf105b102012-05-14 12:18:26 +0000484}
485
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000486// get a texture to act as a temporary buffer for AA clip boolean operations
487// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000488void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000489 GrAutoScratchTexture* temp) {
490 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000491 // we've already allocated the temp texture
492 return;
493 }
494
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000495 const GrTextureDesc desc = {
496 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000497 bounds.width(),
498 bounds.height(),
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000499 kAlpha_8_GrPixelConfig,
500 0 // samples
501 };
502
robertphillips@google.com2c756812012-05-22 20:28:23 +0000503 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000504}
505
robertphillips@google.comf105b102012-05-14 12:18:26 +0000506
507void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000508 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000509 // Since we are setting up the cache we know the last lookup was a miss
510 // Free up the currently cached mask so it can be reused
511 fAACache.reset();
512
513 const GrTextureDesc desc = {
514 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000515 bounds.width(),
516 bounds.height(),
robertphillips@google.comf105b102012-05-14 12:18:26 +0000517 kAlpha_8_GrPixelConfig,
518 0 // samples
519 };
520
521 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000522}
523
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000524////////////////////////////////////////////////////////////////////////////////
525// Shared preamble between gpu and SW-only AA clip mask creation paths.
526// Handles caching, determination of clip mask bound & allocation (if needed)
527// of the result texture
528// Returns true if there is no more work to be done (i.e., we got a cache hit)
529bool GrClipMaskManager::clipMaskPreamble(GrGpu* gpu,
530 const GrClip& clipIn,
531 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000532 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000533 GrDrawState* origDrawState = gpu->drawState();
534 GrAssert(origDrawState->isClipState());
535
536 GrRenderTarget* rt = origDrawState->getRenderTarget();
537 GrAssert(NULL != rt);
538
539 GrRect rtRect;
540 rtRect.setLTRB(0, 0,
541 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
542
543 // unlike the stencil path the alpha path is not bound to the size of the
544 // render target - determine the minimum size required for the mask
545 GrRect bounds;
546
547 if (clipIn.hasConservativeBounds()) {
548 bounds = clipIn.getConservativeBounds();
549 if (!bounds.intersect(rtRect)) {
550 // the mask will be empty in this case
551 GrAssert(false);
552 bounds.setEmpty();
553 }
554 } else {
555 // still locked to the size of the render target
556 bounds = rtRect;
557 }
558
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000559 GrIRect intBounds;
560 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561
562 // need to outset a pixel since the standard bounding box computation
563 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000564 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000566 // TODO: make sure we don't outset if bounds are still 0,0 @ min
567
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000568 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000569 intBounds.width(),
570 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000571 *result = fAACache.getLastMask();
572 fAACache.getLastBound(resultBounds);
573 return true;
574 }
575
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000576 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000577
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000578 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000579 return false;
580}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000581
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000582////////////////////////////////////////////////////////////////////////////////
583// Create a 8-bit clip mask in alpha
584bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu,
585 const GrClip& clipIn,
586 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000587 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000588
robertphillips@google.comf105b102012-05-14 12:18:26 +0000589 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000590 return true;
591 }
592
robertphillips@google.comf105b102012-05-14 12:18:26 +0000593 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000594 if (NULL == accum) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000595 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000596 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597 return false;
598 }
599
600 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
601 GrDrawState* drawState = gpu->drawState();
602
603 GrDrawTarget::AutoGeometryPush agp(gpu);
604
605 int count = clipIn.getElementCount();
606
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000607 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608 // if we were able to trim down the size of the mask we need to
609 // offset the paths & rects that will be used to compute it
610 GrMatrix m;
611
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000612 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
613 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000614
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000615 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000616 }
617
618 bool clearToInside;
619 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
620 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000621 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000622 &clearToInside,
623 &startOp);
624
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000625 clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626
robertphillips@google.comf105b102012-05-14 12:18:26 +0000627 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000628
robertphillips@google.comf294b772012-04-27 14:29:26 +0000629 // walk through each clip element and perform its set op
630 for (int c = start; c < count; ++c) {
631
632 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
633
634 if (SkRegion::kReplace_Op == op) {
635 // TODO: replace is actually a lot faster then intersection
636 // for this path - refactor the stencil path so it can handle
637 // replace ops and alter GrClip to allow them through
638
639 // clear the accumulator and draw the new object directly into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000640 clear(gpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000641
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000642 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000643 this->drawClipShape(gpu, accum, clipIn, c);
644
645 } else if (SkRegion::kReverseDifference_Op == op ||
646 SkRegion::kIntersect_Op == op) {
647 // there is no point in intersecting a screen filling rectangle.
648 if (SkRegion::kIntersect_Op == op &&
649 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000650 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651 continue;
652 }
653
robertphillips@google.comf105b102012-05-14 12:18:26 +0000654 getTemp(*resultBounds, &temp);
655 if (NULL == temp.texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000656 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000657 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000658 return false;
659 }
660
robertphillips@google.comf294b772012-04-27 14:29:26 +0000661 // clear the temp target & draw into it
robertphillips@google.comf105b102012-05-14 12:18:26 +0000662 clear(gpu, temp.texture(), 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000663
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000664 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000665 this->drawClipShape(gpu, temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000666
667 // TODO: rather than adding these two translations here
668 // compute the bounding box needed to render the texture
669 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000670 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000671 GrMatrix m;
672
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000673 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
674 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000675
676 drawState->preConcatViewMatrix(m);
677 }
678
679 // Now draw into the accumulator using the real operation
680 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000681 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000682 this->drawTexture(gpu, accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000683
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000684 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000685 GrMatrix m;
686
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000687 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
688 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689
690 drawState->preConcatViewMatrix(m);
691 }
692
693 } else {
694 // all the remaining ops can just be directly draw into
695 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000696 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000697 this->drawClipShape(gpu, accum, clipIn, c);
698 }
699 }
700
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000701 *result = accum;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000702
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703 return true;
704}
705
706////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707// Create a 1-bit clip mask in the stencil buffer
708bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
709 const GrClip& clipIn,
710 const GrRect& bounds,
711 ScissoringSettings* scissorSettings) {
712
713 GrAssert(fClipMaskInStencil);
714
715 GrDrawState* drawState = gpu->drawState();
716 GrAssert(drawState->isClipState());
717
718 GrRenderTarget* rt = drawState->getRenderTarget();
719 GrAssert(NULL != rt);
720
721 // TODO: dynamically attach a SB when needed.
722 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
723 if (NULL == stencilBuffer) {
724 return false;
725 }
726
727 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
728
729 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
730
731 // we set the current clip to the bounds so that our recursive
732 // draws are scissored to them. We use the copy of the complex clip
733 // we just stashed on the SB to render from. We set it back after
734 // we finish drawing it into the stencil.
735 const GrClip& clipCopy = stencilBuffer->getLastClip();
736 gpu->setClip(GrClip(bounds));
737
738 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
739 drawState = gpu->drawState();
740 drawState->setRenderTarget(rt);
741 GrDrawTarget::AutoGeometryPush agp(gpu);
742
743 gpu->disableScissor();
744#if !VISUALIZE_COMPLEX_CLIP
745 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
746#endif
747
748 int count = clipCopy.getElementCount();
749 int clipBit = stencilBuffer->bits();
750 SkASSERT((clipBit <= 16) &&
751 "Ganesh only handles 16b or smaller stencil buffers");
752 clipBit = (1 << (clipBit-1));
753
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000754 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755
756 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000757 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 int start = process_initial_clip_elements(clipCopy,
759 rtRect,
760 &clearToInside,
761 &startOp);
762
763 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
764
765 // walk through each clip element and perform its set op
766 // with the existing clip.
767 for (int c = start; c < count; ++c) {
768 GrPathFill fill;
769 bool fillInverted;
770 // enabled at bottom of loop
771 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
772
773 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000774 // directly to the stencil buffer
775 // with a non-inverted fill rule
776 // without extra passes to
777 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778
robertphillips@google.comf294b772012-04-27 14:29:26 +0000779 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
780
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000782 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000783 if (kRect_ClipType == clipCopy.getElementType(c)) {
784 canRenderDirectToStencil = true;
785 fill = kEvenOdd_PathFill;
786 fillInverted = false;
787 // there is no point in intersecting a screen filling
788 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000789 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000790 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791 continue;
792 }
793 } else {
794 fill = clipCopy.getPathFill(c);
795 fillInverted = GrIsFillInverted(fill);
796 fill = GrNonInvertedFill(fill);
797 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000798 pr = this->getContext()->getPathRenderer(*clipPath,
799 fill, gpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 if (NULL == pr) {
801 fClipMaskInStencil = false;
802 gpu->setClip(clipCopy); // restore to the original
803 return false;
804 }
805 canRenderDirectToStencil =
806 !pr->requiresStencilPass(*clipPath, fill, gpu);
807 }
808
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 int passes;
810 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
811
812 bool canDrawDirectToClip; // Given the renderer, the element,
813 // fill rule, and set operation can
814 // we render the element directly to
815 // stencil bit used for clipping.
816 canDrawDirectToClip =
817 GrStencilSettings::GetClipPasses(op,
818 canRenderDirectToStencil,
819 clipBit,
820 fillInverted,
821 &passes, stencilSettings);
822
823 // draw the element to the client stencil bits if necessary
824 if (!canDrawDirectToClip) {
825 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
826 kIncClamp_StencilOp,
827 kIncClamp_StencilOp,
828 kAlways_StencilFunc,
829 0xffff,
830 0x0000,
831 0xffff);
832 SET_RANDOM_COLOR
833 if (kRect_ClipType == clipCopy.getElementType(c)) {
834 *drawState->stencil() = gDrawToStencil;
835 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
836 } else {
837 if (canRenderDirectToStencil) {
838 *drawState->stencil() = gDrawToStencil;
839 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
840 } else {
841 pr->drawPathToStencil(*clipPath, fill, gpu);
842 }
843 }
844 }
845
846 // now we modify the clip bit by rendering either the clip
847 // element directly or a bounding rect of the entire clip.
848 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
849 for (int p = 0; p < passes; ++p) {
850 *drawState->stencil() = stencilSettings[p];
851 if (canDrawDirectToClip) {
852 if (kRect_ClipType == clipCopy.getElementType(c)) {
853 SET_RANDOM_COLOR
854 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
855 } else {
856 SET_RANDOM_COLOR
857 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
858 }
859 } else {
860 SET_RANDOM_COLOR
861 gpu->drawSimpleRect(bounds, NULL, 0);
862 }
863 }
864 }
865 // restore clip
866 gpu->setClip(clipCopy);
867 // recusive draws would have disabled this since they drew with
868 // the clip bounds as clip.
869 fClipMaskInStencil = true;
870 }
871
872 return true;
873}
874
robertphillips@google.comfa662942012-05-17 12:20:22 +0000875namespace {
876
877GrPathFill invert_fill(GrPathFill fill) {
878 static const GrPathFill gInvertedFillTable[] = {
879 kInverseWinding_PathFill, // kWinding_PathFill
880 kInverseEvenOdd_PathFill, // kEvenOdd_PathFill
881 kWinding_PathFill, // kInverseWinding_PathFill
882 kEvenOdd_PathFill, // kInverseEvenOdd_PathFill
883 kHairLine_PathFill, // kHairLine_PathFill
884 };
885 GR_STATIC_ASSERT(0 == kWinding_PathFill);
886 GR_STATIC_ASSERT(1 == kEvenOdd_PathFill);
887 GR_STATIC_ASSERT(2 == kInverseWinding_PathFill);
888 GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill);
889 GR_STATIC_ASSERT(4 == kHairLine_PathFill);
890 GR_STATIC_ASSERT(5 == kPathFillCount);
891 return gInvertedFillTable[fill];
892}
893
894}
895
robertphillips@google.comf294b772012-04-27 14:29:26 +0000896////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000897bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu,
898 const GrClip& clipIn,
899 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000900 GrIRect *resultBounds) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000901
robertphillips@google.comf105b102012-05-14 12:18:26 +0000902 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000903 return true;
904 }
905
robertphillips@google.comf105b102012-05-14 12:18:26 +0000906 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000907 if (NULL == accum) {
908 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000909 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000910 return false;
911 }
912
robertphillips@google.com2c756812012-05-22 20:28:23 +0000913 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000914
robertphillips@google.comfa662942012-05-17 12:20:22 +0000915 helper.init(*resultBounds, NULL, false);
916
917 int count = clipIn.getElementCount();
918
919 bool clearToInside;
920 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
921 int start = process_initial_clip_elements(clipIn,
922 *resultBounds,
923 &clearToInside,
924 &startOp);
925
926 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
927
928 for (int i = start; i < count; ++i) {
929
930 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
931
932 if (SkRegion::kIntersect_Op == op ||
933 SkRegion::kReverseDifference_Op == op) {
934 // Intersect and reverse difference require modifying pixels
935 // outside of the geometry that is being "drawn". In both cases
936 // we erase all the pixels outside of the geometry but
937 // leave the pixels inside the geometry alone. For reverse
938 // difference we invert all the pixels before clearing the ones
939 // outside the geometry.
940 if (SkRegion::kReverseDifference_Op == op) {
941 SkRect temp = SkRect::MakeLTRB(
942 SkIntToScalar(resultBounds->left()),
943 SkIntToScalar(resultBounds->top()),
944 SkIntToScalar(resultBounds->right()),
945 SkIntToScalar(resultBounds->bottom()));
946
947 // invert the entire scene
948 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
949 }
950
951 if (kRect_ClipType == clipIn.getElementType(i)) {
952
953 // convert the rect to a path so we can invert the fill
954 SkPath temp;
955 temp.addRect(clipIn.getRect(i));
956
957 helper.draw(temp, SkRegion::kReplace_Op,
958 kInverseEvenOdd_PathFill, clipIn.getDoAA(i),
959 0x00000000);
960 } else {
961 GrAssert(kPath_ClipType == clipIn.getElementType(i));
962
963 helper.draw(clipIn.getPath(i),
964 SkRegion::kReplace_Op,
965 invert_fill(clipIn.getPathFill(i)),
966 clipIn.getDoAA(i),
967 0x00000000);
968 }
969
970 continue;
971 }
972
973 // The other ops (union, xor, diff) only affect pixels inside
974 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000975 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000976
977 helper.draw(clipIn.getRect(i),
978 op,
979 clipIn.getDoAA(i), SK_ColorWHITE);
980
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000981 } else {
982 GrAssert(kPath_ClipType == clipIn.getElementType(i));
983
robertphillips@google.comfa662942012-05-17 12:20:22 +0000984 helper.draw(clipIn.getPath(i),
985 op,
986 clipIn.getPathFill(i),
987 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000988 }
989 }
990
robertphillips@google.comfa662942012-05-17 12:20:22 +0000991 // Because we are using the scratch texture cache, "accum" may be
992 // larger than expected and have some cruft in the areas we aren't using.
993 // Clear it out.
994
995 // TODO: need a simpler way to clear the texture - can we combine
996 // the clear and the writePixels (inside toTexture)
997 GrDrawState* drawState = gpu->drawState();
998 GrAssert(NULL != drawState);
999 GrRenderTarget* temp = drawState->getRenderTarget();
1000 clear(gpu, accum, 0x00000000);
1001 // can't leave the accum bound as a rendertarget
1002 drawState->setRenderTarget(temp);
1003
1004 helper.toTexture(accum);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001005
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001006 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001007
1008 return true;
1009}
1010
robertphillips@google.comf294b772012-04-27 14:29:26 +00001011////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001012void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001013 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001014}