blob: 98cea1c9a8c21ca0480461fc84f977fee7fc3ad2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
bsalomon@google.com8fe72472011-03-30 21:26:44 +00007 */
8
bsalomon6d3fe022014-07-25 08:35:45 -07009#include "GrGpuResource.h"
kkinnunencabe20c2015-06-01 01:37:26 -070010#include "GrContext.h"
bsalomon0ea80f42015-02-11 10:49:59 -080011#include "GrResourceCache.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000012#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
ericrk0a5fa482015-09-15 14:16:10 -070014#include "SkTraceMemoryDump.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000015
bsalomon0ea80f42015-02-11 10:49:59 -080016static inline GrResourceCache* get_resource_cache(GrGpu* gpu) {
bsalomon49f085d2014-09-05 13:34:00 -070017 SkASSERT(gpu);
18 SkASSERT(gpu->getContext());
bsalomon0ea80f42015-02-11 10:49:59 -080019 SkASSERT(gpu->getContext()->getResourceCache());
20 return gpu->getContext()->getResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070021}
22
bsalomon5236cf42015-01-14 10:42:08 -080023GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle)
bsalomon7775c852014-12-30 12:50:52 -080024 : fGpu(gpu)
bsalomon69ed47f2014-11-12 11:13:39 -080025 , fGpuMemorySize(kInvalidGpuMemorySize)
bsalomon5236cf42015-01-14 10:42:08 -080026 , fLifeCycle(lifeCycle)
bsalomon84c8e622014-11-17 09:33:27 -080027 , fUniqueID(CreateUniqueID()) {
bsalomon9f2d1572015-02-17 11:47:40 -080028 SkDEBUGCODE(fCacheArrayIndex = -1);
bsalomon16961262014-08-26 14:01:07 -070029}
30
31void GrGpuResource::registerWithCache() {
bsalomon0ea80f42015-02-11 10:49:59 -080032 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000033}
34
bsalomon6d3fe022014-07-25 08:35:45 -070035GrGpuResource::~GrGpuResource() {
bsalomon12299ab2014-11-14 13:33:09 -080036 // The cache should have released or destroyed this resource.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000037 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000038}
39
bsalomon16961262014-08-26 14:01:07 -070040void GrGpuResource::release() {
bsalomon12299ab2014-11-14 13:33:09 -080041 SkASSERT(fGpu);
42 this->onRelease();
bsalomon0ea80f42015-02-11 10:49:59 -080043 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
halcanary96fcdcc2015-08-27 07:41:13 -070044 fGpu = nullptr;
bsalomon12299ab2014-11-14 13:33:09 -080045 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000046}
47
bsalomon6d3fe022014-07-25 08:35:45 -070048void GrGpuResource::abandon() {
bsalomonc6363ef2015-09-24 07:07:40 -070049 if (this->wasDestroyed()) {
50 return;
51 }
bsalomon12299ab2014-11-14 13:33:09 -080052 SkASSERT(fGpu);
53 this->onAbandon();
bsalomon0ea80f42015-02-11 10:49:59 -080054 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
halcanary96fcdcc2015-08-27 07:41:13 -070055 fGpu = nullptr;
bsalomon12299ab2014-11-14 13:33:09 -080056 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000057}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000058
ericrk0a5fa482015-09-15 14:16:10 -070059void GrGpuResource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
60 // Dump resource as "skia/gpu_resources/resource_#".
61 SkString dumpName("skia/gpu_resources/resource_");
62 dumpName.appendS32(this->getUniqueID());
63
64 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", this->gpuMemorySize());
65
66 if (this->isPurgeable()) {
67 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes",
68 this->gpuMemorySize());
69 }
70
71 // Call setMemoryBacking to allow sub-classes with implementation specific backings (such as GL
72 // objects) to provide additional information.
73 this->setMemoryBacking(traceMemoryDump, dumpName);
74}
75
junov5756aff2014-12-11 14:59:31 -080076const SkData* GrGpuResource::setCustomData(const SkData* data) {
77 SkSafeRef(data);
78 fData.reset(data);
79 return data;
80}
81
bsalomon6d3fe022014-07-25 08:35:45 -070082const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -070083 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000084 return fGpu->getContext();
85 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070086 return nullptr;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000087 }
88}
89
bsalomon6d3fe022014-07-25 08:35:45 -070090GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -070091 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000092 return fGpu->getContext();
93 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070094 return nullptr;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000095 }
96}
bsalomonc44be0e2014-07-25 07:32:33 -070097
bsalomon71cb0c22014-11-14 12:10:14 -080098void GrGpuResource::didChangeGpuMemorySize() const {
99 if (this->wasDestroyed()) {
100 return;
101 }
102
103 size_t oldSize = fGpuMemorySize;
104 SkASSERT(kInvalidGpuMemorySize != oldSize);
105 fGpuMemorySize = kInvalidGpuMemorySize;
bsalomon0ea80f42015-02-11 10:49:59 -0800106 get_resource_cache(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
bsalomon71cb0c22014-11-14 12:10:14 -0800107}
108
bsalomon8718aaf2015-02-19 07:24:21 -0800109void GrGpuResource::removeUniqueKey() {
bsalomonc6363ef2015-09-24 07:07:40 -0700110 if (this->wasDestroyed()) {
111 return;
112 }
bsalomon8718aaf2015-02-19 07:24:21 -0800113 SkASSERT(fUniqueKey.isValid());
bsalomonf99e9612015-02-19 08:24:16 -0800114 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
bsalomon23e619c2015-02-06 11:54:28 -0800115}
116
bsalomonf99e9612015-02-19 08:24:16 -0800117void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
bsalomon6d4488c2014-11-11 07:27:16 -0800118 SkASSERT(this->internalHasRef());
bsalomon23e619c2015-02-06 11:54:28 -0800119 SkASSERT(key.isValid());
bsalomondace19e2014-11-17 07:34:06 -0800120
bsalomon8718aaf2015-02-19 07:24:21 -0800121 // Wrapped and uncached resources can never have a unique key.
bsalomon5ec26ae2016-02-25 08:33:02 -0800122 if (SkBudgeted::kNo == this->resourcePriv().isBudgeted()) {
bsalomonf99e9612015-02-19 08:24:16 -0800123 return;
bsalomondace19e2014-11-17 07:34:06 -0800124 }
bsalomon84c8e622014-11-17 09:33:27 -0800125
bsalomonf99e9612015-02-19 08:24:16 -0800126 if (this->wasDestroyed()) {
127 return;
bsalomon8b79d232014-11-10 10:19:06 -0800128 }
129
bsalomonf99e9612015-02-19 08:24:16 -0800130 get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
bsalomon8b79d232014-11-10 10:19:06 -0800131}
132
bsalomon3f324322015-04-08 11:01:54 -0700133void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const {
bsalomon12299ab2014-11-14 13:33:09 -0800134 if (this->wasDestroyed()) {
135 // We've already been removed from the cache. Goodbye cruel world!
halcanary385fe4d2015-08-26 13:07:48 -0700136 delete this;
bsalomon3f324322015-04-08 11:01:54 -0700137 return;
bsalomonbcf0a522014-10-08 08:40:09 -0700138 }
bsalomon3f324322015-04-08 11:01:54 -0700139
140 // We should have already handled this fully in notifyRefCntIsZero().
141 SkASSERT(kRef_CntType != lastCntTypeToReachZero);
142
143 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
144 static const uint32_t kFlag =
145 GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
146 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, kFlag);
147}
148
149bool GrGpuResource::notifyRefCountIsZero() const {
150 if (this->wasDestroyed()) {
151 // handle this in notifyAllCntsAreZero().
152 return true;
153 }
154
155 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
156 uint32_t flags =
157 GrResourceCache::ResourceAccess::kRefCntReachedZero_RefNotificationFlag;
158 if (!this->internalHasPendingIO()) {
159 flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
160 }
161 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, flags);
162
163 // There is no need to call our notifyAllCntsAreZero function at this point since we already
164 // told the cache about the state of cnts.
165 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700166}
167
bsalomon7775c852014-12-30 12:50:52 -0800168void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) {
169 SkASSERT(!fScratchKey.isValid());
170 SkASSERT(scratchKey.isValid());
171 // Wrapped resources can never have a scratch key.
bsalomon6dc6f5f2015-06-18 09:12:16 -0700172 if (this->cacheAccess().isExternal()) {
bsalomondace19e2014-11-17 07:34:06 -0800173 return;
174 }
bsalomon744998e2014-08-28 09:54:34 -0700175 fScratchKey = scratchKey;
176}
177
bsalomon10e23ca2014-11-25 05:52:06 -0800178void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800179 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon0ea80f42015-02-11 10:49:59 -0800180 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800181 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800182 }
183}
184
bsalomonafe30052015-01-16 07:32:33 -0800185void GrGpuResource::makeBudgeted() {
bsalomonc6363ef2015-09-24 07:07:40 -0700186 if (!this->wasDestroyed() && GrGpuResource::kUncached_LifeCycle == fLifeCycle) {
bsalomonafe30052015-01-16 07:32:33 -0800187 fLifeCycle = kCached_LifeCycle;
bsalomon0ea80f42015-02-11 10:49:59 -0800188 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonafe30052015-01-16 07:32:33 -0800189 }
190}
191
bsalomonc2f35b72015-01-23 07:19:22 -0800192void GrGpuResource::makeUnbudgeted() {
bsalomonc6363ef2015-09-24 07:07:40 -0700193 if (!this->wasDestroyed() && GrGpuResource::kCached_LifeCycle == fLifeCycle &&
194 !fUniqueKey.isValid()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800195 fLifeCycle = kUncached_LifeCycle;
bsalomon0ea80f42015-02-11 10:49:59 -0800196 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonc2f35b72015-01-23 07:19:22 -0800197 }
198}
199
bsalomon6d3fe022014-07-25 08:35:45 -0700200uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -0700201 static int32_t gUniqueID = SK_InvalidUniqueID;
202 uint32_t id;
203 do {
204 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
205 } while (id == SK_InvalidUniqueID);
206 return id;
207}