2020-04-18 18:25:57 -07:00
|
|
|
using Ryujinx.Common;
|
2018-10-17 10:15:50 -07:00
|
|
|
using Ryujinx.Common.Logging;
|
2019-10-12 23:02:07 -07:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2019-11-23 18:24:03 -08:00
|
|
|
using Ryujinx.Graphics.Gpu;
|
2018-12-17 21:33:36 -08:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2019-09-18 17:45:11 -07:00
|
|
|
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap;
|
2018-02-23 13:48:27 -08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-04-18 18:25:57 -07:00
|
|
|
using System.Diagnostics;
|
2018-03-20 13:00:00 -07:00
|
|
|
using System.IO;
|
2020-04-18 18:25:57 -07:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-01-05 13:26:16 -08:00
|
|
|
using System.Runtime.InteropServices;
|
2018-02-23 13:48:27 -08:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
2019-11-03 09:26:29 -08:00
|
|
|
using static Ryujinx.HLE.HOS.Services.SurfaceFlinger.Parcel;
|
2018-06-09 09:05:41 -07:00
|
|
|
|
2019-11-03 09:26:29 -08:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
|
|
|
class NvFlinger : IDisposable
|
|
|
|
{
|
2019-07-14 12:04:38 -07:00
|
|
|
private delegate ResultCode ServiceProcessParcel(ServiceCtx context, BinaryReader parcelReader);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private Dictionary<(string, int), ServiceProcessParcel> _commands;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private KEvent _binderEvent;
|
2018-03-19 11:58:46 -07:00
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
private IRenderer _renderer;
|
2018-03-19 11:58:46 -07:00
|
|
|
|
2018-02-23 13:48:27 -08:00
|
|
|
private const int BufferQueueCount = 0x40;
|
|
|
|
private const int BufferQueueMask = BufferQueueCount - 1;
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private BufferEntry[] _bufferQueue;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private AutoResetEvent _waitBufferFree;
|
2018-04-05 21:01:52 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private bool _disposed;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
public NvFlinger(IRenderer renderer, KEvent binderEvent)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_commands = new Dictionary<(string, int), ServiceProcessParcel>
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x1), GbpRequestBuffer },
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x3), GbpDequeueBuffer },
|
2018-03-11 21:04:52 -07:00
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x4), GbpDetachBuffer },
|
2018-02-23 13:48:27 -08:00
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x7), GbpQueueBuffer },
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x8), GbpCancelBuffer },
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x9), GbpQuery },
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0xa), GbpConnect },
|
2018-03-06 12:27:50 -08:00
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0xb), GbpDisconnect },
|
2018-02-23 13:48:27 -08:00
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0xe), GbpPreallocBuffer }
|
|
|
|
};
|
2018-04-05 21:01:52 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_renderer = renderer;
|
|
|
|
_binderEvent = binderEvent;
|
2018-03-11 21:04:52 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_bufferQueue = new BufferEntry[0x40];
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_waitBufferFree = new AutoResetEvent(false);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
public ResultCode ProcessParcelRequest(ServiceCtx context, byte[] parcelData, int code)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
using (MemoryStream ms = new MemoryStream(parcelData))
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
BinaryReader reader = new BinaryReader(ms);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
ms.Seek(4, SeekOrigin.Current);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
int strSize = reader.ReadInt32();
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
string interfaceName = Encoding.Unicode.GetString(reader.ReadBytes(strSize * 2));
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
long remainder = ms.Position & 0xf;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (remainder != 0)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
ms.Seek(0x10 - remainder, SeekOrigin.Current);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
ms.Seek(0x50, SeekOrigin.Begin);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (_commands.TryGetValue((interfaceName, code), out ServiceProcessParcel procReq))
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
Logger.PrintDebug(LogClass.ServiceVi, $"{interfaceName} {procReq.Method.Name}");
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return procReq(context, reader);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
throw new NotImplementedException($"{interfaceName} {code}");
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpRequestBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
int slot = parcelReader.ReadInt32();
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-04-05 21:01:52 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
BufferEntry entry = _bufferQueue[slot];
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
int bufferCount = 1; //?
|
|
|
|
long bufferSize = entry.Data.Size;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
writer.Write(bufferCount);
|
|
|
|
writer.Write(bufferSize);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
entry.Data.Write(writer);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
writer.Write(0);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, ms.ToArray());
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpDequeueBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2019-07-01 19:39:22 -07:00
|
|
|
// TODO: Errors.
|
2020-04-18 18:25:57 -07:00
|
|
|
int async = parcelReader.ReadInt32();
|
|
|
|
int width = parcelReader.ReadInt32();
|
|
|
|
int height = parcelReader.ReadInt32();
|
|
|
|
int format = parcelReader.ReadInt32();
|
|
|
|
int usage = parcelReader.ReadInt32();
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
int slot = GetFreeSlotBlocking(width, height);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2020-04-18 18:25:57 -07:00
|
|
|
MultiFence multiFence = MultiFence.NoFence;
|
|
|
|
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
|
|
{
|
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
|
|
|
|
|
|
|
// Allocated slot
|
|
|
|
writer.Write(slot);
|
|
|
|
|
|
|
|
// Has multi fence
|
|
|
|
writer.Write(1);
|
|
|
|
|
|
|
|
// Write the multi fnece
|
|
|
|
WriteFlattenedObject(writer, multiFence);
|
|
|
|
|
|
|
|
// Padding
|
|
|
|
writer.Write(0);
|
|
|
|
|
|
|
|
// Status
|
|
|
|
writer.Write(0);
|
|
|
|
|
|
|
|
return MakeReplyParcel(context, ms.ToArray());
|
|
|
|
}
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpQueueBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
context.Device.Statistics.RecordGameFrameTime();
|
2018-03-06 12:18:49 -08:00
|
|
|
|
2019-07-01 19:39:22 -07:00
|
|
|
// TODO: Errors.
|
2018-12-06 03:16:24 -08:00
|
|
|
int slot = parcelReader.ReadInt32();
|
2019-01-05 13:26:16 -08:00
|
|
|
|
|
|
|
long Position = parcelReader.BaseStream.Position;
|
|
|
|
|
|
|
|
QueueBufferObject queueBufferObject = ReadFlattenedObject<QueueBufferObject>(parcelReader);
|
|
|
|
|
|
|
|
parcelReader.BaseStream.Position = Position;
|
|
|
|
|
|
|
|
_bufferQueue[slot].Transform = queueBufferObject.Transform;
|
2020-04-18 18:25:57 -07:00
|
|
|
_bufferQueue[slot].Fence = queueBufferObject.Fence;
|
2019-01-05 13:26:16 -08:00
|
|
|
_bufferQueue[slot].Crop = queueBufferObject.Crop;
|
2020-04-18 18:25:57 -07:00
|
|
|
_bufferQueue[slot].State = BufferState.Queued;
|
2018-12-06 03:16:24 -08:00
|
|
|
|
|
|
|
SendFrameBuffer(context, slot);
|
|
|
|
|
|
|
|
if (context.Device.EnableDeviceVsync)
|
2018-09-09 16:38:56 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
context.Device.VsyncEvent.WaitOne();
|
2018-09-09 16:38:56 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, 1280, 720, 0, 0, 0);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpDetachBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-03-11 21:04:52 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, 0);
|
2018-03-11 21:04:52 -07:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpCancelBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2019-07-01 19:39:22 -07:00
|
|
|
// TODO: Errors.
|
2018-12-06 03:16:24 -08:00
|
|
|
int slot = parcelReader.ReadInt32();
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2019-01-05 13:26:16 -08:00
|
|
|
MultiFence fence = ReadFlattenedObject<MultiFence>(parcelReader);
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_bufferQueue[slot].State = BufferState.Free;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_waitBufferFree.Set();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, 0);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpQuery(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, 0, 0);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpConnect(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, 1280, 720, 0, 0, 0);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpDisconnect(ServiceCtx context, BinaryReader parcelReader)
|
2018-03-06 12:27:50 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, 0);
|
2018-03-06 12:27:50 -08:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode GbpPreallocBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
int slot = parcelReader.ReadInt32();
|
2018-04-05 21:01:52 -07:00
|
|
|
|
2019-01-05 13:26:16 -08:00
|
|
|
bool hasInput = parcelReader.ReadInt32() == 1;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2019-01-05 13:26:16 -08:00
|
|
|
if (hasInput)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2019-01-05 13:26:16 -08:00
|
|
|
byte[] graphicBuffer = ReadFlattenedObject(parcelReader);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_bufferQueue[slot].State = BufferState.Free;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2019-01-05 13:26:16 -08:00
|
|
|
using (BinaryReader graphicBufferReader = new BinaryReader(new MemoryStream(graphicBuffer)))
|
|
|
|
{
|
|
|
|
_bufferQueue[slot].Data = new GbpBuffer(graphicBufferReader);
|
|
|
|
}
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, 0);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2019-01-05 13:26:16 -08:00
|
|
|
private byte[] ReadFlattenedObject(BinaryReader reader)
|
|
|
|
{
|
|
|
|
long flattenedObjectSize = reader.ReadInt64();
|
|
|
|
|
|
|
|
return reader.ReadBytes((int)flattenedObjectSize);
|
|
|
|
}
|
|
|
|
|
2020-04-18 18:25:57 -07:00
|
|
|
private T ReadFlattenedObject<T>(BinaryReader reader) where T: struct
|
2019-01-05 13:26:16 -08:00
|
|
|
{
|
2020-04-18 18:25:57 -07:00
|
|
|
long flattenedObjectSize = reader.ReadInt64();
|
2019-01-05 13:26:16 -08:00
|
|
|
|
2020-04-18 18:25:57 -07:00
|
|
|
Debug.Assert(flattenedObjectSize == Unsafe.SizeOf<T>());
|
|
|
|
|
|
|
|
return reader.ReadStruct<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private unsafe void WriteFlattenedObject<T>(BinaryWriter writer, T value) where T : struct
|
|
|
|
{
|
|
|
|
writer.Write(Unsafe.SizeOf<T>());
|
|
|
|
writer.WriteStruct(value);
|
2019-01-05 13:26:16 -08:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode MakeReplyParcel(ServiceCtx context, params int[] ints)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
foreach (int Int in ints)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
writer.Write(Int);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeReplyParcel(context, ms.ToArray());
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
private ResultCode MakeReplyParcel(ServiceCtx context, byte[] data)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
(long replyPos, long replySize) = context.Request.GetBufferType0x22();
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
byte[] reply = MakeParcel(data, new byte[0]);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
context.Memory.WriteBytes(replyPos, reply);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2019-07-14 12:04:38 -07:00
|
|
|
return ResultCode.Success;
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
private Format ConvertColorFormat(ColorFormat colorFormat)
|
2019-01-05 13:26:16 -08:00
|
|
|
{
|
|
|
|
switch (colorFormat)
|
|
|
|
{
|
|
|
|
case ColorFormat.A8B8G8R8:
|
2019-10-12 23:02:07 -07:00
|
|
|
return Format.R8G8B8A8Unorm;
|
2019-01-05 13:26:16 -08:00
|
|
|
case ColorFormat.X8B8G8R8:
|
2019-10-12 23:02:07 -07:00
|
|
|
return Format.R8G8B8A8Unorm;
|
2019-01-05 13:26:16 -08:00
|
|
|
case ColorFormat.R5G6B5:
|
2020-01-09 10:12:47 -08:00
|
|
|
return Format.B5G6R5Unorm;
|
2019-01-05 13:26:16 -08:00
|
|
|
case ColorFormat.A8R8G8B8:
|
2019-10-12 23:02:07 -07:00
|
|
|
return Format.B8G8R8A8Unorm;
|
2019-01-05 13:26:16 -08:00
|
|
|
case ColorFormat.A4B4G4R4:
|
2019-10-12 23:02:07 -07:00
|
|
|
return Format.R4G4B4A4Unorm;
|
2019-01-05 13:26:16 -08:00
|
|
|
default:
|
|
|
|
throw new NotImplementedException($"Color Format \"{colorFormat}\" not implemented!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support multi surface
|
2018-12-06 03:16:24 -08:00
|
|
|
private void SendFrameBuffer(ServiceCtx context, int slot)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2019-01-05 13:26:16 -08:00
|
|
|
int fbWidth = _bufferQueue[slot].Data.Header.Width;
|
|
|
|
int fbHeight = _bufferQueue[slot].Data.Header.Height;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2019-01-05 13:26:16 -08:00
|
|
|
int nvMapHandle = _bufferQueue[slot].Data.Buffer.Surfaces[0].NvMapHandle;
|
|
|
|
|
|
|
|
if (nvMapHandle == 0)
|
|
|
|
{
|
|
|
|
nvMapHandle = _bufferQueue[slot].Data.Buffer.NvMapId;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bufferOffset = _bufferQueue[slot].Data.Buffer.Surfaces[0].Offset;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2019-11-02 15:47:56 -07:00
|
|
|
NvMapHandle map = NvMapDeviceFile.GetMapFromHandle(context.Process, nvMapHandle);
|
2018-03-20 08:18:25 -07:00
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
ulong fbAddr = (ulong)(map.Address + bufferOffset);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_bufferQueue[slot].State = BufferState.Acquired;
|
2018-02-28 18:37:40 -08:00
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
Format format = ConvertColorFormat(_bufferQueue[slot].Data.Buffer.Surfaces[0].ColorFormat);
|
2019-01-05 13:26:16 -08:00
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
int bytesPerPixel =
|
2020-01-09 10:12:47 -08:00
|
|
|
format == Format.B5G6R5Unorm ||
|
2019-10-12 23:02:07 -07:00
|
|
|
format == Format.R4G4B4A4Unorm ? 2 : 4;
|
2019-01-05 13:26:16 -08:00
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
int gobBlocksInY = 1 << _bufferQueue[slot].Data.Buffer.Surfaces[0].BlockHeightLog2;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
// Note: Rotation is being ignored.
|
2018-03-11 21:04:52 -07:00
|
|
|
|
2019-11-23 18:24:03 -08:00
|
|
|
Rect cropRect = _bufferQueue[slot].Crop;
|
|
|
|
|
|
|
|
bool flipX = _bufferQueue[slot].Transform.HasFlag(HalTransform.FlipX);
|
|
|
|
bool flipY = _bufferQueue[slot].Transform.HasFlag(HalTransform.FlipY);
|
|
|
|
|
|
|
|
ImageCrop crop = new ImageCrop(
|
|
|
|
cropRect.Left,
|
|
|
|
cropRect.Right,
|
|
|
|
cropRect.Top,
|
|
|
|
cropRect.Bottom,
|
|
|
|
flipX,
|
|
|
|
flipY);
|
|
|
|
|
|
|
|
context.Device.Gpu.Window.EnqueueFrameThreadSafe(
|
2019-10-12 23:02:07 -07:00
|
|
|
fbAddr,
|
|
|
|
fbWidth,
|
|
|
|
fbHeight,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
gobBlocksInY,
|
|
|
|
format,
|
2019-11-23 18:24:03 -08:00
|
|
|
bytesPerPixel,
|
|
|
|
crop,
|
2020-04-18 18:25:57 -07:00
|
|
|
AcquireBuffer,
|
2019-11-23 18:24:03 -08:00
|
|
|
ReleaseBuffer,
|
|
|
|
slot);
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
2018-03-19 11:58:46 -07:00
|
|
|
|
2020-04-18 18:25:57 -07:00
|
|
|
private void AcquireBuffer(GpuContext context, object slot)
|
|
|
|
{
|
|
|
|
AcquireBuffer(context, (int)slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AcquireBuffer(GpuContext context, int slot)
|
|
|
|
{
|
|
|
|
_bufferQueue[slot].Fence.WaitForever(context);
|
|
|
|
}
|
|
|
|
|
2019-11-23 18:24:03 -08:00
|
|
|
private void ReleaseBuffer(object slot)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
2019-11-23 18:24:03 -08:00
|
|
|
ReleaseBuffer((int)slot);
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private void ReleaseBuffer(int slot)
|
2018-04-13 11:12:58 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_bufferQueue[slot].State = BufferState.Free;
|
2018-04-13 11:12:58 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_binderEvent.ReadableEvent.Signal();
|
2018-04-13 11:12:58 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_waitBufferFree.Set();
|
2018-04-13 11:12:58 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private int GetFreeSlotBlocking(int width, int height)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
int slot;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if ((slot = GetFreeSlot(width, height)) != -1)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-09-18 16:36:43 -07:00
|
|
|
break;
|
|
|
|
}
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (_disposed)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
break;
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_waitBufferFree.WaitOne();
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
2018-12-06 03:16:24 -08:00
|
|
|
while (!_disposed);
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return slot;
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private int GetFreeSlot(int width, int height)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
lock (_bufferQueue)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
for (int slot = 0; slot < _bufferQueue.Length; slot++)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (_bufferQueue[slot].State != BufferState.Free)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
GbpBuffer data = _bufferQueue[slot].Data;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2019-01-05 13:26:16 -08:00
|
|
|
if (data.Header.Width == width &&
|
|
|
|
data.Header.Height == height)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_bufferQueue[slot].State = BufferState.Dequeued;
|
2018-02-23 13:48:27 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return slot;
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
protected virtual void Dispose(bool disposing)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (disposing && !_disposed)
|
2018-02-23 13:48:27 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_disposed = true;
|
2018-03-11 21:04:52 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_waitBufferFree.Set();
|
|
|
|
_waitBufferFree.Dispose();
|
2018-02-23 13:48:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|