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 | #ifndef GrClipMaskManager_DEFINED |
| 10 | #define GrClipMaskManager_DEFINED |
| 11 | |
| 12 | #include "GrRect.h" |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 13 | #include "SkPath.h" |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 14 | #include "GrNoncopyable.h" |
| 15 | #include "GrClip.h" |
| 16 | #include "SkRefCnt.h" |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 17 | #include "GrTexture.h" |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 18 | #include "SkDeque.h" |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 19 | |
| 20 | class GrGpu; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 21 | class GrPathRenderer; |
| 22 | class GrPathRendererChain; |
| 23 | class SkPath; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 24 | class GrTexture; |
| 25 | class GrDrawState; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Scissoring needs special handling during stencil clip mask creation |
| 29 | * since the creation process re-entrantly invokes setupClipAndFlushState. |
| 30 | * During this process the call stack is used to keep |
| 31 | * track of (and apply to the GPU) the current scissor settings. |
| 32 | */ |
| 33 | struct ScissoringSettings { |
| 34 | bool fEnableScissoring; |
| 35 | GrIRect fScissorRect; |
| 36 | |
| 37 | void setupScissoring(GrGpu* gpu); |
| 38 | }; |
| 39 | |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 40 | /** |
| 41 | * The stencil buffer stores the last clip path - providing a single entry |
| 42 | * "cache". This class provides similar functionality for AA clip paths |
| 43 | */ |
| 44 | class GrClipMaskCache : public GrNoncopyable { |
| 45 | public: |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 46 | GrClipMaskCache() |
| 47 | : fStack(sizeof(GrClipStackFrame)) { |
| 48 | // We need an initial frame to capture the clip state prior to |
| 49 | // any pushes |
| 50 | new (fStack.push_back()) GrClipStackFrame(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 53 | ~GrClipMaskCache() { |
| 54 | |
| 55 | while (!fStack.empty()) { |
| 56 | GrClipStackFrame* temp = (GrClipStackFrame*) fStack.back(); |
| 57 | temp->~GrClipStackFrame(); |
| 58 | fStack.pop_back(); |
| 59 | } |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool canReuse(const GrClip& clip, int width, int height) { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 63 | |
| 64 | if (fStack.empty()) { |
| 65 | GrAssert(false); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 70 | |
| 71 | if (back->fLastWidth >= width && |
| 72 | back->fLastHeight >= height && |
| 73 | clip == back->fLastClip) { |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | void set(const GrClip& clip, int width, int height, |
| 81 | GrTexture* mask, const GrRect& bound) { |
| 82 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 83 | if (fStack.empty()) { |
| 84 | GrAssert(false); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 89 | |
| 90 | back->fLastWidth = width; |
| 91 | back->fLastHeight = height; |
| 92 | back->fLastClip = clip; |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 93 | SkSafeRef(mask); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 94 | back->fLastMask.reset(mask); |
| 95 | back->fLastBound = bound; |
| 96 | } |
| 97 | |
| 98 | void reset() { |
| 99 | if (fStack.empty()) { |
| 100 | GrAssert(false); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 105 | |
| 106 | back->reset(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * After a "push" the clip state is entirely open. Currently, the |
| 111 | * entire clip stack will be re-rendered into a new clip mask. |
| 112 | * TODO: can we take advantage of the nested nature of the clips to |
| 113 | * reduce the mask creation cost? |
| 114 | */ |
| 115 | void push() { |
| 116 | new (fStack.push_back()) GrClipStackFrame(); |
| 117 | } |
| 118 | |
| 119 | void pop() { |
| 120 | GrAssert(!fStack.empty()); |
| 121 | |
| 122 | if (!fStack.empty()) { |
| 123 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 124 | |
| 125 | back->~GrClipStackFrame(); |
| 126 | fStack.pop_back(); |
| 127 | } |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 128 | } |
| 129 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 130 | int getLastWidth() const { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 131 | |
| 132 | if (fStack.empty()) { |
| 133 | GrAssert(false); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 138 | |
| 139 | return back->fLastWidth; |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | int getLastHeight() const { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 143 | |
| 144 | if (fStack.empty()) { |
| 145 | GrAssert(false); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 150 | |
| 151 | return back->fLastHeight; |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 152 | } |
| 153 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 154 | void getLastClip(GrClip* clip) const { |
| 155 | |
| 156 | if (fStack.empty()) { |
| 157 | GrAssert(false); |
| 158 | clip->setEmpty(); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 163 | |
| 164 | *clip = back->fLastClip; |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | GrTexture* getLastMask() { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 168 | |
| 169 | if (fStack.empty()) { |
| 170 | GrAssert(false); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 175 | |
| 176 | return back->fLastMask.get(); |
| 177 | } |
| 178 | |
| 179 | const GrTexture* getLastMask() const { |
| 180 | |
| 181 | if (fStack.empty()) { |
| 182 | GrAssert(false); |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 187 | |
| 188 | return back->fLastMask.get(); |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 189 | } |
| 190 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 191 | GrTexture* detachLastMask() { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 192 | |
| 193 | if (fStack.empty()) { |
| 194 | GrAssert(false); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 199 | |
| 200 | return back->fLastMask.detach(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | int getLastMaskWidth() const { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 204 | |
| 205 | if (fStack.empty()) { |
| 206 | GrAssert(false); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 210 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 211 | |
| 212 | if (NULL == back->fLastMask.get()) { |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | return back->fLastMask.get()->width(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | int getLastMaskHeight() const { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 220 | |
| 221 | if (fStack.empty()) { |
| 222 | GrAssert(false); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 223 | return -1; |
| 224 | } |
| 225 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 226 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 227 | |
| 228 | if (NULL == back->fLastMask.get()) { |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | return back->fLastMask.get()->height(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 233 | } |
| 234 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 235 | void getLastBound(GrRect* bound) const { |
| 236 | |
| 237 | if (fStack.empty()) { |
| 238 | GrAssert(false); |
| 239 | bound->setEmpty(); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 244 | |
| 245 | *bound = back->fLastBound; |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | protected: |
| 249 | private: |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame^] | 250 | struct GrClipStackFrame { |
| 251 | |
| 252 | GrClipStackFrame() { |
| 253 | reset(); |
| 254 | } |
| 255 | |
| 256 | void reset () { |
| 257 | fLastWidth = -1; |
| 258 | fLastHeight = -1; |
| 259 | fLastClip.setEmpty(); |
| 260 | fLastMask.reset(NULL); |
| 261 | fLastBound.setEmpty(); |
| 262 | } |
| 263 | |
| 264 | // fLastWidth & fLastHeight store the render target size used when |
| 265 | // creating the mask. They factor into the reuse decision (in canReuse) |
| 266 | // TODO: We should probably use the mask's width & height rather than |
| 267 | // the render target's width & height for reuse decisions |
| 268 | int fLastWidth; |
| 269 | int fLastHeight; |
| 270 | GrClip fLastClip; |
| 271 | // The mask's width & height values are used in setupDrawStateAAClip to |
| 272 | // correctly scale the uvs for geometry drawn with this mask |
| 273 | SkAutoTUnref<GrTexture> fLastMask; |
| 274 | // fLastBound stores the bounding box of the clip mask in canvas |
| 275 | // space. The left and top fields are used to offset the uvs for |
| 276 | // geometry drawn with this mask (in setupDrawStateAAClip) |
| 277 | GrRect fLastBound; |
| 278 | }; |
| 279 | |
| 280 | SkDeque fStack; |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 281 | |
| 282 | typedef GrNoncopyable INHERITED; |
| 283 | }; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * The clip mask creator handles the generation of the clip mask. If anti |
| 287 | * aliasing is requested it will (in the future) generate a single channel |
| 288 | * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit |
| 289 | * mask in the stencil buffer. In the non anti-aliasing case, if the clip |
| 290 | * mask can be represented as a rectangle then scissoring is used. In all |
| 291 | * cases scissoring is used to bound the range of the clip mask. |
| 292 | */ |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 293 | class GrClipMaskManager : public GrNoncopyable { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 294 | public: |
| 295 | GrClipMaskManager() |
| 296 | : fClipMaskInStencil(false) |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 297 | , fClipMaskInAlpha(false) |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 298 | , fPathRendererChain(NULL) { |
| 299 | } |
| 300 | |
| 301 | bool createClipMask(GrGpu* gpu, |
| 302 | const GrClip& clip, |
| 303 | ScissoringSettings* scissorSettings); |
| 304 | |
| 305 | void freeResources(); |
| 306 | |
| 307 | bool isClipInStencil() const { return fClipMaskInStencil; } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 308 | bool isClipInAlpha() const { return fClipMaskInAlpha; } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 309 | |
| 310 | void resetMask() { |
| 311 | fClipMaskInStencil = false; |
| 312 | } |
| 313 | |
| 314 | protected: |
| 315 | private: |
| 316 | bool fClipMaskInStencil; // is the clip mask in the stencil buffer? |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 317 | bool fClipMaskInAlpha; // is the clip mask in an alpha texture? |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 318 | GrClipMaskCache fAACache; // cache for the AA path |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 319 | |
| 320 | // must be instantiated after GrGpu object has been given its owning |
| 321 | // GrContext ptr. (GrGpu is constructed first then handed off to GrContext). |
| 322 | GrPathRendererChain* fPathRendererChain; |
| 323 | |
| 324 | bool createStencilClipMask(GrGpu* gpu, |
| 325 | const GrClip& clip, |
| 326 | const GrRect& bounds, |
| 327 | ScissoringSettings* scissorSettings); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 328 | bool createAlphaClipMask(GrGpu* gpu, |
| 329 | const GrClip& clipIn, |
| 330 | GrTexture** result, |
| 331 | GrRect *resultBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 332 | |
| 333 | bool drawPath(GrGpu* gpu, |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 334 | const SkPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 335 | GrPathFill fill, |
| 336 | bool doAA); |
| 337 | |
| 338 | bool drawClipShape(GrGpu* gpu, |
| 339 | GrTexture* target, |
| 340 | const GrClip& clipIn, |
| 341 | int index); |
| 342 | |
| 343 | void drawTexture(GrGpu* gpu, |
| 344 | GrTexture* target, |
| 345 | const GrRect& rect, |
| 346 | GrTexture* texture); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 347 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 348 | void getAccum(GrGpu* gpu, |
| 349 | const GrTextureDesc& desc, |
| 350 | GrTexture** accum); |
| 351 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 352 | // determines the path renderer used to draw a clip path element. |
| 353 | GrPathRenderer* getClipPathRenderer(GrGpu* gpu, |
| 354 | const SkPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 355 | GrPathFill fill, |
| 356 | bool antiAlias); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 357 | |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 358 | typedef GrNoncopyable INHERITED; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | #endif // GrClipMaskManager_DEFINED |