commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 1 | /* |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 2 | * Copyright 2014 Google Inc. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 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 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 8 | #ifndef GrGpuResource_DEFINED |
| 9 | #define GrGpuResource_DEFINED |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 10 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 11 | #include "SkInstCnt.h" |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 12 | #include "SkTInternalLList.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 13 | #include "GrResourceKey.h" |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 14 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 15 | class GrResourceCacheEntry; |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 16 | class GrResourceCache2; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 17 | class GrGpu; |
| 18 | class GrContext; |
| 19 | |
| 20 | /** |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 21 | * Base class for objects that can be kept in the GrResourceCache. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 22 | */ |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 23 | class GrGpuResource : public SkNoncopyable { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 24 | public: |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 25 | SK_DECLARE_INST_COUNT_ROOT(GrGpuResource) |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 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 boundaries. |
| 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 |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Frees the object in the underlying 3D API. It must be safe to call this |
| 43 | * when the object has been previously abandoned. |
| 44 | */ |
| 45 | void release(); |
| 46 | |
| 47 | /** |
| 48 | * 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. |
| 50 | */ |
| 51 | void abandon(); |
| 52 | |
| 53 | /** |
| 54 | * Tests whether a object has been abandoned or released. All objects will |
| 55 | * be in this state after their creating GrContext is destroyed or has |
| 56 | * contextLost called. It's up to the client to test wasDestroyed() before |
| 57 | * attempting to use an object if it holds refs on objects across |
| 58 | * ~GrContext, freeResources with the force flag, or contextLost. |
| 59 | * |
| 60 | * @return true if the object has been released or abandoned, |
| 61 | * false otherwise. |
| 62 | */ |
| 63 | bool wasDestroyed() const { return NULL == fGpu; } |
| 64 | |
| 65 | /** |
| 66 | * Retrieves the context that owns the object. Note that it is possible for |
| 67 | * this to return NULL. When objects have been release()ed or abandon()ed |
| 68 | * they no longer have an owning context. Destroying a GrContext |
| 69 | * automatically releases all its resources. |
| 70 | */ |
| 71 | const GrContext* getContext() const; |
| 72 | GrContext* getContext(); |
| 73 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Retrieves the amount of GPU memory used by this resource in bytes. It is |
| 76 | * approximate since we aren't aware of additional padding or copies made |
| 77 | * by the driver. |
| 78 | * |
| 79 | * @return the amount of GPU memory used in bytes |
| 80 | */ |
| 81 | virtual size_t gpuMemorySize() const = 0; |
| 82 | |
| 83 | void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEntry; } |
| 84 | GrResourceCacheEntry* getCacheEntry() { return fCacheEntry; } |
| 85 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 86 | /** |
| 87 | * If this resource can be used as a scratch resource this returns a valid |
| 88 | * scratch key. Otherwise it returns a key for which isNullScratch is true. |
| 89 | */ |
| 90 | const GrResourceKey& getScratchKey() const { return fScratchKey; } |
| 91 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Gets an id that is unique for this GrCacheable object. It is static in that it does |
| 94 | * not change when the content of the GrCacheable object changes. This will never return |
| 95 | * 0. |
| 96 | */ |
| 97 | uint32_t getUniqueID() const { return fUniqueID; } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 98 | |
| 99 | protected: |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 100 | // This must be called by every GrGpuObject. It should be called once the object is fully |
| 101 | // initialized (i.e. not in a base class constructor). |
| 102 | void registerWithCache(); |
| 103 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 104 | GrGpuResource(GrGpu*, bool isWrapped); |
| 105 | virtual ~GrGpuResource(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 106 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 107 | bool isInCache() const { return NULL != fCacheEntry; } |
| 108 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 109 | GrGpu* getGpu() const { return fGpu; } |
| 110 | |
| 111 | // Derived classes should always call their parent class' onRelease |
| 112 | // and onAbandon methods in their overrides. |
| 113 | virtual void onRelease() {}; |
| 114 | virtual void onAbandon() {}; |
| 115 | |
| 116 | bool isWrapped() const { return kWrapped_FlagBit & fFlags; } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 117 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 118 | /** |
| 119 | * This entry point should be called whenever gpuMemorySize() begins |
| 120 | * reporting a different size. If the object is in the cache, it will call |
| 121 | * gpuMemorySize() immediately and pass the new size on to the resource |
| 122 | * cache. |
| 123 | */ |
| 124 | void didChangeGpuMemorySize() const; |
| 125 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Optionally called by the GrGpuResource subclass if the resource can be used as scratch. |
| 128 | * By default resources are not usable as scratch. This should only be called once. |
| 129 | **/ |
| 130 | void setScratchKey(const GrResourceKey& scratchKey); |
| 131 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 132 | private: |
| 133 | #ifdef SK_DEBUG |
| 134 | friend class GrGpu; // for assert in GrGpu to access getGpu |
| 135 | #endif |
| 136 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 137 | static uint32_t CreateUniqueID(); |
| 138 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 139 | // We're in an internal doubly linked list owned by GrResourceCache2 |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 140 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 141 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 142 | // This is not ref'ed but abandon() or release() will be called before the GrGpu object |
| 143 | // is destroyed. Those calls set will this to NULL. |
| 144 | GrGpu* fGpu; |
| 145 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 146 | enum Flags { |
| 147 | /** |
| 148 | * 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 |
| 150 | * free it). |
| 151 | */ |
| 152 | kWrapped_FlagBit = 0x1, |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 153 | }; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 154 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 155 | uint32_t fFlags; |
| 156 | |
| 157 | mutable int32_t fRefCnt; |
| 158 | GrResourceCacheEntry* fCacheEntry; // NULL if not in cache |
| 159 | const uint32_t fUniqueID; |
| 160 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 161 | GrResourceKey fScratchKey; |
| 162 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 163 | typedef SkNoncopyable INHERITED; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | #endif |