blob: 61fc7e10934c9ccbc4204da520b26ec7d0401e8c [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
8#ifndef GrBatchAtlas_DEFINED
9#define GrBatchAtlas_DEFINED
10
11#include "GrTexture.h"
12#include "SkPoint.h"
13#include "SkTDArray.h"
14#include "SkTInternalLList.h"
15
joshualittddd22d82016-02-16 06:47:52 -080016#include "batches/GrDrawBatch.h"
17
joshualitt5bf99f12015-03-13 11:47:42 -070018class GrRectanizer;
19
joshualittda04e0e2015-08-19 08:16:43 -070020struct GrBatchAtlasConfig {
21 int numPlotsX() const { return fWidth / fPlotWidth; }
22 int numPlotsY() const { return fHeight / fPlotWidth; }
23 int fWidth;
24 int fHeight;
25 int fPlotWidth;
26 int fPlotHeight;
27};
28
joshualitt5bf99f12015-03-13 11:47:42 -070029class GrBatchAtlas {
30public:
joshualitt5bf99f12015-03-13 11:47:42 -070031 // An AtlasID is an opaque handle which callers can use to determine if the atlas contains
32 // a specific piece of data
joshualitt8db6fdc2015-07-31 08:25:07 -070033 typedef uint64_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070034 static const uint32_t kInvalidAtlasID = 0;
35 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070036
37 // A function pointer for use as a callback during eviction. Whenever GrBatchAtlas evicts a
38 // specific AtlasID, it will call all of the registered listeners so they can optionally process
39 // the eviction
40 typedef void (*EvictionFunc)(GrBatchAtlas::AtlasID, void*);
41
42 GrBatchAtlas(GrTexture*, int numPlotsX, int numPlotsY);
43 ~GrBatchAtlas();
44
45 // Adds a width x height subimage to the atlas. Upon success it returns
46 // the containing GrPlot and absolute location in the backing texture.
halcanary96fcdcc2015-08-27 07:41:13 -070047 // nullptr is returned if the subimage cannot fit in the atlas.
joshualitt5bf99f12015-03-13 11:47:42 -070048 // If provided, the image data will be written to the CPU-side backing bitmap.
joshualittb4c507e2015-04-08 08:07:59 -070049 // NOTE: If the client intends to refer to the atlas, they should immediately call 'setUseToken'
50 // with the currentToken from the batch target, otherwise the next call to addToAtlas might
51 // cause an eviction
bsalomon75398562015-08-17 12:55:38 -070052 bool addToAtlas(AtlasID*, GrDrawBatch::Target*, int width, int height, const void* image,
joshualitt5bf99f12015-03-13 11:47:42 -070053 SkIPoint16* loc);
54
55 GrTexture* getTexture() const { return fTexture; }
56
joshualitt7c3a2f82015-03-31 13:32:05 -070057 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5df175e2015-11-18 13:37:54 -080058
59 inline bool hasID(AtlasID id) {
60 uint32_t index = GetIndexFromID(id);
61 SkASSERT(index < fNumPlots);
62 return fPlotArray[index]->genID() == GetGenerationFromID(id);
63 }
joshualittb4c507e2015-04-08 08:07:59 -070064
65 // To ensure the atlas does not evict a given entry, the client must set the last use token
joshualitt5df175e2015-11-18 13:37:54 -080066 inline void setLastUseToken(AtlasID id, GrBatchToken batchToken) {
67 SkASSERT(this->hasID(id));
68 uint32_t index = GetIndexFromID(id);
69 SkASSERT(index < fNumPlots);
70 this->makeMRU(fPlotArray[index]);
71 fPlotArray[index]->setLastUseToken(batchToken);
72 }
73
74 inline void registerEvictionCallback(EvictionFunc func, void* userData) {
joshualitt5bf99f12015-03-13 11:47:42 -070075 EvictionData* data = fEvictionCallbacks.append();
76 data->fFunc = func;
77 data->fData = userData;
78 }
79
joshualittb4c507e2015-04-08 08:07:59 -070080 /*
81 * A class which can be handed back to GrBatchAtlas for updating in bulk last use tokens. The
82 * current max number of plots the GrBatchAtlas can handle is 32, if in the future this is
83 * insufficient then we can move to a 64 bit int
84 */
85 class BulkUseTokenUpdater {
86 public:
joshualitt4314e082015-04-23 08:03:35 -070087 BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {}
joshualitt7e97b0b2015-07-31 15:18:08 -070088 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
89 : fPlotsToUpdate(that.fPlotsToUpdate)
90 , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) {
91 }
92
joshualittb4c507e2015-04-08 08:07:59 -070093 void add(AtlasID id) {
94 int index = GrBatchAtlas::GetIndexFromID(id);
95 if (!this->find(index)) {
96 this->set(index);
97 }
98 }
99
100 void reset() {
joshualitt4314e082015-04-23 08:03:35 -0700101 fPlotsToUpdate.reset();
joshualittb4c507e2015-04-08 08:07:59 -0700102 fPlotAlreadyUpdated = 0;
103 }
104
105 private:
106 bool find(int index) const {
107 SkASSERT(index < kMaxPlots);
108 return (fPlotAlreadyUpdated >> index) & 1;
109 }
110
111 void set(int index) {
112 SkASSERT(!this->find(index));
113 fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index);
joshualitt97202d22015-04-22 13:47:02 -0700114 fPlotsToUpdate.push_back(index);
joshualittb4c507e2015-04-08 08:07:59 -0700115 }
116
117 static const int kMinItems = 4;
118 static const int kMaxPlots = 32;
joshualitt97202d22015-04-22 13:47:02 -0700119 SkSTArray<kMinItems, int, true> fPlotsToUpdate;
joshualitt8672f4d2015-04-21 08:03:04 -0700120 uint32_t fPlotAlreadyUpdated;
joshualittb4c507e2015-04-08 08:07:59 -0700121
122 friend class GrBatchAtlas;
123 };
124
joshualitt5df175e2015-11-18 13:37:54 -0800125 void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrBatchToken batchToken) {
126 int count = updater.fPlotsToUpdate.count();
127 for (int i = 0; i < count; i++) {
128 BatchPlot* plot = fPlotArray[updater.fPlotsToUpdate[i]];
129 this->makeMRU(plot);
130 plot->setLastUseToken(batchToken);
131 }
132 }
joshualittb4c507e2015-04-08 08:07:59 -0700133
joshualitt010db532015-04-21 10:07:26 -0700134 static const int kGlyphMaxDim = 256;
135 static bool GlyphTooLargeForAtlas(int width, int height) {
136 return width > kGlyphMaxDim || height > kGlyphMaxDim;
137 }
138
joshualitt5bf99f12015-03-13 11:47:42 -0700139private:
joshualitt5df175e2015-11-18 13:37:54 -0800140 // The backing GrTexture for a GrBatchAtlas is broken into a spatial grid of BatchPlots.
141 // The BatchPlots keep track of subimage placement via their GrRectanizer. A BatchPlot
142 // manages the lifetime of its data using two tokens, a last use token and a last upload token.
143 // Once a BatchPlot is "full" (i.e. there is no room for the new subimage according to the
144 // GrRectanizer), it can no longer be used unless the last use of the GrPlot has already been
145 // flushed through to the gpu.
146 class BatchPlot : public SkRefCnt {
147 SK_DECLARE_INTERNAL_LLIST_INTERFACE(BatchPlot);
148
149 public:
150 // index() is a unique id for the plot relative to the owning GrAtlas. genID() is a
151 // monotonically incremented number which is bumped every time this plot is
152 // evicted from the cache (i.e., there is continuity in genID() across atlas spills).
153 uint32_t index() const { return fIndex; }
154 uint64_t genID() const { return fGenID; }
155 GrBatchAtlas::AtlasID id() const {
156 SkASSERT(GrBatchAtlas::kInvalidAtlasID != fID);
157 return fID;
158 }
159 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
160
161 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc);
162
163 // To manage the lifetime of a plot, we use two tokens. We use the last upload token to
164 // know when we can 'piggy back' uploads, ie if the last upload hasn't been flushed to gpu,
165 // we don't need to issue a new upload even if we update the cpu backing store. We use
166 // lastUse to determine when we can evict a plot from the cache, ie if the last use has
167 // already flushed through the gpu then we can reuse the plot.
168 GrBatchToken lastUploadToken() const { return fLastUpload; }
169 GrBatchToken lastUseToken() const { return fLastUse; }
170 void setLastUploadToken(GrBatchToken batchToken) {
171 SkASSERT(batchToken >= fLastUpload);
172 fLastUpload = batchToken;
173 }
174 void setLastUseToken(GrBatchToken batchToken) {
175 SkASSERT(batchToken >= fLastUse);
176 fLastUse = batchToken;
177 }
178
179 void uploadToTexture(GrBatchUploader::TextureUploader* uploader, GrTexture* texture);
180 void resetRects();
181
182 private:
183 BatchPlot(int index, uint64_t genID, int offX, int offY, int width, int height,
184 GrPixelConfig config);
185
186 ~BatchPlot() override;
187
188 // Create a clone of this plot. The cloned plot will take the place of the
189 // current plot in the atlas.
190 BatchPlot* clone() const {
191 return new BatchPlot(fIndex, fGenID+1, fX, fY, fWidth, fHeight, fConfig);
192 }
193
194 static GrBatchAtlas::AtlasID CreateId(uint32_t index, uint64_t generation) {
195 SkASSERT(index < (1 << 16));
196 SkASSERT(generation < ((uint64_t)1 << 48));
197 return generation << 16 | index;
198 }
199
200 GrBatchToken fLastUpload;
201 GrBatchToken fLastUse;
202
203 const uint32_t fIndex;
204 uint64_t fGenID;
205 GrBatchAtlas::AtlasID fID;
206 unsigned char* fData;
207 const int fWidth;
208 const int fHeight;
209 const int fX;
210 const int fY;
211 GrRectanizer* fRects;
212 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
213 const GrPixelConfig fConfig;
214 const size_t fBytesPerPixel;
215 SkIRect fDirtyRect;
216 SkDEBUGCODE(bool fDirty;)
217
218 friend class GrBatchAtlas;
219
220 typedef SkRefCnt INHERITED;
221 };
222
robertphillips2b0536f2015-11-06 14:10:42 -0800223 typedef SkTInternalLList<BatchPlot> GrBatchPlotList;
224
joshualitt8db6fdc2015-07-31 08:25:07 -0700225 static uint32_t GetIndexFromID(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700226 return id & 0xffff;
227 }
228
joshualitt8db6fdc2015-07-31 08:25:07 -0700229 // top 48 bits are reserved for the generation ID
230 static uint64_t GetGenerationFromID(AtlasID id) {
231 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700232 }
233
bsalomon75398562015-08-17 12:55:38 -0700234 inline void updatePlot(GrDrawBatch::Target*, AtlasID*, BatchPlot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700235
joshualitt5df175e2015-11-18 13:37:54 -0800236 inline void makeMRU(BatchPlot* plot) {
237 if (fPlotList.head() == plot) {
238 return;
239 }
240
241 fPlotList.remove(plot);
242 fPlotList.addToHead(plot);
243 }
joshualitt5bf99f12015-03-13 11:47:42 -0700244
245 inline void processEviction(AtlasID);
246
joshualitt5df175e2015-11-18 13:37:54 -0800247 friend class GrPlotUploader; // to access GrBatchPlot
248
joshualitt5bf99f12015-03-13 11:47:42 -0700249 GrTexture* fTexture;
robertphillips2b0536f2015-11-06 14:10:42 -0800250 SkDEBUGCODE(uint32_t fNumPlots;)
251
joshualitt7c3a2f82015-03-31 13:32:05 -0700252 uint64_t fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700253
254 struct EvictionData {
255 EvictionFunc fFunc;
256 void* fData;
257 };
258
259 SkTDArray<EvictionData> fEvictionCallbacks;
260 // allocated array of GrBatchPlots
261 SkAutoTUnref<BatchPlot>* fPlotArray;
262 // LRU list of GrPlots (MRU at head - LRU at tail)
263 GrBatchPlotList fPlotList;
264};
265
266#endif