blob: 842ff99197b6412fdc8e40a3f5f0d1179669df1f [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 */
egdaniel8dd688b2015-01-22 10:16:09 -080046 bool setupClipping(GrPipelineBuilder*,
bsalomon6be6f7c2015-02-26 13:05:21 -080047 GrPipelineBuilder::AutoRestoreFragmentProcessors*,
egdaniel8dd688b2015-01-22 10:16:09 -080048 GrPipelineBuilder::AutoRestoreStencil*,
bsalomon3e791242014-12-17 13:43:13 -080049 GrScissorState*,
joshualitt9853cce2014-11-17 14:22:48 -080050 const SkRect* devBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000051
bsalomonc8dc1f72014-08-21 13:02:13 -070052 /**
53 * Purge resources to free up memory. TODO: This class shouldn't hold any long lived refs
bsalomon0ea80f42015-02-11 10:49:59 -080054 * which will allow Resourcecache to automatically purge anything this class has created.
bsalomonc8dc1f72014-08-21 13:02:13 -070055 */
56 void purgeResources();
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 }
joshualitt9853cce2014-11-17 14:22:48 -080061
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000062 bool isClipInAlpha() const {
63 return kAlpha_ClipMaskType == fCurrClipMaskType;
64 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000065
joshualitt329bf482014-10-29 12:31:28 -070066 void setClipTarget(GrClipTarget*);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000067
egdaniel8dc7c3a2015-04-16 11:22:42 -070068 void adjustPathStencilParams(const GrStencilAttachment*, GrStencilSettings*);
joshualitt329bf482014-10-29 12:31:28 -070069
bsalomon@google.coma3201942012-06-21 19:58:20 +000070private:
bsalomonedd77a12015-05-29 09:45:57 -070071 inline GrContext* getContext();
72
bsalomon@google.com411dad02012-06-05 20:24:20 +000073 /**
74 * Informs the helper function adjustStencilParams() about how the stencil
75 * buffer clip is being used.
76 */
77 enum StencilClipMode {
78 // Draw to the clip bit of the stencil buffer
79 kModifyClip_StencilClipMode,
80 // Clip against the existing representation of the clip in the high bit
81 // of the stencil buffer.
82 kRespectClip_StencilClipMode,
83 // Neither writing to nor clipping against the clip bit.
84 kIgnoreClip_StencilClipMode,
85 };
86
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000087 // Attempts to install a series of coverage effects to implement the clip. Return indicates
mtklein217daa72014-07-02 12:55:21 -070088 // whether the element list was successfully converted to effects.
egdaniel8dd688b2015-01-22 10:16:09 -080089 bool installClipEffects(GrPipelineBuilder*,
bsalomon6be6f7c2015-02-26 13:05:21 -080090 GrPipelineBuilder::AutoRestoreFragmentProcessors*,
joshualitt9853cce2014-11-17 14:22:48 -080091 const GrReducedClip::ElementList&,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000092 const SkVector& clipOffset,
mtklein217daa72014-07-02 12:55:21 -070093 const SkRect* devBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000094
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000095 // Draws the clip into the stencil buffer
joshualitt9853cce2014-11-17 14:22:48 -080096 bool createStencilClipMask(GrRenderTarget*,
97 int32_t elementsGenID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +000098 GrReducedClip::InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000099 const GrReducedClip::ElementList& elements,
100 const SkIRect& clipSpaceIBounds,
101 const SkIPoint& clipSpaceToStencilOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800102
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000103 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
104 // rect specified by clipSpaceIBounds.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000105 GrTexture* createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 GrReducedClip::InitialState initialState,
107 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800108 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000109 const SkIRect& clipSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800110
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000111 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000112 GrTexture* createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000113 GrReducedClip::InitialState initialState,
114 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800115 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000116 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000117
krajcevskiad1dc582014-06-10 15:06:47 -0700118 // Returns the cached mask texture if it matches the elementsGenID and the clipSpaceIBounds.
119 // Returns NULL if not found.
120 GrTexture* getCachedMaskTexture(int32_t elementsGenID, const SkIRect& clipSpaceIBounds);
121
krajcevskiad1dc582014-06-10 15:06:47 -0700122 // Handles allocation (if needed) of a clip alpha-mask texture for both the sw-upload
123 // or gpu-rendered cases.
124 GrTexture* allocMaskTexture(int32_t elementsGenID,
125 const SkIRect& clipSpaceIBounds,
126 bool willUpload);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000127
egdaniel8dd688b2015-01-22 10:16:09 -0800128 bool useSWOnlyPath(const GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800129 const SkVector& clipToMaskOffset,
130 const GrReducedClip::ElementList& elements);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000131
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
robertphillips@google.come79f3202014-02-11 16:30:21 +0000133 // 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.
egdaniel8dd688b2015-01-22 10:16:09 -0800135 bool drawElement(GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800136 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800137 GrTexture* target,
138 const SkClipStack::Element*,
139 GrPathRenderer* pr = NULL);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000140
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.
egdaniel8dd688b2015-01-22 10:16:09 -0800144 bool canStencilAndDrawElement(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800145 GrTexture* target,
146 GrPathRenderer**,
147 const SkClipStack::Element*);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000148
egdaniel8dd688b2015-01-22 10:16:09 -0800149 void mergeMask(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800150 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000151 GrTexture* srcMask,
152 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000153 const SkIRect& dstBound,
154 const SkIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000155
bsalomon427cf282014-10-16 13:41:43 -0700156 GrTexture* createTempMask(int width, int height);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000157
rmistry@google.comd6176b02012-08-23 18:14:13 +0000158 void setupCache(const SkClipStack& clip,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000159 const SkIRect& bounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000160 /**
bsalomon9e5fc722015-02-23 10:01:36 -0800161 * Called prior to return control back the GrGpu in setupClipping. It updates the
162 * GrPipelineBuilder with stencil settings that account for stencil-based clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000163 */
egdaniel8dd688b2015-01-22 10:16:09 -0800164 void setPipelineBuilderStencil(GrPipelineBuilder*, GrPipelineBuilder::AutoRestoreStencil*);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000165
166 /**
167 * Adjusts the stencil settings to account for interaction with stencil
168 * clipping.
169 */
170 void adjustStencilParams(GrStencilSettings* settings,
171 StencilClipMode mode,
172 int stencilBitCnt);
173
joshualitt329bf482014-10-29 12:31:28 -0700174 /**
175 * We may represent the clip as a mask in the stencil buffer or as an alpha
176 * texture. It may be neither because the scissor rect suffices or we
177 * haven't yet examined the clip.
178 */
179 enum ClipMaskType {
180 kNone_ClipMaskType,
181 kStencil_ClipMaskType,
182 kAlpha_ClipMaskType,
183 } fCurrClipMaskType;
184
185 GrClipMaskCache fAACache; // cache for the AA path
bsalomonedd77a12015-05-29 09:45:57 -0700186 GrClipTarget* fClipTarget; // This is our owning clip target.
joshualitt7a6184f2014-10-29 18:29:27 -0700187 StencilClipMode fClipMode;
joshualitt329bf482014-10-29 12:31:28 -0700188
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000189 typedef SkNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000190};
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000191#endif // GrClipMaskManager_DEFINED