blob: 6d2fb8fdf8daef78da47b5560a0901d7fdde7cd0 [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 "GrBinHashKey.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000012#include "SkBitmap.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 {
28 Desc() { memset(this, 0, sizeof(*this)); }
29 uint16_t fWidth, fHeight, fRowHeight;
30 GrPixelConfig fConfig;
31 GrContext* fContext;
32 const uint32_t* asKey() const { return reinterpret_cast<const uint32_t*>(this); }
33 };
34
35 /**
36 * Try to find an atlas with the required parameters, creates a new one if necessary
37 */
38 static GrTextureStripAtlas* GetAtlas(const Desc& desc);
39
40 ~GrTextureStripAtlas();
41
42 /**
43 * Add a texture to the atlas
44 * @param data Bitmap data to copy into the row
45 * @return The row index we inserted into, or -1 if we failed to find an open row. The caller
46 * is responsible for calling unlockRow() with this row index when it's done with it.
47 */
48 int lockRow(const SkBitmap& data);
49 void unlockRow(int row);
50
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051 /**
52 * 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 +000053 * texture coordinate in [0, 1] that we can use in a shader.
54 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +000055 * If a regular texture access without using the atlas looks like:
rileya@google.com2e2aedc2012-08-13 20:28:48 +000056 *
57 * texture2D(sampler, vec2(x, y))
58 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +000059 * Then when using the atlas we'd replace it with:
rileya@google.com2e2aedc2012-08-13 20:28:48 +000060 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +000061 * texture2D(sampler, vec2(x, yOffset + y * scaleFactor))
rileya@google.com2e2aedc2012-08-13 20:28:48 +000062 *
63 * Where yOffset, returned by getYOffset(), is the offset to the start of the row within the
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064 * atlas and scaleFactor, returned by getVerticalScaleFactor(), is the y-scale of the row,
rileya@google.com2e2aedc2012-08-13 20:28:48 +000065 * relative to the height of the overall atlas texture.
66 */
bsalomon@google.com81712882012-11-01 17:12:34 +000067 SkScalar getYOffset(int row) const { return SkIntToScalar(row) / fNumRows; }
68 SkScalar getVerticalScaleFactor() const { return SkIntToScalar(fDesc.fRowHeight) / fDesc.fHeight; }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000069
70 GrContext* getContext() const { return fDesc.fContext; }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000071 GrTexture* getTexture() const { return fTexture; }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000072
73private:
74
75 // Key to indicate an atlas row without any meaningful data stored in it
76 const static uint32_t kEmptyAtlasRowKey = 0xffffffff;
77
rmistry@google.comfbfcd562012-08-23 18:09:54 +000078 /**
rileya@google.com2e2aedc2012-08-13 20:28:48 +000079 * The state of a single row in our cache, next/prev pointers allow these to be chained
80 * together to represent LRU status
81 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000082 struct AtlasRow : SkNoncopyable {
rileya@google.com2e2aedc2012-08-13 20:28:48 +000083 AtlasRow() : fKey(kEmptyAtlasRowKey), fLocks(0), fNext(NULL), fPrev(NULL) { }
84 // GenerationID of the bitmap that is represented by this row, 0xffffffff means "empty"
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085 uint32_t fKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000086 // How many times this has been locked (0 == unlocked)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000087 int32_t fLocks;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000088 // We maintain an LRU linked list between unlocked nodes with these pointers
89 AtlasRow* fNext;
90 AtlasRow* fPrev;
91 };
92
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093 /**
rileya@google.com2e2aedc2012-08-13 20:28:48 +000094 * We'll only allow construction via the static GrTextureStripAtlas::GetAtlas
95 */
96 GrTextureStripAtlas(Desc desc);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097
rileya@google.com2e2aedc2012-08-13 20:28:48 +000098 void lockTexture();
99 void unlockTexture();
100
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000101 /**
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000102 * Initialize our LRU list (if one already exists, clear it and start anew)
103 */
104 void initLRU();
105
106 /**
107 * Grabs the least recently used free row out of the LRU list, returns NULL if no rows are free.
108 */
109 AtlasRow* getLRU();
110
111 void appendLRU(AtlasRow* row);
112 void removeFromLRU(AtlasRow* row);
113
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000114 /**
115 * 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 +0000116 * the bitwise not of the index at which we could insert the key to maintain a sorted list.
117 **/
118 int searchByKey(uint32_t key);
119
120 /**
121 * Compare two atlas rows by key, so we can sort/search by key
122 */
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000123 static bool KeyLess(const AtlasRow& lhs, const AtlasRow& rhs) {
124 return lhs.fKey < rhs.fKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000125 }
126
127#ifdef SK_DEBUG
128 void validate();
129#endif
130
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000131 /**
132 * Clean up callback registered with GrContext. Allows this class to
133 * free up any allocated AtlasEntry and GrTextureStripAtlas objects
134 */
135 static void CleanUp(const GrContext* context, void* info);
136
137 // Hash table entry for atlases
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000138 class AtlasEntry : public ::SkNoncopyable {
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000139 public:
robertphillips3d533ac2014-07-20 09:40:00 -0700140 // for SkTDynamicHash
141 class Key : public GrMurmur3HashKey<sizeof(GrTextureStripAtlas::Desc)> {};
142 static const Key& GetKey(const AtlasEntry& entry) { return entry.fKey; }
143 static uint32_t Hash(const Key& key) { return key.getHash(); }
144
145 // AtlasEntry proper
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000146 AtlasEntry() : fAtlas(NULL) {}
147 ~AtlasEntry() { SkDELETE(fAtlas); }
robertphillips3d533ac2014-07-20 09:40:00 -0700148 Key fKey;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000149 GrTextureStripAtlas* fAtlas;
150 };
151
robertphillips3d533ac2014-07-20 09:40:00 -0700152 class Hash;
153 static Hash* gAtlasCache;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000154
robertphillips3d533ac2014-07-20 09:40:00 -0700155 static Hash* GetCache();
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000156
rileya@google.comf61c7462012-08-13 21:03:39 +0000157 // We increment gCacheCount for each atlas
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000158 static int32_t gCacheCount;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000159
rileya@google.comf61c7462012-08-13 21:03:39 +0000160 // A unique ID for this texture (formed with: gCacheCount++), so we can be sure that if we
161 // 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 +0000162 const int32_t fCacheKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000163
164 // Total locks on all rows (when this reaches zero, we can unlock our texture)
165 int32_t fLockedRows;
166
167 const Desc fDesc;
168 const uint16_t fNumRows;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000169 GrTexture* fTexture;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000170
171 // 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 +0000172 // 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 +0000173 // pointer to get its index in the texture, and can save storing a row number in AtlasRow.
174 AtlasRow* fRows;
175
176 // Head and tail for linked list of least-recently-used rows (front = least recently used).
177 // Note that when a texture is locked, it gets removed from this list until it is unlocked.
178 AtlasRow* fLRUFront;
179 AtlasRow* fLRUBack;
180
181 // A list of pointers to AtlasRows that currently contain cached images, sorted by key
182 SkTDArray<AtlasRow*> fKeyTable;
183};
184
185#endif