BufferQueueCore.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Kernel.Process;
  3. using Ryujinx.HLE.HOS.Kernel.Threading;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
  8. {
  9. class BufferQueueCore
  10. {
  11. public BufferSlotArray Slots;
  12. public int OverrideMaxBufferCount;
  13. public bool UseAsyncBuffer;
  14. public bool DequeueBufferCannotBlock;
  15. public PixelFormat DefaultBufferFormat;
  16. public int DefaultWidth;
  17. public int DefaultHeight;
  18. public int DefaultMaxBufferCount;
  19. public int MaxAcquiredBufferCount;
  20. public bool BufferHasBeenQueued;
  21. public ulong FrameCounter;
  22. public NativeWindowTransform TransformHint;
  23. public bool IsAbandoned;
  24. public NativeWindowApi ConnectedApi;
  25. public bool IsAllocating;
  26. public IProducerListener ProducerListener;
  27. public IConsumerListener ConsumerListener;
  28. public bool ConsumerControlledByApp;
  29. public uint ConsumerUsageBits;
  30. public List<BufferItem> Queue;
  31. public readonly object Lock = new object();
  32. private KEvent _waitBufferFreeEvent;
  33. private KEvent _frameAvailableEvent;
  34. public KProcess Owner { get; }
  35. public BufferQueueCore(Switch device, KProcess process)
  36. {
  37. Slots = new BufferSlotArray();
  38. IsAbandoned = false;
  39. OverrideMaxBufferCount = 0;
  40. DequeueBufferCannotBlock = false;
  41. UseAsyncBuffer = false;
  42. DefaultWidth = 1;
  43. DefaultHeight = 1;
  44. DefaultMaxBufferCount = 2;
  45. MaxAcquiredBufferCount = 1;
  46. FrameCounter = 0;
  47. TransformHint = 0;
  48. DefaultBufferFormat = PixelFormat.Rgba8888;
  49. IsAllocating = false;
  50. ProducerListener = null;
  51. ConsumerListener = null;
  52. ConsumerUsageBits = 0;
  53. Queue = new List<BufferItem>();
  54. // TODO: CreateGraphicBufferAlloc?
  55. _waitBufferFreeEvent = new KEvent(device.System);
  56. _frameAvailableEvent = new KEvent(device.System);
  57. Owner = process;
  58. }
  59. public int GetMinUndequeuedBufferCountLocked(bool async)
  60. {
  61. if (!UseAsyncBuffer)
  62. {
  63. return 0;
  64. }
  65. if (DequeueBufferCannotBlock || async)
  66. {
  67. return MaxAcquiredBufferCount + 1;
  68. }
  69. return MaxAcquiredBufferCount;
  70. }
  71. public int GetMinMaxBufferCountLocked(bool async)
  72. {
  73. return GetMinUndequeuedBufferCountLocked(async);
  74. }
  75. public int GetMaxBufferCountLocked(bool async)
  76. {
  77. int minMaxBufferCount = GetMinMaxBufferCountLocked(async);
  78. int maxBufferCount = Math.Max(DefaultMaxBufferCount, minMaxBufferCount);
  79. if (OverrideMaxBufferCount != 0)
  80. {
  81. maxBufferCount = OverrideMaxBufferCount;
  82. }
  83. // Preserve all buffers already in control of the producer and the consumer.
  84. for (int slot = maxBufferCount; slot < Slots.Length; slot++)
  85. {
  86. BufferState state = Slots[slot].BufferState;
  87. if (state == BufferState.Queued || state == BufferState.Dequeued)
  88. {
  89. maxBufferCount = slot + 1;
  90. }
  91. }
  92. return maxBufferCount;
  93. }
  94. public Status SetDefaultMaxBufferCountLocked(int count)
  95. {
  96. int minBufferCount = UseAsyncBuffer ? 2 : 1;
  97. if (count < minBufferCount || count > Slots.Length)
  98. {
  99. return Status.BadValue;
  100. }
  101. DefaultMaxBufferCount = count;
  102. SignalDequeueEvent();
  103. return Status.Success;
  104. }
  105. public void SignalWaitBufferFreeEvent()
  106. {
  107. _waitBufferFreeEvent.WritableEvent.Signal();
  108. }
  109. public void SignalFrameAvailableEvent()
  110. {
  111. _frameAvailableEvent.WritableEvent.Signal();
  112. }
  113. // TODO: Find an accurate way to handle a regular condvar here as this will wake up unwanted threads in some edge cases.
  114. public void SignalDequeueEvent()
  115. {
  116. Monitor.PulseAll(Lock);
  117. }
  118. public void WaitDequeueEvent()
  119. {
  120. Monitor.Wait(Lock);
  121. }
  122. public void SignalIsAbandonedEvent()
  123. {
  124. Monitor.PulseAll(Lock);
  125. }
  126. public void WaitIsAbandonedEvent()
  127. {
  128. Monitor.Wait(Lock);
  129. }
  130. public void FreeBufferLocked(int slot)
  131. {
  132. Slots[slot].GraphicBuffer.Reset();
  133. if (Slots[slot].BufferState == BufferState.Acquired)
  134. {
  135. Slots[slot].NeedsCleanupOnRelease = true;
  136. }
  137. Slots[slot].BufferState = BufferState.Free;
  138. Slots[slot].FrameNumber = uint.MaxValue;
  139. Slots[slot].AcquireCalled = false;
  140. Slots[slot].Fence.FenceCount = 0;
  141. }
  142. public void FreeAllBuffersLocked()
  143. {
  144. BufferHasBeenQueued = false;
  145. for (int slot = 0; slot < Slots.Length; slot++)
  146. {
  147. FreeBufferLocked(slot);
  148. }
  149. }
  150. public bool StillTracking(ref BufferItem item)
  151. {
  152. BufferSlot slot = Slots[item.Slot];
  153. // TODO: Check this. On Android, this checks the "handle". I assume NvMapHandle is the handle, but it might not be.
  154. return !slot.GraphicBuffer.IsNull && slot.GraphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle == item.GraphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle;
  155. }
  156. public void WaitWhileAllocatingLocked()
  157. {
  158. while (IsAbandoned)
  159. {
  160. WaitIsAbandonedEvent();
  161. }
  162. }
  163. public void CheckSystemEventsLocked(int maxBufferCount)
  164. {
  165. bool needBufferReleaseSignal = false;
  166. bool needFrameAvailableSignal = false;
  167. if (maxBufferCount > 1)
  168. {
  169. for (int i = 0; i < maxBufferCount; i++)
  170. {
  171. if (Slots[i].BufferState == BufferState.Queued)
  172. {
  173. needFrameAvailableSignal = true;
  174. }
  175. else if (Slots[i].BufferState == BufferState.Free)
  176. {
  177. needBufferReleaseSignal = true;
  178. }
  179. }
  180. }
  181. if (needBufferReleaseSignal)
  182. {
  183. SignalWaitBufferFreeEvent();
  184. }
  185. else
  186. {
  187. _waitBufferFreeEvent.WritableEvent.Clear();
  188. }
  189. if (needFrameAvailableSignal)
  190. {
  191. SignalFrameAvailableEvent();
  192. }
  193. else
  194. {
  195. _frameAvailableEvent.WritableEvent.Clear();
  196. }
  197. }
  198. public bool IsProducerConnectedLocked()
  199. {
  200. return ConnectedApi != NativeWindowApi.NoApi;
  201. }
  202. public bool IsConsumerConnectedLocked()
  203. {
  204. return ConsumerListener != null;
  205. }
  206. public KReadableEvent GetWaitBufferFreeEvent()
  207. {
  208. lock (Lock)
  209. {
  210. return _waitBufferFreeEvent.ReadableEvent;
  211. }
  212. }
  213. public bool IsOwnedByConsumerLocked(int slot)
  214. {
  215. if (Slots[slot].BufferState != BufferState.Acquired)
  216. {
  217. Logger.PrintError(LogClass.SurfaceFlinger, $"Slot {slot} is not owned by the consumer (state = {Slots[slot].BufferState})");
  218. return false;
  219. }
  220. return true;
  221. }
  222. public bool IsOwnedByProducerLocked(int slot)
  223. {
  224. if (Slots[slot].BufferState != BufferState.Dequeued)
  225. {
  226. Logger.PrintError(LogClass.SurfaceFlinger, $"Slot {slot} is not owned by the producer (state = {Slots[slot].BufferState})");
  227. return false;
  228. }
  229. return true;
  230. }
  231. }
  232. }