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 | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 23 | #include "Caches.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]; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 36 | if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, nullptr) > 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 | |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 52 | size_t LayerCache::getCount() { |
| 53 | return mCache.size(); |
| 54 | } |
| 55 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 56 | uint32_t LayerCache::getSize() { |
| 57 | return mSize; |
| 58 | } |
| 59 | |
| 60 | uint32_t LayerCache::getMaxSize() { |
| 61 | return mMaxSize; |
| 62 | } |
| 63 | |
| 64 | void LayerCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 65 | clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 66 | mMaxSize = maxSize; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | // Caching |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 73 | int LayerCache::LayerEntry::compare(const LayerCache::LayerEntry& lhs, |
| 74 | const LayerCache::LayerEntry& rhs) { |
| 75 | int deltaInt = int(lhs.mWidth) - int(rhs.mWidth); |
| 76 | if (deltaInt != 0) return deltaInt; |
| 77 | |
| 78 | return int(lhs.mHeight) - int(rhs.mHeight); |
| 79 | } |
| 80 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 81 | void LayerCache::deleteLayer(Layer* layer) { |
| 82 | if (layer) { |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 83 | LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(), |
| 84 | layer->getFbo()); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 85 | mSize -= layer->getWidth() * layer->getHeight() * 4; |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 86 | layer->state = Layer::kState_DeletedFromCache; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 87 | layer->decStrong(nullptr); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | void LayerCache::clear() { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 92 | size_t count = mCache.size(); |
| 93 | for (size_t i = 0; i < count; i++) { |
| 94 | deleteLayer(mCache.itemAt(i).mLayer); |
| 95 | } |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 96 | mCache.clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 97 | } |
| 98 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 99 | Layer* LayerCache::get(RenderState& renderState, const uint32_t width, const uint32_t height) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 100 | Layer* layer = nullptr; |
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 | LayerEntry entry(width, height); |
| 103 | ssize_t index = mCache.indexOf(entry); |
| 104 | |
| 105 | if (index >= 0) { |
| 106 | entry = mCache.itemAt(index); |
| 107 | mCache.removeAt(index); |
| 108 | |
| 109 | layer = entry.mLayer; |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 110 | layer->state = Layer::kState_RemovedFromCache; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 111 | mSize -= layer->getWidth() * layer->getHeight() * 4; |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 112 | |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 113 | LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight()); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 114 | } else { |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 115 | LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 116 | |
Chris Craik | 8a226d2 | 2014-09-08 16:40:21 -0700 | [diff] [blame] | 117 | layer = new Layer(Layer::kType_DisplayList, renderState, entry.mWidth, entry.mHeight); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 118 | layer->setBlend(true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 119 | layer->generateTexture(); |
| 120 | layer->bindTexture(); |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 121 | layer->setFilter(GL_NEAREST); |
| 122 | layer->setWrap(GL_CLAMP_TO_EDGE, false); |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 123 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 124 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 125 | #if DEBUG_LAYERS |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 126 | dump(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 127 | #endif |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 128 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 129 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 130 | return layer; |
| 131 | } |
| 132 | |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 133 | void LayerCache::dump() { |
| 134 | size_t size = mCache.size(); |
| 135 | for (size_t i = 0; i < size; i++) { |
| 136 | const LayerEntry& entry = mCache.itemAt(i); |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 137 | ALOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 141 | bool LayerCache::put(Layer* layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 142 | if (!layer->isCacheable()) return false; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 143 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 144 | const uint32_t size = layer->getWidth() * layer->getHeight() * 4; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 145 | // Don't even try to cache a layer that's bigger than the cache |
| 146 | if (size < mMaxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 147 | // TODO: Use an LRU |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 148 | while (mSize + size > mMaxSize) { |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 149 | size_t position = 0; |
Romain Guy | 28d8ff6 | 2011-08-22 14:01:34 -0700 | [diff] [blame] | 150 | #if LAYER_REMOVE_BIGGEST_FIRST |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 151 | position = mCache.size() - 1; |
| 152 | #endif |
| 153 | Layer* victim = mCache.itemAt(position).mLayer; |
| 154 | deleteLayer(victim); |
| 155 | mCache.removeAt(position); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 156 | |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 157 | LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(), |
| 158 | victim->layer.getHeight()); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 161 | layer->cancelDefer(); |
Romain Guy | 5c88fc7 | 2012-04-02 17:43:05 -0700 | [diff] [blame] | 162 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 163 | LayerEntry entry(layer); |
| 164 | |
| 165 | mCache.add(entry); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 166 | mSize += size; |
| 167 | |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 168 | layer->state = Layer::kState_InCache; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 169 | return true; |
| 170 | } |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 171 | |
| 172 | layer->state = Layer::kState_FailedToCache; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
| 176 | }; // namespace uirenderer |
| 177 | }; // namespace android |