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; |
| 123 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 124 | /** |
| 125 | * 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^] | 126 | * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future |
| 127 | * this is insufficient then we can move to a 64 bit int. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 128 | */ |
| 129 | class BulkUseTokenUpdater { |
| 130 | public: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 131 | BulkUseTokenUpdater() { |
| 132 | memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated)); |
| 133 | } |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 134 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 135 | : fPlotsToUpdate(that.fPlotsToUpdate) { |
| 136 | memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated)); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 137 | } |
| 138 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 139 | void add(AtlasID id) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 140 | int index = GrDrawOpAtlas::GetPlotIndexFromID(id); |
| 141 | int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id); |
| 142 | if (!this->find(pageIdx, index)) { |
| 143 | this->set(pageIdx, index); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | void reset() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 148 | fPlotsToUpdate.reset(); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 149 | memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated)); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 152 | struct PlotData { |
| 153 | PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {} |
| 154 | uint32_t fPageIndex; |
| 155 | uint32_t fPlotIndex; |
| 156 | }; |
| 157 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 158 | private: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 159 | bool find(int pageIdx, int index) const { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 160 | SkASSERT(index < kMaxPlots); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 161 | return (fPlotAlreadyUpdated[pageIdx] >> index) & 1; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 164 | void set(int pageIdx, int index) { |
| 165 | SkASSERT(!this->find(pageIdx, index)); |
| 166 | fPlotAlreadyUpdated[pageIdx] |= (1 << index); |
| 167 | fPlotsToUpdate.push_back(PlotData(pageIdx, index)); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 170 | static constexpr int kMinItems = 4; |
| 171 | static constexpr int kMaxPlots = 32; |
| 172 | SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate; |
| 173 | uint32_t fPlotAlreadyUpdated[kMaxPages]; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 174 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 175 | friend class GrDrawOpAtlas; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 178 | void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDrawOpUploadToken token) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 179 | int count = updater.fPlotsToUpdate.count(); |
| 180 | for (int i = 0; i < count; i++) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 181 | const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i]; |
| 182 | Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get(); |
| 183 | this->makeMRU(plot, pd.fPageIndex); |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 184 | plot->setLastUseToken(token); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 187 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 188 | static constexpr auto kGlyphMaxDim = 256; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 189 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 190 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 191 | } |
| 192 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 193 | private: |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 194 | GrDrawOpAtlas(GrContext*, GrPixelConfig config, int width, int height, |
| 195 | int numPlotsX, int numPlotsY); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 196 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 197 | /** |
| 198 | * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots |
| 199 | * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its |
| 200 | * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e. |
| 201 | * there is no room for the new subimage according to the GrRectanizer), it can no longer be |
| 202 | * used unless the last use of the Plot has already been flushed through to the gpu. |
| 203 | */ |
| 204 | class Plot : public SkRefCnt { |
| 205 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 206 | |
| 207 | public: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 208 | /** index() is a unique id for the plot relative to the owning GrAtlas and page. */ |
| 209 | uint32_t index() const { return fPlotIndex; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 210 | /** |
| 211 | * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know |
| 212 | * if a particular subimage is still present in the atlas. |
| 213 | */ |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 214 | uint64_t genID() const { return fGenID; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 215 | GrDrawOpAtlas::AtlasID id() const { |
| 216 | SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 217 | return fID; |
| 218 | } |
| 219 | SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; }) |
| 220 | |
| 221 | bool addSubImage(int width, int height, const void* image, SkIPoint16* loc); |
| 222 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 223 | /** |
| 224 | * To manage the lifetime of a plot, we use two tokens. We use the last upload token to |
| 225 | * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to |
| 226 | * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We |
| 227 | * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use |
| 228 | * has already flushed through the gpu then we can reuse the plot. |
| 229 | */ |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 230 | GrDrawOpUploadToken lastUploadToken() const { return fLastUpload; } |
| 231 | GrDrawOpUploadToken lastUseToken() const { return fLastUse; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 232 | void setLastUploadToken(GrDrawOpUploadToken token) { fLastUpload = token; } |
| 233 | void setLastUseToken(GrDrawOpUploadToken token) { fLastUse = token; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 234 | |
Robert Phillips | acaa607 | 2017-07-28 10:54:53 -0400 | [diff] [blame] | 235 | void uploadToTexture(GrDrawOp::WritePixelsFn&, GrTextureProxy*); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 236 | void resetRects(); |
| 237 | |
| 238 | private: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 239 | 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] | 240 | GrPixelConfig config); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 241 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 242 | ~Plot() override; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 243 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 244 | /** |
| 245 | * Create a clone of this plot. The cloned plot will take the place of the current plot in |
| 246 | * the atlas |
| 247 | */ |
| 248 | Plot* clone() const { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 249 | return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 252 | static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx, |
| 253 | uint64_t generation) { |
| 254 | SkASSERT(pageIdx < (1 << 8)); |
| 255 | SkASSERT(pageIdx == 0); // for now, we only support one page |
| 256 | SkASSERT(plotIdx < (1 << 8)); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 257 | SkASSERT(generation < ((uint64_t)1 << 48)); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 258 | return generation << 16 | plotIdx << 8 | pageIdx; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 261 | GrDrawOpUploadToken fLastUpload; |
| 262 | GrDrawOpUploadToken fLastUse; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 263 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 264 | struct { |
| 265 | const uint32_t fPageIndex : 16; |
| 266 | const uint32_t fPlotIndex : 16; |
| 267 | }; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 268 | uint64_t fGenID; |
| 269 | GrDrawOpAtlas::AtlasID fID; |
| 270 | unsigned char* fData; |
| 271 | const int fWidth; |
| 272 | const int fHeight; |
| 273 | const int fX; |
| 274 | const int fY; |
| 275 | GrRectanizer* fRects; |
| 276 | const SkIPoint16 fOffset; // the offset of the plot in the backing texture |
| 277 | const GrPixelConfig fConfig; |
| 278 | const size_t fBytesPerPixel; |
| 279 | SkIRect fDirtyRect; |
| 280 | SkDEBUGCODE(bool fDirty); |
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 | friend class GrDrawOpAtlas; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 283 | |
| 284 | typedef SkRefCnt INHERITED; |
| 285 | }; |
| 286 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 287 | typedef SkTInternalLList<Plot> PlotList; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 288 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 289 | static uint32_t GetPageIndexFromID(AtlasID id) { |
| 290 | return id & 0xff; |
| 291 | } |
| 292 | |
| 293 | static uint32_t GetPlotIndexFromID(AtlasID id) { |
| 294 | return (id >> 8) & 0xff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 295 | } |
| 296 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 297 | // top 48 bits are reserved for the generation ID |
| 298 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 299 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 302 | inline bool updatePlot(GrDrawOp::Target*, AtlasID*, Plot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 303 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 304 | inline void makeMRU(Plot* plot, int pageIdx) { |
| 305 | if (fPages[pageIdx].fPlotList.head() == plot) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 306 | return; |
| 307 | } |
| 308 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 309 | fPages[pageIdx].fPlotList.remove(plot); |
| 310 | fPages[pageIdx].fPlotList.addToHead(plot); |
| 311 | |
| 312 | // TODO: make page MRU |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 313 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 314 | |
| 315 | inline void processEviction(AtlasID); |
| 316 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 317 | GrContext* fContext; |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 318 | GrPixelConfig fPixelConfig; |
| 319 | int fTextureWidth; |
| 320 | int fTextureHeight; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 321 | int fPlotWidth; |
| 322 | int fPlotHeight; |
| 323 | SkDEBUGCODE(uint32_t fNumPlots;) |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 324 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 325 | uint64_t fAtlasGeneration; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 326 | |
| 327 | struct EvictionData { |
| 328 | EvictionFunc fFunc; |
| 329 | void* fData; |
| 330 | }; |
| 331 | |
| 332 | SkTDArray<EvictionData> fEvictionCallbacks; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame^] | 333 | |
| 334 | struct Page { |
| 335 | // allocated array of Plots |
| 336 | std::unique_ptr<sk_sp<Plot>[]> fPlotArray; |
| 337 | // LRU list of Plots (MRU at head - LRU at tail) |
| 338 | PlotList fPlotList; |
| 339 | }; |
| 340 | // proxies kept separate to make it easier to pass them up to client |
| 341 | sk_sp<GrTextureProxy> fProxies[kMaxPages]; |
| 342 | Page fPages[kMaxPages]; |
| 343 | SkDEBUGCODE(uint32_t fNumPages;) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | #endif |