Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
VideoFramePresenter.cs
1using System.Drawing;
3using SkiaSharp;
4
5namespace Gondwana.Video;
6
10public sealed class VideoFramePresenter : IDisposable
11{
12 private readonly VideoFrameMailbox _mailbox = new();
13 private readonly int _maximumFramePixels;
14 private SKBitmap? _frame;
15
18 public VideoFramePresenter(int maximumFramePixels = int.MaxValue / 4)
19 {
20 ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maximumFramePixels);
21 _maximumFramePixels = maximumFramePixels;
22 }
23
27 {
28 if ((long)frame.Width * frame.Height > _maximumFramePixels)
29 throw new InvalidDataException($"Video exceeds the {_maximumFramePixels:N0}-pixel frame limit.");
30
31 _mailbox.Publish(frame);
32 }
33
35 public void Reset() => _mailbox.Reset();
36
39 public bool Update() => _mailbox.Consume(ref _frame);
40
45 public void Draw(SKCanvas canvas, RectangleF bounds, StretchMode stretch = StretchMode.Uniform)
46 {
47 if (_frame is not { } bitmap)
48 return;
49
50 var destination = ComputeDestRect(
51 bounds,
52 bitmap.Width,
53 bitmap.Height,
54 stretch);
55 canvas.Save();
56 canvas.ClipRect(new SKRect(
57 bounds.Left,
58 bounds.Top,
59 bounds.Right,
60 bounds.Bottom));
61 canvas.DrawBitmap(bitmap, destination);
62 canvas.Restore();
63 }
64
66 public void Dispose()
67 {
68 _mailbox.Dispose();
69 _frame?.Dispose();
70 _frame = null;
71 }
72
73 internal static SKRect ComputeDestRect(
74 RectangleF bounds,
75 int srcW,
76 int srcH,
77 StretchMode mode)
78 {
79 var b = bounds;
80
81 var dst = new SKRect(
82 b.Left,
83 b.Top,
84 b.Right,
85 b.Bottom);
86
87 if (mode == StretchMode.Fill || srcW <= 0 || srcH <= 0)
88 return dst;
89
90 float bw = b.Width;
91 float bh = b.Height;
92 float arSrc = (float)srcW / srcH;
93 float arDst = bw / bh;
94
95 if (mode == StretchMode.None)
96 return new SKRect(
97 b.Left,
98 b.Top,
99 b.Left + srcW,
100 b.Top + srcH);
101
102 if (mode == StretchMode.Uniform)
103 {
104 if (arSrc > arDst)
105 {
106 float w = bw;
107 float h = w / arSrc;
108 float y = b.Top + (bh - h) / 2f;
109
110 return new SKRect(
111 b.Left,
112 y,
113 b.Left + w,
114 y + h);
115 }
116 else
117 {
118 float h = bh;
119 float w = h * arSrc;
120 float x = b.Left + (bw - w) / 2f;
121
122 return new SKRect(
123 x,
124 b.Top,
125 x + w,
126 b.Top + h);
127 }
128 }
129
130 // UniformToFill
131 if (arSrc < arDst)
132 {
133 float w = bw;
134 float h = w / arSrc;
135 float y = b.Top + (bh - h) / 2f;
136
137 return new SKRect(
138 b.Left,
139 y,
140 b.Left + w,
141 y + h);
142 }
143 else
144 {
145 float h = bh;
146 float w = h * arSrc;
147 float x = b.Left + (bw - w) / 2f;
148
149 return new SKRect(
150 x,
151 b.Top,
152 x + w,
153 b.Top + h);
154 }
155 }
156}
void Reset()
Clears queued frames when the source changes; Update clears the displayed frame.
void Dispose()
Closes the callback mailbox and releases presentation-owned pixels.
void Draw(SKCanvas canvas, RectangleF bounds, StretchMode stretch=StretchMode.Uniform)
Draws the retained frame, fitting and clipping it to the requested bounds.
VideoFramePresenter(int maximumFramePixels=int.MaxValue/4)
Creates a bounded frame presenter.
void Publish(VideoFrameReadyEventArgs frame)
Copies a borrowed frame before its decoder callback returns.
bool Update()
Consumes the latest frame on the presentation thread.
Provides data for video frame ready events, containing frame buffer information and timing data.
int Width
Gets the width of the video frame in pixels.
int Height
Gets the height of the video frame in pixels.
StretchMode
Defines how video content should be stretched or scaled to fit within its display bounds.
Definition StretchMode.cs:7