blob: 53a4a65b669a28ebab56e99dc2dd7cf0556befc6 [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() {
26 GrAssert(NULL == fCacheEntry);
27 fCacheEntry =
28 this->getGpu()->getContext()->addAndLockStencilBuffer(this);
29}
bsalomon@google.comeefe6f12011-08-09 17:57:12 +000030
31void GrStencilBuffer::onRelease() {
32 // When the GrGpu rips through its list of resources and releases
33 // them it may release an SB before it releases its attached RTs.
34 // In that case when GrStencilBuffer sees its last detach it no
35 // long has a gpu ptr (gets nulled in GrResource::release()) and can't
36 // access the cache to unlock itself. So if we're being released and still
37 // have attachments go ahead and unlock now.
38 if (fRTAttachmentCnt) {
39 this->unlockInCache();
40 // we shouldn't be deleted here because some RT still has a ref on us.
41 }
42 fCacheEntry = NULL;
43}
44
45void GrStencilBuffer::onAbandon() {
46 // we can use the same behavior as release.
47 this->onRelease();
48}
49
50void GrStencilBuffer::unlockInCache() {
51 if (NULL != fCacheEntry) {
52 GrGpu* gpu = this->getGpu();
53 if (NULL != gpu) {
54 GrAssert(NULL != gpu->getContext());
55 gpu->getContext()->unlockStencilBuffer(fCacheEntry);
56 }
57 }
58}
robertphillips@google.com46a86002012-08-08 10:42:44 +000059
60namespace {
61// we should never have more than one stencil buffer with same combo of
62// (width,height,samplecount)
63void gen_stencil_key_values(int width,
64 int height,
65 int sampleCnt,
66 GrCacheID* cacheID) {
67 cacheID->fPublicID = GrCacheID::kDefaultPublicCacheID;
68 cacheID->fResourceSpecific32 = width | (height << 16);
robertphillips@google.com9c2ea842012-08-13 17:47:59 +000069 cacheID->fDomain = GrCacheData::kScratch_ResourceDomain;
robertphillips@google.com46a86002012-08-08 10:42:44 +000070
71 GrAssert(sampleCnt >= 0 && sampleCnt < 256);
72 cacheID->fResourceSpecific16 = sampleCnt << 8;
73
74 // last 8 bits of 'fResourceSpecific16' is free for flags
75}
76}
77
78GrResourceKey GrStencilBuffer::ComputeKey(int width,
79 int height,
80 int sampleCnt) {
81 GrCacheID id(GrStencilBuffer::GetResourceType());
82 gen_stencil_key_values(width, height, sampleCnt, &id);
83
84 uint32_t v[4];
85 id.toRaw(v);
86 return GrResourceKey(v);
87}