blob: d9422c9edd691bd503556a27f55b06d9e2928884 [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,
Romain Guy07ae5052017-06-13 18:25:32 -070046 uint32_t viewportWidth, uint32_t viewportHeight, bool wideColorGamut = false);
Chris Craik9fded232015-11-11 16:42:34 -080047 ~OffscreenBuffer();
48
Chris Craik7435eb12016-01-07 17:41:40 -080049 Rect getTextureCoordinates();
50
Chris Craik64db2bf2016-02-26 15:01:24 -080051 void dirty(Rect dirtyArea);
52
Chris Craik9fded232015-11-11 16:42:34 -080053 // must be called prior to rendering, to construct/update vertex buffer
54 void updateMeshFromRegion();
55
Chris Craik98787e62015-11-13 10:55:30 -080056 // Set by RenderNode for HW layers, TODO for clipped saveLayers
57 void setWindowTransform(const Matrix4& transform) {
58 inverseTransformInWindow.loadInverse(transform);
59 }
60
Chris Craik9fded232015-11-11 16:42:34 -080061 static uint32_t computeIdealDimension(uint32_t dimension);
62
John Reck38e0c322015-11-10 12:19:17 -080063 uint32_t getSizeInBytes() { return texture.objectSize(); }
Chris Craik9fded232015-11-11 16:42:34 -080064
65 RenderState& renderState;
Chris Craik98787e62015-11-13 10:55:30 -080066
Chris Craik9fded232015-11-11 16:42:34 -080067 uint32_t viewportWidth;
68 uint32_t viewportHeight;
69 Texture texture;
70
Romain Guy07ae5052017-06-13 18:25:32 -070071 bool wideColorGamut = false;
72
Chris Craik9fded232015-11-11 16:42:34 -080073 // Portion of layer that has been drawn to. Used to minimize drawing area when
74 // drawing back to screen / parent FBO.
75 Region region;
Chris Craik98787e62015-11-13 10:55:30 -080076
77 Matrix4 inverseTransformInWindow;
78
79 // vbo / size of mesh
Chris Craik9fded232015-11-11 16:42:34 -080080 GLsizei elementCount = 0;
81 GLuint vbo = 0;
Chris Craik37413282016-05-12 17:48:51 -070082
83 bool hasRenderedSinceRepaint;
Chris Craik9fded232015-11-11 16:42:34 -080084};
85
86/**
87 * Pool of OffscreenBuffers allocated, but not currently in use.
88 */
89class OffscreenBufferPool {
90public:
91 OffscreenBufferPool();
92 ~OffscreenBufferPool();
93
94 WARN_UNUSED_RESULT OffscreenBuffer* get(RenderState& renderState,
Romain Guy07ae5052017-06-13 18:25:32 -070095 const uint32_t width, const uint32_t height, bool wideColorGamut = false);
Chris Craik9fded232015-11-11 16:42:34 -080096
97 WARN_UNUSED_RESULT OffscreenBuffer* resize(OffscreenBuffer* layer,
98 const uint32_t width, const uint32_t height);
99
100 void putOrDelete(OffscreenBuffer* layer);
101
102 /**
103 * Clears the pool. This causes all layers to be deleted.
104 */
105 void clear();
106
107 /**
108 * Returns the maximum size of the pool in bytes.
109 */
110 uint32_t getMaxSize() { return mMaxSize; }
111
112 /**
113 * Returns the current size of the pool in bytes.
114 */
115 uint32_t getSize() { return mSize; }
116
117 size_t getCount() { return mPool.size(); }
118
119 /**
120 * Prints out the content of the pool.
121 */
122 void dump();
123private:
124 struct Entry {
125 Entry() {}
126
Romain Guy07ae5052017-06-13 18:25:32 -0700127 Entry(const uint32_t layerWidth, const uint32_t layerHeight, bool wideColorGamut)
Chris Craik9fded232015-11-11 16:42:34 -0800128 : width(OffscreenBuffer::computeIdealDimension(layerWidth))
Romain Guy07ae5052017-06-13 18:25:32 -0700129 , height(OffscreenBuffer::computeIdealDimension(layerHeight))
130 , wideColorGamut(wideColorGamut) {}
Chris Craik9fded232015-11-11 16:42:34 -0800131
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700132 explicit Entry(OffscreenBuffer* layer)
Chris Craik9fded232015-11-11 16:42:34 -0800133 : layer(layer)
John Reck38e0c322015-11-10 12:19:17 -0800134 , width(layer->texture.width())
Romain Guy07ae5052017-06-13 18:25:32 -0700135 , height(layer->texture.height())
136 , wideColorGamut(layer->wideColorGamut) {
Chris Craik9fded232015-11-11 16:42:34 -0800137 }
138
139 static int compare(const Entry& lhs, const Entry& rhs);
140
141 bool operator==(const Entry& other) const {
142 return compare(*this, other) == 0;
143 }
144
145 bool operator!=(const Entry& other) const {
146 return compare(*this, other) != 0;
147 }
148
149 bool operator<(const Entry& other) const {
150 return Entry::compare(*this, other) < 0;
151 }
152
153 OffscreenBuffer* layer = nullptr;
154 uint32_t width = 0;
155 uint32_t height = 0;
Romain Guy07ae5052017-06-13 18:25:32 -0700156 bool wideColorGamut = false;
Chris Craik9fded232015-11-11 16:42:34 -0800157 }; // struct Entry
158
159 std::multiset<Entry> mPool;
160
161 uint32_t mSize = 0;
162 uint32_t mMaxSize;
163}; // class OffscreenBufferCache
164
165}; // namespace uirenderer
166}; // namespace android
167
168#endif // ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H