diff options
| author | Thog <me@thog.eu> | 2020-04-22 06:10:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-22 14:10:27 +1000 |
| commit | 36749c358d85dd0bc7f1e4a36a0b607fc0f724d5 (patch) | |
| tree | 0c24eb54082b80e8f15aa8138a3e85c5c4e89675 /Ryujinx.HLE/HOS/Services/SurfaceFlinger/Parcel.cs | |
| parent | 03711dd7b5d44e20fb45c728803ea6b9599dec87 (diff) | |
SurfaceFlinger v2 (#981)
* Rewrite SurfaceFlinger
Reimplement accurately SurfaceFlinger (based on my 8.1.0 reversing of it)
TODO: support swap interval properly and reintroduce disabled "game vsync" support.
* Some fixes for SetBufferCount
* uncomment a test from last commit
* SurfaceFlinger: don't free the graphic buffer in SetBufferCount
* SurfaceFlinger: Implement swap interval correctly
* SurfaceFlinger: Reintegrate Game VSync toggle
* SurfaceFlinger: do not push a fence on buffer release on the consumer side
* Revert "SurfaceFlinger: do not push a fence on buffer release on the consumer side"
This reverts commit 586b52b0bfab2d11f361f4b59ab7b7141020bbad.
* Make the game vsync toggle work dynamically again
* Unregister producer's Binder object when closing layer
* Address ripinperi's comments
* Add a timeout on syncpoint wait operation
Syncpoint aren't supposed to be waited on for more than a second.
This effectively workaround issues caused by not having a channel
scheduling in place yet.
PS: Also introduce Android WaitForever warning about fence being not
signaled for 3s
* Fix a print of previous commit
* Address Ac_K's comments
* Address gdkchan's comments
* Address final comments
Diffstat (limited to 'Ryujinx.HLE/HOS/Services/SurfaceFlinger/Parcel.cs')
| -rw-r--r-- | Ryujinx.HLE/HOS/Services/SurfaceFlinger/Parcel.cs | 220 |
1 files changed, 189 insertions, 31 deletions
diff --git a/Ryujinx.HLE/HOS/Services/SurfaceFlinger/Parcel.cs b/Ryujinx.HLE/HOS/Services/SurfaceFlinger/Parcel.cs index f5d93423..3def026b 100644 --- a/Ryujinx.HLE/HOS/Services/SurfaceFlinger/Parcel.cs +++ b/Ryujinx.HLE/HOS/Services/SurfaceFlinger/Parcel.cs @@ -1,58 +1,216 @@ +using Ryujinx.Common; +using Ryujinx.Common.Utilities; +using Ryujinx.HLE.HOS.Services.SurfaceFlinger.Types; using System; -using System.IO; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger { - static class Parcel + class Parcel { - public static byte[] GetParcelData(byte[] parcel) + private readonly byte[] _rawData; + + private Span<byte> Raw => new Span<byte>(_rawData); + + private ref ParcelHeader Header => ref MemoryMarshal.Cast<byte, ParcelHeader>(_rawData)[0]; + + private Span<byte> Payload => Raw.Slice((int)Header.PayloadOffset, (int)Header.PayloadSize); + + private Span<byte> Objects => Raw.Slice((int)Header.ObjectOffset, (int)Header.ObjectsSize); + + private int _payloadPosition; + private int _objectPosition; + + public Parcel(byte[] rawData) + { + _rawData = rawData; + + _payloadPosition = 0; + _objectPosition = 0; + } + + public Parcel(uint payloadSize, uint objectsSize) { - if (parcel == null) + uint headerSize = (uint)Unsafe.SizeOf<ParcelHeader>(); + + _rawData = new byte[BitUtils.AlignUp(headerSize + payloadSize + objectsSize, 4)]; + + Header.PayloadSize = payloadSize; + Header.ObjectsSize = objectsSize; + Header.PayloadOffset = headerSize; + Header.ObjectOffset = Header.PayloadOffset + Header.ObjectsSize; + } + + public string ReadInterfaceToken() + { + // Ignore the policy flags + int strictPolicy = ReadInt32(); + + return ReadString16(); + } + + public string ReadString16() + { + int size = ReadInt32(); + + if (size < 0) { - throw new ArgumentNullException(nameof(parcel)); + return ""; } - using (MemoryStream ms = new MemoryStream(parcel)) + ReadOnlySpan<byte> data = ReadInPlace((size + 1) * 2); + + // Return the unicode string without the last character (null terminator) + return Encoding.Unicode.GetString(data.Slice(0, size * 2)); + } + + public int ReadInt32() => ReadUnmanagedType<int>(); + public uint ReadUInt32() => ReadUnmanagedType<uint>(); + public bool ReadBoolean() => ReadUnmanagedType<uint>() != 0; + public long ReadInt64() => ReadUnmanagedType<long>(); + public ulong ReadUInt64() => ReadUnmanagedType<ulong>(); + + public T ReadFlattenable<T>() where T : unmanaged, IFlattenable + { + long flattenableSize = ReadInt64(); + + T result = new T(); + + Debug.Assert(flattenableSize == result.GetFlattenedSize()); + + result.Unflatten(this); + + return result; + } + + public T ReadUnmanagedType<T>() where T: unmanaged + { + ReadOnlySpan<byte> data = ReadInPlace(Unsafe.SizeOf<T>()); + + return MemoryMarshal.Cast<byte, T>(data)[0]; + } + + public ReadOnlySpan<byte> ReadInPlace(int size) + { + ReadOnlySpan<byte> result = Payload.Slice(_payloadPosition, size); + + _payloadPosition += BitUtils.AlignUp(size, 4); + + return result; + } + + [StructLayout(LayoutKind.Sequential, Size = 0x28)] + private struct FlatBinderObject + { + public int Type; + public int Flags; + public long BinderId; + public long Cookie; + + private byte _serviceNameStart; + + public Span<byte> ServiceName => MemoryMarshal.CreateSpan(ref _serviceNameStart, 0x8); + } + + public void WriteObject<T>(T obj, string serviceName) where T: IBinder + { + FlatBinderObject flatBinderObject = new FlatBinderObject { - BinaryReader reader = new BinaryReader(ms); + Type = 2, + Flags = 0, + BinderId = HOSBinderDriverServer.GetBinderId(obj), + }; - int dataSize = reader.ReadInt32(); - int dataOffset = reader.ReadInt32(); - int objsSize = reader.ReadInt32(); - int objsOffset = reader.ReadInt32(); + Encoding.ASCII.GetBytes(serviceName).CopyTo(flatBinderObject.ServiceName); - ms.Seek(dataOffset - 0x10, SeekOrigin.Current); + WriteUnmanagedType(ref flatBinderObject); - return reader.ReadBytes(dataSize); - } + // TODO: figure out what this value is + + WriteInplaceObject(new byte[4] { 0, 0, 0, 0 }); } - public static byte[] MakeParcel(byte[] data, byte[] objs) + public AndroidStrongPointer<T> ReadStrongPointer<T>() where T : unmanaged, IFlattenable { - if (data == null) + bool hasObject = ReadBoolean(); + + if (hasObject) { - throw new ArgumentNullException(nameof(data)); - } + T obj = ReadFlattenable<T>(); - if (objs == null) + return new AndroidStrongPointer<T>(obj); + } + else { - throw new ArgumentNullException(nameof(objs)); + return new AndroidStrongPointer<T>(); } + } - using (MemoryStream ms = new MemoryStream()) + public void WriteStrongPointer<T>(ref AndroidStrongPointer<T> value) where T: unmanaged, IFlattenable + { + WriteBoolean(!value.IsNull); + + if (!value.IsNull) { - BinaryWriter writer = new BinaryWriter(ms); + WriteFlattenable<T>(ref value.Object); + } + } - writer.Write(data.Length); - writer.Write(0x10); - writer.Write(objs.Length); - writer.Write(data.Length + 0x10); + public void WriteFlattenable<T>(ref T value) where T : unmanaged, IFlattenable + { + WriteInt64(value.GetFlattenedSize()); - writer.Write(data); - writer.Write(objs); + value.Flatten(this); + } - return ms.ToArray(); - } + public void WriteStatus(Status status) => WriteUnmanagedType(ref status); + public void WriteBoolean(bool value) => WriteUnmanagedType(ref value); + public void WriteInt32(int value) => WriteUnmanagedType(ref value); + public void WriteUInt32(uint value) => WriteUnmanagedType(ref value); + public void WriteInt64(long value) => WriteUnmanagedType(ref value); + public void WriteUInt64(ulong value) => WriteUnmanagedType(ref value); + + public void WriteUnmanagedType<T>(ref T value) where T : unmanaged + { + WriteInplace(SpanHelpers.AsByteSpan(ref value)); + } + + public void WriteInplace(ReadOnlySpan<byte> data) + { + Span<byte> result = Payload.Slice(_payloadPosition, data.Length); + + data.CopyTo(result); + + _payloadPosition += BitUtils.AlignUp(data.Length, 4); + } + + public void WriteInplaceObject(ReadOnlySpan<byte> data) + { + Span<byte> result = Objects.Slice(_objectPosition, data.Length); + + data.CopyTo(result); + + _objectPosition += BitUtils.AlignUp(data.Length, 4); + } + + private void UpdateHeader() + { + uint headerSize = (uint)Unsafe.SizeOf<ParcelHeader>(); + + Header.PayloadSize = (uint)_payloadPosition; + Header.ObjectsSize = (uint)_objectPosition; + Header.PayloadOffset = headerSize; + Header.ObjectOffset = Header.PayloadOffset + Header.PayloadSize; + } + + public ReadOnlySpan<byte> Finish() + { + UpdateHeader(); + + return Raw.Slice(0, (int)(Header.PayloadSize + Header.ObjectsSize + Unsafe.SizeOf<ParcelHeader>())); } } -}
\ No newline at end of file +} |
