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