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