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