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