GrClipMaskManager.h revision d21444aab7128c97f4e0eb5e9bf05111d5037292
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 "GrContext.h"
13#include "GrNoncopyable.h"
14#include "GrRect.h"
15#include "GrReducedClip.h"
16#include "GrStencil.h"
17#include "GrTexture.h"
18
19#include "SkClipStack.h"
20#include "SkDeque.h"
21#include "SkPath.h"
22#include "SkRefCnt.h"
23#include "SkTLList.h"
24
25#include "GrClipMaskCache.h"
26
27class GrGpu;
28class GrPathRenderer;
29class GrPathRendererChain;
30class SkPath;
31class GrTexture;
32class GrDrawState;
33
34/**
35 * The clip mask creator handles the generation of the clip mask. If anti
36 * aliasing is requested it will (in the future) generate a single channel
37 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
38 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
39 * mask can be represented as a rectangle then scissoring is used. In all
40 * cases scissoring is used to bound the range of the clip mask.
41 */
42class GrClipMaskManager : public GrNoncopyable {
43public:
44    GR_DECLARE_RESOURCE_CACHE_DOMAIN(GetAlphaMaskDomain)
45
46    GrClipMaskManager()
47        : fGpu(NULL)
48        , fCurrClipMaskType(kNone_ClipMaskType) {
49    }
50
51    /**
52     * Creates a clip mask if necessary as a stencil buffer or alpha texture
53     * and sets the GrGpu's scissor and stencil state. If the return is false
54     * then the draw can be skipped.
55     */
56    bool setupClipping(const GrClipData* clipDataIn);
57
58    void releaseResources();
59
60    bool isClipInStencil() const {
61        return kStencil_ClipMaskType == fCurrClipMaskType;
62    }
63    bool isClipInAlpha() const {
64        return kAlpha_ClipMaskType == fCurrClipMaskType;
65    }
66
67    void invalidateStencilMask() {
68        if (kStencil_ClipMaskType == fCurrClipMaskType) {
69            fCurrClipMaskType = kNone_ClipMaskType;
70        }
71    }
72
73    void setContext(GrContext* context) {
74        fAACache.setContext(context);
75    }
76
77    GrContext* getContext() {
78        return fAACache.getContext();
79    }
80
81    void setGpu(GrGpu* gpu) {
82        fGpu = gpu;
83    }
84
85private:
86    /**
87     * Informs the helper function adjustStencilParams() about how the stencil
88     * buffer clip is being used.
89     */
90    enum StencilClipMode {
91        // Draw to the clip bit of the stencil buffer
92        kModifyClip_StencilClipMode,
93        // Clip against the existing representation of the clip in the high bit
94        // of the stencil buffer.
95        kRespectClip_StencilClipMode,
96        // Neither writing to nor clipping against the clip bit.
97        kIgnoreClip_StencilClipMode,
98    };
99
100    GrGpu* fGpu;
101
102    /**
103     * We may represent the clip as a mask in the stencil buffer or as an alpha
104     * texture. It may be neither because the scissor rect suffices or we
105     * haven't yet examined the clip.
106     */
107    enum ClipMaskType {
108        kNone_ClipMaskType,
109        kStencil_ClipMaskType,
110        kAlpha_ClipMaskType,
111    } fCurrClipMaskType;
112
113    GrClipMaskCache fAACache;       // cache for the AA path
114
115    // Draws the clip into the stencil buffer
116    bool createStencilClipMask(GrReducedClip::InitialState initialState,
117                               const GrReducedClip::ElementList& elements,
118                               const SkIRect& clipSpaceIBounds,
119                               const SkIPoint& clipSpaceToStencilOffset);
120    // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
121    // rect specified by clipSpaceIBounds.
122    GrTexture* createAlphaClipMask(int32_t clipStackGenID,
123                                   GrReducedClip::InitialState initialState,
124                                   const GrReducedClip::ElementList& elements,
125                                   const SkIRect& clipSpaceIBounds);
126    // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
127    GrTexture* createSoftwareClipMask(int32_t clipStackGenID,
128                                      GrReducedClip::InitialState initialState,
129                                      const GrReducedClip::ElementList& elements,
130                                      const SkIRect& clipSpaceIBounds);
131
132    // Gets a texture to use for the clip mask. If true is returned then a cached mask was found
133    // that already contains the rasterization of the clip stack, otherwise an uninitialized texture
134    // is returned.
135    bool getMaskTexture(int32_t clipStackGenID,
136                        const SkIRect& clipSpaceIBounds,
137                        GrTexture** result);
138
139    bool useSWOnlyPath(const GrReducedClip::ElementList& elements);
140
141    bool drawClipShape(GrTexture* target, const SkClipStack::Element* element);
142
143    void mergeMask(GrTexture* dstMask,
144                   GrTexture* srcMask,
145                   SkRegion::Op op,
146                   const GrIRect& dstBound,
147                   const GrIRect& srcBound);
148
149    void getTemp(int width, int height, GrAutoScratchTexture* temp);
150
151    void setupCache(const SkClipStack& clip,
152                    const GrIRect& bounds);
153
154    /**
155     * Called prior to return control back the GrGpu in setupClipping. It
156     * updates the GrGpu with stencil settings that account stencil-based
157     * clipping.
158     */
159    void setGpuStencil();
160
161    /**
162     * Adjusts the stencil settings to account for interaction with stencil
163     * clipping.
164     */
165    void adjustStencilParams(GrStencilSettings* settings,
166                             StencilClipMode mode,
167                             int stencilBitCnt);
168
169    typedef GrNoncopyable INHERITED;
170};
171
172#endif // GrClipMaskManager_DEFINED
173