| 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 { | 
|  | 77 | RenderBufferEntry(): | 
| Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 78 | mBuffer(nullptr), mWidth(0), mHeight(0) { | 
| Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
|  | 81 | RenderBufferEntry(GLenum format, const uint32_t width, const uint32_t height): | 
| Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 82 | mBuffer(nullptr), mFormat(format), mWidth(width), mHeight(height) { | 
| Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
|  | 85 | RenderBufferEntry(RenderBuffer* buffer): | 
|  | 86 | mBuffer(buffer), mFormat(buffer->getFormat()), | 
|  | 87 | mWidth(buffer->getWidth()), mHeight(buffer->getHeight()) { | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | static int compare(const RenderBufferEntry& lhs, const RenderBufferEntry& rhs); | 
|  | 91 |  | 
|  | 92 | bool operator==(const RenderBufferEntry& other) const { | 
|  | 93 | return compare(*this, other) == 0; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | bool operator!=(const RenderBufferEntry& other) const { | 
|  | 97 | return compare(*this, other) != 0; | 
|  | 98 | } | 
|  | 99 |  | 
| John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 100 | bool operator<(const RenderBufferEntry& other) const { | 
|  | 101 | return RenderBufferEntry::compare(*this, other) < 0; | 
| Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | RenderBuffer* mBuffer; | 
|  | 105 | GLenum mFormat; | 
|  | 106 | uint32_t mWidth; | 
|  | 107 | uint32_t mHeight; | 
|  | 108 | }; // struct RenderBufferEntry | 
|  | 109 |  | 
|  | 110 | void deleteBuffer(RenderBuffer* buffer); | 
|  | 111 |  | 
| John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 112 | std::multiset<RenderBufferEntry> mCache; | 
| Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 113 |  | 
|  | 114 | uint32_t mSize; | 
|  | 115 | uint32_t mMaxSize; | 
|  | 116 | }; // class RenderBufferCache | 
|  | 117 |  | 
|  | 118 | }; // namespace uirenderer | 
|  | 119 | }; // namespace android | 
|  | 120 |  | 
|  | 121 | #endif // ANDROID_HWUI_RENDER_BUFFER_CACHE_H |