XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Runtime.InteropServices;
using XFESystem.Desktop;
using XFESystem.Platform;

namespace XFESystem.NativeRuntime.Application;

// Pinning lasts until every native AP has completed its disjoint row range.
// The GC never sees an AP as an unattached managed thread. Small damage and
// unsupported formats retain the validated, allocation-free SSE presenter.
internal sealed unsafe class NativeFramebufferPresenter : IFramebufferPresenter
{
    private readonly uint output;
    private readonly MemoryFramebufferPresenter fallback;
    internal NativeFramebufferPresenter(uint output, FramebufferSurface surface)
    { this.output = output; fallback = new(surface); }
    public int Width => fallback.Width;
    public int Height => fallback.Height;
    [DllImport("xfe_pal", EntryPoint = "xfe_runtime_display_copy32", CallingConvention = CallingConvention.Cdecl)]
    private static extern int Copy32(uint output, int* source, ulong sourcePixels, uint sourceWidth, uint sourceHeight,
        uint x, uint y, uint width, uint height);
    public void Present(ReadOnlySpan<int> rgbPixels, DesktopBounds damage)
    {
        if ((long)Width * Height > rgbPixels.Length) throw new ArgumentException("The CPU pixel buffer is incomplete.", nameof(rgbPixels));
        long right = Math.Min((long)Width, (long)damage.X + damage.Width);
        long bottom = Math.Min((long)Height, (long)damage.Y + damage.Height);
        int left = Math.Max(0, damage.X), top = Math.Max(0, damage.Y);
        if (damage.Width <= 0 || damage.Height <= 0 || right <= left || bottom <= top) return;
        if ((right - left) * (bottom - top) >= 262144)
        {
            int result;
            fixed (int* pixels = rgbPixels)
                result = Copy32(output, pixels, (ulong)rgbPixels.Length, (uint)Width, (uint)Height,
                    (uint)left, (uint)top, (uint)(right - left), (uint)(bottom - top));
            if (result == 1) return;
            if (result != 0) throw new InvalidOperationException("The native parallel framebuffer copy rejected its extent.");
        }
        fallback.Present(rgbPixels, damage);
    }
}