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