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 | |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 71 | if (back->fLastMask && |
| 72 | back->fLastMask->width() >= width && |
| 73 | back->fLastMask->height() >= height && |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 74 | clip == back->fLastClip) { |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 81 | void set(const GrClip& clip, GrTexture* mask, const GrRect& bound) { |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 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 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 90 | back->fLastClip = clip; |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 91 | SkSafeRef(mask); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 92 | back->fLastMask.reset(mask); |
| 93 | back->fLastBound = bound; |
| 94 | } |
| 95 | |
| 96 | void reset() { |
| 97 | if (fStack.empty()) { |
| 98 | GrAssert(false); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 103 | |
| 104 | back->reset(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * After a "push" the clip state is entirely open. Currently, the |
| 109 | * entire clip stack will be re-rendered into a new clip mask. |
| 110 | * TODO: can we take advantage of the nested nature of the clips to |
| 111 | * reduce the mask creation cost? |
| 112 | */ |
| 113 | void push() { |
| 114 | new (fStack.push_back()) GrClipStackFrame(); |
| 115 | } |
| 116 | |
| 117 | void pop() { |
| 118 | GrAssert(!fStack.empty()); |
| 119 | |
| 120 | if (!fStack.empty()) { |
| 121 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 122 | |
| 123 | back->~GrClipStackFrame(); |
| 124 | fStack.pop_back(); |
| 125 | } |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 126 | } |
| 127 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 128 | void getLastClip(GrClip* clip) const { |
| 129 | |
| 130 | if (fStack.empty()) { |
| 131 | GrAssert(false); |
| 132 | clip->setEmpty(); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 137 | |
| 138 | *clip = back->fLastClip; |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | GrTexture* getLastMask() { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 142 | |
| 143 | if (fStack.empty()) { |
| 144 | GrAssert(false); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 149 | |
| 150 | return back->fLastMask.get(); |
| 151 | } |
| 152 | |
| 153 | const GrTexture* getLastMask() const { |
| 154 | |
| 155 | if (fStack.empty()) { |
| 156 | GrAssert(false); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 161 | |
| 162 | return back->fLastMask.get(); |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 163 | } |
| 164 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 165 | GrTexture* detachLastMask() { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 166 | |
| 167 | if (fStack.empty()) { |
| 168 | GrAssert(false); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 173 | |
| 174 | return back->fLastMask.detach(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | int getLastMaskWidth() const { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 178 | |
| 179 | if (fStack.empty()) { |
| 180 | GrAssert(false); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 181 | return -1; |
| 182 | } |
| 183 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 184 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 185 | |
| 186 | if (NULL == back->fLastMask.get()) { |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | return back->fLastMask.get()->width(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | int getLastMaskHeight() const { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 194 | |
| 195 | if (fStack.empty()) { |
| 196 | GrAssert(false); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 197 | return -1; |
| 198 | } |
| 199 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 200 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 201 | |
| 202 | if (NULL == back->fLastMask.get()) { |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | return back->fLastMask.get()->height(); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 207 | } |
| 208 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 209 | void getLastBound(GrRect* bound) const { |
| 210 | |
| 211 | if (fStack.empty()) { |
| 212 | GrAssert(false); |
| 213 | bound->setEmpty(); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 218 | |
| 219 | *bound = back->fLastBound; |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | protected: |
| 223 | private: |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 224 | struct GrClipStackFrame { |
| 225 | |
| 226 | GrClipStackFrame() { |
| 227 | reset(); |
| 228 | } |
| 229 | |
| 230 | void reset () { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 231 | fLastClip.setEmpty(); |
| 232 | fLastMask.reset(NULL); |
| 233 | fLastBound.setEmpty(); |
| 234 | } |
| 235 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 236 | GrClip fLastClip; |
| 237 | // The mask's width & height values are used in setupDrawStateAAClip to |
| 238 | // correctly scale the uvs for geometry drawn with this mask |
| 239 | SkAutoTUnref<GrTexture> fLastMask; |
| 240 | // fLastBound stores the bounding box of the clip mask in canvas |
| 241 | // space. The left and top fields are used to offset the uvs for |
| 242 | // geometry drawn with this mask (in setupDrawStateAAClip) |
| 243 | GrRect fLastBound; |
| 244 | }; |
| 245 | |
| 246 | SkDeque fStack; |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 247 | |
| 248 | typedef GrNoncopyable INHERITED; |
| 249 | }; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * The clip mask creator handles the generation of the clip mask. If anti |
| 253 | * aliasing is requested it will (in the future) generate a single channel |
| 254 | * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit |
| 255 | * mask in the stencil buffer. In the non anti-aliasing case, if the clip |
| 256 | * mask can be represented as a rectangle then scissoring is used. In all |
| 257 | * cases scissoring is used to bound the range of the clip mask. |
| 258 | */ |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 259 | class GrClipMaskManager : public GrNoncopyable { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 260 | public: |
| 261 | GrClipMaskManager() |
| 262 | : fClipMaskInStencil(false) |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 263 | , fClipMaskInAlpha(false) |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 264 | , fPathRendererChain(NULL) { |
| 265 | } |
| 266 | |
| 267 | bool createClipMask(GrGpu* gpu, |
| 268 | const GrClip& clip, |
| 269 | ScissoringSettings* scissorSettings); |
| 270 | |
| 271 | void freeResources(); |
| 272 | |
| 273 | bool isClipInStencil() const { return fClipMaskInStencil; } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 274 | bool isClipInAlpha() const { return fClipMaskInAlpha; } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 275 | |
| 276 | void resetMask() { |
| 277 | fClipMaskInStencil = false; |
| 278 | } |
| 279 | |
| 280 | protected: |
| 281 | private: |
| 282 | bool fClipMaskInStencil; // is the clip mask in the stencil buffer? |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 283 | bool fClipMaskInAlpha; // is the clip mask in an alpha texture? |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 284 | GrClipMaskCache fAACache; // cache for the AA path |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 285 | |
| 286 | // must be instantiated after GrGpu object has been given its owning |
| 287 | // GrContext ptr. (GrGpu is constructed first then handed off to GrContext). |
| 288 | GrPathRendererChain* fPathRendererChain; |
| 289 | |
| 290 | bool createStencilClipMask(GrGpu* gpu, |
| 291 | const GrClip& clip, |
| 292 | const GrRect& bounds, |
| 293 | ScissoringSettings* scissorSettings); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 294 | bool createAlphaClipMask(GrGpu* gpu, |
| 295 | const GrClip& clipIn, |
| 296 | GrTexture** result, |
| 297 | GrRect *resultBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 298 | |
| 299 | bool drawPath(GrGpu* gpu, |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 300 | const SkPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 301 | GrPathFill fill, |
| 302 | bool doAA); |
| 303 | |
| 304 | bool drawClipShape(GrGpu* gpu, |
| 305 | GrTexture* target, |
| 306 | const GrClip& clipIn, |
| 307 | int index); |
| 308 | |
| 309 | void drawTexture(GrGpu* gpu, |
| 310 | GrTexture* target, |
| 311 | const GrRect& rect, |
| 312 | GrTexture* texture); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 313 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 314 | void getAccum(GrGpu* gpu, |
| 315 | const GrTextureDesc& desc, |
| 316 | GrTexture** accum); |
| 317 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 318 | // determines the path renderer used to draw a clip path element. |
| 319 | GrPathRenderer* getClipPathRenderer(GrGpu* gpu, |
| 320 | const SkPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 321 | GrPathFill fill, |
| 322 | bool antiAlias); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 323 | |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 324 | typedef GrNoncopyable INHERITED; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | #endif // GrClipMaskManager_DEFINED |