blob: cb543e1979f28840d89d26fa957a3f81a877c102 [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"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000019
20//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000021//#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000022
robertphillips@google.comf294b772012-04-27 14:29:26 +000023////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000024namespace {
25// set up the draw state to enable the aa clipping mask. Besides setting up the
26// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000027void setup_drawstate_aaclip(GrGpu* gpu,
28 GrTexture* result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000029 const GrIRect &bound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000030 GrDrawState* drawState = gpu->drawState();
31 GrAssert(drawState);
32
33 static const int maskStage = GrPaint::kTotalStages+1;
34
35 GrMatrix mat;
36 mat.setIDiv(result->width(), result->height());
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000037 mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038 mat.preConcat(drawState->getViewMatrix());
39
40 drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode,
41 GrSamplerState::kNearest_Filter,
42 mat);
43
44 drawState->setTexture(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045}
46
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000047bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000048 GrGpu* gpu,
49 const SkPath& path,
50 GrPathFill fill,
51 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000052 // last (false) parameter disallows use of the SW path renderer
53 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
54}
55
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000056}
57
robertphillips@google.comfa662942012-05-17 12:20:22 +000058/*
59 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
60 * will be used on any element. If so, it returns true to indicate that the
61 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
62 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000063bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000064
65 if (!clipIn.requiresAA()) {
66 // The stencil buffer can handle this case
67 return false;
68 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000069
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000070 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000071 // a clip gets complex enough it can just be done in SW regardless
72 // of whether it would invoke the GrSoftwarePathRenderer.
73 bool useSW = false;
74
75 for (int i = 0; i < clipIn.getElementCount(); ++i) {
76
77 if (SkRegion::kReplace_Op == clipIn.getOp(i)) {
78 // Everything before a replace op can be ignored so start
79 // afresh w.r.t. determining if any element uses the SW path
80 useSW = false;
81 }
82
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000083 // rects can always be drawn directly w/o using the software path
84 // so only paths need to be checked
85 if (kPath_ClipType == clipIn.getElementType(i) &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000086 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000087 clipIn.getPath(i),
88 clipIn.getPathFill(i),
89 clipIn.getDoAA(i))) {
90 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +000091 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000092 }
93
94 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000095}
96
robertphillips@google.comf294b772012-04-27 14:29:26 +000097////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000098// sort out what kind of clip mask needs to be created: alpha, stencil,
99// scissor, or entirely software
bsalomon@google.coma3201942012-06-21 19:58:20 +0000100bool GrClipMaskManager::setupClipping(const GrClip& clipIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000101 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000102
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000103 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000104 if (!drawState->isClipState() || clipIn.isEmpty()) {
105 fGpu->disableScissor();
106 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000107 return true;
108 }
109
110 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000111 // GrDrawTarget should have filtered this for us
112 GrAssert(NULL != rt);
113
bsalomon@google.coma3201942012-06-21 19:58:20 +0000114 GrIRect bounds;
115 GrIRect rtRect;
116 rtRect.setLTRB(0, 0, rt->width(), rt->height());
117 if (clipIn.hasConservativeBounds()) {
118 GrRect softBounds = clipIn.getConservativeBounds();
119 softBounds.roundOut(&bounds);
120 if (!bounds.intersect(rtRect)) {
121 bounds.setEmpty();
122 }
123 if (bounds.isEmpty()) {
124 return false;
125 }
126 } else {
127 bounds = rtRect;
128 }
129
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000130#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000131 // If MSAA is enabled we can do everything in the stencil buffer.
132 // Otherwise check if we should just create the entire clip mask
133 // in software (this will only happen if the clip mask is anti-aliased
134 // and too complex for the gpu to handle in its entirety)
bsalomon@google.coma3201942012-06-21 19:58:20 +0000135 if (0 == rt->numSamples() && this->useSWOnlyPath(clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000136 // The clip geometry is complex enough that it will be more
137 // efficient to create it entirely in software
138 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000139 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000140 if (this->createSoftwareClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000141 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000142 fGpu->disableScissor();
143 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000144 return true;
145 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000146
147 // if SW clip mask creation fails fall through to the other
148 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000149 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000150#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000151
robertphillips@google.comf294b772012-04-27 14:29:26 +0000152#if GR_AA_CLIP
153 // If MSAA is enabled use the (faster) stencil path for AA clipping
154 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000155 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000156 // Since we are going to create a destination texture of the correct
157 // size for the mask (rather than being bound by the size of the
158 // render target) we aren't going to use scissoring like the stencil
159 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000160 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000161 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000162 if (this->createAlphaClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000163 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000164 fGpu->disableScissor();
165 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000166 return true;
167 }
168
169 // if alpha clip mask creation fails fall through to the stencil
170 // buffer method
171 }
172#endif // GR_AA_CLIP
173
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000174 // Either a hard (stencil buffer) clip was explicitly requested or
175 // an antialiased clip couldn't be created. In either case, free up
176 // the texture in the antialiased mask cache.
177 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000178 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
179 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000180 // AA cache.
181 fAACache.reset();
182
bsalomon@google.coma3201942012-06-21 19:58:20 +0000183 // If the clip is a rectangle then just set the scissor. Otherwise, create
184 // a stencil mask.
185 if (clipIn.isRect()) {
186 fGpu->enableScissor(bounds);
187 this->setGpuStencil();
188 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000189 }
190
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000191 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000192 bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
193 !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000194
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000195 if (useStencil) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000196 this->createStencilClipMask(clipIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000197 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 // This must occur after createStencilClipMask. That function may change
199 // the scissor. Also, it only guarantees that the stencil mask is correct
200 // within the bounds it was passed, so we must use both stencil and scissor
201 // test to the bounds for the final draw.
202 fGpu->enableScissor(bounds);
203 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000204 return true;
205}
206
207#define VISUALIZE_COMPLEX_CLIP 0
208
209#if VISUALIZE_COMPLEX_CLIP
210 #include "GrRandom.h"
211 GrRandom gRandom;
212 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
213#else
214 #define SET_RANDOM_COLOR
215#endif
216
217namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000218/**
219 * Does "container" contain "containee"? If either is empty then
220 * no containment is possible.
221 */
222bool contains(const SkRect& container, const SkIRect& containee) {
223 return !containee.isEmpty() && !container.isEmpty() &&
224 container.fLeft <= SkIntToScalar(containee.fLeft) &&
225 container.fTop <= SkIntToScalar(containee.fTop) &&
226 container.fRight >= SkIntToScalar(containee.fRight) &&
227 container.fBottom >= SkIntToScalar(containee.fBottom);
228}
229
230
robertphillips@google.comf294b772012-04-27 14:29:26 +0000231////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232// determines how many elements at the head of the clip can be skipped and
233// whether the initial clear should be to the inside- or outside-the-clip value,
234// and what op should be used to draw the first element that isn't skipped.
235int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000236 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000237 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000238 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000239
240 // logically before the first element of the clip stack is
241 // processed the clip is entirely open. However, depending on the
242 // first set op we may prefer to clear to 0 for performance. We may
243 // also be able to skip the initial clip paths/rects. We loop until
244 // we cannot skip an element.
245 int curr;
246 bool done = false;
247 *clearToInside = true;
248 int count = clip.getElementCount();
249
250 for (curr = 0; curr < count && !done; ++curr) {
251 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000252 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000253 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000254 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000255 *clearToInside = false;
256 done = true;
257 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000258 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000259 // if this element contains the entire bounds then we
260 // can skip it.
261 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000262 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000263 break;
264 }
265 // if everything is initially clearToInside then intersect is
266 // same as clear to 0 and treat as a replace. Otherwise,
267 // set stays empty.
268 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000269 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000270 *clearToInside = false;
271 done = true;
272 }
273 break;
274 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000275 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276 // if everything is initially outside then union is
277 // same as replace. Otherwise, every pixel is still
278 // clearToInside
279 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000280 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281 done = true;
282 }
283 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000284 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000285 // xor is same as difference or replace both of which
286 // can be 1-pass instead of 2 for xor.
287 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000288 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000290 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 }
292 done = true;
293 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000294 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 // if all pixels are clearToInside then we have to process the
296 // difference, otherwise it has no effect and all pixels
297 // remain outside.
298 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000299 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 done = true;
301 }
302 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000303 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000304 // if all pixels are clearToInside then reverse difference
305 // produces empty set. Otherise it is same as replace
306 if (*clearToInside) {
307 *clearToInside = false;
308 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000309 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 done = true;
311 }
312 break;
313 default:
314 GrCrash("Unknown set op.");
315 }
316 }
317 return done ? curr-1 : count;
318}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000319
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320}
321
robertphillips@google.comf294b772012-04-27 14:29:26 +0000322
323namespace {
324
325////////////////////////////////////////////////////////////////////////////////
326// set up the OpenGL blend function to perform the specified
327// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000328void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000329
330 switch (op) {
331 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000332 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000333 break;
334 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000335 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000336 break;
337 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000338 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000339 break;
340 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000341 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000342 break;
343 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000344 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000345 break;
346 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000347 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348 break;
349 default:
350 GrAssert(false);
351 break;
352 }
353}
354
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000356bool draw_path(GrContext* context,
357 GrGpu* gpu,
358 const SkPath& path,
359 GrPathFill fill,
360 bool doAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361
robertphillips@google.com72176b22012-05-23 13:19:12 +0000362 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363 if (NULL == pr) {
364 return false;
365 }
366
367 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
368 return true;
369}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000370
371}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372
373////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000374bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000375 const GrClip& clipIn,
376 int index) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000377 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378 GrAssert(NULL != drawState);
379
380 drawState->setRenderTarget(target->asRenderTarget());
381
382 if (kRect_ClipType == clipIn.getElementType(index)) {
383 if (clipIn.getDoAA(index)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000384 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000385 clipIn.getRect(index),
386 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000388 fGpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 }
390 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000391 return draw_path(this->getContext(), fGpu,
robertphillips@google.com2c756812012-05-22 20:28:23 +0000392 clipIn.getPath(index),
393 clipIn.getPathFill(index),
394 clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395 }
396 return true;
397}
398
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000399void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000400 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000401 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402 GrAssert(NULL != drawState);
403
404 // no AA here since it is encoded in the texture
405 drawState->setRenderTarget(target->asRenderTarget());
406
407 GrMatrix sampleM;
408 sampleM.setIDiv(texture->width(), texture->height());
409 drawState->setTexture(0, texture);
410
411 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
412 GrSamplerState::kNearest_Filter,
413 sampleM);
414
robertphillips@google.comf105b102012-05-14 12:18:26 +0000415 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
416 SkIntToScalar(target->height()));
417
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000418 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000419
420 drawState->setTexture(0, NULL);
421}
422
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000423// get a texture to act as a temporary buffer for AA clip boolean operations
424// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000425void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000426 GrAutoScratchTexture* temp) {
427 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000428 // we've already allocated the temp texture
429 return;
430 }
431
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000432 GrTextureDesc desc;
433 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
434 desc.fWidth = bounds.width();
435 desc.fHeight = bounds.height();
436 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000437
robertphillips@google.com2c756812012-05-22 20:28:23 +0000438 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000439}
440
robertphillips@google.comf105b102012-05-14 12:18:26 +0000441
442void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000443 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000444 // Since we are setting up the cache we know the last lookup was a miss
445 // Free up the currently cached mask so it can be reused
446 fAACache.reset();
447
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000448 GrTextureDesc desc;
449 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
450 desc.fWidth = bounds.width();
451 desc.fHeight = bounds.height();
452 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000453
454 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000455}
456
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000457////////////////////////////////////////////////////////////////////////////////
458// Shared preamble between gpu and SW-only AA clip mask creation paths.
459// Handles caching, determination of clip mask bound & allocation (if needed)
460// of the result texture
461// 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 +0000462bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000463 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000464 GrIRect *resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000465 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000466 GrAssert(origDrawState->isClipState());
467
468 GrRenderTarget* rt = origDrawState->getRenderTarget();
469 GrAssert(NULL != rt);
470
471 GrRect rtRect;
472 rtRect.setLTRB(0, 0,
473 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
474
475 // unlike the stencil path the alpha path is not bound to the size of the
476 // render target - determine the minimum size required for the mask
477 GrRect bounds;
478
479 if (clipIn.hasConservativeBounds()) {
480 bounds = clipIn.getConservativeBounds();
481 if (!bounds.intersect(rtRect)) {
482 // the mask will be empty in this case
483 GrAssert(false);
484 bounds.setEmpty();
485 }
486 } else {
487 // still locked to the size of the render target
488 bounds = rtRect;
489 }
490
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000491 GrIRect intBounds;
492 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000493
494 // need to outset a pixel since the standard bounding box computation
495 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000496 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000498 // TODO: make sure we don't outset if bounds are still 0,0 @ min
499
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000500 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000501 intBounds.width(),
502 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000503 *result = fAACache.getLastMask();
504 fAACache.getLastBound(resultBounds);
505 return true;
506 }
507
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000508 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000509
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000510 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000511 return false;
512}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000513
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000514////////////////////////////////////////////////////////////////////////////////
515// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000516bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000517 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000518 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000519
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000520 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
521
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000522 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000523 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000524 return true;
525 }
526
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000528 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000529 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000530 return false;
531 }
532
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000533 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
534 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000535
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000536 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000537
538 int count = clipIn.getElementCount();
539
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000540 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000541 // if we were able to trim down the size of the mask we need to
542 // offset the paths & rects that will be used to compute it
543 GrMatrix m;
544
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000545 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
546 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000547
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000548 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000549 }
550
551 bool clearToInside;
552 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
553 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000554 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555 &clearToInside,
556 &startOp);
557
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000558 fGpu->clear(NULL,
559 clearToInside ? 0xffffffff : 0x00000000,
560 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561
robertphillips@google.comf105b102012-05-14 12:18:26 +0000562 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000563
robertphillips@google.comf294b772012-04-27 14:29:26 +0000564 // walk through each clip element and perform its set op
565 for (int c = start; c < count; ++c) {
566
567 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
568
569 if (SkRegion::kReplace_Op == op) {
570 // TODO: replace is actually a lot faster then intersection
571 // for this path - refactor the stencil path so it can handle
572 // replace ops and alter GrClip to allow them through
573
574 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000575 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000576
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000577 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000578 this->drawClipShape(accum, clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000579
580 } else if (SkRegion::kReverseDifference_Op == op ||
581 SkRegion::kIntersect_Op == op) {
582 // there is no point in intersecting a screen filling rectangle.
583 if (SkRegion::kIntersect_Op == op &&
584 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000585 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586 continue;
587 }
588
robertphillips@google.comf105b102012-05-14 12:18:26 +0000589 getTemp(*resultBounds, &temp);
590 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000591 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000592 return false;
593 }
594
robertphillips@google.comf294b772012-04-27 14:29:26 +0000595 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000596 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000598 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000599 this->drawClipShape(temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000600
601 // TODO: rather than adding these two translations here
602 // compute the bounding box needed to render the texture
603 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000604 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605 GrMatrix m;
606
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000607 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
608 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000609
610 drawState->preConcatViewMatrix(m);
611 }
612
613 // Now draw into the accumulator using the real operation
614 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000615 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000616 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000617
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000618 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000619 GrMatrix m;
620
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000621 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
622 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000623
624 drawState->preConcatViewMatrix(m);
625 }
626
627 } else {
628 // all the remaining ops can just be directly draw into
629 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000630 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000631 this->drawClipShape(accum, clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632 }
633 }
634
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000635 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000636 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000637 return true;
638}
639
640////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000642bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000643 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000644
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000645 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000646
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000647 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648 GrAssert(drawState->isClipState());
649
650 GrRenderTarget* rt = drawState->getRenderTarget();
651 GrAssert(NULL != rt);
652
653 // TODO: dynamically attach a SB when needed.
654 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
655 if (NULL == stencilBuffer) {
656 return false;
657 }
658
659 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
660
661 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
662
663 // we set the current clip to the bounds so that our recursive
664 // draws are scissored to them. We use the copy of the complex clip
665 // we just stashed on the SB to render from. We set it back after
666 // we finish drawing it into the stencil.
667 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000668 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000669
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000670 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
671 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000672 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000673 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000674
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000675#if !VISUALIZE_COMPLEX_CLIP
676 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
677#endif
678
679 int count = clipCopy.getElementCount();
680 int clipBit = stencilBuffer->bits();
681 SkASSERT((clipBit <= 16) &&
682 "Ganesh only handles 16b or smaller stencil buffers");
683 clipBit = (1 << (clipBit-1));
684
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000685 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000686
687 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000688 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 int start = process_initial_clip_elements(clipCopy,
690 rtRect,
691 &clearToInside,
692 &startOp);
693
bsalomon@google.coma3201942012-06-21 19:58:20 +0000694 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000695
696 // walk through each clip element and perform its set op
697 // with the existing clip.
698 for (int c = start; c < count; ++c) {
699 GrPathFill fill;
700 bool fillInverted;
701 // enabled at bottom of loop
702 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
703
704 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000705 // directly to the stencil buffer
706 // with a non-inverted fill rule
707 // without extra passes to
708 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709
robertphillips@google.comf294b772012-04-27 14:29:26 +0000710 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
711
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000713 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 if (kRect_ClipType == clipCopy.getElementType(c)) {
715 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000716 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717 fillInverted = false;
718 // there is no point in intersecting a screen filling
719 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000720 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000721 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 continue;
723 }
724 } else {
725 fill = clipCopy.getPathFill(c);
726 fillInverted = GrIsFillInverted(fill);
727 fill = GrNonInvertedFill(fill);
728 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000729 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000730 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000731 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000733 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734 return false;
735 }
736 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000737 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 }
739
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 int passes;
741 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
742
743 bool canDrawDirectToClip; // Given the renderer, the element,
744 // fill rule, and set operation can
745 // we render the element directly to
746 // stencil bit used for clipping.
747 canDrawDirectToClip =
748 GrStencilSettings::GetClipPasses(op,
749 canRenderDirectToStencil,
750 clipBit,
751 fillInverted,
752 &passes, stencilSettings);
753
754 // draw the element to the client stencil bits if necessary
755 if (!canDrawDirectToClip) {
756 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
757 kIncClamp_StencilOp,
758 kIncClamp_StencilOp,
759 kAlways_StencilFunc,
760 0xffff,
761 0x0000,
762 0xffff);
763 SET_RANDOM_COLOR
764 if (kRect_ClipType == clipCopy.getElementType(c)) {
765 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000766 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000767 } else {
768 if (canRenderDirectToStencil) {
769 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000770 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000771 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000772 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000773 }
774 }
775 }
776
777 // now we modify the clip bit by rendering either the clip
778 // element directly or a bounding rect of the entire clip.
779 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
780 for (int p = 0; p < passes; ++p) {
781 *drawState->stencil() = stencilSettings[p];
782 if (canDrawDirectToClip) {
783 if (kRect_ClipType == clipCopy.getElementType(c)) {
784 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000785 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 } else {
787 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000788 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000789 }
790 } else {
791 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000792 GrRect rect = GrRect::MakeLTRB(
793 SkIntToScalar(bounds.fLeft),
794 SkIntToScalar(bounds.fTop),
795 SkIntToScalar(bounds.fRight),
796 SkIntToScalar(bounds.fBottom));
797 fGpu->drawSimpleRect(rect, NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 }
799 }
800 }
801 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000802 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000804 // set this last because recursive draws may overwrite it back to kNone.
805 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
806 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 return true;
808}
809
bsalomon@google.com411dad02012-06-05 20:24:20 +0000810// mapping of clip-respecting stencil funcs to normal stencil funcs
811// mapping depends on whether stencil-clipping is in effect.
812static const GrStencilFunc
813 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
814 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
815 // In the Clip Funcs
816 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
817 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
818 kLess_StencilFunc, // kLessIfInClip_StencilFunc
819 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
820 // Special in the clip func that forces user's ref to be 0.
821 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
822 // make ref 0 and do normal nequal.
823 },
824 {// Stencil-Clipping is ENABLED
825 // In the Clip Funcs
826 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
827 // eq stencil clip bit, mask
828 // out user bits.
829
830 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
831 // add stencil bit to mask and ref
832
833 kLess_StencilFunc, // kLessIfInClip_StencilFunc
834 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
835 // for both of these we can add
836 // the clip bit to the mask and
837 // ref and compare as normal
838 // Special in the clip func that forces user's ref to be 0.
839 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
840 // make ref have only the clip bit set
841 // and make comparison be less
842 // 10..0 < 1..user_bits..
843 }
844};
845
bsalomon@google.coma3201942012-06-21 19:58:20 +0000846namespace {
847// Sets the settings to clip against the stencil buffer clip while ignoring the
848// client bits.
849const GrStencilSettings& basic_apply_stencil_clip_settings() {
850 // stencil settings to use when clip is in stencil
851 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
852 kKeep_StencilOp,
853 kKeep_StencilOp,
854 kAlwaysIfInClip_StencilFunc,
855 0x0000,
856 0x0000,
857 0x0000);
858 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
859}
860}
861
862void GrClipMaskManager::setGpuStencil() {
863 // We make two copies of the StencilSettings here (except in the early
864 // exit scenario. One copy from draw state to the stack var. Then another
865 // from the stack var to the gpu. We could make this class hold a ptr to
866 // GrGpu's fStencilSettings and eliminate the stack copy here.
867
868 const GrDrawState& drawState = fGpu->getDrawState();
869
870 // use stencil for clipping if clipping is enabled and the clip
871 // has been written into the stencil.
872 GrClipMaskManager::StencilClipMode clipMode;
873 if (this->isClipInStencil() && drawState.isClipState()) {
874 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
875 // We can't be modifying the clip and respecting it at the same time.
876 GrAssert(!drawState.isStateFlagEnabled(
877 GrGpu::kModifyStencilClip_StateBit));
878 } else if (drawState.isStateFlagEnabled(
879 GrGpu::kModifyStencilClip_StateBit)) {
880 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
881 } else {
882 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
883 }
884
885 GrStencilSettings settings;
886 // The GrGpu client may not be using the stencil buffer but we may need to
887 // enable it in order to respect a stencil clip.
888 if (drawState.getStencil().isDisabled()) {
889 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
890 settings = basic_apply_stencil_clip_settings();
891 } else {
892 fGpu->disableStencil();
893 return;
894 }
895 } else {
896 settings = drawState.getStencil();
897 }
898
899 // TODO: dynamically attach a stencil buffer
900 int stencilBits = 0;
901 GrStencilBuffer* stencilBuffer =
902 drawState.getRenderTarget()->getStencilBuffer();
903 if (NULL != stencilBuffer) {
904 stencilBits = stencilBuffer->bits();
905 }
906
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000907 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
908 !settings.usesWrapOp());
909 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000910 this->adjustStencilParams(&settings, clipMode, stencilBits);
911 fGpu->setStencilSettings(settings);
912}
913
914void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
915 StencilClipMode mode,
916 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000917 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000918
919 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920 // We assume that this clip manager itself is drawing to the GrGpu and
921 // has already setup the correct values.
922 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000923 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000924
bsalomon@google.com411dad02012-06-05 20:24:20 +0000925 unsigned int clipBit = (1 << (stencilBitCnt - 1));
926 unsigned int userBits = clipBit - 1;
927
bsalomon@google.coma3201942012-06-21 19:58:20 +0000928 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
929 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000930
bsalomon@google.coma3201942012-06-21 19:58:20 +0000931 bool finished = false;
932 while (!finished) {
933 GrStencilFunc func = settings->func(face);
934 uint16_t writeMask = settings->writeMask(face);
935 uint16_t funcMask = settings->funcMask(face);
936 uint16_t funcRef = settings->funcRef(face);
937
938 GrAssert((unsigned) func < kStencilFuncCount);
939
940 writeMask &= userBits;
941
942 if (func >= kBasicStencilFuncCount) {
943 int respectClip = kRespectClip_StencilClipMode == mode;
944 if (respectClip) {
945 // The GrGpu class should have checked this
946 GrAssert(this->isClipInStencil());
947 switch (func) {
948 case kAlwaysIfInClip_StencilFunc:
949 funcMask = clipBit;
950 funcRef = clipBit;
951 break;
952 case kEqualIfInClip_StencilFunc:
953 case kLessIfInClip_StencilFunc:
954 case kLEqualIfInClip_StencilFunc:
955 funcMask = (funcMask & userBits) | clipBit;
956 funcRef = (funcRef & userBits) | clipBit;
957 break;
958 case kNonZeroIfInClip_StencilFunc:
959 funcMask = (funcMask & userBits) | clipBit;
960 funcRef = clipBit;
961 break;
962 default:
963 GrCrash("Unknown stencil func");
964 }
965 } else {
966 funcMask &= userBits;
967 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000968 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000969 const GrStencilFunc* table =
970 gSpecialToBasicStencilFunc[respectClip];
971 func = table[func - kBasicStencilFuncCount];
972 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000973 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000974 funcMask &= userBits;
975 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000976 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000977
978 settings->setFunc(face, func);
979 settings->setWriteMask(face, writeMask);
980 settings->setFuncMask(face, funcMask);
981 settings->setFuncRef(face, funcRef);
982
983 if (GrStencilSettings::kFront_Face == face) {
984 face = GrStencilSettings::kBack_Face;
985 finished = !twoSided;
986 } else {
987 finished = true;
988 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000989 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000990 if (!twoSided) {
991 settings->copyFrontSettingsToBack();
992 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000993}
994
995////////////////////////////////////////////////////////////////////////////////
996
robertphillips@google.comfa662942012-05-17 12:20:22 +0000997namespace {
998
999GrPathFill invert_fill(GrPathFill fill) {
1000 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001001 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1002 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1003 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1004 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1005 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001006 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001007 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1008 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1009 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1010 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1011 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1012 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001013 return gInvertedFillTable[fill];
1014}
1015
1016}
1017
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001018bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001019 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +00001020 GrIRect *resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001021 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001022
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001023 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001024 return true;
1025 }
1026
robertphillips@google.comf105b102012-05-14 12:18:26 +00001027 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001028 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001029 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001030 return false;
1031 }
1032
robertphillips@google.com2c756812012-05-22 20:28:23 +00001033 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001034
robertphillips@google.comfa662942012-05-17 12:20:22 +00001035 helper.init(*resultBounds, NULL, false);
1036
1037 int count = clipIn.getElementCount();
1038
1039 bool clearToInside;
1040 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
1041 int start = process_initial_clip_elements(clipIn,
1042 *resultBounds,
1043 &clearToInside,
1044 &startOp);
1045
1046 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
1047
1048 for (int i = start; i < count; ++i) {
1049
1050 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
1051
1052 if (SkRegion::kIntersect_Op == op ||
1053 SkRegion::kReverseDifference_Op == op) {
1054 // Intersect and reverse difference require modifying pixels
1055 // outside of the geometry that is being "drawn". In both cases
1056 // we erase all the pixels outside of the geometry but
1057 // leave the pixels inside the geometry alone. For reverse
1058 // difference we invert all the pixels before clearing the ones
1059 // outside the geometry.
1060 if (SkRegion::kReverseDifference_Op == op) {
1061 SkRect temp = SkRect::MakeLTRB(
1062 SkIntToScalar(resultBounds->left()),
1063 SkIntToScalar(resultBounds->top()),
1064 SkIntToScalar(resultBounds->right()),
1065 SkIntToScalar(resultBounds->bottom()));
1066
1067 // invert the entire scene
1068 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
1069 }
1070
1071 if (kRect_ClipType == clipIn.getElementType(i)) {
1072
1073 // convert the rect to a path so we can invert the fill
1074 SkPath temp;
1075 temp.addRect(clipIn.getRect(i));
1076
1077 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001078 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001079 0x00000000);
1080 } else {
1081 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1082
1083 helper.draw(clipIn.getPath(i),
1084 SkRegion::kReplace_Op,
1085 invert_fill(clipIn.getPathFill(i)),
1086 clipIn.getDoAA(i),
1087 0x00000000);
1088 }
1089
1090 continue;
1091 }
1092
1093 // The other ops (union, xor, diff) only affect pixels inside
1094 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001095 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001096
1097 helper.draw(clipIn.getRect(i),
1098 op,
1099 clipIn.getDoAA(i), SK_ColorWHITE);
1100
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001101 } else {
1102 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1103
robertphillips@google.comfa662942012-05-17 12:20:22 +00001104 helper.draw(clipIn.getPath(i),
1105 op,
1106 clipIn.getPathFill(i),
1107 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001108 }
1109 }
1110
robertphillips@google.comfa662942012-05-17 12:20:22 +00001111 // Because we are using the scratch texture cache, "accum" may be
1112 // larger than expected and have some cruft in the areas we aren't using.
1113 // Clear it out.
1114
1115 // TODO: need a simpler way to clear the texture - can we combine
1116 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001117 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001118 GrAssert(NULL != drawState);
1119 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001120 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001121 // can't leave the accum bound as a rendertarget
1122 drawState->setRenderTarget(temp);
1123
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001124 helper.toTexture(accum, clearToInside);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001125
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001126 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001127
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001128 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001129 return true;
1130}
1131
robertphillips@google.comf294b772012-04-27 14:29:26 +00001132////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001133void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001134 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001135}