Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #ifndef ANDROID_HWUI_RENDER_BUFFER_CACHE_H |
| 18 | #define ANDROID_HWUI_RENDER_BUFFER_CACHE_H |
| 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | |
| 22 | #include "RenderBuffer.h" |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 23 | |
| 24 | #include <set> |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | class RenderBufferCache { |
| 30 | public: |
| 31 | RenderBufferCache(); |
| 32 | ~RenderBufferCache(); |
| 33 | |
| 34 | /** |
| 35 | * Returns a buffer with the exact specified dimensions. If no suitable |
| 36 | * buffer can be found, a new one is created and returned. If creating a |
| 37 | * new buffer fails, NULL is returned. |
| 38 | * |
| 39 | * When a buffer is obtained from the cache, it is removed and the total |
| 40 | * size of the cache goes down. |
| 41 | * |
| 42 | * The returned buffer is always allocated and bound |
| 43 | * (see RenderBuffer::isAllocated()). |
| 44 | * |
| 45 | * @param format The desired render buffer format |
| 46 | * @param width The desired width of the buffer |
| 47 | * @param height The desired height of the buffer |
| 48 | */ |
| 49 | RenderBuffer* get(GLenum format, const uint32_t width, const uint32_t height); |
| 50 | |
| 51 | /** |
| 52 | * Adds the buffer to the cache. The buffer will not be added if there is |
| 53 | * not enough space available. Adding a buffer can cause other buffer to |
| 54 | * be removed from the cache. |
| 55 | * |
| 56 | * @param buffer The render buffer to add to the cache |
| 57 | * |
| 58 | * @return True if the buffer was added, false otherwise. |
| 59 | */ |
| 60 | bool put(RenderBuffer* buffer); |
| 61 | /** |
| 62 | * Clears the cache. This causes all layers to be deleted. |
| 63 | */ |
| 64 | void clear(); |
| 65 | |
| 66 | /** |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 67 | * Returns the maximum size of the cache in bytes. |
| 68 | */ |
| 69 | uint32_t getMaxSize(); |
| 70 | /** |
| 71 | * Returns the current size of the cache in bytes. |
| 72 | */ |
| 73 | uint32_t getSize(); |
| 74 | |
| 75 | private: |
| 76 | struct RenderBufferEntry { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 77 | RenderBufferEntry() : mBuffer(nullptr), mWidth(0), mHeight(0) {} |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 78 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 79 | RenderBufferEntry(GLenum format, const uint32_t width, const uint32_t height) |
| 80 | : mBuffer(nullptr), mFormat(format), mWidth(width), mHeight(height) {} |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 81 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 82 | explicit RenderBufferEntry(RenderBuffer* buffer) |
| 83 | : mBuffer(buffer) |
| 84 | , mFormat(buffer->getFormat()) |
| 85 | , mWidth(buffer->getWidth()) |
| 86 | , mHeight(buffer->getHeight()) {} |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 87 | |
| 88 | static int compare(const RenderBufferEntry& lhs, const RenderBufferEntry& rhs); |
| 89 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 90 | bool operator==(const RenderBufferEntry& other) const { return compare(*this, other) == 0; } |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 91 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 92 | bool operator!=(const RenderBufferEntry& other) const { return compare(*this, other) != 0; } |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 93 | |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 94 | bool operator<(const RenderBufferEntry& other) const { |
| 95 | return RenderBufferEntry::compare(*this, other) < 0; |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | RenderBuffer* mBuffer; |
| 99 | GLenum mFormat; |
| 100 | uint32_t mWidth; |
| 101 | uint32_t mHeight; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 102 | }; // struct RenderBufferEntry |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 103 | |
| 104 | void deleteBuffer(RenderBuffer* buffer); |
| 105 | |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 106 | std::multiset<RenderBufferEntry> mCache; |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 107 | |
| 108 | uint32_t mSize; |
| 109 | uint32_t mMaxSize; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 110 | }; // class RenderBufferCache |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 111 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 112 | }; // namespace uirenderer |
| 113 | }; // namespace android |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 114 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 115 | #endif // ANDROID_HWUI_RENDER_BUFFER_CACHE_H |