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 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 10 | #include "GrGpuResource.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 11 | #include "GrResourceCache2.h" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 12 | #include "GrGpu.h" |
| 13 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 14 | static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 15 | SkASSERT(gpu); |
| 16 | SkASSERT(gpu->getContext()); |
| 17 | SkASSERT(gpu->getContext()->getResourceCache2()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 18 | return gpu->getContext()->getResourceCache2(); |
| 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 | , fFlags(0) |
| 25 | , fLifeCycle(lifeCycle) |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 26 | , fUniqueID(CreateUniqueID()) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void GrGpuResource::registerWithCache() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 30 | get_resource_cache2(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(); |
| 41 | get_resource_cache2(fGpu)->resourceAccess().removeResource(this); |
| 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(); |
| 49 | get_resource_cache2(fGpu)->resourceAccess().removeResource(this); |
| 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; |
| 84 | get_resource_cache2(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize); |
| 85 | } |
| 86 | |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 87 | bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) { |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 88 | // Currently this can only be called once and can't be called when the resource is scratch. |
| 89 | SkASSERT(this->internalHasRef()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 90 | |
| 91 | // Wrapped resources can never have a key. |
| 92 | if (this->isWrapped()) { |
| 93 | return false; |
| 94 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 95 | |
| 96 | if ((fFlags & kContentKeySet_Flag) || this->wasDestroyed()) { |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 97 | return false; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 98 | } |
| 99 | |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 100 | fContentKey = contentKey; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 101 | fFlags |= kContentKeySet_Flag; |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 102 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 103 | if (!get_resource_cache2(fGpu)->resourceAccess().didSetContentKey(this)) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 104 | fFlags &= ~kContentKeySet_Flag; |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 105 | return false; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 110 | void GrGpuResource::notifyIsPurgable() const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 111 | if (this->wasDestroyed()) { |
| 112 | // We've already been removed from the cache. Goodbye cruel world! |
| 113 | SkDELETE(this); |
| 114 | } else { |
| 115 | GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this); |
| 116 | get_resource_cache2(fGpu)->resourceAccess().notifyPurgable(mutableThis); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 120 | void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) { |
| 121 | SkASSERT(!fScratchKey.isValid()); |
| 122 | SkASSERT(scratchKey.isValid()); |
| 123 | // Wrapped resources can never have a scratch key. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 124 | if (this->isWrapped()) { |
| 125 | return; |
| 126 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 127 | fScratchKey = scratchKey; |
| 128 | } |
| 129 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 130 | void GrGpuResource::removeScratchKey() { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 131 | if (!this->wasDestroyed() && fScratchKey.isValid()) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 132 | get_resource_cache2(fGpu)->resourceAccess().willRemoveScratchKey(this); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 133 | fScratchKey.reset(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 137 | uint32_t GrGpuResource::CreateUniqueID() { |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 138 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 139 | uint32_t id; |
| 140 | do { |
| 141 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 142 | } while (id == SK_InvalidUniqueID); |
| 143 | return id; |
| 144 | } |