blob: 8a4b45a82e2d016445123b05f174ef1c058f75e9 [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 */
7
8#ifndef GrClipMaskManager_DEFINED
9#define GrClipMaskManager_DEFINED
10
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000011#include "GrClipMaskCache.h"
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.com4c2443e2012-12-06 20:58:57 +000014#include "GrReducedClip.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000015#include "GrStencil.h"
16#include "GrTexture.h"
17
robertphillips@google.com641f8b12012-07-31 19:15:58 +000018#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000019#include "SkDeque.h"
20#include "SkPath.h"
21#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000022#include "SkTLList.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000023#include "SkTypes.h"
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000024
robertphillips@google.com1e945b72012-04-16 18:03:03 +000025class GrGpu;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000026class GrPathRenderer;
27class GrPathRendererChain;
robertphillips@google.comf294b772012-04-27 14:29:26 +000028class GrTexture;
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000029class SkPath;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000030
31/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000032 * The clip mask creator handles the generation of the clip mask. If anti
33 * aliasing is requested it will (in the future) generate a single channel
34 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
robertphillips@google.com1e945b72012-04-16 18:03:03 +000035 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
36 * mask can be represented as a rectangle then scissoring is used. In all
37 * cases scissoring is used to bound the range of the clip mask.
38 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000039class GrClipMaskManager : SkNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000040public:
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000041 GrClipMaskManager()
42 : fGpu(NULL)
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000043 , fCurrClipMaskType(kNone_ClipMaskType) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000044 }
45
bsalomon@google.coma3201942012-06-21 19:58:20 +000046 /**
47 * Creates a clip mask if necessary as a stencil buffer or alpha texture
48 * and sets the GrGpu's scissor and stencil state. If the return is false
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +000049 * then the draw can be skipped. The AutoRestoreEffects is initialized by
50 * the manager when it must install additional effects to implement the
51 * clip. devBounds is optional but can help optimize clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +000052 */
joshualitta58fe352014-10-27 08:39:00 -070053 bool setupClipping(const GrClipData* clipDataIn,
54 GrDrawState::AutoRestoreEffects*,
55 GrDrawState::AutoRestoreStencil*,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +000056 const SkRect* devBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000057
bsalomonc8dc1f72014-08-21 13:02:13 -070058 /**
59 * Purge resources to free up memory. TODO: This class shouldn't hold any long lived refs
60 * which will allow ResourceCache2 to automatically purge anything this class has created.
61 */
62 void purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +000063
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000064 bool isClipInStencil() const {
65 return kStencil_ClipMaskType == fCurrClipMaskType;
66 }
67 bool isClipInAlpha() const {
68 return kAlpha_ClipMaskType == fCurrClipMaskType;
69 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000070
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000071 void invalidateStencilMask() {
72 if (kStencil_ClipMaskType == fCurrClipMaskType) {
73 fCurrClipMaskType = kNone_ClipMaskType;
74 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000075 }
76
robertphillips@google.com2c756812012-05-22 20:28:23 +000077 GrContext* getContext() {
78 return fAACache.getContext();
79 }
80
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000081 void setGpu(GrGpu* gpu);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000082
83 void adjustPathStencilParams(GrStencilSettings* settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +000084private:
bsalomon@google.com411dad02012-06-05 20:24:20 +000085 /**
86 * Informs the helper function adjustStencilParams() about how the stencil
87 * buffer clip is being used.
88 */
89 enum StencilClipMode {
90 // Draw to the clip bit of the stencil buffer
91 kModifyClip_StencilClipMode,
92 // Clip against the existing representation of the clip in the high bit
93 // of the stencil buffer.
94 kRespectClip_StencilClipMode,
95 // Neither writing to nor clipping against the clip bit.
96 kIgnoreClip_StencilClipMode,
97 };
98
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000099 GrGpu* fGpu;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000100
101 /**
102 * We may represent the clip as a mask in the stencil buffer or as an alpha
103 * texture. It may be neither because the scissor rect suffices or we
104 * haven't yet examined the clip.
105 */
106 enum ClipMaskType {
107 kNone_ClipMaskType,
108 kStencil_ClipMaskType,
109 kAlpha_ClipMaskType,
110 } fCurrClipMaskType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000112 GrClipMaskCache fAACache; // cache for the AA path
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000113
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000114 // Attempts to install a series of coverage effects to implement the clip. Return indicates
mtklein217daa72014-07-02 12:55:21 -0700115 // whether the element list was successfully converted to effects.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000116 bool installClipEffects(const GrReducedClip::ElementList&,
117 GrDrawState::AutoRestoreEffects*,
118 const SkVector& clipOffset,
mtklein217daa72014-07-02 12:55:21 -0700119 const SkRect* devBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000120
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000121 // Draws the clip into the stencil buffer
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000122 bool createStencilClipMask(int32_t elementsGenID,
123 GrReducedClip::InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000124 const GrReducedClip::ElementList& elements,
125 const SkIRect& clipSpaceIBounds,
126 const SkIPoint& clipSpaceToStencilOffset);
127 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
128 // rect specified by clipSpaceIBounds.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000129 GrTexture* createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000130 GrReducedClip::InitialState initialState,
131 const GrReducedClip::ElementList& elements,
132 const SkIRect& clipSpaceIBounds);
133 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000134 GrTexture* createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000135 GrReducedClip::InitialState initialState,
136 const GrReducedClip::ElementList& elements,
137 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000138
krajcevskiad1dc582014-06-10 15:06:47 -0700139 // Returns the cached mask texture if it matches the elementsGenID and the clipSpaceIBounds.
140 // Returns NULL if not found.
141 GrTexture* getCachedMaskTexture(int32_t elementsGenID, const SkIRect& clipSpaceIBounds);
142
143
144 // Handles allocation (if needed) of a clip alpha-mask texture for both the sw-upload
145 // or gpu-rendered cases.
146 GrTexture* allocMaskTexture(int32_t elementsGenID,
147 const SkIRect& clipSpaceIBounds,
148 bool willUpload);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000149
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000150 bool useSWOnlyPath(const GrReducedClip::ElementList& elements);
151
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000152 // Draws a clip element into the target alpha mask. The caller should have already setup the
robertphillips@google.come79f3202014-02-11 16:30:21 +0000153 // desired blend operation. Optionally if the caller already selected a path renderer it can
154 // be passed. Otherwise the function will select one if the element is a path.
155 bool drawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer* = NULL);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000156
157 // Determines whether it is possible to draw the element to both the stencil buffer and the
158 // alpha mask simultaneously. If so and the element is a path a compatible path renderer is
159 // also returned.
robertphillips@google.come79f3202014-02-11 16:30:21 +0000160 bool canStencilAndDrawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer**);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000161
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000162 void mergeMask(GrTexture* dstMask,
163 GrTexture* srcMask,
164 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000165 const SkIRect& dstBound,
166 const SkIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000167
bsalomon427cf282014-10-16 13:41:43 -0700168 GrTexture* createTempMask(int width, int height);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000169
rmistry@google.comd6176b02012-08-23 18:14:13 +0000170 void setupCache(const SkClipStack& clip,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000171 const SkIRect& bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000172
bsalomon@google.coma3201942012-06-21 19:58:20 +0000173 /**
174 * Called prior to return control back the GrGpu in setupClipping. It
175 * updates the GrGpu with stencil settings that account stencil-based
176 * clipping.
177 */
joshualitta58fe352014-10-27 08:39:00 -0700178 void setDrawStateStencil(GrDrawState::AutoRestoreStencil* asr);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000179
180 /**
181 * Adjusts the stencil settings to account for interaction with stencil
182 * clipping.
183 */
184 void adjustStencilParams(GrStencilSettings* settings,
185 StencilClipMode mode,
186 int stencilBitCnt);
187
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000188 typedef SkNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000189};
190
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000191#endif // GrClipMaskManager_DEFINED