1 /*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/graphite/Resource.h"
9
10 #include "src/gpu/graphite/ResourceCache.h"
11
12 namespace skgpu::graphite {
13
Resource(const SharedContext * sharedContext,Ownership ownership,skgpu::Budgeted budgeted)14 Resource::Resource(const SharedContext* sharedContext,
15 Ownership ownership,
16 skgpu::Budgeted budgeted)
17 : fSharedContext(sharedContext)
18 , fUsageRefCnt(1)
19 , fCommandBufferRefCnt(0)
20 , fCacheRefCnt(0)
21 , fOwnership(ownership)
22 , fBudgeted(budgeted) {
23 // If we don't own the resource that must mean its wrapped in a client object. Thus we should
24 // not be budgeted
25 SkASSERT(fOwnership == Ownership::kOwned || fBudgeted == skgpu::Budgeted::kNo);
26 }
27
~Resource()28 Resource::~Resource() {
29 // The cache should have released or destroyed this resource.
30 SkASSERT(this->wasDestroyed());
31 }
32
registerWithCache(sk_sp<ResourceCache> returnCache)33 void Resource::registerWithCache(sk_sp<ResourceCache> returnCache) {
34 SkASSERT(!fReturnCache);
35 SkASSERT(returnCache);
36
37 fReturnCache = std::move(returnCache);
38 }
39
notifyARefIsZero(LastRemovedRef removedRef) const40 bool Resource::notifyARefIsZero(LastRemovedRef removedRef) const {
41 // No resource should have been destroyed if there was still any sort of ref on it.
42 SkASSERT(!this->wasDestroyed());
43
44 Resource* mutableThis = const_cast<Resource*>(this);
45
46 // TODO: We have not switched all resources to use the ResourceCache yet. Once we do we should
47 // be able to assert that we have an fCacheReturn.
48 // SkASSERT(fReturnCache);
49 if (removedRef != LastRemovedRef::kCache &&
50 fReturnCache &&
51 fReturnCache->returnResource(mutableThis, removedRef)) {
52 return false;
53 }
54
55 if (!this->hasAnyRefs()) {
56 return true;
57 }
58 return false;
59 }
60
internalDispose()61 void Resource::internalDispose() {
62 SkASSERT(fSharedContext);
63 this->freeGpuData();
64 fSharedContext = nullptr;
65 // TODO: If we ever support freeing all the backend objects without deleting the object, we'll
66 // need to add a hasAnyRefs() check here.
67 delete this;
68 }
69
isPurgeable() const70 bool Resource::isPurgeable() const {
71 return !this->hasAnyRefs();
72 }
73
74 } // namespace skgpu::graphite
75
76