BufferQueueCore.cs 9.9 KB

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