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 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 20 | struct 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 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 29 | class GrBatchAtlas { |
| 30 | public: |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 31 | // An AtlasID is an opaque handle which callers can use to determine if the atlas contains |
| 32 | // a specific piece of data |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 33 | typedef uint64_t AtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 34 | static const uint32_t kInvalidAtlasID = 0; |
| 35 | static const uint64_t kInvalidAtlasGeneration = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 36 | |
| 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 47 | // nullptr is returned if the subimage cannot fit in the atlas. |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 48 | // 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] | 49 | // 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 |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 52 | bool addToAtlas(AtlasID*, GrDrawBatch::Target*, int width, int height, const void* image, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 53 | SkIPoint16* loc); |
| 54 | |
| 55 | GrTexture* getTexture() const { return fTexture; } |
| 56 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 57 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 58 | bool hasID(AtlasID id); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 59 | |
| 60 | // 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] | 61 | void setLastUseToken(AtlasID id, GrBatchToken batchToken); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 62 | void registerEvictionCallback(EvictionFunc func, void* userData) { |
| 63 | EvictionData* data = fEvictionCallbacks.append(); |
| 64 | data->fFunc = func; |
| 65 | data->fData = userData; |
| 66 | } |
| 67 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 68 | /* |
| 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: |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 75 | BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {} |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 76 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
| 77 | : fPlotsToUpdate(that.fPlotsToUpdate) |
| 78 | , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) { |
| 79 | } |
| 80 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 81 | 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() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 89 | fPlotsToUpdate.reset(); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 90 | 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); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 102 | fPlotsToUpdate.push_back(index); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static const int kMinItems = 4; |
| 106 | static const int kMaxPlots = 32; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 107 | SkSTArray<kMinItems, int, true> fPlotsToUpdate; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 108 | uint32_t fPlotAlreadyUpdated; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 109 | |
| 110 | friend class GrBatchAtlas; |
| 111 | }; |
| 112 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 113 | void setLastUseTokenBulk(const BulkUseTokenUpdater& reffer, GrBatchToken); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 114 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 115 | static const int kGlyphMaxDim = 256; |
| 116 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 117 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 118 | } |
| 119 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 120 | private: |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame^] | 121 | typedef SkTInternalLList<BatchPlot> GrBatchPlotList; |
| 122 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 123 | static uint32_t GetIndexFromID(AtlasID id) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 124 | return id & 0xffff; |
| 125 | } |
| 126 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 127 | // top 48 bits are reserved for the generation ID |
| 128 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 129 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 130 | } |
| 131 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 132 | inline void updatePlot(GrDrawBatch::Target*, AtlasID*, BatchPlot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 133 | |
| 134 | inline void makeMRU(BatchPlot* plot); |
| 135 | |
| 136 | inline void processEviction(AtlasID); |
| 137 | |
| 138 | GrTexture* fTexture; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame^] | 139 | SkDEBUGCODE(uint32_t fNumPlots;) |
| 140 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 141 | uint64_t fAtlasGeneration; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 142 | |
| 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 |