blob: f83590a54196c6d626defbe873a62e869583ad1c [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"
robertphillips@google.com46a86002012-08-08 10:42:44 +000015#include "GrCacheID.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000016
bsalomon@google.com558a75b2011-08-08 17:01:14 +000017class GrRenderTarget;
18class GrResourceEntry;
robertphillips@google.com46a86002012-08-08 10:42:44 +000019class GrResourceKey;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000020
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000021class GrStencilBuffer : public GrResource {
22public:
robertphillips@google.com46a86002012-08-08 10:42:44 +000023 GR_DECLARE_RESOURCE_CACHE_TYPE()
24
bsalomon@google.com558a75b2011-08-08 17:01:14 +000025 virtual ~GrStencilBuffer() {
26 // currently each rt that has attached this sb keeps a ref
27 // TODO: allow SB to be purged and detach itself from rts
28 GrAssert(0 == fRTAttachmentCnt);
29 }
30
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000031 int width() const { return fWidth; }
32 int height() const { return fHeight; }
33 int bits() const { return fBits; }
bsalomon@google.com558a75b2011-08-08 17:01:14 +000034 int numSamples() const { return fSampleCnt; }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000035
36 // called to note the last clip drawn to this buffer.
robertphillips@google.combeb1af72012-07-26 18:52:16 +000037 void setLastClip(const GrClipData& clipData, int width, int height) {
38 // the clip stack needs to be copied separately (and deeply) since
39 // it could change beneath the stencil buffer
40 fLastClipStack = *clipData.fClipStack;
41 fLastClipData.fClipStack = &fLastClipStack;
42 fLastClipData.fOrigin = clipData.fOrigin;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000043 fLastClipWidth = width;
44 fLastClipHeight = height;
45 GrAssert(width <= fWidth);
46 GrAssert(height <= fHeight);
47 }
48
49 // called to determine if we have to render the clip into SB.
robertphillips@google.combeb1af72012-07-26 18:52:16 +000050 bool mustRenderClip(const GrClipData& clipData, int width, int height) const {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000051 // The clip is in device space. That is it doesn't scale to fit a
52 // smaller RT. It is just truncated on the right / bottom edges.
53 // Note that this assumes that the viewport origin never moves within
54 // the stencil buffer. This is valid today.
55 return width > fLastClipWidth ||
56 height > fLastClipHeight ||
robertphillips@google.combeb1af72012-07-26 18:52:16 +000057 clipData != fLastClipData;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000058 }
59
robertphillips@google.combeb1af72012-07-26 18:52:16 +000060 const GrClipData& getLastClip() const {
61 return fLastClipData;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000062 }
63
bsalomon@google.com558a75b2011-08-08 17:01:14 +000064 // places the sb in the cache and locks it. Caller transfers
65 // a ref to the the cache which will unref when purged.
66 void transferToCacheAndLock();
67
68 void wasAttachedToRenderTarget(const GrRenderTarget* rt) {
69 ++fRTAttachmentCnt;
70 }
71
72 void wasDetachedFromRenderTarget(const GrRenderTarget* rt);
73
robertphillips@google.com46a86002012-08-08 10:42:44 +000074 static GrResourceKey ComputeKey(int width, int height, int sampleCnt);
75
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000076protected:
bsalomon@google.com558a75b2011-08-08 17:01:14 +000077 GrStencilBuffer(GrGpu* gpu, int width, int height, int bits, int sampleCnt)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000078 : GrResource(gpu)
79 , fWidth(width)
80 , fHeight(height)
81 , fBits(bits)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000082 , fSampleCnt(sampleCnt)
robertphillips@google.combeb1af72012-07-26 18:52:16 +000083 , fLastClipStack()
84 , fLastClipData()
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000085 , fLastClipWidth(-1)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000086 , fLastClipHeight(-1)
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000087 , fHoldingLock(false)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000088 , fRTAttachmentCnt(0) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000089 }
90
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000091 // GrResource overrides
92
93 // subclass override must call INHERITED::onRelease
94 virtual void onRelease();
95 // subclass override must call INHERITED::onAbandon
96 virtual void onAbandon();
97
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000098private:
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000099
100 void unlockInCache();
101
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000102 int fWidth;
103 int fHeight;
104 int fBits;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000105 int fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000106
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000107 SkClipStack fLastClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000108 GrClipData fLastClipData;
109 int fLastClipWidth;
110 int fLastClipHeight;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000111
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000112 bool fHoldingLock;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000113 int fRTAttachmentCnt;
114
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000115 typedef GrResource INHERITED;
116};
117
118#endif