Skip to content

Commit dedd939

Browse files
Remove FramebufferSize from WebGPUSurfaceFrame
1 parent 66b9e71 commit dedd939

12 files changed

Lines changed: 29 additions & 61 deletions

File tree

samples/WebGPUExternalSurfaceDemo/Controls/WebGPURenderControl.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,10 @@ public WebGPURenderControl()
4444
}
4545

4646
/// <summary>
47-
/// Raised each frame once the external surface has acquired a drawable frame. The canvas dimensions match
48-
/// <see cref="FramebufferSize"/>.
47+
/// Raised each frame once the external surface has acquired a drawable frame.
4948
/// </summary>
5049
public event Action<DrawingCanvas<Bgra32>, TimeSpan>? PaintFrame;
5150

52-
/// <summary>
53-
/// Gets the drawable framebuffer size in pixels.
54-
/// </summary>
55-
public Size FramebufferSize => this.framebufferSize;
56-
5751
/// <inheritdoc />
5852
protected override void OnHandleCreated(EventArgs e)
5953
{

samples/WebGPUExternalSurfaceDemo/MainForm.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,14 @@ public MainForm()
117117
}
118118

119119
private void OnPaintClock(DrawingCanvas<Bgra32> canvas, TimeSpan delta)
120-
{
121-
Size s = this.clockControl.FramebufferSize;
122-
123-
// Scene coordinates match the drawable framebuffer, not the form's layout bounds.
124-
this.clockScene.Paint(canvas, new SixLabors.ImageSharp.Size(s.Width, s.Height), delta);
125-
}
120+
=> this.clockScene.Paint(canvas, delta);
126121

127122
private void OnPaintTiger(DrawingCanvas<Bgra32> canvas, TimeSpan delta)
128123
{
129-
Size s = this.tigerControl.FramebufferSize;
130-
131-
// Scene coordinates match the drawable framebuffer, not the form's layout bounds.
132-
this.tigerScene.Paint(canvas, new SixLabors.ImageSharp.Size(s.Width, s.Height), delta);
124+
this.tigerScene.Paint(canvas, delta);
133125
this.tigerStatusLabel.Text = this.tigerScene.StatusText;
134126
}
135127

136128
private void OnPaintApply(DrawingCanvas<Bgra32> canvas, TimeSpan delta)
137-
{
138-
Size s = this.applyControl.FramebufferSize;
139-
140-
// Scene coordinates match the drawable framebuffer, not the form's layout bounds.
141-
this.applyScene.Paint(canvas, new SixLabors.ImageSharp.Size(s.Width, s.Height), delta);
142-
}
129+
=> this.applyScene.Paint(canvas, delta);
143130
}

