blob: 0c0c10cf30a6eceacf595925e6c4ec6c1d0d4838 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
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 */
robertphillips@google.com1e945b72012-04-16 18:03:03 +00007#ifndef GrClipMaskManager_DEFINED
8#define GrClipMaskManager_DEFINED
9
egdaniel8dd688b2015-01-22 10:16:09 -080010#include "GrPipelineBuilder.h"
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000011#include "GrReducedClip.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000012#include "GrStencil.h"
13#include "GrTexture.h"
robertphillips@google.com641f8b12012-07-31 19:15:58 +000014#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000015#include "SkDeque.h"
16#include "SkPath.h"
17#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000018#include "SkTLList.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "SkTypes.h"
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000020
bsalomonb3b9aec2015-09-10 11:16:35 -070021class GrDrawTarget;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000022class GrPathRenderer;
23class GrPathRendererChain;
robertphillips@google.comf294b772012-04-27 14:29:26 +000024class GrTexture;
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000025class SkPath;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000026/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000027 * The clip mask creator handles the generation of the clip mask. If anti
28 * aliasing is requested it will (in the future) generate a single channel
29 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
robertphillips@google.com1e945b72012-04-16 18:03:03 +000030 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
31 * mask can be represented as a rectangle then scissoring is used. In all
32 * cases scissoring is used to bound the range of the clip mask.
33 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000034class GrClipMaskManager : SkNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000035public:
bsalomonb3b9aec2015-09-10 11:16:35 -070036 GrClipMaskManager(GrDrawTarget* owner);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000037
bsalomon@google.coma3201942012-06-21 19:58:20 +000038 /**
39 * Creates a clip mask if necessary as a stencil buffer or alpha texture
40 * and sets the GrGpu's scissor and stencil state. If the return is false
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +000041 * then the draw can be skipped. The AutoRestoreEffects is initialized by
42 * the manager when it must install additional effects to implement the
43 * clip. devBounds is optional but can help optimize clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +000044 */
joshualitt5e6ba212015-07-13 07:35:05 -070045 bool setupClipping(const GrPipelineBuilder&,
joshualitt4421a4c2015-07-13 09:36:41 -070046 GrPipelineBuilder::AutoRestoreFragmentProcessorState*,
egdaniel8dd688b2015-01-22 10:16:09 -080047 GrPipelineBuilder::AutoRestoreStencil*,
bsalomon3e791242014-12-17 13:43:13 -080048 GrScissorState*,
joshualitt9853cce2014-11-17 14:22:48 -080049 const SkRect* devBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000050
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000051 bool isClipInStencil() const {
52 return kStencil_ClipMaskType == fCurrClipMaskType;
53 }
joshualitt9853cce2014-11-17 14:22:48 -080054
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000055 bool isClipInAlpha() const {
56 return kAlpha_ClipMaskType == fCurrClipMaskType;
57 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000058
egdaniel8dc7c3a2015-04-16 11:22:42 -070059 void adjustPathStencilParams(const GrStencilAttachment*, GrStencilSettings*);
joshualitt329bf482014-10-29 12:31:28 -070060
bsalomon@google.coma3201942012-06-21 19:58:20 +000061private:
bsalomonedd77a12015-05-29 09:45:57 -070062 inline GrContext* getContext();
63
bsalomon@google.com411dad02012-06-05 20:24:20 +000064 /**
65 * Informs the helper function adjustStencilParams() about how the stencil
66 * buffer clip is being used.
67 */
68 enum StencilClipMode {
69 // Draw to the clip bit of the stencil buffer
70 kModifyClip_StencilClipMode,
71 // Clip against the existing representation of the clip in the high bit
72 // of the stencil buffer.
73 kRespectClip_StencilClipMode,
74 // Neither writing to nor clipping against the clip bit.
75 kIgnoreClip_StencilClipMode,
76 };
77
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000078 // Attempts to install a series of coverage effects to implement the clip. Return indicates
mtklein217daa72014-07-02 12:55:21 -070079 // whether the element list was successfully converted to effects.
joshualitt5e6ba212015-07-13 07:35:05 -070080 bool installClipEffects(const GrPipelineBuilder&,
joshualitt4421a4c2015-07-13 09:36:41 -070081 GrPipelineBuilder::AutoRestoreFragmentProcessorState*,
joshualitt9853cce2014-11-17 14:22:48 -080082 const GrReducedClip::ElementList&,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000083 const SkVector& clipOffset,
mtklein217daa72014-07-02 12:55:21 -070084 const SkRect* devBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000085
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000086 // Draws the clip into the stencil buffer
joshualitt9853cce2014-11-17 14:22:48 -080087 bool createStencilClipMask(GrRenderTarget*,
88 int32_t elementsGenID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +000089 GrReducedClip::InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000090 const GrReducedClip::ElementList& elements,
91 const SkIRect& clipSpaceIBounds,
92 const SkIPoint& clipSpaceToStencilOffset);
joshualitt9853cce2014-11-17 14:22:48 -080093
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000094 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
95 // rect specified by clipSpaceIBounds.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +000096 GrTexture* createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000097 GrReducedClip::InitialState initialState,
98 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -080099 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000100 const SkIRect& clipSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800101
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000102 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000103 GrTexture* createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000104 GrReducedClip::InitialState initialState,
105 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800106 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000107 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000108
bsalomon473addf2015-10-02 07:49:05 -0700109 bool useSWOnlyPath(const GrPipelineBuilder&,
joshualitt8059eb92014-12-29 15:10:07 -0800110 const SkVector& clipToMaskOffset,
111 const GrReducedClip::ElementList& elements);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000112
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000113 // Draws a clip element into the target alpha mask. The caller should have already setup the
robertphillips@google.come79f3202014-02-11 16:30:21 +0000114 // desired blend operation. Optionally if the caller already selected a path renderer it can
115 // be passed. Otherwise the function will select one if the element is a path.
egdaniel8dd688b2015-01-22 10:16:09 -0800116 bool drawElement(GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800117 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800118 GrTexture* target,
119 const SkClipStack::Element*,
halcanary96fcdcc2015-08-27 07:41:13 -0700120 GrPathRenderer* pr = nullptr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000121
122 // Determines whether it is possible to draw the element to both the stencil buffer and the
123 // alpha mask simultaneously. If so and the element is a path a compatible path renderer is
124 // also returned.
egdaniel8dd688b2015-01-22 10:16:09 -0800125 bool canStencilAndDrawElement(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800126 GrTexture* target,
127 GrPathRenderer**,
128 const SkClipStack::Element*);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000129
egdaniel8dd688b2015-01-22 10:16:09 -0800130 void mergeMask(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800131 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000132 GrTexture* srcMask,
133 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000134 const SkIRect& dstBound,
135 const SkIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000136
bsalomon427cf282014-10-16 13:41:43 -0700137 GrTexture* createTempMask(int width, int height);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000138
bsalomon@google.coma3201942012-06-21 19:58:20 +0000139 /**
bsalomon9e5fc722015-02-23 10:01:36 -0800140 * Called prior to return control back the GrGpu in setupClipping. It updates the
141 * GrPipelineBuilder with stencil settings that account for stencil-based clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000142 */
joshualitt5e6ba212015-07-13 07:35:05 -0700143 void setPipelineBuilderStencil(const GrPipelineBuilder&,
144 GrPipelineBuilder::AutoRestoreStencil*);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000145
146 /**
147 * Adjusts the stencil settings to account for interaction with stencil
148 * clipping.
149 */
150 void adjustStencilParams(GrStencilSettings* settings,
151 StencilClipMode mode,
152 int stencilBitCnt);
153
bsalomon473addf2015-10-02 07:49:05 -0700154 GrTexture* createCachedMask(int width, int height, const GrUniqueKey& key, bool renderTarget);
155
joshualitt329bf482014-10-29 12:31:28 -0700156 /**
157 * We may represent the clip as a mask in the stencil buffer or as an alpha
158 * texture. It may be neither because the scissor rect suffices or we
159 * haven't yet examined the clip.
160 */
161 enum ClipMaskType {
162 kNone_ClipMaskType,
163 kStencil_ClipMaskType,
164 kAlpha_ClipMaskType,
165 } fCurrClipMaskType;
166
bsalomonb3b9aec2015-09-10 11:16:35 -0700167 GrDrawTarget* fDrawTarget; // This is our owning draw target.
joshualitt7a6184f2014-10-29 18:29:27 -0700168 StencilClipMode fClipMode;
joshualitt329bf482014-10-29 12:31:28 -0700169
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000170 typedef SkNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000171};
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000172#endif // GrClipMaskManager_DEFINED