2020-07-12 05:07:01 +02:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.Graphics.Device;
|
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
|
|
|
|
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;
|
|
|
|
|
private ConcurrentDictionary<long, NvdecDecoderContext> _contexts;
|
|
|
|
|
private NvdecDecoderContext _currentContext;
|
2020-07-12 05:07:01 +02: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-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)
|
|
|
|
|
{
|
|
|
|
|
Decode((CodecId)_state.State.SetCodecID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Decode(CodecId codecId)
|
|
|
|
|
{
|
|
|
|
|
switch (codecId)
|
|
|
|
|
{
|
|
|
|
|
case CodecId.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;
|
|
|
|
|
case CodecId.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:
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.Nvdec, $"Unsupported codec \"{codecId}\".");
|
2020-07-12 05:07:01 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|