Photo.Unload() is now async

This commit is contained in:
Colin McMillen 2023-07-25 22:00:43 -04:00
parent b0fd20cd89
commit 32cb03ab15

View File

@ -187,9 +187,10 @@ public class Photo {
image = tmp;
}
public void Unload() {
public async void UnloadAsync() {
Loaded = false;
if (texture != placeholder) {
texture.Dispose();
await Task.Run( () => { texture.Dispose(); });
texture = placeholder;
}
}
@ -703,19 +704,15 @@ public class Game : GameWindow {
int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1);
// First, unload images that are far outside our window.
// FIXME: also cancel any in-progress loading tasks that have moved outside our window.
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// FIXME: maybe use an LRU cache for evicting images?
// FIXME: keep around thumbnail-sized textures?
foreach (int i in loadedImages) {
if (i < minUnloadedImage || i > maxUnloadedImage) {
loadedImages.Remove(i);
photos[i].Unload();
// Console.WriteLine("unloading " + i);
loadedImages.Remove(i);
photos[i].UnloadAsync();
}
}
stopwatch.Stop();
if (stopwatch.Elapsed.TotalMilliseconds > 1) {
Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds);
}
// Then, start loading any images that aren't in our window.
for (int i = minLoadedImage; i <= maxLoadedImage; i++) {