BufferQueueConsumer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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.Slots.Length; i++)
  20. {
  21. if (Core.Slots[i].BufferState == BufferState.Acquired)
  22. {
  23. numAcquiredBuffers++;
  24. }
  25. }
  26. if (numAcquiredBuffers >= Core.MaxAcquiredBufferCount + 1)
  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. slot = freeSlot;
  123. Core.Slots[slot].GraphicBuffer.Set(graphicBuffer);
  124. Core.Slots[slot].BufferState = BufferState.Acquired;
  125. Core.Slots[slot].AttachedByConsumer = true;
  126. Core.Slots[slot].NeedsCleanupOnRelease = false;
  127. Core.Slots[slot].Fence = AndroidFence.NoFence;
  128. Core.Slots[slot].FrameNumber = 0;
  129. Core.Slots[slot].AcquireCalled = false;
  130. }
  131. return Status.Success;
  132. }
  133. public Status ReleaseBuffer(int slot, ulong frameNumber, ref AndroidFence fence)
  134. {
  135. if (slot < 0 || slot >= Core.Slots.Length)
  136. {
  137. return Status.BadValue;
  138. }
  139. IProducerListener listener = null;
  140. lock (Core.Lock)
  141. {
  142. if (Core.Slots[slot].FrameNumber != frameNumber)
  143. {
  144. return Status.StaleBufferSlot;
  145. }
  146. foreach (BufferItem item in Core.Queue)
  147. {
  148. if (item.Slot == slot)
  149. {
  150. return Status.BadValue;
  151. }
  152. }
  153. if (Core.Slots[slot].BufferState == BufferState.Acquired)
  154. {
  155. Core.Slots[slot].BufferState = BufferState.Free;
  156. Core.Slots[slot].Fence = fence;
  157. listener = Core.ProducerListener;
  158. }
  159. else if (Core.Slots[slot].NeedsCleanupOnRelease)
  160. {
  161. Core.Slots[slot].NeedsCleanupOnRelease = false;
  162. return Status.StaleBufferSlot;
  163. }
  164. else
  165. {
  166. return Status.BadValue;
  167. }
  168. Core.Slots[slot].GraphicBuffer.Object.DecrementNvMapHandleRefCount(Core.Owner);
  169. Core.CheckSystemEventsLocked(Core.GetMaxBufferCountLocked(true));
  170. Core.SignalDequeueEvent();
  171. }
  172. listener?.OnBufferReleased();
  173. return Status.Success;
  174. }
  175. public Status Connect(IConsumerListener consumerListener, bool controlledByApp)
  176. {
  177. if (consumerListener == null)
  178. {
  179. return Status.BadValue;
  180. }
  181. lock (Core.Lock)
  182. {
  183. if (Core.IsAbandoned)
  184. {
  185. return Status.NoInit;
  186. }
  187. Core.ConsumerListener = consumerListener;
  188. Core.ConsumerControlledByApp = controlledByApp;
  189. }
  190. return Status.Success;
  191. }
  192. public Status Disconnect()
  193. {
  194. lock (Core.Lock)
  195. {
  196. if (!Core.IsConsumerConnectedLocked())
  197. {
  198. return Status.BadValue;
  199. }
  200. Core.IsAbandoned = true;
  201. Core.ConsumerListener = null;
  202. Core.Queue.Clear();
  203. Core.FreeAllBuffersLocked();
  204. Core.SignalDequeueEvent();
  205. }
  206. return Status.Success;
  207. }
  208. public Status GetReleasedBuffers(out ulong slotMask)
  209. {
  210. slotMask = 0;
  211. lock (Core.Lock)
  212. {
  213. if (Core.IsAbandoned)
  214. {
  215. return Status.BadValue;
  216. }
  217. for (int slot = 0; slot < Core.Slots.Length; slot++)
  218. {
  219. if (!Core.Slots[slot].AcquireCalled)
  220. {
  221. slotMask |= 1UL << slot;
  222. }
  223. }
  224. for (int i = 0; i < Core.Queue.Count; i++)
  225. {
  226. if (Core.Queue[i].AcquireCalled)
  227. {
  228. slotMask &= ~(1UL << i);
  229. }
  230. }
  231. }
  232. return Status.Success;
  233. }
  234. public Status SetDefaultBufferSize(uint width, uint height)
  235. {
  236. if (width == 0 || height == 0)
  237. {
  238. return Status.BadValue;
  239. }
  240. lock (Core.Lock)
  241. {
  242. Core.DefaultWidth = (int)width;
  243. Core.DefaultHeight = (int)height;
  244. }
  245. return Status.Success;
  246. }
  247. public Status SetDefaultMaxBufferCount(int bufferMaxCount)
  248. {
  249. lock (Core.Lock)
  250. {
  251. return Core.SetDefaultMaxBufferCountLocked(bufferMaxCount);
  252. }
  253. }
  254. public Status DisableAsyncBuffer()
  255. {
  256. lock (Core.Lock)
  257. {
  258. if (Core.IsConsumerConnectedLocked())
  259. {
  260. return Status.InvalidOperation;
  261. }
  262. Core.UseAsyncBuffer = false;
  263. }
  264. return Status.Success;
  265. }
  266. public Status SetMaxAcquiredBufferCount(int maxAcquiredBufferCount)
  267. {
  268. if (maxAcquiredBufferCount < 0 || maxAcquiredBufferCount > BufferSlotArray.MaxAcquiredBuffers)
  269. {
  270. return Status.BadValue;
  271. }
  272. lock (Core.Lock)
  273. {
  274. if (Core.IsProducerConnectedLocked())
  275. {
  276. return Status.InvalidOperation;
  277. }
  278. Core.MaxAcquiredBufferCount = maxAcquiredBufferCount;
  279. }
  280. return Status.Success;
  281. }
  282. public Status SetDefaultBufferFormat(PixelFormat defaultFormat)
  283. {
  284. lock (Core.Lock)
  285. {
  286. Core.DefaultBufferFormat = defaultFormat;
  287. }
  288. return Status.Success;
  289. }
  290. public Status SetConsumerUsageBits(uint usage)
  291. {
  292. lock (Core.Lock)
  293. {
  294. Core.ConsumerUsageBits = usage;
  295. }
  296. return Status.Success;
  297. }
  298. public Status SetTransformHint(NativeWindowTransform transformHint)
  299. {
  300. lock (Core.Lock)
  301. {
  302. Core.TransformHint = transformHint;
  303. }
  304. return Status.Success;
  305. }
  306. public Status SetPresentTime(int slot, ulong frameNumber, TimeSpanType presentationTime)
  307. {
  308. if (slot < 0 || slot >= Core.Slots.Length)
  309. {
  310. return Status.BadValue;
  311. }
  312. lock (Core.Lock)
  313. {
  314. if (Core.Slots[slot].FrameNumber != frameNumber)
  315. {
  316. return Status.StaleBufferSlot;
  317. }
  318. if (Core.Slots[slot].PresentationTime.NanoSeconds == 0)
  319. {
  320. Core.Slots[slot].PresentationTime = presentationTime;
  321. }
  322. for (int i = 0; i < Core.BufferHistory.Length; i++)
  323. {
  324. if (Core.BufferHistory[i].FrameNumber == frameNumber)
  325. {
  326. Core.BufferHistory[i].PresentationTime = presentationTime;
  327. break;
  328. }
  329. }
  330. }
  331. return Status.Success;
  332. }
  333. }
  334. }