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" |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 14 | #include "GrPaint.h" |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 15 | #include "SkRasterClip.h" |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 16 | |
| 17 | //#define GR_AA_CLIP 1 |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 18 | //#define GR_SW_CLIP 1 |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 20 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 21 | void ScissoringSettings::setupScissoring(GrGpu* gpu) { |
| 22 | if (!fEnableScissoring) { |
| 23 | gpu->disableScissor(); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | gpu->enableScissoring(fScissorRect); |
| 28 | } |
| 29 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | // set up the draw state to enable the aa clipping mask. Besides setting up the |
| 32 | // sampler matrix this also alters the vertex layout |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 33 | void setup_drawstate_aaclip(GrGpu* gpu, |
| 34 | GrTexture* result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 35 | const GrIRect &bound) { |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 36 | GrDrawState* drawState = gpu->drawState(); |
| 37 | GrAssert(drawState); |
| 38 | |
| 39 | static const int maskStage = GrPaint::kTotalStages+1; |
| 40 | |
| 41 | GrMatrix mat; |
| 42 | mat.setIDiv(result->width(), result->height()); |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 43 | mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop)); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 44 | mat.preConcat(drawState->getViewMatrix()); |
| 45 | |
| 46 | drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode, |
| 47 | GrSamplerState::kNearest_Filter, |
| 48 | mat); |
| 49 | |
| 50 | drawState->setTexture(maskStage, result); |
| 51 | |
| 52 | // The AA clipping determination happens long after the geometry has |
| 53 | // been set up to draw. Here we directly enable the AA clip mask stage |
| 54 | gpu->addToVertexLayout( |
| 55 | GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(maskStage)); |
| 56 | } |
| 57 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 58 | bool create_mask_in_sw() { |
| 59 | return false; |
| 60 | } |
| 61 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 62 | } |
| 63 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 64 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 65 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 66 | // scissor, or entirely software |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 67 | bool GrClipMaskManager::createClipMask(GrGpu* gpu, |
| 68 | const GrClip& clipIn, |
| 69 | ScissoringSettings* scissorSettings) { |
| 70 | |
| 71 | GrAssert(scissorSettings); |
| 72 | |
| 73 | scissorSettings->fEnableScissoring = false; |
| 74 | fClipMaskInStencil = false; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 75 | fClipMaskInAlpha = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 76 | |
| 77 | GrDrawState* drawState = gpu->drawState(); |
| 78 | if (!drawState->isClipState()) { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | GrRenderTarget* rt = drawState->getRenderTarget(); |
| 83 | |
| 84 | // GrDrawTarget should have filtered this for us |
| 85 | GrAssert(NULL != rt); |
| 86 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 87 | #if GR_SW_CLIP |
| 88 | if (create_mask_in_sw()) { |
| 89 | // The clip geometry is complex enough that it will be more |
| 90 | // efficient to create it entirely in software |
| 91 | GrTexture* result = NULL; |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 92 | GrIRect bound; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 93 | if (this->createSoftwareClipMask(gpu, clipIn, &result, &bound)) { |
| 94 | fClipMaskInAlpha = true; |
| 95 | |
| 96 | setup_drawstate_aaclip(gpu, result, bound); |
| 97 | return true; |
| 98 | } |
| 99 | } |
| 100 | #endif |
| 101 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 102 | #if GR_AA_CLIP |
| 103 | // If MSAA is enabled use the (faster) stencil path for AA clipping |
| 104 | // otherwise the alpha clip mask is our only option |
| 105 | if (clipIn.requiresAA() && 0 == rt->numSamples()) { |
| 106 | // Since we are going to create a destination texture of the correct |
| 107 | // size for the mask (rather than being bound by the size of the |
| 108 | // render target) we aren't going to use scissoring like the stencil |
| 109 | // path does (see scissorSettings below) |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 110 | GrTexture* result = NULL; |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 111 | GrIRect bound; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 112 | if (this->createAlphaClipMask(gpu, clipIn, &result, &bound)) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 113 | fClipMaskInAlpha = true; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 114 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 115 | setup_drawstate_aaclip(gpu, result, bound); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
| 119 | // if alpha clip mask creation fails fall through to the stencil |
| 120 | // buffer method |
| 121 | } |
| 122 | #endif // GR_AA_CLIP |
| 123 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 124 | GrRect bounds; |
| 125 | GrRect rtRect; |
| 126 | rtRect.setLTRB(0, 0, |
| 127 | GrIntToScalar(rt->width()), GrIntToScalar(rt->height())); |
| 128 | if (clipIn.hasConservativeBounds()) { |
| 129 | bounds = clipIn.getConservativeBounds(); |
| 130 | if (!bounds.intersect(rtRect)) { |
| 131 | bounds.setEmpty(); |
| 132 | } |
| 133 | } else { |
| 134 | bounds = rtRect; |
| 135 | } |
| 136 | |
| 137 | bounds.roundOut(&scissorSettings->fScissorRect); |
| 138 | if (scissorSettings->fScissorRect.isEmpty()) { |
| 139 | scissorSettings->fScissorRect.setLTRB(0,0,0,0); |
| 140 | // TODO: I think we can do an early exit here - after refactoring try: |
| 141 | // set fEnableScissoring to true but leave fClipMaskInStencil false |
| 142 | // and return - everything is going to be scissored away anyway! |
| 143 | } |
| 144 | scissorSettings->fEnableScissoring = true; |
| 145 | |
| 146 | // use the stencil clip if we can't represent the clip as a rectangle. |
| 147 | fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() && |
| 148 | !bounds.isEmpty(); |
| 149 | |
| 150 | if (fClipMaskInStencil) { |
| 151 | return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings); |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | #define VISUALIZE_COMPLEX_CLIP 0 |
| 158 | |
| 159 | #if VISUALIZE_COMPLEX_CLIP |
| 160 | #include "GrRandom.h" |
| 161 | GrRandom gRandom; |
| 162 | #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU()); |
| 163 | #else |
| 164 | #define SET_RANDOM_COLOR |
| 165 | #endif |
| 166 | |
| 167 | namespace { |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 168 | /** |
| 169 | * Does "container" contain "containee"? If either is empty then |
| 170 | * no containment is possible. |
| 171 | */ |
| 172 | bool contains(const SkRect& container, const SkIRect& containee) { |
| 173 | return !containee.isEmpty() && !container.isEmpty() && |
| 174 | container.fLeft <= SkIntToScalar(containee.fLeft) && |
| 175 | container.fTop <= SkIntToScalar(containee.fTop) && |
| 176 | container.fRight >= SkIntToScalar(containee.fRight) && |
| 177 | container.fBottom >= SkIntToScalar(containee.fBottom); |
| 178 | } |
| 179 | |
| 180 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 181 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 182 | // determines how many elements at the head of the clip can be skipped and |
| 183 | // whether the initial clear should be to the inside- or outside-the-clip value, |
| 184 | // and what op should be used to draw the first element that isn't skipped. |
| 185 | int process_initial_clip_elements(const GrClip& clip, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 186 | const GrIRect& bounds, |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 187 | bool* clearToInside, |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 188 | SkRegion::Op* startOp) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 189 | |
| 190 | // logically before the first element of the clip stack is |
| 191 | // processed the clip is entirely open. However, depending on the |
| 192 | // first set op we may prefer to clear to 0 for performance. We may |
| 193 | // also be able to skip the initial clip paths/rects. We loop until |
| 194 | // we cannot skip an element. |
| 195 | int curr; |
| 196 | bool done = false; |
| 197 | *clearToInside = true; |
| 198 | int count = clip.getElementCount(); |
| 199 | |
| 200 | for (curr = 0; curr < count && !done; ++curr) { |
| 201 | switch (clip.getOp(curr)) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 202 | case SkRegion::kReplace_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 203 | // replace ignores everything previous |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 204 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 205 | *clearToInside = false; |
| 206 | done = true; |
| 207 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 208 | case SkRegion::kIntersect_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 209 | // if this element contains the entire bounds then we |
| 210 | // can skip it. |
| 211 | if (kRect_ClipType == clip.getElementType(curr) |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 212 | && contains(clip.getRect(curr), bounds)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | // if everything is initially clearToInside then intersect is |
| 216 | // same as clear to 0 and treat as a replace. Otherwise, |
| 217 | // set stays empty. |
| 218 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 219 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 220 | *clearToInside = false; |
| 221 | done = true; |
| 222 | } |
| 223 | break; |
| 224 | // we can skip a leading union. |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 225 | case SkRegion::kUnion_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 226 | // if everything is initially outside then union is |
| 227 | // same as replace. Otherwise, every pixel is still |
| 228 | // clearToInside |
| 229 | if (!*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 230 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 231 | done = true; |
| 232 | } |
| 233 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 234 | case SkRegion::kXOR_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 235 | // xor is same as difference or replace both of which |
| 236 | // can be 1-pass instead of 2 for xor. |
| 237 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 238 | *startOp = SkRegion::kDifference_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 239 | } else { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 240 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 241 | } |
| 242 | done = true; |
| 243 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 244 | case SkRegion::kDifference_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 245 | // if all pixels are clearToInside then we have to process the |
| 246 | // difference, otherwise it has no effect and all pixels |
| 247 | // remain outside. |
| 248 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 249 | *startOp = SkRegion::kDifference_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 250 | done = true; |
| 251 | } |
| 252 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 253 | case SkRegion::kReverseDifference_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 254 | // if all pixels are clearToInside then reverse difference |
| 255 | // produces empty set. Otherise it is same as replace |
| 256 | if (*clearToInside) { |
| 257 | *clearToInside = false; |
| 258 | } else { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 259 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 260 | done = true; |
| 261 | } |
| 262 | break; |
| 263 | default: |
| 264 | GrCrash("Unknown set op."); |
| 265 | } |
| 266 | } |
| 267 | return done ? curr-1 : count; |
| 268 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 269 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 270 | } |
| 271 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 272 | |
| 273 | namespace { |
| 274 | |
| 275 | //////////////////////////////////////////////////////////////////////////////// |
| 276 | // set up the OpenGL blend function to perform the specified |
| 277 | // boolean operation for alpha clip mask creation |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 278 | void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 279 | |
| 280 | switch (op) { |
| 281 | case SkRegion::kReplace_Op: |
| 282 | drawState->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff); |
| 283 | break; |
| 284 | case SkRegion::kIntersect_Op: |
| 285 | drawState->setBlendFunc(kDC_BlendCoeff, kZero_BlendCoeff); |
| 286 | break; |
| 287 | case SkRegion::kUnion_Op: |
| 288 | drawState->setBlendFunc(kOne_BlendCoeff, kISC_BlendCoeff); |
| 289 | break; |
| 290 | case SkRegion::kXOR_Op: |
| 291 | drawState->setBlendFunc(kIDC_BlendCoeff, kISC_BlendCoeff); |
| 292 | break; |
| 293 | case SkRegion::kDifference_Op: |
| 294 | drawState->setBlendFunc(kZero_BlendCoeff, kISC_BlendCoeff); |
| 295 | break; |
| 296 | case SkRegion::kReverseDifference_Op: |
| 297 | drawState->setBlendFunc(kIDC_BlendCoeff, kZero_BlendCoeff); |
| 298 | break; |
| 299 | default: |
| 300 | GrAssert(false); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | } |
| 306 | |
| 307 | //////////////////////////////////////////////////////////////////////////////// |
| 308 | bool GrClipMaskManager::drawPath(GrGpu* gpu, |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 309 | const SkPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 310 | GrPathFill fill, |
| 311 | bool doAA) { |
| 312 | |
| 313 | GrPathRenderer* pr = this->getClipPathRenderer(gpu, path, fill, doAA); |
| 314 | if (NULL == pr) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | pr->drawPath(path, fill, NULL, gpu, 0, doAA); |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | //////////////////////////////////////////////////////////////////////////////// |
| 323 | bool GrClipMaskManager::drawClipShape(GrGpu* gpu, |
| 324 | GrTexture* target, |
| 325 | const GrClip& clipIn, |
| 326 | int index) { |
| 327 | GrDrawState* drawState = gpu->drawState(); |
| 328 | GrAssert(NULL != drawState); |
| 329 | |
| 330 | drawState->setRenderTarget(target->asRenderTarget()); |
| 331 | |
| 332 | if (kRect_ClipType == clipIn.getElementType(index)) { |
| 333 | if (clipIn.getDoAA(index)) { |
| 334 | // convert the rect to a path for AA |
| 335 | SkPath temp; |
| 336 | temp.addRect(clipIn.getRect(index)); |
| 337 | |
| 338 | return this->drawPath(gpu, temp, |
| 339 | kEvenOdd_PathFill, clipIn.getDoAA(index)); |
| 340 | } else { |
| 341 | gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0); |
| 342 | } |
| 343 | } else { |
| 344 | return this->drawPath(gpu, |
| 345 | clipIn.getPath(index), |
| 346 | clipIn.getPathFill(index), |
| 347 | clipIn.getDoAA(index)); |
| 348 | } |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | void GrClipMaskManager::drawTexture(GrGpu* gpu, |
| 353 | GrTexture* target, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 354 | GrTexture* texture) { |
| 355 | GrDrawState* drawState = gpu->drawState(); |
| 356 | GrAssert(NULL != drawState); |
| 357 | |
| 358 | // no AA here since it is encoded in the texture |
| 359 | drawState->setRenderTarget(target->asRenderTarget()); |
| 360 | |
| 361 | GrMatrix sampleM; |
| 362 | sampleM.setIDiv(texture->width(), texture->height()); |
| 363 | drawState->setTexture(0, texture); |
| 364 | |
| 365 | drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode, |
| 366 | GrSamplerState::kNearest_Filter, |
| 367 | sampleM); |
| 368 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 369 | GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()), |
| 370 | SkIntToScalar(target->height())); |
| 371 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 372 | gpu->drawSimpleRect(rect, NULL, 1 << 0); |
| 373 | |
| 374 | drawState->setTexture(0, NULL); |
| 375 | } |
| 376 | |
| 377 | namespace { |
| 378 | |
| 379 | void clear(GrGpu* gpu, |
| 380 | GrTexture* target, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 381 | GrColor color) { |
| 382 | GrDrawState* drawState = gpu->drawState(); |
| 383 | GrAssert(NULL != drawState); |
| 384 | |
| 385 | // zap entire target to specified color |
| 386 | drawState->setRenderTarget(target->asRenderTarget()); |
| 387 | gpu->clear(NULL, color); |
| 388 | } |
| 389 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 390 | } |
| 391 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 392 | // get a texture to act as a temporary buffer for AA clip boolean operations |
| 393 | // TODO: given the expense of createTexture we may want to just cache this too |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 394 | void GrClipMaskManager::getTemp(const GrIRect& bounds, |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 395 | GrAutoScratchTexture* temp) { |
| 396 | if (NULL != temp->texture()) { |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 397 | // we've already allocated the temp texture |
| 398 | return; |
| 399 | } |
| 400 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 401 | const GrTextureDesc desc = { |
| 402 | kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 403 | bounds.width(), |
| 404 | bounds.height(), |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 405 | kAlpha_8_GrPixelConfig, |
| 406 | 0 // samples |
| 407 | }; |
| 408 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 409 | temp->set(fAACache.getContext(), desc); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 410 | } |
| 411 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 412 | |
| 413 | void GrClipMaskManager::setupCache(const GrClip& clipIn, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 414 | const GrIRect& bounds) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 415 | // Since we are setting up the cache we know the last lookup was a miss |
| 416 | // Free up the currently cached mask so it can be reused |
| 417 | fAACache.reset(); |
| 418 | |
| 419 | const GrTextureDesc desc = { |
| 420 | kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 421 | bounds.width(), |
| 422 | bounds.height(), |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 423 | kAlpha_8_GrPixelConfig, |
| 424 | 0 // samples |
| 425 | }; |
| 426 | |
| 427 | fAACache.acquireMask(clipIn, desc, bounds); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 428 | } |
| 429 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 430 | //////////////////////////////////////////////////////////////////////////////// |
| 431 | // Shared preamble between gpu and SW-only AA clip mask creation paths. |
| 432 | // Handles caching, determination of clip mask bound & allocation (if needed) |
| 433 | // of the result texture |
| 434 | // Returns true if there is no more work to be done (i.e., we got a cache hit) |
| 435 | bool GrClipMaskManager::clipMaskPreamble(GrGpu* gpu, |
| 436 | const GrClip& clipIn, |
| 437 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 438 | GrIRect *resultBounds) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 439 | GrDrawState* origDrawState = gpu->drawState(); |
| 440 | GrAssert(origDrawState->isClipState()); |
| 441 | |
| 442 | GrRenderTarget* rt = origDrawState->getRenderTarget(); |
| 443 | GrAssert(NULL != rt); |
| 444 | |
| 445 | GrRect rtRect; |
| 446 | rtRect.setLTRB(0, 0, |
| 447 | GrIntToScalar(rt->width()), GrIntToScalar(rt->height())); |
| 448 | |
| 449 | // unlike the stencil path the alpha path is not bound to the size of the |
| 450 | // render target - determine the minimum size required for the mask |
| 451 | GrRect bounds; |
| 452 | |
| 453 | if (clipIn.hasConservativeBounds()) { |
| 454 | bounds = clipIn.getConservativeBounds(); |
| 455 | if (!bounds.intersect(rtRect)) { |
| 456 | // the mask will be empty in this case |
| 457 | GrAssert(false); |
| 458 | bounds.setEmpty(); |
| 459 | } |
| 460 | } else { |
| 461 | // still locked to the size of the render target |
| 462 | bounds = rtRect; |
| 463 | } |
| 464 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 465 | GrIRect intBounds; |
| 466 | bounds.roundOut(&intBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 467 | |
| 468 | // need to outset a pixel since the standard bounding box computation |
| 469 | // path doesn't leave any room for antialiasing (esp. w.r.t. rects) |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 470 | intBounds.outset(1, 1); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 471 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 472 | // TODO: make sure we don't outset if bounds are still 0,0 @ min |
| 473 | |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 474 | if (fAACache.canReuse(clipIn, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 475 | intBounds.width(), |
| 476 | intBounds.height())) { |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 477 | *result = fAACache.getLastMask(); |
| 478 | fAACache.getLastBound(resultBounds); |
| 479 | return true; |
| 480 | } |
| 481 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 482 | this->setupCache(clipIn, intBounds); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 483 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 484 | *resultBounds = intBounds; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 485 | return false; |
| 486 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 487 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 488 | //////////////////////////////////////////////////////////////////////////////// |
| 489 | // Create a 8-bit clip mask in alpha |
| 490 | bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu, |
| 491 | const GrClip& clipIn, |
| 492 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 493 | GrIRect *resultBounds) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 494 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 495 | if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 496 | return true; |
| 497 | } |
| 498 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 499 | GrTexture* accum = fAACache.getLastMask(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 500 | if (NULL == accum) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 501 | fClipMaskInAlpha = false; |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 502 | fAACache.reset(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | |
| 506 | GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit); |
| 507 | GrDrawState* drawState = gpu->drawState(); |
| 508 | |
| 509 | GrDrawTarget::AutoGeometryPush agp(gpu); |
| 510 | |
| 511 | int count = clipIn.getElementCount(); |
| 512 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 513 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 514 | // if we were able to trim down the size of the mask we need to |
| 515 | // offset the paths & rects that will be used to compute it |
| 516 | GrMatrix m; |
| 517 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 518 | m.setTranslate(SkIntToScalar(-resultBounds->fLeft), |
| 519 | SkIntToScalar(-resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 520 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 521 | drawState->setViewMatrix(m); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | bool clearToInside; |
| 525 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
| 526 | int start = process_initial_clip_elements(clipIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 527 | *resultBounds, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 528 | &clearToInside, |
| 529 | &startOp); |
| 530 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 531 | clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 532 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 533 | GrAutoScratchTexture temp; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 534 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 535 | // walk through each clip element and perform its set op |
| 536 | for (int c = start; c < count; ++c) { |
| 537 | |
| 538 | SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c); |
| 539 | |
| 540 | if (SkRegion::kReplace_Op == op) { |
| 541 | // TODO: replace is actually a lot faster then intersection |
| 542 | // for this path - refactor the stencil path so it can handle |
| 543 | // replace ops and alter GrClip to allow them through |
| 544 | |
| 545 | // clear the accumulator and draw the new object directly into it |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 546 | clear(gpu, accum, 0x00000000); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 547 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 548 | setup_boolean_blendcoeffs(drawState, op); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 549 | this->drawClipShape(gpu, accum, clipIn, c); |
| 550 | |
| 551 | } else if (SkRegion::kReverseDifference_Op == op || |
| 552 | SkRegion::kIntersect_Op == op) { |
| 553 | // there is no point in intersecting a screen filling rectangle. |
| 554 | if (SkRegion::kIntersect_Op == op && |
| 555 | kRect_ClipType == clipIn.getElementType(c) && |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 556 | contains(clipIn.getRect(c), *resultBounds)) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 557 | continue; |
| 558 | } |
| 559 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 560 | getTemp(*resultBounds, &temp); |
| 561 | if (NULL == temp.texture()) { |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 562 | fClipMaskInAlpha = false; |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 563 | fAACache.reset(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 564 | return false; |
| 565 | } |
| 566 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 567 | // clear the temp target & draw into it |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 568 | clear(gpu, temp.texture(), 0x00000000); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 569 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 570 | setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 571 | this->drawClipShape(gpu, temp.texture(), clipIn, c); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 572 | |
| 573 | // TODO: rather than adding these two translations here |
| 574 | // compute the bounding box needed to render the texture |
| 575 | // into temp |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 576 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 577 | GrMatrix m; |
| 578 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 579 | m.setTranslate(SkIntToScalar(resultBounds->fLeft), |
| 580 | SkIntToScalar(resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 581 | |
| 582 | drawState->preConcatViewMatrix(m); |
| 583 | } |
| 584 | |
| 585 | // Now draw into the accumulator using the real operation |
| 586 | // and the temp buffer as a texture |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 587 | setup_boolean_blendcoeffs(drawState, op); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 588 | this->drawTexture(gpu, accum, temp.texture()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 589 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 590 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 591 | GrMatrix m; |
| 592 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 593 | m.setTranslate(SkIntToScalar(-resultBounds->fLeft), |
| 594 | SkIntToScalar(-resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 595 | |
| 596 | drawState->preConcatViewMatrix(m); |
| 597 | } |
| 598 | |
| 599 | } else { |
| 600 | // all the remaining ops can just be directly draw into |
| 601 | // the accumulation buffer |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 602 | setup_boolean_blendcoeffs(drawState, op); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 603 | this->drawClipShape(gpu, accum, clipIn, c); |
| 604 | } |
| 605 | } |
| 606 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 607 | *result = accum; |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 608 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 609 | return true; |
| 610 | } |
| 611 | |
| 612 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 613 | // Create a 1-bit clip mask in the stencil buffer |
| 614 | bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu, |
| 615 | const GrClip& clipIn, |
| 616 | const GrRect& bounds, |
| 617 | ScissoringSettings* scissorSettings) { |
| 618 | |
| 619 | GrAssert(fClipMaskInStencil); |
| 620 | |
| 621 | GrDrawState* drawState = gpu->drawState(); |
| 622 | GrAssert(drawState->isClipState()); |
| 623 | |
| 624 | GrRenderTarget* rt = drawState->getRenderTarget(); |
| 625 | GrAssert(NULL != rt); |
| 626 | |
| 627 | // TODO: dynamically attach a SB when needed. |
| 628 | GrStencilBuffer* stencilBuffer = rt->getStencilBuffer(); |
| 629 | if (NULL == stencilBuffer) { |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) { |
| 634 | |
| 635 | stencilBuffer->setLastClip(clipIn, rt->width(), rt->height()); |
| 636 | |
| 637 | // we set the current clip to the bounds so that our recursive |
| 638 | // draws are scissored to them. We use the copy of the complex clip |
| 639 | // we just stashed on the SB to render from. We set it back after |
| 640 | // we finish drawing it into the stencil. |
| 641 | const GrClip& clipCopy = stencilBuffer->getLastClip(); |
| 642 | gpu->setClip(GrClip(bounds)); |
| 643 | |
| 644 | GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit); |
| 645 | drawState = gpu->drawState(); |
| 646 | drawState->setRenderTarget(rt); |
| 647 | GrDrawTarget::AutoGeometryPush agp(gpu); |
| 648 | |
| 649 | gpu->disableScissor(); |
| 650 | #if !VISUALIZE_COMPLEX_CLIP |
| 651 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
| 652 | #endif |
| 653 | |
| 654 | int count = clipCopy.getElementCount(); |
| 655 | int clipBit = stencilBuffer->bits(); |
| 656 | SkASSERT((clipBit <= 16) && |
| 657 | "Ganesh only handles 16b or smaller stencil buffers"); |
| 658 | clipBit = (1 << (clipBit-1)); |
| 659 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 660 | GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 661 | |
| 662 | bool clearToInside; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 663 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 664 | int start = process_initial_clip_elements(clipCopy, |
| 665 | rtRect, |
| 666 | &clearToInside, |
| 667 | &startOp); |
| 668 | |
| 669 | gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside); |
| 670 | |
| 671 | // walk through each clip element and perform its set op |
| 672 | // with the existing clip. |
| 673 | for (int c = start; c < count; ++c) { |
| 674 | GrPathFill fill; |
| 675 | bool fillInverted; |
| 676 | // enabled at bottom of loop |
| 677 | drawState->disableState(GrGpu::kModifyStencilClip_StateBit); |
| 678 | |
| 679 | bool canRenderDirectToStencil; // can the clip element be drawn |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 680 | // directly to the stencil buffer |
| 681 | // with a non-inverted fill rule |
| 682 | // without extra passes to |
| 683 | // resolve in/out status. |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 684 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 685 | SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c); |
| 686 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 687 | GrPathRenderer* pr = NULL; |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 688 | const SkPath* clipPath = NULL; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 689 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 690 | canRenderDirectToStencil = true; |
| 691 | fill = kEvenOdd_PathFill; |
| 692 | fillInverted = false; |
| 693 | // there is no point in intersecting a screen filling |
| 694 | // rectangle. |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 695 | if (SkRegion::kIntersect_Op == op && |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 696 | contains(clipCopy.getRect(c), rtRect)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 697 | continue; |
| 698 | } |
| 699 | } else { |
| 700 | fill = clipCopy.getPathFill(c); |
| 701 | fillInverted = GrIsFillInverted(fill); |
| 702 | fill = GrNonInvertedFill(fill); |
| 703 | clipPath = &clipCopy.getPath(c); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 704 | pr = this->getClipPathRenderer(gpu, *clipPath, fill, false); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 705 | if (NULL == pr) { |
| 706 | fClipMaskInStencil = false; |
| 707 | gpu->setClip(clipCopy); // restore to the original |
| 708 | return false; |
| 709 | } |
| 710 | canRenderDirectToStencil = |
| 711 | !pr->requiresStencilPass(*clipPath, fill, gpu); |
| 712 | } |
| 713 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 714 | int passes; |
| 715 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 716 | |
| 717 | bool canDrawDirectToClip; // Given the renderer, the element, |
| 718 | // fill rule, and set operation can |
| 719 | // we render the element directly to |
| 720 | // stencil bit used for clipping. |
| 721 | canDrawDirectToClip = |
| 722 | GrStencilSettings::GetClipPasses(op, |
| 723 | canRenderDirectToStencil, |
| 724 | clipBit, |
| 725 | fillInverted, |
| 726 | &passes, stencilSettings); |
| 727 | |
| 728 | // draw the element to the client stencil bits if necessary |
| 729 | if (!canDrawDirectToClip) { |
| 730 | GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil, |
| 731 | kIncClamp_StencilOp, |
| 732 | kIncClamp_StencilOp, |
| 733 | kAlways_StencilFunc, |
| 734 | 0xffff, |
| 735 | 0x0000, |
| 736 | 0xffff); |
| 737 | SET_RANDOM_COLOR |
| 738 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 739 | *drawState->stencil() = gDrawToStencil; |
| 740 | gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0); |
| 741 | } else { |
| 742 | if (canRenderDirectToStencil) { |
| 743 | *drawState->stencil() = gDrawToStencil; |
| 744 | pr->drawPath(*clipPath, fill, NULL, gpu, 0, false); |
| 745 | } else { |
| 746 | pr->drawPathToStencil(*clipPath, fill, gpu); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | // now we modify the clip bit by rendering either the clip |
| 752 | // element directly or a bounding rect of the entire clip. |
| 753 | drawState->enableState(GrGpu::kModifyStencilClip_StateBit); |
| 754 | for (int p = 0; p < passes; ++p) { |
| 755 | *drawState->stencil() = stencilSettings[p]; |
| 756 | if (canDrawDirectToClip) { |
| 757 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 758 | SET_RANDOM_COLOR |
| 759 | gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0); |
| 760 | } else { |
| 761 | SET_RANDOM_COLOR |
| 762 | pr->drawPath(*clipPath, fill, NULL, gpu, 0, false); |
| 763 | } |
| 764 | } else { |
| 765 | SET_RANDOM_COLOR |
| 766 | gpu->drawSimpleRect(bounds, NULL, 0); |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | // restore clip |
| 771 | gpu->setClip(clipCopy); |
| 772 | // recusive draws would have disabled this since they drew with |
| 773 | // the clip bounds as clip. |
| 774 | fClipMaskInStencil = true; |
| 775 | } |
| 776 | |
| 777 | return true; |
| 778 | } |
| 779 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 780 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 781 | bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu, |
| 782 | const GrClip& clipIn, |
| 783 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame^] | 784 | GrIRect *resultBounds) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 785 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 786 | if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 787 | return true; |
| 788 | } |
| 789 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 790 | GrTexture* accum = fAACache.getLastMask(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 791 | if (NULL == accum) { |
| 792 | fClipMaskInAlpha = false; |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 793 | fAACache.reset(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 794 | return false; |
| 795 | } |
| 796 | |
| 797 | #if 0 |
| 798 | SkRasterClip rasterClip; |
| 799 | |
| 800 | // TODO: refactor GrClip out of existance and use SkCanvas's ClipVisitor |
| 801 | // - may have to move it to SkClipStack |
| 802 | for (int i = 0; i < clipIn.getElementCount(); ++i) { |
| 803 | if (kRect_ClipType == clipIn.getElementType(i)) { |
| 804 | rasterClip.op(clipIn.getRect(i), clipIn.getOp(i), clipIn.getDoAA(i)); |
| 805 | } else { |
| 806 | GrAssert(kPath_ClipType == clipIn.getElementType(i)); |
| 807 | |
| 808 | SkIPoint deviceSize = SkIPoint::Make(resultBounds->width(), |
| 809 | resultBounds->height()); |
| 810 | |
| 811 | SkRasterClip::clipPathHelper(&rasterClip, |
| 812 | clipIn.getPath(i), |
| 813 | clipIn.getOp(i), |
| 814 | clipIn.getDoAA(i), |
| 815 | deviceSize); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | // TODO: need to get pixels out of SkRasterClip & into the texture! |
| 820 | #endif |
| 821 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 822 | *result = accum; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 823 | |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | |
| 828 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 829 | GrPathRenderer* GrClipMaskManager::getClipPathRenderer(GrGpu* gpu, |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 830 | const SkPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 831 | GrPathFill fill, |
| 832 | bool antiAlias) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 833 | if (NULL == fPathRendererChain) { |
| 834 | fPathRendererChain = |
| 835 | new GrPathRendererChain(gpu->getContext(), |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 836 | GrPathRendererChain::kNone_UsageFlag); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 837 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 838 | return fPathRendererChain->getPathRenderer(path, fill, gpu, antiAlias); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 839 | } |
| 840 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 841 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 842 | void GrClipMaskManager::releaseResources() { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 843 | // in case path renderer has any GrResources, start from scratch |
| 844 | GrSafeSetNull(fPathRendererChain); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 845 | fAACache.releaseResources(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 846 | } |