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. |
| 24 * |
| 25 * Gpu resources can have three types of refs: |
| 26 * 1) Normal ref (+ by ref(), - by unref()): These are used by code that is is
suing draw calls |
| 27 * that read and write the resource via GrDrawTarget and by any object that
must own a |
| 28 * GrGpuResource and is itself owned (directly or indirectly) by Skia-clien
t code. |
| 29 * 2) Pending read (+ by addPendingRead(), - by readCompleted()): GrContext ha
s scheduled a read |
| 30 * of the resource by the GPU as a result of a skia API call but hasn't exe
cuted it yet. |
| 31 * 3) Pending write (+ by addPendingWrite(), - by writeCompleted()): GrContext
has scheduled a |
| 32 * write to the resource by the GPU as a result of a skia API call but hasn
't executed it yet. |
| 33 * |
| 34 * The latter two ref types are private and intended only for Gr core code. |
| 35 */ |
| 36 class GrGpuRef : public SkNoncopyable { |
| 37 public: |
| 38 SK_DECLARE_INST_COUNT_ROOT(GrGpuRef) |
| 39 |
| 40 virtual ~GrGpuRef(); |
| 41 |
| 42 // Some of the signatures are written to mirror SkRefCnt so that GrGpuResour
ce can work with |
| 43 // templated helper classes (e.g. SkAutoTUnref). However, we have different
categories of |
| 44 // refs (e.g. pending reads). We also don't require thread safety as GrCache
able objects are |
| 45 // not intended to cross thread boundaries. |
| 46 // internal_dispose() exists because of GrTexture's reliance on it. It will
be removed |
| 47 // soon. |
| 48 void ref() const { |
| 49 ++fRefCnt; |
| 50 // pre-validate once internal_dispose is removed (and therefore 0 ref cn
t is not allowed). |
| 51 this->validate(); |
| 52 } |
| 53 |
| 54 void unref() const { |
| 55 this->validate(); |
| 56 --fRefCnt; |
| 57 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { |
| 58 this->internal_dispose(); |
| 59 } |
| 60 } |
| 61 |
| 62 virtual void internal_dispose() const { SkDELETE(this); } |
| 63 |
| 64 /** This is exists to service the old mechanism for recycling scratch textur
es. It will |
| 65 be removed soon. */ |
| 66 bool unique() const { return 1 == (fRefCnt + fPendingReads + fPendingWrites)
; } |
| 67 |
| 68 void validate() const { |
| 69 #ifdef SK_DEBUG |
| 70 SkASSERT(fRefCnt >= 0); |
| 71 SkASSERT(fPendingReads >= 0); |
| 72 SkASSERT(fPendingWrites >= 0); |
| 73 SkASSERT(fRefCnt + fPendingReads + fPendingWrites > 0); |
| 74 #endif |
| 75 } |
| 76 |
| 77 protected: |
| 78 GrGpuRef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {} |
| 79 |
| 80 private: |
| 81 void addPendingRead() const { |
| 82 this->validate(); |
| 83 ++fPendingReads; |
| 84 } |
| 85 |
| 86 void completedRead() const { |
| 87 this->validate(); |
| 88 --fPendingReads; |
| 89 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { |
| 90 this->internal_dispose(); |
| 91 } |
| 92 } |
| 93 |
| 94 void addPendingWrite() const { |
| 95 this->validate(); |
| 96 ++fPendingWrites; |
| 97 } |
| 98 |
| 99 void completedWrite() const { |
| 100 this->validate(); |
| 101 --fPendingWrites; |
| 102 if (0 == fRefCnt && 0 == fPendingReads && 0 == fPendingWrites) { |
| 103 this->internal_dispose(); |
| 104 } |
| 105 } |
| 106 |
| 107 private: |
| 108 mutable int32_t fRefCnt; |
| 109 mutable int32_t fPendingReads; |
| 110 mutable int32_t fPendingWrites; |
| 111 |
| 112 // These functions need access to the pending read/write member functions. |
| 113 friend class GrDrawState; |
| 114 friend class GrProgramResource; |
| 115 }; |
| 116 |
| 117 /** |
21 * Base class for objects that can be kept in the GrResourceCache. | 118 * Base class for objects that can be kept in the GrResourceCache. |
22 */ | 119 */ |
23 class GrGpuResource : public SkNoncopyable { | 120 class GrGpuResource : public GrGpuRef { |
24 public: | 121 public: |
25 SK_DECLARE_INST_COUNT_ROOT(GrGpuResource) | 122 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 | 123 |
41 /** | 124 /** |
42 * Frees the object in the underlying 3D API. It must be safe to call this | 125 * Frees the object in the underlying 3D API. It must be safe to call this |
43 * when the object has been previously abandoned. | 126 * when the object has been previously abandoned. |
44 */ | 127 */ |
45 void release(); | 128 void release(); |
46 | 129 |
47 /** | 130 /** |
48 * Removes references to objects in the underlying 3D API without freeing | 131 * 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. | 132 * 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 /** | 230 /** |
148 * This object wraps a GPU object given to us by the user. | 231 * 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 | 232 * Lifetime management is left up to the user (i.e., we will not |
150 * free it). | 233 * free it). |
151 */ | 234 */ |
152 kWrapped_FlagBit = 0x1, | 235 kWrapped_FlagBit = 0x1, |
153 }; | 236 }; |
154 | 237 |
155 uint32_t fFlags; | 238 uint32_t fFlags; |
156 | 239 |
157 mutable int32_t fRefCnt; | |
158 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache | 240 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache |
159 const uint32_t fUniqueID; | 241 const uint32_t fUniqueID; |
160 | 242 |
161 GrResourceKey fScratchKey; | 243 GrResourceKey fScratchKey; |
162 | 244 |
163 typedef SkNoncopyable INHERITED; | 245 typedef GrGpuRef INHERITED; |
164 }; | 246 }; |
165 | 247 |
166 #endif | 248 #endif |
OLD | NEW |