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