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 | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 17 | |
| 18 | class GrGpu; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 19 | class GrPathRenderer; |
| 20 | class GrPathRendererChain; |
| 21 | class SkPath; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 22 | class GrTexture; |
| 23 | class GrDrawState; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Scissoring needs special handling during stencil clip mask creation |
| 27 | * since the creation process re-entrantly invokes setupClipAndFlushState. |
| 28 | * During this process the call stack is used to keep |
| 29 | * track of (and apply to the GPU) the current scissor settings. |
| 30 | */ |
| 31 | struct ScissoringSettings { |
| 32 | bool fEnableScissoring; |
| 33 | GrIRect fScissorRect; |
| 34 | |
| 35 | void setupScissoring(GrGpu* gpu); |
| 36 | }; |
| 37 | |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame^] | 38 | /** |
| 39 | * The stencil buffer stores the last clip path - providing a single entry |
| 40 | * "cache". This class provides similar functionality for AA clip paths |
| 41 | */ |
| 42 | class GrClipMaskCache : public GrNoncopyable { |
| 43 | public: |
| 44 | GrClipMaskCache() |
| 45 | : fLastWidth(-1) |
| 46 | , fLastHeight(-1) { |
| 47 | fLastBound.MakeEmpty(); |
| 48 | } |
| 49 | |
| 50 | bool canReuse(const GrClip& clip, int width, int height) { |
| 51 | if (fLastWidth >= width && |
| 52 | fLastHeight >= height && |
| 53 | clip == fLastClip) { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | void set(const GrClip& clip, int width, int height, |
| 61 | GrTexture* mask, const GrRect& bound) { |
| 62 | |
| 63 | fLastWidth = width; |
| 64 | fLastHeight = height; |
| 65 | fLastClip = clip; |
| 66 | SkSafeRef(mask); |
| 67 | fLastMask.reset(mask); |
| 68 | fLastBound = bound; |
| 69 | } |
| 70 | |
| 71 | GrTexture* getLastMask() { |
| 72 | return fLastMask.get(); |
| 73 | } |
| 74 | |
| 75 | const GrRect& getLastBound() const { |
| 76 | return fLastBound; |
| 77 | } |
| 78 | |
| 79 | protected: |
| 80 | private: |
| 81 | int fLastWidth; |
| 82 | int fLastHeight; |
| 83 | GrClip fLastClip; |
| 84 | SkAutoTUnref<GrTexture> fLastMask; |
| 85 | GrRect fLastBound; |
| 86 | |
| 87 | typedef GrNoncopyable INHERITED; |
| 88 | }; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * The clip mask creator handles the generation of the clip mask. If anti |
| 92 | * aliasing is requested it will (in the future) generate a single channel |
| 93 | * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit |
| 94 | * mask in the stencil buffer. In the non anti-aliasing case, if the clip |
| 95 | * mask can be represented as a rectangle then scissoring is used. In all |
| 96 | * cases scissoring is used to bound the range of the clip mask. |
| 97 | */ |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame^] | 98 | class GrClipMaskManager : public GrNoncopyable { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 99 | public: |
| 100 | GrClipMaskManager() |
| 101 | : fClipMaskInStencil(false) |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 102 | , fClipMaskInAlpha(false) |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 103 | , fPathRendererChain(NULL) { |
| 104 | } |
| 105 | |
| 106 | bool createClipMask(GrGpu* gpu, |
| 107 | const GrClip& clip, |
| 108 | ScissoringSettings* scissorSettings); |
| 109 | |
| 110 | void freeResources(); |
| 111 | |
| 112 | bool isClipInStencil() const { return fClipMaskInStencil; } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 113 | bool isClipInAlpha() const { return fClipMaskInAlpha; } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 114 | |
| 115 | void resetMask() { |
| 116 | fClipMaskInStencil = false; |
| 117 | } |
| 118 | |
| 119 | protected: |
| 120 | private: |
| 121 | bool fClipMaskInStencil; // is the clip mask in the stencil buffer? |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 122 | bool fClipMaskInAlpha; // is the clip mask in an alpha texture? |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame^] | 123 | GrClipMaskCache fAACache; // cache for the AA path |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 124 | |
| 125 | // must be instantiated after GrGpu object has been given its owning |
| 126 | // GrContext ptr. (GrGpu is constructed first then handed off to GrContext). |
| 127 | GrPathRendererChain* fPathRendererChain; |
| 128 | |
| 129 | bool createStencilClipMask(GrGpu* gpu, |
| 130 | const GrClip& clip, |
| 131 | const GrRect& bounds, |
| 132 | ScissoringSettings* scissorSettings); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 133 | bool createAlphaClipMask(GrGpu* gpu, |
| 134 | const GrClip& clipIn, |
| 135 | GrTexture** result, |
| 136 | GrRect *resultBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 137 | |
| 138 | bool drawPath(GrGpu* gpu, |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 139 | const SkPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 140 | GrPathFill fill, |
| 141 | bool doAA); |
| 142 | |
| 143 | bool drawClipShape(GrGpu* gpu, |
| 144 | GrTexture* target, |
| 145 | const GrClip& clipIn, |
| 146 | int index); |
| 147 | |
| 148 | void drawTexture(GrGpu* gpu, |
| 149 | GrTexture* target, |
| 150 | const GrRect& rect, |
| 151 | GrTexture* texture); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 152 | |
| 153 | // determines the path renderer used to draw a clip path element. |
| 154 | GrPathRenderer* getClipPathRenderer(GrGpu* gpu, |
| 155 | const SkPath& path, |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 156 | GrPathFill fill, |
| 157 | bool antiAlias); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 158 | |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame^] | 159 | typedef GrNoncopyable INHERITED; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | #endif // GrClipMaskManager_DEFINED |