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