blob: 9f44a5bd5125e7fe005751059daaee0fcdd4ad8a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkSize.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkGlyphRunPainter.h"
16#include "src/core/SkIPoint16.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040017#include "src/core/SkTInternalLList.h"
joshualitt5bf99f12015-03-13 11:47:42 -070018
Herb Derby73c75872020-01-22 18:09:16 -050019#include "src/gpu/GrRectanizerSkyline.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/ops/GrDrawOp.h"
joshualittddd22d82016-02-16 06:47:52 -080021
Robert Phillipscd5099c2018-02-09 09:56:56 -050022class GrOnFlushResourceProvider;
joshualitt5bf99f12015-03-13 11:47:42 -070023
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 /**
Herb Derby4d721712020-01-24 14:31:16 -050063 * A PlotLocator specifies the plot and is analogous to a directory path:
64 * page/plot/plotGeneration
65 *
66 * In fact PlotLocator is a portion of a glyph image location in the atlas fully specified by:
67 * format/atlasGeneration/page/plot/plotGeneration/(u,v)
Brian Salomon2ee084e2016-12-16 18:59:19 -050068 */
Herb Derby4d721712020-01-24 14:31:16 -050069 typedef uint64_t PlotLocator;
70 static const uint64_t kInvalidPlotLocator = 0;
joshualitt7c3a2f82015-03-31 13:32:05 -070071 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070072
Brian Salomon2ee084e2016-12-16 18:59:19 -050073 /**
Herb Derby1a496c52020-01-22 17:26:56 -050074 * An interface for eviction callbacks. Whenever GrDrawOpAtlas evicts a
Herb Derby4d721712020-01-24 14:31:16 -050075 * specific PlotLocator, it will call all of the registered listeners so they can process the
Brian Salomon2ee084e2016-12-16 18:59:19 -050076 * eviction.
77 */
Herb Derby1a496c52020-01-22 17:26:56 -050078 class EvictionCallback {
79 public:
80 virtual ~EvictionCallback() = default;
Herb Derby4d721712020-01-24 14:31:16 -050081 virtual void evict(PlotLocator plotLocator) = 0;
Herb Derby1a496c52020-01-22 17:26:56 -050082 };
joshualitt5bf99f12015-03-13 11:47:42 -070083
Robert Phillips256c37b2017-03-01 14:32:46 -050084 /**
Herb Derby0ef780b2020-01-24 15:57:11 -050085 * Keep track of generation number for Atlases and Plots.
86 */
87 class GenerationCounter {
88 public:
89 static constexpr uint64_t kInvalidGeneration = 0;
90 uint64_t next() {
91 return fGeneration++;
92 }
93
94 private:
95 uint64_t fGeneration{1};
96 };
97
98 /**
Robert Phillips256c37b2017-03-01 14:32:46 -050099 * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
100 * should only be used inside of GrMeshDrawOp::onPrepareDraws.
Robert Phillips42dda082019-05-14 13:29:45 -0400101 * @param GrColorType The colorType which this atlas will store
Robert Phillips256c37b2017-03-01 14:32:46 -0500102 * @param width width in pixels of the atlas
103 * @param height height in pixels of the atlas
104 * @param numPlotsX The number of plots the atlas should be broken up into in the X
105 * direction
106 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
107 * direction
Herb Derby0ef780b2020-01-24 15:57:11 -0500108 * @param atlasGeneration a pointer to the context's generation counter.
Brian Salomon9f545bc2017-11-06 10:36:57 -0500109 * @param allowMultitexturing Can the atlas use more than one texture.
Herb Derby1a496c52020-01-22 17:26:56 -0500110 * @param evictor A pointer to an eviction callback class.
111 *
Robert Phillips256c37b2017-03-01 14:32:46 -0500112 * @return An initialized GrDrawOpAtlas, or nullptr if creation fails
113 */
Greg Daniel4065d452018-11-16 15:43:41 -0500114 static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*,
115 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -0400116 GrColorType,
Robert Phillips4bc70112018-03-01 10:24:02 -0500117 int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500118 int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -0500119 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500120 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -0500121 EvictionCallback* evictor);
joshualitt5bf99f12015-03-13 11:47:42 -0700122
Brian Salomon2ee084e2016-12-16 18:59:19 -0500123 /**
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500124 * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns
125 * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if
126 * the subimage cannot fit in the atlas without overwriting texels that will be read in the
127 * current draw. This indicates that the op should end its current draw and begin another
128 * before adding more data. Upon success, an upload of the provided image data will have
129 * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode.
130 * Successive uploads in either mode may be consolidated.
131 * 'kError' will be returned when some unrecoverable error was encountered while trying to
132 * add the subimage. In this case the op being created should be discarded.
133 *
Brian Salomon2ee084e2016-12-16 18:59:19 -0500134 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
135 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
136 * addToAtlas might cause the previous data to be overwritten before it has been read.
137 */
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500138
139 enum class ErrorCode {
140 kError,
141 kSucceeded,
142 kTryAgain
143 };
144
Herb Derby4d721712020-01-24 14:31:16 -0500145 ErrorCode addToAtlas(GrResourceProvider*, PlotLocator*, GrDeferredUploadTarget*,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500146 int width, int height,
147 const void* image, SkIPoint16* loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700148
Greg Daniel9715b6c2019-12-10 15:03:10 -0500149 const GrSurfaceProxyView* getViews() const { return fViews; }
joshualitt5bf99f12015-03-13 11:47:42 -0700150
joshualitt7c3a2f82015-03-31 13:32:05 -0700151 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -0800152
Herb Derby4d721712020-01-24 14:31:16 -0500153 bool hasID(PlotLocator plotLocator) {
154 if (kInvalidPlotLocator == plotLocator) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500155 return false;
156 }
Herb Derby4d721712020-01-24 14:31:16 -0500157
158 uint32_t plot = GetPlotIndexFromID(plotLocator);
Herb Derby4d721712020-01-24 14:31:16 -0500159 uint32_t page = GetPageIndexFromID(plotLocator);
Herb Derby0ef780b2020-01-24 15:57:11 -0500160 uint64_t plotGeneration = fPages[page].fPlotArray[plot]->genID();
161 uint64_t locatorGeneration = GetGenerationFromID(plotLocator);
162 return plot < fNumPlots && page < fNumActivePages && plotGeneration == locatorGeneration;
joshualitt5df175e2015-11-18 13:37:54 -0800163 }
joshualittb4c507e2015-04-08 08:07:59 -0700164
Brian Salomon2ee084e2016-12-16 18:59:19 -0500165 /** To ensure the atlas does not evict a given entry, the client must set the last use token. */
Herb Derby4d721712020-01-24 14:31:16 -0500166 void setLastUseToken(PlotLocator plotLocator, GrDeferredUploadToken token) {
167 SkASSERT(this->hasID(plotLocator));
168 uint32_t plotIdx = GetPlotIndexFromID(plotLocator);
Jim Van Vertha950b632017-09-12 11:54:11 -0400169 SkASSERT(plotIdx < fNumPlots);
Herb Derby4d721712020-01-24 14:31:16 -0500170 uint32_t pageIdx = GetPageIndexFromID(plotLocator);
Robert Phillips4bc70112018-03-01 10:24:02 -0500171 SkASSERT(pageIdx < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400172 Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
173 this->makeMRU(plot, pageIdx);
174 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800175 }
176
Robert Phillips4bc70112018-03-01 10:24:02 -0500177 uint32_t numActivePages() { return fNumActivePages; }
Jim Van Vertha950b632017-09-12 11:54:11 -0400178
Brian Salomon2ee084e2016-12-16 18:59:19 -0500179 /**
180 * 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 -0400181 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
182 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700183 */
184 class BulkUseTokenUpdater {
185 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400186 BulkUseTokenUpdater() {
187 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
188 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700189 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Jim Van Vertha950b632017-09-12 11:54:11 -0400190 : fPlotsToUpdate(that.fPlotsToUpdate) {
191 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700192 }
193
Herb Derby4d721712020-01-24 14:31:16 -0500194 bool add(PlotLocator plotLocator) {
195 int index = GrDrawOpAtlas::GetPlotIndexFromID(plotLocator);
196 int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(plotLocator);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500197 if (this->find(pageIdx, index)) {
198 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700199 }
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500200 this->set(pageIdx, index);
201 return true;
joshualittb4c507e2015-04-08 08:07:59 -0700202 }
203
204 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700205 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400206 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700207 }
208
Jim Van Vertha950b632017-09-12 11:54:11 -0400209 struct PlotData {
210 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
211 uint32_t fPageIndex;
212 uint32_t fPlotIndex;
213 };
214
joshualittb4c507e2015-04-08 08:07:59 -0700215 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400216 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700217 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400218 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700219 }
220
Jim Van Vertha950b632017-09-12 11:54:11 -0400221 void set(int pageIdx, int index) {
222 SkASSERT(!this->find(pageIdx, index));
223 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
224 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700225 }
226
Jim Van Vertha950b632017-09-12 11:54:11 -0400227 static constexpr int kMinItems = 4;
Jim Van Vertha950b632017-09-12 11:54:11 -0400228 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
Jim Van Verthf6206f92018-12-14 08:22:24 -0500229 uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages]; // TODO: increase this to uint64_t
230 // to allow more plots per page
joshualittb4c507e2015-04-08 08:07:59 -0700231
Brian Salomon2ee084e2016-12-16 18:59:19 -0500232 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700233 };
234
Brian Salomon943ed792017-10-30 09:37:55 -0400235 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800236 int count = updater.fPlotsToUpdate.count();
237 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400238 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400239 // it's possible we've added a plot to the updater and subsequently the plot's page
240 // was deleted -- so we check to prevent a crash
Robert Phillips4bc70112018-03-01 10:24:02 -0500241 if (pd.fPageIndex < fNumActivePages) {
Jim Van Verth6ca9c6f2017-09-27 18:04:34 -0400242 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
243 this->makeMRU(plot, pd.fPageIndex);
244 plot->setLastUseToken(token);
245 }
joshualitt5df175e2015-11-18 13:37:54 -0800246 }
247 }
joshualittb4c507e2015-04-08 08:07:59 -0700248
Brian Salomon943ed792017-10-30 09:37:55 -0400249 void compact(GrDeferredUploadToken startTokenForNextFlush);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400250
Herb Derby4d721712020-01-24 14:31:16 -0500251 static uint32_t GetPageIndexFromID(PlotLocator plotLocator) {
252 return plotLocator & 0xff;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400253 }
254
Robert Phillipscd5099c2018-02-09 09:56:56 -0500255 void instantiate(GrOnFlushResourceProvider*);
256
Brian Salomon9f545bc2017-11-06 10:36:57 -0500257 uint32_t maxPages() const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500258 return fMaxPages;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500259 }
260
Robert Phillips4bc70112018-03-01 10:24:02 -0500261 int numAllocated_TestingOnly() const;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500262 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillips4bc70112018-03-01 10:24:02 -0500263
264private:
Robert Phillips42dda082019-05-14 13:29:45 -0400265 GrDrawOpAtlas(GrProxyProvider*, const GrBackendFormat& format, GrColorType, int width,
Herb Derby0ef780b2020-01-24 15:57:11 -0500266 int height, int plotWidth, int plotHeight, GenerationCounter* generationCounter,
Greg Daniel4065d452018-11-16 15:43:41 -0500267 AllowMultitexturing allowMultitexturing);
Robert Phillips256c37b2017-03-01 14:32:46 -0500268
Brian Salomon2ee084e2016-12-16 18:59:19 -0500269 /**
270 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
271 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
272 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
273 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
274 * used unless the last use of the Plot has already been flushed through to the gpu.
275 */
276 class Plot : public SkRefCnt {
277 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800278
279 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400280 /** index() is a unique id for the plot relative to the owning GrAtlas and page. */
281 uint32_t index() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500282 /**
283 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
284 * if a particular subimage is still present in the atlas.
285 */
joshualitt5df175e2015-11-18 13:37:54 -0800286 uint64_t genID() const { return fGenID; }
Herb Derby4d721712020-01-24 14:31:16 -0500287 GrDrawOpAtlas::PlotLocator plotLocator() const {
288 SkASSERT(GrDrawOpAtlas::kInvalidPlotLocator != fPlotLocator);
289 return fPlotLocator;
joshualitt5df175e2015-11-18 13:37:54 -0800290 }
291 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
292
293 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
294
Brian Salomon2ee084e2016-12-16 18:59:19 -0500295 /**
296 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
297 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
298 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
299 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
300 * has already flushed through the gpu then we can reuse the plot.
301 */
Brian Salomon943ed792017-10-30 09:37:55 -0400302 GrDeferredUploadToken lastUploadToken() const { return fLastUpload; }
303 GrDeferredUploadToken lastUseToken() const { return fLastUse; }
304 void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; }
305 void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800306
Brian Salomon943ed792017-10-30 09:37:55 -0400307 void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800308 void resetRects();
309
Jim Van Verth106b5c42017-09-26 12:45:29 -0400310 int flushesSinceLastUsed() { return fFlushesSinceLastUse; }
311 void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; }
312 void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; }
313
joshualitt5df175e2015-11-18 13:37:54 -0800314 private:
Herb Derby0ef780b2020-01-24 15:57:11 -0500315 Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
316 int offX, int offY, int width, int height, GrColorType colorType);
joshualitt5df175e2015-11-18 13:37:54 -0800317
Brian Salomon2ee084e2016-12-16 18:59:19 -0500318 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800319
Brian Salomon2ee084e2016-12-16 18:59:19 -0500320 /**
321 * Create a clone of this plot. The cloned plot will take the place of the current plot in
322 * the atlas
323 */
324 Plot* clone() const {
Herb Derby0ef780b2020-01-24 15:57:11 -0500325 return new Plot(
326 fPageIndex, fPlotIndex, fGenerationCounter, fX, fY, fWidth, fHeight, fColorType);
joshualitt5df175e2015-11-18 13:37:54 -0800327 }
328
Herb Derby4d721712020-01-24 14:31:16 -0500329 static GrDrawOpAtlas::PlotLocator CreatePlotLocator(
330 uint32_t pageIdx, uint32_t plotIdx, uint64_t generation) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400331 SkASSERT(pageIdx < (1 << 8));
Brian Salomon9f545bc2017-11-06 10:36:57 -0500332 SkASSERT(pageIdx < kMaxMultitexturePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400333 SkASSERT(plotIdx < (1 << 8));
joshualitt5df175e2015-11-18 13:37:54 -0800334 SkASSERT(generation < ((uint64_t)1 << 48));
Jim Van Vertha950b632017-09-12 11:54:11 -0400335 return generation << 16 | plotIdx << 8 | pageIdx;
joshualitt5df175e2015-11-18 13:37:54 -0800336 }
337
Brian Salomon943ed792017-10-30 09:37:55 -0400338 GrDeferredUploadToken fLastUpload;
339 GrDeferredUploadToken fLastUse;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400340 // the number of flushes since this plot has been last used
341 int fFlushesSinceLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800342
Jim Van Vertha950b632017-09-12 11:54:11 -0400343 struct {
344 const uint32_t fPageIndex : 16;
345 const uint32_t fPlotIndex : 16;
346 };
Herb Derby0ef780b2020-01-24 15:57:11 -0500347 GenerationCounter* const fGenerationCounter;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500348 uint64_t fGenID;
Herb Derby4d721712020-01-24 14:31:16 -0500349 GrDrawOpAtlas::PlotLocator fPlotLocator;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500350 unsigned char* fData;
351 const int fWidth;
352 const int fHeight;
353 const int fX;
354 const int fY;
Herb Derby73c75872020-01-22 18:09:16 -0500355 GrRectanizerSkyline fRectanizer;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500356 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
Robert Phillips42dda082019-05-14 13:29:45 -0400357 const GrColorType fColorType;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500358 const size_t fBytesPerPixel;
359 SkIRect fDirtyRect;
360 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800361
Brian Salomon2ee084e2016-12-16 18:59:19 -0500362 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800363
364 typedef SkRefCnt INHERITED;
365 };
366
Brian Salomon2ee084e2016-12-16 18:59:19 -0500367 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800368
Herb Derby4d721712020-01-24 14:31:16 -0500369 static uint32_t GetPlotIndexFromID(PlotLocator plotLocator) {
370 return (plotLocator >> 8) & 0xff;
joshualitt5bf99f12015-03-13 11:47:42 -0700371 }
372
joshualitt8db6fdc2015-07-31 08:25:07 -0700373 // top 48 bits are reserved for the generation ID
Herb Derby4d721712020-01-24 14:31:16 -0500374 static uint64_t GetGenerationFromID(PlotLocator plotLocator) {
375 return (plotLocator >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700376 }
377
Herb Derby4d721712020-01-24 14:31:16 -0500378 inline bool updatePlot(GrDeferredUploadTarget*, PlotLocator*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700379
Jim Van Vertha950b632017-09-12 11:54:11 -0400380 inline void makeMRU(Plot* plot, int pageIdx) {
381 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800382 return;
383 }
384
Jim Van Vertha950b632017-09-12 11:54:11 -0400385 fPages[pageIdx].fPlotList.remove(plot);
386 fPages[pageIdx].fPlotList.addToHead(plot);
387
Jim Van Verth106b5c42017-09-26 12:45:29 -0400388 // No MRU update for pages -- since we will always try to add from
389 // the front and remove from the back there is no need for MRU.
joshualitt5df175e2015-11-18 13:37:54 -0800390 }
joshualitt5bf99f12015-03-13 11:47:42 -0700391
Herb Derby4d721712020-01-24 14:31:16 -0500392 bool uploadToPage(const GrCaps&, unsigned int pageIdx, PlotLocator* plotLocator,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400393 GrDeferredUploadTarget* target, int width, int height, const void* image,
394 SkIPoint16* loc);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500395
Herb Derby0ef780b2020-01-24 15:57:11 -0500396 bool createPages(GrProxyProvider*, GenerationCounter*);
Robert Phillips4bc70112018-03-01 10:24:02 -0500397 bool activateNewPage(GrResourceProvider*);
398 void deactivateLastPage();
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400399
Herb Derby4d721712020-01-24 14:31:16 -0500400 void processEviction(PlotLocator);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400401 inline void processEvictionAndResetRects(Plot* plot) {
Herb Derby4d721712020-01-24 14:31:16 -0500402 this->processEviction(plot->plotLocator());
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400403 plot->resetRects();
404 }
joshualitt5bf99f12015-03-13 11:47:42 -0700405
Greg Daniel4065d452018-11-16 15:43:41 -0500406 GrBackendFormat fFormat;
Robert Phillips42dda082019-05-14 13:29:45 -0400407 GrColorType fColorType;
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400408 int fTextureWidth;
409 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500410 int fPlotWidth;
411 int fPlotHeight;
Jim Van Verth06f593c2018-02-20 11:30:10 -0500412 unsigned int fNumPlots;
robertphillips2b0536f2015-11-06 14:10:42 -0800413
Herb Derby0ef780b2020-01-24 15:57:11 -0500414 GenerationCounter* const fGenerationCounter;
415 uint64_t fAtlasGeneration;
416
Jim Van Verth106b5c42017-09-26 12:45:29 -0400417 // nextTokenToFlush() value at the end of the previous flush
Brian Salomon943ed792017-10-30 09:37:55 -0400418 GrDeferredUploadToken fPrevFlushToken;
joshualitt5bf99f12015-03-13 11:47:42 -0700419
Herb Derby1a496c52020-01-22 17:26:56 -0500420 std::vector<EvictionCallback*> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400421
422 struct Page {
423 // allocated array of Plots
424 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
425 // LRU list of Plots (MRU at head - LRU at tail)
426 PlotList fPlotList;
427 };
428 // proxies kept separate to make it easier to pass them up to client
Greg Daniel9715b6c2019-12-10 15:03:10 -0500429 GrSurfaceProxyView fViews[kMaxMultitexturePages];
Brian Salomon9f545bc2017-11-06 10:36:57 -0500430 Page fPages[kMaxMultitexturePages];
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500431 uint32_t fMaxPages;
Robert Phillips4bc70112018-03-01 10:24:02 -0500432
433 uint32_t fNumActivePages;
joshualitt5bf99f12015-03-13 11:47:42 -0700434};
435
Herb Derbybbf5fb52018-10-15 16:39:39 -0400436// There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In
Jim Van Verthf6206f92018-12-14 08:22:24 -0500437// general, the A8 dimensions are 2x the 565 and ARGB dimensions with the constraint that an atlas
Herb Derbybbf5fb52018-10-15 16:39:39 -0400438// size will always contain at least one plot. Since the ARGB atlas takes the most space, its
439// dimensions are used to size the other two atlases.
440class GrDrawOpAtlasConfig {
441public:
Jim Van Verthf6206f92018-12-14 08:22:24 -0500442 // The capabilities of the GPU define maxTextureSize. The client provides maxBytes, and this
443 // represents the largest they want a single atlas texture to be. Due to multitexturing, we
444 // may expand temporarily to use more space as needed.
445 GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes);
Herb Derbybbf5fb52018-10-15 16:39:39 -0400446
Jim Van Verthf6206f92018-12-14 08:22:24 -0500447 // For testing only - make minimum sized atlases -- a single plot for ARGB, four for A8
448 GrDrawOpAtlasConfig() : GrDrawOpAtlasConfig(kMaxAtlasDim, 0) {}
Herb Derbybbf5fb52018-10-15 16:39:39 -0400449
Herb Derby15d9ef22018-10-18 13:41:32 -0400450 SkISize atlasDimensions(GrMaskFormat type) const;
Jim Van Verthf6206f92018-12-14 08:22:24 -0500451 SkISize plotDimensions(GrMaskFormat type) const;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400452
453private:
Jim Van Verthf6206f92018-12-14 08:22:24 -0500454 // On some systems texture coordinates are represented using half-precision floating point,
455 // which limits the largest atlas dimensions to 2048x2048.
456 // For simplicity we'll use this constraint for all of our atlas textures.
457 // This can be revisited later if we need larger atlases.
458 static constexpr int kMaxAtlasDim = 2048;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400459
Jim Van Verthf6206f92018-12-14 08:22:24 -0500460 SkISize fARGBDimensions;
461 int fMaxTextureSize;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400462};
463
joshualitt5bf99f12015-03-13 11:47:42 -0700464#endif