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