| 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 GrCacheable_DEFINED | |
| 9 #define GrCacheable_DEFINED | |
| 10 | |
| 11 #include "SkRefCnt.h" | |
| 12 | |
| 13 class GrResourceCacheEntry; | |
| 14 | |
| 15 /** | |
| 16 * Base class for objects that can be kept in the GrResourceCache. | |
| 17 */ | |
| 18 class GrCacheable : public SkNoncopyable { | |
| 19 public: | |
| 20 SK_DECLARE_INST_COUNT_ROOT(GrCacheable) | |
| 21 | |
| 22 // These method signatures are written to mirror SkRefCnt. However, we don't
require | |
| 23 // thread safety as GrCacheable objects are not intended to cross thread bou
ndaries. | |
| 24 // internal_dispose() exists because of GrTexture's reliance on it. It will
be removed | |
| 25 // soon. | |
| 26 void ref() const { ++fRefCnt; } | |
| 27 void unref() const { --fRefCnt; if (0 == fRefCnt) { this->internal_dispose()
; } } | |
| 28 virtual void internal_dispose() const { SkDELETE(this); } | |
| 29 bool unique() const { return 1 == fRefCnt; } | |
| 30 #ifdef SK_DEBUG | |
| 31 void validate() const { | |
| 32 SkASSERT(fRefCnt > 0); | |
| 33 } | |
| 34 #endif | |
| 35 | |
| 36 virtual ~GrCacheable() { SkASSERT(0 == fRefCnt); } | |
| 37 | |
| 38 /** | |
| 39 * Retrieves the amount of GPU memory used by this resource in bytes. It is | |
| 40 * approximate since we aren't aware of additional padding or copies made | |
| 41 * by the driver. | |
| 42 * | |
| 43 * @return the amount of GPU memory used in bytes | |
| 44 */ | |
| 45 virtual size_t gpuMemorySize() const = 0; | |
| 46 | |
| 47 /** | |
| 48 * Checks whether the GPU memory allocated to this resource is still in effe
ct. | |
| 49 * It can become invalid if its context is destroyed or lost, in which case
it | |
| 50 * should no longer count against the GrResourceCache budget. | |
| 51 * | |
| 52 * @return true if this resource is still holding GPU memory | |
| 53 * false otherwise. | |
| 54 */ | |
| 55 virtual bool isValidOnGpu() const = 0; | |
| 56 | |
| 57 void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEn
try; } | |
| 58 GrResourceCacheEntry* getCacheEntry() { return fCacheEntry; } | |
| 59 | |
| 60 /** | |
| 61 * Gets an id that is unique for this GrCacheable object. It is static in th
at it does | |
| 62 * not change when the content of the GrCacheable object changes. This will
never return | |
| 63 * 0. | |
| 64 */ | |
| 65 uint32_t getUniqueID() const { return fUniqueID; } | |
| 66 | |
| 67 protected: | |
| 68 GrCacheable() | |
| 69 : fRefCnt(1) | |
| 70 , fCacheEntry(NULL) | |
| 71 , fUniqueID(CreateUniqueID()) {} | |
| 72 | |
| 73 bool isInCache() const { return NULL != fCacheEntry; } | |
| 74 | |
| 75 /** | |
| 76 * This entry point should be called whenever gpuMemorySize() begins | |
| 77 * reporting a different size. If the object is in the cache, it will call | |
| 78 * gpuMemorySize() immediately and pass the new size on to the resource | |
| 79 * cache. | |
| 80 */ | |
| 81 void didChangeGpuMemorySize() const; | |
| 82 | |
| 83 private: | |
| 84 static uint32_t CreateUniqueID(); | |
| 85 | |
| 86 mutable int32_t fRefCnt; | |
| 87 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache | |
| 88 const uint32_t fUniqueID; | |
| 89 | |
| 90 typedef SkNoncopyable INHERITED; | |
| 91 }; | |
| 92 | |
| 93 #endif | |
| OLD | NEW |