CounterQueueEvent.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 object _lock = new object();
  19. public CounterQueueEvent(CounterQueue queue, QueryTarget type, ulong drawIndex)
  20. {
  21. _queue = queue;
  22. _counter = queue.GetQueryObject();
  23. Type = type;
  24. DrawIndex = drawIndex;
  25. _counter.Begin();
  26. }
  27. internal void Clear()
  28. {
  29. _counter.Reset();
  30. ClearCounter = true;
  31. }
  32. internal void Complete(bool withResult)
  33. {
  34. _counter.End(withResult);
  35. }
  36. internal bool TryConsume(ref ulong result, bool block, AutoResetEvent wakeSignal = null)
  37. {
  38. lock (_lock)
  39. {
  40. if (Disposed)
  41. {
  42. return true;
  43. }
  44. if (ClearCounter || Type == QueryTarget.Timestamp)
  45. {
  46. result = 0;
  47. }
  48. long queryResult;
  49. if (block)
  50. {
  51. queryResult = _counter.AwaitResult(wakeSignal);
  52. }
  53. else
  54. {
  55. if (!_counter.TryGetResult(out queryResult))
  56. {
  57. return false;
  58. }
  59. }
  60. result += (ulong)queryResult;
  61. OnResult?.Invoke(this, result);
  62. Dispose(); // Return the our resources to the pool.
  63. return true;
  64. }
  65. }
  66. public void Flush()
  67. {
  68. if (Disposed)
  69. {
  70. return;
  71. }
  72. // Tell the queue to process all events up to this one.
  73. _queue.FlushTo(this);
  74. }
  75. public void Dispose()
  76. {
  77. Disposed = true;
  78. _queue.ReturnQueryObject(_counter);
  79. }
  80. }
  81. }