blob: b2fad0b3f7da2dcd7df53c2d21851be26ce71705 [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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon6d3fe022014-07-25 08:35:45 -070010#include "GrGpuResource.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070011#include "GrResourceCache2.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000012#include "GrGpu.h"
13
bsalomonc8dc1f72014-08-21 13:02:13 -070014static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) {
bsalomon49f085d2014-09-05 13:34:00 -070015 SkASSERT(gpu);
16 SkASSERT(gpu->getContext());
17 SkASSERT(gpu->getContext()->getResourceCache2());
bsalomonc8dc1f72014-08-21 13:02:13 -070018 return gpu->getContext()->getResourceCache2();
19}
20
bsalomon6d3fe022014-07-25 08:35:45 -070021GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped)
bsalomon7775c852014-12-30 12:50:52 -080022 : fGpu(gpu)
bsalomon69ed47f2014-11-12 11:13:39 -080023 , fGpuMemorySize(kInvalidGpuMemorySize)
bsalomon84c8e622014-11-17 09:33:27 -080024 , fUniqueID(CreateUniqueID()) {
bsalomon@google.com72830222013-01-23 20:25:22 +000025 if (isWrapped) {
bsalomon84c8e622014-11-17 09:33:27 -080026 fFlags = kWrapped_Flag;
bsalomon@google.com72830222013-01-23 20:25:22 +000027 } else {
bsalomon84c8e622014-11-17 09:33:27 -080028 // By default all non-wrapped resources are budgeted.
29 fFlags = kBudgeted_Flag;
bsalomon@google.com72830222013-01-23 20:25:22 +000030 }
bsalomon16961262014-08-26 14:01:07 -070031}
32
33void GrGpuResource::registerWithCache() {
bsalomon71cb0c22014-11-14 12:10:14 -080034 get_resource_cache2(fGpu)->resourceAccess().insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000035}
36
bsalomon6d3fe022014-07-25 08:35:45 -070037GrGpuResource::~GrGpuResource() {
bsalomon12299ab2014-11-14 13:33:09 -080038 // The cache should have released or destroyed this resource.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000039 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000040}
41
bsalomon16961262014-08-26 14:01:07 -070042void GrGpuResource::release() {
bsalomon12299ab2014-11-14 13:33:09 -080043 SkASSERT(fGpu);
44 this->onRelease();
45 get_resource_cache2(fGpu)->resourceAccess().removeResource(this);
46 fGpu = NULL;
47 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000048}
49
bsalomon6d3fe022014-07-25 08:35:45 -070050void GrGpuResource::abandon() {
bsalomon12299ab2014-11-14 13:33:09 -080051 SkASSERT(fGpu);
52 this->onAbandon();
53 get_resource_cache2(fGpu)->resourceAccess().removeResource(this);
54 fGpu = NULL;
55 fGpuMemorySize = 0;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000056}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000057
junov5756aff2014-12-11 14:59:31 -080058const SkData* GrGpuResource::setCustomData(const SkData* data) {
59 SkSafeRef(data);
60 fData.reset(data);
61 return data;
62}
63
bsalomon6d3fe022014-07-25 08:35:45 -070064const GrContext* GrGpuResource::getContext() const {
bsalomon49f085d2014-09-05 13:34:00 -070065 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000066 return fGpu->getContext();
67 } else {
68 return NULL;
69 }
70}
71
bsalomon6d3fe022014-07-25 08:35:45 -070072GrContext* GrGpuResource::getContext() {
bsalomon49f085d2014-09-05 13:34:00 -070073 if (fGpu) {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000074 return fGpu->getContext();
75 } else {
76 return NULL;
77 }
78}
bsalomonc44be0e2014-07-25 07:32:33 -070079
bsalomon71cb0c22014-11-14 12:10:14 -080080void GrGpuResource::didChangeGpuMemorySize() const {
81 if (this->wasDestroyed()) {
82 return;
83 }
84
85 size_t oldSize = fGpuMemorySize;
86 SkASSERT(kInvalidGpuMemorySize != oldSize);
87 fGpuMemorySize = kInvalidGpuMemorySize;
88 get_resource_cache2(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
89}
90
bsalomon6d4488c2014-11-11 07:27:16 -080091bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) {
bsalomon6d4488c2014-11-11 07:27:16 -080092 // Currently this can only be called once and can't be called when the resource is scratch.
93 SkASSERT(this->internalHasRef());
bsalomondace19e2014-11-17 07:34:06 -080094
95 // Wrapped resources can never have a key.
96 if (this->isWrapped()) {
97 return false;
98 }
bsalomon84c8e622014-11-17 09:33:27 -080099
100 if ((fFlags & kContentKeySet_Flag) || this->wasDestroyed()) {
bsalomon6d4488c2014-11-11 07:27:16 -0800101 return false;
bsalomon8b79d232014-11-10 10:19:06 -0800102 }
103
bsalomon6d4488c2014-11-11 07:27:16 -0800104 fContentKey = contentKey;
bsalomon84c8e622014-11-17 09:33:27 -0800105 fFlags |= kContentKeySet_Flag;
bsalomon6d4488c2014-11-11 07:27:16 -0800106
bsalomon71cb0c22014-11-14 12:10:14 -0800107 if (!get_resource_cache2(fGpu)->resourceAccess().didSetContentKey(this)) {
bsalomon84c8e622014-11-17 09:33:27 -0800108 fFlags &= ~kContentKeySet_Flag;
bsalomon6d4488c2014-11-11 07:27:16 -0800109 return false;
bsalomon8b79d232014-11-10 10:19:06 -0800110 }
111 return true;
112}
113
bsalomonbcf0a522014-10-08 08:40:09 -0700114void GrGpuResource::notifyIsPurgable() const {
bsalomon12299ab2014-11-14 13:33:09 -0800115 if (this->wasDestroyed()) {
116 // We've already been removed from the cache. Goodbye cruel world!
117 SkDELETE(this);
118 } else {
119 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
120 get_resource_cache2(fGpu)->resourceAccess().notifyPurgable(mutableThis);
bsalomonbcf0a522014-10-08 08:40:09 -0700121 }
122}
123
bsalomon7775c852014-12-30 12:50:52 -0800124void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) {
125 SkASSERT(!fScratchKey.isValid());
126 SkASSERT(scratchKey.isValid());
127 // Wrapped resources can never have a scratch key.
bsalomondace19e2014-11-17 07:34:06 -0800128 if (this->isWrapped()) {
129 return;
130 }
bsalomon744998e2014-08-28 09:54:34 -0700131 fScratchKey = scratchKey;
132}
133
bsalomon10e23ca2014-11-25 05:52:06 -0800134void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800135 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon10e23ca2014-11-25 05:52:06 -0800136 get_resource_cache2(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800137 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800138 }
139}
140
bsalomon6d3fe022014-07-25 08:35:45 -0700141uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -0700142 static int32_t gUniqueID = SK_InvalidUniqueID;
143 uint32_t id;
144 do {
145 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
146 } while (id == SK_InvalidUniqueID);
147 return id;
148}
bsalomon84c8e622014-11-17 09:33:27 -0800149
150void GrGpuResource::setBudgeted(bool countsAgainstBudget) {
151 // Wrapped resources never count against the budget, nothing to do. No point in changing the
152 // budgeting of destroyed resources.
153 if (this->isWrapped() || this->wasDestroyed()) {
154 return;
155 }
156
157 uint32_t oldFlags = fFlags;
158 if (countsAgainstBudget) {
159 fFlags |= kBudgeted_Flag;
160 } else {
161 fFlags &= ~kBudgeted_Flag;
162 }
163 if (fFlags != oldFlags) {
164 get_resource_cache2(fGpu)->resourceAccess().didChangeBudgetStatus(this);
165 }
166}