Window.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using System;
  4. using System.Collections.Concurrent;
  5. namespace Ryujinx.Graphics.Gpu
  6. {
  7. using Texture = Image.Texture;
  8. /// <summary>
  9. /// GPU image presentation window.
  10. /// </summary>
  11. public class Window
  12. {
  13. private readonly GpuContext _context;
  14. /// <summary>
  15. /// Texture presented on the window.
  16. /// </summary>
  17. private struct PresentationTexture
  18. {
  19. /// <summary>
  20. /// Texture information.
  21. /// </summary>
  22. public TextureInfo Info { get; }
  23. /// <summary>
  24. /// Texture crop region.
  25. /// </summary>
  26. public ImageCrop Crop { get; }
  27. /// <summary>
  28. /// Texture release callback.
  29. /// </summary>
  30. public Action<object> Callback { get; }
  31. /// <summary>
  32. /// User defined object, passed to the release callback.
  33. /// </summary>
  34. public object UserObj { get; }
  35. /// <summary>
  36. /// Creates a new instance of the presentation texture.
  37. /// </summary>
  38. /// <param name="info">Information of the texture to be presented</param>
  39. /// <param name="crop">Texture crop region</param>
  40. /// <param name="callback">Texture release callback</param>
  41. /// <param name="userObj">User defined object passed to the release callback, can be used to identify the texture</param>
  42. public PresentationTexture(
  43. TextureInfo info,
  44. ImageCrop crop,
  45. Action<object> callback,
  46. object userObj)
  47. {
  48. Info = info;
  49. Crop = crop;
  50. Callback = callback;
  51. UserObj = userObj;
  52. }
  53. }
  54. private readonly ConcurrentQueue<PresentationTexture> _frameQueue;
  55. /// <summary>
  56. /// Creates a new instance of the GPU presentation window.
  57. /// </summary>
  58. /// <param name="context">GPU emulation context</param>
  59. public Window(GpuContext context)
  60. {
  61. _context = context;
  62. _frameQueue = new ConcurrentQueue<PresentationTexture>();
  63. }
  64. /// <summary>
  65. /// Enqueues a frame for presentation.
  66. /// This method is thread safe and can be called from any thread.
  67. /// When the texture is presented and not needed anymore, the release callback is called.
  68. /// It's an error to modify the texture after calling this method, before the release callback is called.
  69. /// </summary>
  70. /// <param name="address">CPU virtual address of the texture data</param>
  71. /// <param name="width">Texture width</param>
  72. /// <param name="height">Texture height</param>
  73. /// <param name="stride">Texture stride for linear texture, should be zero otherwise</param>
  74. /// <param name="isLinear">Indicates if the texture is linear, normally false</param>
  75. /// <param name="gobBlocksInY">GOB blocks in the Y direction, for block linear textures</param>
  76. /// <param name="format">Texture format</param>
  77. /// <param name="bytesPerPixel">Texture format bytes per pixel (must match the format)</param>
  78. /// <param name="crop">Texture crop region</param>
  79. /// <param name="callback">Texture release callback</param>
  80. /// <param name="userObj">User defined object passed to the release callback</param>
  81. public void EnqueueFrameThreadSafe(
  82. ulong address,
  83. int width,
  84. int height,
  85. int stride,
  86. bool isLinear,
  87. int gobBlocksInY,
  88. Format format,
  89. int bytesPerPixel,
  90. ImageCrop crop,
  91. Action<object> callback,
  92. object userObj)
  93. {
  94. FormatInfo formatInfo = new FormatInfo(format, 1, 1, bytesPerPixel);
  95. TextureInfo info = new TextureInfo(
  96. address,
  97. width,
  98. height,
  99. 1,
  100. 1,
  101. 1,
  102. 1,
  103. stride,
  104. isLinear,
  105. gobBlocksInY,
  106. 1,
  107. 1,
  108. Target.Texture2D,
  109. formatInfo);
  110. _frameQueue.Enqueue(new PresentationTexture(info, crop, callback, userObj));
  111. }
  112. /// <summary>
  113. /// Presents a texture on the queue.
  114. /// If the queue is empty, then no texture is presented.
  115. /// </summary>
  116. /// <param name="swapBuffersCallback">Callback method to call when a new texture should be presented on the screen</param>
  117. public void Present(Action swapBuffersCallback)
  118. {
  119. _context.AdvanceSequence();
  120. if (_frameQueue.TryDequeue(out PresentationTexture pt))
  121. {
  122. Texture texture = _context.Methods.TextureManager.FindOrCreateTexture(pt.Info);
  123. texture.SynchronizeMemory();
  124. _context.Renderer.Window.Present(texture.HostTexture, pt.Crop);
  125. swapBuffersCallback();
  126. pt.Callback(pt.UserObj);
  127. }
  128. }
  129. }
  130. }