AutoDeleteCache.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Collections;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Gpu.Image
  5. {
  6. /// <summary>
  7. /// A texture cache that automatically removes older textures that are not used for some time.
  8. /// The cache works with a rotated list with a fixed size. When new textures are added, the
  9. /// old ones at the bottom of the list are deleted.
  10. /// </summary>
  11. class AutoDeleteCache : IEnumerable<Texture>
  12. {
  13. private const int MaxCapacity = 2048;
  14. private readonly LinkedList<Texture> _textures;
  15. private readonly ConcurrentQueue<Texture> _deferredRemovals;
  16. /// <summary>
  17. /// Creates a new instance of the automatic deletion cache.
  18. /// </summary>
  19. public AutoDeleteCache()
  20. {
  21. _textures = new LinkedList<Texture>();
  22. _deferredRemovals = new ConcurrentQueue<Texture>();
  23. }
  24. /// <summary>
  25. /// Adds a new texture to the cache, even if the texture added is already on the cache.
  26. /// </summary>
  27. /// <remarks>
  28. /// Using this method is only recommended if you know that the texture is not yet on the cache,
  29. /// otherwise it would store the same texture more than once.
  30. /// </remarks>
  31. /// <param name="texture">The texture to be added to the cache</param>
  32. public void Add(Texture texture)
  33. {
  34. texture.IncrementReferenceCount();
  35. texture.CacheNode = _textures.AddLast(texture);
  36. if (_textures.Count > MaxCapacity)
  37. {
  38. Texture oldestTexture = _textures.First.Value;
  39. if (!oldestTexture.CheckModified(false))
  40. {
  41. // The texture must be flushed if it falls out of the auto delete cache.
  42. // Flushes out of the auto delete cache do not trigger write tracking,
  43. // as it is expected that other overlapping textures exist that have more up-to-date contents.
  44. oldestTexture.Group.SynchronizeDependents(oldestTexture);
  45. oldestTexture.FlushModified(false);
  46. }
  47. _textures.RemoveFirst();
  48. oldestTexture.DecrementReferenceCount();
  49. oldestTexture.CacheNode = null;
  50. }
  51. if (_deferredRemovals.Count > 0)
  52. {
  53. while (_deferredRemovals.TryDequeue(out Texture textureToRemove))
  54. {
  55. Remove(textureToRemove, false);
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Adds a new texture to the cache, or just moves it to the top of the list if the
  61. /// texture is already on the cache.
  62. /// </summary>
  63. /// <remarks>
  64. /// Moving the texture to the top of the list prevents it from being deleted,
  65. /// as the textures on the bottom of the list are deleted when new ones are added.
  66. /// </remarks>
  67. /// <param name="texture">The texture to be added, or moved to the top</param>
  68. public void Lift(Texture texture)
  69. {
  70. if (texture.CacheNode != null)
  71. {
  72. if (texture.CacheNode != _textures.Last)
  73. {
  74. _textures.Remove(texture.CacheNode);
  75. texture.CacheNode = _textures.AddLast(texture);
  76. }
  77. }
  78. else
  79. {
  80. Add(texture);
  81. }
  82. }
  83. /// <summary>
  84. /// Removes a texture from the cache.
  85. /// </summary>
  86. /// <param name="texture">The texture to be removed from the cache</param>
  87. /// <param name="flush">True to remove the texture if it was on the cache</param>
  88. /// <returns>True if the texture was found and removed, false otherwise</returns>
  89. public bool Remove(Texture texture, bool flush)
  90. {
  91. if (texture.CacheNode == null)
  92. {
  93. return false;
  94. }
  95. // Remove our reference to this texture.
  96. if (flush)
  97. {
  98. texture.FlushModified(false);
  99. }
  100. _textures.Remove(texture.CacheNode);
  101. texture.CacheNode = null;
  102. return texture.DecrementReferenceCount();
  103. }
  104. /// <summary>
  105. /// Queues removal of a texture from the cache in a thread safe way.
  106. /// </summary>
  107. /// <param name="texture">The texture to be removed from the cache</param>
  108. public void RemoveDeferred(Texture texture)
  109. {
  110. _deferredRemovals.Enqueue(texture);
  111. }
  112. public IEnumerator<Texture> GetEnumerator()
  113. {
  114. return _textures.GetEnumerator();
  115. }
  116. IEnumerator IEnumerable.GetEnumerator()
  117. {
  118. return _textures.GetEnumerator();
  119. }
  120. }
  121. }