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 | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 23 | #include "Debug.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 24 | #include "LayerCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 25 | #include "Properties.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // Constructors/destructor |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 34 | LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 35 | char property[PROPERTY_VALUE_MAX]; |
| 36 | if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 37 | INIT_LOGD(" Setting layer cache size to %sMB", property); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 38 | setMaxSize(MB(atof(property))); |
| 39 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 40 | INIT_LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 44 | LayerCache::~LayerCache() { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 45 | clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | // Size management |
| 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | uint32_t LayerCache::getSize() { |
| 53 | return mSize; |
| 54 | } |
| 55 | |
| 56 | uint32_t LayerCache::getMaxSize() { |
| 57 | return mMaxSize; |
| 58 | } |
| 59 | |
| 60 | void LayerCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 61 | clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 62 | mMaxSize = maxSize; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /////////////////////////////////////////////////////////////////////////////// |
| 66 | // Caching |
| 67 | /////////////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | void LayerCache::deleteLayer(Layer* layer) { |
| 70 | if (layer) { |
Romain Guy | 65b345f | 2011-07-27 18:51:50 -0700 | [diff] [blame] | 71 | LAYER_LOGD("Destroying layer %dx%d", layer->getWidth(), layer->getHeight()); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 72 | mSize -= layer->getWidth() * layer->getHeight() * 4; |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 73 | layer->deleteFbo(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 74 | layer->deleteTexture(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 75 | delete layer; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void LayerCache::clear() { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 80 | size_t count = mCache.size(); |
| 81 | for (size_t i = 0; i < count; i++) { |
| 82 | deleteLayer(mCache.itemAt(i).mLayer); |
| 83 | } |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 84 | mCache.clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 87 | Layer* LayerCache::get(const uint32_t width, const uint32_t height) { |
| 88 | Layer* layer = NULL; |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 89 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 90 | LayerEntry entry(width, height); |
| 91 | ssize_t index = mCache.indexOf(entry); |
| 92 | |
| 93 | if (index >= 0) { |
| 94 | entry = mCache.itemAt(index); |
| 95 | mCache.removeAt(index); |
| 96 | |
| 97 | layer = entry.mLayer; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 98 | mSize -= layer->getWidth() * layer->getHeight() * 4; |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 99 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 100 | LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight()); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 101 | } else { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 102 | LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 103 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 104 | layer = new Layer(entry.mWidth, entry.mHeight); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 105 | layer->setBlend(true); |
| 106 | layer->setEmpty(true); |
| 107 | layer->setFbo(0); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 108 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 109 | layer->generateTexture(); |
| 110 | layer->bindTexture(); |
| 111 | layer->setFilter(GL_NEAREST, GL_NEAREST); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 112 | layer->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, false); |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 113 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 114 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 115 | #if DEBUG_LAYERS |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 116 | dump(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 117 | #endif |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 118 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 119 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 120 | return layer; |
| 121 | } |
| 122 | |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 123 | void LayerCache::dump() { |
| 124 | size_t size = mCache.size(); |
| 125 | for (size_t i = 0; i < size; i++) { |
| 126 | const LayerEntry& entry = mCache.itemAt(i); |
| 127 | LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); |
| 128 | } |
| 129 | } |
| 130 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 131 | bool LayerCache::resize(Layer* layer, const uint32_t width, const uint32_t height) { |
| 132 | // TODO: We should be smarter and see if we have a texture of the appropriate |
| 133 | // size already in the cache, and reuse it instead of creating a new one |
| 134 | |
| 135 | LayerEntry entry(width, height); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 136 | if (entry.mWidth <= layer->getWidth() && entry.mHeight <= layer->getHeight()) { |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 140 | uint32_t oldWidth = layer->getWidth(); |
| 141 | uint32_t oldHeight = layer->getHeight(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 142 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 143 | glActiveTexture(GL_TEXTURE0); |
| 144 | layer->bindTexture(); |
| 145 | layer->setSize(entry.mWidth, entry.mHeight); |
| 146 | layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 147 | |
| 148 | if (glGetError() != GL_NO_ERROR) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 149 | layer->setSize(oldWidth, oldHeight); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 156 | bool LayerCache::put(Layer* layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 157 | if (!layer->isCacheable()) return false; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 158 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 159 | const uint32_t size = layer->getWidth() * layer->getHeight() * 4; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 160 | // Don't even try to cache a layer that's bigger than the cache |
| 161 | if (size < mMaxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 162 | // TODO: Use an LRU |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 163 | while (mSize + size > mMaxSize) { |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 164 | size_t position = 0; |
Romain Guy | 28d8ff6 | 2011-08-22 14:01:34 -0700 | [diff] [blame] | 165 | #if LAYER_REMOVE_BIGGEST_FIRST |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 166 | position = mCache.size() - 1; |
| 167 | #endif |
| 168 | Layer* victim = mCache.itemAt(position).mLayer; |
| 169 | deleteLayer(victim); |
| 170 | mCache.removeAt(position); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 171 | |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 172 | LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(), |
| 173 | victim->layer.getHeight()); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 176 | LayerEntry entry(layer); |
| 177 | |
| 178 | mCache.add(entry); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 179 | mSize += size; |
| 180 | |
| 181 | return true; |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | }; // namespace uirenderer |
| 187 | }; // namespace android |