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