blob: f8279482022531b105104d009621fc0b11b15f63 [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
12#include "GrRect.h"
bsalomon@google.com8d033a12012-04-27 15:52:53 +000013#include "SkPath.h"
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000014#include "GrNoncopyable.h"
15#include "GrClip.h"
16#include "SkRefCnt.h"
robertphillips@google.com6d62df42012-05-07 18:07:36 +000017#include "GrTexture.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000018
19class GrGpu;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000020class GrPathRenderer;
21class GrPathRendererChain;
22class SkPath;
robertphillips@google.comf294b772012-04-27 14:29:26 +000023class GrTexture;
24class GrDrawState;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000025
26/**
27 * Scissoring needs special handling during stencil clip mask creation
28 * since the creation process re-entrantly invokes setupClipAndFlushState.
29 * During this process the call stack is used to keep
30 * track of (and apply to the GPU) the current scissor settings.
31 */
32struct ScissoringSettings {
33 bool fEnableScissoring;
34 GrIRect fScissorRect;
35
36 void setupScissoring(GrGpu* gpu);
37};
38
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000039/**
40 * The stencil buffer stores the last clip path - providing a single entry
41 * "cache". This class provides similar functionality for AA clip paths
42 */
43class GrClipMaskCache : public GrNoncopyable {
44public:
robertphillips@google.com6d62df42012-05-07 18:07:36 +000045 GrClipMaskCache() {
46 reset();
47 }
48
49 void reset () {
50 fLastWidth = -1;
51 fLastHeight = -1;
52 fLastClip.setEmpty();
53 fLastMask.reset(NULL);
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000054 fLastBound.MakeEmpty();
55 }
56
57 bool canReuse(const GrClip& clip, int width, int height) {
58 if (fLastWidth >= width &&
59 fLastHeight >= height &&
60 clip == fLastClip) {
61 return true;
62 }
63
64 return false;
65 }
66
67 void set(const GrClip& clip, int width, int height,
68 GrTexture* mask, const GrRect& bound) {
69
70 fLastWidth = width;
71 fLastHeight = height;
72 fLastClip = clip;
73 SkSafeRef(mask);
74 fLastMask.reset(mask);
75 fLastBound = bound;
76 }
77
robertphillips@google.com6d62df42012-05-07 18:07:36 +000078 int getLastWidth() const {
79 return fLastWidth;
80 }
81
82 int getLastHeight() const {
83 return fLastHeight;
84 }
85
86 const GrClip& getLastClip() const {
87 return fLastClip;
88 }
89
90 GrTexture* getLastMask() {
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000091 return fLastMask.get();
92 }
93
robertphillips@google.com6d62df42012-05-07 18:07:36 +000094 GrTexture* detachLastMask() {
95 return fLastMask.detach();
96 }
97
98 int getLastMaskWidth() const {
99 if (NULL == fLastMask.get()) {
100 return -1;
101 }
102
103 return fLastMask.get()->width();
104 }
105
106 int getLastMaskHeight() const {
107 if (NULL == fLastMask.get()) {
108 return -1;
109 }
110
111 return fLastMask.get()->height();
112 }
113
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000114 const GrRect& getLastBound() const {
115 return fLastBound;
116 }
117
118protected:
119private:
120 int fLastWidth;
121 int fLastHeight;
122 GrClip fLastClip;
123 SkAutoTUnref<GrTexture> fLastMask;
124 GrRect fLastBound;
125
126 typedef GrNoncopyable INHERITED;
127};
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000128
129/**
130 * The clip mask creator handles the generation of the clip mask. If anti
131 * aliasing is requested it will (in the future) generate a single channel
132 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
133 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
134 * mask can be represented as a rectangle then scissoring is used. In all
135 * cases scissoring is used to bound the range of the clip mask.
136 */
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000137class GrClipMaskManager : public GrNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000138public:
139 GrClipMaskManager()
140 : fClipMaskInStencil(false)
robertphillips@google.comf294b772012-04-27 14:29:26 +0000141 , fClipMaskInAlpha(false)
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000142 , fPathRendererChain(NULL) {
143 }
144
145 bool createClipMask(GrGpu* gpu,
146 const GrClip& clip,
147 ScissoringSettings* scissorSettings);
148
149 void freeResources();
150
151 bool isClipInStencil() const { return fClipMaskInStencil; }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000152 bool isClipInAlpha() const { return fClipMaskInAlpha; }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000153
154 void resetMask() {
155 fClipMaskInStencil = false;
156 }
157
158protected:
159private:
160 bool fClipMaskInStencil; // is the clip mask in the stencil buffer?
robertphillips@google.comf294b772012-04-27 14:29:26 +0000161 bool fClipMaskInAlpha; // is the clip mask in an alpha texture?
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000162 GrClipMaskCache fAACache; // cache for the AA path
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000163
164 // must be instantiated after GrGpu object has been given its owning
165 // GrContext ptr. (GrGpu is constructed first then handed off to GrContext).
166 GrPathRendererChain* fPathRendererChain;
167
168 bool createStencilClipMask(GrGpu* gpu,
169 const GrClip& clip,
170 const GrRect& bounds,
171 ScissoringSettings* scissorSettings);
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000172 bool createAlphaClipMask(GrGpu* gpu,
173 const GrClip& clipIn,
174 GrTexture** result,
175 GrRect *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000176
177 bool drawPath(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000178 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000179 GrPathFill fill,
180 bool doAA);
181
182 bool drawClipShape(GrGpu* gpu,
183 GrTexture* target,
184 const GrClip& clipIn,
185 int index);
186
187 void drawTexture(GrGpu* gpu,
188 GrTexture* target,
189 const GrRect& rect,
190 GrTexture* texture);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000191
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000192 void getAccum(GrGpu* gpu,
193 const GrTextureDesc& desc,
194 GrTexture** accum);
195
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000196 // determines the path renderer used to draw a clip path element.
197 GrPathRenderer* getClipPathRenderer(GrGpu* gpu,
198 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000199 GrPathFill fill,
200 bool antiAlias);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000201
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000202 typedef GrNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000203};
204
205#endif // GrClipMaskManager_DEFINED