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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 kWrite_IOType, | 42 kWrite_IOType, |
43 kRW_IOType | 43 kRW_IOType |
44 }; | 44 }; |
45 | 45 |
46 virtual ~GrIORef(); | 46 virtual ~GrIORef(); |
47 | 47 |
48 // Some of the signatures are written to mirror SkRefCnt so that GrGpuResour ce can work with | 48 // Some of the signatures are written to mirror SkRefCnt so that GrGpuResour ce can work with |
49 // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of | 49 // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of |
50 // refs (e.g. pending reads). We also don't require thread safety as GrCache able objects are | 50 // refs (e.g. pending reads). We also don't require thread safety as GrCache able objects are |
51 // not intended to cross thread boundaries. | 51 // not intended to cross thread boundaries. |
52 // internal_dispose() exists because of GrTexture's reliance on it. It will be removed | |
53 // soon. | |
54 void ref() const { | 52 void ref() const { |
53 this->validate(); | |
55 ++fRefCnt; | 54 ++fRefCnt; |
56 // pre-validate once internal_dispose is removed (and therefore 0 ref cn t is not allowed). | |
57 this->validate(); | |
58 } | 55 } |
59 | 56 |
60 void unref() const { | 57 void unref() const { |
61 this->validate(); | 58 this->validate(); |
62 --fRefCnt; | 59 --fRefCnt; |
63 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { | 60 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { |
64 this->internal_dispose(); | 61 SkDELETE(this); |
65 } | 62 } |
66 } | 63 } |
67 | 64 |
68 virtual void internal_dispose() const { SkDELETE(this); } | 65 bool isPurgable() const { return this->reffedOnlyByCache() && !this->interna lHasPendingIO(); } |
69 | 66 bool reffedOnlyByCache() const { return 1 == fRefCnt; } |
70 /** This is exists to service the old mechanism for recycling scratch textur es. It will | |
71 be removed soon. */ | |
72 bool unique() const { return 1 == (fRefCnt + fPendingReads + fPendingWrites) ; } | |
73 | 67 |
74 void validate() const { | 68 void validate() const { |
75 #ifdef SK_DEBUG | 69 #ifdef SK_DEBUG |
76 SkASSERT(fRefCnt >= 0); | 70 SkASSERT(fRefCnt >= 0); |
77 SkASSERT(fPendingReads >= 0); | 71 SkASSERT(fPendingReads >= 0); |
78 SkASSERT(fPendingWrites >= 0); | 72 SkASSERT(fPendingWrites >= 0); |
79 SkASSERT(fRefCnt + fPendingReads + fPendingWrites > 0); | 73 SkASSERT(fRefCnt + fPendingReads + fPendingWrites > 0); |
80 #endif | 74 #endif |
81 } | 75 } |
82 | 76 |
83 | |
84 protected: | 77 protected: |
85 GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {} | 78 GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0), fIsScratch(kNo_ IsScratch) { } |
86 | 79 |
87 bool internalHasPendingRead() const { return SkToBool(fPendingReads); } | 80 bool internalHasPendingRead() const { return SkToBool(fPendingReads); } |
88 bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); } | 81 bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); } |
89 bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendin gReads); } | 82 bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendin gReads); } |
90 | 83 |
91 private: | 84 private: |
92 void addPendingRead() const { | 85 void addPendingRead() const { |
93 this->validate(); | 86 this->validate(); |
94 ++fPendingReads; | 87 ++fPendingReads; |
95 } | 88 } |
96 | 89 |
97 void completedRead() const { | 90 void completedRead() const { |
98 this->validate(); | 91 this->validate(); |
99 --fPendingReads; | 92 --fPendingReads; |
100 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { | 93 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { |
101 this->internal_dispose(); | 94 SkDELETE(this); |
102 } | 95 } |
103 } | 96 } |
104 | 97 |
105 void addPendingWrite() const { | 98 void addPendingWrite() const { |
106 this->validate(); | 99 this->validate(); |
107 ++fPendingWrites; | 100 ++fPendingWrites; |
108 } | 101 } |
109 | 102 |
110 void completedWrite() const { | 103 void completedWrite() const { |
111 this->validate(); | 104 this->validate(); |
112 --fPendingWrites; | 105 --fPendingWrites; |
113 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { | 106 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { |
114 this->internal_dispose(); | 107 SkDELETE(this); |
115 } | 108 } |
116 } | 109 } |
117 | 110 |
118 private: | 111 private: |
119 mutable int32_t fRefCnt; | 112 mutable int32_t fRefCnt; |
120 mutable int32_t fPendingReads; | 113 mutable int32_t fPendingReads; |
121 mutable int32_t fPendingWrites; | 114 mutable int32_t fPendingWrites; |
122 | 115 |
123 // This class is used to manage conversion of refs to pending reads/writes. | 116 // This class is used to manage conversion of refs to pending reads/writes. |
124 friend class GrGpuResourceRef; | 117 friend class GrGpuResourceRef; |
118 | |
robertphillips
2014/09/29 15:25:07
typo
bsalomon
2014/09/29 19:58:13
Done.
| |
119 // This is temporary until GrReosurceCache is fully replaced by GrResourceCa che2. | |
120 enum IsScratch { | |
121 kNo_IsScratch, | |
122 kYes_IsScratch | |
robertphillips
2014/09/29 15:25:07
Could fIsScratch be const for the time being?
bsalomon
2014/09/29 19:58:13
It'd have to be passed through a whole lot of laye
| |
123 } fIsScratch; | |
124 | |
125 friend class GrContext; // to set the above field. | |
126 friend class GrResourceCache; // to check the above field. | |
127 friend class GrResourceCache2; // to check the above field. | |
128 | |
125 template <typename, IOType> friend class GrPendingIOResource; | 129 template <typename, IOType> friend class GrPendingIOResource; |
126 }; | 130 }; |
127 | 131 |
128 /** | 132 /** |
129 * Base class for objects that can be kept in the GrResourceCache. | 133 * Base class for objects that can be kept in the GrResourceCache. |
130 */ | 134 */ |
131 class GrGpuResource : public GrIORef { | 135 class GrGpuResource : public GrIORef { |
132 public: | 136 public: |
133 SK_DECLARE_INST_COUNT(GrGpuResource) | 137 SK_DECLARE_INST_COUNT(GrGpuResource) |
134 | 138 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 | 254 |
251 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache | 255 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache |
252 const uint32_t fUniqueID; | 256 const uint32_t fUniqueID; |
253 | 257 |
254 GrResourceKey fScratchKey; | 258 GrResourceKey fScratchKey; |
255 | 259 |
256 typedef GrIORef INHERITED; | 260 typedef GrIORef INHERITED; |
257 }; | 261 }; |
258 | 262 |
259 #endif | 263 #endif |
OLD | NEW |