OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2014 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 GrGpuObject_DEFINED | 8 #ifndef GrGpuObject_DEFINED |
9 #define GrGpuObject_DEFINED | 9 #define GrGpuObject_DEFINED |
10 | 10 |
11 #include "GrCacheable.h" | 11 #include "SkInstCnt.h" |
12 #include "SkTInternalLList.h" | 12 #include "SkTInternalLList.h" |
13 | 13 |
| 14 class GrResourceCacheEntry; |
14 class GrGpu; | 15 class GrGpu; |
15 class GrContext; | 16 class GrContext; |
16 | 17 |
17 /** | 18 /** |
18 * Base class for the GPU objects created by a GrContext. | 19 * Base class for objects that can be kept in the GrResourceCache. |
19 */ | 20 */ |
20 class GrGpuObject : public GrCacheable { | 21 class GrGpuObject : public SkNoncopyable { |
21 public: | 22 public: |
22 SK_DECLARE_INST_COUNT(GrGpuObject) | 23 SK_DECLARE_INST_COUNT_ROOT(GrGpuObject) |
| 24 |
| 25 // These method signatures are written to mirror SkRefCnt. However, we don't
require |
| 26 // thread safety as GrCacheable objects are not intended to cross thread bou
ndaries. |
| 27 // internal_dispose() exists because of GrTexture's reliance on it. It will
be removed |
| 28 // soon. |
| 29 void ref() const { ++fRefCnt; } |
| 30 void unref() const { --fRefCnt; if (0 == fRefCnt) { this->internal_dispose()
; } } |
| 31 virtual void internal_dispose() const { SkDELETE(this); } |
| 32 bool unique() const { return 1 == fRefCnt; } |
| 33 #ifdef SK_DEBUG |
| 34 void validate() const { |
| 35 SkASSERT(fRefCnt > 0); |
| 36 } |
| 37 #endif |
23 | 38 |
24 /** | 39 /** |
25 * Frees the object in the underlying 3D API. It must be safe to call this | 40 * Frees the object in the underlying 3D API. It must be safe to call this |
26 * when the object has been previously abandoned. | 41 * when the object has been previously abandoned. |
27 */ | 42 */ |
28 void release(); | 43 void release(); |
29 | 44 |
30 /** | 45 /** |
31 * Removes references to objects in the underlying 3D API without freeing | 46 * Removes references to objects in the underlying 3D API without freeing |
32 * them. Used when the API context has been torn down before the GrContext. | 47 * them. Used when the API context has been torn down before the GrContext. |
(...skipping 14 matching lines...) Expand all Loading... |
47 | 62 |
48 /** | 63 /** |
49 * Retrieves the context that owns the object. Note that it is possible for | 64 * Retrieves the context that owns the object. Note that it is possible for |
50 * this to return NULL. When objects have been release()ed or abandon()ed | 65 * this to return NULL. When objects have been release()ed or abandon()ed |
51 * they no longer have an owning context. Destroying a GrContext | 66 * they no longer have an owning context. Destroying a GrContext |
52 * automatically releases all its resources. | 67 * automatically releases all its resources. |
53 */ | 68 */ |
54 const GrContext* getContext() const; | 69 const GrContext* getContext() const; |
55 GrContext* getContext(); | 70 GrContext* getContext(); |
56 | 71 |
57 virtual bool isValidOnGpu() const SK_OVERRIDE { return !this->wasDestroyed()
; } | 72 /** |
| 73 * Retrieves the amount of GPU memory used by this resource in bytes. It is |
| 74 * approximate since we aren't aware of additional padding or copies made |
| 75 * by the driver. |
| 76 * |
| 77 * @return the amount of GPU memory used in bytes |
| 78 */ |
| 79 virtual size_t gpuMemorySize() const = 0; |
| 80 |
| 81 void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEn
try; } |
| 82 GrResourceCacheEntry* getCacheEntry() { return fCacheEntry; } |
| 83 |
| 84 /** |
| 85 * Gets an id that is unique for this GrCacheable object. It is static in th
at it does |
| 86 * not change when the content of the GrCacheable object changes. This will
never return |
| 87 * 0. |
| 88 */ |
| 89 uint32_t getUniqueID() const { return fUniqueID; } |
58 | 90 |
59 protected: | 91 protected: |
60 /** | 92 GrGpuObject(GrGpu*, bool isWrapped); |
61 * isWrapped indicates we have wrapped a client-created backend object in a
GrGpuObject. If it | |
62 * is true then the client is responsible for the lifetime of the underlying
backend object. | |
63 * Otherwise, our onRelease() should free the object. | |
64 */ | |
65 GrGpuObject(GrGpu* gpu, bool isWrapped); | |
66 virtual ~GrGpuObject(); | 93 virtual ~GrGpuObject(); |
67 | 94 |
| 95 bool isInCache() const { return NULL != fCacheEntry; } |
| 96 |
68 GrGpu* getGpu() const { return fGpu; } | 97 GrGpu* getGpu() const { return fGpu; } |
69 | 98 |
70 // Derived classes should always call their parent class' onRelease | 99 // Derived classes should always call their parent class' onRelease |
71 // and onAbandon methods in their overrides. | 100 // and onAbandon methods in their overrides. |
72 virtual void onRelease() {}; | 101 virtual void onRelease() {}; |
73 virtual void onAbandon() {}; | 102 virtual void onAbandon() {}; |
74 | 103 |
75 bool isWrapped() const { return kWrapped_FlagBit & fFlags; } | 104 bool isWrapped() const { return kWrapped_FlagBit & fFlags; } |
76 | 105 |
| 106 /** |
| 107 * This entry point should be called whenever gpuMemorySize() begins |
| 108 * reporting a different size. If the object is in the cache, it will call |
| 109 * gpuMemorySize() immediately and pass the new size on to the resource |
| 110 * cache. |
| 111 */ |
| 112 void didChangeGpuMemorySize() const; |
| 113 |
77 private: | 114 private: |
78 #ifdef SK_DEBUG | 115 #ifdef SK_DEBUG |
79 friend class GrGpu; // for assert in GrGpu to access getGpu | 116 friend class GrGpu; // for assert in GrGpu to access getGpu |
80 #endif | 117 #endif |
81 | 118 |
| 119 static uint32_t CreateUniqueID(); |
| 120 |
82 // We're in an internal doubly linked list | 121 // We're in an internal doubly linked list |
83 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuObject); | 122 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuObject); |
84 | 123 |
85 GrGpu* fGpu; // not reffed. The GrGpu can be dele
ted while there | 124 GrGpu* fGpu; // not reffed. The GrGpu can be dele
ted while there |
86 // are still live GrGpuObjects. It w
ill call | 125 // are still live GrGpuObjects. It w
ill call |
87 // release() on all such objects in
its destructor. | 126 // release() on all such objects in
its destructor. |
88 enum Flags { | 127 enum Flags { |
89 /** | 128 /** |
90 * This object wraps a GPU object given to us by the user. | 129 * This object wraps a GPU object given to us by the user. |
91 * Lifetime management is left up to the user (i.e., we will not | 130 * Lifetime management is left up to the user (i.e., we will not |
92 * free it). | 131 * free it). |
93 */ | 132 */ |
94 kWrapped_FlagBit = 0x1, | 133 kWrapped_FlagBit = 0x1, |
95 }; | 134 }; |
96 uint32_t fFlags; | |
97 | 135 |
98 typedef GrCacheable INHERITED; | 136 uint32_t fFlags; |
| 137 |
| 138 mutable int32_t fRefCnt; |
| 139 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache |
| 140 const uint32_t fUniqueID; |
| 141 |
| 142 typedef SkNoncopyable INHERITED; |
99 }; | 143 }; |
100 | 144 |
101 #endif | 145 #endif |
OLD | NEW |