Skip to content

Commit

Permalink
Fixed disposable issues with PropVariant which caused a breaking chan…
Browse files Browse the repository at this point in the history
…ge when using methods with PropVariant. Added Media Foundation GUID constants. Fixes #53
  • Loading branch information
smourier committed Mar 22, 2024
1 parent 7e79685 commit b2a458a
Show file tree
Hide file tree
Showing 128 changed files with 2,792 additions and 408 deletions.
35 changes: 35 additions & 0 deletions DirectN/DirectN/DirectN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9651,6 +9651,41 @@
<Compile Include="Generated\ID3D10ShaderReflectionConstantBuffer.cs" />
<Compile Include="Generated\ID3D10ShaderReflectionType.cs" />
<Compile Include="Generated\ID3D10ShaderReflectionVariable.cs" />
<Compile Include="Generated\D3D12_DEVICE_CONFIGURATION_DESC.cs" />
<Compile Include="Generated\D3D12_DEVICE_FACTORY_FLAGS.cs" />
<Compile Include="Generated\D3D12_DEVICE_FLAGS.cs" />
<Compile Include="Generated\D3D12_FEATURE_DATA_D3D12_OPTIONS19.cs" />
<Compile Include="Generated\D3D12_FEATURE_DATA_D3D12_OPTIONS20.cs" />
<Compile Include="Generated\D3D12DDI_OPTIONS_0092.cs" />
<Compile Include="Generated\DWRITE_BITMAP_DATA_BGRA32.cs" />
<Compile Include="Generated\DWRITE_COLOR_COMPOSITE_MODE.cs" />
<Compile Include="Generated\DWRITE_PAINT_ATTRIBUTES.cs" />
<Compile Include="Generated\DWRITE_PAINT_COLOR.cs" />
<Compile Include="Generated\DWRITE_PAINT_ELEMENT.cs" />
<Compile Include="Generated\DWRITE_PAINT_FEATURE_LEVEL.cs" />
<Compile Include="Generated\DWRITE_PAINT_TYPE.cs" />
<Compile Include="Generated\ID2D1Device7.cs" />
<Compile Include="Generated\ID2D1DeviceContext7.cs" />
<Compile Include="Generated\ID2D1Factory8.cs" />
<Compile Include="Generated\ID3D12DeviceConfiguration.cs" />
<Compile Include="Generated\ID3D12DeviceFactory.cs" />
<Compile Include="Generated\ID3D12SDKConfiguration1.cs" />
<Compile Include="Generated\IDCompositionDevice4.cs" />
<Compile Include="Generated\IDCompositionTexture.cs" />
<Compile Include="Generated\IDWriteBitmapRenderTarget2.cs" />
<Compile Include="Generated\IDWriteBitmapRenderTarget3.cs" />
<Compile Include="Generated\IDWriteFactory8.cs" />
<Compile Include="Generated\IDWriteFontFace7.cs" />
<Compile Include="Generated\IDWritePaintReader.cs" />
<Compile Include="Generated\PAINT_COLOR_GLYPH.cs" />
<Compile Include="Generated\PAINT_COMPOSITE.cs" />
<Compile Include="Generated\PAINT_GLYPH.cs" />
<Compile Include="Generated\PAINT_LAYERS.cs" />
<Compile Include="Generated\PAINT_LINEAR_GRADIENT.cs" />
<Compile Include="Generated\PAINT_RADIAL_GRADIENT.cs" />
<Compile Include="Generated\PAINT_SOLID_GLYPH.cs" />
<Compile Include="Generated\PAINT_SWEEP_GRADIENT.cs" />
<Compile Include="Generated\PAINT_UNION.cs" />
</ItemGroup>
<ItemGroup>
<None Include="DirectN.nuspec">
Expand Down
4 changes: 1 addition & 3 deletions DirectN/DirectN/Extensions/ComObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ public static ComObject<T> From<T>(IntPtr unknown, bool dispose = true)
return null;

var typed = (T)Marshal.GetTypedObjectForIUnknown(unknown, typeof(T)); // does addref
var co = new ComObject<T>(typed, dispose);
Marshal.Release(unknown);
return co;
return new ComObject<T>(typed, dispose);
}
#endif

Expand Down
20 changes: 20 additions & 0 deletions DirectN/DirectN/Extensions/DisplayConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;

namespace DirectN
Expand Down Expand Up @@ -111,6 +112,25 @@ public static string GetGdiDeviceName(int adapterIdHighPart, uint adapterIdLowPa
return info.viewGdiDeviceName;
}

public static uint? GetSourceId(uint targetId)
{
var err = GetDisplayConfigBufferSizes(QDC.QDC_ONLY_ACTIVE_PATHS, out var pathCount, out var modeCount);
if (err != 0)
throw new Win32Exception(err);

var paths = new DISPLAYCONFIG_PATH_INFO[pathCount];
var modes = new DISPLAYCONFIG_MODE_INFO[modeCount];
err = QueryDisplayConfig(QDC.QDC_ONLY_ACTIVE_PATHS, ref pathCount, paths, ref modeCount, modes, IntPtr.Zero);
if (err != 0)
throw new Win32Exception(err);

var first = paths.FirstOrDefault(p => p.targetInfo.id == targetId);
if (first.targetInfo.id == targetId)
return first.sourceInfo.id;

return null;
}

