using System;
using System.Drawing;
using System.Runtime.CompilerServices;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Interop.Effects;
///
/// A base type to support non-generic uses.
///
internal abstract class D2D1TransformMapper
{
///
/// The buffer with the serialized shader dispatch data, if any.
/// The input rectangles to be mapped to the output rectangle. This parameter is always equal to the input bounds.
/// The input rectangles to be mapped to the opaque output rectangle.
/// The output rectangle that maps to the corresponding input rectangle.
/// The output rectangle that maps to the corresponding opaque input rectangle.
public abstract void MapInputsToOutput(
ReadOnlySpan buffer,
ReadOnlySpan inputs,
ReadOnlySpan opaqueInputs,
out Rectangle output,
out Rectangle opaqueOutput);
///
/// The output rectangle from which the inputs must be mapped.
/// The corresponding set of inputs. The inputs will directly correspond to the transform inputs.
public abstract void MapOutputToInputs(in Rectangle output, Span inputs);
///
/// The index of the input rectangle.
/// The invalid input rectangle.
/// The output rectangle to which the input rectangle must be mapped.
public abstract void MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput);
///
/// A generic implementation for a specific shader type.
///
/// The target shader type.
public sealed class For : D2D1TransformMapper
where T : unmanaged, ID2D1PixelShader
{
///
/// The instance to use.
///
private readonly ID2D1TransformMapper transformMapper;
///
/// Creates a new instance with the specified parameters.
///
/// The instance to use.
private For(ID2D1TransformMapper transformMapper)
{
this.transformMapper = transformMapper;
}
///
/// Creates a new instance for a given transform mapper, if there is one.
///
/// The instance to usse, if there is one.
/// A new instance wrapping , or .
public static D2D1TransformMapper? Get(ID2D1TransformMapper? transformMapper)
{
if (transformMapper is null)
{
return null;
}
// If one of the built-in transform mapper factories from D2D1TransformMapperFactory was used,
// the returned ID2D1TransformMapper instance will already inherit from D2D1TransformMapper.
// If that's the case, that object can be returned directly, saving one allocation and avoiding
// an extra interface stub dispatch call every time one of the transform mapping APIs is called.
if (transformMapper is D2D1TransformMapper d2D1TransformMapper)
{
return d2D1TransformMapper;
}
return new For(transformMapper);
}
///
public override void MapInputsToOutput(ReadOnlySpan buffer, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput)
{
Unsafe.SkipInit(out T shader);
shader.InitializeFromDispatchData(buffer);
this.transformMapper.MapInputsToOutput(in shader, inputs, opaqueInputs, out output, out opaqueOutput);
}
///
public override void MapOutputToInputs(in Rectangle output, Span inputs)
{
this.transformMapper.MapOutputToInputs(in output, inputs);
}
///
public override void MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput)
{
this.transformMapper.MapInvalidOutput(inputIndex, invalidInput, out invalidOutput);
}
}
///
/// A generic implementation for a mapper with a parameterless constructor.
///
/// The type of D2D1 pixel shader to register.
/// The type of implementation to register.
public sealed class FactoryOf : ID2D1TransformMapperFactory
where T : unmanaged, ID2D1PixelShader
where TMapper : class, ID2D1TransformMapper, new()
{
///
/// Creates a new instance.
///
private FactoryOf()
{
}
///
/// Gets the shared instance for a given transform mapper.
///
public static FactoryOf Instance { get; } = new();
///
public ID2D1TransformMapper Create()
{
return new TMapper();
}
}
}