• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #ifndef GrDrawOpAtlas_DEFINED
9 #define GrDrawOpAtlas_DEFINED
10 
11 #include <cmath>
12 
13 #include "include/core/SkSize.h"
14 #include "include/private/SkTDArray.h"
15 #include "src/core/SkGlyphRunPainter.h"
16 #include "src/core/SkIPoint16.h"
17 #include "src/core/SkTInternalLList.h"
18 
19 #include "src/gpu/ops/GrDrawOp.h"
20 
21 class GrOnFlushResourceProvider;
22 class GrRectanizer;
23 
24 
25 /**
26  * This class manages one or more atlas textures on behalf of GrDrawOps. The draw ops that use the
27  * atlas perform texture uploads when preparing their draws during flush. The class provides
28  * facilities for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in
29  * "ASAP" mode until it is impossible to add data without overwriting texels read by draws that
30  * have not yet executed on the gpu. At that point, the atlas will attempt to allocate a new
31  * atlas texture (or "page") of the same size, up to a maximum number of textures, and upload
32  * to that texture. If that's not possible, the uploads are performed "inline" between draws. If a
33  * single draw would use enough subimage space to overflow the atlas texture then the atlas will
34  * fail to add a subimage. This gives the op the chance to end the draw and begin a new one.
35  * Additional uploads will then succeed in inline mode.
36  *
37  * When the atlas has multiple pages, new uploads are prioritized to the lower index pages, i.e.,
38  * it will try to upload to page 0 before page 1 or 2. To keep the atlas from continually using
39  * excess space, periodic garbage collection is needed to shift data from the higher index pages to
40  * the lower ones, and then eventually remove any pages that are no longer in use. "In use" is
41  * determined by using the GrDrawUploadToken system: After a flush each subarea of the page
42  * is checked to see whether it was used in that flush; if it is not, a counter is incremented.
43  * Once that counter reaches a threshold that subarea is considered to be no longer in use.
44  *
45  * Garbage collection is initiated by the GrDrawOpAtlas's client via the compact() method. One
46  * solution is to make the client a subclass of GrOnFlushCallbackObject, register it with the
47  * GrContext via addOnFlushCallbackObject(), and the client's postFlush() method calls compact()
48  * and passes in the given GrDrawUploadToken.
49  */
50 class GrDrawOpAtlas {
51 private:
52     static constexpr auto kMaxMultitexturePages = 16;
53 
54 
55 public:
56     /** Is the atlas allowed to use more than one texture? */
57     enum class AllowMultitexturing : bool { kNo, kYes };
58 
59     static constexpr int kMaxPlots = 32; // restricted by the fPlotAlreadyUpdated bitfield
60                                          // in BulkUseTokenUpdater
61 
62     /**
63      * An AtlasID is an opaque handle which callers can use to determine if the atlas contains
64      * a specific piece of data.
65      */
66     typedef uint64_t AtlasID;
67     static const uint32_t kInvalidAtlasID = 0;
68     static const uint64_t kInvalidAtlasGeneration = 0;
69 
70     /**
71      * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a
72      * specific AtlasID, it will call all of the registered listeners so they can process the
73      * eviction.
74      */
75     typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*);
76 
77     /**
78      * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
79      * should only be used inside of GrMeshDrawOp::onPrepareDraws.
80      *  @param GrColorType      The colorType which this atlas will store
81      *  @param width            width in pixels of the atlas
82      *  @param height           height in pixels of the atlas
83      *  @param numPlotsX        The number of plots the atlas should be broken up into in the X
84      *                          direction
85      *  @param numPlotsY        The number of plots the atlas should be broken up into in the Y
86      *                          direction
87      *  @param allowMultitexturing Can the atlas use more than one texture.
88      *  @param func             An eviction function which will be called whenever the atlas has to
89      *                          evict data
90      *  @param data             User supplied data which will be passed into func whenever an
91      *                          eviction occurs
92      *  @return                 An initialized GrDrawOpAtlas, or nullptr if creation fails
93      */
94     static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*,
95                                                const GrBackendFormat& format,
96                                                GrColorType,
97                                                int width, int height,
98                                                int plotWidth, int plotHeight,
99                                                AllowMultitexturing allowMultitexturing,
100                                                int atlasPageCount,
101                                                int plotOldThreshold,
102                                                GrDrawOpAtlas::EvictionFunc func, void* data);
103 
104     /**
105      * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns
106      * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if
107      * the subimage cannot fit in the atlas without overwriting texels that will be read in the
108      * current draw. This indicates that the op should end its current draw and begin another
109      * before adding more data. Upon success, an upload of the provided image data will have
110      * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode.
111      * Successive uploads in either mode may be consolidated.
112      * 'kError' will be returned when some unrecoverable error was encountered while trying to
113      * add the subimage. In this case the op being created should be discarded.
114      *
115      * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
116      * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
117      * addToAtlas might cause the previous data to be overwritten before it has been read.
118      */
119 
120     enum class ErrorCode {
121         kError,
122         kSucceeded,
123         kTryAgain
124     };
125 
126     ErrorCode addToAtlas(GrResourceProvider*, AtlasID*, GrDeferredUploadTarget*,
127                          int width, int height,
128                          const void* image, SkIPoint16* loc);
129 
getProxies()130     const sk_sp<GrTextureProxy>* getProxies() const { return fProxies; }
131 
atlasGeneration()132     uint64_t atlasGeneration() const { return fAtlasGeneration; }
133 
hasID(AtlasID id)134     inline bool hasID(AtlasID id) {
135         if (kInvalidAtlasID == id) {
136             return false;
137         }
138         uint32_t plot = GetPlotIndexFromID(id);
139         SkASSERT(plot < fNumPlots);
140         uint32_t page = GetPageIndexFromID(id);
141         SkASSERT(page < fNumActivePages);
142         return fPages[page].fPlotArray[plot]->genID() == GetGenerationFromID(id);
143     }
144 
145     /** To ensure the atlas does not evict a given entry, the client must set the last use token. */
setLastUseToken(AtlasID id,GrDeferredUploadToken token)146     inline void setLastUseToken(AtlasID id, GrDeferredUploadToken token) {
147         SkASSERT(this->hasID(id));
148         uint32_t plotIdx = GetPlotIndexFromID(id);
149         SkASSERT(plotIdx < fNumPlots);
150         uint32_t pageIdx = GetPageIndexFromID(id);
151         SkASSERT(pageIdx < fNumActivePages);
152         Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get();
153         this->makeMRU(plot, pageIdx);
154         plot->setLastUseToken(token);
155     }
156 
registerEvictionCallback(EvictionFunc func,void * userData)157     inline void registerEvictionCallback(EvictionFunc func, void* userData) {
158         EvictionData* data = fEvictionCallbacks.append();
159         data->fFunc = func;
160         data->fData = userData;
161     }
162 
numActivePages()163     uint32_t numActivePages() { return fNumActivePages; }
164 
165     /**
166      * A class which can be handed back to GrDrawOpAtlas for updating last use tokens in bulk.  The
167      * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future
168      * this is insufficient then we can move to a 64 bit int.
169      */
170     class BulkUseTokenUpdater {
171     public:
BulkUseTokenUpdater()172         BulkUseTokenUpdater() {
173             memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
174         }
BulkUseTokenUpdater(const BulkUseTokenUpdater & that)175         BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
176             : fPlotsToUpdate(that.fPlotsToUpdate) {
177             memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated));
178         }
179 
add(AtlasID id)180         bool add(AtlasID id) {
181             int index = GrDrawOpAtlas::GetPlotIndexFromID(id);
182             int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id);
183             if (this->find(pageIdx, index)) {
184                 return false;
185             }
186             this->set(pageIdx, index);
187             return true;
188         }
189 
reset()190         void reset() {
191             fPlotsToUpdate.reset();
192             memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated));
193         }
194 
195         struct PlotData {
PlotDataPlotData196             PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {}
197             uint32_t fPageIndex;
198             uint32_t fPlotIndex;
199         };
200 
201     private:
find(int pageIdx,int index)202         bool find(int pageIdx, int index) const {
203             SkASSERT(index < kMaxPlots);
204             return (fPlotAlreadyUpdated[pageIdx] >> index) & 1;
205         }
206 
set(int pageIdx,int index)207         void set(int pageIdx, int index) {
208             SkASSERT(!this->find(pageIdx, index));
209             fPlotAlreadyUpdated[pageIdx] |= (1 << index);
210             fPlotsToUpdate.push_back(PlotData(pageIdx, index));
211         }
212 
213         static constexpr int kMinItems = 4;
214         SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate;
215         uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages]; // TODO: increase this to uint64_t
216                                                              //       to allow more plots per page
217 
218         friend class GrDrawOpAtlas;
219     };
220 
setLastUseTokenBulk(const BulkUseTokenUpdater & updater,GrDeferredUploadToken token)221     void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) {
222         int count = updater.fPlotsToUpdate.count();
223         for (int i = 0; i < count; i++) {
224             const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i];
225             // it's possible we've added a plot to the updater and subsequently the plot's page
226             // was deleted -- so we check to prevent a crash
227             if (pd.fPageIndex < fNumActivePages) {
228                 Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get();
229                 this->makeMRU(plot, pd.fPageIndex);
230                 plot->setLastUseToken(token);
231             }
232         }
233     }
234 
235     void compact(GrDeferredUploadToken startTokenForNextFlush, bool isRadicals);
236 
GetPageIndexFromID(AtlasID id)237     static uint32_t GetPageIndexFromID(AtlasID id) {
238         return id & 0xff;
239     }
240 
241     void instantiate(GrOnFlushResourceProvider*);
242 
maxPages()243     uint32_t maxPages() const {
244         return fMaxPages;
245     }
246 
247     int numAllocated_TestingOnly() const;
248     void setMaxPages_TestingOnly(uint32_t maxPages);
249 
250 private:
251     GrDrawOpAtlas(GrProxyProvider*, const GrBackendFormat& format, GrColorType, int width,
252                   int height, int plotWidth, int plotHeight,
253                   AllowMultitexturing allowMultitexturing, int atlasPageCount, int plotOldThreshold);
254 
255     /**
256      * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
257      * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
258      * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
259      * there is no room for the new subimage according to the GrRectanizer), it can no longer be
260      * used unless the last use of the Plot has already been flushed through to the gpu.
261      */
262     class Plot : public SkRefCnt {
263         SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
264 
265     public:
266         /** index() is a unique id for the plot relative to the owning GrAtlas and page. */
index()267         uint32_t index() const { return fPlotIndex; }
268         /**
269          * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
270          * if a particular subimage is still present in the atlas.
271          */
genID()272         uint64_t genID() const { return fGenID; }
id()273         GrDrawOpAtlas::AtlasID id() const {
274             SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID);
275             return fID;
276         }
277         SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
278 
279         bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
280 
281         /**
282          * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
283          * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
284          * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
285          * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
286          * has already flushed through the gpu then we can reuse the plot.
287          */
lastUploadToken()288         GrDeferredUploadToken lastUploadToken() const { return fLastUpload; }
lastUseToken()289         GrDeferredUploadToken lastUseToken() const { return fLastUse; }
setLastUploadToken(GrDeferredUploadToken token)290         void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; }
setLastUseToken(GrDeferredUploadToken token)291         void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; }
292 
293         void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*);
294         void resetRects();
295 
flushesSinceLastUsed()296         int flushesSinceLastUsed() { return fFlushesSinceLastUse; }
resetFlushesSinceLastUsed()297         void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; }
incFlushesSinceLastUsed()298         void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; }
299 
300     private:
301         Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY,
302              int width, int height, GrColorType colorType);
303 
304         ~Plot() override;
305 
306         /**
307          * Create a clone of this plot. The cloned plot will take the place of the current plot in
308          * the atlas
309          */
clone()310         Plot* clone() const {
311             return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight,
312                             fColorType);
313         }
314 
CreateId(uint32_t pageIdx,uint32_t plotIdx,uint64_t generation)315         static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx,
316                                                uint64_t generation) {
317             SkASSERT(pageIdx < (1 << 8));
318             SkASSERT(pageIdx < kMaxMultitexturePages);
319             SkASSERT(plotIdx < (1 << 8));
320             SkASSERT(generation < ((uint64_t)1 << 48));
321             return generation << 16 | plotIdx << 8 | pageIdx;
322         }
323 
324         GrDeferredUploadToken fLastUpload;
325         GrDeferredUploadToken fLastUse;
326         // the number of flushes since this plot has been last used
327         int                   fFlushesSinceLastUse;
328 
329         struct {
330             const uint32_t fPageIndex : 16;
331             const uint32_t fPlotIndex : 16;
332         };
333         uint64_t fGenID;
334         GrDrawOpAtlas::AtlasID fID;
335         unsigned char* fData;
336         const int fWidth;
337         const int fHeight;
338         const int fX;
339         const int fY;
340         GrRectanizer* fRects;
341         const SkIPoint16 fOffset;  // the offset of the plot in the backing texture
342         const GrColorType fColorType;
343         const size_t fBytesPerPixel;
344         SkIRect fDirtyRect;
345         SkDEBUGCODE(bool fDirty);
346 
347         friend class GrDrawOpAtlas;
348 
349         typedef SkRefCnt INHERITED;
350     };
351 
352     typedef SkTInternalLList<Plot> PlotList;
353 
GetPlotIndexFromID(AtlasID id)354     static uint32_t GetPlotIndexFromID(AtlasID id) {
355         return (id >> 8) & 0xff;
356     }
357 
358     // top 48 bits are reserved for the generation ID
GetGenerationFromID(AtlasID id)359     static uint64_t GetGenerationFromID(AtlasID id) {
360         return (id >> 16) & 0xffffffffffff;
361     }
362 
363     inline bool updatePlot(GrDeferredUploadTarget*, AtlasID*, Plot*);
364 
makeMRU(Plot * plot,int pageIdx)365     inline void makeMRU(Plot* plot, int pageIdx) {
366         if (fPages[pageIdx].fPlotList.head() == plot) {
367             return;
368         }
369 
370         fPages[pageIdx].fPlotList.remove(plot);
371         fPages[pageIdx].fPlotList.addToHead(plot);
372 
373         // No MRU update for pages -- since we will always try to add from
374         // the front and remove from the back there is no need for MRU.
375     }
376 
377     bool uploadToPage(unsigned int pageIdx, AtlasID* id, GrDeferredUploadTarget* target,
378                       int width, int height, const void* image, SkIPoint16* loc);
379 
380     bool createPages(GrProxyProvider*);
381     bool activateNewPage(GrResourceProvider*);
382     void deactivateLastPage();
383     void compactRadicals(GrDeferredUploadToken startTokenForNextFlush);
384 
385     void processEviction(AtlasID);
processEvictionAndResetRects(Plot * plot)386     inline void processEvictionAndResetRects(Plot* plot) {
387         this->processEviction(plot->id());
388         plot->resetRects();
389     }
390 
391     GrBackendFormat       fFormat;
392     GrColorType           fColorType;
393     int                   fTextureWidth;
394     int                   fTextureHeight;
395     int                   fPlotWidth;
396     int                   fPlotHeight;
397     unsigned int          fNumPlots;
398 
399     uint64_t              fAtlasGeneration;
400     // nextTokenToFlush() value at the end of the previous flush
401     GrDeferredUploadToken fPrevFlushToken;
402 
403     struct EvictionData {
404         EvictionFunc fFunc;
405         void* fData;
406     };
407 
408     SkTDArray<EvictionData> fEvictionCallbacks;
409 
410     struct Page {
411         // allocated array of Plots
412         std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
413         // LRU list of Plots (MRU at head - LRU at tail)
414         PlotList fPlotList;
415     };
416     // proxies kept separate to make it easier to pass them up to client
417     sk_sp<GrTextureProxy> fProxies[kMaxMultitexturePages];
418     Page fPages[kMaxMultitexturePages];
419     uint32_t fMaxPages;
420 
421     uint32_t fNumActivePages;
422     int fPlotOldThreshold;
423 };
424 
425 // There are three atlases (A8, 565, ARGB) that are kept in relation with one another. In
426 // general, the A8 dimensions are 2x the 565 and ARGB dimensions with the constraint that an atlas
427 // size will always contain at least one plot. Since the ARGB atlas takes the most space, its
428 // dimensions are used to size the other two atlases.
429 class GrDrawOpAtlasConfig {
430 public:
431     // The capabilities of the GPU define maxTextureSize. The client provides maxBytes, and this
432     // represents the largest they want a single atlas texture to be. Due to multitexturing, we
433     // may expand temporarily to use more space as needed.
434     GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes);
435 
436     // For testing only - make minimum sized atlases -- a single plot for ARGB, four for A8
GrDrawOpAtlasConfig()437     GrDrawOpAtlasConfig() : GrDrawOpAtlasConfig(kMaxAtlasDim, 0) {}
getARGBDimensions()438     SkISize getARGBDimensions(){ return fARGBDimensions; }
439     SkISize atlasDimensions(GrMaskFormat type) const;
440     SkISize plotDimensions(GrMaskFormat type) const;
441     int resetAsSmallPage();
442 
443 private:
444     // On some systems texture coordinates are represented using half-precision floating point,
445     // which limits the largest atlas dimensions to 2048x2048.
446     // For simplicity we'll use this constraint for all of our atlas textures.
447     // This can be revisited later if we need larger atlases.
448     static constexpr int kMaxAtlasDim = 2048;
449 
450     SkISize fARGBDimensions;
451     int     fMaxTextureSize;
452 };
453 
454 #endif
455