-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add images to the service using memory mapped files.
May need to address that global namespace thing. I don't know how this can work without it otherwise (but it works for the pipes somehow?)
- Loading branch information
Showing
12 changed files
with
198 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Exo/Service/Exo.Service.Core/MemoryMappedFileMemoryManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Buffers; | ||
using System.IO.MemoryMappedFiles; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
namespace Exo.Service; | ||
|
||
internal sealed unsafe class MemoryMappedFileMemoryManager : MemoryManager<byte> | ||
{ | ||
private readonly SafeMemoryMappedViewHandle _viewHandle; | ||
private readonly nint _offset; | ||
private readonly int _length; | ||
private MemoryMappedViewAccessor? _viewAccessor; | ||
|
||
public MemoryMappedFileMemoryManager(MemoryMappedFile memoryMappedFile, nint offset, int length, MemoryMappedFileAccess access) | ||
{ | ||
ArgumentNullException.ThrowIfNull(memoryMappedFile); | ||
ArgumentOutOfRangeException.ThrowIfNegative(offset); | ||
ArgumentOutOfRangeException.ThrowIfNegative(length); | ||
_viewAccessor = memoryMappedFile.CreateViewAccessor((long)offset, length, access); | ||
_viewHandle = _viewAccessor.SafeMemoryMappedViewHandle; | ||
_offset = offset; | ||
_length = length; | ||
bool success = false; | ||
_viewHandle.DangerousAddRef(ref success); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (Interlocked.Exchange(ref _viewAccessor, null) is { } accessor) | ||
{ | ||
accessor.Dispose(); | ||
} | ||
} | ||
|
||
public override Span<byte> GetSpan() => new((byte*)_viewHandle.DangerousGetHandle(), _length); | ||
|
||
public override MemoryHandle Pin(int elementIndex = 0) | ||
{ | ||
byte* pointer = null; | ||
_viewHandle.AcquirePointer(ref pointer); | ||
return new MemoryHandle(pointer, pinnable: this); | ||
} | ||
|
||
public override void Unpin() => _viewHandle.ReleasePointer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Exo/Ui/Exo.Settings.Ui/Converters/SharedMemoryToBitmapImageConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Exo.Settings.Ui.ViewModels; | ||
using Microsoft.UI.Xaml.Data; | ||
using Microsoft.UI.Xaml.Media.Imaging; | ||
|
||
namespace Exo.Settings.Ui.Converters; | ||
|
||
internal sealed class SharedMemoryToBitmapImageConverter : IValueConverter | ||
{ | ||
public object? Convert(object value, Type targetType, object parameter, string language) | ||
{ | ||
if (value is not SharedMemory data) return null; | ||
|
||
var bitmapImage = new BitmapImage(); | ||
LoadImage(bitmapImage, data); | ||
return bitmapImage; | ||
} | ||
|
||
private async void LoadImage(BitmapImage bitmapImage, SharedMemory sharedMemory) | ||
{ | ||
try | ||
{ | ||
using (var sharedMemoryStream = sharedMemory.CreateReadStream()) | ||
using (var randomAccessStream = sharedMemoryStream.AsRandomAccessStream()) | ||
{ | ||
await bitmapImage.SetSourceAsync(randomAccessStream); | ||
} | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.IO.MemoryMappedFiles; | ||
using System.Security.Cryptography; | ||
|
||
namespace Exo.Settings.Ui.ViewModels; | ||
|
||
internal sealed class SharedMemory : IDisposable | ||
{ | ||
public static SharedMemory Create(string prefix, ulong length) | ||
{ | ||
ArgumentNullException.ThrowIfNull(prefix); | ||
ArgumentOutOfRangeException.ThrowIfGreaterThan(length, (ulong)long.MaxValue); | ||
|
||
string name = string.Create | ||
( | ||
0 + 32 + prefix.Length, | ||
prefix, | ||
static (span, prefix) => | ||
{ | ||
//@"Global\".CopyTo(span[..7]); | ||
prefix.CopyTo(span[0..]); | ||
RandomNumberGenerator.GetHexString(span[(0 + prefix.Length)..], true); | ||
} | ||
); | ||
return new(name, MemoryMappedFile.CreateNew(name, (long)length, MemoryMappedFileAccess.ReadWrite), length); | ||
} | ||
|
||
private readonly string _name; | ||
private readonly MemoryMappedFile _file; | ||
private readonly ulong _length; | ||
|
||
private SharedMemory(string name, MemoryMappedFile file, ulong length) | ||
{ | ||
_name = name; | ||
_file = file; | ||
_length = length; | ||
} | ||
|
||
public void Dispose() => _file.Dispose(); | ||
|
||
public string Name => _name; | ||
public ulong Length => _length; | ||
|
||
public Stream CreateReadStream() => _file.CreateViewStream(0, (long)_length, MemoryMappedFileAccess.Read); | ||
public Stream CreateWriteStream() => _file.CreateViewStream(0, (long)_length, MemoryMappedFileAccess.Write); | ||
} |