blob: 302ca6fd2ae60158e45f6f40ad5638c4fd72d761 [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
13#include "SkGlyphRun.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 */
Robert Phillips4bc70112018-03-01 10:24:02 -050093 static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*, GrPixelConfig,
94 int width, int height,
Robert Phillips256c37b2017-03-01 14:32:46 -050095 int numPlotsX, int numPlotsY,
Brian Salomon9f545bc2017-11-06 10:36:57 -050096 AllowMultitexturing allowMultitexturing,
Robert Phillips256c37b2017-03-01 14:32:46 -050097 GrDrawOpAtlas::EvictionFunc func, void* data);
joshualitt5bf99f12015-03-13 11:47:42 -070098
Brian Salomon2ee084e2016-12-16 18:59:19 -050099 /**
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500100 * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns
101 * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if
102 * the subimage cannot fit in the atlas without overwriting texels that will be read in the
103 * current draw. This indicates that the op should end its current draw and begin another
104 * before adding more data. Upon success, an upload of the provided image data will have
105 * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode.
106 * Successive uploads in either mode may be consolidated.
107 * 'kError' will be returned when some unrecoverable error was encountered while trying to
108 * add the subimage. In this case the op being created should be discarded.
109 *
Brian Salomon2ee084e2016-12-16 18:59:19 -0500110 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
111 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
112 * addToAtlas might cause the previous data to be overwritten before it has been read.
113 */
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500114
115 enum class ErrorCode {
116 kError,
117 kSucceeded,
118 kTryAgain
119 };
120
121 ErrorCode addToAtlas(GrResourceProvider*, AtlasID*, GrDeferredUploadTarget*,
122 int width, int height,
123 const void* image, SkIPoint16* loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700124
Jim Van Vertha950b632017-09-12 11:54:11 -0400125 const sk_sp<GrTextureProxy>* getProxies() const { return fProxies; }
joshualitt5bf99f12015-03-13 11:47:42 -0700126
joshualitt7c3a2f82015-03-31 13:32:05 -0700127 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -0800128
129 inline bool hasID(AtlasID id) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500130 if (kInvalidAtlasID == id) {
131 return false;
132 }
Jim Van Vertha950b632017-09-12 11:54:11 -0400133 uint32_t plot = GetPlotIndexFromID(id);
134 SkASSERT(plot < fNumPlots);
135 uint32_t page = GetPageIndexFromID(id);
Robert Phillips4bc70112018-03-01 10:24:02 -0500136 SkASSERT(page < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400137 return fPages[page].fPlotArray[plot]->genID() == GetGenerationFromID(id);
joshualitt5df175e2015-11-18 13:37:54 -0800138 }
joshualittb4c507e2015-04-08 08:07:59 -0700139
Brian Salomon2ee084e2016-12-16 18:59:19 -0500140 /** 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 -0400141 inline void setLastUseToken(AtlasID id, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800142 SkASSERT(this->hasID(id));
Jim Van Vertha950b632017-09-12 11:54:11 -0400143 uint32_t plotIdx = GetPlotIndexFromID(id);
144 SkASSERT(plotIdx < fNumPlots);
145 uint32_t pageIdx = GetPageIndexFromID(id);
Robert Phillips4bc70112018-03-01 10:24:02 -0500146 SkASSERT(pageIdx < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400147 Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
148 this->makeMRU(plot, pageIdx);
149 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800150 }
151
152 inline void registerEvictionCallback(EvictionFunc func, void* userData) {
joshualitt5bf99f12015-03-13 11:47:42 -0700153 EvictionData* data = fEvictionCallbacks.append();
154 data->fFunc = func;
155 data->fData = userData;
156 }
157
Robert Phillips4bc70112018-03-01 10:24:02 -0500158 uint32_t numActivePages() { return fNumActivePages; }
Jim Van Vertha950b632017-09-12 11:54:11 -0400159
Brian Salomon2ee084e2016-12-16 18:59:19 -0500160 /**
161 * 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 -0400162 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
163 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700164 */
165 class BulkUseTokenUpdater {
166 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400167 BulkUseTokenUpdater() {
168 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
169 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700170 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Jim Van Vertha950b632017-09-12 11:54:11 -0400171 : fPlotsToUpdate(that.fPlotsToUpdate) {
172 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700173 }
174
joshualittb4c507e2015-04-08 08:07:59 -0700175 void add(AtlasID id) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400176 int index = GrDrawOpAtlas::GetPlotIndexFromID(id);
177 int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id);
178 if (!this->find(pageIdx, index)) {
179 this->set(pageIdx, index);
joshualittb4c507e2015-04-08 08:07:59 -0700180 }
181 }
182
183 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700184 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400185 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700186 }
187
Jim Van Vertha950b632017-09-12 11:54:11 -0400188 struct PlotData {
189 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
190 uint32_t fPageIndex;
191 uint32_t fPlotIndex;
192 };
193
joshualittb4c507e2015-04-08 08:07:59 -0700194 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400195 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700196 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400197 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700198 }
199
Jim Van Vertha950b632017-09-12 11:54:11 -0400200 void set(int pageIdx, int index) {
201 SkASSERT(!this->find(pageIdx, index));
202 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
203 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700204 }
205
Jim Van Vertha950b632017-09-12 11:54:11 -0400206 static constexpr int kMinItems = 4;
Jim Van Vertha950b632017-09-12 11:54:11 -0400207 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500208 uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages];
joshualittb4c507e2015-04-08 08:07:59 -0700209
Brian Salomon2ee084e2016-12-16 18:59:19 -0500210 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700211 };
212
Brian Salomon943ed792017-10-30 09:37:55 -0400213 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800214 int count = updater.fPlotsToUpdate.count();
215 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400216 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400217 // it's possible we've added a plot to the updater and subsequently the plot's page
218 // was deleted -- so we check to prevent a crash
Robert Phillips4bc70112018-03-01 10:24:02 -0500219 if (pd.fPageIndex < fNumActivePages) {
Jim Van Verth6ca9c6f2017-09-27 18:04:34 -0400220 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
221 this->makeMRU(plot, pd.fPageIndex);
222 plot->setLastUseToken(token);
223 }
joshualitt5df175e2015-11-18 13:37:54 -0800224 }
225 }
joshualittb4c507e2015-04-08 08:07:59 -0700226
Brian Salomon943ed792017-10-30 09:37:55 -0400227 void compact(GrDeferredUploadToken startTokenForNextFlush);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400228
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400229 static uint32_t GetPageIndexFromID(AtlasID id) {
230 return id & 0xff;
231 }
232
Robert Phillipscd5099c2018-02-09 09:56:56 -0500233 void instantiate(GrOnFlushResourceProvider*);
234
Brian Salomon9f545bc2017-11-06 10:36:57 -0500235 uint32_t maxPages() const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500236 return fMaxPages;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500237 }
238
Robert Phillips4bc70112018-03-01 10:24:02 -0500239 int numAllocated_TestingOnly() const;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500240 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillips4bc70112018-03-01 10:24:02 -0500241
242private:
243 GrDrawOpAtlas(GrProxyProvider*, GrPixelConfig, int width, int height, int numPlotsX,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500244 int numPlotsY, AllowMultitexturing allowMultitexturing);
Robert Phillips256c37b2017-03-01 14:32:46 -0500245
Brian Salomon2ee084e2016-12-16 18:59:19 -0500246 /**
247 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
248 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
249 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
250 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
251 * used unless the last use of the Plot has already been flushed through to the gpu.
252 */
253 class Plot : public SkRefCnt {
254 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800255
256 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400257 /** index() is a unique id for the plot relative to the owning GrAtlas and page. */
258 uint32_t index() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500259 /**
260 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
261 * if a particular subimage is still present in the atlas.
262 */
joshualitt5df175e2015-11-18 13:37:54 -0800263 uint64_t genID() const { return fGenID; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500264 GrDrawOpAtlas::AtlasID id() const {
265 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID);
joshualitt5df175e2015-11-18 13:37:54 -0800266 return fID;
267 }
268 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
269
270 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
271
Brian Salomon2ee084e2016-12-16 18:59:19 -0500272 /**
273 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
274 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
275 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
276 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
277 * has already flushed through the gpu then we can reuse the plot.
278 */
Brian Salomon943ed792017-10-30 09:37:55 -0400279 GrDeferredUploadToken lastUploadToken() const { return fLastUpload; }
280 GrDeferredUploadToken lastUseToken() const { return fLastUse; }
281 void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; }
282 void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800283
Brian Salomon943ed792017-10-30 09:37:55 -0400284 void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800285 void resetRects();
286
Jim Van Verth106b5c42017-09-26 12:45:29 -0400287 int flushesSinceLastUsed() { return fFlushesSinceLastUse; }
288 void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; }
289 void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; }
290
joshualitt5df175e2015-11-18 13:37:54 -0800291 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400292 Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, int width, int height,
Brian Salomon2ee084e2016-12-16 18:59:19 -0500293 GrPixelConfig config);
joshualitt5df175e2015-11-18 13:37:54 -0800294
Brian Salomon2ee084e2016-12-16 18:59:19 -0500295 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800296
Brian Salomon2ee084e2016-12-16 18:59:19 -0500297 /**
298 * Create a clone of this plot. The cloned plot will take the place of the current plot in
299 * the atlas
300 */
301 Plot* clone() const {
Jim Van Vertha950b632017-09-12 11:54:11 -0400302 return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig);
joshualitt5df175e2015-11-18 13:37:54 -0800303 }
304
Jim Van Vertha950b632017-09-12 11:54:11 -0400305 static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx,
306 uint64_t generation) {
307 SkASSERT(pageIdx < (1 << 8));
Brian Salomon9f545bc2017-11-06 10:36:57 -0500308 SkASSERT(pageIdx < kMaxMultitexturePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400309 SkASSERT(plotIdx < (1 << 8));
joshualitt5df175e2015-11-18 13:37:54 -0800310 SkASSERT(generation < ((uint64_t)1 << 48));
Jim Van Vertha950b632017-09-12 11:54:11 -0400311 return generation << 16 | plotIdx << 8 | pageIdx;
joshualitt5df175e2015-11-18 13:37:54 -0800312 }
313
Brian Salomon943ed792017-10-30 09:37:55 -0400314 GrDeferredUploadToken fLastUpload;
315 GrDeferredUploadToken fLastUse;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400316 // the number of flushes since this plot has been last used
317 int fFlushesSinceLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800318
Jim Van Vertha950b632017-09-12 11:54:11 -0400319 struct {
320 const uint32_t fPageIndex : 16;
321 const uint32_t fPlotIndex : 16;
322 };
Brian Salomon2ee084e2016-12-16 18:59:19 -0500323 uint64_t fGenID;
324 GrDrawOpAtlas::AtlasID fID;
325 unsigned char* fData;
326 const int fWidth;
327 const int fHeight;
328 const int fX;
329 const int fY;
330 GrRectanizer* fRects;
331 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
332 const GrPixelConfig fConfig;
333 const size_t fBytesPerPixel;
334 SkIRect fDirtyRect;
335 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800336
Brian Salomon2ee084e2016-12-16 18:59:19 -0500337 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800338
339 typedef SkRefCnt INHERITED;
340 };
341
Brian Salomon2ee084e2016-12-16 18:59:19 -0500342 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800343
Jim Van Vertha950b632017-09-12 11:54:11 -0400344 static uint32_t GetPlotIndexFromID(AtlasID id) {
345 return (id >> 8) & 0xff;
joshualitt5bf99f12015-03-13 11:47:42 -0700346 }
347
joshualitt8db6fdc2015-07-31 08:25:07 -0700348 // top 48 bits are reserved for the generation ID
349 static uint64_t GetGenerationFromID(AtlasID id) {
350 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700351 }
352
Brian Salomon29b60c92017-10-31 14:42:10 -0400353 inline bool updatePlot(GrDeferredUploadTarget*, AtlasID*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700354
Jim Van Vertha950b632017-09-12 11:54:11 -0400355 inline void makeMRU(Plot* plot, int pageIdx) {
356 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800357 return;
358 }
359
Jim Van Vertha950b632017-09-12 11:54:11 -0400360 fPages[pageIdx].fPlotList.remove(plot);
361 fPages[pageIdx].fPlotList.addToHead(plot);
362
Jim Van Verth106b5c42017-09-26 12:45:29 -0400363 // No MRU update for pages -- since we will always try to add from
364 // the front and remove from the back there is no need for MRU.
joshualitt5df175e2015-11-18 13:37:54 -0800365 }
joshualitt5bf99f12015-03-13 11:47:42 -0700366
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500367 bool uploadToPage(unsigned int pageIdx, AtlasID* id, GrDeferredUploadTarget* target,
368 int width, int height, const void* image, SkIPoint16* loc);
369
Robert Phillips4bc70112018-03-01 10:24:02 -0500370 bool createPages(GrProxyProvider*);
371 bool activateNewPage(GrResourceProvider*);
372 void deactivateLastPage();
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400373
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400374 void processEviction(AtlasID);
375 inline void processEvictionAndResetRects(Plot* plot) {
376 this->processEviction(plot->id());
377 plot->resetRects();
378 }
joshualitt5bf99f12015-03-13 11:47:42 -0700379
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400380 GrPixelConfig fPixelConfig;
381 int fTextureWidth;
382 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500383 int fPlotWidth;
384 int fPlotHeight;
Jim Van Verth06f593c2018-02-20 11:30:10 -0500385 unsigned int fNumPlots;
robertphillips2b0536f2015-11-06 14:10:42 -0800386
Robert Phillips32f28182017-02-28 16:20:03 -0500387 uint64_t fAtlasGeneration;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400388 // nextTokenToFlush() value at the end of the previous flush
Brian Salomon943ed792017-10-30 09:37:55 -0400389 GrDeferredUploadToken fPrevFlushToken;
joshualitt5bf99f12015-03-13 11:47:42 -0700390
391 struct EvictionData {
392 EvictionFunc fFunc;
393 void* fData;
394 };
395
396 SkTDArray<EvictionData> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400397
398 struct Page {
399 // allocated array of Plots
400 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
401 // LRU list of Plots (MRU at head - LRU at tail)
402 PlotList fPlotList;
403 };
404 // proxies kept separate to make it easier to pass them up to client
Brian Salomon9f545bc2017-11-06 10:36:57 -0500405 sk_sp<GrTextureProxy> fProxies[kMaxMultitexturePages];
406 Page fPages[kMaxMultitexturePages];
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500407 uint32_t fMaxPages;
Robert Phillips4bc70112018-03-01 10:24:02 -0500408
409 uint32_t fNumActivePages;
joshualitt5bf99f12015-03-13 11:47:42 -0700410};
411
Herb Derbybbf5fb52018-10-15 16:39:39 -0400412// There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In
413// general, the A8 dimensions are NxN and 565 and ARGB are N/2xN with the constraint that an atlas
414// size will always contain at least one plot. Since the ARGB atlas takes the most space, its
415// dimensions are used to size the other two atlases.
416class GrDrawOpAtlasConfig {
417public:
Herb Derby15d9ef22018-10-18 13:41:32 -0400418 GrDrawOpAtlasConfig(int maxDimension, size_t maxBytes);
Herb Derbybbf5fb52018-10-15 16:39:39 -0400419
420 // For testing only - make minimum sized atlases -- 1x1 plots wide.
Herb Derby15d9ef22018-10-18 13:41:32 -0400421 GrDrawOpAtlasConfig();
Herb Derbybbf5fb52018-10-15 16:39:39 -0400422
Herb Derby15d9ef22018-10-18 13:41:32 -0400423 SkISize numPlots(GrMaskFormat type) const;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400424
Herb Derby15d9ef22018-10-18 13:41:32 -0400425 SkISize atlasDimensions(GrMaskFormat type) const;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400426
Herb Derby15d9ef22018-10-18 13:41:32 -0400427 static int PlotsPerLongDimensionForARGB(int maxDimension);
Herb Derbybbf5fb52018-10-15 16:39:39 -0400428
429private:
Herb Derby15d9ef22018-10-18 13:41:32 -0400430 // The distance field text implementation limits the largest atlas dimension to 2048.
431 static constexpr int kMaxDistanceFieldDim = 2048;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400432
433 // The width and height of a plot.
434 static constexpr int kPlotSize = 256;
435
436 // This is the height (longest dimension) of the ARGB atlas divided by the plot size.
437 const int fPlotsPerLongDimension;
438};
439
joshualitt5bf99f12015-03-13 11:47:42 -0700440#endif