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