CounterQueueEvent.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. using System.Threading;
  5. namespace Ryujinx.Graphics.OpenGL.Queries
  6. {
  7. class CounterQueueEvent : ICounterEvent
  8. {
  9. public event EventHandler<ulong> OnResult;
  10. public QueryTarget Type { get; }
  11. public bool ClearCounter { get; private set; }
  12. public int Query => _counter.Query;
  13. public bool Disposed { get; private set; }
  14. public bool Invalid { get; set; }
  15. public ulong DrawIndex { get; }
  16. private CounterQueue _queue;
  17. private BufferedQuery _counter;
  18. private bool _hostAccessReserved = false;
  19. private int _refCount = 1; // Starts with a reference from the counter queue.
  20. private object _lock = new object();
  21. private ulong _result = ulong.MaxValue;
  22. public CounterQueueEvent(CounterQueue queue, QueryTarget type, ulong drawIndex)
  23. {
  24. _queue = queue;
  25. _counter = queue.GetQueryObject();
  26. Type = type;
  27. DrawIndex = drawIndex;
  28. _counter.Begin();
  29. }
  30. internal void Clear()
  31. {
  32. _counter.Reset();
  33. ClearCounter = true;
  34. }
  35. internal void Complete(bool withResult)
  36. {
  37. _counter.End(withResult);
  38. }
  39. internal bool TryConsume(ref ulong result, bool block, AutoResetEvent wakeSignal = null)
  40. {
  41. lock (_lock)
  42. {
  43. if (Disposed)
  44. {
  45. return true;
  46. }
  47. if (ClearCounter || Type == QueryTarget.Timestamp)
  48. {
  49. result = 0;
  50. }
  51. long queryResult;
  52. if (block)
  53. {
  54. queryResult = _counter.AwaitResult(wakeSignal);
  55. }
  56. else
  57. {
  58. if (!_counter.TryGetResult(out queryResult))
  59. {
  60. return false;
  61. }
  62. }
  63. result += (ulong)queryResult;
  64. _result = result;
  65. OnResult?.Invoke(this, result);
  66. Dispose(); // Return the our resources to the pool.
  67. return true;
  68. }
  69. }
  70. public void Flush()
  71. {
  72. if (Disposed)
  73. {
  74. return;
  75. }
  76. // Tell the queue to process all events up to this one.
  77. _queue.FlushTo(this);
  78. }
  79. public void DecrementRefCount()
  80. {
  81. if (Interlocked.Decrement(ref _refCount) == 0)
  82. {
  83. DisposeInternal();
  84. }
  85. }
  86. public bool ReserveForHostAccess()
  87. {
  88. if (_hostAccessReserved)
  89. {
  90. return true;
  91. }
  92. if (IsValueAvailable())
  93. {
  94. return false;
  95. }
  96. if (Interlocked.Increment(ref _refCount) == 1)
  97. {
  98. Interlocked.Decrement(ref _refCount);
  99. return false;
  100. }
  101. _hostAccessReserved = true;
  102. return true;
  103. }
  104. public void ReleaseHostAccess()
  105. {
  106. _hostAccessReserved = false;
  107. DecrementRefCount();
  108. }
  109. private void DisposeInternal()
  110. {
  111. _queue.ReturnQueryObject(_counter);
  112. }
  113. private bool IsValueAvailable()
  114. {
  115. return _result != ulong.MaxValue || _counter.TryGetResult(out _);
  116. }
  117. public void Dispose()
  118. {
  119. Disposed = true;
  120. DecrementRefCount();
  121. }
  122. }
  123. }