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 | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | // set up the draw state to enable the aa clipping mask. Besides setting up the |
| 28 | // sampler matrix this also alters the vertex layout |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 29 | void setup_drawstate_aaclip(GrGpu* gpu, |
| 30 | GrTexture* result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 31 | const GrIRect &bound) { |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 32 | GrDrawState* drawState = gpu->drawState(); |
| 33 | GrAssert(drawState); |
| 34 | |
| 35 | static const int maskStage = GrPaint::kTotalStages+1; |
| 36 | |
| 37 | GrMatrix mat; |
| 38 | mat.setIDiv(result->width(), result->height()); |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 39 | mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop)); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 40 | mat.preConcat(drawState->getViewMatrix()); |
| 41 | |
| 42 | drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode, |
| 43 | GrSamplerState::kNearest_Filter, |
| 44 | mat); |
| 45 | |
| 46 | drawState->setTexture(maskStage, result); |
| 47 | |
| 48 | // The AA clipping determination happens long after the geometry has |
| 49 | // been set up to draw. Here we directly enable the AA clip mask stage |
| 50 | gpu->addToVertexLayout( |
| 51 | GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(maskStage)); |
| 52 | } |
| 53 | |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 54 | bool path_needs_SW_renderer(GrContext* context, |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 55 | GrGpu* gpu, |
| 56 | const SkPath& path, |
| 57 | GrPathFill fill, |
| 58 | bool doAA) { |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 59 | // last (false) parameter disallows use of the SW path renderer |
| 60 | return NULL == context->getPathRenderer(path, fill, gpu, doAA, false); |
| 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 | */ |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 70 | bool GrClipMaskManager::useSWOnlyPath(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 | |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 77 | // TODO: generalize this function so that when |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 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 | |
robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 90 | // rects can always be drawn directly w/o using the software path |
| 91 | // so only paths need to be checked |
| 92 | if (kPath_ClipType == clipIn.getElementType(i) && |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 93 | path_needs_SW_renderer(this->getContext(), fGpu, |
robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 94 | clipIn.getPath(i), |
| 95 | clipIn.getPathFill(i), |
| 96 | clipIn.getDoAA(i))) { |
| 97 | useSW = true; |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 98 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return useSW; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 102 | } |
| 103 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 104 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 105 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 106 | // scissor, or entirely software |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 107 | bool GrClipMaskManager::setupClipping(const GrClip& clipIn) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 108 | fCurrClipMaskType = kNone_ClipMaskType; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 109 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 110 | GrDrawState* drawState = fGpu->drawState(); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 111 | if (!drawState->isClipState() || clipIn.isEmpty()) { |
| 112 | fGpu->disableScissor(); |
| 113 | this->setGpuStencil(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
| 117 | GrRenderTarget* rt = drawState->getRenderTarget(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 118 | // GrDrawTarget should have filtered this for us |
| 119 | GrAssert(NULL != rt); |
| 120 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 121 | GrIRect bounds; |
| 122 | GrIRect rtRect; |
| 123 | rtRect.setLTRB(0, 0, rt->width(), rt->height()); |
| 124 | if (clipIn.hasConservativeBounds()) { |
| 125 | GrRect softBounds = clipIn.getConservativeBounds(); |
| 126 | softBounds.roundOut(&bounds); |
| 127 | if (!bounds.intersect(rtRect)) { |
| 128 | bounds.setEmpty(); |
| 129 | } |
| 130 | if (bounds.isEmpty()) { |
| 131 | return false; |
| 132 | } |
| 133 | } else { |
| 134 | bounds = rtRect; |
| 135 | } |
| 136 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 137 | #if GR_SW_CLIP |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 138 | // If MSAA is enabled we can do everything in the stencil buffer. |
| 139 | // Otherwise check if we should just create the entire clip mask |
| 140 | // in software (this will only happen if the clip mask is anti-aliased |
| 141 | // and too complex for the gpu to handle in its entirety) |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 142 | if (0 == rt->numSamples() && this->useSWOnlyPath(clipIn)) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 143 | // The clip geometry is complex enough that it will be more |
| 144 | // efficient to create it entirely in software |
| 145 | GrTexture* result = NULL; |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 146 | GrIRect bound; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 147 | if (this->createSoftwareClipMask(clipIn, &result, &bound)) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 148 | setup_drawstate_aaclip(fGpu, result, bound); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 149 | fGpu->disableScissor(); |
| 150 | this->setGpuStencil(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 151 | return true; |
| 152 | } |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 153 | |
| 154 | // if SW clip mask creation fails fall through to the other |
| 155 | // two possible methods (bottoming out at stencil clipping) |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 156 | } |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 157 | #endif // GR_SW_CLIP |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 158 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 159 | #if GR_AA_CLIP |
| 160 | // If MSAA is enabled use the (faster) stencil path for AA clipping |
| 161 | // otherwise the alpha clip mask is our only option |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 162 | if (0 == rt->numSamples() && clipIn.requiresAA()) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 163 | // Since we are going to create a destination texture of the correct |
| 164 | // size for the mask (rather than being bound by the size of the |
| 165 | // render target) we aren't going to use scissoring like the stencil |
| 166 | // path does (see scissorSettings below) |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 167 | GrTexture* result = NULL; |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 168 | GrIRect bound; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 169 | if (this->createAlphaClipMask(clipIn, &result, &bound)) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 170 | setup_drawstate_aaclip(fGpu, result, bound); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 171 | fGpu->disableScissor(); |
| 172 | this->setGpuStencil(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
| 176 | // if alpha clip mask creation fails fall through to the stencil |
| 177 | // buffer method |
| 178 | } |
| 179 | #endif // GR_AA_CLIP |
| 180 | |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 181 | // Either a hard (stencil buffer) clip was explicitly requested or |
| 182 | // an antialiased clip couldn't be created. In either case, free up |
| 183 | // the texture in the antialiased mask cache. |
| 184 | // TODO: this may require more investigation. Ganesh performs a lot of |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 185 | // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit |
| 186 | // the stencil buffer path. These may be "incorrectly" clearing the |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 187 | // AA cache. |
| 188 | fAACache.reset(); |
| 189 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 190 | // If the clip is a rectangle then just set the scissor. Otherwise, create |
| 191 | // a stencil mask. |
| 192 | if (clipIn.isRect()) { |
| 193 | fGpu->enableScissor(bounds); |
| 194 | this->setGpuStencil(); |
| 195 | return true; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 196 | } |
| 197 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 198 | // use the stencil clip if we can't represent the clip as a rectangle. |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 199 | bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() && |
| 200 | !bounds.isEmpty(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 201 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 202 | if (useStencil) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 203 | this->createStencilClipMask(clipIn, bounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 204 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 205 | // This must occur after createStencilClipMask. That function may change |
| 206 | // the scissor. Also, it only guarantees that the stencil mask is correct |
| 207 | // within the bounds it was passed, so we must use both stencil and scissor |
| 208 | // test to the bounds for the final draw. |
| 209 | fGpu->enableScissor(bounds); |
| 210 | this->setGpuStencil(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
| 214 | #define VISUALIZE_COMPLEX_CLIP 0 |
| 215 | |
| 216 | #if VISUALIZE_COMPLEX_CLIP |
| 217 | #include "GrRandom.h" |
| 218 | GrRandom gRandom; |
| 219 | #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU()); |
| 220 | #else |
| 221 | #define SET_RANDOM_COLOR |
| 222 | #endif |
| 223 | |
| 224 | namespace { |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 225 | /** |
| 226 | * Does "container" contain "containee"? If either is empty then |
| 227 | * no containment is possible. |
| 228 | */ |
| 229 | bool contains(const SkRect& container, const SkIRect& containee) { |
| 230 | return !containee.isEmpty() && !container.isEmpty() && |
| 231 | container.fLeft <= SkIntToScalar(containee.fLeft) && |
| 232 | container.fTop <= SkIntToScalar(containee.fTop) && |
| 233 | container.fRight >= SkIntToScalar(containee.fRight) && |
| 234 | container.fBottom >= SkIntToScalar(containee.fBottom); |
| 235 | } |
| 236 | |
| 237 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 238 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 239 | // determines how many elements at the head of the clip can be skipped and |
| 240 | // whether the initial clear should be to the inside- or outside-the-clip value, |
| 241 | // and what op should be used to draw the first element that isn't skipped. |
| 242 | int process_initial_clip_elements(const GrClip& clip, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 243 | const GrIRect& bounds, |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 244 | bool* clearToInside, |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 245 | SkRegion::Op* startOp) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 246 | |
| 247 | // logically before the first element of the clip stack is |
| 248 | // processed the clip is entirely open. However, depending on the |
| 249 | // first set op we may prefer to clear to 0 for performance. We may |
| 250 | // also be able to skip the initial clip paths/rects. We loop until |
| 251 | // we cannot skip an element. |
| 252 | int curr; |
| 253 | bool done = false; |
| 254 | *clearToInside = true; |
| 255 | int count = clip.getElementCount(); |
| 256 | |
| 257 | for (curr = 0; curr < count && !done; ++curr) { |
| 258 | switch (clip.getOp(curr)) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 259 | case SkRegion::kReplace_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 260 | // replace ignores everything previous |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 261 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 262 | *clearToInside = false; |
| 263 | done = true; |
| 264 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 265 | case SkRegion::kIntersect_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 266 | // if this element contains the entire bounds then we |
| 267 | // can skip it. |
| 268 | if (kRect_ClipType == clip.getElementType(curr) |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 269 | && contains(clip.getRect(curr), bounds)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | // if everything is initially clearToInside then intersect is |
| 273 | // same as clear to 0 and treat as a replace. Otherwise, |
| 274 | // set stays empty. |
| 275 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 276 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 277 | *clearToInside = false; |
| 278 | done = true; |
| 279 | } |
| 280 | break; |
| 281 | // we can skip a leading union. |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 282 | case SkRegion::kUnion_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 283 | // if everything is initially outside then union is |
| 284 | // same as replace. Otherwise, every pixel is still |
| 285 | // clearToInside |
| 286 | if (!*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 287 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 288 | done = true; |
| 289 | } |
| 290 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 291 | case SkRegion::kXOR_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 292 | // xor is same as difference or replace both of which |
| 293 | // can be 1-pass instead of 2 for xor. |
| 294 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 295 | *startOp = SkRegion::kDifference_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 296 | } else { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 297 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 298 | } |
| 299 | done = true; |
| 300 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 301 | case SkRegion::kDifference_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 302 | // if all pixels are clearToInside then we have to process the |
| 303 | // difference, otherwise it has no effect and all pixels |
| 304 | // remain outside. |
| 305 | if (*clearToInside) { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 306 | *startOp = SkRegion::kDifference_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 307 | done = true; |
| 308 | } |
| 309 | break; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 310 | case SkRegion::kReverseDifference_Op: |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 311 | // if all pixels are clearToInside then reverse difference |
| 312 | // produces empty set. Otherise it is same as replace |
| 313 | if (*clearToInside) { |
| 314 | *clearToInside = false; |
| 315 | } else { |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 316 | *startOp = SkRegion::kReplace_Op; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 317 | done = true; |
| 318 | } |
| 319 | break; |
| 320 | default: |
| 321 | GrCrash("Unknown set op."); |
| 322 | } |
| 323 | } |
| 324 | return done ? curr-1 : count; |
| 325 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 326 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 327 | } |
| 328 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 329 | |
| 330 | namespace { |
| 331 | |
| 332 | //////////////////////////////////////////////////////////////////////////////// |
| 333 | // set up the OpenGL blend function to perform the specified |
| 334 | // boolean operation for alpha clip mask creation |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 335 | void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 336 | |
| 337 | switch (op) { |
| 338 | case SkRegion::kReplace_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 339 | drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 340 | break; |
| 341 | case SkRegion::kIntersect_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 342 | drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 343 | break; |
| 344 | case SkRegion::kUnion_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 345 | drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 346 | break; |
| 347 | case SkRegion::kXOR_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 348 | drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 349 | break; |
| 350 | case SkRegion::kDifference_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 351 | drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 352 | break; |
| 353 | case SkRegion::kReverseDifference_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 354 | drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 355 | break; |
| 356 | default: |
| 357 | GrAssert(false); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 362 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 363 | bool draw_path(GrContext* context, |
| 364 | GrGpu* gpu, |
| 365 | const SkPath& path, |
| 366 | GrPathFill fill, |
| 367 | bool doAA) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 368 | |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 369 | GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, true); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 370 | if (NULL == pr) { |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | pr->drawPath(path, fill, NULL, gpu, 0, doAA); |
| 375 | return true; |
| 376 | } |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 377 | |
| 378 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 379 | |
| 380 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 381 | bool GrClipMaskManager::drawClipShape(GrTexture* target, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 382 | const GrClip& clipIn, |
| 383 | int index) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 384 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 385 | GrAssert(NULL != drawState); |
| 386 | |
| 387 | drawState->setRenderTarget(target->asRenderTarget()); |
| 388 | |
| 389 | if (kRect_ClipType == clipIn.getElementType(index)) { |
| 390 | if (clipIn.getDoAA(index)) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 391 | getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu, |
robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 392 | clipIn.getRect(index), |
| 393 | true); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 394 | } else { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 395 | fGpu->drawSimpleRect(clipIn.getRect(index), NULL, 0); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 396 | } |
| 397 | } else { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 398 | return draw_path(this->getContext(), fGpu, |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 399 | clipIn.getPath(index), |
| 400 | clipIn.getPathFill(index), |
| 401 | clipIn.getDoAA(index)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 402 | } |
| 403 | return true; |
| 404 | } |
| 405 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 406 | void GrClipMaskManager::drawTexture(GrTexture* target, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 407 | GrTexture* texture) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 408 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 409 | GrAssert(NULL != drawState); |
| 410 | |
| 411 | // no AA here since it is encoded in the texture |
| 412 | drawState->setRenderTarget(target->asRenderTarget()); |
| 413 | |
| 414 | GrMatrix sampleM; |
| 415 | sampleM.setIDiv(texture->width(), texture->height()); |
| 416 | drawState->setTexture(0, texture); |
| 417 | |
| 418 | drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode, |
| 419 | GrSamplerState::kNearest_Filter, |
| 420 | sampleM); |
| 421 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 422 | GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()), |
| 423 | SkIntToScalar(target->height())); |
| 424 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 425 | fGpu->drawSimpleRect(rect, NULL, 1 << 0); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 426 | |
| 427 | drawState->setTexture(0, NULL); |
| 428 | } |
| 429 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 430 | // get a texture to act as a temporary buffer for AA clip boolean operations |
| 431 | // 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] | 432 | void GrClipMaskManager::getTemp(const GrIRect& bounds, |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 433 | GrAutoScratchTexture* temp) { |
| 434 | if (NULL != temp->texture()) { |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 435 | // we've already allocated the temp texture |
| 436 | return; |
| 437 | } |
| 438 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 439 | GrTextureDesc desc; |
| 440 | desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit; |
| 441 | desc.fWidth = bounds.width(); |
| 442 | desc.fHeight = bounds.height(); |
| 443 | desc.fConfig = kAlpha_8_GrPixelConfig; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 444 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 445 | temp->set(this->getContext(), desc); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 446 | } |
| 447 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 448 | |
| 449 | void GrClipMaskManager::setupCache(const GrClip& clipIn, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 450 | const GrIRect& bounds) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 451 | // Since we are setting up the cache we know the last lookup was a miss |
| 452 | // Free up the currently cached mask so it can be reused |
| 453 | fAACache.reset(); |
| 454 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 455 | GrTextureDesc desc; |
| 456 | desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit; |
| 457 | desc.fWidth = bounds.width(); |
| 458 | desc.fHeight = bounds.height(); |
| 459 | desc.fConfig = kAlpha_8_GrPixelConfig; |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 460 | |
| 461 | fAACache.acquireMask(clipIn, desc, bounds); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 462 | } |
| 463 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 464 | //////////////////////////////////////////////////////////////////////////////// |
| 465 | // Shared preamble between gpu and SW-only AA clip mask creation paths. |
| 466 | // Handles caching, determination of clip mask bound & allocation (if needed) |
| 467 | // of the result texture |
| 468 | // Returns true if there is no more work to be done (i.e., we got a cache hit) |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 469 | bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 470 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 471 | GrIRect *resultBounds) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 472 | GrDrawState* origDrawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 473 | GrAssert(origDrawState->isClipState()); |
| 474 | |
| 475 | GrRenderTarget* rt = origDrawState->getRenderTarget(); |
| 476 | GrAssert(NULL != rt); |
| 477 | |
| 478 | GrRect rtRect; |
| 479 | rtRect.setLTRB(0, 0, |
| 480 | GrIntToScalar(rt->width()), GrIntToScalar(rt->height())); |
| 481 | |
| 482 | // unlike the stencil path the alpha path is not bound to the size of the |
| 483 | // render target - determine the minimum size required for the mask |
| 484 | GrRect bounds; |
| 485 | |
| 486 | if (clipIn.hasConservativeBounds()) { |
| 487 | bounds = clipIn.getConservativeBounds(); |
| 488 | if (!bounds.intersect(rtRect)) { |
| 489 | // the mask will be empty in this case |
| 490 | GrAssert(false); |
| 491 | bounds.setEmpty(); |
| 492 | } |
| 493 | } else { |
| 494 | // still locked to the size of the render target |
| 495 | bounds = rtRect; |
| 496 | } |
| 497 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 498 | GrIRect intBounds; |
| 499 | bounds.roundOut(&intBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 500 | |
| 501 | // need to outset a pixel since the standard bounding box computation |
| 502 | // 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] | 503 | intBounds.outset(1, 1); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 504 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 505 | // TODO: make sure we don't outset if bounds are still 0,0 @ min |
| 506 | |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 507 | if (fAACache.canReuse(clipIn, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 508 | intBounds.width(), |
| 509 | intBounds.height())) { |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 510 | *result = fAACache.getLastMask(); |
| 511 | fAACache.getLastBound(resultBounds); |
| 512 | return true; |
| 513 | } |
| 514 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 515 | this->setupCache(clipIn, intBounds); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 516 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 517 | *resultBounds = intBounds; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 518 | return false; |
| 519 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 520 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 521 | //////////////////////////////////////////////////////////////////////////////// |
| 522 | // Create a 8-bit clip mask in alpha |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 523 | bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 524 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 525 | GrIRect *resultBounds) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 526 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 527 | GrAssert(kNone_ClipMaskType == fCurrClipMaskType); |
| 528 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 529 | if (this->clipMaskPreamble(clipIn, result, resultBounds)) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 530 | fCurrClipMaskType = kAlpha_ClipMaskType; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 531 | return true; |
| 532 | } |
| 533 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 534 | GrTexture* accum = fAACache.getLastMask(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 535 | if (NULL == accum) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 536 | fAACache.reset(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 537 | return false; |
| 538 | } |
| 539 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 540 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
| 541 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 542 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 543 | GrDrawTarget::AutoGeometryPush agp(fGpu); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 544 | |
| 545 | int count = clipIn.getElementCount(); |
| 546 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 547 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 548 | // if we were able to trim down the size of the mask we need to |
| 549 | // offset the paths & rects that will be used to compute it |
| 550 | GrMatrix m; |
| 551 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 552 | m.setTranslate(SkIntToScalar(-resultBounds->fLeft), |
| 553 | SkIntToScalar(-resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 554 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 555 | drawState->setViewMatrix(m); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | bool clearToInside; |
| 559 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
| 560 | int start = process_initial_clip_elements(clipIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 561 | *resultBounds, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 562 | &clearToInside, |
| 563 | &startOp); |
| 564 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 565 | fGpu->clear(NULL, |
| 566 | clearToInside ? 0xffffffff : 0x00000000, |
| 567 | accum->asRenderTarget()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 568 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 569 | GrAutoScratchTexture temp; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 570 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 571 | // walk through each clip element and perform its set op |
| 572 | for (int c = start; c < count; ++c) { |
| 573 | |
| 574 | SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c); |
| 575 | |
| 576 | if (SkRegion::kReplace_Op == op) { |
| 577 | // TODO: replace is actually a lot faster then intersection |
| 578 | // for this path - refactor the stencil path so it can handle |
| 579 | // replace ops and alter GrClip to allow them through |
| 580 | |
| 581 | // clear the accumulator and draw the new object directly into it |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 582 | fGpu->clear(NULL, 0x00000000, accum->asRenderTarget()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 583 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 584 | setup_boolean_blendcoeffs(drawState, op); |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 585 | this->drawClipShape(accum, clipIn, c); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 586 | |
| 587 | } else if (SkRegion::kReverseDifference_Op == op || |
| 588 | SkRegion::kIntersect_Op == op) { |
| 589 | // there is no point in intersecting a screen filling rectangle. |
| 590 | if (SkRegion::kIntersect_Op == op && |
| 591 | kRect_ClipType == clipIn.getElementType(c) && |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 592 | contains(clipIn.getRect(c), *resultBounds)) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 593 | continue; |
| 594 | } |
| 595 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 596 | getTemp(*resultBounds, &temp); |
| 597 | if (NULL == temp.texture()) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 598 | fAACache.reset(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 599 | return false; |
| 600 | } |
| 601 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 602 | // clear the temp target & draw into it |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 603 | fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 604 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 605 | setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op); |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 606 | this->drawClipShape(temp.texture(), clipIn, c); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 607 | |
| 608 | // TODO: rather than adding these two translations here |
| 609 | // compute the bounding box needed to render the texture |
| 610 | // into temp |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 611 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 612 | GrMatrix m; |
| 613 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 614 | m.setTranslate(SkIntToScalar(resultBounds->fLeft), |
| 615 | SkIntToScalar(resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 616 | |
| 617 | drawState->preConcatViewMatrix(m); |
| 618 | } |
| 619 | |
| 620 | // Now draw into the accumulator using the real operation |
| 621 | // and the temp buffer as a texture |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 622 | setup_boolean_blendcoeffs(drawState, op); |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 623 | this->drawTexture(accum, temp.texture()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 624 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 625 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 626 | GrMatrix m; |
| 627 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 628 | m.setTranslate(SkIntToScalar(-resultBounds->fLeft), |
| 629 | SkIntToScalar(-resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 630 | |
| 631 | drawState->preConcatViewMatrix(m); |
| 632 | } |
| 633 | |
| 634 | } else { |
| 635 | // all the remaining ops can just be directly draw into |
| 636 | // the accumulation buffer |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 637 | setup_boolean_blendcoeffs(drawState, op); |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 638 | this->drawClipShape(accum, clipIn, c); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 642 | *result = accum; |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 643 | fCurrClipMaskType = kAlpha_ClipMaskType; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 644 | return true; |
| 645 | } |
| 646 | |
| 647 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 648 | // Create a 1-bit clip mask in the stencil buffer |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 649 | bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn, |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 650 | const GrIRect& bounds) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 651 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 652 | GrAssert(kNone_ClipMaskType == fCurrClipMaskType); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 653 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 654 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 655 | GrAssert(drawState->isClipState()); |
| 656 | |
| 657 | GrRenderTarget* rt = drawState->getRenderTarget(); |
| 658 | GrAssert(NULL != rt); |
| 659 | |
| 660 | // TODO: dynamically attach a SB when needed. |
| 661 | GrStencilBuffer* stencilBuffer = rt->getStencilBuffer(); |
| 662 | if (NULL == stencilBuffer) { |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) { |
| 667 | |
| 668 | stencilBuffer->setLastClip(clipIn, rt->width(), rt->height()); |
| 669 | |
| 670 | // we set the current clip to the bounds so that our recursive |
| 671 | // draws are scissored to them. We use the copy of the complex clip |
| 672 | // we just stashed on the SB to render from. We set it back after |
| 673 | // we finish drawing it into the stencil. |
| 674 | const GrClip& clipCopy = stencilBuffer->getLastClip(); |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 675 | fGpu->setClip(GrClip(bounds)); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 676 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 677 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
| 678 | drawState = fGpu->drawState(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 679 | drawState->setRenderTarget(rt); |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 680 | GrDrawTarget::AutoGeometryPush agp(fGpu); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 681 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 682 | #if !VISUALIZE_COMPLEX_CLIP |
| 683 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
| 684 | #endif |
| 685 | |
| 686 | int count = clipCopy.getElementCount(); |
| 687 | int clipBit = stencilBuffer->bits(); |
| 688 | SkASSERT((clipBit <= 16) && |
| 689 | "Ganesh only handles 16b or smaller stencil buffers"); |
| 690 | clipBit = (1 << (clipBit-1)); |
| 691 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 692 | GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 693 | |
| 694 | bool clearToInside; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 695 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 696 | int start = process_initial_clip_elements(clipCopy, |
| 697 | rtRect, |
| 698 | &clearToInside, |
| 699 | &startOp); |
| 700 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 701 | fGpu->clearStencilClip(bounds, clearToInside); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 702 | |
| 703 | // walk through each clip element and perform its set op |
| 704 | // with the existing clip. |
| 705 | for (int c = start; c < count; ++c) { |
| 706 | GrPathFill fill; |
| 707 | bool fillInverted; |
| 708 | // enabled at bottom of loop |
| 709 | drawState->disableState(GrGpu::kModifyStencilClip_StateBit); |
| 710 | |
| 711 | bool canRenderDirectToStencil; // can the clip element be drawn |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 712 | // directly to the stencil buffer |
| 713 | // with a non-inverted fill rule |
| 714 | // without extra passes to |
| 715 | // resolve in/out status. |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 716 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 717 | SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c); |
| 718 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 719 | GrPathRenderer* pr = NULL; |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 720 | const SkPath* clipPath = NULL; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 721 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 722 | canRenderDirectToStencil = true; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 723 | fill = kEvenOdd_GrPathFill; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 724 | fillInverted = false; |
| 725 | // there is no point in intersecting a screen filling |
| 726 | // rectangle. |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 727 | if (SkRegion::kIntersect_Op == op && |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 728 | contains(clipCopy.getRect(c), rtRect)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 729 | continue; |
| 730 | } |
| 731 | } else { |
| 732 | fill = clipCopy.getPathFill(c); |
| 733 | fillInverted = GrIsFillInverted(fill); |
| 734 | fill = GrNonInvertedFill(fill); |
| 735 | clipPath = &clipCopy.getPath(c); |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 736 | pr = this->getContext()->getPathRenderer(*clipPath, |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 737 | fill, fGpu, false, |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 738 | true); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 739 | if (NULL == pr) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 740 | fGpu->setClip(clipCopy); // restore to the original |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | canRenderDirectToStencil = |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 744 | !pr->requiresStencilPass(*clipPath, fill, fGpu); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 745 | } |
| 746 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 747 | int passes; |
| 748 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 749 | |
| 750 | bool canDrawDirectToClip; // Given the renderer, the element, |
| 751 | // fill rule, and set operation can |
| 752 | // we render the element directly to |
| 753 | // stencil bit used for clipping. |
| 754 | canDrawDirectToClip = |
| 755 | GrStencilSettings::GetClipPasses(op, |
| 756 | canRenderDirectToStencil, |
| 757 | clipBit, |
| 758 | fillInverted, |
| 759 | &passes, stencilSettings); |
| 760 | |
| 761 | // draw the element to the client stencil bits if necessary |
| 762 | if (!canDrawDirectToClip) { |
| 763 | GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil, |
| 764 | kIncClamp_StencilOp, |
| 765 | kIncClamp_StencilOp, |
| 766 | kAlways_StencilFunc, |
| 767 | 0xffff, |
| 768 | 0x0000, |
| 769 | 0xffff); |
| 770 | SET_RANDOM_COLOR |
| 771 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 772 | *drawState->stencil() = gDrawToStencil; |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 773 | fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 774 | } else { |
| 775 | if (canRenderDirectToStencil) { |
| 776 | *drawState->stencil() = gDrawToStencil; |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 777 | pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 778 | } else { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 779 | pr->drawPathToStencil(*clipPath, fill, fGpu); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // now we modify the clip bit by rendering either the clip |
| 785 | // element directly or a bounding rect of the entire clip. |
| 786 | drawState->enableState(GrGpu::kModifyStencilClip_StateBit); |
| 787 | for (int p = 0; p < passes; ++p) { |
| 788 | *drawState->stencil() = stencilSettings[p]; |
| 789 | if (canDrawDirectToClip) { |
| 790 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 791 | SET_RANDOM_COLOR |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 792 | fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 793 | } else { |
| 794 | SET_RANDOM_COLOR |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 795 | pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 796 | } |
| 797 | } else { |
| 798 | SET_RANDOM_COLOR |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 799 | GrRect rect = GrRect::MakeLTRB( |
| 800 | SkIntToScalar(bounds.fLeft), |
| 801 | SkIntToScalar(bounds.fTop), |
| 802 | SkIntToScalar(bounds.fRight), |
| 803 | SkIntToScalar(bounds.fBottom)); |
| 804 | fGpu->drawSimpleRect(rect, NULL, 0); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | } |
| 808 | // restore clip |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 809 | fGpu->setClip(clipCopy); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 810 | } |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 811 | // set this last because recursive draws may overwrite it back to kNone. |
| 812 | GrAssert(kNone_ClipMaskType == fCurrClipMaskType); |
| 813 | fCurrClipMaskType = kStencil_ClipMaskType; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 814 | return true; |
| 815 | } |
| 816 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 817 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 818 | // mapping depends on whether stencil-clipping is in effect. |
| 819 | static const GrStencilFunc |
| 820 | gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = { |
| 821 | {// Stencil-Clipping is DISABLED, we are effectively always inside the clip |
| 822 | // In the Clip Funcs |
| 823 | kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 824 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 825 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 826 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 827 | // Special in the clip func that forces user's ref to be 0. |
| 828 | kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 829 | // make ref 0 and do normal nequal. |
| 830 | }, |
| 831 | {// Stencil-Clipping is ENABLED |
| 832 | // In the Clip Funcs |
| 833 | kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 834 | // eq stencil clip bit, mask |
| 835 | // out user bits. |
| 836 | |
| 837 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 838 | // add stencil bit to mask and ref |
| 839 | |
| 840 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 841 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 842 | // for both of these we can add |
| 843 | // the clip bit to the mask and |
| 844 | // ref and compare as normal |
| 845 | // Special in the clip func that forces user's ref to be 0. |
| 846 | kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 847 | // make ref have only the clip bit set |
| 848 | // and make comparison be less |
| 849 | // 10..0 < 1..user_bits.. |
| 850 | } |
| 851 | }; |
| 852 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 853 | namespace { |
| 854 | // Sets the settings to clip against the stencil buffer clip while ignoring the |
| 855 | // client bits. |
| 856 | const GrStencilSettings& basic_apply_stencil_clip_settings() { |
| 857 | // stencil settings to use when clip is in stencil |
| 858 | GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, |
| 859 | kKeep_StencilOp, |
| 860 | kKeep_StencilOp, |
| 861 | kAlwaysIfInClip_StencilFunc, |
| 862 | 0x0000, |
| 863 | 0x0000, |
| 864 | 0x0000); |
| 865 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | void GrClipMaskManager::setGpuStencil() { |
| 870 | // We make two copies of the StencilSettings here (except in the early |
| 871 | // exit scenario. One copy from draw state to the stack var. Then another |
| 872 | // from the stack var to the gpu. We could make this class hold a ptr to |
| 873 | // GrGpu's fStencilSettings and eliminate the stack copy here. |
| 874 | |
| 875 | const GrDrawState& drawState = fGpu->getDrawState(); |
| 876 | |
| 877 | // use stencil for clipping if clipping is enabled and the clip |
| 878 | // has been written into the stencil. |
| 879 | GrClipMaskManager::StencilClipMode clipMode; |
| 880 | if (this->isClipInStencil() && drawState.isClipState()) { |
| 881 | clipMode = GrClipMaskManager::kRespectClip_StencilClipMode; |
| 882 | // We can't be modifying the clip and respecting it at the same time. |
| 883 | GrAssert(!drawState.isStateFlagEnabled( |
| 884 | GrGpu::kModifyStencilClip_StateBit)); |
| 885 | } else if (drawState.isStateFlagEnabled( |
| 886 | GrGpu::kModifyStencilClip_StateBit)) { |
| 887 | clipMode = GrClipMaskManager::kModifyClip_StencilClipMode; |
| 888 | } else { |
| 889 | clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode; |
| 890 | } |
| 891 | |
| 892 | GrStencilSettings settings; |
| 893 | // The GrGpu client may not be using the stencil buffer but we may need to |
| 894 | // enable it in order to respect a stencil clip. |
| 895 | if (drawState.getStencil().isDisabled()) { |
| 896 | if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) { |
| 897 | settings = basic_apply_stencil_clip_settings(); |
| 898 | } else { |
| 899 | fGpu->disableStencil(); |
| 900 | return; |
| 901 | } |
| 902 | } else { |
| 903 | settings = drawState.getStencil(); |
| 904 | } |
| 905 | |
| 906 | // TODO: dynamically attach a stencil buffer |
| 907 | int stencilBits = 0; |
| 908 | GrStencilBuffer* stencilBuffer = |
| 909 | drawState.getRenderTarget()->getStencilBuffer(); |
| 910 | if (NULL != stencilBuffer) { |
| 911 | stencilBits = stencilBuffer->bits(); |
| 912 | } |
| 913 | |
| 914 | #if GR_DEBUG |
| 915 | if (!fGpu->getCaps().fStencilWrapOpsSupport) { |
| 916 | GrAssert(settings.frontPassOp() != kIncWrap_StencilOp); |
| 917 | GrAssert(settings.frontPassOp() != kDecWrap_StencilOp); |
| 918 | GrAssert(settings.frontFailOp() != kIncWrap_StencilOp); |
| 919 | GrAssert(settings.backFailOp() != kDecWrap_StencilOp); |
| 920 | GrAssert(settings.backPassOp() != kIncWrap_StencilOp); |
| 921 | GrAssert(settings.backPassOp() != kDecWrap_StencilOp); |
| 922 | GrAssert(settings.backFailOp() != kIncWrap_StencilOp); |
| 923 | GrAssert(settings.frontFailOp() != kDecWrap_StencilOp); |
| 924 | } |
| 925 | #endif |
| 926 | this->adjustStencilParams(&settings, clipMode, stencilBits); |
| 927 | fGpu->setStencilSettings(settings); |
| 928 | } |
| 929 | |
| 930 | void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings, |
| 931 | StencilClipMode mode, |
| 932 | int stencilBitCnt) { |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 933 | GrAssert(stencilBitCnt > 0); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 934 | |
| 935 | if (kModifyClip_StencilClipMode == mode) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 936 | // We assume that this clip manager itself is drawing to the GrGpu and |
| 937 | // has already setup the correct values. |
| 938 | return; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 939 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 940 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 941 | unsigned int clipBit = (1 << (stencilBitCnt - 1)); |
| 942 | unsigned int userBits = clipBit - 1; |
| 943 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 944 | GrStencilSettings::Face face = GrStencilSettings::kFront_Face; |
| 945 | bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 946 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 947 | bool finished = false; |
| 948 | while (!finished) { |
| 949 | GrStencilFunc func = settings->func(face); |
| 950 | uint16_t writeMask = settings->writeMask(face); |
| 951 | uint16_t funcMask = settings->funcMask(face); |
| 952 | uint16_t funcRef = settings->funcRef(face); |
| 953 | |
| 954 | GrAssert((unsigned) func < kStencilFuncCount); |
| 955 | |
| 956 | writeMask &= userBits; |
| 957 | |
| 958 | if (func >= kBasicStencilFuncCount) { |
| 959 | int respectClip = kRespectClip_StencilClipMode == mode; |
| 960 | if (respectClip) { |
| 961 | // The GrGpu class should have checked this |
| 962 | GrAssert(this->isClipInStencil()); |
| 963 | switch (func) { |
| 964 | case kAlwaysIfInClip_StencilFunc: |
| 965 | funcMask = clipBit; |
| 966 | funcRef = clipBit; |
| 967 | break; |
| 968 | case kEqualIfInClip_StencilFunc: |
| 969 | case kLessIfInClip_StencilFunc: |
| 970 | case kLEqualIfInClip_StencilFunc: |
| 971 | funcMask = (funcMask & userBits) | clipBit; |
| 972 | funcRef = (funcRef & userBits) | clipBit; |
| 973 | break; |
| 974 | case kNonZeroIfInClip_StencilFunc: |
| 975 | funcMask = (funcMask & userBits) | clipBit; |
| 976 | funcRef = clipBit; |
| 977 | break; |
| 978 | default: |
| 979 | GrCrash("Unknown stencil func"); |
| 980 | } |
| 981 | } else { |
| 982 | funcMask &= userBits; |
| 983 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 984 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 985 | const GrStencilFunc* table = |
| 986 | gSpecialToBasicStencilFunc[respectClip]; |
| 987 | func = table[func - kBasicStencilFuncCount]; |
| 988 | GrAssert(func >= 0 && func < kBasicStencilFuncCount); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 989 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 990 | funcMask &= userBits; |
| 991 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 992 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 993 | |
| 994 | settings->setFunc(face, func); |
| 995 | settings->setWriteMask(face, writeMask); |
| 996 | settings->setFuncMask(face, funcMask); |
| 997 | settings->setFuncRef(face, funcRef); |
| 998 | |
| 999 | if (GrStencilSettings::kFront_Face == face) { |
| 1000 | face = GrStencilSettings::kBack_Face; |
| 1001 | finished = !twoSided; |
| 1002 | } else { |
| 1003 | finished = true; |
| 1004 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1005 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1006 | if (!twoSided) { |
| 1007 | settings->copyFrontSettingsToBack(); |
| 1008 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | //////////////////////////////////////////////////////////////////////////////// |
| 1012 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1013 | namespace { |
| 1014 | |
| 1015 | GrPathFill invert_fill(GrPathFill fill) { |
| 1016 | static const GrPathFill gInvertedFillTable[] = { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1017 | kInverseWinding_GrPathFill, // kWinding_GrPathFill |
| 1018 | kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill |
| 1019 | kWinding_GrPathFill, // kInverseWinding_GrPathFill |
| 1020 | kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill |
| 1021 | kHairLine_GrPathFill, // kHairLine_GrPathFill |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1022 | }; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1023 | GR_STATIC_ASSERT(0 == kWinding_GrPathFill); |
| 1024 | GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill); |
| 1025 | GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill); |
| 1026 | GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill); |
| 1027 | GR_STATIC_ASSERT(4 == kHairLine_GrPathFill); |
| 1028 | GR_STATIC_ASSERT(5 == kGrPathFillCount); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1029 | return gInvertedFillTable[fill]; |
| 1030 | } |
| 1031 | |
| 1032 | } |
| 1033 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 1034 | bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1035 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 1036 | GrIRect *resultBounds) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1037 | GrAssert(kNone_ClipMaskType == fCurrClipMaskType); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1038 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 1039 | if (this->clipMaskPreamble(clipIn, result, resultBounds)) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1040 | return true; |
| 1041 | } |
| 1042 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1043 | GrTexture* accum = fAACache.getLastMask(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1044 | if (NULL == accum) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1045 | fAACache.reset(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1046 | return false; |
| 1047 | } |
| 1048 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 1049 | GrSWMaskHelper helper(this->getContext()); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1050 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1051 | helper.init(*resultBounds, NULL, false); |
| 1052 | |
| 1053 | int count = clipIn.getElementCount(); |
| 1054 | |
| 1055 | bool clearToInside; |
| 1056 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
| 1057 | int start = process_initial_clip_elements(clipIn, |
| 1058 | *resultBounds, |
| 1059 | &clearToInside, |
| 1060 | &startOp); |
| 1061 | |
| 1062 | helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000); |
| 1063 | |
| 1064 | for (int i = start; i < count; ++i) { |
| 1065 | |
| 1066 | SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i); |
| 1067 | |
| 1068 | if (SkRegion::kIntersect_Op == op || |
| 1069 | SkRegion::kReverseDifference_Op == op) { |
| 1070 | // Intersect and reverse difference require modifying pixels |
| 1071 | // outside of the geometry that is being "drawn". In both cases |
| 1072 | // we erase all the pixels outside of the geometry but |
| 1073 | // leave the pixels inside the geometry alone. For reverse |
| 1074 | // difference we invert all the pixels before clearing the ones |
| 1075 | // outside the geometry. |
| 1076 | if (SkRegion::kReverseDifference_Op == op) { |
| 1077 | SkRect temp = SkRect::MakeLTRB( |
| 1078 | SkIntToScalar(resultBounds->left()), |
| 1079 | SkIntToScalar(resultBounds->top()), |
| 1080 | SkIntToScalar(resultBounds->right()), |
| 1081 | SkIntToScalar(resultBounds->bottom())); |
| 1082 | |
| 1083 | // invert the entire scene |
| 1084 | helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE); |
| 1085 | } |
| 1086 | |
| 1087 | if (kRect_ClipType == clipIn.getElementType(i)) { |
| 1088 | |
| 1089 | // convert the rect to a path so we can invert the fill |
| 1090 | SkPath temp; |
| 1091 | temp.addRect(clipIn.getRect(i)); |
| 1092 | |
| 1093 | helper.draw(temp, SkRegion::kReplace_Op, |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 1094 | kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i), |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1095 | 0x00000000); |
| 1096 | } else { |
| 1097 | GrAssert(kPath_ClipType == clipIn.getElementType(i)); |
| 1098 | |
| 1099 | helper.draw(clipIn.getPath(i), |
| 1100 | SkRegion::kReplace_Op, |
| 1101 | invert_fill(clipIn.getPathFill(i)), |
| 1102 | clipIn.getDoAA(i), |
| 1103 | 0x00000000); |
| 1104 | } |
| 1105 | |
| 1106 | continue; |
| 1107 | } |
| 1108 | |
| 1109 | // The other ops (union, xor, diff) only affect pixels inside |
| 1110 | // the geometry so they can just be drawn normally |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1111 | if (kRect_ClipType == clipIn.getElementType(i)) { |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1112 | |
| 1113 | helper.draw(clipIn.getRect(i), |
| 1114 | op, |
| 1115 | clipIn.getDoAA(i), SK_ColorWHITE); |
| 1116 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1117 | } else { |
| 1118 | GrAssert(kPath_ClipType == clipIn.getElementType(i)); |
| 1119 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1120 | helper.draw(clipIn.getPath(i), |
| 1121 | op, |
| 1122 | clipIn.getPathFill(i), |
| 1123 | clipIn.getDoAA(i), SK_ColorWHITE); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1127 | // Because we are using the scratch texture cache, "accum" may be |
| 1128 | // larger than expected and have some cruft in the areas we aren't using. |
| 1129 | // Clear it out. |
| 1130 | |
| 1131 | // TODO: need a simpler way to clear the texture - can we combine |
| 1132 | // the clear and the writePixels (inside toTexture) |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 1133 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1134 | GrAssert(NULL != drawState); |
| 1135 | GrRenderTarget* temp = drawState->getRenderTarget(); |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 1136 | fGpu->clear(NULL, 0x00000000, accum->asRenderTarget()); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1137 | // can't leave the accum bound as a rendertarget |
| 1138 | drawState->setRenderTarget(temp); |
| 1139 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 1140 | helper.toTexture(accum, clearToInside); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1141 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1142 | *result = accum; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1143 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1144 | fCurrClipMaskType = kAlpha_ClipMaskType; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1145 | return true; |
| 1146 | } |
| 1147 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1148 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1149 | void GrClipMaskManager::releaseResources() { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1150 | fAACache.releaseResources(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1151 | } |