blob: 94155efcbb0a3944786d91e2802c2dea51725988 [file] [log] [blame]
Chris Craik9fded232015-11-11 16:42:34 -08001/*
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
John Reck38e0c322015-11-10 12:19:17 -080020#include <GpuMemoryTracker.h>
Chris Craik9fded232015-11-11 16:42:34 -080021#include "Caches.h"
22#include "Texture.h"
23#include "utils/Macros.h"
Chris Craik9fded232015-11-11 16:42:34 -080024#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).
Chris Craik98787e62015-11-13 10:55:30 -080037 *
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.
Chris Craik9fded232015-11-11 16:42:34 -080042 */
John Reck38e0c322015-11-10 12:19:17 -080043class OffscreenBuffer : GpuMemoryTracker {
Chris Craik9fded232015-11-11 16:42:34 -080044public:
45 OffscreenBuffer(RenderState& renderState, Caches& caches,
46 uint32_t viewportWidth, uint32_t viewportHeight);
47 ~OffscreenBuffer();
48
Chris Craik7435eb12016-01-07 17:41:40 -080049 Rect getTextureCoordinates();
50
Chris Craik9fded232015-11-11 16:42:34 -080051 // must be called prior to rendering, to construct/update vertex buffer
52 void updateMeshFromRegion();
53
Chris Craik98787e62015-11-13 10:55:30 -080054 // Set by RenderNode for HW layers, TODO for clipped saveLayers
55 void setWindowTransform(const Matrix4& transform) {
56 inverseTransformInWindow.loadInverse(transform);
57 }
58
Chris Craik9fded232015-11-11 16:42:34 -080059 static uint32_t computeIdealDimension(uint32_t dimension);
60
John Reck38e0c322015-11-10 12:19:17 -080061 uint32_t getSizeInBytes() { return texture.objectSize(); }
Chris Craik9fded232015-11-11 16:42:34 -080062
63 RenderState& renderState;
Chris Craik98787e62015-11-13 10:55:30 -080064
Chris Craik9fded232015-11-11 16:42:34 -080065 uint32_t viewportWidth;
66 uint32_t viewportHeight;
67 Texture texture;
68
69 // Portion of layer that has been drawn to. Used to minimize drawing area when
70 // drawing back to screen / parent FBO.
71 Region region;
Chris Craik98787e62015-11-13 10:55:30 -080072
73 Matrix4 inverseTransformInWindow;
74
75 // vbo / size of mesh
Chris Craik9fded232015-11-11 16:42:34 -080076 GLsizei elementCount = 0;
77 GLuint vbo = 0;
78};
79
80/**
81 * Pool of OffscreenBuffers allocated, but not currently in use.
82 */
83class OffscreenBufferPool {
84public:
85 OffscreenBufferPool();
86 ~OffscreenBufferPool();
87
88 WARN_UNUSED_RESULT OffscreenBuffer* get(RenderState& renderState,
89 const uint32_t width, const uint32_t height);
90
91 WARN_UNUSED_RESULT OffscreenBuffer* resize(OffscreenBuffer* layer,
92 const uint32_t width, const uint32_t height);
93
94 void putOrDelete(OffscreenBuffer* layer);
95
96 /**
97 * Clears the pool. This causes all layers to be deleted.
98 */
99 void clear();
100
101 /**
102 * Returns the maximum size of the pool in bytes.
103 */
104 uint32_t getMaxSize() { return mMaxSize; }
105
106 /**
107 * Returns the current size of the pool in bytes.
108 */
109 uint32_t getSize() { return mSize; }
110
111 size_t getCount() { return mPool.size(); }
112
113 /**
114 * Prints out the content of the pool.
115 */
116 void dump();
117private:
118 struct Entry {
119 Entry() {}
120
121 Entry(const uint32_t layerWidth, const uint32_t layerHeight)
122 : width(OffscreenBuffer::computeIdealDimension(layerWidth))
123 , height(OffscreenBuffer::computeIdealDimension(layerHeight)) {}
124
125 Entry(OffscreenBuffer* layer)
126 : layer(layer)
John Reck38e0c322015-11-10 12:19:17 -0800127 , width(layer->texture.width())
128 , height(layer->texture.height()) {
Chris Craik9fded232015-11-11 16:42:34 -0800129 }
130
131 static int compare(const Entry& lhs, const Entry& rhs);
132
133 bool operator==(const Entry& other) const {
134 return compare(*this, other) == 0;
135 }
136
137 bool operator!=(const Entry& other) const {
138 return compare(*this, other) != 0;
139 }
140
141 bool operator<(const Entry& other) const {
142 return Entry::compare(*this, other) < 0;
143 }
144
145 OffscreenBuffer* layer = nullptr;
146 uint32_t width = 0;
147 uint32_t height = 0;
148 }; // struct Entry
149
150 std::multiset<Entry> mPool;
151
152 uint32_t mSize = 0;
153 uint32_t mMaxSize;
154}; // class OffscreenBufferCache
155
156}; // namespace uirenderer
157}; // namespace android
158
159#endif // ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H