blob: 0ab74a7081d14280707f5c6916617c4eba6d7b6d [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"
robertphillips@google.comf105b102012-05-14 12:18:26 +000011#include "GrContext.h"
egdaniel8dd688b2015-01-22 10:16:09 -080012#include "GrPipelineBuilder.h"
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000013#include "GrReducedClip.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000014#include "GrStencil.h"
15#include "GrTexture.h"
robertphillips@google.com641f8b12012-07-31 19:15:58 +000016#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000017#include "SkDeque.h"
18#include "SkPath.h"
19#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000020#include "SkTLList.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000021#include "SkTypes.h"
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000022
joshualitt329bf482014-10-29 12:31:28 -070023class GrClipTarget;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000024class GrPathRenderer;
25class GrPathRendererChain;
robertphillips@google.comf294b772012-04-27 14:29:26 +000026class GrTexture;
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000027class SkPath;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000028/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000029 * The clip mask creator handles the generation of the clip mask. If anti
30 * aliasing is requested it will (in the future) generate a single channel
31 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
robertphillips@google.com1e945b72012-04-16 18:03:03 +000032 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
33 * mask can be represented as a rectangle then scissoring is used. In all
34 * cases scissoring is used to bound the range of the clip mask.
35 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000036class GrClipMaskManager : SkNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000037public:
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000038 GrClipMaskManager()
joshualitt329bf482014-10-29 12:31:28 -070039 : fCurrClipMaskType(kNone_ClipMaskType)
joshualitt7a6184f2014-10-29 18:29:27 -070040 , fClipTarget(NULL)
41 , fClipMode(kIgnoreClip_StencilClipMode) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000042 }
43
bsalomon@google.coma3201942012-06-21 19:58:20 +000044 /**
45 * Creates a clip mask if necessary as a stencil buffer or alpha texture
46 * and sets the GrGpu's scissor and stencil state. If the return is false
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +000047 * then the draw can be skipped. The AutoRestoreEffects is initialized by
48 * the manager when it must install additional effects to implement the
49 * clip. devBounds is optional but can help optimize clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +000050 */
egdaniel8dd688b2015-01-22 10:16:09 -080051 bool setupClipping(GrPipelineBuilder*,
52 GrPipelineBuilder::AutoRestoreEffects*,
53 GrPipelineBuilder::AutoRestoreStencil*,
bsalomon3e791242014-12-17 13:43:13 -080054 GrScissorState*,
joshualitt9853cce2014-11-17 14:22:48 -080055 const SkRect* devBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000056
bsalomonc8dc1f72014-08-21 13:02:13 -070057 /**
58 * Purge resources to free up memory. TODO: This class shouldn't hold any long lived refs
bsalomon0ea80f42015-02-11 10:49:59 -080059 * which will allow Resourcecache to automatically purge anything this class has created.
bsalomonc8dc1f72014-08-21 13:02:13 -070060 */
61 void purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +000062
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000063 bool isClipInStencil() const {
64 return kStencil_ClipMaskType == fCurrClipMaskType;
65 }
joshualitt9853cce2014-11-17 14:22:48 -080066
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000067 bool isClipInAlpha() const {
68 return kAlpha_ClipMaskType == fCurrClipMaskType;
69 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000070
robertphillips@google.com2c756812012-05-22 20:28:23 +000071 GrContext* getContext() {
72 return fAACache.getContext();
73 }
74
joshualitt329bf482014-10-29 12:31:28 -070075 void setClipTarget(GrClipTarget*);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000076
joshualitt9853cce2014-11-17 14:22:48 -080077 void adjustPathStencilParams(const GrStencilBuffer*, GrStencilSettings*);
joshualitt329bf482014-10-29 12:31:28 -070078
bsalomon@google.coma3201942012-06-21 19:58:20 +000079private:
bsalomon@google.com411dad02012-06-05 20:24:20 +000080 /**
81 * Informs the helper function adjustStencilParams() about how the stencil
82 * buffer clip is being used.
83 */
84 enum StencilClipMode {
85 // Draw to the clip bit of the stencil buffer
86 kModifyClip_StencilClipMode,
87 // Clip against the existing representation of the clip in the high bit
88 // of the stencil buffer.
89 kRespectClip_StencilClipMode,
90 // Neither writing to nor clipping against the clip bit.
91 kIgnoreClip_StencilClipMode,
92 };
93
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000094 // Attempts to install a series of coverage effects to implement the clip. Return indicates
mtklein217daa72014-07-02 12:55:21 -070095 // whether the element list was successfully converted to effects.
egdaniel8dd688b2015-01-22 10:16:09 -080096 bool installClipEffects(GrPipelineBuilder*,
97 GrPipelineBuilder::AutoRestoreEffects*,
joshualitt9853cce2014-11-17 14:22:48 -080098 const GrReducedClip::ElementList&,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000099 const SkVector& clipOffset,
mtklein217daa72014-07-02 12:55:21 -0700100 const SkRect* devBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000101
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000102 // Draws the clip into the stencil buffer
joshualitt9853cce2014-11-17 14:22:48 -0800103 bool createStencilClipMask(GrRenderTarget*,
104 int32_t elementsGenID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000105 GrReducedClip::InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 const GrReducedClip::ElementList& elements,
107 const SkIRect& clipSpaceIBounds,
108 const SkIPoint& clipSpaceToStencilOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800109
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000110 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
111 // rect specified by clipSpaceIBounds.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000112 GrTexture* createAlphaClipMask(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);
joshualitt9853cce2014-11-17 14:22:48 -0800117
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000118 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000119 GrTexture* createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000120 GrReducedClip::InitialState initialState,
121 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800122 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000123 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000124
krajcevskiad1dc582014-06-10 15:06:47 -0700125 // Returns the cached mask texture if it matches the elementsGenID and the clipSpaceIBounds.
126 // Returns NULL if not found.
127 GrTexture* getCachedMaskTexture(int32_t elementsGenID, const SkIRect& clipSpaceIBounds);
128
krajcevskiad1dc582014-06-10 15:06:47 -0700129 // Handles allocation (if needed) of a clip alpha-mask texture for both the sw-upload
130 // or gpu-rendered cases.
131 GrTexture* allocMaskTexture(int32_t elementsGenID,
132 const SkIRect& clipSpaceIBounds,
133 bool willUpload);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000134
egdaniel8dd688b2015-01-22 10:16:09 -0800135 bool useSWOnlyPath(const GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800136 const SkVector& clipToMaskOffset,
137 const GrReducedClip::ElementList& elements);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000138
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000139 // Draws a clip element into the target alpha mask. The caller should have already setup the
robertphillips@google.come79f3202014-02-11 16:30:21 +0000140 // desired blend operation. Optionally if the caller already selected a path renderer it can
141 // be passed. Otherwise the function will select one if the element is a path.
egdaniel8dd688b2015-01-22 10:16:09 -0800142 bool drawElement(GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800143 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800144 GrTexture* target,
145 const SkClipStack::Element*,
146 GrPathRenderer* pr = NULL);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000147
148 // Determines whether it is possible to draw the element to both the stencil buffer and the
149 // alpha mask simultaneously. If so and the element is a path a compatible path renderer is
150 // also returned.
egdaniel8dd688b2015-01-22 10:16:09 -0800151 bool canStencilAndDrawElement(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800152 GrTexture* target,
153 GrPathRenderer**,
154 const SkClipStack::Element*);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000155
egdaniel8dd688b2015-01-22 10:16:09 -0800156 void mergeMask(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800157 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000158 GrTexture* srcMask,
159 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000160 const SkIRect& dstBound,
161 const SkIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000162
bsalomon427cf282014-10-16 13:41:43 -0700163 GrTexture* createTempMask(int width, int height);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000164
rmistry@google.comd6176b02012-08-23 18:14:13 +0000165 void setupCache(const SkClipStack& clip,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000166 const SkIRect& bounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000167 /**
bsalomon9e5fc722015-02-23 10:01:36 -0800168 * Called prior to return control back the GrGpu in setupClipping. It updates the
169 * GrPipelineBuilder with stencil settings that account for stencil-based clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000170 */
egdaniel8dd688b2015-01-22 10:16:09 -0800171 void setPipelineBuilderStencil(GrPipelineBuilder*, GrPipelineBuilder::AutoRestoreStencil*);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000172
173 /**
174 * Adjusts the stencil settings to account for interaction with stencil
175 * clipping.
176 */
177 void adjustStencilParams(GrStencilSettings* settings,
178 StencilClipMode mode,
179 int stencilBitCnt);
180
joshualitt329bf482014-10-29 12:31:28 -0700181 /**
182 * We may represent the clip as a mask in the stencil buffer or as an alpha
183 * texture. It may be neither because the scissor rect suffices or we
184 * haven't yet examined the clip.
185 */
186 enum ClipMaskType {
187 kNone_ClipMaskType,
188 kStencil_ClipMaskType,
189 kAlpha_ClipMaskType,
190 } fCurrClipMaskType;
191
192 GrClipMaskCache fAACache; // cache for the AA path
joshualitt7a6184f2014-10-29 18:29:27 -0700193 GrClipTarget* fClipTarget;
194 StencilClipMode fClipMode;
joshualitt329bf482014-10-29 12:31:28 -0700195
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000196 typedef SkNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000197};
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000198#endif // GrClipMaskManager_DEFINED