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