BufferQueueCore.cs 10 KB

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