blob: 64ead1a3364c3de64625b38a4bee0bfc64b8c706 [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
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
robertphillips@google.comf294b772012-04-27 14:29:26 +000028////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com51a62862012-11-26 21:19:43 +000029
30namespace GrReducedClip {
31
32/*
33There are plenty of optimizations that could be added here. For example we could consider
34checking for cases where an inverse path can be changed to a regular fill with a different op.
35(e.g. [kIntersect, inverse path] -> [kDifference, path]). Maybe flips could be folded into
36earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
37for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
38based on later intersect operations, and perhaps remove intersect-rects. We could optionally
39take a rect in case the caller knows a bound on what is to be drawn through this clip.
40*/
41void GrReduceClipStack(const SkClipStack& stack,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000042 ElementList* result,
bsalomon@google.com51a62862012-11-26 21:19:43 +000043 SkRect* resultBounds,
44 bool* resultsAreBounded,
45 InitialState* initialState) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +000046 result->reset();
bsalomon@google.com51a62862012-11-26 21:19:43 +000047
48 if (stack.isWideOpen()) {
49 *initialState = kAllIn_InitialState;
50 *resultsAreBounded = false;
51 return;
52 }
53
54 SkClipStack::BoundsType type;
55 bool iior;
56 stack.getBounds(resultBounds, &type, &iior);
57 if (iior) {
58 *resultsAreBounded = true;
59 *initialState = kAllOut_InitialState;
bsalomon@google.com51a62862012-11-26 21:19:43 +000060 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +000061 // iior should only be true if aa/non-aa status matches among all elements.
62 bool doAA = iter.prev()->isAA();
63 SkNEW_INSERT_AT_LLIST_TAIL(result, Element, (*resultBounds, SkRegion::kReplace_Op, doAA));
bsalomon@google.com51a62862012-11-26 21:19:43 +000064 return;
65 }
66
67 *resultsAreBounded = SkClipStack::kNormal_BoundsType == type && !resultBounds->isEmpty();
68
69 // walk backwards until we get to:
70 // a) the beginning
71 // b) an operation that is known to make the bounds all inside/outside
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +000072 // c) a replace operation
bsalomon@google.com51a62862012-11-26 21:19:43 +000073
74 static const InitialState kUnknown_InitialState = static_cast<InitialState>(-1);
75 *initialState = kUnknown_InitialState;
76
77 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
78 // TODO: track these per saved clip so that we can consider them on the forward pass.
79 bool embiggens = false;
80 bool emsmallens = false;
81
82 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
83 while ((kUnknown_InitialState == *initialState)) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +000084 const Element* element = iter.prev();
85 if (NULL == element) {
bsalomon@google.com51a62862012-11-26 21:19:43 +000086 *initialState = kAllIn_InitialState;
87 break;
88 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +000089 if (SkClipStack::kEmptyGenID == element->getGenID()) {
bsalomon@google.com51a62862012-11-26 21:19:43 +000090 *initialState = kAllOut_InitialState;
91 break;
92 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +000093 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
bsalomon@google.com51a62862012-11-26 21:19:43 +000094 *initialState = kAllIn_InitialState;
95 break;
96 }
97
98 bool skippable = false;
99 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
100
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000101 switch (element->getOp()) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000102 case SkRegion::kDifference_Op:
103 if (*resultsAreBounded) {
104 // check if the shape subtracted either contains the entire bounds (and makes
105 // the clip empty) or is outside the bounds and therefore can be skipped.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000106 if (element->isInverseFilled()) {
107 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000108 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000109 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000110 *initialState = kAllOut_InitialState;
111 skippable = true;
112 }
113 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000114 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000115 *initialState = kAllOut_InitialState;
116 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000117 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000118 skippable = true;
119 }
120 }
121 }
122 if (!skippable) {
123 emsmallens = true;
124 }
125 break;
126 case SkRegion::kIntersect_Op:
127 if (*resultsAreBounded) {
128 // check if the shape intersected contains the entire bounds and therefore can
bsalomon@google.com705e8402012-11-27 15:43:57 +0000129 // be skipped or it is outside the entire bounds and therefore makes the clip
bsalomon@google.com51a62862012-11-26 21:19:43 +0000130 // empty.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000131 if (element->isInverseFilled()) {
132 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000133 *initialState = kAllOut_InitialState;
134 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000135 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000136 skippable = true;
137 }
138 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000139 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000140 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000141 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000142 *initialState = kAllOut_InitialState;
143 skippable = true;
144 }
145 }
146 }
147 if (!skippable) {
148 emsmallens = true;
149 }
150 break;
151 case SkRegion::kUnion_Op:
152 if (*resultsAreBounded) {
bsalomon@google.com705e8402012-11-27 15:43:57 +0000153 // If the union-ed shape contains the entire bounds then after this element
154 // the bounds is entirely inside the clip. If the union-ed shape is outside the
bsalomon@google.com51a62862012-11-26 21:19:43 +0000155 // bounds then this op can be skipped.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000156 if (element->isInverseFilled()) {
157 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000158 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000159 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000160 *initialState = kAllIn_InitialState;
161 skippable = true;
162 }
163 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000164 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000165 *initialState = kAllIn_InitialState;
166 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000167 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000168 skippable = true;
169 }
170 }
171 }
172 if (!skippable) {
173 embiggens = true;
174 }
175 break;
176 case SkRegion::kXOR_Op:
177 if (*resultsAreBounded) {
bsalomon@google.com705e8402012-11-27 15:43:57 +0000178 // If the bounds is entirely inside the shape being xor-ed then the effect is
179 // to flip the inside/outside state of every point in the bounds. We may be
180 // able to take advantage of this in the forward pass. If the xor-ed shape
181 // doesn't intersect the bounds then it can be skipped.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000182 if (element->isInverseFilled()) {
183 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000184 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000185 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000186 isFlip = true;
187 }
188 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000189 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000190 isFlip = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000191 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000192 skippable = true;
193 }
194 }
195 }
196 if (!skippable) {
197 emsmallens = embiggens = true;
198 }
199 break;
200 case SkRegion::kReverseDifference_Op:
bsalomon@google.com705e8402012-11-27 15:43:57 +0000201 // When the bounds is entirely within the rev-diff shape then this behaves like xor
202 // and reverses every point inside the bounds. If the shape is completely outside
203 // the bounds then we know after this element is applied that the bounds will be
204 // all outside the current clip.B
bsalomon@google.com51a62862012-11-26 21:19:43 +0000205 if (*resultsAreBounded) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000206 if (element->isInverseFilled()) {
207 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000208 *initialState = kAllOut_InitialState;
209 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000210 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000211 isFlip = true;
212 }
213 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000214 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000215 isFlip = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000216 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000217 *initialState = kAllOut_InitialState;
218 skippable = true;
219 }
220 }
221 }
222 if (!skippable) {
223 emsmallens = embiggens = true;
224 }
225 break;
226 case SkRegion::kReplace_Op:
bsalomon@google.com705e8402012-11-27 15:43:57 +0000227 // Replace will always terminate our walk. We will either begin the forward walk
228 // at the replace op or detect here than the shape is either completely inside
229 // or completely outside the bounds. In this latter case it can be skipped by
230 // setting the correct value for initialState.
skia.committer@gmail.comab38f7a2012-11-28 02:02:11 +0000231 if (*resultsAreBounded) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000232 if (element->isInverseFilled()) {
233 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000234 *initialState = kAllOut_InitialState;
235 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000236 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000237 *initialState = kAllIn_InitialState;
238 skippable = true;
239 }
240 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000241 if (element->contains(*resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000242 *initialState = kAllIn_InitialState;
243 skippable = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000244 } else if (!SkRect::Intersects(element->getBounds(), *resultBounds)) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000245 *initialState = kAllOut_InitialState;
246 skippable = true;
247 }
248 }
249 }
250 if (!skippable) {
251 *initialState = kAllOut_InitialState;
252 embiggens = emsmallens = true;
253 }
254 break;
255 default:
256 SkDEBUGFAIL("Unexpected op.");
257 break;
258 }
259 if (!skippable) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000260 // if it is a flip, change it to a bounds-filling rect
261 if (isFlip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000262 SkASSERT(SkRegion::kXOR_Op == element->getOp() ||
263 SkRegion::kReverseDifference_Op == element->getOp());
264 SkNEW_INSERT_AT_LLIST_HEAD(result,
265 Element,
266 (*resultBounds, SkRegion::kReverseDifference_Op, false));
bsalomon@google.com51a62862012-11-26 21:19:43 +0000267 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000268 result->addToHead(*element);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000269 }
270 }
271 }
272
273 if ((kAllOut_InitialState == *initialState && !embiggens) ||
274 (kAllIn_InitialState == *initialState && !emsmallens)) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000275 result->reset();
bsalomon@google.com51a62862012-11-26 21:19:43 +0000276 } else {
277 int clipsToSkip = 0;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000278 Element* element = result->headIter().get();
279 while (NULL != element) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000280 bool skippable = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000281 switch (element->getOp()) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000282 case SkRegion::kDifference_Op:
bsalomon@google.com705e8402012-11-27 15:43:57 +0000283 // subtracting from the empty set yields the empty set.
bsalomon@google.com51a62862012-11-26 21:19:43 +0000284 skippable = kAllOut_InitialState == *initialState;
285 break;
286 case SkRegion::kIntersect_Op:
bsalomon@google.com705e8402012-11-27 15:43:57 +0000287 // intersecting with the empty set yields the empty set
bsalomon@google.com51a62862012-11-26 21:19:43 +0000288 skippable = kAllOut_InitialState == *initialState;
289 break;
290 case SkRegion::kUnion_Op:
291 if (kAllIn_InitialState == *initialState) {
292 // unioning the infinite plane with anything is a no-op.
293 skippable = true;
294 } else {
295 // unioning the empty set with a shape is the shape.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000296 element->setOp(SkRegion::kReplace_Op);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000297 }
298 break;
299 case SkRegion::kXOR_Op:
300 if (kAllOut_InitialState == *initialState) {
301 // xor could be changed to diff in the kAllIn case, not sure it's a win.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000302 element->setOp(SkRegion::kReplace_Op);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000303 }
304 break;
305 case SkRegion::kReverseDifference_Op:
306 if (kAllIn_InitialState == *initialState) {
307 // subtracting the whole plane will yield the empty set.
308 skippable = true;
309 *initialState = kAllOut_InitialState;
310 } else {
311 // this picks up flips inserted in the backwards pass.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000312 if (*resultsAreBounded) {
313 skippable = element->isInverseFilled() ?
314 !SkRect::Intersects(element->getBounds(), *resultBounds) :
315 element->contains(*resultBounds);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000316 }
317 if (skippable) {
318 *initialState = kAllIn_InitialState;
319 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000320 element->setOp(SkRegion::kReplace_Op);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000321 }
322 }
323 break;
324 case SkRegion::kReplace_Op:
325 SkASSERT(!clipsToSkip); // replace should always be the first op
326 skippable = false; // we would have skipped it in the backwards walk if we
327 // could've.
328 break;
329 default:
330 SkDEBUGFAIL("Unexpected op.");
331 break;
332 }
333 if (!skippable) {
334 break;
335 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000336 result->popHead();
337 element = result->headIter().get();
bsalomon@google.com51a62862012-11-26 21:19:43 +0000338 }
339 }
bsalomon@google.com51a62862012-11-26 21:19:43 +0000340 }
341}
342} // namespace GrReducedClip
343
344////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000345namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000346// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +0000347// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000348void setup_drawstate_aaclip(GrGpu* gpu,
349 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000350 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000351 GrDrawState* drawState = gpu->drawState();
352 GrAssert(drawState);
353
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000354 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000355
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000356 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000357 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000358 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000359 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000360 mat.preConcat(drawState->getViewMatrix());
361
bsalomon@google.com08283af2012-10-26 13:01:20 +0000362 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000363
364 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
365 drawState->stage(kMaskStage)->setEffect(
366 GrTextureDomainEffect::Create(result,
367 mat,
368 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
369 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000370}
371
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000372bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000373 GrGpu* gpu,
374 const SkPath& path,
375 GrPathFill fill,
376 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000377 // last (false) parameter disallows use of the SW path renderer
378 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
379}
380
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000381GrPathFill get_path_fill(const SkPath& path) {
382 switch (path.getFillType()) {
383 case SkPath::kWinding_FillType:
384 return kWinding_GrPathFill;
385 case SkPath::kEvenOdd_FillType:
386 return kEvenOdd_GrPathFill;
387 case SkPath::kInverseWinding_FillType:
388 return kInverseWinding_GrPathFill;
389 case SkPath::kInverseEvenOdd_FillType:
390 return kInverseEvenOdd_GrPathFill;
391 default:
392 GrCrash("Unsupported path fill in clip.");
393 return kWinding_GrPathFill; // suppress warning
394 }
395}
396
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000397/**
398 * Does any individual clip in 'clipIn' use anti-aliasing?
399 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000400bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000401
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000402 SkClipStack::Iter iter;
403 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000404
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000405 const Element* element = NULL;
406 for (element = iter.skipToTopmost(SkRegion::kReplace_Op);
407 NULL != element;
408 element = iter.next()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000409
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000410 if (element->isAA()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000411 return true;
412 }
413 }
414
415 return false;
416}
417
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000418}
419
robertphillips@google.comfa662942012-05-17 12:20:22 +0000420/*
421 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
422 * will be used on any element. If so, it returns true to indicate that the
423 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
424 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000425bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000426
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000427 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000428 // a clip gets complex enough it can just be done in SW regardless
429 // of whether it would invoke the GrSoftwarePathRenderer.
430 bool useSW = false;
431
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000432 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000433 const Element* element = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000434
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000435 for (element = iter.skipToTopmost(SkRegion::kReplace_Op);
436 NULL != element;
437 element = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000438
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000439 // rects can always be drawn directly w/o using the software path
440 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000441 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000442 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000443 element->getPath(),
444 get_path_fill(element->getPath()),
445 element->isAA())) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000446 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000447 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000448 }
449
450 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000451}
452
robertphillips@google.comf294b772012-04-27 14:29:26 +0000453////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000454// sort out what kind of clip mask needs to be created: alpha, stencil,
455// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000456bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000457 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000458
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000459 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000460 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000461 fGpu->disableScissor();
462 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000463 return true;
464 }
465
466 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000467 // GrDrawTarget should have filtered this for us
468 GrAssert(NULL != rt);
469
robertphillips@google.com7b112892012-07-31 15:18:21 +0000470 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000471 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000472
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000473 clipDataIn->getConservativeBounds(rt, &devClipBounds, &isIntersectionOfRects);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000474 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000475 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000476 }
477
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000478#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000479 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000480
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000481 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000482 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000483 // in software (this will only happen if the clip mask is anti-aliased
484 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000485 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000486 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000487 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000488 // The clip geometry is complex enough that it will be more
489 // efficient to create it entirely in software
490 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000491 GrIRect devBound;
492 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
493 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000494 fGpu->disableScissor();
495 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000496 return true;
497 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000498
499 // if SW clip mask creation fails fall through to the other
500 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000501 }
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000502#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000503
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000504#if GR_AA_CLIP
robertphillips@google.comf294b772012-04-27 14:29:26 +0000505 // If MSAA is enabled use the (faster) stencil path for AA clipping
506 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000507 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000508 // Since we are going to create a destination texture of the correct
509 // size for the mask (rather than being bound by the size of the
510 // render target) we aren't going to use scissoring like the stencil
511 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000512 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000513 GrIRect devBound;
514 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
515 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000516 fGpu->disableScissor();
517 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000518 return true;
519 }
520
521 // if alpha clip mask creation fails fall through to the stencil
522 // buffer method
523 }
524#endif // GR_AA_CLIP
525
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000526 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000527 // an antialiased clip couldn't be created. In either case, free up
528 // the texture in the antialiased mask cache.
529 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000530 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000531 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000532 // AA cache.
533 fAACache.reset();
534
bsalomon@google.coma3201942012-06-21 19:58:20 +0000535 // If the clip is a rectangle then just set the scissor. Otherwise, create
536 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000537 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000538 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000539 this->setGpuStencil();
540 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000541 }
542
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000543 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000544 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000545 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000546
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000547 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000548 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000549 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000550 // This must occur after createStencilClipMask. That function may change
551 // the scissor. Also, it only guarantees that the stencil mask is correct
552 // within the bounds it was passed, so we must use both stencil and scissor
553 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000554 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000555 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000556 return true;
557}
558
559#define VISUALIZE_COMPLEX_CLIP 0
560
561#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000562 #include "SkRandom.h"
563 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000564 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
565#else
566 #define SET_RANDOM_COLOR
567#endif
568
569namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000570/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000571 * Does "canvContainer" contain "devContainee"? If either is empty then
572 * no containment is possible. "canvContainer" is in canvas coordinates while
573 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000574 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000575 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000576bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000577 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000578 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000579 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000580 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000581 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000582 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000583 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000584}
585
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000587// determines how many elements at the head of the clip can be skipped and
588// whether the initial clear should be to the inside- or outside-the-clip value,
589// and what op should be used to draw the first element that isn't skipped.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000590const SkClipStack::Element* process_initial_clip_elements(
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000591 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000592 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000593 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000594 SkRegion::Op* firstOp,
595 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000596
597 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000598
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000599 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000600 // processed the clip is entirely open. However, depending on the
601 // first set op we may prefer to clear to 0 for performance. We may
602 // also be able to skip the initial clip paths/rects. We loop until
603 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604 bool done = false;
605 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000606
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000607 const SkClipStack::Element* element = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000608
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000609 for (element = iter->skipToTopmost(SkRegion::kReplace_Op);
610 NULL != element && !done;
611 element = iter->next()) {
612 switch (element->getOp()) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000613 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000614 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000615 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616 *clearToInside = false;
617 done = true;
618 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000619 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000620 // if this element contains the entire bounds then we
621 // can skip it.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000622 if (Element::kRect_Type == element->getType() &&
623 contains(element->getRect(), devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000624 break;
625 }
626 // if everything is initially clearToInside then intersect is
627 // same as clear to 0 and treat as a replace. Otherwise,
628 // set stays empty.
629 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000630 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631 *clearToInside = false;
632 done = true;
633 }
634 break;
635 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000636 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000637 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000638 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639 // clearToInside
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 done = true;
643 }
644 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000645 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000646 // xor is same as difference or replace both of which
647 // can be 1-pass instead of 2 for xor.
648 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000649 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000650 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000651 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000652 }
653 done = true;
654 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000655 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000656 // if all pixels are clearToInside then we have to process the
657 // difference, otherwise it has no effect and all pixels
658 // remain outside.
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 done = true;
662 }
663 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000664 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665 // if all pixels are clearToInside then reverse difference
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000666 // produces empty set. Otherwise it is same as replace
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667 if (*clearToInside) {
668 *clearToInside = false;
669 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000670 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000671 done = true;
672 }
673 break;
674 default:
675 GrCrash("Unknown set op.");
676 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000677
678 if (done) {
679 // we need to break out here (rather than letting the test in
680 // the loop do it) since backing up the iterator is very expensive
681 break;
682 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000683 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000684 return element;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000685}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000686
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687}
688
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689namespace {
690
691////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000692// set up the OpenGL blend function to perform the specified
693// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000694void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695
696 switch (op) {
697 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000698 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000699 break;
700 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000701 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000702 break;
703 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000704 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000705 break;
706 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000707 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000708 break;
709 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000710 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000711 break;
712 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000713 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000714 break;
715 default:
716 GrAssert(false);
717 break;
718 }
719}
720
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000721////////////////////////////////////////////////////////////////////////////////
722bool draw_path_in_software(GrContext* context,
723 GrGpu* gpu,
724 const SkPath& path,
725 GrPathFill fill,
726 bool doAA,
727 const GrIRect& resultBounds) {
728
729 SkAutoTUnref<GrTexture> texture(
730 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
731 resultBounds, fill,
732 doAA, NULL));
733 if (NULL == texture) {
734 return false;
735 }
736
737 // The ClipMaskManager accumulates the clip mask in the UL corner
738 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
739
740 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
741
742 GrAssert(!GrIsFillInverted(fill));
743 return true;
744}
745
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000746
747////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000748bool draw_path(GrContext* context,
749 GrGpu* gpu,
750 const SkPath& path,
751 GrPathFill fill,
752 bool doAA,
753 const GrIRect& resultBounds) {
754
755 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
756 if (NULL == pr) {
757 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
758 }
759
760 pr->drawPath(path, fill, gpu, doAA);
761 return true;
762}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000763
robertphillips@google.com7b112892012-07-31 15:18:21 +0000764// 'rect' enters in device coordinates and leaves in canvas coordinates
765void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
766 GrAssert(NULL != rect);
767
768 rect->fLeft += SkIntToScalar(origin.fX);
769 rect->fTop += SkIntToScalar(origin.fY);
770 rect->fRight += SkIntToScalar(origin.fX);
771 rect->fBottom += SkIntToScalar(origin.fY);
772}
773
robertphillips@google.com72176b22012-05-23 13:19:12 +0000774}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000775
776////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000777bool GrClipMaskManager::drawClipShape(GrTexture* target,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000778 const SkClipStack::Element* element,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000779 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000780 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000781 GrAssert(NULL != drawState);
782
783 drawState->setRenderTarget(target->asRenderTarget());
784
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000785 switch (element->getType()) {
786 case Element::kRect_Type:
787 if (element->isAA()) {
788 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu, element->getRect(), true);
789 } else {
790 fGpu->drawSimpleRect(element->getRect(), NULL);
791 }
792 return true;
793 case Element::kPath_Type:
794 return draw_path(this->getContext(), fGpu,
795 element->getPath(),
796 get_path_fill(element->getPath()),
797 element->isAA(),
798 resultBounds);
799 default:
800 // something is wrong if we're trying to draw an empty element.
801 GrCrash("Unexpected element type");
802 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000803 }
804 return true;
805}
806
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000807void GrClipMaskManager::mergeMask(GrTexture* dstMask,
808 GrTexture* srcMask,
809 SkRegion::Op op,
810 const GrIRect& dstBound,
811 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000812 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000813 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000814 SkMatrix oldMatrix = drawState->getViewMatrix();
815 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000816
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000817 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000818
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000819 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000820
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000821 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000822 sampleM.setIDiv(srcMask->width(), srcMask->height());
823 drawState->stage(0)->setEffect(
824 GrTextureDomainEffect::Create(srcMask,
825 sampleM,
826 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
827 GrTextureDomainEffect::kDecal_WrapMode))->unref();
828 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000829
tomhudson@google.com676e6602012-07-10 17:21:48 +0000830 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000831 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000832}
833
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000834// get a texture to act as a temporary buffer for AA clip boolean operations
835// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000836void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000837 GrAutoScratchTexture* temp) {
838 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000839 // we've already allocated the temp texture
840 return;
841 }
842
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000843 GrTextureDesc desc;
844 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
845 desc.fWidth = bounds.width();
846 desc.fHeight = bounds.height();
847 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000848
robertphillips@google.com2c756812012-05-22 20:28:23 +0000849 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000850}
851
robertphillips@google.comf105b102012-05-14 12:18:26 +0000852
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000853void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000854 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000855 // Since we are setting up the cache we know the last lookup was a miss
856 // Free up the currently cached mask so it can be reused
857 fAACache.reset();
858
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000859 GrTextureDesc desc;
860 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
861 desc.fWidth = bounds.width();
862 desc.fHeight = bounds.height();
863 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000864
865 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000866}
867
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000868////////////////////////////////////////////////////////////////////////////////
869// Shared preamble between gpu and SW-only AA clip mask creation paths.
870// Handles caching, determination of clip mask bound & allocation (if needed)
871// of the result texture
872// 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 +0000873bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000874 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000875 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000876 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000877 GrAssert(origDrawState->isClipState());
878
879 GrRenderTarget* rt = origDrawState->getRenderTarget();
880 GrAssert(NULL != rt);
881
robertphillips@google.comf294b772012-04-27 14:29:26 +0000882 // unlike the stencil path the alpha path is not bound to the size of the
883 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000884 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000885 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000886
887 // need to outset a pixel since the standard bounding box computation
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000888 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000889 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000890
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000891 // TODO: make sure we don't outset if bounds are still 0,0 @ min
892
robertphillips@google.comba998f22012-10-12 11:33:56 +0000893 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000894 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000895 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000896 return true;
897 }
898
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000899 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000900 return false;
901}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000902
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000903////////////////////////////////////////////////////////////////////////////////
904// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000905bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000906 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000907 GrIRect *devResultBounds) {
908 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000909 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
910
robertphillips@google.com7b112892012-07-31 15:18:21 +0000911 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000912 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000913 return true;
914 }
915
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000916 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
917
robertphillips@google.comf105b102012-05-14 12:18:26 +0000918 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000919 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000920 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000921 return false;
922 }
923
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000924 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
925 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000926
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000927 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000928
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000929 // The mask we generate is translated so that its upper-left corner is at devResultBounds
930 // upper-left corner in device space.
931 GrIRect maskResultBounds = GrIRect::MakeWH(devResultBounds->width(), devResultBounds->height());
932
933 // Set the matrix so that rendered clip elements are transformed from the space of the clip
934 // stack to the alpha-mask. This accounts for both translation due to the clip-origin and the
935 // placement of the mask within the device.
936 SkVector clipToMaskOffset = {
937 SkIntToScalar(-devResultBounds->fLeft - clipDataIn.fOrigin.fX),
938 SkIntToScalar(-devResultBounds->fTop - clipDataIn.fOrigin.fY)
939 };
940 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000941
942 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000943 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
944
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000945 SkClipStack::Iter iter(*clipDataIn.fClipStack,
946 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000947 const Element* element = process_initial_clip_elements(&iter,
948 *devResultBounds,
949 &clearToInside,
950 &firstOp,
951 clipDataIn);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000952 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
953 // clear the part that we care about.
954 fGpu->clear(&maskResultBounds,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000955 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000956 accum->asRenderTarget());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000957 bool accumClearedToZero = !clearToInside;
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000958
robertphillips@google.comf105b102012-05-14 12:18:26 +0000959 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000960 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000961 // walk through each clip element and perform its set op
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000962 for ( ; NULL != element; element = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000963
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000964 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000965 if (first) {
966 first = false;
967 op = firstOp;
968 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000969
bsalomon@google.com6794a252012-11-08 15:30:53 +0000970 if (SkRegion::kReplace_Op == op) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000971 // clear the accumulator and draw the new object directly into it
972 if (!accumClearedToZero) {
973 fGpu->clear(&maskResultBounds, 0x00000000, accum->asRenderTarget());
974 }
975
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000976 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000977 this->drawClipShape(accum, element, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000978
979 } else if (SkRegion::kReverseDifference_Op == op ||
980 SkRegion::kIntersect_Op == op) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000981 // there is no point in intersecting a screen filling rectangle.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000982 if (SkRegion::kIntersect_Op == op && Element::kRect_Type == element->getType() &&
983 contains(element->getRect(), *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000984 continue;
985 }
986
robertphillips@google.com7b112892012-07-31 15:18:21 +0000987 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000988 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000989 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000990 return false;
991 }
992
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000993 // 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 +0000994 // mask buffer can be substantially larger than the actually clip stack element. We
995 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000996 // the accumulator
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000997 GrRect elementMaskBounds = element->getBounds();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000998 elementMaskBounds.offset(clipToMaskOffset);
999 GrIRect elementMaskIBounds;
1000 elementMaskBounds.roundOut(&elementMaskIBounds);
1001
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001002 // clear the temp target & draw into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001003 fGpu->clear(&elementMaskIBounds, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +00001004
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001005 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001006 this->drawClipShape(temp.texture(), element, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +00001007
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001008 // Now draw into the accumulator using the real operation
1009 // and the temp buffer as a texture
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001010 this->mergeMask(accum, temp.texture(), op, maskResultBounds, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +00001011 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001012 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +00001013 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001014 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001015 this->drawClipShape(accum, element, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +00001016 }
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +00001017 accumClearedToZero = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +00001018 }
1019
robertphillips@google.coma72eef32012-05-01 17:22:59 +00001020 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001021 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +00001022 return true;
1023}
1024
1025////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001026// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001027// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001028bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001029 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001030
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001031 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001032
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001033 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001034 GrAssert(drawState->isClipState());
1035
1036 GrRenderTarget* rt = drawState->getRenderTarget();
1037 GrAssert(NULL != rt);
1038
1039 // TODO: dynamically attach a SB when needed.
1040 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
1041 if (NULL == stencilBuffer) {
1042 return false;
1043 }
1044
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001045 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001046
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001047 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001048
1049 // we set the current clip to the bounds so that our recursive
1050 // draws are scissored to them. We use the copy of the complex clip
1051 // we just stashed on the SB to render from. We set it back after
1052 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001053 const GrClipData* oldClipData = fGpu->getClip();
1054
robertphillips@google.com7b112892012-07-31 15:18:21 +00001055 // The origin of 'newClipData' is (0, 0) so it is okay to place
1056 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001057 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001058 GrClipData newClipData;
1059 newClipData.fClipStack = &newClipStack;
1060
1061 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001062
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001063 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1064 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001065 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001066 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001067
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001068 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001069 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001070 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +00001071 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001072 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001073 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001074 }
1075
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001076#if !VISUALIZE_COMPLEX_CLIP
1077 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
1078#endif
1079
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001080 int clipBit = stencilBuffer->bits();
1081 SkASSERT((clipBit <= 16) &&
1082 "Ganesh only handles 16b or smaller stencil buffers");
1083 clipBit = (1 << (clipBit-1));
1084
robertphillips@google.com7b112892012-07-31 15:18:21 +00001085 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001086
1087 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001088 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1089
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001090 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001091 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001092 const Element* element = process_initial_clip_elements(&iter,
1093 devRTRect,
1094 &clearToInside,
1095 &firstOp,
1096 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001097
robertphillips@google.com7b112892012-07-31 15:18:21 +00001098 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001099 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001100
1101 // walk through each clip element and perform its set op
1102 // with the existing clip.
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001103 for ( ; NULL != element; element = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001104 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +00001105 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001106 // enabled at bottom of loop
1107 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001108 // if the target is MSAA then we want MSAA enabled when the clip is soft
1109 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001110 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001111 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001112
tomhudson@google.com8afae612012-08-14 15:03:35 +00001113 // Can the clip element be drawn directly to the stencil buffer
1114 // with a non-inverted fill rule without extra passes to
1115 // resolve in/out status?
1116 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001117
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001118 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001119 if (first) {
1120 first = false;
1121 op = firstOp;
1122 }
robertphillips@google.comf294b772012-04-27 14:29:26 +00001123
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001124 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001125 const SkPath* clipPath = NULL;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001126 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001127 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +00001128 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001129 fillInverted = false;
1130 // there is no point in intersecting a screen filling
1131 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +00001132 if (SkRegion::kIntersect_Op == op &&
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001133 contains(element->getRect(), devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001134 continue;
1135 }
tomhudson@google.com8afae612012-08-14 15:03:35 +00001136 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001137 GrAssert(Element::kPath_Type == element->getType());
1138 clipPath = &element->getPath();
1139 fill = get_path_fill(*clipPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001140 fillInverted = GrIsFillInverted(fill);
1141 fill = GrNonInvertedFill(fill);
tomhudson@google.com8afae612012-08-14 15:03:35 +00001142 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001143 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +00001144 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001145 return false;
1146 }
1147 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001148 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001149 }
1150
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001151 int passes;
1152 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
1153
1154 bool canDrawDirectToClip; // Given the renderer, the element,
1155 // fill rule, and set operation can
1156 // we render the element directly to
1157 // stencil bit used for clipping.
1158 canDrawDirectToClip =
1159 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +00001160 canRenderDirectToStencil,
1161 clipBit,
1162 fillInverted,
1163 &passes,
1164 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001165
1166 // draw the element to the client stencil bits if necessary
1167 if (!canDrawDirectToClip) {
1168 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
1169 kIncClamp_StencilOp,
1170 kIncClamp_StencilOp,
1171 kAlways_StencilFunc,
1172 0xffff,
1173 0x0000,
1174 0xffff);
1175 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001176 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001177 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001178 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001179 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001180 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001181 if (canRenderDirectToStencil) {
1182 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001183 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001184 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001185 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001186 }
1187 }
1188 }
1189
1190 // now we modify the clip bit by rendering either the clip
1191 // element directly or a bounding rect of the entire clip.
1192 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
1193 for (int p = 0; p < passes; ++p) {
1194 *drawState->stencil() = stencilSettings[p];
1195 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001196 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001197 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001198 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001199 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001200 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001201 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001202 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001203 }
1204 } else {
1205 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001206 // 'devClipBounds' is already in device coordinates so the
1207 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +00001208 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001209 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +00001210 GrRect canvClipBounds;
1211 canvClipBounds.set(devClipBounds);
1212 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
1213 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001214 }
1215 }
1216 }
1217 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001218 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001219 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001220 // set this last because recursive draws may overwrite it back to kNone.
1221 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
1222 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001223 return true;
1224}
1225
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001226
bsalomon@google.com411dad02012-06-05 20:24:20 +00001227// mapping of clip-respecting stencil funcs to normal stencil funcs
1228// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001229static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +00001230 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
1231 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
1232 // In the Clip Funcs
1233 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
1234 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
1235 kLess_StencilFunc, // kLessIfInClip_StencilFunc
1236 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
1237 // Special in the clip func that forces user's ref to be 0.
1238 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
1239 // make ref 0 and do normal nequal.
1240 },
1241 {// Stencil-Clipping is ENABLED
1242 // In the Clip Funcs
1243 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
1244 // eq stencil clip bit, mask
1245 // out user bits.
1246
1247 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
1248 // add stencil bit to mask and ref
1249
1250 kLess_StencilFunc, // kLessIfInClip_StencilFunc
1251 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
1252 // for both of these we can add
1253 // the clip bit to the mask and
1254 // ref and compare as normal
1255 // Special in the clip func that forces user's ref to be 0.
1256 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
1257 // make ref have only the clip bit set
1258 // and make comparison be less
1259 // 10..0 < 1..user_bits..
1260 }
1261};
1262
bsalomon@google.coma3201942012-06-21 19:58:20 +00001263namespace {
1264// Sets the settings to clip against the stencil buffer clip while ignoring the
1265// client bits.
1266const GrStencilSettings& basic_apply_stencil_clip_settings() {
1267 // stencil settings to use when clip is in stencil
1268 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1269 kKeep_StencilOp,
1270 kKeep_StencilOp,
1271 kAlwaysIfInClip_StencilFunc,
1272 0x0000,
1273 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001274 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001275 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1276}
1277}
1278
1279void GrClipMaskManager::setGpuStencil() {
1280 // We make two copies of the StencilSettings here (except in the early
1281 // exit scenario. One copy from draw state to the stack var. Then another
1282 // from the stack var to the gpu. We could make this class hold a ptr to
1283 // GrGpu's fStencilSettings and eliminate the stack copy here.
1284
1285 const GrDrawState& drawState = fGpu->getDrawState();
1286
1287 // use stencil for clipping if clipping is enabled and the clip
1288 // has been written into the stencil.
1289 GrClipMaskManager::StencilClipMode clipMode;
1290 if (this->isClipInStencil() && drawState.isClipState()) {
1291 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1292 // We can't be modifying the clip and respecting it at the same time.
1293 GrAssert(!drawState.isStateFlagEnabled(
1294 GrGpu::kModifyStencilClip_StateBit));
1295 } else if (drawState.isStateFlagEnabled(
1296 GrGpu::kModifyStencilClip_StateBit)) {
1297 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1298 } else {
1299 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1300 }
1301
1302 GrStencilSettings settings;
1303 // The GrGpu client may not be using the stencil buffer but we may need to
1304 // enable it in order to respect a stencil clip.
1305 if (drawState.getStencil().isDisabled()) {
1306 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1307 settings = basic_apply_stencil_clip_settings();
1308 } else {
1309 fGpu->disableStencil();
1310 return;
1311 }
1312 } else {
1313 settings = drawState.getStencil();
1314 }
1315
1316 // TODO: dynamically attach a stencil buffer
1317 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001318 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001319 drawState.getRenderTarget()->getStencilBuffer();
1320 if (NULL != stencilBuffer) {
1321 stencilBits = stencilBuffer->bits();
1322 }
1323
bsalomon@google.comf6601872012-08-28 21:11:35 +00001324 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001325 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001326 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001327 this->adjustStencilParams(&settings, clipMode, stencilBits);
1328 fGpu->setStencilSettings(settings);
1329}
1330
1331void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1332 StencilClipMode mode,
1333 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001334 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001335
1336 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001337 // We assume that this clip manager itself is drawing to the GrGpu and
1338 // has already setup the correct values.
1339 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001340 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001341
bsalomon@google.com411dad02012-06-05 20:24:20 +00001342 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1343 unsigned int userBits = clipBit - 1;
1344
bsalomon@google.coma3201942012-06-21 19:58:20 +00001345 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001346 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001347
bsalomon@google.coma3201942012-06-21 19:58:20 +00001348 bool finished = false;
1349 while (!finished) {
1350 GrStencilFunc func = settings->func(face);
1351 uint16_t writeMask = settings->writeMask(face);
1352 uint16_t funcMask = settings->funcMask(face);
1353 uint16_t funcRef = settings->funcRef(face);
1354
1355 GrAssert((unsigned) func < kStencilFuncCount);
1356
1357 writeMask &= userBits;
1358
1359 if (func >= kBasicStencilFuncCount) {
1360 int respectClip = kRespectClip_StencilClipMode == mode;
1361 if (respectClip) {
1362 // The GrGpu class should have checked this
1363 GrAssert(this->isClipInStencil());
1364 switch (func) {
1365 case kAlwaysIfInClip_StencilFunc:
1366 funcMask = clipBit;
1367 funcRef = clipBit;
1368 break;
1369 case kEqualIfInClip_StencilFunc:
1370 case kLessIfInClip_StencilFunc:
1371 case kLEqualIfInClip_StencilFunc:
1372 funcMask = (funcMask & userBits) | clipBit;
1373 funcRef = (funcRef & userBits) | clipBit;
1374 break;
1375 case kNonZeroIfInClip_StencilFunc:
1376 funcMask = (funcMask & userBits) | clipBit;
1377 funcRef = clipBit;
1378 break;
1379 default:
1380 GrCrash("Unknown stencil func");
1381 }
1382 } else {
1383 funcMask &= userBits;
1384 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001385 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001386 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001387 gSpecialToBasicStencilFunc[respectClip];
1388 func = table[func - kBasicStencilFuncCount];
1389 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001390 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001391 funcMask &= userBits;
1392 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001393 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001394
1395 settings->setFunc(face, func);
1396 settings->setWriteMask(face, writeMask);
1397 settings->setFuncMask(face, funcMask);
1398 settings->setFuncRef(face, funcRef);
1399
1400 if (GrStencilSettings::kFront_Face == face) {
1401 face = GrStencilSettings::kBack_Face;
1402 finished = !twoSided;
1403 } else {
1404 finished = true;
1405 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001406 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001407 if (!twoSided) {
1408 settings->copyFrontSettingsToBack();
1409 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001410}
1411
1412////////////////////////////////////////////////////////////////////////////////
1413
robertphillips@google.comfa662942012-05-17 12:20:22 +00001414namespace {
1415
1416GrPathFill invert_fill(GrPathFill fill) {
1417 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001418 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1419 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1420 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1421 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1422 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001423 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001424 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1425 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1426 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1427 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1428 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1429 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001430 return gInvertedFillTable[fill];
1431}
1432
1433}
1434
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001435bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001436 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001437 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001438 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001439
robertphillips@google.com7b112892012-07-31 15:18:21 +00001440 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001441 return true;
1442 }
1443
robertphillips@google.comf105b102012-05-14 12:18:26 +00001444 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001445 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001446 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001447 return false;
1448 }
1449
robertphillips@google.com2c756812012-05-22 20:28:23 +00001450 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001451
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001452 SkMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001453 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001454 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001455 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001456
robertphillips@google.comfa662942012-05-17 12:20:22 +00001457 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001458 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1459
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001460 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001461 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001462 const Element* element = process_initial_clip_elements(&iter,
1463 *devResultBounds,
1464 &clearToInside,
1465 &firstOp,
1466 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001467
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001468 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001469
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001470 bool first = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001471 for ( ; NULL != element; element = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001472
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001473 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001474 if (first) {
1475 first = false;
1476 op = firstOp;
1477 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001478
1479 if (SkRegion::kIntersect_Op == op ||
1480 SkRegion::kReverseDifference_Op == op) {
1481 // Intersect and reverse difference require modifying pixels
1482 // outside of the geometry that is being "drawn". In both cases
1483 // we erase all the pixels outside of the geometry but
1484 // leave the pixels inside the geometry alone. For reverse
1485 // difference we invert all the pixels before clearing the ones
1486 // outside the geometry.
1487 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001488 SkRect temp;
1489 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001490 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1491 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001492
1493 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001494 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001495 }
1496
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001497 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001498 // convert the rect to a path so we can invert the fill
1499 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001500 temp.addRect(element->getRect());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001501
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001502 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001503 kInverseEvenOdd_GrPathFill, element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001504 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001505 } else {
1506 GrAssert(Element::kPath_Type == element->getType());
1507 helper.draw(element->getPath(),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001508 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001509 invert_fill(get_path_fill(element->getPath())),
1510 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001511 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001512 }
1513
1514 continue;
1515 }
1516
1517 // The other ops (union, xor, diff) only affect pixels inside
1518 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001519 if (Element::kRect_Type == element->getType()) {
1520 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1521 } else {
1522 GrAssert(Element::kPath_Type == element->getType());
1523 helper.draw(element->getPath(),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001524 op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001525 get_path_fill(element->getPath()),
1526 element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001527 }
1528 }
1529
robertphillips@google.comfa662942012-05-17 12:20:22 +00001530 // Because we are using the scratch texture cache, "accum" may be
1531 // larger than expected and have some cruft in the areas we aren't using.
1532 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001533 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001534
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001535 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001536
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001537 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001538
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001539 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001540 return true;
1541}
1542
robertphillips@google.comf294b772012-04-27 14:29:26 +00001543////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001544void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001545 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001546}