/
FramebufferPool.uno
267 lines (219 loc) · 6.75 KB
/
FramebufferPool.uno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using Uno;
using Uno.Collections;
using Uno.Compiler.ExportTargetInterop;
using Uno.Graphics;
using Fuse.Resources;
namespace Fuse
{
public static class FramebufferPool
{
static FramebufferPoolImpl framebufferPool;
static void EnsurePool() { if (framebufferPool == null) framebufferPool = new FramebufferPoolImpl(); }
internal static void Register(CacheFramebuffer cfb)
{
EnsurePool();
framebufferPool.Register(cfb);
}
internal static void UnRegister(CacheFramebuffer cfb)
{
EnsurePool();
framebufferPool.UnRegister(cfb);
}
public static framebuffer Lock(int2 size, Format format, bool depth)
{
return Lock(size.X, size.Y, format, depth);
}
public static framebuffer Lock(int width, int height, Format format, bool depth)
{
EnsurePool();
return framebufferPool.Lock(width, height, format, depth);
}
public static void Release(framebuffer fb)
{
EnsurePool();
framebufferPool.Release(fb);
}
internal static int Frame
{
get
{
EnsurePool();
return framebufferPool.frame;
}
}
/** `true` if there are no framebuffers that still need to be released */
internal static bool TestIsLockedClean
{
get { return framebufferPool == null ? true : framebufferPool.IsLockedClean; }
}
}
[extern(ANDROID) Require("source.include", "uDroid/GLHelper.h")]
internal class FramebufferPoolImpl : ISoftDisposable
{
public FramebufferPoolImpl()
{
UpdateManager.AddAction(Update);
DisposalManager.Add(this);
}
internal int frame = 0;
// Holds framebuffers not currently in use and tracks how long they've been unused
List<framebuffer> framebufferPool = new List<framebuffer>();
Dictionary<framebuffer, int> lastFrameUsed = new Dictionary<framebuffer, int>();
// Holds framebuffers currenlty locked by Lock()
HashSet<framebuffer> lockedFramebuffers = new HashSet<framebuffer>();
List<CacheFramebuffer> cacheFramebuffers = new List<CacheFramebuffer>();
internal bool IsLockedClean
{
get { return lockedFramebuffers.Count == 0; }
}
// Finds a free buffer from the framebuffer pool
framebuffer FindBuffer(int width, int height, Uno.Graphics.Format format, FramebufferFlags flags)
{
width = Math.Max(1, width);
height = Math.Max(1, height);
for (int i = 0; i < framebufferPool.Count; i++)
{
var fb = framebufferPool[i];
if (fb.Size.X != width) continue;
if (fb.Size.Y != height) continue;
if (fb.Format != format) continue;
if (fb.HasDepth != flags.HasFlag(FramebufferFlags.DepthBuffer)) continue;
if (fb.ColorBuffer.IsMipmap != flags.HasFlag(FramebufferFlags.Mipmap)) continue;
framebufferPool.RemoveAt(i);
lockedFramebuffers.Add(fb);
lastFrameUsed[fb] = frame;
return fb;
}
int maxSize = texture2D.MaxSize;
if (width > maxSize || height > maxSize)
{
throw new Exception("Attempted to allocate " + width + "x" + height + " framebuffer, max is " + maxSize + "x" + maxSize);
}
extern double t = 0.0;
if defined(FUSELIBS_PROFILING)
t = Uno.Diagnostics.Clock.GetSeconds();
var buffer = new framebuffer(int2(width, height), format, flags);
if defined(FUSELIBS_PROFILING)
Fuse.Profiling.NewFramebuffer(buffer, Uno.Diagnostics.Clock.GetSeconds() - t);
return buffer;
}
int framebuffersProvidedSinceLastCollect = 0;
int pixelsProvidedSinceLastCollect = 0;
internal void Register(CacheFramebuffer cfb)
{
framebuffersProvidedSinceLastCollect += 1;
pixelsProvidedSinceLastCollect += cfb.Width * cfb.Height;
cacheFramebuffers.Add(cfb);
if (pixelsProvidedSinceLastCollect > 1000000)
{
CollectCacheFramebuffers();
}
else if (framebuffersProvidedSinceLastCollect > 50)
{
CollectCacheFramebuffers();
}
}
internal void UnRegister(CacheFramebuffer cfb)
{
framebuffersProvidedSinceLastCollect = 0;
//pixelsProvidedSinceLastCollect -= cfb.Width * cfb.Height;
cacheFramebuffers.Remove(cfb);
}
void CollectCacheFramebuffers()
{
if (cacheFramebuffers.Count < 3) return;
// Calculate average time since last use
int sum = 0;
foreach (var cfb in cacheFramebuffers)
{
sum += cfb.FramesSinceLastUse;
}
int avg = sum / cacheFramebuffers.Count;
// Collect all buffers older than 3 frames more than the average
int limit = avg + 3;
for (int i = 0; i < cacheFramebuffers.Count; i++)
{
var c = cacheFramebuffers[i];
if (!c.IsPinned && c.FramesSinceLastUse >= limit)
{
c.Collect();
cacheFramebuffers.RemoveAt(i--);
}
}
framebuffersProvidedSinceLastCollect = 0;
pixelsProvidedSinceLastCollect = 0;
}
void ISoftDisposable.SoftDispose()
{
for (int i = 0; i < cacheFramebuffers.Count; i++)
{
var c = cacheFramebuffers[i];
if (c.IsPinned)
throw new Exception("framebuffer pinned while app going into the background");
c.Collect();
cacheFramebuffers.RemoveAt(i--);
}
for (int i = 0; i < framebufferPool.Count; i++)
{
var fb = framebufferPool[i];
fb.Dispose();
framebufferPool.RemoveAt(i--);
lastFrameUsed.Remove(fb);
}
}
internal framebuffer Lock(int width, int height, Uno.Graphics.Format format, bool depth)
{
var fb = FindBuffer(width, height, format, depth ? FramebufferFlags.DepthBuffer : FramebufferFlags.None);
lastFrameUsed[fb] = frame;
lockedFramebuffers.Add(fb);
return fb;
}
internal void Release(framebuffer fb)
{
if (lockedFramebuffers.Contains(fb))
{
lockedFramebuffers.Remove(fb);
lastFrameUsed[fb] = frame;
framebufferPool.Add(fb);
}
}
public void Update()
{
extern bool contextBound = false;
frame++;
for (int i = 0; i < framebufferPool.Count; i++)
{
var fb = framebufferPool[i];
//var framesSinceUse = frame - lastFrameUsed[fb];
// Note: Workaround for mobile rotation bug
int framesSinceUse;
lastFrameUsed.TryGetValue(fb, out framesSinceUse);
framesSinceUse = frame - framesSinceUse;
if (framesSinceUse < 0)
throw new Exception("Pool is leaking");
if (framesSinceUse > 1)
{
// We've had issues on Android with getting here without an OpenGL ES context bound,
// so let's make sure we have a context bound before calling fb.Dispose, which is
// releasing textures.
//
// This is still not quite correct, though. Because framebuffer objects are not shared
// on all devices, the FBO should have been released from the context that was used
// to create it. And that's not the background-surface.
if defined(Android)
{
if (!contextBound)
{
extern "GLHelper::SwapBackToBackgroundSurface()";
contextBound = true;
}
}
//debug_log "disposing buffer " + fb.Size.X + "x" + fb.Size.Y;
fb.Dispose();
framebufferPool.RemoveAt(i--);
lastFrameUsed.Remove(fb);
}
}
}
}
}