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