BufferQueueConsumer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Services.SurfaceFlinger.Types;
  3. using Ryujinx.HLE.HOS.Services.Time.Clock;
  4. using System;
  5. namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
  6. {
  7. class BufferQueueConsumer
  8. {
  9. public BufferQueueCore Core { get; }
  10. public BufferQueueConsumer(BufferQueueCore core)
  11. {
  12. Core = core;
  13. }
  14. public Status AcquireBuffer(out BufferItem bufferItem, ulong expectedPresent)
  15. {
  16. lock (Core.Lock)
  17. {
  18. int numAcquiredBuffers = 0;
  19. for (int i = 0; i < Core.MaxBufferCountCached; i++)
  20. {
  21. if (Core.Slots[i].BufferState == BufferState.Acquired)
  22. {
  23. numAcquiredBuffers++;
  24. }
  25. }
  26. if (numAcquiredBuffers > Core.MaxAcquiredBufferCount)
  27. {
  28. bufferItem = null;
  29. Logger.PrintDebug(LogClass.SurfaceFlinger, $"Max acquired buffer count reached: {numAcquiredBuffers} (max: {Core.MaxAcquiredBufferCount})");
  30. return Status.InvalidOperation;
  31. }
  32. if (Core.Queue.Count == 0)
  33. {
  34. bufferItem = null;
  35. return Status.NoBufferAvailaible;
  36. }
  37. if (expectedPresent != 0)
  38. {
  39. // TODO: support this for advanced presenting.
  40. throw new NotImplementedException();
  41. }
  42. bufferItem = Core.Queue[0];
  43. if (Core.StillTracking(ref bufferItem))
  44. {
  45. Core.Slots[bufferItem.Slot].AcquireCalled = true;
  46. Core.Slots[bufferItem.Slot].NeedsCleanupOnRelease = true;
  47. Core.Slots[bufferItem.Slot].BufferState = BufferState.Acquired;
  48. Core.Slots[bufferItem.Slot].Fence = AndroidFence.NoFence;
  49. ulong targetFrameNumber = Core.Slots[bufferItem.Slot].FrameNumber;
  50. for (int i = 0; i < Core.BufferHistory.Length; i++)
  51. {
  52. if (Core.BufferHistory[i].FrameNumber == targetFrameNumber)
  53. {
  54. Core.BufferHistory[i].State = BufferState.Acquired;
  55. break;
  56. }
  57. }
  58. }
  59. if (bufferItem.AcquireCalled)
  60. {
  61. bufferItem.GraphicBuffer.Reset();
  62. }
  63. Core.Queue.RemoveAt(0);
  64. Core.CheckSystemEventsLocked(Core.GetMaxBufferCountLocked(true));
  65. Core.SignalDequeueEvent();
  66. }
  67. return Status.Success;
  68. }
  69. public Status DetachBuffer(int slot)
  70. {
  71. lock (Core.Lock)
  72. {
  73. if (Core.IsAbandoned)
  74. {
  75. return Status.NoInit;
  76. }
  77. if (slot < 0 || slot >= Core.Slots.Length || !Core.IsOwnedByConsumerLocked(slot))
  78. {
  79. return Status.BadValue;
  80. }
  81. if (!Core.Slots[slot].RequestBufferCalled)
  82. {
  83. Logger.PrintError(LogClass.SurfaceFlinger, $"Slot {slot} was detached without requesting a buffer");
  84. return Status.BadValue;
  85. }
  86. Core.FreeBufferLocked(slot);
  87. Core.SignalDequeueEvent();
  88. return Status.Success;
  89. }
  90. }
  91. public Status AttachBuffer(out int slot, ref AndroidStrongPointer<GraphicBuffer> graphicBuffer)
  92. {
  93. lock (Core.Lock)
  94. {
  95. int numAcquiredBuffers = 0;
  96. int freeSlot = BufferSlotArray.InvalidBufferSlot;
  97. for (int i = 0; i < Core.Slots.Length; i++)
  98. {
  99. if (Core.Slots[i].BufferState == BufferState.Acquired)
  100. {
  101. numAcquiredBuffers++;
  102. }
  103. else if (Core.Slots[i].BufferState == BufferState.Free)
  104. {
  105. if (freeSlot == BufferSlotArray.InvalidBufferSlot || Core.Slots[i].FrameNumber < Core.Slots[freeSlot].FrameNumber)
  106. {
  107. freeSlot = i;
  108. }
  109. }
  110. }
  111. if (numAcquiredBuffers > Core.MaxAcquiredBufferCount + 1)
  112. {
  113. slot = BufferSlotArray.InvalidBufferSlot;
  114. Logger.PrintError(LogClass.SurfaceFlinger, $"Max acquired buffer count reached: {numAcquiredBuffers} (max: {Core.MaxAcquiredBufferCount})");
  115. return Status.InvalidOperation;
  116. }
  117. if (freeSlot == BufferSlotArray.InvalidBufferSlot)
  118. {
  119. slot = BufferSlotArray.InvalidBufferSlot;
  120. return Status.NoMemory;
  121. }
  122. Core.UpdateMaxBufferCountCachedLocked(freeSlot);
  123. slot = freeSlot;
  124. Core.Slots[slot].GraphicBuffer.Set(graphicBuffer);
  125. Core.Slots[slot].BufferState = BufferState.Acquired;
  126. Core.Slots[slot].AttachedByConsumer = true;
  127. Core.Slots[slot].NeedsCleanupOnRelease = false;
  128. Core.Slots[slot].Fence = AndroidFence.NoFence;
  129. Core.Slots[slot].FrameNumber = 0;
  130. Core.Slots[slot].AcquireCalled = false;
  131. }
  132. return Status.Success;
  133. }
  134. public Status ReleaseBuffer(int slot, ulong frameNumber, ref AndroidFence fence)
  135. {
  136. if (slot < 0 || slot >= Core.Slots.Length)
  137. {
  138. return Status.BadValue;
  139. }
  140. IProducerListener listener = null;
  141. lock (Core.Lock)
  142. {
  143. if (Core.Slots[slot].FrameNumber != frameNumber)
  144. {
  145. return Status.StaleBufferSlot;
  146. }
  147. foreach (BufferItem item in Core.Queue)
  148. {
  149. if (item.Slot == slot)
  150. {
  151. return Status.BadValue;
  152. }
  153. }
  154. if (Core.Slots[slot].BufferState == BufferState.Acquired)
  155. {
  156. Core.Slots[slot].BufferState = BufferState.Free;
  157. Core.Slots[slot].Fence = fence;
  158. listener = Core.ProducerListener;
  159. }
  160. else if (Core.Slots[slot].NeedsCleanupOnRelease)
  161. {
  162. Core.Slots[slot].NeedsCleanupOnRelease = false;
  163. return Status.StaleBufferSlot;
  164. }
  165. else
  166. {
  167. return Status.BadValue;
  168. }
  169. Core.Slots[slot].GraphicBuffer.Object.DecrementNvMapHandleRefCount(Core.Owner);
  170. Core.CheckSystemEventsLocked(Core.GetMaxBufferCountLocked(true));
  171. Core.SignalDequeueEvent();
  172. }
  173. listener?.OnBufferReleased();
  174. return Status.Success;
  175. }
  176. public Status Connect(IConsumerListener consumerListener, bool controlledByApp)
  177. {
  178. if (consumerListener == null)
  179. {
  180. return Status.BadValue;
  181. }
  182. lock (Core.Lock)
  183. {
  184. if (Core.IsAbandoned)
  185. {
  186. return Status.NoInit;
  187. }
  188. Core.ConsumerListener = consumerListener;
  189. Core.ConsumerControlledByApp = controlledByApp;
  190. }
  191. return Status.Success;
  192. }
  193. public Status Disconnect()
  194. {
  195. lock (Core.Lock)
  196. {
  197. if (!Core.IsConsumerConnectedLocked())
  198. {
  199. return Status.BadValue;
  200. }
  201. Core.IsAbandoned = true;
  202. Core.ConsumerListener = null;
  203. Core.Queue.Clear();
  204. Core.FreeAllBuffersLocked();
  205. Core.SignalDequeueEvent();
  206. }
  207. return Status.Success;
  208. }
  209. public Status GetReleasedBuffers(out ulong slotMask)
  210. {
  211. slotMask = 0;
  212. lock (Core.Lock)
  213. {
  214. if (Core.IsAbandoned)
  215. {
  216. return Status.BadValue;
  217. }
  218. for (int slot = 0; slot < Core.Slots.Length; slot++)
  219. {
  220. if (!Core.Slots[slot].AcquireCalled)
  221. {
  222. slotMask |= 1UL << slot;
  223. }
  224. }
  225. for (int i = 0; i < Core.Queue.Count; i++)
  226. {
  227. if (Core.Queue[i].AcquireCalled)
  228. {
  229. slotMask &= ~(1UL << i);
  230. }
  231. }
  232. }
  233. return Status.Success;
  234. }
  235. public Status SetDefaultBufferSize(uint width, uint height)
  236. {
  237. if (width == 0 || height == 0)
  238. {
  239. return Status.BadValue;
  240. }
  241. lock (Core.Lock)
  242. {
  243. Core.DefaultWidth = (int)width;
  244. Core.DefaultHeight = (int)height;
  245. }
  246. return Status.Success;
  247. }
  248. public Status SetDefaultMaxBufferCount(int bufferMaxCount)
  249. {
  250. lock (Core.Lock)
  251. {
  252. return Core.SetDefaultMaxBufferCountLocked(bufferMaxCount);
  253. }
  254. }
  255. public Status DisableAsyncBuffer()
  256. {
  257. lock (Core.Lock)
  258. {
  259. if (Core.IsConsumerConnectedLocked())
  260. {
  261. return Status.InvalidOperation;
  262. }
  263. Core.UseAsyncBuffer = false;
  264. }
  265. return Status.Success;
  266. }
  267. public Status SetMaxAcquiredBufferCount(int maxAcquiredBufferCount)
  268. {
  269. if (maxAcquiredBufferCount < 0 || maxAcquiredBufferCount > BufferSlotArray.MaxAcquiredBuffers)
  270. {
  271. return Status.BadValue;
  272. }
  273. lock (Core.Lock)
  274. {
  275. if (Core.IsProducerConnectedLocked())
  276. {
  277. return Status.InvalidOperation;
  278. }
  279. Core.MaxAcquiredBufferCount = maxAcquiredBufferCount;
  280. }
  281. return Status.Success;
  282. }
  283. public Status SetDefaultBufferFormat(PixelFormat defaultFormat)
  284. {
  285. lock (Core.Lock)
  286. {
  287. Core.DefaultBufferFormat = defaultFormat;
  288. }
  289. return Status.Success;
  290. }
  291. public Status SetConsumerUsageBits(uint usage)
  292. {
  293. lock (Core.Lock)
  294. {
  295. Core.ConsumerUsageBits = usage;
  296. }
  297. return Status.Success;
  298. }
  299. public Status SetTransformHint(NativeWindowTransform transformHint)
  300. {
  301. lock (Core.Lock)
  302. {
  303. Core.TransformHint = transformHint;
  304. }
  305. return Status.Success;
  306. }
  307. public Status SetPresentTime(int slot, ulong frameNumber, TimeSpanType presentationTime)
  308. {
  309. if (slot < 0 || slot >= Core.Slots.Length)
  310. {
  311. return Status.BadValue;
  312. }
  313. lock (Core.Lock)
  314. {
  315. if (Core.Slots[slot].FrameNumber != frameNumber)
  316. {
  317. return Status.StaleBufferSlot;
  318. }
  319. if (Core.Slots[slot].PresentationTime.NanoSeconds == 0)
  320. {
  321. Core.Slots[slot].PresentationTime = presentationTime;
  322. }
  323. for (int i = 0; i < Core.BufferHistory.Length; i++)
  324. {
  325. if (Core.BufferHistory[i].FrameNumber == frameNumber)
  326. {
  327. Core.BufferHistory[i].PresentationTime = presentationTime;
  328. break;
  329. }
  330. }
  331. }
  332. return Status.Success;
  333. }
  334. }
  335. }