blob: 6e84724e8f635783d425ad58939e139aec3204a0 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
10#include "GrGpu.h"
11#include "GrRenderTarget.h"
12#include "GrStencilBuffer.h"
13#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000014#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000015#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000016#include "GrAAConvexPathRenderer.h"
17#include "GrAAHairLinePathRenderer.h"
18
19// TODO: move GrSWMaskHelper out of GrSoftwarePathRender.h & remove this include
20#include "GrSoftwarePathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000021
22//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000023//#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000024
robertphillips@google.comf294b772012-04-27 14:29:26 +000025////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +000026void ScissoringSettings::setupScissoring(GrGpu* gpu) {
27 if (!fEnableScissoring) {
28 gpu->disableScissor();
29 return;
30 }
31
32 gpu->enableScissoring(fScissorRect);
33}
34
robertphillips@google.coma72eef32012-05-01 17:22:59 +000035namespace {
36// set up the draw state to enable the aa clipping mask. Besides setting up the
37// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000038void setup_drawstate_aaclip(GrGpu* gpu,
39 GrTexture* result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000040 const GrIRect &bound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041 GrDrawState* drawState = gpu->drawState();
42 GrAssert(drawState);
43
44 static const int maskStage = GrPaint::kTotalStages+1;
45
46 GrMatrix mat;
47 mat.setIDiv(result->width(), result->height());
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000048 mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049 mat.preConcat(drawState->getViewMatrix());
50
51 drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode,
52 GrSamplerState::kNearest_Filter,
53 mat);
54
55 drawState->setTexture(maskStage, result);
56
57 // The AA clipping determination happens long after the geometry has
58 // been set up to draw. Here we directly enable the AA clip mask stage
59 gpu->addToVertexLayout(
60 GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(maskStage));
61}
62
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000063bool path_needs_SW_renderer(GrContext* context,
64 GrGpu* gpu,
65 const SkPath& path,
66 GrPathFill fill,
67 bool doAA) {
68 // last (false) parameter disallows use of the SW path renderer
69 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
70}
71
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000072}
73
robertphillips@google.comfa662942012-05-17 12:20:22 +000074/*
75 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
76 * will be used on any element. If so, it returns true to indicate that the
77 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
78 */
79bool GrClipMaskManager::useSWOnlyPath(GrGpu* gpu, const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000080
81 if (!clipIn.requiresAA()) {
82 // The stencil buffer can handle this case
83 return false;
84 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000085
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000086 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000087 // a clip gets complex enough it can just be done in SW regardless
88 // of whether it would invoke the GrSoftwarePathRenderer.
89 bool useSW = false;
90
91 for (int i = 0; i < clipIn.getElementCount(); ++i) {
92
93 if (SkRegion::kReplace_Op == clipIn.getOp(i)) {
94 // Everything before a replace op can be ignored so start
95 // afresh w.r.t. determining if any element uses the SW path
96 useSW = false;
97 }
98
robertphillips@google.comfa662942012-05-17 12:20:22 +000099 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000100 // Non-anti-aliased rects can always be drawn directly (w/o
101 // using the software path) so the anti-aliased rects are all
102 // that need to be checked here
103 if (clipIn.getDoAA(i)) {
104 // Antialiased rects are converted to paths and then drawn with
105 // kEvenOdd_PathFill.
106
107 // TODO: wrap GrContext::fillAARect in a helper class and
108 // draw AA rects directly rather than converting to paths
109 SkPath temp;
110 temp.addRect(clipIn.getRect(i));
111
112 if (path_needs_SW_renderer(this->getContext(), gpu, temp,
113 kEvenOdd_PathFill, true)) {
114 useSW = true;
115 }
116 }
117 } else {
118 if (path_needs_SW_renderer(this->getContext(), gpu,
119 clipIn.getPath(i),
120 clipIn.getPathFill(i),
121 clipIn.getDoAA(i))) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000122 useSW = true;
123 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000124 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000125 }
126
127 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000128}
129
robertphillips@google.comf294b772012-04-27 14:29:26 +0000130////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000131// sort out what kind of clip mask needs to be created: alpha, stencil,
132// scissor, or entirely software
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000133bool GrClipMaskManager::createClipMask(GrGpu* gpu,
134 const GrClip& clipIn,
135 ScissoringSettings* scissorSettings) {
136
137 GrAssert(scissorSettings);
138
139 scissorSettings->fEnableScissoring = false;
140 fClipMaskInStencil = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000141 fClipMaskInAlpha = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000142
143 GrDrawState* drawState = gpu->drawState();
144 if (!drawState->isClipState()) {
145 return true;
146 }
147
148 GrRenderTarget* rt = drawState->getRenderTarget();
149
150 // GrDrawTarget should have filtered this for us
151 GrAssert(NULL != rt);
152
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000153#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000154 // If MSAA is enabled we can do everything in the stencil buffer.
155 // Otherwise check if we should just create the entire clip mask
156 // in software (this will only happen if the clip mask is anti-aliased
157 // and too complex for the gpu to handle in its entirety)
158 if (0 == rt->numSamples() && useSWOnlyPath(gpu, clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000159 // The clip geometry is complex enough that it will be more
160 // efficient to create it entirely in software
161 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000162 GrIRect bound;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000163 if (this->createSoftwareClipMask(gpu, clipIn, &result, &bound)) {
164 fClipMaskInAlpha = true;
165
166 setup_drawstate_aaclip(gpu, result, bound);
167 return true;
168 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000169
170 // if SW clip mask creation fails fall through to the other
171 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000172 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000173#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000174
robertphillips@google.comf294b772012-04-27 14:29:26 +0000175#if GR_AA_CLIP
176 // If MSAA is enabled use the (faster) stencil path for AA clipping
177 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000178 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000179 // Since we are going to create a destination texture of the correct
180 // size for the mask (rather than being bound by the size of the
181 // render target) we aren't going to use scissoring like the stencil
182 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000183 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000184 GrIRect bound;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000185 if (this->createAlphaClipMask(gpu, clipIn, &result, &bound)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000186 fClipMaskInAlpha = true;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000187
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000188 setup_drawstate_aaclip(gpu, result, bound);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000189 return true;
190 }
191
192 // if alpha clip mask creation fails fall through to the stencil
193 // buffer method
194 }
195#endif // GR_AA_CLIP
196
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000197 // Either a hard (stencil buffer) clip was explicitly requested or
198 // an antialiased clip couldn't be created. In either case, free up
199 // the texture in the antialiased mask cache.
200 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000201 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
202 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000203 // AA cache.
204 fAACache.reset();
205
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000206 GrRect bounds;
207 GrRect rtRect;
208 rtRect.setLTRB(0, 0,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000209 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000210 if (clipIn.hasConservativeBounds()) {
211 bounds = clipIn.getConservativeBounds();
212 if (!bounds.intersect(rtRect)) {
213 bounds.setEmpty();
214 }
215 } else {
216 bounds = rtRect;
217 }
218
219 bounds.roundOut(&scissorSettings->fScissorRect);
220 if (scissorSettings->fScissorRect.isEmpty()) {
221 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
222 // TODO: I think we can do an early exit here - after refactoring try:
223 // set fEnableScissoring to true but leave fClipMaskInStencil false
224 // and return - everything is going to be scissored away anyway!
225 }
226 scissorSettings->fEnableScissoring = true;
227
228 // use the stencil clip if we can't represent the clip as a rectangle.
229 fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
230 !bounds.isEmpty();
231
232 if (fClipMaskInStencil) {
233 return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings);
234 }
235
236 return true;
237}
238
239#define VISUALIZE_COMPLEX_CLIP 0
240
241#if VISUALIZE_COMPLEX_CLIP
242 #include "GrRandom.h"
243 GrRandom gRandom;
244 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
245#else
246 #define SET_RANDOM_COLOR
247#endif
248
249namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000250/**
251 * Does "container" contain "containee"? If either is empty then
252 * no containment is possible.
253 */
254bool contains(const SkRect& container, const SkIRect& containee) {
255 return !containee.isEmpty() && !container.isEmpty() &&
256 container.fLeft <= SkIntToScalar(containee.fLeft) &&
257 container.fTop <= SkIntToScalar(containee.fTop) &&
258 container.fRight >= SkIntToScalar(containee.fRight) &&
259 container.fBottom >= SkIntToScalar(containee.fBottom);
260}
261
262
robertphillips@google.comf294b772012-04-27 14:29:26 +0000263////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000264// determines how many elements at the head of the clip can be skipped and
265// whether the initial clear should be to the inside- or outside-the-clip value,
266// and what op should be used to draw the first element that isn't skipped.
267int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000268 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000269 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000270 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000271
272 // logically before the first element of the clip stack is
273 // processed the clip is entirely open. However, depending on the
274 // first set op we may prefer to clear to 0 for performance. We may
275 // also be able to skip the initial clip paths/rects. We loop until
276 // we cannot skip an element.
277 int curr;
278 bool done = false;
279 *clearToInside = true;
280 int count = clip.getElementCount();
281
282 for (curr = 0; curr < count && !done; ++curr) {
283 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000284 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000285 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000286 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000287 *clearToInside = false;
288 done = true;
289 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000290 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 // if this element contains the entire bounds then we
292 // can skip it.
293 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000294 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 break;
296 }
297 // if everything is initially clearToInside then intersect is
298 // same as clear to 0 and treat as a replace. Otherwise,
299 // set stays empty.
300 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000301 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000302 *clearToInside = false;
303 done = true;
304 }
305 break;
306 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000307 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000308 // if everything is initially outside then union is
309 // same as replace. Otherwise, every pixel is still
310 // clearToInside
311 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000312 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000313 done = true;
314 }
315 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000316 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000317 // xor is same as difference or replace both of which
318 // can be 1-pass instead of 2 for xor.
319 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000320 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000321 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000322 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000323 }
324 done = true;
325 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000326 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000327 // if all pixels are clearToInside then we have to process the
328 // difference, otherwise it has no effect and all pixels
329 // remain outside.
330 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000331 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000332 done = true;
333 }
334 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000335 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000336 // if all pixels are clearToInside then reverse difference
337 // produces empty set. Otherise it is same as replace
338 if (*clearToInside) {
339 *clearToInside = false;
340 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000341 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000342 done = true;
343 }
344 break;
345 default:
346 GrCrash("Unknown set op.");
347 }
348 }
349 return done ? curr-1 : count;
350}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000351
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000352}
353
robertphillips@google.comf294b772012-04-27 14:29:26 +0000354
355namespace {
356
357////////////////////////////////////////////////////////////////////////////////
358// set up the OpenGL blend function to perform the specified
359// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000360void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361
362 switch (op) {
363 case SkRegion::kReplace_Op:
364 drawState->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff);
365 break;
366 case SkRegion::kIntersect_Op:
367 drawState->setBlendFunc(kDC_BlendCoeff, kZero_BlendCoeff);
368 break;
369 case SkRegion::kUnion_Op:
370 drawState->setBlendFunc(kOne_BlendCoeff, kISC_BlendCoeff);
371 break;
372 case SkRegion::kXOR_Op:
373 drawState->setBlendFunc(kIDC_BlendCoeff, kISC_BlendCoeff);
374 break;
375 case SkRegion::kDifference_Op:
376 drawState->setBlendFunc(kZero_BlendCoeff, kISC_BlendCoeff);
377 break;
378 case SkRegion::kReverseDifference_Op:
379 drawState->setBlendFunc(kIDC_BlendCoeff, kZero_BlendCoeff);
380 break;
381 default:
382 GrAssert(false);
383 break;
384 }
385}
386
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000388bool draw_path(GrContext* context,
389 GrGpu* gpu,
390 const SkPath& path,
391 GrPathFill fill,
392 bool doAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000393
robertphillips@google.com72176b22012-05-23 13:19:12 +0000394 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395 if (NULL == pr) {
396 return false;
397 }
398
399 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
400 return true;
401}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000402
403}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404
405////////////////////////////////////////////////////////////////////////////////
406bool GrClipMaskManager::drawClipShape(GrGpu* gpu,
407 GrTexture* target,
408 const GrClip& clipIn,
409 int index) {
410 GrDrawState* drawState = gpu->drawState();
411 GrAssert(NULL != drawState);
412
413 drawState->setRenderTarget(target->asRenderTarget());
414
415 if (kRect_ClipType == clipIn.getElementType(index)) {
416 if (clipIn.getDoAA(index)) {
417 // convert the rect to a path for AA
418 SkPath temp;
419 temp.addRect(clipIn.getRect(index));
420
robertphillips@google.com2c756812012-05-22 20:28:23 +0000421 return draw_path(this->getContext(), gpu, temp,
422 kEvenOdd_PathFill, clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000423 } else {
424 gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
425 }
426 } else {
robertphillips@google.com2c756812012-05-22 20:28:23 +0000427 return draw_path(this->getContext(), gpu,
428 clipIn.getPath(index),
429 clipIn.getPathFill(index),
430 clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431 }
432 return true;
433}
434
435void GrClipMaskManager::drawTexture(GrGpu* gpu,
436 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437 GrTexture* texture) {
438 GrDrawState* drawState = gpu->drawState();
439 GrAssert(NULL != drawState);
440
441 // no AA here since it is encoded in the texture
442 drawState->setRenderTarget(target->asRenderTarget());
443
444 GrMatrix sampleM;
445 sampleM.setIDiv(texture->width(), texture->height());
446 drawState->setTexture(0, texture);
447
448 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
449 GrSamplerState::kNearest_Filter,
450 sampleM);
451
robertphillips@google.comf105b102012-05-14 12:18:26 +0000452 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
453 SkIntToScalar(target->height()));
454
robertphillips@google.comf294b772012-04-27 14:29:26 +0000455 gpu->drawSimpleRect(rect, NULL, 1 << 0);
456
457 drawState->setTexture(0, NULL);
458}
459
460namespace {
461
462void clear(GrGpu* gpu,
463 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464 GrColor color) {
465 GrDrawState* drawState = gpu->drawState();
466 GrAssert(NULL != drawState);
467
468 // zap entire target to specified color
469 drawState->setRenderTarget(target->asRenderTarget());
470 gpu->clear(NULL, color);
471}
472
robertphillips@google.comf105b102012-05-14 12:18:26 +0000473}
474
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000475// get a texture to act as a temporary buffer for AA clip boolean operations
476// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000477void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000478 GrAutoScratchTexture* temp) {
479 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000480 // we've already allocated the temp texture
481 return;
482 }
483
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000484 const GrTextureDesc desc = {
485 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000486 bounds.width(),
487 bounds.height(),
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000488 kAlpha_8_GrPixelConfig,
489 0 // samples
490 };
491
robertphillips@google.com2c756812012-05-22 20:28:23 +0000492 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000493}
494
robertphillips@google.comf105b102012-05-14 12:18:26 +0000495
496void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000497 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000498 // Since we are setting up the cache we know the last lookup was a miss
499 // Free up the currently cached mask so it can be reused
500 fAACache.reset();
501
502 const GrTextureDesc desc = {
503 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000504 bounds.width(),
505 bounds.height(),
robertphillips@google.comf105b102012-05-14 12:18:26 +0000506 kAlpha_8_GrPixelConfig,
507 0 // samples
508 };
509
510 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000511}
512
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000513////////////////////////////////////////////////////////////////////////////////
514// Shared preamble between gpu and SW-only AA clip mask creation paths.
515// Handles caching, determination of clip mask bound & allocation (if needed)
516// of the result texture
517// Returns true if there is no more work to be done (i.e., we got a cache hit)
518bool GrClipMaskManager::clipMaskPreamble(GrGpu* gpu,
519 const GrClip& clipIn,
520 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000521 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000522 GrDrawState* origDrawState = gpu->drawState();
523 GrAssert(origDrawState->isClipState());
524
525 GrRenderTarget* rt = origDrawState->getRenderTarget();
526 GrAssert(NULL != rt);
527
528 GrRect rtRect;
529 rtRect.setLTRB(0, 0,
530 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
531
532 // unlike the stencil path the alpha path is not bound to the size of the
533 // render target - determine the minimum size required for the mask
534 GrRect bounds;
535
536 if (clipIn.hasConservativeBounds()) {
537 bounds = clipIn.getConservativeBounds();
538 if (!bounds.intersect(rtRect)) {
539 // the mask will be empty in this case
540 GrAssert(false);
541 bounds.setEmpty();
542 }
543 } else {
544 // still locked to the size of the render target
545 bounds = rtRect;
546 }
547
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000548 GrIRect intBounds;
549 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000550
551 // need to outset a pixel since the standard bounding box computation
552 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000553 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000554
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000555 // TODO: make sure we don't outset if bounds are still 0,0 @ min
556
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000557 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000558 intBounds.width(),
559 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000560 *result = fAACache.getLastMask();
561 fAACache.getLastBound(resultBounds);
562 return true;
563 }
564
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000565 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000566
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000567 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000568 return false;
569}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000570
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000571////////////////////////////////////////////////////////////////////////////////
572// Create a 8-bit clip mask in alpha
573bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu,
574 const GrClip& clipIn,
575 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000576 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000577
robertphillips@google.comf105b102012-05-14 12:18:26 +0000578 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000579 return true;
580 }
581
robertphillips@google.comf105b102012-05-14 12:18:26 +0000582 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000583 if (NULL == accum) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000584 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000585 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586 return false;
587 }
588
589 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
590 GrDrawState* drawState = gpu->drawState();
591
592 GrDrawTarget::AutoGeometryPush agp(gpu);
593
594 int count = clipIn.getElementCount();
595
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000596 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597 // if we were able to trim down the size of the mask we need to
598 // offset the paths & rects that will be used to compute it
599 GrMatrix m;
600
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000601 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
602 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000603
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000604 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605 }
606
607 bool clearToInside;
608 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
609 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000610 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611 &clearToInside,
612 &startOp);
613
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000614 clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000615
robertphillips@google.comf105b102012-05-14 12:18:26 +0000616 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000617
robertphillips@google.comf294b772012-04-27 14:29:26 +0000618 // walk through each clip element and perform its set op
619 for (int c = start; c < count; ++c) {
620
621 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
622
623 if (SkRegion::kReplace_Op == op) {
624 // TODO: replace is actually a lot faster then intersection
625 // for this path - refactor the stencil path so it can handle
626 // replace ops and alter GrClip to allow them through
627
628 // clear the accumulator and draw the new object directly into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000629 clear(gpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000630
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000631 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632 this->drawClipShape(gpu, accum, clipIn, c);
633
634 } else if (SkRegion::kReverseDifference_Op == op ||
635 SkRegion::kIntersect_Op == op) {
636 // there is no point in intersecting a screen filling rectangle.
637 if (SkRegion::kIntersect_Op == op &&
638 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000639 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640 continue;
641 }
642
robertphillips@google.comf105b102012-05-14 12:18:26 +0000643 getTemp(*resultBounds, &temp);
644 if (NULL == temp.texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000645 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000646 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000647 return false;
648 }
649
robertphillips@google.comf294b772012-04-27 14:29:26 +0000650 // clear the temp target & draw into it
robertphillips@google.comf105b102012-05-14 12:18:26 +0000651 clear(gpu, temp.texture(), 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000652
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000653 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000654 this->drawClipShape(gpu, temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000655
656 // TODO: rather than adding these two translations here
657 // compute the bounding box needed to render the texture
658 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000659 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000660 GrMatrix m;
661
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000662 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
663 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000664
665 drawState->preConcatViewMatrix(m);
666 }
667
668 // Now draw into the accumulator using the real operation
669 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000670 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000671 this->drawTexture(gpu, accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000672
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000673 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000674 GrMatrix m;
675
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000676 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
677 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000678
679 drawState->preConcatViewMatrix(m);
680 }
681
682 } else {
683 // all the remaining ops can just be directly draw into
684 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000685 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000686 this->drawClipShape(gpu, accum, clipIn, c);
687 }
688 }
689
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000690 *result = accum;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000691
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692 return true;
693}
694
695////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696// Create a 1-bit clip mask in the stencil buffer
697bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
698 const GrClip& clipIn,
699 const GrRect& bounds,
700 ScissoringSettings* scissorSettings) {
701
702 GrAssert(fClipMaskInStencil);
703
704 GrDrawState* drawState = gpu->drawState();
705 GrAssert(drawState->isClipState());
706
707 GrRenderTarget* rt = drawState->getRenderTarget();
708 GrAssert(NULL != rt);
709
710 // TODO: dynamically attach a SB when needed.
711 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
712 if (NULL == stencilBuffer) {
713 return false;
714 }
715
716 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
717
718 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
719
720 // we set the current clip to the bounds so that our recursive
721 // draws are scissored to them. We use the copy of the complex clip
722 // we just stashed on the SB to render from. We set it back after
723 // we finish drawing it into the stencil.
724 const GrClip& clipCopy = stencilBuffer->getLastClip();
725 gpu->setClip(GrClip(bounds));
726
727 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
728 drawState = gpu->drawState();
729 drawState->setRenderTarget(rt);
730 GrDrawTarget::AutoGeometryPush agp(gpu);
731
732 gpu->disableScissor();
733#if !VISUALIZE_COMPLEX_CLIP
734 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
735#endif
736
737 int count = clipCopy.getElementCount();
738 int clipBit = stencilBuffer->bits();
739 SkASSERT((clipBit <= 16) &&
740 "Ganesh only handles 16b or smaller stencil buffers");
741 clipBit = (1 << (clipBit-1));
742
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000743 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744
745 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000746 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747 int start = process_initial_clip_elements(clipCopy,
748 rtRect,
749 &clearToInside,
750 &startOp);
751
752 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
753
754 // walk through each clip element and perform its set op
755 // with the existing clip.
756 for (int c = start; c < count; ++c) {
757 GrPathFill fill;
758 bool fillInverted;
759 // enabled at bottom of loop
760 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
761
762 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000763 // directly to the stencil buffer
764 // with a non-inverted fill rule
765 // without extra passes to
766 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000767
robertphillips@google.comf294b772012-04-27 14:29:26 +0000768 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
769
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000771 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 if (kRect_ClipType == clipCopy.getElementType(c)) {
773 canRenderDirectToStencil = true;
774 fill = kEvenOdd_PathFill;
775 fillInverted = false;
776 // there is no point in intersecting a screen filling
777 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000778 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000779 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780 continue;
781 }
782 } else {
783 fill = clipCopy.getPathFill(c);
784 fillInverted = GrIsFillInverted(fill);
785 fill = GrNonInvertedFill(fill);
786 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000787 pr = this->getContext()->getPathRenderer(*clipPath,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000788 fill, gpu, false,
789 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790 if (NULL == pr) {
791 fClipMaskInStencil = false;
792 gpu->setClip(clipCopy); // restore to the original
793 return false;
794 }
795 canRenderDirectToStencil =
796 !pr->requiresStencilPass(*clipPath, fill, gpu);
797 }
798
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 int passes;
800 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
801
802 bool canDrawDirectToClip; // Given the renderer, the element,
803 // fill rule, and set operation can
804 // we render the element directly to
805 // stencil bit used for clipping.
806 canDrawDirectToClip =
807 GrStencilSettings::GetClipPasses(op,
808 canRenderDirectToStencil,
809 clipBit,
810 fillInverted,
811 &passes, stencilSettings);
812
813 // draw the element to the client stencil bits if necessary
814 if (!canDrawDirectToClip) {
815 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
816 kIncClamp_StencilOp,
817 kIncClamp_StencilOp,
818 kAlways_StencilFunc,
819 0xffff,
820 0x0000,
821 0xffff);
822 SET_RANDOM_COLOR
823 if (kRect_ClipType == clipCopy.getElementType(c)) {
824 *drawState->stencil() = gDrawToStencil;
825 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
826 } else {
827 if (canRenderDirectToStencil) {
828 *drawState->stencil() = gDrawToStencil;
829 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
830 } else {
831 pr->drawPathToStencil(*clipPath, fill, gpu);
832 }
833 }
834 }
835
836 // now we modify the clip bit by rendering either the clip
837 // element directly or a bounding rect of the entire clip.
838 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
839 for (int p = 0; p < passes; ++p) {
840 *drawState->stencil() = stencilSettings[p];
841 if (canDrawDirectToClip) {
842 if (kRect_ClipType == clipCopy.getElementType(c)) {
843 SET_RANDOM_COLOR
844 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
845 } else {
846 SET_RANDOM_COLOR
847 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
848 }
849 } else {
850 SET_RANDOM_COLOR
851 gpu->drawSimpleRect(bounds, NULL, 0);
852 }
853 }
854 }
855 // restore clip
856 gpu->setClip(clipCopy);
857 // recusive draws would have disabled this since they drew with
858 // the clip bounds as clip.
859 fClipMaskInStencil = true;
860 }
861
862 return true;
863}
864
robertphillips@google.comfa662942012-05-17 12:20:22 +0000865namespace {
866
867GrPathFill invert_fill(GrPathFill fill) {
868 static const GrPathFill gInvertedFillTable[] = {
869 kInverseWinding_PathFill, // kWinding_PathFill
870 kInverseEvenOdd_PathFill, // kEvenOdd_PathFill
871 kWinding_PathFill, // kInverseWinding_PathFill
872 kEvenOdd_PathFill, // kInverseEvenOdd_PathFill
873 kHairLine_PathFill, // kHairLine_PathFill
874 };
875 GR_STATIC_ASSERT(0 == kWinding_PathFill);
876 GR_STATIC_ASSERT(1 == kEvenOdd_PathFill);
877 GR_STATIC_ASSERT(2 == kInverseWinding_PathFill);
878 GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill);
879 GR_STATIC_ASSERT(4 == kHairLine_PathFill);
880 GR_STATIC_ASSERT(5 == kPathFillCount);
881 return gInvertedFillTable[fill];
882}
883
884}
885
robertphillips@google.comf294b772012-04-27 14:29:26 +0000886////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000887bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu,
888 const GrClip& clipIn,
889 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000890 GrIRect *resultBounds) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000891
robertphillips@google.comf105b102012-05-14 12:18:26 +0000892 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000893 return true;
894 }
895
robertphillips@google.comf105b102012-05-14 12:18:26 +0000896 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000897 if (NULL == accum) {
898 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000899 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000900 return false;
901 }
902
robertphillips@google.com2c756812012-05-22 20:28:23 +0000903 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000904
robertphillips@google.comfa662942012-05-17 12:20:22 +0000905 helper.init(*resultBounds, NULL, false);
906
907 int count = clipIn.getElementCount();
908
909 bool clearToInside;
910 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
911 int start = process_initial_clip_elements(clipIn,
912 *resultBounds,
913 &clearToInside,
914 &startOp);
915
916 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
917
918 for (int i = start; i < count; ++i) {
919
920 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
921
922 if (SkRegion::kIntersect_Op == op ||
923 SkRegion::kReverseDifference_Op == op) {
924 // Intersect and reverse difference require modifying pixels
925 // outside of the geometry that is being "drawn". In both cases
926 // we erase all the pixels outside of the geometry but
927 // leave the pixels inside the geometry alone. For reverse
928 // difference we invert all the pixels before clearing the ones
929 // outside the geometry.
930 if (SkRegion::kReverseDifference_Op == op) {
931 SkRect temp = SkRect::MakeLTRB(
932 SkIntToScalar(resultBounds->left()),
933 SkIntToScalar(resultBounds->top()),
934 SkIntToScalar(resultBounds->right()),
935 SkIntToScalar(resultBounds->bottom()));
936
937 // invert the entire scene
938 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
939 }
940
941 if (kRect_ClipType == clipIn.getElementType(i)) {
942
943 // convert the rect to a path so we can invert the fill
944 SkPath temp;
945 temp.addRect(clipIn.getRect(i));
946
947 helper.draw(temp, SkRegion::kReplace_Op,
948 kInverseEvenOdd_PathFill, clipIn.getDoAA(i),
949 0x00000000);
950 } else {
951 GrAssert(kPath_ClipType == clipIn.getElementType(i));
952
953 helper.draw(clipIn.getPath(i),
954 SkRegion::kReplace_Op,
955 invert_fill(clipIn.getPathFill(i)),
956 clipIn.getDoAA(i),
957 0x00000000);
958 }
959
960 continue;
961 }
962
963 // The other ops (union, xor, diff) only affect pixels inside
964 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000965 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000966
967 helper.draw(clipIn.getRect(i),
968 op,
969 clipIn.getDoAA(i), SK_ColorWHITE);
970
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000971 } else {
972 GrAssert(kPath_ClipType == clipIn.getElementType(i));
973
robertphillips@google.comfa662942012-05-17 12:20:22 +0000974 helper.draw(clipIn.getPath(i),
975 op,
976 clipIn.getPathFill(i),
977 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000978 }
979 }
980
robertphillips@google.comfa662942012-05-17 12:20:22 +0000981 // Because we are using the scratch texture cache, "accum" may be
982 // larger than expected and have some cruft in the areas we aren't using.
983 // Clear it out.
984
985 // TODO: need a simpler way to clear the texture - can we combine
986 // the clear and the writePixels (inside toTexture)
987 GrDrawState* drawState = gpu->drawState();
988 GrAssert(NULL != drawState);
989 GrRenderTarget* temp = drawState->getRenderTarget();
990 clear(gpu, accum, 0x00000000);
991 // can't leave the accum bound as a rendertarget
992 drawState->setRenderTarget(temp);
993
994 helper.toTexture(accum);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000995
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000996 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000997
998 return true;
999}
1000
robertphillips@google.comf294b772012-04-27 14:29:26 +00001001////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001002void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001003 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001004}