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