blob: 48551a3c899bd8d0dddfcc734eec4d34a05147a3 [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>
12
Herbert Derbya0f59352018-11-05 11:48:00 -050013#include "SkGlyphRunPainter.h"
Mike Reed1fda0242018-04-04 15:39:46 -040014#include "SkIPoint16.h"
Herb Derby3c4d5332018-09-07 15:27:57 -040015#include "SkSize.h"
joshualitt5bf99f12015-03-13 11:47:42 -070016#include "SkTDArray.h"
17#include "SkTInternalLList.h"
18
Brian Salomon89527432016-12-16 09:52:16 -050019#include "ops/GrDrawOp.h"
joshualittddd22d82016-02-16 06:47:52 -080020
Robert Phillipscd5099c2018-02-09 09:56:56 -050021class GrOnFlushResourceProvider;
joshualitt5bf99f12015-03-13 11:47:42 -070022class GrRectanizer;
23
joshualittda04e0e2015-08-19 08:16:43 -070024
Brian Salomon2ee084e2016-12-16 18:59:19 -050025/**
Jim Van Verth106b5c42017-09-26 12:45:29 -040026 * This class manages one or more atlas textures on behalf of GrDrawOps. The draw ops that use the
27 * atlas perform texture uploads when preparing their draws during flush. The class provides
28 * facilities for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in
Brian Salomon29b60c92017-10-31 14:42:10 -040029 * "ASAP" mode until it is impossible to add data without overwriting texels read by draws that
Jim Van Verth106b5c42017-09-26 12:45:29 -040030 * have not yet executed on the gpu. At that point, the atlas will attempt to allocate a new
31 * atlas texture (or "page") of the same size, up to a maximum number of textures, and upload
32 * to that texture. If that's not possible, the uploads are performed "inline" between draws. If a
33 * single draw would use enough subimage space to overflow the atlas texture then the atlas will
34 * fail to add a subimage. This gives the op the chance to end the draw and begin a new one.
35 * Additional uploads will then succeed in inline mode.
36 *
37 * When the atlas has multiple pages, new uploads are prioritized to the lower index pages, i.e.,
38 * it will try to upload to page 0 before page 1 or 2. To keep the atlas from continually using
39 * excess space, periodic garbage collection is needed to shift data from the higher index pages to
40 * the lower ones, and then eventually remove any pages that are no longer in use. "In use" is
41 * determined by using the GrDrawUploadToken system: After a flush each subarea of the page
42 * is checked to see whether it was used in that flush; if it is not, a counter is incremented.
43 * Once that counter reaches a threshold that subarea is considered to be no longer in use.
44 *
45 * Garbage collection is initiated by the GrDrawOpAtlas's client via the compact() method. One
46 * solution is to make the client a subclass of GrOnFlushCallbackObject, register it with the
47 * GrContext via addOnFlushCallbackObject(), and the client's postFlush() method calls compact()
48 * and passes in the given GrDrawUploadToken.
Brian Salomon2ee084e2016-12-16 18:59:19 -050049 */
50class GrDrawOpAtlas {
Brian Salomon9f545bc2017-11-06 10:36:57 -050051private:
52 static constexpr auto kMaxMultitexturePages = 4;
53
Herb Derby3c4d5332018-09-07 15:27:57 -040054
joshualitt5bf99f12015-03-13 11:47:42 -070055public:
Brian Salomon9f545bc2017-11-06 10:36:57 -050056 /** Is the atlas allowed to use more than one texture? */
57 enum class AllowMultitexturing : bool { kNo, kYes };
58
Herb Derbybbf5fb52018-10-15 16:39:39 -040059 static constexpr int kMaxPlots = 32;
60
Brian Salomon2ee084e2016-12-16 18:59:19 -050061 /**
62 * An AtlasID is an opaque handle which callers can use to determine if the atlas contains
63 * a specific piece of data.
64 */
joshualitt8db6fdc2015-07-31 08:25:07 -070065 typedef uint64_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070066 static const uint32_t kInvalidAtlasID = 0;
67 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070068
Brian Salomon2ee084e2016-12-16 18:59:19 -050069 /**
70 * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a
71 * specific AtlasID, it will call all of the registered listeners so they can process the
72 * eviction.
73 */
74 typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*);
joshualitt5bf99f12015-03-13 11:47:42 -070075
Robert Phillips256c37b2017-03-01 14:32:46 -050076 /**
77 * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
78 * should only be used inside of GrMeshDrawOp::onPrepareDraws.
79 * @param GrPixelConfig The pixel config which this atlas will store
80 * @param width width in pixels of the atlas
81 * @param height height in pixels of the atlas
82 * @param numPlotsX The number of plots the atlas should be broken up into in the X
83 * direction
84 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
85 * direction
Brian Salomon9f545bc2017-11-06 10:36:57 -050086 * @param allowMultitexturing Can the atlas use more than one texture.
Robert Phillips256c37b2017-03-01 14:32:46 -050087 * @param func An eviction function which will be called whenever the atlas has to
88 * evict data
Brian Salomon9f545bc2017-11-06 10:36:57 -050089 * @param data User supplied data which will be passed into func whenever an
Robert Phillips256c37b2017-03-01 14:32:46 -050090 * eviction occurs
91 * @return An initialized GrDrawOpAtlas, or nullptr if creation fails
92 */
Greg Daniel4065d452018-11-16 15:43:41 -050093 static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*,
94 const GrBackendFormat& format,
95 GrPixelConfig,
Robert Phillips4bc70112018-03-01 10:24:02 -050096 int width, int height,
Robert Phillips256c37b2017-03-01 14:32:46 -050097 int numPlotsX, int numPlotsY,
Brian Salomon9f545bc2017-11-06 10:36:57 -050098 AllowMultitexturing allowMultitexturing,
Robert Phillips256c37b2017-03-01 14:32:46 -050099 GrDrawOpAtlas::EvictionFunc func, void* data);
joshualitt5bf99f12015-03-13 11:47:42 -0700100
Brian Salomon2ee084e2016-12-16 18:59:19 -0500101 /**
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500102 * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns
103 * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if
104 * the subimage cannot fit in the atlas without overwriting texels that will be read in the
105 * current draw. This indicates that the op should end its current draw and begin another
106 * before adding more data. Upon success, an upload of the provided image data will have
107 * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode.
108 * Successive uploads in either mode may be consolidated.
109 * 'kError' will be returned when some unrecoverable error was encountered while trying to
110 * add the subimage. In this case the op being created should be discarded.
111 *
Brian Salomon2ee084e2016-12-16 18:59:19 -0500112 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
113 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
114 * addToAtlas might cause the previous data to be overwritten before it has been read.
115 */
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500116
117 enum class ErrorCode {
118 kError,
119 kSucceeded,
120 kTryAgain
121 };
122
123 ErrorCode addToAtlas(GrResourceProvider*, AtlasID*, GrDeferredUploadTarget*,
124 int width, int height,
125 const void* image, SkIPoint16* loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700126
Jim Van Vertha950b632017-09-12 11:54:11 -0400127 const sk_sp<GrTextureProxy>* getProxies() const { return fProxies; }
joshualitt5bf99f12015-03-13 11:47:42 -0700128
joshualitt7c3a2f82015-03-31 13:32:05 -0700129 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -0800130
131 inline bool hasID(AtlasID id) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500132 if (kInvalidAtlasID == id) {
133 return false;
134 }
Jim Van Vertha950b632017-09-12 11:54:11 -0400135 uint32_t plot = GetPlotIndexFromID(id);
136 SkASSERT(plot < fNumPlots);
137 uint32_t page = GetPageIndexFromID(id);
Robert Phillips4bc70112018-03-01 10:24:02 -0500138 SkASSERT(page < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400139 return fPages[page].fPlotArray[plot]->genID() == GetGenerationFromID(id);
joshualitt5df175e2015-11-18 13:37:54 -0800140 }
joshualittb4c507e2015-04-08 08:07:59 -0700141
Brian Salomon2ee084e2016-12-16 18:59:19 -0500142 /** To ensure the atlas does not evict a given entry, the client must set the last use token. */
Brian Salomon943ed792017-10-30 09:37:55 -0400143 inline void setLastUseToken(AtlasID id, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800144 SkASSERT(this->hasID(id));
Jim Van Vertha950b632017-09-12 11:54:11 -0400145 uint32_t plotIdx = GetPlotIndexFromID(id);
146 SkASSERT(plotIdx < fNumPlots);
147 uint32_t pageIdx = GetPageIndexFromID(id);
Robert Phillips4bc70112018-03-01 10:24:02 -0500148 SkASSERT(pageIdx < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400149 Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
150 this->makeMRU(plot, pageIdx);
151 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800152 }
153
154 inline void registerEvictionCallback(EvictionFunc func, void* userData) {
joshualitt5bf99f12015-03-13 11:47:42 -0700155 EvictionData* data = fEvictionCallbacks.append();
156 data->fFunc = func;
157 data->fData = userData;
158 }
159
Robert Phillips4bc70112018-03-01 10:24:02 -0500160 uint32_t numActivePages() { return fNumActivePages; }
Jim Van Vertha950b632017-09-12 11:54:11 -0400161
Brian Salomon2ee084e2016-12-16 18:59:19 -0500162 /**
163 * 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 -0400164 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
165 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700166 */
167 class BulkUseTokenUpdater {
168 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400169 BulkUseTokenUpdater() {
170 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
171 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700172 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Jim Van Vertha950b632017-09-12 11:54:11 -0400173 : fPlotsToUpdate(that.fPlotsToUpdate) {
174 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700175 }
176
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500177 bool add(AtlasID id) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400178 int index = GrDrawOpAtlas::GetPlotIndexFromID(id);
179 int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500180 if (this->find(pageIdx, index)) {
181 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700182 }
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500183 this->set(pageIdx, index);
184 return true;
joshualittb4c507e2015-04-08 08:07:59 -0700185 }
186
187 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700188 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400189 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700190 }
191
Jim Van Vertha950b632017-09-12 11:54:11 -0400192 struct PlotData {
193 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
194 uint32_t fPageIndex;
195 uint32_t fPlotIndex;
196 };
197
joshualittb4c507e2015-04-08 08:07:59 -0700198 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400199 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700200 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400201 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700202 }
203
Jim Van Vertha950b632017-09-12 11:54:11 -0400204 void set(int pageIdx, int index) {
205 SkASSERT(!this->find(pageIdx, index));
206 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
207 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700208 }
209
Jim Van Vertha950b632017-09-12 11:54:11 -0400210 static constexpr int kMinItems = 4;
Jim Van Vertha950b632017-09-12 11:54:11 -0400211 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500212 uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages];
joshualittb4c507e2015-04-08 08:07:59 -0700213
Brian Salomon2ee084e2016-12-16 18:59:19 -0500214 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700215 };
216
Brian Salomon943ed792017-10-30 09:37:55 -0400217 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800218 int count = updater.fPlotsToUpdate.count();
219 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400220 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400221 // it's possible we've added a plot to the updater and subsequently the plot's page
222 // was deleted -- so we check to prevent a crash
Robert Phillips4bc70112018-03-01 10:24:02 -0500223 if (pd.fPageIndex < fNumActivePages) {
Jim Van Verth6ca9c6f2017-09-27 18:04:34 -0400224 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
225 this->makeMRU(plot, pd.fPageIndex);
226 plot->setLastUseToken(token);
227 }
joshualitt5df175e2015-11-18 13:37:54 -0800228 }
229 }
joshualittb4c507e2015-04-08 08:07:59 -0700230
Brian Salomon943ed792017-10-30 09:37:55 -0400231 void compact(GrDeferredUploadToken startTokenForNextFlush);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400232
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400233 static uint32_t GetPageIndexFromID(AtlasID id) {
234 return id & 0xff;
235 }
236
Robert Phillipscd5099c2018-02-09 09:56:56 -0500237 void instantiate(GrOnFlushResourceProvider*);
238
Brian Salomon9f545bc2017-11-06 10:36:57 -0500239 uint32_t maxPages() const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500240 return fMaxPages;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500241 }
242
Robert Phillips4bc70112018-03-01 10:24:02 -0500243 int numAllocated_TestingOnly() const;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500244 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillips4bc70112018-03-01 10:24:02 -0500245
246private:
Greg Daniel4065d452018-11-16 15:43:41 -0500247 GrDrawOpAtlas(GrProxyProvider*, const GrBackendFormat& format, GrPixelConfig, int width,
248 int height, int numPlotsX, int numPlotsY,
249 AllowMultitexturing allowMultitexturing);
Robert Phillips256c37b2017-03-01 14:32:46 -0500250
Brian Salomon2ee084e2016-12-16 18:59:19 -0500251 /**
252 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
253 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
254 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
255 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
256 * used unless the last use of the Plot has already been flushed through to the gpu.
257 */
258 class Plot : public SkRefCnt {
259 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800260
261 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400262 /** index() is a unique id for the plot relative to the owning GrAtlas and page. */
263 uint32_t index() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500264 /**
265 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
266 * if a particular subimage is still present in the atlas.
267 */
joshualitt5df175e2015-11-18 13:37:54 -0800268 uint64_t genID() const { return fGenID; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500269 GrDrawOpAtlas::AtlasID id() const {
270 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID);
joshualitt5df175e2015-11-18 13:37:54 -0800271 return fID;
272 }
273 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
274
275 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
276
Brian Salomon2ee084e2016-12-16 18:59:19 -0500277 /**
278 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
279 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
280 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
281 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
282 * has already flushed through the gpu then we can reuse the plot.
283 */
Brian Salomon943ed792017-10-30 09:37:55 -0400284 GrDeferredUploadToken lastUploadToken() const { return fLastUpload; }
285 GrDeferredUploadToken lastUseToken() const { return fLastUse; }
286 void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; }
287 void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800288
Brian Salomon943ed792017-10-30 09:37:55 -0400289 void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800290 void resetRects();
291
Jim Van Verth106b5c42017-09-26 12:45:29 -0400292 int flushesSinceLastUsed() { return fFlushesSinceLastUse; }
293 void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; }
294 void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; }
295
joshualitt5df175e2015-11-18 13:37:54 -0800296 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400297 Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, int width, int height,
Brian Salomon2ee084e2016-12-16 18:59:19 -0500298 GrPixelConfig config);
joshualitt5df175e2015-11-18 13:37:54 -0800299
Brian Salomon2ee084e2016-12-16 18:59:19 -0500300 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800301
Brian Salomon2ee084e2016-12-16 18:59:19 -0500302 /**
303 * Create a clone of this plot. The cloned plot will take the place of the current plot in
304 * the atlas
305 */
306 Plot* clone() const {
Jim Van Vertha950b632017-09-12 11:54:11 -0400307 return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig);
joshualitt5df175e2015-11-18 13:37:54 -0800308 }
309
Jim Van Vertha950b632017-09-12 11:54:11 -0400310 static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx,
311 uint64_t generation) {
312 SkASSERT(pageIdx < (1 << 8));
Brian Salomon9f545bc2017-11-06 10:36:57 -0500313 SkASSERT(pageIdx < kMaxMultitexturePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400314 SkASSERT(plotIdx < (1 << 8));
joshualitt5df175e2015-11-18 13:37:54 -0800315 SkASSERT(generation < ((uint64_t)1 << 48));
Jim Van Vertha950b632017-09-12 11:54:11 -0400316 return generation << 16 | plotIdx << 8 | pageIdx;
joshualitt5df175e2015-11-18 13:37:54 -0800317 }
318
Brian Salomon943ed792017-10-30 09:37:55 -0400319 GrDeferredUploadToken fLastUpload;
320 GrDeferredUploadToken fLastUse;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400321 // the number of flushes since this plot has been last used
322 int fFlushesSinceLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800323
Jim Van Vertha950b632017-09-12 11:54:11 -0400324 struct {
325 const uint32_t fPageIndex : 16;
326 const uint32_t fPlotIndex : 16;
327 };
Brian Salomon2ee084e2016-12-16 18:59:19 -0500328 uint64_t fGenID;
329 GrDrawOpAtlas::AtlasID fID;
330 unsigned char* fData;
331 const int fWidth;
332 const int fHeight;
333 const int fX;
334 const int fY;
335 GrRectanizer* fRects;
336 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
337 const GrPixelConfig fConfig;
338 const size_t fBytesPerPixel;
339 SkIRect fDirtyRect;
340 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800341
Brian Salomon2ee084e2016-12-16 18:59:19 -0500342 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800343
344 typedef SkRefCnt INHERITED;
345 };
346
Brian Salomon2ee084e2016-12-16 18:59:19 -0500347 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800348
Jim Van Vertha950b632017-09-12 11:54:11 -0400349 static uint32_t GetPlotIndexFromID(AtlasID id) {
350 return (id >> 8) & 0xff;
joshualitt5bf99f12015-03-13 11:47:42 -0700351 }
352
joshualitt8db6fdc2015-07-31 08:25:07 -0700353 // top 48 bits are reserved for the generation ID
354 static uint64_t GetGenerationFromID(AtlasID id) {
355 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700356 }
357
Brian Salomon29b60c92017-10-31 14:42:10 -0400358 inline bool updatePlot(GrDeferredUploadTarget*, AtlasID*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700359
Jim Van Vertha950b632017-09-12 11:54:11 -0400360 inline void makeMRU(Plot* plot, int pageIdx) {
361 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800362 return;
363 }
364
Jim Van Vertha950b632017-09-12 11:54:11 -0400365 fPages[pageIdx].fPlotList.remove(plot);
366 fPages[pageIdx].fPlotList.addToHead(plot);
367
Jim Van Verth106b5c42017-09-26 12:45:29 -0400368 // No MRU update for pages -- since we will always try to add from
369 // the front and remove from the back there is no need for MRU.
joshualitt5df175e2015-11-18 13:37:54 -0800370 }
joshualitt5bf99f12015-03-13 11:47:42 -0700371
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500372 bool uploadToPage(unsigned int pageIdx, AtlasID* id, GrDeferredUploadTarget* target,
373 int width, int height, const void* image, SkIPoint16* loc);
374
Robert Phillips4bc70112018-03-01 10:24:02 -0500375 bool createPages(GrProxyProvider*);
376 bool activateNewPage(GrResourceProvider*);
377 void deactivateLastPage();
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400378
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400379 void processEviction(AtlasID);
380 inline void processEvictionAndResetRects(Plot* plot) {
381 this->processEviction(plot->id());
382 plot->resetRects();
383 }
joshualitt5bf99f12015-03-13 11:47:42 -0700384
Greg Daniel4065d452018-11-16 15:43:41 -0500385 GrBackendFormat fFormat;
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400386 GrPixelConfig fPixelConfig;
387 int fTextureWidth;
388 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500389 int fPlotWidth;
390 int fPlotHeight;
Jim Van Verth06f593c2018-02-20 11:30:10 -0500391 unsigned int fNumPlots;
robertphillips2b0536f2015-11-06 14:10:42 -0800392
Robert Phillips32f28182017-02-28 16:20:03 -0500393 uint64_t fAtlasGeneration;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400394 // nextTokenToFlush() value at the end of the previous flush
Brian Salomon943ed792017-10-30 09:37:55 -0400395 GrDeferredUploadToken fPrevFlushToken;
joshualitt5bf99f12015-03-13 11:47:42 -0700396
397 struct EvictionData {
398 EvictionFunc fFunc;
399 void* fData;
400 };
401
402 SkTDArray<EvictionData> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400403
404 struct Page {
405 // allocated array of Plots
406 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
407 // LRU list of Plots (MRU at head - LRU at tail)
408 PlotList fPlotList;
409 };
410 // proxies kept separate to make it easier to pass them up to client
Brian Salomon9f545bc2017-11-06 10:36:57 -0500411 sk_sp<GrTextureProxy> fProxies[kMaxMultitexturePages];
412 Page fPages[kMaxMultitexturePages];
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500413 uint32_t fMaxPages;
Robert Phillips4bc70112018-03-01 10:24:02 -0500414
415 uint32_t fNumActivePages;
joshualitt5bf99f12015-03-13 11:47:42 -0700416};
417
Herb Derbybbf5fb52018-10-15 16:39:39 -0400418// There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In
419// general, the A8 dimensions are NxN and 565 and ARGB are N/2xN with the constraint that an atlas
420// size will always contain at least one plot. Since the ARGB atlas takes the most space, its
421// dimensions are used to size the other two atlases.
422class GrDrawOpAtlasConfig {
423public:
Herb Derby15d9ef22018-10-18 13:41:32 -0400424 GrDrawOpAtlasConfig(int maxDimension, size_t maxBytes);
Herb Derbybbf5fb52018-10-15 16:39:39 -0400425
426 // For testing only - make minimum sized atlases -- 1x1 plots wide.
Herb Derby15d9ef22018-10-18 13:41:32 -0400427 GrDrawOpAtlasConfig();
Herb Derbybbf5fb52018-10-15 16:39:39 -0400428
Herb Derby15d9ef22018-10-18 13:41:32 -0400429 SkISize numPlots(GrMaskFormat type) const;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400430
Herb Derby15d9ef22018-10-18 13:41:32 -0400431 SkISize atlasDimensions(GrMaskFormat type) const;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400432
Herb Derby15d9ef22018-10-18 13:41:32 -0400433 static int PlotsPerLongDimensionForARGB(int maxDimension);
Herb Derbybbf5fb52018-10-15 16:39:39 -0400434
435private:
Herb Derby15d9ef22018-10-18 13:41:32 -0400436 // The distance field text implementation limits the largest atlas dimension to 2048.
437 static constexpr int kMaxDistanceFieldDim = 2048;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400438
439 // The width and height of a plot.
440 static constexpr int kPlotSize = 256;
441
442 // This is the height (longest dimension) of the ARGB atlas divided by the plot size.
443 const int fPlotsPerLongDimension;
444};
445
joshualitt5bf99f12015-03-13 11:47:42 -0700446#endif