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-11-28 17:35:51 -08:00
|
|
|
#include <utils/LruCache.h>
|
2012-03-05 16:48:32 -05:00
|
|
|
#include <utils/Mutex.h>
|
2010-11-11 15:36:56 -08:00
|
|
|
|
2010-11-10 11:59:15 -08:00
|
|
|
#include "Debug.h"
|
2010-06-29 21:05:21 -07:00
|
|
|
|
2015-07-29 16:48:58 -07:00
|
|
|
#include <vector>
|
2015-11-10 12:19:17 -08:00
|
|
|
#include <unordered_map>
|
2015-07-29 16:48:58 -07:00
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2014-10-15 15:46:42 -04:00
|
|
|
class Texture;
|
|
|
|
|
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
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-12-10 16:47:36 -08:00
|
|
|
class AssetAtlas;
|
|
|
|
|
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.
|
|
|
|
*/
|
2015-02-05 10:12:38 -08:00
|
|
|
class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
|
2010-06-29 21:05:21 -07:00
|
|
|
public:
|
2010-08-23 21:05:08 -07:00
|
|
|
TextureCache();
|
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.
|
|
|
|
*/
|
2014-12-22 14:28:49 -08:00
|
|
|
void operator()(uint32_t&, Texture*& texture) override;
|
2010-06-29 21:05:21 -07:00
|
|
|
|
2014-04-11 19:15:05 -07:00
|
|
|
/**
|
|
|
|
* Resets all Textures to not be marked as in use
|
|
|
|
*/
|
2015-07-21 10:23:59 -07:00
|
|
|
void resetMarkInUse(void* ownerToken);
|
2014-04-11 19:15:05 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to precache the SkBitmap. Returns true if a Texture was successfully
|
|
|
|
* acquired for the bitmap, false otherwise. If a Texture was acquired it is
|
|
|
|
* marked as in use.
|
|
|
|
*/
|
2015-07-21 10:23:59 -07:00
|
|
|
bool prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap);
|
2014-04-11 19:15:05 -07:00
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
/**
|
2015-07-17 15:51:36 -07:00
|
|
|
* Returns the texture associated with the specified bitmap from either within the cache, or
|
|
|
|
* the AssetAtlas. If the texture cannot be found in the cache, a new texture is generated.
|
2010-07-01 18:26:52 -07:00
|
|
|
*/
|
2015-07-17 15:51:36 -07:00
|
|
|
Texture* get(const SkBitmap* bitmap) {
|
|
|
|
return get(bitmap, AtlasUsageType::Use);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the texture associated with the specified bitmap. If the texture cannot be found in
|
|
|
|
* the cache, a new texture is generated, even if it resides in the AssetAtlas.
|
|
|
|
*/
|
|
|
|
Texture* getAndBypassAtlas(const SkBitmap* bitmap) {
|
|
|
|
return get(bitmap, AtlasUsageType::Bypass);
|
|
|
|
}
|
2014-11-24 15:21:28 -08:00
|
|
|
|
2010-11-11 15:36:56 -08:00
|
|
|
/**
|
2014-12-04 15:20:29 -05:00
|
|
|
* Removes the texture associated with the specified pixelRef. This is meant
|
2010-11-11 15:36:56 -08:00
|
|
|
* to be called from threads that are not the EGL context thread.
|
|
|
|
*/
|
2014-12-04 15:20:29 -05:00
|
|
|
ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
|
2010-11-11 15:36:56 -08:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2014-12-10 16:47:36 -08:00
|
|
|
void setAssetAtlas(AssetAtlas* assetAtlas);
|
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
private:
|
2015-07-17 15:51:36 -07:00
|
|
|
enum class AtlasUsageType {
|
|
|
|
Use,
|
|
|
|
Bypass,
|
|
|
|
};
|
2014-04-11 19:15:05 -07:00
|
|
|
|
|
|
|
bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
|
|
|
|
|
2015-07-17 15:51:36 -07:00
|
|
|
Texture* get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
|
|
|
|
Texture* getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
|
2014-04-11 19:15:05 -07:00
|
|
|
|
2014-11-24 15:21:28 -08:00
|
|
|
LruCache<uint32_t, 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;
|
|
|
|
|
2015-07-29 16:48:58 -07:00
|
|
|
std::vector<uint32_t> mGarbage;
|
2010-09-08 15:15:43 -07:00
|
|
|
mutable Mutex mLock;
|
2014-12-10 16:47:36 -08:00
|
|
|
|
|
|
|
AssetAtlas* mAssetAtlas;
|
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
|