Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 GrResourceAllocator_DEFINED |
| 9 | #define GrResourceAllocator_DEFINED |
| 10 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 11 | #include "GrGpuResourcePriv.h" |
| 12 | #include "GrSurface.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 13 | #include "GrSurfaceProxy.h" |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 14 | |
| 15 | #include "SkArenaAlloc.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 16 | #include "SkTDynamicHash.h" |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 17 | #include "SkTMultiMap.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 18 | |
| 19 | class GrResourceProvider; |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 20 | class GrUninstantiateProxyTracker; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 21 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 22 | // Print out explicit allocation information |
| 23 | #define GR_ALLOCATION_SPEW 0 |
| 24 | |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 25 | // Print out information about interval creation |
| 26 | #define GR_TRACK_INTERVAL_CREATION 0 |
| 27 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 28 | /* |
| 29 | * The ResourceAllocator explicitly distributes GPU resources at flush time. It operates by |
| 30 | * being given the usage intervals of the various proxies. It keeps these intervals in a singly |
| 31 | * linked list sorted by increasing start index. (It also maintains a hash table from proxyID |
| 32 | * to interval to find proxy reuse). When it comes time to allocate the resources it |
| 33 | * traverses the sorted list and: |
| 34 | * removes intervals from the active list that have completed (returning their GrSurfaces |
| 35 | * to the free pool) |
| 36 | |
| 37 | * allocates a new resource (preferably from the free pool) for the new interval |
| 38 | * adds the new interval to the active list (that is sorted by increasing end index) |
| 39 | * |
| 40 | * Note: the op indices (used in the usage intervals) come from the order of the ops in |
| 41 | * their opLists after the opList DAG has been linearized. |
| 42 | */ |
| 43 | class GrResourceAllocator { |
| 44 | public: |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 45 | GrResourceAllocator(GrResourceProvider* resourceProvider, GrUninstantiateProxyTracker* tracker) |
| 46 | : fResourceProvider(resourceProvider), fUninstantiateTracker(tracker) {} |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 47 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 48 | ~GrResourceAllocator(); |
| 49 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 50 | unsigned int curOp() const { return fNumOps; } |
| 51 | void incOps() { fNumOps++; } |
| 52 | unsigned int numOps() const { return fNumOps; } |
| 53 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 54 | // Add a usage interval from 'start' to 'end' inclusive. This is usually used for renderTargets. |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 55 | // If an existing interval already exists it will be expanded to include the new range. |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 56 | void addInterval(GrSurfaceProxy*, unsigned int start, unsigned int end |
| 57 | SkDEBUGCODE(, bool isDirectDstRead = false)); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 58 | |
| 59 | // Add an interval that spans just the current op. Usually this is for texture uses. |
| 60 | // If an existing interval already exists it will be expanded to include the new operation. |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 61 | void addInterval(GrSurfaceProxy* proxy |
| 62 | SkDEBUGCODE(, bool isDirectDstRead = false)) { |
| 63 | this->addInterval(proxy, fNumOps, fNumOps SkDEBUGCODE(, isDirectDstRead)); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 66 | enum class AssignError { |
| 67 | kNoError, |
| 68 | kFailedProxyInstantiation |
| 69 | }; |
| 70 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 71 | // Returns true when the opLists from 'startIndex' to 'stopIndex' should be executed; |
| 72 | // false when nothing remains to be executed. |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 73 | // If any proxy fails to instantiate, the AssignError will be set to kFailedProxyInstantiation. |
| 74 | // If this happens, the caller should remove all ops which reference an uninstantiated proxy. |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 75 | // This is used to execute a portion of the queued opLists in order to reduce the total |
| 76 | // amount of GPU resources required. |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 77 | bool assign(int* startIndex, int* stopIndex, AssignError* outError); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 78 | |
| 79 | void markEndOfOpList(int opListIndex); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 80 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 81 | #if GR_ALLOCATION_SPEW |
| 82 | void dumpIntervals(); |
| 83 | #endif |
| 84 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 85 | private: |
| 86 | class Interval; |
| 87 | |
| 88 | // Remove dead intervals from the active list |
| 89 | void expire(unsigned int curIndex); |
| 90 | |
| 91 | // These two methods wrap the interactions with the free pool |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 92 | void recycleSurface(sk_sp<GrSurface> surface); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 93 | sk_sp<GrSurface> findSurfaceFor(const GrSurfaceProxy* proxy, bool needsStencil); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 94 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 95 | struct FreePoolTraits { |
| 96 | static const GrScratchKey& GetKey(const GrSurface& s) { |
| 97 | return s.resourcePriv().getScratchKey(); |
| 98 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 99 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 100 | static uint32_t Hash(const GrScratchKey& key) { return key.hash(); } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 101 | static void OnFree(GrSurface* s) { s->unref(); } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 102 | }; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 103 | typedef SkTMultiMap<GrSurface, GrScratchKey, FreePoolTraits> FreePoolMultiMap; |
| 104 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 105 | typedef SkTDynamicHash<Interval, unsigned int> IntvlHash; |
| 106 | |
| 107 | class Interval { |
| 108 | public: |
| 109 | Interval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end) |
| 110 | : fProxy(proxy) |
| 111 | , fProxyID(proxy->uniqueID().asUInt()) |
| 112 | , fStart(start) |
| 113 | , fEnd(end) |
| 114 | , fNext(nullptr) { |
| 115 | SkASSERT(proxy); |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 116 | #if GR_TRACK_INTERVAL_CREATION |
| 117 | fUniqueID = CreateUniqueID(); |
| 118 | SkDebugf("New intvl %d: proxyID: %d [ %d, %d ]\n", |
| 119 | fUniqueID, proxy->uniqueID().asUInt(), start, end); |
| 120 | #endif |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 123 | void resetTo(GrSurfaceProxy* proxy, unsigned int start, unsigned int end) { |
| 124 | SkASSERT(proxy); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 125 | SkASSERT(!fNext); |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 126 | |
| 127 | fProxy = proxy; |
| 128 | fProxyID = proxy->uniqueID().asUInt(); |
| 129 | fStart = start; |
| 130 | fEnd = end; |
| 131 | fNext = nullptr; |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 132 | #if GR_TRACK_INTERVAL_CREATION |
| 133 | fUniqueID = CreateUniqueID(); |
| 134 | SkDebugf("New intvl %d: proxyID: %d [ %d, %d ]\n", |
| 135 | fUniqueID, proxy->uniqueID().asUInt(), start, end); |
| 136 | #endif |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 139 | ~Interval() { |
| 140 | SkASSERT(!fAssignedSurface); |
| 141 | } |
| 142 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 143 | const GrSurfaceProxy* proxy() const { return fProxy; } |
| 144 | GrSurfaceProxy* proxy() { return fProxy; } |
| 145 | unsigned int start() const { return fStart; } |
| 146 | unsigned int end() const { return fEnd; } |
| 147 | const Interval* next() const { return fNext; } |
| 148 | Interval* next() { return fNext; } |
| 149 | |
| 150 | void setNext(Interval* next) { fNext = next; } |
| 151 | |
| 152 | void extendEnd(unsigned int newEnd) { |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 153 | if (newEnd > fEnd) { |
| 154 | fEnd = newEnd; |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 155 | #if GR_TRACK_INTERVAL_CREATION |
| 156 | SkDebugf("intvl %d: extending from %d to %d\n", fUniqueID, fEnd, newEnd); |
| 157 | #endif |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 158 | } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 161 | void assign(sk_sp<GrSurface>); |
Ben Wagner | 5d1adbf | 2018-05-28 13:35:39 -0400 | [diff] [blame] | 162 | bool wasAssignedSurface() const { return fAssignedSurface != nullptr; } |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 163 | sk_sp<GrSurface> detachSurface() { return std::move(fAssignedSurface); } |
| 164 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 165 | // for SkTDynamicHash |
| 166 | static const uint32_t& GetKey(const Interval& intvl) { |
| 167 | return intvl.fProxyID; |
| 168 | } |
| 169 | static uint32_t Hash(const uint32_t& key) { return key; } |
| 170 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 171 | private: |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 172 | sk_sp<GrSurface> fAssignedSurface; |
| 173 | GrSurfaceProxy* fProxy; |
| 174 | uint32_t fProxyID; // This is here b.c. DynamicHash requires a ref to the key |
| 175 | unsigned int fStart; |
| 176 | unsigned int fEnd; |
| 177 | Interval* fNext; |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 178 | |
| 179 | #if GR_TRACK_INTERVAL_CREATION |
| 180 | uint32_t fUniqueID; |
| 181 | |
| 182 | uint32_t CreateUniqueID(); |
| 183 | #endif |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | class IntervalList { |
| 187 | public: |
| 188 | IntervalList() = default; |
| 189 | ~IntervalList() { |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 190 | // The only time we delete an IntervalList is in the GrResourceAllocator dtor. |
| 191 | // Since the arena allocator will clean up for us we don't bother here. |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 194 | bool empty() const { |
| 195 | SkASSERT(SkToBool(fHead) == SkToBool(fTail)); |
| 196 | return !SkToBool(fHead); |
| 197 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 198 | const Interval* peekHead() const { return fHead; } |
| 199 | Interval* popHead(); |
| 200 | void insertByIncreasingStart(Interval*); |
| 201 | void insertByIncreasingEnd(Interval*); |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 202 | Interval* detachAll(); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 203 | |
| 204 | private: |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 205 | SkDEBUGCODE(void validate() const;) |
| 206 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 207 | Interval* fHead = nullptr; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 208 | Interval* fTail = nullptr; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 209 | }; |
| 210 | |
Brian Salomon | 309b053 | 2018-10-10 17:22:00 -0400 | [diff] [blame] | 211 | // Compositing use cases can create > 80 intervals. |
| 212 | static const int kInitialArenaSize = 128 * sizeof(Interval); |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 213 | |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 214 | GrResourceProvider* fResourceProvider; |
| 215 | GrUninstantiateProxyTracker* fUninstantiateTracker; |
| 216 | FreePoolMultiMap fFreePool; // Recently created/used GrSurfaces |
| 217 | IntvlHash fIntvlHash; // All the intervals, hashed by proxyID |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 218 | |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 219 | IntervalList fIntvlList; // All the intervals sorted by increasing start |
| 220 | IntervalList fActiveIntvls; // List of live intervals during assignment |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 221 | // (sorted by increasing end) |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 222 | unsigned int fNumOps = 1; // op # 0 is reserved for uploads at the start |
Robert Phillips | 51b20f2 | 2017-12-01 15:32:35 -0500 | [diff] [blame] | 223 | // of a flush |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 224 | SkTArray<unsigned int> fEndOfOpListOpIndices; |
| 225 | int fCurOpListIndex = 0; |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 226 | |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 227 | SkDEBUGCODE(bool fAssigned = false;) |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 228 | |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 229 | char fStorage[kInitialArenaSize]; |
| 230 | SkArenaAlloc fIntervalAllocator{fStorage, kInitialArenaSize, kInitialArenaSize}; |
| 231 | Interval* fFreeIntervalList = nullptr; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | #endif // GrResourceAllocator_DEFINED |