blob: 2b85da1f7d7d2c3083323917d7b7dabedb02f213 [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
16class BatchPlot;
17class GrBatchTarget;
18class GrRectanizer;
19
20typedef SkTInternalLList<BatchPlot> GrBatchPlotList;
21
22class GrBatchAtlas {
23public:
24 typedef uint64_t BatchToken;
25 // An AtlasID is an opaque handle which callers can use to determine if the atlas contains
26 // a specific piece of data
27 typedef uint32_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070028 static const uint32_t kInvalidAtlasID = 0;
29 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070030
31 // A function pointer for use as a callback during eviction. Whenever GrBatchAtlas evicts a
32 // specific AtlasID, it will call all of the registered listeners so they can optionally process
33 // the eviction
34 typedef void (*EvictionFunc)(GrBatchAtlas::AtlasID, void*);
35
36 GrBatchAtlas(GrTexture*, int numPlotsX, int numPlotsY);
37 ~GrBatchAtlas();
38
39 // Adds a width x height subimage to the atlas. Upon success it returns
40 // the containing GrPlot and absolute location in the backing texture.
41 // NULL is returned if the subimage cannot fit in the atlas.
42 // If provided, the image data will be written to the CPU-side backing bitmap.
joshualittb4c507e2015-04-08 08:07:59 -070043 // NOTE: If the client intends to refer to the atlas, they should immediately call 'setUseToken'
44 // with the currentToken from the batch target, otherwise the next call to addToAtlas might
45 // cause an eviction
joshualitt5bf99f12015-03-13 11:47:42 -070046 bool addToAtlas(AtlasID*, GrBatchTarget*, int width, int height, const void* image,
47 SkIPoint16* loc);
48
49 GrTexture* getTexture() const { return fTexture; }
50
joshualitt7c3a2f82015-03-31 13:32:05 -070051 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5bf99f12015-03-13 11:47:42 -070052 bool hasID(AtlasID id);
joshualittb4c507e2015-04-08 08:07:59 -070053
54 // To ensure the atlas does not evict a given entry, the client must set the last use token
55 void setLastUseToken(AtlasID id, BatchToken batchToken);
joshualitt5bf99f12015-03-13 11:47:42 -070056 void registerEvictionCallback(EvictionFunc func, void* userData) {
57 EvictionData* data = fEvictionCallbacks.append();
58 data->fFunc = func;
59 data->fData = userData;
60 }
61
joshualittb4c507e2015-04-08 08:07:59 -070062 /*
63 * A class which can be handed back to GrBatchAtlas for updating in bulk last use tokens. The
64 * current max number of plots the GrBatchAtlas can handle is 32, if in the future this is
65 * insufficient then we can move to a 64 bit int
66 */
67 class BulkUseTokenUpdater {
68 public:
69 BulkUseTokenUpdater() : fPlotAlreadyUpdated(0), fCount(0), fAllocated(kMinItems) {}
70 void add(AtlasID id) {
71 int index = GrBatchAtlas::GetIndexFromID(id);
72 if (!this->find(index)) {
73 this->set(index);
74 }
75 }
76
77 void reset() {
78 fPlotsToUpdate.reset(kMinItems);
79 fAllocated = kMinItems;
80 fCount = 0;
81 fPlotAlreadyUpdated = 0;
82 }
83
84 private:
85 bool find(int index) const {
86 SkASSERT(index < kMaxPlots);
87 return (fPlotAlreadyUpdated >> index) & 1;
88 }
89
90 void set(int index) {
91 SkASSERT(!this->find(index));
92 fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index);
93 if (fCount < fAllocated) {
94 fPlotsToUpdate[fCount++] = index;
95 } else {
96 // This case will almost never happen
97 fAllocated = fCount << 1;
98 fPlotsToUpdate.realloc(fAllocated);
99 fPlotsToUpdate[fCount++] = index;
100 }
101 }
102
103 static const int kMinItems = 4;
104 static const int kMaxPlots = 32;
joshualittb4c507e2015-04-08 08:07:59 -0700105 SkAutoSTMalloc<kMinItems, int> fPlotsToUpdate;
joshualitt8672f4d2015-04-21 08:03:04 -0700106 uint32_t fPlotAlreadyUpdated;
joshualittb4c507e2015-04-08 08:07:59 -0700107 int fCount;
108 int fAllocated;
109
110 friend class GrBatchAtlas;
111 };
112
113 void setLastUseTokenBulk(const BulkUseTokenUpdater& reffer, BatchToken);
114
joshualitt010db532015-04-21 10:07:26 -0700115 static const int kGlyphMaxDim = 256;
116 static bool GlyphTooLargeForAtlas(int width, int height) {
117 return width > kGlyphMaxDim || height > kGlyphMaxDim;
118 }
119
joshualitt5bf99f12015-03-13 11:47:42 -0700120private:
joshualittb4c507e2015-04-08 08:07:59 -0700121 static int GetIndexFromID(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700122 return id & 0xffff;
123 }
124
joshualittb4c507e2015-04-08 08:07:59 -0700125 static int GetGenerationFromID(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700126 return (id >> 16) & 0xffff;
127 }
128
129 inline void updatePlot(GrBatchTarget*, AtlasID*, BatchPlot*);
130
131 inline void makeMRU(BatchPlot* plot);
132
133 inline void processEviction(AtlasID);
134
135 GrTexture* fTexture;
136 int fNumPlotsX;
137 int fNumPlotsY;
138 int fPlotWidth;
139 int fPlotHeight;
140 size_t fBPP;
joshualitt7c3a2f82015-03-31 13:32:05 -0700141 uint64_t fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700142
143 struct EvictionData {
144 EvictionFunc fFunc;
145 void* fData;
146 };
147
148 SkTDArray<EvictionData> fEvictionCallbacks;
149 // allocated array of GrBatchPlots
150 SkAutoTUnref<BatchPlot>* fPlotArray;
151 // LRU list of GrPlots (MRU at head - LRU at tail)
152 GrBatchPlotList fPlotList;
153};
154
155#endif