blob: d16665ee5a9a032798fc3f5a513f52586f421876 [file] [log] [blame]
joshualitt5bf99f12015-03-13 11:47:42 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomon2ee084e2016-12-16 18:59:19 -05008#ifndef GrDrawOpAtlas_DEFINED
9#define GrDrawOpAtlas_DEFINED
joshualitt5bf99f12015-03-13 11:47:42 -070010
Herb Derby3c4d5332018-09-07 15:27:57 -040011#include <cmath>
Herb Derby1a496c52020-01-22 17:26:56 -050012#include <vector>
Herb Derby3c4d5332018-09-07 15:27:57 -040013
Robert Phillips51b3e602020-04-09 12:48:50 -040014#include "include/gpu/GrBackendSurface.h"
15#include "include/private/SkTArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkIPoint16.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040017#include "src/core/SkTInternalLList.h"
Robert Phillips51b3e602020-04-09 12:48:50 -040018#include "src/gpu/GrDeferredUpload.h"
Herb Derby73c75872020-01-22 18:09:16 -050019#include "src/gpu/GrRectanizerSkyline.h"
Robert Phillips51b3e602020-04-09 12:48:50 -040020#include "src/gpu/GrSurfaceProxyView.h"
21#include "src/gpu/geometry/GrRect.h"
joshualittddd22d82016-02-16 06:47:52 -080022
Robert Phillipscd5099c2018-02-09 09:56:56 -050023class GrOnFlushResourceProvider;
Robert Phillips51b3e602020-04-09 12:48:50 -040024class GrProxyProvider;
25class GrResourceProvider;
26class GrTextureProxy;
joshualittda04e0e2015-08-19 08:16:43 -070027
Brian Salomon2ee084e2016-12-16 18:59:19 -050028/**
Jim Van Verth106b5c42017-09-26 12:45:29 -040029 * This class manages one or more atlas textures on behalf of GrDrawOps. The draw ops that use the
30 * atlas perform texture uploads when preparing their draws during flush. The class provides
31 * facilities for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in
Brian Salomon29b60c92017-10-31 14:42:10 -040032 * "ASAP" mode until it is impossible to add data without overwriting texels read by draws that
Jim Van Verth106b5c42017-09-26 12:45:29 -040033 * have not yet executed on the gpu. At that point, the atlas will attempt to allocate a new
34 * atlas texture (or "page") of the same size, up to a maximum number of textures, and upload
35 * to that texture. If that's not possible, the uploads are performed "inline" between draws. If a
36 * single draw would use enough subimage space to overflow the atlas texture then the atlas will
37 * fail to add a subimage. This gives the op the chance to end the draw and begin a new one.
38 * Additional uploads will then succeed in inline mode.
39 *
40 * When the atlas has multiple pages, new uploads are prioritized to the lower index pages, i.e.,
41 * it will try to upload to page 0 before page 1 or 2. To keep the atlas from continually using
42 * excess space, periodic garbage collection is needed to shift data from the higher index pages to
43 * the lower ones, and then eventually remove any pages that are no longer in use. "In use" is
44 * determined by using the GrDrawUploadToken system: After a flush each subarea of the page
45 * is checked to see whether it was used in that flush; if it is not, a counter is incremented.
46 * Once that counter reaches a threshold that subarea is considered to be no longer in use.
47 *
48 * Garbage collection is initiated by the GrDrawOpAtlas's client via the compact() method. One
49 * solution is to make the client a subclass of GrOnFlushCallbackObject, register it with the
50 * GrContext via addOnFlushCallbackObject(), and the client's postFlush() method calls compact()
51 * and passes in the given GrDrawUploadToken.
Brian Salomon2ee084e2016-12-16 18:59:19 -050052 */
53class GrDrawOpAtlas {
Brian Salomon9f545bc2017-11-06 10:36:57 -050054private:
55 static constexpr auto kMaxMultitexturePages = 4;
56
joshualitt5bf99f12015-03-13 11:47:42 -070057public:
Brian Salomon9f545bc2017-11-06 10:36:57 -050058 /** Is the atlas allowed to use more than one texture? */
59 enum class AllowMultitexturing : bool { kNo, kYes };
60
Jim Van Verthf6206f92018-12-14 08:22:24 -050061 static constexpr int kMaxPlots = 32; // restricted by the fPlotAlreadyUpdated bitfield
62 // in BulkUseTokenUpdater
Herb Derbybbf5fb52018-10-15 16:39:39 -040063
Brian Salomon2ee084e2016-12-16 18:59:19 -050064 /**
Herb Derby4d721712020-01-24 14:31:16 -050065 * A PlotLocator specifies the plot and is analogous to a directory path:
66 * page/plot/plotGeneration
67 *
68 * In fact PlotLocator is a portion of a glyph image location in the atlas fully specified by:
69 * format/atlasGeneration/page/plot/plotGeneration/(u,v)
Brian Salomon2ee084e2016-12-16 18:59:19 -050070 */
Herb Derby4d721712020-01-24 14:31:16 -050071 typedef uint64_t PlotLocator;
72 static const uint64_t kInvalidPlotLocator = 0;
joshualitt7c3a2f82015-03-31 13:32:05 -070073 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070074
Robert Phillips6d3bc292020-04-06 10:29:28 -040075 class AtlasLocator {
76 public:
77 std::array<uint16_t, 4> getUVs(int padding) const;
78
79 // TODO: Remove the small path renderer's use of this for eviction
Robert Phillips51b3e602020-04-09 12:48:50 -040080 PlotLocator plotLocator() const { return fPlotLocator; }
Robert Phillips6d3bc292020-04-06 10:29:28 -040081
82 uint32_t pageIndex() const {
83 uint32_t pageIndex = fPlotLocator & 0xff;
84 SkASSERT(pageIndex < 4);
85 return pageIndex;
86 }
87
88 uint32_t plotIndex() const {
89 uint32_t plotIndex = (fPlotLocator >> 8) & 0xff;
90 SkASSERT(plotIndex < kMaxPlots);
91 return plotIndex;
92 }
93
94 uint64_t genID() const {
95 // top 48 bits are reserved for the generation ID
96 return (fPlotLocator >> 16) & 0xffffffffffff;
97 }
98
99 private:
100 friend class GrDrawOpAtlas;
101
Robert Phillipse87106d2020-04-09 14:21:33 -0400102 SkDEBUGCODE(void validate(const GrDrawOpAtlas*) const;)
103
Robert Phillips51b3e602020-04-09 12:48:50 -0400104 PlotLocator fPlotLocator{GrDrawOpAtlas::kInvalidPlotLocator};
105 GrIRect16 fRect{0, 0, 0, 0};
Robert Phillips6d3bc292020-04-06 10:29:28 -0400106
107 // TODO: the inset to the actual data w/in 'fRect' could also be stored in this class
108 // This would simplify the 'getUVs' call. The valid values would be 0, 1, 2 & 4.
109 };
110
Brian Salomon2ee084e2016-12-16 18:59:19 -0500111 /**
Herb Derby1a496c52020-01-22 17:26:56 -0500112 * An interface for eviction callbacks. Whenever GrDrawOpAtlas evicts a
Herb Derby4d721712020-01-24 14:31:16 -0500113 * specific PlotLocator, it will call all of the registered listeners so they can process the
Brian Salomon2ee084e2016-12-16 18:59:19 -0500114 * eviction.
115 */
Herb Derby1a496c52020-01-22 17:26:56 -0500116 class EvictionCallback {
117 public:
118 virtual ~EvictionCallback() = default;
Herb Derby4d721712020-01-24 14:31:16 -0500119 virtual void evict(PlotLocator plotLocator) = 0;
Herb Derby1a496c52020-01-22 17:26:56 -0500120 };
joshualitt5bf99f12015-03-13 11:47:42 -0700121
Robert Phillips256c37b2017-03-01 14:32:46 -0500122 /**
Herb Derby0ef780b2020-01-24 15:57:11 -0500123 * Keep track of generation number for Atlases and Plots.
124 */
125 class GenerationCounter {
126 public:
127 static constexpr uint64_t kInvalidGeneration = 0;
128 uint64_t next() {
129 return fGeneration++;
130 }
131
132 private:
133 uint64_t fGeneration{1};
134 };
135
136 /**
Robert Phillips256c37b2017-03-01 14:32:46 -0500137 * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
138 * should only be used inside of GrMeshDrawOp::onPrepareDraws.
Robert Phillips42dda082019-05-14 13:29:45 -0400139 * @param GrColorType The colorType which this atlas will store
Robert Phillips256c37b2017-03-01 14:32:46 -0500140 * @param width width in pixels of the atlas
141 * @param height height in pixels of the atlas
142 * @param numPlotsX The number of plots the atlas should be broken up into in the X
143 * direction
144 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
145 * direction
Herb Derby0ef780b2020-01-24 15:57:11 -0500146 * @param atlasGeneration a pointer to the context's generation counter.
Brian Salomon9f545bc2017-11-06 10:36:57 -0500147 * @param allowMultitexturing Can the atlas use more than one texture.
Herb Derby1a496c52020-01-22 17:26:56 -0500148 * @param evictor A pointer to an eviction callback class.
149 *
Robert Phillips256c37b2017-03-01 14:32:46 -0500150 * @return An initialized GrDrawOpAtlas, or nullptr if creation fails
151 */
Greg Daniel4065d452018-11-16 15:43:41 -0500152 static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*,
153 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -0400154 GrColorType,
Robert Phillips4bc70112018-03-01 10:24:02 -0500155 int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500156 int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -0500157 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500158 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -0500159 EvictionCallback* evictor);
joshualitt5bf99f12015-03-13 11:47:42 -0700160
Brian Salomon2ee084e2016-12-16 18:59:19 -0500161 /**
Jim Van Verthfb395102020-02-03 10:11:19 -0500162 * Packs a texture atlas page index into the uint16 texture coordinates.
163 * @param u U texture coordinate
164 * @param v V texture coordinate
165 * @param pageIndex index of the texture these coordinates apply to.
166 Must be in the range [0, 3].
167 * @return The new u and v coordinates with the packed value
168 */
169 static std::pair<uint16_t, uint16_t> PackIndexInTexCoords(uint16_t u, uint16_t v,
170 int pageIndex);
171 /**
172 * Unpacks a texture atlas page index from uint16 texture coordinates.
173 * @param u Packed U texture coordinate
174 * @param v Packed V texture coordinate
175 * @return The unpacked u and v coordinates with the page index.
176 */
177 static std::tuple<uint16_t, uint16_t, int> UnpackIndexFromTexCoords(uint16_t u, uint16_t v);
178
179 /**
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500180 * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns
181 * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if
182 * the subimage cannot fit in the atlas without overwriting texels that will be read in the
183 * current draw. This indicates that the op should end its current draw and begin another
184 * before adding more data. Upon success, an upload of the provided image data will have
185 * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode.
186 * Successive uploads in either mode may be consolidated.
187 * 'kError' will be returned when some unrecoverable error was encountered while trying to
188 * add the subimage. In this case the op being created should be discarded.
189 *
Brian Salomon2ee084e2016-12-16 18:59:19 -0500190 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
191 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
192 * addToAtlas might cause the previous data to be overwritten before it has been read.
193 */
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500194
195 enum class ErrorCode {
196 kError,
197 kSucceeded,
198 kTryAgain
199 };
200
Robert Phillips6d3bc292020-04-06 10:29:28 -0400201 ErrorCode addToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
202 int width, int height, const void* image, AtlasLocator*);
joshualitt5bf99f12015-03-13 11:47:42 -0700203
Greg Daniel9715b6c2019-12-10 15:03:10 -0500204 const GrSurfaceProxyView* getViews() const { return fViews; }
joshualitt5bf99f12015-03-13 11:47:42 -0700205
joshualitt7c3a2f82015-03-31 13:32:05 -0700206 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -0800207
Robert Phillips6d3bc292020-04-06 10:29:28 -0400208 bool hasID(const AtlasLocator& atlasLocator) {
Robert Phillipse87106d2020-04-09 14:21:33 -0400209 if (kInvalidPlotLocator == atlasLocator.plotLocator()) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500210 return false;
211 }
Herb Derby4d721712020-01-24 14:31:16 -0500212
Robert Phillips6d3bc292020-04-06 10:29:28 -0400213 uint32_t plot = atlasLocator.plotIndex();
214 uint32_t page = atlasLocator.pageIndex();
Herb Derby0ef780b2020-01-24 15:57:11 -0500215 uint64_t plotGeneration = fPages[page].fPlotArray[plot]->genID();
Robert Phillips6d3bc292020-04-06 10:29:28 -0400216 uint64_t locatorGeneration = atlasLocator.genID();
Herb Derby0ef780b2020-01-24 15:57:11 -0500217 return plot < fNumPlots && page < fNumActivePages && plotGeneration == locatorGeneration;
joshualitt5df175e2015-11-18 13:37:54 -0800218 }
joshualittb4c507e2015-04-08 08:07:59 -0700219
Brian Salomon2ee084e2016-12-16 18:59:19 -0500220 /** To ensure the atlas does not evict a given entry, the client must set the last use token. */
Robert Phillips6d3bc292020-04-06 10:29:28 -0400221 void setLastUseToken(const AtlasLocator& atlasLocator, GrDeferredUploadToken token) {
222 SkASSERT(this->hasID(atlasLocator));
223 uint32_t plotIdx = atlasLocator.plotIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400224 SkASSERT(plotIdx < fNumPlots);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400225 uint32_t pageIdx = atlasLocator.pageIndex();
Robert Phillips4bc70112018-03-01 10:24:02 -0500226 SkASSERT(pageIdx < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400227 Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
228 this->makeMRU(plot, pageIdx);
229 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800230 }
231
Robert Phillips4bc70112018-03-01 10:24:02 -0500232 uint32_t numActivePages() { return fNumActivePages; }
Jim Van Vertha950b632017-09-12 11:54:11 -0400233
Brian Salomon2ee084e2016-12-16 18:59:19 -0500234 /**
235 * 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 -0400236 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
237 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700238 */
239 class BulkUseTokenUpdater {
240 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400241 BulkUseTokenUpdater() {
242 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
243 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700244 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Robert Phillips6d3bc292020-04-06 10:29:28 -0400245 : fPlotsToUpdate(that.fPlotsToUpdate) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400246 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700247 }
248
Robert Phillips6d3bc292020-04-06 10:29:28 -0400249 bool add(const AtlasLocator& atlasLocator) {
250 int plotIdx = atlasLocator.plotIndex();
251 int pageIdx = atlasLocator.pageIndex();
252 if (this->find(pageIdx, plotIdx)) {
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500253 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700254 }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400255 this->set(pageIdx, plotIdx);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500256 return true;
joshualittb4c507e2015-04-08 08:07:59 -0700257 }
258
259 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700260 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400261 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700262 }
263
Jim Van Vertha950b632017-09-12 11:54:11 -0400264 struct PlotData {
265 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
266 uint32_t fPageIndex;
267 uint32_t fPlotIndex;
268 };
269
joshualittb4c507e2015-04-08 08:07:59 -0700270 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400271 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700272 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400273 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700274 }
275
Jim Van Vertha950b632017-09-12 11:54:11 -0400276 void set(int pageIdx, int index) {
277 SkASSERT(!this->find(pageIdx, index));
278 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
279 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700280 }
281
Jim Van Vertha950b632017-09-12 11:54:11 -0400282 static constexpr int kMinItems = 4;
Jim Van Vertha950b632017-09-12 11:54:11 -0400283 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
Jim Van Verthf6206f92018-12-14 08:22:24 -0500284 uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages]; // TODO: increase this to uint64_t
285 // to allow more plots per page
joshualittb4c507e2015-04-08 08:07:59 -0700286
Brian Salomon2ee084e2016-12-16 18:59:19 -0500287 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700288 };
289
Brian Salomon943ed792017-10-30 09:37:55 -0400290 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800291 int count = updater.fPlotsToUpdate.count();
292 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400293 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400294 // it's possible we've added a plot to the updater and subsequently the plot's page
295 // was deleted -- so we check to prevent a crash
Robert Phillips4bc70112018-03-01 10:24:02 -0500296 if (pd.fPageIndex < fNumActivePages) {
Jim Van Verth6ca9c6f2017-09-27 18:04:34 -0400297 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
298 this->makeMRU(plot, pd.fPageIndex);
299 plot->setLastUseToken(token);
300 }
joshualitt5df175e2015-11-18 13:37:54 -0800301 }
302 }
joshualittb4c507e2015-04-08 08:07:59 -0700303
Brian Salomon943ed792017-10-30 09:37:55 -0400304 void compact(GrDeferredUploadToken startTokenForNextFlush);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400305
Robert Phillipscd5099c2018-02-09 09:56:56 -0500306 void instantiate(GrOnFlushResourceProvider*);
307
Brian Salomon9f545bc2017-11-06 10:36:57 -0500308 uint32_t maxPages() const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500309 return fMaxPages;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500310 }
311
Robert Phillips4bc70112018-03-01 10:24:02 -0500312 int numAllocated_TestingOnly() const;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500313 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillips4bc70112018-03-01 10:24:02 -0500314
315private:
Robert Phillips42dda082019-05-14 13:29:45 -0400316 GrDrawOpAtlas(GrProxyProvider*, const GrBackendFormat& format, GrColorType, int width,
Herb Derby0ef780b2020-01-24 15:57:11 -0500317 int height, int plotWidth, int plotHeight, GenerationCounter* generationCounter,
Greg Daniel4065d452018-11-16 15:43:41 -0500318 AllowMultitexturing allowMultitexturing);
Robert Phillips256c37b2017-03-01 14:32:46 -0500319
Brian Salomon2ee084e2016-12-16 18:59:19 -0500320 /**
321 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
322 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
323 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
324 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
325 * used unless the last use of the Plot has already been flushed through to the gpu.
326 */
327 class Plot : public SkRefCnt {
328 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800329
330 public:
Robert Phillips6d3bc292020-04-06 10:29:28 -0400331 uint32_t pageIndex() const { return fPageIndex; }
332
333 /** plotIndex() is a unique id for the plot relative to the owning GrAtlas and page. */
334 uint32_t plotIndex() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500335 /**
336 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
337 * if a particular subimage is still present in the atlas.
338 */
joshualitt5df175e2015-11-18 13:37:54 -0800339 uint64_t genID() const { return fGenID; }
Herb Derby4d721712020-01-24 14:31:16 -0500340 GrDrawOpAtlas::PlotLocator plotLocator() const {
341 SkASSERT(GrDrawOpAtlas::kInvalidPlotLocator != fPlotLocator);
342 return fPlotLocator;
joshualitt5df175e2015-11-18 13:37:54 -0800343 }
344 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
345
Robert Phillips6d3bc292020-04-06 10:29:28 -0400346 bool addSubImage(int width, int height, const void* image, GrIRect16* rect);
joshualitt5df175e2015-11-18 13:37:54 -0800347
Brian Salomon2ee084e2016-12-16 18:59:19 -0500348 /**
349 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
350 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
351 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
352 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
353 * has already flushed through the gpu then we can reuse the plot.
354 */
Brian Salomon943ed792017-10-30 09:37:55 -0400355 GrDeferredUploadToken lastUploadToken() const { return fLastUpload; }
356 GrDeferredUploadToken lastUseToken() const { return fLastUse; }
357 void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; }
358 void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800359
Brian Salomon943ed792017-10-30 09:37:55 -0400360 void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800361 void resetRects();
362
Jim Van Verth106b5c42017-09-26 12:45:29 -0400363 int flushesSinceLastUsed() { return fFlushesSinceLastUse; }
364 void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; }
365 void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; }
366
joshualitt5df175e2015-11-18 13:37:54 -0800367 private:
Herb Derby0ef780b2020-01-24 15:57:11 -0500368 Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
369 int offX, int offY, int width, int height, GrColorType colorType);
joshualitt5df175e2015-11-18 13:37:54 -0800370
Brian Salomon2ee084e2016-12-16 18:59:19 -0500371 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800372
Brian Salomon2ee084e2016-12-16 18:59:19 -0500373 /**
374 * Create a clone of this plot. The cloned plot will take the place of the current plot in
375 * the atlas
376 */
377 Plot* clone() const {
Herb Derby0ef780b2020-01-24 15:57:11 -0500378 return new Plot(
379 fPageIndex, fPlotIndex, fGenerationCounter, fX, fY, fWidth, fHeight, fColorType);
joshualitt5df175e2015-11-18 13:37:54 -0800380 }
381
Herb Derby4d721712020-01-24 14:31:16 -0500382 static GrDrawOpAtlas::PlotLocator CreatePlotLocator(
383 uint32_t pageIdx, uint32_t plotIdx, uint64_t generation) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400384 SkASSERT(pageIdx < (1 << 8));
Brian Salomon9f545bc2017-11-06 10:36:57 -0500385 SkASSERT(pageIdx < kMaxMultitexturePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400386 SkASSERT(plotIdx < (1 << 8));
joshualitt5df175e2015-11-18 13:37:54 -0800387 SkASSERT(generation < ((uint64_t)1 << 48));
Jim Van Vertha950b632017-09-12 11:54:11 -0400388 return generation << 16 | plotIdx << 8 | pageIdx;
joshualitt5df175e2015-11-18 13:37:54 -0800389 }
390
Brian Salomon943ed792017-10-30 09:37:55 -0400391 GrDeferredUploadToken fLastUpload;
392 GrDeferredUploadToken fLastUse;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400393 // the number of flushes since this plot has been last used
394 int fFlushesSinceLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800395
Jim Van Vertha950b632017-09-12 11:54:11 -0400396 struct {
397 const uint32_t fPageIndex : 16;
398 const uint32_t fPlotIndex : 16;
399 };
Herb Derby0ef780b2020-01-24 15:57:11 -0500400 GenerationCounter* const fGenerationCounter;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500401 uint64_t fGenID;
Herb Derby4d721712020-01-24 14:31:16 -0500402 GrDrawOpAtlas::PlotLocator fPlotLocator;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500403 unsigned char* fData;
404 const int fWidth;
405 const int fHeight;
406 const int fX;
407 const int fY;
Herb Derby73c75872020-01-22 18:09:16 -0500408 GrRectanizerSkyline fRectanizer;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500409 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
Robert Phillips42dda082019-05-14 13:29:45 -0400410 const GrColorType fColorType;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500411 const size_t fBytesPerPixel;
412 SkIRect fDirtyRect;
413 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800414
Brian Salomon2ee084e2016-12-16 18:59:19 -0500415 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800416
417 typedef SkRefCnt INHERITED;
418 };
419
Brian Salomon2ee084e2016-12-16 18:59:19 -0500420 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800421
Robert Phillips6d3bc292020-04-06 10:29:28 -0400422 inline bool updatePlot(GrDeferredUploadTarget*, AtlasLocator*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700423
Jim Van Vertha950b632017-09-12 11:54:11 -0400424 inline void makeMRU(Plot* plot, int pageIdx) {
425 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800426 return;
427 }
428
Jim Van Vertha950b632017-09-12 11:54:11 -0400429 fPages[pageIdx].fPlotList.remove(plot);
430 fPages[pageIdx].fPlotList.addToHead(plot);
431
Jim Van Verth106b5c42017-09-26 12:45:29 -0400432 // No MRU update for pages -- since we will always try to add from
433 // the front and remove from the back there is no need for MRU.
joshualitt5df175e2015-11-18 13:37:54 -0800434 }
joshualitt5bf99f12015-03-13 11:47:42 -0700435
Robert Phillips6d3bc292020-04-06 10:29:28 -0400436 bool uploadToPage(const GrCaps&, unsigned int pageIdx, GrDeferredUploadTarget*,
437 int width, int height, const void* image, AtlasLocator*);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500438
Herb Derby0ef780b2020-01-24 15:57:11 -0500439 bool createPages(GrProxyProvider*, GenerationCounter*);
Robert Phillips4bc70112018-03-01 10:24:02 -0500440 bool activateNewPage(GrResourceProvider*);
441 void deactivateLastPage();
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400442
Herb Derby4d721712020-01-24 14:31:16 -0500443 void processEviction(PlotLocator);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400444 inline void processEvictionAndResetRects(Plot* plot) {
Herb Derby4d721712020-01-24 14:31:16 -0500445 this->processEviction(plot->plotLocator());
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400446 plot->resetRects();
447 }
joshualitt5bf99f12015-03-13 11:47:42 -0700448
Greg Daniel4065d452018-11-16 15:43:41 -0500449 GrBackendFormat fFormat;
Robert Phillips42dda082019-05-14 13:29:45 -0400450 GrColorType fColorType;
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400451 int fTextureWidth;
452 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500453 int fPlotWidth;
454 int fPlotHeight;
Jim Van Verth06f593c2018-02-20 11:30:10 -0500455 unsigned int fNumPlots;
robertphillips2b0536f2015-11-06 14:10:42 -0800456
Herb Derby0ef780b2020-01-24 15:57:11 -0500457 GenerationCounter* const fGenerationCounter;
458 uint64_t fAtlasGeneration;
459
Jim Van Verth106b5c42017-09-26 12:45:29 -0400460 // nextTokenToFlush() value at the end of the previous flush
Brian Salomon943ed792017-10-30 09:37:55 -0400461 GrDeferredUploadToken fPrevFlushToken;
joshualitt5bf99f12015-03-13 11:47:42 -0700462
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400463 // the number of flushes since this atlas has been last used
464 int fFlushesSinceLastUse;
465
Herb Derby1a496c52020-01-22 17:26:56 -0500466 std::vector<EvictionCallback*> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400467
468 struct Page {
469 // allocated array of Plots
470 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
471 // LRU list of Plots (MRU at head - LRU at tail)
472 PlotList fPlotList;
473 };
474 // proxies kept separate to make it easier to pass them up to client
Greg Daniel9715b6c2019-12-10 15:03:10 -0500475 GrSurfaceProxyView fViews[kMaxMultitexturePages];
Brian Salomon9f545bc2017-11-06 10:36:57 -0500476 Page fPages[kMaxMultitexturePages];
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500477 uint32_t fMaxPages;
Robert Phillips4bc70112018-03-01 10:24:02 -0500478
479 uint32_t fNumActivePages;
joshualitt5bf99f12015-03-13 11:47:42 -0700480};
481
Herb Derbybbf5fb52018-10-15 16:39:39 -0400482// There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In
Jim Van Verthf6206f92018-12-14 08:22:24 -0500483// general, the A8 dimensions are 2x the 565 and ARGB dimensions with the constraint that an atlas
Herb Derbybbf5fb52018-10-15 16:39:39 -0400484// size will always contain at least one plot. Since the ARGB atlas takes the most space, its
485// dimensions are used to size the other two atlases.
486class GrDrawOpAtlasConfig {
487public:
Jim Van Verthf6206f92018-12-14 08:22:24 -0500488 // The capabilities of the GPU define maxTextureSize. The client provides maxBytes, and this
489 // represents the largest they want a single atlas texture to be. Due to multitexturing, we
490 // may expand temporarily to use more space as needed.
491 GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes);
Herb Derbybbf5fb52018-10-15 16:39:39 -0400492
Jim Van Verthf6206f92018-12-14 08:22:24 -0500493 // For testing only - make minimum sized atlases -- a single plot for ARGB, four for A8
494 GrDrawOpAtlasConfig() : GrDrawOpAtlasConfig(kMaxAtlasDim, 0) {}
Herb Derbybbf5fb52018-10-15 16:39:39 -0400495
Herb Derby15d9ef22018-10-18 13:41:32 -0400496 SkISize atlasDimensions(GrMaskFormat type) const;
Jim Van Verthf6206f92018-12-14 08:22:24 -0500497 SkISize plotDimensions(GrMaskFormat type) const;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400498
499private:
Jim Van Verthf6206f92018-12-14 08:22:24 -0500500 // On some systems texture coordinates are represented using half-precision floating point,
501 // which limits the largest atlas dimensions to 2048x2048.
502 // For simplicity we'll use this constraint for all of our atlas textures.
503 // This can be revisited later if we need larger atlases.
504 static constexpr int kMaxAtlasDim = 2048;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400505
Jim Van Verthf6206f92018-12-14 08:22:24 -0500506 SkISize fARGBDimensions;
507 int fMaxTextureSize;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400508};
509
joshualitt5bf99f12015-03-13 11:47:42 -0700510#endif