From 19443040c4a1a15271defcfc378436088d47e2c3 Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Tue, 22 Aug 2023 17:57:46 -0700 Subject: [PATCH] slightly refactor OSD layer, no longer uses a render callback, rather it will do its logic itself and has its own enable drawing flag --- .../DisplayManager/DisplayManagerBase.cs | 56 ++-------------- .../DisplayManager/Filters/Gui.cs | 67 +++++++++++++++---- .../DisplayManager/IBlitter.cs | 9 +-- .../DisplayManager/OSDManager.cs | 26 +++---- 4 files changed, 75 insertions(+), 83 deletions(-) diff --git a/src/BizHawk.Client.Common/DisplayManager/DisplayManagerBase.cs b/src/BizHawk.Client.Common/DisplayManager/DisplayManagerBase.cs index 7c333484fd..5a1c1e3a2d 100644 --- a/src/BizHawk.Client.Common/DisplayManager/DisplayManagerBase.cs +++ b/src/BizHawk.Client.Common/DisplayManager/DisplayManagerBase.cs @@ -280,32 +280,15 @@ namespace BizHawk.Client.Common } if (!includeUserFilters) + { selectedChain = null; + } var fCoreScreenControl = CreateCoreScreenControl(); var fPresent = new FinalPresentation(chainOutSize); var fInput = new SourceImage(chainInSize); - var fOSD = new OSD(); - fOSD.RenderCallback = () => - { - if (!includeOSD) - { - return; - } - - var size = fOSD.FindInput().SurfaceFormat.Size; - _renderer.Begin(size.Width, size.Height); - var myBlitter = new MyBlitter(this) - { - ClipBounds = new Rectangle(0, 0, size.Width, size.Height) - }; - _renderer.EnableBlending(); - OSD.Begin(myBlitter); - OSD.DrawScreenInfo(myBlitter); - OSD.DrawMessages(myBlitter); - _renderer.End(); - }; + var fOSD = new OSD(includeOSD, OSD, _theOneFont); var chain = new FilterProgram(); @@ -392,18 +375,17 @@ namespace BizHawk.Client.Common // add final presentation if (includeUserFilters) + { chain.AddFilter(fPresent, "presentation"); + } //add lua layer 'native' AppendApiHawkLayer(chain, DisplaySurfaceID.Client); // and OSD goes on top of that // TODO - things break if this isn't present (the final presentation filter gets messed up when used with prescaling) - // so, always include it (we'll handle this flag in the callback to do no rendering) - if (true /*includeOSD*/) - { - chain.AddFilter(fOSD, "osd"); - } + // so, always include it (we'll handle includeOSD on Run) + chain.AddFilter(fOSD, "osd"); return chain; } @@ -1067,29 +1049,5 @@ namespace BizHawk.Client.Common _apiHawkIDToSurface.Remove(surfaceID); _apiHawkSurfaceSets[surfaceID].SetPending(dispSurfaceImpl); } - - // helper classes: - private class MyBlitter : IBlitter - { - private readonly DisplayManagerBase _owner; - - public MyBlitter(DisplayManagerBase dispManager) - { - _owner = dispManager; - } - - public StringRenderer GetFontType(string fontType) => _owner._theOneFont; - - public void DrawString(string s, StringRenderer font, Color color, float x, float y) - { - _owner._renderer.SetModulateColor(color); - font.RenderString(_owner._renderer, x, y, s); - _owner._renderer.SetModulateColorWhite(); - } - - public SizeF MeasureString(string s, StringRenderer font) => font.Measure(s); - - public Rectangle ClipBounds { get; set; } - } } } diff --git a/src/BizHawk.Client.Common/DisplayManager/Filters/Gui.cs b/src/BizHawk.Client.Common/DisplayManager/Filters/Gui.cs index c7de3c7d73..060ed3a95d 100644 --- a/src/BizHawk.Client.Common/DisplayManager/Filters/Gui.cs +++ b/src/BizHawk.Client.Common/DisplayManager/Filters/Gui.cs @@ -802,31 +802,70 @@ namespace BizHawk.Client.Common.Filters public class OSD : BaseFilter { - // this class has the ability to disable its operations for higher performance when the callback is removed, - // without having to take it out of the chain. although, its presence in the chain may slow down performance due to added resolves/renders - // so, we should probably rebuild the chain. + // This class has the ability to disable its operations for higher performance _drawOsd is false, while still allowing it to be in the chain + // (although its presence in the chain may slow down performance itself due to added resolves/renders) + + private readonly bool _drawOsd; + private readonly OSDManager _manager; + private readonly StringRenderer _font; + + public OSD(bool drawOsd, OSDManager manager, StringRenderer font) + { + _drawOsd = drawOsd; + _manager = manager; + _font = font; + } public override void Initialize() { - if (RenderCallback != null) - { - DeclareInput(SurfaceDisposition.RenderTarget); - } + DeclareInput(SurfaceDisposition.RenderTarget); } public override void SetInputFormat(string channel, SurfaceState state) { - if (RenderCallback != null) - { - DeclareOutput(state); - } + DeclareOutput(state); } - public Action RenderCallback; - public override void Run() { - RenderCallback?.Invoke(); + if (!_drawOsd) + { + return; + } + + var size = FindInput().SurfaceFormat.Size; + + FilterProgram.GuiRenderer.Begin(size.Width, size.Height); + var blitter = new OSDBlitter(_font, FilterProgram.GuiRenderer, new(0, 0, size.Width, size.Height)); + FilterProgram.GuiRenderer.EnableBlending(); + _manager.DrawScreenInfo(blitter); + _manager.DrawMessages(blitter); + FilterProgram.GuiRenderer.End(); + } + + private class OSDBlitter : IBlitter + { + private readonly StringRenderer _font; + private readonly IGuiRenderer _renderer; + + public OSDBlitter(StringRenderer font, IGuiRenderer renderer, Rectangle clipBounds) + { + _font = font; + _renderer = renderer; + ClipBounds = clipBounds; + } + + public void DrawString(string s, Color color, float x, float y) + { + _renderer.SetModulateColor(color); + _font.RenderString(_renderer, x, y, s); + _renderer.SetModulateColorWhite(); + } + + public SizeF MeasureString(string s) + => _font.Measure(s); + + public Rectangle ClipBounds { get; } } } } \ No newline at end of file diff --git a/src/BizHawk.Client.Common/DisplayManager/IBlitter.cs b/src/BizHawk.Client.Common/DisplayManager/IBlitter.cs index 7d5c8e11d0..42b01856be 100644 --- a/src/BizHawk.Client.Common/DisplayManager/IBlitter.cs +++ b/src/BizHawk.Client.Common/DisplayManager/IBlitter.cs @@ -11,9 +11,10 @@ namespace BizHawk.Client.Common /// public interface IBlitter { - StringRenderer GetFontType(string fontType); - void DrawString(string s, StringRenderer font, Color color, float x, float y); - SizeF MeasureString(string s, StringRenderer font); - Rectangle ClipBounds { get; set; } + void DrawString(string s, Color color, float x, float y); + + SizeF MeasureString(string s); + + Rectangle ClipBounds { get; } } } diff --git a/src/BizHawk.Client.Common/DisplayManager/OSDManager.cs b/src/BizHawk.Client.Common/DisplayManager/OSDManager.cs index cb4d6cdff1..cbfa23a0d5 100644 --- a/src/BizHawk.Client.Common/DisplayManager/OSDManager.cs +++ b/src/BizHawk.Client.Common/DisplayManager/OSDManager.cs @@ -34,19 +34,13 @@ namespace BizHawk.Client.Common } public string Fps { get; set; } - public StringRenderer MessageFont; - - public void Begin(IBlitter blitter) - { - MessageFont = blitter.GetFontType(nameof(MessageFont)); - } public Color FixedMessagesColor => Color.FromArgb(_config.MessagesColor); public Color FixedAlertMessageColor => Color.FromArgb(_config.AlertMessageColor); private PointF GetCoordinates(IBlitter g, MessagePosition position, string message) { - var size = g.MeasureString(message, MessageFont); + var size = g.MeasureString(message); float x = position.Anchor.IsLeft() ? position.X : g.ClipBounds.Width - position.X - size.Width; @@ -132,7 +126,7 @@ namespace BizHawk.Client.Common { var point = GetCoordinates(g, _config.Messages, message.Message); var y = point.Y + yOffset; // TODO: clean me up - g.DrawString(message.Message, MessageFont, FixedMessagesColor, point.X, y); + g.DrawString(message.Message, FixedMessagesColor, point.X, y); } public void DrawMessages(IBlitter g) @@ -173,7 +167,7 @@ namespace BizHawk.Client.Common { var point = GetCoordinates(g, text.Position, text.Message); if (point.Y >= g.ClipBounds.Height) continue; // simple optimisation; don't bother drawing off-screen - g.DrawString(text.Message, MessageFont, text.ForeColor, point.X, point.Y); + g.DrawString(text.Message, text.ForeColor, point.X, point.Y); } catch (Exception) { @@ -245,7 +239,7 @@ namespace BizHawk.Client.Common private void DrawOsdMessage(IBlitter g, string message, Color color, float x, float y) { - g.DrawString(message, MessageFont, color, x, y); + g.DrawString(message, color, x, y); } /// @@ -275,7 +269,7 @@ namespace BizHawk.Client.Common var input = InputStrMovie(); var point = GetCoordinates(g, _config.InputDisplay, input); Color c = Color.FromArgb(_config.MovieInput); - g.DrawString(input, MessageFont, c, point.X, point.Y); + g.DrawString(input, c, point.X, point.Y); } if (!moviePlaying) // TODO: message config -- allow setting of "mixed", and "auto" @@ -294,14 +288,14 @@ namespace BizHawk.Client.Common // first display previous frame's input. // note: that's only available in case we're working on a movie var previousStr = InputPrevious(); - g.DrawString(previousStr, MessageFont, previousColor, point.X, point.Y); + g.DrawString(previousStr, previousColor, point.X, point.Y); // next, draw the immediate input. // that is, whatever is being held down interactively right this moment even if the game is paused // this includes things held down due to autohold or autofire // I know, this is all really confusing var immediate = InputStrImmediate(); - g.DrawString(immediate, MessageFont, immediateColor, point.X, point.Y); + g.DrawString(immediate, immediateColor, point.X, point.Y); // next draw anything that's pressed because it's sticky. // this applies to autofire and autohold both. somehow. I don't understand it. @@ -310,11 +304,11 @@ namespace BizHawk.Client.Common // so we make an AND adapter and combine it using immediate & sticky // (adapter creation moved to InputManager) var autoString = MakeStringFor(_inputManager.WeirdStickyControllerForInputDisplay, cache: true); - g.DrawString(autoString, MessageFont, autoColor, point.X, point.Y); + g.DrawString(autoString, autoColor, point.X, point.Y); //recolor everything that's changed from the previous input var immediateOverlay = MakeIntersectImmediatePrevious(); - g.DrawString(immediateOverlay, MessageFont, changedColor, point.X, point.Y); + g.DrawString(immediateOverlay, changedColor, point.X, point.Y); } } @@ -357,7 +351,7 @@ namespace BizHawk.Client.Common var message = sb.ToString(); var point = GetCoordinates(g, _config.Autohold, message); - g.DrawString(message, MessageFont, Color.White, point.X, point.Y); + g.DrawString(message, Color.White, point.X, point.Y); } if (_movieSession.Movie.IsActive() && _config.DisplaySubtitles)