blob: 0c4353a3e0a05bcf8c8e57af30ca0adbe6ae2d1d [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.coma72eef32012-05-01 17:22:59 +000026namespace {
27// set up the draw state to enable the aa clipping mask. Besides setting up the
28// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000029void setup_drawstate_aaclip(GrGpu* gpu,
30 GrTexture* result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000031 const GrIRect &bound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000032 GrDrawState* drawState = gpu->drawState();
33 GrAssert(drawState);
34
35 static const int maskStage = GrPaint::kTotalStages+1;
36
37 GrMatrix mat;
38 mat.setIDiv(result->width(), result->height());
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000039 mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 mat.preConcat(drawState->getViewMatrix());
41
42 drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode,
43 GrSamplerState::kNearest_Filter,
44 mat);
45
46 drawState->setTexture(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047}
48
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000049bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000050 GrGpu* gpu,
51 const SkPath& path,
52 GrPathFill fill,
53 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000054 // last (false) parameter disallows use of the SW path renderer
55 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
56}
57
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000058}
59
robertphillips@google.comfa662942012-05-17 12:20:22 +000060/*
61 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
62 * will be used on any element. If so, it returns true to indicate that the
63 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
64 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000065bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000066
67 if (!clipIn.requiresAA()) {
68 // The stencil buffer can handle this case
69 return false;
70 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000071
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000072 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000073 // a clip gets complex enough it can just be done in SW regardless
74 // of whether it would invoke the GrSoftwarePathRenderer.
75 bool useSW = false;
76
77 for (int i = 0; i < clipIn.getElementCount(); ++i) {
78
79 if (SkRegion::kReplace_Op == clipIn.getOp(i)) {
80 // Everything before a replace op can be ignored so start
81 // afresh w.r.t. determining if any element uses the SW path
82 useSW = false;
83 }
84
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000085 // rects can always be drawn directly w/o using the software path
86 // so only paths need to be checked
87 if (kPath_ClipType == clipIn.getElementType(i) &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000088 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000089 clipIn.getPath(i),
90 clipIn.getPathFill(i),
91 clipIn.getDoAA(i))) {
92 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +000093 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000094 }
95
96 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000097}
98
robertphillips@google.comf294b772012-04-27 14:29:26 +000099////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000100// sort out what kind of clip mask needs to be created: alpha, stencil,
101// scissor, or entirely software
bsalomon@google.coma3201942012-06-21 19:58:20 +0000102bool GrClipMaskManager::setupClipping(const GrClip& clipIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000103 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000104
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000105 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000106 if (!drawState->isClipState() || clipIn.isEmpty()) {
107 fGpu->disableScissor();
108 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000109 return true;
110 }
111
112 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000113 // GrDrawTarget should have filtered this for us
114 GrAssert(NULL != rt);
115
bsalomon@google.coma3201942012-06-21 19:58:20 +0000116 GrIRect bounds;
117 GrIRect rtRect;
118 rtRect.setLTRB(0, 0, rt->width(), rt->height());
119 if (clipIn.hasConservativeBounds()) {
120 GrRect softBounds = clipIn.getConservativeBounds();
121 softBounds.roundOut(&bounds);
122 if (!bounds.intersect(rtRect)) {
123 bounds.setEmpty();
124 }
125 if (bounds.isEmpty()) {
126 return false;
127 }
128 } else {
129 bounds = rtRect;
130 }
131
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000132#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000133 // If MSAA is enabled we can do everything in the stencil buffer.
134 // Otherwise check if we should just create the entire clip mask
135 // in software (this will only happen if the clip mask is anti-aliased
136 // and too complex for the gpu to handle in its entirety)
bsalomon@google.coma3201942012-06-21 19:58:20 +0000137 if (0 == rt->numSamples() && this->useSWOnlyPath(clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000138 // The clip geometry is complex enough that it will be more
139 // efficient to create it entirely in software
140 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000141 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000142 if (this->createSoftwareClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000143 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000144 fGpu->disableScissor();
145 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000146 return true;
147 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000148
149 // if SW clip mask creation fails fall through to the other
150 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000151 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000152#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000153
robertphillips@google.comf294b772012-04-27 14:29:26 +0000154#if GR_AA_CLIP
155 // If MSAA is enabled use the (faster) stencil path for AA clipping
156 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000157 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000158 // Since we are going to create a destination texture of the correct
159 // size for the mask (rather than being bound by the size of the
160 // render target) we aren't going to use scissoring like the stencil
161 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000162 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000163 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000164 if (this->createAlphaClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000165 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000166 fGpu->disableScissor();
167 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000168 return true;
169 }
170
171 // if alpha clip mask creation fails fall through to the stencil
172 // buffer method
173 }
174#endif // GR_AA_CLIP
175
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000176 // Either a hard (stencil buffer) clip was explicitly requested or
177 // an antialiased clip couldn't be created. In either case, free up
178 // the texture in the antialiased mask cache.
179 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000180 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
181 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000182 // AA cache.
183 fAACache.reset();
184
bsalomon@google.coma3201942012-06-21 19:58:20 +0000185 // If the clip is a rectangle then just set the scissor. Otherwise, create
186 // a stencil mask.
187 if (clipIn.isRect()) {
188 fGpu->enableScissor(bounds);
189 this->setGpuStencil();
190 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000191 }
192
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000193 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000194 bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
195 !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000196
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000197 if (useStencil) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 this->createStencilClipMask(clipIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000199 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000200 // This must occur after createStencilClipMask. That function may change
201 // the scissor. Also, it only guarantees that the stencil mask is correct
202 // within the bounds it was passed, so we must use both stencil and scissor
203 // test to the bounds for the final draw.
204 fGpu->enableScissor(bounds);
205 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000206 return true;
207}
208
209#define VISUALIZE_COMPLEX_CLIP 0
210
211#if VISUALIZE_COMPLEX_CLIP
212 #include "GrRandom.h"
213 GrRandom gRandom;
214 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
215#else
216 #define SET_RANDOM_COLOR
217#endif
218
219namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000220/**
221 * Does "container" contain "containee"? If either is empty then
222 * no containment is possible.
223 */
224bool contains(const SkRect& container, const SkIRect& containee) {
225 return !containee.isEmpty() && !container.isEmpty() &&
226 container.fLeft <= SkIntToScalar(containee.fLeft) &&
227 container.fTop <= SkIntToScalar(containee.fTop) &&
228 container.fRight >= SkIntToScalar(containee.fRight) &&
229 container.fBottom >= SkIntToScalar(containee.fBottom);
230}
231
232
robertphillips@google.comf294b772012-04-27 14:29:26 +0000233////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000234// determines how many elements at the head of the clip can be skipped and
235// whether the initial clear should be to the inside- or outside-the-clip value,
236// and what op should be used to draw the first element that isn't skipped.
237int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000238 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000239 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000240 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000241
242 // logically before the first element of the clip stack is
243 // processed the clip is entirely open. However, depending on the
244 // first set op we may prefer to clear to 0 for performance. We may
245 // also be able to skip the initial clip paths/rects. We loop until
246 // we cannot skip an element.
247 int curr;
248 bool done = false;
249 *clearToInside = true;
250 int count = clip.getElementCount();
251
252 for (curr = 0; curr < count && !done; ++curr) {
253 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000254 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000255 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000256 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000257 *clearToInside = false;
258 done = true;
259 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000260 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000261 // if this element contains the entire bounds then we
262 // can skip it.
263 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000264 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000265 break;
266 }
267 // if everything is initially clearToInside then intersect is
268 // same as clear to 0 and treat as a replace. Otherwise,
269 // set stays empty.
270 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000271 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000272 *clearToInside = false;
273 done = true;
274 }
275 break;
276 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000277 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000278 // if everything is initially outside then union is
279 // same as replace. Otherwise, every pixel is still
280 // clearToInside
281 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000282 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000283 done = true;
284 }
285 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000286 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000287 // xor is same as difference or replace both of which
288 // can be 1-pass instead of 2 for xor.
289 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000290 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000292 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000293 }
294 done = true;
295 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000296 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000297 // if all pixels are clearToInside then we have to process the
298 // difference, otherwise it has no effect and all pixels
299 // remain outside.
300 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000301 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000302 done = true;
303 }
304 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000305 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000306 // if all pixels are clearToInside then reverse difference
307 // produces empty set. Otherise it is same as replace
308 if (*clearToInside) {
309 *clearToInside = false;
310 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000311 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 done = true;
313 }
314 break;
315 default:
316 GrCrash("Unknown set op.");
317 }
318 }
319 return done ? curr-1 : count;
320}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000321
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000322}
323
robertphillips@google.comf294b772012-04-27 14:29:26 +0000324
325namespace {
326
327////////////////////////////////////////////////////////////////////////////////
328// set up the OpenGL blend function to perform the specified
329// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000330void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000331
332 switch (op) {
333 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000334 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000335 break;
336 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000337 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000338 break;
339 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000340 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000341 break;
342 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000343 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000344 break;
345 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000346 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000347 break;
348 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000349 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000350 break;
351 default:
352 GrAssert(false);
353 break;
354 }
355}
356
robertphillips@google.comf294b772012-04-27 14:29:26 +0000357////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000358bool draw_path(GrContext* context,
359 GrGpu* gpu,
360 const SkPath& path,
361 GrPathFill fill,
362 bool doAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363
robertphillips@google.com72176b22012-05-23 13:19:12 +0000364 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365 if (NULL == pr) {
366 return false;
367 }
368
369 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
370 return true;
371}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000372
373}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374
375////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000376bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 const GrClip& clipIn,
378 int index) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000379 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380 GrAssert(NULL != drawState);
381
382 drawState->setRenderTarget(target->asRenderTarget());
383
384 if (kRect_ClipType == clipIn.getElementType(index)) {
385 if (clipIn.getDoAA(index)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000386 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000387 clipIn.getRect(index),
388 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000390 fGpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391 }
392 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000393 return draw_path(this->getContext(), fGpu,
robertphillips@google.com2c756812012-05-22 20:28:23 +0000394 clipIn.getPath(index),
395 clipIn.getPathFill(index),
396 clipIn.getDoAA(index));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397 }
398 return true;
399}
400
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000401void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000403 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404 GrAssert(NULL != drawState);
405
406 // no AA here since it is encoded in the texture
407 drawState->setRenderTarget(target->asRenderTarget());
408
409 GrMatrix sampleM;
410 sampleM.setIDiv(texture->width(), texture->height());
411 drawState->setTexture(0, texture);
412
413 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
414 GrSamplerState::kNearest_Filter,
415 sampleM);
416
robertphillips@google.comf105b102012-05-14 12:18:26 +0000417 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
418 SkIntToScalar(target->height()));
419
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000420 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000421
422 drawState->setTexture(0, NULL);
423}
424
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000425// get a texture to act as a temporary buffer for AA clip boolean operations
426// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000427void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000428 GrAutoScratchTexture* temp) {
429 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000430 // we've already allocated the temp texture
431 return;
432 }
433
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000434 GrTextureDesc desc;
435 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
436 desc.fWidth = bounds.width();
437 desc.fHeight = bounds.height();
438 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000439
robertphillips@google.com2c756812012-05-22 20:28:23 +0000440 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000441}
442
robertphillips@google.comf105b102012-05-14 12:18:26 +0000443
444void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000445 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000446 // Since we are setting up the cache we know the last lookup was a miss
447 // Free up the currently cached mask so it can be reused
448 fAACache.reset();
449
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000450 GrTextureDesc desc;
451 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
452 desc.fWidth = bounds.width();
453 desc.fHeight = bounds.height();
454 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000455
456 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000457}
458
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000459////////////////////////////////////////////////////////////////////////////////
460// Shared preamble between gpu and SW-only AA clip mask creation paths.
461// Handles caching, determination of clip mask bound & allocation (if needed)
462// of the result texture
463// 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 +0000464bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000465 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000466 GrIRect *resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000467 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000468 GrAssert(origDrawState->isClipState());
469
470 GrRenderTarget* rt = origDrawState->getRenderTarget();
471 GrAssert(NULL != rt);
472
473 GrRect rtRect;
474 rtRect.setLTRB(0, 0,
475 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
476
477 // unlike the stencil path the alpha path is not bound to the size of the
478 // render target - determine the minimum size required for the mask
479 GrRect bounds;
480
481 if (clipIn.hasConservativeBounds()) {
482 bounds = clipIn.getConservativeBounds();
483 if (!bounds.intersect(rtRect)) {
484 // the mask will be empty in this case
485 GrAssert(false);
486 bounds.setEmpty();
487 }
488 } else {
489 // still locked to the size of the render target
490 bounds = rtRect;
491 }
492
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000493 GrIRect intBounds;
494 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495
496 // need to outset a pixel since the standard bounding box computation
497 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000498 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000499
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000500 // TODO: make sure we don't outset if bounds are still 0,0 @ min
501
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000502 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000503 intBounds.width(),
504 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000505 *result = fAACache.getLastMask();
506 fAACache.getLastBound(resultBounds);
507 return true;
508 }
509
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000510 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000511
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000512 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000513 return false;
514}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000515
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000516////////////////////////////////////////////////////////////////////////////////
517// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000518bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000519 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000520 GrIRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000521
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000522 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
523
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000524 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000525 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000526 return true;
527 }
528
robertphillips@google.comf105b102012-05-14 12:18:26 +0000529 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000530 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000531 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000532 return false;
533 }
534
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000535 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
536 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000537
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000538 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000539
540 int count = clipIn.getElementCount();
541
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000542 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000543 // if we were able to trim down the size of the mask we need to
544 // offset the paths & rects that will be used to compute it
545 GrMatrix m;
546
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000547 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
548 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000549
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000550 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000551 }
552
553 bool clearToInside;
554 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
555 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000556 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000557 &clearToInside,
558 &startOp);
559
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000560 fGpu->clear(NULL,
561 clearToInside ? 0xffffffff : 0x00000000,
562 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563
robertphillips@google.comf105b102012-05-14 12:18:26 +0000564 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000565
robertphillips@google.comf294b772012-04-27 14:29:26 +0000566 // walk through each clip element and perform its set op
567 for (int c = start; c < count; ++c) {
568
569 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
570
571 if (SkRegion::kReplace_Op == op) {
572 // TODO: replace is actually a lot faster then intersection
573 // for this path - refactor the stencil path so it can handle
574 // replace ops and alter GrClip to allow them through
575
576 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000577 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000578
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000579 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000580 this->drawClipShape(accum, clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000581
582 } else if (SkRegion::kReverseDifference_Op == op ||
583 SkRegion::kIntersect_Op == op) {
584 // there is no point in intersecting a screen filling rectangle.
585 if (SkRegion::kIntersect_Op == op &&
586 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000587 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000588 continue;
589 }
590
robertphillips@google.comf105b102012-05-14 12:18:26 +0000591 getTemp(*resultBounds, &temp);
592 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000593 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000594 return false;
595 }
596
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000598 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000599
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000600 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000601 this->drawClipShape(temp.texture(), clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000602
603 // TODO: rather than adding these two translations here
604 // compute the bounding box needed to render the texture
605 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000606 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000607 GrMatrix m;
608
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000609 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
610 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611
612 drawState->preConcatViewMatrix(m);
613 }
614
615 // Now draw into the accumulator using the real operation
616 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000617 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000618 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000619
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000620 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000621 GrMatrix m;
622
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000623 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
624 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000625
626 drawState->preConcatViewMatrix(m);
627 }
628
629 } else {
630 // all the remaining ops can just be directly draw into
631 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000632 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000633 this->drawClipShape(accum, clipIn, c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000634 }
635 }
636
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000637 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000638 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000639 return true;
640}
641
642////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000643// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000644bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000645 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000646
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000647 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000649 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000650 GrAssert(drawState->isClipState());
651
652 GrRenderTarget* rt = drawState->getRenderTarget();
653 GrAssert(NULL != rt);
654
655 // TODO: dynamically attach a SB when needed.
656 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
657 if (NULL == stencilBuffer) {
658 return false;
659 }
660
661 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
662
663 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
664
665 // we set the current clip to the bounds so that our recursive
666 // draws are scissored to them. We use the copy of the complex clip
667 // we just stashed on the SB to render from. We set it back after
668 // we finish drawing it into the stencil.
669 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000670 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000671
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000672 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
673 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000674 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000675 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000677#if !VISUALIZE_COMPLEX_CLIP
678 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
679#endif
680
681 int count = clipCopy.getElementCount();
682 int clipBit = stencilBuffer->bits();
683 SkASSERT((clipBit <= 16) &&
684 "Ganesh only handles 16b or smaller stencil buffers");
685 clipBit = (1 << (clipBit-1));
686
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000687 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688
689 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000690 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691 int start = process_initial_clip_elements(clipCopy,
692 rtRect,
693 &clearToInside,
694 &startOp);
695
bsalomon@google.coma3201942012-06-21 19:58:20 +0000696 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697
698 // walk through each clip element and perform its set op
699 // with the existing clip.
700 for (int c = start; c < count; ++c) {
701 GrPathFill fill;
702 bool fillInverted;
703 // enabled at bottom of loop
704 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
705
706 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000707 // directly to the stencil buffer
708 // with a non-inverted fill rule
709 // without extra passes to
710 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
robertphillips@google.comf294b772012-04-27 14:29:26 +0000712 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
713
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000715 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 if (kRect_ClipType == clipCopy.getElementType(c)) {
717 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000718 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 fillInverted = false;
720 // there is no point in intersecting a screen filling
721 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000722 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000723 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724 continue;
725 }
726 } else {
727 fill = clipCopy.getPathFill(c);
728 fillInverted = GrIsFillInverted(fill);
729 fill = GrNonInvertedFill(fill);
730 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000731 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000732 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000733 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000735 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736 return false;
737 }
738 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000739 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 }
741
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 int passes;
743 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
744
745 bool canDrawDirectToClip; // Given the renderer, the element,
746 // fill rule, and set operation can
747 // we render the element directly to
748 // stencil bit used for clipping.
749 canDrawDirectToClip =
750 GrStencilSettings::GetClipPasses(op,
751 canRenderDirectToStencil,
752 clipBit,
753 fillInverted,
754 &passes, stencilSettings);
755
756 // draw the element to the client stencil bits if necessary
757 if (!canDrawDirectToClip) {
758 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
759 kIncClamp_StencilOp,
760 kIncClamp_StencilOp,
761 kAlways_StencilFunc,
762 0xffff,
763 0x0000,
764 0xffff);
765 SET_RANDOM_COLOR
766 if (kRect_ClipType == clipCopy.getElementType(c)) {
767 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000768 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769 } else {
770 if (canRenderDirectToStencil) {
771 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000772 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000773 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000774 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 }
776 }
777 }
778
779 // now we modify the clip bit by rendering either the clip
780 // element directly or a bounding rect of the entire clip.
781 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
782 for (int p = 0; p < passes; ++p) {
783 *drawState->stencil() = stencilSettings[p];
784 if (canDrawDirectToClip) {
785 if (kRect_ClipType == clipCopy.getElementType(c)) {
786 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000787 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 } else {
789 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000790 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791 }
792 } else {
793 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000794 GrRect rect = GrRect::MakeLTRB(
795 SkIntToScalar(bounds.fLeft),
796 SkIntToScalar(bounds.fTop),
797 SkIntToScalar(bounds.fRight),
798 SkIntToScalar(bounds.fBottom));
799 fGpu->drawSimpleRect(rect, NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 }
801 }
802 }
803 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000804 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000806 // set this last because recursive draws may overwrite it back to kNone.
807 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
808 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 return true;
810}
811
bsalomon@google.com411dad02012-06-05 20:24:20 +0000812// mapping of clip-respecting stencil funcs to normal stencil funcs
813// mapping depends on whether stencil-clipping is in effect.
814static const GrStencilFunc
815 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
816 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
817 // In the Clip Funcs
818 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
819 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
820 kLess_StencilFunc, // kLessIfInClip_StencilFunc
821 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
822 // Special in the clip func that forces user's ref to be 0.
823 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
824 // make ref 0 and do normal nequal.
825 },
826 {// Stencil-Clipping is ENABLED
827 // In the Clip Funcs
828 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
829 // eq stencil clip bit, mask
830 // out user bits.
831
832 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
833 // add stencil bit to mask and ref
834
835 kLess_StencilFunc, // kLessIfInClip_StencilFunc
836 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
837 // for both of these we can add
838 // the clip bit to the mask and
839 // ref and compare as normal
840 // Special in the clip func that forces user's ref to be 0.
841 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
842 // make ref have only the clip bit set
843 // and make comparison be less
844 // 10..0 < 1..user_bits..
845 }
846};
847
bsalomon@google.coma3201942012-06-21 19:58:20 +0000848namespace {
849// Sets the settings to clip against the stencil buffer clip while ignoring the
850// client bits.
851const GrStencilSettings& basic_apply_stencil_clip_settings() {
852 // stencil settings to use when clip is in stencil
853 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
854 kKeep_StencilOp,
855 kKeep_StencilOp,
856 kAlwaysIfInClip_StencilFunc,
857 0x0000,
858 0x0000,
859 0x0000);
860 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
861}
862}
863
864void GrClipMaskManager::setGpuStencil() {
865 // We make two copies of the StencilSettings here (except in the early
866 // exit scenario. One copy from draw state to the stack var. Then another
867 // from the stack var to the gpu. We could make this class hold a ptr to
868 // GrGpu's fStencilSettings and eliminate the stack copy here.
869
870 const GrDrawState& drawState = fGpu->getDrawState();
871
872 // use stencil for clipping if clipping is enabled and the clip
873 // has been written into the stencil.
874 GrClipMaskManager::StencilClipMode clipMode;
875 if (this->isClipInStencil() && drawState.isClipState()) {
876 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
877 // We can't be modifying the clip and respecting it at the same time.
878 GrAssert(!drawState.isStateFlagEnabled(
879 GrGpu::kModifyStencilClip_StateBit));
880 } else if (drawState.isStateFlagEnabled(
881 GrGpu::kModifyStencilClip_StateBit)) {
882 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
883 } else {
884 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
885 }
886
887 GrStencilSettings settings;
888 // The GrGpu client may not be using the stencil buffer but we may need to
889 // enable it in order to respect a stencil clip.
890 if (drawState.getStencil().isDisabled()) {
891 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
892 settings = basic_apply_stencil_clip_settings();
893 } else {
894 fGpu->disableStencil();
895 return;
896 }
897 } else {
898 settings = drawState.getStencil();
899 }
900
901 // TODO: dynamically attach a stencil buffer
902 int stencilBits = 0;
903 GrStencilBuffer* stencilBuffer =
904 drawState.getRenderTarget()->getStencilBuffer();
905 if (NULL != stencilBuffer) {
906 stencilBits = stencilBuffer->bits();
907 }
908
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000909 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
910 !settings.usesWrapOp());
911 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000912 this->adjustStencilParams(&settings, clipMode, stencilBits);
913 fGpu->setStencilSettings(settings);
914}
915
916void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
917 StencilClipMode mode,
918 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000919 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000920
921 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000922 // We assume that this clip manager itself is drawing to the GrGpu and
923 // has already setup the correct values.
924 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000925 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000926
bsalomon@google.com411dad02012-06-05 20:24:20 +0000927 unsigned int clipBit = (1 << (stencilBitCnt - 1));
928 unsigned int userBits = clipBit - 1;
929
bsalomon@google.coma3201942012-06-21 19:58:20 +0000930 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
931 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000932
bsalomon@google.coma3201942012-06-21 19:58:20 +0000933 bool finished = false;
934 while (!finished) {
935 GrStencilFunc func = settings->func(face);
936 uint16_t writeMask = settings->writeMask(face);
937 uint16_t funcMask = settings->funcMask(face);
938 uint16_t funcRef = settings->funcRef(face);
939
940 GrAssert((unsigned) func < kStencilFuncCount);
941
942 writeMask &= userBits;
943
944 if (func >= kBasicStencilFuncCount) {
945 int respectClip = kRespectClip_StencilClipMode == mode;
946 if (respectClip) {
947 // The GrGpu class should have checked this
948 GrAssert(this->isClipInStencil());
949 switch (func) {
950 case kAlwaysIfInClip_StencilFunc:
951 funcMask = clipBit;
952 funcRef = clipBit;
953 break;
954 case kEqualIfInClip_StencilFunc:
955 case kLessIfInClip_StencilFunc:
956 case kLEqualIfInClip_StencilFunc:
957 funcMask = (funcMask & userBits) | clipBit;
958 funcRef = (funcRef & userBits) | clipBit;
959 break;
960 case kNonZeroIfInClip_StencilFunc:
961 funcMask = (funcMask & userBits) | clipBit;
962 funcRef = clipBit;
963 break;
964 default:
965 GrCrash("Unknown stencil func");
966 }
967 } else {
968 funcMask &= userBits;
969 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000970 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000971 const GrStencilFunc* table =
972 gSpecialToBasicStencilFunc[respectClip];
973 func = table[func - kBasicStencilFuncCount];
974 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000975 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 funcMask &= userBits;
977 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000978 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000979
980 settings->setFunc(face, func);
981 settings->setWriteMask(face, writeMask);
982 settings->setFuncMask(face, funcMask);
983 settings->setFuncRef(face, funcRef);
984
985 if (GrStencilSettings::kFront_Face == face) {
986 face = GrStencilSettings::kBack_Face;
987 finished = !twoSided;
988 } else {
989 finished = true;
990 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000991 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000992 if (!twoSided) {
993 settings->copyFrontSettingsToBack();
994 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000995}
996
997////////////////////////////////////////////////////////////////////////////////
998
robertphillips@google.comfa662942012-05-17 12:20:22 +0000999namespace {
1000
1001GrPathFill invert_fill(GrPathFill fill) {
1002 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001003 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1004 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1005 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1006 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1007 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001008 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001009 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1010 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1011 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1012 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1013 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1014 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001015 return gInvertedFillTable[fill];
1016}
1017
1018}
1019
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001020bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001021 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +00001022 GrIRect *resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001023 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001024
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001025 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001026 return true;
1027 }
1028
robertphillips@google.comf105b102012-05-14 12:18:26 +00001029 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001030 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001031 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001032 return false;
1033 }
1034
robertphillips@google.com2c756812012-05-22 20:28:23 +00001035 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001036
robertphillips@google.comfa662942012-05-17 12:20:22 +00001037 helper.init(*resultBounds, NULL, false);
1038
1039 int count = clipIn.getElementCount();
1040
1041 bool clearToInside;
1042 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
1043 int start = process_initial_clip_elements(clipIn,
1044 *resultBounds,
1045 &clearToInside,
1046 &startOp);
1047
1048 helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000);
1049
1050 for (int i = start; i < count; ++i) {
1051
1052 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
1053
1054 if (SkRegion::kIntersect_Op == op ||
1055 SkRegion::kReverseDifference_Op == op) {
1056 // Intersect and reverse difference require modifying pixels
1057 // outside of the geometry that is being "drawn". In both cases
1058 // we erase all the pixels outside of the geometry but
1059 // leave the pixels inside the geometry alone. For reverse
1060 // difference we invert all the pixels before clearing the ones
1061 // outside the geometry.
1062 if (SkRegion::kReverseDifference_Op == op) {
1063 SkRect temp = SkRect::MakeLTRB(
1064 SkIntToScalar(resultBounds->left()),
1065 SkIntToScalar(resultBounds->top()),
1066 SkIntToScalar(resultBounds->right()),
1067 SkIntToScalar(resultBounds->bottom()));
1068
1069 // invert the entire scene
1070 helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE);
1071 }
1072
1073 if (kRect_ClipType == clipIn.getElementType(i)) {
1074
1075 // convert the rect to a path so we can invert the fill
1076 SkPath temp;
1077 temp.addRect(clipIn.getRect(i));
1078
1079 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001080 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001081 0x00000000);
1082 } else {
1083 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1084
1085 helper.draw(clipIn.getPath(i),
1086 SkRegion::kReplace_Op,
1087 invert_fill(clipIn.getPathFill(i)),
1088 clipIn.getDoAA(i),
1089 0x00000000);
1090 }
1091
1092 continue;
1093 }
1094
1095 // The other ops (union, xor, diff) only affect pixels inside
1096 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001097 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001098
1099 helper.draw(clipIn.getRect(i),
1100 op,
1101 clipIn.getDoAA(i), SK_ColorWHITE);
1102
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001103 } else {
1104 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1105
robertphillips@google.comfa662942012-05-17 12:20:22 +00001106 helper.draw(clipIn.getPath(i),
1107 op,
1108 clipIn.getPathFill(i),
1109 clipIn.getDoAA(i), SK_ColorWHITE);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001110 }
1111 }
1112
robertphillips@google.comfa662942012-05-17 12:20:22 +00001113 // Because we are using the scratch texture cache, "accum" may be
1114 // larger than expected and have some cruft in the areas we aren't using.
1115 // Clear it out.
1116
1117 // TODO: need a simpler way to clear the texture - can we combine
1118 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001119 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001120 GrAssert(NULL != drawState);
1121 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001122 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001123 // can't leave the accum bound as a rendertarget
1124 drawState->setRenderTarget(temp);
1125
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001126 helper.toTexture(accum, clearToInside);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001127
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001128 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001129
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001130 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001131 return true;
1132}
1133
robertphillips@google.comf294b772012-04-27 14:29:26 +00001134////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001135void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001136 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001137}