Window.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.OpenGL.Image;
  4. using System;
  5. namespace Ryujinx.Graphics.OpenGL
  6. {
  7. class Window : IWindow, IDisposable
  8. {
  9. private readonly Renderer _renderer;
  10. private int _width;
  11. private int _height;
  12. private int _copyFramebufferHandle;
  13. internal BackgroundContextWorker BackgroundContext { get; private set; }
  14. internal bool ScreenCaptureRequested { get; set; }
  15. public Window(Renderer renderer)
  16. {
  17. _renderer = renderer;
  18. }
  19. public void Present(ITexture texture, ImageCrop crop, Action swapBuffersCallback)
  20. {
  21. GL.Disable(EnableCap.FramebufferSrgb);
  22. CopyTextureToFrameBufferRGB(0, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop);
  23. GL.Enable(EnableCap.FramebufferSrgb);
  24. swapBuffersCallback();
  25. }
  26. public void SetSize(int width, int height)
  27. {
  28. _width = width;
  29. _height = height;
  30. }
  31. private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop)
  32. {
  33. (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
  34. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
  35. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
  36. TextureView viewConverted = view.Format.IsBgr() ? _renderer.TextureCopy.BgraSwap(view) : view;
  37. GL.FramebufferTexture(
  38. FramebufferTarget.ReadFramebuffer,
  39. FramebufferAttachment.ColorAttachment0,
  40. viewConverted.Handle,
  41. 0);
  42. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  43. GL.Disable(EnableCap.RasterizerDiscard);
  44. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  45. GL.Clear(ClearBufferMask.ColorBufferBit);
  46. int srcX0, srcX1, srcY0, srcY1;
  47. float scale = view.ScaleFactor;
  48. if (crop.Left == 0 && crop.Right == 0)
  49. {
  50. srcX0 = 0;
  51. srcX1 = (int)(view.Width / scale);
  52. }
  53. else
  54. {
  55. srcX0 = crop.Left;
  56. srcX1 = crop.Right;
  57. }
  58. if (crop.Top == 0 && crop.Bottom == 0)
  59. {
  60. srcY0 = 0;
  61. srcY1 = (int)(view.Height / scale);
  62. }
  63. else
  64. {
  65. srcY0 = crop.Top;
  66. srcY1 = crop.Bottom;
  67. }
  68. if (scale != 1f)
  69. {
  70. srcX0 = (int)(srcX0 * scale);
  71. srcY0 = (int)(srcY0 * scale);
  72. srcX1 = (int)Math.Ceiling(srcX1 * scale);
  73. srcY1 = (int)Math.Ceiling(srcY1 * scale);
  74. }
  75. float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
  76. float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
  77. int dstWidth = (int)(_width * ratioX);
  78. int dstHeight = (int)(_height * ratioY);
  79. int dstPaddingX = (_width - dstWidth) / 2;
  80. int dstPaddingY = (_height - dstHeight) / 2;
  81. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  82. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  83. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  84. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  85. if (ScreenCaptureRequested)
  86. {
  87. CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY);
  88. ScreenCaptureRequested = false;
  89. }
  90. GL.BlitFramebuffer(
  91. srcX0,
  92. srcY0,
  93. srcX1,
  94. srcY1,
  95. dstX0,
  96. dstY0,
  97. dstX1,
  98. dstY1,
  99. ClearBufferMask.ColorBufferBit,
  100. BlitFramebufferFilter.Linear);
  101. // Remove Alpha channel
  102. GL.ColorMask(false, false, false, true);
  103. GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  104. GL.Clear(ClearBufferMask.ColorBufferBit);
  105. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  106. {
  107. ((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
  108. }
  109. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  110. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  111. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  112. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  113. if (viewConverted != view)
  114. {
  115. viewConverted.Dispose();
  116. }
  117. }
  118. private int GetCopyFramebufferHandleLazy()
  119. {
  120. int handle = _copyFramebufferHandle;
  121. if (handle == 0)
  122. {
  123. handle = GL.GenFramebuffer();
  124. _copyFramebufferHandle = handle;
  125. }
  126. return handle;
  127. }
  128. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  129. {
  130. BackgroundContext = new BackgroundContextWorker(baseContext);
  131. }
  132. public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
  133. {
  134. long size = Math.Abs(4 * width * height);
  135. byte[] bitmap = new byte[size];
  136. GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
  137. _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
  138. }
  139. public void Dispose()
  140. {
  141. BackgroundContext.Dispose();
  142. if (_copyFramebufferHandle != 0)
  143. {
  144. GL.DeleteFramebuffer(_copyFramebufferHandle);
  145. _copyFramebufferHandle = 0;
  146. }
  147. }
  148. }
  149. }