blob: d4e6a8d9ee25d351dc0da3a366b71b52431e9241 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
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
robertphillips@google.comf105b102012-05-14 12:18:26 +000012#include "GrContext.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000013#include "GrNoncopyable.h"
14#include "GrRect.h"
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000015#include "GrReducedClip.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000016#include "GrStencil.h"
17#include "GrTexture.h"
18
robertphillips@google.com641f8b12012-07-31 19:15:58 +000019#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000020#include "SkDeque.h"
21#include "SkPath.h"
22#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000023#include "SkTLList.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000024
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000025#include "GrClipMaskCache.h"
26
robertphillips@google.com1e945b72012-04-16 18:03:03 +000027class GrGpu;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000028class GrPathRenderer;
29class GrPathRendererChain;
30class SkPath;
robertphillips@google.comf294b772012-04-27 14:29:26 +000031class GrTexture;
32class GrDrawState;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000033
34/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000035 * 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
robertphillips@google.com1e945b72012-04-16 18:03:03 +000038 * 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 */
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000042class GrClipMaskManager : public GrNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000043public:
robertphillips@google.com46a86002012-08-08 10:42:44 +000044 GR_DECLARE_RESOURCE_CACHE_DOMAIN(GetAlphaMaskDomain)
45
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000046 GrClipMaskManager()
47 : fGpu(NULL)
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000048 , fCurrClipMaskType(kNone_ClipMaskType) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000049 }
50
bsalomon@google.coma3201942012-06-21 19:58:20 +000051 /**
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 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000056 bool setupClipping(const GrClipData* clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000057
robertphillips@google.comf105b102012-05-14 12:18:26 +000058 void releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +000059
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000060 bool isClipInStencil() const {
61 return kStencil_ClipMaskType == fCurrClipMaskType;
62 }
63 bool isClipInAlpha() const {
64 return kAlpha_ClipMaskType == fCurrClipMaskType;
65 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000066
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000067 void invalidateStencilMask() {
68 if (kStencil_ClipMaskType == fCurrClipMaskType) {
69 fCurrClipMaskType = kNone_ClipMaskType;
70 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000071 }
72
robertphillips@google.comf105b102012-05-14 12:18:26 +000073 void setContext(GrContext* context) {
74 fAACache.setContext(context);
75 }
76
robertphillips@google.com2c756812012-05-22 20:28:23 +000077 GrContext* getContext() {
78 return fAACache.getContext();
79 }
80
rmistry@google.comd6176b02012-08-23 18:14:13 +000081 void setGpu(GrGpu* gpu) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000082 fGpu = gpu;
83 }
84
bsalomon@google.coma3201942012-06-21 19:58:20 +000085private:
bsalomon@google.com411dad02012-06-05 20:24:20 +000086 /**
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
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000100 GrGpu* fGpu;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000101
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;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000113 GrClipMaskCache fAACache; // cache for the AA path
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000114
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000115 // 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);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000131
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000132 // Gets a texture to use for the clip mask. If true is returned then a cached mask was found
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000133 // 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);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000138
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000139 bool useSWOnlyPath(const GrReducedClip::ElementList& elements);
140
141 bool drawClipShape(GrTexture* target, const SkClipStack::Element* element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000142
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000143 void mergeMask(GrTexture* dstMask,
144 GrTexture* srcMask,
145 SkRegion::Op op,
146 const GrIRect& dstBound,
147 const GrIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000148
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000149 void getTemp(int width, int height, GrAutoScratchTexture* temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000150
rmistry@google.comd6176b02012-08-23 18:14:13 +0000151 void setupCache(const SkClipStack& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000152 const GrIRect& bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000153
bsalomon@google.coma3201942012-06-21 19:58:20 +0000154 /**
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
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000169 typedef GrNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000170};
171
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000172#endif // GrClipMaskManager_DEFINED