samples/WebGPUExternalSurfaceDemo/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ this.framebufferSize = this.ClientSize;
7575
this.surface.Resize(new ImageSharpSize(this.framebufferSize.Width, this.framebufferSize.Height));
7676
```
7777

78-
The sample stores that size as `FramebufferSize` so scene code can draw in the same pixel coordinate space as the acquired frame.
78+
The acquired frame exposes the same pixel coordinate space through `frame.Canvas.Bounds`.
7979

8080
### Frame Acquisition
8181

@@ -114,7 +114,6 @@ The scenes are deliberately ordinary canvas code:
114114
Each scene receives:
115115

116116
- `DrawingCanvas<Bgra32>` for the acquired frame
117-
- the current framebuffer size
118117
- elapsed time since the previous frame
119118

120119
## Files

samples/WebGPUExternalSurfaceDemo/Scenes/ApplyReadbackScene.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ internal sealed class ApplyReadbackScene : RenderScene
3535

3636
public override string DisplayName => "Apply";
3737

38-
public override void Paint(DrawingCanvas<Bgra32> canvas, Size viewportSize, TimeSpan deltaTime)
38+
public override void Paint(DrawingCanvas<Bgra32> canvas, TimeSpan deltaTime)
3939
{
40-
if (viewportSize.Width <= 0 || viewportSize.Height <= 0)
41-
{
42-
return;
43-
}
40+
Size viewportSize = canvas.Bounds.Size;
4441

45-
canvas.Fill(Brushes.Solid(BackgroundColor), new RectangularPolygon(0, 0, viewportSize.Width, viewportSize.Height));
42+
canvas.Fill(Brushes.Solid(BackgroundColor), canvas.Bounds);
4643
DrawPattern(canvas, viewportSize);
4744

4845
// Keep both regions tied to the pointer so every movement exercises readback from a different part
@@ -63,7 +60,7 @@ public override void Paint(DrawingCanvas<Bgra32> canvas, Size viewportSize, Time
6360
canvas.Apply(edgeRegion, ctx => ctx.DetectEdges());
6461
canvas.Apply(blurRegion, ctx => ctx.GaussianBlur(Math.Max(3F, Math.Min(viewportSize.Width, viewportSize.Height) / 120F)));
6562

66-
canvas.Draw(Pens.Solid(OutlineColor, 3), new RectangularPolygon(edgeRegion.X, edgeRegion.Y, edgeRegion.Width, edgeRegion.Height));
63+
canvas.Draw(Pens.Solid(OutlineColor, 3), new RectangularPolygon(edgeRegion));
6764
canvas.Draw(Pens.Solid(OutlineColor, 3), blurRegion);
6865
}
6966

samples/WebGPUExternalSurfaceDemo/Scenes/ClockScene.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public ClockScene()
5252

5353
public override string DisplayName => "Clock";
5454

55-
public override void Paint(DrawingCanvas<Bgra32> canvas, Size viewportSize, TimeSpan deltaTime)
55+
public override void Paint(DrawingCanvas<Bgra32> canvas, TimeSpan deltaTime)
5656
{
57+
Size viewportSize = canvas.Bounds.Size;
58+
5759
// Background clear.
58-
canvas.Fill(
59-
Brushes.Solid(BackgroundColor),
60-
new RectangularPolygon(0, 0, viewportSize.Width, viewportSize.Height));
60+
canvas.Fill(Brushes.Solid(BackgroundColor), canvas.Bounds);
6161

6262
// The scene is rebuilt from the framebuffer size each frame. That keeps resize handling simple
6363
// and demonstrates drawing directly in surface pixel coordinates.

samples/WebGPUExternalSurfaceDemo/Scenes/RenderScene.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ internal abstract class RenderScene
2222
/// Draws the scene into <paramref name="canvas"/> for the current frame.
2323
/// </summary>
2424
/// <param name="canvas">The per-frame drawing canvas bound to the external surface's swap-chain texture.</param>
25-
/// <param name="viewportSize">The framebuffer size in pixels.</param>
2625
/// <param name="deltaTime">Elapsed time since the previous frame. Scenes that render from absolute state can ignore it.</param>
27-
public abstract void Paint(DrawingCanvas<Bgra32> canvas, Size viewportSize, TimeSpan deltaTime);
26+
public abstract void Paint(DrawingCanvas<Bgra32> canvas, TimeSpan deltaTime);
2827

2928
/// <summary>
3029
/// Handles a mouse-button press. Default implementation is a no-op.

samples/WebGPUExternalSurfaceDemo/Scenes/TigerViewerScene.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ public string StatusText
7171
}
7272
}
7373

74-
public override void Paint(DrawingCanvas<Bgra32> canvas, Size viewportSize, TimeSpan deltaTime)
74+
public override void Paint(DrawingCanvas<Bgra32> canvas, TimeSpan deltaTime)
7575
{
76+
Size viewportSize = canvas.Bounds.Size;
77+
7678
if (this.needsInitialFit || viewportSize != this.lastViewportSize)
7779
{
7880
// The first frame and every resize recenter the artwork. User pan/zoom then operates from
@@ -83,9 +85,7 @@ public override void Paint(DrawingCanvas<Bgra32> canvas, Size viewportSize, Time
8385

8486
this.lastViewportSize = viewportSize;
8587

86-
canvas.Fill(
87-
Brushes.Solid(BackgroundColor),
88-
new RectangularPolygon(0, 0, viewportSize.Width, viewportSize.Height));
88+
canvas.Fill(Brushes.Solid(BackgroundColor), canvas.Bounds);
8989

9090
SizeF screenCenter = viewportSize / 2f;
9191

samples/WebGPUWindowDemo/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,15 @@ private void OnUpdate(TimeSpan deltaTime)
142142
/// <summary>
143143
/// Draws one frame into the acquired WebGPU window surface.
144144
/// </summary>
145-
/// <param name="frame">The acquired frame that exposes the canvas and per-frame timing data.</param>
145+
/// <param name="frame">The acquired frame that exposes the drawing canvas.</param>
146146
/// <remarks>
147147
/// The window loop disposes the frame after this callback returns, which flushes any queued canvas work,
148148
/// presents the swapchain texture, and releases the per-frame WebGPU handles.
149149
/// </remarks>
150150
private void OnRender(WebGPUSurfaceFrame<Bgra32> frame)
151151
{
152152
DrawingCanvas<Bgra32> canvas = frame.Canvas;
153+
Rectangle bounds = canvas.Bounds;
153154
canvas.Fill(Brushes.Solid(Color.FromPixel(new Bgra32(30, 30, 40, 255))));
154155

155156
for (int i = 0; i < this.balls.Length; i++)
@@ -159,7 +160,7 @@ private void OnRender(WebGPUSurfaceFrame<Bgra32> frame)
159160
canvas.Fill(Brushes.Solid(ball.Color), ellipse);
160161
}
161162

162-
this.DrawScrollingText(canvas, frame.FramebufferSize.Width, frame.FramebufferSize.Height);
163+
this.DrawScrollingText(canvas, bounds.Width, bounds.Height);
163164

164165
this.frameCount++;
165166
TimeSpan elapsed = this.fpsWindow.Elapsed;

src/ImageSharp.Drawing.WebGPU/WebGPUSurfaceFrame{TPixel}.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ internal WebGPUSurfaceFrame(
2929
WebGPUTextureHandle textureHandle,
3030
WebGPUTextureViewHandle textureViewHandle,
3131
DrawingCanvas<TPixel> canvas,
32-
Size framebufferSize,
3332
Action? onDisposed = null)
3433
{
3534
this.api = api;
3635
this.surfaceReference = surfaceHandle.AcquireReference();
3736
this.textureHandle = textureHandle;
3837
this.textureViewHandle = textureViewHandle;
3938
this.Canvas = canvas;
40-
this.FramebufferSize = framebufferSize;
4139
this.onDisposed = onDisposed;
4240
}
4341

@@ -46,11 +44,6 @@ internal WebGPUSurfaceFrame(
4644
/// </summary>
4745
public DrawingCanvas<TPixel> Canvas { get; }
4846

49-
/// <summary>
50-
/// Gets the framebuffer size at frame acquisition time.
51-
/// </summary>
52-
public Size FramebufferSize { get; }
53-
5447
/// <summary>
5548
/// Flushes pending canvas work and presents the frame on screen.
5649
/// </summary>

src/ImageSharp.Drawing.WebGPU/WebGPUSurfaceResources{TPixel}.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,8 @@ private void ConfigureSurfaceCore(WebGPUPresentMode presentMode, Size framebuffe
266266
/// <param name="presentMode">The present mode applied when the surface needs to be reconfigured in response to a
267267
/// <c>Timeout</c>/<c>Outdated</c>/<c>Lost</c> acquire status.</param>
268268
/// <param name="framebufferSize">The current framebuffer size in pixels. A zero-area value causes the method to
269-
/// return <see langword="false"/> without touching the surface. Otherwise this size is used both for the returned
270-
/// frame's <see cref="WebGPUSurfaceFrame{TPixel}.FramebufferSize"/> and for any in-place surface
271-
/// reconfiguration triggered by a non-success acquire status.</param>
269+
/// return <see langword="false"/> without touching the surface. Otherwise this size is used for the returned
270+
/// frame canvas and for any in-place surface reconfiguration triggered by a non-success acquire status.</param>
272271
/// <param name="options">The drawing options that seed the canvas on the returned frame.</param>
273272
/// <param name="frame">Receives the acquired frame on success.</param>
274273
/// <returns><see langword="true"/> when a frame is available; <see langword="false"/> when no drawable frame is
@@ -361,7 +360,6 @@ public bool TryAcquireFrame(
361360
textureHandle,
362361
textureViewHandle,
363362
canvas,
364-
framebufferSize,
365363
onDisposed: () => this.frameInFlight = false);
366364

367365
return true;

0 commit comments

Comments
 (0)