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