2023-12-04 14:17:13 +01:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-07-12 05:07:01 +02:00
|
|
|
using Ryujinx.Graphics.Device;
|
|
|
|
using Ryujinx.Graphics.Nvdec.Image;
|
2021-09-29 00:43:40 +02:00
|
|
|
using System.Collections.Concurrent;
|
2020-07-12 05:07:01 +02:00
|
|
|
using System.Collections.Generic;
|
2021-09-29 00:43:40 +02:00
|
|
|
using System.Threading;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Nvdec
|
|
|
|
{
|
2021-09-29 00:43:40 +02:00
|
|
|
public class NvdecDevice : IDeviceStateWithContext
|
2020-07-12 05:07:01 +02:00
|
|
|
{
|
|
|
|
private readonly ResourceManager _rm;
|
|
|
|
private readonly DeviceState<NvdecRegisters> _state;
|
|
|
|
|
2021-09-29 00:43:40 +02:00
|
|
|
private long _currentId;
|
2023-06-25 21:44:42 +02:00
|
|
|
private readonly ConcurrentDictionary<long, NvdecDecoderContext> _contexts;
|
2021-09-29 00:43:40 +02:00
|
|
|
private NvdecDecoderContext _currentContext;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
2024-01-22 21:14:46 +01:00
|
|
|
public NvdecDevice(DeviceMemoryManager mm)
|
2020-07-12 05:07:01 +02:00
|
|
|
{
|
2024-01-22 21:14:46 +01:00
|
|
|
_rm = new ResourceManager(mm, new SurfaceCache(mm));
|
2020-07-12 05:07:01 +02:00
|
|
|
_state = new DeviceState<NvdecRegisters>(new Dictionary<string, RwCallback>
|
|
|
|
{
|
2023-07-24 18:35:04 +02:00
|
|
|
{ nameof(NvdecRegisters.Execute), new RwCallback(Execute, null) },
|
2020-07-12 05:07:01 +02:00
|
|
|
});
|
2021-09-29 00:43:40 +02:00
|
|
|
_contexts = new ConcurrentDictionary<long, NvdecDecoderContext>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public long CreateContext()
|
|
|
|
{
|
|
|
|
long id = Interlocked.Increment(ref _currentId);
|
|
|
|
_contexts.TryAdd(id, new NvdecDecoderContext());
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DestroyContext(long id)
|
|
|
|
{
|
|
|
|
if (_contexts.TryRemove(id, out var context))
|
|
|
|
{
|
|
|
|
context.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
_rm.Cache.Trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void BindContext(long id)
|
|
|
|
{
|
|
|
|
if (_contexts.TryGetValue(id, out var context))
|
|
|
|
{
|
|
|
|
_currentContext = context;
|
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int Read(int offset) => _state.Read(offset);
|
|
|
|
public void Write(int offset, int data) => _state.Write(offset, data);
|
|
|
|
|
|
|
|
private void Execute(int data)
|
|
|
|
{
|
2023-01-02 15:48:46 +01:00
|
|
|
Decode((ApplicationId)_state.State.SetApplicationId);
|
2020-07-12 05:07:01 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 15:48:46 +01:00
|
|
|
private void Decode(ApplicationId applicationId)
|
2020-07-12 05:07:01 +02:00
|
|
|
{
|
2023-01-02 15:48:46 +01:00
|
|
|
switch (applicationId)
|
2020-07-12 05:07:01 +02:00
|
|
|
{
|
2023-01-02 15:48:46 +01:00
|
|
|
case ApplicationId.H264:
|
2021-09-29 00:43:40 +02:00
|
|
|
H264Decoder.Decode(_currentContext, _rm, ref _state.State);
|
2020-07-12 05:07:01 +02:00
|
|
|
break;
|
2023-01-02 15:48:46 +01:00
|
|
|
case ApplicationId.Vp8:
|
2021-10-12 22:55:57 +02:00
|
|
|
Vp8Decoder.Decode(_currentContext, _rm, ref _state.State);
|
|
|
|
break;
|
2023-01-02 15:48:46 +01:00
|
|
|
case ApplicationId.Vp9:
|
2021-09-29 00:43:40 +02:00
|
|
|
Vp9Decoder.Decode(_rm, ref _state.State);
|
2020-07-12 05:07:01 +02:00
|
|
|
break;
|
|
|
|
default:
|
2023-01-02 15:48:46 +01:00
|
|
|
Logger.Error?.Print(LogClass.Nvdec, $"Unsupported codec \"{applicationId}\".");
|
2020-07-12 05:07:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|