blob: 5b90a342d7f90af5e8dcd39b28eb68ba4bdbc2ee [file] [log] [blame]
rileya@google.com2e2aedc2012-08-13 20:28:48 +00001/*
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.orga0b40282013-09-18 13:00:55 +000011#include "SkBitmap.h"
mtklein4e976072016-08-08 09:06:27 -070012#include "SkOpts.h"
rileya@google.com2e2aedc2012-08-13 20:28:48 +000013#include "SkGr.h"
14#include "SkTDArray.h"
robertphillips3d533ac2014-07-20 09:40:00 -070015#include "SkTDynamicHash.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000016#include "SkTypes.h"
rileya@google.com2e2aedc2012-08-13 20:28:48 +000017
18/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000019 * Maintains a single large texture whose rows store many textures of a small fixed height,
rileya@google.com2e2aedc2012-08-13 20:28:48 +000020 * stored in rows across the x-axis such that we can safely wrap/repeat them horizontally.
21 */
22class GrTextureStripAtlas {
23public:
rileya@google.com2e2aedc2012-08-13 20:28:48 +000024 /**
25 * Descriptor struct which we'll use as a hash table key
26 **/
27 struct Desc {
joshualitt690fc752015-07-13 12:49:13 -070028 Desc() { sk_bzero(this, sizeof(*this)); }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000029 GrContext* fContext;
joshualitt690fc752015-07-13 12:49:13 -070030 GrPixelConfig fConfig;
31 uint16_t fWidth, fHeight, fRowHeight;
32 uint16_t fUnusedPadding;
33 bool operator==(const Desc& other) const {
34 return 0 == memcmp(this, &other, sizeof(Desc));
35 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000036 };
37
38 /**
39 * Try to find an atlas with the required parameters, creates a new one if necessary
40 */
41 static GrTextureStripAtlas* GetAtlas(const Desc& desc);
42
43 ~GrTextureStripAtlas();
44
45 /**
46 * Add a texture to the atlas
47 * @param data Bitmap data to copy into the row
48 * @return The row index we inserted into, or -1 if we failed to find an open row. The caller
49 * is responsible for calling unlockRow() with this row index when it's done with it.
50 */
51 int lockRow(const SkBitmap& data);
52 void unlockRow(int row);
53
rmistry@google.comfbfcd562012-08-23 18:09:54 +000054 /**
55 * These functions help turn an integer row index in [0, 1, 2, ... numRows] into a scalar y
rileya@google.com2e2aedc2012-08-13 20:28:48 +000056 * texture coordinate in [0, 1] that we can use in a shader.
57 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +000058 * If a regular texture access without using the atlas looks like:
rileya@google.com2e2aedc2012-08-13 20:28:48 +000059 *
60 * texture2D(sampler, vec2(x, y))
61 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +000062 * Then when using the atlas we'd replace it with:
rileya@google.com2e2aedc2012-08-13 20:28:48 +000063 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064 * texture2D(sampler, vec2(x, yOffset + y * scaleFactor))
rileya@google.com2e2aedc2012-08-13 20:28:48 +000065 *
66 * Where yOffset, returned by getYOffset(), is the offset to the start of the row within the
bsalomonc6327a82014-10-27 12:53:08 -070067 * atlas and scaleFactor, returned by getNormalizedTexelHeight, is the normalized height of
68 * one texel row.
rileya@google.com2e2aedc2012-08-13 20:28:48 +000069 */
bsalomon@google.com81712882012-11-01 17:12:34 +000070 SkScalar getYOffset(int row) const { return SkIntToScalar(row) / fNumRows; }
bsalomonc6327a82014-10-27 12:53:08 -070071 SkScalar getNormalizedTexelHeight() const { return fNormalizedYHeight; }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000072
73 GrContext* getContext() const { return fDesc.fContext; }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000074 GrTexture* getTexture() const { return fTexture; }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000075
76private:
77
78 // Key to indicate an atlas row without any meaningful data stored in it
79 const static uint32_t kEmptyAtlasRowKey = 0xffffffff;
80
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081 /**
rileya@google.com2e2aedc2012-08-13 20:28:48 +000082 * The state of a single row in our cache, next/prev pointers allow these to be chained
83 * together to represent LRU status
84 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000085 struct AtlasRow : SkNoncopyable {
halcanary96fcdcc2015-08-27 07:41:13 -070086 AtlasRow() : fKey(kEmptyAtlasRowKey), fLocks(0), fNext(nullptr), fPrev(nullptr) { }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000087 // GenerationID of the bitmap that is represented by this row, 0xffffffff means "empty"
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088 uint32_t fKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000089 // How many times this has been locked (0 == unlocked)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090 int32_t fLocks;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000091 // We maintain an LRU linked list between unlocked nodes with these pointers
92 AtlasRow* fNext;
93 AtlasRow* fPrev;
94 };
95
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096 /**
rileya@google.com2e2aedc2012-08-13 20:28:48 +000097 * We'll only allow construction via the static GrTextureStripAtlas::GetAtlas
98 */
99 GrTextureStripAtlas(Desc desc);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000100
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000101 void lockTexture();
102 void unlockTexture();
103
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000104 /**
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000105 * Initialize our LRU list (if one already exists, clear it and start anew)
106 */
107 void initLRU();
108
109 /**
halcanary96fcdcc2015-08-27 07:41:13 -0700110 * Grabs the least recently used free row out of the LRU list, returns nullptr if no rows are free.
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000111 */
112 AtlasRow* getLRU();
113
114 void appendLRU(AtlasRow* row);
115 void removeFromLRU(AtlasRow* row);
116
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000117 /**
118 * Searches the key table for a key and returns the index if found; if not found, it returns
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000119 * the bitwise not of the index at which we could insert the key to maintain a sorted list.
120 **/
121 int searchByKey(uint32_t key);
122
123 /**
124 * Compare two atlas rows by key, so we can sort/search by key
125 */
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000126 static bool KeyLess(const AtlasRow& lhs, const AtlasRow& rhs) {
127 return lhs.fKey < rhs.fKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000128 }
129
130#ifdef SK_DEBUG
131 void validate();
132#endif
133
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000134 /**
135 * Clean up callback registered with GrContext. Allows this class to
136 * free up any allocated AtlasEntry and GrTextureStripAtlas objects
137 */
138 static void CleanUp(const GrContext* context, void* info);
139
140 // Hash table entry for atlases
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000141 class AtlasEntry : public ::SkNoncopyable {
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000142 public:
robertphillips3d533ac2014-07-20 09:40:00 -0700143 // for SkTDynamicHash
joshualitt690fc752015-07-13 12:49:13 -0700144 static const Desc& GetKey(const AtlasEntry& entry) { return entry.fDesc; }
mtklein4e976072016-08-08 09:06:27 -0700145 static uint32_t Hash(const Desc& desc) { return SkOpts::hash(&desc, sizeof(Desc)); }
robertphillips3d533ac2014-07-20 09:40:00 -0700146
147 // AtlasEntry proper
halcanary96fcdcc2015-08-27 07:41:13 -0700148 AtlasEntry() : fAtlas(nullptr) {}
halcanary385fe4d2015-08-26 13:07:48 -0700149 ~AtlasEntry() { delete fAtlas; }
joshualitt690fc752015-07-13 12:49:13 -0700150 Desc fDesc;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000151 GrTextureStripAtlas* fAtlas;
152 };
153
robertphillips3d533ac2014-07-20 09:40:00 -0700154 class Hash;
155 static Hash* gAtlasCache;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000156
robertphillips3d533ac2014-07-20 09:40:00 -0700157 static Hash* GetCache();
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000158
rileya@google.comf61c7462012-08-13 21:03:39 +0000159 // We increment gCacheCount for each atlas
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000160 static int32_t gCacheCount;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000161
rileya@google.comf61c7462012-08-13 21:03:39 +0000162 // A unique ID for this texture (formed with: gCacheCount++), so we can be sure that if we
163 // get a texture back from the texture cache, that it's the same one we last used.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000164 const int32_t fCacheKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000165
166 // Total locks on all rows (when this reaches zero, we can unlock our texture)
167 int32_t fLockedRows;
168
169 const Desc fDesc;
170 const uint16_t fNumRows;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000171 GrTexture* fTexture;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000172
bsalomonc6327a82014-10-27 12:53:08 -0700173 SkScalar fNormalizedYHeight;
174
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000175 // Array of AtlasRows which store the state of all our rows. Stored in a contiguous array, in
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000176 // order that they appear in our texture, this means we can subtract this pointer from a row
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000177 // pointer to get its index in the texture, and can save storing a row number in AtlasRow.
178 AtlasRow* fRows;
179
180 // Head and tail for linked list of least-recently-used rows (front = least recently used).
181 // Note that when a texture is locked, it gets removed from this list until it is unlocked.
182 AtlasRow* fLRUFront;
183 AtlasRow* fLRUBack;
184
185 // A list of pointers to AtlasRows that currently contain cached images, sorted by key
186 SkTDArray<AtlasRow*> fKeyTable;
187};
188
189#endif