OGLCachedResource.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. public OGLCachedResource(DeleteValue DeleteValueCallback)
  28. {
  29. if (DeleteValueCallback == null)
  30. {
  31. throw new ArgumentNullException(nameof(DeleteValueCallback));
  32. }
  33. this.DeleteValueCallback = DeleteValueCallback;
  34. Cache = new Dictionary<long, CacheBucket>();
  35. SortedCache = new LinkedList<long>();
  36. }
  37. public void AddOrUpdate(long Key, T Value, long Size)
  38. {
  39. ClearCacheIfNeeded();
  40. LinkedListNode<long> Node = SortedCache.AddLast(Key);
  41. CacheBucket NewBucket = new CacheBucket(Value, Size, Node);
  42. if (Cache.TryGetValue(Key, out CacheBucket Bucket))
  43. {
  44. DeleteValueCallback(Bucket.Value);
  45. SortedCache.Remove(Bucket.Node);
  46. Cache[Key] = NewBucket;
  47. }
  48. else
  49. {
  50. Cache.Add(Key, NewBucket);
  51. }
  52. }
  53. public bool TryGetValue(long Key, out T Value)
  54. {
  55. if (Cache.TryGetValue(Key, out CacheBucket Bucket))
  56. {
  57. Value = Bucket.Value;
  58. return true;
  59. }
  60. Value = default(T);
  61. return false;
  62. }
  63. public bool TryGetSize(long Key, out long Size)
  64. {
  65. if (Cache.TryGetValue(Key, out CacheBucket Bucket))
  66. {
  67. Size = Bucket.DataSize;
  68. return true;
  69. }
  70. Size = 0;
  71. return false;
  72. }
  73. private void ClearCacheIfNeeded()
  74. {
  75. int Timestamp = Environment.TickCount;
  76. int Count = 0;
  77. while (Count++ < MaxRemovalsPerRun)
  78. {
  79. LinkedListNode<long> Node = SortedCache.First;
  80. if (Node == null)
  81. {
  82. break;
  83. }
  84. CacheBucket Bucket = Cache[Node.Value];
  85. int TimeDelta = RingDelta(Bucket.Timestamp, Timestamp);
  86. if ((uint)TimeDelta <= (uint)MaxTimeDelta)
  87. {
  88. break;
  89. }
  90. SortedCache.Remove(Node);
  91. Cache.Remove(Node.Value);
  92. DeleteValueCallback(Bucket.Value);
  93. }
  94. }
  95. private int RingDelta(int Old, int New)
  96. {
  97. if ((uint)New < (uint)Old)
  98. {
  99. return New + (~Old + 1);
  100. }
  101. else
  102. {
  103. return New - Old;
  104. }
  105. }
  106. }
  107. }