blob: 5249ce8466ed65a476f1fb58b3aa28a06cb8e7b5 [file] [log] [blame]
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001
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
bsalomon@google.com558a75b2011-08-08 17:01:14 +000016class GrRenderTarget;
17class GrResourceEntry;
18
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000019class GrStencilBuffer : public GrResource {
20public:
bsalomon@google.com558a75b2011-08-08 17:01:14 +000021 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
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000027 int width() const { return fWidth; }
28 int height() const { return fHeight; }
29 int bits() const { return fBits; }
bsalomon@google.com558a75b2011-08-08 17:01:14 +000030 int numSamples() const { return fSampleCnt; }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000031
32 // called to note the last clip drawn to this buffer.
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.
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
52 const GrClip& getLastClip() const {
53 return fLastClip;
54 }
55
bsalomon@google.com558a75b2011-08-08 17:01:14 +000056 // 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
60 void wasAttachedToRenderTarget(const GrRenderTarget* rt) {
61 ++fRTAttachmentCnt;
62 }
63
64 void wasDetachedFromRenderTarget(const GrRenderTarget* rt);
65
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000066protected:
bsalomon@google.com558a75b2011-08-08 17:01:14 +000067 GrStencilBuffer(GrGpu* gpu, int width, int height, int bits, int sampleCnt)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000068 : GrResource(gpu)
69 , fWidth(width)
70 , fHeight(height)
71 , fBits(bits)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000072 , fSampleCnt(sampleCnt)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000073 , fLastClip()
74 , fLastClipWidth(-1)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000075 , fLastClipHeight(-1)
76 , fCacheEntry(NULL)
77 , fRTAttachmentCnt(0) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000078 }
79
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000080 // 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
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000087private:
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000088
89 void unlockInCache();
90
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000091 int fWidth;
92 int fHeight;
93 int fBits;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000094 int fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000095
96 GrClip fLastClip;
97 int fLastClipWidth;
98 int fLastClipHeight;
99
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000100 GrResourceEntry* fCacheEntry;
101 int fRTAttachmentCnt;
102
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000103 typedef GrResource INHERITED;
104};
105
106#endif