BufferQueueCore.cs 9.1 KB

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