blob: c2d93620d5df3a7d06c6ef444a29bcd22b171afa [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"
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
bsalomon5236cf42015-01-14 10:42:08 -080022GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle)
bsalomon7775c852014-12-30 12:50:52 -080023 : fGpu(gpu)
bsalomon69ed47f2014-11-12 11:13:39 -080024 , fGpuMemorySize(kInvalidGpuMemorySize)
bsalomon5236cf42015-01-14 10:42:08 -080025 , fLifeCycle(lifeCycle)
bsalomon84c8e622014-11-17 09:33:27 -080026 , fUniqueID(CreateUniqueID()) {
bsalomon9f2d1572015-02-17 11:47:40 -080027 SkDEBUGCODE(fCacheArrayIndex = -1);
bsalomon16961262014-08-26 14:01:07 -070028}
29
30void GrGpuResource::registerWithCache() {
bsalomon0ea80f42015-02-11 10:49:59 -080031 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000032}
33
bsalomon6d3fe022014-07-25 08:35:45 -070034GrGpuResource::~GrGpuResource() {
bsalomon12299ab2014-11-14 13:33:09 -080035 // The cache should have released or destroyed this resource.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000036 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000037}
38
bsalomon16961262014-08-26 14:01:07 -070039void GrGpuResource::release() {
bsalomon12299ab2014-11-14 13:33:09 -080040 SkASSERT(fGpu);
41 this->onRelease();
bsalomon0ea80f42015-02-11 10:49:59 -080042 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
bsalomon12299ab2014-11-14 13:33:09 -080043 fGpu = NULL;
44 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000045}
46
bsalomon6d3fe022014-07-25 08:35:45 -070047void GrGpuResource::abandon() {
bsalomon12299ab2014-11-14 13:33:09 -080048 SkASSERT(fGpu);
49 this->onAbandon();
bsalomon0ea80f42015-02-11 10:49:59 -080050 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
bsalomon12299ab2014-11-14 13:33:09 -080051 fGpu = NULL;
52 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000053}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000054
junov5756aff2014-12-11 14:59:31 -080055const SkData* GrGpuResource::setCustomData(const SkData* data) {
56 SkSafeRef(data);
57 fData.reset(data);
58 return data;
59}
60
bsalomon6d3fe022014-07-25 08:35:45 -070061const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -070062 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000063 return fGpu->getContext();
64 } else {
65 return NULL;
66 }
67}
68
bsalomon6d3fe022014-07-25 08:35:45 -070069GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -070070 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000071 return fGpu->getContext();
72 } else {
73 return NULL;
74 }
75}
bsalomonc44be0e2014-07-25 07:32:33 -070076
bsalomon71cb0c22014-11-14 12:10:14 -080077void GrGpuResource::didChangeGpuMemorySize() const {
78 if (this->wasDestroyed()) {
79 return;
80 }
81
82 size_t oldSize = fGpuMemorySize;
83 SkASSERT(kInvalidGpuMemorySize != oldSize);
84 fGpuMemorySize = kInvalidGpuMemorySize;
bsalomon0ea80f42015-02-11 10:49:59 -080085 get_resource_cache(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
bsalomon71cb0c22014-11-14 12:10:14 -080086}
87
bsalomon8718aaf2015-02-19 07:24:21 -080088void GrGpuResource::removeUniqueKey() {
89 SkASSERT(fUniqueKey.isValid());
bsalomonf99e9612015-02-19 08:24:16 -080090 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
bsalomon23e619c2015-02-06 11:54:28 -080091}
92
bsalomonf99e9612015-02-19 08:24:16 -080093void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
bsalomon6d4488c2014-11-11 07:27:16 -080094 SkASSERT(this->internalHasRef());
bsalomon23e619c2015-02-06 11:54:28 -080095 SkASSERT(key.isValid());
bsalomondace19e2014-11-17 07:34:06 -080096
bsalomon8718aaf2015-02-19 07:24:21 -080097 // Wrapped and uncached resources can never have a unique key.
bsalomon3582d3e2015-02-13 14:20:05 -080098 if (!this->resourcePriv().isBudgeted()) {
bsalomonf99e9612015-02-19 08:24:16 -080099 return;
bsalomondace19e2014-11-17 07:34:06 -0800100 }
bsalomon84c8e622014-11-17 09:33:27 -0800101
bsalomonf99e9612015-02-19 08:24:16 -0800102 if (this->wasDestroyed()) {
103 return;
bsalomon8b79d232014-11-10 10:19:06 -0800104 }
105
bsalomonf99e9612015-02-19 08:24:16 -0800106 get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
bsalomon8b79d232014-11-10 10:19:06 -0800107}
108
bsalomon3f324322015-04-08 11:01:54 -0700109void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const {
bsalomon12299ab2014-11-14 13:33:09 -0800110 if (this->wasDestroyed()) {
111 // We've already been removed from the cache. Goodbye cruel world!
112 SkDELETE(this);
bsalomon3f324322015-04-08 11:01:54 -0700113 return;
bsalomonbcf0a522014-10-08 08:40:09 -0700114 }
bsalomon3f324322015-04-08 11:01:54 -0700115
116 // We should have already handled this fully in notifyRefCntIsZero().
117 SkASSERT(kRef_CntType != lastCntTypeToReachZero);
118
119 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
120 static const uint32_t kFlag =
121 GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
122 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, kFlag);
123}
124
125bool GrGpuResource::notifyRefCountIsZero() const {
126 if (this->wasDestroyed()) {
127 // handle this in notifyAllCntsAreZero().
128 return true;
129 }
130
131 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
132 uint32_t flags =
133 GrResourceCache::ResourceAccess::kRefCntReachedZero_RefNotificationFlag;
134 if (!this->internalHasPendingIO()) {
135 flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
136 }
137 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, flags);
138
139 // There is no need to call our notifyAllCntsAreZero function at this point since we already
140 // told the cache about the state of cnts.
141 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700142}
143
bsalomon7775c852014-12-30 12:50:52 -0800144void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) {
145 SkASSERT(!fScratchKey.isValid());
146 SkASSERT(scratchKey.isValid());
147 // Wrapped resources can never have a scratch key.
bsalomondace19e2014-11-17 07:34:06 -0800148 if (this->isWrapped()) {
149 return;
150 }
bsalomon744998e2014-08-28 09:54:34 -0700151 fScratchKey = scratchKey;
152}
153
bsalomon10e23ca2014-11-25 05:52:06 -0800154void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800155 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon0ea80f42015-02-11 10:49:59 -0800156 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800157 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800158 }
159}
160
bsalomonafe30052015-01-16 07:32:33 -0800161void GrGpuResource::makeBudgeted() {
162 if (GrGpuResource::kUncached_LifeCycle == fLifeCycle) {
163 fLifeCycle = kCached_LifeCycle;
bsalomon0ea80f42015-02-11 10:49:59 -0800164 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonafe30052015-01-16 07:32:33 -0800165 }
166}
167
bsalomonc2f35b72015-01-23 07:19:22 -0800168void GrGpuResource::makeUnbudgeted() {
bsalomon8718aaf2015-02-19 07:24:21 -0800169 if (GrGpuResource::kCached_LifeCycle == fLifeCycle && !fUniqueKey.isValid()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800170 fLifeCycle = kUncached_LifeCycle;
bsalomon0ea80f42015-02-11 10:49:59 -0800171 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonc2f35b72015-01-23 07:19:22 -0800172 }
173}
174
bsalomon6d3fe022014-07-25 08:35:45 -0700175uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -0700176 static int32_t gUniqueID = SK_InvalidUniqueID;
177 uint32_t id;
178 do {
179 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
180 } while (id == SK_InvalidUniqueID);
181 return id;
182}