Romain Guy | dda57020 | 2010-07-06 11:39:32 -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 | |
| 19 | #include <GLES2/gl2.h> |
| 20 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 23 | #include "LayerCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 24 | #include "Properties.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // Constructors/destructor |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 33 | LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 34 | char property[PROPERTY_VALUE_MAX]; |
| 35 | if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { |
| 36 | LOGD(" Setting layer cache size to %sMB", property); |
| 37 | setMaxSize(MB(atof(property))); |
| 38 | } else { |
| 39 | LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); |
| 40 | } |
| 41 | } |
| 42 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 43 | LayerCache::~LayerCache() { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 44 | clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | // Size management |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | |
| 51 | uint32_t LayerCache::getSize() { |
| 52 | return mSize; |
| 53 | } |
| 54 | |
| 55 | uint32_t LayerCache::getMaxSize() { |
| 56 | return mMaxSize; |
| 57 | } |
| 58 | |
| 59 | void LayerCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 60 | clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 61 | mMaxSize = maxSize; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | // Caching |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | void LayerCache::deleteLayer(Layer* layer) { |
| 69 | if (layer) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 70 | mSize -= layer->width * layer->height * 4; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 71 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 72 | glDeleteTextures(1, &layer->texture); |
| 73 | delete layer; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void LayerCache::clear() { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 78 | size_t count = mCache.size(); |
| 79 | for (size_t i = 0; i < count; i++) { |
| 80 | deleteLayer(mCache.itemAt(i).mLayer); |
| 81 | } |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 82 | mCache.clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 85 | Layer* LayerCache::get(const uint32_t width, const uint32_t height) { |
| 86 | Layer* layer = NULL; |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 87 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 88 | LayerEntry entry(width, height); |
| 89 | ssize_t index = mCache.indexOf(entry); |
| 90 | |
| 91 | if (index >= 0) { |
| 92 | entry = mCache.itemAt(index); |
| 93 | mCache.removeAt(index); |
| 94 | |
| 95 | layer = entry.mLayer; |
| 96 | mSize -= layer->width * layer->height * 4; |
| 97 | |
| 98 | LAYER_LOGD("Reusing layer %dx%d", layer->width, layer->height); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 99 | } else { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 100 | LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 101 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 102 | layer = new Layer(entry.mWidth, entry.mHeight); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 103 | layer->blend = true; |
Romain Guy | 38c85b9 | 2010-09-22 22:48:20 -0700 | [diff] [blame] | 104 | layer->empty = true; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 105 | layer->fbo = 0; |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 106 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 107 | glGenTextures(1, &layer->texture); |
| 108 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
| 109 | |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 110 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 111 | |
Romain Guy | 38c85b9 | 2010-09-22 22:48:20 -0700 | [diff] [blame] | 112 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 113 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 114 | |
| 115 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 116 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 117 | |
| 118 | #if DEBUG_LAYERS |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 119 | size_t size = mCache.size(); |
| 120 | for (size_t i = 0; i < size; i++) { |
| 121 | const LayerEntry& entry = mCache.itemAt(i); |
| 122 | LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 123 | } |
| 124 | #endif |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 125 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 126 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 127 | return layer; |
| 128 | } |
| 129 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 130 | bool LayerCache::put(Layer* layer) { |
| 131 | const uint32_t size = layer->width * layer->height * 4; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 132 | // Don't even try to cache a layer that's bigger than the cache |
| 133 | if (size < mMaxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 134 | // TODO: Use an LRU |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 135 | while (mSize + size > mMaxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 136 | Layer* biggest = mCache.top().mLayer; |
| 137 | deleteLayer(biggest); |
| 138 | mCache.removeAt(mCache.size() - 1); |
| 139 | |
| 140 | LAYER_LOGD(" Deleting layer %.2fx%.2f", biggest->layer.getWidth(), |
| 141 | biggest->layer.getHeight()); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame^] | 144 | LayerEntry entry(layer); |
| 145 | |
| 146 | mCache.add(entry); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 147 | mSize += size; |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | }; // namespace uirenderer |
| 155 | }; // namespace android |