OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2014 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #ifndef GrGpuObject_DEFINED | |
9 #define GrGpuObject_DEFINED | |
10 | |
11 #include "SkInstCnt.h" | |
12 #include "SkTInternalLList.h" | |
13 | |
14 class GrResourceCacheEntry; | |
15 class GrGpu; | |
16 class GrContext; | |
17 | |
18 /** | |
19 * Base class for objects that can be kept in the GrResourceCache. | |
20 */ | |
21 class GrGpuObject : public SkNoncopyable { | |
22 public: | |
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 | |
38 | |
39 /** | |
40 * Frees the object in the underlying 3D API. It must be safe to call this | |
41 * when the object has been previously abandoned. | |
42 */ | |
43 void release(); | |
44 | |
45 /** | |
46 * Removes references to objects in the underlying 3D API without freeing | |
47 * them. Used when the API context has been torn down before the GrContext. | |
48 */ | |
49 void abandon(); | |
50 | |
51 /** | |
52 * Tests whether a object has been abandoned or released. All objects will | |
53 * be in this state after their creating GrContext is destroyed or has | |
54 * contextLost called. It's up to the client to test wasDestroyed() before | |
55 * attempting to use an object if it holds refs on objects across | |
56 * ~GrContext, freeResources with the force flag, or contextLost. | |
57 * | |
58 * @return true if the object has been released or abandoned, | |
59 * false otherwise. | |
60 */ | |
61 bool wasDestroyed() const { return NULL == fGpu; } | |
62 | |
63 /** | |
64 * Retrieves the context that owns the object. Note that it is possible for | |
65 * this to return NULL. When objects have been release()ed or abandon()ed | |
66 * they no longer have an owning context. Destroying a GrContext | |
67 * automatically releases all its resources. | |
68 */ | |
69 const GrContext* getContext() const; | |
70 GrContext* getContext(); | |
71 | |
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; } | |
90 | |
91 protected: | |
92 GrGpuObject(GrGpu*, bool isWrapped); | |
93 virtual ~GrGpuObject(); | |
94 | |
95 bool isInCache() const { return NULL != fCacheEntry; } | |
96 | |
97 GrGpu* getGpu() const { return fGpu; } | |
98 | |
99 // Derived classes should always call their parent class' onRelease | |
100 // and onAbandon methods in their overrides. | |
101 virtual void onRelease() {}; | |
102 virtual void onAbandon() {}; | |
103 | |
104 bool isWrapped() const { return kWrapped_FlagBit & fFlags; } | |
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 | |
114 private: | |
115 #ifdef SK_DEBUG | |
116 friend class GrGpu; // for assert in GrGpu to access getGpu | |
117 #endif | |
118 | |
119 static uint32_t CreateUniqueID(); | |
120 | |
121 // We're in an internal doubly linked list | |
122 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuObject); | |
123 | |
124 GrGpu* fGpu; // not reffed. The GrGpu can be dele
ted while there | |
125 // are still live GrGpuObjects. It w
ill call | |
126 // release() on all such objects in
its destructor. | |
127 enum Flags { | |
128 /** | |
129 * This object wraps a GPU object given to us by the user. | |
130 * Lifetime management is left up to the user (i.e., we will not | |
131 * free it). | |
132 */ | |
133 kWrapped_FlagBit = 0x1, | |
134 }; | |
135 | |
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; | |
143 }; | |
144 | |
145 #endif | |
OLD | NEW |