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" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 10 | #include "GrResourceCache.h" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 11 | #include "GrGpu.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 13 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 14 | static inline GrResourceCache* get_resource_cache(GrGpu* gpu) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 15 | SkASSERT(gpu); |
| 16 | SkASSERT(gpu->getContext()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 17 | SkASSERT(gpu->getContext()->getResourceCache()); |
| 18 | return gpu->getContext()->getResourceCache(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 19 | } |
| 20 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 21 | GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle) |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 22 | : fGpu(gpu) |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 23 | , fGpuMemorySize(kInvalidGpuMemorySize) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 24 | , fLifeCycle(lifeCycle) |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 25 | , fUniqueID(CreateUniqueID()) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame^] | 26 | SkDEBUGCODE(fCacheArrayIndex = -1); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void GrGpuResource::registerWithCache() { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 30 | get_resource_cache(fGpu)->resourceAccess().insertResource(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 31 | } |
| 32 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 33 | GrGpuResource::~GrGpuResource() { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 34 | // The cache should have released or destroyed this resource. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 35 | SkASSERT(this->wasDestroyed()); |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 36 | } |
| 37 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 38 | void GrGpuResource::release() { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 39 | SkASSERT(fGpu); |
| 40 | this->onRelease(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 41 | get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 42 | fGpu = NULL; |
| 43 | fGpuMemorySize = 0; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 44 | } |
| 45 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 46 | void GrGpuResource::abandon() { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 47 | SkASSERT(fGpu); |
| 48 | this->onAbandon(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 49 | get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 50 | fGpu = NULL; |
| 51 | fGpuMemorySize = 0; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 52 | } |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 53 | |
junov | 5756aff | 2014-12-11 14:59:31 -0800 | [diff] [blame] | 54 | const SkData* GrGpuResource::setCustomData(const SkData* data) { |
| 55 | SkSafeRef(data); |
| 56 | fData.reset(data); |
| 57 | return data; |
| 58 | } |
| 59 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 60 | const GrContext* GrGpuResource::getContext() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 61 | if (fGpu) { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 62 | return fGpu->getContext(); |
| 63 | } else { |
| 64 | return NULL; |
| 65 | } |
| 66 | } |
| 67 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 68 | GrContext* GrGpuResource::getContext() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 69 | if (fGpu) { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 70 | return fGpu->getContext(); |
| 71 | } else { |
| 72 | return NULL; |
| 73 | } |
| 74 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 75 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 76 | void GrGpuResource::didChangeGpuMemorySize() const { |
| 77 | if (this->wasDestroyed()) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | size_t oldSize = fGpuMemorySize; |
| 82 | SkASSERT(kInvalidGpuMemorySize != oldSize); |
| 83 | fGpuMemorySize = kInvalidGpuMemorySize; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 84 | get_resource_cache(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 85 | } |
| 86 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 87 | void GrGpuResource::removeContentKey() { |
| 88 | SkASSERT(fContentKey.isValid()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 89 | get_resource_cache(fGpu)->resourceAccess().willRemoveContentKey(this); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 90 | fContentKey.reset(); |
| 91 | } |
| 92 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 93 | bool GrGpuResource::setContentKey(const GrContentKey& key) { |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 94 | // Currently this can only be called once and can't be called when the resource is scratch. |
| 95 | SkASSERT(this->internalHasRef()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 96 | SkASSERT(key.isValid()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 97 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 98 | // Wrapped and uncached resources can never have a content key. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 99 | if (!this->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 100 | return false; |
| 101 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 102 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 103 | if (fContentKey.isValid() || this->wasDestroyed()) { |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 104 | return false; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 105 | } |
| 106 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 107 | fContentKey = key; |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 108 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 109 | if (!get_resource_cache(fGpu)->resourceAccess().didSetContentKey(this)) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 110 | fContentKey.reset(); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 111 | return false; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 116 | void GrGpuResource::notifyIsPurgeable() const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 117 | if (this->wasDestroyed()) { |
| 118 | // We've already been removed from the cache. Goodbye cruel world! |
| 119 | SkDELETE(this); |
| 120 | } else { |
| 121 | GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 122 | get_resource_cache(fGpu)->resourceAccess().notifyPurgeable(mutableThis); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 126 | void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) { |
| 127 | SkASSERT(!fScratchKey.isValid()); |
| 128 | SkASSERT(scratchKey.isValid()); |
| 129 | // Wrapped resources can never have a scratch key. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 130 | if (this->isWrapped()) { |
| 131 | return; |
| 132 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 133 | fScratchKey = scratchKey; |
| 134 | } |
| 135 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 136 | void GrGpuResource::removeScratchKey() { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 137 | if (!this->wasDestroyed() && fScratchKey.isValid()) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 138 | get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 139 | fScratchKey.reset(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 143 | void GrGpuResource::makeBudgeted() { |
| 144 | if (GrGpuResource::kUncached_LifeCycle == fLifeCycle) { |
| 145 | fLifeCycle = kCached_LifeCycle; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 146 | get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 150 | void GrGpuResource::makeUnbudgeted() { |
| 151 | if (GrGpuResource::kCached_LifeCycle == fLifeCycle && !fContentKey.isValid()) { |
| 152 | fLifeCycle = kUncached_LifeCycle; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 153 | get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 157 | uint32_t GrGpuResource::CreateUniqueID() { |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 158 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 159 | uint32_t id; |
| 160 | do { |
| 161 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 162 | } while (id == SK_InvalidUniqueID); |
| 163 | return id; |
| 164 | } |