2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <GLES2/gl2.h>
19 #include <utils/Mutex.h>
21 #include "AssetAtlas.h"
24 #include "TextureCache.h"
25 #include "Properties.h"
26 #include "utils/TraceUtils.h"
29 namespace uirenderer {
31 ///////////////////////////////////////////////////////////////////////////////
32 // Constructors/destructor
33 ///////////////////////////////////////////////////////////////////////////////
35 TextureCache::TextureCache()
36 : mCache(LruCache<uint32_t, Texture*>::kUnlimitedCapacity)
38 , mMaxSize(Properties::textureCacheSize)
39 , mFlushRate(Properties::textureCacheFlushRate)
40 , mAssetAtlas(nullptr) {
41 mCache.setOnEntryRemovedListener(this);
43 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
44 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
46 mDebugEnabled = Properties::debugLevel & kDebugCaches;
49 TextureCache::~TextureCache() {
53 ///////////////////////////////////////////////////////////////////////////////
55 ///////////////////////////////////////////////////////////////////////////////
57 uint32_t TextureCache::getSize() {
61 uint32_t TextureCache::getMaxSize() {
65 ///////////////////////////////////////////////////////////////////////////////
67 ///////////////////////////////////////////////////////////////////////////////
69 void TextureCache::operator()(uint32_t&, Texture*& texture) {
70 // This will be called already locked
72 mSize -= texture->bitmapSize;
73 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
74 texture->id, texture->bitmapSize, mSize);
76 ALOGD("Texture deleted, size = %d", texture->bitmapSize);
78 texture->deleteTexture();
83 ///////////////////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////////////////
87 void TextureCache::setAssetAtlas(AssetAtlas* assetAtlas) {
88 mAssetAtlas = assetAtlas;
91 void TextureCache::resetMarkInUse(void* ownerToken) {
92 LruCache<uint32_t, Texture*>::Iterator iter(mCache);
94 if (iter.value()->isInUse == ownerToken) {
95 iter.value()->isInUse = nullptr;
100 bool TextureCache::canMakeTextureFromBitmap(const SkBitmap* bitmap) {
101 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
102 ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)",
103 bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize);
109 // Returns a prepared Texture* that either is already in the cache or can fit
110 // in the cache (and is thus added to the cache)
111 Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) {
112 if (CC_LIKELY(mAssetAtlas != nullptr) && atlasUsageType == AtlasUsageType::Use) {
113 AssetAtlas::Entry* entry = mAssetAtlas->getEntry(bitmap->pixelRef());
114 if (CC_UNLIKELY(entry)) {
115 return entry->texture;
119 Texture* texture = mCache.get(bitmap->pixelRef()->getStableID());
122 if (!canMakeTextureFromBitmap(bitmap)) {
126 const uint32_t size = bitmap->rowBytes() * bitmap->height();
127 bool canCache = size < mMaxSize;
128 // Don't even try to cache a bitmap that's bigger than the cache
129 while (canCache && mSize + size > mMaxSize) {
130 Texture* oldest = mCache.peekOldestValue();
131 if (oldest && !oldest->isInUse) {
132 mCache.removeOldest();
139 texture = new Texture(Caches::getInstance());
140 texture->bitmapSize = size;
141 texture->generation = bitmap->getGenerationID();
142 texture->upload(*bitmap);
145 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
146 bitmap, texture->id, size, mSize);
148 ALOGD("Texture created, size = %d", size);
150 mCache.put(bitmap->pixelRef()->getStableID(), texture);
152 } else if (!texture->isInUse && bitmap->getGenerationID() != texture->generation) {
153 // Texture was in the cache but is dirty, re-upload
154 // TODO: Re-adjust the cache size if the bitmap's dimensions have changed
155 texture->upload(*bitmap);
156 texture->generation = bitmap->getGenerationID();
162 bool TextureCache::prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap) {
163 Texture* texture = getCachedTexture(bitmap, AtlasUsageType::Use);
165 texture->isInUse = ownerToken;
170 bool TextureCache::prefetch(const SkBitmap* bitmap) {
171 return getCachedTexture(bitmap, AtlasUsageType::Use);
174 Texture* TextureCache::get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) {
175 Texture* texture = getCachedTexture(bitmap, atlasUsageType);
178 if (!canMakeTextureFromBitmap(bitmap)) {
182 const uint32_t size = bitmap->rowBytes() * bitmap->height();
183 texture = new Texture(Caches::getInstance());
184 texture->bitmapSize = size;
185 texture->upload(*bitmap);
186 texture->generation = bitmap->getGenerationID();
187 texture->cleanup = true;
193 void TextureCache::releaseTexture(uint32_t pixelRefStableID) {
194 Mutex::Autolock _l(mLock);
195 mGarbage.push_back(pixelRefStableID);
198 void TextureCache::clearGarbage() {
199 Mutex::Autolock _l(mLock);
200 size_t count = mGarbage.size();
201 for (size_t i = 0; i < count; i++) {
202 uint32_t pixelRefId = mGarbage[i];
203 mCache.remove(pixelRefId);
208 void TextureCache::clear() {
210 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
213 void TextureCache::flush() {
214 if (mFlushRate >= 1.0f || mCache.size() == 0) return;
215 if (mFlushRate <= 0.0f) {
220 uint32_t targetSize = uint32_t(mSize * mFlushRate);
221 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
223 while (mSize > targetSize) {
224 mCache.removeOldest();
228 }; // namespace uirenderer
229 }; // namespace android