blob: 099524f6365c7c66a0e68c34e4865a03d274060a [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
Jim Van Verthf6206f92018-12-14 08:22:24 -050059 static constexpr int kMaxPlots = 32; // restricted by the fPlotAlreadyUpdated bitfield
60 // in BulkUseTokenUpdater
Herb Derbybbf5fb52018-10-15 16:39:39 -040061
Brian Salomon2ee084e2016-12-16 18:59:19 -050062 /**
63 * An AtlasID is an opaque handle which callers can use to determine if the atlas contains
64 * a specific piece of data.
65 */
joshualitt8db6fdc2015-07-31 08:25:07 -070066 typedef uint64_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070067 static const uint32_t kInvalidAtlasID = 0;
68 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070069
Brian Salomon2ee084e2016-12-16 18:59:19 -050070 /**
71 * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a
72 * specific AtlasID, it will call all of the registered listeners so they can process the
73 * eviction.
74 */
75 typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*);
joshualitt5bf99f12015-03-13 11:47:42 -070076
Robert Phillips256c37b2017-03-01 14:32:46 -050077 /**
78 * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
79 * should only be used inside of GrMeshDrawOp::onPrepareDraws.
80 * @param GrPixelConfig The pixel config which this atlas will store
81 * @param width width in pixels of the atlas
82 * @param height height in pixels of the atlas
83 * @param numPlotsX The number of plots the atlas should be broken up into in the X
84 * direction
85 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
86 * direction
Brian Salomon9f545bc2017-11-06 10:36:57 -050087 * @param allowMultitexturing Can the atlas use more than one texture.
Robert Phillips256c37b2017-03-01 14:32:46 -050088 * @param func An eviction function which will be called whenever the atlas has to
89 * evict data
Brian Salomon9f545bc2017-11-06 10:36:57 -050090 * @param data User supplied data which will be passed into func whenever an
Robert Phillips256c37b2017-03-01 14:32:46 -050091 * eviction occurs
92 * @return An initialized GrDrawOpAtlas, or nullptr if creation fails
93 */
Greg Daniel4065d452018-11-16 15:43:41 -050094 static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*,
95 const GrBackendFormat& format,
96 GrPixelConfig,
Robert Phillips4bc70112018-03-01 10:24:02 -050097 int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -050098 int plotWidth, int plotHeight,
Brian Salomon9f545bc2017-11-06 10:36:57 -050099 AllowMultitexturing allowMultitexturing,
Robert Phillips256c37b2017-03-01 14:32:46 -0500100 GrDrawOpAtlas::EvictionFunc func, void* data);
joshualitt5bf99f12015-03-13 11:47:42 -0700101
Brian Salomon2ee084e2016-12-16 18:59:19 -0500102 /**
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500103 * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns
104 * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if
105 * the subimage cannot fit in the atlas without overwriting texels that will be read in the
106 * current draw. This indicates that the op should end its current draw and begin another
107 * before adding more data. Upon success, an upload of the provided image data will have
108 * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode.
109 * Successive uploads in either mode may be consolidated.
110 * 'kError' will be returned when some unrecoverable error was encountered while trying to
111 * add the subimage. In this case the op being created should be discarded.
112 *
Brian Salomon2ee084e2016-12-16 18:59:19 -0500113 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
114 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
115 * addToAtlas might cause the previous data to be overwritten before it has been read.
116 */
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500117
118 enum class ErrorCode {
119 kError,
120 kSucceeded,
121 kTryAgain
122 };
123
124 ErrorCode addToAtlas(GrResourceProvider*, AtlasID*, GrDeferredUploadTarget*,
125 int width, int height,
126 const void* image, SkIPoint16* loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700127
Jim Van Vertha950b632017-09-12 11:54:11 -0400128 const sk_sp<GrTextureProxy>* getProxies() const { return fProxies; }
joshualitt5bf99f12015-03-13 11:47:42 -0700129
joshualitt7c3a2f82015-03-31 13:32:05 -0700130 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -0800131
132 inline bool hasID(AtlasID id) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500133 if (kInvalidAtlasID == id) {
134 return false;
135 }
Jim Van Vertha950b632017-09-12 11:54:11 -0400136 uint32_t plot = GetPlotIndexFromID(id);
137 SkASSERT(plot < fNumPlots);
138 uint32_t page = GetPageIndexFromID(id);
Robert Phillips4bc70112018-03-01 10:24:02 -0500139 SkASSERT(page < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400140 return fPages[page].fPlotArray[plot]->genID() == GetGenerationFromID(id);
joshualitt5df175e2015-11-18 13:37:54 -0800141 }
joshualittb4c507e2015-04-08 08:07:59 -0700142
Brian Salomon2ee084e2016-12-16 18:59:19 -0500143 /** 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 -0400144 inline void setLastUseToken(AtlasID id, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800145 SkASSERT(this->hasID(id));
Jim Van Vertha950b632017-09-12 11:54:11 -0400146 uint32_t plotIdx = GetPlotIndexFromID(id);
147 SkASSERT(plotIdx < fNumPlots);
148 uint32_t pageIdx = GetPageIndexFromID(id);
Robert Phillips4bc70112018-03-01 10:24:02 -0500149 SkASSERT(pageIdx < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400150 Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
151 this->makeMRU(plot, pageIdx);
152 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800153 }
154
155 inline void registerEvictionCallback(EvictionFunc func, void* userData) {
joshualitt5bf99f12015-03-13 11:47:42 -0700156 EvictionData* data = fEvictionCallbacks.append();
157 data->fFunc = func;
158 data->fData = userData;
159 }
160
Robert Phillips4bc70112018-03-01 10:24:02 -0500161 uint32_t numActivePages() { return fNumActivePages; }
Jim Van Vertha950b632017-09-12 11:54:11 -0400162
Brian Salomon2ee084e2016-12-16 18:59:19 -0500163 /**
164 * 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 -0400165 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
166 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700167 */
168 class BulkUseTokenUpdater {
169 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400170 BulkUseTokenUpdater() {
171 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
172 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700173 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Jim Van Vertha950b632017-09-12 11:54:11 -0400174 : fPlotsToUpdate(that.fPlotsToUpdate) {
175 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700176 }
177
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500178 bool add(AtlasID id) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400179 int index = GrDrawOpAtlas::GetPlotIndexFromID(id);
180 int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500181 if (this->find(pageIdx, index)) {
182 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700183 }
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500184 this->set(pageIdx, index);
185 return true;
joshualittb4c507e2015-04-08 08:07:59 -0700186 }
187
188 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700189 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400190 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700191 }
192
Jim Van Vertha950b632017-09-12 11:54:11 -0400193 struct PlotData {
194 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
195 uint32_t fPageIndex;
196 uint32_t fPlotIndex;
197 };
198
joshualittb4c507e2015-04-08 08:07:59 -0700199 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400200 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700201 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400202 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700203 }
204
Jim Van Vertha950b632017-09-12 11:54:11 -0400205 void set(int pageIdx, int index) {
206 SkASSERT(!this->find(pageIdx, index));
207 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
208 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700209 }
210
Jim Van Vertha950b632017-09-12 11:54:11 -0400211 static constexpr int kMinItems = 4;
Jim Van Vertha950b632017-09-12 11:54:11 -0400212 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
Jim Van Verthf6206f92018-12-14 08:22:24 -0500213 uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages]; // TODO: increase this to uint64_t
214 // to allow more plots per page
joshualittb4c507e2015-04-08 08:07:59 -0700215
Brian Salomon2ee084e2016-12-16 18:59:19 -0500216 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700217 };
218
Brian Salomon943ed792017-10-30 09:37:55 -0400219 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800220 int count = updater.fPlotsToUpdate.count();
221 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400222 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400223 // it's possible we've added a plot to the updater and subsequently the plot's page
224 // was deleted -- so we check to prevent a crash
Robert Phillips4bc70112018-03-01 10:24:02 -0500225 if (pd.fPageIndex < fNumActivePages) {
Jim Van Verth6ca9c6f2017-09-27 18:04:34 -0400226 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
227 this->makeMRU(plot, pd.fPageIndex);
228 plot->setLastUseToken(token);
229 }
joshualitt5df175e2015-11-18 13:37:54 -0800230 }
231 }
joshualittb4c507e2015-04-08 08:07:59 -0700232
Brian Salomon943ed792017-10-30 09:37:55 -0400233 void compact(GrDeferredUploadToken startTokenForNextFlush);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400234
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400235 static uint32_t GetPageIndexFromID(AtlasID id) {
236 return id & 0xff;
237 }
238
Robert Phillipscd5099c2018-02-09 09:56:56 -0500239 void instantiate(GrOnFlushResourceProvider*);
240
Brian Salomon9f545bc2017-11-06 10:36:57 -0500241 uint32_t maxPages() const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500242 return fMaxPages;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500243 }
244
Robert Phillips4bc70112018-03-01 10:24:02 -0500245 int numAllocated_TestingOnly() const;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500246 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillips4bc70112018-03-01 10:24:02 -0500247
248private:
Greg Daniel4065d452018-11-16 15:43:41 -0500249 GrDrawOpAtlas(GrProxyProvider*, const GrBackendFormat& format, GrPixelConfig, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500250 int height, int plotWidth, int plotHeight,
Greg Daniel4065d452018-11-16 15:43:41 -0500251 AllowMultitexturing allowMultitexturing);
Robert Phillips256c37b2017-03-01 14:32:46 -0500252
Brian Salomon2ee084e2016-12-16 18:59:19 -0500253 /**
254 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
255 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
256 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
257 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
258 * used unless the last use of the Plot has already been flushed through to the gpu.
259 */
260 class Plot : public SkRefCnt {
261 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800262
263 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400264 /** index() is a unique id for the plot relative to the owning GrAtlas and page. */
265 uint32_t index() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500266 /**
267 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
268 * if a particular subimage is still present in the atlas.
269 */
joshualitt5df175e2015-11-18 13:37:54 -0800270 uint64_t genID() const { return fGenID; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500271 GrDrawOpAtlas::AtlasID id() const {
272 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID);
joshualitt5df175e2015-11-18 13:37:54 -0800273 return fID;
274 }
275 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
276
277 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
278
Brian Salomon2ee084e2016-12-16 18:59:19 -0500279 /**
280 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
281 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
282 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
283 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
284 * has already flushed through the gpu then we can reuse the plot.
285 */
Brian Salomon943ed792017-10-30 09:37:55 -0400286 GrDeferredUploadToken lastUploadToken() const { return fLastUpload; }
287 GrDeferredUploadToken lastUseToken() const { return fLastUse; }
288 void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; }
289 void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800290
Brian Salomon943ed792017-10-30 09:37:55 -0400291 void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800292 void resetRects();
293
Jim Van Verth106b5c42017-09-26 12:45:29 -0400294 int flushesSinceLastUsed() { return fFlushesSinceLastUse; }
295 void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; }
296 void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; }
297
joshualitt5df175e2015-11-18 13:37:54 -0800298 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400299 Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, int width, int height,
Brian Salomon2ee084e2016-12-16 18:59:19 -0500300 GrPixelConfig config);
joshualitt5df175e2015-11-18 13:37:54 -0800301
Brian Salomon2ee084e2016-12-16 18:59:19 -0500302 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800303
Brian Salomon2ee084e2016-12-16 18:59:19 -0500304 /**
305 * Create a clone of this plot. The cloned plot will take the place of the current plot in
306 * the atlas
307 */
308 Plot* clone() const {
Jim Van Vertha950b632017-09-12 11:54:11 -0400309 return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig);
joshualitt5df175e2015-11-18 13:37:54 -0800310 }
311
Jim Van Vertha950b632017-09-12 11:54:11 -0400312 static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx,
313 uint64_t generation) {
314 SkASSERT(pageIdx < (1 << 8));
Brian Salomon9f545bc2017-11-06 10:36:57 -0500315 SkASSERT(pageIdx < kMaxMultitexturePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400316 SkASSERT(plotIdx < (1 << 8));
joshualitt5df175e2015-11-18 13:37:54 -0800317 SkASSERT(generation < ((uint64_t)1 << 48));
Jim Van Vertha950b632017-09-12 11:54:11 -0400318 return generation << 16 | plotIdx << 8 | pageIdx;
joshualitt5df175e2015-11-18 13:37:54 -0800319 }
320
Brian Salomon943ed792017-10-30 09:37:55 -0400321 GrDeferredUploadToken fLastUpload;
322 GrDeferredUploadToken fLastUse;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400323 // the number of flushes since this plot has been last used
324 int fFlushesSinceLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800325
Jim Van Vertha950b632017-09-12 11:54:11 -0400326 struct {
327 const uint32_t fPageIndex : 16;
328 const uint32_t fPlotIndex : 16;
329 };
Brian Salomon2ee084e2016-12-16 18:59:19 -0500330 uint64_t fGenID;
331 GrDrawOpAtlas::AtlasID fID;
332 unsigned char* fData;
333 const int fWidth;
334 const int fHeight;
335 const int fX;
336 const int fY;
337 GrRectanizer* fRects;
338 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
339 const GrPixelConfig fConfig;
340 const size_t fBytesPerPixel;
341 SkIRect fDirtyRect;
342 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800343
Brian Salomon2ee084e2016-12-16 18:59:19 -0500344 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800345
346 typedef SkRefCnt INHERITED;
347 };
348
Brian Salomon2ee084e2016-12-16 18:59:19 -0500349 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800350
Jim Van Vertha950b632017-09-12 11:54:11 -0400351 static uint32_t GetPlotIndexFromID(AtlasID id) {
352 return (id >> 8) & 0xff;
joshualitt5bf99f12015-03-13 11:47:42 -0700353 }
354
joshualitt8db6fdc2015-07-31 08:25:07 -0700355 // top 48 bits are reserved for the generation ID
356 static uint64_t GetGenerationFromID(AtlasID id) {
357 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700358 }
359
Brian Salomon29b60c92017-10-31 14:42:10 -0400360 inline bool updatePlot(GrDeferredUploadTarget*, AtlasID*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700361
Jim Van Vertha950b632017-09-12 11:54:11 -0400362 inline void makeMRU(Plot* plot, int pageIdx) {
363 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800364 return;
365 }
366
Jim Van Vertha950b632017-09-12 11:54:11 -0400367 fPages[pageIdx].fPlotList.remove(plot);
368 fPages[pageIdx].fPlotList.addToHead(plot);
369
Jim Van Verth106b5c42017-09-26 12:45:29 -0400370 // No MRU update for pages -- since we will always try to add from
371 // the front and remove from the back there is no need for MRU.
joshualitt5df175e2015-11-18 13:37:54 -0800372 }
joshualitt5bf99f12015-03-13 11:47:42 -0700373
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500374 bool uploadToPage(unsigned int pageIdx, AtlasID* id, GrDeferredUploadTarget* target,
375 int width, int height, const void* image, SkIPoint16* loc);
376
Robert Phillips4bc70112018-03-01 10:24:02 -0500377 bool createPages(GrProxyProvider*);
378 bool activateNewPage(GrResourceProvider*);
379 void deactivateLastPage();
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400380
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400381 void processEviction(AtlasID);
382 inline void processEvictionAndResetRects(Plot* plot) {
383 this->processEviction(plot->id());
384 plot->resetRects();
385 }
joshualitt5bf99f12015-03-13 11:47:42 -0700386
Greg Daniel4065d452018-11-16 15:43:41 -0500387 GrBackendFormat fFormat;
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400388 GrPixelConfig fPixelConfig;
389 int fTextureWidth;
390 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500391 int fPlotWidth;
392 int fPlotHeight;
Jim Van Verth06f593c2018-02-20 11:30:10 -0500393 unsigned int fNumPlots;
robertphillips2b0536f2015-11-06 14:10:42 -0800394
Robert Phillips32f28182017-02-28 16:20:03 -0500395 uint64_t fAtlasGeneration;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400396 // nextTokenToFlush() value at the end of the previous flush
Brian Salomon943ed792017-10-30 09:37:55 -0400397 GrDeferredUploadToken fPrevFlushToken;
joshualitt5bf99f12015-03-13 11:47:42 -0700398
399 struct EvictionData {
400 EvictionFunc fFunc;
401 void* fData;
402 };
403
404 SkTDArray<EvictionData> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400405
406 struct Page {
407 // allocated array of Plots
408 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
409 // LRU list of Plots (MRU at head - LRU at tail)
410 PlotList fPlotList;
411 };
412 // proxies kept separate to make it easier to pass them up to client
Brian Salomon9f545bc2017-11-06 10:36:57 -0500413 sk_sp<GrTextureProxy> fProxies[kMaxMultitexturePages];
414 Page fPages[kMaxMultitexturePages];
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500415 uint32_t fMaxPages;
Robert Phillips4bc70112018-03-01 10:24:02 -0500416
417 uint32_t fNumActivePages;
joshualitt5bf99f12015-03-13 11:47:42 -0700418};
419
Herb Derbybbf5fb52018-10-15 16:39:39 -0400420// There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In
Jim Van Verthf6206f92018-12-14 08:22:24 -0500421// general, the A8 dimensions are 2x the 565 and ARGB dimensions with the constraint that an atlas
Herb Derbybbf5fb52018-10-15 16:39:39 -0400422// size will always contain at least one plot. Since the ARGB atlas takes the most space, its
423// dimensions are used to size the other two atlases.
424class GrDrawOpAtlasConfig {
425public:
Jim Van Verthf6206f92018-12-14 08:22:24 -0500426 // The capabilities of the GPU define maxTextureSize. The client provides maxBytes, and this
427 // represents the largest they want a single atlas texture to be. Due to multitexturing, we
428 // may expand temporarily to use more space as needed.
429 GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes);
Herb Derbybbf5fb52018-10-15 16:39:39 -0400430
Jim Van Verthf6206f92018-12-14 08:22:24 -0500431 // For testing only - make minimum sized atlases -- a single plot for ARGB, four for A8
432 GrDrawOpAtlasConfig() : GrDrawOpAtlasConfig(kMaxAtlasDim, 0) {}
Herb Derbybbf5fb52018-10-15 16:39:39 -0400433
Herb Derby15d9ef22018-10-18 13:41:32 -0400434 SkISize atlasDimensions(GrMaskFormat type) const;
Jim Van Verthf6206f92018-12-14 08:22:24 -0500435 SkISize plotDimensions(GrMaskFormat type) const;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400436
437private:
Jim Van Verthf6206f92018-12-14 08:22:24 -0500438 // On some systems texture coordinates are represented using half-precision floating point,
439 // which limits the largest atlas dimensions to 2048x2048.
440 // For simplicity we'll use this constraint for all of our atlas textures.
441 // This can be revisited later if we need larger atlases.
442 static constexpr int kMaxAtlasDim = 2048;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400443
Jim Van Verthf6206f92018-12-14 08:22:24 -0500444 SkISize fARGBDimensions;
445 int fMaxTextureSize;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400446};
447
joshualitt5bf99f12015-03-13 11:47:42 -0700448#endif