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 | |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 11 | #include <cmath> |
Herb Derby | 1a496c5 | 2020-01-22 17:26:56 -0500 | [diff] [blame] | 12 | #include <vector> |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 13 | |
Robert Phillips | 51b3e60 | 2020-04-09 12:48:50 -0400 | [diff] [blame] | 14 | #include "include/gpu/GrBackendSurface.h" |
| 15 | #include "include/private/SkTArray.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/core/SkIPoint16.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 17 | #include "src/core/SkTInternalLList.h" |
Robert Phillips | 51b3e60 | 2020-04-09 12:48:50 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrDeferredUpload.h" |
Herb Derby | 73c7587 | 2020-01-22 18:09:16 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrRectanizerSkyline.h" |
Robert Phillips | 51b3e60 | 2020-04-09 12:48:50 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrSurfaceProxyView.h" |
| 21 | #include "src/gpu/geometry/GrRect.h" |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 22 | |
Robert Phillips | cd5099c | 2018-02-09 09:56:56 -0500 | [diff] [blame] | 23 | class GrOnFlushResourceProvider; |
Robert Phillips | 51b3e60 | 2020-04-09 12:48:50 -0400 | [diff] [blame] | 24 | class GrProxyProvider; |
| 25 | class GrResourceProvider; |
| 26 | class GrTextureProxy; |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 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 { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 54 | public: |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 55 | /** Is the atlas allowed to use more than one texture? */ |
| 56 | enum class AllowMultitexturing : bool { kNo, kYes }; |
| 57 | |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 58 | // These are both restricted by the space they occupy in the PlotLocator. |
| 59 | // maxPages is also limited by being crammed into the glyph uvs. |
| 60 | // maxPlots is also limited by the fPlotAlreadyUpdated bitfield in BulkUseTokenUpdater |
| 61 | static constexpr auto kMaxMultitexturePages = 4; |
| 62 | static constexpr int kMaxPlots = 32; |
Herb Derby | bbf5fb5 | 2018-10-15 16:39:39 -0400 | [diff] [blame] | 63 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 64 | /** |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame] | 65 | * A PlotLocator specifies the plot and is analogous to a directory path: |
| 66 | * page/plot/plotGeneration |
| 67 | * |
| 68 | * In fact PlotLocator is a portion of a glyph image location in the atlas fully specified by: |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 69 | * format/atlasGeneration/page/plot/plotGeneration/rect |
| 70 | * |
| 71 | * TODO: Remove the small path renderer's use of the PlotLocator for eviction. |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 72 | */ |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 73 | class PlotLocator { |
| 74 | public: |
| 75 | PlotLocator(uint32_t pageIdx, uint32_t plotIdx, uint64_t generation) |
| 76 | : fGenID(generation) |
| 77 | , fPlotIndex(plotIdx) |
| 78 | , fPageIndex(pageIdx) { |
| 79 | SkASSERT(pageIdx < kMaxMultitexturePages); |
| 80 | SkASSERT(plotIdx < kMaxPlots); |
| 81 | SkASSERT(generation < ((uint64_t)1 << 48)); |
| 82 | } |
| 83 | |
| 84 | PlotLocator() : fGenID(0), fPlotIndex(0), fPageIndex(0) {} |
| 85 | |
| 86 | bool isValid() const { |
| 87 | return fGenID != 0 || fPlotIndex != 0 || fPageIndex != 0; |
| 88 | } |
| 89 | |
Robert Phillips | 6507e63 | 2020-08-07 14:35:56 -0400 | [diff] [blame] | 90 | void makeInvalid() { |
| 91 | fGenID = 0; |
| 92 | fPlotIndex = 0; |
| 93 | fPageIndex = 0; |
| 94 | } |
| 95 | |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 96 | bool operator==(const PlotLocator& other) const { |
| 97 | return fGenID == other.fGenID && |
| 98 | fPlotIndex == other.fPlotIndex && |
| 99 | fPageIndex == other.fPageIndex; } |
| 100 | |
| 101 | uint32_t pageIndex() const { return fPageIndex; } |
| 102 | uint32_t plotIndex() const { return fPlotIndex; } |
| 103 | uint64_t genID() const { return fGenID; } |
| 104 | |
| 105 | private: |
| 106 | uint64_t fGenID:48; |
| 107 | uint64_t fPlotIndex:8; |
| 108 | uint64_t fPageIndex:8; |
| 109 | }; |
| 110 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 111 | static const uint64_t kInvalidAtlasGeneration = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 112 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 113 | class AtlasLocator { |
| 114 | public: |
Herb Derby | 7491e8b | 2020-08-14 15:28:53 -0400 | [diff] [blame^] | 115 | std::array<uint16_t, 4> getUVs() const { |
| 116 | |
| 117 | // We pack the 2bit page index in the low bit of the u and v texture coords |
| 118 | uint32_t pageIndex = this->pageIndex(); |
| 119 | auto [left, top] = PackIndexInTexCoords(fRect.fLeft, fRect.fTop, pageIndex); |
| 120 | auto [right, bottom] = PackIndexInTexCoords(fRect.fRight, fRect.fBottom, pageIndex); |
| 121 | return { left, top, right, bottom }; |
| 122 | } |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 123 | |
Robert Phillips | 6507e63 | 2020-08-07 14:35:56 -0400 | [diff] [blame] | 124 | void invalidatePlotLocator() { fPlotLocator.makeInvalid(); } |
| 125 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 126 | // TODO: Remove the small path renderer's use of this for eviction |
Robert Phillips | 51b3e60 | 2020-04-09 12:48:50 -0400 | [diff] [blame] | 127 | PlotLocator plotLocator() const { return fPlotLocator; } |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 128 | |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 129 | uint32_t pageIndex() const { return fPlotLocator.pageIndex(); } |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 130 | |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 131 | uint32_t plotIndex() const { return fPlotLocator.plotIndex(); } |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 132 | |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 133 | uint64_t genID() const { return fPlotLocator.genID(); } |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 134 | |
Herb Derby | 71984a4 | 2020-07-31 16:08:41 -0400 | [diff] [blame] | 135 | void insetSrc(int padding) { |
| 136 | fRect.fLeft += padding; |
| 137 | fRect.fTop += padding; |
| 138 | fRect.fRight -= padding; |
| 139 | fRect.fBottom -= padding; |
| 140 | } |
| 141 | |
Herb Derby | 70df33d | 2020-08-04 15:34:58 -0400 | [diff] [blame] | 142 | GrIRect16 rect() const { return fRect; } |
| 143 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 144 | private: |
| 145 | friend class GrDrawOpAtlas; |
| 146 | |
Robert Phillips | e87106d | 2020-04-09 14:21:33 -0400 | [diff] [blame] | 147 | SkDEBUGCODE(void validate(const GrDrawOpAtlas*) const;) |
| 148 | |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 149 | PlotLocator fPlotLocator{0, 0, 0}; |
Herb Derby | 71984a4 | 2020-07-31 16:08:41 -0400 | [diff] [blame] | 150 | |
Herb Derby | 1318e45 | 2020-08-03 14:38:10 -0400 | [diff] [blame] | 151 | // The inset padded bounds in the atlas. |
Robert Phillips | 51b3e60 | 2020-04-09 12:48:50 -0400 | [diff] [blame] | 152 | GrIRect16 fRect{0, 0, 0, 0}; |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 153 | }; |
| 154 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 155 | /** |
Herb Derby | 1a496c5 | 2020-01-22 17:26:56 -0500 | [diff] [blame] | 156 | * An interface for eviction callbacks. Whenever GrDrawOpAtlas evicts a |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame] | 157 | * specific PlotLocator, it will call all of the registered listeners so they can process the |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 158 | * eviction. |
| 159 | */ |
Herb Derby | 1a496c5 | 2020-01-22 17:26:56 -0500 | [diff] [blame] | 160 | class EvictionCallback { |
| 161 | public: |
| 162 | virtual ~EvictionCallback() = default; |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 163 | virtual void evict(PlotLocator) = 0; |
Herb Derby | 1a496c5 | 2020-01-22 17:26:56 -0500 | [diff] [blame] | 164 | }; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 165 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 166 | /** |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 167 | * Keep track of generation number for Atlases and Plots. |
| 168 | */ |
| 169 | class GenerationCounter { |
| 170 | public: |
| 171 | static constexpr uint64_t kInvalidGeneration = 0; |
| 172 | uint64_t next() { |
| 173 | return fGeneration++; |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | uint64_t fGeneration{1}; |
| 178 | }; |
| 179 | |
| 180 | /** |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 181 | * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas |
| 182 | * should only be used inside of GrMeshDrawOp::onPrepareDraws. |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 183 | * @param GrColorType The colorType which this atlas will store |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 184 | * @param width width in pixels of the atlas |
| 185 | * @param height height in pixels of the atlas |
| 186 | * @param numPlotsX The number of plots the atlas should be broken up into in the X |
| 187 | * direction |
| 188 | * @param numPlotsY The number of plots the atlas should be broken up into in the Y |
| 189 | * direction |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 190 | * @param atlasGeneration a pointer to the context's generation counter. |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 191 | * @param allowMultitexturing Can the atlas use more than one texture. |
Herb Derby | 1a496c5 | 2020-01-22 17:26:56 -0500 | [diff] [blame] | 192 | * @param evictor A pointer to an eviction callback class. |
| 193 | * |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 194 | * @return An initialized GrDrawOpAtlas, or nullptr if creation fails |
| 195 | */ |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 196 | static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*, |
| 197 | const GrBackendFormat& format, |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 198 | GrColorType, |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 199 | int width, int height, |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 200 | int plotWidth, int plotHeight, |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 201 | GenerationCounter* generationCounter, |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 202 | AllowMultitexturing allowMultitexturing, |
Herb Derby | 1a496c5 | 2020-01-22 17:26:56 -0500 | [diff] [blame] | 203 | EvictionCallback* evictor); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 204 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 205 | /** |
Jim Van Verth | fb39510 | 2020-02-03 10:11:19 -0500 | [diff] [blame] | 206 | * Packs a texture atlas page index into the uint16 texture coordinates. |
| 207 | * @param u U texture coordinate |
| 208 | * @param v V texture coordinate |
| 209 | * @param pageIndex index of the texture these coordinates apply to. |
| 210 | Must be in the range [0, 3]. |
| 211 | * @return The new u and v coordinates with the packed value |
| 212 | */ |
Herb Derby | 7491e8b | 2020-08-14 15:28:53 -0400 | [diff] [blame^] | 213 | static std::pair<uint16_t, uint16_t> PackIndexInTexCoords( |
| 214 | uint16_t u, uint16_t v, int pageIndex) { |
| 215 | // The two bits that make up the texture index are packed into the lower bits of the u and v |
| 216 | // coordinate respectively. |
| 217 | SkASSERT(pageIndex >= 0 && pageIndex < 4); |
| 218 | uint16_t uBit = (pageIndex >> 1u) & 0x1u; |
| 219 | uint16_t vBit = pageIndex & 0x1u; |
| 220 | u <<= 1u; |
| 221 | u |= uBit; |
| 222 | v <<= 1u; |
| 223 | v |= vBit; |
| 224 | return std::make_pair(u, v); |
| 225 | } |
| 226 | |
Jim Van Verth | fb39510 | 2020-02-03 10:11:19 -0500 | [diff] [blame] | 227 | /** |
| 228 | * Unpacks a texture atlas page index from uint16 texture coordinates. |
| 229 | * @param u Packed U texture coordinate |
| 230 | * @param v Packed V texture coordinate |
| 231 | * @return The unpacked u and v coordinates with the page index. |
| 232 | */ |
| 233 | static std::tuple<uint16_t, uint16_t, int> UnpackIndexFromTexCoords(uint16_t u, uint16_t v); |
| 234 | |
| 235 | /** |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 236 | * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns |
| 237 | * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if |
| 238 | * the subimage cannot fit in the atlas without overwriting texels that will be read in the |
| 239 | * current draw. This indicates that the op should end its current draw and begin another |
| 240 | * before adding more data. Upon success, an upload of the provided image data will have |
| 241 | * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode. |
| 242 | * Successive uploads in either mode may be consolidated. |
| 243 | * 'kError' will be returned when some unrecoverable error was encountered while trying to |
| 244 | * add the subimage. In this case the op being created should be discarded. |
| 245 | * |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 246 | * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call |
| 247 | * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to |
| 248 | * addToAtlas might cause the previous data to be overwritten before it has been read. |
| 249 | */ |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 250 | |
| 251 | enum class ErrorCode { |
| 252 | kError, |
| 253 | kSucceeded, |
| 254 | kTryAgain |
| 255 | }; |
| 256 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 257 | ErrorCode addToAtlas(GrResourceProvider*, GrDeferredUploadTarget*, |
| 258 | int width, int height, const void* image, AtlasLocator*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 259 | |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 260 | const GrSurfaceProxyView* getViews() const { return fViews; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 261 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 262 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 263 | |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 264 | bool hasID(const PlotLocator& plotLocator) { |
| 265 | if (!plotLocator.isValid()) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 266 | return false; |
| 267 | } |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame] | 268 | |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 269 | uint32_t plot = plotLocator.plotIndex(); |
| 270 | uint32_t page = plotLocator.pageIndex(); |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 271 | uint64_t plotGeneration = fPages[page].fPlotArray[plot]->genID(); |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 272 | uint64_t locatorGeneration = plotLocator.genID(); |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 273 | return plot < fNumPlots && page < fNumActivePages && plotGeneration == locatorGeneration; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 274 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 275 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 276 | /** To ensure the atlas does not evict a given entry, the client must set the last use token. */ |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 277 | void setLastUseToken(const AtlasLocator& atlasLocator, GrDeferredUploadToken token) { |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 278 | SkASSERT(this->hasID(atlasLocator.plotLocator())); |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 279 | uint32_t plotIdx = atlasLocator.plotIndex(); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 280 | SkASSERT(plotIdx < fNumPlots); |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 281 | uint32_t pageIdx = atlasLocator.pageIndex(); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 282 | SkASSERT(pageIdx < fNumActivePages); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 283 | Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get(); |
| 284 | this->makeMRU(plot, pageIdx); |
| 285 | plot->setLastUseToken(token); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 288 | uint32_t numActivePages() { return fNumActivePages; } |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 289 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 290 | /** |
| 291 | * 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] | 292 | * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future |
| 293 | * this is insufficient then we can move to a 64 bit int. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 294 | */ |
| 295 | class BulkUseTokenUpdater { |
| 296 | public: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 297 | BulkUseTokenUpdater() { |
| 298 | memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated)); |
| 299 | } |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 300 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 301 | : fPlotsToUpdate(that.fPlotsToUpdate) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 302 | memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated)); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 305 | bool add(const AtlasLocator& atlasLocator) { |
| 306 | int plotIdx = atlasLocator.plotIndex(); |
| 307 | int pageIdx = atlasLocator.pageIndex(); |
| 308 | if (this->find(pageIdx, plotIdx)) { |
Jim Van Verth | ba98b7d | 2018-12-05 12:33:43 -0500 | [diff] [blame] | 309 | return false; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 310 | } |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 311 | this->set(pageIdx, plotIdx); |
Jim Van Verth | ba98b7d | 2018-12-05 12:33:43 -0500 | [diff] [blame] | 312 | return true; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void reset() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 316 | fPlotsToUpdate.reset(); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 317 | memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated)); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 320 | struct PlotData { |
| 321 | PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {} |
| 322 | uint32_t fPageIndex; |
| 323 | uint32_t fPlotIndex; |
| 324 | }; |
| 325 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 326 | private: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 327 | bool find(int pageIdx, int index) const { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 328 | SkASSERT(index < kMaxPlots); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 329 | return (fPlotAlreadyUpdated[pageIdx] >> index) & 1; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 332 | void set(int pageIdx, int index) { |
| 333 | SkASSERT(!this->find(pageIdx, index)); |
| 334 | fPlotAlreadyUpdated[pageIdx] |= (1 << index); |
| 335 | fPlotsToUpdate.push_back(PlotData(pageIdx, index)); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 338 | static constexpr int kMinItems = 4; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 339 | SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate; |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 340 | uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages]; // TODO: increase this to uint64_t |
| 341 | // to allow more plots per page |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 342 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 343 | friend class GrDrawOpAtlas; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 344 | }; |
| 345 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 346 | void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 347 | int count = updater.fPlotsToUpdate.count(); |
| 348 | for (int i = 0; i < count; i++) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 349 | const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i]; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 350 | // it's possible we've added a plot to the updater and subsequently the plot's page |
| 351 | // was deleted -- so we check to prevent a crash |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 352 | if (pd.fPageIndex < fNumActivePages) { |
Jim Van Verth | 6ca9c6f | 2017-09-27 18:04:34 -0400 | [diff] [blame] | 353 | Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get(); |
| 354 | this->makeMRU(plot, pd.fPageIndex); |
| 355 | plot->setLastUseToken(token); |
| 356 | } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 359 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 360 | void compact(GrDeferredUploadToken startTokenForNextFlush); |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 361 | |
Robert Phillips | cd5099c | 2018-02-09 09:56:56 -0500 | [diff] [blame] | 362 | void instantiate(GrOnFlushResourceProvider*); |
| 363 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 364 | uint32_t maxPages() const { |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 365 | return fMaxPages; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 368 | int numAllocated_TestingOnly() const; |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 369 | void setMaxPages_TestingOnly(uint32_t maxPages); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 370 | |
| 371 | private: |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 372 | GrDrawOpAtlas(GrProxyProvider*, const GrBackendFormat& format, GrColorType, int width, |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 373 | int height, int plotWidth, int plotHeight, GenerationCounter* generationCounter, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 374 | AllowMultitexturing allowMultitexturing); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 375 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 376 | /** |
| 377 | * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots |
| 378 | * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its |
| 379 | * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e. |
| 380 | * there is no room for the new subimage according to the GrRectanizer), it can no longer be |
| 381 | * used unless the last use of the Plot has already been flushed through to the gpu. |
| 382 | */ |
| 383 | class Plot : public SkRefCnt { |
| 384 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 385 | |
| 386 | public: |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 387 | uint32_t pageIndex() const { return fPageIndex; } |
| 388 | |
| 389 | /** plotIndex() is a unique id for the plot relative to the owning GrAtlas and page. */ |
| 390 | uint32_t plotIndex() const { return fPlotIndex; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 391 | /** |
| 392 | * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know |
| 393 | * if a particular subimage is still present in the atlas. |
| 394 | */ |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 395 | uint64_t genID() const { return fGenID; } |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 396 | PlotLocator plotLocator() const { |
| 397 | SkASSERT(fPlotLocator.isValid()); |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame] | 398 | return fPlotLocator; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 399 | } |
| 400 | SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; }) |
| 401 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 402 | bool addSubImage(int width, int height, const void* image, GrIRect16* rect); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 403 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 404 | /** |
| 405 | * To manage the lifetime of a plot, we use two tokens. We use the last upload token to |
| 406 | * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to |
| 407 | * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We |
| 408 | * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use |
| 409 | * has already flushed through the gpu then we can reuse the plot. |
| 410 | */ |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 411 | GrDeferredUploadToken lastUploadToken() const { return fLastUpload; } |
| 412 | GrDeferredUploadToken lastUseToken() const { return fLastUse; } |
| 413 | void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; } |
| 414 | void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 415 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 416 | void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 417 | void resetRects(); |
| 418 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 419 | int flushesSinceLastUsed() { return fFlushesSinceLastUse; } |
| 420 | void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; } |
| 421 | void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; } |
| 422 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 423 | private: |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 424 | Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter, |
| 425 | int offX, int offY, int width, int height, GrColorType colorType); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 426 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 427 | ~Plot() override; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 428 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 429 | /** |
| 430 | * Create a clone of this plot. The cloned plot will take the place of the current plot in |
| 431 | * the atlas |
| 432 | */ |
| 433 | Plot* clone() const { |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 434 | return new Plot( |
| 435 | fPageIndex, fPlotIndex, fGenerationCounter, fX, fY, fWidth, fHeight, fColorType); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 438 | GrDeferredUploadToken fLastUpload; |
| 439 | GrDeferredUploadToken fLastUse; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 440 | // the number of flushes since this plot has been last used |
| 441 | int fFlushesSinceLastUse; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 442 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 443 | struct { |
| 444 | const uint32_t fPageIndex : 16; |
| 445 | const uint32_t fPlotIndex : 16; |
| 446 | }; |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 447 | GenerationCounter* const fGenerationCounter; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 448 | uint64_t fGenID; |
Robert Phillips | bf5bf74 | 2020-04-13 09:29:08 -0400 | [diff] [blame] | 449 | PlotLocator fPlotLocator; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 450 | unsigned char* fData; |
| 451 | const int fWidth; |
| 452 | const int fHeight; |
| 453 | const int fX; |
| 454 | const int fY; |
Herb Derby | 73c7587 | 2020-01-22 18:09:16 -0500 | [diff] [blame] | 455 | GrRectanizerSkyline fRectanizer; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 456 | const SkIPoint16 fOffset; // the offset of the plot in the backing texture |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 457 | const GrColorType fColorType; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 458 | const size_t fBytesPerPixel; |
| 459 | SkIRect fDirtyRect; |
| 460 | SkDEBUGCODE(bool fDirty); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 461 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 462 | friend class GrDrawOpAtlas; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 463 | |
| 464 | typedef SkRefCnt INHERITED; |
| 465 | }; |
| 466 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 467 | typedef SkTInternalLList<Plot> PlotList; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 468 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 469 | inline bool updatePlot(GrDeferredUploadTarget*, AtlasLocator*, Plot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 470 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 471 | inline void makeMRU(Plot* plot, int pageIdx) { |
| 472 | if (fPages[pageIdx].fPlotList.head() == plot) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 476 | fPages[pageIdx].fPlotList.remove(plot); |
| 477 | fPages[pageIdx].fPlotList.addToHead(plot); |
| 478 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 479 | // No MRU update for pages -- since we will always try to add from |
| 480 | // the front and remove from the back there is no need for MRU. |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 481 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 482 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 483 | bool uploadToPage(const GrCaps&, unsigned int pageIdx, GrDeferredUploadTarget*, |
| 484 | int width, int height, const void* image, AtlasLocator*); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 485 | |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 486 | bool createPages(GrProxyProvider*, GenerationCounter*); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 487 | bool activateNewPage(GrResourceProvider*); |
| 488 | void deactivateLastPage(); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 489 | |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame] | 490 | void processEviction(PlotLocator); |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 491 | inline void processEvictionAndResetRects(Plot* plot) { |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame] | 492 | this->processEviction(plot->plotLocator()); |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 493 | plot->resetRects(); |
| 494 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 495 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 496 | GrBackendFormat fFormat; |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 497 | GrColorType fColorType; |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 498 | int fTextureWidth; |
| 499 | int fTextureHeight; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 500 | int fPlotWidth; |
| 501 | int fPlotHeight; |
Jim Van Verth | 06f593c | 2018-02-20 11:30:10 -0500 | [diff] [blame] | 502 | unsigned int fNumPlots; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 503 | |
Herb Derby | 0ef780b | 2020-01-24 15:57:11 -0500 | [diff] [blame] | 504 | GenerationCounter* const fGenerationCounter; |
| 505 | uint64_t fAtlasGeneration; |
| 506 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 507 | // nextTokenToFlush() value at the end of the previous flush |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 508 | GrDeferredUploadToken fPrevFlushToken; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 509 | |
Jim Van Verth | 77eb96d | 2020-03-18 12:32:34 -0400 | [diff] [blame] | 510 | // the number of flushes since this atlas has been last used |
| 511 | int fFlushesSinceLastUse; |
| 512 | |
Herb Derby | 1a496c5 | 2020-01-22 17:26:56 -0500 | [diff] [blame] | 513 | std::vector<EvictionCallback*> fEvictionCallbacks; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 514 | |
| 515 | struct Page { |
| 516 | // allocated array of Plots |
| 517 | std::unique_ptr<sk_sp<Plot>[]> fPlotArray; |
| 518 | // LRU list of Plots (MRU at head - LRU at tail) |
| 519 | PlotList fPlotList; |
| 520 | }; |
| 521 | // proxies kept separate to make it easier to pass them up to client |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 522 | GrSurfaceProxyView fViews[kMaxMultitexturePages]; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 523 | Page fPages[kMaxMultitexturePages]; |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 524 | uint32_t fMaxPages; |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 525 | |
| 526 | uint32_t fNumActivePages; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 527 | }; |
| 528 | |
Herb Derby | bbf5fb5 | 2018-10-15 16:39:39 -0400 | [diff] [blame] | 529 | // There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 530 | // general, the A8 dimensions are 2x the 565 and ARGB dimensions with the constraint that an atlas |
Herb Derby | bbf5fb5 | 2018-10-15 16:39:39 -0400 | [diff] [blame] | 531 | // size will always contain at least one plot. Since the ARGB atlas takes the most space, its |
| 532 | // dimensions are used to size the other two atlases. |
| 533 | class GrDrawOpAtlasConfig { |
| 534 | public: |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 535 | // The capabilities of the GPU define maxTextureSize. The client provides maxBytes, and this |
| 536 | // represents the largest they want a single atlas texture to be. Due to multitexturing, we |
| 537 | // may expand temporarily to use more space as needed. |
| 538 | GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes); |
Herb Derby | bbf5fb5 | 2018-10-15 16:39:39 -0400 | [diff] [blame] | 539 | |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 540 | // For testing only - make minimum sized atlases -- a single plot for ARGB, four for A8 |
| 541 | GrDrawOpAtlasConfig() : GrDrawOpAtlasConfig(kMaxAtlasDim, 0) {} |
Herb Derby | bbf5fb5 | 2018-10-15 16:39:39 -0400 | [diff] [blame] | 542 | |
Herb Derby | 15d9ef2 | 2018-10-18 13:41:32 -0400 | [diff] [blame] | 543 | SkISize atlasDimensions(GrMaskFormat type) const; |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 544 | SkISize plotDimensions(GrMaskFormat type) const; |
Herb Derby | bbf5fb5 | 2018-10-15 16:39:39 -0400 | [diff] [blame] | 545 | |
| 546 | private: |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 547 | // On some systems texture coordinates are represented using half-precision floating point, |
| 548 | // which limits the largest atlas dimensions to 2048x2048. |
| 549 | // For simplicity we'll use this constraint for all of our atlas textures. |
| 550 | // This can be revisited later if we need larger atlases. |
| 551 | static constexpr int kMaxAtlasDim = 2048; |
Herb Derby | bbf5fb5 | 2018-10-15 16:39:39 -0400 | [diff] [blame] | 552 | |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 553 | SkISize fARGBDimensions; |
| 554 | int fMaxTextureSize; |
Herb Derby | bbf5fb5 | 2018-10-15 16:39:39 -0400 | [diff] [blame] | 555 | }; |
| 556 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 557 | #endif |