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