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