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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrGpuResourcePriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 12 | #include "src/gpu/GrSurface.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrSurfaceProxy.h" |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 14 | |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 15 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/core/SkTDynamicHash.h" |
| 17 | #include "src/core/SkTMultiMap.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 18 | |
| 19 | class GrResourceProvider; |
| 20 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 21 | // Print out explicit allocation information |
| 22 | #define GR_ALLOCATION_SPEW 0 |
| 23 | |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 24 | // Print out information about interval creation |
| 25 | #define GR_TRACK_INTERVAL_CREATION 0 |
| 26 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 27 | /* |
| 28 | * The ResourceAllocator explicitly distributes GPU resources at flush time. It operates by |
| 29 | * being given the usage intervals of the various proxies. It keeps these intervals in a singly |
| 30 | * linked list sorted by increasing start index. (It also maintains a hash table from proxyID |
| 31 | * to interval to find proxy reuse). When it comes time to allocate the resources it |
| 32 | * traverses the sorted list and: |
| 33 | * removes intervals from the active list that have completed (returning their GrSurfaces |
| 34 | * to the free pool) |
| 35 | |
| 36 | * allocates a new resource (preferably from the free pool) for the new interval |
| 37 | * adds the new interval to the active list (that is sorted by increasing end index) |
| 38 | * |
| 39 | * Note: the op indices (used in the usage intervals) come from the order of the ops in |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 40 | * their opsTasks after the opsTask DAG has been linearized. |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 41 | * |
| 42 | ************************************************************************************************* |
| 43 | * How does instantiation failure handling work when explicitly allocating? |
| 44 | * |
| 45 | * In the gather usage intervals pass all the GrSurfaceProxies used in the flush should be |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 46 | * gathered (i.e., in GrOpsTask::gatherProxyIntervals). |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 47 | * |
| 48 | * The allocator will churn through this list but could fail anywhere. |
| 49 | * |
| 50 | * Allocation failure handling occurs at two levels: |
| 51 | * |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 52 | * 1) If the GrSurface backing an opsTask fails to allocate then the entire opsTask is dropped. |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 53 | * |
| 54 | * 2) If an individual GrSurfaceProxy fails to allocate then any ops that use it are dropped |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 55 | * (via GrOpsTask::purgeOpsWithUninstantiatedProxies) |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 56 | * |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 57 | * The pass to determine which ops to drop is a bit laborious so we only check the opsTasks and |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 58 | * individual ops when something goes wrong in allocation (i.e., when the return code from |
| 59 | * GrResourceAllocator::assign is bad) |
| 60 | * |
| 61 | * All together this means we should never attempt to draw an op which is missing some |
| 62 | * required GrSurface. |
| 63 | * |
| 64 | * One wrinkle in this plan is that promise images are fulfilled during the gather interval pass. |
| 65 | * If any of the promise images fail at this stage then the allocator is set into an error |
| 66 | * state and all allocations are then scanned for failures during the main allocation pass. |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 67 | */ |
| 68 | class GrResourceAllocator { |
| 69 | public: |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 70 | GrResourceAllocator(GrResourceProvider* resourceProvider SkDEBUGCODE(, int numOpsTasks)) |
Adlai Holler | c616e1c | 2021-02-11 15:18:17 -0500 | [diff] [blame] | 71 | : fResourceProvider(resourceProvider) {} |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 72 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 73 | ~GrResourceAllocator(); |
| 74 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 75 | unsigned int curOp() const { return fNumOps; } |
| 76 | void incOps() { fNumOps++; } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 77 | |
Adlai Holler | 7f7a5df | 2021-02-09 17:41:10 +0000 | [diff] [blame] | 78 | /** Indicates whether a given call to addInterval represents an actual usage of the |
| 79 | * provided proxy. This is mainly here to accommodate deferred proxies attached to opsTasks. |
| 80 | * In that case we need to create an extra long interval for them (due to the upload) but |
| 81 | * don't want to count that usage/reference towards the proxy's recyclability. |
| 82 | */ |
| 83 | enum class ActualUse : bool { |
| 84 | kNo = false, |
| 85 | kYes = true |
| 86 | }; |
| 87 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 88 | // 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] | 89 | // If an existing interval already exists it will be expanded to include the new range. |
Adlai Holler | 7f7a5df | 2021-02-09 17:41:10 +0000 | [diff] [blame] | 90 | void addInterval(GrSurfaceProxy*, unsigned int start, unsigned int end, ActualUse actualUse |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 91 | SkDEBUGCODE(, bool isDirectDstRead = false)); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 92 | |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame^] | 93 | // Assign resources to all proxies. Returns whether the assignment was successful. |
| 94 | bool assign(); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 95 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 96 | #if GR_ALLOCATION_SPEW |
| 97 | void dumpIntervals(); |
| 98 | #endif |
| 99 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 100 | private: |
| 101 | class Interval; |
| 102 | |
| 103 | // Remove dead intervals from the active list |
| 104 | void expire(unsigned int curIndex); |
| 105 | |
| 106 | // These two methods wrap the interactions with the free pool |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 107 | void recycleSurface(sk_sp<GrSurface> surface); |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 108 | sk_sp<GrSurface> findSurfaceFor(const GrSurfaceProxy* proxy); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 109 | |
Adlai Holler | c616e1c | 2021-02-11 15:18:17 -0500 | [diff] [blame] | 110 | void determineRecyclability(); |
| 111 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 112 | struct FreePoolTraits { |
| 113 | static const GrScratchKey& GetKey(const GrSurface& s) { |
| 114 | return s.resourcePriv().getScratchKey(); |
| 115 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 116 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 117 | static uint32_t Hash(const GrScratchKey& key) { return key.hash(); } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 118 | static void OnFree(GrSurface* s) { s->unref(); } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 119 | }; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 120 | typedef SkTMultiMap<GrSurface, GrScratchKey, FreePoolTraits> FreePoolMultiMap; |
| 121 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 122 | typedef SkTDynamicHash<Interval, unsigned int> IntvlHash; |
| 123 | |
| 124 | class Interval { |
| 125 | public: |
| 126 | Interval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end) |
| 127 | : fProxy(proxy) |
| 128 | , fProxyID(proxy->uniqueID().asUInt()) |
| 129 | , fStart(start) |
| 130 | , fEnd(end) |
| 131 | , fNext(nullptr) { |
| 132 | SkASSERT(proxy); |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 133 | #if GR_TRACK_INTERVAL_CREATION |
| 134 | fUniqueID = CreateUniqueID(); |
Robert Phillips | 047d5bb | 2021-01-08 13:39:19 -0500 | [diff] [blame] | 135 | SkString proxyStr = proxy->dump(); |
| 136 | SkDebugf("New intvl %d: %s [%d, %d]\n", fUniqueID, proxyStr.c_str(), start, end); |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 137 | #endif |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Robert Phillips | 3966738 | 2019-04-17 16:03:30 -0400 | [diff] [blame] | 140 | // Used when recycling an interval |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 141 | void resetTo(GrSurfaceProxy* proxy, unsigned int start, unsigned int end) { |
| 142 | SkASSERT(proxy); |
Robert Phillips | 3966738 | 2019-04-17 16:03:30 -0400 | [diff] [blame] | 143 | SkASSERT(!fProxy && !fNext); |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 144 | |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 145 | fUses = 0; |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 146 | fProxy = proxy; |
| 147 | fProxyID = proxy->uniqueID().asUInt(); |
| 148 | fStart = start; |
| 149 | fEnd = end; |
| 150 | fNext = nullptr; |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 151 | #if GR_TRACK_INTERVAL_CREATION |
| 152 | fUniqueID = CreateUniqueID(); |
Robert Phillips | 047d5bb | 2021-01-08 13:39:19 -0500 | [diff] [blame] | 153 | SkString proxyStr = proxy->dump(); |
| 154 | SkDebugf("New intvl %d: %s [ %d, %d ]\n", fUniqueID, proxyStr.c_str(), start, end); |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 155 | #endif |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 158 | ~Interval() { |
| 159 | SkASSERT(!fAssignedSurface); |
| 160 | } |
| 161 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 162 | const GrSurfaceProxy* proxy() const { return fProxy; } |
| 163 | GrSurfaceProxy* proxy() { return fProxy; } |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 164 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 165 | unsigned int start() const { return fStart; } |
| 166 | unsigned int end() const { return fEnd; } |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 167 | |
| 168 | void setNext(Interval* next) { fNext = next; } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 169 | const Interval* next() const { return fNext; } |
| 170 | Interval* next() { return fNext; } |
| 171 | |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 172 | void markAsRecyclable() { fIsRecyclable = true;} |
| 173 | bool isRecyclable() const { return fIsRecyclable; } |
| 174 | |
| 175 | void addUse() { fUses++; } |
| 176 | int uses() { return fUses; } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 177 | |
| 178 | void extendEnd(unsigned int newEnd) { |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 179 | if (newEnd > fEnd) { |
| 180 | fEnd = newEnd; |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 181 | #if GR_TRACK_INTERVAL_CREATION |
| 182 | SkDebugf("intvl %d: extending from %d to %d\n", fUniqueID, fEnd, newEnd); |
| 183 | #endif |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 184 | } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 187 | void assign(sk_sp<GrSurface>); |
Ben Wagner | 5d1adbf | 2018-05-28 13:35:39 -0400 | [diff] [blame] | 188 | bool wasAssignedSurface() const { return fAssignedSurface != nullptr; } |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 189 | sk_sp<GrSurface> detachSurface() { return std::move(fAssignedSurface); } |
| 190 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 191 | // for SkTDynamicHash |
| 192 | static const uint32_t& GetKey(const Interval& intvl) { |
| 193 | return intvl.fProxyID; |
| 194 | } |
| 195 | static uint32_t Hash(const uint32_t& key) { return key; } |
| 196 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 197 | private: |
Adlai Holler | c616e1c | 2021-02-11 15:18:17 -0500 | [diff] [blame] | 198 | // TODO: Do we really need this variable? |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 199 | sk_sp<GrSurface> fAssignedSurface; |
| 200 | GrSurfaceProxy* fProxy; |
| 201 | uint32_t fProxyID; // This is here b.c. DynamicHash requires a ref to the key |
| 202 | unsigned int fStart; |
| 203 | unsigned int fEnd; |
| 204 | Interval* fNext; |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 205 | unsigned int fUses = 0; |
| 206 | bool fIsRecyclable = false; |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 207 | |
| 208 | #if GR_TRACK_INTERVAL_CREATION |
| 209 | uint32_t fUniqueID; |
| 210 | |
| 211 | uint32_t CreateUniqueID(); |
| 212 | #endif |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | class IntervalList { |
| 216 | public: |
| 217 | IntervalList() = default; |
| 218 | ~IntervalList() { |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 219 | // The only time we delete an IntervalList is in the GrResourceAllocator dtor. |
| 220 | // 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] | 221 | } |
| 222 | |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 223 | bool empty() const { |
| 224 | SkASSERT(SkToBool(fHead) == SkToBool(fTail)); |
| 225 | return !SkToBool(fHead); |
| 226 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 227 | const Interval* peekHead() const { return fHead; } |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 228 | Interval* peekHead() { return fHead; } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 229 | Interval* popHead(); |
| 230 | void insertByIncreasingStart(Interval*); |
| 231 | void insertByIncreasingEnd(Interval*); |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 232 | Interval* detachAll(); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 233 | |
| 234 | private: |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 235 | SkDEBUGCODE(void validate() const;) |
| 236 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 237 | Interval* fHead = nullptr; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 238 | Interval* fTail = nullptr; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 239 | }; |
| 240 | |
Brian Salomon | 309b053 | 2018-10-10 17:22:00 -0400 | [diff] [blame] | 241 | // Compositing use cases can create > 80 intervals. |
| 242 | static const int kInitialArenaSize = 128 * sizeof(Interval); |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 243 | |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 244 | GrResourceProvider* fResourceProvider; |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 245 | FreePoolMultiMap fFreePool; // Recently created/used GrSurfaces |
| 246 | IntvlHash fIntvlHash; // All the intervals, hashed by proxyID |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 247 | |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 248 | IntervalList fIntvlList; // All the intervals sorted by increasing start |
| 249 | IntervalList fActiveIntvls; // List of live intervals during assignment |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 250 | // (sorted by increasing end) |
| 251 | unsigned int fNumOps = 0; |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 252 | |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 253 | SkDEBUGCODE(bool fAssigned = false;) |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 254 | |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame^] | 255 | SkSTArenaAlloc<kInitialArenaSize> fIntervalAllocator; |
| 256 | Interval* fFreeIntervalList = nullptr; |
| 257 | bool fFailedInstantiation = false; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | #endif // GrResourceAllocator_DEFINED |