blob: 1cb9dc456c603ddec6189426cb892461606bf9da [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.comf69a11b2012-06-15 13:58:07 +000099 // rects can always be drawn directly w/o using the software path
100 // so only paths need to be checked
101 if (kPath_ClipType == clipIn.getElementType(i) &&
102 path_needs_SW_renderer(this->getContext(), gpu,
103 clipIn.getPath(i),
104 clipIn.getPathFill(i),
105 clipIn.getDoAA(i))) {
106 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000108 }
109
110 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000111}
112
robertphillips@google.comf294b772012-04-27 14:29:26 +0000113////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000114// sort out what kind of clip mask needs to be created: alpha, stencil,
115// scissor, or entirely software
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000116bool GrClipMaskManager::createClipMask(GrGpu* gpu,
117 const GrClip& clipIn,
118 ScissoringSettings* scissorSettings) {
119
120 GrAssert(scissorSettings);
121
122 scissorSettings->fEnableScissoring = false;
123 fClipMaskInStencil = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000124 fClipMaskInAlpha = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000125
126 GrDrawState* drawState = gpu->drawState();
127 if (!drawState->isClipState()) {
128 return true;
129 }
130
131 GrRenderTarget* rt = drawState->getRenderTarget();
132
133 // GrDrawTarget should have filtered this for us
134 GrAssert(NULL != rt);
135
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000136#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000137 // If MSAA is enabled we can do everything in the stencil buffer.
138 // Otherwise check if we should just create the entire clip mask
139 // in software (this will only happen if the clip mask is anti-aliased
140 // and too complex for the gpu to handle in its entirety)
141 if (0 == rt->numSamples() && useSWOnlyPath(gpu, clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000142 // The clip geometry is complex enough that it will be more
143 // efficient to create it entirely in software
144 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000145 GrIRect bound;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000146 if (this->createSoftwareClipMask(gpu, clipIn, &result, &bound)) {
147 fClipMaskInAlpha = true;
148
149 setup_drawstate_aaclip(gpu, result, bound);
150 return true;
151 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000152
153 // if SW clip mask creation fails fall through to the other
154 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000155 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000156#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000157
robertphillips@google.comf294b772012-04-27 14:29:26 +0000158#if GR_AA_CLIP
159 // If MSAA is enabled use the (faster) stencil path for AA clipping
160 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000161 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000162 // Since we are going to create a destination texture of the correct
163 // size for the mask (rather than being bound by the size of the
164 // render target) we aren't going to use scissoring like the stencil
165 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000166 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000167 GrIRect bound;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000168 if (this->createAlphaClipMask(gpu, clipIn, &result, &bound)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000169 fClipMaskInAlpha = true;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000170
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000171 setup_drawstate_aaclip(gpu, result, bound);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000172 return true;
173 }
174
175 // if alpha clip mask creation fails fall through to the stencil
176 // buffer method
177 }
178#endif // GR_AA_CLIP
179
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000180 // Either a hard (stencil buffer) clip was explicitly requested or
181 // an antialiased clip couldn't be created. In either case, free up
182 // the texture in the antialiased mask cache.
183 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000184 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
185 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000186 // AA cache.
187 fAACache.reset();
188
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000189 GrRect bounds;
190 GrRect rtRect;
191 rtRect.setLTRB(0, 0,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000192 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000193 if (clipIn.hasConservativeBounds()) {
194 bounds = clipIn.getConservativeBounds();
195 if (!bounds.intersect(rtRect)) {
196 bounds.setEmpty();
197 }
198 } else {
199 bounds = rtRect;
200 }
201
202 bounds.roundOut(&scissorSettings->fScissorRect);
203 if (scissorSettings->fScissorRect.isEmpty()) {
204 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
205 // TODO: I think we can do an early exit here - after refactoring try:
206 // set fEnableScissoring to true but leave fClipMaskInStencil false
207 // and return - everything is going to be scissored away anyway!
208 }
209 scissorSettings->fEnableScissoring = true;
210
211 // use the stencil clip if we can't represent the clip as a rectangle.
212 fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
213 !bounds.isEmpty();
214
215 if (fClipMaskInStencil) {
216 return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings);
217 }
218
219 return true;
220}
221
222#define VISUALIZE_COMPLEX_CLIP 0
223
224#if VISUALIZE_COMPLEX_CLIP
225 #include "GrRandom.h"
226 GrRandom gRandom;
227 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
228#else
229 #define SET_RANDOM_COLOR
230#endif
231
232namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000233/**
234 * Does "container" contain "containee"? If either is empty then
235 * no containment is possible.
236 */
237bool contains(const SkRect& container, const SkIRect& containee) {
238 return !containee.isEmpty() && !container.isEmpty() &&
239 container.fLeft <= SkIntToScalar(containee.fLeft) &&
240 container.fTop <= SkIntToScalar(containee.fTop) &&
241 container.fRight >= SkIntToScalar(containee.fRight) &&
242 container.fBottom >= SkIntToScalar(containee.fBottom);
243}
244
245
robertphillips@google.comf294b772012-04-27 14:29:26 +0000246////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000247// determines how many elements at the head of the clip can be skipped and
248// whether the initial clear should be to the inside- or outside-the-clip value,
249// and what op should be used to draw the first element that isn't skipped.
250int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000251 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000252 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000253 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000254
255 // logically before the first element of the clip stack is
256 // processed the clip is entirely open. However, depending on the
257 // first set op we may prefer to clear to 0 for performance. We may
258 // also be able to skip the initial clip paths/rects. We loop until
259 // we cannot skip an element.
260 int curr;
261 bool done = false;
262 *clearToInside = true;
263 int count = clip.getElementCount();
264
265 for (curr = 0; curr < count && !done; ++curr) {
266 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000267 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000268 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000269 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000270 *clearToInside = false;
271 done = true;
272 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000273 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000274 // if this element contains the entire bounds then we
275 // can skip it.
276 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000277 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000278 break;
279 }
280 // if everything is initially clearToInside then intersect is
281 // same as clear to 0 and treat as a replace. Otherwise,
282 // set stays empty.
283 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000284 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000285 *clearToInside = false;
286 done = true;
287 }
288 break;
289 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000290 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 // if everything is initially outside then union is
292 // same as replace. Otherwise, every pixel is still
293 // clearToInside
294 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000295 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000296 done = true;
297 }
298 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000299 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 // xor is same as difference or replace both of which
301 // can be 1-pass instead of 2 for xor.
302 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000303 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000304 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000305 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000306 }
307 done = true;
308 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000309 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 // if all pixels are clearToInside then we have to process the
311 // difference, otherwise it has no effect and all pixels
312 // remain outside.
313 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000314 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000315 done = true;
316 }
317 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000318 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000319 // if all pixels are clearToInside then reverse difference
320 // produces empty set. Otherise it is same as replace
321 if (*clearToInside) {
322 *clearToInside = false;
323 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000324 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000325 done = true;
326 }
327 break;
328 default:
329 GrCrash("Unknown set op.");
330 }
331 }
332 return done ? curr-1 : count;
333}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000334
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335}
336
robertphillips@google.comf294b772012-04-27 14:29:26 +0000337
338namespace {
339
340////////////////////////////////////////////////////////////////////////////////
341// set up the OpenGL blend function to perform the specified
342// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000343void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000344
345 switch (op) {
346 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000347 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348 break;
349 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000350 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000351 break;
352 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000353 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000354 break;
355 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000356 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000357 break;
358 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000359 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360 break;
361 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000362 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363 break;
364 default:
365 GrAssert(false);
366 break;
367 }
368}
369
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000371bool draw_path(GrContext* context,
372 GrGpu* gpu,
373 const SkPath& path,
374 GrPathFill fill,
375 bool doAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376
robertphillips@google.com72176b22012-05-23 13:19:12 +0000377 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378 if (NULL == pr) {
379 return false;
380 }
381
382 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
383 return true;
384}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000385
386}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387
388////////////////////////////////////////////////////////////////////////////////
389bool GrClipMaskManager::drawClipShape(GrGpu* gpu,
390 GrTexture* target,
391 const GrClip& clipIn,
392 int index) {
393 GrDrawState* drawState = gpu->drawState();
394 GrAssert(NULL != drawState);
395
396 drawState->setRenderTarget(target->asRenderTarget());
397
398 if (kRect_ClipType == clipIn.getElementType(index)) {
399 if (clipIn.getDoAA(index)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000400 getContext()->getAARectRenderer()->fillAARect(gpu, gpu,
401 clipIn.getRect(index),
402 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403 } else {
404 gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
405 }
406 } else {
robertphillips@google.com2c756812012-05-22 20:28:23 +0000407 return draw_path(this->getContext(), gpu,
408 clipIn.getPath(index),
409 clipIn.getPathFill(index),
410 clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411 }
412 return true;
413}
414
415void GrClipMaskManager::drawTexture(GrGpu* gpu,
416 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000417 GrTexture* texture) {
418 GrDrawState* drawState = gpu->drawState();
419 GrAssert(NULL != drawState);
420
421 // no AA here since it is encoded in the texture
422 drawState->setRenderTarget(target->asRenderTarget());
423
424 GrMatrix sampleM;
425 sampleM.setIDiv(texture->width(), texture->height());
426 drawState->setTexture(0, texture);
427
428 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
429 GrSamplerState::kNearest_Filter,
430 sampleM);
431
robertphillips@google.comf105b102012-05-14 12:18:26 +0000432 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
433 SkIntToScalar(target->height()));
434
robertphillips@google.comf294b772012-04-27 14:29:26 +0000435 gpu->drawSimpleRect(rect, NULL, 1 << 0);
436
437 drawState->setTexture(0, NULL);
438}
439
440namespace {
441
442void clear(GrGpu* gpu,
443 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000444 GrColor color) {
445 GrDrawState* drawState = gpu->drawState();
446 GrAssert(NULL != drawState);
447
448 // zap entire target to specified color
449 drawState->setRenderTarget(target->asRenderTarget());
450 gpu->clear(NULL, color);
451}
452
robertphillips@google.comf105b102012-05-14 12:18:26 +0000453}
454
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000455// get a texture to act as a temporary buffer for AA clip boolean operations
456// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000457void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000458 GrAutoScratchTexture* temp) {
459 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000460 // we've already allocated the temp texture
461 return;
462 }
463
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000464 GrTextureDesc desc;
465 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
466 desc.fWidth = bounds.width();
467 desc.fHeight = bounds.height();
468 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000469
robertphillips@google.com2c756812012-05-22 20:28:23 +0000470 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000471}
472
robertphillips@google.comf105b102012-05-14 12:18:26 +0000473
474void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000475 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000476 // Since we are setting up the cache we know the last lookup was a miss
477 // Free up the currently cached mask so it can be reused
478 fAACache.reset();
479
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000480 GrTextureDesc desc;
481 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
482 desc.fWidth = bounds.width();
483 desc.fHeight = bounds.height();
484 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000485
486 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000487}
488
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000489////////////////////////////////////////////////////////////////////////////////
490// Shared preamble between gpu and SW-only AA clip mask creation paths.
491// Handles caching, determination of clip mask bound & allocation (if needed)
492// of the result texture
493// Returns true if there is no more work to be done (i.e., we got a cache hit)
494bool GrClipMaskManager::clipMaskPreamble(GrGpu* gpu,
495 const GrClip& clipIn,
496 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000497 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498 GrDrawState* origDrawState = gpu->drawState();
499 GrAssert(origDrawState->isClipState());
500
501 GrRenderTarget* rt = origDrawState->getRenderTarget();
502 GrAssert(NULL != rt);
503
504 GrRect rtRect;
505 rtRect.setLTRB(0, 0,
506 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
507
508 // unlike the stencil path the alpha path is not bound to the size of the
509 // render target - determine the minimum size required for the mask
510 GrRect bounds;
511
512 if (clipIn.hasConservativeBounds()) {
513 bounds = clipIn.getConservativeBounds();
514 if (!bounds.intersect(rtRect)) {
515 // the mask will be empty in this case
516 GrAssert(false);
517 bounds.setEmpty();
518 }
519 } else {
520 // still locked to the size of the render target
521 bounds = rtRect;
522 }
523
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000524 GrIRect intBounds;
525 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000526
527 // need to outset a pixel since the standard bounding box computation
528 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000529 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000530
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000531 // TODO: make sure we don't outset if bounds are still 0,0 @ min
532
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000533 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000534 intBounds.width(),
535 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000536 *result = fAACache.getLastMask();
537 fAACache.getLastBound(resultBounds);
538 return true;
539 }
540
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000541 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000542
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000543 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000544 return false;
545}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000546
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000547////////////////////////////////////////////////////////////////////////////////
548// Create a 8-bit clip mask in alpha
549bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu,
550 const GrClip& clipIn,
551 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000552 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000553
robertphillips@google.comf105b102012-05-14 12:18:26 +0000554 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000555 return true;
556 }
557
robertphillips@google.comf105b102012-05-14 12:18:26 +0000558 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000559 if (NULL == accum) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000561 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562 return false;
563 }
564
565 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
566 GrDrawState* drawState = gpu->drawState();
567
568 GrDrawTarget::AutoGeometryPush agp(gpu);
569
570 int count = clipIn.getElementCount();
571
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000572 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000573 // if we were able to trim down the size of the mask we need to
574 // offset the paths & rects that will be used to compute it
575 GrMatrix m;
576
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000577 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
578 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000579
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000580 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000581 }
582
583 bool clearToInside;
584 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
585 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000586 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000587 &clearToInside,
588 &startOp);
589
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000590 clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591
robertphillips@google.comf105b102012-05-14 12:18:26 +0000592 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000593
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594 // walk through each clip element and perform its set op
595 for (int c = start; c < count; ++c) {
596
597 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
598
599 if (SkRegion::kReplace_Op == op) {
600 // TODO: replace is actually a lot faster then intersection
601 // for this path - refactor the stencil path so it can handle
602 // replace ops and alter GrClip to allow them through
603
604 // clear the accumulator and draw the new object directly into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000605 clear(gpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000606
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000607 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608 this->drawClipShape(gpu, accum, clipIn, c);
609
610 } else if (SkRegion::kReverseDifference_Op == op ||
611 SkRegion::kIntersect_Op == op) {
612 // there is no point in intersecting a screen filling rectangle.
613 if (SkRegion::kIntersect_Op == op &&
614 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000615 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000616 continue;
617 }
618
robertphillips@google.comf105b102012-05-14 12:18:26 +0000619 getTemp(*resultBounds, &temp);
620 if (NULL == temp.texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000621 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000622 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000623 return false;
624 }
625
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626 // clear the temp target & draw into it
robertphillips@google.comf105b102012-05-14 12:18:26 +0000627 clear(gpu, temp.texture(), 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000629 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000630 this->drawClipShape(gpu, temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000631
632 // TODO: rather than adding these two translations here
633 // compute the bounding box needed to render the texture
634 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000635 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000636 GrMatrix m;
637
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000638 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
639 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640
641 drawState->preConcatViewMatrix(m);
642 }
643
644 // Now draw into the accumulator using the real operation
645 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000646 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000647 this->drawTexture(gpu, accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000649 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000650 GrMatrix m;
651
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000652 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
653 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000654
655 drawState->preConcatViewMatrix(m);
656 }
657
658 } else {
659 // all the remaining ops can just be directly draw into
660 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000661 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662 this->drawClipShape(gpu, accum, clipIn, c);
663 }
664 }
665
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000666 *result = accum;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000667
robertphillips@google.comf294b772012-04-27 14:29:26 +0000668 return true;
669}
670
671////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000672// Create a 1-bit clip mask in the stencil buffer
673bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
674 const GrClip& clipIn,
675 const GrRect& bounds,
676 ScissoringSettings* scissorSettings) {
677
678 GrAssert(fClipMaskInStencil);
679
680 GrDrawState* drawState = gpu->drawState();
681 GrAssert(drawState->isClipState());
682
683 GrRenderTarget* rt = drawState->getRenderTarget();
684 GrAssert(NULL != rt);
685
686 // TODO: dynamically attach a SB when needed.
687 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
688 if (NULL == stencilBuffer) {
689 return false;
690 }
691
692 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
693
694 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
695
696 // we set the current clip to the bounds so that our recursive
697 // draws are scissored to them. We use the copy of the complex clip
698 // we just stashed on the SB to render from. We set it back after
699 // we finish drawing it into the stencil.
700 const GrClip& clipCopy = stencilBuffer->getLastClip();
701 gpu->setClip(GrClip(bounds));
702
703 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
704 drawState = gpu->drawState();
705 drawState->setRenderTarget(rt);
706 GrDrawTarget::AutoGeometryPush agp(gpu);
707
708 gpu->disableScissor();
709#if !VISUALIZE_COMPLEX_CLIP
710 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
711#endif
712
713 int count = clipCopy.getElementCount();
714 int clipBit = stencilBuffer->bits();
715 SkASSERT((clipBit <= 16) &&
716 "Ganesh only handles 16b or smaller stencil buffers");
717 clipBit = (1 << (clipBit-1));
718
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000719 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720
721 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000722 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 int start = process_initial_clip_elements(clipCopy,
724 rtRect,
725 &clearToInside,
726 &startOp);
727
728 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
729
730 // walk through each clip element and perform its set op
731 // with the existing clip.
732 for (int c = start; c < count; ++c) {
733 GrPathFill fill;
734 bool fillInverted;
735 // enabled at bottom of loop
736 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
737
738 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000739 // directly to the stencil buffer
740 // with a non-inverted fill rule
741 // without extra passes to
742 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743
robertphillips@google.comf294b772012-04-27 14:29:26 +0000744 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
745
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000747 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000748 if (kRect_ClipType == clipCopy.getElementType(c)) {
749 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000750 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 fillInverted = false;
752 // there is no point in intersecting a screen filling
753 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000754 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000755 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756 continue;
757 }
758 } else {
759 fill = clipCopy.getPathFill(c);
760 fillInverted = GrIsFillInverted(fill);
761 fill = GrNonInvertedFill(fill);
762 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000763 pr = this->getContext()->getPathRenderer(*clipPath,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000764 fill, gpu, false,
765 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766 if (NULL == pr) {
767 fClipMaskInStencil = false;
768 gpu->setClip(clipCopy); // restore to the original
769 return false;
770 }
771 canRenderDirectToStencil =
772 !pr->requiresStencilPass(*clipPath, fill, gpu);
773 }
774
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 int passes;
776 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
777
778 bool canDrawDirectToClip; // Given the renderer, the element,
779 // fill rule, and set operation can
780 // we render the element directly to
781 // stencil bit used for clipping.
782 canDrawDirectToClip =
783 GrStencilSettings::GetClipPasses(op,
784 canRenderDirectToStencil,
785 clipBit,
786 fillInverted,
787 &passes, stencilSettings);
788
789 // draw the element to the client stencil bits if necessary
790 if (!canDrawDirectToClip) {
791 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
792 kIncClamp_StencilOp,
793 kIncClamp_StencilOp,
794 kAlways_StencilFunc,
795 0xffff,
796 0x0000,
797 0xffff);
798 SET_RANDOM_COLOR
799 if (kRect_ClipType == clipCopy.getElementType(c)) {
800 *drawState->stencil() = gDrawToStencil;
801 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
802 } else {
803 if (canRenderDirectToStencil) {
804 *drawState->stencil() = gDrawToStencil;
805 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
806 } else {
807 pr->drawPathToStencil(*clipPath, fill, gpu);
808 }
809 }
810 }
811
812 // now we modify the clip bit by rendering either the clip
813 // element directly or a bounding rect of the entire clip.
814 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
815 for (int p = 0; p < passes; ++p) {
816 *drawState->stencil() = stencilSettings[p];
817 if (canDrawDirectToClip) {
818 if (kRect_ClipType == clipCopy.getElementType(c)) {
819 SET_RANDOM_COLOR
820 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
821 } else {
822 SET_RANDOM_COLOR
823 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
824 }
825 } else {
826 SET_RANDOM_COLOR
827 gpu->drawSimpleRect(bounds, NULL, 0);
828 }
829 }
830 }
831 // restore clip
832 gpu->setClip(clipCopy);
833 // recusive draws would have disabled this since they drew with
834 // the clip bounds as clip.
835 fClipMaskInStencil = true;
836 }
837
838 return true;
839}
840
bsalomon@google.com411dad02012-06-05 20:24:20 +0000841// mapping of clip-respecting stencil funcs to normal stencil funcs
842// mapping depends on whether stencil-clipping is in effect.
843static const GrStencilFunc
844 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
845 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
846 // In the Clip Funcs
847 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
848 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
849 kLess_StencilFunc, // kLessIfInClip_StencilFunc
850 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
851 // Special in the clip func that forces user's ref to be 0.
852 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
853 // make ref 0 and do normal nequal.
854 },
855 {// Stencil-Clipping is ENABLED
856 // In the Clip Funcs
857 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
858 // eq stencil clip bit, mask
859 // out user bits.
860
861 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
862 // add stencil bit to mask and ref
863
864 kLess_StencilFunc, // kLessIfInClip_StencilFunc
865 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
866 // for both of these we can add
867 // the clip bit to the mask and
868 // ref and compare as normal
869 // Special in the clip func that forces user's ref to be 0.
870 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
871 // make ref have only the clip bit set
872 // and make comparison be less
873 // 10..0 < 1..user_bits..
874 }
875};
876
877GrStencilFunc GrClipMaskManager::adjustStencilParams(GrStencilFunc func,
878 StencilClipMode mode,
879 unsigned int stencilBitCnt,
880 unsigned int* ref,
881 unsigned int* mask,
882 unsigned int* writeMask) {
883 GrAssert(stencilBitCnt > 0);
884 GrAssert((unsigned) func < kStencilFuncCount);
885
886 if (kModifyClip_StencilClipMode == mode) {
887 // We assume that this class is the client/draw-caller of the GrGpu and
888 // has already setup the correct values
889 return func;
890 }
891 unsigned int clipBit = (1 << (stencilBitCnt - 1));
892 unsigned int userBits = clipBit - 1;
893
894 *writeMask &= userBits;
895
896 if (func >= kBasicStencilFuncCount) {
897 int respectClip = kRespectClip_StencilClipMode == mode;
898 if (respectClip) {
899 // The GrGpu class should have checked this
900 GrAssert(this->isClipInStencil());
901 switch (func) {
902 case kAlwaysIfInClip_StencilFunc:
903 *mask = clipBit;
904 *ref = clipBit;
905 break;
906 case kEqualIfInClip_StencilFunc:
907 case kLessIfInClip_StencilFunc:
908 case kLEqualIfInClip_StencilFunc:
909 *mask = (*mask & userBits) | clipBit;
910 *ref = (*ref & userBits) | clipBit;
911 break;
912 case kNonZeroIfInClip_StencilFunc:
913 *mask = (*mask & userBits) | clipBit;
914 *ref = clipBit;
915 break;
916 default:
917 GrCrash("Unknown stencil func");
918 }
919 } else {
920 *mask &= userBits;
921 *ref &= userBits;
922 }
923 const GrStencilFunc* table = gSpecialToBasicStencilFunc[respectClip];
924 func = table[func - kBasicStencilFuncCount];
925 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
926 } else {
927 *mask &= userBits;
928 *ref &= userBits;
929 }
930 return func;
931}
932
933////////////////////////////////////////////////////////////////////////////////
934
robertphillips@google.comfa662942012-05-17 12:20:22 +0000935namespace {
936
937GrPathFill invert_fill(GrPathFill fill) {
938 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000939 kInverseWinding_GrPathFill, // kWinding_GrPathFill
940 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
941 kWinding_GrPathFill, // kInverseWinding_GrPathFill
942 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
943 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +0000944 };
bsalomon@google.com47059542012-06-06 20:51:20 +0000945 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
946 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
947 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
948 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
949 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
950 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000951 return gInvertedFillTable[fill];
952}
953
954}
955
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000956bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu,
957 const GrClip& clipIn,
958 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000959 GrIRect *resultBounds) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000960
robertphillips@google.comf105b102012-05-14 12:18:26 +0000961 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000962 return true;
963 }
964
robertphillips@google.comf105b102012-05-14 12:18:26 +0000965 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000966 if (NULL == accum) {
967 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000968 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000969 return false;
970 }
971
robertphillips@google.com2c756812012-05-22 20:28:23 +0000972 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000973
robertphillips@google.comfa662942012-05-17 12:20:22 +0000974 helper.init(*resultBounds, NULL, false);
975
976 int count = clipIn.getElementCount();
977
978 bool clearToInside;
979 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
980 int start = process_initial_clip_elements(clipIn,
981 *resultBounds,
982 &clearToInside,
983 &startOp);
984
985 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
986
987 for (int i = start; i < count; ++i) {
988
989 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
990
991 if (SkRegion::kIntersect_Op == op ||
992 SkRegion::kReverseDifference_Op == op) {
993 // Intersect and reverse difference require modifying pixels
994 // outside of the geometry that is being "drawn". In both cases
995 // we erase all the pixels outside of the geometry but
996 // leave the pixels inside the geometry alone. For reverse
997 // difference we invert all the pixels before clearing the ones
998 // outside the geometry.
999 if (SkRegion::kReverseDifference_Op == op) {
1000 SkRect temp = SkRect::MakeLTRB(
1001 SkIntToScalar(resultBounds->left()),
1002 SkIntToScalar(resultBounds->top()),
1003 SkIntToScalar(resultBounds->right()),
1004 SkIntToScalar(resultBounds->bottom()));
1005
1006 // invert the entire scene
1007 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
1008 }
1009
1010 if (kRect_ClipType == clipIn.getElementType(i)) {
1011
1012 // convert the rect to a path so we can invert the fill
1013 SkPath temp;
1014 temp.addRect(clipIn.getRect(i));
1015
1016 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001017 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001018 0x00000000);
1019 } else {
1020 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1021
1022 helper.draw(clipIn.getPath(i),
1023 SkRegion::kReplace_Op,
1024 invert_fill(clipIn.getPathFill(i)),
1025 clipIn.getDoAA(i),
1026 0x00000000);
1027 }
1028
1029 continue;
1030 }
1031
1032 // The other ops (union, xor, diff) only affect pixels inside
1033 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001034 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001035
1036 helper.draw(clipIn.getRect(i),
1037 op,
1038 clipIn.getDoAA(i), SK_ColorWHITE);
1039
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001040 } else {
1041 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1042
robertphillips@google.comfa662942012-05-17 12:20:22 +00001043 helper.draw(clipIn.getPath(i),
1044 op,
1045 clipIn.getPathFill(i),
1046 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001047 }
1048 }
1049
robertphillips@google.comfa662942012-05-17 12:20:22 +00001050 // Because we are using the scratch texture cache, "accum" may be
1051 // larger than expected and have some cruft in the areas we aren't using.
1052 // Clear it out.
1053
1054 // TODO: need a simpler way to clear the texture - can we combine
1055 // the clear and the writePixels (inside toTexture)
1056 GrDrawState* drawState = gpu->drawState();
1057 GrAssert(NULL != drawState);
1058 GrRenderTarget* temp = drawState->getRenderTarget();
1059 clear(gpu, accum, 0x00000000);
1060 // can't leave the accum bound as a rendertarget
1061 drawState->setRenderTarget(temp);
1062
1063 helper.toTexture(accum);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001064
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001065 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001066
1067 return true;
1068}
1069
robertphillips@google.comf294b772012-04-27 14:29:26 +00001070////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001071void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001072 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001073}