ConsumerBase.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using Ryujinx.HLE.HOS.Services.SurfaceFlinger.Types;
  2. using System;
  3. namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
  4. {
  5. class ConsumerBase : IConsumerListener
  6. {
  7. public class Slot
  8. {
  9. public AndroidStrongPointer<GraphicBuffer> GraphicBuffer;
  10. public AndroidFence Fence;
  11. public ulong FrameNumber;
  12. public Slot()
  13. {
  14. GraphicBuffer = new AndroidStrongPointer<GraphicBuffer>();
  15. }
  16. }
  17. protected Slot[] Slots = new Slot[BufferSlotArray.NumBufferSlots];
  18. protected bool IsAbandoned;
  19. protected BufferQueueConsumer Consumer;
  20. protected readonly object Lock = new object();
  21. private IConsumerListener _listener;
  22. public ConsumerBase(BufferQueueConsumer consumer, bool controlledByApp, IConsumerListener listener)
  23. {
  24. for (int i = 0; i < Slots.Length; i++)
  25. {
  26. Slots[i] = new Slot();
  27. }
  28. IsAbandoned = false;
  29. Consumer = consumer;
  30. _listener = listener;
  31. Status connectStatus = consumer.Connect(this, controlledByApp);
  32. if (connectStatus != Status.Success)
  33. {
  34. throw new InvalidOperationException();
  35. }
  36. }
  37. public virtual void OnBuffersReleased()
  38. {
  39. lock (Lock)
  40. {
  41. if (IsAbandoned)
  42. {
  43. return;
  44. }
  45. Consumer.GetReleasedBuffers(out ulong slotMask);
  46. for (int i = 0; i < Slots.Length; i++)
  47. {
  48. if ((slotMask & (1UL << i)) != 0)
  49. {
  50. FreeBufferLocked(i);
  51. }
  52. }
  53. }
  54. }
  55. public virtual void OnFrameAvailable(ref BufferItem item)
  56. {
  57. _listener?.OnFrameAvailable(ref item);
  58. }
  59. public virtual void OnFrameReplaced(ref BufferItem item)
  60. {
  61. _listener?.OnFrameReplaced(ref item);
  62. }
  63. protected virtual void FreeBufferLocked(int slotIndex)
  64. {
  65. Slots[slotIndex].GraphicBuffer.Reset();
  66. Slots[slotIndex].Fence = AndroidFence.NoFence;
  67. Slots[slotIndex].FrameNumber = 0;
  68. }
  69. public void Abandon()
  70. {
  71. lock (Lock)
  72. {
  73. if (!IsAbandoned)
  74. {
  75. AbandonLocked();
  76. IsAbandoned = true;
  77. }
  78. }
  79. }
  80. protected virtual void AbandonLocked()
  81. {
  82. for (int i = 0; i < Slots.Length; i++)
  83. {
  84. FreeBufferLocked(i);
  85. }
  86. Consumer.Disconnect();
  87. }
  88. protected virtual Status AcquireBufferLocked(out BufferItem bufferItem, ulong expectedPresent)
  89. {
  90. Status acquireStatus = Consumer.AcquireBuffer(out bufferItem, expectedPresent);
  91. if (acquireStatus != Status.Success)
  92. {
  93. return acquireStatus;
  94. }
  95. if (!bufferItem.GraphicBuffer.IsNull)
  96. {
  97. Slots[bufferItem.Slot].GraphicBuffer.Set(bufferItem.GraphicBuffer.Object);
  98. }
  99. Slots[bufferItem.Slot].FrameNumber = bufferItem.FrameNumber;
  100. Slots[bufferItem.Slot].Fence = bufferItem.Fence;
  101. return Status.Success;
  102. }
  103. protected virtual Status AddReleaseFenceLocked(int slot, ref AndroidStrongPointer<GraphicBuffer> graphicBuffer, ref AndroidFence fence)
  104. {
  105. if (!StillTracking(slot, ref graphicBuffer))
  106. {
  107. return Status.Success;
  108. }
  109. Slots[slot].Fence = fence;
  110. return Status.Success;
  111. }
  112. protected virtual Status ReleaseBufferLocked(int slot, ref AndroidStrongPointer<GraphicBuffer> graphicBuffer)
  113. {
  114. if (!StillTracking(slot, ref graphicBuffer))
  115. {
  116. return Status.Success;
  117. }
  118. Status result = Consumer.ReleaseBuffer(slot, Slots[slot].FrameNumber, ref Slots[slot].Fence);
  119. if (result == Status.StaleBufferSlot)
  120. {
  121. FreeBufferLocked(slot);
  122. }
  123. Slots[slot].Fence = AndroidFence.NoFence;
  124. return result;
  125. }
  126. protected virtual bool StillTracking(int slotIndex, ref AndroidStrongPointer<GraphicBuffer> graphicBuffer)
  127. {
  128. if (slotIndex < 0 || slotIndex >= Slots.Length)
  129. {
  130. return false;
  131. }
  132. Slot slot = Slots[slotIndex];
  133. // TODO: Check this. On Android, this checks the "handle". I assume NvMapHandle is the handle, but it might not be.
  134. return !slot.GraphicBuffer.IsNull && slot.GraphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle == graphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle;
  135. }
  136. }
  137. }