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 |
| 105 | // kEvenOdd_PathFill. |
| 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, |
| 113 | kEvenOdd_PathFill, true)) { |
| 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: |
| 364 | drawState->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff); |
| 365 | break; |
| 366 | case SkRegion::kIntersect_Op: |
| 367 | drawState->setBlendFunc(kDC_BlendCoeff, kZero_BlendCoeff); |
| 368 | break; |
| 369 | case SkRegion::kUnion_Op: |
| 370 | drawState->setBlendFunc(kOne_BlendCoeff, kISC_BlendCoeff); |
| 371 | break; |
| 372 | case SkRegion::kXOR_Op: |
| 373 | drawState->setBlendFunc(kIDC_BlendCoeff, kISC_BlendCoeff); |
| 374 | break; |
| 375 | case SkRegion::kDifference_Op: |
| 376 | drawState->setBlendFunc(kZero_BlendCoeff, kISC_BlendCoeff); |
| 377 | break; |
| 378 | case SkRegion::kReverseDifference_Op: |
| 379 | drawState->setBlendFunc(kIDC_BlendCoeff, kZero_BlendCoeff); |
| 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, |
| 422 | kEvenOdd_PathFill, 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 | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 484 | const GrTextureDesc desc = { |
| 485 | kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 486 | bounds.width(), |
| 487 | bounds.height(), |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 488 | kAlpha_8_GrPixelConfig, |
| 489 | 0 // samples |
| 490 | }; |
| 491 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 492 | temp->set(this->getContext(), desc); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 493 | } |
| 494 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 495 | |
| 496 | void GrClipMaskManager::setupCache(const GrClip& clipIn, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 497 | const GrIRect& bounds) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 498 | // Since we are setting up the cache we know the last lookup was a miss |
| 499 | // Free up the currently cached mask so it can be reused |
| 500 | fAACache.reset(); |
| 501 | |
| 502 | const GrTextureDesc desc = { |
| 503 | kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 504 | bounds.width(), |
| 505 | bounds.height(), |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 506 | kAlpha_8_GrPixelConfig, |
| 507 | 0 // samples |
| 508 | }; |
| 509 | |
| 510 | fAACache.acquireMask(clipIn, desc, bounds); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 511 | } |
| 512 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 513 | //////////////////////////////////////////////////////////////////////////////// |
| 514 | // Shared preamble between gpu and SW-only AA clip mask creation paths. |
| 515 | // Handles caching, determination of clip mask bound & allocation (if needed) |
| 516 | // of the result texture |
| 517 | // Returns true if there is no more work to be done (i.e., we got a cache hit) |
| 518 | bool GrClipMaskManager::clipMaskPreamble(GrGpu* gpu, |
| 519 | const GrClip& clipIn, |
| 520 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 521 | GrIRect *resultBounds) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 522 | GrDrawState* origDrawState = gpu->drawState(); |
| 523 | GrAssert(origDrawState->isClipState()); |
| 524 | |
| 525 | GrRenderTarget* rt = origDrawState->getRenderTarget(); |
| 526 | GrAssert(NULL != rt); |
| 527 | |
| 528 | GrRect rtRect; |
| 529 | rtRect.setLTRB(0, 0, |
| 530 | GrIntToScalar(rt->width()), GrIntToScalar(rt->height())); |
| 531 | |
| 532 | // unlike the stencil path the alpha path is not bound to the size of the |
| 533 | // render target - determine the minimum size required for the mask |
| 534 | GrRect bounds; |
| 535 | |
| 536 | if (clipIn.hasConservativeBounds()) { |
| 537 | bounds = clipIn.getConservativeBounds(); |
| 538 | if (!bounds.intersect(rtRect)) { |
| 539 | // the mask will be empty in this case |
| 540 | GrAssert(false); |
| 541 | bounds.setEmpty(); |
| 542 | } |
| 543 | } else { |
| 544 | // still locked to the size of the render target |
| 545 | bounds = rtRect; |
| 546 | } |
| 547 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 548 | GrIRect intBounds; |
| 549 | bounds.roundOut(&intBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 550 | |
| 551 | // need to outset a pixel since the standard bounding box computation |
| 552 | // 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] | 553 | intBounds.outset(1, 1); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 554 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 555 | // TODO: make sure we don't outset if bounds are still 0,0 @ min |
| 556 | |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 557 | if (fAACache.canReuse(clipIn, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 558 | intBounds.width(), |
| 559 | intBounds.height())) { |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 560 | *result = fAACache.getLastMask(); |
| 561 | fAACache.getLastBound(resultBounds); |
| 562 | return true; |
| 563 | } |
| 564 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 565 | this->setupCache(clipIn, intBounds); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 566 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 567 | *resultBounds = intBounds; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 568 | return false; |
| 569 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 570 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 571 | //////////////////////////////////////////////////////////////////////////////// |
| 572 | // Create a 8-bit clip mask in alpha |
| 573 | bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu, |
| 574 | const GrClip& clipIn, |
| 575 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 576 | GrIRect *resultBounds) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 577 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 578 | if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 579 | return true; |
| 580 | } |
| 581 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 582 | GrTexture* accum = fAACache.getLastMask(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 583 | if (NULL == accum) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 584 | fClipMaskInAlpha = false; |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 585 | fAACache.reset(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 586 | return false; |
| 587 | } |
| 588 | |
| 589 | GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit); |
| 590 | GrDrawState* drawState = gpu->drawState(); |
| 591 | |
| 592 | GrDrawTarget::AutoGeometryPush agp(gpu); |
| 593 | |
| 594 | int count = clipIn.getElementCount(); |
| 595 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 596 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 597 | // if we were able to trim down the size of the mask we need to |
| 598 | // offset the paths & rects that will be used to compute it |
| 599 | GrMatrix m; |
| 600 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 601 | m.setTranslate(SkIntToScalar(-resultBounds->fLeft), |
| 602 | SkIntToScalar(-resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 603 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 604 | drawState->setViewMatrix(m); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | bool clearToInside; |
| 608 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
| 609 | int start = process_initial_clip_elements(clipIn, |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 610 | *resultBounds, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 611 | &clearToInside, |
| 612 | &startOp); |
| 613 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 614 | clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 615 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 616 | GrAutoScratchTexture temp; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 617 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 618 | // walk through each clip element and perform its set op |
| 619 | for (int c = start; c < count; ++c) { |
| 620 | |
| 621 | SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c); |
| 622 | |
| 623 | if (SkRegion::kReplace_Op == op) { |
| 624 | // TODO: replace is actually a lot faster then intersection |
| 625 | // for this path - refactor the stencil path so it can handle |
| 626 | // replace ops and alter GrClip to allow them through |
| 627 | |
| 628 | // clear the accumulator and draw the new object directly into it |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 629 | clear(gpu, accum, 0x00000000); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 630 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 631 | setup_boolean_blendcoeffs(drawState, op); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 632 | this->drawClipShape(gpu, accum, clipIn, c); |
| 633 | |
| 634 | } else if (SkRegion::kReverseDifference_Op == op || |
| 635 | SkRegion::kIntersect_Op == op) { |
| 636 | // there is no point in intersecting a screen filling rectangle. |
| 637 | if (SkRegion::kIntersect_Op == op && |
| 638 | kRect_ClipType == clipIn.getElementType(c) && |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 639 | contains(clipIn.getRect(c), *resultBounds)) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 640 | continue; |
| 641 | } |
| 642 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 643 | getTemp(*resultBounds, &temp); |
| 644 | if (NULL == temp.texture()) { |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 645 | fClipMaskInAlpha = false; |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 646 | fAACache.reset(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 647 | return false; |
| 648 | } |
| 649 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 650 | // clear the temp target & draw into it |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 651 | clear(gpu, temp.texture(), 0x00000000); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 652 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 653 | setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 654 | this->drawClipShape(gpu, temp.texture(), clipIn, c); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 655 | |
| 656 | // TODO: rather than adding these two translations here |
| 657 | // compute the bounding box needed to render the texture |
| 658 | // into temp |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 659 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 660 | GrMatrix m; |
| 661 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 662 | m.setTranslate(SkIntToScalar(resultBounds->fLeft), |
| 663 | SkIntToScalar(resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 664 | |
| 665 | drawState->preConcatViewMatrix(m); |
| 666 | } |
| 667 | |
| 668 | // Now draw into the accumulator using the real operation |
| 669 | // and the temp buffer as a texture |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 670 | setup_boolean_blendcoeffs(drawState, op); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 671 | this->drawTexture(gpu, accum, temp.texture()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 672 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 673 | if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 674 | GrMatrix m; |
| 675 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 676 | m.setTranslate(SkIntToScalar(-resultBounds->fLeft), |
| 677 | SkIntToScalar(-resultBounds->fTop)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 678 | |
| 679 | drawState->preConcatViewMatrix(m); |
| 680 | } |
| 681 | |
| 682 | } else { |
| 683 | // all the remaining ops can just be directly draw into |
| 684 | // the accumulation buffer |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 685 | setup_boolean_blendcoeffs(drawState, op); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 686 | this->drawClipShape(gpu, accum, clipIn, c); |
| 687 | } |
| 688 | } |
| 689 | |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 690 | *result = accum; |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 691 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 692 | return true; |
| 693 | } |
| 694 | |
| 695 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 696 | // Create a 1-bit clip mask in the stencil buffer |
| 697 | bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu, |
| 698 | const GrClip& clipIn, |
| 699 | const GrRect& bounds, |
| 700 | ScissoringSettings* scissorSettings) { |
| 701 | |
| 702 | GrAssert(fClipMaskInStencil); |
| 703 | |
| 704 | GrDrawState* drawState = gpu->drawState(); |
| 705 | GrAssert(drawState->isClipState()); |
| 706 | |
| 707 | GrRenderTarget* rt = drawState->getRenderTarget(); |
| 708 | GrAssert(NULL != rt); |
| 709 | |
| 710 | // TODO: dynamically attach a SB when needed. |
| 711 | GrStencilBuffer* stencilBuffer = rt->getStencilBuffer(); |
| 712 | if (NULL == stencilBuffer) { |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) { |
| 717 | |
| 718 | stencilBuffer->setLastClip(clipIn, rt->width(), rt->height()); |
| 719 | |
| 720 | // we set the current clip to the bounds so that our recursive |
| 721 | // draws are scissored to them. We use the copy of the complex clip |
| 722 | // we just stashed on the SB to render from. We set it back after |
| 723 | // we finish drawing it into the stencil. |
| 724 | const GrClip& clipCopy = stencilBuffer->getLastClip(); |
| 725 | gpu->setClip(GrClip(bounds)); |
| 726 | |
| 727 | GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit); |
| 728 | drawState = gpu->drawState(); |
| 729 | drawState->setRenderTarget(rt); |
| 730 | GrDrawTarget::AutoGeometryPush agp(gpu); |
| 731 | |
| 732 | gpu->disableScissor(); |
| 733 | #if !VISUALIZE_COMPLEX_CLIP |
| 734 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
| 735 | #endif |
| 736 | |
| 737 | int count = clipCopy.getElementCount(); |
| 738 | int clipBit = stencilBuffer->bits(); |
| 739 | SkASSERT((clipBit <= 16) && |
| 740 | "Ganesh only handles 16b or smaller stencil buffers"); |
| 741 | clipBit = (1 << (clipBit-1)); |
| 742 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 743 | GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 744 | |
| 745 | bool clearToInside; |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 746 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 747 | int start = process_initial_clip_elements(clipCopy, |
| 748 | rtRect, |
| 749 | &clearToInside, |
| 750 | &startOp); |
| 751 | |
| 752 | gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside); |
| 753 | |
| 754 | // walk through each clip element and perform its set op |
| 755 | // with the existing clip. |
| 756 | for (int c = start; c < count; ++c) { |
| 757 | GrPathFill fill; |
| 758 | bool fillInverted; |
| 759 | // enabled at bottom of loop |
| 760 | drawState->disableState(GrGpu::kModifyStencilClip_StateBit); |
| 761 | |
| 762 | bool canRenderDirectToStencil; // can the clip element be drawn |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 763 | // directly to the stencil buffer |
| 764 | // with a non-inverted fill rule |
| 765 | // without extra passes to |
| 766 | // resolve in/out status. |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 767 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 768 | SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c); |
| 769 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 770 | GrPathRenderer* pr = NULL; |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 771 | const SkPath* clipPath = NULL; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 772 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 773 | canRenderDirectToStencil = true; |
| 774 | fill = kEvenOdd_PathFill; |
| 775 | fillInverted = false; |
| 776 | // there is no point in intersecting a screen filling |
| 777 | // rectangle. |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 778 | if (SkRegion::kIntersect_Op == op && |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 779 | contains(clipCopy.getRect(c), rtRect)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 780 | continue; |
| 781 | } |
| 782 | } else { |
| 783 | fill = clipCopy.getPathFill(c); |
| 784 | fillInverted = GrIsFillInverted(fill); |
| 785 | fill = GrNonInvertedFill(fill); |
| 786 | clipPath = &clipCopy.getPath(c); |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 787 | pr = this->getContext()->getPathRenderer(*clipPath, |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 788 | fill, gpu, false, |
| 789 | true); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 790 | if (NULL == pr) { |
| 791 | fClipMaskInStencil = false; |
| 792 | gpu->setClip(clipCopy); // restore to the original |
| 793 | return false; |
| 794 | } |
| 795 | canRenderDirectToStencil = |
| 796 | !pr->requiresStencilPass(*clipPath, fill, gpu); |
| 797 | } |
| 798 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 799 | int passes; |
| 800 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 801 | |
| 802 | bool canDrawDirectToClip; // Given the renderer, the element, |
| 803 | // fill rule, and set operation can |
| 804 | // we render the element directly to |
| 805 | // stencil bit used for clipping. |
| 806 | canDrawDirectToClip = |
| 807 | GrStencilSettings::GetClipPasses(op, |
| 808 | canRenderDirectToStencil, |
| 809 | clipBit, |
| 810 | fillInverted, |
| 811 | &passes, stencilSettings); |
| 812 | |
| 813 | // draw the element to the client stencil bits if necessary |
| 814 | if (!canDrawDirectToClip) { |
| 815 | GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil, |
| 816 | kIncClamp_StencilOp, |
| 817 | kIncClamp_StencilOp, |
| 818 | kAlways_StencilFunc, |
| 819 | 0xffff, |
| 820 | 0x0000, |
| 821 | 0xffff); |
| 822 | SET_RANDOM_COLOR |
| 823 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 824 | *drawState->stencil() = gDrawToStencil; |
| 825 | gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0); |
| 826 | } else { |
| 827 | if (canRenderDirectToStencil) { |
| 828 | *drawState->stencil() = gDrawToStencil; |
| 829 | pr->drawPath(*clipPath, fill, NULL, gpu, 0, false); |
| 830 | } else { |
| 831 | pr->drawPathToStencil(*clipPath, fill, gpu); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // now we modify the clip bit by rendering either the clip |
| 837 | // element directly or a bounding rect of the entire clip. |
| 838 | drawState->enableState(GrGpu::kModifyStencilClip_StateBit); |
| 839 | for (int p = 0; p < passes; ++p) { |
| 840 | *drawState->stencil() = stencilSettings[p]; |
| 841 | if (canDrawDirectToClip) { |
| 842 | if (kRect_ClipType == clipCopy.getElementType(c)) { |
| 843 | SET_RANDOM_COLOR |
| 844 | gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0); |
| 845 | } else { |
| 846 | SET_RANDOM_COLOR |
| 847 | pr->drawPath(*clipPath, fill, NULL, gpu, 0, false); |
| 848 | } |
| 849 | } else { |
| 850 | SET_RANDOM_COLOR |
| 851 | gpu->drawSimpleRect(bounds, NULL, 0); |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | // restore clip |
| 856 | gpu->setClip(clipCopy); |
| 857 | // recusive draws would have disabled this since they drew with |
| 858 | // the clip bounds as clip. |
| 859 | fClipMaskInStencil = true; |
| 860 | } |
| 861 | |
| 862 | return true; |
| 863 | } |
| 864 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 865 | namespace { |
| 866 | |
| 867 | GrPathFill invert_fill(GrPathFill fill) { |
| 868 | static const GrPathFill gInvertedFillTable[] = { |
| 869 | kInverseWinding_PathFill, // kWinding_PathFill |
| 870 | kInverseEvenOdd_PathFill, // kEvenOdd_PathFill |
| 871 | kWinding_PathFill, // kInverseWinding_PathFill |
| 872 | kEvenOdd_PathFill, // kInverseEvenOdd_PathFill |
| 873 | kHairLine_PathFill, // kHairLine_PathFill |
| 874 | }; |
| 875 | GR_STATIC_ASSERT(0 == kWinding_PathFill); |
| 876 | GR_STATIC_ASSERT(1 == kEvenOdd_PathFill); |
| 877 | GR_STATIC_ASSERT(2 == kInverseWinding_PathFill); |
| 878 | GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill); |
| 879 | GR_STATIC_ASSERT(4 == kHairLine_PathFill); |
| 880 | GR_STATIC_ASSERT(5 == kPathFillCount); |
| 881 | return gInvertedFillTable[fill]; |
| 882 | } |
| 883 | |
| 884 | } |
| 885 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 886 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 887 | bool GrClipMaskManager::createSoftwareClipMask(GrGpu* gpu, |
| 888 | const GrClip& clipIn, |
| 889 | GrTexture** result, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 890 | GrIRect *resultBounds) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 891 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 892 | if (this->clipMaskPreamble(gpu, clipIn, result, resultBounds)) { |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 893 | return true; |
| 894 | } |
| 895 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 896 | GrTexture* accum = fAACache.getLastMask(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 897 | if (NULL == accum) { |
| 898 | fClipMaskInAlpha = false; |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 899 | fAACache.reset(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 900 | return false; |
| 901 | } |
| 902 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 903 | GrSWMaskHelper helper(this->getContext()); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 904 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 905 | helper.init(*resultBounds, NULL, false); |
| 906 | |
| 907 | int count = clipIn.getElementCount(); |
| 908 | |
| 909 | bool clearToInside; |
| 910 | SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning |
| 911 | int start = process_initial_clip_elements(clipIn, |
| 912 | *resultBounds, |
| 913 | &clearToInside, |
| 914 | &startOp); |
| 915 | |
| 916 | helper.clear(clearToInside ? SK_ColorWHITE : 0x00000000); |
| 917 | |
| 918 | for (int i = start; i < count; ++i) { |
| 919 | |
| 920 | SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i); |
| 921 | |
| 922 | if (SkRegion::kIntersect_Op == op || |
| 923 | SkRegion::kReverseDifference_Op == op) { |
| 924 | // Intersect and reverse difference require modifying pixels |
| 925 | // outside of the geometry that is being "drawn". In both cases |
| 926 | // we erase all the pixels outside of the geometry but |
| 927 | // leave the pixels inside the geometry alone. For reverse |
| 928 | // difference we invert all the pixels before clearing the ones |
| 929 | // outside the geometry. |
| 930 | if (SkRegion::kReverseDifference_Op == op) { |
| 931 | SkRect temp = SkRect::MakeLTRB( |
| 932 | SkIntToScalar(resultBounds->left()), |
| 933 | SkIntToScalar(resultBounds->top()), |
| 934 | SkIntToScalar(resultBounds->right()), |
| 935 | SkIntToScalar(resultBounds->bottom())); |
| 936 | |
| 937 | // invert the entire scene |
| 938 | helper.draw(temp, SkRegion::kXOR_Op, false, SK_ColorWHITE); |
| 939 | } |
| 940 | |
| 941 | if (kRect_ClipType == clipIn.getElementType(i)) { |
| 942 | |
| 943 | // convert the rect to a path so we can invert the fill |
| 944 | SkPath temp; |
| 945 | temp.addRect(clipIn.getRect(i)); |
| 946 | |
| 947 | helper.draw(temp, SkRegion::kReplace_Op, |
| 948 | kInverseEvenOdd_PathFill, clipIn.getDoAA(i), |
| 949 | 0x00000000); |
| 950 | } else { |
| 951 | GrAssert(kPath_ClipType == clipIn.getElementType(i)); |
| 952 | |
| 953 | helper.draw(clipIn.getPath(i), |
| 954 | SkRegion::kReplace_Op, |
| 955 | invert_fill(clipIn.getPathFill(i)), |
| 956 | clipIn.getDoAA(i), |
| 957 | 0x00000000); |
| 958 | } |
| 959 | |
| 960 | continue; |
| 961 | } |
| 962 | |
| 963 | // The other ops (union, xor, diff) only affect pixels inside |
| 964 | // the geometry so they can just be drawn normally |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 965 | if (kRect_ClipType == clipIn.getElementType(i)) { |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 966 | |
| 967 | helper.draw(clipIn.getRect(i), |
| 968 | op, |
| 969 | clipIn.getDoAA(i), SK_ColorWHITE); |
| 970 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 971 | } else { |
| 972 | GrAssert(kPath_ClipType == clipIn.getElementType(i)); |
| 973 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 974 | helper.draw(clipIn.getPath(i), |
| 975 | op, |
| 976 | clipIn.getPathFill(i), |
| 977 | clipIn.getDoAA(i), SK_ColorWHITE); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 981 | // Because we are using the scratch texture cache, "accum" may be |
| 982 | // larger than expected and have some cruft in the areas we aren't using. |
| 983 | // Clear it out. |
| 984 | |
| 985 | // TODO: need a simpler way to clear the texture - can we combine |
| 986 | // the clear and the writePixels (inside toTexture) |
| 987 | GrDrawState* drawState = gpu->drawState(); |
| 988 | GrAssert(NULL != drawState); |
| 989 | GrRenderTarget* temp = drawState->getRenderTarget(); |
| 990 | clear(gpu, accum, 0x00000000); |
| 991 | // can't leave the accum bound as a rendertarget |
| 992 | drawState->setRenderTarget(temp); |
| 993 | |
| 994 | helper.toTexture(accum); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 995 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 996 | *result = accum; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 997 | |
| 998 | return true; |
| 999 | } |
| 1000 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1001 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1002 | void GrClipMaskManager::releaseResources() { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 1003 | fAACache.releaseResources(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1004 | } |