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 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 19 | #include "Debug.h" |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 20 | #include "FboCache.h" |
| 21 | #include "Properties.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Constructors/destructor |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | FboCache::FboCache(): mMaxSize(DEFAULT_FBO_CACHE_SIZE) { |
| 31 | char property[PROPERTY_VALUE_MAX]; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 32 | if (property_get(PROPERTY_FBO_CACHE_SIZE, property, nullptr) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 33 | INIT_LOGD(" Setting fbo cache size to %s", property); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 34 | mMaxSize = atoi(property); |
| 35 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 36 | 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] | 37 | } |
| 38 | } |
| 39 | |
| 40 | FboCache::~FboCache() { |
| 41 | clear(); |
| 42 | } |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | // Size management |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | |
| 48 | uint32_t FboCache::getSize() { |
| 49 | return mCache.size(); |
| 50 | } |
| 51 | |
| 52 | uint32_t FboCache::getMaxSize() { |
| 53 | return mMaxSize; |
| 54 | } |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | // Caching |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | void FboCache::clear() { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 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(); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | GLuint FboCache::get() { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 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; |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool FboCache::put(GLuint fbo) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 80 | if (mCache.size() < mMaxSize) { |
| 81 | mCache.add(fbo); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | glDeleteFramebuffers(1, &fbo); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | |
| 89 | }; // namespace uirenderer |
| 90 | }; // namespace android |