joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | class BatchPlot; |
| 17 | class GrBatchTarget; |
| 18 | class GrRectanizer; |
| 19 | |
| 20 | typedef SkTInternalLList<BatchPlot> GrBatchPlotList; |
| 21 | |
| 22 | class GrBatchAtlas { |
| 23 | public: |
| 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 |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 27 | typedef uint64_t AtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 28 | static const uint32_t kInvalidAtlasID = 0; |
| 29 | static const uint64_t kInvalidAtlasGeneration = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 30 | |
| 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. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 43 | // 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 |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 46 | bool addToAtlas(AtlasID*, GrBatchTarget*, int width, int height, const void* image, |
| 47 | SkIPoint16* loc); |
| 48 | |
| 49 | GrTexture* getTexture() const { return fTexture; } |
| 50 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 51 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 52 | bool hasID(AtlasID id); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 53 | |
| 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); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 56 | void registerEvictionCallback(EvictionFunc func, void* userData) { |
| 57 | EvictionData* data = fEvictionCallbacks.append(); |
| 58 | data->fFunc = func; |
| 59 | data->fData = userData; |
| 60 | } |
| 61 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 62 | /* |
| 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: |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 69 | BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {} |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame^] | 70 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
| 71 | : fPlotsToUpdate(that.fPlotsToUpdate) |
| 72 | , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) { |
| 73 | } |
| 74 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 75 | void add(AtlasID id) { |
| 76 | int index = GrBatchAtlas::GetIndexFromID(id); |
| 77 | if (!this->find(index)) { |
| 78 | this->set(index); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void reset() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 83 | fPlotsToUpdate.reset(); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 84 | fPlotAlreadyUpdated = 0; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | bool find(int index) const { |
| 89 | SkASSERT(index < kMaxPlots); |
| 90 | return (fPlotAlreadyUpdated >> index) & 1; |
| 91 | } |
| 92 | |
| 93 | void set(int index) { |
| 94 | SkASSERT(!this->find(index)); |
| 95 | fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 96 | fPlotsToUpdate.push_back(index); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static const int kMinItems = 4; |
| 100 | static const int kMaxPlots = 32; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 101 | SkSTArray<kMinItems, int, true> fPlotsToUpdate; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 102 | uint32_t fPlotAlreadyUpdated; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 103 | |
| 104 | friend class GrBatchAtlas; |
| 105 | }; |
| 106 | |
| 107 | void setLastUseTokenBulk(const BulkUseTokenUpdater& reffer, BatchToken); |
| 108 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 109 | static const int kGlyphMaxDim = 256; |
| 110 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 111 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 112 | } |
| 113 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 114 | private: |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 115 | static uint32_t GetIndexFromID(AtlasID id) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 116 | return id & 0xffff; |
| 117 | } |
| 118 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 119 | // top 48 bits are reserved for the generation ID |
| 120 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 121 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | inline void updatePlot(GrBatchTarget*, AtlasID*, BatchPlot*); |
| 125 | |
| 126 | inline void makeMRU(BatchPlot* plot); |
| 127 | |
| 128 | inline void processEviction(AtlasID); |
| 129 | |
| 130 | GrTexture* fTexture; |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 131 | uint32_t fNumPlotsX; |
| 132 | uint32_t fNumPlotsY; |
| 133 | uint32_t fPlotWidth; |
| 134 | uint32_t fPlotHeight; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 135 | size_t fBPP; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 136 | uint64_t fAtlasGeneration; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 137 | |
| 138 | struct EvictionData { |
| 139 | EvictionFunc fFunc; |
| 140 | void* fData; |
| 141 | }; |
| 142 | |
| 143 | SkTDArray<EvictionData> fEvictionCallbacks; |
| 144 | // allocated array of GrBatchPlots |
| 145 | SkAutoTUnref<BatchPlot>* fPlotArray; |
| 146 | // LRU list of GrPlots (MRU at head - LRU at tail) |
| 147 | GrBatchPlotList fPlotList; |
| 148 | }; |
| 149 | |
| 150 | #endif |