blob: 2a0dbc022a99f804b1f562ff0cbda18968777dd8 [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() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000026 // TODO: allow SB to be purged and detach itself from rts
bsalomon@google.com558a75b2011-08-08 17:01:14 +000027 }
28
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000029 int width() const { return fWidth; }
30 int height() const { return fHeight; }
31 int bits() const { return fBits; }
bsalomon@google.com558a75b2011-08-08 17:01:14 +000032 int numSamples() const { return fSampleCnt; }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000033
34 // called to note the last clip drawn to this buffer.
robertphillips@google.combeb1af72012-07-26 18:52:16 +000035 void setLastClip(const GrClipData& clipData, int width, int height) {
36 // the clip stack needs to be copied separately (and deeply) since
37 // it could change beneath the stencil buffer
38 fLastClipStack = *clipData.fClipStack;
39 fLastClipData.fClipStack = &fLastClipStack;
40 fLastClipData.fOrigin = clipData.fOrigin;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000041 fLastClipWidth = width;
42 fLastClipHeight = height;
43 GrAssert(width <= fWidth);
44 GrAssert(height <= fHeight);
45 }
46
47 // called to determine if we have to render the clip into SB.
robertphillips@google.combeb1af72012-07-26 18:52:16 +000048 bool mustRenderClip(const GrClipData& clipData, int width, int height) const {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000049 // The clip is in device space. That is it doesn't scale to fit a
50 // smaller RT. It is just truncated on the right / bottom edges.
51 // Note that this assumes that the viewport origin never moves within
52 // the stencil buffer. This is valid today.
53 return width > fLastClipWidth ||
54 height > fLastClipHeight ||
robertphillips@google.combeb1af72012-07-26 18:52:16 +000055 clipData != fLastClipData;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000056 }
57
robertphillips@google.combeb1af72012-07-26 18:52:16 +000058 const GrClipData& getLastClip() const {
59 return fLastClipData;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000060 }
61
robertphillips@google.com9fbcad02012-09-09 14:44:15 +000062 // Places the sb in the cache. The cache takes a ref of the stencil buffer.
63 void transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +000064
robertphillips@google.com46a86002012-08-08 10:42:44 +000065 static GrResourceKey ComputeKey(int width, int height, int sampleCnt);
66
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000067protected:
bsalomon@google.com558a75b2011-08-08 17:01:14 +000068 GrStencilBuffer(GrGpu* gpu, int width, int height, int bits, int sampleCnt)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000069 : GrResource(gpu)
70 , fWidth(width)
71 , fHeight(height)
72 , fBits(bits)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000073 , fSampleCnt(sampleCnt)
robertphillips@google.combeb1af72012-07-26 18:52:16 +000074 , fLastClipStack()
75 , fLastClipData()
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000076 , fLastClipWidth(-1)
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000077 , fLastClipHeight(-1) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000078 }
79
80private:
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000081
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000082 int fWidth;
83 int fHeight;
84 int fBits;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000085 int fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000086
robertphillips@google.com641f8b12012-07-31 19:15:58 +000087 SkClipStack fLastClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +000088 GrClipData fLastClipData;
89 int fLastClipWidth;
90 int fLastClipHeight;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000091
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000092 typedef GrResource INHERITED;
93};
94
95#endif