OGLCachedResource.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using Ryujinx.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Gal.OpenGL
  5. {
  6. class OGLCachedResource<T>
  7. {
  8. public delegate void DeleteValue(T Value);
  9. private const int MaxTimeDelta = 5 * 60000;
  10. private const int MaxRemovalsPerRun = 10;
  11. private struct CacheBucket
  12. {
  13. public T Value { get; private set; }
  14. public LinkedListNode<long> Node { get; private set; }
  15. public long DataSize { get; private set; }
  16. public long Timestamp { get; private set; }
  17. public CacheBucket(T Value, long DataSize, LinkedListNode<long> Node)
  18. {
  19. this.Value = Value;
  20. this.DataSize = DataSize;
  21. this.Node = Node;
  22. Timestamp = PerformanceCounter.ElapsedMilliseconds;
  23. }
  24. }
  25. private Dictionary<long, CacheBucket> Cache;
  26. private LinkedList<long> SortedCache;
  27. private DeleteValue DeleteValueCallback;
  28. private Queue<T> DeletePending;
  29. private bool Locked;
  30. public OGLCachedResource(DeleteValue DeleteValueCallback)
  31. {
  32. if (DeleteValueCallback == null)
  33. {
  34. throw new ArgumentNullException(nameof(DeleteValueCallback));
  35. }
  36. this.DeleteValueCallback = DeleteValueCallback;
  37. Cache = new Dictionary<long, CacheBucket>();
  38. SortedCache = new LinkedList<long>();
  39. DeletePending = new Queue<T>();
  40. }
  41. public void Lock()
  42. {
  43. Locked = true;
  44. }
  45. public void Unlock()
  46. {
  47. Locked = false;
  48. while (DeletePending.TryDequeue(out T Value))
  49. {
  50. DeleteValueCallback(Value);
  51. }
  52. ClearCacheIfNeeded();
  53. }
  54. public void AddOrUpdate(long Key, T Value, long Size)
  55. {
  56. if (!Locked)
  57. {
  58. ClearCacheIfNeeded();
  59. }
  60. LinkedListNode<long> Node = SortedCache.AddLast(Key);
  61. CacheBucket NewBucket = new CacheBucket(Value, Size, Node);
  62. if (Cache.TryGetValue(Key, out CacheBucket Bucket))
  63. {
  64. if (Locked)
  65. {
  66. DeletePending.Enqueue(Bucket.Value);
  67. }
  68. else
  69. {
  70. DeleteValueCallback(Bucket.Value);
  71. }
  72. SortedCache.Remove(Bucket.Node);
  73. Cache[Key] = NewBucket;
  74. }
  75. else
  76. {
  77. Cache.Add(Key, NewBucket);
  78. }
  79. }
  80. public bool TryGetValue(long Key, out T Value)
  81. {
  82. if (Cache.TryGetValue(Key, out CacheBucket Bucket))
  83. {
  84. Value = Bucket.Value;
  85. SortedCache.Remove(Bucket.Node);
  86. LinkedListNode<long> Node = SortedCache.AddLast(Key);
  87. Cache[Key] = new CacheBucket(Value, Bucket.DataSize, Node);
  88. return true;
  89. }
  90. Value = default(T);
  91. return false;
  92. }
  93. public bool TryGetSize(long Key, out long Size)
  94. {
  95. if (Cache.TryGetValue(Key, out CacheBucket Bucket))
  96. {
  97. Size = Bucket.DataSize;
  98. return true;
  99. }
  100. Size = 0;
  101. return false;
  102. }
  103. private void ClearCacheIfNeeded()
  104. {
  105. long Timestamp = PerformanceCounter.ElapsedMilliseconds;
  106. int Count = 0;
  107. while (Count++ < MaxRemovalsPerRun)
  108. {
  109. LinkedListNode<long> Node = SortedCache.First;
  110. if (Node == null)
  111. {
  112. break;
  113. }
  114. CacheBucket Bucket = Cache[Node.Value];
  115. long TimeDelta = Timestamp - Bucket.Timestamp;
  116. if ((uint)TimeDelta <= (uint)MaxTimeDelta)
  117. {
  118. break;
  119. }
  120. SortedCache.Remove(Node);
  121. Cache.Remove(Node.Value);
  122. DeleteValueCallback(Bucket.Value);
  123. }
  124. }
  125. }
  126. }