Skip to content

Commit 66327e4

Browse files
Rename Graphics to DeviceContext and make field
1 parent be9ab86 commit 66327e4

3 files changed

Lines changed: 51 additions & 60 deletions

File tree

src/ImageSharp.Drawing.WebGPU/WebGPURenderTarget.cs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace SixLabors.ImageSharp.Drawing.Processing.Backends;
1515
/// </remarks>
1616
public sealed class WebGPURenderTarget : IDisposable
1717
{
18-
private readonly bool ownsGraphics;
18+
private readonly WebGPUDeviceContext deviceContext;
19+
private readonly bool ownsDeviceContext;
1920
private bool isDisposed;
2021

2122
/// <summary>
@@ -65,29 +66,29 @@ public WebGPURenderTarget(
6566
WebGPUTextureFormat format,
6667
int width,
6768
int height)
68-
: this(new WebGPUDeviceContext(configuration), ownsGraphics: true, format, width, height)
69+
: this(new WebGPUDeviceContext(configuration), true, format, width, height)
6970
{
7071
}
7172

7273
private WebGPURenderTarget(
73-
WebGPUDeviceContext graphics,
74-
bool ownsGraphics,
74+
WebGPUDeviceContext deviceContext,
75+
bool ownsDeviceContext,
7576
WebGPUTextureFormat format,
7677
int width,
7778
int height)
7879
{
79-
this.Graphics = graphics;
80-
this.ownsGraphics = ownsGraphics;
80+
this.deviceContext = deviceContext;
81+
this.ownsDeviceContext = ownsDeviceContext;
8182

8283
try
8384
{
84-
graphics.ThrowIfDisposed();
85+
deviceContext.ThrowIfDisposed();
8586

8687
WebGPU api = WebGPURuntime.GetApi();
8788
NativeSurface surface = WebGPURenderTargetAllocation.CreateRenderTarget(
8889
api,
89-
graphics.DeviceHandle,
90-
graphics.QueueHandle,
90+
deviceContext.DeviceHandle,
91+
deviceContext.QueueHandle,
9192
format,
9293
width,
9394
height,
@@ -102,9 +103,9 @@ private WebGPURenderTarget(
102103
}
103104
catch
104105
{
105-
if (ownsGraphics)
106+
if (ownsDeviceContext)
106107
{
107-
graphics.Dispose();
108+
deviceContext.Dispose();
108109
}
109110

110111
throw;
@@ -114,12 +115,7 @@ private WebGPURenderTarget(
114115
/// <summary>
115116
/// Gets the WebGPU drawing backend used by this target.
116117
/// </summary>
117-
internal WebGPUDrawingBackend Backend => this.Graphics.Backend;
118-
119-
/// <summary>
120-
/// Gets the graphics device context used by this target.
121-
/// </summary>
122-
private WebGPUDeviceContext Graphics { get; }
118+
internal WebGPUDrawingBackend Backend => this.deviceContext.Backend;
123119

124120
/// <summary>
125121
/// Gets the native surface backing this render target.
@@ -172,12 +168,12 @@ public DrawingCanvas CreateCanvas()
172168
public DrawingCanvas CreateCanvas(DrawingOptions options)
173169
{
174170
this.ThrowIfDisposed();
175-
this.Graphics.ThrowIfDisposed();
171+
this.deviceContext.ThrowIfDisposed();
176172

177173
return WebGPUCanvasFactory.CreateCanvas(
178-
this.Graphics.Configuration,
174+
this.deviceContext.Configuration,
179175
options,
180-
this.Graphics.Backend,
176+
this.deviceContext.Backend,
181177
this.Bounds,
182178
this.Surface,
183179
this.Format);
@@ -192,7 +188,7 @@ public Image<TPixel> Readback<TPixel>()
192188
where TPixel : unmanaged, IPixel<TPixel>
193189
{
194190
this.ThrowIfDisposed();
195-
this.Graphics.ThrowIfDisposed();
191+
this.deviceContext.ThrowIfDisposed();
196192

197193
Image<TPixel> image = new(this.Width, this.Height);
198194
try
@@ -217,7 +213,7 @@ public void ReadbackInto<TPixel>(Image<TPixel> destination)
217213
{
218214
Guard.NotNull(destination, nameof(destination));
219215
this.ThrowIfDisposed();
220-
this.Graphics.ThrowIfDisposed();
216+
this.deviceContext.ThrowIfDisposed();
221217

222218
if (destination.Width != this.Width || destination.Height != this.Height)
223219
{
@@ -230,8 +226,8 @@ public void ReadbackInto<TPixel>(Image<TPixel> destination)
230226

231227
// ReadRegion owns the pixel-format check because it is the point where
232228
// typed CPU pixels are copied from the native WebGPU texture.
233-
this.Graphics.Backend.ReadRegion(
234-
this.Graphics.Configuration,
229+
this.deviceContext.Backend.ReadRegion(
230+
this.deviceContext.Configuration,
235231
frame,
236232
new Rectangle(0, 0, this.Width, this.Height),
237233
new Buffer2DRegion<TPixel>(destination.Frames.RootFrame.PixelBuffer));
@@ -250,9 +246,9 @@ public void Dispose()
250246
this.TextureViewHandle.Dispose();
251247
this.TextureHandle.Dispose();
252248

253-
if (this.ownsGraphics)
249+
if (this.ownsDeviceContext)
254250
{
255-
this.Graphics.Dispose();
251+
this.deviceContext.Dispose();
256252
}
257253

258254
this.isDisposed = true;
@@ -262,11 +258,11 @@ public void Dispose()
262258
/// Allocates an owned render target for the specified context, format, and size.
263259
/// </summary>
264260
internal static WebGPURenderTarget CreateFromContext(
265-
WebGPUDeviceContext graphics,
261+
WebGPUDeviceContext deviceContext,
266262
WebGPUTextureFormat format,
267263
int width,
268264
int height)
269-
=> new(graphics, ownsGraphics: false, format, width, height);
265+
=> new(deviceContext, false, format, width, height);
270266

271267
private void ThrowIfDisposed()
272268
=> ObjectDisposedException.ThrowIf(this.isDisposed, this);

src/ImageSharp.Drawing.WebGPU/WebGPURuntime.DeviceSharedState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal sealed class DeviceSharedState : IDisposable
6161
private readonly ConcurrentDictionary<string, CompositeComputePipelineInfrastructure> compositeComputePipelines = new(StringComparer.Ordinal);
6262
private readonly HashSet<FeatureName> deviceFeatures;
6363
private WebGPUHandle.HandleReference deviceReference;
64-
private PfnErrorCallback uncapturedErrorCallback;
64+
private readonly PfnErrorCallback uncapturedErrorCallback;
6565
private bool disposed;
6666

6767
internal DeviceSharedState(WebGPU api, WebGPUDeviceHandle deviceHandle)

src/ImageSharp.Drawing.WebGPU/WebGPUSurfaceResources.cs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace SixLabors.ImageSharp.Drawing.Processing.Backends;
1010

1111
/// <summary>
12-
/// Owning container for the per-surface WebGPU stack: instance, surface, adapter, device, queue, drawing context,
12+
/// Owning container for the per-surface WebGPU stack: instance, surface, adapter, device, queue, device context,
1313
/// and the negotiated swapchain texture format.
1414
/// </summary>
1515
/// <remarks>
@@ -38,19 +38,20 @@ internal sealed unsafe class WebGPUSurfaceResources : IDisposable
3838
private bool frameInFlight;
3939
private readonly FeatureName requiredFeature;
4040
private readonly Configuration configuration;
41+
private WebGPUDeviceContext deviceContext;
4142

4243
/// <summary>
4344
/// Initializes a new instance of the <see cref="WebGPUSurfaceResources"/> class with already-acquired handles.
4445
/// Only invoked by <see cref="Create"/> after every handle has been successfully bootstrapped.
4546
/// </summary>
4647
/// <param name="api">The shared WebGPU API loader.</param>
47-
/// <param name="configuration">The ImageSharp configuration the drawing context uses for its rendering backend.</param>
48+
/// <param name="configuration">The ImageSharp configuration the device context uses for its rendering backend.</param>
4849
/// <param name="instanceHandle">The owned WebGPU instance handle.</param>
4950
/// <param name="surfaceHandle">The owned WebGPU surface handle attached to the native host.</param>
5051
/// <param name="adapterHandle">The owned adapter handle selected for <paramref name="surfaceHandle"/>.</param>
5152
/// <param name="deviceHandle">The owned device handle requested from <paramref name="adapterHandle"/>.</param>
5253
/// <param name="queueHandle">The owned default queue handle paired with <paramref name="deviceHandle"/>.</param>
53-
/// <param name="graphics">The drawing context bound to <paramref name="deviceHandle"/> and <paramref name="queueHandle"/>.</param>
54+
/// <param name="deviceContext">The device context bound to <paramref name="deviceHandle"/> and <paramref name="queueHandle"/>.</param>
5455
/// <param name="format">The negotiated swapchain texture format.</param>
5556
/// <param name="requiredFeature">The optional WebGPU feature required by the selected texture format.</param>
5657
private WebGPUSurfaceResources(
@@ -61,7 +62,7 @@ private WebGPUSurfaceResources(
6162
WebGPUAdapterHandle adapterHandle,
6263
WebGPUDeviceHandle deviceHandle,
6364
WebGPUQueueHandle queueHandle,
64-
WebGPUDeviceContext graphics,
65+
WebGPUDeviceContext deviceContext,
6566
WebGPUTextureFormat format,
6667
FeatureName requiredFeature)
6768
{
@@ -72,7 +73,7 @@ private WebGPUSurfaceResources(
7273
this.AdapterHandle = adapterHandle;
7374
this.DeviceHandle = deviceHandle;
7475
this.QueueHandle = queueHandle;
75-
this.Graphics = graphics;
76+
this.deviceContext = deviceContext;
7677
this.Format = format;
7778
this.requiredFeature = requiredFeature;
7879
}
@@ -108,12 +109,6 @@ private WebGPUSurfaceResources(
108109
/// </summary>
109110
public WebGPUQueueHandle QueueHandle { get; private set; }
110111

111-
/// <summary>
112-
/// Gets the drawing context bound to <see cref="DeviceHandle"/>/<see cref="QueueHandle"/>, used to wrap acquired
113-
/// per-frame textures into <see cref="DrawingCanvas"/> instances.
114-
/// </summary>
115-
internal WebGPUDeviceContext Graphics { get; private set; }
116-
117112
/// <summary>
118113
/// Gets the swapchain texture format chosen at construction time.
119114
/// Stable for the lifetime of this instance.
@@ -124,7 +119,7 @@ private WebGPUSurfaceResources(
124119
/// Bootstraps the full per-surface WebGPU stack bound to <paramref name="nativeSource"/> and leaves the surface
125120
/// configured against <paramref name="initialPresentMode"/> and <paramref name="initialFramebufferSize"/>.
126121
/// </summary>
127-
/// <param name="configuration">The ImageSharp configuration the drawing context will use for its rendering backend.</param>
122+
/// <param name="configuration">The ImageSharp configuration the device context will use for its rendering backend.</param>
128123
/// <param name="nativeSource">The native surface source that provides the platform handles for surface creation.</param>
129124
/// <param name="format">The swapchain texture format.</param>
130125
/// <param name="initialPresentMode">The present mode to apply to the first surface configuration.</param>
@@ -180,7 +175,7 @@ public static WebGPUSurfaceResources Create(
180175
deviceResources.AdapterHandle,
181176
deviceResources.DeviceHandle,
182177
deviceResources.QueueHandle,
183-
deviceResources.Graphics,
178+
deviceResources.DeviceContext,
184179
format,
185180
requiredFeature);
186181

@@ -339,7 +334,7 @@ public bool TryAcquireFrame(
339334
{
340335
textureHandle = new WebGPUTextureHandle(this.Api, (nint)surfaceTexture.Texture, ownsHandle: true);
341336
textureViewHandle = new WebGPUTextureViewHandle(this.Api, (nint)textureView, ownsHandle: true);
342-
DrawingCanvas canvas = this.Graphics.CreateCanvas(
337+
DrawingCanvas canvas = this.deviceContext.CreateCanvas(
343338
options,
344339
textureHandle,
345340
textureViewHandle,
@@ -378,7 +373,7 @@ public bool TryAcquireFrame(
378373
}
379374

380375
/// <summary>
381-
/// Releases every owned handle in reverse acquisition order (graphics context, queue, device, adapter, surface, instance).
376+
/// Releases every owned handle in reverse acquisition order (device context, queue, device, adapter, surface, instance).
382377
/// Idempotent; subsequent calls are no-ops.
383378
/// </summary>
384379
public void Dispose()
@@ -388,7 +383,7 @@ public void Dispose()
388383
return;
389384
}
390385

391-
this.Graphics.Dispose();
386+
this.deviceContext.Dispose();
392387
this.QueueHandle.Dispose();
393388
this.DeviceHandle.Dispose();
394389
this.AdapterHandle.Dispose();
@@ -398,10 +393,10 @@ public void Dispose()
398393
}
399394

400395
/// <summary>
401-
/// Requests the adapter, device, queue, and drawing context for an existing surface.
396+
/// Requests the adapter, device, queue, and device context for an existing surface.
402397
/// </summary>
403398
/// <param name="api">The shared WebGPU API loader.</param>
404-
/// <param name="configuration">The ImageSharp configuration the drawing context will use.</param>
399+
/// <param name="configuration">The ImageSharp configuration the device context will use.</param>
405400
/// <param name="instance">The instance that owns the surface.</param>
406401
/// <param name="surface">The surface the adapter must be compatible with.</param>
407402
/// <param name="requiredFeature">The optional WebGPU feature required by the selected texture format.</param>
@@ -417,7 +412,7 @@ private static DeviceResources CreateDeviceResources(
417412
WebGPUAdapterHandle? adapterHandle = null;
418413
WebGPUDeviceHandle? deviceHandle = null;
419414
WebGPUQueueHandle? queueHandle = null;
420-
WebGPUDeviceContext? graphics = null;
415+
WebGPUDeviceContext? deviceContext = null;
421416

422417
try
423418
{
@@ -433,18 +428,18 @@ private static DeviceResources CreateDeviceResources(
433428
}
434429

435430
queueHandle = new WebGPUQueueHandle(api, (nint)queue, ownsHandle: true);
436-
graphics = new WebGPUDeviceContext(configuration, deviceHandle, queueHandle);
431+
deviceContext = new WebGPUDeviceContext(configuration, deviceHandle, queueHandle);
437432

438-
DeviceResources resources = new(adapterHandle, deviceHandle, queueHandle, graphics);
433+
DeviceResources resources = new(adapterHandle, deviceHandle, queueHandle, deviceContext);
439434
adapterHandle = null;
440435
deviceHandle = null;
441436
queueHandle = null;
442-
graphics = null;
437+
deviceContext = null;
443438
return resources;
444439
}
445440
catch
446441
{
447-
graphics?.Dispose();
442+
deviceContext?.Dispose();
448443
queueHandle?.Dispose();
449444
deviceHandle?.Dispose();
450445
adapterHandle?.Dispose();
@@ -460,7 +455,7 @@ private static DeviceResources CreateDeviceResources(
460455

461456
/// <summary>
462457
/// Recovers the device-owned portion of the surface stack after device loss.
463-
/// The existing instance and surface remain valid; only adapter, device, queue, and drawing context are replaced.
458+
/// The existing instance and surface remain valid; only adapter, device, queue, and device context are replaced.
464459
/// </summary>
465460
/// <param name="presentMode">The present mode applied to the recovered swapchain.</param>
466461
/// <param name="framebufferSize">The framebuffer size in pixels.</param>
@@ -481,18 +476,18 @@ private void RecoverDeviceResources(WebGPUPresentMode presentMode, Size framebuf
481476

482477
this.ConfigureSurfaceCore(presentMode, framebufferSize, deviceResources.DeviceHandle);
483478

484-
WebGPUDeviceContext oldGraphics = this.Graphics;
479+
WebGPUDeviceContext oldDeviceContext = this.deviceContext;
485480
WebGPUQueueHandle oldQueueHandle = this.QueueHandle;
486481
WebGPUDeviceHandle oldDeviceHandle = this.DeviceHandle;
487482
WebGPUAdapterHandle oldAdapterHandle = this.AdapterHandle;
488483

489-
this.Graphics = deviceResources.Graphics;
484+
this.deviceContext = deviceResources.DeviceContext;
490485
this.QueueHandle = deviceResources.QueueHandle;
491486
this.DeviceHandle = deviceResources.DeviceHandle;
492487
this.AdapterHandle = deviceResources.AdapterHandle;
493488
deviceResources = null;
494489

495-
oldGraphics.Dispose();
490+
oldDeviceContext.Dispose();
496491
oldQueueHandle.Dispose();
497492
oldDeviceHandle.Dispose();
498493
oldAdapterHandle.Dispose();
@@ -617,12 +612,12 @@ public DeviceResources(
617612
WebGPUAdapterHandle adapterHandle,
618613
WebGPUDeviceHandle deviceHandle,
619614
WebGPUQueueHandle queueHandle,
620-
WebGPUDeviceContext graphics)
615+
WebGPUDeviceContext deviceContext)
621616
{
622617
this.AdapterHandle = adapterHandle;
623618
this.DeviceHandle = deviceHandle;
624619
this.QueueHandle = queueHandle;
625-
this.Graphics = graphics;
620+
this.DeviceContext = deviceContext;
626621
}
627622

628623
public WebGPUAdapterHandle AdapterHandle { get; }
@@ -631,11 +626,11 @@ public DeviceResources(
631626

632627
public WebGPUQueueHandle QueueHandle { get; }
633628

634-
internal WebGPUDeviceContext Graphics { get; }
629+
internal WebGPUDeviceContext DeviceContext { get; }
635630

636631
public void Dispose()
637632
{
638-
this.Graphics.Dispose();
633+
this.DeviceContext.Dispose();
639634
this.QueueHandle.Dispose();
640635
this.DeviceHandle.Dispose();
641636
this.AdapterHandle.Dispose();

0 commit comments

Comments
 (0)