blob: bf14f4864e89703d871d74446eacc71ecf3d6ef6 [file] [log] [blame]
bsalomon@google.com558a75b2011-08-08 17:01:14 +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#include "GrStencilBuffer.h"
10
11#include "GrContext.h"
12#include "GrGpu.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000013#include "GrResourceCache.h"
14
15GR_DEFINE_RESOURCE_CACHE_TYPE(GrStencilBuffer)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000016
17void GrStencilBuffer::wasDetachedFromRenderTarget(const GrRenderTarget* rt) {
18 GrAssert(fRTAttachmentCnt > 0);
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000019 if (0 == --fRTAttachmentCnt) {
20 this->unlockInCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +000021 // At this point we could be deleted!
22 }
23}
24
25void GrStencilBuffer::transferToCacheAndLock() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000026 GrAssert(NULL == this->getCacheEntry());
27 GrAssert(!fHoldingLock);
28
29 this->getGpu()->getContext()->addAndLockStencilBuffer(this);
30 fHoldingLock = true;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000031}
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000032
33void 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.com1f47f4f2012-08-16 14:49:16 +000044 fHoldingLock = false;
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000045}
46
47void GrStencilBuffer::onAbandon() {
48 // we can use the same behavior as release.
49 this->onRelease();
50}
51
52void GrStencilBuffer::unlockInCache() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000053 if (fHoldingLock) {
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000054 GrGpu* gpu = this->getGpu();
55 if (NULL != gpu) {
56 GrAssert(NULL != gpu->getContext());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000057 gpu->getContext()->unlockStencilBuffer(this);
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000058 }
59 }
60}
robertphillips@google.com46a86002012-08-08 10:42:44 +000061
62namespace {
63// we should never have more than one stencil buffer with same combo of
64// (width,height,samplecount)
rmistry@google.comd6176b02012-08-23 18:14:13 +000065void gen_stencil_key_values(int width,
robertphillips@google.com46a86002012-08-08 10:42:44 +000066 int height,
67 int sampleCnt,
68 GrCacheID* cacheID) {
69 cacheID->fPublicID = GrCacheID::kDefaultPublicCacheID;
70 cacheID->fResourceSpecific32 = width | (height << 16);
robertphillips@google.com9c2ea842012-08-13 17:47:59 +000071 cacheID->fDomain = GrCacheData::kScratch_ResourceDomain;
robertphillips@google.com46a86002012-08-08 10:42:44 +000072
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.comd6176b02012-08-23 18:14:13 +000080GrResourceKey GrStencilBuffer::ComputeKey(int width,
81 int height,
robertphillips@google.com46a86002012-08-08 10:42:44 +000082 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}