OffscreenBufferPool.h revision 98787e6c9b2c10b1ab7820bdac168686025b924a
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 *
38 * Has two distinct sizes - viewportWidth/viewportHeight describe content area,
39 * texture.width/.height are actual allocated texture size. Texture will tend to be larger than the
40 * viewport bounds, since textures are always allocated with width / height as a multiple of 64, for
41 * the purpose of improving reuse.
42 */
43class OffscreenBuffer {
44public:
45    OffscreenBuffer(RenderState& renderState, Caches& caches,
46            uint32_t viewportWidth, uint32_t viewportHeight);
47    ~OffscreenBuffer();
48
49    // must be called prior to rendering, to construct/update vertex buffer
50    void updateMeshFromRegion();
51
52    // Set by RenderNode for HW layers, TODO for clipped saveLayers
53    void setWindowTransform(const Matrix4& transform) {
54        inverseTransformInWindow.loadInverse(transform);
55    }
56
57    static uint32_t computeIdealDimension(uint32_t dimension);
58
59    uint32_t getSizeInBytes() { return texture.width * texture.height * 4; }
60
61    RenderState& renderState;
62
63    uint32_t viewportWidth;
64    uint32_t viewportHeight;
65    Texture texture;
66
67    // Portion of layer that has been drawn to. Used to minimize drawing area when
68    // drawing back to screen / parent FBO.
69    Region region;
70
71    Matrix4 inverseTransformInWindow;
72
73    // vbo / size of mesh
74    GLsizei elementCount = 0;
75    GLuint vbo = 0;
76};
77
78/**
79 * Pool of OffscreenBuffers allocated, but not currently in use.
80 */
81class OffscreenBufferPool {
82public:
83    OffscreenBufferPool();
84    ~OffscreenBufferPool();
85
86    WARN_UNUSED_RESULT OffscreenBuffer* get(RenderState& renderState,
87            const uint32_t width, const uint32_t height);
88
89    WARN_UNUSED_RESULT OffscreenBuffer* resize(OffscreenBuffer* layer,
90            const uint32_t width, const uint32_t height);
91
92    void putOrDelete(OffscreenBuffer* layer);
93
94    /**
95     * Clears the pool. This causes all layers to be deleted.
96     */
97    void clear();
98
99    /**
100     * Returns the maximum size of the pool in bytes.
101     */
102    uint32_t getMaxSize() { return mMaxSize; }
103
104    /**
105     * Returns the current size of the pool in bytes.
106     */
107    uint32_t getSize() { return mSize; }
108
109    size_t getCount() { return mPool.size(); }
110
111    /**
112     * Prints out the content of the pool.
113     */
114    void dump();
115private:
116    struct Entry {
117        Entry() {}
118
119        Entry(const uint32_t layerWidth, const uint32_t layerHeight)
120                : width(OffscreenBuffer::computeIdealDimension(layerWidth))
121                , height(OffscreenBuffer::computeIdealDimension(layerHeight)) {}
122
123        Entry(OffscreenBuffer* layer)
124                : layer(layer)
125                , width(layer->texture.width)
126                , height(layer->texture.height) {
127        }
128
129        static int compare(const Entry& lhs, const Entry& rhs);
130
131        bool operator==(const Entry& other) const {
132            return compare(*this, other) == 0;
133        }
134
135        bool operator!=(const Entry& other) const {
136            return compare(*this, other) != 0;
137        }
138
139        bool operator<(const Entry& other) const {
140            return Entry::compare(*this, other) < 0;
141        }
142
143        OffscreenBuffer* layer = nullptr;
144        uint32_t width = 0;
145        uint32_t height = 0;
146    }; // struct Entry
147
148    std::multiset<Entry> mPool;
149
150    uint32_t mSize = 0;
151    uint32_t mMaxSize;
152}; // class OffscreenBufferCache
153
154}; // namespace uirenderer
155}; // namespace android
156
157#endif // ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H
158