TextureCacheCompositingThread.h [plain text]
#ifndef TextureCacheCompositingThread_h
#define TextureCacheCompositingThread_h
#if USE(ACCELERATED_COMPOSITING)
#include "Color.h"
#include "LayerTileIndex.h"
#include "Texture.h"
#include <wtf/HashMap.h>
#include <wtf/ListHashSet.h>
#include <wtf/Noncopyable.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
namespace WebCore {
class IntRect;
class TextureCacheCompositingThread {
WTF_MAKE_NONCOPYABLE(TextureCacheCompositingThread);
WTF_MAKE_FAST_ALLOCATED;
public:
friend TextureCacheCompositingThread* textureCacheCompositingThread();
PassRefPtr<Texture> createTexture()
{
return Texture::create();
}
PassRefPtr<Texture> textureForTiledContents(const SkBitmap& contents, const IntRect& tileRect, const TileIndex&, bool isOpaque);
PassRefPtr<Texture> textureForColor(const Color&);
PassRefPtr<Texture> updateContents(const RefPtr<Texture>&, const SkBitmap& contents, const IntRect& dirtyRect, const IntRect& tileRect, bool isOpaque);
size_t memoryUsage() const { return m_memoryUsage; }
size_t memoryLimit() const { return m_memoryLimit; }
void setMemoryLimit(size_t limit) { m_memoryLimit = limit; }
void prune(size_t limit);
void prune() { prune(memoryLimit()); }
void clear();
void textureAccessed(Texture*);
void collectGarbage();
void textureDestroyed(Texture*);
void textureResized(Texture*, const IntSize& oldSize);
bool install(Texture*);
private:
struct ZombieTexture {
explicit ZombieTexture(Texture* texture)
: id(texture->textureId())
, size(texture->size())
{
}
unsigned id;
IntSize size;
};
typedef ListHashSet<Texture* > TextureSet;
typedef std::pair<uint32_t, size_t> ContentsKey;
typedef HashMap<TileIndex, RefPtr<Texture> > TextureMap;
typedef Vector<ZombieTexture> Garbage;
TextureCacheCompositingThread();
~TextureCacheCompositingThread();
unsigned allocateTextureId();
void freeTextureId(unsigned id);
void incMemoryUsage(int delta) { setMemoryUsage(memoryUsage() + delta); }
void decMemoryUsage(int delta) { setMemoryUsage(memoryUsage() - delta); }
void setMemoryUsage(size_t);
ContentsKey key(const SkBitmap& contents);
void evict(const TextureSet::iterator&);
TextureSet m_textures;
size_t m_memoryUsage;
size_t m_memoryLimit;
HashMap<ContentsKey, TextureMap> m_cache;
struct ColorHash {
static unsigned hash(const Color& key) { return WTF::intHash(key.rgb()); }
static bool equal(const Color& a, const Color& b) { return a == b; }
static const bool safeToCompareToEmptyOrDeleted = true;
};
struct ColorHashTraits : WTF::GenericHashTraits<Color> {
static const bool emptyValueIsZero = true;
static void constructDeletedValue(Color& slot) { new (&slot) Color(); *reinterpret_cast<RGBA32*>(&slot) = Color::white; }
static bool isDeletedValue(const Color& value) { return !value.isValid() && value.rgb() == Color::white; }
};
typedef HashMap<Color, RefPtr<Texture>, ColorHash, ColorHashTraits> ColorTextureMap;
ColorTextureMap m_colors;
Garbage m_garbage;
};
TextureCacheCompositingThread* textureCacheCompositingThread();
}
#endif // USE(ACCELERATED_COMPOSITING)
#endif // TextureCacheCompositingThread_h