//
// Copyright (c) 2019-2020 Ryujinx
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
using Ryujinx.Audio.Renderer.Parameter;
using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Ryujinx.Audio.Renderer.Server.Splitter
{
///
/// Server state for a splitter.
///
[StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = Alignment)]
public struct SplitterState
{
public const int Alignment = 0x10;
///
/// The unique id of this .
///
public int Id;
///
/// Target sample rate to use on the splitter.
///
public uint SampleRate;
///
/// Count of splitter destinations ().
///
public int DestinationCount;
///
/// Set to true if the splitter has a new connection.
///
[MarshalAs(UnmanagedType.I1)]
public bool HasNewConnection;
///
/// Linked list of .
///
private unsafe SplitterDestination* _destinationsData;
///
/// Span to the first element of the linked list of .
///
public Span Destinations
{
get
{
unsafe
{
return (IntPtr)_destinationsData != IntPtr.Zero ? new Span(_destinationsData, 1) : Span.Empty;
}
}
}
///
/// Create a new .
///
/// The unique id of this .
public SplitterState(int id) : this()
{
Id = id;
}
public Span GetData(int index)
{
int i = 0;
Span result = Destinations;
while (i < index)
{
if (result.IsEmpty)
{
break;
}
result = result[0].Next;
i++;
}
return result;
}
///
/// Clear the new connection flag.
///
public void ClearNewConnectionFlag()
{
HasNewConnection = false;
}
///
/// Utility function to apply a given to all .
///
/// The action to execute on each elements.
private void ForEachDestination(SpanAction action)
{
Span temp = Destinations;
int i = 0;
while (true)
{
if (temp.IsEmpty)
{
break;
}
Span next = temp[0].Next;
action.Invoke(temp, i++);
temp = next;
}
}
///
/// Update the from user parameter.
///
/// The splitter context.
/// The user parameter.
/// The raw input data after the .
public void Update(SplitterContext context, ref SplitterInParameter parameter, ReadOnlySpan input)
{
ClearLinks();
int destinationCount;
if (context.IsBugFixed)
{
destinationCount = parameter.DestinationCount;
}
else
{
destinationCount = Math.Min(context.GetDestinationCountPerStateForCompatibility(), parameter.DestinationCount);
}
if (destinationCount > 0)
{
ReadOnlySpan destinationIds = MemoryMarshal.Cast(input);
Memory destination = context.GetDestinationMemory(destinationIds[0]);
SetDestination(ref destination.Span[0]);
DestinationCount = destinationCount;
for (int i = 1; i < destinationCount; i++)
{
Memory nextDestination = context.GetDestinationMemory(destinationIds[i]);
destination.Span[0].Link(ref nextDestination.Span[0]);
destination = nextDestination;
}
}
Debug.Assert(parameter.Id == Id);
if (parameter.Id == Id)
{
SampleRate = parameter.SampleRate;
HasNewConnection = true;
}
}
///
/// Set the head of the linked list of .
///
/// A reference to a .
public void SetDestination(ref SplitterDestination newValue)
{
unsafe
{
fixed (SplitterDestination* newValuePtr = &newValue)
{
_destinationsData = newValuePtr;
}
}
}
///
/// Update the internal state of this instance.
///
public void UpdateInternalState()
{
ForEachDestination((destination, _) => destination[0].UpdateInternalState());
}
///
/// Clear all links from the .
///
public void ClearLinks()
{
ForEachDestination((destination, _) => destination[0].Unlink());
unsafe
{
_destinationsData = (SplitterDestination*)IntPtr.Zero;
}
}
///
/// Initialize a given .
///
/// All the to initialize.
public static void InitializeSplitters(Span splitters)
{
foreach (ref SplitterState splitter in splitters)
{
unsafe
{
splitter._destinationsData = (SplitterDestination*)IntPtr.Zero;
}
splitter.DestinationCount = 0;
}
}
}
}