blob: 41807b3d8ea3bace47545eab4d0ca4dbccaf92d1 [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
Herb Derby3c4d5332018-09-07 15:27:57 -040024// There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In
25// general, the A8 dimensions are NxN and 565 and ARGB are N/2xN with the constraint that an atlas
26// size will always contain at least one plot. Since the ARGB atlas takes the most space, its
27// dimensions are used to size the other two atlases.
28class GrDrawOpAtlasConfig {
29public:
30 GrDrawOpAtlasConfig(int maxDimension, size_t maxBytes)
31 : fPlotsPerLongDimension{PlotsPerLongDimensionForARGB(maxDimension, maxBytes)} {
32 SkASSERT(kPlotSize >= SkGlyphCacheCommon::kSkSideTooBigForAtlas);
33 }
34
35 // For testing only - make minimum sized atlases -- 1x1 plots wide.
36 GrDrawOpAtlasConfig() : fPlotsPerLongDimension{1} {
37 SkASSERT(kPlotSize >= SkGlyphCacheCommon::kSkSideTooBigForAtlas);
38 }
39
40 SkISize numPlots(GrMaskFormat type) const {
41 switch(type) {
42 case kA8_GrMaskFormat:
43 return {fPlotsPerLongDimension, fPlotsPerLongDimension};
44 case kA565_GrMaskFormat:
45 case kARGB_GrMaskFormat: {
46 int plotsPerWidth = std::max(1, fPlotsPerLongDimension / 2);
47 return {plotsPerWidth, fPlotsPerLongDimension};
48 }
49 }
50
51 // This make some compilers happy.
52 return {1,1};
53 }
54
55 SkISize atlasDimensions(GrMaskFormat type) const {
56 SkISize plots = this->numPlots(type);
57 return {plots.width() * kPlotSize, plots.height() * kPlotSize};
58 }
59
60private:
61 static int PlotsPerLongDimensionForARGB(size_t maxDimension, size_t maxBytes) {
62 // Find the largest area of pixels in a width:height with a proportion of 1:2 that fits in
63 // maxTextureBytes. In the following P is pixel size, H is height, and W is width.
64 // P*H*W = maxTextureSize => P*H*(H/2) = maxTextureSize => H = sqrt(2*maxTextureSize/P)
65 double fitsHeight =
66 std::sqrt(2.0 * maxBytes / GrMaskFormatBytesPerPixel(kARGB_GrMaskFormat));
67
68 // Because of limitations of the distance field text, the largest an atlas can be is 2048.
69 maxDimension = std::min(maxDimension, SkTo<size_t>(2048));
70
71 // Limit height to the maximum texture dimension and the minimum atlas size.
72 double height = std::max(std::min(fitsHeight, (double)maxDimension), (double)kPlotSize);
73
74 // Find the greatest power of 2 that is less than height.
75 double alignedHeight = std::exp2(std::floor(std::log2(height)));
76
77 // Calculate the atlas dimensions.
78 return (int)alignedHeight / kPlotSize;
79 }
80
81 // The width and height of a plot.
82 static constexpr int kPlotSize = 512;
83
84 // This is the height (longest dimension) of the ARGB atlas divided by the plot size.
85 const int fPlotsPerLongDimension;
joshualittda04e0e2015-08-19 08:16:43 -070086};
87
Brian Salomon2ee084e2016-12-16 18:59:19 -050088/**
Jim Van Verth106b5c42017-09-26 12:45:29 -040089 * This class manages one or more atlas textures on behalf of GrDrawOps. The draw ops that use the
90 * atlas perform texture uploads when preparing their draws during flush. The class provides
91 * facilities for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in
Brian Salomon29b60c92017-10-31 14:42:10 -040092 * "ASAP" mode until it is impossible to add data without overwriting texels read by draws that
Jim Van Verth106b5c42017-09-26 12:45:29 -040093 * have not yet executed on the gpu. At that point, the atlas will attempt to allocate a new
94 * atlas texture (or "page") of the same size, up to a maximum number of textures, and upload
95 * to that texture. If that's not possible, the uploads are performed "inline" between draws. If a
96 * single draw would use enough subimage space to overflow the atlas texture then the atlas will
97 * fail to add a subimage. This gives the op the chance to end the draw and begin a new one.
98 * Additional uploads will then succeed in inline mode.
99 *
100 * When the atlas has multiple pages, new uploads are prioritized to the lower index pages, i.e.,
101 * it will try to upload to page 0 before page 1 or 2. To keep the atlas from continually using
102 * excess space, periodic garbage collection is needed to shift data from the higher index pages to
103 * the lower ones, and then eventually remove any pages that are no longer in use. "In use" is
104 * determined by using the GrDrawUploadToken system: After a flush each subarea of the page
105 * is checked to see whether it was used in that flush; if it is not, a counter is incremented.
106 * Once that counter reaches a threshold that subarea is considered to be no longer in use.
107 *
108 * Garbage collection is initiated by the GrDrawOpAtlas's client via the compact() method. One
109 * solution is to make the client a subclass of GrOnFlushCallbackObject, register it with the
110 * GrContext via addOnFlushCallbackObject(), and the client's postFlush() method calls compact()
111 * and passes in the given GrDrawUploadToken.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500112 */
113class GrDrawOpAtlas {
Brian Salomon9f545bc2017-11-06 10:36:57 -0500114private:
115 static constexpr auto kMaxMultitexturePages = 4;
116
Herb Derby3c4d5332018-09-07 15:27:57 -0400117
joshualitt5bf99f12015-03-13 11:47:42 -0700118public:
Brian Salomon9f545bc2017-11-06 10:36:57 -0500119 /** Is the atlas allowed to use more than one texture? */
120 enum class AllowMultitexturing : bool { kNo, kYes };
121
Brian Salomon2ee084e2016-12-16 18:59:19 -0500122 /**
123 * An AtlasID is an opaque handle which callers can use to determine if the atlas contains
124 * a specific piece of data.
125 */
joshualitt8db6fdc2015-07-31 08:25:07 -0700126 typedef uint64_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -0700127 static const uint32_t kInvalidAtlasID = 0;
128 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700129
Brian Salomon2ee084e2016-12-16 18:59:19 -0500130 /**
131 * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a
132 * specific AtlasID, it will call all of the registered listeners so they can process the
133 * eviction.
134 */
135 typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*);
joshualitt5bf99f12015-03-13 11:47:42 -0700136
Robert Phillips256c37b2017-03-01 14:32:46 -0500137 /**
138 * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
139 * should only be used inside of GrMeshDrawOp::onPrepareDraws.
140 * @param GrPixelConfig The pixel config which this atlas will store
141 * @param width width in pixels of the atlas
142 * @param height height in pixels of the atlas
143 * @param numPlotsX The number of plots the atlas should be broken up into in the X
144 * direction
145 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
146 * direction
Brian Salomon9f545bc2017-11-06 10:36:57 -0500147 * @param allowMultitexturing Can the atlas use more than one texture.
Robert Phillips256c37b2017-03-01 14:32:46 -0500148 * @param func An eviction function which will be called whenever the atlas has to
149 * evict data
Brian Salomon9f545bc2017-11-06 10:36:57 -0500150 * @param data User supplied data which will be passed into func whenever an
Robert Phillips256c37b2017-03-01 14:32:46 -0500151 * eviction occurs
152 * @return An initialized GrDrawOpAtlas, or nullptr if creation fails
153 */
Robert Phillips4bc70112018-03-01 10:24:02 -0500154 static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*, GrPixelConfig,
155 int width, int height,
Robert Phillips256c37b2017-03-01 14:32:46 -0500156 int numPlotsX, int numPlotsY,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500157 AllowMultitexturing allowMultitexturing,
Robert Phillips256c37b2017-03-01 14:32:46 -0500158 GrDrawOpAtlas::EvictionFunc func, void* data);
joshualitt5bf99f12015-03-13 11:47:42 -0700159
Brian Salomon2ee084e2016-12-16 18:59:19 -0500160 /**
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500161 * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns
162 * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if
163 * the subimage cannot fit in the atlas without overwriting texels that will be read in the
164 * current draw. This indicates that the op should end its current draw and begin another
165 * before adding more data. Upon success, an upload of the provided image data will have
166 * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode.
167 * Successive uploads in either mode may be consolidated.
168 * 'kError' will be returned when some unrecoverable error was encountered while trying to
169 * add the subimage. In this case the op being created should be discarded.
170 *
Brian Salomon2ee084e2016-12-16 18:59:19 -0500171 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
172 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
173 * addToAtlas might cause the previous data to be overwritten before it has been read.
174 */
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500175
176 enum class ErrorCode {
177 kError,
178 kSucceeded,
179 kTryAgain
180 };
181
182 ErrorCode addToAtlas(GrResourceProvider*, AtlasID*, GrDeferredUploadTarget*,
183 int width, int height,
184 const void* image, SkIPoint16* loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700185
Jim Van Vertha950b632017-09-12 11:54:11 -0400186 const sk_sp<GrTextureProxy>* getProxies() const { return fProxies; }
joshualitt5bf99f12015-03-13 11:47:42 -0700187
joshualitt7c3a2f82015-03-31 13:32:05 -0700188 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -0800189
190 inline bool hasID(AtlasID id) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500191 if (kInvalidAtlasID == id) {
192 return false;
193 }
Jim Van Vertha950b632017-09-12 11:54:11 -0400194 uint32_t plot = GetPlotIndexFromID(id);
195 SkASSERT(plot < fNumPlots);
196 uint32_t page = GetPageIndexFromID(id);
Robert Phillips4bc70112018-03-01 10:24:02 -0500197 SkASSERT(page < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400198 return fPages[page].fPlotArray[plot]->genID() == GetGenerationFromID(id);
joshualitt5df175e2015-11-18 13:37:54 -0800199 }
joshualittb4c507e2015-04-08 08:07:59 -0700200
Brian Salomon2ee084e2016-12-16 18:59:19 -0500201 /** 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 -0400202 inline void setLastUseToken(AtlasID id, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800203 SkASSERT(this->hasID(id));
Jim Van Vertha950b632017-09-12 11:54:11 -0400204 uint32_t plotIdx = GetPlotIndexFromID(id);
205 SkASSERT(plotIdx < fNumPlots);
206 uint32_t pageIdx = GetPageIndexFromID(id);
Robert Phillips4bc70112018-03-01 10:24:02 -0500207 SkASSERT(pageIdx < fNumActivePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400208 Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
209 this->makeMRU(plot, pageIdx);
210 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800211 }
212
213 inline void registerEvictionCallback(EvictionFunc func, void* userData) {
joshualitt5bf99f12015-03-13 11:47:42 -0700214 EvictionData* data = fEvictionCallbacks.append();
215 data->fFunc = func;
216 data->fData = userData;
217 }
218
Robert Phillips4bc70112018-03-01 10:24:02 -0500219 uint32_t numActivePages() { return fNumActivePages; }
Jim Van Vertha950b632017-09-12 11:54:11 -0400220
Brian Salomon2ee084e2016-12-16 18:59:19 -0500221 /**
222 * 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 -0400223 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
224 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700225 */
226 class BulkUseTokenUpdater {
227 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400228 BulkUseTokenUpdater() {
229 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
230 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700231 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Jim Van Vertha950b632017-09-12 11:54:11 -0400232 : fPlotsToUpdate(that.fPlotsToUpdate) {
233 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700234 }
235
joshualittb4c507e2015-04-08 08:07:59 -0700236 void add(AtlasID id) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400237 int index = GrDrawOpAtlas::GetPlotIndexFromID(id);
238 int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id);
239 if (!this->find(pageIdx, index)) {
240 this->set(pageIdx, index);
joshualittb4c507e2015-04-08 08:07:59 -0700241 }
242 }
243
244 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700245 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400246 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700247 }
248
Jim Van Vertha950b632017-09-12 11:54:11 -0400249 struct PlotData {
250 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
251 uint32_t fPageIndex;
252 uint32_t fPlotIndex;
253 };
254
joshualittb4c507e2015-04-08 08:07:59 -0700255 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400256 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700257 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400258 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700259 }
260
Jim Van Vertha950b632017-09-12 11:54:11 -0400261 void set(int pageIdx, int index) {
262 SkASSERT(!this->find(pageIdx, index));
263 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
264 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700265 }
266
Jim Van Vertha950b632017-09-12 11:54:11 -0400267 static constexpr int kMinItems = 4;
268 static constexpr int kMaxPlots = 32;
269 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500270 uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages];
joshualittb4c507e2015-04-08 08:07:59 -0700271
Brian Salomon2ee084e2016-12-16 18:59:19 -0500272 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700273 };
274
Brian Salomon943ed792017-10-30 09:37:55 -0400275 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800276 int count = updater.fPlotsToUpdate.count();
277 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400278 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400279 // it's possible we've added a plot to the updater and subsequently the plot's page
280 // was deleted -- so we check to prevent a crash
Robert Phillips4bc70112018-03-01 10:24:02 -0500281 if (pd.fPageIndex < fNumActivePages) {
Jim Van Verth6ca9c6f2017-09-27 18:04:34 -0400282 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
283 this->makeMRU(plot, pd.fPageIndex);
284 plot->setLastUseToken(token);
285 }
joshualitt5df175e2015-11-18 13:37:54 -0800286 }
287 }
joshualittb4c507e2015-04-08 08:07:59 -0700288
Brian Salomon943ed792017-10-30 09:37:55 -0400289 void compact(GrDeferredUploadToken startTokenForNextFlush);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400290
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400291 static uint32_t GetPageIndexFromID(AtlasID id) {
292 return id & 0xff;
293 }
294
Robert Phillipscd5099c2018-02-09 09:56:56 -0500295 void instantiate(GrOnFlushResourceProvider*);
296
Brian Salomon9f545bc2017-11-06 10:36:57 -0500297 uint32_t maxPages() const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500298 return fMaxPages;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500299 }
300
Robert Phillips4bc70112018-03-01 10:24:02 -0500301 int numAllocated_TestingOnly() const;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500302 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillips4bc70112018-03-01 10:24:02 -0500303
304private:
305 GrDrawOpAtlas(GrProxyProvider*, GrPixelConfig, int width, int height, int numPlotsX,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500306 int numPlotsY, AllowMultitexturing allowMultitexturing);
Robert Phillips256c37b2017-03-01 14:32:46 -0500307
Brian Salomon2ee084e2016-12-16 18:59:19 -0500308 /**
309 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
310 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
311 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
312 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
313 * used unless the last use of the Plot has already been flushed through to the gpu.
314 */
315 class Plot : public SkRefCnt {
316 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800317
318 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400319 /** index() is a unique id for the plot relative to the owning GrAtlas and page. */
320 uint32_t index() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500321 /**
322 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
323 * if a particular subimage is still present in the atlas.
324 */
joshualitt5df175e2015-11-18 13:37:54 -0800325 uint64_t genID() const { return fGenID; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500326 GrDrawOpAtlas::AtlasID id() const {
327 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID);
joshualitt5df175e2015-11-18 13:37:54 -0800328 return fID;
329 }
330 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
331
332 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
333
Brian Salomon2ee084e2016-12-16 18:59:19 -0500334 /**
335 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
336 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
337 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
338 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
339 * has already flushed through the gpu then we can reuse the plot.
340 */
Brian Salomon943ed792017-10-30 09:37:55 -0400341 GrDeferredUploadToken lastUploadToken() const { return fLastUpload; }
342 GrDeferredUploadToken lastUseToken() const { return fLastUse; }
343 void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; }
344 void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800345
Brian Salomon943ed792017-10-30 09:37:55 -0400346 void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800347 void resetRects();
348
Jim Van Verth106b5c42017-09-26 12:45:29 -0400349 int flushesSinceLastUsed() { return fFlushesSinceLastUse; }
350 void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; }
351 void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; }
352
joshualitt5df175e2015-11-18 13:37:54 -0800353 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400354 Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, int width, int height,
Brian Salomon2ee084e2016-12-16 18:59:19 -0500355 GrPixelConfig config);
joshualitt5df175e2015-11-18 13:37:54 -0800356
Brian Salomon2ee084e2016-12-16 18:59:19 -0500357 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800358
Brian Salomon2ee084e2016-12-16 18:59:19 -0500359 /**
360 * Create a clone of this plot. The cloned plot will take the place of the current plot in
361 * the atlas
362 */
363 Plot* clone() const {
Jim Van Vertha950b632017-09-12 11:54:11 -0400364 return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig);
joshualitt5df175e2015-11-18 13:37:54 -0800365 }
366
Jim Van Vertha950b632017-09-12 11:54:11 -0400367 static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx,
368 uint64_t generation) {
369 SkASSERT(pageIdx < (1 << 8));
Brian Salomon9f545bc2017-11-06 10:36:57 -0500370 SkASSERT(pageIdx < kMaxMultitexturePages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400371 SkASSERT(plotIdx < (1 << 8));
joshualitt5df175e2015-11-18 13:37:54 -0800372 SkASSERT(generation < ((uint64_t)1 << 48));
Jim Van Vertha950b632017-09-12 11:54:11 -0400373 return generation << 16 | plotIdx << 8 | pageIdx;
joshualitt5df175e2015-11-18 13:37:54 -0800374 }
375
Brian Salomon943ed792017-10-30 09:37:55 -0400376 GrDeferredUploadToken fLastUpload;
377 GrDeferredUploadToken fLastUse;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400378 // the number of flushes since this plot has been last used
379 int fFlushesSinceLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800380
Jim Van Vertha950b632017-09-12 11:54:11 -0400381 struct {
382 const uint32_t fPageIndex : 16;
383 const uint32_t fPlotIndex : 16;
384 };
Brian Salomon2ee084e2016-12-16 18:59:19 -0500385 uint64_t fGenID;
386 GrDrawOpAtlas::AtlasID fID;
387 unsigned char* fData;
388 const int fWidth;
389 const int fHeight;
390 const int fX;
391 const int fY;
392 GrRectanizer* fRects;
393 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
394 const GrPixelConfig fConfig;
395 const size_t fBytesPerPixel;
396 SkIRect fDirtyRect;
397 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800398
Brian Salomon2ee084e2016-12-16 18:59:19 -0500399 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800400
401 typedef SkRefCnt INHERITED;
402 };
403
Brian Salomon2ee084e2016-12-16 18:59:19 -0500404 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800405
Jim Van Vertha950b632017-09-12 11:54:11 -0400406 static uint32_t GetPlotIndexFromID(AtlasID id) {
407 return (id >> 8) & 0xff;
joshualitt5bf99f12015-03-13 11:47:42 -0700408 }
409
joshualitt8db6fdc2015-07-31 08:25:07 -0700410 // top 48 bits are reserved for the generation ID
411 static uint64_t GetGenerationFromID(AtlasID id) {
412 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700413 }
414
Brian Salomon29b60c92017-10-31 14:42:10 -0400415 inline bool updatePlot(GrDeferredUploadTarget*, AtlasID*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700416
Jim Van Vertha950b632017-09-12 11:54:11 -0400417 inline void makeMRU(Plot* plot, int pageIdx) {
418 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800419 return;
420 }
421
Jim Van Vertha950b632017-09-12 11:54:11 -0400422 fPages[pageIdx].fPlotList.remove(plot);
423 fPages[pageIdx].fPlotList.addToHead(plot);
424
Jim Van Verth106b5c42017-09-26 12:45:29 -0400425 // No MRU update for pages -- since we will always try to add from
426 // the front and remove from the back there is no need for MRU.
joshualitt5df175e2015-11-18 13:37:54 -0800427 }
joshualitt5bf99f12015-03-13 11:47:42 -0700428
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500429 bool uploadToPage(unsigned int pageIdx, AtlasID* id, GrDeferredUploadTarget* target,
430 int width, int height, const void* image, SkIPoint16* loc);
431
Robert Phillips4bc70112018-03-01 10:24:02 -0500432 bool createPages(GrProxyProvider*);
433 bool activateNewPage(GrResourceProvider*);
434 void deactivateLastPage();
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400435
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400436 void processEviction(AtlasID);
437 inline void processEvictionAndResetRects(Plot* plot) {
438 this->processEviction(plot->id());
439 plot->resetRects();
440 }
joshualitt5bf99f12015-03-13 11:47:42 -0700441
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400442 GrPixelConfig fPixelConfig;
443 int fTextureWidth;
444 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500445 int fPlotWidth;
446 int fPlotHeight;
Jim Van Verth06f593c2018-02-20 11:30:10 -0500447 unsigned int fNumPlots;
robertphillips2b0536f2015-11-06 14:10:42 -0800448
Robert Phillips32f28182017-02-28 16:20:03 -0500449 uint64_t fAtlasGeneration;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400450 // nextTokenToFlush() value at the end of the previous flush
Brian Salomon943ed792017-10-30 09:37:55 -0400451 GrDeferredUploadToken fPrevFlushToken;
joshualitt5bf99f12015-03-13 11:47:42 -0700452
453 struct EvictionData {
454 EvictionFunc fFunc;
455 void* fData;
456 };
457
458 SkTDArray<EvictionData> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400459
460 struct Page {
461 // allocated array of Plots
462 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
463 // LRU list of Plots (MRU at head - LRU at tail)
464 PlotList fPlotList;
465 };
466 // proxies kept separate to make it easier to pass them up to client
Brian Salomon9f545bc2017-11-06 10:36:57 -0500467 sk_sp<GrTextureProxy> fProxies[kMaxMultitexturePages];
468 Page fPages[kMaxMultitexturePages];
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500469 uint32_t fMaxPages;
Robert Phillips4bc70112018-03-01 10:24:02 -0500470
471 uint32_t fNumActivePages;
joshualitt5bf99f12015-03-13 11:47:42 -0700472};
473
474#endif