blob: 690fa329a879cea5d8a488df1bc948ef6f25a9b7 [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.comf2e93fc2012-09-05 19:44:18 +000062 // Places the sb in the cache and locks it. The cache takes a ref
63 // of the stencil buffer.
bsalomon@google.com558a75b2011-08-08 17:01:14 +000064 void transferToCacheAndLock();
65
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