2018-10-17 17:15:50 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-03-12 04:04:52 +00:00
|
|
|
using Ryujinx.Graphics.Gal;
|
2018-09-18 04:30:35 +00:00
|
|
|
using Ryujinx.Graphics.Memory;
|
2018-12-18 05:33:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2019-09-19 00:45:11 +00:00
|
|
|
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuAS;
|
|
|
|
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap;
|
2018-02-23 21:48:27 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-03-20 20:00:00 +00:00
|
|
|
using System.IO;
|
2019-01-05 21:26:16 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2018-02-23 21:48:27 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
using static Ryujinx.HLE.HOS.Services.Android.Parcel;
|
2018-06-09 16:05:41 +00:00
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Android
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
|
|
|
class NvFlinger : IDisposable
|
|
|
|
{
|
2019-07-14 19:04:38 +00:00
|
|
|
private delegate ResultCode ServiceProcessParcel(ServiceCtx context, BinaryReader parcelReader);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private Dictionary<(string, int), ServiceProcessParcel> _commands;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private KEvent _binderEvent;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private IGalRenderer _renderer;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-02-23 21:48:27 +00:00
|
|
|
private const int BufferQueueCount = 0x40;
|
|
|
|
private const int BufferQueueMask = BufferQueueCount - 1;
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private BufferEntry[] _bufferQueue;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private AutoResetEvent _waitBufferFree;
|
2018-04-06 04:01:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private bool _disposed;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public NvFlinger(IGalRenderer renderer, KEvent binderEvent)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_commands = new Dictionary<(string, int), ServiceProcessParcel>
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x1), GbpRequestBuffer },
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x3), GbpDequeueBuffer },
|
2018-03-12 04:04:52 +00:00
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x4), GbpDetachBuffer },
|
2018-02-23 21:48:27 +00:00
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x7), GbpQueueBuffer },
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x8), GbpCancelBuffer },
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0x9), GbpQuery },
|
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0xa), GbpConnect },
|
2018-03-06 20:27:50 +00:00
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0xb), GbpDisconnect },
|
2018-02-23 21:48:27 +00:00
|
|
|
{ ("android.gui.IGraphicBufferProducer", 0xe), GbpPreallocBuffer }
|
|
|
|
};
|
2018-04-06 04:01:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_renderer = renderer;
|
|
|
|
_binderEvent = binderEvent;
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_bufferQueue = new BufferEntry[0x40];
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitBufferFree = new AutoResetEvent(false);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
public ResultCode ProcessParcelRequest(ServiceCtx context, byte[] parcelData, int code)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
using (MemoryStream ms = new MemoryStream(parcelData))
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
BinaryReader reader = new BinaryReader(ms);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
ms.Seek(4, SeekOrigin.Current);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int strSize = reader.ReadInt32();
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
string interfaceName = Encoding.Unicode.GetString(reader.ReadBytes(strSize * 2));
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
long remainder = ms.Position & 0xf;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (remainder != 0)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
ms.Seek(0x10 - remainder, SeekOrigin.Current);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
ms.Seek(0x50, SeekOrigin.Begin);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (_commands.TryGetValue((interfaceName, code), out ServiceProcessParcel procReq))
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Logger.PrintDebug(LogClass.ServiceVi, $"{interfaceName} {procReq.Method.Name}");
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return procReq(context, reader);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
throw new NotImplementedException($"{interfaceName} {code}");
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpRequestBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
int slot = parcelReader.ReadInt32();
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-04-06 04:01:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
BufferEntry entry = _bufferQueue[slot];
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int bufferCount = 1; //?
|
|
|
|
long bufferSize = entry.Data.Size;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
writer.Write(bufferCount);
|
|
|
|
writer.Write(bufferSize);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
entry.Data.Write(writer);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
writer.Write(0);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, ms.ToArray());
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpDequeueBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2019-07-02 02:39:22 +00:00
|
|
|
// TODO: Errors.
|
2018-12-06 11:16:24 +00:00
|
|
|
int format = parcelReader.ReadInt32();
|
|
|
|
int width = parcelReader.ReadInt32();
|
|
|
|
int height = parcelReader.ReadInt32();
|
|
|
|
int getTimestamps = parcelReader.ReadInt32();
|
|
|
|
int usage = parcelReader.ReadInt32();
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int slot = GetFreeSlotBlocking(width, height);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, slot, 1, 0x24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpQueueBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Device.Statistics.RecordGameFrameTime();
|
2018-03-06 20:18:49 +00:00
|
|
|
|
2019-07-02 02:39:22 +00:00
|
|
|
// TODO: Errors.
|
2018-12-06 11:16:24 +00:00
|
|
|
int slot = parcelReader.ReadInt32();
|
2019-01-05 21:26:16 +00:00
|
|
|
|
|
|
|
long Position = parcelReader.BaseStream.Position;
|
|
|
|
|
|
|
|
QueueBufferObject queueBufferObject = ReadFlattenedObject<QueueBufferObject>(parcelReader);
|
|
|
|
|
|
|
|
parcelReader.BaseStream.Position = Position;
|
|
|
|
|
|
|
|
_bufferQueue[slot].Transform = queueBufferObject.Transform;
|
|
|
|
_bufferQueue[slot].Crop = queueBufferObject.Crop;
|
2018-12-06 11:16:24 +00:00
|
|
|
|
|
|
|
_bufferQueue[slot].State = BufferState.Queued;
|
|
|
|
|
|
|
|
SendFrameBuffer(context, slot);
|
|
|
|
|
|
|
|
if (context.Device.EnableDeviceVsync)
|
2018-09-09 23:38:56 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Device.VsyncEvent.WaitOne();
|
2018-09-09 23:38:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, 1280, 720, 0, 0, 0);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpDetachBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-03-12 04:04:52 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, 0);
|
2018-03-12 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpCancelBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2019-07-02 02:39:22 +00:00
|
|
|
// TODO: Errors.
|
2018-12-06 11:16:24 +00:00
|
|
|
int slot = parcelReader.ReadInt32();
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2019-01-05 21:26:16 +00:00
|
|
|
MultiFence fence = ReadFlattenedObject<MultiFence>(parcelReader);
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_bufferQueue[slot].State = BufferState.Free;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitBufferFree.Set();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, 0);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpQuery(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, 0, 0);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpConnect(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, 1280, 720, 0, 0, 0);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpDisconnect(ServiceCtx context, BinaryReader parcelReader)
|
2018-03-06 20:27:50 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, 0);
|
2018-03-06 20:27:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode GbpPreallocBuffer(ServiceCtx context, BinaryReader parcelReader)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
int slot = parcelReader.ReadInt32();
|
2018-04-06 04:01:52 +00:00
|
|
|
|
2019-01-05 21:26:16 +00:00
|
|
|
bool hasInput = parcelReader.ReadInt32() == 1;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2019-01-05 21:26:16 +00:00
|
|
|
if (hasInput)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-01-05 21:26:16 +00:00
|
|
|
byte[] graphicBuffer = ReadFlattenedObject(parcelReader);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_bufferQueue[slot].State = BufferState.Free;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-01-05 21:26:16 +00:00
|
|
|
using (BinaryReader graphicBufferReader = new BinaryReader(new MemoryStream(graphicBuffer)))
|
|
|
|
{
|
|
|
|
_bufferQueue[slot].Data = new GbpBuffer(graphicBufferReader);
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, 0);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 21:26:16 +00:00
|
|
|
private byte[] ReadFlattenedObject(BinaryReader reader)
|
|
|
|
{
|
|
|
|
long flattenedObjectSize = reader.ReadInt64();
|
|
|
|
|
|
|
|
return reader.ReadBytes((int)flattenedObjectSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
private unsafe T ReadFlattenedObject<T>(BinaryReader reader) where T: struct
|
|
|
|
{
|
|
|
|
byte[] data = ReadFlattenedObject(reader);
|
|
|
|
|
|
|
|
fixed (byte* ptr = data)
|
|
|
|
{
|
|
|
|
return Marshal.PtrToStructure<T>((IntPtr)ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode MakeReplyParcel(ServiceCtx context, params int[] ints)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
foreach (int Int in ints)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
writer.Write(Int);
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return MakeReplyParcel(context, ms.ToArray());
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
private ResultCode MakeReplyParcel(ServiceCtx context, byte[] data)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
(long replyPos, long replySize) = context.Request.GetBufferType0x22();
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
byte[] reply = MakeParcel(data, new byte[0]);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Memory.WriteBytes(replyPos, reply);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
return ResultCode.Success;
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 21:26:16 +00:00
|
|
|
private GalImageFormat ConvertColorFormat(ColorFormat colorFormat)
|
|
|
|
{
|
|
|
|
switch (colorFormat)
|
|
|
|
{
|
|
|
|
case ColorFormat.A8B8G8R8:
|
2019-03-04 01:45:25 +00:00
|
|
|
return GalImageFormat.Rgba8 | GalImageFormat.Unorm;
|
2019-01-05 21:26:16 +00:00
|
|
|
case ColorFormat.X8B8G8R8:
|
2019-03-04 01:45:25 +00:00
|
|
|
return GalImageFormat.Rgbx8 | GalImageFormat.Unorm;
|
2019-01-05 21:26:16 +00:00
|
|
|
case ColorFormat.R5G6B5:
|
2019-03-04 01:45:25 +00:00
|
|
|
return GalImageFormat.Bgr565 | GalImageFormat.Unorm;
|
2019-01-05 21:26:16 +00:00
|
|
|
case ColorFormat.A8R8G8B8:
|
2019-03-04 01:45:25 +00:00
|
|
|
return GalImageFormat.Bgra8 | GalImageFormat.Unorm;
|
2019-01-05 21:26:16 +00:00
|
|
|
case ColorFormat.A4B4G4R4:
|
2019-03-04 01:45:25 +00:00
|
|
|
return GalImageFormat.Rgba4 | GalImageFormat.Unorm;
|
2019-01-05 21:26:16 +00:00
|
|
|
default:
|
|
|
|
throw new NotImplementedException($"Color Format \"{colorFormat}\" not implemented!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support multi surface
|
2018-12-06 11:16:24 +00:00
|
|
|
private void SendFrameBuffer(ServiceCtx context, int slot)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2019-01-05 21:26:16 +00:00
|
|
|
int fbWidth = _bufferQueue[slot].Data.Header.Width;
|
|
|
|
int fbHeight = _bufferQueue[slot].Data.Header.Height;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2019-01-05 21:26:16 +00: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 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
NvMapHandle map = NvMapIoctl.GetNvMap(context, nvMapHandle);
|
2018-03-20 15:18:25 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
long fbAddr = map.Address + bufferOffset;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_bufferQueue[slot].State = BufferState.Acquired;
|
2018-03-01 02:37:40 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
Rect crop = _bufferQueue[slot].Crop;
|
2018-03-01 02:37:40 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
bool flipX = _bufferQueue[slot].Transform.HasFlag(HalTransform.FlipX);
|
|
|
|
bool flipY = _bufferQueue[slot].Transform.HasFlag(HalTransform.FlipY);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2019-01-05 21:26:16 +00:00
|
|
|
GalImageFormat imageFormat = ConvertColorFormat(_bufferQueue[slot].Data.Buffer.Surfaces[0].ColorFormat);
|
|
|
|
|
|
|
|
int BlockHeight = 1 << _bufferQueue[slot].Data.Buffer.Surfaces[0].BlockHeightLog2;
|
|
|
|
|
2019-07-02 02:39:22 +00:00
|
|
|
// Note: Rotation is being ignored.
|
2018-03-04 23:32:18 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int top = crop.Top;
|
|
|
|
int left = crop.Left;
|
|
|
|
int right = crop.Right;
|
|
|
|
int bottom = crop.Bottom;
|
2018-03-04 23:32:18 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_renderer.QueueAction(() =>
|
2018-04-13 18:12:58 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (!_renderer.Texture.TryGetImage(fbAddr, out GalImage image))
|
2018-09-18 04:30:35 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
image = new GalImage(
|
|
|
|
fbWidth,
|
2019-02-28 01:12:24 +00:00
|
|
|
fbHeight, 1, 1, 1, BlockHeight, 1,
|
2018-09-18 04:30:35 +00:00
|
|
|
GalMemoryLayout.BlockLinear,
|
2019-02-28 01:12:24 +00:00
|
|
|
imageFormat,
|
|
|
|
GalTextureTarget.TwoD);
|
2018-09-18 04:30:35 +00:00
|
|
|
}
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Device.Gpu.ResourceManager.ClearPbCache();
|
|
|
|
context.Device.Gpu.ResourceManager.SendTexture(vmm, fbAddr, image);
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_renderer.RenderTarget.SetTransform(flipX, flipY, top, left, right, bottom);
|
|
|
|
_renderer.RenderTarget.Present(fbAddr);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
ReleaseBuffer(slot);
|
2018-09-18 04:30:35 +00:00
|
|
|
});
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private void ReleaseBuffer(int slot)
|
2018-04-13 18:12:58 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_bufferQueue[slot].State = BufferState.Free;
|
2018-04-13 18:12:58 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_binderEvent.ReadableEvent.Signal();
|
2018-04-13 18:12:58 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitBufferFree.Set();
|
2018-04-13 18:12:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private int GetFreeSlotBlocking(int width, int height)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
int slot;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if ((slot = GetFreeSlot(width, height)) != -1)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-09-18 23:36:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (_disposed)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
break;
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitBufferFree.WaitOne();
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
while (!_disposed);
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return slot;
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private int GetFreeSlot(int width, int height)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
lock (_bufferQueue)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int slot = 0; slot < _bufferQueue.Length; slot++)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (_bufferQueue[slot].State != BufferState.Free)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
GbpBuffer data = _bufferQueue[slot].Data;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2019-01-05 21:26:16 +00:00
|
|
|
if (data.Header.Width == width &&
|
|
|
|
data.Header.Height == height)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_bufferQueue[slot].State = BufferState.Dequeued;
|
2018-02-23 21:48:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return slot;
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
protected virtual void Dispose(bool disposing)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (disposing && !_disposed)
|
2018-02-23 21:48:27 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_disposed = true;
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitBufferFree.Set();
|
|
|
|
_waitBufferFree.Dispose();
|
2018-02-23 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|