Add CreateFromGameScreen

This commit is contained in:
Soreepeong 2024-03-03 20:08:52 +09:00
parent c0938bd3a9
commit c04ce36b9c
19 changed files with 1403 additions and 581 deletions

View file

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Numerics;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
@ -34,32 +33,47 @@ public partial interface ITextureProvider
/// </summary>
/// <param name="wrap">The source texture wrap. The passed value may be disposed once this function returns,
/// without having to wait for the completion of the returned <see cref="Task{TResult}"/>.</param>
/// <param name="uv0">The left top coordinates relative to the size of the source texture.</param>
/// <param name="uv1">The right bottom coordinates relative to the size of the source texture.</param>
/// <param name="dxgiFormat">The desired target format. Use 0 to use the source format.</param>
/// <param name="args">The texture modification arguments.</param>
/// <param name="leaveWrapOpen">Whether to leave <paramref name="wrap"/> non-disposed when the returned
/// <see cref="Task{TResult}"/> completes.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{TResult}"/> containing the copied texture on success. Dispose after use.</returns>
/// <remarks>
/// <para>Coordinates in <paramref name="uv0"/> and <paramref name="uv1"/> should be in range between 0 and 1.
/// </para>
/// <para>Supported values for <paramref name="dxgiFormat"/> may not necessarily match
/// <see cref="IsDxgiFormatSupported"/>.</para>
/// </remarks>
/// <remarks><para>This function may throw an exception.</para></remarks>
Task<IDalamudTextureWrap> CreateFromExistingTextureAsync(
IDalamudTextureWrap wrap,
Vector2 uv0,
Vector2 uv1,
int dxgiFormat = 0,
ExistingTextureModificationArgs args = default,
bool leaveWrapOpen = false,
CancellationToken cancellationToken = default);
/// <summary>Creates a texture from the game screen, before rendering Dalamud.</summary>
/// <param name="autoUpdate">If <c>true</c>, automatically update the underlying texture.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{TResult}"/> containing the copied texture on success. Dispose after use.</returns>
/// <remarks><para>This function may throw an exception.</para></remarks>
Task<IDalamudTextureWrap> CreateFromGameScreen(
bool autoUpdate = false,
CancellationToken cancellationToken = default);
/// <summary>Creates a texture from the game screen, before rendering Dalamud.</summary>
/// <param name="viewportId">The viewport ID.</param>
/// <param name="autoUpdate">If <c>true</c>, automatically update the underlying texture.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{TResult}"/> containing the copied texture on success. Dispose after use.</returns>
/// <remarks>
/// <para>Use <c>ImGui.GetMainViewport().ID</c> to capture the game screen with Dalamud rendered.</para>
/// <para>This function may throw an exception.</para>
/// </remarks>
Task<IDalamudTextureWrap> CreateFromImGuiViewport(
uint viewportId,
bool autoUpdate = false,
CancellationToken cancellationToken = default);
/// <summary>Gets a texture from the given bytes, trying to interpret it as a .tex file or other well-known image
/// files, such as .png.</summary>
/// <param name="bytes">The bytes to load.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{TResult}"/> containing the loaded texture on success. Dispose after use.</returns>
/// <remarks><para>This function may throw an exception.</para></remarks>
Task<IDalamudTextureWrap> CreateFromImageAsync(
ReadOnlyMemory<byte> bytes,
CancellationToken cancellationToken = default);
@ -70,8 +84,10 @@ public partial interface ITextureProvider
/// <param name="leaveOpen">Whether to leave the stream open once the task completes, sucessfully or not.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{TResult}"/> containing the loaded texture on success. Dispose after use.</returns>
/// <remarks><paramref name="stream"/> will be closed or not only according to <paramref name="leaveOpen"/>;
/// <paramref name="cancellationToken"/> is irrelevant in closing the stream.</remarks>
/// <remarks>
/// <para><paramref name="stream"/> will be closed or not only according to <paramref name="leaveOpen"/>;
/// <paramref name="cancellationToken"/> is irrelevant in closing the stream.</para>
/// <para>This function may throw an exception.</para></remarks>
Task<IDalamudTextureWrap> CreateFromImageAsync(
Stream stream,
bool leaveOpen = false,
@ -81,6 +97,7 @@ public partial interface ITextureProvider
/// <param name="specs">The specifications for the raw bitmap.</param>
/// <param name="bytes">The bytes to load.</param>
/// <returns>The texture loaded from the supplied raw bitmap. Dispose after use.</returns>
/// <remarks><para>This function may throw an exception.</para></remarks>
IDalamudTextureWrap CreateFromRaw(
RawImageSpecification specs,
ReadOnlySpan<byte> bytes);
@ -90,6 +107,7 @@ public partial interface ITextureProvider
/// <param name="bytes">The bytes to load.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{TResult}"/> containing the loaded texture on success. Dispose after use.</returns>
/// <remarks><para>This function may throw an exception.</para></remarks>
Task<IDalamudTextureWrap> CreateFromRawAsync(
RawImageSpecification specs,
ReadOnlyMemory<byte> bytes,
@ -101,8 +119,11 @@ public partial interface ITextureProvider
/// <param name="leaveOpen">Whether to leave the stream open once the task completes, sucessfully or not.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{TResult}"/> containing the loaded texture on success. Dispose after use.</returns>
/// <remarks><paramref name="stream"/> will be closed or not only according to <paramref name="leaveOpen"/>;
/// <paramref name="cancellationToken"/> is irrelevant in closing the stream.</remarks>
/// <remarks>
/// <para><paramref name="stream"/> will be closed or not only according to <paramref name="leaveOpen"/>;
/// <paramref name="cancellationToken"/> is irrelevant in closing the stream.</para>
/// <para>This function may throw an exception.</para>
/// </remarks>
Task<IDalamudTextureWrap> CreateFromRawAsync(
RawImageSpecification specs,
Stream stream,
@ -115,12 +136,14 @@ public partial interface ITextureProvider
/// </summary>
/// <param name="file">The texture to obtain a handle to.</param>
/// <returns>A texture wrap that can be used to render the texture. Dispose after use.</returns>
/// <remarks><para>This function may throw an exception.</para></remarks>
IDalamudTextureWrap CreateFromTexFile(TexFile file);
/// <summary>Get a texture handle for the specified Lumina <see cref="TexFile"/>.</summary>
/// <param name="file">The texture to obtain a handle to.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A texture wrap that can be used to render the texture. Dispose after use.</returns>
/// <remarks><para>This function may throw an exception.</para></remarks>
Task<IDalamudTextureWrap> CreateFromTexFileAsync(
TexFile file,
CancellationToken cancellationToken = default);
@ -128,13 +151,14 @@ public partial interface ITextureProvider
/// <summary>Gets the supported bitmap decoders.</summary>
/// <returns>The supported bitmap decoders.</returns>
/// <remarks>
/// The following functions support the files of the container types pointed by yielded values.
/// <para>The following functions support the files of the container types pointed by yielded values.</para>
/// <ul>
/// <li><see cref="GetFromFile"/></li>
/// <li><see cref="GetFromManifestResource"/></li>
/// <li><see cref="CreateFromImageAsync(ReadOnlyMemory{byte},CancellationToken)"/></li>
/// <li><see cref="CreateFromImageAsync(Stream,bool,CancellationToken)"/></li>
/// </ul>
/// <para>This function may throw an exception.</para>
/// </remarks>
IEnumerable<IBitmapCodecInfo> GetSupportedImageDecoderInfos();
@ -145,32 +169,39 @@ public partial interface ITextureProvider
/// <ul>
/// <li><see cref="SaveToStreamAsync"/></li>
/// </ul>
/// <para>This function may throw an exception.</para>
/// </remarks>
IEnumerable<IBitmapCodecInfo> GetSupportedImageEncoderInfos();
/// <summary>Gets a shared texture corresponding to the given game resource icon specifier.</summary>
/// <param name="lookup">A game icon specifier.</param>
/// <returns>The shared texture that you may use to obtain the loaded texture wrap and load states.</returns>
/// <remarks>This function is under the effect of <see cref="ITextureSubstitutionProvider.GetSubstitutedPath"/>.
/// <remarks>
/// <para>This function is under the effect of <see cref="ITextureSubstitutionProvider.GetSubstitutedPath"/>.</para>
/// <para>This function does not throw exceptions.</para>
/// </remarks>
ISharedImmediateTexture GetFromGameIcon(in GameIconLookup lookup);
/// <summary>Gets a shared texture corresponding to the given path to a game resource.</summary>
/// <param name="path">A path to a game resource.</param>
/// <returns>The shared texture that you may use to obtain the loaded texture wrap and load states.</returns>
/// <remarks>This function is under the effect of <see cref="ITextureSubstitutionProvider.GetSubstitutedPath"/>.
/// <remarks>
/// <para>This function is under the effect of <see cref="ITextureSubstitutionProvider.GetSubstitutedPath"/>.</para>
/// <para>This function does not throw exceptions.</para>
/// </remarks>
ISharedImmediateTexture GetFromGame(string path);
/// <summary>Gets a shared texture corresponding to the given file on the filesystem.</summary>
/// <param name="path">A path to a file on the filesystem.</param>
/// <returns>The shared texture that you may use to obtain the loaded texture wrap and load states.</returns>
/// <remarks><para>This function does not throw exceptions.</para></remarks>
ISharedImmediateTexture GetFromFile(string path);
/// <summary>Gets a shared texture corresponding to the given file of the assembly manifest resources.</summary>
/// <param name="assembly">The assembly containing manifest resources.</param>
/// <param name="name">The case-sensitive name of the manifest resource being requested.</param>
/// <returns>The shared texture that you may use to obtain the loaded texture wrap and load states.</returns>
/// <remarks><para>This function does not throw exceptions.</para></remarks>
ISharedImmediateTexture GetFromManifestResource(Assembly assembly, string name);
/// <summary>Get a path for a specific icon's .tex file.</summary>
@ -185,14 +216,12 @@ public partial interface ITextureProvider
/// <param name="lookup">The icon lookup.</param>
/// <param name="path">The resolved path.</param>
/// <returns><c>true</c> if the corresponding file exists and <paramref name="path"/> has been set.</returns>
/// <remarks><para>This function does not throw exceptions.</para></remarks>
bool TryGetIconPath(in GameIconLookup lookup, [NotNullWhen(true)] out string? path);
/// <summary>Gets the raw data of a texture wrap.</summary>
/// <param name="wrap">The source texture wrap.</param>
/// <param name="uv0">The left top coordinates relative to the size of the source texture.</param>
/// <param name="uv1">The right bottom coordinates relative to the size of the source texture.</param>
/// <param name="dxgiFormat">The desired target format.
/// If 0 (unknown) is passed, then the format will not be converted.</param>
/// <param name="args">The texture modification arguments.</param>
/// <param name="leaveWrapOpen">Whether to leave <paramref name="wrap"/> non-disposed when the returned
/// <see cref="Task{TResult}"/> completes.</param>
/// <param name="cancellationToken">The cancellation token.</param>
@ -200,16 +229,11 @@ public partial interface ITextureProvider
/// <remarks>
/// <para>The length of the returned <c>RawData</c> may not match
/// <see cref="RawImageSpecification.Height"/> * <see cref="RawImageSpecification.Pitch"/>.</para>
/// <para>If <paramref name="uv0"/> is <see cref="Vector2.Zero"/>,
/// <paramref name="uv1"/> is <see cref="Vector2.One"/>, and <paramref name="dxgiFormat"/> is <c>0</c>,
/// then the source data will be returned.</para>
/// <para>This function can fail.</para>
/// <para>This function may throw an exception.</para>
/// </remarks>
Task<(RawImageSpecification Specification, byte[] RawData)> GetRawDataFromExistingTextureAsync(
IDalamudTextureWrap wrap,
Vector2 uv0,
Vector2 uv1,
int dxgiFormat = 0,
ExistingTextureModificationArgs args = default,
bool leaveWrapOpen = false,
CancellationToken cancellationToken = default);
@ -217,9 +241,7 @@ public partial interface ITextureProvider
/// <param name="wrap">The texture wrap to save.</param>
/// <param name="containerGuid">The container GUID, obtained from <see cref="GetSupportedImageEncoderInfos"/>.</param>
/// <param name="stream">The stream to save to.</param>
/// <param name="props">Properties to pass to the encoder. See
/// <a href="https://learn.microsoft.com/en-us/windows/win32/wic/-wic-creating-encoder#encoder-options">Microsoft
/// Learn</a> for available parameters.</param>
/// <param name="props">Properties to pass to the encoder. See remarks for valid values.</param>
/// <param name="leaveWrapOpen">Whether to leave <paramref name="wrap"/> non-disposed when the returned
/// <see cref="Task{TResult}"/> completes.</param>
/// <param name="leaveStreamOpen">Whether to leave <paramref name="stream"/> open when the returned
@ -228,6 +250,15 @@ public partial interface ITextureProvider
/// <returns>A task representing the save process.</returns>
/// <remarks>
/// <para><paramref name="wrap"/> must not be disposed until the task finishes.</para>
/// <para>See the following webpages for the valid values for <paramref name="props"/> per
/// <paramref name="containerGuid"/>.</para>
/// <ul>
/// <li><a href="https://learn.microsoft.com/en-us/windows/win32/wic/native-wic-codecs">
/// WIC Codecs from Microsoft</a></li>
/// <li><a href="https://learn.microsoft.com/en-us/windows/win32/wic/-wic-creating-encoder#encoder-options">
/// Image Encoding Overview: Encoder options</a></li>
/// </ul>
/// <para>This function may throw an exception.</para>
/// </remarks>
Task SaveToStreamAsync(
IDalamudTextureWrap wrap,
@ -242,15 +273,22 @@ public partial interface ITextureProvider
/// <param name="wrap">The texture wrap to save.</param>
/// <param name="containerGuid">The container GUID, obtained from <see cref="GetSupportedImageEncoderInfos"/>.</param>
/// <param name="path">The target file path. The target file will be overwritten if it exist.</param>
/// <param name="props">Properties to pass to the encoder. See
/// <a href="https://learn.microsoft.com/en-us/windows/win32/wic/-wic-creating-encoder#encoder-options">Microsoft
/// Learn</a> for available parameters.</param>
/// <param name="props">Properties to pass to the encoder. See remarks for valid values.</param>
/// <param name="leaveWrapOpen">Whether to leave <paramref name="wrap"/> non-disposed when the returned
/// <see cref="Task{TResult}"/> completes.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task representing the save process.</returns>
/// <remarks>
/// <para><paramref name="wrap"/> must not be disposed until the task finishes.</para>
/// <para>See the following webpages for the valid values for <paramref name="props"/> per
/// <paramref name="containerGuid"/>.</para>
/// <ul>
/// <li><a href="https://learn.microsoft.com/en-us/windows/win32/wic/native-wic-codecs">
/// WIC Codecs from Microsoft</a></li>
/// <li><a href="https://learn.microsoft.com/en-us/windows/win32/wic/-wic-creating-encoder#encoder-options">
/// Image Encoding Overview: Encoder options</a></li>
/// </ul>
/// <para>This function may throw an exception.</para>
/// </remarks>
Task SaveToFileAsync(
IDalamudTextureWrap wrap,
@ -266,11 +304,13 @@ public partial interface ITextureProvider
/// </summary>
/// <param name="dxgiFormat">The DXGI format.</param>
/// <returns><c>true</c> if supported.</returns>
/// <remarks><para>This function does not throw exceptions.</para></remarks>
bool IsDxgiFormatSupported(int dxgiFormat);
/// <summary>Determines whether the system supports the given DXGI format for use with
/// <see cref="CreateFromExistingTextureAsync"/>.</summary>
/// <param name="dxgiFormat">The DXGI format.</param>
/// <returns><c>true</c> if supported.</returns>
/// <remarks><para>This function does not throw exceptions.</para></remarks>
bool IsDxgiFormatSupportedForCreateFromExistingTextureAsync(int dxgiFormat);
}