FboCache.cpp revision eb99356a0548684a501766e6a524529ab93304c8
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 <stdlib.h> 20 21#include "FboCache.h" 22#include "Properties.h" 23 24namespace android { 25namespace uirenderer { 26 27/////////////////////////////////////////////////////////////////////////////// 28// Constructors/destructor 29/////////////////////////////////////////////////////////////////////////////// 30 31FboCache::FboCache(): mMaxSize(DEFAULT_FBO_CACHE_SIZE) { 32 char property[PROPERTY_VALUE_MAX]; 33 if (property_get(PROPERTY_FBO_CACHE_SIZE, property, NULL) > 0) { 34 LOGD(" Setting fbo cache size to %s", property); 35 mMaxSize = atoi(property); 36 } else { 37 LOGD(" Using default fbo cache size of %d", DEFAULT_FBO_CACHE_SIZE); 38 } 39} 40 41FboCache::~FboCache() { 42 clear(); 43} 44 45/////////////////////////////////////////////////////////////////////////////// 46// Size management 47/////////////////////////////////////////////////////////////////////////////// 48 49uint32_t FboCache::getSize() { 50 return mCache.size(); 51} 52 53uint32_t FboCache::getMaxSize() { 54 return mMaxSize; 55} 56 57/////////////////////////////////////////////////////////////////////////////// 58// Caching 59/////////////////////////////////////////////////////////////////////////////// 60 61void FboCache::clear() { 62 for (size_t i = 0; i < mCache.size(); i++) { 63 const GLuint fbo = mCache.itemAt(i); 64 glDeleteFramebuffers(1, &fbo); 65 } 66 mCache.clear(); 67} 68 69GLuint FboCache::get() { 70 GLuint fbo; 71 if (mCache.size() > 0) { 72 fbo = mCache.itemAt(mCache.size() - 1); 73 mCache.removeAt(mCache.size() - 1); 74 } else { 75 glGenFramebuffers(1, &fbo); 76 } 77 return fbo; 78} 79 80bool FboCache::put(GLuint fbo) { 81 if (mCache.size() < mMaxSize) { 82 mCache.add(fbo); 83 return true; 84 } 85 86 glDeleteFramebuffers(1, &fbo); 87 return false; 88} 89 90}; // namespace uirenderer 91}; // namespace android 92