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