introduce loadingImages & add a lock around image load/unload state
This commit is contained in:
parent
ad598e68a5
commit
33f6ee739e
51
Program.cs
51
Program.cs
@ -571,6 +571,8 @@ public class Game : GameWindow {
|
|||||||
List<Photo> allPhotos = new();
|
List<Photo> allPhotos = new();
|
||||||
List<Photo> photos = new();
|
List<Photo> photos = new();
|
||||||
HashSet<Photo> loadedImages = new();
|
HashSet<Photo> loadedImages = new();
|
||||||
|
HashSet<Photo> loadingImages = new();
|
||||||
|
readonly object loadedImagesLock = new();
|
||||||
int photoIndex = 0;
|
int photoIndex = 0;
|
||||||
int ribbonIndex = 0;
|
int ribbonIndex = 0;
|
||||||
Shader shader = new();
|
Shader shader = new();
|
||||||
@ -777,10 +779,10 @@ public class Game : GameWindow {
|
|||||||
|
|
||||||
// Load photos from a directory.
|
// Load photos from a directory.
|
||||||
// string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\");
|
// string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\");
|
||||||
string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\");
|
// string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\");
|
||||||
// string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\");
|
// string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\");
|
||||||
// string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23");
|
// string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23");
|
||||||
// string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000");
|
string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000");
|
||||||
// string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\");
|
// string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\");
|
||||||
|
|
||||||
for (int i = 0; i < files.Count(); i++) {
|
for (int i = 0; i < files.Count(); i++) {
|
||||||
@ -809,12 +811,10 @@ public class Game : GameWindow {
|
|||||||
base.OnUnload();
|
base.OnUnload();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void LoadAndUnloadImagesAsync() {
|
private void UnloadImages() {
|
||||||
int minLoadedImage = Math.Max(0, photoIndex - 20);
|
// Unload images that haven't been touched in a while.
|
||||||
int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1);
|
|
||||||
// First, unload images that haven't been touched in a while.
|
|
||||||
// FIXME: also cancel any of these if they still have an in-progress loading task?
|
|
||||||
// FIXME: keep around thumbnail-sized textures?
|
// FIXME: keep around thumbnail-sized textures?
|
||||||
|
lock (loadedImagesLock) {
|
||||||
while (loadedImages.Count > 60) {
|
while (loadedImages.Count > 60) {
|
||||||
long earliestTime = long.MaxValue;
|
long earliestTime = long.MaxValue;
|
||||||
Photo? earliest = null;
|
Photo? earliest = null;
|
||||||
@ -825,27 +825,48 @@ public class Game : GameWindow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (earliest != null) {
|
if (earliest != null) {
|
||||||
Console.WriteLine($"loadedImages.Count: {loadedImages.Count}, evicting {earliest.Filename} @ {earliestTime}");
|
// Console.WriteLine($"loadedImages.Count: {loadedImages.Count}, evicting {earliest.Filename} @ {earliestTime}");
|
||||||
// TODO: we have to free textures on the GL thread, but could we do that async'ly to keep the UI responsive?
|
// TODO: we have to free textures on the GL thread, but could we do that async'ly to keep the UI responsive?
|
||||||
earliest.Unload();
|
earliest.Unload();
|
||||||
loadedImages.Remove(earliest);
|
loadedImages.Remove(earliest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Then, start loading any images that are in our window but not yet loaded.
|
|
||||||
for (int i = minLoadedImage; i <= maxLoadedImage; i++) {
|
|
||||||
if (!loadedImages.Contains(photos[i])) {
|
|
||||||
Console.WriteLine("loading " + i);
|
|
||||||
loadedImages.Add(photos[i]);
|
|
||||||
await Task.Run( () => { photos[i].LoadAsync(); });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void LoadImagesAsync() {
|
||||||
|
foreach (Photo p in loadingImages) {
|
||||||
|
if (p.Loaded) {
|
||||||
|
lock (loadedImagesLock) {
|
||||||
|
loadedImages.Add(p);
|
||||||
|
loadingImages.Remove(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Start loading any images that are in our window but not yet loaded.
|
||||||
|
int minLoadedImage = Math.Max(0, photoIndex - 20);
|
||||||
|
int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1);
|
||||||
|
List<Photo> toLoad = new();
|
||||||
|
for (int i = minLoadedImage; i <= maxLoadedImage; i++) {
|
||||||
|
lock (loadedImagesLock) {
|
||||||
|
if (!loadedImages.Contains(photos[i]) && !loadingImages.Contains(photos[i])) {
|
||||||
|
Console.WriteLine("loading " + i);
|
||||||
|
loadingImages.Add(photos[i]);
|
||||||
|
toLoad.Add(photos[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (Photo p in toLoad) {
|
||||||
|
await Task.Run( () => { p.LoadAsync(); });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnRenderFrame(FrameEventArgs e) {
|
protected override void OnRenderFrame(FrameEventArgs e) {
|
||||||
base.OnRenderFrame(e);
|
base.OnRenderFrame(e);
|
||||||
fpsCounter.Update();
|
fpsCounter.Update();
|
||||||
|
|
||||||
LoadAndUnloadImagesAsync();
|
UnloadImages();
|
||||||
|
LoadImagesAsync();
|
||||||
|
|
||||||
GL.Clear(ClearBufferMask.ColorBufferBit);
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
||||||
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
|
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
|
||||||
|
Loading…
Reference in New Issue
Block a user