CounterQueueEvent.cs 2.3 KB

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