blob: eaaa2e3f77a54f8d89b9af4852a99326a70219b1 [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.com411dad02012-06-05 20:24:20 +000013#include "GrNoncopyable.h"
14#include "GrRect.h"
15#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"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000022
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000023#include "GrClipMaskCache.h"
24
robertphillips@google.com1e945b72012-04-16 18:03:03 +000025class GrGpu;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000026class GrPathRenderer;
27class GrPathRendererChain;
28class SkPath;
robertphillips@google.comf294b772012-04-27 14:29:26 +000029class GrTexture;
30class GrDrawState;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000031
32/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000033 * The clip mask creator handles the generation of the clip mask. If anti
34 * aliasing is requested it will (in the future) generate a single channel
35 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
robertphillips@google.com1e945b72012-04-16 18:03:03 +000036 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
37 * mask can be represented as a rectangle then scissoring is used. In all
38 * cases scissoring is used to bound the range of the clip mask.
39 */
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000040class GrClipMaskManager : public GrNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000041public:
robertphillips@google.com46a86002012-08-08 10:42:44 +000042 GR_DECLARE_RESOURCE_CACHE_DOMAIN(GetAlphaMaskDomain)
43
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000044 GrClipMaskManager()
45 : fGpu(NULL)
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000046 , fCurrClipMaskType(kNone_ClipMaskType) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000047 }
48
bsalomon@google.coma3201942012-06-21 19:58:20 +000049 /**
50 * Creates a clip mask if necessary as a stencil buffer or alpha texture
51 * and sets the GrGpu's scissor and stencil state. If the return is false
52 * then the draw can be skipped.
53 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000054 bool setupClipping(const GrClipData* clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000055
robertphillips@google.comf105b102012-05-14 12:18:26 +000056 void releaseResources();
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 }
61 bool isClipInAlpha() const {
62 return kAlpha_ClipMaskType == fCurrClipMaskType;
63 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000064
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000065 void invalidateStencilMask() {
66 if (kStencil_ClipMaskType == fCurrClipMaskType) {
67 fCurrClipMaskType = kNone_ClipMaskType;
68 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000069 }
70
robertphillips@google.comf105b102012-05-14 12:18:26 +000071 void setContext(GrContext* context) {
72 fAACache.setContext(context);
73 }
74
robertphillips@google.com2c756812012-05-22 20:28:23 +000075 GrContext* getContext() {
76 return fAACache.getContext();
77 }
78
rmistry@google.comd6176b02012-08-23 18:14:13 +000079 void setGpu(GrGpu* gpu) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000080 fGpu = gpu;
81 }
82
bsalomon@google.coma3201942012-06-21 19:58:20 +000083private:
bsalomon@google.com411dad02012-06-05 20:24:20 +000084 /**
85 * Informs the helper function adjustStencilParams() about how the stencil
86 * buffer clip is being used.
87 */
88 enum StencilClipMode {
89 // Draw to the clip bit of the stencil buffer
90 kModifyClip_StencilClipMode,
91 // Clip against the existing representation of the clip in the high bit
92 // of the stencil buffer.
93 kRespectClip_StencilClipMode,
94 // Neither writing to nor clipping against the clip bit.
95 kIgnoreClip_StencilClipMode,
96 };
97
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000098 GrGpu* fGpu;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000099
100 /**
101 * We may represent the clip as a mask in the stencil buffer or as an alpha
102 * texture. It may be neither because the scissor rect suffices or we
103 * haven't yet examined the clip.
104 */
105 enum ClipMaskType {
106 kNone_ClipMaskType,
107 kStencil_ClipMaskType,
108 kAlpha_ClipMaskType,
109 } fCurrClipMaskType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000111 GrClipMaskCache fAACache; // cache for the AA path
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000112
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000113 bool createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000114 const GrIRect& devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000115 bool createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000116 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000117 GrIRect *devResultBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000118 bool createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000119 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000120 GrIRect *devResultBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000121 bool clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000122 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000123 GrIRect *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000124
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000125 bool useSWOnlyPath(const SkClipStack& clipIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000126
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000127 bool drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000128 const SkClipStack::Iter::Clip* clip,
bsalomon@google.com6794a252012-11-08 15:30:53 +0000129 const SkIRect& clipRect);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000130
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000131 void mergeMask(GrTexture* dstMask,
132 GrTexture* srcMask,
133 SkRegion::Op op,
134 const GrIRect& dstBound,
135 const GrIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000136
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000137 void getTemp(const GrIRect& bounds, GrAutoScratchTexture* temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000138
rmistry@google.comd6176b02012-08-23 18:14:13 +0000139 void setupCache(const SkClipStack& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000140 const GrIRect& bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000141
bsalomon@google.coma3201942012-06-21 19:58:20 +0000142 /**
143 * Called prior to return control back the GrGpu in setupClipping. It
144 * updates the GrGpu with stencil settings that account stencil-based
145 * clipping.
146 */
147 void setGpuStencil();
148
149 /**
150 * Adjusts the stencil settings to account for interaction with stencil
151 * clipping.
152 */
153 void adjustStencilParams(GrStencilSettings* settings,
154 StencilClipMode mode,
155 int stencilBitCnt);
156
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000157 typedef GrNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000158};
159
160#endif // GrClipMaskManager_DEFINED