AutoDeleteCache.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Gpu.Image
  4. {
  5. class AutoDeleteCache : IEnumerable<Texture>
  6. {
  7. private const int MaxCapacity = 2048;
  8. private LinkedList<Texture> _textures;
  9. public AutoDeleteCache()
  10. {
  11. _textures = new LinkedList<Texture>();
  12. }
  13. public void Add(Texture texture)
  14. {
  15. texture.IncrementReferenceCount();
  16. texture.CacheNode = _textures.AddLast(texture);
  17. if (_textures.Count > MaxCapacity)
  18. {
  19. Texture oldestTexture = _textures.First.Value;
  20. _textures.RemoveFirst();
  21. oldestTexture.DecrementReferenceCount();
  22. oldestTexture.CacheNode = null;
  23. }
  24. }
  25. public void Lift(Texture texture)
  26. {
  27. if (texture.CacheNode != null)
  28. {
  29. if (texture.CacheNode != _textures.Last)
  30. {
  31. _textures.Remove(texture.CacheNode);
  32. texture.CacheNode = _textures.AddLast(texture);
  33. }
  34. }
  35. else
  36. {
  37. Add(texture);
  38. }
  39. }
  40. public IEnumerator<Texture> GetEnumerator()
  41. {
  42. return _textures.GetEnumerator();
  43. }
  44. IEnumerator IEnumerable.GetEnumerator()
  45. {
  46. return _textures.GetEnumerator();
  47. }
  48. }
  49. }