blob: c1a5bd1cc796b42a1292f2a4e98491ef8d08ec1c [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
robertphillips@google.comf294b772012-04-27 14:29:26 +0000396////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000397bool draw_path(GrContext* context,
398 GrGpu* gpu,
399 const SkPath& path,
400 GrPathFill fill,
401 bool doAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402
robertphillips@google.com72176b22012-05-23 13:19:12 +0000403 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404 if (NULL == pr) {
405 return false;
406 }
407
408 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
409 return true;
410}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000411
412}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413
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
robertphillips@google.com2c756812012-05-22 20:28:23 +0000430 return draw_path(this->getContext(), gpu, temp,
431 kEvenOdd_PathFill, clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000432 } else {
433 gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
434 }
435 } else {
robertphillips@google.com2c756812012-05-22 20:28:23 +0000436 return draw_path(this->getContext(), gpu,
437 clipIn.getPath(index),
438 clipIn.getPathFill(index),
439 clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000440 }
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.com2c756812012-05-22 20:28:23 +0000501 temp->set(this->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.com2c756812012-05-22 20:28:23 +0000796 pr = this->getContext()->getPathRenderer(*clipPath,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000797 fill, gpu, false,
798 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 if (NULL == pr) {
800 fClipMaskInStencil = false;
801 gpu->setClip(clipCopy); // restore to the original
802 return false;
803 }
804 canRenderDirectToStencil =
805 !pr->requiresStencilPass(*clipPath, fill, gpu);
806 }
807
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808 int passes;
809 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
810
811 bool canDrawDirectToClip; // Given the renderer, the element,
812 // fill rule, and set operation can
813 // we render the element directly to
814 // stencil bit used for clipping.
815 canDrawDirectToClip =
816 GrStencilSettings::GetClipPasses(op,
817 canRenderDirectToStencil,
818 clipBit,
819 fillInverted,
820 &passes, stencilSettings);
821
822 // draw the element to the client stencil bits if necessary
823 if (!canDrawDirectToClip) {
824 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
825 kIncClamp_StencilOp,
826 kIncClamp_StencilOp,
827 kAlways_StencilFunc,
828 0xffff,
829 0x0000,
830 0xffff);
831 SET_RANDOM_COLOR
832 if (kRect_ClipType == clipCopy.getElementType(c)) {
833 *drawState->stencil() = gDrawToStencil;
834 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
835 } else {
836 if (canRenderDirectToStencil) {
837 *drawState->stencil() = gDrawToStencil;
838 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
839 } else {
840 pr->drawPathToStencil(*clipPath, fill, gpu);
841 }
842 }
843 }
844
845 // now we modify the clip bit by rendering either the clip
846 // element directly or a bounding rect of the entire clip.
847 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
848 for (int p = 0; p < passes; ++p) {
849 *drawState->stencil() = stencilSettings[p];
850 if (canDrawDirectToClip) {
851 if (kRect_ClipType == clipCopy.getElementType(c)) {
852 SET_RANDOM_COLOR
853 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
854 } else {
855 SET_RANDOM_COLOR
856 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
857 }
858 } else {
859 SET_RANDOM_COLOR
860 gpu->drawSimpleRect(bounds, NULL, 0);
861 }
862 }
863 }
864 // restore clip
865 gpu->setClip(clipCopy);
866 // recusive draws would have disabled this since they drew with
867 // the clip bounds as clip.
868 fClipMaskInStencil = true;
869 }
870
871 return true;
872}
873
robertphillips@google.comfa662942012-05-17 12:20:22 +0000874namespace {
875
876GrPathFill invert_fill(GrPathFill fill) {
877 static const GrPathFill gInvertedFillTable[] = {
878 kInverseWinding_PathFill, // kWinding_PathFill
879 kInverseEvenOdd_PathFill, // kEvenOdd_PathFill
880 kWinding_PathFill, // kInverseWinding_PathFill
881 kEvenOdd_PathFill, // kInverseEvenOdd_PathFill
882 kHairLine_PathFill, // kHairLine_PathFill
883 };
884 GR_STATIC_ASSERT(0 == kWinding_PathFill);
885 GR_STATIC_ASSERT(1 == kEvenOdd_PathFill);
886 GR_STATIC_ASSERT(2 == kInverseWinding_PathFill);
887 GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill);
888 GR_STATIC_ASSERT(4 == kHairLine_PathFill);
889 GR_STATIC_ASSERT(5 == kPathFillCount);
890 return gInvertedFillTable[fill];
891}
892
893}
894
robertphillips@google.comf294b772012-04-27 14:29:26 +0000895////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000896bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu,
897 const GrClip& clipIn,
898 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000899 GrIRect *resultBounds) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000900
robertphillips@google.comf105b102012-05-14 12:18:26 +0000901 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000902 return true;
903 }
904
robertphillips@google.comf105b102012-05-14 12:18:26 +0000905 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000906 if (NULL == accum) {
907 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000908 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000909 return false;
910 }
911
robertphillips@google.com2c756812012-05-22 20:28:23 +0000912 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000913
robertphillips@google.comfa662942012-05-17 12:20:22 +0000914 helper.init(*resultBounds, NULL, false);
915
916 int count = clipIn.getElementCount();
917
918 bool clearToInside;
919 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
920 int start = process_initial_clip_elements(clipIn,
921 *resultBounds,
922 &clearToInside,
923 &startOp);
924
925 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
926
927 for (int i = start; i < count; ++i) {
928
929 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
930
931 if (SkRegion::kIntersect_Op == op ||
932 SkRegion::kReverseDifference_Op == op) {
933 // Intersect and reverse difference require modifying pixels
934 // outside of the geometry that is being "drawn". In both cases
935 // we erase all the pixels outside of the geometry but
936 // leave the pixels inside the geometry alone. For reverse
937 // difference we invert all the pixels before clearing the ones
938 // outside the geometry.
939 if (SkRegion::kReverseDifference_Op == op) {
940 SkRect temp = SkRect::MakeLTRB(
941 SkIntToScalar(resultBounds->left()),
942 SkIntToScalar(resultBounds->top()),
943 SkIntToScalar(resultBounds->right()),
944 SkIntToScalar(resultBounds->bottom()));
945
946 // invert the entire scene
947 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
948 }
949
950 if (kRect_ClipType == clipIn.getElementType(i)) {
951
952 // convert the rect to a path so we can invert the fill
953 SkPath temp;
954 temp.addRect(clipIn.getRect(i));
955
956 helper.draw(temp, SkRegion::kReplace_Op,
957 kInverseEvenOdd_PathFill, clipIn.getDoAA(i),
958 0x00000000);
959 } else {
960 GrAssert(kPath_ClipType == clipIn.getElementType(i));
961
962 helper.draw(clipIn.getPath(i),
963 SkRegion::kReplace_Op,
964 invert_fill(clipIn.getPathFill(i)),
965 clipIn.getDoAA(i),
966 0x00000000);
967 }
968
969 continue;
970 }
971
972 // The other ops (union, xor, diff) only affect pixels inside
973 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000974 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000975
976 helper.draw(clipIn.getRect(i),
977 op,
978 clipIn.getDoAA(i), SK_ColorWHITE);
979
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000980 } else {
981 GrAssert(kPath_ClipType == clipIn.getElementType(i));
982
robertphillips@google.comfa662942012-05-17 12:20:22 +0000983 helper.draw(clipIn.getPath(i),
984 op,
985 clipIn.getPathFill(i),
986 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000987 }
988 }
989
robertphillips@google.comfa662942012-05-17 12:20:22 +0000990 // Because we are using the scratch texture cache, "accum" may be
991 // larger than expected and have some cruft in the areas we aren't using.
992 // Clear it out.
993
994 // TODO: need a simpler way to clear the texture - can we combine
995 // the clear and the writePixels (inside toTexture)
996 GrDrawState* drawState = gpu->drawState();
997 GrAssert(NULL != drawState);
998 GrRenderTarget* temp = drawState->getRenderTarget();
999 clear(gpu, accum, 0x00000000);
1000 // can't leave the accum bound as a rendertarget
1001 drawState->setRenderTarget(temp);
1002
1003 helper.toTexture(accum);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001004
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001005 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001006
1007 return true;
1008}
1009
robertphillips@google.comf294b772012-04-27 14:29:26 +00001010////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001011void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001012 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001013}