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