OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrVkResource_DEFINED | 8 #ifndef GrVkResource_DEFINED |
9 #define GrVkResource_DEFINED | 9 #define GrVkResource_DEFINED |
10 | 10 |
11 #include "SkAtomics.h" | 11 #include "SkAtomics.h" |
12 #include "SkTDynamicHash.h" | |
13 #include "SkRandom.h" | 12 #include "SkRandom.h" |
13 #include "SkTHash.h" | |
14 | 14 |
15 class GrVkGpu; | 15 class GrVkGpu; |
16 | 16 |
17 // uncomment to enable tracing of resource refs | 17 // uncomment to enable tracing of resource refs |
18 #ifdef SK_DEBUG | 18 #ifdef SK_DEBUG |
19 #define SK_TRACE_VK_RESOURCES | 19 #define SK_TRACE_VK_RESOURCES |
20 #endif | 20 #endif |
21 | 21 |
22 /** \class GrVkResource | 22 /** \class GrVkResource |
23 | 23 |
24 GrVkResource is the base class for Vulkan resources that may be shared by mult iple | 24 GrVkResource is the base class for Vulkan resources that may be shared by mult iple |
25 objects. When an existing owner wants to share a reference, it calls ref(). | 25 objects. When an existing owner wants to share a reference, it calls ref(). |
26 When an owner wants to release its reference, it calls unref(). When the | 26 When an owner wants to release its reference, it calls unref(). When the |
27 shared object's reference count goes to zero as the result of an unref() | 27 shared object's reference count goes to zero as the result of an unref() |
28 call, its (virtual) destructor is called. It is an error for the | 28 call, its (virtual) destructor is called. It is an error for the |
29 destructor to be called explicitly (or via the object going out of scope on | 29 destructor to be called explicitly (or via the object going out of scope on |
30 the stack or calling delete) if getRefCnt() > 1. | 30 the stack or calling delete) if getRefCnt() > 1. |
31 | 31 |
32 This is nearly identical to SkRefCntBase. The exceptions are that unref() | 32 This is nearly identical to SkRefCntBase. The exceptions are that unref() |
33 takes a GrVkGpu, and any derived classes must implement freeGPUData() and | 33 takes a GrVkGpu, and any derived classes must implement freeGPUData() and |
34 possibly abandonSubResources(). | 34 possibly abandonSubResources(). |
35 */ | 35 */ |
36 | 36 |
37 class GrVkResource : SkNoncopyable { | 37 class GrVkResource : SkNoncopyable { |
38 public: | 38 public: |
39 // Simple refCount tracing, to ensure that everything ref'ed is unref'ed. | 39 // Simple refCount tracing, to ensure that everything ref'ed is unref'ed. |
40 #ifdef SK_TRACE_VK_RESOURCES | 40 #ifdef SK_TRACE_VK_RESOURCES |
41 static const uint32_t& GetKey(const GrVkResource& r) { return r.fKey; } | 41 struct Hash { |
42 static uint32_t Hash(const uint32_t& k) { return k; } | 42 uint32_t operator()(const GrVkResource* const& r) const { |
43 SkASSERT(r); | |
44 return r->fKey; | |
45 } | |
46 }; | |
43 | 47 |
44 class Trace { | 48 class Trace { |
45 public: | 49 public: |
46 ~Trace() { | 50 ~Trace() { |
47 if (fHash.count()) { | 51 if (fHashSet.count()) { |
mtklein
2016/07/29 14:33:19
No real need for this if?
egdaniel
2016/07/29 15:05:41
Ah right good catch. Gone.
| |
48 SkTDynamicHash<GrVkResource, uint32_t>::Iter iter(&fHash); | 52 fHashSet.foreach([](const GrVkResource* r) { |
49 for (; !iter.done(); ++iter) { | 53 r->dumpInfo(); |
50 (*iter).dumpInfo(); | 54 }); |
51 } | |
52 } | 55 } |
53 SkASSERT(0 == fHash.count()); | 56 SkASSERT(0 == fHashSet.count()); |
54 } | 57 } |
55 void add(GrVkResource* r) { fHash.add(r); } | 58 void add(const GrVkResource* r) { fHashSet.add(r); } |
56 void remove(const GrVkResource* r) { fHash.remove(GetKey(*r)); } | 59 void remove(const GrVkResource* r) { fHashSet.remove(r); } |
57 | 60 |
58 private: | 61 private: |
59 SkTDynamicHash<GrVkResource, uint32_t> fHash; | 62 SkTHashSet<const GrVkResource*, GrVkResource::Hash> fHashSet; |
60 }; | 63 }; |
61 static Trace fTrace; | 64 static Trace fTrace; |
62 | 65 |
63 static uint32_t fKeyCounter; | 66 static uint32_t fKeyCounter; |
64 #endif | 67 #endif |
65 | 68 |
66 /** Default construct, initializing the reference count to 1. | 69 /** Default construct, initializing the reference count to 1. |
67 */ | 70 */ |
68 GrVkResource() : fRefCnt(1) { | 71 GrVkResource() : fRefCnt(1) { |
69 #ifdef SK_TRACE_VK_RESOURCES | 72 #ifdef SK_TRACE_VK_RESOURCES |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 } else { | 206 } else { |
204 this->unref(gpu); | 207 this->unref(gpu); |
205 } | 208 } |
206 } | 209 } |
207 | 210 |
208 private: | 211 private: |
209 virtual void onRecycle(GrVkGpu* gpu) const = 0; | 212 virtual void onRecycle(GrVkGpu* gpu) const = 0; |
210 }; | 213 }; |
211 | 214 |
212 #endif | 215 #endif |
OLD | NEW |