Overlay system

This commit is contained in:
Barış Hamil 2025-06-21 00:17:10 +03:00
parent 3be20385ed
commit 069a703f22
13 changed files with 1088 additions and 19 deletions

View file

@ -1,10 +1,14 @@
using Ryujinx.Common.Memory;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.Image;
using Ryujinx.Graphics.Gpu.Memory;
using Ryujinx.Graphics.Gpu.Overlay;
using Ryujinx.Graphics.Texture;
using Ryujinx.Memory.Range;
using SkiaSharp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace Ryujinx.Graphics.Gpu
@ -15,6 +19,8 @@ namespace Ryujinx.Graphics.Gpu
public class Window
{
private readonly GpuContext _context;
private readonly OverlayManager _overlayManager;
private DateTime _lastUpdateTime = DateTime.UtcNow;
/// <summary>
/// Texture presented on the window.
@ -98,8 +104,12 @@ namespace Ryujinx.Graphics.Gpu
public Window(GpuContext context)
{
_context = context;
_overlayManager = new OverlayManager();
_frameQueue = new ConcurrentQueue<PresentationTexture>();
// Initialize controller overlay
InitializeControllerOverlay();
}
/// <summary>
@ -207,6 +217,9 @@ namespace Ryujinx.Graphics.Gpu
texture.SynchronizeMemory();
// Add overlays by modifying texture data directly
AddOverlaysToTexture(texture);
ImageCrop crop = new(
(int)(pt.Crop.Left * texture.ScaleFactor),
(int)MathF.Ceiling(pt.Crop.Right * texture.ScaleFactor),
@ -244,6 +257,88 @@ namespace Ryujinx.Graphics.Gpu
}
}
/// <summary>
/// Initialize controller overlay
/// </summary>
private void InitializeControllerOverlay()
{
var controllerOverlay = new ControllerOverlay();
_overlayManager.AddOverlay(controllerOverlay);
}
/// <summary>
/// Add overlays to the texture using SkiaSharp
/// </summary>
/// <param name="texture">The texture to modify</param>
private void AddOverlaysToTexture(Image.Texture texture)
{
try
{
// Calculate delta time for lifespan updates
DateTime currentTime = DateTime.UtcNow;
float deltaTime = (float)(currentTime - _lastUpdateTime).TotalSeconds;
_lastUpdateTime = currentTime;
// Update overlay animations
_overlayManager.Update(deltaTime, new SKSize(texture.Info.Width, texture.Info.Height));
// Get texture data from host texture
using var pinnedData = texture.HostTexture.GetData();
var data = pinnedData.Get().ToArray();
if (data == null || data.Length == 0)
return;
int width = texture.Info.Width;
int height = texture.Info.Height;
int bytesPerPixel = texture.Info.FormatInfo.BytesPerPixel;
// Determine the SKColorType based on bytes per pixel
SKColorType colorType = bytesPerPixel switch
{
4 => SKColorType.Rgba8888,
3 => SKColorType.Rgb888x,
2 => SKColorType.Rgb565,
_ => SKColorType.Rgba8888
};
// Create SKBitmap from texture data
var imageInfo = new SKImageInfo(width, height, colorType, SKAlphaType.Premul);
using var bitmap = new SKBitmap(imageInfo);
// Copy texture data to bitmap
unsafe
{
fixed (byte* dataPtr = data)
{
bitmap.SetPixels((IntPtr)dataPtr);
}
}
// Create canvas for drawing overlays
using var canvas = new SKCanvas(bitmap);
// Render all overlays
_overlayManager.Render(canvas);
// Copy modified bitmap data back to texture data array
var pixels = bitmap.Bytes;
if (pixels.Length <= data.Length)
{
Array.Copy(pixels, data, pixels.Length);
}
// Upload modified data back to texture
var memoryOwner = MemoryOwner<byte>.Rent(data.Length);
data.CopyTo(memoryOwner.Span);
texture.HostTexture.SetData(memoryOwner); // SetData will dispose the MemoryOwner
}
catch (Exception ex)
{
// Silently fail if overlay rendering doesn't work
System.Diagnostics.Debug.WriteLine($"Overlay rendering failed: {ex.Message}");
}
}
/// <summary>
/// Indicate that a frame on the queue is ready to be acquired.
/// </summary>
@ -267,5 +362,32 @@ namespace Ryujinx.Graphics.Gpu
return false;
}
/// <summary>
/// Show controller overlay with the provided input configurations
/// </summary>
/// <param name="inputConfigs">List of input configurations to display</param>
/// <param name="durationSeconds">Duration to show the overlay in seconds</param>
public void ShowControllerBindings(List<Common.Configuration.Hid.InputConfig> inputConfigs, int durationSeconds = 3)
{
var controllerOverlay = _overlayManager.FindOverlay("ControllerOverlay") as ControllerOverlay;
controllerOverlay?.ShowControllerBindings(inputConfigs, durationSeconds);
}
/// <summary>
/// Get the overlay manager for external access
/// </summary>
public OverlayManager GetOverlayManager()
{
return _overlayManager;
}
/// <summary>
/// Dispose resources
/// </summary>
public void Dispose()
{
_overlayManager?.Dispose();
}
}
}