• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
23 #include "utils/SortedList.h"
24 
25 namespace android {
26 namespace uirenderer {
27 
28 class RenderBufferCache {
29 public:
30     RenderBufferCache();
31     ~RenderBufferCache();
32 
33     /**
34      * Returns a buffer with the exact specified dimensions. If no suitable
35      * buffer can be found, a new one is created and returned. If creating a
36      * new buffer fails, NULL is returned.
37      *
38      * When a buffer is obtained from the cache, it is removed and the total
39      * size of the cache goes down.
40      *
41      * The returned buffer is always allocated and bound
42      * (see RenderBuffer::isAllocated()).
43      *
44      * @param format The desired render buffer format
45      * @param width The desired width of the buffer
46      * @param height The desired height of the buffer
47      */
48     RenderBuffer* get(GLenum format, const uint32_t width, const uint32_t height);
49 
50     /**
51      * Adds the buffer to the cache. The buffer will not be added if there is
52      * not enough space available. Adding a buffer can cause other buffer to
53      * be removed from the cache.
54      *
55      * @param buffer The render buffer to add to the cache
56      *
57      * @return True if the buffer was added, false otherwise.
58      */
59     bool put(RenderBuffer* buffer);
60     /**
61      * Clears the cache. This causes all layers to be deleted.
62      */
63     void clear();
64 
65     /**
66      * Sets the maximum size of the cache in bytes.
67      */
68     void setMaxSize(uint32_t maxSize);
69     /**
70      * Returns the maximum size of the cache in bytes.
71      */
72     uint32_t getMaxSize();
73     /**
74      * Returns the current size of the cache in bytes.
75      */
76     uint32_t getSize();
77 
78 private:
79     struct RenderBufferEntry {
RenderBufferEntryRenderBufferEntry80         RenderBufferEntry():
81             mBuffer(NULL), mWidth(0), mHeight(0) {
82         }
83 
RenderBufferEntryRenderBufferEntry84         RenderBufferEntry(GLenum format, const uint32_t width, const uint32_t height):
85             mBuffer(NULL), mFormat(format), mWidth(width), mHeight(height) {
86         }
87 
RenderBufferEntryRenderBufferEntry88         RenderBufferEntry(RenderBuffer* buffer):
89             mBuffer(buffer), mFormat(buffer->getFormat()),
90             mWidth(buffer->getWidth()), mHeight(buffer->getHeight()) {
91         }
92 
93         static int compare(const RenderBufferEntry& lhs, const RenderBufferEntry& rhs);
94 
95         bool operator==(const RenderBufferEntry& other) const {
96             return compare(*this, other) == 0;
97         }
98 
99         bool operator!=(const RenderBufferEntry& other) const {
100             return compare(*this, other) != 0;
101         }
102 
strictly_order_typeRenderBufferEntry103         friend inline int strictly_order_type(const RenderBufferEntry& lhs,
104                 const RenderBufferEntry& rhs) {
105             return RenderBufferEntry::compare(lhs, rhs) < 0;
106         }
107 
compare_typeRenderBufferEntry108         friend inline int compare_type(const RenderBufferEntry& lhs,
109                 const RenderBufferEntry& rhs) {
110             return RenderBufferEntry::compare(lhs, rhs);
111         }
112 
113         RenderBuffer* mBuffer;
114         GLenum mFormat;
115         uint32_t mWidth;
116         uint32_t mHeight;
117     }; // struct RenderBufferEntry
118 
119     void deleteBuffer(RenderBuffer* buffer);
120 
121     SortedList<RenderBufferEntry> mCache;
122 
123     uint32_t mSize;
124     uint32_t mMaxSize;
125 }; // class RenderBufferCache
126 
127 }; // namespace uirenderer
128 }; // namespace android
129 
130 #endif // ANDROID_HWUI_RENDER_BUFFER_CACHE_H
131