blob: ee1bcaeb030576469a30af5179172dc12fb8a52f [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,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000064 GrGpu* gpu,
65 const SkPath& path,
66 GrPathFill fill,
67 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000068 // 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 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000079bool GrClipMaskManager::useSWOnlyPath(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) &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000102 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000103 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
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000116bool GrClipMaskManager::createClipMask(const GrClip& clipIn,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000117 ScissoringSettings* scissorSettings) {
118
119 GrAssert(scissorSettings);
120
121 scissorSettings->fEnableScissoring = false;
122 fClipMaskInStencil = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000123 fClipMaskInAlpha = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000124
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000125 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000126 if (!drawState->isClipState()) {
127 return true;
128 }
129
130 GrRenderTarget* rt = drawState->getRenderTarget();
131
132 // GrDrawTarget should have filtered this for us
133 GrAssert(NULL != rt);
134
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000135#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000136 // If MSAA is enabled we can do everything in the stencil buffer.
137 // Otherwise check if we should just create the entire clip mask
138 // in software (this will only happen if the clip mask is anti-aliased
139 // and too complex for the gpu to handle in its entirety)
140 if (0 == rt->numSamples() && useSWOnlyPath(gpu, clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000141 // The clip geometry is complex enough that it will be more
142 // efficient to create it entirely in software
143 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000144 GrIRect bound;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000145 if (this->createSoftwareClipMask(fGpu, clipIn, &result, &bound)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000146 fClipMaskInAlpha = true;
147
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000148 setup_drawstate_aaclip(fGpu, result, bound);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000149 return true;
150 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000151
152 // if SW clip mask creation fails fall through to the other
153 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000154 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000155#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000156
robertphillips@google.comf294b772012-04-27 14:29:26 +0000157#if GR_AA_CLIP
158 // If MSAA is enabled use the (faster) stencil path for AA clipping
159 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000160 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000161 // Since we are going to create a destination texture of the correct
162 // size for the mask (rather than being bound by the size of the
163 // render target) we aren't going to use scissoring like the stencil
164 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000165 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000166 GrIRect bound;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000167 if (this->createAlphaClipMask(fGpu, clipIn, &result, &bound)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000168 fClipMaskInAlpha = true;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000169
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000170 setup_drawstate_aaclip(fGpu, result, bound);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000171 return true;
172 }
173
174 // if alpha clip mask creation fails fall through to the stencil
175 // buffer method
176 }
177#endif // GR_AA_CLIP
178
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000179 // Either a hard (stencil buffer) clip was explicitly requested or
180 // an antialiased clip couldn't be created. In either case, free up
181 // the texture in the antialiased mask cache.
182 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000183 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
184 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000185 // AA cache.
186 fAACache.reset();
187
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000188 GrRect bounds;
189 GrRect rtRect;
190 rtRect.setLTRB(0, 0,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000191 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000192 if (clipIn.hasConservativeBounds()) {
193 bounds = clipIn.getConservativeBounds();
194 if (!bounds.intersect(rtRect)) {
195 bounds.setEmpty();
196 }
197 } else {
198 bounds = rtRect;
199 }
200
201 bounds.roundOut(&scissorSettings->fScissorRect);
202 if (scissorSettings->fScissorRect.isEmpty()) {
203 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
204 // TODO: I think we can do an early exit here - after refactoring try:
205 // set fEnableScissoring to true but leave fClipMaskInStencil false
206 // and return - everything is going to be scissored away anyway!
207 }
208 scissorSettings->fEnableScissoring = true;
209
210 // use the stencil clip if we can't represent the clip as a rectangle.
211 fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
212 !bounds.isEmpty();
213
214 if (fClipMaskInStencil) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000215 return this->createStencilClipMask(clipIn, bounds, scissorSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000216 }
217
218 return true;
219}
220
221#define VISUALIZE_COMPLEX_CLIP 0
222
223#if VISUALIZE_COMPLEX_CLIP
224 #include "GrRandom.h"
225 GrRandom gRandom;
226 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
227#else
228 #define SET_RANDOM_COLOR
229#endif
230
231namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000232/**
233 * Does "container" contain "containee"? If either is empty then
234 * no containment is possible.
235 */
236bool contains(const SkRect& container, const SkIRect& containee) {
237 return !containee.isEmpty() && !container.isEmpty() &&
238 container.fLeft <= SkIntToScalar(containee.fLeft) &&
239 container.fTop <= SkIntToScalar(containee.fTop) &&
240 container.fRight >= SkIntToScalar(containee.fRight) &&
241 container.fBottom >= SkIntToScalar(containee.fBottom);
242}
243
244
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000246// determines how many elements at the head of the clip can be skipped and
247// whether the initial clear should be to the inside- or outside-the-clip value,
248// and what op should be used to draw the first element that isn't skipped.
249int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000250 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000251 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000252 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000253
254 // logically before the first element of the clip stack is
255 // processed the clip is entirely open. However, depending on the
256 // first set op we may prefer to clear to 0 for performance. We may
257 // also be able to skip the initial clip paths/rects. We loop until
258 // we cannot skip an element.
259 int curr;
260 bool done = false;
261 *clearToInside = true;
262 int count = clip.getElementCount();
263
264 for (curr = 0; curr < count && !done; ++curr) {
265 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000266 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000267 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000268 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000269 *clearToInside = false;
270 done = true;
271 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000272 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000273 // if this element contains the entire bounds then we
274 // can skip it.
275 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000276 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000277 break;
278 }
279 // if everything is initially clearToInside then intersect is
280 // same as clear to 0 and treat as a replace. Otherwise,
281 // set stays empty.
282 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000283 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284 *clearToInside = false;
285 done = true;
286 }
287 break;
288 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000289 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000290 // if everything is initially outside then union is
291 // same as replace. Otherwise, every pixel is still
292 // clearToInside
293 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000294 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 done = true;
296 }
297 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000298 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000299 // xor is same as difference or replace both of which
300 // can be 1-pass instead of 2 for xor.
301 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000302 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000303 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000304 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305 }
306 done = true;
307 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000308 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000309 // if all pixels are clearToInside then we have to process the
310 // difference, otherwise it has no effect and all pixels
311 // remain outside.
312 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000313 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000314 done = true;
315 }
316 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000317 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000318 // if all pixels are clearToInside then reverse difference
319 // produces empty set. Otherise it is same as replace
320 if (*clearToInside) {
321 *clearToInside = false;
322 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000323 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000324 done = true;
325 }
326 break;
327 default:
328 GrCrash("Unknown set op.");
329 }
330 }
331 return done ? curr-1 : count;
332}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000333
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000334}
335
robertphillips@google.comf294b772012-04-27 14:29:26 +0000336
337namespace {
338
339////////////////////////////////////////////////////////////////////////////////
340// set up the OpenGL blend function to perform the specified
341// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000342void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343
344 switch (op) {
345 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000346 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000347 break;
348 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000349 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000350 break;
351 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000352 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353 break;
354 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000355 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356 break;
357 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000358 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359 break;
360 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000361 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362 break;
363 default:
364 GrAssert(false);
365 break;
366 }
367}
368
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000370bool draw_path(GrContext* context,
371 GrGpu* gpu,
372 const SkPath& path,
373 GrPathFill fill,
374 bool doAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000375
robertphillips@google.com72176b22012-05-23 13:19:12 +0000376 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 if (NULL == pr) {
378 return false;
379 }
380
381 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
382 return true;
383}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000384
385}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386
387////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000388bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 const GrClip& clipIn,
390 int index) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000391 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392 GrAssert(NULL != drawState);
393
394 drawState->setRenderTarget(target->asRenderTarget());
395
396 if (kRect_ClipType == clipIn.getElementType(index)) {
397 if (clipIn.getDoAA(index)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000398 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000399 clipIn.getRect(index),
400 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000401 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000402 fGpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403 }
404 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000405 return draw_path(this->getContext(), fGpu,
robertphillips@google.com2c756812012-05-22 20:28:23 +0000406 clipIn.getPath(index),
407 clipIn.getPathFill(index),
408 clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000409 }
410 return true;
411}
412
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000413void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000414 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000415 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000416 GrAssert(NULL != drawState);
417
418 // no AA here since it is encoded in the texture
419 drawState->setRenderTarget(target->asRenderTarget());
420
421 GrMatrix sampleM;
422 sampleM.setIDiv(texture->width(), texture->height());
423 drawState->setTexture(0, texture);
424
425 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
426 GrSamplerState::kNearest_Filter,
427 sampleM);
428
robertphillips@google.comf105b102012-05-14 12:18:26 +0000429 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
430 SkIntToScalar(target->height()));
431
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000432 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433
434 drawState->setTexture(0, NULL);
435}
436
437namespace {
438
439void clear(GrGpu* gpu,
440 GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441 GrColor color) {
442 GrDrawState* drawState = gpu->drawState();
443 GrAssert(NULL != drawState);
444
445 // zap entire target to specified color
446 drawState->setRenderTarget(target->asRenderTarget());
447 gpu->clear(NULL, color);
448}
449
robertphillips@google.comf105b102012-05-14 12:18:26 +0000450}
451
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000452// get a texture to act as a temporary buffer for AA clip boolean operations
453// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000454void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000455 GrAutoScratchTexture* temp) {
456 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000457 // we've already allocated the temp texture
458 return;
459 }
460
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000461 GrTextureDesc desc;
462 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
463 desc.fWidth = bounds.width();
464 desc.fHeight = bounds.height();
465 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000466
robertphillips@google.com2c756812012-05-22 20:28:23 +0000467 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000468}
469
robertphillips@google.comf105b102012-05-14 12:18:26 +0000470
471void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000472 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000473 // Since we are setting up the cache we know the last lookup was a miss
474 // Free up the currently cached mask so it can be reused
475 fAACache.reset();
476
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000477 GrTextureDesc desc;
478 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
479 desc.fWidth = bounds.width();
480 desc.fHeight = bounds.height();
481 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000482
483 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000484}
485
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000486////////////////////////////////////////////////////////////////////////////////
487// Shared preamble between gpu and SW-only AA clip mask creation paths.
488// Handles caching, determination of clip mask bound & allocation (if needed)
489// of the result texture
490// Returns true if there is no more work to be done (i.e., we got a cache hit)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000491bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000492 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000493 GrIRect *resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000494 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495 GrAssert(origDrawState->isClipState());
496
497 GrRenderTarget* rt = origDrawState->getRenderTarget();
498 GrAssert(NULL != rt);
499
500 GrRect rtRect;
501 rtRect.setLTRB(0, 0,
502 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
503
504 // unlike the stencil path the alpha path is not bound to the size of the
505 // render target - determine the minimum size required for the mask
506 GrRect bounds;
507
508 if (clipIn.hasConservativeBounds()) {
509 bounds = clipIn.getConservativeBounds();
510 if (!bounds.intersect(rtRect)) {
511 // the mask will be empty in this case
512 GrAssert(false);
513 bounds.setEmpty();
514 }
515 } else {
516 // still locked to the size of the render target
517 bounds = rtRect;
518 }
519
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000520 GrIRect intBounds;
521 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000522
523 // need to outset a pixel since the standard bounding box computation
524 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000525 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000526
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000527 // TODO: make sure we don't outset if bounds are still 0,0 @ min
528
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000529 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000530 intBounds.width(),
531 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000532 *result = fAACache.getLastMask();
533 fAACache.getLastBound(resultBounds);
534 return true;
535 }
536
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000537 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000538
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000539 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000540 return false;
541}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000542
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000543////////////////////////////////////////////////////////////////////////////////
544// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000545bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000547 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000548
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000549 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000550 return true;
551 }
552
robertphillips@google.comf105b102012-05-14 12:18:26 +0000553 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000554 if (NULL == accum) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000556 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000557 return false;
558 }
559
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000560 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
561 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000563 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000564
565 int count = clipIn.getElementCount();
566
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000567 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000568 // if we were able to trim down the size of the mask we need to
569 // offset the paths & rects that will be used to compute it
570 GrMatrix m;
571
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000572 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
573 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000574
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000575 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000576 }
577
578 bool clearToInside;
579 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
580 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000581 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000582 &clearToInside,
583 &startOp);
584
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000585 clear(fGpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586
robertphillips@google.comf105b102012-05-14 12:18:26 +0000587 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000588
robertphillips@google.comf294b772012-04-27 14:29:26 +0000589 // walk through each clip element and perform its set op
590 for (int c = start; c < count; ++c) {
591
592 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
593
594 if (SkRegion::kReplace_Op == op) {
595 // TODO: replace is actually a lot faster then intersection
596 // for this path - refactor the stencil path so it can handle
597 // replace ops and alter GrClip to allow them through
598
599 // clear the accumulator and draw the new object directly into it
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000600 clear(fGpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000601
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000602 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000603 this->drawClipShape(accum, clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000604
605 } else if (SkRegion::kReverseDifference_Op == op ||
606 SkRegion::kIntersect_Op == op) {
607 // there is no point in intersecting a screen filling rectangle.
608 if (SkRegion::kIntersect_Op == op &&
609 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000610 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611 continue;
612 }
613
robertphillips@google.comf105b102012-05-14 12:18:26 +0000614 getTemp(*resultBounds, &temp);
615 if (NULL == temp.texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000616 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000617 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000618 return false;
619 }
620
robertphillips@google.comf294b772012-04-27 14:29:26 +0000621 // clear the temp target & draw into it
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000622 clear(fGpu, temp.texture(), 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000623
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000624 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000625 this->drawClipShape(temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626
627 // TODO: rather than adding these two translations here
628 // compute the bounding box needed to render the texture
629 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000630 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000631 GrMatrix m;
632
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000633 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
634 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000635
636 drawState->preConcatViewMatrix(m);
637 }
638
639 // Now draw into the accumulator using the real operation
640 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000641 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000642 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000643
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000644 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000645 GrMatrix m;
646
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000647 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
648 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000649
650 drawState->preConcatViewMatrix(m);
651 }
652
653 } else {
654 // all the remaining ops can just be directly draw into
655 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000656 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000657 this->drawClipShape(accum, clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000658 }
659 }
660
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000661 *result = accum;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000662
robertphillips@google.comf294b772012-04-27 14:29:26 +0000663 return true;
664}
665
666////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000668bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000669 const GrRect& bounds,
670 ScissoringSettings* scissorSettings) {
671
672 GrAssert(fClipMaskInStencil);
673
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000674 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000675 GrAssert(drawState->isClipState());
676
677 GrRenderTarget* rt = drawState->getRenderTarget();
678 GrAssert(NULL != rt);
679
680 // TODO: dynamically attach a SB when needed.
681 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
682 if (NULL == stencilBuffer) {
683 return false;
684 }
685
686 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
687
688 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
689
690 // we set the current clip to the bounds so that our recursive
691 // draws are scissored to them. We use the copy of the complex clip
692 // we just stashed on the SB to render from. We set it back after
693 // we finish drawing it into the stencil.
694 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000695 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000697 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
698 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000700 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000702 fGpu->disableScissor();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000703#if !VISUALIZE_COMPLEX_CLIP
704 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
705#endif
706
707 int count = clipCopy.getElementCount();
708 int clipBit = stencilBuffer->bits();
709 SkASSERT((clipBit <= 16) &&
710 "Ganesh only handles 16b or smaller stencil buffers");
711 clipBit = (1 << (clipBit-1));
712
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000713 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714
715 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000716 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717 int start = process_initial_clip_elements(clipCopy,
718 rtRect,
719 &clearToInside,
720 &startOp);
721
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000722 fGpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723
724 // walk through each clip element and perform its set op
725 // with the existing clip.
726 for (int c = start; c < count; ++c) {
727 GrPathFill fill;
728 bool fillInverted;
729 // enabled at bottom of loop
730 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
731
732 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000733 // directly to the stencil buffer
734 // with a non-inverted fill rule
735 // without extra passes to
736 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737
robertphillips@google.comf294b772012-04-27 14:29:26 +0000738 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
739
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000741 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 if (kRect_ClipType == clipCopy.getElementType(c)) {
743 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000744 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745 fillInverted = false;
746 // there is no point in intersecting a screen filling
747 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000748 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000749 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750 continue;
751 }
752 } else {
753 fill = clipCopy.getPathFill(c);
754 fillInverted = GrIsFillInverted(fill);
755 fill = GrNonInvertedFill(fill);
756 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000757 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000758 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000759 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760 if (NULL == pr) {
761 fClipMaskInStencil = false;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000762 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000763 return false;
764 }
765 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000766 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000767 }
768
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769 int passes;
770 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
771
772 bool canDrawDirectToClip; // Given the renderer, the element,
773 // fill rule, and set operation can
774 // we render the element directly to
775 // stencil bit used for clipping.
776 canDrawDirectToClip =
777 GrStencilSettings::GetClipPasses(op,
778 canRenderDirectToStencil,
779 clipBit,
780 fillInverted,
781 &passes, stencilSettings);
782
783 // draw the element to the client stencil bits if necessary
784 if (!canDrawDirectToClip) {
785 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
786 kIncClamp_StencilOp,
787 kIncClamp_StencilOp,
788 kAlways_StencilFunc,
789 0xffff,
790 0x0000,
791 0xffff);
792 SET_RANDOM_COLOR
793 if (kRect_ClipType == clipCopy.getElementType(c)) {
794 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000795 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000796 } else {
797 if (canRenderDirectToStencil) {
798 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000799 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000801 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802 }
803 }
804 }
805
806 // now we modify the clip bit by rendering either the clip
807 // element directly or a bounding rect of the entire clip.
808 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
809 for (int p = 0; p < passes; ++p) {
810 *drawState->stencil() = stencilSettings[p];
811 if (canDrawDirectToClip) {
812 if (kRect_ClipType == clipCopy.getElementType(c)) {
813 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000814 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000815 } else {
816 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000817 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 }
819 } else {
820 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000821 fGpu->drawSimpleRect(bounds, NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 }
823 }
824 }
825 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000826 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000827 // recusive draws would have disabled this since they drew with
828 // the clip bounds as clip.
829 fClipMaskInStencil = true;
830 }
831
832 return true;
833}
834
bsalomon@google.com411dad02012-06-05 20:24:20 +0000835// mapping of clip-respecting stencil funcs to normal stencil funcs
836// mapping depends on whether stencil-clipping is in effect.
837static const GrStencilFunc
838 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
839 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
840 // In the Clip Funcs
841 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
842 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
843 kLess_StencilFunc, // kLessIfInClip_StencilFunc
844 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
845 // Special in the clip func that forces user's ref to be 0.
846 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
847 // make ref 0 and do normal nequal.
848 },
849 {// Stencil-Clipping is ENABLED
850 // In the Clip Funcs
851 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
852 // eq stencil clip bit, mask
853 // out user bits.
854
855 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
856 // add stencil bit to mask and ref
857
858 kLess_StencilFunc, // kLessIfInClip_StencilFunc
859 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
860 // for both of these we can add
861 // the clip bit to the mask and
862 // ref and compare as normal
863 // Special in the clip func that forces user's ref to be 0.
864 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
865 // make ref have only the clip bit set
866 // and make comparison be less
867 // 10..0 < 1..user_bits..
868 }
869};
870
871GrStencilFunc GrClipMaskManager::adjustStencilParams(GrStencilFunc func,
872 StencilClipMode mode,
873 unsigned int stencilBitCnt,
874 unsigned int* ref,
875 unsigned int* mask,
876 unsigned int* writeMask) {
877 GrAssert(stencilBitCnt > 0);
878 GrAssert((unsigned) func < kStencilFuncCount);
879
880 if (kModifyClip_StencilClipMode == mode) {
881 // We assume that this class is the client/draw-caller of the GrGpu and
882 // has already setup the correct values
883 return func;
884 }
885 unsigned int clipBit = (1 << (stencilBitCnt - 1));
886 unsigned int userBits = clipBit - 1;
887
888 *writeMask &= userBits;
889
890 if (func >= kBasicStencilFuncCount) {
891 int respectClip = kRespectClip_StencilClipMode == mode;
892 if (respectClip) {
893 // The GrGpu class should have checked this
894 GrAssert(this->isClipInStencil());
895 switch (func) {
896 case kAlwaysIfInClip_StencilFunc:
897 *mask = clipBit;
898 *ref = clipBit;
899 break;
900 case kEqualIfInClip_StencilFunc:
901 case kLessIfInClip_StencilFunc:
902 case kLEqualIfInClip_StencilFunc:
903 *mask = (*mask & userBits) | clipBit;
904 *ref = (*ref & userBits) | clipBit;
905 break;
906 case kNonZeroIfInClip_StencilFunc:
907 *mask = (*mask & userBits) | clipBit;
908 *ref = clipBit;
909 break;
910 default:
911 GrCrash("Unknown stencil func");
912 }
913 } else {
914 *mask &= userBits;
915 *ref &= userBits;
916 }
917 const GrStencilFunc* table = gSpecialToBasicStencilFunc[respectClip];
918 func = table[func - kBasicStencilFuncCount];
919 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
920 } else {
921 *mask &= userBits;
922 *ref &= userBits;
923 }
924 return func;
925}
926
927////////////////////////////////////////////////////////////////////////////////
928
robertphillips@google.comfa662942012-05-17 12:20:22 +0000929namespace {
930
931GrPathFill invert_fill(GrPathFill fill) {
932 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000933 kInverseWinding_GrPathFill, // kWinding_GrPathFill
934 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
935 kWinding_GrPathFill, // kInverseWinding_GrPathFill
936 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
937 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +0000938 };
bsalomon@google.com47059542012-06-06 20:51:20 +0000939 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
940 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
941 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
942 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
943 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
944 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000945 return gInvertedFillTable[fill];
946}
947
948}
949
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000950bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000951 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000952 GrIRect *resultBounds) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000953
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000954 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000955 return true;
956 }
957
robertphillips@google.comf105b102012-05-14 12:18:26 +0000958 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000959 if (NULL == accum) {
960 fClipMaskInAlpha = false;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000961 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000962 return false;
963 }
964
robertphillips@google.com2c756812012-05-22 20:28:23 +0000965 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000966
robertphillips@google.comfa662942012-05-17 12:20:22 +0000967 helper.init(*resultBounds, NULL, false);
968
969 int count = clipIn.getElementCount();
970
971 bool clearToInside;
972 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
973 int start = process_initial_clip_elements(clipIn,
974 *resultBounds,
975 &clearToInside,
976 &startOp);
977
978 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
979
980 for (int i = start; i < count; ++i) {
981
982 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
983
984 if (SkRegion::kIntersect_Op == op ||
985 SkRegion::kReverseDifference_Op == op) {
986 // Intersect and reverse difference require modifying pixels
987 // outside of the geometry that is being "drawn". In both cases
988 // we erase all the pixels outside of the geometry but
989 // leave the pixels inside the geometry alone. For reverse
990 // difference we invert all the pixels before clearing the ones
991 // outside the geometry.
992 if (SkRegion::kReverseDifference_Op == op) {
993 SkRect temp = SkRect::MakeLTRB(
994 SkIntToScalar(resultBounds->left()),
995 SkIntToScalar(resultBounds->top()),
996 SkIntToScalar(resultBounds->right()),
997 SkIntToScalar(resultBounds->bottom()));
998
999 // invert the entire scene
1000 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
1001 }
1002
1003 if (kRect_ClipType == clipIn.getElementType(i)) {
1004
1005 // convert the rect to a path so we can invert the fill
1006 SkPath temp;
1007 temp.addRect(clipIn.getRect(i));
1008
1009 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001010 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001011 0x00000000);
1012 } else {
1013 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1014
1015 helper.draw(clipIn.getPath(i),
1016 SkRegion::kReplace_Op,
1017 invert_fill(clipIn.getPathFill(i)),
1018 clipIn.getDoAA(i),
1019 0x00000000);
1020 }
1021
1022 continue;
1023 }
1024
1025 // The other ops (union, xor, diff) only affect pixels inside
1026 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001027 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001028
1029 helper.draw(clipIn.getRect(i),
1030 op,
1031 clipIn.getDoAA(i), SK_ColorWHITE);
1032
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001033 } else {
1034 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1035
robertphillips@google.comfa662942012-05-17 12:20:22 +00001036 helper.draw(clipIn.getPath(i),
1037 op,
1038 clipIn.getPathFill(i),
1039 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001040 }
1041 }
1042
robertphillips@google.comfa662942012-05-17 12:20:22 +00001043 // Because we are using the scratch texture cache, "accum" may be
1044 // larger than expected and have some cruft in the areas we aren't using.
1045 // Clear it out.
1046
1047 // TODO: need a simpler way to clear the texture - can we combine
1048 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001049 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001050 GrAssert(NULL != drawState);
1051 GrRenderTarget* temp = drawState->getRenderTarget();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001052 clear(fGpu, accum, 0x00000000);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001053 // can't leave the accum bound as a rendertarget
1054 drawState->setRenderTarget(temp);
1055
1056 helper.toTexture(accum);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001057
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001058 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001059
1060 return true;
1061}
1062
robertphillips@google.comf294b772012-04-27 14:29:26 +00001063////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001064void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001065 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001066}