blob: 15d5888cc07384d74f2c7db5a1ff078a6030be2a [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"
bsalomonc8dc1f72014-08-21 13:02:13 -070010#include "GrResourceCache2.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000011#include "GrGpu.h"
12
bsalomonc8dc1f72014-08-21 13:02:13 -070013static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) {
bsalomon49f085d2014-09-05 13:34:00 -070014 SkASSERT(gpu);
15 SkASSERT(gpu->getContext());
16 SkASSERT(gpu->getContext()->getResourceCache2());
bsalomonc8dc1f72014-08-21 13:02:13 -070017 return gpu->getContext()->getResourceCache2();
18}
19
bsalomon5236cf42015-01-14 10:42:08 -080020GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle)
bsalomon7775c852014-12-30 12:50:52 -080021 : fGpu(gpu)
bsalomon69ed47f2014-11-12 11:13:39 -080022 , fGpuMemorySize(kInvalidGpuMemorySize)
bsalomon5236cf42015-01-14 10:42:08 -080023 , fLifeCycle(lifeCycle)
bsalomon84c8e622014-11-17 09:33:27 -080024 , fUniqueID(CreateUniqueID()) {
bsalomon16961262014-08-26 14:01:07 -070025}
26
27void GrGpuResource::registerWithCache() {
bsalomon71cb0c22014-11-14 12:10:14 -080028 get_resource_cache2(fGpu)->resourceAccess().insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000029}
30
bsalomon6d3fe022014-07-25 08:35:45 -070031GrGpuResource::~GrGpuResource() {
bsalomon12299ab2014-11-14 13:33:09 -080032 // The cache should have released or destroyed this resource.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000033 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000034}
35
bsalomon16961262014-08-26 14:01:07 -070036void GrGpuResource::release() {
bsalomon12299ab2014-11-14 13:33:09 -080037 SkASSERT(fGpu);
38 this->onRelease();
39 get_resource_cache2(fGpu)->resourceAccess().removeResource(this);
40 fGpu = NULL;
41 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000042}
43
bsalomon6d3fe022014-07-25 08:35:45 -070044void GrGpuResource::abandon() {
bsalomon12299ab2014-11-14 13:33:09 -080045 SkASSERT(fGpu);
46 this->onAbandon();
47 get_resource_cache2(fGpu)->resourceAccess().removeResource(this);
48 fGpu = NULL;
49 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000050}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000051
junov5756aff2014-12-11 14:59:31 -080052const SkData* GrGpuResource::setCustomData(const SkData* data) {
53 SkSafeRef(data);
54 fData.reset(data);
55 return data;
56}
57
bsalomon6d3fe022014-07-25 08:35:45 -070058const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -070059 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000060 return fGpu->getContext();
61 } else {
62 return NULL;
63 }
64}
65
bsalomon6d3fe022014-07-25 08:35:45 -070066GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -070067 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000068 return fGpu->getContext();
69 } else {
70 return NULL;
71 }
72}
bsalomonc44be0e2014-07-25 07:32:33 -070073
bsalomon71cb0c22014-11-14 12:10:14 -080074void 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
bsalomon24db3b12015-01-23 04:24:04 -080085bool GrGpuResource::setContentKey(const GrContentKey& key) {
bsalomon6d4488c2014-11-11 07:27:16 -080086 // Currently this can only be called once and can't be called when the resource is scratch.
87 SkASSERT(this->internalHasRef());
bsalomondace19e2014-11-17 07:34:06 -080088
bsalomonc2f35b72015-01-23 07:19:22 -080089 // Wrapped and uncached resources can never have a content key.
90 if (!this->cacheAccess().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -080091 return false;
92 }
bsalomon84c8e622014-11-17 09:33:27 -080093
bsalomon24db3b12015-01-23 04:24:04 -080094 if (fContentKey.isValid() || this->wasDestroyed()) {
bsalomon6d4488c2014-11-11 07:27:16 -080095 return false;
bsalomon8b79d232014-11-10 10:19:06 -080096 }
97
bsalomon24db3b12015-01-23 04:24:04 -080098 fContentKey = key;
bsalomon6d4488c2014-11-11 07:27:16 -080099
bsalomon71cb0c22014-11-14 12:10:14 -0800100 if (!get_resource_cache2(fGpu)->resourceAccess().didSetContentKey(this)) {
bsalomon24db3b12015-01-23 04:24:04 -0800101 fContentKey.reset();
bsalomon6d4488c2014-11-11 07:27:16 -0800102 return false;
bsalomon8b79d232014-11-10 10:19:06 -0800103 }
104 return true;
105}
106
bsalomonbcf0a522014-10-08 08:40:09 -0700107void GrGpuResource::notifyIsPurgable() const {
bsalomon12299ab2014-11-14 13:33:09 -0800108 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);
bsalomonbcf0a522014-10-08 08:40:09 -0700114 }
115}
116
bsalomon7775c852014-12-30 12:50:52 -0800117void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) {
118 SkASSERT(!fScratchKey.isValid());
119 SkASSERT(scratchKey.isValid());
120 // Wrapped resources can never have a scratch key.
bsalomondace19e2014-11-17 07:34:06 -0800121 if (this->isWrapped()) {
122 return;
123 }
bsalomon744998e2014-08-28 09:54:34 -0700124 fScratchKey = scratchKey;
125}
126
bsalomon10e23ca2014-11-25 05:52:06 -0800127void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800128 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon10e23ca2014-11-25 05:52:06 -0800129 get_resource_cache2(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800130 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800131 }
132}
133
bsalomonafe30052015-01-16 07:32:33 -0800134void GrGpuResource::makeBudgeted() {
135 if (GrGpuResource::kUncached_LifeCycle == fLifeCycle) {
136 fLifeCycle = kCached_LifeCycle;
137 get_resource_cache2(fGpu)->resourceAccess().didChangeBudgetStatus(this);
138 }
139}
140
bsalomonc2f35b72015-01-23 07:19:22 -0800141void 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
bsalomon6d3fe022014-07-25 08:35:45 -0700148uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -0700149 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}