2020-12-16 03:19:07 +01:00
|
|
|
|
using Ryujinx.Common.Configuration;
|
|
|
|
|
using Ryujinx.Common.Logging;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
|
using Ryujinx.Graphics.Gpu;
|
|
|
|
|
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2021-04-13 02:56:16 +02:00
|
|
|
|
using System.Linq;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
|
|
|
|
|
{
|
2022-07-06 18:37:36 +02:00
|
|
|
|
using ResultCode = Ryujinx.HLE.HOS.Services.Vi.ResultCode;
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
class SurfaceFlinger : IConsumerListener, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private const int TargetFps = 60;
|
|
|
|
|
|
|
|
|
|
private Switch _device;
|
|
|
|
|
|
|
|
|
|
private Dictionary<long, Layer> _layers;
|
|
|
|
|
|
|
|
|
|
private bool _isRunning;
|
|
|
|
|
|
|
|
|
|
private Thread _composerThread;
|
|
|
|
|
|
|
|
|
|
private Stopwatch _chrono;
|
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
private ManualResetEvent _event = new ManualResetEvent(false);
|
|
|
|
|
private AutoResetEvent _nextFrameEvent = new AutoResetEvent(true);
|
2020-04-22 06:10:27 +02:00
|
|
|
|
private long _ticks;
|
|
|
|
|
private long _ticksPerFrame;
|
2020-12-17 19:39:52 +01:00
|
|
|
|
private long _spinTicks;
|
|
|
|
|
private long _1msTicks;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
private int _swapInterval;
|
2022-09-01 22:57:50 +02:00
|
|
|
|
private int _swapIntervalDelay;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
private readonly object Lock = new object();
|
|
|
|
|
|
2021-04-13 02:56:16 +02:00
|
|
|
|
public long RenderLayerId { get; private set; }
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
private class Layer
|
|
|
|
|
{
|
|
|
|
|
public int ProducerBinderId;
|
|
|
|
|
public IGraphicBufferProducer Producer;
|
|
|
|
|
public BufferItemConsumer Consumer;
|
2020-12-02 00:23:43 +01:00
|
|
|
|
public BufferQueueCore Core;
|
2022-02-09 21:18:07 +01:00
|
|
|
|
public ulong Owner;
|
2022-07-06 18:37:36 +02:00
|
|
|
|
public LayerState State;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TextureCallbackInformation
|
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
|
public Layer Layer;
|
|
|
|
|
public BufferItem Item;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SurfaceFlinger(Switch device)
|
|
|
|
|
{
|
|
|
|
|
_device = device;
|
|
|
|
|
_layers = new Dictionary<long, Layer>();
|
2021-04-13 02:56:16 +02:00
|
|
|
|
RenderLayerId = 0;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
_composerThread = new Thread(HandleComposition)
|
|
|
|
|
{
|
|
|
|
|
Name = "SurfaceFlinger.Composer"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_chrono = new Stopwatch();
|
2020-12-17 19:39:52 +01:00
|
|
|
|
_chrono.Start();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
_ticks = 0;
|
2020-12-17 19:39:52 +01:00
|
|
|
|
_spinTicks = Stopwatch.Frequency / 500;
|
|
|
|
|
_1msTicks = Stopwatch.Frequency / 1000;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
UpdateSwapInterval(1);
|
|
|
|
|
|
|
|
|
|
_composerThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateSwapInterval(int swapInterval)
|
|
|
|
|
{
|
|
|
|
|
_swapInterval = swapInterval;
|
|
|
|
|
|
|
|
|
|
// If the swap interval is 0, Game VSync is disabled.
|
|
|
|
|
if (_swapInterval == 0)
|
|
|
|
|
{
|
2020-12-17 19:39:52 +01:00
|
|
|
|
_nextFrameEvent.Set();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
_ticksPerFrame = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-09-01 22:57:50 +02:00
|
|
|
|
_ticksPerFrame = Stopwatch.Frequency / TargetFps;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 18:37:36 +02:00
|
|
|
|
public IGraphicBufferProducer CreateLayer(out long layerId, ulong pid, LayerState initialState = LayerState.ManagedClosed)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
|
|
|
|
layerId = 1;
|
|
|
|
|
|
|
|
|
|
lock (Lock)
|
|
|
|
|
{
|
|
|
|
|
foreach (KeyValuePair<long, Layer> pair in _layers)
|
|
|
|
|
{
|
|
|
|
|
if (pair.Key >= layerId)
|
|
|
|
|
{
|
|
|
|
|
layerId = pair.Key + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 18:37:36 +02:00
|
|
|
|
CreateLayerFromId(pid, layerId, initialState);
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
return GetProducerByLayerId(layerId);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 18:37:36 +02:00
|
|
|
|
private void CreateLayerFromId(ulong pid, long layerId, LayerState initialState)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
|
|
|
|
lock (Lock)
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Info?.Print(LogClass.SurfaceFlinger, $"Creating layer {layerId}");
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
|
BufferQueueCore core = BufferQueue.CreateBufferQueue(_device, pid, out BufferQueueProducer producer, out BufferQueueConsumer consumer);
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
core.BufferQueued += () =>
|
|
|
|
|
{
|
|
|
|
|
_nextFrameEvent.Set();
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
_layers.Add(layerId, new Layer
|
|
|
|
|
{
|
|
|
|
|
ProducerBinderId = HOSBinderDriverServer.RegisterBinderObject(producer),
|
|
|
|
|
Producer = producer,
|
|
|
|
|
Consumer = new BufferItemConsumer(_device, consumer, 0, -1, false, this),
|
2020-12-02 00:23:43 +01:00
|
|
|
|
Core = core,
|
2022-07-06 18:37:36 +02:00
|
|
|
|
Owner = pid,
|
|
|
|
|
State = initialState
|
2020-04-22 06:10:27 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 18:37:36 +02:00
|
|
|
|
public ResultCode OpenLayer(ulong pid, long layerId, out IBinder producer)
|
|
|
|
|
{
|
|
|
|
|
Layer layer = GetLayerByIdLocked(layerId);
|
|
|
|
|
|
|
|
|
|
if (layer == null || layer.State != LayerState.ManagedClosed)
|
|
|
|
|
{
|
|
|
|
|
producer = null;
|
|
|
|
|
|
|
|
|
|
return ResultCode.InvalidArguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layer.State = LayerState.ManagedOpened;
|
|
|
|
|
producer = layer.Producer;
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultCode CloseLayer(long layerId)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
|
|
|
|
lock (Lock)
|
|
|
|
|
{
|
|
|
|
|
Layer layer = GetLayerByIdLocked(layerId);
|
|
|
|
|
|
2022-07-06 18:37:36 +02:00
|
|
|
|
if (layer == null)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
2022-07-06 18:37:36 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, $"Failed to close layer {layerId}");
|
|
|
|
|
|
|
|
|
|
return ResultCode.InvalidValue;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 18:37:36 +02:00
|
|
|
|
CloseLayer(layerId, layer);
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-13 02:56:16 +02:00
|
|
|
|
|
2022-07-06 18:37:36 +02:00
|
|
|
|
public ResultCode DestroyManagedLayer(long layerId)
|
|
|
|
|
{
|
|
|
|
|
lock (Lock)
|
|
|
|
|
{
|
|
|
|
|
Layer layer = GetLayerByIdLocked(layerId);
|
|
|
|
|
|
|
|
|
|
if (layer == null)
|
2021-04-13 02:56:16 +02:00
|
|
|
|
{
|
2022-07-06 18:37:36 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, $"Failed to destroy managed layer {layerId} (not found)");
|
|
|
|
|
|
|
|
|
|
return ResultCode.InvalidValue;
|
2021-04-13 02:56:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 18:37:36 +02:00
|
|
|
|
if (layer.State != LayerState.ManagedClosed && layer.State != LayerState.ManagedOpened)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, $"Failed to destroy managed layer {layerId} (permission denied)");
|
|
|
|
|
|
|
|
|
|
return ResultCode.PermissionDenied;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HOSBinderDriverServer.UnregisterBinderObject(layer.ProducerBinderId);
|
|
|
|
|
|
|
|
|
|
if (_layers.Remove(layerId) && layer.State == LayerState.ManagedOpened)
|
|
|
|
|
{
|
|
|
|
|
CloseLayer(layerId, layer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultCode DestroyStrayLayer(long layerId)
|
|
|
|
|
{
|
|
|
|
|
lock (Lock)
|
|
|
|
|
{
|
|
|
|
|
Layer layer = GetLayerByIdLocked(layerId);
|
|
|
|
|
|
|
|
|
|
if (layer == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, $"Failed to destroy stray layer {layerId} (not found)");
|
|
|
|
|
|
|
|
|
|
return ResultCode.InvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (layer.State != LayerState.Stray)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, $"Failed to destroy stray layer {layerId} (permission denied)");
|
|
|
|
|
|
|
|
|
|
return ResultCode.PermissionDenied;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HOSBinderDriverServer.UnregisterBinderObject(layer.ProducerBinderId);
|
|
|
|
|
|
|
|
|
|
if (_layers.Remove(layerId))
|
|
|
|
|
{
|
|
|
|
|
CloseLayer(layerId, layer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CloseLayer(long layerId, Layer layer)
|
|
|
|
|
{
|
|
|
|
|
// If the layer was removed and the current in use, we need to change the current layer in use.
|
|
|
|
|
if (RenderLayerId == layerId)
|
|
|
|
|
{
|
|
|
|
|
// If no layer is availaible, reset to default value.
|
|
|
|
|
if (_layers.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
SetRenderLayer(0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetRenderLayer(_layers.Last().Key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (layer.State == LayerState.ManagedOpened)
|
|
|
|
|
{
|
|
|
|
|
layer.State = LayerState.ManagedClosed;
|
2021-04-13 02:56:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetRenderLayer(long layerId)
|
|
|
|
|
{
|
|
|
|
|
lock (Lock)
|
|
|
|
|
{
|
|
|
|
|
RenderLayerId = layerId;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Layer GetLayerByIdLocked(long layerId)
|
|
|
|
|
{
|
|
|
|
|
foreach (KeyValuePair<long, Layer> pair in _layers)
|
|
|
|
|
{
|
|
|
|
|
if (pair.Key == layerId)
|
|
|
|
|
{
|
|
|
|
|
return pair.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IGraphicBufferProducer GetProducerByLayerId(long layerId)
|
|
|
|
|
{
|
|
|
|
|
lock (Lock)
|
|
|
|
|
{
|
|
|
|
|
Layer layer = GetLayerByIdLocked(layerId);
|
|
|
|
|
|
|
|
|
|
if (layer != null)
|
|
|
|
|
{
|
|
|
|
|
return layer.Producer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleComposition()
|
|
|
|
|
{
|
|
|
|
|
_isRunning = true;
|
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
long lastTicks = _chrono.ElapsedTicks;
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
while (_isRunning)
|
|
|
|
|
{
|
2020-12-17 19:39:52 +01:00
|
|
|
|
long ticks = _chrono.ElapsedTicks;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
if (_swapInterval == 0)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
|
|
|
|
Compose();
|
|
|
|
|
|
2020-05-15 03:30:08 +02:00
|
|
|
|
_device.System?.SignalVsync();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
_nextFrameEvent.WaitOne(17);
|
|
|
|
|
lastTicks = ticks;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
2020-12-17 19:39:52 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_ticks += ticks - lastTicks;
|
|
|
|
|
lastTicks = ticks;
|
|
|
|
|
|
|
|
|
|
if (_ticks >= _ticksPerFrame)
|
|
|
|
|
{
|
2022-09-01 22:57:50 +02:00
|
|
|
|
if (_swapIntervalDelay-- == 0)
|
|
|
|
|
{
|
|
|
|
|
Compose();
|
|
|
|
|
|
|
|
|
|
// When a frame is presented, delay the next one by its swap interval value.
|
|
|
|
|
_swapIntervalDelay = Math.Max(0, _swapInterval - 1);
|
|
|
|
|
}
|
2020-12-17 19:39:52 +01:00
|
|
|
|
|
|
|
|
|
_device.System?.SignalVsync();
|
|
|
|
|
|
|
|
|
|
// Apply a maximum bound of 3 frames to the tick remainder, in case some event causes Ryujinx to pause for a long time or messes with the timer.
|
|
|
|
|
_ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame * 3);
|
|
|
|
|
}
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
// Sleep if possible. If the time til the next frame is too low, spin wait instead.
|
|
|
|
|
long diff = _ticksPerFrame - (_ticks + _chrono.ElapsedTicks - ticks);
|
|
|
|
|
if (diff > 0)
|
|
|
|
|
{
|
|
|
|
|
if (diff < _spinTicks)
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
// SpinWait is a little more HT/SMT friendly than aggressively updating/checking ticks.
|
|
|
|
|
// The value of 5 still gives us quite a bit of precision (~0.0003ms variance at worst) while waiting a reasonable amount of time.
|
|
|
|
|
Thread.SpinWait(5);
|
|
|
|
|
|
|
|
|
|
ticks = _chrono.ElapsedTicks;
|
|
|
|
|
_ticks += ticks - lastTicks;
|
|
|
|
|
lastTicks = ticks;
|
|
|
|
|
} while (_ticks < _ticksPerFrame);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_event.WaitOne((int)(diff / _1msTicks));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Compose()
|
|
|
|
|
{
|
|
|
|
|
lock (Lock)
|
|
|
|
|
{
|
|
|
|
|
// TODO: support multilayers (& multidisplay ?)
|
2021-04-13 02:56:16 +02:00
|
|
|
|
if (RenderLayerId == 0)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 02:56:16 +02:00
|
|
|
|
Layer layer = GetLayerByIdLocked(RenderLayerId);
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
Status acquireStatus = layer.Consumer.AcquireBuffer(out BufferItem item, 0);
|
|
|
|
|
|
|
|
|
|
if (acquireStatus == Status.Success)
|
|
|
|
|
{
|
|
|
|
|
// If device vsync is disabled, reflect the change.
|
|
|
|
|
if (!_device.EnableDeviceVsync)
|
|
|
|
|
{
|
|
|
|
|
if (_swapInterval != 0)
|
|
|
|
|
{
|
|
|
|
|
UpdateSwapInterval(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (item.SwapInterval != _swapInterval)
|
|
|
|
|
{
|
|
|
|
|
UpdateSwapInterval(item.SwapInterval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PostFrameBuffer(layer, item);
|
|
|
|
|
}
|
|
|
|
|
else if (acquireStatus != Status.NoBufferAvailaible && acquireStatus != Status.InvalidOperation)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PostFrameBuffer(Layer layer, BufferItem item)
|
2020-07-26 05:03:40 +02:00
|
|
|
|
{
|
2020-04-22 06:10:27 +02:00
|
|
|
|
int frameBufferWidth = item.GraphicBuffer.Object.Width;
|
|
|
|
|
int frameBufferHeight = item.GraphicBuffer.Object.Height;
|
|
|
|
|
|
|
|
|
|
int nvMapHandle = item.GraphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle;
|
|
|
|
|
|
|
|
|
|
if (nvMapHandle == 0)
|
|
|
|
|
{
|
|
|
|
|
nvMapHandle = item.GraphicBuffer.Object.Buffer.NvMapId;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:03:33 +01:00
|
|
|
|
ulong bufferOffset = (ulong)item.GraphicBuffer.Object.Buffer.Surfaces[0].Offset;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
NvMapHandle map = NvMapDeviceFile.GetMapFromHandle(layer.Owner, nvMapHandle);
|
|
|
|
|
|
2021-01-01 23:03:33 +01:00
|
|
|
|
ulong frameBufferAddress = map.Address + bufferOffset;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
Format format = ConvertColorFormat(item.GraphicBuffer.Object.Buffer.Surfaces[0].ColorFormat);
|
|
|
|
|
|
|
|
|
|
int bytesPerPixel =
|
|
|
|
|
format == Format.B5G6R5Unorm ||
|
|
|
|
|
format == Format.R4G4B4A4Unorm ? 2 : 4;
|
|
|
|
|
|
|
|
|
|
int gobBlocksInY = 1 << item.GraphicBuffer.Object.Buffer.Surfaces[0].BlockHeightLog2;
|
|
|
|
|
|
|
|
|
|
// Note: Rotation is being ignored.
|
|
|
|
|
Rect cropRect = item.Crop;
|
|
|
|
|
|
|
|
|
|
bool flipX = item.Transform.HasFlag(NativeWindowTransform.FlipX);
|
|
|
|
|
bool flipY = item.Transform.HasFlag(NativeWindowTransform.FlipY);
|
|
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
|
AspectRatio aspectRatio = _device.Configuration.AspectRatio;
|
2020-12-16 03:19:07 +01:00
|
|
|
|
bool isStretched = aspectRatio == AspectRatio.Stretched;
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
ImageCrop crop = new ImageCrop(
|
|
|
|
|
cropRect.Left,
|
|
|
|
|
cropRect.Right,
|
|
|
|
|
cropRect.Top,
|
|
|
|
|
cropRect.Bottom,
|
|
|
|
|
flipX,
|
2020-12-16 03:19:07 +01:00
|
|
|
|
flipY,
|
|
|
|
|
isStretched,
|
|
|
|
|
aspectRatio.ToFloatX(),
|
|
|
|
|
aspectRatio.ToFloatY());
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
TextureCallbackInformation textureCallbackInformation = new TextureCallbackInformation
|
|
|
|
|
{
|
|
|
|
|
Layer = layer,
|
2021-01-02 23:21:44 +01:00
|
|
|
|
Item = item
|
2020-04-22 06:10:27 +02:00
|
|
|
|
};
|
|
|
|
|
|
2022-10-02 23:50:03 +02:00
|
|
|
|
if (_device.Gpu.Window.EnqueueFrameThreadSafe(
|
2021-06-29 19:32:02 +02:00
|
|
|
|
layer.Owner,
|
2020-04-22 06:10:27 +02:00
|
|
|
|
frameBufferAddress,
|
|
|
|
|
frameBufferWidth,
|
|
|
|
|
frameBufferHeight,
|
|
|
|
|
0,
|
|
|
|
|
false,
|
|
|
|
|
gobBlocksInY,
|
|
|
|
|
format,
|
|
|
|
|
bytesPerPixel,
|
|
|
|
|
crop,
|
|
|
|
|
AcquireBuffer,
|
|
|
|
|
ReleaseBuffer,
|
2022-10-02 23:50:03 +02:00
|
|
|
|
textureCallbackInformation))
|
2021-10-12 23:29:50 +02:00
|
|
|
|
{
|
2022-10-02 23:50:03 +02:00
|
|
|
|
if (item.Fence.FenceCount == 0)
|
2021-10-12 23:29:50 +02:00
|
|
|
|
{
|
|
|
|
|
_device.Gpu.Window.SignalFrameReady();
|
|
|
|
|
_device.Gpu.GPFifo.Interrupt();
|
2022-10-02 23:50:03 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item.Fence.RegisterCallback(_device.Gpu, (x) =>
|
|
|
|
|
{
|
|
|
|
|
_device.Gpu.Window.SignalFrameReady();
|
|
|
|
|
_device.Gpu.GPFifo.Interrupt();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ReleaseBuffer(textureCallbackInformation);
|
2021-10-12 23:29:50 +02:00
|
|
|
|
}
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReleaseBuffer(object obj)
|
|
|
|
|
{
|
|
|
|
|
ReleaseBuffer((TextureCallbackInformation)obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReleaseBuffer(TextureCallbackInformation information)
|
|
|
|
|
{
|
2020-05-02 22:47:06 +02:00
|
|
|
|
AndroidFence fence = AndroidFence.NoFence;
|
|
|
|
|
|
|
|
|
|
information.Layer.Consumer.ReleaseBuffer(information.Item, ref fence);
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AcquireBuffer(GpuContext ignored, object obj)
|
|
|
|
|
{
|
|
|
|
|
AcquireBuffer((TextureCallbackInformation)obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AcquireBuffer(TextureCallbackInformation information)
|
|
|
|
|
{
|
|
|
|
|
information.Item.Fence.WaitForever(_device.Gpu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Format ConvertColorFormat(ColorFormat colorFormat)
|
|
|
|
|
{
|
|
|
|
|
return colorFormat switch
|
|
|
|
|
{
|
|
|
|
|
ColorFormat.A8B8G8R8 => Format.R8G8B8A8Unorm,
|
|
|
|
|
ColorFormat.X8B8G8R8 => Format.R8G8B8A8Unorm,
|
|
|
|
|
ColorFormat.R5G6B5 => Format.B5G6R5Unorm,
|
|
|
|
|
ColorFormat.A8R8G8B8 => Format.B8G8R8A8Unorm,
|
|
|
|
|
ColorFormat.A4B4G4R4 => Format.R4G4B4A4Unorm,
|
|
|
|
|
_ => throw new NotImplementedException($"Color Format \"{colorFormat}\" not implemented!"),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_isRunning = false;
|
2020-12-02 00:23:43 +01:00
|
|
|
|
|
|
|
|
|
foreach (Layer layer in _layers.Values)
|
|
|
|
|
{
|
|
|
|
|
layer.Core.PrepareForExit();
|
|
|
|
|
}
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnFrameAvailable(ref BufferItem item)
|
|
|
|
|
{
|
|
|
|
|
_device.Statistics.RecordGameFrameTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnFrameReplaced(ref BufferItem item)
|
|
|
|
|
{
|
|
|
|
|
_device.Statistics.RecordGameFrameTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnBuffersReleased() {}
|
|
|
|
|
}
|
|
|
|
|
}
|