OGLCachedResource.cs 4.4 KB

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