blob: 2cba4776d9ac2224ef722f8dcbf598897eb8701f [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef GrClipMaskManager_DEFINED
10#define GrClipMaskManager_DEFINED
11
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.com411dad02012-06-05 20:24:20 +000014#include "GrNoncopyable.h"
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000015#include "GrReducedClip.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000016#include "GrStencil.h"
17#include "GrTexture.h"
18
robertphillips@google.com641f8b12012-07-31 19:15:58 +000019#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000020#include "SkDeque.h"
21#include "SkPath.h"
22#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000023#include "SkTLList.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000024
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000025#include "GrClipMaskCache.h"
26
robertphillips@google.com1e945b72012-04-16 18:03:03 +000027class GrGpu;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000028class GrPathRenderer;
29class GrPathRendererChain;
30class SkPath;
robertphillips@google.comf294b772012-04-27 14:29:26 +000031class GrTexture;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000032
33/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000034 * The clip mask creator handles the generation of the clip mask. If anti
35 * aliasing is requested it will (in the future) generate a single channel
36 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
robertphillips@google.com1e945b72012-04-16 18:03:03 +000037 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
38 * mask can be represented as a rectangle then scissoring is used. In all
39 * cases scissoring is used to bound the range of the clip mask.
40 */
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000041class GrClipMaskManager : public GrNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000042public:
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000043 GrClipMaskManager()
44 : fGpu(NULL)
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000045 , fCurrClipMaskType(kNone_ClipMaskType) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000046 }
47
bsalomon@google.coma3201942012-06-21 19:58:20 +000048 /**
49 * Creates a clip mask if necessary as a stencil buffer or alpha texture
50 * and sets the GrGpu's scissor and stencil state. If the return is false
51 * then the draw can be skipped.
52 */
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000053 bool setupClipping(const GrClipData* clipDataIn, GrDrawState::AutoRestoreEffects*);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000054
robertphillips@google.comf105b102012-05-14 12:18:26 +000055 void releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +000056
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000057 bool isClipInStencil() const {
58 return kStencil_ClipMaskType == fCurrClipMaskType;
59 }
60 bool isClipInAlpha() const {
61 return kAlpha_ClipMaskType == fCurrClipMaskType;
62 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000063
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000064 void invalidateStencilMask() {
65 if (kStencil_ClipMaskType == fCurrClipMaskType) {
66 fCurrClipMaskType = kNone_ClipMaskType;
67 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000068 }
69
robertphillips@google.com2c756812012-05-22 20:28:23 +000070 GrContext* getContext() {
71 return fAACache.getContext();
72 }
73
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000074 void setGpu(GrGpu* gpu);
bsalomon@google.coma3201942012-06-21 19:58:20 +000075private:
bsalomon@google.com411dad02012-06-05 20:24:20 +000076 /**
77 * Informs the helper function adjustStencilParams() about how the stencil
78 * buffer clip is being used.
79 */
80 enum StencilClipMode {
81 // Draw to the clip bit of the stencil buffer
82 kModifyClip_StencilClipMode,
83 // Clip against the existing representation of the clip in the high bit
84 // of the stencil buffer.
85 kRespectClip_StencilClipMode,
86 // Neither writing to nor clipping against the clip bit.
87 kIgnoreClip_StencilClipMode,
88 };
89
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000090 GrGpu* fGpu;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000091
92 /**
93 * We may represent the clip as a mask in the stencil buffer or as an alpha
94 * texture. It may be neither because the scissor rect suffices or we
95 * haven't yet examined the clip.
96 */
97 enum ClipMaskType {
98 kNone_ClipMaskType,
99 kStencil_ClipMaskType,
100 kAlpha_ClipMaskType,
101 } fCurrClipMaskType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000103 GrClipMaskCache fAACache; // cache for the AA path
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000104
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000105 // Draws the clip into the stencil buffer
106 bool createStencilClipMask(GrReducedClip::InitialState initialState,
107 const GrReducedClip::ElementList& elements,
108 const SkIRect& clipSpaceIBounds,
109 const SkIPoint& clipSpaceToStencilOffset);
110 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
111 // rect specified by clipSpaceIBounds.
112 GrTexture* createAlphaClipMask(int32_t clipStackGenID,
113 GrReducedClip::InitialState initialState,
114 const GrReducedClip::ElementList& elements,
115 const SkIRect& clipSpaceIBounds);
116 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
117 GrTexture* createSoftwareClipMask(int32_t clipStackGenID,
118 GrReducedClip::InitialState initialState,
119 const GrReducedClip::ElementList& elements,
120 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000121
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000122 // Gets a texture to use for the clip mask. If true is returned then a cached mask was found
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000123 // that already contains the rasterization of the clip stack, otherwise an uninitialized texture
124 // is returned.
125 bool getMaskTexture(int32_t clipStackGenID,
126 const SkIRect& clipSpaceIBounds,
127 GrTexture** result);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000128
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000129 bool useSWOnlyPath(const GrReducedClip::ElementList& elements);
130
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000131 // Draws a clip element into the target alpha mask. The caller should have already setup the
132 // desired blend operation. Optionally if the caller already selected a path renderer it can
133 // be passed. Otherwise the function will select one if the element is a path.
134 bool drawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer* = NULL);
135
136 // Determines whether it is possible to draw the element to both the stencil buffer and the
137 // alpha mask simultaneously. If so and the element is a path a compatible path renderer is
138 // also returned.
139 bool canStencilAndDrawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer**);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000140
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000141 void mergeMask(GrTexture* dstMask,
142 GrTexture* srcMask,
143 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000144 const SkIRect& dstBound,
145 const SkIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000146
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000147 void getTemp(int width, int height, GrAutoScratchTexture* temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000148
rmistry@google.comd6176b02012-08-23 18:14:13 +0000149 void setupCache(const SkClipStack& clip,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000150 const SkIRect& bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000151
bsalomon@google.coma3201942012-06-21 19:58:20 +0000152 /**
153 * Called prior to return control back the GrGpu in setupClipping. It
154 * updates the GrGpu with stencil settings that account stencil-based
155 * clipping.
156 */
157 void setGpuStencil();
158
159 /**
160 * Adjusts the stencil settings to account for interaction with stencil
161 * clipping.
162 */
163 void adjustStencilParams(GrStencilSettings* settings,
164 StencilClipMode mode,
165 int stencilBitCnt);
166
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000167 typedef GrNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000168};
169
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000170#endif // GrClipMaskManager_DEFINED