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" |
| 10 | #include "GrGpu.h" |
| 11 | #include "GrRenderTarget.h" |
| 12 | #include "GrStencilBuffer.h" |
| 13 | #include "GrPathRenderer.h" |
| 14 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 15 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 16 | void ScissoringSettings::setupScissoring(GrGpu* gpu) { |
| 17 | if (!fEnableScissoring) { |
| 18 | gpu->disableScissor(); |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | gpu->enableScissoring(fScissorRect); |
| 23 | } |
| 24 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 25 | //////////////////////////////////////////////////////////////////////////////// |
| 26 | // sort out what kind of clip mask needs to be created: alpha, stencil |
| 27 | // or scissor |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 28 | bool GrClipMaskManager::createClipMask(GrGpu* gpu, |
| 29 | const GrClip& clipIn, |
| 30 | ScissoringSettings* scissorSettings) { |
| 31 | |
| 32 | GrAssert(scissorSettings); |
| 33 | |
| 34 | scissorSettings->fEnableScissoring = false; |
| 35 | fClipMaskInStencil = false; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 36 | fClipMaskInAlpha = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 37 | |
| 38 | GrDrawState* drawState = gpu->drawState(); |
| 39 | if (!drawState->isClipState()) { |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | GrRenderTarget* rt = drawState->getRenderTarget(); |
| 44 | |
| 45 | // GrDrawTarget should have filtered this for us |
| 46 | GrAssert(NULL != rt); |
| 47 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 48 | #if GR_AA_CLIP |
| 49 | // If MSAA is enabled use the (faster) stencil path for AA clipping |
| 50 | // otherwise the alpha clip mask is our only option |
| 51 | if (clipIn.requiresAA() && 0 == rt->numSamples()) { |
| 52 | // Since we are going to create a destination texture of the correct |
| 53 | // size for the mask (rather than being bound by the size of the |
| 54 | // render target) we aren't going to use scissoring like the stencil |
| 55 | // path does (see scissorSettings below) |
| 56 | if (this->createAlphaClipMask(gpu, clipIn)) { |
| 57 | fClipMaskInAlpha = true; |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | // if alpha clip mask creation fails fall through to the stencil |
| 62 | // buffer method |
| 63 | } |
| 64 | #endif // GR_AA_CLIP |
| 65 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 66 | GrRect bounds; |
| 67 | GrRect rtRect; |
| 68 | rtRect.setLTRB(0, 0, |
| 69 | GrIntToScalar(rt->width()), GrIntToScalar(rt->height())); |
| 70 | if (clipIn.hasConservativeBounds()) { |
| 71 | bounds = clipIn.getConservativeBounds(); |
| 72 | if (!bounds.intersect(rtRect)) { |
| 73 | bounds.setEmpty(); |
| 74 | } |
| 75 | } else { |
| 76 | bounds = rtRect; |
| 77 | } |
| 78 | |
| 79 | bounds.roundOut(&scissorSettings->fScissorRect); |
| 80 | if (scissorSettings->fScissorRect.isEmpty()) { |
| 81 | scissorSettings->fScissorRect.setLTRB(0,0,0,0); |
| 82 | // TODO: I think we can do an early exit here - after refactoring try: |
| 83 | // set fEnableScissoring to true but leave fClipMaskInStencil false |
| 84 | // and return - everything is going to be scissored away anyway! |
| 85 | } |
| 86 | scissorSettings->fEnableScissoring = true; |
| 87 | |
| 88 | // use the stencil clip if we can't represent the clip as a rectangle. |
| 89 | fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() && |
| 90 | !bounds.isEmpty(); |
| 91 | |
| 92 | if (fClipMaskInStencil) { |
| 93 | return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings); |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | #define VISUALIZE_COMPLEX_CLIP 0 |
| 100 | |
| 101 | #if VISUALIZE_COMPLEX_CLIP |
| 102 | #include "GrRandom.h" |
| 103 | GrRandom gRandom; |
| 104 | #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU()); |
| 105 | #else |
| 106 | #define SET_RANDOM_COLOR |
| 107 | #endif |
| 108 | |
| 109 | namespace { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 110 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 111 | // determines how many elements at the head of the clip can be skipped and |
| 112 | // whether the initial clear should be to the inside- or outside-the-clip value, |
| 113 | // and what op should be used to draw the first element that isn't skipped. |
| 114 | int process_initial_clip_elements(const GrClip& clip, |
| 115 | const GrRect& bounds, |
| 116 | bool* clearToInside, |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 117 | SkRegion::Op* startOp) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 118 | |
| 119 | // logically before the first element of the clip stack is |
| 120 | // processed the clip is entirely open. However, depending on the |
| 121 | // first set op we may prefer to clear to 0 for performance. We may |
| 122 | // also be able to skip the initial clip paths/rects. We loop until |
| 123 | // we cannot skip an element. |
| 124 | int curr; |
| 125 | bool done = false; |
| 126 | *clearToInside = true; |
| 127 | int count = clip.getElementCount(); |
| 128 | |
| 129 | for (curr = 0; curr < count && !done; ++curr) { |
| 130 | switch (clip.getOp(curr)) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 131 | case SkRegion::kReplace_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 132 | // replace ignores everything previous |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 133 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 134 | *clearToInside = false; |
| 135 | done = true; |
| 136 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 137 | case SkRegion::kIntersect_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 138 | // if this element contains the entire bounds then we |
| 139 | // can skip it. |
| 140 | if (kRect_ClipType == clip.getElementType(curr) |
| 141 | && clip.getRect(curr).contains(bounds)) { |
| 142 | break; |
| 143 | } |
| 144 | // if everything is initially clearToInside then intersect is |
| 145 | // same as clear to 0 and treat as a replace. Otherwise, |
| 146 | // set stays empty. |
| 147 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 148 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 149 | *clearToInside = false; |
| 150 | done = true; |
| 151 | } |
| 152 | break; |
| 153 | // we can skip a leading union. |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 154 | case SkRegion::kUnion_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 155 | // if everything is initially outside then union is |
| 156 | // same as replace. Otherwise, every pixel is still |
| 157 | // clearToInside |
| 158 | if (!*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 159 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 160 | done = true; |
| 161 | } |
| 162 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 163 | case SkRegion::kXOR_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 164 | // xor is same as difference or replace both of which |
| 165 | // can be 1-pass instead of 2 for xor. |
| 166 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 167 | *startOp = SkRegion::kDifference_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 168 | } else { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 169 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 170 | } |
| 171 | done = true; |
| 172 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 173 | case SkRegion::kDifference_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 174 | // if all pixels are clearToInside then we have to process the |
| 175 | // difference, otherwise it has no effect and all pixels |
| 176 | // remain outside. |
| 177 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 178 | *startOp = SkRegion::kDifference_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 179 | done = true; |
| 180 | } |
| 181 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 182 | case SkRegion::kReverseDifference_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 183 | // if all pixels are clearToInside then reverse difference |
| 184 | // produces empty set. Otherise it is same as replace |
| 185 | if (*clearToInside) { |
| 186 | *clearToInside = false; |
| 187 | } else { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 188 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 189 | done = true; |
| 190 | } |
| 191 | break; |
| 192 | default: |
| 193 | GrCrash("Unknown set op."); |
| 194 | } |
| 195 | } |
| 196 | return done ? curr-1 : count; |
| 197 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 198 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 199 | } |
| 200 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 201 | |
| 202 | namespace { |
| 203 | |
| 204 | //////////////////////////////////////////////////////////////////////////////// |
| 205 | // set up the OpenGL blend function to perform the specified |
| 206 | // boolean operation for alpha clip mask creation |
| 207 | void setUpBooleanBlendCoeffs(GrDrawState* drawState, SkRegion::Op op) { |
| 208 | |
| 209 | switch (op) { |
| 210 | case SkRegion::kReplace_Op: |
| 211 | drawState->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff); |
| 212 | break; |
| 213 | case SkRegion::kIntersect_Op: |
| 214 | drawState->setBlendFunc(kDC_BlendCoeff, kZero_BlendCoeff); |
| 215 | break; |
| 216 | case SkRegion::kUnion_Op: |
| 217 | drawState->setBlendFunc(kOne_BlendCoeff, kISC_BlendCoeff); |
| 218 | break; |
| 219 | case SkRegion::kXOR_Op: |
| 220 | drawState->setBlendFunc(kIDC_BlendCoeff, kISC_BlendCoeff); |
| 221 | break; |
| 222 | case SkRegion::kDifference_Op: |
| 223 | drawState->setBlendFunc(kZero_BlendCoeff, kISC_BlendCoeff); |
| 224 | break; |
| 225 | case SkRegion::kReverseDifference_Op: |
| 226 | drawState->setBlendFunc(kIDC_BlendCoeff, kZero_BlendCoeff); |
| 227 | break; |
| 228 | default: |
| 229 | GrAssert(false); |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | } |
| 235 | |
| 236 | //////////////////////////////////////////////////////////////////////////////// |
| 237 | bool GrClipMaskManager::drawPath(GrGpu* gpu, |
| 238 | const GrPath& path, |
| 239 | GrPathFill fill, |
| 240 | bool doAA) { |
| 241 | |
| 242 | GrPathRenderer* pr = this->getClipPathRenderer(gpu, path, fill, doAA); |
| 243 | if (NULL == pr) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | pr->drawPath(path, fill, NULL, gpu, 0, doAA); |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | //////////////////////////////////////////////////////////////////////////////// |
| 252 | bool GrClipMaskManager::drawClipShape(GrGpu* gpu, |
| 253 | GrTexture* target, |
| 254 | const GrClip& clipIn, |
| 255 | int index) { |
| 256 | GrDrawState* drawState = gpu->drawState(); |
| 257 | GrAssert(NULL != drawState); |
| 258 | |
| 259 | drawState->setRenderTarget(target->asRenderTarget()); |
| 260 | |
| 261 | if (kRect_ClipType == clipIn.getElementType(index)) { |
| 262 | if (clipIn.getDoAA(index)) { |
| 263 | // convert the rect to a path for AA |
| 264 | SkPath temp; |
| 265 | temp.addRect(clipIn.getRect(index)); |
| 266 | |
| 267 | return this->drawPath(gpu, temp, |
| 268 | kEvenOdd_PathFill, clipIn.getDoAA(index)); |
| 269 | } else { |
| 270 | gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0); |
| 271 | } |
| 272 | } else { |
| 273 | return this->drawPath(gpu, |
| 274 | clipIn.getPath(index), |
| 275 | clipIn.getPathFill(index), |
| 276 | clipIn.getDoAA(index)); |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | void GrClipMaskManager::drawTexture(GrGpu* gpu, |
| 282 | GrTexture* target, |
| 283 | const GrRect& rect, |
| 284 | GrTexture* texture) { |
| 285 | GrDrawState* drawState = gpu->drawState(); |
| 286 | GrAssert(NULL != drawState); |
| 287 | |
| 288 | // no AA here since it is encoded in the texture |
| 289 | drawState->setRenderTarget(target->asRenderTarget()); |
| 290 | |
| 291 | GrMatrix sampleM; |
| 292 | sampleM.setIDiv(texture->width(), texture->height()); |
| 293 | drawState->setTexture(0, texture); |
| 294 | |
| 295 | drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode, |
| 296 | GrSamplerState::kNearest_Filter, |
| 297 | sampleM); |
| 298 | |
| 299 | gpu->drawSimpleRect(rect, NULL, 1 << 0); |
| 300 | |
| 301 | drawState->setTexture(0, NULL); |
| 302 | } |
| 303 | |
| 304 | namespace { |
| 305 | |
| 306 | void clear(GrGpu* gpu, |
| 307 | GrTexture* target, |
| 308 | const GrRect& bounds, |
| 309 | GrColor color) { |
| 310 | GrDrawState* drawState = gpu->drawState(); |
| 311 | GrAssert(NULL != drawState); |
| 312 | |
| 313 | // zap entire target to specified color |
| 314 | drawState->setRenderTarget(target->asRenderTarget()); |
| 315 | gpu->clear(NULL, color); |
| 316 | } |
| 317 | |
| 318 | } |
| 319 | |
| 320 | //////////////////////////////////////////////////////////////////////////////// |
| 321 | // Create a 8-bit clip mask in alpha |
| 322 | bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu, const GrClip& clipIn) { |
| 323 | |
| 324 | GrDrawState* origDrawState = gpu->drawState(); |
| 325 | GrAssert(origDrawState->isClipState()); |
| 326 | |
| 327 | GrRenderTarget* rt = origDrawState->getRenderTarget(); |
| 328 | GrAssert(NULL != rt); |
| 329 | |
| 330 | GrRect rtRect; |
| 331 | rtRect.setLTRB(0, 0, |
| 332 | GrIntToScalar(rt->width()), GrIntToScalar(rt->height())); |
| 333 | |
| 334 | // unlike the stencil path the alpha path is not bound to the size of the |
| 335 | // render target - determine the minimum size required for the mask |
| 336 | GrRect bounds; |
| 337 | |
| 338 | if (clipIn.hasConservativeBounds()) { |
| 339 | bounds = clipIn.getConservativeBounds(); |
| 340 | if (!bounds.intersect(rtRect)) { |
| 341 | // the mask will be empty in this case |
| 342 | GrAssert(false); |
| 343 | bounds.setEmpty(); |
| 344 | } |
| 345 | } else { |
| 346 | // still locked to the size of the render target |
| 347 | bounds = rtRect; |
| 348 | } |
| 349 | |
| 350 | bounds.roundOut(); |
| 351 | |
| 352 | // need to outset a pixel since the standard bounding box computation |
| 353 | // path doesn't leave any room for antialiasing (esp. w.r.t. rects) |
| 354 | bounds.outset(SkIntToScalar(1), SkIntToScalar(1)); |
| 355 | |
| 356 | GrAssert(SkScalarIsInt(bounds.width())); |
| 357 | GrAssert(SkScalarIsInt(bounds.height())); |
| 358 | |
| 359 | GrTextureDesc desc = { |
| 360 | kRenderTarget_GrTextureFlagBit, |
| 361 | SkScalarCeilToInt(bounds.width()), |
| 362 | SkScalarCeilToInt(bounds.height()), |
| 363 | kAlpha_8_GrPixelConfig, |
| 364 | 0 // samples |
| 365 | }; |
| 366 | |
| 367 | GrRect newRTBounds; |
| 368 | newRTBounds.setLTRB(0, 0, bounds.width(), bounds.height()); |
| 369 | |
| 370 | GrTexture* accum = gpu->createTexture(desc, NULL, 0); |
| 371 | GrTexture* temp = gpu->createTexture(desc, NULL, 0); |
| 372 | if (NULL == accum || NULL == temp) { |
| 373 | // TODO: free up accum & temp here! |
| 374 | fClipMaskInAlpha = false; |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit); |
| 379 | GrDrawState* drawState = gpu->drawState(); |
| 380 | |
| 381 | GrDrawTarget::AutoGeometryPush agp(gpu); |
| 382 | |
| 383 | int count = clipIn.getElementCount(); |
| 384 | |
| 385 | if (0 != bounds.fTop || 0 != bounds.fLeft) { |
| 386 | // if we were able to trim down the size of the mask we need to |
| 387 | // offset the paths & rects that will be used to compute it |
| 388 | GrMatrix m; |
| 389 | |
| 390 | m.setTranslate(-bounds.fLeft, -bounds.fTop); |
| 391 | |
| 392 | drawState->preConcatViewMatrix(m); |
| 393 | } |
| 394 | |
| 395 | bool clearToInside; |
| 396 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
| 397 | int start = process_initial_clip_elements(clipIn, |
| 398 | bounds, |
| 399 | &clearToInside, |
| 400 | &startOp); |
| 401 | |
| 402 | clear(gpu, accum, newRTBounds, clearToInside ? 0xffffffff : 0x00000000); |
| 403 | |
| 404 | // walk through each clip element and perform its set op |
| 405 | for (int c = start; c < count; ++c) { |
| 406 | |
| 407 | SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c); |
| 408 | |
| 409 | if (SkRegion::kReplace_Op == op) { |
| 410 | // TODO: replace is actually a lot faster then intersection |
| 411 | // for this path - refactor the stencil path so it can handle |
| 412 | // replace ops and alter GrClip to allow them through |
| 413 | |
| 414 | // clear the accumulator and draw the new object directly into it |
| 415 | clear(gpu, accum, newRTBounds, 0x00000000); |
| 416 | |
| 417 | setUpBooleanBlendCoeffs(drawState, op); |
| 418 | this->drawClipShape(gpu, accum, clipIn, c); |
| 419 | |
| 420 | } else if (SkRegion::kReverseDifference_Op == op || |
| 421 | SkRegion::kIntersect_Op == op) { |
| 422 | // there is no point in intersecting a screen filling rectangle. |
| 423 | if (SkRegion::kIntersect_Op == op && |
| 424 | kRect_ClipType == clipIn.getElementType(c) && |
| 425 | clipIn.getRect(c).contains(bounds)) { |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | // clear the temp target & draw into it |
| 430 | clear(gpu, temp, newRTBounds, 0x00000000); |
| 431 | |
| 432 | setUpBooleanBlendCoeffs(drawState, SkRegion::kReplace_Op); |
| 433 | this->drawClipShape(gpu, temp, clipIn, c); |
| 434 | |
| 435 | // TODO: rather than adding these two translations here |
| 436 | // compute the bounding box needed to render the texture |
| 437 | // into temp |
| 438 | if (0 != bounds.fTop || 0 != bounds.fLeft) { |
| 439 | GrMatrix m; |
| 440 | |
| 441 | m.setTranslate(bounds.fLeft, bounds.fTop); |
| 442 | |
| 443 | drawState->preConcatViewMatrix(m); |
| 444 | } |
| 445 | |
| 446 | // Now draw into the accumulator using the real operation |
| 447 | // and the temp buffer as a texture |
| 448 | setUpBooleanBlendCoeffs(drawState, op); |
| 449 | this->drawTexture(gpu, accum, newRTBounds, temp); |
| 450 | |
| 451 | if (0 != bounds.fTop || 0 != bounds.fLeft) { |
| 452 | GrMatrix m; |
| 453 | |
| 454 | m.setTranslate(-bounds.fLeft, -bounds.fTop); |
| 455 | |
| 456 | drawState->preConcatViewMatrix(m); |
| 457 | } |
| 458 | |
| 459 | } else { |
| 460 | // all the remaining ops can just be directly draw into |
| 461 | // the accumulation buffer |
| 462 | setUpBooleanBlendCoeffs(drawState, op); |
| 463 | this->drawClipShape(gpu, accum, clipIn, c); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 471 | // Create a 1-bit clip mask in the stencil buffer |
| 472 | bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu, |
| 473 | const GrClip& clipIn, |
| 474 | const GrRect& bounds, |
| 475 | ScissoringSettings* scissorSettings) { |
| 476 | |
| 477 | GrAssert(fClipMaskInStencil); |
| 478 | |
| 479 | GrDrawState* drawState = gpu->drawState(); |
| 480 | GrAssert(drawState->isClipState()); |
| 481 | |
| 482 | GrRenderTarget* rt = drawState->getRenderTarget(); |
| 483 | GrAssert(NULL != rt); |
| 484 | |
| 485 | // TODO: dynamically attach a SB when needed. |
| 486 | GrStencilBuffer* stencilBuffer = rt->getStencilBuffer(); |
| 487 | if (NULL == stencilBuffer) { |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) { |
| 492 | |
| 493 | stencilBuffer->setLastClip(clipIn, rt->width(), rt->height()); |
| 494 | |
| 495 | // we set the current clip to the bounds so that our recursive |
| 496 | // draws are scissored to them. We use the copy of the complex clip |
| 497 | // we just stashed on the SB to render from. We set it back after |
| 498 | // we finish drawing it into the stencil. |
| 499 | const GrClip& clipCopy = stencilBuffer->getLastClip(); |
| 500 | gpu->setClip(GrClip(bounds)); |
| 501 | |
| 502 | GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit); |
| 503 | drawState = gpu->drawState(); |
| 504 | drawState->setRenderTarget(rt); |
| 505 | GrDrawTarget::AutoGeometryPush agp(gpu); |
| 506 | |
| 507 | gpu->disableScissor(); |
| 508 | #if !VISUALIZE_COMPLEX_CLIP |
| 509 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
| 510 | #endif |
| 511 | |
| 512 | int count = clipCopy.getElementCount(); |
| 513 | int clipBit = stencilBuffer->bits(); |
| 514 | SkASSERT((clipBit <= 16) && |
| 515 | "Ganesh only handles 16b or smaller stencil buffers"); |
| 516 | clipBit = (1 << (clipBit-1)); |
| 517 | |
| 518 | GrRect rtRect; |
| 519 | rtRect.setLTRB(0, 0, |
| 520 | GrIntToScalar(rt->width()), GrIntToScalar(rt->height())); |
| 521 | |
| 522 | bool clearToInside; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 523 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 524 | int start = process_initial_clip_elements(clipCopy, |
| 525 | rtRect, |
| 526 | &clearToInside, |
| 527 | &startOp); |
| 528 | |
| 529 | gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside); |
| 530 | |
| 531 | // walk through each clip element and perform its set op |
| 532 | // with the existing clip. |
| 533 | for (int c = start; c < count; ++c) { |
| 534 | GrPathFill fill; |
| 535 | bool fillInverted; |
| 536 | // enabled at bottom of loop |
| 537 | drawState->disableState(GrGpu::kModifyStencilClip_StateBit); |
| 538 | |
| 539 | bool canRenderDirectToStencil; // can the clip element be drawn |
| 540 | // directly to the stencil buffer |
| 541 | // with a non-inverted fill rule |
| 542 | // without extra passes to |
| 543 | // resolve in/out status. |
| 544 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 545 | SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c); |
| 546 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 547 | GrPathRenderer* pr = NULL; |
| 548 | const GrPath* clipPath = NULL; |
| 549 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 550 | canRenderDirectToStencil = true; |
| 551 | fill = kEvenOdd_PathFill; |
| 552 | fillInverted = false; |
| 553 | // there is no point in intersecting a screen filling |
| 554 | // rectangle. |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 555 | if (SkRegion::kIntersect_Op == op && |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 556 | clipCopy.getRect(c).contains(rtRect)) { |
| 557 | continue; |
| 558 | } |
| 559 | } else { |
| 560 | fill = clipCopy.getPathFill(c); |
| 561 | fillInverted = GrIsFillInverted(fill); |
| 562 | fill = GrNonInvertedFill(fill); |
| 563 | clipPath = &clipCopy.getPath(c); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 564 | pr = this->getClipPathRenderer(gpu, *clipPath, fill, false); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 565 | if (NULL == pr) { |
| 566 | fClipMaskInStencil = false; |
| 567 | gpu->setClip(clipCopy); // restore to the original |
| 568 | return false; |
| 569 | } |
| 570 | canRenderDirectToStencil = |
| 571 | !pr->requiresStencilPass(*clipPath, fill, gpu); |
| 572 | } |
| 573 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 574 | int passes; |
| 575 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 576 | |
| 577 | bool canDrawDirectToClip; // Given the renderer, the element, |
| 578 | // fill rule, and set operation can |
| 579 | // we render the element directly to |
| 580 | // stencil bit used for clipping. |
| 581 | canDrawDirectToClip = |
| 582 | GrStencilSettings::GetClipPasses(op, |
| 583 | canRenderDirectToStencil, |
| 584 | clipBit, |
| 585 | fillInverted, |
| 586 | &passes, stencilSettings); |
| 587 | |
| 588 | // draw the element to the client stencil bits if necessary |
| 589 | if (!canDrawDirectToClip) { |
| 590 | GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil, |
| 591 | kIncClamp_StencilOp, |
| 592 | kIncClamp_StencilOp, |
| 593 | kAlways_StencilFunc, |
| 594 | 0xffff, |
| 595 | 0x0000, |
| 596 | 0xffff); |
| 597 | SET_RANDOM_COLOR |
| 598 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 599 | *drawState->stencil() = gDrawToStencil; |
| 600 | gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0); |
| 601 | } else { |
| 602 | if (canRenderDirectToStencil) { |
| 603 | *drawState->stencil() = gDrawToStencil; |
| 604 | pr->drawPath(*clipPath, fill, NULL, gpu, 0, false); |
| 605 | } else { |
| 606 | pr->drawPathToStencil(*clipPath, fill, gpu); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // now we modify the clip bit by rendering either the clip |
| 612 | // element directly or a bounding rect of the entire clip. |
| 613 | drawState->enableState(GrGpu::kModifyStencilClip_StateBit); |
| 614 | for (int p = 0; p < passes; ++p) { |
| 615 | *drawState->stencil() = stencilSettings[p]; |
| 616 | if (canDrawDirectToClip) { |
| 617 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 618 | SET_RANDOM_COLOR |
| 619 | gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0); |
| 620 | } else { |
| 621 | SET_RANDOM_COLOR |
| 622 | pr->drawPath(*clipPath, fill, NULL, gpu, 0, false); |
| 623 | } |
| 624 | } else { |
| 625 | SET_RANDOM_COLOR |
| 626 | gpu->drawSimpleRect(bounds, NULL, 0); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | // restore clip |
| 631 | gpu->setClip(clipCopy); |
| 632 | // recusive draws would have disabled this since they drew with |
| 633 | // the clip bounds as clip. |
| 634 | fClipMaskInStencil = true; |
| 635 | } |
| 636 | |
| 637 | return true; |
| 638 | } |
| 639 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 640 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 641 | GrPathRenderer* GrClipMaskManager::getClipPathRenderer(GrGpu* gpu, |
| 642 | const GrPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 643 | GrPathFill fill, |
| 644 | bool antiAlias) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 645 | if (NULL == fPathRendererChain) { |
| 646 | fPathRendererChain = |
| 647 | new GrPathRendererChain(gpu->getContext(), |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 648 | GrPathRendererChain::kNone_UsageFlag); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 649 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 650 | return fPathRendererChain->getPathRenderer(path, fill, gpu, antiAlias); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 651 | } |
| 652 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame^] | 653 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 654 | void GrClipMaskManager::freeResources() { |
| 655 | // in case path renderer has any GrResources, start from scratch |
| 656 | GrSafeSetNull(fPathRendererChain); |
| 657 | } |