blob: 87d9c5b619dafac8e0e1209c96c98a2591382ad7 [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
joshualitt5bf99f12015-03-13 11:47:42 -070011#include "SkPoint.h"
12#include "SkTDArray.h"
13#include "SkTInternalLList.h"
14
Brian Salomon89527432016-12-16 09:52:16 -050015#include "ops/GrDrawOp.h"
joshualittddd22d82016-02-16 06:47:52 -080016
joshualitt5bf99f12015-03-13 11:47:42 -070017class GrRectanizer;
18
Brian Salomon2ee084e2016-12-16 18:59:19 -050019struct GrDrawOpAtlasConfig {
joshualittda04e0e2015-08-19 08:16:43 -070020 int numPlotsX() const { return fWidth / fPlotWidth; }
21 int numPlotsY() const { return fHeight / fPlotWidth; }
22 int fWidth;
23 int fHeight;
24 int fPlotWidth;
25 int fPlotHeight;
26};
27
Brian Salomon2ee084e2016-12-16 18:59:19 -050028/**
29 * This class manages an atlas texture on behalf of GrDrawOps. The draw ops that use the atlas
30 * perform texture uploads when preparing their draws during flush. The class provides facilities
31 * for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in "asap" mode
32 * until it is impossible to add data without overwriting texels read by draws that have not yet
33 * executed on the gpu. At that point the uploads are performed "inline" between draws. If a single
34 * draw would use enough subimage space to overflow the atlas texture then the atlas will fail to
35 * add a subimage. This gives the op the chance to end the draw and begin a new one. Additional
36 * uploads will then succeed in inline mode.
37 */
38class GrDrawOpAtlas {
joshualitt5bf99f12015-03-13 11:47:42 -070039public:
Brian Salomon2ee084e2016-12-16 18:59:19 -050040 /**
41 * An AtlasID is an opaque handle which callers can use to determine if the atlas contains
42 * a specific piece of data.
43 */
joshualitt8db6fdc2015-07-31 08:25:07 -070044 typedef uint64_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070045 static const uint32_t kInvalidAtlasID = 0;
46 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070047
Brian Salomon2ee084e2016-12-16 18:59:19 -050048 /**
49 * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a
50 * specific AtlasID, it will call all of the registered listeners so they can process the
51 * eviction.
52 */
53 typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*);
joshualitt5bf99f12015-03-13 11:47:42 -070054
Robert Phillips256c37b2017-03-01 14:32:46 -050055 /**
56 * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
57 * should only be used inside of GrMeshDrawOp::onPrepareDraws.
58 * @param GrPixelConfig The pixel config which this atlas will store
59 * @param width width in pixels of the atlas
60 * @param height height in pixels of the atlas
61 * @param numPlotsX The number of plots the atlas should be broken up into in the X
62 * direction
63 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
64 * direction
65 * @param func An eviction function which will be called whenever the atlas has to
66 * evict data
67 * @param data User supplied data which will be passed into func whenver an
68 * eviction occurs
69 * @return An initialized GrDrawOpAtlas, or nullptr if creation fails
70 */
71 static std::unique_ptr<GrDrawOpAtlas> Make(GrContext*, GrPixelConfig,
72 int width, int height,
73 int numPlotsX, int numPlotsY,
74 GrDrawOpAtlas::EvictionFunc func, void* data);
joshualitt5bf99f12015-03-13 11:47:42 -070075
Brian Salomon2ee084e2016-12-16 18:59:19 -050076 /**
77 * Adds a width x height subimage to the atlas. Upon success it returns an ID and the subimage's
78 * coordinates in the backing texture. False is returned if the subimage cannot fit in the
79 * atlas without overwriting texels that will be read in the current draw. This indicates that
80 * the op should end its current draw and begin another before adding more data. Upon success,
81 * an upload of the provided image data will have been added to the GrDrawOp::Target, in "asap"
82 * mode if possible, otherwise in "inline" mode. Successive uploads in either mode may be
83 * consolidated.
84 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
85 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
86 * addToAtlas might cause the previous data to be overwritten before it has been read.
87 */
Brian Salomon9afd3712016-12-01 10:59:09 -050088 bool addToAtlas(AtlasID*, GrDrawOp::Target*, int width, int height, const void* image,
joshualitt5bf99f12015-03-13 11:47:42 -070089 SkIPoint16* loc);
90
Robert Phillips32f28182017-02-28 16:20:03 -050091 GrContext* context() const { return fContext; }
Jim Van Vertha950b632017-09-12 11:54:11 -040092 const sk_sp<GrTextureProxy>* getProxies() const { return fProxies; }
joshualitt5bf99f12015-03-13 11:47:42 -070093
joshualitt7c3a2f82015-03-31 13:32:05 -070094 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -080095
96 inline bool hasID(AtlasID id) {
Jim Van Vertha950b632017-09-12 11:54:11 -040097 uint32_t plot = GetPlotIndexFromID(id);
98 SkASSERT(plot < fNumPlots);
99 uint32_t page = GetPageIndexFromID(id);
100 SkASSERT(page < fNumPages);
101 return fPages[page].fPlotArray[plot]->genID() == GetGenerationFromID(id);
joshualitt5df175e2015-11-18 13:37:54 -0800102 }
joshualittb4c507e2015-04-08 08:07:59 -0700103
Brian Salomon2ee084e2016-12-16 18:59:19 -0500104 /** To ensure the atlas does not evict a given entry, the client must set the last use token. */
105 inline void setLastUseToken(AtlasID id, GrDrawOpUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800106 SkASSERT(this->hasID(id));
Jim Van Vertha950b632017-09-12 11:54:11 -0400107 uint32_t plotIdx = GetPlotIndexFromID(id);
108 SkASSERT(plotIdx < fNumPlots);
109 uint32_t pageIdx = GetPageIndexFromID(id);
110 SkASSERT(pageIdx < fNumPages);
111 Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
112 this->makeMRU(plot, pageIdx);
113 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800114 }
115
116 inline void registerEvictionCallback(EvictionFunc func, void* userData) {
joshualitt5bf99f12015-03-13 11:47:42 -0700117 EvictionData* data = fEvictionCallbacks.append();
118 data->fFunc = func;
119 data->fData = userData;
120 }
121
Jim Van Vertha950b632017-09-12 11:54:11 -0400122 static constexpr auto kMaxPages = 4;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400123 uint32_t pageCount() { return fNumPages; }
Jim Van Vertha950b632017-09-12 11:54:11 -0400124
Brian Salomon2ee084e2016-12-16 18:59:19 -0500125 /**
126 * 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 -0400127 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
128 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700129 */
130 class BulkUseTokenUpdater {
131 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400132 BulkUseTokenUpdater() {
133 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
134 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700135 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Jim Van Vertha950b632017-09-12 11:54:11 -0400136 : fPlotsToUpdate(that.fPlotsToUpdate) {
137 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700138 }
139
joshualittb4c507e2015-04-08 08:07:59 -0700140 void add(AtlasID id) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400141 int index = GrDrawOpAtlas::GetPlotIndexFromID(id);
142 int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id);
143 if (!this->find(pageIdx, index)) {
144 this->set(pageIdx, index);
joshualittb4c507e2015-04-08 08:07:59 -0700145 }
146 }
147
148 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700149 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400150 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700151 }
152
Jim Van Vertha950b632017-09-12 11:54:11 -0400153 struct PlotData {
154 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
155 uint32_t fPageIndex;
156 uint32_t fPlotIndex;
157 };
158
joshualittb4c507e2015-04-08 08:07:59 -0700159 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400160 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700161 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400162 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700163 }
164
Jim Van Vertha950b632017-09-12 11:54:11 -0400165 void set(int pageIdx, int index) {
166 SkASSERT(!this->find(pageIdx, index));
167 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
168 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700169 }
170
Jim Van Vertha950b632017-09-12 11:54:11 -0400171 static constexpr int kMinItems = 4;
172 static constexpr int kMaxPlots = 32;
173 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
174 uint32_t fPlotAlreadyUpdated[kMaxPages];
joshualittb4c507e2015-04-08 08:07:59 -0700175
Brian Salomon2ee084e2016-12-16 18:59:19 -0500176 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700177 };
178
Brian Salomon2ee084e2016-12-16 18:59:19 -0500179 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDrawOpUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800180 int count = updater.fPlotsToUpdate.count();
181 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400182 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
183 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
184 this->makeMRU(plot, pd.fPageIndex);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500185 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800186 }
187 }
joshualittb4c507e2015-04-08 08:07:59 -0700188
Jim Van Vertha950b632017-09-12 11:54:11 -0400189 static constexpr auto kGlyphMaxDim = 256;
joshualitt010db532015-04-21 10:07:26 -0700190 static bool GlyphTooLargeForAtlas(int width, int height) {
191 return width > kGlyphMaxDim || height > kGlyphMaxDim;
192 }
193
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400194 static uint32_t GetPageIndexFromID(AtlasID id) {
195 return id & 0xff;
196 }
197
joshualitt5bf99f12015-03-13 11:47:42 -0700198private:
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400199 GrDrawOpAtlas(GrContext*, GrPixelConfig config, int width, int height,
200 int numPlotsX, int numPlotsY);
Robert Phillips256c37b2017-03-01 14:32:46 -0500201
Brian Salomon2ee084e2016-12-16 18:59:19 -0500202 /**
203 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
204 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
205 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
206 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
207 * used unless the last use of the Plot has already been flushed through to the gpu.
208 */
209 class Plot : public SkRefCnt {
210 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800211
212 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400213 /** index() is a unique id for the plot relative to the owning GrAtlas and page. */
214 uint32_t index() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500215 /**
216 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
217 * if a particular subimage is still present in the atlas.
218 */
joshualitt5df175e2015-11-18 13:37:54 -0800219 uint64_t genID() const { return fGenID; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500220 GrDrawOpAtlas::AtlasID id() const {
221 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID);
joshualitt5df175e2015-11-18 13:37:54 -0800222 return fID;
223 }
224 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
225
226 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
227
Brian Salomon2ee084e2016-12-16 18:59:19 -0500228 /**
229 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
230 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
231 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
232 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
233 * has already flushed through the gpu then we can reuse the plot.
234 */
Brian Salomon9afd3712016-12-01 10:59:09 -0500235 GrDrawOpUploadToken lastUploadToken() const { return fLastUpload; }
236 GrDrawOpUploadToken lastUseToken() const { return fLastUse; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500237 void setLastUploadToken(GrDrawOpUploadToken token) { fLastUpload = token; }
238 void setLastUseToken(GrDrawOpUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800239
Robert Phillipsacaa6072017-07-28 10:54:53 -0400240 void uploadToTexture(GrDrawOp::WritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800241 void resetRects();
242
243 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400244 Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, int width, int height,
Brian Salomon2ee084e2016-12-16 18:59:19 -0500245 GrPixelConfig config);
joshualitt5df175e2015-11-18 13:37:54 -0800246
Brian Salomon2ee084e2016-12-16 18:59:19 -0500247 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800248
Brian Salomon2ee084e2016-12-16 18:59:19 -0500249 /**
250 * Create a clone of this plot. The cloned plot will take the place of the current plot in
251 * the atlas
252 */
253 Plot* clone() const {
Jim Van Vertha950b632017-09-12 11:54:11 -0400254 return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig);
joshualitt5df175e2015-11-18 13:37:54 -0800255 }
256
Jim Van Vertha950b632017-09-12 11:54:11 -0400257 static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx,
258 uint64_t generation) {
259 SkASSERT(pageIdx < (1 << 8));
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400260 SkASSERT(pageIdx < kMaxPages);
Jim Van Vertha950b632017-09-12 11:54:11 -0400261 SkASSERT(plotIdx < (1 << 8));
joshualitt5df175e2015-11-18 13:37:54 -0800262 SkASSERT(generation < ((uint64_t)1 << 48));
Jim Van Vertha950b632017-09-12 11:54:11 -0400263 return generation << 16 | plotIdx << 8 | pageIdx;
joshualitt5df175e2015-11-18 13:37:54 -0800264 }
265
Brian Salomon9afd3712016-12-01 10:59:09 -0500266 GrDrawOpUploadToken fLastUpload;
267 GrDrawOpUploadToken fLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800268
Jim Van Vertha950b632017-09-12 11:54:11 -0400269 struct {
270 const uint32_t fPageIndex : 16;
271 const uint32_t fPlotIndex : 16;
272 };
Brian Salomon2ee084e2016-12-16 18:59:19 -0500273 uint64_t fGenID;
274 GrDrawOpAtlas::AtlasID fID;
275 unsigned char* fData;
276 const int fWidth;
277 const int fHeight;
278 const int fX;
279 const int fY;
280 GrRectanizer* fRects;
281 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
282 const GrPixelConfig fConfig;
283 const size_t fBytesPerPixel;
284 SkIRect fDirtyRect;
285 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800286
Brian Salomon2ee084e2016-12-16 18:59:19 -0500287 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800288
289 typedef SkRefCnt INHERITED;
290 };
291
Brian Salomon2ee084e2016-12-16 18:59:19 -0500292 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800293
Jim Van Vertha950b632017-09-12 11:54:11 -0400294 static uint32_t GetPlotIndexFromID(AtlasID id) {
295 return (id >> 8) & 0xff;
joshualitt5bf99f12015-03-13 11:47:42 -0700296 }
297
joshualitt8db6fdc2015-07-31 08:25:07 -0700298 // top 48 bits are reserved for the generation ID
299 static uint64_t GetGenerationFromID(AtlasID id) {
300 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700301 }
302
Robert Phillips256c37b2017-03-01 14:32:46 -0500303 inline bool updatePlot(GrDrawOp::Target*, AtlasID*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700304
Jim Van Vertha950b632017-09-12 11:54:11 -0400305 inline void makeMRU(Plot* plot, int pageIdx) {
306 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800307 return;
308 }
309
Jim Van Vertha950b632017-09-12 11:54:11 -0400310 fPages[pageIdx].fPlotList.remove(plot);
311 fPages[pageIdx].fPlotList.addToHead(plot);
312
313 // TODO: make page MRU
joshualitt5df175e2015-11-18 13:37:54 -0800314 }
joshualitt5bf99f12015-03-13 11:47:42 -0700315
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400316 bool createNewPage();
317
joshualitt5bf99f12015-03-13 11:47:42 -0700318 inline void processEviction(AtlasID);
319
Robert Phillips32f28182017-02-28 16:20:03 -0500320 GrContext* fContext;
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400321 GrPixelConfig fPixelConfig;
322 int fTextureWidth;
323 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500324 int fPlotWidth;
325 int fPlotHeight;
326 SkDEBUGCODE(uint32_t fNumPlots;)
robertphillips2b0536f2015-11-06 14:10:42 -0800327
Robert Phillips32f28182017-02-28 16:20:03 -0500328 uint64_t fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700329
330 struct EvictionData {
331 EvictionFunc fFunc;
332 void* fData;
333 };
334
335 SkTDArray<EvictionData> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400336
337 struct Page {
338 // allocated array of Plots
339 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
340 // LRU list of Plots (MRU at head - LRU at tail)
341 PlotList fPlotList;
342 };
343 // proxies kept separate to make it easier to pass them up to client
344 sk_sp<GrTextureProxy> fProxies[kMaxPages];
345 Page fPages[kMaxPages];
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400346 uint32_t fNumPages;
joshualitt5bf99f12015-03-13 11:47:42 -0700347};
348
349#endif