blob: df8e72c5ed9fe9a7bdba4f12bc53b6630686f301 [file] [log] [blame]
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com8fe72472011-03-30 21:26:44 +00006 */
7
bsalomon6d3fe022014-07-25 08:35:45 -07008#include "GrGpuResource.h"
kkinnunencabe20c2015-06-01 01:37:26 -07009#include "GrContext.h"
bsalomon0ea80f42015-02-11 10:49:59 -080010#include "GrResourceCache.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000011#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourcePriv.h"
ericrk0a5fa482015-09-15 14:16:10 -070013#include "SkTraceMemoryDump.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000014
bsalomon0ea80f42015-02-11 10:49:59 -080015static inline GrResourceCache* get_resource_cache(GrGpu* gpu) {
bsalomon49f085d2014-09-05 13:34:00 -070016 SkASSERT(gpu);
17 SkASSERT(gpu->getContext());
bsalomon0ea80f42015-02-11 10:49:59 -080018 SkASSERT(gpu->getContext()->getResourceCache());
19 return gpu->getContext()->getResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070020}
21
kkinnunen2e6055b2016-04-22 01:48:29 -070022GrGpuResource::GrGpuResource(GrGpu* gpu)
bsalomone2e87f32016-09-22 12:42:11 -070023 : fExternalFlushCntWhenBecamePurgeable(0)
24 , fGpu(gpu)
bsalomon69ed47f2014-11-12 11:13:39 -080025 , fGpuMemorySize(kInvalidGpuMemorySize)
kkinnunen2e6055b2016-04-22 01:48:29 -070026 , fBudgeted(SkBudgeted::kNo)
27 , fRefsWrappedObjects(false)
bsalomon84c8e622014-11-17 09:33:27 -080028 , fUniqueID(CreateUniqueID()) {
bsalomon9f2d1572015-02-17 11:47:40 -080029 SkDEBUGCODE(fCacheArrayIndex = -1);
bsalomon16961262014-08-26 14:01:07 -070030}
31
kkinnunen2e6055b2016-04-22 01:48:29 -070032void GrGpuResource::registerWithCache(SkBudgeted budgeted) {
33 SkASSERT(fBudgeted == SkBudgeted::kNo);
34 fBudgeted = budgeted;
35 this->computeScratchKey(&fScratchKey);
36 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
37}
38
39void GrGpuResource::registerWithCacheWrapped() {
40 SkASSERT(fBudgeted == SkBudgeted::kNo);
41 // Currently resources referencing wrapped objects are not budgeted.
42 fRefsWrappedObjects = true;
bsalomon0ea80f42015-02-11 10:49:59 -080043 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044}
45
bsalomon6d3fe022014-07-25 08:35:45 -070046GrGpuResource::~GrGpuResource() {
bsalomon12299ab2014-11-14 13:33:09 -080047 // The cache should have released or destroyed this resource.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000048 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000049}
50
halcanary9d524f22016-03-29 09:03:52 -070051void GrGpuResource::release() {
bsalomon12299ab2014-11-14 13:33:09 -080052 SkASSERT(fGpu);
53 this->onRelease();
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}
58
bsalomon6d3fe022014-07-25 08:35:45 -070059void GrGpuResource::abandon() {
bsalomonc6363ef2015-09-24 07:07:40 -070060 if (this->wasDestroyed()) {
61 return;
62 }
bsalomon12299ab2014-11-14 13:33:09 -080063 SkASSERT(fGpu);
64 this->onAbandon();
bsalomon0ea80f42015-02-11 10:49:59 -080065 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
halcanary96fcdcc2015-08-27 07:41:13 -070066 fGpu = nullptr;
bsalomon12299ab2014-11-14 13:33:09 -080067 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000068}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000069
ericrk0a5fa482015-09-15 14:16:10 -070070void GrGpuResource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
71 // Dump resource as "skia/gpu_resources/resource_#".
72 SkString dumpName("skia/gpu_resources/resource_");
Robert Phillips294870f2016-11-11 12:38:40 -050073 dumpName.appendU32(this->uniqueID().asUInt());
ericrk0a5fa482015-09-15 14:16:10 -070074
75 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", this->gpuMemorySize());
76
77 if (this->isPurgeable()) {
78 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes",
79 this->gpuMemorySize());
80 }
81
82 // Call setMemoryBacking to allow sub-classes with implementation specific backings (such as GL
83 // objects) to provide additional information.
84 this->setMemoryBacking(traceMemoryDump, dumpName);
85}
86
bsalomon6d3fe022014-07-25 08:35:45 -070087const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -070088 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000089 return fGpu->getContext();
90 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070091 return nullptr;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000092 }
93}
94
bsalomon6d3fe022014-07-25 08:35:45 -070095GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -070096 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000097 return fGpu->getContext();
98 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070099 return nullptr;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +0000100 }
101}
bsalomonc44be0e2014-07-25 07:32:33 -0700102
bsalomon71cb0c22014-11-14 12:10:14 -0800103void GrGpuResource::didChangeGpuMemorySize() const {
104 if (this->wasDestroyed()) {
105 return;
106 }
107
108 size_t oldSize = fGpuMemorySize;
109 SkASSERT(kInvalidGpuMemorySize != oldSize);
110 fGpuMemorySize = kInvalidGpuMemorySize;
bsalomon0ea80f42015-02-11 10:49:59 -0800111 get_resource_cache(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
bsalomon71cb0c22014-11-14 12:10:14 -0800112}
113
bsalomon8718aaf2015-02-19 07:24:21 -0800114void GrGpuResource::removeUniqueKey() {
bsalomonc6363ef2015-09-24 07:07:40 -0700115 if (this->wasDestroyed()) {
116 return;
117 }
bsalomon8718aaf2015-02-19 07:24:21 -0800118 SkASSERT(fUniqueKey.isValid());
bsalomonf99e9612015-02-19 08:24:16 -0800119 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
bsalomon23e619c2015-02-06 11:54:28 -0800120}
121
bsalomonf99e9612015-02-19 08:24:16 -0800122void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
bsalomon6d4488c2014-11-11 07:27:16 -0800123 SkASSERT(this->internalHasRef());
bsalomon23e619c2015-02-06 11:54:28 -0800124 SkASSERT(key.isValid());
bsalomondace19e2014-11-17 07:34:06 -0800125
Brian Osman0562eb92017-05-08 11:16:39 -0400126 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
127 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
128 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
129 // it will always be released - it is never converted to a scratch resource.
130 if (SkBudgeted::kNo == this->resourcePriv().isBudgeted() && !this->fRefsWrappedObjects) {
bsalomonf99e9612015-02-19 08:24:16 -0800131 return;
bsalomondace19e2014-11-17 07:34:06 -0800132 }
bsalomon84c8e622014-11-17 09:33:27 -0800133
bsalomonf99e9612015-02-19 08:24:16 -0800134 if (this->wasDestroyed()) {
135 return;
bsalomon8b79d232014-11-10 10:19:06 -0800136 }
137
bsalomonf99e9612015-02-19 08:24:16 -0800138 get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
bsalomon8b79d232014-11-10 10:19:06 -0800139}
140
bsalomon3f324322015-04-08 11:01:54 -0700141void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const {
bsalomon12299ab2014-11-14 13:33:09 -0800142 if (this->wasDestroyed()) {
143 // We've already been removed from the cache. Goodbye cruel world!
halcanary385fe4d2015-08-26 13:07:48 -0700144 delete this;
bsalomon3f324322015-04-08 11:01:54 -0700145 return;
bsalomonbcf0a522014-10-08 08:40:09 -0700146 }
bsalomon3f324322015-04-08 11:01:54 -0700147
148 // We should have already handled this fully in notifyRefCntIsZero().
149 SkASSERT(kRef_CntType != lastCntTypeToReachZero);
150
151 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
152 static const uint32_t kFlag =
153 GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
154 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, kFlag);
155}
156
157bool GrGpuResource::notifyRefCountIsZero() const {
158 if (this->wasDestroyed()) {
159 // handle this in notifyAllCntsAreZero().
160 return true;
161 }
162
163 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
Robert Phillipsb6deea82017-05-11 14:14:30 -0400164 uint32_t flags = GrResourceCache::ResourceAccess::kRefCntReachedZero_RefNotificationFlag;
bsalomon3f324322015-04-08 11:01:54 -0700165 if (!this->internalHasPendingIO()) {
166 flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
167 }
168 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, flags);
169
170 // There is no need to call our notifyAllCntsAreZero function at this point since we already
171 // told the cache about the state of cnts.
172 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700173}
174
bsalomon10e23ca2014-11-25 05:52:06 -0800175void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800176 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon0ea80f42015-02-11 10:49:59 -0800177 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800178 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800179 }
180}
181
bsalomonafe30052015-01-16 07:32:33 -0800182void GrGpuResource::makeBudgeted() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700183 if (!this->wasDestroyed() && SkBudgeted::kNo == fBudgeted) {
184 // Currently resources referencing wrapped objects are not budgeted.
185 SkASSERT(!fRefsWrappedObjects);
186 fBudgeted = SkBudgeted::kYes;
bsalomon0ea80f42015-02-11 10:49:59 -0800187 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonafe30052015-01-16 07:32:33 -0800188 }
189}
190
bsalomonc2f35b72015-01-23 07:19:22 -0800191void GrGpuResource::makeUnbudgeted() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700192 if (!this->wasDestroyed() && SkBudgeted::kYes == fBudgeted &&
bsalomonc6363ef2015-09-24 07:07:40 -0700193 !fUniqueKey.isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700194 fBudgeted = SkBudgeted::kNo;
bsalomon0ea80f42015-02-11 10:49:59 -0800195 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonc2f35b72015-01-23 07:19:22 -0800196 }
197}
198
bsalomon6d3fe022014-07-25 08:35:45 -0700199uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -0700200 static int32_t gUniqueID = SK_InvalidUniqueID;
201 uint32_t id;
202 do {
203 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
204 } while (id == SK_InvalidUniqueID);
205 return id;
206}