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