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 | |
| 21 | #include "LayerCache.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Constructors/destructor |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | LayerCache::LayerCache(uint32_t maxByteSize): |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame^] | 31 | mCache(GenerationMultiCache<LayerSize, Layer*>::kUnlimitedCapacity), |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 32 | mSize(0), mMaxSize(maxByteSize) { |
| 33 | } |
| 34 | |
| 35 | LayerCache::~LayerCache() { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame^] | 36 | clear(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | // Size management |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | uint32_t LayerCache::getSize() { |
| 44 | return mSize; |
| 45 | } |
| 46 | |
| 47 | uint32_t LayerCache::getMaxSize() { |
| 48 | return mMaxSize; |
| 49 | } |
| 50 | |
| 51 | void LayerCache::setMaxSize(uint32_t maxSize) { |
| 52 | mMaxSize = maxSize; |
| 53 | while (mSize > mMaxSize) { |
| 54 | Layer* oldest = mCache.removeOldest(); |
| 55 | deleteLayer(oldest); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | // Callbacks |
| 61 | /////////////////////////////////////////////////////////////////////////////// |
| 62 | |
| 63 | void LayerCache::operator()(LayerSize& size, Layer*& layer) { |
| 64 | deleteLayer(layer); |
| 65 | } |
| 66 | |
| 67 | /////////////////////////////////////////////////////////////////////////////// |
| 68 | // Caching |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | |
| 71 | void LayerCache::deleteLayer(Layer* layer) { |
| 72 | if (layer) { |
| 73 | mSize -= layer->layer.getWidth() * layer->layer.getHeight() * 4; |
| 74 | |
| 75 | glDeleteFramebuffers(1, &layer->fbo); |
| 76 | glDeleteTextures(1, &layer->texture); |
| 77 | delete layer; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void LayerCache::clear() { |
| 82 | mCache.setOnEntryRemovedListener(this); |
| 83 | mCache.clear(); |
| 84 | mCache.setOnEntryRemovedListener(NULL); |
| 85 | } |
| 86 | |
| 87 | Layer* LayerCache::get(LayerSize& size) { |
| 88 | Layer* layer = mCache.remove(size); |
| 89 | if (layer) { |
| 90 | mSize -= layer->layer.getWidth() * layer->layer.getHeight() * 4; |
| 91 | } |
| 92 | return layer; |
| 93 | } |
| 94 | |
| 95 | bool LayerCache::put(LayerSize& layerSize, Layer* layer) { |
| 96 | const uint32_t size = layerSize.width * layerSize.height * 4; |
| 97 | // Don't even try to cache a layer that's bigger than the cache |
| 98 | if (size < mMaxSize) { |
| 99 | while (mSize + size > mMaxSize) { |
| 100 | Layer* oldest = mCache.removeOldest(); |
| 101 | deleteLayer(oldest); |
| 102 | } |
| 103 | |
| 104 | mCache.put(layerSize, layer); |
| 105 | mSize += size; |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | }; // namespace uirenderer |
| 113 | }; // namespace android |