BufferQueueCore.cs 9.4 KB

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