joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 8 | #ifndef GrDrawOpAtlas_DEFINED |
| 9 | #define GrDrawOpAtlas_DEFINED |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 10 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 11 | #include "SkPoint.h" |
| 12 | #include "SkTDArray.h" |
| 13 | #include "SkTInternalLList.h" |
| 14 | |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 15 | #include "ops/GrDrawOp.h" |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 16 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 17 | class GrRectanizer; |
| 18 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 19 | struct GrDrawOpAtlasConfig { |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 20 | int numPlotsX() const { return fWidth / fPlotWidth; } |
| 21 | int numPlotsY() const { return fHeight / fPlotWidth; } |
| 22 | int fWidth; |
| 23 | int fHeight; |
| 24 | int fPlotWidth; |
| 25 | int fPlotHeight; |
| 26 | }; |
| 27 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 28 | /** |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 29 | * This class manages one or more atlas textures on behalf of GrDrawOps. The draw ops that use the |
| 30 | * atlas perform texture uploads when preparing their draws during flush. The class provides |
| 31 | * facilities for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 32 | * "ASAP" mode until it is impossible to add data without overwriting texels read by draws that |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 33 | * have not yet executed on the gpu. At that point, the atlas will attempt to allocate a new |
| 34 | * atlas texture (or "page") of the same size, up to a maximum number of textures, and upload |
| 35 | * to that texture. If that's not possible, the uploads are performed "inline" between draws. If a |
| 36 | * single draw would use enough subimage space to overflow the atlas texture then the atlas will |
| 37 | * fail to add a subimage. This gives the op the chance to end the draw and begin a new one. |
| 38 | * Additional uploads will then succeed in inline mode. |
| 39 | * |
| 40 | * When the atlas has multiple pages, new uploads are prioritized to the lower index pages, i.e., |
| 41 | * it will try to upload to page 0 before page 1 or 2. To keep the atlas from continually using |
| 42 | * excess space, periodic garbage collection is needed to shift data from the higher index pages to |
| 43 | * the lower ones, and then eventually remove any pages that are no longer in use. "In use" is |
| 44 | * determined by using the GrDrawUploadToken system: After a flush each subarea of the page |
| 45 | * is checked to see whether it was used in that flush; if it is not, a counter is incremented. |
| 46 | * Once that counter reaches a threshold that subarea is considered to be no longer in use. |
| 47 | * |
| 48 | * Garbage collection is initiated by the GrDrawOpAtlas's client via the compact() method. One |
| 49 | * solution is to make the client a subclass of GrOnFlushCallbackObject, register it with the |
| 50 | * GrContext via addOnFlushCallbackObject(), and the client's postFlush() method calls compact() |
| 51 | * and passes in the given GrDrawUploadToken. |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 52 | */ |
| 53 | class GrDrawOpAtlas { |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 54 | private: |
| 55 | static constexpr auto kMaxMultitexturePages = 4; |
| 56 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 57 | public: |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 58 | /** Is the atlas allowed to use more than one texture? */ |
| 59 | enum class AllowMultitexturing : bool { kNo, kYes }; |
| 60 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 61 | /** |
| 62 | * An AtlasID is an opaque handle which callers can use to determine if the atlas contains |
| 63 | * a specific piece of data. |
| 64 | */ |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 65 | typedef uint64_t AtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 66 | static const uint32_t kInvalidAtlasID = 0; |
| 67 | static const uint64_t kInvalidAtlasGeneration = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 68 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 69 | /** |
| 70 | * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a |
| 71 | * specific AtlasID, it will call all of the registered listeners so they can process the |
| 72 | * eviction. |
| 73 | */ |
| 74 | typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 75 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 76 | /** |
| 77 | * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas |
| 78 | * should only be used inside of GrMeshDrawOp::onPrepareDraws. |
| 79 | * @param GrPixelConfig The pixel config which this atlas will store |
| 80 | * @param width width in pixels of the atlas |
| 81 | * @param height height in pixels of the atlas |
| 82 | * @param numPlotsX The number of plots the atlas should be broken up into in the X |
| 83 | * direction |
| 84 | * @param numPlotsY The number of plots the atlas should be broken up into in the Y |
| 85 | * direction |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 86 | * @param allowMultitexturing Can the atlas use more than one texture. |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 87 | * @param func An eviction function which will be called whenever the atlas has to |
| 88 | * evict data |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 89 | * @param data User supplied data which will be passed into func whenever an |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 90 | * eviction occurs |
| 91 | * @return An initialized GrDrawOpAtlas, or nullptr if creation fails |
| 92 | */ |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 93 | static std::unique_ptr<GrDrawOpAtlas> Make(GrContext*, GrPixelConfig, int width, int height, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 94 | int numPlotsX, int numPlotsY, |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 95 | AllowMultitexturing allowMultitexturing, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 96 | GrDrawOpAtlas::EvictionFunc func, void* data); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 97 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 98 | /** |
| 99 | * Adds a width x height subimage to the atlas. Upon success it returns an ID and the subimage's |
| 100 | * coordinates in the backing texture. False is returned if the subimage cannot fit in the |
| 101 | * atlas without overwriting texels that will be read in the current draw. This indicates that |
| 102 | * the op should end its current draw and begin another before adding more data. Upon success, |
| 103 | * an upload of the provided image data will have been added to the GrDrawOp::Target, in "asap" |
| 104 | * mode if possible, otherwise in "inline" mode. Successive uploads in either mode may be |
| 105 | * consolidated. |
| 106 | * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call |
| 107 | * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to |
| 108 | * addToAtlas might cause the previous data to be overwritten before it has been read. |
| 109 | */ |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 110 | bool addToAtlas(AtlasID*, GrDeferredUploadTarget*, int width, int height, const void* image, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 111 | SkIPoint16* loc); |
| 112 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 113 | GrContext* context() const { return fContext; } |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 114 | const sk_sp<GrTextureProxy>* getProxies() const { return fProxies; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 115 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 116 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 117 | |
| 118 | inline bool hasID(AtlasID id) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 119 | uint32_t plot = GetPlotIndexFromID(id); |
| 120 | SkASSERT(plot < fNumPlots); |
| 121 | uint32_t page = GetPageIndexFromID(id); |
| 122 | SkASSERT(page < fNumPages); |
| 123 | return fPages[page].fPlotArray[plot]->genID() == GetGenerationFromID(id); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 124 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 125 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 126 | /** To ensure the atlas does not evict a given entry, the client must set the last use token. */ |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 127 | inline void setLastUseToken(AtlasID id, GrDeferredUploadToken token) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 128 | SkASSERT(this->hasID(id)); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 129 | uint32_t plotIdx = GetPlotIndexFromID(id); |
| 130 | SkASSERT(plotIdx < fNumPlots); |
| 131 | uint32_t pageIdx = GetPageIndexFromID(id); |
| 132 | SkASSERT(pageIdx < fNumPages); |
| 133 | Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get(); |
| 134 | this->makeMRU(plot, pageIdx); |
| 135 | plot->setLastUseToken(token); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | inline void registerEvictionCallback(EvictionFunc func, void* userData) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 139 | EvictionData* data = fEvictionCallbacks.append(); |
| 140 | data->fFunc = func; |
| 141 | data->fData = userData; |
| 142 | } |
| 143 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 144 | uint32_t pageCount() { return fNumPages; } |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 145 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 146 | /** |
| 147 | * A class which can be handed back to GrDrawOpAtlas for updating last use tokens in bulk. The |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 148 | * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future |
| 149 | * this is insufficient then we can move to a 64 bit int. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 150 | */ |
| 151 | class BulkUseTokenUpdater { |
| 152 | public: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 153 | BulkUseTokenUpdater() { |
| 154 | memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated)); |
| 155 | } |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 156 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 157 | : fPlotsToUpdate(that.fPlotsToUpdate) { |
| 158 | memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated)); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 159 | } |
| 160 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 161 | void add(AtlasID id) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 162 | int index = GrDrawOpAtlas::GetPlotIndexFromID(id); |
| 163 | int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id); |
| 164 | if (!this->find(pageIdx, index)) { |
| 165 | this->set(pageIdx, index); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | void reset() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 170 | fPlotsToUpdate.reset(); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 171 | memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated)); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 174 | struct PlotData { |
| 175 | PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {} |
| 176 | uint32_t fPageIndex; |
| 177 | uint32_t fPlotIndex; |
| 178 | }; |
| 179 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 180 | private: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 181 | bool find(int pageIdx, int index) const { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 182 | SkASSERT(index < kMaxPlots); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 183 | return (fPlotAlreadyUpdated[pageIdx] >> index) & 1; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 186 | void set(int pageIdx, int index) { |
| 187 | SkASSERT(!this->find(pageIdx, index)); |
| 188 | fPlotAlreadyUpdated[pageIdx] |= (1 << index); |
| 189 | fPlotsToUpdate.push_back(PlotData(pageIdx, index)); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 192 | static constexpr int kMinItems = 4; |
| 193 | static constexpr int kMaxPlots = 32; |
| 194 | SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 195 | uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages]; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 196 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 197 | friend class GrDrawOpAtlas; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 200 | void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 201 | int count = updater.fPlotsToUpdate.count(); |
| 202 | for (int i = 0; i < count; i++) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 203 | const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i]; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 204 | // it's possible we've added a plot to the updater and subsequently the plot's page |
| 205 | // was deleted -- so we check to prevent a crash |
Jim Van Verth | 6ca9c6f | 2017-09-27 18:04:34 -0400 | [diff] [blame] | 206 | if (pd.fPageIndex < fNumPages) { |
| 207 | Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get(); |
| 208 | this->makeMRU(plot, pd.fPageIndex); |
| 209 | plot->setLastUseToken(token); |
| 210 | } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 213 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 214 | void compact(GrDeferredUploadToken startTokenForNextFlush); |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 215 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 216 | static constexpr auto kGlyphMaxDim = 256; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 217 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 218 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 219 | } |
| 220 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 221 | static uint32_t GetPageIndexFromID(AtlasID id) { |
| 222 | return id & 0xff; |
| 223 | } |
| 224 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 225 | private: |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 226 | uint32_t maxPages() const { |
| 227 | return AllowMultitexturing::kYes == fAllowMultitexturing ? kMaxMultitexturePages : 1; |
| 228 | } |
| 229 | |
| 230 | GrDrawOpAtlas(GrContext*, GrPixelConfig config, int width, int height, int numPlotsX, |
| 231 | int numPlotsY, AllowMultitexturing allowMultitexturing); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 232 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 233 | /** |
| 234 | * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots |
| 235 | * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its |
| 236 | * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e. |
| 237 | * there is no room for the new subimage according to the GrRectanizer), it can no longer be |
| 238 | * used unless the last use of the Plot has already been flushed through to the gpu. |
| 239 | */ |
| 240 | class Plot : public SkRefCnt { |
| 241 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 242 | |
| 243 | public: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 244 | /** index() is a unique id for the plot relative to the owning GrAtlas and page. */ |
| 245 | uint32_t index() const { return fPlotIndex; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 246 | /** |
| 247 | * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know |
| 248 | * if a particular subimage is still present in the atlas. |
| 249 | */ |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 250 | uint64_t genID() const { return fGenID; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 251 | GrDrawOpAtlas::AtlasID id() const { |
| 252 | SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 253 | return fID; |
| 254 | } |
| 255 | SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; }) |
| 256 | |
| 257 | bool addSubImage(int width, int height, const void* image, SkIPoint16* loc); |
| 258 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 259 | /** |
| 260 | * To manage the lifetime of a plot, we use two tokens. We use the last upload token to |
| 261 | * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to |
| 262 | * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We |
| 263 | * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use |
| 264 | * has already flushed through the gpu then we can reuse the plot. |
| 265 | */ |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 266 | GrDeferredUploadToken lastUploadToken() const { return fLastUpload; } |
| 267 | GrDeferredUploadToken lastUseToken() const { return fLastUse; } |
| 268 | void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; } |
| 269 | void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 270 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 271 | void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 272 | void resetRects(); |
| 273 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 274 | int flushesSinceLastUsed() { return fFlushesSinceLastUse; } |
| 275 | void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; } |
| 276 | void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; } |
| 277 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 278 | private: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 279 | Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, int width, int height, |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 280 | GrPixelConfig config); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 281 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 282 | ~Plot() override; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 283 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 284 | /** |
| 285 | * Create a clone of this plot. The cloned plot will take the place of the current plot in |
| 286 | * the atlas |
| 287 | */ |
| 288 | Plot* clone() const { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 289 | return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 292 | static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx, |
| 293 | uint64_t generation) { |
| 294 | SkASSERT(pageIdx < (1 << 8)); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 295 | SkASSERT(pageIdx < kMaxMultitexturePages); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 296 | SkASSERT(plotIdx < (1 << 8)); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 297 | SkASSERT(generation < ((uint64_t)1 << 48)); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 298 | return generation << 16 | plotIdx << 8 | pageIdx; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 301 | GrDeferredUploadToken fLastUpload; |
| 302 | GrDeferredUploadToken fLastUse; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 303 | // the number of flushes since this plot has been last used |
| 304 | int fFlushesSinceLastUse; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 305 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 306 | struct { |
| 307 | const uint32_t fPageIndex : 16; |
| 308 | const uint32_t fPlotIndex : 16; |
| 309 | }; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 310 | uint64_t fGenID; |
| 311 | GrDrawOpAtlas::AtlasID fID; |
| 312 | unsigned char* fData; |
| 313 | const int fWidth; |
| 314 | const int fHeight; |
| 315 | const int fX; |
| 316 | const int fY; |
| 317 | GrRectanizer* fRects; |
| 318 | const SkIPoint16 fOffset; // the offset of the plot in the backing texture |
| 319 | const GrPixelConfig fConfig; |
| 320 | const size_t fBytesPerPixel; |
| 321 | SkIRect fDirtyRect; |
| 322 | SkDEBUGCODE(bool fDirty); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 323 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 324 | friend class GrDrawOpAtlas; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 325 | |
| 326 | typedef SkRefCnt INHERITED; |
| 327 | }; |
| 328 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 329 | typedef SkTInternalLList<Plot> PlotList; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 330 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 331 | static uint32_t GetPlotIndexFromID(AtlasID id) { |
| 332 | return (id >> 8) & 0xff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 333 | } |
| 334 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 335 | // top 48 bits are reserved for the generation ID |
| 336 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 337 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 340 | inline bool updatePlot(GrDeferredUploadTarget*, AtlasID*, Plot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 341 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 342 | inline void makeMRU(Plot* plot, int pageIdx) { |
| 343 | if (fPages[pageIdx].fPlotList.head() == plot) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 347 | fPages[pageIdx].fPlotList.remove(plot); |
| 348 | fPages[pageIdx].fPlotList.addToHead(plot); |
| 349 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 350 | // No MRU update for pages -- since we will always try to add from |
| 351 | // the front and remove from the back there is no need for MRU. |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 352 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 353 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 354 | bool createNewPage(); |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 355 | void deleteLastPage(); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 356 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 357 | void processEviction(AtlasID); |
| 358 | inline void processEvictionAndResetRects(Plot* plot) { |
| 359 | this->processEviction(plot->id()); |
| 360 | plot->resetRects(); |
| 361 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 362 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 363 | GrContext* fContext; |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 364 | GrPixelConfig fPixelConfig; |
| 365 | int fTextureWidth; |
| 366 | int fTextureHeight; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 367 | int fPlotWidth; |
| 368 | int fPlotHeight; |
| 369 | SkDEBUGCODE(uint32_t fNumPlots;) |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 370 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 371 | uint64_t fAtlasGeneration; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 372 | // nextTokenToFlush() value at the end of the previous flush |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 373 | GrDeferredUploadToken fPrevFlushToken; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 374 | |
| 375 | struct EvictionData { |
| 376 | EvictionFunc fFunc; |
| 377 | void* fData; |
| 378 | }; |
| 379 | |
| 380 | SkTDArray<EvictionData> fEvictionCallbacks; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 381 | |
| 382 | struct Page { |
| 383 | // allocated array of Plots |
| 384 | std::unique_ptr<sk_sp<Plot>[]> fPlotArray; |
| 385 | // LRU list of Plots (MRU at head - LRU at tail) |
| 386 | PlotList fPlotList; |
| 387 | }; |
| 388 | // proxies kept separate to make it easier to pass them up to client |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 389 | sk_sp<GrTextureProxy> fProxies[kMaxMultitexturePages]; |
| 390 | Page fPages[kMaxMultitexturePages]; |
| 391 | AllowMultitexturing fAllowMultitexturing; |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 392 | uint32_t fNumPages; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 393 | }; |
| 394 | |
| 395 | #endif |