blob: 448d8475d099d1280b69169f0c722879b20eee44 [file] [log] [blame]
joshualitt5bf99f12015-03-13 11:47:42 -07001/*
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 Salomon2ee084e2016-12-16 18:59:19 -05008#ifndef GrDrawOpAtlas_DEFINED
9#define GrDrawOpAtlas_DEFINED
joshualitt5bf99f12015-03-13 11:47:42 -070010
Herb Derby3c4d5332018-09-07 15:27:57 -040011#include <cmath>
Herb Derby1a496c52020-01-22 17:26:56 -050012#include <vector>
Herb Derby3c4d5332018-09-07 15:27:57 -040013
Robert Phillips51b3e602020-04-09 12:48:50 -040014#include "include/gpu/GrBackendSurface.h"
15#include "include/private/SkTArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkIPoint16.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040017#include "src/core/SkTInternalLList.h"
Robert Phillips51b3e602020-04-09 12:48:50 -040018#include "src/gpu/GrDeferredUpload.h"
Herb Derby73c75872020-01-22 18:09:16 -050019#include "src/gpu/GrRectanizerSkyline.h"
Robert Phillips51b3e602020-04-09 12:48:50 -040020#include "src/gpu/GrSurfaceProxyView.h"
21#include "src/gpu/geometry/GrRect.h"
joshualittddd22d82016-02-16 06:47:52 -080022
Robert Phillipscd5099c2018-02-09 09:56:56 -050023class GrOnFlushResourceProvider;
Robert Phillips51b3e602020-04-09 12:48:50 -040024class GrProxyProvider;
25class GrResourceProvider;
26class GrTextureProxy;
joshualittda04e0e2015-08-19 08:16:43 -070027
Brian Salomon2ee084e2016-12-16 18:59:19 -050028/**
Jim Van Verth106b5c42017-09-26 12:45:29 -040029 * 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 Salomon29b60c92017-10-31 14:42:10 -040032 * "ASAP" mode until it is impossible to add data without overwriting texels read by draws that
Jim Van Verth106b5c42017-09-26 12:45:29 -040033 * 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 Salomon2ee084e2016-12-16 18:59:19 -050052 */
53class GrDrawOpAtlas {
joshualitt5bf99f12015-03-13 11:47:42 -070054public:
Brian Salomon9f545bc2017-11-06 10:36:57 -050055 /** Is the atlas allowed to use more than one texture? */
56 enum class AllowMultitexturing : bool { kNo, kYes };
57
Robert Phillipsbf5bf742020-04-13 09:29:08 -040058 // 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 Derbybbf5fb52018-10-15 16:39:39 -040063
Brian Salomon2ee084e2016-12-16 18:59:19 -050064 /**
Herb Derby4d721712020-01-24 14:31:16 -050065 * 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 Phillipsbf5bf742020-04-13 09:29:08 -040069 * format/atlasGeneration/page/plot/plotGeneration/rect
70 *
71 * TODO: Remove the small path renderer's use of the PlotLocator for eviction.
Brian Salomon2ee084e2016-12-16 18:59:19 -050072 */
Robert Phillipsbf5bf742020-04-13 09:29:08 -040073 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 Phillips6507e632020-08-07 14:35:56 -040090 void makeInvalid() {
91 fGenID = 0;
92 fPlotIndex = 0;
93 fPageIndex = 0;
94 }
95
Robert Phillipsbf5bf742020-04-13 09:29:08 -040096 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
joshualitt7c3a2f82015-03-31 13:32:05 -0700111 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700112
Robert Phillips6d3bc292020-04-06 10:29:28 -0400113 class AtlasLocator {
114 public:
Herb Derby7491e8b2020-08-14 15:28:53 -0400115 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 Phillips6d3bc292020-04-06 10:29:28 -0400123
Robert Phillips6507e632020-08-07 14:35:56 -0400124 void invalidatePlotLocator() { fPlotLocator.makeInvalid(); }
125
Robert Phillips6d3bc292020-04-06 10:29:28 -0400126 // TODO: Remove the small path renderer's use of this for eviction
Robert Phillips51b3e602020-04-09 12:48:50 -0400127 PlotLocator plotLocator() const { return fPlotLocator; }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400128
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400129 uint32_t pageIndex() const { return fPlotLocator.pageIndex(); }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400130
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400131 uint32_t plotIndex() const { return fPlotLocator.plotIndex(); }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400132
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400133 uint64_t genID() const { return fPlotLocator.genID(); }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400134
Herb Derby71984a42020-07-31 16:08:41 -0400135 void insetSrc(int padding) {
136 fRect.fLeft += padding;
137 fRect.fTop += padding;
138 fRect.fRight -= padding;
139 fRect.fBottom -= padding;
140 }
141
Herb Derby70df33d2020-08-04 15:34:58 -0400142 GrIRect16 rect() const { return fRect; }
143
Herb Derby06296c62020-08-28 13:42:31 -0400144 void updatePlotLocator(PlotLocator p) {
145 fPlotLocator = p;
146 }
147
148 void updateRect(GrIRect16 rect) {
149 fRect = rect;
150 }
151
Robert Phillips6d3bc292020-04-06 10:29:28 -0400152 private:
Herb Derbyc27d5352020-08-12 13:58:34 -0400153 PlotLocator fPlotLocator{0, 0, 0};
Herb Derby71984a42020-07-31 16:08:41 -0400154
Herb Derby1318e452020-08-03 14:38:10 -0400155 // The inset padded bounds in the atlas.
Robert Phillips51b3e602020-04-09 12:48:50 -0400156 GrIRect16 fRect{0, 0, 0, 0};
Robert Phillips6d3bc292020-04-06 10:29:28 -0400157 };
158
Brian Salomon2ee084e2016-12-16 18:59:19 -0500159 /**
Herb Derby1a496c52020-01-22 17:26:56 -0500160 * An interface for eviction callbacks. Whenever GrDrawOpAtlas evicts a
Herb Derby4d721712020-01-24 14:31:16 -0500161 * specific PlotLocator, it will call all of the registered listeners so they can process the
Brian Salomon2ee084e2016-12-16 18:59:19 -0500162 * eviction.
163 */
Herb Derby1a496c52020-01-22 17:26:56 -0500164 class EvictionCallback {
165 public:
166 virtual ~EvictionCallback() = default;
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400167 virtual void evict(PlotLocator) = 0;
Herb Derby1a496c52020-01-22 17:26:56 -0500168 };
joshualitt5bf99f12015-03-13 11:47:42 -0700169
Robert Phillips256c37b2017-03-01 14:32:46 -0500170 /**
Herb Derby0ef780b2020-01-24 15:57:11 -0500171 * Keep track of generation number for Atlases and Plots.
172 */
173 class GenerationCounter {
174 public:
175 static constexpr uint64_t kInvalidGeneration = 0;
176 uint64_t next() {
177 return fGeneration++;
178 }
179
180 private:
181 uint64_t fGeneration{1};
182 };
183
184 /**
Robert Phillips256c37b2017-03-01 14:32:46 -0500185 * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
186 * should only be used inside of GrMeshDrawOp::onPrepareDraws.
Robert Phillips42dda082019-05-14 13:29:45 -0400187 * @param GrColorType The colorType which this atlas will store
Robert Phillips256c37b2017-03-01 14:32:46 -0500188 * @param width width in pixels of the atlas
189 * @param height height in pixels of the atlas
190 * @param numPlotsX The number of plots the atlas should be broken up into in the X
191 * direction
192 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
193 * direction
Herb Derby0ef780b2020-01-24 15:57:11 -0500194 * @param atlasGeneration a pointer to the context's generation counter.
Brian Salomon9f545bc2017-11-06 10:36:57 -0500195 * @param allowMultitexturing Can the atlas use more than one texture.
Herb Derby1a496c52020-01-22 17:26:56 -0500196 * @param evictor A pointer to an eviction callback class.
197 *
Robert Phillips256c37b2017-03-01 14:32:46 -0500198 * @return An initialized GrDrawOpAtlas, or nullptr if creation fails
199 */
Greg Daniel4065d452018-11-16 15:43:41 -0500200 static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*,
201 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -0400202 GrColorType,
Robert Phillips4bc70112018-03-01 10:24:02 -0500203 int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500204 int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -0500205 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500206 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -0500207 EvictionCallback* evictor);
joshualitt5bf99f12015-03-13 11:47:42 -0700208
Brian Salomon2ee084e2016-12-16 18:59:19 -0500209 /**
Jim Van Verthfb395102020-02-03 10:11:19 -0500210 * Packs a texture atlas page index into the uint16 texture coordinates.
211 * @param u U texture coordinate
212 * @param v V texture coordinate
213 * @param pageIndex index of the texture these coordinates apply to.
214 Must be in the range [0, 3].
215 * @return The new u and v coordinates with the packed value
216 */
Herb Derby7491e8b2020-08-14 15:28:53 -0400217 static std::pair<uint16_t, uint16_t> PackIndexInTexCoords(
218 uint16_t u, uint16_t v, int pageIndex) {
Herb Derby6fd391a2020-08-28 16:07:28 -0400219 // Pack the two bits of page in bits 13 and 14 of u.
Herb Derby532a2642020-08-26 16:58:41 -0400220 SkASSERT(0 <= pageIndex && pageIndex < 4);
Herb Derby6fd391a2020-08-28 16:07:28 -0400221 u &= 0x1FFF;
222 u |= pageIndex << 13;
Herb Derby7491e8b2020-08-14 15:28:53 -0400223 return std::make_pair(u, v);
224 }
225
Jim Van Verthfb395102020-02-03 10:11:19 -0500226 /**
227 * Unpacks a texture atlas page index from uint16 texture coordinates.
228 * @param u Packed U texture coordinate
229 * @param v Packed V texture coordinate
230 * @return The unpacked u and v coordinates with the page index.
231 */
232 static std::tuple<uint16_t, uint16_t, int> UnpackIndexFromTexCoords(uint16_t u, uint16_t v);
233
234 /**
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500235 * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns
236 * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if
237 * the subimage cannot fit in the atlas without overwriting texels that will be read in the
238 * current draw. This indicates that the op should end its current draw and begin another
239 * before adding more data. Upon success, an upload of the provided image data will have
240 * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode.
241 * Successive uploads in either mode may be consolidated.
242 * 'kError' will be returned when some unrecoverable error was encountered while trying to
243 * add the subimage. In this case the op being created should be discarded.
244 *
Brian Salomon2ee084e2016-12-16 18:59:19 -0500245 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
246 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
247 * addToAtlas might cause the previous data to be overwritten before it has been read.
248 */
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500249
250 enum class ErrorCode {
251 kError,
252 kSucceeded,
253 kTryAgain
254 };
255
Robert Phillips6d3bc292020-04-06 10:29:28 -0400256 ErrorCode addToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
257 int width, int height, const void* image, AtlasLocator*);
joshualitt5bf99f12015-03-13 11:47:42 -0700258
Greg Daniel9715b6c2019-12-10 15:03:10 -0500259 const GrSurfaceProxyView* getViews() const { return fViews; }
joshualitt5bf99f12015-03-13 11:47:42 -0700260
joshualitt7c3a2f82015-03-31 13:32:05 -0700261 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -0800262
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400263 bool hasID(const PlotLocator& plotLocator) {
264 if (!plotLocator.isValid()) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500265 return false;
266 }
Herb Derby4d721712020-01-24 14:31:16 -0500267
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400268 uint32_t plot = plotLocator.plotIndex();
269 uint32_t page = plotLocator.pageIndex();
Herb Derby0ef780b2020-01-24 15:57:11 -0500270 uint64_t plotGeneration = fPages[page].fPlotArray[plot]->genID();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400271 uint64_t locatorGeneration = plotLocator.genID();
Herb Derby0ef780b2020-01-24 15:57:11 -0500272 return plot < fNumPlots && page < fNumActivePages && plotGeneration == locatorGeneration;
joshualitt5df175e2015-11-18 13:37:54 -0800273 }
joshualittb4c507e2015-04-08 08:07:59 -0700274
Brian Salomon2ee084e2016-12-16 18:59:19 -0500275 /** To ensure the atlas does not evict a given entry, the client must set the last use token. */
Robert Phillips6d3bc292020-04-06 10:29:28 -0400276 void setLastUseToken(const AtlasLocator& atlasLocator, GrDeferredUploadToken token) {
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400277 SkASSERT(this->hasID(atlasLocator.plotLocator()));
Robert Phillips6d3bc292020-04-06 10:29:28 -0400278 uint32_t plotIdx = atlasLocator.plotIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400279 SkASSERT(plotIdx < fNumPlots);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400280 uint32_t pageIdx = atlasLocator.pageIndex();
Robert Phillips4bc70112018-03-01 10:24:02 -0500281 SkASSERT(pageIdx < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400282 Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
283 this->makeMRU(plot, pageIdx);
284 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800285 }
286
Robert Phillips4bc70112018-03-01 10:24:02 -0500287 uint32_t numActivePages() { return fNumActivePages; }
Jim Van Vertha950b632017-09-12 11:54:11 -0400288
Brian Salomon2ee084e2016-12-16 18:59:19 -0500289 /**
290 * A class which can be handed back to GrDrawOpAtlas for updating last use tokens in bulk. The
Jim Van Vertha950b632017-09-12 11:54:11 -0400291 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
292 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700293 */
294 class BulkUseTokenUpdater {
295 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400296 BulkUseTokenUpdater() {
297 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
298 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700299 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Robert Phillips6d3bc292020-04-06 10:29:28 -0400300 : fPlotsToUpdate(that.fPlotsToUpdate) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400301 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700302 }
303
Robert Phillips6d3bc292020-04-06 10:29:28 -0400304 bool add(const AtlasLocator& atlasLocator) {
305 int plotIdx = atlasLocator.plotIndex();
306 int pageIdx = atlasLocator.pageIndex();
307 if (this->find(pageIdx, plotIdx)) {
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500308 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700309 }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400310 this->set(pageIdx, plotIdx);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500311 return true;
joshualittb4c507e2015-04-08 08:07:59 -0700312 }
313
314 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700315 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400316 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700317 }
318
Jim Van Vertha950b632017-09-12 11:54:11 -0400319 struct PlotData {
320 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
321 uint32_t fPageIndex;
322 uint32_t fPlotIndex;
323 };
324
joshualittb4c507e2015-04-08 08:07:59 -0700325 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400326 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700327 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400328 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700329 }
330
Jim Van Vertha950b632017-09-12 11:54:11 -0400331 void set(int pageIdx, int index) {
332 SkASSERT(!this->find(pageIdx, index));
333 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
334 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700335 }
336
Jim Van Vertha950b632017-09-12 11:54:11 -0400337 static constexpr int kMinItems = 4;
Jim Van Vertha950b632017-09-12 11:54:11 -0400338 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
Jim Van Verthf6206f92018-12-14 08:22:24 -0500339 uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages]; // TODO: increase this to uint64_t
340 // to allow more plots per page
joshualittb4c507e2015-04-08 08:07:59 -0700341
Brian Salomon2ee084e2016-12-16 18:59:19 -0500342 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700343 };
344
Brian Salomon943ed792017-10-30 09:37:55 -0400345 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800346 int count = updater.fPlotsToUpdate.count();
347 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400348 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400349 // it's possible we've added a plot to the updater and subsequently the plot's page
350 // was deleted -- so we check to prevent a crash
Robert Phillips4bc70112018-03-01 10:24:02 -0500351 if (pd.fPageIndex < fNumActivePages) {
Jim Van Verth6ca9c6f2017-09-27 18:04:34 -0400352 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
353 this->makeMRU(plot, pd.fPageIndex);
354 plot->setLastUseToken(token);
355 }
joshualitt5df175e2015-11-18 13:37:54 -0800356 }
357 }
joshualittb4c507e2015-04-08 08:07:59 -0700358
Brian Salomon943ed792017-10-30 09:37:55 -0400359 void compact(GrDeferredUploadToken startTokenForNextFlush);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400360
Robert Phillipscd5099c2018-02-09 09:56:56 -0500361 void instantiate(GrOnFlushResourceProvider*);
362
Brian Salomon9f545bc2017-11-06 10:36:57 -0500363 uint32_t maxPages() const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500364 return fMaxPages;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500365 }
366
Robert Phillips4bc70112018-03-01 10:24:02 -0500367 int numAllocated_TestingOnly() const;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500368 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillips4bc70112018-03-01 10:24:02 -0500369
370private:
Robert Phillips42dda082019-05-14 13:29:45 -0400371 GrDrawOpAtlas(GrProxyProvider*, const GrBackendFormat& format, GrColorType, int width,
Herb Derby0ef780b2020-01-24 15:57:11 -0500372 int height, int plotWidth, int plotHeight, GenerationCounter* generationCounter,
Greg Daniel4065d452018-11-16 15:43:41 -0500373 AllowMultitexturing allowMultitexturing);
Robert Phillips256c37b2017-03-01 14:32:46 -0500374
Brian Salomon2ee084e2016-12-16 18:59:19 -0500375 /**
376 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
377 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
378 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
379 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
380 * used unless the last use of the Plot has already been flushed through to the gpu.
381 */
382 class Plot : public SkRefCnt {
383 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800384
385 public:
Robert Phillips6d3bc292020-04-06 10:29:28 -0400386 uint32_t pageIndex() const { return fPageIndex; }
387
388 /** plotIndex() is a unique id for the plot relative to the owning GrAtlas and page. */
389 uint32_t plotIndex() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500390 /**
391 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
392 * if a particular subimage is still present in the atlas.
393 */
joshualitt5df175e2015-11-18 13:37:54 -0800394 uint64_t genID() const { return fGenID; }
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400395 PlotLocator plotLocator() const {
396 SkASSERT(fPlotLocator.isValid());
Herb Derby4d721712020-01-24 14:31:16 -0500397 return fPlotLocator;
joshualitt5df175e2015-11-18 13:37:54 -0800398 }
399 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
400
Herb Derby06296c62020-08-28 13:42:31 -0400401 bool addSubImage(int width, int height, const void* image, AtlasLocator* atlasLocator);
joshualitt5df175e2015-11-18 13:37:54 -0800402
Brian Salomon2ee084e2016-12-16 18:59:19 -0500403 /**
404 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
405 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
406 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
407 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
408 * has already flushed through the gpu then we can reuse the plot.
409 */
Brian Salomon943ed792017-10-30 09:37:55 -0400410 GrDeferredUploadToken lastUploadToken() const { return fLastUpload; }
411 GrDeferredUploadToken lastUseToken() const { return fLastUse; }
412 void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; }
413 void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800414
Brian Salomon943ed792017-10-30 09:37:55 -0400415 void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800416 void resetRects();
417
Jim Van Verth106b5c42017-09-26 12:45:29 -0400418 int flushesSinceLastUsed() { return fFlushesSinceLastUse; }
419 void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; }
420 void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; }
421
joshualitt5df175e2015-11-18 13:37:54 -0800422 private:
Herb Derby0ef780b2020-01-24 15:57:11 -0500423 Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
424 int offX, int offY, int width, int height, GrColorType colorType);
joshualitt5df175e2015-11-18 13:37:54 -0800425
Brian Salomon2ee084e2016-12-16 18:59:19 -0500426 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800427
Brian Salomon2ee084e2016-12-16 18:59:19 -0500428 /**
429 * Create a clone of this plot. The cloned plot will take the place of the current plot in
430 * the atlas
431 */
432 Plot* clone() const {
Herb Derby0ef780b2020-01-24 15:57:11 -0500433 return new Plot(
434 fPageIndex, fPlotIndex, fGenerationCounter, fX, fY, fWidth, fHeight, fColorType);
joshualitt5df175e2015-11-18 13:37:54 -0800435 }
436
Brian Salomon943ed792017-10-30 09:37:55 -0400437 GrDeferredUploadToken fLastUpload;
438 GrDeferredUploadToken fLastUse;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400439 // the number of flushes since this plot has been last used
440 int fFlushesSinceLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800441
Jim Van Vertha950b632017-09-12 11:54:11 -0400442 struct {
443 const uint32_t fPageIndex : 16;
444 const uint32_t fPlotIndex : 16;
445 };
Herb Derby0ef780b2020-01-24 15:57:11 -0500446 GenerationCounter* const fGenerationCounter;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500447 uint64_t fGenID;
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400448 PlotLocator fPlotLocator;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500449 unsigned char* fData;
450 const int fWidth;
451 const int fHeight;
452 const int fX;
453 const int fY;
Herb Derby73c75872020-01-22 18:09:16 -0500454 GrRectanizerSkyline fRectanizer;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500455 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
Robert Phillips42dda082019-05-14 13:29:45 -0400456 const GrColorType fColorType;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500457 const size_t fBytesPerPixel;
458 SkIRect fDirtyRect;
459 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800460
Brian Salomon2ee084e2016-12-16 18:59:19 -0500461 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800462
463 typedef SkRefCnt INHERITED;
464 };
465
Brian Salomon2ee084e2016-12-16 18:59:19 -0500466 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800467
Robert Phillips6d3bc292020-04-06 10:29:28 -0400468 inline bool updatePlot(GrDeferredUploadTarget*, AtlasLocator*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700469
Jim Van Vertha950b632017-09-12 11:54:11 -0400470 inline void makeMRU(Plot* plot, int pageIdx) {
471 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800472 return;
473 }
474
Jim Van Vertha950b632017-09-12 11:54:11 -0400475 fPages[pageIdx].fPlotList.remove(plot);
476 fPages[pageIdx].fPlotList.addToHead(plot);
477
Jim Van Verth106b5c42017-09-26 12:45:29 -0400478 // No MRU update for pages -- since we will always try to add from
479 // the front and remove from the back there is no need for MRU.
joshualitt5df175e2015-11-18 13:37:54 -0800480 }
joshualitt5bf99f12015-03-13 11:47:42 -0700481
Robert Phillips6d3bc292020-04-06 10:29:28 -0400482 bool uploadToPage(const GrCaps&, unsigned int pageIdx, GrDeferredUploadTarget*,
483 int width, int height, const void* image, AtlasLocator*);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500484
Herb Derby0ef780b2020-01-24 15:57:11 -0500485 bool createPages(GrProxyProvider*, GenerationCounter*);
Robert Phillips4bc70112018-03-01 10:24:02 -0500486 bool activateNewPage(GrResourceProvider*);
487 void deactivateLastPage();
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400488
Herb Derby4d721712020-01-24 14:31:16 -0500489 void processEviction(PlotLocator);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400490 inline void processEvictionAndResetRects(Plot* plot) {
Herb Derby4d721712020-01-24 14:31:16 -0500491 this->processEviction(plot->plotLocator());
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400492 plot->resetRects();
493 }
joshualitt5bf99f12015-03-13 11:47:42 -0700494
Greg Daniel4065d452018-11-16 15:43:41 -0500495 GrBackendFormat fFormat;
Robert Phillips42dda082019-05-14 13:29:45 -0400496 GrColorType fColorType;
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400497 int fTextureWidth;
498 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500499 int fPlotWidth;
500 int fPlotHeight;
Jim Van Verth06f593c2018-02-20 11:30:10 -0500501 unsigned int fNumPlots;
robertphillips2b0536f2015-11-06 14:10:42 -0800502
Herb Derby0ef780b2020-01-24 15:57:11 -0500503 GenerationCounter* const fGenerationCounter;
504 uint64_t fAtlasGeneration;
505
Jim Van Verth106b5c42017-09-26 12:45:29 -0400506 // nextTokenToFlush() value at the end of the previous flush
Brian Salomon943ed792017-10-30 09:37:55 -0400507 GrDeferredUploadToken fPrevFlushToken;
joshualitt5bf99f12015-03-13 11:47:42 -0700508
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400509 // the number of flushes since this atlas has been last used
510 int fFlushesSinceLastUse;
511
Herb Derby1a496c52020-01-22 17:26:56 -0500512 std::vector<EvictionCallback*> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400513
514 struct Page {
515 // allocated array of Plots
516 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
517 // LRU list of Plots (MRU at head - LRU at tail)
518 PlotList fPlotList;
519 };
520 // proxies kept separate to make it easier to pass them up to client
Greg Daniel9715b6c2019-12-10 15:03:10 -0500521 GrSurfaceProxyView fViews[kMaxMultitexturePages];
Brian Salomon9f545bc2017-11-06 10:36:57 -0500522 Page fPages[kMaxMultitexturePages];
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500523 uint32_t fMaxPages;
Robert Phillips4bc70112018-03-01 10:24:02 -0500524
525 uint32_t fNumActivePages;
Herb Derby06296c62020-08-28 13:42:31 -0400526
527 SkDEBUGCODE(void validate(const AtlasLocator& atlasLocator) const;)
joshualitt5bf99f12015-03-13 11:47:42 -0700528};
529
Herb Derbybbf5fb52018-10-15 16:39:39 -0400530// There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In
Jim Van Verthf6206f92018-12-14 08:22:24 -0500531// general, the A8 dimensions are 2x the 565 and ARGB dimensions with the constraint that an atlas
Herb Derbybbf5fb52018-10-15 16:39:39 -0400532// size will always contain at least one plot. Since the ARGB atlas takes the most space, its
533// dimensions are used to size the other two atlases.
534class GrDrawOpAtlasConfig {
535public:
Jim Van Verthf6206f92018-12-14 08:22:24 -0500536 // The capabilities of the GPU define maxTextureSize. The client provides maxBytes, and this
537 // represents the largest they want a single atlas texture to be. Due to multitexturing, we
538 // may expand temporarily to use more space as needed.
539 GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes);
Herb Derbybbf5fb52018-10-15 16:39:39 -0400540
Jim Van Verthf6206f92018-12-14 08:22:24 -0500541 // For testing only - make minimum sized atlases -- a single plot for ARGB, four for A8
542 GrDrawOpAtlasConfig() : GrDrawOpAtlasConfig(kMaxAtlasDim, 0) {}
Herb Derbybbf5fb52018-10-15 16:39:39 -0400543
Herb Derby15d9ef22018-10-18 13:41:32 -0400544 SkISize atlasDimensions(GrMaskFormat type) const;
Jim Van Verthf6206f92018-12-14 08:22:24 -0500545 SkISize plotDimensions(GrMaskFormat type) const;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400546
547private:
Jim Van Verthf6206f92018-12-14 08:22:24 -0500548 // On some systems texture coordinates are represented using half-precision floating point,
549 // which limits the largest atlas dimensions to 2048x2048.
550 // For simplicity we'll use this constraint for all of our atlas textures.
551 // This can be revisited later if we need larger atlases.
552 static constexpr int kMaxAtlasDim = 2048;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400553
Jim Van Verthf6206f92018-12-14 08:22:24 -0500554 SkISize fARGBDimensions;
555 int fMaxTextureSize;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400556};
557
joshualitt5bf99f12015-03-13 11:47:42 -0700558#endif