blob: c936a5283965339d612c1d4c8ac5a873b3bac1c8 [file] [log] [blame]
Romain Guy8d4aeb72013-02-12 16:08:55 -08001/*
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 Reckbef837d2015-07-29 16:51:05 -070023
24#include <set>
Romain Guy8d4aeb72013-02-12 16:08:55 -080025
26namespace android {
27namespace uirenderer {
28
29class RenderBufferCache {
30public:
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 Guy8d4aeb72013-02-12 16:08:55 -080067 * 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
75private:
76 struct RenderBufferEntry {
John Reck1bcacfd2017-11-03 10:12:19 -070077 RenderBufferEntry() : mBuffer(nullptr), mWidth(0), mHeight(0) {}
Romain Guy8d4aeb72013-02-12 16:08:55 -080078
John Reck1bcacfd2017-11-03 10:12:19 -070079 RenderBufferEntry(GLenum format, const uint32_t width, const uint32_t height)
80 : mBuffer(nullptr), mFormat(format), mWidth(width), mHeight(height) {}
Romain Guy8d4aeb72013-02-12 16:08:55 -080081
John Reck1bcacfd2017-11-03 10:12:19 -070082 explicit RenderBufferEntry(RenderBuffer* buffer)
83 : mBuffer(buffer)
84 , mFormat(buffer->getFormat())
85 , mWidth(buffer->getWidth())
86 , mHeight(buffer->getHeight()) {}
Romain Guy8d4aeb72013-02-12 16:08:55 -080087
88 static int compare(const RenderBufferEntry& lhs, const RenderBufferEntry& rhs);
89
John Reck1bcacfd2017-11-03 10:12:19 -070090 bool operator==(const RenderBufferEntry& other) const { return compare(*this, other) == 0; }
Romain Guy8d4aeb72013-02-12 16:08:55 -080091
John Reck1bcacfd2017-11-03 10:12:19 -070092 bool operator!=(const RenderBufferEntry& other) const { return compare(*this, other) != 0; }
Romain Guy8d4aeb72013-02-12 16:08:55 -080093
John Reckbef837d2015-07-29 16:51:05 -070094 bool operator<(const RenderBufferEntry& other) const {
95 return RenderBufferEntry::compare(*this, other) < 0;
Romain Guy8d4aeb72013-02-12 16:08:55 -080096 }
97
98 RenderBuffer* mBuffer;
99 GLenum mFormat;
100 uint32_t mWidth;
101 uint32_t mHeight;
John Reck1bcacfd2017-11-03 10:12:19 -0700102 }; // struct RenderBufferEntry
Romain Guy8d4aeb72013-02-12 16:08:55 -0800103
104 void deleteBuffer(RenderBuffer* buffer);
105
John Reckbef837d2015-07-29 16:51:05 -0700106 std::multiset<RenderBufferEntry> mCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800107
108 uint32_t mSize;
109 uint32_t mMaxSize;
John Reck1bcacfd2017-11-03 10:12:19 -0700110}; // class RenderBufferCache
Romain Guy8d4aeb72013-02-12 16:08:55 -0800111
John Reck1bcacfd2017-11-03 10:12:19 -0700112}; // namespace uirenderer
113}; // namespace android
Romain Guy8d4aeb72013-02-12 16:08:55 -0800114
John Reck1bcacfd2017-11-03 10:12:19 -0700115#endif // ANDROID_HWUI_RENDER_BUFFER_CACHE_H