PatchCache.cpp revision 4bb942083a0d4db746adf95349108dd8ef842e32
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17#define LOG_TAG "OpenGLRenderer" 18 19#include <utils/Log.h> 20 21#include "PatchCache.h" 22#include "Properties.h" 23 24namespace android { 25namespace uirenderer { 26 27/////////////////////////////////////////////////////////////////////////////// 28// Constructors/destructor 29/////////////////////////////////////////////////////////////////////////////// 30 31PatchCache::PatchCache(): mMaxEntries(DEFAULT_PATCH_CACHE_SIZE) { 32} 33 34PatchCache::PatchCache(uint32_t maxEntries): mMaxEntries(maxEntries) { 35} 36 37PatchCache::~PatchCache() { 38 clear(); 39} 40 41/////////////////////////////////////////////////////////////////////////////// 42// Caching 43/////////////////////////////////////////////////////////////////////////////// 44 45void PatchCache::clear() { 46 size_t count = mCache.size(); 47 for (size_t i = 0; i < count; i++) { 48 delete mCache.valueAt(i); 49 } 50 mCache.clear(); 51} 52 53Patch* PatchCache::get(const float bitmapWidth, const float bitmapHeight, 54 const float pixelWidth, const float pixelHeight, 55 const int32_t* xDivs, const int32_t* yDivs, const uint32_t* colors, 56 const uint32_t width, const uint32_t height, const int8_t numColors) { 57 58 int8_t transparentQuads = 0; 59 uint32_t colorKey = 0; 60 61 if (uint8_t(numColors) < sizeof(uint32_t) * 4) { 62 for (int8_t i = 0; i < numColors; i++) { 63 if (colors[i] == 0x0) { 64 transparentQuads++; 65 colorKey |= 0x1 << i; 66 } 67 } 68 } 69 70 const PatchDescription description(bitmapWidth, bitmapHeight, 71 pixelWidth, pixelHeight, width, height, transparentQuads, colorKey); 72 73 ssize_t index = mCache.indexOfKey(description); 74 Patch* mesh = NULL; 75 if (index >= 0) { 76 mesh = mCache.valueAt(index); 77 } 78 79 if (!mesh) { 80 PATCH_LOGD("New patch mesh " 81 "xCount=%d yCount=%d, w=%.2f h=%.2f, bw=%.2f bh=%.2f", 82 width, height, pixelWidth, pixelHeight, bitmapWidth, bitmapHeight); 83 84 mesh = new Patch(width, height, transparentQuads); 85 mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f, 86 pixelWidth, pixelHeight, xDivs, yDivs, width, height, colorKey); 87 88 if (mCache.size() >= mMaxEntries) { 89 delete mCache.valueAt(mCache.size() - 1); 90 mCache.removeItemsAt(mCache.size() - 1, 1); 91 } 92 93 mCache.add(description, mesh); 94 } 95 96 return mesh; 97} 98 99}; // namespace uirenderer 100}; // namespace android 101