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