From 28618c58d7ee1ae63fc57deca791a64ab38b57af Mon Sep 17 00:00:00 2001
From: emmauss <emmausssss@gmail.com>
Date: Mon, 28 Jun 2021 20:09:43 +0000
Subject: Add Screenshot Feature (#2354)

* Add internal screenshot  capabilities

* update version notice
---
 Ryujinx.Graphics.OpenGL/Window.cs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

(limited to 'Ryujinx.Graphics.OpenGL/Window.cs')

diff --git a/Ryujinx.Graphics.OpenGL/Window.cs b/Ryujinx.Graphics.OpenGL/Window.cs
index b7525ae5..35b04d6d 100644
--- a/Ryujinx.Graphics.OpenGL/Window.cs
+++ b/Ryujinx.Graphics.OpenGL/Window.cs
@@ -16,6 +16,8 @@ namespace Ryujinx.Graphics.OpenGL
 
         internal BackgroundContextWorker BackgroundContext { get; private set; }
 
+        internal bool ScreenCaptureRequested { get; set; }
+
         public Window(Renderer renderer)
         {
             _renderer = renderer;
@@ -106,6 +108,13 @@ namespace Ryujinx.Graphics.OpenGL
             int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
             int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
 
+            if (ScreenCaptureRequested)
+            {
+                CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgra8(), crop.FlipX, crop.FlipY);
+
+                ScreenCaptureRequested = false;
+            }
+
             GL.BlitFramebuffer(
                 srcX0,
                 srcY0,
@@ -159,6 +168,16 @@ namespace Ryujinx.Graphics.OpenGL
             BackgroundContext = new BackgroundContextWorker(baseContext);
         }
 
+        public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
+        {
+            long size = Math.Abs(4 * width * height);
+            byte[] bitmap = new byte[size];
+
+            GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
+            
+            _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
+        }
+
         public void Dispose()
         {
             BackgroundContext.Dispose();
-- 
cgit v1.2.3-70-g09d2