blob: fdaa067c9d9ec3451a814033425fd46b335fa10c [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"
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000010#include "effects/GrTextureDomainEffect.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000011#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrStencilBuffer.h"
14#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000015#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000016#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000017#include "GrAAConvexPathRenderer.h"
18#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000020#include "GrCacheID.h"
21
22GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000023
robertphillips@google.comba998f22012-10-12 11:33:56 +000024#define GR_AA_CLIP 1
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +000025#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000026
robertphillips@google.comf294b772012-04-27 14:29:26 +000027////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
29namespace GrReducedClip {
30
31/*
32There are plenty of optimizations that could be added here. For example we could consider
33checking for cases where an inverse path can be changed to a regular fill with a different op.
34(e.g. [kIntersect, inverse path] -> [kDifference, path]). Maybe flips could be folded into
35earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
36for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
37based on later intersect operations, and perhaps remove intersect-rects. We could optionally
38take a rect in case the caller knows a bound on what is to be drawn through this clip.
39*/
40void GrReduceClipStack(const SkClipStack& stack,
41 SkTDArray<SkClipStack::Iter::Clip>* resultClips,
42 SkRect* resultBounds,
43 bool* resultsAreBounded,
44 InitialState* initialState) {
45 resultClips->reset();
46
47 if (stack.isWideOpen()) {
48 *initialState = kAllIn_InitialState;
49 *resultsAreBounded = false;
50 return;
51 }
52
53 SkClipStack::BoundsType type;
54 bool iior;
55 stack.getBounds(resultBounds, &type, &iior);
56 if (iior) {
57 *resultsAreBounded = true;
58 *initialState = kAllOut_InitialState;
59 SkClipStack::Iter::Clip* clip = resultClips->append();
60 // append doesn't call the default cons.
61 *clip = SkClipStack::Iter::Clip();
62
63 // iior should only be true if aa/non-aa status matches.
64 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
65 clip->fDoAA = iter.prev()->fDoAA;
66 clip->fOp = SkRegion::kReplace_Op;
67 clip->fRect = resultBounds;
68 return;
69 }
70
71 *resultsAreBounded = SkClipStack::kNormal_BoundsType == type && !resultBounds->isEmpty();
72
73 // walk backwards until we get to:
74 // a) the beginning
75 // b) an operation that is known to make the bounds all inside/outside
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +000076 // c) a replace operation
bsalomon@google.com51a62862012-11-26 21:19:43 +000077
78 static const InitialState kUnknown_InitialState = static_cast<InitialState>(-1);
79 *initialState = kUnknown_InitialState;
80
81 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
82 // TODO: track these per saved clip so that we can consider them on the forward pass.
83 bool embiggens = false;
84 bool emsmallens = false;
85
86 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
87 while ((kUnknown_InitialState == *initialState)) {
88 const SkClipStack::Iter::Clip* clip = iter.prev();
89 if (NULL == clip) {
90 *initialState = kAllIn_InitialState;
91 break;
92 }
bsalomon@google.comedb26fd2012-11-28 14:42:41 +000093 if (SkClipStack::kEmptyGenID == clip->fGenID) {
bsalomon@google.com51a62862012-11-26 21:19:43 +000094 *initialState = kAllOut_InitialState;
95 break;
96 }
bsalomon@google.comedb26fd2012-11-28 14:42:41 +000097 if (SkClipStack::kWideOpenGenID == clip->fGenID) {
bsalomon@google.com51a62862012-11-26 21:19:43 +000098 *initialState = kAllIn_InitialState;
99 break;
100 }
101
102 bool skippable = false;
103 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
104
105 switch (clip->fOp) {
106 case SkRegion::kDifference_Op:
107 if (*resultsAreBounded) {
108 // check if the shape subtracted either contains the entire bounds (and makes
109 // the clip empty) or is outside the bounds and therefore can be skipped.
110 if (clip->isInverseFilled()) {
111 if (clip->contains(*resultBounds)) {
112 skippable = true;
113 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
114 *initialState = kAllOut_InitialState;
115 skippable = true;
116 }
117 } else {
118 if (clip->contains(*resultBounds)) {
119 *initialState = kAllOut_InitialState;
120 skippable = true;
121 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
122 skippable = true;
123 }
124 }
125 }
126 if (!skippable) {
127 emsmallens = true;
128 }
129 break;
130 case SkRegion::kIntersect_Op:
131 if (*resultsAreBounded) {
132 // check if the shape intersected contains the entire bounds and therefore can
bsalomon@google.com705e8402012-11-27 15:43:57 +0000133 // be skipped or it is outside the entire bounds and therefore makes the clip
bsalomon@google.com51a62862012-11-26 21:19:43 +0000134 // empty.
135 if (clip->isInverseFilled()) {
136 if (clip->contains(*resultBounds)) {
137 *initialState = kAllOut_InitialState;
138 skippable = true;
139 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
140 skippable = true;
141 }
142 } else {
143 if (clip->contains(*resultBounds)) {
144 skippable = true;
145 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
146 *initialState = kAllOut_InitialState;
147 skippable = true;
148 }
149 }
150 }
151 if (!skippable) {
152 emsmallens = true;
153 }
154 break;
155 case SkRegion::kUnion_Op:
156 if (*resultsAreBounded) {
bsalomon@google.com705e8402012-11-27 15:43:57 +0000157 // If the union-ed shape contains the entire bounds then after this element
158 // the bounds is entirely inside the clip. If the union-ed shape is outside the
bsalomon@google.com51a62862012-11-26 21:19:43 +0000159 // bounds then this op can be skipped.
160 if (clip->isInverseFilled()) {
161 if (clip->contains(*resultBounds)) {
162 skippable = true;
163 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
164 *initialState = kAllIn_InitialState;
165 skippable = true;
166 }
167 } else {
168 if (clip->contains(*resultBounds)) {
169 *initialState = kAllIn_InitialState;
170 skippable = true;
171 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
172 skippable = true;
173 }
174 }
175 }
176 if (!skippable) {
177 embiggens = true;
178 }
179 break;
180 case SkRegion::kXOR_Op:
181 if (*resultsAreBounded) {
bsalomon@google.com705e8402012-11-27 15:43:57 +0000182 // If the bounds is entirely inside the shape being xor-ed then the effect is
183 // to flip the inside/outside state of every point in the bounds. We may be
184 // able to take advantage of this in the forward pass. If the xor-ed shape
185 // doesn't intersect the bounds then it can be skipped.
bsalomon@google.com51a62862012-11-26 21:19:43 +0000186 if (clip->isInverseFilled()) {
187 if (clip->contains(*resultBounds)) {
188 skippable = true;
189 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
190 isFlip = true;
191 }
192 } else {
193 if (clip->contains(*resultBounds)) {
194 isFlip = true;
195 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
196 skippable = true;
197 }
198 }
199 }
200 if (!skippable) {
201 emsmallens = embiggens = true;
202 }
203 break;
204 case SkRegion::kReverseDifference_Op:
bsalomon@google.com705e8402012-11-27 15:43:57 +0000205 // When the bounds is entirely within the rev-diff shape then this behaves like xor
206 // and reverses every point inside the bounds. If the shape is completely outside
207 // the bounds then we know after this element is applied that the bounds will be
208 // all outside the current clip.B
bsalomon@google.com51a62862012-11-26 21:19:43 +0000209 if (*resultsAreBounded) {
210 if (clip->isInverseFilled()) {
211 if (clip->contains(*resultBounds)) {
212 *initialState = kAllOut_InitialState;
213 skippable = true;
214 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
215 isFlip = true;
216 }
217 } else {
218 if (clip->contains(*resultBounds)) {
219 isFlip = true;
220 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
221 *initialState = kAllOut_InitialState;
222 skippable = true;
223 }
224 }
225 }
226 if (!skippable) {
227 emsmallens = embiggens = true;
228 }
229 break;
230 case SkRegion::kReplace_Op:
bsalomon@google.com705e8402012-11-27 15:43:57 +0000231 // Replace will always terminate our walk. We will either begin the forward walk
232 // at the replace op or detect here than the shape is either completely inside
233 // or completely outside the bounds. In this latter case it can be skipped by
234 // setting the correct value for initialState.
skia.committer@gmail.comab38f7a2012-11-28 02:02:11 +0000235 if (*resultsAreBounded) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000236 if (clip->isInverseFilled()) {
237 if (clip->contains(*resultBounds)) {
238 *initialState = kAllOut_InitialState;
239 skippable = true;
240 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
241 *initialState = kAllIn_InitialState;
242 skippable = true;
243 }
244 } else {
245 if (clip->contains(*resultBounds)) {
246 *initialState = kAllIn_InitialState;
247 skippable = true;
248 } else if (!SkRect::Intersects(clip->getBounds(), *resultBounds)) {
249 *initialState = kAllOut_InitialState;
250 skippable = true;
251 }
252 }
253 }
254 if (!skippable) {
255 *initialState = kAllOut_InitialState;
256 embiggens = emsmallens = true;
257 }
258 break;
259 default:
260 SkDEBUGFAIL("Unexpected op.");
261 break;
262 }
263 if (!skippable) {
264 SkClipStack::Iter::Clip* newClip = resultClips->prepend();
265 // if it is a flip, change it to a bounds-filling rect
266 if (isFlip) {
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000267 SkASSERT(SkRegion::kXOR_Op == clip->fOp ||
bsalomon@google.com51a62862012-11-26 21:19:43 +0000268 SkRegion::kReverseDifference_Op == clip->fOp);
269 newClip->fPath = NULL;
270 newClip->fRect = resultBounds;
271 // assuming this is faster to perform on GPU with stenciling than xor.
272 newClip->fOp = SkRegion::kReverseDifference_Op;
273 newClip->fDoAA = false;
274 newClip->fGenID = SkClipStack::kInvalidGenID;
275 } else {
276 *newClip = *clip;
277 }
278 }
279 }
280
281 if ((kAllOut_InitialState == *initialState && !embiggens) ||
282 (kAllIn_InitialState == *initialState && !emsmallens)) {
283 resultClips->reset();
284 } else {
285 int clipsToSkip = 0;
286 while (1) {
287 SkClipStack::Iter::Clip* clip = &(*resultClips)[clipsToSkip];
288 bool skippable = false;
289 switch (clip->fOp) {
290 case SkRegion::kDifference_Op:
bsalomon@google.com705e8402012-11-27 15:43:57 +0000291 // subtracting from the empty set yields the empty set.
bsalomon@google.com51a62862012-11-26 21:19:43 +0000292 skippable = kAllOut_InitialState == *initialState;
293 break;
294 case SkRegion::kIntersect_Op:
bsalomon@google.com705e8402012-11-27 15:43:57 +0000295 // intersecting with the empty set yields the empty set
bsalomon@google.com51a62862012-11-26 21:19:43 +0000296 skippable = kAllOut_InitialState == *initialState;
297 break;
298 case SkRegion::kUnion_Op:
299 if (kAllIn_InitialState == *initialState) {
300 // unioning the infinite plane with anything is a no-op.
301 skippable = true;
302 } else {
303 // unioning the empty set with a shape is the shape.
304 clip->fOp = SkRegion::kReplace_Op;
305 }
306 break;
307 case SkRegion::kXOR_Op:
308 if (kAllOut_InitialState == *initialState) {
309 // xor could be changed to diff in the kAllIn case, not sure it's a win.
310 clip->fOp = SkRegion::kReplace_Op;
311 }
312 break;
313 case SkRegion::kReverseDifference_Op:
314 if (kAllIn_InitialState == *initialState) {
315 // subtracting the whole plane will yield the empty set.
316 skippable = true;
317 *initialState = kAllOut_InitialState;
318 } else {
319 // this picks up flips inserted in the backwards pass.
320 if (*resultsAreBounded && NULL != clip->fRect) {
321 skippable = clip->isInverseFilled() ?
322 !SkRect::Intersects(clip->getBounds(), *resultBounds) :
323 clip->contains(*resultBounds);
324 }
325 if (skippable) {
326 *initialState = kAllIn_InitialState;
327 } else {
328 clip->fOp = SkRegion::kReplace_Op;
329 }
330 }
331 break;
332 case SkRegion::kReplace_Op:
333 SkASSERT(!clipsToSkip); // replace should always be the first op
334 skippable = false; // we would have skipped it in the backwards walk if we
335 // could've.
336 break;
337 default:
338 SkDEBUGFAIL("Unexpected op.");
339 break;
340 }
341 if (!skippable) {
342 break;
343 } else {
344 ++clipsToSkip;
345 if (clipsToSkip == resultClips->count()) {
346 break;
347 }
348 }
349 }
350 resultClips->remove(0, clipsToSkip);
351 }
352}
353} // namespace GrReducedClip
354
355////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000356namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000357// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +0000358// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000359void setup_drawstate_aaclip(GrGpu* gpu,
360 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000361 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000362 GrDrawState* drawState = gpu->drawState();
363 GrAssert(drawState);
364
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000365 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000366
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000367 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000368 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000369 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000370 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000371 mat.preConcat(drawState->getViewMatrix());
372
bsalomon@google.com08283af2012-10-26 13:01:20 +0000373 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000374
375 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
376 drawState->stage(kMaskStage)->setEffect(
377 GrTextureDomainEffect::Create(result,
378 mat,
379 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
380 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000381}
382
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000383bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000384 GrGpu* gpu,
385 const SkPath& path,
386 GrPathFill fill,
387 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000388 // last (false) parameter disallows use of the SW path renderer
389 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
390}
391
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000392GrPathFill get_path_fill(const SkPath& path) {
393 switch (path.getFillType()) {
394 case SkPath::kWinding_FillType:
395 return kWinding_GrPathFill;
396 case SkPath::kEvenOdd_FillType:
397 return kEvenOdd_GrPathFill;
398 case SkPath::kInverseWinding_FillType:
399 return kInverseWinding_GrPathFill;
400 case SkPath::kInverseEvenOdd_FillType:
401 return kInverseEvenOdd_GrPathFill;
402 default:
403 GrCrash("Unsupported path fill in clip.");
404 return kWinding_GrPathFill; // suppress warning
405 }
406}
407
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000408/**
409 * Does any individual clip in 'clipIn' use anti-aliasing?
410 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000411bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000412
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000413 SkClipStack::Iter iter;
414 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000415
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000416 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000417 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
418 NULL != clip;
bsalomon@google.com5fac58c2012-11-29 21:14:59 +0000419 clip = iter.next()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000420
421 if (clip->fDoAA) {
422 return true;
423 }
424 }
425
426 return false;
427}
428
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000429}
430
robertphillips@google.comfa662942012-05-17 12:20:22 +0000431/*
432 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
433 * will be used on any element. If so, it returns true to indicate that the
434 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
435 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000436bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000437
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000438 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000439 // a clip gets complex enough it can just be done in SW regardless
440 // of whether it would invoke the GrSoftwarePathRenderer.
441 bool useSW = false;
442
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000443 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
444 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000445
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000446 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
447 NULL != clip;
bsalomon@google.com5fac58c2012-11-29 21:14:59 +0000448 clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000449
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000450 // rects can always be drawn directly w/o using the software path
451 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000452 if (NULL != clip->fPath &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000453 path_needs_SW_renderer(this->getContext(), fGpu,
454 *clip->fPath,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000455 get_path_fill(*clip->fPath),
456 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000457 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000458 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000459 }
460
461 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000462}
463
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000465// sort out what kind of clip mask needs to be created: alpha, stencil,
466// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000467bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000468 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000469
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000470 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000471 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000472 fGpu->disableScissor();
473 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000474 return true;
475 }
476
477 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000478 // GrDrawTarget should have filtered this for us
479 GrAssert(NULL != rt);
480
robertphillips@google.com7b112892012-07-31 15:18:21 +0000481 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000482 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000483
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000484 clipDataIn->getConservativeBounds(rt, &devClipBounds, &isIntersectionOfRects);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000485 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000486 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000487 }
488
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000489#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000490 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000491
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000492 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000493 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000494 // in software (this will only happen if the clip mask is anti-aliased
495 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000496 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000497 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000498 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000499 // The clip geometry is complex enough that it will be more
500 // efficient to create it entirely in software
501 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000502 GrIRect devBound;
503 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
504 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000505 fGpu->disableScissor();
506 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000507 return true;
508 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000509
510 // if SW clip mask creation fails fall through to the other
511 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000512 }
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000513#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000514
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000515#if GR_AA_CLIP
robertphillips@google.comf294b772012-04-27 14:29:26 +0000516 // If MSAA is enabled use the (faster) stencil path for AA clipping
517 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000518 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000519 // Since we are going to create a destination texture of the correct
520 // size for the mask (rather than being bound by the size of the
521 // render target) we aren't going to use scissoring like the stencil
522 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000523 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000524 GrIRect devBound;
525 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
526 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000527 fGpu->disableScissor();
528 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000529 return true;
530 }
531
532 // if alpha clip mask creation fails fall through to the stencil
533 // buffer method
534 }
535#endif // GR_AA_CLIP
536
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000537 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000538 // an antialiased clip couldn't be created. In either case, free up
539 // the texture in the antialiased mask cache.
540 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000541 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000542 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000543 // AA cache.
544 fAACache.reset();
545
bsalomon@google.coma3201942012-06-21 19:58:20 +0000546 // If the clip is a rectangle then just set the scissor. Otherwise, create
547 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000548 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000549 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000550 this->setGpuStencil();
551 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000552 }
553
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000554 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000555 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000556 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000557
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000558 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000559 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000560 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000561 // This must occur after createStencilClipMask. That function may change
562 // the scissor. Also, it only guarantees that the stencil mask is correct
563 // within the bounds it was passed, so we must use both stencil and scissor
564 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000565 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000566 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000567 return true;
568}
569
570#define VISUALIZE_COMPLEX_CLIP 0
571
572#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000573 #include "SkRandom.h"
574 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000575 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
576#else
577 #define SET_RANDOM_COLOR
578#endif
579
580namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000581/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000582 * Does "canvContainer" contain "devContainee"? If either is empty then
583 * no containment is possible. "canvContainer" is in canvas coordinates while
584 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000585 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000586 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000587bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000588 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000589 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000590 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000591 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000592 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000593 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000594 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000595}
596
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000598// determines how many elements at the head of the clip can be skipped and
599// whether the initial clear should be to the inside- or outside-the-clip value,
600// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000601const SkClipStack::Iter::Clip* process_initial_clip_elements(
602 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000603 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000605 SkRegion::Op* firstOp,
606 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000607
608 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000610 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611 // processed the clip is entirely open. However, depending on the
612 // first set op we may prefer to clear to 0 for performance. We may
613 // also be able to skip the initial clip paths/rects. We loop until
614 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000615 bool done = false;
616 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000617
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000618 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000619
620 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
621 NULL != clip && !done;
bsalomon@google.com5fac58c2012-11-29 21:14:59 +0000622 clip = iter->next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000623 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000624 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000625 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000626 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627 *clearToInside = false;
628 done = true;
629 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000630 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631 // if this element contains the entire bounds then we
632 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000633 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000634 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000635 break;
636 }
637 // if everything is initially clearToInside then intersect is
638 // same as clear to 0 and treat as a replace. Otherwise,
639 // set stays empty.
640 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000641 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000642 *clearToInside = false;
643 done = true;
644 }
645 break;
646 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000647 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000649 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000650 // clearToInside
651 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000652 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000653 done = true;
654 }
655 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000656 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000657 // xor is same as difference or replace both of which
658 // can be 1-pass instead of 2 for xor.
659 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000660 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000661 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000662 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 }
664 done = true;
665 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000666 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667 // if all pixels are clearToInside then we have to process the
668 // difference, otherwise it has no effect and all pixels
669 // remain outside.
670 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000671 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000672 done = true;
673 }
674 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000675 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676 // if all pixels are clearToInside then reverse difference
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000677 // produces empty set. Otherise it is same as replace
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678 if (*clearToInside) {
679 *clearToInside = false;
680 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000681 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682 done = true;
683 }
684 break;
685 default:
686 GrCrash("Unknown set op.");
687 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000688
689 if (done) {
690 // we need to break out here (rather than letting the test in
691 // the loop do it) since backing up the iterator is very expensive
692 break;
693 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000695 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000697
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698}
699
robertphillips@google.comf294b772012-04-27 14:29:26 +0000700namespace {
701
702////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000703// set up the OpenGL blend function to perform the specified
704// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000705void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000706
707 switch (op) {
708 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000709 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000710 break;
711 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000712 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000713 break;
714 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000715 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000716 break;
717 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000718 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000719 break;
720 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000721 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000722 break;
723 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000724 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000725 break;
726 default:
727 GrAssert(false);
728 break;
729 }
730}
731
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000732////////////////////////////////////////////////////////////////////////////////
733bool draw_path_in_software(GrContext* context,
734 GrGpu* gpu,
735 const SkPath& path,
736 GrPathFill fill,
737 bool doAA,
738 const GrIRect& resultBounds) {
739
740 SkAutoTUnref<GrTexture> texture(
741 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
742 resultBounds, fill,
743 doAA, NULL));
744 if (NULL == texture) {
745 return false;
746 }
747
748 // The ClipMaskManager accumulates the clip mask in the UL corner
749 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
750
751 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
752
753 GrAssert(!GrIsFillInverted(fill));
754 return true;
755}
756
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000757
758////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000759bool draw_path(GrContext* context,
760 GrGpu* gpu,
761 const SkPath& path,
762 GrPathFill fill,
763 bool doAA,
764 const GrIRect& resultBounds) {
765
766 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
767 if (NULL == pr) {
768 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
769 }
770
771 pr->drawPath(path, fill, gpu, doAA);
772 return true;
773}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000774
robertphillips@google.com7b112892012-07-31 15:18:21 +0000775// 'rect' enters in device coordinates and leaves in canvas coordinates
776void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
777 GrAssert(NULL != rect);
778
779 rect->fLeft += SkIntToScalar(origin.fX);
780 rect->fTop += SkIntToScalar(origin.fY);
781 rect->fRight += SkIntToScalar(origin.fX);
782 rect->fBottom += SkIntToScalar(origin.fY);
783}
784
robertphillips@google.com72176b22012-05-23 13:19:12 +0000785}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000786
787////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000788bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000789 const SkClipStack::Iter::Clip* clip,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000790 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000791 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000792 GrAssert(NULL != drawState);
793
794 drawState->setRenderTarget(target->asRenderTarget());
795
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000796 if (NULL != clip->fRect) {
797 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000798 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000799 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000800 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000801 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000802 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000803 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000804 } else if (NULL != clip->fPath) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000805 return draw_path(this->getContext(), fGpu,
806 *clip->fPath,
807 get_path_fill(*clip->fPath),
808 clip->fDoAA,
809 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000810 }
811 return true;
812}
813
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000814void GrClipMaskManager::mergeMask(GrTexture* dstMask,
815 GrTexture* srcMask,
816 SkRegion::Op op,
817 const GrIRect& dstBound,
818 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000819 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000820 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000821 SkMatrix oldMatrix = drawState->getViewMatrix();
822 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000823
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000824 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000825
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000826 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000827
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000828 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000829 sampleM.setIDiv(srcMask->width(), srcMask->height());
830 drawState->stage(0)->setEffect(
831 GrTextureDomainEffect::Create(srcMask,
832 sampleM,
833 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
834 GrTextureDomainEffect::kDecal_WrapMode))->unref();
835 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000836
tomhudson@google.com676e6602012-07-10 17:21:48 +0000837 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000838 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000839}
840
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000841// get a texture to act as a temporary buffer for AA clip boolean operations
842// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000843void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000844 GrAutoScratchTexture* temp) {
845 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000846 // we've already allocated the temp texture
847 return;
848 }
849
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000850 GrTextureDesc desc;
851 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
852 desc.fWidth = bounds.width();
853 desc.fHeight = bounds.height();
854 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000855
robertphillips@google.com2c756812012-05-22 20:28:23 +0000856 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000857}
858
robertphillips@google.comf105b102012-05-14 12:18:26 +0000859
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000860void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000861 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000862 // Since we are setting up the cache we know the last lookup was a miss
863 // Free up the currently cached mask so it can be reused
864 fAACache.reset();
865
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000866 GrTextureDesc desc;
867 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
868 desc.fWidth = bounds.width();
869 desc.fHeight = bounds.height();
870 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000871
872 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000873}
874
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000875////////////////////////////////////////////////////////////////////////////////
876// Shared preamble between gpu and SW-only AA clip mask creation paths.
877// Handles caching, determination of clip mask bound & allocation (if needed)
878// of the result texture
879// Returns true if there is no more work to be done (i.e., we got a cache hit)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000880bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000881 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000882 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000883 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000884 GrAssert(origDrawState->isClipState());
885
886 GrRenderTarget* rt = origDrawState->getRenderTarget();
887 GrAssert(NULL != rt);
888
robertphillips@google.comf294b772012-04-27 14:29:26 +0000889 // unlike the stencil path the alpha path is not bound to the size of the
890 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000891 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000892 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000893
894 // need to outset a pixel since the standard bounding box computation
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000895 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000896 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000897
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000898 // TODO: make sure we don't outset if bounds are still 0,0 @ min
899
robertphillips@google.comba998f22012-10-12 11:33:56 +0000900 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000901 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000902 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000903 return true;
904 }
905
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000906 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000907 return false;
908}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000909
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000910////////////////////////////////////////////////////////////////////////////////
911// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000912bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000913 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000914 GrIRect *devResultBounds) {
915 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000916 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
917
robertphillips@google.com7b112892012-07-31 15:18:21 +0000918 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000919 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000920 return true;
921 }
922
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000923 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
924
robertphillips@google.comf105b102012-05-14 12:18:26 +0000925 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000926 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000927 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000928 return false;
929 }
930
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000931 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
932 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000933
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000934 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000935
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000936 // The mask we generate is translated so that its upper-left corner is at devResultBounds
937 // upper-left corner in device space.
938 GrIRect maskResultBounds = GrIRect::MakeWH(devResultBounds->width(), devResultBounds->height());
939
940 // Set the matrix so that rendered clip elements are transformed from the space of the clip
941 // stack to the alpha-mask. This accounts for both translation due to the clip-origin and the
942 // placement of the mask within the device.
943 SkVector clipToMaskOffset = {
944 SkIntToScalar(-devResultBounds->fLeft - clipDataIn.fOrigin.fX),
945 SkIntToScalar(-devResultBounds->fTop - clipDataIn.fOrigin.fY)
946 };
947 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000948
949 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000950 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
951
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000952 SkClipStack::Iter iter(*clipDataIn.fClipStack,
953 SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000954 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000955 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000956 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000957 &firstOp,
958 clipDataIn);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000959 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
960 // clear the part that we care about.
961 fGpu->clear(&maskResultBounds,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000962 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000963 accum->asRenderTarget());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000964 bool accumClearedToZero = !clearToInside;
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000965
robertphillips@google.comf105b102012-05-14 12:18:26 +0000966 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000967 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000968 // walk through each clip element and perform its set op
bsalomon@google.com5fac58c2012-11-29 21:14:59 +0000969 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000970
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000971 SkRegion::Op op = clip->fOp;
972 if (first) {
973 first = false;
974 op = firstOp;
975 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000976
bsalomon@google.com6794a252012-11-08 15:30:53 +0000977 if (SkRegion::kReplace_Op == op) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000978 // clear the accumulator and draw the new object directly into it
979 if (!accumClearedToZero) {
980 fGpu->clear(&maskResultBounds, 0x00000000, accum->asRenderTarget());
981 }
982
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000983 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000984 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000985
986 } else if (SkRegion::kReverseDifference_Op == op ||
987 SkRegion::kIntersect_Op == op) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000988 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000989 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000990 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000991 continue;
992 }
993
robertphillips@google.com7b112892012-07-31 15:18:21 +0000994 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000995 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000996 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000997 return false;
998 }
999
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001000 // this is the bounds of the clip element in the space of the alpha-mask. The temporary
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001001 // mask buffer can be substantially larger than the actually clip stack element. We
1002 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001003 // the accumulator
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001004 GrRect elementMaskBounds = clip->getBounds();
1005 elementMaskBounds.offset(clipToMaskOffset);
1006 GrIRect elementMaskIBounds;
1007 elementMaskBounds.roundOut(&elementMaskIBounds);
1008
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001009 // clear the temp target & draw into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001010 fGpu->clear(&elementMaskIBounds, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +00001011
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001012 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001013 this->drawClipShape(temp.texture(), clip, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +00001014
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001015 // Now draw into the accumulator using the real operation
1016 // and the temp buffer as a texture
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001017 this->mergeMask(accum, temp.texture(), op, maskResultBounds, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +00001018 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001019 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +00001020 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001021 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001022 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +00001023 }
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001024 accumClearedToZero = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +00001025 }
1026
robertphillips@google.coma72eef32012-05-01 17:22:59 +00001027 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001028 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +00001029 return true;
1030}
1031
1032////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001033// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001034// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001035bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001036 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001037
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001038 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001039
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001040 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001041 GrAssert(drawState->isClipState());
1042
1043 GrRenderTarget* rt = drawState->getRenderTarget();
1044 GrAssert(NULL != rt);
1045
1046 // TODO: dynamically attach a SB when needed.
1047 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
1048 if (NULL == stencilBuffer) {
1049 return false;
1050 }
1051
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001052 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001053
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001054 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001055
1056 // we set the current clip to the bounds so that our recursive
1057 // draws are scissored to them. We use the copy of the complex clip
1058 // we just stashed on the SB to render from. We set it back after
1059 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001060 const GrClipData* oldClipData = fGpu->getClip();
1061
robertphillips@google.com7b112892012-07-31 15:18:21 +00001062 // The origin of 'newClipData' is (0, 0) so it is okay to place
1063 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001064 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001065 GrClipData newClipData;
1066 newClipData.fClipStack = &newClipStack;
1067
1068 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001069
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001070 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1071 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001072 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001073 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001074
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001075 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001076 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001077 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +00001078 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001079 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001080 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001081 }
1082
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001083#if !VISUALIZE_COMPLEX_CLIP
1084 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
1085#endif
1086
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001087 int clipBit = stencilBuffer->bits();
1088 SkASSERT((clipBit <= 16) &&
1089 "Ganesh only handles 16b or smaller stencil buffers");
1090 clipBit = (1 << (clipBit-1));
1091
robertphillips@google.com7b112892012-07-31 15:18:21 +00001092 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001093
1094 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001095 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1096
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001097 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001098 SkClipStack::Iter::kBottom_IterStart);
1099 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001100 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001101 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001102 &firstOp,
1103 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001104
robertphillips@google.com7b112892012-07-31 15:18:21 +00001105 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001106 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001107
1108 // walk through each clip element and perform its set op
1109 // with the existing clip.
bsalomon@google.com5fac58c2012-11-29 21:14:59 +00001110 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001111 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +00001112 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001113 // enabled at bottom of loop
1114 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001115 // if the target is MSAA then we want MSAA enabled when the clip is soft
1116 if (rt->isMultisampled()) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +00001117 drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001118 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001119
tomhudson@google.com8afae612012-08-14 15:03:35 +00001120 // Can the clip element be drawn directly to the stencil buffer
1121 // with a non-inverted fill rule without extra passes to
1122 // resolve in/out status?
1123 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001124
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001125 SkRegion::Op op = clip->fOp;
1126 if (first) {
1127 first = false;
1128 op = firstOp;
1129 }
robertphillips@google.comf294b772012-04-27 14:29:26 +00001130
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001131 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001132 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001133 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001134 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +00001135 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001136 fillInverted = false;
1137 // there is no point in intersecting a screen filling
1138 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +00001139 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +00001140 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001141 continue;
1142 }
tomhudson@google.com8afae612012-08-14 15:03:35 +00001143 } else {
1144 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001145 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001146 fillInverted = GrIsFillInverted(fill);
1147 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001148 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +00001149 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001150 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +00001151 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001152 return false;
1153 }
1154 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001155 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001156 }
1157
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001158 int passes;
1159 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
1160
1161 bool canDrawDirectToClip; // Given the renderer, the element,
1162 // fill rule, and set operation can
1163 // we render the element directly to
1164 // stencil bit used for clipping.
1165 canDrawDirectToClip =
1166 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +00001167 canRenderDirectToStencil,
1168 clipBit,
1169 fillInverted,
1170 &passes,
1171 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001172
1173 // draw the element to the client stencil bits if necessary
1174 if (!canDrawDirectToClip) {
1175 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
1176 kIncClamp_StencilOp,
1177 kIncClamp_StencilOp,
1178 kAlways_StencilFunc,
1179 0xffff,
1180 0x0000,
1181 0xffff);
1182 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001183 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001184 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001185 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001186 } else {
1187 if (canRenderDirectToStencil) {
1188 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001189 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001190 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001191 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001192 }
1193 }
1194 }
1195
1196 // now we modify the clip bit by rendering either the clip
1197 // element directly or a bounding rect of the entire clip.
1198 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
1199 for (int p = 0; p < passes; ++p) {
1200 *drawState->stencil() = stencilSettings[p];
1201 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001203 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001204 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001205 } else {
1206 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001207 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001208 }
1209 } else {
1210 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001211 // 'devClipBounds' is already in device coordinates so the
1212 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +00001213 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001214 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +00001215 GrRect canvClipBounds;
1216 canvClipBounds.set(devClipBounds);
1217 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
1218 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001219 }
1220 }
1221 }
1222 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001223 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001224 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001225 // set this last because recursive draws may overwrite it back to kNone.
1226 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
1227 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001228 return true;
1229}
1230
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001231
bsalomon@google.com411dad02012-06-05 20:24:20 +00001232// mapping of clip-respecting stencil funcs to normal stencil funcs
1233// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001234static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +00001235 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
1236 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
1237 // In the Clip Funcs
1238 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
1239 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
1240 kLess_StencilFunc, // kLessIfInClip_StencilFunc
1241 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
1242 // Special in the clip func that forces user's ref to be 0.
1243 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
1244 // make ref 0 and do normal nequal.
1245 },
1246 {// Stencil-Clipping is ENABLED
1247 // In the Clip Funcs
1248 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
1249 // eq stencil clip bit, mask
1250 // out user bits.
1251
1252 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
1253 // add stencil bit to mask and ref
1254
1255 kLess_StencilFunc, // kLessIfInClip_StencilFunc
1256 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
1257 // for both of these we can add
1258 // the clip bit to the mask and
1259 // ref and compare as normal
1260 // Special in the clip func that forces user's ref to be 0.
1261 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
1262 // make ref have only the clip bit set
1263 // and make comparison be less
1264 // 10..0 < 1..user_bits..
1265 }
1266};
1267
bsalomon@google.coma3201942012-06-21 19:58:20 +00001268namespace {
1269// Sets the settings to clip against the stencil buffer clip while ignoring the
1270// client bits.
1271const GrStencilSettings& basic_apply_stencil_clip_settings() {
1272 // stencil settings to use when clip is in stencil
1273 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1274 kKeep_StencilOp,
1275 kKeep_StencilOp,
1276 kAlwaysIfInClip_StencilFunc,
1277 0x0000,
1278 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001279 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001280 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1281}
1282}
1283
1284void GrClipMaskManager::setGpuStencil() {
1285 // We make two copies of the StencilSettings here (except in the early
1286 // exit scenario. One copy from draw state to the stack var. Then another
1287 // from the stack var to the gpu. We could make this class hold a ptr to
1288 // GrGpu's fStencilSettings and eliminate the stack copy here.
1289
1290 const GrDrawState& drawState = fGpu->getDrawState();
1291
1292 // use stencil for clipping if clipping is enabled and the clip
1293 // has been written into the stencil.
1294 GrClipMaskManager::StencilClipMode clipMode;
1295 if (this->isClipInStencil() && drawState.isClipState()) {
1296 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1297 // We can't be modifying the clip and respecting it at the same time.
1298 GrAssert(!drawState.isStateFlagEnabled(
1299 GrGpu::kModifyStencilClip_StateBit));
1300 } else if (drawState.isStateFlagEnabled(
1301 GrGpu::kModifyStencilClip_StateBit)) {
1302 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1303 } else {
1304 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1305 }
1306
1307 GrStencilSettings settings;
1308 // The GrGpu client may not be using the stencil buffer but we may need to
1309 // enable it in order to respect a stencil clip.
1310 if (drawState.getStencil().isDisabled()) {
1311 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1312 settings = basic_apply_stencil_clip_settings();
1313 } else {
1314 fGpu->disableStencil();
1315 return;
1316 }
1317 } else {
1318 settings = drawState.getStencil();
1319 }
1320
1321 // TODO: dynamically attach a stencil buffer
1322 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001323 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001324 drawState.getRenderTarget()->getStencilBuffer();
1325 if (NULL != stencilBuffer) {
1326 stencilBits = stencilBuffer->bits();
1327 }
1328
bsalomon@google.comf6601872012-08-28 21:11:35 +00001329 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001330 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001331 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001332 this->adjustStencilParams(&settings, clipMode, stencilBits);
1333 fGpu->setStencilSettings(settings);
1334}
1335
1336void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1337 StencilClipMode mode,
1338 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001339 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001340
1341 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001342 // We assume that this clip manager itself is drawing to the GrGpu and
1343 // has already setup the correct values.
1344 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001345 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001346
bsalomon@google.com411dad02012-06-05 20:24:20 +00001347 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1348 unsigned int userBits = clipBit - 1;
1349
bsalomon@google.coma3201942012-06-21 19:58:20 +00001350 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001351 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001352
bsalomon@google.coma3201942012-06-21 19:58:20 +00001353 bool finished = false;
1354 while (!finished) {
1355 GrStencilFunc func = settings->func(face);
1356 uint16_t writeMask = settings->writeMask(face);
1357 uint16_t funcMask = settings->funcMask(face);
1358 uint16_t funcRef = settings->funcRef(face);
1359
1360 GrAssert((unsigned) func < kStencilFuncCount);
1361
1362 writeMask &= userBits;
1363
1364 if (func >= kBasicStencilFuncCount) {
1365 int respectClip = kRespectClip_StencilClipMode == mode;
1366 if (respectClip) {
1367 // The GrGpu class should have checked this
1368 GrAssert(this->isClipInStencil());
1369 switch (func) {
1370 case kAlwaysIfInClip_StencilFunc:
1371 funcMask = clipBit;
1372 funcRef = clipBit;
1373 break;
1374 case kEqualIfInClip_StencilFunc:
1375 case kLessIfInClip_StencilFunc:
1376 case kLEqualIfInClip_StencilFunc:
1377 funcMask = (funcMask & userBits) | clipBit;
1378 funcRef = (funcRef & userBits) | clipBit;
1379 break;
1380 case kNonZeroIfInClip_StencilFunc:
1381 funcMask = (funcMask & userBits) | clipBit;
1382 funcRef = clipBit;
1383 break;
1384 default:
1385 GrCrash("Unknown stencil func");
1386 }
1387 } else {
1388 funcMask &= userBits;
1389 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001390 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001391 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001392 gSpecialToBasicStencilFunc[respectClip];
1393 func = table[func - kBasicStencilFuncCount];
1394 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001395 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001396 funcMask &= userBits;
1397 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001398 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001399
1400 settings->setFunc(face, func);
1401 settings->setWriteMask(face, writeMask);
1402 settings->setFuncMask(face, funcMask);
1403 settings->setFuncRef(face, funcRef);
1404
1405 if (GrStencilSettings::kFront_Face == face) {
1406 face = GrStencilSettings::kBack_Face;
1407 finished = !twoSided;
1408 } else {
1409 finished = true;
1410 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001411 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001412 if (!twoSided) {
1413 settings->copyFrontSettingsToBack();
1414 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001415}
1416
1417////////////////////////////////////////////////////////////////////////////////
1418
robertphillips@google.comfa662942012-05-17 12:20:22 +00001419namespace {
1420
1421GrPathFill invert_fill(GrPathFill fill) {
1422 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001423 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1424 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1425 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1426 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1427 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001428 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001429 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1430 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1431 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1432 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1433 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1434 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001435 return gInvertedFillTable[fill];
1436}
1437
1438}
1439
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001440bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001441 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001442 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001443 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001444
robertphillips@google.com7b112892012-07-31 15:18:21 +00001445 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001446 return true;
1447 }
1448
robertphillips@google.comf105b102012-05-14 12:18:26 +00001449 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001450 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001451 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001452 return false;
1453 }
1454
robertphillips@google.com2c756812012-05-22 20:28:23 +00001455 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001456
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001457 SkMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001458 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001459 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001460 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001461
robertphillips@google.comfa662942012-05-17 12:20:22 +00001462 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001463 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1464
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001465 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001466 SkClipStack::Iter::kBottom_IterStart);
1467 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001468 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001469 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001470 &firstOp,
1471 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001472
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001473 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001474
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001475 bool first = true;
bsalomon@google.com5fac58c2012-11-29 21:14:59 +00001476 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001477
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001478 SkRegion::Op op = clip->fOp;
1479 if (first) {
1480 first = false;
1481 op = firstOp;
1482 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001483
1484 if (SkRegion::kIntersect_Op == op ||
1485 SkRegion::kReverseDifference_Op == op) {
1486 // Intersect and reverse difference require modifying pixels
1487 // outside of the geometry that is being "drawn". In both cases
1488 // we erase all the pixels outside of the geometry but
1489 // leave the pixels inside the geometry alone. For reverse
1490 // difference we invert all the pixels before clearing the ones
1491 // outside the geometry.
1492 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001493 SkRect temp;
1494 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001495 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1496 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001497
1498 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001499 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001500 }
1501
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001502 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001503
1504 // convert the rect to a path so we can invert the fill
1505 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001506 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001507
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001508 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001509 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001510 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001511 } else if (NULL != clip->fPath) {
1512 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001513 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001514 invert_fill(get_path_fill(*clip->fPath)),
1515 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001516 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001517 }
1518
1519 continue;
1520 }
1521
1522 // The other ops (union, xor, diff) only affect pixels inside
1523 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001524 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001525
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001526 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001527 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001528 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001529
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001530 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001531 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001532 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001533 get_path_fill(*clip->fPath),
1534 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001535 }
1536 }
1537
robertphillips@google.comfa662942012-05-17 12:20:22 +00001538 // Because we are using the scratch texture cache, "accum" may be
1539 // larger than expected and have some cruft in the areas we aren't using.
1540 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001541 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001542
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001543 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001544
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001545 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001546
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001547 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001548 return true;
1549}
1550
robertphillips@google.comf294b772012-04-27 14:29:26 +00001551////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001552void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001553 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001554}