blob: f4554b67c4f7360e6ea1cec3e35375774b0f6750 [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"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000022#include "SkTLList.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000023
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000024#include "GrClipMaskCache.h"
25
robertphillips@google.com1e945b72012-04-16 18:03:03 +000026class GrGpu;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000027class GrPathRenderer;
28class GrPathRendererChain;
29class SkPath;
robertphillips@google.comf294b772012-04-27 14:29:26 +000030class GrTexture;
31class GrDrawState;
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.com46a86002012-08-08 10:42:44 +000043 GR_DECLARE_RESOURCE_CACHE_DOMAIN(GetAlphaMaskDomain)
44
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000045 GrClipMaskManager()
46 : fGpu(NULL)
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000047 , fCurrClipMaskType(kNone_ClipMaskType) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000048 }
49
bsalomon@google.coma3201942012-06-21 19:58:20 +000050 /**
51 * Creates a clip mask if necessary as a stencil buffer or alpha texture
52 * and sets the GrGpu's scissor and stencil state. If the return is false
53 * then the draw can be skipped.
54 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +000055 bool setupClipping(const GrClipData* clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000056
robertphillips@google.comf105b102012-05-14 12:18:26 +000057 void releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +000058
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000059 bool isClipInStencil() const {
60 return kStencil_ClipMaskType == fCurrClipMaskType;
61 }
62 bool isClipInAlpha() const {
63 return kAlpha_ClipMaskType == fCurrClipMaskType;
64 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000065
bsalomon@google.comc8f7f472012-06-18 13:44:51 +000066 void invalidateStencilMask() {
67 if (kStencil_ClipMaskType == fCurrClipMaskType) {
68 fCurrClipMaskType = kNone_ClipMaskType;
69 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +000070 }
71
robertphillips@google.comf105b102012-05-14 12:18:26 +000072 void setContext(GrContext* context) {
73 fAACache.setContext(context);
74 }
75
robertphillips@google.com2c756812012-05-22 20:28:23 +000076 GrContext* getContext() {
77 return fAACache.getContext();
78 }
79
rmistry@google.comd6176b02012-08-23 18:14:13 +000080 void setGpu(GrGpu* gpu) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000081 fGpu = gpu;
82 }
83
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
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000114 bool createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000115 const GrIRect& devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000116 bool createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000117 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000118 GrIRect *devResultBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000119 bool createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000120 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000121 GrIRect *devResultBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000122 bool clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000123 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000124 GrIRect *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000125
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000126 bool useSWOnlyPath(const SkClipStack& clipIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000127
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000128 bool drawClipShape(GrTexture* target,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000129 const SkClipStack::Element* element,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000130 const GrIRect& resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000131
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000132 void mergeMask(GrTexture* dstMask,
133 GrTexture* srcMask,
134 SkRegion::Op op,
135 const GrIRect& dstBound,
136 const GrIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000137
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000138 void getTemp(const GrIRect& bounds, GrAutoScratchTexture* temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000139
rmistry@google.comd6176b02012-08-23 18:14:13 +0000140 void setupCache(const SkClipStack& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000141 const GrIRect& bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000142
bsalomon@google.coma3201942012-06-21 19:58:20 +0000143 /**
144 * Called prior to return control back the GrGpu in setupClipping. It
145 * updates the GrGpu with stencil settings that account stencil-based
146 * clipping.
147 */
148 void setGpuStencil();
149
150 /**
151 * Adjusts the stencil settings to account for interaction with stencil
152 * clipping.
153 */
154 void adjustStencilParams(GrStencilSettings* settings,
155 StencilClipMode mode,
156 int stencilBitCnt);
157
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000158 typedef GrNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000159};
160
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000161#endif // GrClipMaskManager_DEFINED