blob: c857d07703132e2784be9d372ee8ce9e41d26810 [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
11#include "GrTexture.h"
12#include "SkPoint.h"
13#include "SkTDArray.h"
14#include "SkTInternalLList.h"
15
Brian Salomon89527432016-12-16 09:52:16 -050016#include "ops/GrDrawOp.h"
joshualittddd22d82016-02-16 06:47:52 -080017
joshualitt5bf99f12015-03-13 11:47:42 -070018class GrRectanizer;
19
Brian Salomon2ee084e2016-12-16 18:59:19 -050020struct GrDrawOpAtlasConfig {
joshualittda04e0e2015-08-19 08:16:43 -070021 int numPlotsX() const { return fWidth / fPlotWidth; }
22 int numPlotsY() const { return fHeight / fPlotWidth; }
23 int fWidth;
24 int fHeight;
jvanverth7023a002016-02-22 11:25:32 -080025 int fLog2Width;
26 int fLog2Height;
joshualittda04e0e2015-08-19 08:16:43 -070027 int fPlotWidth;
28 int fPlotHeight;
29};
30
Brian Salomon2ee084e2016-12-16 18:59:19 -050031/**
32 * This class manages an atlas texture on behalf of GrDrawOps. The draw ops that use the atlas
33 * perform texture uploads when preparing their draws during flush. The class provides facilities
34 * for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in "asap" mode
35 * until it is impossible to add data without overwriting texels read by draws that have not yet
36 * executed on the gpu. At that point the uploads are performed "inline" between draws. If a single
37 * draw would use enough subimage space to overflow the atlas texture then the atlas will fail to
38 * add a subimage. This gives the op the chance to end the draw and begin a new one. Additional
39 * uploads will then succeed in inline mode.
40 */
41class GrDrawOpAtlas {
joshualitt5bf99f12015-03-13 11:47:42 -070042public:
Brian Salomon2ee084e2016-12-16 18:59:19 -050043 /**
44 * An AtlasID is an opaque handle which callers can use to determine if the atlas contains
45 * a specific piece of data.
46 */
joshualitt8db6fdc2015-07-31 08:25:07 -070047 typedef uint64_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070048 static const uint32_t kInvalidAtlasID = 0;
49 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070050
Brian Salomon2ee084e2016-12-16 18:59:19 -050051 /**
52 * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a
53 * specific AtlasID, it will call all of the registered listeners so they can process the
54 * eviction.
55 */
56 typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*);
joshualitt5bf99f12015-03-13 11:47:42 -070057
Robert Phillips256c37b2017-03-01 14:32:46 -050058 /**
59 * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas
60 * should only be used inside of GrMeshDrawOp::onPrepareDraws.
61 * @param GrPixelConfig The pixel config which this atlas will store
62 * @param width width in pixels of the atlas
63 * @param height height in pixels of the atlas
64 * @param numPlotsX The number of plots the atlas should be broken up into in the X
65 * direction
66 * @param numPlotsY The number of plots the atlas should be broken up into in the Y
67 * direction
68 * @param func An eviction function which will be called whenever the atlas has to
69 * evict data
70 * @param data User supplied data which will be passed into func whenver an
71 * eviction occurs
72 * @return An initialized GrDrawOpAtlas, or nullptr if creation fails
73 */
74 static std::unique_ptr<GrDrawOpAtlas> Make(GrContext*, GrPixelConfig,
75 int width, int height,
76 int numPlotsX, int numPlotsY,
77 GrDrawOpAtlas::EvictionFunc func, void* data);
joshualitt5bf99f12015-03-13 11:47:42 -070078
Brian Salomon2ee084e2016-12-16 18:59:19 -050079 /**
80 * Adds a width x height subimage to the atlas. Upon success it returns an ID and the subimage's
81 * coordinates in the backing texture. False is returned if the subimage cannot fit in the
82 * atlas without overwriting texels that will be read in the current draw. This indicates that
83 * the op should end its current draw and begin another before adding more data. Upon success,
84 * an upload of the provided image data will have been added to the GrDrawOp::Target, in "asap"
85 * mode if possible, otherwise in "inline" mode. Successive uploads in either mode may be
86 * consolidated.
87 * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call
88 * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to
89 * addToAtlas might cause the previous data to be overwritten before it has been read.
90 */
Brian Salomon9afd3712016-12-01 10:59:09 -050091 bool addToAtlas(AtlasID*, GrDrawOp::Target*, int width, int height, const void* image,
joshualitt5bf99f12015-03-13 11:47:42 -070092 SkIPoint16* loc);
93
Robert Phillips32f28182017-02-28 16:20:03 -050094 GrContext* context() const { return fContext; }
95 sk_sp<GrTextureProxy> getProxy() const { return fProxy; }
joshualitt5bf99f12015-03-13 11:47:42 -070096
joshualitt7c3a2f82015-03-31 13:32:05 -070097 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -080098
99 inline bool hasID(AtlasID id) {
100 uint32_t index = GetIndexFromID(id);
101 SkASSERT(index < fNumPlots);
102 return fPlotArray[index]->genID() == GetGenerationFromID(id);
103 }
joshualittb4c507e2015-04-08 08:07:59 -0700104
Brian Salomon2ee084e2016-12-16 18:59:19 -0500105 /** To ensure the atlas does not evict a given entry, the client must set the last use token. */
106 inline void setLastUseToken(AtlasID id, GrDrawOpUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800107 SkASSERT(this->hasID(id));
108 uint32_t index = GetIndexFromID(id);
109 SkASSERT(index < fNumPlots);
Hal Canary144caf52016-11-07 17:57:18 -0500110 this->makeMRU(fPlotArray[index].get());
Brian Salomon2ee084e2016-12-16 18:59:19 -0500111 fPlotArray[index]->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800112 }
113
114 inline void registerEvictionCallback(EvictionFunc func, void* userData) {
joshualitt5bf99f12015-03-13 11:47:42 -0700115 EvictionData* data = fEvictionCallbacks.append();
116 data->fFunc = func;
117 data->fData = userData;
118 }
119
Brian Salomon2ee084e2016-12-16 18:59:19 -0500120 /**
121 * A class which can be handed back to GrDrawOpAtlas for updating last use tokens in bulk. The
122 * current max number of plots the GrDrawOpAtlas can handle is 32. If in the future this is
123 * insufficient then we can move to a 64 bit int.
joshualittb4c507e2015-04-08 08:07:59 -0700124 */
125 class BulkUseTokenUpdater {
126 public:
joshualitt4314e082015-04-23 08:03:35 -0700127 BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {}
joshualitt7e97b0b2015-07-31 15:18:08 -0700128 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
129 : fPlotsToUpdate(that.fPlotsToUpdate)
130 , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) {
131 }
132
joshualittb4c507e2015-04-08 08:07:59 -0700133 void add(AtlasID id) {
Brian Salomon2ee084e2016-12-16 18:59:19 -0500134 int index = GrDrawOpAtlas::GetIndexFromID(id);
joshualittb4c507e2015-04-08 08:07:59 -0700135 if (!this->find(index)) {
136 this->set(index);
137 }
138 }
139
140 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700141 fPlotsToUpdate.reset();
joshualittb4c507e2015-04-08 08:07:59 -0700142 fPlotAlreadyUpdated = 0;
143 }
144
145 private:
146 bool find(int index) const {
147 SkASSERT(index < kMaxPlots);
148 return (fPlotAlreadyUpdated >> index) & 1;
149 }
150
151 void set(int index) {
152 SkASSERT(!this->find(index));
153 fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index);
joshualitt97202d22015-04-22 13:47:02 -0700154 fPlotsToUpdate.push_back(index);
joshualittb4c507e2015-04-08 08:07:59 -0700155 }
156
157 static const int kMinItems = 4;
158 static const int kMaxPlots = 32;
joshualitt97202d22015-04-22 13:47:02 -0700159 SkSTArray<kMinItems, int, true> fPlotsToUpdate;
joshualitt8672f4d2015-04-21 08:03:04 -0700160 uint32_t fPlotAlreadyUpdated;
joshualittb4c507e2015-04-08 08:07:59 -0700161
Brian Salomon2ee084e2016-12-16 18:59:19 -0500162 friend class GrDrawOpAtlas;
joshualittb4c507e2015-04-08 08:07:59 -0700163 };
164
Brian Salomon2ee084e2016-12-16 18:59:19 -0500165 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDrawOpUploadToken token) {
joshualitt5df175e2015-11-18 13:37:54 -0800166 int count = updater.fPlotsToUpdate.count();
167 for (int i = 0; i < count; i++) {
Brian Salomon2ee084e2016-12-16 18:59:19 -0500168 Plot* plot = fPlotArray[updater.fPlotsToUpdate[i]].get();
joshualitt5df175e2015-11-18 13:37:54 -0800169 this->makeMRU(plot);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500170 plot->setLastUseToken(token);
joshualitt5df175e2015-11-18 13:37:54 -0800171 }
172 }
joshualittb4c507e2015-04-08 08:07:59 -0700173
joshualitt010db532015-04-21 10:07:26 -0700174 static const int kGlyphMaxDim = 256;
175 static bool GlyphTooLargeForAtlas(int width, int height) {
176 return width > kGlyphMaxDim || height > kGlyphMaxDim;
177 }
178
joshualitt5bf99f12015-03-13 11:47:42 -0700179private:
Robert Phillips256c37b2017-03-01 14:32:46 -0500180 GrDrawOpAtlas(GrContext*, sk_sp<GrTextureProxy>, int numPlotsX, int numPlotsY);
181
Brian Salomon2ee084e2016-12-16 18:59:19 -0500182 /**
183 * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots
184 * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its
185 * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e.
186 * there is no room for the new subimage according to the GrRectanizer), it can no longer be
187 * used unless the last use of the Plot has already been flushed through to the gpu.
188 */
189 class Plot : public SkRefCnt {
190 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot);
joshualitt5df175e2015-11-18 13:37:54 -0800191
192 public:
Brian Salomon2ee084e2016-12-16 18:59:19 -0500193 /** index() is a unique id for the plot relative to the owning GrAtlas. */
joshualitt5df175e2015-11-18 13:37:54 -0800194 uint32_t index() const { return fIndex; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500195 /**
196 * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know
197 * if a particular subimage is still present in the atlas.
198 */
joshualitt5df175e2015-11-18 13:37:54 -0800199 uint64_t genID() const { return fGenID; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500200 GrDrawOpAtlas::AtlasID id() const {
201 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID);
joshualitt5df175e2015-11-18 13:37:54 -0800202 return fID;
203 }
204 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
205
206 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
207
Brian Salomon2ee084e2016-12-16 18:59:19 -0500208 /**
209 * To manage the lifetime of a plot, we use two tokens. We use the last upload token to
210 * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to
211 * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We
212 * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use
213 * has already flushed through the gpu then we can reuse the plot.
214 */
Brian Salomon9afd3712016-12-01 10:59:09 -0500215 GrDrawOpUploadToken lastUploadToken() const { return fLastUpload; }
216 GrDrawOpUploadToken lastUseToken() const { return fLastUse; }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500217 void setLastUploadToken(GrDrawOpUploadToken token) { fLastUpload = token; }
218 void setLastUseToken(GrDrawOpUploadToken token) { fLastUse = token; }
joshualitt5df175e2015-11-18 13:37:54 -0800219
Brian Salomon9afd3712016-12-01 10:59:09 -0500220 void uploadToTexture(GrDrawOp::WritePixelsFn&, GrTexture* texture);
joshualitt5df175e2015-11-18 13:37:54 -0800221 void resetRects();
222
223 private:
Brian Salomon2ee084e2016-12-16 18:59:19 -0500224 Plot(int index, uint64_t genID, int offX, int offY, int width, int height,
225 GrPixelConfig config);
joshualitt5df175e2015-11-18 13:37:54 -0800226
Brian Salomon2ee084e2016-12-16 18:59:19 -0500227 ~Plot() override;
joshualitt5df175e2015-11-18 13:37:54 -0800228
Brian Salomon2ee084e2016-12-16 18:59:19 -0500229 /**
230 * Create a clone of this plot. The cloned plot will take the place of the current plot in
231 * the atlas
232 */
233 Plot* clone() const {
234 return new Plot(fIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig);
joshualitt5df175e2015-11-18 13:37:54 -0800235 }
236
Brian Salomon2ee084e2016-12-16 18:59:19 -0500237 static GrDrawOpAtlas::AtlasID CreateId(uint32_t index, uint64_t generation) {
joshualitt5df175e2015-11-18 13:37:54 -0800238 SkASSERT(index < (1 << 16));
239 SkASSERT(generation < ((uint64_t)1 << 48));
240 return generation << 16 | index;
241 }
242
Brian Salomon9afd3712016-12-01 10:59:09 -0500243 GrDrawOpUploadToken fLastUpload;
244 GrDrawOpUploadToken fLastUse;
joshualitt5df175e2015-11-18 13:37:54 -0800245
Brian Salomon2ee084e2016-12-16 18:59:19 -0500246 const uint32_t fIndex;
247 uint64_t fGenID;
248 GrDrawOpAtlas::AtlasID fID;
249 unsigned char* fData;
250 const int fWidth;
251 const int fHeight;
252 const int fX;
253 const int fY;
254 GrRectanizer* fRects;
255 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
256 const GrPixelConfig fConfig;
257 const size_t fBytesPerPixel;
258 SkIRect fDirtyRect;
259 SkDEBUGCODE(bool fDirty);
joshualitt5df175e2015-11-18 13:37:54 -0800260
Brian Salomon2ee084e2016-12-16 18:59:19 -0500261 friend class GrDrawOpAtlas;
joshualitt5df175e2015-11-18 13:37:54 -0800262
263 typedef SkRefCnt INHERITED;
264 };
265
Brian Salomon2ee084e2016-12-16 18:59:19 -0500266 typedef SkTInternalLList<Plot> PlotList;
robertphillips2b0536f2015-11-06 14:10:42 -0800267
joshualitt8db6fdc2015-07-31 08:25:07 -0700268 static uint32_t GetIndexFromID(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700269 return id & 0xffff;
270 }
271
joshualitt8db6fdc2015-07-31 08:25:07 -0700272 // top 48 bits are reserved for the generation ID
273 static uint64_t GetGenerationFromID(AtlasID id) {
274 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700275 }
276
Robert Phillips256c37b2017-03-01 14:32:46 -0500277 inline bool updatePlot(GrDrawOp::Target*, AtlasID*, Plot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700278
Brian Salomon2ee084e2016-12-16 18:59:19 -0500279 inline void makeMRU(Plot* plot) {
joshualitt5df175e2015-11-18 13:37:54 -0800280 if (fPlotList.head() == plot) {
281 return;
282 }
283
284 fPlotList.remove(plot);
285 fPlotList.addToHead(plot);
286 }
joshualitt5bf99f12015-03-13 11:47:42 -0700287
288 inline void processEviction(AtlasID);
289
Robert Phillips32f28182017-02-28 16:20:03 -0500290 GrContext* fContext;
291 sk_sp<GrTextureProxy> fProxy;
292 int fPlotWidth;
293 int fPlotHeight;
294 SkDEBUGCODE(uint32_t fNumPlots;)
robertphillips2b0536f2015-11-06 14:10:42 -0800295
Robert Phillips32f28182017-02-28 16:20:03 -0500296 uint64_t fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700297
298 struct EvictionData {
299 EvictionFunc fFunc;
300 void* fData;
301 };
302
303 SkTDArray<EvictionData> fEvictionCallbacks;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500304 // allocated array of Plots
305 std::unique_ptr<sk_sp<Plot>[]> fPlotArray;
306 // LRU list of Plots (MRU at head - LRU at tail)
307 PlotList fPlotList;
joshualitt5bf99f12015-03-13 11:47:42 -0700308};
309
310#endif