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