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
24#include <set>
25
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    /**
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
75private:
76    struct RenderBufferEntry {
77        RenderBufferEntry() : mBuffer(nullptr), mWidth(0), mHeight(0) {}
78
79        RenderBufferEntry(GLenum format, const uint32_t width, const uint32_t height)
80                : mBuffer(nullptr), mFormat(format), mWidth(width), mHeight(height) {}
81
82        explicit RenderBufferEntry(RenderBuffer* buffer)
83                : mBuffer(buffer)
84                , mFormat(buffer->getFormat())
85                , mWidth(buffer->getWidth())
86                , mHeight(buffer->getHeight()) {}
87
88        static int compare(const RenderBufferEntry& lhs, const RenderBufferEntry& rhs);
89
90        bool operator==(const RenderBufferEntry& other) const { return compare(*this, other) == 0; }
91
92        bool operator!=(const RenderBufferEntry& other) const { return compare(*this, other) != 0; }
93
94        bool operator<(const RenderBufferEntry& other) const {
95            return RenderBufferEntry::compare(*this, other) < 0;
96        }
97
98        RenderBuffer* mBuffer;
99        GLenum mFormat;
100        uint32_t mWidth;
101        uint32_t mHeight;
102    };  // struct RenderBufferEntry
103
104    void deleteBuffer(RenderBuffer* buffer);
105
106    std::multiset<RenderBufferEntry> mCache;
107
108    uint32_t mSize;
109    uint32_t mMaxSize;
110};  // class RenderBufferCache
111
112};  // namespace uirenderer
113};  // namespace android
114
115#endif  // ANDROID_HWUI_RENDER_BUFFER_CACHE_H
116