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 | |
| 11 | #include "GrTexture.h" |
| 12 | #include "SkPoint.h" |
| 13 | #include "SkTDArray.h" |
| 14 | #include "SkTInternalLList.h" |
| 15 | |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 16 | #include "ops/GrDrawOp.h" |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 17 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 18 | class GrRectanizer; |
| 19 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 20 | struct GrDrawOpAtlasConfig { |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 21 | int numPlotsX() const { return fWidth / fPlotWidth; } |
| 22 | int numPlotsY() const { return fHeight / fPlotWidth; } |
| 23 | int fWidth; |
| 24 | int fHeight; |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 25 | int fLog2Width; |
| 26 | int fLog2Height; |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 27 | int fPlotWidth; |
| 28 | int fPlotHeight; |
| 29 | }; |
| 30 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 31 | /** |
| 32 | * This class manages an atlas texture on behalf of GrDrawOps. The draw ops that use the atlas |
| 33 | * perform texture uploads when preparing their draws during flush. The class provides facilities |
| 34 | * for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in "asap" mode |
| 35 | * until it is impossible to add data without overwriting texels read by draws that have not yet |
| 36 | * executed on the gpu. At that point the uploads are performed "inline" between draws. If a single |
| 37 | * draw would use enough subimage space to overflow the atlas texture then the atlas will fail to |
| 38 | * add a subimage. This gives the op the chance to end the draw and begin a new one. Additional |
| 39 | * uploads will then succeed in inline mode. |
| 40 | */ |
| 41 | class GrDrawOpAtlas { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 42 | public: |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 43 | /** |
| 44 | * An AtlasID is an opaque handle which callers can use to determine if the atlas contains |
| 45 | * a specific piece of data. |
| 46 | */ |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 47 | typedef uint64_t AtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 48 | static const uint32_t kInvalidAtlasID = 0; |
| 49 | static const uint64_t kInvalidAtlasGeneration = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 50 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 51 | /** |
| 52 | * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a |
| 53 | * specific AtlasID, it will call all of the registered listeners so they can process the |
| 54 | * eviction. |
| 55 | */ |
| 56 | typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 57 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 58 | GrDrawOpAtlas(sk_sp<GrTexture>, int numPlotsX, int numPlotsY); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 59 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 60 | /** |
| 61 | * Adds a width x height subimage to the atlas. Upon success it returns an ID and the subimage's |
| 62 | * coordinates in the backing texture. False is returned if the subimage cannot fit in the |
| 63 | * atlas without overwriting texels that will be read in the current draw. This indicates that |
| 64 | * the op should end its current draw and begin another before adding more data. Upon success, |
| 65 | * an upload of the provided image data will have been added to the GrDrawOp::Target, in "asap" |
| 66 | * mode if possible, otherwise in "inline" mode. Successive uploads in either mode may be |
| 67 | * consolidated. |
| 68 | * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call |
| 69 | * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to |
| 70 | * addToAtlas might cause the previous data to be overwritten before it has been read. |
| 71 | */ |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 72 | bool addToAtlas(AtlasID*, GrDrawOp::Target*, int width, int height, const void* image, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 73 | SkIPoint16* loc); |
| 74 | |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 75 | GrTexture* getTexture() const { return fTexture.get(); } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 76 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 77 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 78 | |
| 79 | inline bool hasID(AtlasID id) { |
| 80 | uint32_t index = GetIndexFromID(id); |
| 81 | SkASSERT(index < fNumPlots); |
| 82 | return fPlotArray[index]->genID() == GetGenerationFromID(id); |
| 83 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 84 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 85 | /** To ensure the atlas does not evict a given entry, the client must set the last use token. */ |
| 86 | inline void setLastUseToken(AtlasID id, GrDrawOpUploadToken token) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 87 | SkASSERT(this->hasID(id)); |
| 88 | uint32_t index = GetIndexFromID(id); |
| 89 | SkASSERT(index < fNumPlots); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 90 | this->makeMRU(fPlotArray[index].get()); |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 91 | fPlotArray[index]->setLastUseToken(token); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | inline void registerEvictionCallback(EvictionFunc func, void* userData) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 95 | EvictionData* data = fEvictionCallbacks.append(); |
| 96 | data->fFunc = func; |
| 97 | data->fData = userData; |
| 98 | } |
| 99 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 100 | /** |
| 101 | * A class which can be handed back to GrDrawOpAtlas for updating last use tokens in bulk. The |
| 102 | * current max number of plots the GrDrawOpAtlas can handle is 32. If in the future this is |
| 103 | * insufficient then we can move to a 64 bit int. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 104 | */ |
| 105 | class BulkUseTokenUpdater { |
| 106 | public: |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 107 | BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {} |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 108 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
| 109 | : fPlotsToUpdate(that.fPlotsToUpdate) |
| 110 | , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) { |
| 111 | } |
| 112 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 113 | void add(AtlasID id) { |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 114 | int index = GrDrawOpAtlas::GetIndexFromID(id); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 115 | if (!this->find(index)) { |
| 116 | this->set(index); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void reset() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 121 | fPlotsToUpdate.reset(); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 122 | fPlotAlreadyUpdated = 0; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | bool find(int index) const { |
| 127 | SkASSERT(index < kMaxPlots); |
| 128 | return (fPlotAlreadyUpdated >> index) & 1; |
| 129 | } |
| 130 | |
| 131 | void set(int index) { |
| 132 | SkASSERT(!this->find(index)); |
| 133 | fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 134 | fPlotsToUpdate.push_back(index); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static const int kMinItems = 4; |
| 138 | static const int kMaxPlots = 32; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 139 | SkSTArray<kMinItems, int, true> fPlotsToUpdate; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 140 | uint32_t fPlotAlreadyUpdated; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 141 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 142 | friend class GrDrawOpAtlas; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 145 | void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDrawOpUploadToken token) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 146 | int count = updater.fPlotsToUpdate.count(); |
| 147 | for (int i = 0; i < count; i++) { |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 148 | Plot* plot = fPlotArray[updater.fPlotsToUpdate[i]].get(); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 149 | this->makeMRU(plot); |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 150 | plot->setLastUseToken(token); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 153 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 154 | static const int kGlyphMaxDim = 256; |
| 155 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 156 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 157 | } |
| 158 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 159 | private: |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 160 | /** |
| 161 | * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots |
| 162 | * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its |
| 163 | * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e. |
| 164 | * there is no room for the new subimage according to the GrRectanizer), it can no longer be |
| 165 | * used unless the last use of the Plot has already been flushed through to the gpu. |
| 166 | */ |
| 167 | class Plot : public SkRefCnt { |
| 168 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 169 | |
| 170 | public: |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 171 | /** index() is a unique id for the plot relative to the owning GrAtlas. */ |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 172 | uint32_t index() const { return fIndex; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 173 | /** |
| 174 | * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know |
| 175 | * if a particular subimage is still present in the atlas. |
| 176 | */ |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 177 | uint64_t genID() const { return fGenID; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 178 | GrDrawOpAtlas::AtlasID id() const { |
| 179 | SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 180 | return fID; |
| 181 | } |
| 182 | SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; }) |
| 183 | |
| 184 | bool addSubImage(int width, int height, const void* image, SkIPoint16* loc); |
| 185 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 186 | /** |
| 187 | * To manage the lifetime of a plot, we use two tokens. We use the last upload token to |
| 188 | * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to |
| 189 | * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We |
| 190 | * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use |
| 191 | * has already flushed through the gpu then we can reuse the plot. |
| 192 | */ |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 193 | GrDrawOpUploadToken lastUploadToken() const { return fLastUpload; } |
| 194 | GrDrawOpUploadToken lastUseToken() const { return fLastUse; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 195 | void setLastUploadToken(GrDrawOpUploadToken token) { fLastUpload = token; } |
| 196 | void setLastUseToken(GrDrawOpUploadToken token) { fLastUse = token; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 197 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 198 | void uploadToTexture(GrDrawOp::WritePixelsFn&, GrTexture* texture); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 199 | void resetRects(); |
| 200 | |
| 201 | private: |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 202 | Plot(int index, uint64_t genID, int offX, int offY, int width, int height, |
| 203 | GrPixelConfig config); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 204 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 205 | ~Plot() override; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 206 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 207 | /** |
| 208 | * Create a clone of this plot. The cloned plot will take the place of the current plot in |
| 209 | * the atlas |
| 210 | */ |
| 211 | Plot* clone() const { |
| 212 | return new Plot(fIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 215 | static GrDrawOpAtlas::AtlasID CreateId(uint32_t index, uint64_t generation) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 216 | SkASSERT(index < (1 << 16)); |
| 217 | SkASSERT(generation < ((uint64_t)1 << 48)); |
| 218 | return generation << 16 | index; |
| 219 | } |
| 220 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 221 | GrDrawOpUploadToken fLastUpload; |
| 222 | GrDrawOpUploadToken fLastUse; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 223 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 224 | const uint32_t fIndex; |
| 225 | uint64_t fGenID; |
| 226 | GrDrawOpAtlas::AtlasID fID; |
| 227 | unsigned char* fData; |
| 228 | const int fWidth; |
| 229 | const int fHeight; |
| 230 | const int fX; |
| 231 | const int fY; |
| 232 | GrRectanizer* fRects; |
| 233 | const SkIPoint16 fOffset; // the offset of the plot in the backing texture |
| 234 | const GrPixelConfig fConfig; |
| 235 | const size_t fBytesPerPixel; |
| 236 | SkIRect fDirtyRect; |
| 237 | SkDEBUGCODE(bool fDirty); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 238 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 239 | friend class GrDrawOpAtlas; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 240 | |
| 241 | typedef SkRefCnt INHERITED; |
| 242 | }; |
| 243 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 244 | typedef SkTInternalLList<Plot> PlotList; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 245 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 246 | static uint32_t GetIndexFromID(AtlasID id) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 247 | return id & 0xffff; |
| 248 | } |
| 249 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 250 | // top 48 bits are reserved for the generation ID |
| 251 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 252 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 255 | inline void updatePlot(GrDrawOp::Target*, AtlasID*, Plot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 256 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 257 | inline void makeMRU(Plot* plot) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 258 | if (fPlotList.head() == plot) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | fPlotList.remove(plot); |
| 263 | fPlotList.addToHead(plot); |
| 264 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 265 | |
| 266 | inline void processEviction(AtlasID); |
| 267 | |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 268 | sk_sp<GrTexture> fTexture; |
| 269 | int fPlotWidth; |
| 270 | int fPlotHeight; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 271 | SkDEBUGCODE(uint32_t fNumPlots;) |
| 272 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 273 | uint64_t fAtlasGeneration; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 274 | |
| 275 | struct EvictionData { |
| 276 | EvictionFunc fFunc; |
| 277 | void* fData; |
| 278 | }; |
| 279 | |
| 280 | SkTDArray<EvictionData> fEvictionCallbacks; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 281 | // allocated array of Plots |
| 282 | std::unique_ptr<sk_sp<Plot>[]> fPlotArray; |
| 283 | // LRU list of Plots (MRU at head - LRU at tail) |
| 284 | PlotList fPlotList; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | #endif |