Skip to content

Commit

Permalink
Fix BitmapEncoder dp to 96
Browse files Browse the repository at this point in the history
Change OpenReadAsync to OpenStreamForReadAsync
  • Loading branch information
wherewhere committed Jan 5, 2024
1 parent d647124 commit a0bf4f1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public virtual async Task<VersionInfo> GetVersionInfoAsync(string packageName, C
/// <returns>A <see cref="Task{Stream}"/> which returns a read-only <see cref="Stream"/> on the specified path.</returns>
protected virtual Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default) =>
#if WINDOWS_UWP
StorageFile.GetFileFromPathAsync(path).AsTask(cancellationToken).ContinueWith(x => x.Result.OpenReadAsync().AsTask(cancellationToken)).Unwrap().ContinueWith(x => x.Result.AsStream());
StorageFile.GetFileFromPathAsync(path).AsTask(cancellationToken).ContinueWith(x => x.Result.OpenStreamForReadAsync()).Unwrap();
#else
TaskExExtensions.FromResult<Stream>(File.OpenRead(path));
#endif
Expand Down
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient/DeviceCommands/PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ protected void ValidateDevice()
/// <returns>A read-only <see cref="Stream"/> on the specified path.</returns>
protected virtual Stream GetFileStream(string path) =>
#if WINDOWS_UWP
StorageFile.GetFileFromPathAsync(path).AwaitByTaskCompleteSource().OpenReadAsync().AwaitByTaskCompleteSource().AsStream();
StorageFile.GetFileFromPathAsync(path).AwaitByTaskCompleteSource().OpenStreamForReadAsync().AwaitByTaskCompleteSource();
#else
File.OpenRead(path);
#endif
Expand Down
19 changes: 6 additions & 13 deletions AdvancedSharpAdbClient/Models/FramebufferHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Runtime.InteropServices;

#if WINDOWS_UWP
using System.IO;
using System.Threading;
#endif

Expand Down Expand Up @@ -432,22 +431,19 @@ private PixelFormat StandardizePixelFormat(byte[] buffer)
else
{
FramebufferHeader self = this;

TaskCompletionSource<WriteableBitmap?> taskCompletionSource = new();

_ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
try
{
WriteableBitmap? result = await self.ToBitmapAsync(buffer, cancellationToken).ConfigureAwait(false);
taskCompletionSource.SetResult(result);
_ = taskCompletionSource.TrySetResult(result);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
_ = taskCompletionSource.TrySetException(e);
}
});

return taskCompletionSource.Task;
}
}
Expand All @@ -470,25 +466,22 @@ private PixelFormat StandardizePixelFormat(byte[] buffer)
else
{
FramebufferHeader self = this;

TaskCompletionSource<WriteableBitmap?> taskCompletionSource = new();

if (!dispatcher.TryEnqueue(async () =>
{
try
{
WriteableBitmap? result = await self.ToBitmapAsync(buffer, cancellationToken).ConfigureAwait(false);
taskCompletionSource.SetResult(result);
_ = taskCompletionSource.TrySetResult(result);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
_ = taskCompletionSource.TrySetException(e);
}
}))
{
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
_ = taskCompletionSource.TrySetException(new InvalidOperationException("Failed to enqueue the operation"));
}

return taskCompletionSource.Task;
}
}
Expand Down Expand Up @@ -519,7 +512,7 @@ private PixelFormat StandardizePixelFormat(byte[] buffer)

using InMemoryRandomAccessStream random = new();
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, random).AsTask(cancellationToken);
encoder.SetPixelData(bitmapPixelFormat, alphaMode, Width, Height, 960, 960, buffer);
encoder.SetPixelData(bitmapPixelFormat, alphaMode, Width, Height, 96, 96, buffer);
await encoder.FlushAsync().AsTask(cancellationToken);
WriteableBitmap WriteableImage = new((int)Width, (int)Height);
await WriteableImage.SetSourceAsync(random).AsTask(cancellationToken).ConfigureAwait(false);
Expand Down
12 changes: 6 additions & 6 deletions AdvancedSharpAdbClient/Polyfills/Extensions/TaskExExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public static TResult AwaitByTaskCompleteSource<TResult>(this IAsyncOperation<TR
try
{
TResult result = await function.AsTask(cancellationToken).ConfigureAwait(false);
taskCompletionSource.SetResult(result);
_ = taskCompletionSource.TrySetResult(result);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
_ = taskCompletionSource.TrySetException(e);
}
}, cancellationToken);
TResult taskResult = task.Result;
Expand All @@ -143,11 +143,11 @@ public static void AwaitByTaskCompleteSource(this Task function, CancellationTok
try
{
await function.ConfigureAwait(false);
taskCompletionSource.SetResult(null);
_ = taskCompletionSource.TrySetResult(null);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
_ = taskCompletionSource.TrySetException(e);
}
}, cancellationToken);
_ = task.Result;
Expand All @@ -169,11 +169,11 @@ public static TResult AwaitByTaskCompleteSource<TResult>(this Task<TResult> func
try
{
TResult result = await function.ConfigureAwait(false);
taskCompletionSource.SetResult(result);
_ = taskCompletionSource.TrySetResult(result);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
_ = taskCompletionSource.TrySetException(e);
}
}, cancellationToken);
TResult taskResult = task.Result;
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<RepositoryUrl>https://github.com/SharpAdb/AdvancedSharpAdbClient</RepositoryUrl>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Title>.NET client for adb, Android Debug Bridge (AdvancedSharpAdbClient)</Title>
<VersionPrefix>3.0.9</VersionPrefix>
<VersionPrefix>3.0.10</VersionPrefix>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit a0bf4f1

Please sign in to comment.