epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 9 | #include "GrGpuResource.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 11 | #include "GrResourceCache.h" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 12 | #include "GrGpu.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 14 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 15 | static inline GrResourceCache* get_resource_cache(GrGpu* gpu) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 16 | SkASSERT(gpu); |
| 17 | SkASSERT(gpu->getContext()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 18 | SkASSERT(gpu->getContext()->getResourceCache()); |
| 19 | return gpu->getContext()->getResourceCache(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 20 | } |
| 21 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 22 | GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle) |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 23 | : fGpu(gpu) |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 24 | , fGpuMemorySize(kInvalidGpuMemorySize) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 25 | , fLifeCycle(lifeCycle) |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 26 | , fUniqueID(CreateUniqueID()) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 27 | SkDEBUGCODE(fCacheArrayIndex = -1); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void GrGpuResource::registerWithCache() { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 31 | get_resource_cache(fGpu)->resourceAccess().insertResource(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 32 | } |
| 33 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 34 | GrGpuResource::~GrGpuResource() { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 35 | // The cache should have released or destroyed this resource. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 36 | SkASSERT(this->wasDestroyed()); |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 37 | } |
| 38 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 39 | void GrGpuResource::release() { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 40 | SkASSERT(fGpu); |
| 41 | this->onRelease(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 42 | get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 43 | fGpu = NULL; |
| 44 | fGpuMemorySize = 0; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 47 | void GrGpuResource::abandon() { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 48 | SkASSERT(fGpu); |
| 49 | this->onAbandon(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 50 | get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 51 | fGpu = NULL; |
| 52 | fGpuMemorySize = 0; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 53 | } |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 54 | |
junov | 5756aff | 2014-12-11 14:59:31 -0800 | [diff] [blame] | 55 | const SkData* GrGpuResource::setCustomData(const SkData* data) { |
| 56 | SkSafeRef(data); |
| 57 | fData.reset(data); |
| 58 | return data; |
| 59 | } |
| 60 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 61 | const GrContext* GrGpuResource::getContext() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 62 | if (fGpu) { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 63 | return fGpu->getContext(); |
| 64 | } else { |
| 65 | return NULL; |
| 66 | } |
| 67 | } |
| 68 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 69 | GrContext* GrGpuResource::getContext() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 70 | if (fGpu) { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 71 | return fGpu->getContext(); |
| 72 | } else { |
| 73 | return NULL; |
| 74 | } |
| 75 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 76 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 77 | void GrGpuResource::didChangeGpuMemorySize() const { |
| 78 | if (this->wasDestroyed()) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | size_t oldSize = fGpuMemorySize; |
| 83 | SkASSERT(kInvalidGpuMemorySize != oldSize); |
| 84 | fGpuMemorySize = kInvalidGpuMemorySize; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 85 | get_resource_cache(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 88 | void GrGpuResource::removeUniqueKey() { |
| 89 | SkASSERT(fUniqueKey.isValid()); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 90 | get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 91 | } |
| 92 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 93 | void GrGpuResource::setUniqueKey(const GrUniqueKey& key) { |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 94 | SkASSERT(this->internalHasRef()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 95 | SkASSERT(key.isValid()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 96 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 97 | // Wrapped and uncached resources can never have a unique key. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 98 | if (!this->resourcePriv().isBudgeted()) { |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 99 | return; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 100 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 101 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 102 | if (this->wasDestroyed()) { |
| 103 | return; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 104 | } |
| 105 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 106 | get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 107 | } |
| 108 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 109 | void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 110 | if (this->wasDestroyed()) { |
| 111 | // We've already been removed from the cache. Goodbye cruel world! |
| 112 | SkDELETE(this); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 113 | return; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 114 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 115 | |
| 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 | |
| 125 | bool 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; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 144 | void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) { |
| 145 | SkASSERT(!fScratchKey.isValid()); |
| 146 | SkASSERT(scratchKey.isValid()); |
| 147 | // Wrapped resources can never have a scratch key. |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 148 | if (this->cacheAccess().isExternal()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 149 | return; |
| 150 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 151 | fScratchKey = scratchKey; |
| 152 | } |
| 153 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 154 | void GrGpuResource::removeScratchKey() { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 155 | if (!this->wasDestroyed() && fScratchKey.isValid()) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 156 | get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 157 | fScratchKey.reset(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 161 | void GrGpuResource::makeBudgeted() { |
| 162 | if (GrGpuResource::kUncached_LifeCycle == fLifeCycle) { |
| 163 | fLifeCycle = kCached_LifeCycle; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 164 | get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 168 | void GrGpuResource::makeUnbudgeted() { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 169 | if (GrGpuResource::kCached_LifeCycle == fLifeCycle && !fUniqueKey.isValid()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 170 | fLifeCycle = kUncached_LifeCycle; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 171 | get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 175 | uint32_t GrGpuResource::CreateUniqueID() { |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 176 | 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 | } |