BufferQueueCore.cs 9.9 KB

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