bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 13 | #include "GrClip.h" |
| 14 | #include "GrResource.h" |
| 15 | |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 16 | class GrRenderTarget; |
| 17 | class GrResourceEntry; |
| 18 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 19 | class GrStencilBuffer : public GrResource { |
| 20 | public: |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 21 | 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 27 | int width() const { return fWidth; } |
| 28 | int height() const { return fHeight; } |
| 29 | int bits() const { return fBits; } |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 30 | int numSamples() const { return fSampleCnt; } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 31 | |
| 32 | // called to note the last clip drawn to this buffer. |
| 33 | void setLastClip(const GrClip& clip, int width, int height) { |
| 34 | fLastClip = clip; |
| 35 | fLastClipWidth = width; |
| 36 | fLastClipHeight = height; |
| 37 | GrAssert(width <= fWidth); |
| 38 | GrAssert(height <= fHeight); |
| 39 | } |
| 40 | |
| 41 | // called to determine if we have to render the clip into SB. |
| 42 | bool mustRenderClip(const GrClip& clip, int width, int height) const { |
| 43 | // The clip is in device space. That is it doesn't scale to fit a |
| 44 | // smaller RT. It is just truncated on the right / bottom edges. |
| 45 | // Note that this assumes that the viewport origin never moves within |
| 46 | // the stencil buffer. This is valid today. |
| 47 | return width > fLastClipWidth || |
| 48 | height > fLastClipHeight || |
| 49 | clip != fLastClip; |
| 50 | } |
| 51 | |
| 52 | const GrClip& getLastClip() const { |
| 53 | return fLastClip; |
| 54 | } |
| 55 | |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 56 | // places the sb in the cache and locks it. Caller transfers |
| 57 | // a ref to the the cache which will unref when purged. |
| 58 | void transferToCacheAndLock(); |
| 59 | |
| 60 | void wasAttachedToRenderTarget(const GrRenderTarget* rt) { |
| 61 | ++fRTAttachmentCnt; |
| 62 | } |
| 63 | |
| 64 | void wasDetachedFromRenderTarget(const GrRenderTarget* rt); |
| 65 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 66 | protected: |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 67 | GrStencilBuffer(GrGpu* gpu, int width, int height, int bits, int sampleCnt) |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 68 | : GrResource(gpu) |
| 69 | , fWidth(width) |
| 70 | , fHeight(height) |
| 71 | , fBits(bits) |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 72 | , fSampleCnt(sampleCnt) |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 73 | , fLastClip() |
| 74 | , fLastClipWidth(-1) |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 75 | , fLastClipHeight(-1) |
| 76 | , fCacheEntry(NULL) |
| 77 | , fRTAttachmentCnt(0) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 78 | } |
| 79 | |
bsalomon@google.com | eefe6f1 | 2011-08-09 17:57:12 +0000 | [diff] [blame] | 80 | // GrResource overrides |
| 81 | |
| 82 | // subclass override must call INHERITED::onRelease |
| 83 | virtual void onRelease(); |
| 84 | // subclass override must call INHERITED::onAbandon |
| 85 | virtual void onAbandon(); |
| 86 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 87 | private: |
bsalomon@google.com | eefe6f1 | 2011-08-09 17:57:12 +0000 | [diff] [blame] | 88 | |
| 89 | void unlockInCache(); |
| 90 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 91 | int fWidth; |
| 92 | int fHeight; |
| 93 | int fBits; |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 94 | int fSampleCnt; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 95 | |
| 96 | GrClip fLastClip; |
| 97 | int fLastClipWidth; |
| 98 | int fLastClipHeight; |
| 99 | |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 100 | GrResourceEntry* fCacheEntry; |
| 101 | int fRTAttachmentCnt; |
| 102 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 103 | typedef GrResource INHERITED; |
| 104 | }; |
| 105 | |
| 106 | #endif |