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