OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 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 GrGpuResource_DEFINED | 8 #ifndef GrGpuResource_DEFINED |
9 #define GrGpuResource_DEFINED | 9 #define GrGpuResource_DEFINED |
10 | 10 |
11 #include "SkInstCnt.h" | 11 #include "SkInstCnt.h" |
12 #include "SkTInternalLList.h" | 12 #include "SkTInternalLList.h" |
13 #include "GrResourceKey.h" | 13 #include "GrResourceKey.h" |
14 | 14 |
15 class GrResourceCacheEntry; | 15 class GrResourceCacheEntry; |
16 class GrResourceCache2; | 16 class GrResourceCache2; |
17 class GrGpu; | 17 class GrGpu; |
18 class GrContext; | 18 class GrContext; |
19 | 19 |
20 /** | 20 /** |
21 * Base class for GrGpuResource. Handles the various types of refs we need. Sepa rated out as a base | |
22 * class to isolate the ref-cnting behavior and provide friendship without expos ing all of | |
23 * GrGpuResource. | |
robertphillips
2014/09/03 20:29:30
Is here the place for the explanation of expanded
bsalomon
2014/09/03 20:32:24
Yeah, I'll expand on it here.
| |
24 */ | |
25 class GrGpuRef : public SkNoncopyable { | |
26 public: | |
27 SK_DECLARE_INST_COUNT_ROOT(GrGpuRef) | |
28 | |
29 virtual ~GrGpuRef(); | |
30 | |
31 // Some of the signatures are written to mirror SkRefCnt so that GrGpuResour ce can work with | |
32 // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of | |
33 // refs (e.g. pending reads). We also don't require thread safety as GrCache able objects are | |
34 // not intended to cross thread boundaries. | |
35 // internal_dispose() exists because of GrTexture's reliance on it. It will be removed | |
36 // soon. | |
37 void ref() const { | |
38 ++fRefCnt; | |
39 // pre-validate once internal_dispose is removed (and therefore 0 ref cn t is not allowed). | |
40 this->validate(); | |
41 } | |
42 | |
43 void unref() const { | |
44 this->validate(); | |
45 --fRefCnt; | |
46 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { | |
47 this->internal_dispose(); | |
48 } | |
49 } | |
50 | |
51 virtual void internal_dispose() const { SkDELETE(this); } | |
52 | |
53 /** This is exists to service the old mechanism for recycling scratch textur es. It will | |
54 be removed soon. */ | |
55 bool unique() const { return 1 == (fRefCnt + fPendingReads + fPendingWrites) ; } | |
56 | |
57 void validate() const { | |
58 #ifdef SK_DEBUG | |
59 SkASSERT(fRefCnt >= 0); | |
60 SkASSERT(fPendingReads >= 0); | |
61 SkASSERT(fPendingWrites >= 0); | |
62 SkASSERT(fRefCnt + fPendingReads + fPendingWrites > 0); | |
63 #endif | |
64 } | |
65 | |
66 protected: | |
67 GrGpuRef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {} | |
68 | |
69 private: | |
70 void addPendingRead() const { | |
71 this->validate(); | |
72 ++fPendingReads; | |
73 } | |
74 | |
75 void completedRead() const { | |
76 this->validate(); | |
77 --fPendingReads; | |
78 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { | |
79 this->internal_dispose(); | |
80 } | |
81 } | |
82 | |
83 void addPendingWrite() const { | |
84 this->validate(); | |
85 ++fPendingWrites; | |
86 } | |
87 | |
88 void completedWrite() const { | |
89 this->validate(); | |
90 --fPendingWrites; | |
91 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { | |
92 this->internal_dispose(); | |
93 } | |
94 } | |
95 | |
96 private: | |
97 mutable int32_t fRefCnt; | |
98 mutable int32_t fPendingReads; | |
99 mutable int32_t fPendingWrites; | |
100 | |
101 // These functions need access to the pending read/write member functions. | |
102 friend class GrDrawState; | |
103 friend class GrProgramResource; | |
104 }; | |
105 | |
106 /** | |
21 * Base class for objects that can be kept in the GrResourceCache. | 107 * Base class for objects that can be kept in the GrResourceCache. |
22 */ | 108 */ |
23 class GrGpuResource : public SkNoncopyable { | 109 class GrGpuResource : public GrGpuRef { |
24 public: | 110 public: |
25 SK_DECLARE_INST_COUNT_ROOT(GrGpuResource) | 111 SK_DECLARE_INST_COUNT(GrGpuResource) |
26 | |
27 // These method signatures are written to mirror SkRefCnt. However, we don't require | |
28 // thread safety as GrCacheable objects are not intended to cross thread bou ndaries. | |
29 // internal_dispose() exists because of GrTexture's reliance on it. It will be removed | |
30 // soon. | |
31 void ref() const { ++fRefCnt; } | |
32 void unref() const { --fRefCnt; if (0 == fRefCnt) { this->internal_dispose() ; } } | |
33 virtual void internal_dispose() const { SkDELETE(this); } | |
34 bool unique() const { return 1 == fRefCnt; } | |
35 #ifdef SK_DEBUG | |
36 void validate() const { | |
37 SkASSERT(fRefCnt > 0); | |
38 } | |
39 #endif | |
40 | 112 |
41 /** | 113 /** |
42 * Frees the object in the underlying 3D API. It must be safe to call this | 114 * Frees the object in the underlying 3D API. It must be safe to call this |
43 * when the object has been previously abandoned. | 115 * when the object has been previously abandoned. |
44 */ | 116 */ |
45 void release(); | 117 void release(); |
46 | 118 |
47 /** | 119 /** |
48 * Removes references to objects in the underlying 3D API without freeing | 120 * Removes references to objects in the underlying 3D API without freeing |
49 * them. Used when the API context has been torn down before the GrContext. | 121 * them. Used when the API context has been torn down before the GrContext. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 /** | 219 /** |
148 * This object wraps a GPU object given to us by the user. | 220 * This object wraps a GPU object given to us by the user. |
149 * Lifetime management is left up to the user (i.e., we will not | 221 * Lifetime management is left up to the user (i.e., we will not |
150 * free it). | 222 * free it). |
151 */ | 223 */ |
152 kWrapped_FlagBit = 0x1, | 224 kWrapped_FlagBit = 0x1, |
153 }; | 225 }; |
154 | 226 |
155 uint32_t fFlags; | 227 uint32_t fFlags; |
156 | 228 |
157 mutable int32_t fRefCnt; | |
158 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache | 229 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache |
159 const uint32_t fUniqueID; | 230 const uint32_t fUniqueID; |
160 | 231 |
161 GrResourceKey fScratchKey; | 232 GrResourceKey fScratchKey; |
162 | 233 |
163 typedef SkNoncopyable INHERITED; | 234 typedef GrGpuRef INHERITED; |
164 }; | 235 }; |
165 | 236 |
166 #endif | 237 #endif |
OLD | NEW |