blob: 9767b1733f283076c81d12da91e6dad1b13feeba [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
robertphillips@google.coma2d71482012-08-01 20:08:47 +000013#include "GrClipData.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000014#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.
robertphillips@google.combeb1af72012-07-26 18:52:16 +000033 void setLastClip(const GrClipData& clipData, int width, int height) {
34 // the clip stack needs to be copied separately (and deeply) since
35 // it could change beneath the stencil buffer
36 fLastClipStack = *clipData.fClipStack;
37 fLastClipData.fClipStack = &fLastClipStack;
38 fLastClipData.fOrigin = clipData.fOrigin;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000039 fLastClipWidth = width;
40 fLastClipHeight = height;
41 GrAssert(width <= fWidth);
42 GrAssert(height <= fHeight);
43 }
44
45 // called to determine if we have to render the clip into SB.
robertphillips@google.combeb1af72012-07-26 18:52:16 +000046 bool mustRenderClip(const GrClipData& clipData, int width, int height) const {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000047 // The clip is in device space. That is it doesn't scale to fit a
48 // smaller RT. It is just truncated on the right / bottom edges.
49 // Note that this assumes that the viewport origin never moves within
50 // the stencil buffer. This is valid today.
51 return width > fLastClipWidth ||
52 height > fLastClipHeight ||
robertphillips@google.combeb1af72012-07-26 18:52:16 +000053 clipData != fLastClipData;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000054 }
55
robertphillips@google.combeb1af72012-07-26 18:52:16 +000056 const GrClipData& getLastClip() const {
57 return fLastClipData;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000058 }
59
bsalomon@google.com558a75b2011-08-08 17:01:14 +000060 // places the sb in the cache and locks it. Caller transfers
61 // a ref to the the cache which will unref when purged.
62 void transferToCacheAndLock();
63
64 void wasAttachedToRenderTarget(const GrRenderTarget* rt) {
65 ++fRTAttachmentCnt;
66 }
67
68 void wasDetachedFromRenderTarget(const GrRenderTarget* rt);
69
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000070protected:
bsalomon@google.com558a75b2011-08-08 17:01:14 +000071 GrStencilBuffer(GrGpu* gpu, int width, int height, int bits, int sampleCnt)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000072 : GrResource(gpu)
73 , fWidth(width)
74 , fHeight(height)
75 , fBits(bits)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000076 , fSampleCnt(sampleCnt)
robertphillips@google.combeb1af72012-07-26 18:52:16 +000077 , fLastClipStack()
78 , fLastClipData()
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000079 , fLastClipWidth(-1)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000080 , fLastClipHeight(-1)
81 , fCacheEntry(NULL)
82 , fRTAttachmentCnt(0) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000083 }
84
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000085 // GrResource overrides
86
87 // subclass override must call INHERITED::onRelease
88 virtual void onRelease();
89 // subclass override must call INHERITED::onAbandon
90 virtual void onAbandon();
91
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000092private:
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000093
94 void unlockInCache();
95
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000096 int fWidth;
97 int fHeight;
98 int fBits;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000099 int fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000100
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000101 SkClipStack fLastClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000102 GrClipData fLastClipData;
103 int fLastClipWidth;
104 int fLastClipHeight;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000105
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000106 GrResourceEntry* fCacheEntry;
107 int fRTAttachmentCnt;
108
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000109 typedef GrResource INHERITED;
110};
111
112#endif