blob: 7439c761bcb4161b4a9d073d18eae41e291dee97 [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.com7fa18762012-09-11 13:02:31 +000023 SK_DECLARE_INST_COUNT(GrStencilBuffer);
robertphillips@google.com46a86002012-08-08 10:42:44 +000024 GR_DECLARE_RESOURCE_CACHE_TYPE()
25
bsalomon@google.com558a75b2011-08-08 17:01:14 +000026 virtual ~GrStencilBuffer() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000027 // TODO: allow SB to be purged and detach itself from rts
bsalomon@google.com558a75b2011-08-08 17:01:14 +000028 }
29
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000030 int width() const { return fWidth; }
31 int height() const { return fHeight; }
32 int bits() const { return fBits; }
bsalomon@google.com558a75b2011-08-08 17:01:14 +000033 int numSamples() const { return fSampleCnt; }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000034
35 // called to note the last clip drawn to this buffer.
robertphillips@google.combeb1af72012-07-26 18:52:16 +000036 void setLastClip(const GrClipData& clipData, int width, int height) {
37 // the clip stack needs to be copied separately (and deeply) since
38 // it could change beneath the stencil buffer
39 fLastClipStack = *clipData.fClipStack;
40 fLastClipData.fClipStack = &fLastClipStack;
41 fLastClipData.fOrigin = clipData.fOrigin;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000042 fLastClipWidth = width;
43 fLastClipHeight = height;
44 GrAssert(width <= fWidth);
45 GrAssert(height <= fHeight);
46 }
47
48 // called to determine if we have to render the clip into SB.
robertphillips@google.combeb1af72012-07-26 18:52:16 +000049 bool mustRenderClip(const GrClipData& clipData, int width, int height) const {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000050 // The clip is in device space. That is it doesn't scale to fit a
51 // smaller RT. It is just truncated on the right / bottom edges.
52 // Note that this assumes that the viewport origin never moves within
53 // the stencil buffer. This is valid today.
54 return width > fLastClipWidth ||
55 height > fLastClipHeight ||
robertphillips@google.combeb1af72012-07-26 18:52:16 +000056 clipData != fLastClipData;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000057 }
58
robertphillips@google.combeb1af72012-07-26 18:52:16 +000059 const GrClipData& getLastClip() const {
60 return fLastClipData;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000061 }
62
robertphillips@google.com9fbcad02012-09-09 14:44:15 +000063 // Places the sb in the cache. The cache takes a ref of the stencil buffer.
64 void transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +000065
robertphillips@google.com46a86002012-08-08 10:42:44 +000066 static GrResourceKey ComputeKey(int width, int height, int sampleCnt);
67
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000068protected:
bsalomon@google.com558a75b2011-08-08 17:01:14 +000069 GrStencilBuffer(GrGpu* gpu, int width, int height, int bits, int sampleCnt)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000070 : GrResource(gpu)
71 , fWidth(width)
72 , fHeight(height)
73 , fBits(bits)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000074 , fSampleCnt(sampleCnt)
robertphillips@google.combeb1af72012-07-26 18:52:16 +000075 , fLastClipStack()
76 , fLastClipData()
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000077 , fLastClipWidth(-1)
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000078 , fLastClipHeight(-1) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000079 }
80
81private:
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000082
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000083 int fWidth;
84 int fHeight;
85 int fBits;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000086 int fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000087
robertphillips@google.com641f8b12012-07-31 19:15:58 +000088 SkClipStack fLastClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +000089 GrClipData fLastClipData;
90 int fLastClipWidth;
91 int fLastClipHeight;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000092
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000093 typedef GrResource INHERITED;
94};
95
96#endif