OffscreenBufferPool.h revision 9fded232a9548a304e0145011df8849fba0dcda7
1/*
2 * Copyright (C) 2015 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_OFFSCREEN_BUFFER_POOL_H
18#define ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H
19
20#include "Caches.h"
21#include "Texture.h"
22#include "utils/Macros.h"
23
24#include <ui/Region.h>
25
26#include <set>
27
28namespace android {
29namespace uirenderer {
30
31class RenderState;
32
33/**
34 * Lightweight alternative to Layer. Owns the persistent state of an offscreen render target, and
35 * encompasses enough information to draw it back on screen (minus paint properties, which are held
36 * by LayerOp).
37 */
38class OffscreenBuffer {
39public:
40    OffscreenBuffer(RenderState& renderState, Caches& caches,
41            uint32_t viewportWidth, uint32_t viewportHeight);
42    ~OffscreenBuffer();
43
44    // must be called prior to rendering, to construct/update vertex buffer
45    void updateMeshFromRegion();
46
47    static uint32_t computeIdealDimension(uint32_t dimension);
48
49    uint32_t getSizeInBytes() { return texture.width * texture.height * 4; }
50
51    RenderState& renderState;
52    uint32_t viewportWidth;
53    uint32_t viewportHeight;
54    Texture texture;
55
56    // Portion of layer that has been drawn to. Used to minimize drawing area when
57    // drawing back to screen / parent FBO.
58    Region region;
59    GLsizei elementCount = 0;
60    GLuint vbo = 0;
61};
62
63/**
64 * Pool of OffscreenBuffers allocated, but not currently in use.
65 */
66class OffscreenBufferPool {
67public:
68    OffscreenBufferPool();
69    ~OffscreenBufferPool();
70
71    WARN_UNUSED_RESULT OffscreenBuffer* get(RenderState& renderState,
72            const uint32_t width, const uint32_t height);
73
74    WARN_UNUSED_RESULT OffscreenBuffer* resize(OffscreenBuffer* layer,
75            const uint32_t width, const uint32_t height);
76
77    void putOrDelete(OffscreenBuffer* layer);
78
79    /**
80     * Clears the pool. This causes all layers to be deleted.
81     */
82    void clear();
83
84    /**
85     * Returns the maximum size of the pool in bytes.
86     */
87    uint32_t getMaxSize() { return mMaxSize; }
88
89    /**
90     * Returns the current size of the pool in bytes.
91     */
92    uint32_t getSize() { return mSize; }
93
94    size_t getCount() { return mPool.size(); }
95
96    /**
97     * Prints out the content of the pool.
98     */
99    void dump();
100private:
101    struct Entry {
102        Entry() {}
103
104        Entry(const uint32_t layerWidth, const uint32_t layerHeight)
105                : width(OffscreenBuffer::computeIdealDimension(layerWidth))
106                , height(OffscreenBuffer::computeIdealDimension(layerHeight)) {}
107
108        Entry(OffscreenBuffer* layer)
109                : layer(layer)
110                , width(layer->texture.width)
111                , height(layer->texture.height) {
112        }
113
114        static int compare(const Entry& lhs, const Entry& rhs);
115
116        bool operator==(const Entry& other) const {
117            return compare(*this, other) == 0;
118        }
119
120        bool operator!=(const Entry& other) const {
121            return compare(*this, other) != 0;
122        }
123
124        bool operator<(const Entry& other) const {
125            return Entry::compare(*this, other) < 0;
126        }
127
128        OffscreenBuffer* layer = nullptr;
129        uint32_t width = 0;
130        uint32_t height = 0;
131    }; // struct Entry
132
133    std::multiset<Entry> mPool;
134
135    uint32_t mSize = 0;
136    uint32_t mMaxSize;
137}; // class OffscreenBufferCache
138
139}; // namespace uirenderer
140}; // namespace android
141
142#endif // ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H
143