OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "GrGpuResource.h" | 10 #include "GrGpuResource.h" |
11 #include "GrResourceCache2.h" | 11 #include "GrResourceCache2.h" |
12 #include "GrGpu.h" | 12 #include "GrGpu.h" |
13 | 13 |
14 static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) { | 14 static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) { |
15 SkASSERT(gpu); | 15 SkASSERT(gpu); |
16 SkASSERT(gpu->getContext()); | 16 SkASSERT(gpu->getContext()); |
17 SkASSERT(gpu->getContext()->getResourceCache2()); | 17 SkASSERT(gpu->getContext()->getResourceCache2()); |
18 return gpu->getContext()->getResourceCache2(); | 18 return gpu->getContext()->getResourceCache2(); |
19 } | 19 } |
20 | 20 |
| 21 static inline GrResourceCache* get_resource_cache(GrGpu* gpu) { |
| 22 SkASSERT(gpu); |
| 23 SkASSERT(gpu->getContext()); |
| 24 SkASSERT(gpu->getContext()->getResourceCache()); |
| 25 return gpu->getContext()->getResourceCache(); |
| 26 } |
| 27 |
21 GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped) | 28 GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped) |
22 : fGpu(gpu) | 29 : fGpu(gpu) |
| 30 , fCacheEntry(NULL) |
23 , fGpuMemorySize(kInvalidGpuMemorySize) | 31 , fGpuMemorySize(kInvalidGpuMemorySize) |
24 , fUniqueID(CreateUniqueID()) | 32 , fUniqueID(CreateUniqueID()) |
25 , fScratchKey(GrResourceKey::NullScratchKey()) | 33 , fScratchKey(GrResourceKey::NullScratchKey()) |
26 , fContentKeySet(false) { | 34 , fContentKeySet(false) { |
27 if (isWrapped) { | 35 if (isWrapped) { |
28 fFlags = kWrapped_FlagBit; | 36 fFlags = kWrapped_FlagBit; |
29 } else { | 37 } else { |
30 fFlags = 0; | 38 fFlags = 0; |
31 } | 39 } |
32 } | 40 } |
33 | 41 |
34 void GrGpuResource::registerWithCache() { | 42 void GrGpuResource::registerWithCache() { |
35 get_resource_cache2(fGpu)->resourceAccess().insertResource(this); | 43 get_resource_cache2(fGpu)->insertResource(this); |
36 } | 44 } |
37 | 45 |
38 GrGpuResource::~GrGpuResource() { | 46 GrGpuResource::~GrGpuResource() { |
39 // subclass should have released this. | 47 // subclass should have released this. |
40 SkASSERT(this->wasDestroyed()); | 48 SkASSERT(this->wasDestroyed()); |
41 } | 49 } |
42 | 50 |
43 void GrGpuResource::release() { | 51 void GrGpuResource::release() { |
44 if (fGpu) { | 52 if (fGpu) { |
45 this->onRelease(); | 53 this->onRelease(); |
46 get_resource_cache2(fGpu)->resourceAccess().removeResource(this); | 54 get_resource_cache2(fGpu)->removeResource(this); |
47 fGpu = NULL; | 55 fGpu = NULL; |
48 fGpuMemorySize = 0; | |
49 } | 56 } |
50 } | 57 } |
51 | 58 |
52 void GrGpuResource::abandon() { | 59 void GrGpuResource::abandon() { |
53 if (fGpu) { | 60 if (fGpu) { |
54 this->onAbandon(); | 61 this->onAbandon(); |
55 get_resource_cache2(fGpu)->resourceAccess().removeResource(this); | 62 get_resource_cache2(fGpu)->removeResource(this); |
56 fGpu = NULL; | 63 fGpu = NULL; |
57 fGpuMemorySize = 0; | |
58 } | 64 } |
59 } | 65 } |
60 | 66 |
61 const GrContext* GrGpuResource::getContext() const { | 67 const GrContext* GrGpuResource::getContext() const { |
62 if (fGpu) { | 68 if (fGpu) { |
63 return fGpu->getContext(); | 69 return fGpu->getContext(); |
64 } else { | 70 } else { |
65 return NULL; | 71 return NULL; |
66 } | 72 } |
67 } | 73 } |
68 | 74 |
69 GrContext* GrGpuResource::getContext() { | 75 GrContext* GrGpuResource::getContext() { |
70 if (fGpu) { | 76 if (fGpu) { |
71 return fGpu->getContext(); | 77 return fGpu->getContext(); |
72 } else { | 78 } else { |
73 return NULL; | 79 return NULL; |
74 } | 80 } |
75 } | 81 } |
76 | 82 |
77 void GrGpuResource::didChangeGpuMemorySize() const { | |
78 if (this->wasDestroyed()) { | |
79 return; | |
80 } | |
81 | |
82 size_t oldSize = fGpuMemorySize; | |
83 SkASSERT(kInvalidGpuMemorySize != oldSize); | |
84 fGpuMemorySize = kInvalidGpuMemorySize; | |
85 get_resource_cache2(fGpu)->resourceAccess().didChangeGpuMemorySize(this, old
Size); | |
86 } | |
87 | |
88 bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) { | 83 bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) { |
89 // Currently this can only be called once and can't be called when the resou
rce is scratch. | 84 // Currently this can only be called once and can't be called when the resou
rce is scratch. |
90 SkASSERT(!contentKey.isScratch()); | 85 SkASSERT(!contentKey.isScratch()); |
91 SkASSERT(this->internalHasRef()); | 86 SkASSERT(this->internalHasRef()); |
92 | 87 |
93 if (fContentKeySet) { | 88 if (fContentKeySet) { |
94 return false; | 89 return false; |
95 } | 90 } |
96 | 91 |
97 fContentKey = contentKey; | 92 fContentKey = contentKey; |
98 fContentKeySet = true; | 93 fContentKeySet = true; |
99 | 94 |
100 if (!get_resource_cache2(fGpu)->resourceAccess().didSetContentKey(this)) { | 95 if (!get_resource_cache2(fGpu)->didSetContentKey(this)) { |
101 fContentKeySet = false; | 96 fContentKeySet = false; |
102 return false; | 97 return false; |
103 } | 98 } |
104 return true; | 99 return true; |
105 } | 100 } |
106 | 101 |
107 void GrGpuResource::notifyIsPurgable() const { | 102 void GrGpuResource::notifyIsPurgable() const { |
108 if (!this->wasDestroyed()) { | 103 if (fCacheEntry && !this->wasDestroyed()) { |
109 get_resource_cache2(fGpu)->resourceAccess().notifyPurgable(this); | 104 get_resource_cache(fGpu)->notifyPurgable(this); |
110 } | 105 } |
111 } | 106 } |
112 | 107 |
113 void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) { | 108 void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) { |
114 SkASSERT(fScratchKey.isNullScratch()); | 109 SkASSERT(fScratchKey.isNullScratch()); |
115 SkASSERT(scratchKey.isScratch()); | 110 SkASSERT(scratchKey.isScratch()); |
116 SkASSERT(!scratchKey.isNullScratch()); | 111 SkASSERT(!scratchKey.isNullScratch()); |
117 fScratchKey = scratchKey; | 112 fScratchKey = scratchKey; |
118 } | 113 } |
119 | 114 |
120 uint32_t GrGpuResource::CreateUniqueID() { | 115 uint32_t GrGpuResource::CreateUniqueID() { |
121 static int32_t gUniqueID = SK_InvalidUniqueID; | 116 static int32_t gUniqueID = SK_InvalidUniqueID; |
122 uint32_t id; | 117 uint32_t id; |
123 do { | 118 do { |
124 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); | 119 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
125 } while (id == SK_InvalidUniqueID); | 120 } while (id == SK_InvalidUniqueID); |
126 return id; | 121 return id; |
127 } | 122 } |
OLD | NEW |