From 026a9011e649c482d057594de6b4205290efe8bc Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 13 Sep 2026 10:49:32 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=95=E3=83=AC=E3=83=BC=E3=83=A0=E3=81=AE?= =?UTF-8?q?=E7=B8=AE=E5=B0=8F=E3=81=A8=E8=AA=AD=E3=81=BF=E6=88=BB=E3=81=97?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyForge/Encoding/ProxyFrameRenderer.cs | 142 ++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 ProxyForge/Encoding/ProxyFrameRenderer.cs diff --git a/ProxyForge/Encoding/ProxyFrameRenderer.cs b/ProxyForge/Encoding/ProxyFrameRenderer.cs new file mode 100644 index 0000000..d9bd5b7 --- /dev/null +++ b/ProxyForge/Encoding/ProxyFrameRenderer.cs @@ -0,0 +1,142 @@ +using System.Numerics; +using Vortice.DCommon; +using Vortice.Direct2D1; +using Vortice.Direct2D1.Effects; +using Vortice.DXGI; +using Vortice.Mathematics; +using YukkuriMovieMaker.Commons; + +namespace ProxyForge.Encoding; + +internal sealed class ProxyFrameRenderer : IDisposable +{ + public const float ReferenceDpi = 96f; + + readonly ID2D1DeviceContext6 context; + readonly ID2D1Bitmap1 target; + readonly ID2D1Bitmap1 readback; + readonly List chain = []; + readonly List outputs = []; + readonly int width; + readonly int height; + readonly int stride; + bool disposed; + + public int FrameByteCount => stride * height; + + public int HalvingCount { get; } + + public ProxyFrameRenderer(IGraphicsDevicesAndContext devices, ID2D1Image source, DisplayBounds display, ProxyGeometry geometry) + { + context = devices.DeviceContext; + width = geometry.Width; + height = geometry.Height; + stride = width * FrameAlpha.BytesPerPixel; + + var pixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied); + var size = new SizeI(width, height); + + target = context.CreateBitmap(size, nint.Zero, stride, + new BitmapProperties1(pixelFormat, ReferenceDpi, ReferenceDpi, BitmapOptions.Target)); + readback = context.CreateBitmap(size, nint.Zero, stride, + new BitmapProperties1(pixelFormat, ReferenceDpi, ReferenceDpi, + BitmapOptions.CpuRead | BitmapOptions.CannotDraw)); + + var align = new AffineTransform2D(context) + { + InterPolationMode = AffineTransform2DInterpolationMode.Linear, + BorderMode = BorderMode.Hard, + TransformMatrix = Matrix3x2.CreateTranslation(-display.Left, -display.Top), + }; + var extend = new Border(context) + { + EdgeModeX = BorderEdgeMode.Clamp, + EdgeModeY = BorderEdgeMode.Clamp, + }; + var image = Append(extend, Append(align, source)); + + var contentWidth = (double)display.Width; + var contentHeight = (double)display.Height; + while (contentWidth >= 2d * width && contentHeight >= 2d * height) + { + var halve = new Scale(context) + { + Value = new Vector2(0.5f, 0.5f), + CenterPoint = Vector2.Zero, + InterpolationMode = ScaleInterpolationMode.Linear, + BorderMode = BorderMode.Hard, + }; + image = Append(halve, image); + contentWidth /= 2d; + contentHeight /= 2d; + HalvingCount++; + } + + var fit = new AffineTransform2D(context) + { + InterPolationMode = AffineTransform2DInterpolationMode.Cubic, + BorderMode = BorderMode.Hard, + TransformMatrix = Matrix3x2.CreateScale((float)(width / contentWidth), (float)(height / contentHeight)), + }; + Append(fit, image); + } + + ID2D1Image Append(ID2D1Effect effect, ID2D1Image input) + { + effect.SetInput(0, input, true); + chain.Add(effect); + var output = effect.Output; + outputs.Add(output); + return output; + } + + public void Render(Span destination) + { + ObjectDisposedException.ThrowIf(disposed, this); + ArgumentOutOfRangeException.ThrowIfLessThan(destination.Length, FrameByteCount); + + context.Target = target; + context.BeginDraw(); + context.Clear(new Color4(0f, 0f, 0f, 0f)); + context.DrawImage(outputs[^1], InterpolationMode.Linear, CompositeMode.SourceOver); + context.EndDraw(); + context.Target = null; + + readback.CopyFromBitmap(target); + + var mapped = readback.Map(MapOptions.Read); + try + { + unsafe + { + var scanline = (byte*)mapped.Bits; + for (var y = 0; y < height; y++) + { + new ReadOnlySpan(scanline + (long)y * mapped.Pitch, stride) + .CopyTo(destination.Slice(y * stride, stride)); + } + } + } + finally + { + readback.Unmap(); + } + } + + public void Dispose() + { + if (disposed) + return; + + disposed = true; + for (var index = chain.Count - 1; index >= 0; index--) + { + outputs[index].Dispose(); + chain[index].SetInput(0, null, true); + chain[index].Dispose(); + } + + readback.Dispose(); + target.Dispose(); + } +}