blob: 6d12973f4aa496fe80b165a405db5d05b3889310 [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;
123
Brian Salomon2ee084e2016-12-16 18:59:19 -0500124 /**
125 * 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 -0400126 * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
127 * this is insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700128 */
129 class BulkUseTokenUpdater {
130 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400131 BulkUseTokenUpdater() {
132 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
133 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700134 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
Jim Van Vertha950b632017-09-12 11:54:11 -0400135 : fPlotsToUpdate(that.fPlotsToUpdate) {
136 memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
joshualitt7e97b0b2015-07-31 15:18:08 -0700137 }
138
joshualittb4c507e2015-04-08 08:07:59 -0700139 void add(AtlasID id) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400140 int index = GrDrawOpAtlas::GetPlotIndexFromID(id);
141 int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id);
142 if (!this->find(pageIdx, index)) {
143 this->set(pageIdx, index);
joshualittb4c507e2015-04-08 08:07:59 -0700144 }
145 }
146
147 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700148 fPlotsToUpdate.reset();
Jim Van Vertha950b632017-09-12 11:54:11 -0400149 memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
joshualittb4c507e2015-04-08 08:07:59 -0700150 }
151
Jim Van Vertha950b632017-09-12 11:54:11 -0400152 struct PlotData {
153 PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
154 uint32_t fPageIndex;
155 uint32_t fPlotIndex;
156 };
157
joshualittb4c507e2015-04-08 08:07:59 -0700158 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400159 bool find(int pageIdx, int index) const {
joshualittb4c507e2015-04-08 08:07:59 -0700160 SkASSERT(index < kMaxPlots);
Jim Van Vertha950b632017-09-12 11:54:11 -0400161 return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
joshualittb4c507e2015-04-08 08:07:59 -0700162 }
163
Jim Van Vertha950b632017-09-12 11:54:11 -0400164 void set(int pageIdx, int index) {
165 SkASSERT(!this->find(pageIdx, index));
166 fPlotAlreadyUpdated[pageIdx] |= (1 << index);
167 fPlotsToUpdate.push_back(PlotData(pageIdx, index));
joshualittb4c507e2015-04-08 08:07:59 -0700168 }
169
Jim Van Vertha950b632017-09-12 11:54:11 -0400170 static constexpr int kMinItems = 4;
171 static constexpr int kMaxPlots = 32;
172 SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
173 uint32_t fPlotAlreadyUpdated[kMaxPages];
joshualittb4c507e2015-04-08 08:07:59 -0700174
Brian Salomon2ee084e2016-12-16 18:59:19 -0500175 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700176 };
177
Brian Salomon2ee084e2016-12-16 18:59:19 -0500178 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDrawOpUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800179 int count = updater.fPlotsToUpdate.count();
180 for (int i = 0; i < count; i++) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400181 const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
182 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
183 this->makeMRU(plot, pd.fPageIndex);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500184 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800185 }
186 }
joshualittb4c507e2015-04-08 08:07:59 -0700187
Jim Van Vertha950b632017-09-12 11:54:11 -0400188 static constexpr auto kGlyphMaxDim = 256;
joshualitt010db532015-04-21 10:07:26 -0700189 static bool GlyphTooLargeForAtlas(int width, int height) {
190 return width > kGlyphMaxDim || height > kGlyphMaxDim;
191 }
192
joshualitt5bf99f12015-03-13 11:47:42 -0700193private:
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400194 GrDrawOpAtlas(GrContext*, GrPixelConfig config, int width, int height,
195 int numPlotsX, int numPlotsY);
Robert Phillips256c37b2017-03-01 14:32:46 -0500196
Brian Salomon2ee084e2016-12-16 18:59:19 -0500197 /**
198 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
199 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
200 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
201 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
202 * used unless the last use of the Plot has already been flushed through to the gpu.
203 */
204 class Plot : public SkRefCnt {
205 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800206
207 public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400208 /** index() is a unique id for the plot relative to the owning GrAtlas and page. */
209 uint32_t index() const { return fPlotIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500210 /**
211 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
212 * if a particular subimage is still present in the atlas.
213 */
joshualitt5df175e2015-11-18 13:37:54 -0800214 uint64_t genID() const { return fGenID; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500215 GrDrawOpAtlas::AtlasID id() const {
216 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID);
joshualitt5df175e2015-11-18 13:37:54 -0800217 return fID;
218 }
219 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
220
221 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
222
Brian Salomon2ee084e2016-12-16 18:59:19 -0500223 /**
224 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
225 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
226 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
227 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
228 * has already flushed through the gpu then we can reuse the plot.
229 */
Brian Salomon9afd3712016-12-01 10:59:09 -0500230 GrDrawOpUploadToken lastUploadToken() const { return fLastUpload; }
231 GrDrawOpUploadToken lastUseToken() const { return fLastUse; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500232 void setLastUploadToken(GrDrawOpUploadToken token) { fLastUpload = token; }
233 void setLastUseToken(GrDrawOpUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800234
Robert Phillipsacaa6072017-07-28 10:54:53 -0400235 void uploadToTexture(GrDrawOp::WritePixelsFn&, GrTextureProxy*);
joshualitt5df175e2015-11-18 13:37:54 -0800236 void resetRects();
237
238 private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400239 Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, int width, int height,
Brian Salomon2ee084e2016-12-16 18:59:19 -0500240 GrPixelConfig config);
joshualitt5df175e2015-11-18 13:37:54 -0800241
Brian Salomon2ee084e2016-12-16 18:59:19 -0500242 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800243
Brian Salomon2ee084e2016-12-16 18:59:19 -0500244 /**
245 * Create a clone of this plot. The cloned plot will take the place of the current plot in
246 * the atlas
247 */
248 Plot* clone() const {
Jim Van Vertha950b632017-09-12 11:54:11 -0400249 return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig);
joshualitt5df175e2015-11-18 13:37:54 -0800250 }
251
Jim Van Vertha950b632017-09-12 11:54:11 -0400252 static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx,
253 uint64_t generation) {
254 SkASSERT(pageIdx < (1 << 8));
255 SkASSERT(pageIdx == 0); // for now, we only support one page
256 SkASSERT(plotIdx < (1 << 8));
joshualitt5df175e2015-11-18 13:37:54 -0800257 SkASSERT(generation < ((uint64_t)1 << 48));
Jim Van Vertha950b632017-09-12 11:54:11 -0400258 return generation << 16 | plotIdx << 8 | pageIdx;
joshualitt5df175e2015-11-18 13:37:54 -0800259 }
260
Brian Salomon9afd3712016-12-01 10:59:09 -0500261 GrDrawOpUploadToken fLastUpload;
262 GrDrawOpUploadToken fLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800263
Jim Van Vertha950b632017-09-12 11:54:11 -0400264 struct {
265 const uint32_t fPageIndex : 16;
266 const uint32_t fPlotIndex : 16;
267 };
Brian Salomon2ee084e2016-12-16 18:59:19 -0500268 uint64_t fGenID;
269 GrDrawOpAtlas::AtlasID fID;
270 unsigned char* fData;
271 const int fWidth;
272 const int fHeight;
273 const int fX;
274 const int fY;
275 GrRectanizer* fRects;
276 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
277 const GrPixelConfig fConfig;
278 const size_t fBytesPerPixel;
279 SkIRect fDirtyRect;
280 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800281
Brian Salomon2ee084e2016-12-16 18:59:19 -0500282 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800283
284 typedef SkRefCnt INHERITED;
285 };
286
Brian Salomon2ee084e2016-12-16 18:59:19 -0500287 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800288
Jim Van Vertha950b632017-09-12 11:54:11 -0400289 static uint32_t GetPageIndexFromID(AtlasID id) {
290 return id & 0xff;
291 }
292
293 static uint32_t GetPlotIndexFromID(AtlasID id) {
294 return (id >> 8) & 0xff;
joshualitt5bf99f12015-03-13 11:47:42 -0700295 }
296
joshualitt8db6fdc2015-07-31 08:25:07 -0700297 // top 48 bits are reserved for the generation ID
298 static uint64_t GetGenerationFromID(AtlasID id) {
299 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700300 }
301
Robert Phillips256c37b2017-03-01 14:32:46 -0500302 inline bool updatePlot(GrDrawOp::Target*, AtlasID*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700303
Jim Van Vertha950b632017-09-12 11:54:11 -0400304 inline void makeMRU(Plot* plot, int pageIdx) {
305 if (fPages[pageIdx].fPlotList.head() == plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800306 return;
307 }
308
Jim Van Vertha950b632017-09-12 11:54:11 -0400309 fPages[pageIdx].fPlotList.remove(plot);
310 fPages[pageIdx].fPlotList.addToHead(plot);
311
312 // TODO: make page MRU
joshualitt5df175e2015-11-18 13:37:54 -0800313 }
joshualitt5bf99f12015-03-13 11:47:42 -0700314
315 inline void processEviction(AtlasID);
316
Robert Phillips32f28182017-02-28 16:20:03 -0500317 GrContext* fContext;
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400318 GrPixelConfig fPixelConfig;
319 int fTextureWidth;
320 int fTextureHeight;
Robert Phillips32f28182017-02-28 16:20:03 -0500321 int fPlotWidth;
322 int fPlotHeight;
323 SkDEBUGCODE(uint32_t fNumPlots;)
robertphillips2b0536f2015-11-06 14:10:42 -0800324
Robert Phillips32f28182017-02-28 16:20:03 -0500325 uint64_t fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700326
327 struct EvictionData {
328 EvictionFunc fFunc;
329 void* fData;
330 };
331
332 SkTDArray<EvictionData> fEvictionCallbacks;
Jim Van Vertha950b632017-09-12 11:54:11 -0400333
334 struct Page {
335 // allocated array of Plots
336 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
337 // LRU list of Plots (MRU at head - LRU at tail)
338 PlotList fPlotList;
339 };
340 // proxies kept separate to make it easier to pass them up to client
341 sk_sp<GrTextureProxy> fProxies[kMaxPages];
342 Page fPages[kMaxPages];
343 SkDEBUGCODE(uint32_t fNumPages;)
joshualitt5bf99f12015-03-13 11:47:42 -0700344};
345
346#endif