BufferQueueCore.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 BufferQueueCore(Switch device, long 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. private void WaitForLock()
  163. {
  164. if (Active)
  165. {
  166. Monitor.Wait(Lock);
  167. }
  168. }
  169. public void FreeBufferLocked(int slot)
  170. {
  171. Slots[slot].GraphicBuffer.Reset();
  172. if (Slots[slot].BufferState == BufferState.Acquired)
  173. {
  174. Slots[slot].NeedsCleanupOnRelease = true;
  175. }
  176. Slots[slot].BufferState = BufferState.Free;
  177. Slots[slot].FrameNumber = uint.MaxValue;
  178. Slots[slot].AcquireCalled = false;
  179. Slots[slot].Fence.FenceCount = 0;
  180. }
  181. public void FreeAllBuffersLocked()
  182. {
  183. BufferHasBeenQueued = false;
  184. for (int slot = 0; slot < Slots.Length; slot++)
  185. {
  186. FreeBufferLocked(slot);
  187. }
  188. }
  189. public bool StillTracking(ref BufferItem item)
  190. {
  191. BufferSlot slot = Slots[item.Slot];
  192. // TODO: Check this. On Android, this checks the "handle". I assume NvMapHandle is the handle, but it might not be.
  193. return !slot.GraphicBuffer.IsNull && slot.GraphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle == item.GraphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle;
  194. }
  195. public void WaitWhileAllocatingLocked()
  196. {
  197. while (IsAllocating)
  198. {
  199. WaitIsAllocatingEvent();
  200. }
  201. }
  202. public void CheckSystemEventsLocked(int maxBufferCount)
  203. {
  204. if (!EnableExternalEvent)
  205. {
  206. return;
  207. }
  208. bool needBufferReleaseSignal = false;
  209. bool needFrameAvailableSignal = false;
  210. if (maxBufferCount > 1)
  211. {
  212. for (int i = 0; i < maxBufferCount; i++)
  213. {
  214. if (Slots[i].BufferState == BufferState.Queued)
  215. {
  216. needFrameAvailableSignal = true;
  217. }
  218. else if (Slots[i].BufferState == BufferState.Free)
  219. {
  220. needBufferReleaseSignal = true;
  221. }
  222. }
  223. }
  224. if (needBufferReleaseSignal)
  225. {
  226. SignalWaitBufferFreeEvent();
  227. }
  228. else
  229. {
  230. _waitBufferFreeEvent.WritableEvent.Clear();
  231. }
  232. if (needFrameAvailableSignal)
  233. {
  234. SignalFrameAvailableEvent();
  235. }
  236. else
  237. {
  238. _frameAvailableEvent.WritableEvent.Clear();
  239. }
  240. }
  241. public bool IsProducerConnectedLocked()
  242. {
  243. return ConnectedApi != NativeWindowApi.NoApi;
  244. }
  245. public bool IsConsumerConnectedLocked()
  246. {
  247. return ConsumerListener != null;
  248. }
  249. public KReadableEvent GetWaitBufferFreeEvent()
  250. {
  251. lock (Lock)
  252. {
  253. return _waitBufferFreeEvent.ReadableEvent;
  254. }
  255. }
  256. public bool IsOwnedByConsumerLocked(int slot)
  257. {
  258. if (Slots[slot].BufferState != BufferState.Acquired)
  259. {
  260. Logger.Error?.Print(LogClass.SurfaceFlinger, $"Slot {slot} is not owned by the consumer (state = {Slots[slot].BufferState})");
  261. return false;
  262. }
  263. return true;
  264. }
  265. public bool IsOwnedByProducerLocked(int slot)
  266. {
  267. if (Slots[slot].BufferState != BufferState.Dequeued)
  268. {
  269. Logger.Error?.Print(LogClass.SurfaceFlinger, $"Slot {slot} is not owned by the producer (state = {Slots[slot].BufferState})");
  270. return false;
  271. }
  272. return true;
  273. }
  274. }
  275. }