blob: 494895314125b2f0b33f8ecf73f1cbd1027ec1bd [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
17class BatchPlot;
joshualitt5bf99f12015-03-13 11:47:42 -070018class GrRectanizer;
19
20typedef SkTInternalLList<BatchPlot> GrBatchPlotList;
21
22class GrBatchAtlas {
23public:
joshualitt5bf99f12015-03-13 11:47:42 -070024 // An AtlasID is an opaque handle which callers can use to determine if the atlas contains
25 // a specific piece of data
joshualitt8db6fdc2015-07-31 08:25:07 -070026 typedef uint64_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070027 static const uint32_t kInvalidAtlasID = 0;
28 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070029
30 // A function pointer for use as a callback during eviction. Whenever GrBatchAtlas evicts a
31 // specific AtlasID, it will call all of the registered listeners so they can optionally process
32 // the eviction
33 typedef void (*EvictionFunc)(GrBatchAtlas::AtlasID, void*);
34
35 GrBatchAtlas(GrTexture*, int numPlotsX, int numPlotsY);
36 ~GrBatchAtlas();
37
38 // Adds a width x height subimage to the atlas. Upon success it returns
39 // the containing GrPlot and absolute location in the backing texture.
40 // NULL is returned if the subimage cannot fit in the atlas.
41 // If provided, the image data will be written to the CPU-side backing bitmap.
joshualittb4c507e2015-04-08 08:07:59 -070042 // NOTE: If the client intends to refer to the atlas, they should immediately call 'setUseToken'
43 // with the currentToken from the batch target, otherwise the next call to addToAtlas might
44 // cause an eviction
bsalomon75398562015-08-17 12:55:38 -070045 bool addToAtlas(AtlasID*, GrDrawBatch::Target*, int width, int height, const void* image,
joshualitt5bf99f12015-03-13 11:47:42 -070046 SkIPoint16* loc);
47
48 GrTexture* getTexture() const { return fTexture; }
49
joshualitt7c3a2f82015-03-31 13:32:05 -070050 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5bf99f12015-03-13 11:47:42 -070051 bool hasID(AtlasID id);
joshualittb4c507e2015-04-08 08:07:59 -070052
53 // To ensure the atlas does not evict a given entry, the client must set the last use token
bsalomon75398562015-08-17 12:55:38 -070054 void setLastUseToken(AtlasID id, GrBatchToken batchToken);
joshualitt5bf99f12015-03-13 11:47:42 -070055 void registerEvictionCallback(EvictionFunc func, void* userData) {
56 EvictionData* data = fEvictionCallbacks.append();
57 data->fFunc = func;
58 data->fData = userData;
59 }
60
joshualittb4c507e2015-04-08 08:07:59 -070061 /*
62 * A class which can be handed back to GrBatchAtlas for updating in bulk last use tokens. The
63 * current max number of plots the GrBatchAtlas can handle is 32, if in the future this is
64 * insufficient then we can move to a 64 bit int
65 */
66 class BulkUseTokenUpdater {
67 public:
joshualitt4314e082015-04-23 08:03:35 -070068 BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {}
joshualitt7e97b0b2015-07-31 15:18:08 -070069 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
70 : fPlotsToUpdate(that.fPlotsToUpdate)
71 , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) {
72 }
73
joshualittb4c507e2015-04-08 08:07:59 -070074 void add(AtlasID id) {
75 int index = GrBatchAtlas::GetIndexFromID(id);
76 if (!this->find(index)) {
77 this->set(index);
78 }
79 }
80
81 void reset() {
joshualitt4314e082015-04-23 08:03:35 -070082 fPlotsToUpdate.reset();
joshualittb4c507e2015-04-08 08:07:59 -070083 fPlotAlreadyUpdated = 0;
84 }
85
86 private:
87 bool find(int index) const {
88 SkASSERT(index < kMaxPlots);
89 return (fPlotAlreadyUpdated >> index) & 1;
90 }
91
92 void set(int index) {
93 SkASSERT(!this->find(index));
94 fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index);
joshualitt97202d22015-04-22 13:47:02 -070095 fPlotsToUpdate.push_back(index);
joshualittb4c507e2015-04-08 08:07:59 -070096 }
97
98 static const int kMinItems = 4;
99 static const int kMaxPlots = 32;
joshualitt97202d22015-04-22 13:47:02 -0700100 SkSTArray<kMinItems, int, true> fPlotsToUpdate;
joshualitt8672f4d2015-04-21 08:03:04 -0700101 uint32_t fPlotAlreadyUpdated;
joshualittb4c507e2015-04-08 08:07:59 -0700102
103 friend class GrBatchAtlas;
104 };
105
bsalomon75398562015-08-17 12:55:38 -0700106 void setLastUseTokenBulk(const BulkUseTokenUpdater& reffer, GrBatchToken);
joshualittb4c507e2015-04-08 08:07:59 -0700107
joshualitt010db532015-04-21 10:07:26 -0700108 static const int kGlyphMaxDim = 256;
109 static bool GlyphTooLargeForAtlas(int width, int height) {
110 return width > kGlyphMaxDim || height > kGlyphMaxDim;
111 }
112
joshualitt5bf99f12015-03-13 11:47:42 -0700113private:
joshualitt8db6fdc2015-07-31 08:25:07 -0700114 static uint32_t GetIndexFromID(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700115 return id & 0xffff;
116 }
117
joshualitt8db6fdc2015-07-31 08:25:07 -0700118 // top 48 bits are reserved for the generation ID
119 static uint64_t GetGenerationFromID(AtlasID id) {
120 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700121 }
122
bsalomon75398562015-08-17 12:55:38 -0700123 inline void updatePlot(GrDrawBatch::Target*, AtlasID*, BatchPlot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700124
125 inline void makeMRU(BatchPlot* plot);
126
127 inline void processEviction(AtlasID);
128
129 GrTexture* fTexture;
joshualitt8db6fdc2015-07-31 08:25:07 -0700130 uint32_t fNumPlotsX;
131 uint32_t fNumPlotsY;
132 uint32_t fPlotWidth;
133 uint32_t fPlotHeight;
joshualitt5bf99f12015-03-13 11:47:42 -0700134 size_t fBPP;
joshualitt7c3a2f82015-03-31 13:32:05 -0700135 uint64_t fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700136
137 struct EvictionData {
138 EvictionFunc fFunc;
139 void* fData;
140 };
141
142 SkTDArray<EvictionData> fEvictionCallbacks;
143 // allocated array of GrBatchPlots
144 SkAutoTUnref<BatchPlot>* fPlotArray;
145 // LRU list of GrPlots (MRU at head - LRU at tail)
146 GrBatchPlotList fPlotList;
147};
148
149#endif