blob: 7c54009cdb8735dc9328dfa6b8fb9242690884e1 [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());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000117
118 clipIn.getConservativeBounds().roundOut(&bounds);
119 if (!bounds.intersect(rtRect)) {
120 bounds.setEmpty();
121 }
122 if (bounds.isEmpty()) {
123 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000124 }
125
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000126#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000127 // If MSAA is enabled we can do everything in the stencil buffer.
128 // Otherwise check if we should just create the entire clip mask
129 // in software (this will only happen if the clip mask is anti-aliased
130 // and too complex for the gpu to handle in its entirety)
bsalomon@google.coma3201942012-06-21 19:58:20 +0000131 if (0 == rt->numSamples() && this->useSWOnlyPath(clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000132 // The clip geometry is complex enough that it will be more
133 // efficient to create it entirely in software
134 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000135 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000136 if (this->createSoftwareClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000137 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000138 fGpu->disableScissor();
139 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000140 return true;
141 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000142
143 // if SW clip mask creation fails fall through to the other
144 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000145 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000146#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000147
robertphillips@google.comf294b772012-04-27 14:29:26 +0000148#if GR_AA_CLIP
149 // If MSAA is enabled use the (faster) stencil path for AA clipping
150 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000151 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000152 // Since we are going to create a destination texture of the correct
153 // size for the mask (rather than being bound by the size of the
154 // render target) we aren't going to use scissoring like the stencil
155 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000156 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000157 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000158 if (this->createAlphaClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000159 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000160 fGpu->disableScissor();
161 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000162 return true;
163 }
164
165 // if alpha clip mask creation fails fall through to the stencil
166 // buffer method
167 }
168#endif // GR_AA_CLIP
169
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000170 // Either a hard (stencil buffer) clip was explicitly requested or
171 // an antialiased clip couldn't be created. In either case, free up
172 // the texture in the antialiased mask cache.
173 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000174 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
175 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000176 // AA cache.
177 fAACache.reset();
178
bsalomon@google.coma3201942012-06-21 19:58:20 +0000179 // If the clip is a rectangle then just set the scissor. Otherwise, create
180 // a stencil mask.
181 if (clipIn.isRect()) {
182 fGpu->enableScissor(bounds);
183 this->setGpuStencil();
184 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000185 }
186
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000187 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000188 bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
189 !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000190
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000191 if (useStencil) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000192 this->createStencilClipMask(clipIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000193 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000194 // This must occur after createStencilClipMask. That function may change
195 // the scissor. Also, it only guarantees that the stencil mask is correct
196 // within the bounds it was passed, so we must use both stencil and scissor
197 // test to the bounds for the final draw.
198 fGpu->enableScissor(bounds);
199 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000200 return true;
201}
202
203#define VISUALIZE_COMPLEX_CLIP 0
204
205#if VISUALIZE_COMPLEX_CLIP
206 #include "GrRandom.h"
207 GrRandom gRandom;
208 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
209#else
210 #define SET_RANDOM_COLOR
211#endif
212
213namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000214/**
215 * Does "container" contain "containee"? If either is empty then
216 * no containment is possible.
217 */
218bool contains(const SkRect& container, const SkIRect& containee) {
219 return !containee.isEmpty() && !container.isEmpty() &&
220 container.fLeft <= SkIntToScalar(containee.fLeft) &&
221 container.fTop <= SkIntToScalar(containee.fTop) &&
222 container.fRight >= SkIntToScalar(containee.fRight) &&
223 container.fBottom >= SkIntToScalar(containee.fBottom);
224}
225
226
robertphillips@google.comf294b772012-04-27 14:29:26 +0000227////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000228// determines how many elements at the head of the clip can be skipped and
229// whether the initial clear should be to the inside- or outside-the-clip value,
230// and what op should be used to draw the first element that isn't skipped.
231int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000232 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000233 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000234 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000235
236 // logically before the first element of the clip stack is
237 // processed the clip is entirely open. However, depending on the
238 // first set op we may prefer to clear to 0 for performance. We may
239 // also be able to skip the initial clip paths/rects. We loop until
240 // we cannot skip an element.
241 int curr;
242 bool done = false;
243 *clearToInside = true;
244 int count = clip.getElementCount();
245
246 for (curr = 0; curr < count && !done; ++curr) {
247 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000248 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000249 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000250 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000251 *clearToInside = false;
252 done = true;
253 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000254 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000255 // if this element contains the entire bounds then we
256 // can skip it.
257 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000258 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000259 break;
260 }
261 // if everything is initially clearToInside then intersect is
262 // same as clear to 0 and treat as a replace. Otherwise,
263 // set stays empty.
264 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000265 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000266 *clearToInside = false;
267 done = true;
268 }
269 break;
270 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000271 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000272 // if everything is initially outside then union is
273 // same as replace. Otherwise, every pixel is still
274 // clearToInside
275 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000276 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000277 done = true;
278 }
279 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000280 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281 // xor is same as difference or replace both of which
282 // can be 1-pass instead of 2 for xor.
283 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000284 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000285 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000286 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000287 }
288 done = true;
289 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000290 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 // if all pixels are clearToInside then we have to process the
292 // difference, otherwise it has no effect and all pixels
293 // remain outside.
294 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000295 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000296 done = true;
297 }
298 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000299 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 // if all pixels are clearToInside then reverse difference
301 // produces empty set. Otherise it is same as replace
302 if (*clearToInside) {
303 *clearToInside = false;
304 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000305 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000306 done = true;
307 }
308 break;
309 default:
310 GrCrash("Unknown set op.");
311 }
312 }
313 return done ? curr-1 : count;
314}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000315
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000316}
317
robertphillips@google.comf294b772012-04-27 14:29:26 +0000318
319namespace {
320
321////////////////////////////////////////////////////////////////////////////////
322// set up the OpenGL blend function to perform the specified
323// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000324void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000325
326 switch (op) {
327 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000328 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000329 break;
330 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000331 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000332 break;
333 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000334 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000335 break;
336 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000337 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000338 break;
339 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000340 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000341 break;
342 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000343 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000344 break;
345 default:
346 GrAssert(false);
347 break;
348 }
349}
350
robertphillips@google.comf294b772012-04-27 14:29:26 +0000351////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000352bool draw_path_in_software(GrContext* context,
353 GrGpu* gpu,
354 const SkPath& path,
355 GrPathFill fill,
356 bool doAA,
357 const GrIRect& resultBounds) {
358
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000359 SkAutoTUnref<GrTexture> texture(
360 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
361 resultBounds, fill,
362 doAA, NULL));
363 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000364 return false;
365 }
366
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000367 // The ClipMaskManager accumulates the clip mask in the UL corner
368 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000369
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000370 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, 0, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000371
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000372 GrAssert(!GrIsFillInverted(fill));
373 return true;
374}
375
376
377////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000378bool draw_path(GrContext* context,
379 GrGpu* gpu,
380 const SkPath& path,
381 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000382 bool doAA,
383 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000385 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000387 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388 }
389
390 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
391 return true;
392}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000393
394}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395
396////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000397bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000398 const GrClip& clipIn,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000399 int index,
400 const GrIRect& resultBounds) {
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 drawState->setRenderTarget(target->asRenderTarget());
405
406 if (kRect_ClipType == clipIn.getElementType(index)) {
407 if (clipIn.getDoAA(index)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000408 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000409 clipIn.getRect(index),
410 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000412 fGpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413 }
414 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000415 return draw_path(this->getContext(), fGpu,
robertphillips@google.com2c756812012-05-22 20:28:23 +0000416 clipIn.getPath(index),
417 clipIn.getPathFill(index),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000418 clipIn.getDoAA(index),
419 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000420 }
421 return true;
422}
423
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000424void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000425 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000426 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000427 GrAssert(NULL != drawState);
428
429 // no AA here since it is encoded in the texture
430 drawState->setRenderTarget(target->asRenderTarget());
431
432 GrMatrix sampleM;
433 sampleM.setIDiv(texture->width(), texture->height());
434 drawState->setTexture(0, texture);
435
436 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
437 GrSamplerState::kNearest_Filter,
438 sampleM);
439
robertphillips@google.comf105b102012-05-14 12:18:26 +0000440 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
441 SkIntToScalar(target->height()));
442
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000443 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000444
tomhudson@google.com676e6602012-07-10 17:21:48 +0000445 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000446}
447
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000448// get a texture to act as a temporary buffer for AA clip boolean operations
449// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000450void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000451 GrAutoScratchTexture* temp) {
452 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000453 // we've already allocated the temp texture
454 return;
455 }
456
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000457 GrTextureDesc desc;
458 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
459 desc.fWidth = bounds.width();
460 desc.fHeight = bounds.height();
461 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000462
robertphillips@google.com2c756812012-05-22 20:28:23 +0000463 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000464}
465
robertphillips@google.comf105b102012-05-14 12:18:26 +0000466
467void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000468 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000469 // Since we are setting up the cache we know the last lookup was a miss
470 // Free up the currently cached mask so it can be reused
471 fAACache.reset();
472
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000473 GrTextureDesc desc;
474 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
475 desc.fWidth = bounds.width();
476 desc.fHeight = bounds.height();
477 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000478
479 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000480}
481
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000482////////////////////////////////////////////////////////////////////////////////
483// Shared preamble between gpu and SW-only AA clip mask creation paths.
484// Handles caching, determination of clip mask bound & allocation (if needed)
485// of the result texture
486// 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 +0000487bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000488 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000489 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000490 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000491 GrAssert(origDrawState->isClipState());
492
493 GrRenderTarget* rt = origDrawState->getRenderTarget();
494 GrAssert(NULL != rt);
495
496 GrRect rtRect;
497 rtRect.setLTRB(0, 0,
498 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
499
500 // unlike the stencil path the alpha path is not bound to the size of the
501 // render target - determine the minimum size required for the mask
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000502 GrRect bounds = clipIn.getConservativeBounds();
503 if (!bounds.intersect(rtRect)) {
504 // the mask will be empty in this case
505 GrAssert(false);
506 bounds.setEmpty();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000507 }
508
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000509 GrIRect intBounds;
510 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000511
512 // need to outset a pixel since the standard bounding box computation
513 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000514 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000515
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000516 // TODO: make sure we don't outset if bounds are still 0,0 @ min
517
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000518 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000519 intBounds.width(),
520 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000521 *result = fAACache.getLastMask();
522 fAACache.getLastBound(resultBounds);
523 return true;
524 }
525
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000526 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000528 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000529 return false;
530}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000531
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000532////////////////////////////////////////////////////////////////////////////////
533// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000534bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000535 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000536 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000537 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000538 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
539
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000540 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000541 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000542 return true;
543 }
544
robertphillips@google.comf105b102012-05-14 12:18:26 +0000545 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000546 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000547 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000548 return false;
549 }
550
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000551 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
552 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000553
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000554 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555
556 int count = clipIn.getElementCount();
557
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000558 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559 // if we were able to trim down the size of the mask we need to
560 // offset the paths & rects that will be used to compute it
561 GrMatrix m;
562
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000563 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
564 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000566 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000567 }
568
569 bool clearToInside;
570 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
571 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000572 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000573 &clearToInside,
574 &startOp);
575
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000576 fGpu->clear(NULL,
577 clearToInside ? 0xffffffff : 0x00000000,
578 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000579
robertphillips@google.comf105b102012-05-14 12:18:26 +0000580 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000581
robertphillips@google.comf294b772012-04-27 14:29:26 +0000582 // walk through each clip element and perform its set op
583 for (int c = start; c < count; ++c) {
584
585 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
586
587 if (SkRegion::kReplace_Op == op) {
588 // TODO: replace is actually a lot faster then intersection
589 // for this path - refactor the stencil path so it can handle
590 // replace ops and alter GrClip to allow them through
591
592 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000593 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000595 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000596 this->drawClipShape(accum, clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597
598 } else if (SkRegion::kReverseDifference_Op == op ||
599 SkRegion::kIntersect_Op == op) {
600 // there is no point in intersecting a screen filling rectangle.
601 if (SkRegion::kIntersect_Op == op &&
602 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000603 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000604 continue;
605 }
606
robertphillips@google.comf105b102012-05-14 12:18:26 +0000607 getTemp(*resultBounds, &temp);
608 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000609 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000610 return false;
611 }
612
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000614 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000615
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000616 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000617 this->drawClipShape(temp.texture(), clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000618
619 // TODO: rather than adding these two translations here
620 // compute the bounding box needed to render the texture
621 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000622 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000623 GrMatrix m;
624
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000625 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
626 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000627
628 drawState->preConcatViewMatrix(m);
629 }
630
631 // Now draw into the accumulator using the real operation
632 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000633 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000634 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000635
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000636 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000637 GrMatrix m;
638
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000639 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
640 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000641
642 drawState->preConcatViewMatrix(m);
643 }
644
645 } else {
646 // all the remaining ops can just be directly draw into
647 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000648 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000649 this->drawClipShape(accum, clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000650 }
651 }
652
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000653 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000654 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000655 return true;
656}
657
658////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000660bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000661 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000662
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000663 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000665 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666 GrAssert(drawState->isClipState());
667
668 GrRenderTarget* rt = drawState->getRenderTarget();
669 GrAssert(NULL != rt);
670
671 // TODO: dynamically attach a SB when needed.
672 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
673 if (NULL == stencilBuffer) {
674 return false;
675 }
676
677 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
678
679 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
680
681 // we set the current clip to the bounds so that our recursive
682 // draws are scissored to them. We use the copy of the complex clip
683 // we just stashed on the SB to render from. We set it back after
684 // we finish drawing it into the stencil.
685 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000686 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000688 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
689 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000691 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693#if !VISUALIZE_COMPLEX_CLIP
694 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
695#endif
696
697 int count = clipCopy.getElementCount();
698 int clipBit = stencilBuffer->bits();
699 SkASSERT((clipBit <= 16) &&
700 "Ganesh only handles 16b or smaller stencil buffers");
701 clipBit = (1 << (clipBit-1));
702
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000703 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704
705 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000706 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707 int start = process_initial_clip_elements(clipCopy,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000708 rtRect,
709 &clearToInside,
710 &startOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
bsalomon@google.coma3201942012-06-21 19:58:20 +0000712 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713
714 // walk through each clip element and perform its set op
715 // with the existing clip.
716 for (int c = start; c < count; ++c) {
717 GrPathFill fill;
718 bool fillInverted;
719 // enabled at bottom of loop
720 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000721 // if the target is MSAA then we want MSAA enabled when the clip is soft
722 if (rt->isMultisampled()) {
723 if (clipCopy.getDoAA(c)) {
724 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
725 } else {
726 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
727 }
728 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729
730 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000731 // directly to the stencil buffer
732 // with a non-inverted fill rule
733 // without extra passes to
734 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735
robertphillips@google.comf294b772012-04-27 14:29:26 +0000736 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
737
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000739 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 if (kRect_ClipType == clipCopy.getElementType(c)) {
741 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000742 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743 fillInverted = false;
744 // there is no point in intersecting a screen filling
745 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000746 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000747 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000748 continue;
749 }
750 } else {
751 fill = clipCopy.getPathFill(c);
752 fillInverted = GrIsFillInverted(fill);
753 fill = GrNonInvertedFill(fill);
754 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000755 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000756 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000757 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000759 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760 return false;
761 }
762 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000763 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764 }
765
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766 int passes;
767 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
768
769 bool canDrawDirectToClip; // Given the renderer, the element,
770 // fill rule, and set operation can
771 // we render the element directly to
772 // stencil bit used for clipping.
773 canDrawDirectToClip =
774 GrStencilSettings::GetClipPasses(op,
775 canRenderDirectToStencil,
776 clipBit,
777 fillInverted,
778 &passes, stencilSettings);
779
780 // draw the element to the client stencil bits if necessary
781 if (!canDrawDirectToClip) {
782 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
783 kIncClamp_StencilOp,
784 kIncClamp_StencilOp,
785 kAlways_StencilFunc,
786 0xffff,
787 0x0000,
788 0xffff);
789 SET_RANDOM_COLOR
790 if (kRect_ClipType == clipCopy.getElementType(c)) {
791 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000792 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 } else {
794 if (canRenderDirectToStencil) {
795 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000796 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000798 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 }
800 }
801 }
802
803 // now we modify the clip bit by rendering either the clip
804 // element directly or a bounding rect of the entire clip.
805 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
806 for (int p = 0; p < passes; ++p) {
807 *drawState->stencil() = stencilSettings[p];
808 if (canDrawDirectToClip) {
809 if (kRect_ClipType == clipCopy.getElementType(c)) {
810 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000811 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 } else {
813 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000814 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000815 }
816 } else {
817 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000818 GrRect rect = GrRect::MakeLTRB(
819 SkIntToScalar(bounds.fLeft),
820 SkIntToScalar(bounds.fTop),
821 SkIntToScalar(bounds.fRight),
822 SkIntToScalar(bounds.fBottom));
823 fGpu->drawSimpleRect(rect, NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 }
825 }
826 }
827 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000828 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000829 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000830 // set this last because recursive draws may overwrite it back to kNone.
831 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
832 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000833 return true;
834}
835
bsalomon@google.com411dad02012-06-05 20:24:20 +0000836// mapping of clip-respecting stencil funcs to normal stencil funcs
837// mapping depends on whether stencil-clipping is in effect.
838static const GrStencilFunc
839 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
840 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
841 // In the Clip Funcs
842 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
843 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
844 kLess_StencilFunc, // kLessIfInClip_StencilFunc
845 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
846 // Special in the clip func that forces user's ref to be 0.
847 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
848 // make ref 0 and do normal nequal.
849 },
850 {// Stencil-Clipping is ENABLED
851 // In the Clip Funcs
852 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
853 // eq stencil clip bit, mask
854 // out user bits.
855
856 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
857 // add stencil bit to mask and ref
858
859 kLess_StencilFunc, // kLessIfInClip_StencilFunc
860 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
861 // for both of these we can add
862 // the clip bit to the mask and
863 // ref and compare as normal
864 // Special in the clip func that forces user's ref to be 0.
865 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
866 // make ref have only the clip bit set
867 // and make comparison be less
868 // 10..0 < 1..user_bits..
869 }
870};
871
bsalomon@google.coma3201942012-06-21 19:58:20 +0000872namespace {
873// Sets the settings to clip against the stencil buffer clip while ignoring the
874// client bits.
875const GrStencilSettings& basic_apply_stencil_clip_settings() {
876 // stencil settings to use when clip is in stencil
877 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
878 kKeep_StencilOp,
879 kKeep_StencilOp,
880 kAlwaysIfInClip_StencilFunc,
881 0x0000,
882 0x0000,
883 0x0000);
884 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
885}
886}
887
888void GrClipMaskManager::setGpuStencil() {
889 // We make two copies of the StencilSettings here (except in the early
890 // exit scenario. One copy from draw state to the stack var. Then another
891 // from the stack var to the gpu. We could make this class hold a ptr to
892 // GrGpu's fStencilSettings and eliminate the stack copy here.
893
894 const GrDrawState& drawState = fGpu->getDrawState();
895
896 // use stencil for clipping if clipping is enabled and the clip
897 // has been written into the stencil.
898 GrClipMaskManager::StencilClipMode clipMode;
899 if (this->isClipInStencil() && drawState.isClipState()) {
900 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
901 // We can't be modifying the clip and respecting it at the same time.
902 GrAssert(!drawState.isStateFlagEnabled(
903 GrGpu::kModifyStencilClip_StateBit));
904 } else if (drawState.isStateFlagEnabled(
905 GrGpu::kModifyStencilClip_StateBit)) {
906 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
907 } else {
908 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
909 }
910
911 GrStencilSettings settings;
912 // The GrGpu client may not be using the stencil buffer but we may need to
913 // enable it in order to respect a stencil clip.
914 if (drawState.getStencil().isDisabled()) {
915 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
916 settings = basic_apply_stencil_clip_settings();
917 } else {
918 fGpu->disableStencil();
919 return;
920 }
921 } else {
922 settings = drawState.getStencil();
923 }
924
925 // TODO: dynamically attach a stencil buffer
926 int stencilBits = 0;
927 GrStencilBuffer* stencilBuffer =
928 drawState.getRenderTarget()->getStencilBuffer();
929 if (NULL != stencilBuffer) {
930 stencilBits = stencilBuffer->bits();
931 }
932
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000933 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
934 !settings.usesWrapOp());
935 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000936 this->adjustStencilParams(&settings, clipMode, stencilBits);
937 fGpu->setStencilSettings(settings);
938}
939
940void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
941 StencilClipMode mode,
942 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000943 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000944
945 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000946 // We assume that this clip manager itself is drawing to the GrGpu and
947 // has already setup the correct values.
948 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000949 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950
bsalomon@google.com411dad02012-06-05 20:24:20 +0000951 unsigned int clipBit = (1 << (stencilBitCnt - 1));
952 unsigned int userBits = clipBit - 1;
953
bsalomon@google.coma3201942012-06-21 19:58:20 +0000954 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
955 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000956
bsalomon@google.coma3201942012-06-21 19:58:20 +0000957 bool finished = false;
958 while (!finished) {
959 GrStencilFunc func = settings->func(face);
960 uint16_t writeMask = settings->writeMask(face);
961 uint16_t funcMask = settings->funcMask(face);
962 uint16_t funcRef = settings->funcRef(face);
963
964 GrAssert((unsigned) func < kStencilFuncCount);
965
966 writeMask &= userBits;
967
968 if (func >= kBasicStencilFuncCount) {
969 int respectClip = kRespectClip_StencilClipMode == mode;
970 if (respectClip) {
971 // The GrGpu class should have checked this
972 GrAssert(this->isClipInStencil());
973 switch (func) {
974 case kAlwaysIfInClip_StencilFunc:
975 funcMask = clipBit;
976 funcRef = clipBit;
977 break;
978 case kEqualIfInClip_StencilFunc:
979 case kLessIfInClip_StencilFunc:
980 case kLEqualIfInClip_StencilFunc:
981 funcMask = (funcMask & userBits) | clipBit;
982 funcRef = (funcRef & userBits) | clipBit;
983 break;
984 case kNonZeroIfInClip_StencilFunc:
985 funcMask = (funcMask & userBits) | clipBit;
986 funcRef = clipBit;
987 break;
988 default:
989 GrCrash("Unknown stencil func");
990 }
991 } else {
992 funcMask &= userBits;
993 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000994 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000995 const GrStencilFunc* table =
996 gSpecialToBasicStencilFunc[respectClip];
997 func = table[func - kBasicStencilFuncCount];
998 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000999 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001000 funcMask &= userBits;
1001 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001002 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001003
1004 settings->setFunc(face, func);
1005 settings->setWriteMask(face, writeMask);
1006 settings->setFuncMask(face, funcMask);
1007 settings->setFuncRef(face, funcRef);
1008
1009 if (GrStencilSettings::kFront_Face == face) {
1010 face = GrStencilSettings::kBack_Face;
1011 finished = !twoSided;
1012 } else {
1013 finished = true;
1014 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001015 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016 if (!twoSided) {
1017 settings->copyFrontSettingsToBack();
1018 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001019}
1020
1021////////////////////////////////////////////////////////////////////////////////
1022
robertphillips@google.comfa662942012-05-17 12:20:22 +00001023namespace {
1024
1025GrPathFill invert_fill(GrPathFill fill) {
1026 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001027 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1028 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1029 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1030 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1031 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001032 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001033 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1034 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1035 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1036 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1037 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1038 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001039 return gInvertedFillTable[fill];
1040}
1041
1042}
1043
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001044bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001045 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001046 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001047 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001048
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001049 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001050 return true;
1051 }
1052
robertphillips@google.comf105b102012-05-14 12:18:26 +00001053 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001054 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001055 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001056 return false;
1057 }
1058
robertphillips@google.com2c756812012-05-22 20:28:23 +00001059 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001060
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001061 helper.init(*resultBounds, NULL);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001062
1063 int count = clipIn.getElementCount();
1064
1065 bool clearToInside;
1066 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
1067 int start = process_initial_clip_elements(clipIn,
1068 *resultBounds,
1069 &clearToInside,
1070 &startOp);
1071
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001072 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001073
1074 for (int i = start; i < count; ++i) {
1075
1076 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
1077
1078 if (SkRegion::kIntersect_Op == op ||
1079 SkRegion::kReverseDifference_Op == op) {
1080 // Intersect and reverse difference require modifying pixels
1081 // outside of the geometry that is being "drawn". In both cases
1082 // we erase all the pixels outside of the geometry but
1083 // leave the pixels inside the geometry alone. For reverse
1084 // difference we invert all the pixels before clearing the ones
1085 // outside the geometry.
1086 if (SkRegion::kReverseDifference_Op == op) {
1087 SkRect temp = SkRect::MakeLTRB(
1088 SkIntToScalar(resultBounds->left()),
1089 SkIntToScalar(resultBounds->top()),
1090 SkIntToScalar(resultBounds->right()),
1091 SkIntToScalar(resultBounds->bottom()));
1092
1093 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001094 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001095 }
1096
1097 if (kRect_ClipType == clipIn.getElementType(i)) {
1098
1099 // convert the rect to a path so we can invert the fill
1100 SkPath temp;
1101 temp.addRect(clipIn.getRect(i));
1102
1103 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001104 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001105 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001106 } else {
1107 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1108
1109 helper.draw(clipIn.getPath(i),
1110 SkRegion::kReplace_Op,
1111 invert_fill(clipIn.getPathFill(i)),
1112 clipIn.getDoAA(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001113 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001114 }
1115
1116 continue;
1117 }
1118
1119 // The other ops (union, xor, diff) only affect pixels inside
1120 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001121 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001122
1123 helper.draw(clipIn.getRect(i),
1124 op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001125 clipIn.getDoAA(i), 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001126
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001127 } else {
1128 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1129
robertphillips@google.comfa662942012-05-17 12:20:22 +00001130 helper.draw(clipIn.getPath(i),
1131 op,
1132 clipIn.getPathFill(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001133 clipIn.getDoAA(i), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001134 }
1135 }
1136
robertphillips@google.comfa662942012-05-17 12:20:22 +00001137 // Because we are using the scratch texture cache, "accum" may be
1138 // larger than expected and have some cruft in the areas we aren't using.
1139 // Clear it out.
1140
1141 // TODO: need a simpler way to clear the texture - can we combine
1142 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001143 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001144 GrAssert(NULL != drawState);
1145 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001146 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001147 // can't leave the accum bound as a rendertarget
1148 drawState->setRenderTarget(temp);
1149
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001150 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001151
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001152 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001153
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001154 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001155 return true;
1156}
1157
robertphillips@google.comf294b772012-04-27 14:29:26 +00001158////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001159void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001160 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001161}