blob: 28ce46d27ea128761dcc94b22893c949203df282 [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
bsalomon@google.com47059542012-06-06 20:51:20 +0000105 // kEvenOdd_GrPathFill.
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000106
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,
bsalomon@google.com47059542012-06-06 20:51:20 +0000113 kEvenOdd_GrPathFill, true)) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000114 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:
bsalomon@google.com47059542012-06-06 20:51:20 +0000364 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365 break;
366 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000367 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368 break;
369 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000370 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371 break;
372 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000373 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374 break;
375 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000376 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 break;
378 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000379 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380 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,
bsalomon@google.com47059542012-06-06 20:51:20 +0000422 kEvenOdd_GrPathFill, 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.com75b3c962012-06-07 12:08:45 +0000484 GrTextureDesc desc;
485 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
486 desc.fWidth = bounds.width();
487 desc.fHeight = bounds.height();
488 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000489
robertphillips@google.com2c756812012-05-22 20:28:23 +0000490 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000491}
492
robertphillips@google.comf105b102012-05-14 12:18:26 +0000493
494void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000495 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000496 // Since we are setting up the cache we know the last lookup was a miss
497 // Free up the currently cached mask so it can be reused
498 fAACache.reset();
499
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000500 GrTextureDesc desc;
501 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
502 desc.fWidth = bounds.width();
503 desc.fHeight = bounds.height();
504 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000505
506 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000507}
508
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000509////////////////////////////////////////////////////////////////////////////////
510// Shared preamble between gpu and SW-only AA clip mask creation paths.
511// Handles caching, determination of clip mask bound & allocation (if needed)
512// of the result texture
513// Returns true if there is no more work to be done (i.e., we got a cache hit)
514bool GrClipMaskManager::clipMaskPreamble(GrGpu* gpu,
515 const GrClip& clipIn,
516 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000517 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000518 GrDrawState* origDrawState = gpu->drawState();
519 GrAssert(origDrawState->isClipState());
520
521 GrRenderTarget* rt = origDrawState->getRenderTarget();
522 GrAssert(NULL != rt);
523
524 GrRect rtRect;
525 rtRect.setLTRB(0, 0,
526 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
527
528 // unlike the stencil path the alpha path is not bound to the size of the
529 // render target - determine the minimum size required for the mask
530 GrRect bounds;
531
532 if (clipIn.hasConservativeBounds()) {
533 bounds = clipIn.getConservativeBounds();
534 if (!bounds.intersect(rtRect)) {
535 // the mask will be empty in this case
536 GrAssert(false);
537 bounds.setEmpty();
538 }
539 } else {
540 // still locked to the size of the render target
541 bounds = rtRect;
542 }
543
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000544 GrIRect intBounds;
545 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000546
547 // need to outset a pixel since the standard bounding box computation
548 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000549 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000550
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000551 // TODO: make sure we don't outset if bounds are still 0,0 @ min
552
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000553 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000554 intBounds.width(),
555 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000556 *result = fAACache.getLastMask();
557 fAACache.getLastBound(resultBounds);
558 return true;
559 }
560
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000561 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000562
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000563 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000564 return false;
565}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000566
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000567////////////////////////////////////////////////////////////////////////////////
568// Create a 8-bit clip mask in alpha
569bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu,
570 const GrClip& clipIn,
571 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000572 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000573
robertphillips@google.comf105b102012-05-14 12:18:26 +0000574 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000575 return true;
576 }
577
robertphillips@google.comf105b102012-05-14 12:18:26 +0000578 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000579 if (NULL == accum) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000580 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000581 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000582 return false;
583 }
584
585 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
586 GrDrawState* drawState = gpu->drawState();
587
588 GrDrawTarget::AutoGeometryPush agp(gpu);
589
590 int count = clipIn.getElementCount();
591
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000592 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000593 // if we were able to trim down the size of the mask we need to
594 // offset the paths & rects that will be used to compute it
595 GrMatrix m;
596
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000597 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
598 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000599
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000600 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000601 }
602
603 bool clearToInside;
604 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
605 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000606 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000607 &clearToInside,
608 &startOp);
609
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000610 clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611
robertphillips@google.comf105b102012-05-14 12:18:26 +0000612 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000613
robertphillips@google.comf294b772012-04-27 14:29:26 +0000614 // walk through each clip element and perform its set op
615 for (int c = start; c < count; ++c) {
616
617 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
618
619 if (SkRegion::kReplace_Op == op) {
620 // TODO: replace is actually a lot faster then intersection
621 // for this path - refactor the stencil path so it can handle
622 // replace ops and alter GrClip to allow them through
623
624 // clear the accumulator and draw the new object directly into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000625 clear(gpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000627 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628 this->drawClipShape(gpu, accum, clipIn, c);
629
630 } else if (SkRegion::kReverseDifference_Op == op ||
631 SkRegion::kIntersect_Op == op) {
632 // there is no point in intersecting a screen filling rectangle.
633 if (SkRegion::kIntersect_Op == op &&
634 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000635 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000636 continue;
637 }
638
robertphillips@google.comf105b102012-05-14 12:18:26 +0000639 getTemp(*resultBounds, &temp);
640 if (NULL == temp.texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000641 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000642 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000643 return false;
644 }
645
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646 // clear the temp target & draw into it
robertphillips@google.comf105b102012-05-14 12:18:26 +0000647 clear(gpu, temp.texture(), 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000649 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000650 this->drawClipShape(gpu, temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651
652 // TODO: rather than adding these two translations here
653 // compute the bounding box needed to render the texture
654 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000655 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000656 GrMatrix m;
657
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000658 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
659 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000660
661 drawState->preConcatViewMatrix(m);
662 }
663
664 // Now draw into the accumulator using the real operation
665 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000666 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000667 this->drawTexture(gpu, accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000668
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000669 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000670 GrMatrix m;
671
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000672 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
673 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000674
675 drawState->preConcatViewMatrix(m);
676 }
677
678 } else {
679 // all the remaining ops can just be directly draw into
680 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000681 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000682 this->drawClipShape(gpu, accum, clipIn, c);
683 }
684 }
685
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000686 *result = accum;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000687
robertphillips@google.comf294b772012-04-27 14:29:26 +0000688 return true;
689}
690
691////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692// Create a 1-bit clip mask in the stencil buffer
693bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
694 const GrClip& clipIn,
695 const GrRect& bounds,
696 ScissoringSettings* scissorSettings) {
697
698 GrAssert(fClipMaskInStencil);
699
700 GrDrawState* drawState = gpu->drawState();
701 GrAssert(drawState->isClipState());
702
703 GrRenderTarget* rt = drawState->getRenderTarget();
704 GrAssert(NULL != rt);
705
706 // TODO: dynamically attach a SB when needed.
707 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
708 if (NULL == stencilBuffer) {
709 return false;
710 }
711
712 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
713
714 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
715
716 // we set the current clip to the bounds so that our recursive
717 // draws are scissored to them. We use the copy of the complex clip
718 // we just stashed on the SB to render from. We set it back after
719 // we finish drawing it into the stencil.
720 const GrClip& clipCopy = stencilBuffer->getLastClip();
721 gpu->setClip(GrClip(bounds));
722
723 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
724 drawState = gpu->drawState();
725 drawState->setRenderTarget(rt);
726 GrDrawTarget::AutoGeometryPush agp(gpu);
727
728 gpu->disableScissor();
729#if !VISUALIZE_COMPLEX_CLIP
730 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
731#endif
732
733 int count = clipCopy.getElementCount();
734 int clipBit = stencilBuffer->bits();
735 SkASSERT((clipBit <= 16) &&
736 "Ganesh only handles 16b or smaller stencil buffers");
737 clipBit = (1 << (clipBit-1));
738
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000739 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
741 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000742 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743 int start = process_initial_clip_elements(clipCopy,
744 rtRect,
745 &clearToInside,
746 &startOp);
747
748 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
749
750 // walk through each clip element and perform its set op
751 // with the existing clip.
752 for (int c = start; c < count; ++c) {
753 GrPathFill fill;
754 bool fillInverted;
755 // enabled at bottom of loop
756 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
757
758 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000759 // directly to the stencil buffer
760 // with a non-inverted fill rule
761 // without extra passes to
762 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000763
robertphillips@google.comf294b772012-04-27 14:29:26 +0000764 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
765
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000767 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768 if (kRect_ClipType == clipCopy.getElementType(c)) {
769 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000770 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000771 fillInverted = false;
772 // there is no point in intersecting a screen filling
773 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000774 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000775 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776 continue;
777 }
778 } else {
779 fill = clipCopy.getPathFill(c);
780 fillInverted = GrIsFillInverted(fill);
781 fill = GrNonInvertedFill(fill);
782 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000783 pr = this->getContext()->getPathRenderer(*clipPath,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000784 fill, gpu, false,
785 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 if (NULL == pr) {
787 fClipMaskInStencil = false;
788 gpu->setClip(clipCopy); // restore to the original
789 return false;
790 }
791 canRenderDirectToStencil =
792 !pr->requiresStencilPass(*clipPath, fill, gpu);
793 }
794
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000795 int passes;
796 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
797
798 bool canDrawDirectToClip; // Given the renderer, the element,
799 // fill rule, and set operation can
800 // we render the element directly to
801 // stencil bit used for clipping.
802 canDrawDirectToClip =
803 GrStencilSettings::GetClipPasses(op,
804 canRenderDirectToStencil,
805 clipBit,
806 fillInverted,
807 &passes, stencilSettings);
808
809 // draw the element to the client stencil bits if necessary
810 if (!canDrawDirectToClip) {
811 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
812 kIncClamp_StencilOp,
813 kIncClamp_StencilOp,
814 kAlways_StencilFunc,
815 0xffff,
816 0x0000,
817 0xffff);
818 SET_RANDOM_COLOR
819 if (kRect_ClipType == clipCopy.getElementType(c)) {
820 *drawState->stencil() = gDrawToStencil;
821 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
822 } else {
823 if (canRenderDirectToStencil) {
824 *drawState->stencil() = gDrawToStencil;
825 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
826 } else {
827 pr->drawPathToStencil(*clipPath, fill, gpu);
828 }
829 }
830 }
831
832 // now we modify the clip bit by rendering either the clip
833 // element directly or a bounding rect of the entire clip.
834 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
835 for (int p = 0; p < passes; ++p) {
836 *drawState->stencil() = stencilSettings[p];
837 if (canDrawDirectToClip) {
838 if (kRect_ClipType == clipCopy.getElementType(c)) {
839 SET_RANDOM_COLOR
840 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
841 } else {
842 SET_RANDOM_COLOR
843 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
844 }
845 } else {
846 SET_RANDOM_COLOR
847 gpu->drawSimpleRect(bounds, NULL, 0);
848 }
849 }
850 }
851 // restore clip
852 gpu->setClip(clipCopy);
853 // recusive draws would have disabled this since they drew with
854 // the clip bounds as clip.
855 fClipMaskInStencil = true;
856 }
857
858 return true;
859}
860
bsalomon@google.com411dad02012-06-05 20:24:20 +0000861// mapping of clip-respecting stencil funcs to normal stencil funcs
862// mapping depends on whether stencil-clipping is in effect.
863static const GrStencilFunc
864 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
865 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
866 // In the Clip Funcs
867 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
868 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
869 kLess_StencilFunc, // kLessIfInClip_StencilFunc
870 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
871 // Special in the clip func that forces user's ref to be 0.
872 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
873 // make ref 0 and do normal nequal.
874 },
875 {// Stencil-Clipping is ENABLED
876 // In the Clip Funcs
877 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
878 // eq stencil clip bit, mask
879 // out user bits.
880
881 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
882 // add stencil bit to mask and ref
883
884 kLess_StencilFunc, // kLessIfInClip_StencilFunc
885 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
886 // for both of these we can add
887 // the clip bit to the mask and
888 // ref and compare as normal
889 // Special in the clip func that forces user's ref to be 0.
890 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
891 // make ref have only the clip bit set
892 // and make comparison be less
893 // 10..0 < 1..user_bits..
894 }
895};
896
897GrStencilFunc GrClipMaskManager::adjustStencilParams(GrStencilFunc func,
898 StencilClipMode mode,
899 unsigned int stencilBitCnt,
900 unsigned int* ref,
901 unsigned int* mask,
902 unsigned int* writeMask) {
903 GrAssert(stencilBitCnt > 0);
904 GrAssert((unsigned) func < kStencilFuncCount);
905
906 if (kModifyClip_StencilClipMode == mode) {
907 // We assume that this class is the client/draw-caller of the GrGpu and
908 // has already setup the correct values
909 return func;
910 }
911 unsigned int clipBit = (1 << (stencilBitCnt - 1));
912 unsigned int userBits = clipBit - 1;
913
914 *writeMask &= userBits;
915
916 if (func >= kBasicStencilFuncCount) {
917 int respectClip = kRespectClip_StencilClipMode == mode;
918 if (respectClip) {
919 // The GrGpu class should have checked this
920 GrAssert(this->isClipInStencil());
921 switch (func) {
922 case kAlwaysIfInClip_StencilFunc:
923 *mask = clipBit;
924 *ref = clipBit;
925 break;
926 case kEqualIfInClip_StencilFunc:
927 case kLessIfInClip_StencilFunc:
928 case kLEqualIfInClip_StencilFunc:
929 *mask = (*mask & userBits) | clipBit;
930 *ref = (*ref & userBits) | clipBit;
931 break;
932 case kNonZeroIfInClip_StencilFunc:
933 *mask = (*mask & userBits) | clipBit;
934 *ref = clipBit;
935 break;
936 default:
937 GrCrash("Unknown stencil func");
938 }
939 } else {
940 *mask &= userBits;
941 *ref &= userBits;
942 }
943 const GrStencilFunc* table = gSpecialToBasicStencilFunc[respectClip];
944 func = table[func - kBasicStencilFuncCount];
945 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
946 } else {
947 *mask &= userBits;
948 *ref &= userBits;
949 }
950 return func;
951}
952
953////////////////////////////////////////////////////////////////////////////////
954
robertphillips@google.comfa662942012-05-17 12:20:22 +0000955namespace {
956
957GrPathFill invert_fill(GrPathFill fill) {
958 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000959 kInverseWinding_GrPathFill, // kWinding_GrPathFill
960 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
961 kWinding_GrPathFill, // kInverseWinding_GrPathFill
962 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
963 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +0000964 };
bsalomon@google.com47059542012-06-06 20:51:20 +0000965 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
966 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
967 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
968 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
969 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
970 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000971 return gInvertedFillTable[fill];
972}
973
974}
975
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000976bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu,
977 const GrClip& clipIn,
978 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000979 GrIRect *resultBounds) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000980
robertphillips@google.comf105b102012-05-14 12:18:26 +0000981 if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000982 return true;
983 }
984
robertphillips@google.comf105b102012-05-14 12:18:26 +0000985 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000986 if (NULL == accum) {
987 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000988 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000989 return false;
990 }
991
robertphillips@google.com2c756812012-05-22 20:28:23 +0000992 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000993
robertphillips@google.comfa662942012-05-17 12:20:22 +0000994 helper.init(*resultBounds, NULL, false);
995
996 int count = clipIn.getElementCount();
997
998 bool clearToInside;
999 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
1000 int start = process_initial_clip_elements(clipIn,
1001 *resultBounds,
1002 &clearToInside,
1003 &startOp);
1004
1005 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
1006
1007 for (int i = start; i < count; ++i) {
1008
1009 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
1010
1011 if (SkRegion::kIntersect_Op == op ||
1012 SkRegion::kReverseDifference_Op == op) {
1013 // Intersect and reverse difference require modifying pixels
1014 // outside of the geometry that is being "drawn". In both cases
1015 // we erase all the pixels outside of the geometry but
1016 // leave the pixels inside the geometry alone. For reverse
1017 // difference we invert all the pixels before clearing the ones
1018 // outside the geometry.
1019 if (SkRegion::kReverseDifference_Op == op) {
1020 SkRect temp = SkRect::MakeLTRB(
1021 SkIntToScalar(resultBounds->left()),
1022 SkIntToScalar(resultBounds->top()),
1023 SkIntToScalar(resultBounds->right()),
1024 SkIntToScalar(resultBounds->bottom()));
1025
1026 // invert the entire scene
1027 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
1028 }
1029
1030 if (kRect_ClipType == clipIn.getElementType(i)) {
1031
1032 // convert the rect to a path so we can invert the fill
1033 SkPath temp;
1034 temp.addRect(clipIn.getRect(i));
1035
1036 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001037 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001038 0x00000000);
1039 } else {
1040 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1041
1042 helper.draw(clipIn.getPath(i),
1043 SkRegion::kReplace_Op,
1044 invert_fill(clipIn.getPathFill(i)),
1045 clipIn.getDoAA(i),
1046 0x00000000);
1047 }
1048
1049 continue;
1050 }
1051
1052 // The other ops (union, xor, diff) only affect pixels inside
1053 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001054 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001055
1056 helper.draw(clipIn.getRect(i),
1057 op,
1058 clipIn.getDoAA(i), SK_ColorWHITE);
1059
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001060 } else {
1061 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1062
robertphillips@google.comfa662942012-05-17 12:20:22 +00001063 helper.draw(clipIn.getPath(i),
1064 op,
1065 clipIn.getPathFill(i),
1066 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001067 }
1068 }
1069
robertphillips@google.comfa662942012-05-17 12:20:22 +00001070 // Because we are using the scratch texture cache, "accum" may be
1071 // larger than expected and have some cruft in the areas we aren't using.
1072 // Clear it out.
1073
1074 // TODO: need a simpler way to clear the texture - can we combine
1075 // the clear and the writePixels (inside toTexture)
1076 GrDrawState* drawState = gpu->drawState();
1077 GrAssert(NULL != drawState);
1078 GrRenderTarget* temp = drawState->getRenderTarget();
1079 clear(gpu, accum, 0x00000000);
1080 // can't leave the accum bound as a rendertarget
1081 drawState->setRenderTarget(temp);
1082
1083 helper.toTexture(accum);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001084
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001085 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001086
1087 return true;
1088}
1089
robertphillips@google.comf294b772012-04-27 14:29:26 +00001090////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001091void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001092 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001093}