bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +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 | #include "GrStencilBuffer.h" |
| 10 | |
| 11 | #include "GrContext.h" |
| 12 | #include "GrGpu.h" |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 13 | #include "GrResourceCache.h" |
| 14 | |
| 15 | GR_DEFINE_RESOURCE_CACHE_TYPE(GrStencilBuffer) |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 16 | |
| 17 | void GrStencilBuffer::wasDetachedFromRenderTarget(const GrRenderTarget* rt) { |
| 18 | GrAssert(fRTAttachmentCnt > 0); |
bsalomon@google.com | eefe6f1 | 2011-08-09 17:57:12 +0000 | [diff] [blame] | 19 | if (0 == --fRTAttachmentCnt) { |
| 20 | this->unlockInCache(); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 21 | // At this point we could be deleted! |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | void GrStencilBuffer::transferToCacheAndLock() { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 26 | GrAssert(NULL == this->getCacheEntry()); |
| 27 | GrAssert(!fHoldingLock); |
| 28 | |
| 29 | this->getGpu()->getContext()->addAndLockStencilBuffer(this); |
| 30 | fHoldingLock = true; |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 31 | } |
bsalomon@google.com | eefe6f1 | 2011-08-09 17:57:12 +0000 | [diff] [blame] | 32 | |
| 33 | void GrStencilBuffer::onRelease() { |
| 34 | // When the GrGpu rips through its list of resources and releases |
| 35 | // them it may release an SB before it releases its attached RTs. |
| 36 | // In that case when GrStencilBuffer sees its last detach it no |
| 37 | // long has a gpu ptr (gets nulled in GrResource::release()) and can't |
| 38 | // access the cache to unlock itself. So if we're being released and still |
| 39 | // have attachments go ahead and unlock now. |
| 40 | if (fRTAttachmentCnt) { |
| 41 | this->unlockInCache(); |
| 42 | // we shouldn't be deleted here because some RT still has a ref on us. |
| 43 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 44 | fHoldingLock = false; |
bsalomon@google.com | eefe6f1 | 2011-08-09 17:57:12 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void GrStencilBuffer::onAbandon() { |
| 48 | // we can use the same behavior as release. |
| 49 | this->onRelease(); |
| 50 | } |
| 51 | |
| 52 | void GrStencilBuffer::unlockInCache() { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 53 | if (fHoldingLock) { |
bsalomon@google.com | eefe6f1 | 2011-08-09 17:57:12 +0000 | [diff] [blame] | 54 | GrGpu* gpu = this->getGpu(); |
| 55 | if (NULL != gpu) { |
| 56 | GrAssert(NULL != gpu->getContext()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 57 | gpu->getContext()->unlockStencilBuffer(this); |
bsalomon@google.com | eefe6f1 | 2011-08-09 17:57:12 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 61 | |
| 62 | namespace { |
| 63 | // we should never have more than one stencil buffer with same combo of |
| 64 | // (width,height,samplecount) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 65 | void gen_stencil_key_values(int width, |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 66 | int height, |
| 67 | int sampleCnt, |
| 68 | GrCacheID* cacheID) { |
| 69 | cacheID->fPublicID = GrCacheID::kDefaultPublicCacheID; |
| 70 | cacheID->fResourceSpecific32 = width | (height << 16); |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 71 | cacheID->fDomain = GrCacheData::kScratch_ResourceDomain; |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 72 | |
| 73 | GrAssert(sampleCnt >= 0 && sampleCnt < 256); |
| 74 | cacheID->fResourceSpecific16 = sampleCnt << 8; |
| 75 | |
| 76 | // last 8 bits of 'fResourceSpecific16' is free for flags |
| 77 | } |
| 78 | } |
| 79 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 80 | GrResourceKey GrStencilBuffer::ComputeKey(int width, |
| 81 | int height, |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 82 | int sampleCnt) { |
| 83 | GrCacheID id(GrStencilBuffer::GetResourceType()); |
| 84 | gen_stencil_key_values(width, height, sampleCnt, &id); |
| 85 | |
| 86 | uint32_t v[4]; |
| 87 | id.toRaw(v); |
| 88 | return GrResourceKey(v); |
| 89 | } |