2020-07-11 23:07:01 -04:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.Graphics.Device;
|
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
|
|
|
|
using Ryujinx.Graphics.Nvdec.Image;
|
2021-09-28 18:43:40 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-07-11 23:07:01 -04:00
|
|
|
|
using System.Collections.Generic;
|
2021-09-28 18:43:40 -04:00
|
|
|
|
using System.Threading;
|
2020-07-11 23:07:01 -04:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Nvdec
|
|
|
|
|
{
|
2021-09-28 18:43:40 -04:00
|
|
|
|
public class NvdecDevice : IDeviceStateWithContext
|
2020-07-11 23:07:01 -04:00
|
|
|
|
{
|
|
|
|
|
private readonly ResourceManager _rm;
|
|
|
|
|
private readonly DeviceState<NvdecRegisters> _state;
|
|
|
|
|
|
2021-09-28 18:43:40 -04:00
|
|
|
|
private long _currentId;
|
2023-06-25 15:44:42 -04:00
|
|
|
|
private readonly ConcurrentDictionary<long, NvdecDecoderContext> _contexts;
|
2021-09-28 18:43:40 -04:00
|
|
|
|
private NvdecDecoderContext _currentContext;
|
2020-07-11 23:07:01 -04:00
|
|
|
|
|
|
|
|
|
public NvdecDevice(MemoryManager gmm)
|
|
|
|
|
{
|
|
|
|
|
_rm = new ResourceManager(gmm, new SurfaceCache(gmm));
|
|
|
|
|
_state = new DeviceState<NvdecRegisters>(new Dictionary<string, RwCallback>
|
|
|
|
|
{
|
2023-07-24 12:35:04 -04:00
|
|
|
|
{ nameof(NvdecRegisters.Execute), new RwCallback(Execute, null) },
|
2020-07-11 23:07:01 -04:00
|
|
|
|
});
|
2021-09-28 18:43:40 -04: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-11 23:07:01 -04: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 09:48:46 -05:00
|
|
|
|
Decode((ApplicationId)_state.State.SetApplicationId);
|
2020-07-11 23:07:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 09:48:46 -05:00
|
|
|
|
private void Decode(ApplicationId applicationId)
|
2020-07-11 23:07:01 -04:00
|
|
|
|
{
|
2023-01-02 09:48:46 -05:00
|
|
|
|
switch (applicationId)
|
2020-07-11 23:07:01 -04:00
|
|
|
|
{
|
2023-01-02 09:48:46 -05:00
|
|
|
|
case ApplicationId.H264:
|
2021-09-28 18:43:40 -04:00
|
|
|
|
H264Decoder.Decode(_currentContext, _rm, ref _state.State);
|
2020-07-11 23:07:01 -04:00
|
|
|
|
break;
|
2023-01-02 09:48:46 -05:00
|
|
|
|
case ApplicationId.Vp8:
|
2021-10-12 16:55:57 -04:00
|
|
|
|
Vp8Decoder.Decode(_currentContext, _rm, ref _state.State);
|
|
|
|
|
break;
|
2023-01-02 09:48:46 -05:00
|
|
|
|
case ApplicationId.Vp9:
|
2021-09-28 18:43:40 -04:00
|
|
|
|
Vp9Decoder.Decode(_rm, ref _state.State);
|
2020-07-11 23:07:01 -04:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-01-02 09:48:46 -05:00
|
|
|
|
Logger.Error?.Print(LogClass.Nvdec, $"Unsupported codec \"{applicationId}\".");
|
2020-07-11 23:07:01 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|