blob: a2df7e13e8cd388922b840a6b8fbadf34e5e3509 [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
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 , fFlags(0)
25 , fLifeCycle(lifeCycle)
bsalomon84c8e622014-11-17 09:33:27 -080026 , fUniqueID(CreateUniqueID()) {
bsalomon16961262014-08-26 14:01:07 -070027}
28
29void GrGpuResource::registerWithCache() {
bsalomon71cb0c22014-11-14 12:10:14 -080030 get_resource_cache2(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();
41 get_resource_cache2(fGpu)->resourceAccess().removeResource(this);
42 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();
49 get_resource_cache2(fGpu)->resourceAccess().removeResource(this);
50 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;
84 get_resource_cache2(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
85}
86
bsalomon6d4488c2014-11-11 07:27:16 -080087bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) {
bsalomon6d4488c2014-11-11 07:27:16 -080088 // Currently this can only be called once and can't be called when the resource is scratch.
89 SkASSERT(this->internalHasRef());
bsalomondace19e2014-11-17 07:34:06 -080090
91 // Wrapped resources can never have a key.
92 if (this->isWrapped()) {
93 return false;
94 }
bsalomon84c8e622014-11-17 09:33:27 -080095
96 if ((fFlags & kContentKeySet_Flag) || this->wasDestroyed()) {
bsalomon6d4488c2014-11-11 07:27:16 -080097 return false;
bsalomon8b79d232014-11-10 10:19:06 -080098 }
99
bsalomon6d4488c2014-11-11 07:27:16 -0800100 fContentKey = contentKey;
bsalomon84c8e622014-11-17 09:33:27 -0800101 fFlags |= kContentKeySet_Flag;
bsalomon6d4488c2014-11-11 07:27:16 -0800102
bsalomon71cb0c22014-11-14 12:10:14 -0800103 if (!get_resource_cache2(fGpu)->resourceAccess().didSetContentKey(this)) {
bsalomon84c8e622014-11-17 09:33:27 -0800104 fFlags &= ~kContentKeySet_Flag;
bsalomon6d4488c2014-11-11 07:27:16 -0800105 return false;
bsalomon8b79d232014-11-10 10:19:06 -0800106 }
107 return true;
108}
109
bsalomonbcf0a522014-10-08 08:40:09 -0700110void GrGpuResource::notifyIsPurgable() const {
bsalomon12299ab2014-11-14 13:33:09 -0800111 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);
bsalomonbcf0a522014-10-08 08:40:09 -0700117 }
118}
119
bsalomon7775c852014-12-30 12:50:52 -0800120void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) {
121 SkASSERT(!fScratchKey.isValid());
122 SkASSERT(scratchKey.isValid());
123 // Wrapped resources can never have a scratch key.
bsalomondace19e2014-11-17 07:34:06 -0800124 if (this->isWrapped()) {
125 return;
126 }
bsalomon744998e2014-08-28 09:54:34 -0700127 fScratchKey = scratchKey;
128}
129
bsalomon10e23ca2014-11-25 05:52:06 -0800130void GrGpuResource::removeScratchKey() {
bsalomon7775c852014-12-30 12:50:52 -0800131 if (!this->wasDestroyed() && fScratchKey.isValid()) {
bsalomon10e23ca2014-11-25 05:52:06 -0800132 get_resource_cache2(fGpu)->resourceAccess().willRemoveScratchKey(this);
bsalomon7775c852014-12-30 12:50:52 -0800133 fScratchKey.reset();
bsalomon10e23ca2014-11-25 05:52:06 -0800134 }
135}
136
bsalomon6d3fe022014-07-25 08:35:45 -0700137uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -0700138 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}