• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 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 
10 #ifndef GrStencilBuffer_DEFINED
11 #define GrStencilBuffer_DEFINED
12 
13 #include "GrClip.h"
14 #include "GrResource.h"
15 
16 class GrRenderTarget;
17 class GrResourceEntry;
18 
19 class GrStencilBuffer : public GrResource {
20 public:
~GrStencilBuffer()21     virtual ~GrStencilBuffer() {
22         // currently each rt that has attached this sb keeps a ref
23         // TODO: allow SB to be purged and detach itself from rts
24         GrAssert(0 == fRTAttachmentCnt);
25     }
26 
width()27     int width() const { return fWidth; }
height()28     int height() const { return fHeight; }
bits()29     int bits() const { return fBits; }
numSamples()30     int numSamples() const { return fSampleCnt; }
31 
32     // called to note the last clip drawn to this buffer.
setLastClip(const GrClip & clip,int width,int height)33     void setLastClip(const GrClip& clip, int width, int height) {
34         fLastClip = clip;
35         fLastClipWidth = width;
36         fLastClipHeight = height;
37         GrAssert(width <= fWidth);
38         GrAssert(height <= fHeight);
39     }
40 
41     // called to determine if we have to render the clip into SB.
mustRenderClip(const GrClip & clip,int width,int height)42     bool mustRenderClip(const GrClip& clip, int width, int height) const {
43         // The clip is in device space. That is it doesn't scale to fit a
44         // smaller RT. It is just truncated on the right / bottom edges.
45         // Note that this assumes that the viewport origin never moves within
46         // the stencil buffer. This is valid today.
47         return width > fLastClipWidth ||
48                height > fLastClipHeight ||
49                clip != fLastClip;
50     }
51 
getLastClip()52     const GrClip& getLastClip() const {
53         return fLastClip;
54     }
55 
56     // places the sb in the cache and locks it. Caller transfers
57     // a ref to the the cache which will unref when purged.
58     void transferToCacheAndLock();
59 
wasAttachedToRenderTarget(const GrRenderTarget * rt)60     void wasAttachedToRenderTarget(const GrRenderTarget* rt) {
61         ++fRTAttachmentCnt;
62     }
63 
64     void wasDetachedFromRenderTarget(const GrRenderTarget* rt);
65 
66 protected:
GrStencilBuffer(GrGpu * gpu,int width,int height,int bits,int sampleCnt)67     GrStencilBuffer(GrGpu* gpu, int width, int height, int bits, int sampleCnt)
68         : GrResource(gpu)
69         , fWidth(width)
70         , fHeight(height)
71         , fBits(bits)
72         , fSampleCnt(sampleCnt)
73         , fLastClip()
74         , fLastClipWidth(-1)
75         , fLastClipHeight(-1)
76         , fCacheEntry(NULL)
77         , fRTAttachmentCnt(0) {
78     }
79 
80     // GrResource overrides
81 
82     // subclass override must call INHERITED::onRelease
83     virtual void onRelease();
84     // subclass override must call INHERITED::onAbandon
85     virtual void onAbandon();
86 
87 private:
88 
89     void unlockInCache();
90 
91     int fWidth;
92     int fHeight;
93     int fBits;
94     int fSampleCnt;
95 
96     GrClip     fLastClip;
97     int        fLastClipWidth;
98     int        fLastClipHeight;
99 
100     GrResourceEntry* fCacheEntry;
101     int              fRTAttachmentCnt;
102 
103     typedef GrResource INHERITED;
104 };
105 
106 #endif
107