robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | |
| 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.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 10 | #include "effects/GrTextureDomainEffect.h" |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 11 | #include "GrGpu.h" |
| 12 | #include "GrRenderTarget.h" |
| 13 | #include "GrStencilBuffer.h" |
| 14 | #include "GrPathRenderer.h" |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 15 | #include "GrPaint.h" |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 16 | #include "SkRasterClip.h" |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 17 | #include "GrAAConvexPathRenderer.h" |
| 18 | #include "GrAAHairLinePathRenderer.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 19 | #include "GrSWMaskHelper.h" |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 20 | #include "GrCacheID.h" |
| 21 | |
| 22 | GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain) |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 23 | |
robertphillips@google.com | ba998f2 | 2012-10-12 11:33:56 +0000 | [diff] [blame] | 24 | #define GR_AA_CLIP 1 |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 25 | #define GR_SW_CLIP 1 |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 26 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 27 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 28 | |
| 29 | namespace GrReducedClip { |
| 30 | |
| 31 | /* |
| 32 | There are plenty of optimizations that could be added here. For example we could consider |
| 33 | checking 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 |
| 35 | earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps |
| 36 | for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations |
| 37 | based on later intersect operations, and perhaps remove intersect-rects. We could optionally |
| 38 | take a rect in case the caller knows a bound on what is to be drawn through this clip. |
| 39 | */ |
| 40 | void 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.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame] | 76 | // c) a replace operation |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 77 | |
| 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.com | edb26fd | 2012-11-28 14:42:41 +0000 | [diff] [blame] | 93 | if (SkClipStack::kEmptyGenID == clip->fGenID) { |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 94 | *initialState = kAllOut_InitialState; |
| 95 | break; |
| 96 | } |
bsalomon@google.com | edb26fd | 2012-11-28 14:42:41 +0000 | [diff] [blame] | 97 | if (SkClipStack::kWideOpenGenID == clip->fGenID) { |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 98 | *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.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 133 | // be skipped or it is outside the entire bounds and therefore makes the clip |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 134 | // 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.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 157 | // 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.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 159 | // 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.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 182 | // 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.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 186 | 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.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 205 | // 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.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 209 | 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.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 231 | // 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.com | ab38f7a | 2012-11-28 02:02:11 +0000 | [diff] [blame] | 235 | if (*resultsAreBounded) { |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 236 | 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.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame] | 267 | SkASSERT(SkRegion::kXOR_Op == clip->fOp || |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 268 | 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.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 291 | // subtracting from the empty set yields the empty set. |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 292 | skippable = kAllOut_InitialState == *initialState; |
| 293 | break; |
| 294 | case SkRegion::kIntersect_Op: |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 295 | // intersecting with the empty set yields the empty set |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 296 | 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.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 356 | namespace { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 357 | // set up the draw state to enable the aa clipping mask. Besides setting up the |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 358 | // stage matrix this also alters the vertex layout |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 359 | void setup_drawstate_aaclip(GrGpu* gpu, |
| 360 | GrTexture* result, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 361 | const GrIRect &devBound) { |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 362 | GrDrawState* drawState = gpu->drawState(); |
| 363 | GrAssert(drawState); |
| 364 | |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 365 | static const int kMaskStage = GrPaint::kTotalStages+1; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 366 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 367 | SkMatrix mat; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 368 | mat.setIDiv(result->width(), result->height()); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 369 | mat.preTranslate(SkIntToScalar(-devBound.fLeft), |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 370 | SkIntToScalar(-devBound.fTop)); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 371 | mat.preConcat(drawState->getViewMatrix()); |
| 372 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 373 | drawState->stage(kMaskStage)->reset(); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 374 | |
| 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.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 381 | } |
| 382 | |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 383 | bool path_needs_SW_renderer(GrContext* context, |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 384 | GrGpu* gpu, |
| 385 | const SkPath& path, |
| 386 | GrPathFill fill, |
| 387 | bool doAA) { |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 388 | // last (false) parameter disallows use of the SW path renderer |
| 389 | return NULL == context->getPathRenderer(path, fill, gpu, doAA, false); |
| 390 | } |
| 391 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 392 | GrPathFill 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.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame] | 408 | /** |
| 409 | * Does any individual clip in 'clipIn' use anti-aliasing? |
| 410 | */ |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 411 | bool requires_AA(const SkClipStack& clipIn) { |
robertphillips@google.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame] | 412 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 413 | SkClipStack::Iter iter; |
| 414 | iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart); |
robertphillips@google.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame] | 415 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 416 | const SkClipStack::Iter::Clip* clip = NULL; |
robertphillips@google.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame] | 417 | for (clip = iter.skipToTopmost(SkRegion::kReplace_Op); |
| 418 | NULL != clip; |
bsalomon@google.com | 5fac58c | 2012-11-29 21:14:59 +0000 | [diff] [blame] | 419 | clip = iter.next()) { |
robertphillips@google.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame] | 420 | |
| 421 | if (clip->fDoAA) { |
| 422 | return true; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return false; |
| 427 | } |
| 428 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 429 | } |
| 430 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 431 | /* |
| 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.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 436 | bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) { |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 437 | |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 438 | // TODO: generalize this function so that when |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 439 | // 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.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 443 | SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart); |
| 444 | const SkClipStack::Iter::Clip* clip = NULL; |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 445 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 446 | for (clip = iter.skipToTopmost(SkRegion::kReplace_Op); |
| 447 | NULL != clip; |
bsalomon@google.com | 5fac58c | 2012-11-29 21:14:59 +0000 | [diff] [blame] | 448 | clip = iter.next()) { |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 449 | |
robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 450 | // rects can always be drawn directly w/o using the software path |
| 451 | // so only paths need to be checked |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 452 | if (NULL != clip->fPath && |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 453 | path_needs_SW_renderer(this->getContext(), fGpu, |
| 454 | *clip->fPath, |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 455 | get_path_fill(*clip->fPath), |
| 456 | clip->fDoAA)) { |
robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 457 | useSW = true; |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 458 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return useSW; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 462 | } |
| 463 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 464 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 465 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 466 | // scissor, or entirely software |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 467 | bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 468 | fCurrClipMaskType = kNone_ClipMaskType; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 469 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 470 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 471 | if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 472 | fGpu->disableScissor(); |
| 473 | this->setGpuStencil(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 474 | return true; |
| 475 | } |
| 476 | |
| 477 | GrRenderTarget* rt = drawState->getRenderTarget(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 478 | // GrDrawTarget should have filtered this for us |
| 479 | GrAssert(NULL != rt); |
| 480 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 481 | GrIRect devClipBounds; |
robertphillips@google.com | e4d69c0 | 2012-07-26 21:37:40 +0000 | [diff] [blame] | 482 | bool isIntersectionOfRects = false; |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 483 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 484 | clipDataIn->getConservativeBounds(rt, &devClipBounds, &isIntersectionOfRects); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 485 | if (devClipBounds.isEmpty()) { |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 486 | return false; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 487 | } |
| 488 | |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 489 | #if GR_SW_CLIP |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 490 | bool requiresAA = requires_AA(*clipDataIn->fClipStack); |
robertphillips@google.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame] | 491 | |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 492 | // If MSAA is enabled we can do everything in the stencil buffer. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 493 | // Otherwise check if we should just create the entire clip mask |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 494 | // 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.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 496 | if (0 == rt->numSamples() && |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 497 | requiresAA && |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 498 | this->useSWOnlyPath(*clipDataIn->fClipStack)) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 499 | // 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.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 502 | GrIRect devBound; |
| 503 | if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) { |
| 504 | setup_drawstate_aaclip(fGpu, result, devBound); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 505 | fGpu->disableScissor(); |
| 506 | this->setGpuStencil(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 507 | return true; |
| 508 | } |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 509 | |
| 510 | // if SW clip mask creation fails fall through to the other |
| 511 | // two possible methods (bottoming out at stencil clipping) |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 512 | } |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 513 | #endif // GR_SW_CLIP |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 514 | |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 515 | #if GR_AA_CLIP |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 516 | // If MSAA is enabled use the (faster) stencil path for AA clipping |
| 517 | // otherwise the alpha clip mask is our only option |
robertphillips@google.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame] | 518 | if (0 == rt->numSamples() && requiresAA) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 519 | // 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.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 523 | GrTexture* result = NULL; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 524 | GrIRect devBound; |
| 525 | if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) { |
| 526 | setup_drawstate_aaclip(fGpu, result, devBound); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 527 | fGpu->disableScissor(); |
| 528 | this->setGpuStencil(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 529 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 537 | // Either a hard (stencil buffer) clip was explicitly requested or |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 538 | // 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.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 541 | // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 542 | // the stencil buffer path. These may be "incorrectly" clearing the |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 543 | // AA cache. |
| 544 | fAACache.reset(); |
| 545 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 546 | // If the clip is a rectangle then just set the scissor. Otherwise, create |
| 547 | // a stencil mask. |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 548 | if (isIntersectionOfRects) { |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 549 | fGpu->enableScissor(devClipBounds); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 550 | this->setGpuStencil(); |
| 551 | return true; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 552 | } |
| 553 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 554 | // use the stencil clip if we can't represent the clip as a rectangle. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 555 | bool useStencil = !clipDataIn->fClipStack->isWideOpen() && |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 556 | !devClipBounds.isEmpty(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 557 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 558 | if (useStencil) { |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 559 | this->createStencilClipMask(*clipDataIn, devClipBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 560 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 561 | // 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.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 565 | fGpu->enableScissor(devClipBounds); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 566 | this->setGpuStencil(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 567 | return true; |
| 568 | } |
| 569 | |
| 570 | #define VISUALIZE_COMPLEX_CLIP 0 |
| 571 | |
| 572 | #if VISUALIZE_COMPLEX_CLIP |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 573 | #include "SkRandom.h" |
| 574 | SkRandom gRandom; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 575 | #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU()); |
| 576 | #else |
| 577 | #define SET_RANDOM_COLOR |
| 578 | #endif |
| 579 | |
| 580 | namespace { |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 581 | /** |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 582 | * 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.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 585 | * the two. |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 586 | */ |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 587 | bool contains(const SkRect& canvContainer, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 588 | const SkIRect& devContainee, |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 589 | const SkIPoint& origin) { |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 590 | return !devContainee.isEmpty() && !canvContainer.isEmpty() && |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 591 | canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) && |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 592 | canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) && |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 593 | canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) && |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 594 | canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY); |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 595 | } |
| 596 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 597 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 598 | // 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.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 601 | const SkClipStack::Iter::Clip* process_initial_clip_elements( |
| 602 | SkClipStack::Iter* iter, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 603 | const GrIRect& devBounds, |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 604 | bool* clearToInside, |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 605 | SkRegion::Op* firstOp, |
| 606 | const GrClipData& clipData) { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 607 | |
| 608 | GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 609 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 610 | // logically before the first element of the clip stack is |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 611 | // 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.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 615 | bool done = false; |
| 616 | *clearToInside = true; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 617 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 618 | const SkClipStack::Iter::Clip* clip = NULL; |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 619 | |
| 620 | for (clip = iter->skipToTopmost(SkRegion::kReplace_Op); |
| 621 | NULL != clip && !done; |
bsalomon@google.com | 5fac58c | 2012-11-29 21:14:59 +0000 | [diff] [blame] | 622 | clip = iter->next()) { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 623 | switch (clip->fOp) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 624 | case SkRegion::kReplace_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 625 | // replace ignores everything previous |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 626 | *firstOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 627 | *clearToInside = false; |
| 628 | done = true; |
| 629 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 630 | case SkRegion::kIntersect_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 631 | // if this element contains the entire bounds then we |
| 632 | // can skip it. |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 633 | if (NULL != clip->fRect && |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 634 | contains(*clip->fRect, devBounds, clipData.fOrigin)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 635 | 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.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 641 | *firstOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 642 | *clearToInside = false; |
| 643 | done = true; |
| 644 | } |
| 645 | break; |
| 646 | // we can skip a leading union. |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 647 | case SkRegion::kUnion_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 648 | // if everything is initially outside then union is |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 649 | // same as replace. Otherwise, every pixel is still |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 650 | // clearToInside |
| 651 | if (!*clearToInside) { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 652 | *firstOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 653 | done = true; |
| 654 | } |
| 655 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 656 | case SkRegion::kXOR_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 657 | // 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.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 660 | *firstOp = SkRegion::kDifference_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 661 | } else { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 662 | *firstOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 663 | } |
| 664 | done = true; |
| 665 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 666 | case SkRegion::kDifference_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 667 | // 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.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 671 | *firstOp = SkRegion::kDifference_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 672 | done = true; |
| 673 | } |
| 674 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 675 | case SkRegion::kReverseDifference_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 676 | // if all pixels are clearToInside then reverse difference |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 677 | // produces empty set. Otherise it is same as replace |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 678 | if (*clearToInside) { |
| 679 | *clearToInside = false; |
| 680 | } else { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 681 | *firstOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 682 | done = true; |
| 683 | } |
| 684 | break; |
| 685 | default: |
| 686 | GrCrash("Unknown set op."); |
| 687 | } |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 688 | |
| 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.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 694 | } |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 695 | return clip; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 696 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 697 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 698 | } |
| 699 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 700 | namespace { |
| 701 | |
| 702 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 703 | // set up the OpenGL blend function to perform the specified |
| 704 | // boolean operation for alpha clip mask creation |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 705 | void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 706 | |
| 707 | switch (op) { |
| 708 | case SkRegion::kReplace_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 709 | drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 710 | break; |
| 711 | case SkRegion::kIntersect_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 712 | drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 713 | break; |
| 714 | case SkRegion::kUnion_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 715 | drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 716 | break; |
| 717 | case SkRegion::kXOR_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 718 | drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 719 | break; |
| 720 | case SkRegion::kDifference_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 721 | drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 722 | break; |
| 723 | case SkRegion::kReverseDifference_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 724 | drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 725 | break; |
| 726 | default: |
| 727 | GrAssert(false); |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 732 | //////////////////////////////////////////////////////////////////////////////// |
| 733 | bool 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.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 757 | |
| 758 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 759 | bool 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.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 774 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 775 | // 'rect' enters in device coordinates and leaves in canvas coordinates |
| 776 | void 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.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 785 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 786 | |
| 787 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 788 | bool GrClipMaskManager::drawClipShape(GrTexture* target, |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 789 | const SkClipStack::Iter::Clip* clip, |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 790 | const GrIRect& resultBounds) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 791 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 792 | GrAssert(NULL != drawState); |
| 793 | |
| 794 | drawState->setRenderTarget(target->asRenderTarget()); |
| 795 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 796 | if (NULL != clip->fRect) { |
| 797 | if (clip->fDoAA) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 798 | getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu, |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 799 | *clip->fRect, |
robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 800 | true); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 801 | } else { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 802 | fGpu->drawSimpleRect(*clip->fRect, NULL); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 803 | } |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 804 | } else if (NULL != clip->fPath) { |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 805 | return draw_path(this->getContext(), fGpu, |
| 806 | *clip->fPath, |
| 807 | get_path_fill(*clip->fPath), |
| 808 | clip->fDoAA, |
| 809 | resultBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 810 | } |
| 811 | return true; |
| 812 | } |
| 813 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 814 | void GrClipMaskManager::mergeMask(GrTexture* dstMask, |
| 815 | GrTexture* srcMask, |
| 816 | SkRegion::Op op, |
| 817 | const GrIRect& dstBound, |
| 818 | const GrIRect& srcBound) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 819 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 820 | GrAssert(NULL != drawState); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 821 | SkMatrix oldMatrix = drawState->getViewMatrix(); |
| 822 | drawState->viewMatrix()->reset(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 823 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 824 | drawState->setRenderTarget(dstMask->asRenderTarget()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 825 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 826 | setup_boolean_blendcoeffs(drawState, op); |
skia.committer@gmail.com | 72b2e6f | 2012-11-08 02:03:56 +0000 | [diff] [blame] | 827 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 828 | SkMatrix sampleM; |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 829 | 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.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 836 | |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 837 | drawState->disableStage(0); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 838 | drawState->setViewMatrix(oldMatrix); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 839 | } |
| 840 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 841 | // 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 843 | void GrClipMaskManager::getTemp(const GrIRect& bounds, |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 844 | GrAutoScratchTexture* temp) { |
| 845 | if (NULL != temp->texture()) { |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 846 | // we've already allocated the temp texture |
| 847 | return; |
| 848 | } |
| 849 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 850 | 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.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 855 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 856 | temp->set(this->getContext(), desc); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 857 | } |
| 858 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 859 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 860 | void GrClipMaskManager::setupCache(const SkClipStack& clipIn, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 861 | const GrIRect& bounds) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 862 | // 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.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 866 | 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.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 871 | |
| 872 | fAACache.acquireMask(clipIn, desc, bounds); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 873 | } |
| 874 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 875 | //////////////////////////////////////////////////////////////////////////////// |
| 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.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 880 | bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 881 | GrTexture** result, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 882 | GrIRect* devResultBounds) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 883 | GrDrawState* origDrawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 884 | GrAssert(origDrawState->isClipState()); |
| 885 | |
| 886 | GrRenderTarget* rt = origDrawState->getRenderTarget(); |
| 887 | GrAssert(NULL != rt); |
| 888 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 889 | // 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.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 891 | // Note: intBounds is in device (as opposed to canvas) coordinates |
robertphillips@google.com | 9cb5adf | 2012-08-30 11:05:08 +0000 | [diff] [blame] | 892 | clipDataIn.getConservativeBounds(rt, devResultBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 893 | |
| 894 | // need to outset a pixel since the standard bounding box computation |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 895 | // path doesn't leave any room for antialiasing (esp. w.r.t. rects) |
robertphillips@google.com | 9cb5adf | 2012-08-30 11:05:08 +0000 | [diff] [blame] | 896 | devResultBounds->outset(1, 1); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 897 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 898 | // TODO: make sure we don't outset if bounds are still 0,0 @ min |
| 899 | |
robertphillips@google.com | ba998f2 | 2012-10-12 11:33:56 +0000 | [diff] [blame] | 900 | if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) { |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 901 | *result = fAACache.getLastMask(); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 902 | fAACache.getLastBound(devResultBounds); |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 903 | return true; |
| 904 | } |
| 905 | |
robertphillips@google.com | 9cb5adf | 2012-08-30 11:05:08 +0000 | [diff] [blame] | 906 | this->setupCache(*clipDataIn.fClipStack, *devResultBounds); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 907 | return false; |
| 908 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 909 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 910 | //////////////////////////////////////////////////////////////////////////////// |
| 911 | // Create a 8-bit clip mask in alpha |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 912 | bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 913 | GrTexture** result, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 914 | GrIRect *devResultBounds) { |
| 915 | GrAssert(NULL != devResultBounds); |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 916 | GrAssert(kNone_ClipMaskType == fCurrClipMaskType); |
| 917 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 918 | if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 919 | fCurrClipMaskType = kAlpha_ClipMaskType; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 920 | return true; |
| 921 | } |
| 922 | |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 923 | // Note: 'resultBounds' is in device (as opposed to canvas) coordinates |
| 924 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 925 | GrTexture* accum = fAACache.getLastMask(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 926 | if (NULL == accum) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 927 | fAACache.reset(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 928 | return false; |
| 929 | } |
| 930 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 931 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
| 932 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 933 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 934 | GrDrawTarget::AutoGeometryPush agp(fGpu); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 935 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 936 | // 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.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 948 | |
| 949 | bool clearToInside; |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 950 | SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning |
| 951 | |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 952 | SkClipStack::Iter iter(*clipDataIn.fClipStack, |
| 953 | SkClipStack::Iter::kBottom_IterStart); |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 954 | const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 955 | *devResultBounds, |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 956 | &clearToInside, |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 957 | &firstOp, |
| 958 | clipDataIn); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 959 | // 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 962 | clearToInside ? 0xffffffff : 0x00000000, |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 963 | accum->asRenderTarget()); |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 964 | bool accumClearedToZero = !clearToInside; |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 965 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 966 | GrAutoScratchTexture temp; |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 967 | bool first = true; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 968 | // walk through each clip element and perform its set op |
bsalomon@google.com | 5fac58c | 2012-11-29 21:14:59 +0000 | [diff] [blame] | 969 | for ( ; NULL != clip; clip = iter.next()) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 970 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 971 | SkRegion::Op op = clip->fOp; |
| 972 | if (first) { |
| 973 | first = false; |
| 974 | op = firstOp; |
| 975 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 976 | |
bsalomon@google.com | 6794a25 | 2012-11-08 15:30:53 +0000 | [diff] [blame] | 977 | if (SkRegion::kReplace_Op == op) { |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 978 | // 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.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 983 | setup_boolean_blendcoeffs(drawState, op); |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 984 | this->drawClipShape(accum, clip, *devResultBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 985 | |
| 986 | } else if (SkRegion::kReverseDifference_Op == op || |
| 987 | SkRegion::kIntersect_Op == op) { |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 988 | // there is no point in intersecting a screen filling rectangle. |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 989 | if (SkRegion::kIntersect_Op == op && NULL != clip->fRect && |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 990 | contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 991 | continue; |
| 992 | } |
| 993 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 994 | getTemp(*devResultBounds, &temp); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 995 | if (NULL == temp.texture()) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 996 | fAACache.reset(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 997 | return false; |
| 998 | } |
| 999 | |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 1000 | // this is the bounds of the clip element in the space of the alpha-mask. The temporary |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 1001 | // 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.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 1003 | // the accumulator |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 1004 | GrRect elementMaskBounds = clip->getBounds(); |
| 1005 | elementMaskBounds.offset(clipToMaskOffset); |
| 1006 | GrIRect elementMaskIBounds; |
| 1007 | elementMaskBounds.roundOut(&elementMaskIBounds); |
| 1008 | |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 1009 | // clear the temp target & draw into it |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 1010 | fGpu->clear(&elementMaskIBounds, 0x00000000, temp.texture()->asRenderTarget()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1011 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1012 | setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op); |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 1013 | this->drawClipShape(temp.texture(), clip, elementMaskIBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1014 | |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 1015 | // Now draw into the accumulator using the real operation |
| 1016 | // and the temp buffer as a texture |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 1017 | this->mergeMask(accum, temp.texture(), op, maskResultBounds, elementMaskIBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1018 | } else { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1019 | // all the remaining ops can just be directly draw into |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1020 | // the accumulation buffer |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1021 | setup_boolean_blendcoeffs(drawState, op); |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 1022 | this->drawClipShape(accum, clip, *devResultBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1023 | } |
bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 1024 | accumClearedToZero = false; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 1027 | *result = accum; |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1028 | fCurrClipMaskType = kAlpha_ClipMaskType; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1033 | // Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1034 | // (as opposed to canvas) coordinates |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 1035 | bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1036 | const GrIRect& devClipBounds) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1037 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1038 | GrAssert(kNone_ClipMaskType == fCurrClipMaskType); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1039 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 1040 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1041 | 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.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 1052 | if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1053 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 1054 | stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1055 | |
| 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.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 1060 | const GrClipData* oldClipData = fGpu->getClip(); |
| 1061 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1062 | // The origin of 'newClipData' is (0, 0) so it is okay to place |
| 1063 | // a device-coordinate bound in 'newClipStack' |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 1064 | SkClipStack newClipStack(devClipBounds); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 1065 | GrClipData newClipData; |
| 1066 | newClipData.fClipStack = &newClipStack; |
| 1067 | |
| 1068 | fGpu->setClip(&newClipData); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1069 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 1070 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
| 1071 | drawState = fGpu->drawState(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1072 | drawState->setRenderTarget(rt); |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 1073 | GrDrawTarget::AutoGeometryPush agp(fGpu); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1074 | |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1075 | if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1076 | // Add the saveLayer's offset to the view matrix rather than |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1077 | // offset each individual draw |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1078 | drawState->viewMatrix()->setTranslate( |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1079 | SkIntToScalar(-clipDataIn.fOrigin.fX), |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1080 | SkIntToScalar(-clipDataIn.fOrigin.fY)); |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1083 | #if !VISUALIZE_COMPLEX_CLIP |
| 1084 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
| 1085 | #endif |
| 1086 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1087 | 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.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1092 | GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1093 | |
| 1094 | bool clearToInside; |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1095 | SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning |
| 1096 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1097 | SkClipStack::Iter iter(*oldClipData->fClipStack, |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 1098 | SkClipStack::Iter::kBottom_IterStart); |
| 1099 | const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1100 | devRTRect, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 1101 | &clearToInside, |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1102 | &firstOp, |
| 1103 | clipDataIn); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1104 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1105 | fGpu->clearStencilClip(devClipBounds, clearToInside); |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1106 | bool first = true; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1107 | |
| 1108 | // walk through each clip element and perform its set op |
| 1109 | // with the existing clip. |
bsalomon@google.com | 5fac58c | 2012-11-29 21:14:59 +0000 | [diff] [blame] | 1110 | for ( ; NULL != clip; clip = iter.next()) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1111 | GrPathFill fill; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 1112 | bool fillInverted = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1113 | // enabled at bottom of loop |
| 1114 | drawState->disableState(GrGpu::kModifyStencilClip_StateBit); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 1115 | // if the target is MSAA then we want MSAA enabled when the clip is soft |
| 1116 | if (rt->isMultisampled()) { |
bsalomon@google.com | d5d69ff | 2012-10-04 19:42:00 +0000 | [diff] [blame] | 1117 | drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 1118 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1119 | |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 1120 | // 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.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1124 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1125 | SkRegion::Op op = clip->fOp; |
| 1126 | if (first) { |
| 1127 | first = false; |
| 1128 | op = firstOp; |
| 1129 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1130 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1131 | GrPathRenderer* pr = NULL; |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 1132 | const SkPath* clipPath = NULL; |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1133 | if (NULL != clip->fRect) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1134 | canRenderDirectToStencil = true; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1135 | fill = kEvenOdd_GrPathFill; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1136 | fillInverted = false; |
| 1137 | // there is no point in intersecting a screen filling |
| 1138 | // rectangle. |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1139 | if (SkRegion::kIntersect_Op == op && |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1140 | contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1141 | continue; |
| 1142 | } |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 1143 | } else { |
| 1144 | GrAssert(NULL != clip->fPath); |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1145 | fill = get_path_fill(*clip->fPath); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1146 | fillInverted = GrIsFillInverted(fill); |
| 1147 | fill = GrNonInvertedFill(fill); |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1148 | clipPath = clip->fPath; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 1149 | pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1150 | if (NULL == pr) { |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 1151 | fGpu->setClip(oldClipData); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1152 | return false; |
| 1153 | } |
| 1154 | canRenderDirectToStencil = |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 1155 | !pr->requiresStencilPass(*clipPath, fill, fGpu); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1158 | 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.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 1167 | canRenderDirectToStencil, |
| 1168 | clipBit, |
| 1169 | fillInverted, |
| 1170 | &passes, |
| 1171 | stencilSettings); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1172 | |
| 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.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1183 | if (NULL != clip->fRect) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1184 | *drawState->stencil() = gDrawToStencil; |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1185 | fGpu->drawSimpleRect(*clip->fRect, NULL); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1186 | } else { |
| 1187 | if (canRenderDirectToStencil) { |
| 1188 | *drawState->stencil() = gDrawToStencil; |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 1189 | pr->drawPath(*clipPath, fill, fGpu, false); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1190 | } else { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 1191 | pr->drawPathToStencil(*clipPath, fill, fGpu); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1192 | } |
| 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.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1202 | if (NULL != clip->fRect) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1203 | SET_RANDOM_COLOR |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1204 | fGpu->drawSimpleRect(*clip->fRect, NULL); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1205 | } else { |
| 1206 | SET_RANDOM_COLOR |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 1207 | pr->drawPath(*clipPath, fill, fGpu, false); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1208 | } |
| 1209 | } else { |
| 1210 | SET_RANDOM_COLOR |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1211 | // 'devClipBounds' is already in device coordinates so the |
| 1212 | // translation in the view matrix is inappropriate. |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1213 | // Convert it to canvas space so the drawn rect will |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1214 | // be in the correct location |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1215 | GrRect canvClipBounds; |
| 1216 | canvClipBounds.set(devClipBounds); |
| 1217 | device_to_canvas(&canvClipBounds, clipDataIn.fOrigin); |
| 1218 | fGpu->drawSimpleRect(canvClipBounds, NULL); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | // restore clip |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 1223 | fGpu->setClip(oldClipData); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1224 | } |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1225 | // set this last because recursive draws may overwrite it back to kNone. |
| 1226 | GrAssert(kNone_ClipMaskType == fCurrClipMaskType); |
| 1227 | fCurrClipMaskType = kStencil_ClipMaskType; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1228 | return true; |
| 1229 | } |
| 1230 | |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1231 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1232 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 1233 | // mapping depends on whether stencil-clipping is in effect. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1234 | static const GrStencilFunc |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1235 | 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.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1268 | namespace { |
| 1269 | // Sets the settings to clip against the stencil buffer clip while ignoring the |
| 1270 | // client bits. |
| 1271 | const 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1279 | 0x0000); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1280 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | void 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1323 | GrStencilBuffer* stencilBuffer = |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1324 | drawState.getRenderTarget()->getStencilBuffer(); |
| 1325 | if (NULL != stencilBuffer) { |
| 1326 | stencilBits = stencilBuffer->bits(); |
| 1327 | } |
| 1328 | |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 1329 | GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || |
bsalomon@google.com | 9e553c6 | 2012-06-22 12:23:29 +0000 | [diff] [blame] | 1330 | !settings.usesWrapOp()); |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 1331 | GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided()); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1332 | this->adjustStencilParams(&settings, clipMode, stencilBits); |
| 1333 | fGpu->setStencilSettings(settings); |
| 1334 | } |
| 1335 | |
| 1336 | void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings, |
| 1337 | StencilClipMode mode, |
| 1338 | int stencilBitCnt) { |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1339 | GrAssert(stencilBitCnt > 0); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1340 | |
| 1341 | if (kModifyClip_StencilClipMode == mode) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1342 | // We assume that this clip manager itself is drawing to the GrGpu and |
| 1343 | // has already setup the correct values. |
| 1344 | return; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1345 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1346 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1347 | unsigned int clipBit = (1 << (stencilBitCnt - 1)); |
| 1348 | unsigned int userBits = clipBit - 1; |
| 1349 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1350 | GrStencilSettings::Face face = GrStencilSettings::kFront_Face; |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 1351 | bool twoSided = fGpu->getCaps().twoSidedStencilSupport(); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1352 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1353 | 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.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1390 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1391 | const GrStencilFunc* table = |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1392 | gSpecialToBasicStencilFunc[respectClip]; |
| 1393 | func = table[func - kBasicStencilFuncCount]; |
| 1394 | GrAssert(func >= 0 && func < kBasicStencilFuncCount); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1395 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1396 | funcMask &= userBits; |
| 1397 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1398 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1399 | |
| 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.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1411 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1412 | if (!twoSided) { |
| 1413 | settings->copyFrontSettingsToBack(); |
| 1414 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | //////////////////////////////////////////////////////////////////////////////// |
| 1418 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1419 | namespace { |
| 1420 | |
| 1421 | GrPathFill invert_fill(GrPathFill fill) { |
| 1422 | static const GrPathFill gInvertedFillTable[] = { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1423 | 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.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1428 | }; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1429 | 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.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1435 | return gInvertedFillTable[fill]; |
| 1436 | } |
| 1437 | |
| 1438 | } |
| 1439 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 1440 | bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1441 | GrTexture** result, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1442 | GrIRect* devResultBounds) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1443 | GrAssert(kNone_ClipMaskType == fCurrClipMaskType); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1444 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1445 | if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1446 | return true; |
| 1447 | } |
| 1448 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1449 | GrTexture* accum = fAACache.getLastMask(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1450 | if (NULL == accum) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1451 | fAACache.reset(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1452 | return false; |
| 1453 | } |
| 1454 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 1455 | GrSWMaskHelper helper(this->getContext()); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1456 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1457 | SkMatrix matrix; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1458 | matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX), |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1459 | SkIntToScalar(-clipDataIn.fOrigin.fY)); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1460 | helper.init(*devResultBounds, &matrix); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1461 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1462 | bool clearToInside; |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1463 | SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning |
| 1464 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1465 | SkClipStack::Iter iter(*clipDataIn.fClipStack, |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 1466 | SkClipStack::Iter::kBottom_IterStart); |
| 1467 | const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1468 | *devResultBounds, |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1469 | &clearToInside, |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 1470 | &firstOp, |
| 1471 | clipDataIn); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1472 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1473 | helper.clear(clearToInside ? 0xFF : 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1474 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1475 | bool first = true; |
bsalomon@google.com | 5fac58c | 2012-11-29 21:14:59 +0000 | [diff] [blame] | 1476 | for ( ; NULL != clip; clip = iter.next()) { |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1477 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1478 | SkRegion::Op op = clip->fOp; |
| 1479 | if (first) { |
| 1480 | first = false; |
| 1481 | op = firstOp; |
| 1482 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1483 | |
| 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.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 1493 | SkRect temp; |
| 1494 | temp.set(*devResultBounds); |
robertphillips@google.com | ba998f2 | 2012-10-12 11:33:56 +0000 | [diff] [blame] | 1495 | temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX), |
| 1496 | SkIntToScalar(clipDataIn.fOrigin.fX)); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1497 | |
| 1498 | // invert the entire scene |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1499 | helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1502 | if (NULL != clip->fRect) { |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1503 | |
| 1504 | // convert the rect to a path so we can invert the fill |
| 1505 | SkPath temp; |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1506 | temp.addRect(*clip->fRect); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1507 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1508 | helper.draw(temp, SkRegion::kReplace_Op, |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1509 | kInverseEvenOdd_GrPathFill, clip->fDoAA, |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1510 | 0x00); |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1511 | } else if (NULL != clip->fPath) { |
| 1512 | helper.draw(*clip->fPath, |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1513 | SkRegion::kReplace_Op, |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1514 | invert_fill(get_path_fill(*clip->fPath)), |
| 1515 | clip->fDoAA, |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1516 | 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1517 | } |
| 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.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1524 | if (NULL != clip->fRect) { |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1525 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1526 | helper.draw(*clip->fRect, |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1527 | op, |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1528 | clip->fDoAA, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1529 | |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1530 | } else if (NULL != clip->fPath) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1531 | helper.draw(*clip->fPath, |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1532 | op, |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1533 | get_path_fill(*clip->fPath), |
| 1534 | clip->fDoAA, 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1538 | // 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.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 1541 | fGpu->clear(NULL, 0x00000000, accum->asRenderTarget()); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1542 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1543 | helper.toTexture(accum, clearToInside ? 0xFF : 0x00); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1544 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1545 | *result = accum; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1546 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1547 | fCurrClipMaskType = kAlpha_ClipMaskType; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1548 | return true; |
| 1549 | } |
| 1550 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1551 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1552 | void GrClipMaskManager::releaseResources() { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1553 | fAACache.releaseResources(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1554 | } |