rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 GrTextureStripAtlas_DEFINED |
| 9 | #define GrTextureStripAtlas_DEFINED |
| 10 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 11 | #include "SkBitmap.h" |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 12 | #include "SkGr.h" |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 13 | #include "SkOpts.h" |
| 14 | #include "SkRefCnt.h" |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 15 | #include "SkTDArray.h" |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 16 | #include "SkTDynamicHash.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 17 | #include "SkTypes.h" |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 18 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 19 | class GrSurfaceContext; |
| 20 | class GrTextureProxy; |
| 21 | |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 22 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 23 | * Maintains a single large texture whose rows store many textures of a small fixed height, |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 24 | * stored in rows across the x-axis such that we can safely wrap/repeat them horizontally. |
| 25 | */ |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 26 | class GrTextureStripAtlas : public SkRefCnt { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 27 | public: |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Descriptor struct which we'll use as a hash table key |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 30 | */ |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 31 | struct Desc { |
joshualitt | 690fc75 | 2015-07-13 12:49:13 -0700 | [diff] [blame] | 32 | Desc() { sk_bzero(this, sizeof(*this)); } |
joshualitt | 690fc75 | 2015-07-13 12:49:13 -0700 | [diff] [blame] | 33 | GrPixelConfig fConfig; |
| 34 | uint16_t fWidth, fHeight, fRowHeight; |
| 35 | uint16_t fUnusedPadding; |
| 36 | bool operator==(const Desc& other) const { |
| 37 | return 0 == memcmp(this, &other, sizeof(Desc)); |
| 38 | } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 41 | ~GrTextureStripAtlas(); |
| 42 | |
| 43 | /** |
| 44 | * Add a texture to the atlas |
| 45 | * @param data Bitmap data to copy into the row |
| 46 | * @return The row index we inserted into, or -1 if we failed to find an open row. The caller |
| 47 | * is responsible for calling unlockRow() with this row index when it's done with it. |
| 48 | */ |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 49 | int lockRow(GrContext*, const SkBitmap&); |
| 50 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 51 | /** |
| 52 | * This is intended to be used when cloning a processor that already holds a lock. It is |
| 53 | * assumed that the row already has at least one lock. |
| 54 | */ |
| 55 | void lockRow(int row); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 56 | void unlockRow(int row); |
| 57 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 58 | /** |
| 59 | * These functions help turn an integer row index in [0, 1, 2, ... numRows] into a scalar y |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 60 | * texture coordinate in [0, 1] that we can use in a shader. |
| 61 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 62 | * If a regular texture access without using the atlas looks like: |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 63 | * |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 64 | * texture2D(sampler, float2(x, y)) |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 65 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 66 | * Then when using the atlas we'd replace it with: |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 67 | * |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 68 | * texture2D(sampler, float2(x, yOffset + y * scaleFactor)) |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 69 | * |
| 70 | * Where yOffset, returned by getYOffset(), is the offset to the start of the row within the |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 71 | * atlas and scaleFactor, returned by getNormalizedTexelHeight, is the normalized height of |
| 72 | * one texel row. |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 73 | */ |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 74 | SkScalar getYOffset(int row) const { return SkIntToScalar(row) / fNumRows; } |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 75 | SkScalar getNormalizedTexelHeight() const { return fNormalizedYHeight; } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 76 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 77 | sk_sp<GrTextureProxy> asTextureProxyRef() const; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 80 | friend class GrTextureStripAtlasManager; // for ctor |
| 81 | |
| 82 | static uint32_t CreateUniqueID(); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 83 | |
| 84 | // Key to indicate an atlas row without any meaningful data stored in it |
| 85 | const static uint32_t kEmptyAtlasRowKey = 0xffffffff; |
| 86 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 87 | /** |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 88 | * The state of a single row in our cache, next/prev pointers allow these to be chained |
| 89 | * together to represent LRU status |
| 90 | */ |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 91 | struct AtlasRow : ::SkNoncopyable { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | AtlasRow() : fKey(kEmptyAtlasRowKey), fLocks(0), fNext(nullptr), fPrev(nullptr) { } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 93 | // GenerationID of the bitmap that is represented by this row, 0xffffffff means "empty" |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 94 | uint32_t fKey; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 95 | // How many times this has been locked (0 == unlocked) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 96 | int32_t fLocks; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 97 | // We maintain an LRU linked list between unlocked nodes with these pointers |
| 98 | AtlasRow* fNext; |
| 99 | AtlasRow* fPrev; |
| 100 | }; |
| 101 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 102 | /** |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 103 | * Only the GrTextureStripAtlasManager is allowed to create GrTextureStripAtlases |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 104 | */ |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 105 | GrTextureStripAtlas(const Desc& desc); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 106 | |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 107 | void lockTexture(GrContext*); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 108 | void unlockTexture(); |
| 109 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 110 | /** |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 111 | * Initialize our LRU list (if one already exists, clear it and start anew) |
| 112 | */ |
| 113 | void initLRU(); |
| 114 | |
| 115 | /** |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 116 | * Grabs the least recently used free row out of the LRU list, returns nullptr if no rows |
| 117 | * are free. |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 118 | */ |
| 119 | AtlasRow* getLRU(); |
| 120 | |
| 121 | void appendLRU(AtlasRow* row); |
| 122 | void removeFromLRU(AtlasRow* row); |
| 123 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Searches the key table for a key and returns the index if found; if not found, it returns |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 126 | * the bitwise not of the index at which we could insert the key to maintain a sorted list. |
| 127 | **/ |
| 128 | int searchByKey(uint32_t key); |
| 129 | |
| 130 | /** |
| 131 | * Compare two atlas rows by key, so we can sort/search by key |
| 132 | */ |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 133 | static bool KeyLess(const AtlasRow& lhs, const AtlasRow& rhs) { |
| 134 | return lhs.fKey < rhs.fKey; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | #ifdef SK_DEBUG |
| 138 | void validate(); |
| 139 | #endif |
| 140 | |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 141 | // A unique ID for this atlas, so we can be sure that if we |
rileya@google.com | f61c746 | 2012-08-13 21:03:39 +0000 | [diff] [blame] | 142 | // get a texture back from the texture cache, that it's the same one we last used. |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 143 | const uint32_t fCacheKey; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 144 | |
| 145 | // Total locks on all rows (when this reaches zero, we can unlock our texture) |
| 146 | int32_t fLockedRows; |
| 147 | |
| 148 | const Desc fDesc; |
| 149 | const uint16_t fNumRows; |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 150 | sk_sp<GrSurfaceContext> fTexContext; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 151 | |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 152 | SkScalar fNormalizedYHeight; |
| 153 | |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 154 | // Array of AtlasRows which store the state of all our rows. Stored in a contiguous array, in |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 155 | // order that they appear in our texture, this means we can subtract this pointer from a row |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 156 | // pointer to get its index in the texture, and can save storing a row number in AtlasRow. |
| 157 | AtlasRow* fRows; |
| 158 | |
| 159 | // Head and tail for linked list of least-recently-used rows (front = least recently used). |
| 160 | // Note that when a texture is locked, it gets removed from this list until it is unlocked. |
| 161 | AtlasRow* fLRUFront; |
| 162 | AtlasRow* fLRUBack; |
| 163 | |
| 164 | // A list of pointers to AtlasRows that currently contain cached images, sorted by key |
| 165 | SkTDArray<AtlasRow*> fKeyTable; |
| 166 | }; |
| 167 | |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 168 | class GrTextureStripAtlasManager { |
| 169 | public: |
| 170 | GrTextureStripAtlasManager() {} |
| 171 | ~GrTextureStripAtlasManager(); |
| 172 | |
| 173 | void abandon(); |
| 174 | |
| 175 | /** |
| 176 | * Try to find an atlas with the required parameters, creates a new one if necessary |
| 177 | */ |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 178 | sk_sp<GrTextureStripAtlas> refAtlas(const GrTextureStripAtlas::Desc&); |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 179 | |
| 180 | private: |
| 181 | void deleteAllAtlases(); |
| 182 | |
| 183 | // Hash table entry for atlases |
| 184 | class AtlasEntry : public ::SkNoncopyable { |
| 185 | public: |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 186 | AtlasEntry(const GrTextureStripAtlas::Desc& desc, sk_sp<GrTextureStripAtlas> atlas) |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 187 | : fDesc(desc) |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 188 | , fAtlas(std::move(atlas)) { |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 189 | } |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 190 | ~AtlasEntry() { } |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 191 | |
| 192 | // for SkTDynamicHash |
| 193 | static const GrTextureStripAtlas::Desc& GetKey(const AtlasEntry& entry) { |
| 194 | return entry.fDesc; |
| 195 | } |
| 196 | static uint32_t Hash(const GrTextureStripAtlas::Desc& desc) { |
| 197 | return SkOpts::hash(&desc, sizeof(GrTextureStripAtlas::Desc)); |
| 198 | } |
| 199 | |
| 200 | const GrTextureStripAtlas::Desc fDesc; |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 201 | sk_sp<GrTextureStripAtlas> fAtlas; |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | typedef SkTDynamicHash<AtlasEntry, GrTextureStripAtlas::Desc> AtlasHash; |
| 205 | |
| 206 | AtlasHash fAtlasCache; |
| 207 | }; |
| 208 | |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 209 | #endif |