blob: 4e235409246e16f2495e686a055027f4b504bf14 [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
joshualittda04e0e2015-08-19 08:16:43 -070022struct GrBatchAtlasConfig {
23 int numPlotsX() const { return fWidth / fPlotWidth; }
24 int numPlotsY() const { return fHeight / fPlotWidth; }
25 int fWidth;
26 int fHeight;
27 int fPlotWidth;
28 int fPlotHeight;
29};
30
joshualitt5bf99f12015-03-13 11:47:42 -070031class GrBatchAtlas {
32public:
joshualitt5bf99f12015-03-13 11:47:42 -070033 // An AtlasID is an opaque handle which callers can use to determine if the atlas contains
34 // a specific piece of data
joshualitt8db6fdc2015-07-31 08:25:07 -070035 typedef uint64_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070036 static const uint32_t kInvalidAtlasID = 0;
37 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070038
39 // A function pointer for use as a callback during eviction. Whenever GrBatchAtlas evicts a
40 // specific AtlasID, it will call all of the registered listeners so they can optionally process
41 // the eviction
42 typedef void (*EvictionFunc)(GrBatchAtlas::AtlasID, void*);
43
44 GrBatchAtlas(GrTexture*, int numPlotsX, int numPlotsY);
45 ~GrBatchAtlas();
46
47 // Adds a width x height subimage to the atlas. Upon success it returns
48 // the containing GrPlot and absolute location in the backing texture.
49 // NULL is returned if the subimage cannot fit in the atlas.
50 // If provided, the image data will be written to the CPU-side backing bitmap.
joshualittb4c507e2015-04-08 08:07:59 -070051 // NOTE: If the client intends to refer to the atlas, they should immediately call 'setUseToken'
52 // with the currentToken from the batch target, otherwise the next call to addToAtlas might
53 // cause an eviction
bsalomon75398562015-08-17 12:55:38 -070054 bool addToAtlas(AtlasID*, GrDrawBatch::Target*, int width, int height, const void* image,
joshualitt5bf99f12015-03-13 11:47:42 -070055 SkIPoint16* loc);
56
57 GrTexture* getTexture() const { return fTexture; }
58
joshualitt7c3a2f82015-03-31 13:32:05 -070059 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5bf99f12015-03-13 11:47:42 -070060 bool hasID(AtlasID id);
joshualittb4c507e2015-04-08 08:07:59 -070061
62 // To ensure the atlas does not evict a given entry, the client must set the last use token
bsalomon75398562015-08-17 12:55:38 -070063 void setLastUseToken(AtlasID id, GrBatchToken batchToken);
joshualitt5bf99f12015-03-13 11:47:42 -070064 void registerEvictionCallback(EvictionFunc func, void* userData) {
65 EvictionData* data = fEvictionCallbacks.append();
66 data->fFunc = func;
67 data->fData = userData;
68 }
69
joshualittb4c507e2015-04-08 08:07:59 -070070 /*
71 * A class which can be handed back to GrBatchAtlas for updating in bulk last use tokens. The
72 * current max number of plots the GrBatchAtlas can handle is 32, if in the future this is
73 * insufficient then we can move to a 64 bit int
74 */
75 class BulkUseTokenUpdater {
76 public:
joshualitt4314e082015-04-23 08:03:35 -070077 BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {}
joshualitt7e97b0b2015-07-31 15:18:08 -070078 BulkUseTokenUpdater(const BulkUseTokenUpdater& that)
79 : fPlotsToUpdate(that.fPlotsToUpdate)
80 , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) {
81 }
82
joshualittb4c507e2015-04-08 08:07:59 -070083 void add(AtlasID id) {
84 int index = GrBatchAtlas::GetIndexFromID(id);
85 if (!this->find(index)) {
86 this->set(index);
87 }
88 }
89
90 void reset() {
joshualitt4314e082015-04-23 08:03:35 -070091 fPlotsToUpdate.reset();
joshualittb4c507e2015-04-08 08:07:59 -070092 fPlotAlreadyUpdated = 0;
93 }
94
95 private:
96 bool find(int index) const {
97 SkASSERT(index < kMaxPlots);
98 return (fPlotAlreadyUpdated >> index) & 1;
99 }
100
101 void set(int index) {
102 SkASSERT(!this->find(index));
103 fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index);
joshualitt97202d22015-04-22 13:47:02 -0700104 fPlotsToUpdate.push_back(index);
joshualittb4c507e2015-04-08 08:07:59 -0700105 }
106
107 static const int kMinItems = 4;
108 static const int kMaxPlots = 32;
joshualitt97202d22015-04-22 13:47:02 -0700109 SkSTArray<kMinItems, int, true> fPlotsToUpdate;
joshualitt8672f4d2015-04-21 08:03:04 -0700110 uint32_t fPlotAlreadyUpdated;
joshualittb4c507e2015-04-08 08:07:59 -0700111
112 friend class GrBatchAtlas;
113 };
114
bsalomon75398562015-08-17 12:55:38 -0700115 void setLastUseTokenBulk(const BulkUseTokenUpdater& reffer, GrBatchToken);
joshualittb4c507e2015-04-08 08:07:59 -0700116
joshualitt010db532015-04-21 10:07:26 -0700117 static const int kGlyphMaxDim = 256;
118 static bool GlyphTooLargeForAtlas(int width, int height) {
119 return width > kGlyphMaxDim || height > kGlyphMaxDim;
120 }
121
joshualitt5bf99f12015-03-13 11:47:42 -0700122private:
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;
joshualitt8db6fdc2015-07-31 08:25:07 -0700139 uint32_t fNumPlotsX;
140 uint32_t fNumPlotsY;
141 uint32_t fPlotWidth;
142 uint32_t fPlotHeight;
joshualitt5bf99f12015-03-13 11:47:42 -0700143 size_t fBPP;
joshualitt7c3a2f82015-03-31 13:32:05 -0700144 uint64_t fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700145
146 struct EvictionData {
147 EvictionFunc fFunc;
148 void* fData;
149 };
150
151 SkTDArray<EvictionData> fEvictionCallbacks;
152 // allocated array of GrBatchPlots
153 SkAutoTUnref<BatchPlot>* fPlotArray;
154 // LRU list of GrPlots (MRU at head - LRU at tail)
155 GrBatchPlotList fPlotList;
156};
157
158#endif