Romain Guy | dda5702 | 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 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 17 | #include "LayerCache.h" |
| 18 | |
| 19 | #include "Caches.h" |
| 20 | #include "Properties.h" |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 21 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 24 | #include <GLES2/gl2.h> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // Constructors/destructor |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 33 | LayerCache::LayerCache() |
| 34 | : mSize(0) |
| 35 | , mMaxSize(Properties::layerPoolSize) {} |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 36 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 37 | LayerCache::~LayerCache() { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 38 | clear(); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | // Size management |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 45 | size_t LayerCache::getCount() { |
| 46 | return mCache.size(); |
| 47 | } |
| 48 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 49 | uint32_t LayerCache::getSize() { |
| 50 | return mSize; |
| 51 | } |
| 52 | |
| 53 | uint32_t LayerCache::getMaxSize() { |
| 54 | return mMaxSize; |
| 55 | } |
| 56 | |
| 57 | void LayerCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 58 | clear(); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 59 | mMaxSize = maxSize; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | // Caching |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 66 | int LayerCache::LayerEntry::compare(const LayerCache::LayerEntry& lhs, |
| 67 | const LayerCache::LayerEntry& rhs) { |
| 68 | int deltaInt = int(lhs.mWidth) - int(rhs.mWidth); |
| 69 | if (deltaInt != 0) return deltaInt; |
| 70 | |
| 71 | return int(lhs.mHeight) - int(rhs.mHeight); |
| 72 | } |
| 73 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 74 | void LayerCache::deleteLayer(Layer* layer) { |
| 75 | if (layer) { |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 76 | LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(), |
| 77 | layer->getFbo()); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 78 | mSize -= layer->getWidth() * layer->getHeight() * 4; |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 79 | layer->state = Layer::State::DeletedFromCache; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 80 | layer->decStrong(nullptr); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | void LayerCache::clear() { |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 85 | for (auto entry : mCache) { |
| 86 | deleteLayer(entry.mLayer); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 87 | } |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 88 | mCache.clear(); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 89 | } |
| 90 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 91 | 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] | 92 | Layer* layer = nullptr; |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 93 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 94 | LayerEntry entry(width, height); |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 95 | auto iter = mCache.find(entry); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 96 | |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 97 | if (iter != mCache.end()) { |
| 98 | entry = *iter; |
| 99 | mCache.erase(iter); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 100 | |
| 101 | layer = entry.mLayer; |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 102 | layer->state = Layer::State::RemovedFromCache; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 103 | mSize -= layer->getWidth() * layer->getHeight() * 4; |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 104 | |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 105 | LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight()); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 106 | } else { |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 107 | LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 108 | |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 109 | layer = new Layer(Layer::Type::DisplayList, renderState, entry.mWidth, entry.mHeight); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 110 | layer->setBlend(true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 111 | layer->generateTexture(); |
| 112 | layer->bindTexture(); |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 113 | layer->setFilter(GL_NEAREST); |
| 114 | layer->setWrap(GL_CLAMP_TO_EDGE, false); |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 115 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 116 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 117 | #if DEBUG_LAYERS |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 118 | dump(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 119 | #endif |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 120 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 121 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 122 | return layer; |
| 123 | } |
| 124 | |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 125 | void LayerCache::dump() { |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 126 | for (auto entry : mCache) { |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 127 | ALOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 131 | bool LayerCache::put(Layer* layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 132 | if (!layer->isCacheable()) return false; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 133 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 134 | const uint32_t size = layer->getWidth() * layer->getHeight() * 4; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 135 | // Don't even try to cache a layer that's bigger than the cache |
| 136 | if (size < mMaxSize) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 137 | // TODO: Use an LRU |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 138 | while (mSize + size > mMaxSize) { |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 139 | Layer* victim = mCache.begin()->mLayer; |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 140 | deleteLayer(victim); |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 141 | mCache.erase(mCache.begin()); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 142 | |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 143 | LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(), |
| 144 | victim->layer.getHeight()); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 147 | layer->cancelDefer(); |
Romain Guy | 5c88fc7 | 2012-04-02 17:43:05 -0700 | [diff] [blame] | 148 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 149 | LayerEntry entry(layer); |
| 150 | |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 151 | mCache.insert(entry); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 152 | mSize += size; |
| 153 | |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 154 | layer->state = Layer::State::InCache; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 155 | return true; |
| 156 | } |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 157 | |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 158 | layer->state = Layer::State::FailedToCache; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
| 162 | }; // namespace uirenderer |
| 163 | }; // namespace android |