blob: 121737b897407d8a69c2947485c8a0df2446cebe [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.comeb6879f2013-06-13 19:34:18 +000013#include "GrDrawState.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000014#include "GrNoncopyable.h"
15#include "GrRect.h"
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000016#include "GrReducedClip.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000017#include "GrStencil.h"
18#include "GrTexture.h"
19
robertphillips@google.com641f8b12012-07-31 19:15:58 +000020#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000021#include "SkDeque.h"
22#include "SkPath.h"
23#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000024#include "SkTLList.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000025
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000026#include "GrClipMaskCache.h"
27
robertphillips@google.com1e945b72012-04-16 18:03:03 +000028class GrGpu;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000029class GrPathRenderer;
30class GrPathRendererChain;
31class SkPath;
robertphillips@google.comf294b772012-04-27 14:29:26 +000032class GrTexture;
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.com5d8d1862012-08-15 14:36:41 +000044 GrClipMaskManager()
45 : fGpu(NULL)
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000046 , fCurrClipMaskType(kNone_ClipMaskType) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000047 }
48
bsalomon@google.coma3201942012-06-21 19:58:20 +000049 /**
50 * Creates a clip mask if necessary as a stencil buffer or alpha texture
51 * and sets the GrGpu's scissor and stencil state. If the return is false
52 * then the draw can be skipped.
53 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000054 bool setupClipping(const GrClipData* clipDataIn, GrDrawState::AutoRestoreEffects*);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000055
robertphillips@google.comf105b102012-05-14 12:18:26 +000056 void releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +000057
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000058 bool isClipInStencil() const {
59 return kStencil_ClipMaskType == fCurrClipMaskType;
60 }
61 bool isClipInAlpha() const {
62 return kAlpha_ClipMaskType == fCurrClipMaskType;
63 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000064
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000065 void invalidateStencilMask() {
66 if (kStencil_ClipMaskType == fCurrClipMaskType) {
67 fCurrClipMaskType = kNone_ClipMaskType;
68 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000069 }
70
robertphillips@google.com2c756812012-05-22 20:28:23 +000071 GrContext* getContext() {
72 return fAACache.getContext();
73 }
74
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000075 void setGpu(GrGpu* gpu);
bsalomon@google.coma3201942012-06-21 19:58:20 +000076private:
bsalomon@google.com411dad02012-06-05 20:24:20 +000077 /**
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
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000091 GrGpu* fGpu;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000092
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;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000104 GrClipMaskCache fAACache; // cache for the AA path
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000105
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 // Draws the clip into the stencil buffer
107 bool createStencilClipMask(GrReducedClip::InitialState initialState,
108 const GrReducedClip::ElementList& elements,
109 const SkIRect& clipSpaceIBounds,
110 const SkIPoint& clipSpaceToStencilOffset);
111 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
112 // rect specified by clipSpaceIBounds.
113 GrTexture* createAlphaClipMask(int32_t clipStackGenID,
114 GrReducedClip::InitialState initialState,
115 const GrReducedClip::ElementList& elements,
116 const SkIRect& clipSpaceIBounds);
117 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
118 GrTexture* createSoftwareClipMask(int32_t clipStackGenID,
119 GrReducedClip::InitialState initialState,
120 const GrReducedClip::ElementList& elements,
121 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000122
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000123 // 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 +0000124 // that already contains the rasterization of the clip stack, otherwise an uninitialized texture
125 // is returned.
126 bool getMaskTexture(int32_t clipStackGenID,
127 const SkIRect& clipSpaceIBounds,
128 GrTexture** result);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000129
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000130 bool useSWOnlyPath(const GrReducedClip::ElementList& elements);
131
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000132 // Draws a clip element into the target alpha mask. The caller should have already setup the
133 // desired blend operation. Optionally if the caller already selected a path renderer it can
134 // be passed. Otherwise the function will select one if the element is a path.
135 bool drawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer* = NULL);
136
137 // Determines whether it is possible to draw the element to both the stencil buffer and the
138 // alpha mask simultaneously. If so and the element is a path a compatible path renderer is
139 // also returned.
140 bool canStencilAndDrawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer**);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000141
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000142 void mergeMask(GrTexture* dstMask,
143 GrTexture* srcMask,
144 SkRegion::Op op,
145 const GrIRect& dstBound,
146 const GrIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000147
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000148 void getTemp(int width, int height, GrAutoScratchTexture* temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000149
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150 void setupCache(const SkClipStack& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000151 const GrIRect& bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000152
bsalomon@google.coma3201942012-06-21 19:58:20 +0000153 /**
154 * Called prior to return control back the GrGpu in setupClipping. It
155 * updates the GrGpu with stencil settings that account stencil-based
156 * clipping.
157 */
158 void setGpuStencil();
159
160 /**
161 * Adjusts the stencil settings to account for interaction with stencil
162 * clipping.
163 */
164 void adjustStencilParams(GrStencilSettings* settings,
165 StencilClipMode mode,
166 int stencilBitCnt);
167
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000168 typedef GrNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000169};
170
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000171#endif // GrClipMaskManager_DEFINED