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 |
| 27 | typedef uint32_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. |
| 43 | bool addToAtlas(AtlasID*, GrBatchTarget*, int width, int height, const void* image, |
| 44 | SkIPoint16* loc); |
| 45 | |
| 46 | GrTexture* getTexture() const { return fTexture; } |
| 47 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame^] | 48 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 49 | bool hasID(AtlasID id); |
| 50 | void setLastRefToken(AtlasID id, BatchToken batchToken); |
| 51 | void registerEvictionCallback(EvictionFunc func, void* userData) { |
| 52 | EvictionData* data = fEvictionCallbacks.append(); |
| 53 | data->fFunc = func; |
| 54 | data->fData = userData; |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | int getIndexFromID(AtlasID id) { |
| 59 | return id & 0xffff; |
| 60 | } |
| 61 | |
| 62 | int getGenerationFromID(AtlasID id) { |
| 63 | return (id >> 16) & 0xffff; |
| 64 | } |
| 65 | |
| 66 | inline void updatePlot(GrBatchTarget*, AtlasID*, BatchPlot*); |
| 67 | |
| 68 | inline void makeMRU(BatchPlot* plot); |
| 69 | |
| 70 | inline void processEviction(AtlasID); |
| 71 | |
| 72 | GrTexture* fTexture; |
| 73 | int fNumPlotsX; |
| 74 | int fNumPlotsY; |
| 75 | int fPlotWidth; |
| 76 | int fPlotHeight; |
| 77 | size_t fBPP; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame^] | 78 | uint64_t fAtlasGeneration; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 79 | |
| 80 | struct EvictionData { |
| 81 | EvictionFunc fFunc; |
| 82 | void* fData; |
| 83 | }; |
| 84 | |
| 85 | SkTDArray<EvictionData> fEvictionCallbacks; |
| 86 | // allocated array of GrBatchPlots |
| 87 | SkAutoTUnref<BatchPlot>* fPlotArray; |
| 88 | // LRU list of GrPlots (MRU at head - LRU at tail) |
| 89 | GrBatchPlotList fPlotList; |
| 90 | }; |
| 91 | |
| 92 | #endif |