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