Add D2D locking to ID2D1ResourceTextureManagerInternal

このコミットが含まれているのは:
Sergio Pedri committed 2022-08-15 15:01:18 +02:00
1 親 bdd4839f55
コミット fcb783195d
3ファイル変更
+51 -11

検索条件に一致するファイルはありません

@@ -116,13 +116,20 @@ partial struct ResourceTextureManager
// If the method has already been called, just forward the call
if (@this->d2D1EffectContext is not null)
{
return @this->d2D1EffectContext->CreateResourceTexture(
@this->d2D1Multithread->Enter();
// Create the resource after taking a D2D lock
int hresult = @this->d2D1EffectContext->CreateResourceTexture(
resourceId: resourceId,
resourceTextureProperties: resourceTextureProperties,
data: data,
strides: strides,
dataSize: dataSize,
resourceTexture: &@this->d2D1ResourceTexture);
@this->d2D1Multithread->Leave();
return hresult;
}
// Initialize into a staging buffer
@@ -165,13 +172,20 @@ partial struct ResourceTextureManager
// If a resource texture already exists, just forward the call
if (@this->d2D1ResourceTexture is not null)
{
return @this->d2D1ResourceTexture->Update(
@this->d2D1Multithread->Enter();
// Take a D2D lock here too to ensure thread safety
int hresult = @this->d2D1ResourceTexture->Update(
minimumExtents: minimumExtents,
maximimumExtents: maximimumExtents,
strides: strides,
dimensions: dimensions,
data: data,
dataCount: dataCount);
@this->d2D1Multithread->Leave();
return hresult;
}
// Otherwise update the staging buffer
@@ -1,5 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.Shaders.Interop.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#if !NET6_0_OR_GREATER
@@ -91,14 +93,25 @@ partial struct ResourceTextureManager
lock (@this->lockHandle.Target!)
{
// If this is the first time the method is called, just store the context
// If this is the first time the method is called, just store the context.
// Before doing so, get an ID2D1Multithread instance, to ensure thread safety.
// For additional info, see docs on ID2D1EffectFactoryExtensions.GetD2D1Multithread.
if (@this->d2D1EffectContext is null)
{
effectContext->AddRef();
using ComPtr<ID2D1Multithread> d2D1Multithread = default;
@this->d2D1EffectContext = effectContext;
int hresult = effectContext->GetD2D1Multithread(d2D1Multithread.GetAddressOf());
return S.S_OK;
// If an ID2D1Multithread object is available, we can safely store the context
if (Windows.SUCCEEDED(hresult))
{
_ = effectContext->AddRef();
@this->d2D1EffectContext = effectContext;
@this->d2D1Multithread = d2D1Multithread.Detach();
}
return hresult;
}
// Otherwise, just do nothing and return S_FALSE. This allows an existing resource texture
+18 -5
ファイルの表示
@@ -100,6 +100,11 @@ internal unsafe partial struct ResourceTextureManager
/// </summary>
private ID2D1EffectContext* d2D1EffectContext;
/// <summary>
/// The <see cref="ID2D1Multithread"/> instance used to lock accesses to <see cref="d2D1EffectContext"/> and <see cref="d2D1ResourceTexture"/>.
/// </summary>
private ID2D1Multithread* d2D1Multithread;
/// <summary>
/// The <see cref="ID2D1ResourceTexture"/> object, if one was created already.
/// </summary>
@@ -155,6 +160,7 @@ internal unsafe partial struct ResourceTextureManager
@this->lpVtblForID2D1ResourceTextureManagerInternal = VtblForID2D1ResourceTextureManagerInternal;
@this->referenceCount = 1;
@this->d2D1EffectContext = null;
@this->d2D1Multithread = null;
@this->d2D1ResourceTexture = null;
@this->resourceId = null;
@this->resourceTextureProperties = default;
@@ -215,14 +221,21 @@ internal unsafe partial struct ResourceTextureManager
{
lock (this.lockHandle.Target!)
{
if (this.d2D1EffectContext is not null)
// Also synchronize on the ID2D1Multithread instance before touching D2D objects
if (this.d2D1Multithread is not null)
{
// Enter the lock, and then free the effect context. That is guaranteed to
// not be null here, as it is only ever set if ID2D1Multithread is retrieved.
this.d2D1Multithread->Enter();
this.d2D1EffectContext->Release();
}
if (this.d2D1ResourceTexture is not null)
{
this.d2D1ResourceTexture->Release();
// Release the resource too if it has been created
if (this.d2D1ResourceTexture is not null)
{
this.d2D1ResourceTexture->Release();
}
this.d2D1Multithread->Leave();
}
if (this.resourceId is not null)