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" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 12 | #include "batches/GrDrawBatch.h" |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 13 | #include "SkPoint.h" |
| 14 | #include "SkTDArray.h" |
| 15 | #include "SkTInternalLList.h" |
| 16 | |
| 17 | class BatchPlot; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 18 | class GrRectanizer; |
| 19 | |
| 20 | typedef SkTInternalLList<BatchPlot> GrBatchPlotList; |
| 21 | |
| 22 | class GrBatchAtlas { |
| 23 | public: |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 24 | // An AtlasID is an opaque handle which callers can use to determine if the atlas contains |
| 25 | // a specific piece of data |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 26 | typedef uint64_t AtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 27 | static const uint32_t kInvalidAtlasID = 0; |
| 28 | static const uint64_t kInvalidAtlasGeneration = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 29 | |
| 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. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 42 | // 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 |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 45 | bool addToAtlas(AtlasID*, GrDrawBatch::Target*, int width, int height, const void* image, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 46 | SkIPoint16* loc); |
| 47 | |
| 48 | GrTexture* getTexture() const { return fTexture; } |
| 49 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 50 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 51 | bool hasID(AtlasID id); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 52 | |
| 53 | // To ensure the atlas does not evict a given entry, the client must set the last use token |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 54 | void setLastUseToken(AtlasID id, GrBatchToken batchToken); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 55 | void registerEvictionCallback(EvictionFunc func, void* userData) { |
| 56 | EvictionData* data = fEvictionCallbacks.append(); |
| 57 | data->fFunc = func; |
| 58 | data->fData = userData; |
| 59 | } |
| 60 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 61 | /* |
| 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: |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 68 | BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {} |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 69 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
| 70 | : fPlotsToUpdate(that.fPlotsToUpdate) |
| 71 | , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) { |
| 72 | } |
| 73 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 74 | 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() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 82 | fPlotsToUpdate.reset(); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 83 | 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); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 95 | fPlotsToUpdate.push_back(index); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static const int kMinItems = 4; |
| 99 | static const int kMaxPlots = 32; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 100 | SkSTArray<kMinItems, int, true> fPlotsToUpdate; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 101 | uint32_t fPlotAlreadyUpdated; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 102 | |
| 103 | friend class GrBatchAtlas; |
| 104 | }; |
| 105 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 106 | void setLastUseTokenBulk(const BulkUseTokenUpdater& reffer, GrBatchToken); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 107 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 108 | static const int kGlyphMaxDim = 256; |
| 109 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 110 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 111 | } |
| 112 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 113 | private: |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 114 | static uint32_t GetIndexFromID(AtlasID id) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 115 | return id & 0xffff; |
| 116 | } |
| 117 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 118 | // top 48 bits are reserved for the generation ID |
| 119 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 120 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 121 | } |
| 122 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame^] | 123 | inline void updatePlot(GrDrawBatch::Target*, AtlasID*, BatchPlot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 124 | |
| 125 | inline void makeMRU(BatchPlot* plot); |
| 126 | |
| 127 | inline void processEviction(AtlasID); |
| 128 | |
| 129 | GrTexture* fTexture; |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 130 | uint32_t fNumPlotsX; |
| 131 | uint32_t fNumPlotsY; |
| 132 | uint32_t fPlotWidth; |
| 133 | uint32_t fPlotHeight; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 134 | size_t fBPP; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 135 | uint64_t fAtlasGeneration; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 136 | |
| 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 |