blob: dbf5d8c2bbe18830e7f3b7133017906455240e39 [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"
bsalomon0ea80f42015-02-11 10:49:59 -080010#include "GrResourceCache.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000011#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourcePriv.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000013
bsalomon0ea80f42015-02-11 10:49:59 -080014static inline GrResourceCache* get_resource_cache(GrGpu* gpu) {
bsalomon49f085d2014-09-05 13:34:00 -070015 SkASSERT(gpu);
16 SkASSERT(gpu->getContext());
bsalomon0ea80f42015-02-11 10:49:59 -080017 SkASSERT(gpu->getContext()->getResourceCache());
18 return gpu->getContext()->getResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070019}
20
bsalomon5236cf42015-01-14 10:42:08 -080021GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle)
bsalomon7775c852014-12-30 12:50:52 -080022 : fGpu(gpu)
bsalomon69ed47f2014-11-12 11:13:39 -080023 , fGpuMemorySize(kInvalidGpuMemorySize)
bsalomon5236cf42015-01-14 10:42:08 -080024 , fLifeCycle(lifeCycle)
bsalomon84c8e622014-11-17 09:33:27 -080025 , fUniqueID(CreateUniqueID()) {
bsalomon16961262014-08-26 14:01:07 -070026}
27
28void GrGpuResource::registerWithCache() {
bsalomon0ea80f42015-02-11 10:49:59 -080029 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000030}
31
bsalomon6d3fe022014-07-25 08:35:45 -070032GrGpuResource::~GrGpuResource() {
bsalomon12299ab2014-11-14 13:33:09 -080033 // The cache should have released or destroyed this resource.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000034 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000035}
36
bsalomon16961262014-08-26 14:01:07 -070037void GrGpuResource::release() {
bsalomon12299ab2014-11-14 13:33:09 -080038 SkASSERT(fGpu);
39 this->onRelease();
bsalomon0ea80f42015-02-11 10:49:59 -080040 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
bsalomon12299ab2014-11-14 13:33:09 -080041 fGpu = NULL;
42 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043}
44
bsalomon6d3fe022014-07-25 08:35:45 -070045void GrGpuResource::abandon() {
bsalomon12299ab2014-11-14 13:33:09 -080046 SkASSERT(fGpu);
47 this->onAbandon();
bsalomon0ea80f42015-02-11 10:49:59 -080048 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
bsalomon12299ab2014-11-14 13:33:09 -080049 fGpu = NULL;
50 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000051}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000052
junov5756aff2014-12-11 14:59:31 -080053const SkData* GrGpuResource::setCustomData(const SkData* data) {
54 SkSafeRef(data);
55 fData.reset(data);
56 return data;
57}
58
bsalomon6d3fe022014-07-25 08:35:45 -070059const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -070060 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000061 return fGpu->getContext();
62 } else {
63 return NULL;
64 }
65}
66
bsalomon6d3fe022014-07-25 08:35:45 -070067GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -070068 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000069 return fGpu->getContext();
70 } else {
71 return NULL;
72 }
73}
bsalomonc44be0e2014-07-25 07:32:33 -070074
bsalomon71cb0c22014-11-14 12:10:14 -080075void GrGpuResource::didChangeGpuMemorySize() const {
76 if (this->wasDestroyed()) {
77 return;
78 }
79
80 size_t oldSize = fGpuMemorySize;
81 SkASSERT(kInvalidGpuMemorySize != oldSize);
82 fGpuMemorySize = kInvalidGpuMemorySize;
bsalomon0ea80f42015-02-11 10:49:59 -080083 get_resource_cache(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
bsalomon71cb0c22014-11-14 12:10:14 -080084}
85
bsalomon23e619c2015-02-06 11:54:28 -080086void GrGpuResource::removeContentKey() {
87 SkASSERT(fContentKey.isValid());
bsalomon0ea80f42015-02-11 10:49:59 -080088 get_resource_cache(fGpu)->resourceAccess().willRemoveContentKey(this);
bsalomon23e619c2015-02-06 11:54:28 -080089 fContentKey.reset();
90}
91
bsalomon24db3b12015-01-23 04:24:04 -080092bool GrGpuResource::setContentKey(const GrContentKey& key) {
bsalomon6d4488c2014-11-11 07:27:16 -080093 // Currently this can only be called once and can't be called when the resource is scratch.
94 SkASSERT(this->internalHasRef());
bsalomon23e619c2015-02-06 11:54:28 -080095 SkASSERT(key.isValid());
bsalomondace19e2014-11-17 07:34:06 -080096
bsalomonc2f35b72015-01-23 07:19:22 -080097 // Wrapped and uncached resources can never have a content key.
bsalomon3582d3e2015-02-13 14:20:05 -080098 if (!this->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -080099 return false;
100 }
bsalomon84c8e622014-11-17 09:33:27 -0800101
bsalomon24db3b12015-01-23 04:24:04 -0800102 if (fContentKey.isValid() || this->wasDestroyed()) {
bsalomon6d4488c2014-11-11 07:27:16 -0800103 return false;
bsalomon8b79d232014-11-10 10:19:06 -0800104 }
105
bsalomon24db3b12015-01-23 04:24:04 -0800106 fContentKey = key;
bsalomon6d4488c2014-11-11 07:27:16 -0800107
bsalomon0ea80f42015-02-11 10:49:59 -0800108 if (!get_resource_cache(fGpu)->resourceAccess().didSetContentKey(this)) {
bsalomon24db3b12015-01-23 04:24:04 -0800109 fContentKey.reset();
bsalomon6d4488c2014-11-11 07:27:16 -0800110 return false;
bsalomon8b79d232014-11-10 10:19:06 -0800111 }
112 return true;
113}
114
bsalomon63c992f2015-01-23 12:47:59 -0800115void GrGpuResource::notifyIsPurgeable() const {
bsalomon12299ab2014-11-14 13:33:09 -0800116 if (this->wasDestroyed()) {
117 // We've already been removed from the cache. Goodbye cruel world!
118 SkDELETE(this);
119 } else {
120 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
bsalomon0ea80f42015-02-11 10:49:59 -0800121 get_resource_cache(fGpu)->resourceAccess().notifyPurgeable(mutableThis);
bsalomonbcf0a522014-10-08 08:40:09 -0700122 }
123}
124
bsalomon7775c852014-12-30 12:50:52 -0800125void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) {
126 SkASSERT(!fScratchKey.isValid());
127 SkASSERT(scratchKey.isValid());
128 // Wrapped resources can never have a scratch key.
bsalomondace19e2014-11-17 07:34:06 -0800129 if (this->isWrapped()) {
130 return;
131 }
bsalomon744998e2014-08-28 09:54:34 -0700132 fScratchKey = scratchKey;
133}
134
bsalomon10e23ca2014-11-25 05:52:06 -0800135void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800136 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon0ea80f42015-02-11 10:49:59 -0800137 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800138 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800139 }
140}
141
bsalomonafe30052015-01-16 07:32:33 -0800142void GrGpuResource::makeBudgeted() {
143 if (GrGpuResource::kUncached_LifeCycle == fLifeCycle) {
144 fLifeCycle = kCached_LifeCycle;
bsalomon0ea80f42015-02-11 10:49:59 -0800145 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonafe30052015-01-16 07:32:33 -0800146 }
147}
148
bsalomonc2f35b72015-01-23 07:19:22 -0800149void GrGpuResource::makeUnbudgeted() {
150 if (GrGpuResource::kCached_LifeCycle == fLifeCycle && !fContentKey.isValid()) {
151 fLifeCycle = kUncached_LifeCycle;
bsalomon0ea80f42015-02-11 10:49:59 -0800152 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonc2f35b72015-01-23 07:19:22 -0800153 }
154}
155
bsalomon6d3fe022014-07-25 08:35:45 -0700156uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -0700157 static int32_t gUniqueID = SK_InvalidUniqueID;
158 uint32_t id;
159 do {
160 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
161 } while (id == SK_InvalidUniqueID);
162 return id;
163}