Moved from IntPtr to byte[] in more places

This commit is contained in:
Yoshi Askharoun
2021-10-04 14:17:40 -05:00
parent d0f576c266
commit 5ba04306df
19 changed files with 213 additions and 127 deletions
@@ -30,7 +30,7 @@ namespace Microsoft.Iris.Render.Extensions
Size sizeActualPxl,
int nStride,
SurfaceFormat nFormat,
IntPtr pvData,
byte[] pvData,
[MarshalAs(UnmanagedType.LPStruct), In] ImageRequirements req,
BitmapOptions nOptions,
out HSpBitmap hBmp,
@@ -52,7 +52,7 @@ namespace Microsoft.Iris.Render.Extensions
}
internal static HRESULT SpBitmapLoadBuffer(
IntPtr pvSrc,
byte[] pvSrc,
uint cbSize,
[MarshalAs(UnmanagedType.LPStruct), In] ImageRequirements req,
BitmapOptions nOptions,
@@ -68,7 +68,7 @@ namespace Microsoft.Iris.Render.Extensions
}
internal static HRESULT SpSoundLoadBuffer(
IntPtr pBuffer,
byte[] pBuffer,
int dwSize,
SoundOptions options,
out HSpSound hSound,
@@ -18,8 +18,7 @@ namespace Microsoft.Iris.Render.Extensions
protected ImageRequirements m_req;
protected Size m_size;
protected BitmapInformation m_info;
protected IntPtr m_buffer;
protected uint m_length;
protected byte[] m_buffer;
private int m_countUsers;
private int m_countLoadsInProgress;
private bool m_fFullLoadRequested;
@@ -30,15 +29,13 @@ namespace Microsoft.Iris.Render.Extensions
public ImageCacheItem(
IRenderSession renderSession,
string identifier,
IntPtr buffer,
uint length,
byte[] buffer,
Size maxSize,
bool flippable,
bool antialiasEdges)
: this(renderSession, identifier, maxSize, flippable, antialiasEdges)
{
this.m_buffer = buffer;
this.m_length = length;
}
public ImageCacheItem(
@@ -156,10 +153,9 @@ namespace Microsoft.Iris.Render.Extensions
public virtual void StartLoad() => this.LoadBuffer();
protected void SetBuffer(IntPtr buffer, uint length)
protected void SetBuffer(byte[] buffer)
{
this.m_buffer = buffer;
this.m_length = length;
}
protected void SetSize(Size size) => this.m_size = size;
@@ -229,7 +225,7 @@ namespace Microsoft.Iris.Render.Extensions
protected virtual bool DoHeaderLoad()
{
ImageHeader header;
if (!(this.m_buffer != IntPtr.Zero) || this.m_length <= 0U || !ImageLoader.LoadHeader(this.m_buffer, (int)this.m_length, this.m_req, out header))
if (!(this.m_buffer != null) || this.m_buffer.Length <= 0U || !ImageLoader.LoadHeader(this.m_buffer, this.m_req, out header))
return false;
this.SetSize(header.sizeActualPxl);
return true;
@@ -240,7 +236,7 @@ namespace Microsoft.Iris.Render.Extensions
BitmapInformation bitmapInfo = null;
bool flag = false;
if (this.m_image != null)
flag = !(this.m_buffer == IntPtr.Zero) ? ImageLoader.FromBuffer(this.m_image, this.m_buffer, (int)this.m_length, this.m_req.MaximumSize, this.m_req.Flippable, this.m_req.AntialiasEdges, this.m_req.BorderWidth, this.m_req.BorderColor, out bitmapInfo) : ImageLoader.FromFile(this.m_image, this.m_image.Identifier, this.m_req.MaximumSize, this.m_req.Flippable, this.m_req.AntialiasEdges, this.m_req.BorderWidth, this.m_req.BorderColor, out bitmapInfo);
flag = !(this.m_buffer == null) ? ImageLoader.FromBuffer(this.m_image, this.m_buffer, this.m_req.MaximumSize, this.m_req.Flippable, this.m_req.AntialiasEdges, this.m_req.BorderWidth, this.m_req.BorderColor, out bitmapInfo) : ImageLoader.FromFile(this.m_image, this.m_image.Identifier, this.m_req.MaximumSize, this.m_req.Flippable, this.m_req.AntialiasEdges, this.m_req.BorderWidth, this.m_req.BorderColor, out bitmapInfo);
if (flag)
{
this.m_info = bitmapInfo;
@@ -13,27 +13,23 @@ namespace Microsoft.Iris.Render.Extensions
{
public static class ImageLoader
{
public static bool LoadHeader(IntPtr rgData, int length, out ImageHeader header)
public static bool LoadHeader(byte[] rgData, out ImageHeader header)
{
ImageRequirements req = new ImageRequirements();
return LoadHeader(rgData, length, req, out header);
return LoadHeader(rgData, req, out header);
}
public static bool LoadHeader(
IntPtr rgData,
int length,
ImageRequirements req,
out ImageHeader header)
public static bool LoadHeader(byte[] rgData, ImageRequirements req, out ImageHeader header)
{
Debug2.Validate(rgData != IntPtr.Zero, typeof(ArgumentNullException), "Must provide valid data to load");
Debug2.Validate(length > 0, typeof(ArgumentOutOfRangeException), "Must provide non-zero length data to load");
Debug2.Validate(rgData != null, typeof(ArgumentNullException), "Must provide valid data to load");
Debug2.Validate(rgData.Length > 0, typeof(ArgumentOutOfRangeException), "Must provide non-zero length data to load");
Debug2.Validate(req != null, typeof(ArgumentNullException), "Must provide valid ImageRequirements");
ExtensionsApi.BitmapOptions nOptions = ExtensionsApi.BitmapOptions.None;
BitmapInformation bitmapInformation = new BitmapInformation();
HRESULT hresult = new HRESULT(-1);
try
{
hresult = ExtensionsApi.SpBitmapLoadBuffer(rgData, (uint)length, req, nOptions, out bitmapInformation.hBitmap, out bitmapInformation.imageInfo);
hresult = ExtensionsApi.SpBitmapLoadBuffer(rgData, (uint)rgData.Length, req, nOptions, out bitmapInformation.hBitmap, out bitmapInformation.imageInfo);
if (!hresult.IsSuccess())
header = new ImageHeader();
else
@@ -133,8 +129,7 @@ namespace Microsoft.Iris.Render.Extensions
public static bool FromBuffer(
IImage image,
IntPtr buffer,
int length,
byte[] buffer,
Size maxSize,
bool flipRTL,
bool antialiasEdges,
@@ -143,7 +138,7 @@ namespace Microsoft.Iris.Render.Extensions
out BitmapInformation bitmapInfo)
{
Debug2.Validate(image != null, typeof(ArgumentNullException), "Image must be valid");
Debug2.Validate(length > 0, typeof(ArgumentException), "Do not call for zero-length buffer");
Debug2.Validate(buffer.Length > 0, typeof(ArgumentException), "Do not call for zero-length buffer");
ImageRequirements req = new ImageRequirements();
req.BorderWidth = borderWidth;
req.BorderColor = borderColor;
@@ -154,7 +149,7 @@ namespace Microsoft.Iris.Render.Extensions
if (flipRTL)
nOptions |= ExtensionsApi.BitmapOptions.Flip;
BitmapInformation bitmapInformation = new BitmapInformation();
if (!ExtensionsApi.SpBitmapLoadBuffer(buffer, (uint)length, req, nOptions, out bitmapInformation.hBitmap, out bitmapInformation.imageInfo).IsSuccess())
if (!ExtensionsApi.SpBitmapLoadBuffer(buffer, (uint)buffer.Length, req, nOptions, out bitmapInformation.hBitmap, out bitmapInformation.imageInfo).IsSuccess())
{
bitmapInfo = null;
return false;
@@ -174,8 +169,7 @@ namespace Microsoft.Iris.Render.Extensions
public static bool FromRaw(
IImage image,
IntPtr buffer,
int length,
byte[] buffer,
Size imageSize,
int stride,
SurfaceFormat format,
@@ -187,7 +181,7 @@ namespace Microsoft.Iris.Render.Extensions
out BitmapInformation bitmapInfo)
{
Debug2.Validate(image != null, typeof(ArgumentNullException), "Image must be valid");
Debug2.Validate(length > 0, typeof(ArgumentException), "Do not call for zero-length buffer");
Debug2.Validate(buffer.Length > 0, typeof(ArgumentException), "Do not call for zero-length buffer");
ImageRequirements req = new ImageRequirements();
req.BorderWidth = borderWidth;
req.BorderColor = borderColor;
@@ -53,10 +53,10 @@ namespace Microsoft.Iris.Render.Extensions
return hinstance;
}
public void LoadResource(
public unsafe void LoadResource(
Win32Api.HINSTANCE hInstance,
string resourceId,
out IntPtr resourceData,
out byte[] resourceData,
out int resourceSize)
{
IntPtr resource = Win32Api.FindResource(hInstance.h, resourceId, new IntPtr(10));
@@ -66,7 +66,7 @@ namespace Microsoft.Iris.Render.Extensions
IntPtr num1 = Win32Api.LockResource(i);
Debug2.Validate(num1 != IntPtr.Zero, typeof(InvalidOperationException), "Failed to aquire pointer to resource data");
int num2 = Win32Api.SizeofResource(hInstance.h, resource);
resourceData = num1;
resourceData = new Span<byte>(num1.ToPointer(), num2).ToArray();
resourceSize = num2;
}
}
@@ -21,19 +21,19 @@ namespace Microsoft.Iris.Render.Extensions
{
Win32Api.HINSTANCE hInstance = ModuleManager.Instance.LoadModule(moduleName);
Debug2.Validate(hInstance != Win32Api.HINSTANCE.NULL, typeof(ArgumentException), nameof(moduleName));
IntPtr resourceData;
byte[] resourceData;
int resourceSize;
ModuleManager.Instance.LoadResource(hInstance, resourceId, out resourceData, out resourceSize);
FromMemory(resourceData, resourceSize, out soundDataHandle, out soundDataInfo);
}
public static void FromMemory(
IntPtr rawSoundData,
byte[] rawSoundData,
int rawSoundDataSize,
out ExtensionsApi.HSpSound soundDataHandle,
out ExtensionsApi.SoundInformation soundDataInfo)
{
Debug2.Validate(rawSoundData != IntPtr.Zero, typeof(ArgumentNullException), nameof(rawSoundData));
Debug2.Validate(rawSoundData != null, typeof(ArgumentNullException), nameof(rawSoundData));
Debug2.Validate(rawSoundDataSize > 0, typeof(ArgumentException), "Invalid sound data size");
ExtensionsApi.SoundOptions options = ExtensionsApi.SoundOptions.Decode;
ExtensionsApi.SoundInformation info = new ExtensionsApi.SoundInformation();