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