blob: 6dd735b832a91b7f3c346124ed052224e161848f [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
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000010#include "GrClipMaskCache.h"
egdaniel8dd688b2015-01-22 10:16:09 -080011#include "GrPipelineBuilder.h"
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000012#include "GrReducedClip.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000013#include "GrStencil.h"
14#include "GrTexture.h"
robertphillips@google.com641f8b12012-07-31 19:15:58 +000015#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000016#include "SkDeque.h"
17#include "SkPath.h"
18#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000019#include "SkTLList.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000020#include "SkTypes.h"
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000021
joshualitt329bf482014-10-29 12:31:28 -070022class GrClipTarget;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000023class GrPathRenderer;
24class GrPathRendererChain;
robertphillips@google.comf294b772012-04-27 14:29:26 +000025class GrTexture;
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000026class SkPath;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000027/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000028 * The clip mask creator handles the generation of the clip mask. If anti
29 * aliasing is requested it will (in the future) generate a single channel
30 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
robertphillips@google.com1e945b72012-04-16 18:03:03 +000031 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
32 * mask can be represented as a rectangle then scissoring is used. In all
33 * cases scissoring is used to bound the range of the clip mask.
34 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000035class GrClipMaskManager : SkNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000036public:
bsalomonedd77a12015-05-29 09:45:57 -070037 GrClipMaskManager(GrClipTarget* owner);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000038
bsalomon@google.coma3201942012-06-21 19:58:20 +000039 /**
40 * Creates a clip mask if necessary as a stencil buffer or alpha texture
41 * and sets the GrGpu's scissor and stencil state. If the return is false
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +000042 * then the draw can be skipped. The AutoRestoreEffects is initialized by
43 * the manager when it must install additional effects to implement the
44 * clip. devBounds is optional but can help optimize clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +000045 */
joshualitt5e6ba212015-07-13 07:35:05 -070046 bool setupClipping(const GrPipelineBuilder&,
bsalomon6be6f7c2015-02-26 13:05:21 -080047 GrPipelineBuilder::AutoRestoreFragmentProcessors*,
egdaniel8dd688b2015-01-22 10:16:09 -080048 GrPipelineBuilder::AutoRestoreStencil*,
joshualitt5e6ba212015-07-13 07:35:05 -070049 GrPipelineBuilder::AutoRestoreProcessorDataManager*,
bsalomon3e791242014-12-17 13:43:13 -080050 GrScissorState*,
joshualitt9853cce2014-11-17 14:22:48 -080051 const SkRect* devBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000052
bsalomonc8dc1f72014-08-21 13:02:13 -070053 /**
54 * Purge resources to free up memory. TODO: This class shouldn't hold any long lived refs
bsalomon0ea80f42015-02-11 10:49:59 -080055 * which will allow Resourcecache to automatically purge anything this class has created.
bsalomonc8dc1f72014-08-21 13:02:13 -070056 */
57 void purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +000058
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000059 bool isClipInStencil() const {
60 return kStencil_ClipMaskType == fCurrClipMaskType;
61 }
joshualitt9853cce2014-11-17 14:22:48 -080062
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000063 bool isClipInAlpha() const {
64 return kAlpha_ClipMaskType == fCurrClipMaskType;
65 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000066
joshualitt329bf482014-10-29 12:31:28 -070067 void setClipTarget(GrClipTarget*);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000068
egdaniel8dc7c3a2015-04-16 11:22:42 -070069 void adjustPathStencilParams(const GrStencilAttachment*, GrStencilSettings*);
joshualitt329bf482014-10-29 12:31:28 -070070
bsalomon@google.coma3201942012-06-21 19:58:20 +000071private:
bsalomonedd77a12015-05-29 09:45:57 -070072 inline GrContext* getContext();
73
bsalomon@google.com411dad02012-06-05 20:24:20 +000074 /**
75 * Informs the helper function adjustStencilParams() about how the stencil
76 * buffer clip is being used.
77 */
78 enum StencilClipMode {
79 // Draw to the clip bit of the stencil buffer
80 kModifyClip_StencilClipMode,
81 // Clip against the existing representation of the clip in the high bit
82 // of the stencil buffer.
83 kRespectClip_StencilClipMode,
84 // Neither writing to nor clipping against the clip bit.
85 kIgnoreClip_StencilClipMode,
86 };
87
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000088 // Attempts to install a series of coverage effects to implement the clip. Return indicates
mtklein217daa72014-07-02 12:55:21 -070089 // whether the element list was successfully converted to effects.
joshualitt5e6ba212015-07-13 07:35:05 -070090 bool installClipEffects(const GrPipelineBuilder&,
bsalomon6be6f7c2015-02-26 13:05:21 -080091 GrPipelineBuilder::AutoRestoreFragmentProcessors*,
joshualitt9853cce2014-11-17 14:22:48 -080092 const GrReducedClip::ElementList&,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000093 const SkVector& clipOffset,
mtklein217daa72014-07-02 12:55:21 -070094 const SkRect* devBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000095
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000096 // Draws the clip into the stencil buffer
joshualitt9853cce2014-11-17 14:22:48 -080097 bool createStencilClipMask(GrRenderTarget*,
98 int32_t elementsGenID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +000099 GrReducedClip::InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000100 const GrReducedClip::ElementList& elements,
101 const SkIRect& clipSpaceIBounds,
102 const SkIPoint& clipSpaceToStencilOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800103
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000104 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
105 // rect specified by clipSpaceIBounds.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000106 GrTexture* createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000107 GrReducedClip::InitialState initialState,
108 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800109 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000110 const SkIRect& clipSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800111
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000112 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000113 GrTexture* createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000114 GrReducedClip::InitialState initialState,
115 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800116 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000117 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000118
krajcevskiad1dc582014-06-10 15:06:47 -0700119 // Returns the cached mask texture if it matches the elementsGenID and the clipSpaceIBounds.
120 // Returns NULL if not found.
121 GrTexture* getCachedMaskTexture(int32_t elementsGenID, const SkIRect& clipSpaceIBounds);
122
krajcevskiad1dc582014-06-10 15:06:47 -0700123 // Handles allocation (if needed) of a clip alpha-mask texture for both the sw-upload
124 // or gpu-rendered cases.
125 GrTexture* allocMaskTexture(int32_t elementsGenID,
126 const SkIRect& clipSpaceIBounds,
127 bool willUpload);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000128
joshualitt5e6ba212015-07-13 07:35:05 -0700129 bool useSWOnlyPath(const GrPipelineBuilder&,
joshualitt8059eb92014-12-29 15:10:07 -0800130 const SkVector& clipToMaskOffset,
131 const GrReducedClip::ElementList& elements);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000132
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000133 // Draws a clip element into the target alpha mask. The caller should have already setup the
robertphillips@google.come79f3202014-02-11 16:30:21 +0000134 // desired blend operation. Optionally if the caller already selected a path renderer it can
135 // be passed. Otherwise the function will select one if the element is a path.
egdaniel8dd688b2015-01-22 10:16:09 -0800136 bool drawElement(GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800137 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800138 GrTexture* target,
139 const SkClipStack::Element*,
140 GrPathRenderer* pr = NULL);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000141
142 // Determines whether it is possible to draw the element to both the stencil buffer and the
143 // alpha mask simultaneously. If so and the element is a path a compatible path renderer is
144 // also returned.
egdaniel8dd688b2015-01-22 10:16:09 -0800145 bool canStencilAndDrawElement(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800146 GrTexture* target,
147 GrPathRenderer**,
148 const SkClipStack::Element*);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000149
egdaniel8dd688b2015-01-22 10:16:09 -0800150 void mergeMask(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800151 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000152 GrTexture* srcMask,
153 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000154 const SkIRect& dstBound,
155 const SkIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000156
bsalomon427cf282014-10-16 13:41:43 -0700157 GrTexture* createTempMask(int width, int height);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000158
rmistry@google.comd6176b02012-08-23 18:14:13 +0000159 void setupCache(const SkClipStack& clip,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000160 const SkIRect& bounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000161 /**
bsalomon9e5fc722015-02-23 10:01:36 -0800162 * Called prior to return control back the GrGpu in setupClipping. It updates the
163 * GrPipelineBuilder with stencil settings that account for stencil-based clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000164 */
joshualitt5e6ba212015-07-13 07:35:05 -0700165 void setPipelineBuilderStencil(const GrPipelineBuilder&,
166 GrPipelineBuilder::AutoRestoreStencil*);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000167
168 /**
169 * Adjusts the stencil settings to account for interaction with stencil
170 * clipping.
171 */
172 void adjustStencilParams(GrStencilSettings* settings,
173 StencilClipMode mode,
174 int stencilBitCnt);
175
joshualitt329bf482014-10-29 12:31:28 -0700176 /**
177 * We may represent the clip as a mask in the stencil buffer or as an alpha
178 * texture. It may be neither because the scissor rect suffices or we
179 * haven't yet examined the clip.
180 */
181 enum ClipMaskType {
182 kNone_ClipMaskType,
183 kStencil_ClipMaskType,
184 kAlpha_ClipMaskType,
185 } fCurrClipMaskType;
186
187 GrClipMaskCache fAACache; // cache for the AA path
bsalomonedd77a12015-05-29 09:45:57 -0700188 GrClipTarget* fClipTarget; // This is our owning clip target.
joshualitt7a6184f2014-10-29 18:29:27 -0700189 StencilClipMode fClipMode;
joshualitt329bf482014-10-29 12:31:28 -0700190
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000191 typedef SkNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000192};
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000193#endif // GrClipMaskManager_DEFINED