blob: d86ec7cafd1e6634ae676236dcc7c5f2a0236018 [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()) {
bsalomon9f2d1572015-02-17 11:47:40 -080026 SkDEBUGCODE(fCacheArrayIndex = -1);
bsalomon16961262014-08-26 14:01:07 -070027}
28
29void GrGpuResource::registerWithCache() {
bsalomon0ea80f42015-02-11 10:49:59 -080030 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000031}
32
bsalomon6d3fe022014-07-25 08:35:45 -070033GrGpuResource::~GrGpuResource() {
bsalomon12299ab2014-11-14 13:33:09 -080034 // The cache should have released or destroyed this resource.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000035 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000036}
37
bsalomon16961262014-08-26 14:01:07 -070038void GrGpuResource::release() {
bsalomon12299ab2014-11-14 13:33:09 -080039 SkASSERT(fGpu);
40 this->onRelease();
bsalomon0ea80f42015-02-11 10:49:59 -080041 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
bsalomon12299ab2014-11-14 13:33:09 -080042 fGpu = NULL;
43 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044}
45
bsalomon6d3fe022014-07-25 08:35:45 -070046void GrGpuResource::abandon() {
bsalomon12299ab2014-11-14 13:33:09 -080047 SkASSERT(fGpu);
48 this->onAbandon();
bsalomon0ea80f42015-02-11 10:49:59 -080049 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
bsalomon12299ab2014-11-14 13:33:09 -080050 fGpu = NULL;
51 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000052}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000053
junov5756aff2014-12-11 14:59:31 -080054const SkData* GrGpuResource::setCustomData(const SkData* data) {
55 SkSafeRef(data);
56 fData.reset(data);
57 return data;
58}
59
bsalomon6d3fe022014-07-25 08:35:45 -070060const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -070061 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000062 return fGpu->getContext();
63 } else {
64 return NULL;
65 }
66}
67
bsalomon6d3fe022014-07-25 08:35:45 -070068GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -070069 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000070 return fGpu->getContext();
71 } else {
72 return NULL;
73 }
74}
bsalomonc44be0e2014-07-25 07:32:33 -070075
bsalomon71cb0c22014-11-14 12:10:14 -080076void GrGpuResource::didChangeGpuMemorySize() const {
77 if (this->wasDestroyed()) {
78 return;
79 }
80
81 size_t oldSize = fGpuMemorySize;
82 SkASSERT(kInvalidGpuMemorySize != oldSize);
83 fGpuMemorySize = kInvalidGpuMemorySize;
bsalomon0ea80f42015-02-11 10:49:59 -080084 get_resource_cache(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
bsalomon71cb0c22014-11-14 12:10:14 -080085}
86
bsalomon23e619c2015-02-06 11:54:28 -080087void GrGpuResource::removeContentKey() {
88 SkASSERT(fContentKey.isValid());
bsalomon0ea80f42015-02-11 10:49:59 -080089 get_resource_cache(fGpu)->resourceAccess().willRemoveContentKey(this);
bsalomon23e619c2015-02-06 11:54:28 -080090 fContentKey.reset();
91}
92
bsalomon24db3b12015-01-23 04:24:04 -080093bool GrGpuResource::setContentKey(const GrContentKey& key) {
bsalomon6d4488c2014-11-11 07:27:16 -080094 // Currently this can only be called once and can't be called when the resource is scratch.
95 SkASSERT(this->internalHasRef());
bsalomon23e619c2015-02-06 11:54:28 -080096 SkASSERT(key.isValid());
bsalomondace19e2014-11-17 07:34:06 -080097
bsalomonc2f35b72015-01-23 07:19:22 -080098 // Wrapped and uncached resources can never have a content key.
bsalomon3582d3e2015-02-13 14:20:05 -080099 if (!this->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800100 return false;
101 }
bsalomon84c8e622014-11-17 09:33:27 -0800102
bsalomon24db3b12015-01-23 04:24:04 -0800103 if (fContentKey.isValid() || this->wasDestroyed()) {
bsalomon6d4488c2014-11-11 07:27:16 -0800104 return false;
bsalomon8b79d232014-11-10 10:19:06 -0800105 }
106
bsalomon24db3b12015-01-23 04:24:04 -0800107 fContentKey = key;
bsalomon6d4488c2014-11-11 07:27:16 -0800108
bsalomon0ea80f42015-02-11 10:49:59 -0800109 if (!get_resource_cache(fGpu)->resourceAccess().didSetContentKey(this)) {
bsalomon24db3b12015-01-23 04:24:04 -0800110 fContentKey.reset();
bsalomon6d4488c2014-11-11 07:27:16 -0800111 return false;
bsalomon8b79d232014-11-10 10:19:06 -0800112 }
113 return true;
114}
115
bsalomon63c992f2015-01-23 12:47:59 -0800116void GrGpuResource::notifyIsPurgeable() const {
bsalomon12299ab2014-11-14 13:33:09 -0800117 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);
bsalomon0ea80f42015-02-11 10:49:59 -0800122 get_resource_cache(fGpu)->resourceAccess().notifyPurgeable(mutableThis);
bsalomonbcf0a522014-10-08 08:40:09 -0700123 }
124}
125
bsalomon7775c852014-12-30 12:50:52 -0800126void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) {
127 SkASSERT(!fScratchKey.isValid());
128 SkASSERT(scratchKey.isValid());
129 // Wrapped resources can never have a scratch key.
bsalomondace19e2014-11-17 07:34:06 -0800130 if (this->isWrapped()) {
131 return;
132 }
bsalomon744998e2014-08-28 09:54:34 -0700133 fScratchKey = scratchKey;
134}
135
bsalomon10e23ca2014-11-25 05:52:06 -0800136void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800137 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon0ea80f42015-02-11 10:49:59 -0800138 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800139 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800140 }
141}
142
bsalomonafe30052015-01-16 07:32:33 -0800143void GrGpuResource::makeBudgeted() {
144 if (GrGpuResource::kUncached_LifeCycle == fLifeCycle) {
145 fLifeCycle = kCached_LifeCycle;
bsalomon0ea80f42015-02-11 10:49:59 -0800146 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonafe30052015-01-16 07:32:33 -0800147 }
148}
149
bsalomonc2f35b72015-01-23 07:19:22 -0800150void GrGpuResource::makeUnbudgeted() {
151 if (GrGpuResource::kCached_LifeCycle == fLifeCycle && !fContentKey.isValid()) {
152 fLifeCycle = kUncached_LifeCycle;
bsalomon0ea80f42015-02-11 10:49:59 -0800153 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
bsalomonc2f35b72015-01-23 07:19:22 -0800154 }
155}
156
bsalomon6d3fe022014-07-25 08:35:45 -0700157uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -0700158 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}