using System; using System.Numerics; using System.Threading; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; using Microsoft.Graphics.Canvas; using Windows.Foundation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using Win32 = TerraFX.Interop.Windows.Windows; namespace ComputeSharp.D2D1.Uwp; /// /// A custom implementation powered by a supplied shader type. /// /// The type of shader to use to render frames. public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICanvasImage, ICanvasImageInterop where T : unmanaged, ID2D1PixelShader { /// /// The value for the current instance. /// private ReferenceTracker referenceTracker; /// /// Lock object used to synchronize calls to APIs. /// private readonly object lockObject = new(); /// /// Flag to track whether a given call is recursively invoked by , to avoid graph cycles. /// private volatile int isInsideGetD2DImage; /// /// The current device the effect is realized on, if one is available (this is the underlying COM object for ). /// private ComPtr canvasDevice; /// /// The underlying D2D1 device the effect is realized on, if one is available (this is the D2D device backing ). /// private ComPtr d2D1RealizationDevice; /// /// The current realized , if one is available. /// private ComPtr d2D1Effect; /// /// The backing field for . /// private bool cacheOutput; /// /// The backing field for . /// private D2D1_BUFFER_PRECISION d2D1BufferPrecision; /// /// The current value in use. /// public T Value; /// /// Gets or sets a value indicating whether to enable caching the output from drawing this effect. /// public unsafe bool CacheOutput { get { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); lock (this.lockObject) { // If the effect is realized, get the latest value from it if (this.d2D1Effect.Get() is not null) { this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); } return this.cacheOutput; } } set { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); lock (this.lockObject) { this.cacheOutput = value; // If the effect is realized, set the property on the underlying ID2D1Effect object too if (this.d2D1Effect.Get() is not null) { this.d2D1Effect.Get()->SetCachedProperty(value); } } } } /// /// Gets or sets the precision to use for intermediate buffers when drawing this effect. /// /// If , the default precision for effects will be used. public unsafe CanvasBufferPrecision? BufferPrecision { get { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); lock (this.lockObject) { // If the effect is realized, get the latest value from it if (this.d2D1Effect.Get() is not null) { this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); } // Map from D2D1_BUFFER_PRECISION to CanvasBufferPrecision, and return null for D2D1_BUFFER_PRECISION_UNKNOWN return this.d2D1BufferPrecision switch { D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM => CanvasBufferPrecision.Precision8UIntNormalized, D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB => CanvasBufferPrecision.Precision8UIntNormalizedSrgb, D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM => CanvasBufferPrecision.Precision16UIntNormalized, D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT => CanvasBufferPrecision.Precision16Float, D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT => CanvasBufferPrecision.Precision32Float, _ => null }; } } set { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); lock (this.lockObject) { // Map back from CanvasBufferPrecision? to D2D1_BUFFER_PRECISION (null results in D2D1_BUFFER_PRECISION_UNKNOWN) this.d2D1BufferPrecision = value switch { CanvasBufferPrecision.Precision8UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM, CanvasBufferPrecision.Precision8UIntNormalizedSrgb => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB, CanvasBufferPrecision.Precision16UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM, CanvasBufferPrecision.Precision16Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT, CanvasBufferPrecision.Precision32Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT, _ => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN }; // If the effect is realized, set the property on the underlying ID2D1Effect object too if (this.d2D1Effect.Get() is not null) { this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision); } } } } /// Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator) { throw new NotSupportedException(); } /// Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform) { throw new NotSupportedException(); } /// unsafe int ICanvasImageInterop.GetDevice(ICanvasDevice** device) { using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken); // Check for disposal if (!leaseTaken) { return RO.RO_E_CLOSED; } // Ensure we can take a lock without blocking. We can't access the realization device with no // lock, as that could lead to memory leaks or corruption if GetD2DImage was to manipulate that // field concurrently. We also cannot lock directly, because that would deadlock if someone was // to (incorrectly) try to use the same effect from two different threads. To protect against // that, we try to lock, and just return E_NOT_VALID_STATE if another thread holds the lock. If // this call had GetD2DImage in the stack, that would work fine because locks are recursive. if (!Monitor.TryEnter(this.lockObject)) { return E.E_NOT_VALID_STATE; } try { // Copy the device over to the target return this.canvasDevice.CopyTo(device); } finally { Monitor.Exit(this.lockObject); } } /// unsafe int ICanvasImageInterop.GetD2DImage( ICanvasDevice* device, ID2D1DeviceContext* deviceContext, CanvasImageGetD2DImageFlags flags, float targetDpi, float* realizeDpi, ID2D1Image** ppImage) { using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken); // Check for disposal if (!leaseTaken) { return RO.RO_E_CLOSED; } // Check for graph cycles if (Interlocked.CompareExchange(ref this.isInsideGetD2DImage, 1, 0) == 1) { return D2DERR.D2DERR_CYCLIC_GRAPH; } try { // Lock after checking for graph cycles (other public APIs also lock on this object) lock (this.lockObject) { // Process the ReadDpiFromDeviceContext flag (same logic as CanvasEffect::GetD2DImage) if ((flags & CanvasImageGetD2DImageFlags.ReadDpiFromDeviceContext) != CanvasImageGetD2DImageFlags.None) { // If ReadDpiFromDeviceContext is used, the context cannot be null if (deviceContext is null) { return E.E_POINTER; } if (deviceContext->HasCommandListTarget()) { // Command lists are DPI dependent, so DPI compensation effects are always needed flags |= CanvasImageGetD2DImageFlags.AlwaysInsertDpiCompensation; } else { // If the effect is not being drawn onto a command list, the DPIs can be read from the device context targetDpi = deviceContext->GetDpi(); } // ReadDpiFromDeviceContext has been processed, so it can be removed now flags &= ~CanvasImageGetD2DImageFlags.ReadDpiFromDeviceContext; } using ComPtr d2D1Device1 = default; // Get the underlying ID2D1Device1 instance in use device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert(); // Check that the input D2D device is the same instance as the current realization device, if any. // If not and the effect has already been realized, unrealize it and store the new device to use. if (!d2D1Device1.IsSameInstance(in this.d2D1RealizationDevice)) { Unrealize(); // Store the input realization device and canvas device for future uses d2D1Device1.CopyTo(ref this.d2D1RealizationDevice).Assert(); fixed (ICanvasDevice** canvasDevice = this.canvasDevice) { device->QueryInterface(Win32.__uuidof(), (void**)canvasDevice).Assert(); } } // Create the effect, if it doesn't exist already if (this.d2D1Effect.Get() is null) { Realize(flags, targetDpi, deviceContext); } else if (!flags.HasFlag(CanvasImageGetD2DImageFlags.MinimalRealization)) { // Recurse through the effect graph and ensure all nodes are initialized RefreshInputs(flags, targetDpi, deviceContext); } // The realized DPIs are always 0 for pixel shader effects if (realizeDpi is not null) { *realizeDpi = 0; } this.d2D1Effect.CopyTo(ppImage).Assert(); return S.S_OK; } } catch (Exception e) { *ppImage = null; return e.HResult; } finally { // Ensure that reset this flag so that this method can be called again this.isInsideGetD2DImage = 0; } } /// /// Unrealizes the currently realized object. /// private unsafe void Unrealize() { // If there is no ID2D1Effect object, there is nothing left to do if (this.d2D1Effect.Get() is null) { return; } // Cache the last update value of the cache and precision properties this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); // TODO: read back D2D sources from the effect // Finally release the effect as well this.d2D1Effect.Dispose(); } /// /// Realizes the underlying for this object. /// /// The current flags in use. /// The target DPI in use. /// The instance in use. private unsafe void Realize(CanvasImageGetD2DImageFlags flags, float targetDpi, ID2D1DeviceContext* deviceContext) { using ComPtr d2D1Factory = default; // Get the underlying ID2D1Factory from the input context, so we can register and create the effect deviceContext->GetFactory(d2D1Factory.GetAddressOf()); using ComPtr d2D1Factory1 = default; // D2D1PixelShaderEffect APIs specifically need an ID2D1Factory1 (as ID2D1Factory1::RegisterEffectFromString is used) d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert(); // Register the effect with the factory (TODO: only do this if needed) D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1.Get(), out _); fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect) { // Create an instance of the effect in use and store it in the current object D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(deviceContext, (void**)d2D1Effect); } // If cache input has been set, forward that to the new effect if (this.cacheOutput) { this.d2D1Effect.Get()->SetCachedProperty(true); } // If the buffer precision isn't unknown, forward that as well if (this.d2D1BufferPrecision != D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN) { this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision); } // TODO: port base CanvasEffect::Realize logic // Also set the current constants for the effect D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, this.d2D1Effect.Get()); } /// /// Updates the draw state for the currently realized effect. /// /// The current flags in use. /// The target DPI in use. /// The instance in use. private unsafe void RefreshInputs(CanvasImageGetD2DImageFlags flags, float targetDpi, ID2D1DeviceContext* deviceContext) { D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, this.d2D1Effect.Get()); // TODO } }