ThiDevice.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.Device;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Host1x
  6. {
  7. class ThiDevice : IDeviceStateWithContext, IDisposable
  8. {
  9. private readonly ClassId _classId;
  10. private readonly IDeviceState _device;
  11. private readonly SyncptIncrManager _syncptIncrMgr;
  12. private long _currentContextId;
  13. private long _previousContextId;
  14. private class CommandAction
  15. {
  16. public long ContextId { get; }
  17. public int Data { get; }
  18. public CommandAction(long contextId, int data)
  19. {
  20. ContextId = contextId;
  21. Data = data;
  22. }
  23. }
  24. private class MethodCallAction : CommandAction
  25. {
  26. public int Method { get; }
  27. public MethodCallAction(long contextId, int method, int data) : base(contextId, data)
  28. {
  29. Method = method;
  30. }
  31. }
  32. private class SyncptIncrAction : CommandAction
  33. {
  34. public SyncptIncrAction(long contextId, uint syncptIncrHandle) : base(contextId, (int)syncptIncrHandle)
  35. {
  36. }
  37. }
  38. private readonly AsyncWorkQueue<CommandAction> _commandQueue;
  39. private readonly DeviceState<ThiRegisters> _state;
  40. public ThiDevice(ClassId classId, IDeviceState device, SyncptIncrManager syncptIncrMgr)
  41. {
  42. _classId = classId;
  43. _device = device;
  44. _syncptIncrMgr = syncptIncrMgr;
  45. _commandQueue = new AsyncWorkQueue<CommandAction>(Process, $"Ryujinx.{classId}Processor");
  46. _state = new DeviceState<ThiRegisters>(new Dictionary<string, RwCallback>
  47. {
  48. { nameof(ThiRegisters.IncrSyncpt), new RwCallback(IncrSyncpt, null) },
  49. { nameof(ThiRegisters.Method1), new RwCallback(Method1, null) }
  50. });
  51. _previousContextId = -1;
  52. }
  53. public long CreateContext()
  54. {
  55. if (_device is IDeviceStateWithContext deviceWithContext)
  56. {
  57. return deviceWithContext.CreateContext();
  58. }
  59. return -1;
  60. }
  61. public void DestroyContext(long id)
  62. {
  63. if (_device is IDeviceStateWithContext deviceWithContext)
  64. {
  65. deviceWithContext.DestroyContext(id);
  66. }
  67. }
  68. public void BindContext(long id)
  69. {
  70. _currentContextId = id;
  71. }
  72. public int Read(int offset) => _state.Read(offset);
  73. public void Write(int offset, int data) => _state.Write(offset, data);
  74. private void IncrSyncpt(int data)
  75. {
  76. uint syncpointId = (uint)(data & 0xFF);
  77. uint cond = (uint)((data >> 8) & 0xFF); // 0 = Immediate, 1 = Done
  78. if (cond == 0)
  79. {
  80. _syncptIncrMgr.Increment(syncpointId);
  81. }
  82. else
  83. {
  84. _commandQueue.Add(new SyncptIncrAction(_currentContextId, _syncptIncrMgr.IncrementWhenDone(_classId, syncpointId)));
  85. }
  86. }
  87. private void Method1(int data)
  88. {
  89. _commandQueue.Add(new MethodCallAction(_currentContextId, (int)_state.State.Method0 * 4, data));
  90. }
  91. private void Process(CommandAction cmdAction)
  92. {
  93. long contextId = cmdAction.ContextId;
  94. if (contextId != _previousContextId)
  95. {
  96. _previousContextId = contextId;
  97. if (_device is IDeviceStateWithContext deviceWithContext)
  98. {
  99. deviceWithContext.BindContext(contextId);
  100. }
  101. }
  102. if (cmdAction is SyncptIncrAction syncptIncrAction)
  103. {
  104. _syncptIncrMgr.SignalDone((uint)syncptIncrAction.Data);
  105. }
  106. else if (cmdAction is MethodCallAction methodCallAction)
  107. {
  108. _device.Write(methodCallAction.Method, methodCallAction.Data);
  109. }
  110. }
  111. public void Dispose() => _commandQueue.Dispose();
  112. }
  113. }