blob: 0f36951922f47558ea10a2282fa1d8ba38b77645 [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
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; }
joshualitt5bf99f12015-03-13 11:47:42 -070058 bool hasID(AtlasID id);
joshualittb4c507e2015-04-08 08:07:59 -070059
60 // To ensure the atlas does not evict a given entry, the client must set the last use token
bsalomon75398562015-08-17 12:55:38 -070061 void setLastUseToken(AtlasID id, GrBatchToken batchToken);
joshualitt5bf99f12015-03-13 11:47:42 -070062 void registerEvictionCallback(EvictionFunc func, void* userData) {
63 EvictionData* data = fEvictionCallbacks.append();
64 data->fFunc = func;
65 data->fData = userData;
66 }
67
joshualittb4c507e2015-04-08 08:07:59 -070068 /*
69 * A class which can be handed back to GrBatchAtlas for updating in bulk last use tokens. The
70 * current max number of plots the GrBatchAtlas can handle is 32, if in the future this is
71 * insufficient then we can move to a 64 bit int
72 */
73 class BulkUseTokenUpdater {
74 public:
joshualitt4314e082015-04-23 08:03:35 -070075 BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {}
joshualitt7e97b0b2015-07-31 15:18:08 -070076 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
77 : fPlotsToUpdate(that.fPlotsToUpdate)
78 , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) {
79 }
80
joshualittb4c507e2015-04-08 08:07:59 -070081 void add(AtlasID id) {
82 int index = GrBatchAtlas::GetIndexFromID(id);
83 if (!this->find(index)) {
84 this->set(index);
85 }
86 }
87
88 void reset() {
joshualitt4314e082015-04-23 08:03:35 -070089 fPlotsToUpdate.reset();
joshualittb4c507e2015-04-08 08:07:59 -070090 fPlotAlreadyUpdated = 0;
91 }
92
93 private:
94 bool find(int index) const {
95 SkASSERT(index < kMaxPlots);
96 return (fPlotAlreadyUpdated >> index) & 1;
97 }
98
99 void set(int index) {
100 SkASSERT(!this->find(index));
101 fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index);
joshualitt97202d22015-04-22 13:47:02 -0700102 fPlotsToUpdate.push_back(index);
joshualittb4c507e2015-04-08 08:07:59 -0700103 }
104
105 static const int kMinItems = 4;
106 static const int kMaxPlots = 32;
joshualitt97202d22015-04-22 13:47:02 -0700107 SkSTArray<kMinItems, int, true> fPlotsToUpdate;
joshualitt8672f4d2015-04-21 08:03:04 -0700108 uint32_t fPlotAlreadyUpdated;
joshualittb4c507e2015-04-08 08:07:59 -0700109
110 friend class GrBatchAtlas;
111 };
112
bsalomon75398562015-08-17 12:55:38 -0700113 void setLastUseTokenBulk(const BulkUseTokenUpdater& reffer, GrBatchToken);
joshualittb4c507e2015-04-08 08:07:59 -0700114
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:
robertphillips2b0536f2015-11-06 14:10:42 -0800121 typedef SkTInternalLList<BatchPlot> GrBatchPlotList;
122
joshualitt8db6fdc2015-07-31 08:25:07 -0700123 static uint32_t GetIndexFromID(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700124 return id & 0xffff;
125 }
126
joshualitt8db6fdc2015-07-31 08:25:07 -0700127 // top 48 bits are reserved for the generation ID
128 static uint64_t GetGenerationFromID(AtlasID id) {
129 return (id >> 16) & 0xffffffffffff;
joshualitt5bf99f12015-03-13 11:47:42 -0700130 }
131
bsalomon75398562015-08-17 12:55:38 -0700132 inline void updatePlot(GrDrawBatch::Target*, AtlasID*, BatchPlot*);
joshualitt5bf99f12015-03-13 11:47:42 -0700133
134 inline void makeMRU(BatchPlot* plot);
135
136 inline void processEviction(AtlasID);
137
138 GrTexture* fTexture;
robertphillips2b0536f2015-11-06 14:10:42 -0800139 SkDEBUGCODE(uint32_t fNumPlots;)
140
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