[DllImport("user32")]
private static extern int GetDisplayConfigBufferSizes(QDC flags, out int numPathArrayElements, out int numModeInfoArrayElements);

Expand Down
23 changes: 14 additions & 9 deletions DirectN/DirectN/Extensions/IMFAttributesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,18 @@ public static bool TryGetItemByIndex(this IMFAttributes input, uint index, out G
if (input == null)
throw new ArgumentNullException(nameof(input));

HRESULT hr;
using (var pv = new PropVariant())
var detached = new PROPVARIANT();
if (input.GetItemByIndex(index, out key, detached).IsError)
{
hr = input.GetItemByIndex(index, out key, pv);
value = pv.Value;
value = null;
return false;
}

return hr.IsSuccess;
using (var pv = detached.Attach())
{
value = pv.Value;
}
return true;
}

public static void Set2UINT32asUINT64(this IComObject<IMFAttributes> input, Guid key, uint high, uint low) => Set2UINT32asUINT64(input?.Object, key, high, low);
Expand Down Expand Up @@ -344,11 +348,12 @@ public static object GetValue(this IMFAttributes input, Guid key)
if (input == null)
throw new ArgumentNullException(nameof(input));

using (var pv = new PropVariant())
{
if (input.GetItem(key, pv).IsError)
return null;
var detached = new PROPVARIANT();
if (input.GetItem(key, detached).IsError)
return null;

using (var pv = detached.Attach())
{
return pv.Value;
}
}
Expand Down
42 changes: 41 additions & 1 deletion DirectN/DirectN/Extensions/IMFMediaBufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,46 @@ namespace DirectN
{
public static class IMFMediaBufferExtensions
{
public static void WithLock(this IComObject<IMFMediaBuffer> buffer, Action<IntPtr, uint, uint> action) => WithLock(buffer?.Object, action);
public static void WithLock(this IMFMediaBuffer buffer, Action<IntPtr, uint, uint> action)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));

if (action == null)
throw new ArgumentNullException(nameof(action));

var ptr = Lock(buffer, out var max, out var current);
try
{
action(ptr, max, current);
}
finally
{
Unlock(buffer);
}
}

public static T WithLock<T>(this IComObject<IMFMediaBuffer> buffer, Func<IntPtr, uint, uint, T> action) => WithLock<T>(buffer?.Object, action);
public static T WithLock<T>(this IMFMediaBuffer buffer, Func<IntPtr, uint, uint, T> func)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));

if (func == null)
throw new ArgumentNullException(nameof(func));

var ptr = Lock(buffer, out var max, out var current);
try
{
return func(ptr, max, current);
}
finally
{
Unlock(buffer);
}
}

public static IntPtr Lock(this IComObject<IMFMediaBuffer> buffer) => Lock(buffer?.Object, out var max, out var current);
public static IntPtr Lock(this IComObject<IMFMediaBuffer> buffer, out uint maxLength, out uint currentLength) => Lock(buffer?.Object, out maxLength, out currentLength);
public static IntPtr Lock(this IMFMediaBuffer buffer) => Lock(buffer, out _, out _);
Expand All @@ -13,7 +53,7 @@ public static IntPtr Lock(this IMFMediaBuffer buffer, out uint maxLength, out ui
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));

var p = Marshal.AllocHGlobal(8);
var p = Marshal.AllocHGlobal(2 * 4);
try
{
buffer.Lock(out var ptr, p, p + 4).ThrowOnError();
Expand Down
9 changes: 5 additions & 4 deletions DirectN/DirectN/Extensions/IMFMediaTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ public static object GetValue(this IMFMediaType input, Guid key)
if (input == null)
throw new ArgumentNullException(nameof(input));

using (var pv = new PropVariant())
{
if (input.GetItem(key, pv).IsError)
return null;
var detached = new PROPVARIANT();
if (input.GetItem(key, detached).IsError)
return null;

using (var pv = detached.Attach())
{
return pv.Value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void SetMetadataByName(this IWICMetadataQueryWriter writer, string

using (var pv = new PropVariant(value, type))
{
writer.SetMetadataByName(name, pv).ThrowOnError();
writer.SetMetadataByName(name, pv.Detached).ThrowOnError();
}
}

Expand All @@ -31,7 +31,7 @@ public static void SetMetadataByName(this IWICMetadataQueryWriter writer, string
if (pv == null)
throw new ArgumentNullException(nameof(pv));

writer.SetMetadataByName(name, pv).ThrowOnError();
writer.SetMetadataByName(name, pv.Detached).ThrowOnError();
}

public static void RemoveMetadataByName(this IComObject<IWICMetadataQueryWriter> writer, string name) => RemoveMetadataByName(writer?.Object, name);
Expand Down
Loading

0 comments on commit b2a458a

Please sign in to comment.