michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrTextureStripAtlas_DEFINED michael@0: #define GrTextureStripAtlas_DEFINED michael@0: michael@0: #include "GrBinHashKey.h" michael@0: #include "GrTHashTable.h" michael@0: #include "SkBitmap.h" michael@0: #include "SkGr.h" michael@0: #include "SkTDArray.h" michael@0: #include "SkTypes.h" michael@0: michael@0: /** michael@0: * Maintains a single large texture whose rows store many textures of a small fixed height, michael@0: * stored in rows across the x-axis such that we can safely wrap/repeat them horizontally. michael@0: */ michael@0: class GrTextureStripAtlas { michael@0: public: michael@0: /** michael@0: * Descriptor struct which we'll use as a hash table key michael@0: **/ michael@0: struct Desc { michael@0: Desc() { memset(this, 0, sizeof(*this)); } michael@0: uint16_t fWidth, fHeight, fRowHeight; michael@0: GrPixelConfig fConfig; michael@0: GrContext* fContext; michael@0: const uint32_t* asKey() const { return reinterpret_cast(this); } michael@0: }; michael@0: michael@0: /** michael@0: * Try to find an atlas with the required parameters, creates a new one if necessary michael@0: */ michael@0: static GrTextureStripAtlas* GetAtlas(const Desc& desc); michael@0: michael@0: ~GrTextureStripAtlas(); michael@0: michael@0: /** michael@0: * Add a texture to the atlas michael@0: * @param data Bitmap data to copy into the row michael@0: * @return The row index we inserted into, or -1 if we failed to find an open row. The caller michael@0: * is responsible for calling unlockRow() with this row index when it's done with it. michael@0: */ michael@0: int lockRow(const SkBitmap& data); michael@0: void unlockRow(int row); michael@0: michael@0: /** michael@0: * These functions help turn an integer row index in [0, 1, 2, ... numRows] into a scalar y michael@0: * texture coordinate in [0, 1] that we can use in a shader. michael@0: * michael@0: * If a regular texture access without using the atlas looks like: michael@0: * michael@0: * texture2D(sampler, vec2(x, y)) michael@0: * michael@0: * Then when using the atlas we'd replace it with: michael@0: * michael@0: * texture2D(sampler, vec2(x, yOffset + y * scaleFactor)) michael@0: * michael@0: * Where yOffset, returned by getYOffset(), is the offset to the start of the row within the michael@0: * atlas and scaleFactor, returned by getVerticalScaleFactor(), is the y-scale of the row, michael@0: * relative to the height of the overall atlas texture. michael@0: */ michael@0: SkScalar getYOffset(int row) const { return SkIntToScalar(row) / fNumRows; } michael@0: SkScalar getVerticalScaleFactor() const { return SkIntToScalar(fDesc.fRowHeight) / fDesc.fHeight; } michael@0: michael@0: GrContext* getContext() const { return fDesc.fContext; } michael@0: GrTexture* getTexture() const { return fTexture; } michael@0: michael@0: private: michael@0: michael@0: // Key to indicate an atlas row without any meaningful data stored in it michael@0: const static uint32_t kEmptyAtlasRowKey = 0xffffffff; michael@0: michael@0: /** michael@0: * The state of a single row in our cache, next/prev pointers allow these to be chained michael@0: * together to represent LRU status michael@0: */ michael@0: struct AtlasRow : public SkNoncopyable { michael@0: AtlasRow() : fKey(kEmptyAtlasRowKey), fLocks(0), fNext(NULL), fPrev(NULL) { } michael@0: // GenerationID of the bitmap that is represented by this row, 0xffffffff means "empty" michael@0: uint32_t fKey; michael@0: // How many times this has been locked (0 == unlocked) michael@0: int32_t fLocks; michael@0: // We maintain an LRU linked list between unlocked nodes with these pointers michael@0: AtlasRow* fNext; michael@0: AtlasRow* fPrev; michael@0: }; michael@0: michael@0: /** michael@0: * We'll only allow construction via the static GrTextureStripAtlas::GetAtlas michael@0: */ michael@0: GrTextureStripAtlas(Desc desc); michael@0: michael@0: void lockTexture(); michael@0: void unlockTexture(); michael@0: michael@0: /** michael@0: * Initialize our LRU list (if one already exists, clear it and start anew) michael@0: */ michael@0: void initLRU(); michael@0: michael@0: /** michael@0: * Grabs the least recently used free row out of the LRU list, returns NULL if no rows are free. michael@0: */ michael@0: AtlasRow* getLRU(); michael@0: michael@0: void appendLRU(AtlasRow* row); michael@0: void removeFromLRU(AtlasRow* row); michael@0: michael@0: /** michael@0: * Searches the key table for a key and returns the index if found; if not found, it returns michael@0: * the bitwise not of the index at which we could insert the key to maintain a sorted list. michael@0: **/ michael@0: int searchByKey(uint32_t key); michael@0: michael@0: /** michael@0: * Compare two atlas rows by key, so we can sort/search by key michael@0: */ michael@0: static bool KeyLess(const AtlasRow& lhs, const AtlasRow& rhs) { michael@0: return lhs.fKey < rhs.fKey; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void validate(); michael@0: #endif michael@0: michael@0: /** michael@0: * Clean up callback registered with GrContext. Allows this class to michael@0: * free up any allocated AtlasEntry and GrTextureStripAtlas objects michael@0: */ michael@0: static void CleanUp(const GrContext* context, void* info); michael@0: michael@0: // Hash table entry for atlases michael@0: class AtlasEntry; michael@0: class AtlasHashKey : public GrBinHashKey { michael@0: public: michael@0: static bool Equals(const AtlasEntry& entry, const AtlasHashKey& key); michael@0: static bool LessThan(const AtlasEntry& entry, const AtlasHashKey& key); michael@0: }; michael@0: class AtlasEntry : public ::SkNoncopyable { michael@0: public: michael@0: AtlasEntry() : fAtlas(NULL) {} michael@0: ~AtlasEntry() { SkDELETE(fAtlas); } michael@0: AtlasHashKey fKey; michael@0: GrTextureStripAtlas* fAtlas; michael@0: }; michael@0: michael@0: static GrTHashTable* gAtlasCache; michael@0: michael@0: static GrTHashTable* GetCache(); michael@0: michael@0: // We increment gCacheCount for each atlas michael@0: static int32_t gCacheCount; michael@0: michael@0: // A unique ID for this texture (formed with: gCacheCount++), so we can be sure that if we michael@0: // get a texture back from the texture cache, that it's the same one we last used. michael@0: const int32_t fCacheKey; michael@0: michael@0: // Total locks on all rows (when this reaches zero, we can unlock our texture) michael@0: int32_t fLockedRows; michael@0: michael@0: const Desc fDesc; michael@0: const uint16_t fNumRows; michael@0: GrTexture* fTexture; michael@0: michael@0: // Array of AtlasRows which store the state of all our rows. Stored in a contiguous array, in michael@0: // order that they appear in our texture, this means we can subtract this pointer from a row michael@0: // pointer to get its index in the texture, and can save storing a row number in AtlasRow. michael@0: AtlasRow* fRows; michael@0: michael@0: // Head and tail for linked list of least-recently-used rows (front = least recently used). michael@0: // Note that when a texture is locked, it gets removed from this list until it is unlocked. michael@0: AtlasRow* fLRUFront; michael@0: AtlasRow* fLRUBack; michael@0: michael@0: // A list of pointers to AtlasRows that currently contain cached images, sorted by key michael@0: SkTDArray fKeyTable; michael@0: }; michael@0: michael@0: inline bool GrTextureStripAtlas::AtlasHashKey::Equals(const AtlasEntry& entry, michael@0: const AtlasHashKey& key) { michael@0: return entry.fKey == key; michael@0: } michael@0: michael@0: inline bool GrTextureStripAtlas::AtlasHashKey::LessThan(const AtlasEntry& entry, michael@0: const AtlasHashKey& key) { michael@0: return entry.fKey < key; michael@0: } michael@0: michael@0: #endif