2010-06-29 21:05:21 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
#ifndef ANDROID_HWUI_TEXTURE_CACHE_H
|
|
|
|
#define ANDROID_HWUI_TEXTURE_CACHE_H
|
2010-06-29 21:05:21 -07:00
|
|
|
|
|
|
|
#include <SkBitmap.h>
|
|
|
|
|
2012-03-05 16:48:32 -05:00
|
|
|
#include <utils/Mutex.h>
|
2010-11-11 15:36:56 -08:00
|
|
|
#include <utils/Vector.h>
|
|
|
|
|
2010-11-10 11:59:15 -08:00
|
|
|
#include "Debug.h"
|
2010-06-29 21:05:21 -07:00
|
|
|
#include "Texture.h"
|
2010-10-08 18:43:58 -07:00
|
|
|
#include "utils/GenerationCache.h"
|
2010-06-29 21:05:21 -07:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2010-10-25 15:47:32 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Defines
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
#if DEBUG_TEXTURES
|
2011-12-20 16:23:08 +00:00
|
|
|
#define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
|
2010-10-25 15:47:32 -07:00
|
|
|
#else
|
|
|
|
#define TEXTURE_LOGD(...)
|
|
|
|
#endif
|
|
|
|
|
2010-11-09 14:35:20 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Classes
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
|
|
|
* A simple LRU texture cache. The cache has a maximum size expressed in bytes.
|
|
|
|
* Any texture added to the cache causing the cache to grow beyond the maximum
|
|
|
|
* allowed size will also cause the oldest texture to be kicked out.
|
|
|
|
*/
|
2010-06-29 21:05:21 -07:00
|
|
|
class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> {
|
|
|
|
public:
|
2010-08-23 21:05:08 -07:00
|
|
|
TextureCache();
|
2010-07-02 11:20:34 -07:00
|
|
|
TextureCache(uint32_t maxByteSize);
|
2010-06-29 21:05:21 -07:00
|
|
|
~TextureCache();
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
|
|
|
* Used as a callback when an entry is removed from the cache.
|
|
|
|
* Do not invoke directly.
|
|
|
|
*/
|
2010-07-06 11:39:32 -07:00
|
|
|
void operator()(SkBitmap*& bitmap, Texture*& texture);
|
2010-06-29 21:05:21 -07:00
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
|
|
|
* Returns the texture associated with the specified bitmap. If the texture
|
|
|
|
* cannot be found in the cache, a new texture is generated.
|
|
|
|
*/
|
2010-06-29 21:05:21 -07:00
|
|
|
Texture* get(SkBitmap* bitmap);
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
2010-11-11 15:36:56 -08:00
|
|
|
* Removes the texture associated with the specified bitmap.
|
|
|
|
* Upon remove the texture is freed.
|
2010-07-01 18:26:52 -07:00
|
|
|
*/
|
|
|
|
void remove(SkBitmap* bitmap);
|
2010-11-11 15:36:56 -08:00
|
|
|
/**
|
|
|
|
* Removes the texture associated with the specified bitmap. This is meant
|
|
|
|
* to be called from threads that are not the EGL context thread.
|
|
|
|
*/
|
|
|
|
void removeDeferred(SkBitmap* bitmap);
|
|
|
|
/**
|
|
|
|
* Process deferred removals.
|
|
|
|
*/
|
|
|
|
void clearGarbage();
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
|
|
|
* Clears the cache. This causes all textures to be deleted.
|
|
|
|
*/
|
2010-06-29 21:05:21 -07:00
|
|
|
void clear();
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
|
|
|
* Sets the maximum size of the cache in bytes.
|
|
|
|
*/
|
2010-07-02 11:20:34 -07:00
|
|
|
void setMaxSize(uint32_t maxSize);
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
|
|
|
* Returns the maximum size of the cache in bytes.
|
|
|
|
*/
|
2010-07-02 11:20:34 -07:00
|
|
|
uint32_t getMaxSize();
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
|
|
|
* Returns the current size of the cache in bytes.
|
|
|
|
*/
|
2010-07-02 11:20:34 -07:00
|
|
|
uint32_t getSize();
|
2010-07-01 18:26:52 -07:00
|
|
|
|
2011-11-04 15:12:29 -07:00
|
|
|
/**
|
|
|
|
* Partially flushes the cache. The amount of memory freed by a flush
|
|
|
|
* is defined by the flush rate.
|
|
|
|
*/
|
|
|
|
void flush();
|
|
|
|
/**
|
|
|
|
* Indicates the percentage of the cache to retain when a
|
|
|
|
* memory trim is requested (see Caches::flush).
|
|
|
|
*/
|
|
|
|
void setFlushRate(float flushRate);
|
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
private:
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
|
|
|
* Generates the texture from a bitmap into the specified texture structure.
|
|
|
|
*
|
|
|
|
* @param regenerate If true, the bitmap data is reuploaded into the texture, but
|
|
|
|
* no new texture is generated.
|
|
|
|
*/
|
2010-06-30 16:05:32 -07:00
|
|
|
void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
|
2010-06-29 21:05:21 -07:00
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height);
|
2010-09-22 14:13:32 -07:00
|
|
|
void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
|
|
|
|
GLenum type, const GLvoid * data);
|
|
|
|
|
2010-08-23 21:05:08 -07:00
|
|
|
void init();
|
|
|
|
|
2010-07-07 15:15:32 -07:00
|
|
|
GenerationCache<SkBitmap*, Texture*> mCache;
|
2010-07-01 18:26:52 -07:00
|
|
|
|
2010-07-02 11:20:34 -07:00
|
|
|
uint32_t mSize;
|
|
|
|
uint32_t mMaxSize;
|
2010-08-08 00:14:31 -07:00
|
|
|
GLint mMaxTextureSize;
|
2010-09-08 15:15:43 -07:00
|
|
|
|
2011-11-04 15:12:29 -07:00
|
|
|
float mFlushRate;
|
|
|
|
|
2010-11-10 19:01:29 -08:00
|
|
|
bool mDebugEnabled;
|
|
|
|
|
2010-11-11 15:36:56 -08:00
|
|
|
Vector<SkBitmap*> mGarbage;
|
2010-09-08 15:15:43 -07:00
|
|
|
mutable Mutex mLock;
|
2010-06-29 21:05:21 -07:00
|
|
|
}; // class TextureCache
|
|
|
|
|
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
#endif // ANDROID_HWUI_TEXTURE_CACHE_H
|