blob: cca3cb7a98f1f2189c826e753f708dd915290ecc [file] [log] [blame]
Romain Guye2d345e2010-09-24 18:39:22 -07001/*
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 Guyeb993562010-10-05 18:14:38 -070017#include <stdlib.h>
18
Romain Guyc9855a52011-01-21 21:14:15 -080019#include "Debug.h"
Romain Guye2d345e2010-09-24 18:39:22 -070020#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];
Chris Craikd41c4d82015-01-05 15:51:13 -080032 if (property_get(PROPERTY_FBO_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080033 INIT_LOGD(" Setting fbo cache size to %s", property);
Romain Guye2d345e2010-09-24 18:39:22 -070034 mMaxSize = atoi(property);
35 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080036 INIT_LOGD(" Using default fbo cache size of %d", DEFAULT_FBO_CACHE_SIZE);
Romain Guye2d345e2010-09-24 18:39:22 -070037 }
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() {
Romain Guyeb993562010-10-05 18:14:38 -070061 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 Guye2d345e2010-09-24 18:39:22 -070066}
67
68GLuint FboCache::get() {
Romain Guyeb993562010-10-05 18:14:38 -070069 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 Guye2d345e2010-09-24 18:39:22 -070077}
78
79bool FboCache::put(GLuint fbo) {
Romain Guyeb993562010-10-05 18:14:38 -070080 if (mCache.size() < mMaxSize) {
81 mCache.add(fbo);
82 return true;
83 }
84
85 glDeleteFramebuffers(1, &fbo);
Romain Guye2d345e2010-09-24 18:39:22 -070086 return false;
87}
88
89}; // namespace uirenderer
90}; // namespace android