Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 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 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 21 | #include "FboCache.h" |
| 22 | #include "Properties.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // Constructors/destructor |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | FboCache::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 | |
| 41 | FboCache::~FboCache() { |
| 42 | clear(); |
| 43 | } |
| 44 | |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | // Size management |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
| 49 | uint32_t FboCache::getSize() { |
| 50 | return mCache.size(); |
| 51 | } |
| 52 | |
| 53 | uint32_t FboCache::getMaxSize() { |
| 54 | return mMaxSize; |
| 55 | } |
| 56 | |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | // Caching |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | void FboCache::clear() { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 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(); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | GLuint FboCache::get() { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 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; |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | bool FboCache::put(GLuint fbo) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 81 | if (mCache.size() < mMaxSize) { |
| 82 | mCache.add(fbo); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | glDeleteFramebuffers(1, &fbo); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | |
| 90 | }; // namespace uirenderer |
| 91 | }; // namespace android |