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; |
| 20 | |
| 21 | /* |
| 22 | * The ResourceAllocator explicitly distributes GPU resources at flush time. It operates by |
| 23 | * being given the usage intervals of the various proxies. It keeps these intervals in a singly |
| 24 | * linked list sorted by increasing start index. (It also maintains a hash table from proxyID |
| 25 | * to interval to find proxy reuse). When it comes time to allocate the resources it |
| 26 | * traverses the sorted list and: |
| 27 | * removes intervals from the active list that have completed (returning their GrSurfaces |
| 28 | * to the free pool) |
| 29 | |
| 30 | * allocates a new resource (preferably from the free pool) for the new interval |
| 31 | * adds the new interval to the active list (that is sorted by increasing end index) |
| 32 | * |
| 33 | * Note: the op indices (used in the usage intervals) come from the order of the ops in |
| 34 | * their opLists after the opList DAG has been linearized. |
| 35 | */ |
| 36 | class GrResourceAllocator { |
| 37 | public: |
| 38 | GrResourceAllocator(GrResourceProvider* resourceProvider) |
| 39 | : fResourceProvider(resourceProvider) { |
| 40 | } |
| 41 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 42 | ~GrResourceAllocator(); |
| 43 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 44 | unsigned int curOp() const { return fNumOps; } |
| 45 | void incOps() { fNumOps++; } |
| 46 | unsigned int numOps() const { return fNumOps; } |
| 47 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 48 | // 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] | 49 | // 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] | 50 | void addInterval(GrSurfaceProxy*, unsigned int start, unsigned int end |
| 51 | SkDEBUGCODE(, bool isDirectDstRead = false)); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 52 | |
| 53 | // Add an interval that spans just the current op. Usually this is for texture uses. |
| 54 | // 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] | 55 | void addInterval(GrSurfaceProxy* proxy |
| 56 | SkDEBUGCODE(, bool isDirectDstRead = false)) { |
| 57 | this->addInterval(proxy, fNumOps, fNumOps SkDEBUGCODE(, isDirectDstRead)); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 60 | // Returns true when the opLists from 'startIndex' to 'stopIndex' should be executed; |
| 61 | // false when nothing remains to be executed. |
| 62 | // This is used to execute a portion of the queued opLists in order to reduce the total |
| 63 | // amount of GPU resources required. |
| 64 | bool assign(int* startIndex, int* stopIndex); |
| 65 | |
| 66 | void markEndOfOpList(int opListIndex); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | class Interval; |
| 70 | |
| 71 | // Remove dead intervals from the active list |
| 72 | void expire(unsigned int curIndex); |
| 73 | |
| 74 | // These two methods wrap the interactions with the free pool |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 75 | void freeUpSurface(sk_sp<GrSurface> surface); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 76 | sk_sp<GrSurface> findSurfaceFor(const GrSurfaceProxy* proxy, bool needsStencil); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 77 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 78 | struct FreePoolTraits { |
| 79 | static const GrScratchKey& GetKey(const GrSurface& s) { |
| 80 | return s.resourcePriv().getScratchKey(); |
| 81 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 82 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 83 | static uint32_t Hash(const GrScratchKey& key) { return key.hash(); } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 84 | static void OnFree(GrSurface* s) { s->unref(); } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 85 | }; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 86 | typedef SkTMultiMap<GrSurface, GrScratchKey, FreePoolTraits> FreePoolMultiMap; |
| 87 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 88 | typedef SkTDynamicHash<Interval, unsigned int> IntvlHash; |
| 89 | |
| 90 | class Interval { |
| 91 | public: |
| 92 | Interval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end) |
| 93 | : fProxy(proxy) |
| 94 | , fProxyID(proxy->uniqueID().asUInt()) |
| 95 | , fStart(start) |
| 96 | , fEnd(end) |
| 97 | , fNext(nullptr) { |
| 98 | SkASSERT(proxy); |
| 99 | } |
| 100 | |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 101 | void resetTo(GrSurfaceProxy* proxy, unsigned int start, unsigned int end) { |
| 102 | SkASSERT(proxy); |
| 103 | |
| 104 | fProxy = proxy; |
| 105 | fProxyID = proxy->uniqueID().asUInt(); |
| 106 | fStart = start; |
| 107 | fEnd = end; |
| 108 | fNext = nullptr; |
| 109 | } |
| 110 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 111 | ~Interval() { |
| 112 | SkASSERT(!fAssignedSurface); |
| 113 | } |
| 114 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 115 | const GrSurfaceProxy* proxy() const { return fProxy; } |
| 116 | GrSurfaceProxy* proxy() { return fProxy; } |
| 117 | unsigned int start() const { return fStart; } |
| 118 | unsigned int end() const { return fEnd; } |
| 119 | const Interval* next() const { return fNext; } |
| 120 | Interval* next() { return fNext; } |
| 121 | |
| 122 | void setNext(Interval* next) { fNext = next; } |
| 123 | |
| 124 | void extendEnd(unsigned int newEnd) { |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 125 | if (newEnd > fEnd) { |
| 126 | fEnd = newEnd; |
| 127 | } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 130 | void assign(sk_sp<GrSurface>); |
| 131 | bool wasAssignedSurface() const { return fAssignedSurface; } |
| 132 | sk_sp<GrSurface> detachSurface() { return std::move(fAssignedSurface); } |
| 133 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 134 | // for SkTDynamicHash |
| 135 | static const uint32_t& GetKey(const Interval& intvl) { |
| 136 | return intvl.fProxyID; |
| 137 | } |
| 138 | static uint32_t Hash(const uint32_t& key) { return key; } |
| 139 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 140 | private: |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 141 | sk_sp<GrSurface> fAssignedSurface; |
| 142 | GrSurfaceProxy* fProxy; |
| 143 | uint32_t fProxyID; // This is here b.c. DynamicHash requires a ref to the key |
| 144 | unsigned int fStart; |
| 145 | unsigned int fEnd; |
| 146 | Interval* fNext; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | class IntervalList { |
| 150 | public: |
| 151 | IntervalList() = default; |
| 152 | ~IntervalList() { |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 153 | // The only time we delete an IntervalList is in the GrResourceAllocator dtor. |
| 154 | // 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] | 155 | } |
| 156 | |
| 157 | bool empty() const { return !SkToBool(fHead); } |
| 158 | const Interval* peekHead() const { return fHead; } |
| 159 | Interval* popHead(); |
| 160 | void insertByIncreasingStart(Interval*); |
| 161 | void insertByIncreasingEnd(Interval*); |
| 162 | |
| 163 | private: |
| 164 | Interval* fHead = nullptr; |
| 165 | }; |
| 166 | |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 167 | // Gathered statistics indicate that 99% of flushes will be covered by <= 12 Intervals |
| 168 | static const int kInitialArenaSize = 12 * sizeof(Interval); |
| 169 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 170 | GrResourceProvider* fResourceProvider; |
| 171 | FreePoolMultiMap fFreePool; // Recently created/used GrSurfaces |
| 172 | IntvlHash fIntvlHash; // All the intervals, hashed by proxyID |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 173 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 174 | IntervalList fIntvlList; // All the intervals sorted by increasing start |
| 175 | IntervalList fActiveIntvls; // List of live intervals during assignment |
| 176 | // (sorted by increasing end) |
| 177 | unsigned int fNumOps = 0; |
| 178 | SkTArray<unsigned int> fEndOfOpListOpIndices; |
| 179 | int fCurOpListIndex = 0; |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 180 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 181 | SkDEBUGCODE(bool fAssigned = false;) |
| 182 | |
| 183 | char fStorage[kInitialArenaSize]; |
| 184 | SkArenaAlloc fIntervalAllocator { fStorage, kInitialArenaSize, 0 }; |
| 185 | Interval* fFreeIntervalList = nullptr; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | #endif // GrResourceAllocator_DEFINED |