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