From 3203298bed21b9040202ef01f84bf2e2b725258e Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 13 Sep 2026 17:34:34 +0900 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E3=82=A6=E3=82=A3=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=82=A6=E3=81=AE=E9=9A=99=E9=96=93=E3=82=92=E7=84=A1?= =?UTF-8?q?=E3=81=8F=E3=81=97=E3=83=81=E3=83=A3=E3=83=B3=E3=82=AF=E3=81=AE?= =?UTF-8?q?=E7=B6=B2=E7=BE=85=E3=82=92=E4=BD=8D=E7=BD=AE=E3=81=A7=E7=A4=BA?= =?UTF-8?q?=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyForge.Tests/ChunkCoverageBarTests.cs | 120 ++++++++++++++++++ ProxyForge.Tests/ProxyGenerationQueueTests.cs | 17 +++ ProxyForge.Tests/ViewModelTests.cs | 6 + ProxyForge/Encoding/ProxyChunkCoverage.cs | 8 ++ ProxyForge/Encoding/ProxyGenerationItem.cs | 7 + ProxyForge/Encoding/ProxyGenerationQueue.cs | 26 +++- .../ViewModels/GenerationItemViewModel.cs | 5 + ProxyForge/Views/ChunkCoverageBar.cs | 62 +++++++++ .../Views/GenerationProgressWindow.xaml | 16 ++- tools/mutations.json | 36 ++++++ 10 files changed, 291 insertions(+), 12 deletions(-) create mode 100644 ProxyForge.Tests/ChunkCoverageBarTests.cs create mode 100644 ProxyForge/Encoding/ProxyChunkCoverage.cs create mode 100644 ProxyForge/Views/ChunkCoverageBar.cs diff --git a/ProxyForge.Tests/ChunkCoverageBarTests.cs b/ProxyForge.Tests/ChunkCoverageBarTests.cs new file mode 100644 index 0000000..ed1d928 --- /dev/null +++ b/ProxyForge.Tests/ChunkCoverageBarTests.cs @@ -0,0 +1,120 @@ +using System.Windows; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using ProxyForge.Encoding; +using ProxyForge.Views; + +namespace ProxyForge.Tests; + +[Collection("Wpf")] +public sealed class ChunkCoverageBarTests +{ + static (double Start, double Length)[] SegmentsOf(ProxyChunkCoverage coverage) => [.. ChunkCoverageBar.Segments(coverage)]; + + [Fact] + public void NothingIsDrawnWithoutChunks() + { + Assert.Empty(SegmentsOf(ProxyChunkCoverage.Empty)); + Assert.Empty(SegmentsOf(new ProxyChunkCoverage(4, [], null, 0d))); + } + + [Fact] + public void CoveredChunksAreDrawnAtTheirPositions() + { + var segments = SegmentsOf(new ProxyChunkCoverage(4, [3, 1], null, 0d)); + + Assert.Equal([(0.25d, 0.25d), (0.75d, 0.25d)], segments); + } + + [Fact] + public void AdjacentChunksAreMergedIntoOneSegment() + { + var segments = SegmentsOf(new ProxyChunkCoverage(5, [0, 1, 2, 4], null, 0d)); + + Assert.Equal([(0d, 0.6d), (0.8d, 0.2d)], segments); + } + + [Fact] + public void ChunksOutsideTheLayoutAndDuplicatesAreIgnored() + { + var segments = SegmentsOf(new ProxyChunkCoverage(4, [7, -1, 2, 2], null, 0d)); + + Assert.Equal([(0.5d, 0.25d)], segments); + } + + [Fact] + public void TheChunkBeingGeneratedIsDrawnPartially() + { + var segments = SegmentsOf(new ProxyChunkCoverage(4, [0], 2, 0.5d)); + + Assert.Equal([(0d, 0.25d), (0.5d, 0.125d)], segments); + } + + [Fact] + public void TheCurrentChunkIsNotDrawnTwiceOrWhenNothingIsDone() + { + Assert.Equal([(0.5d, 0.25d)], SegmentsOf(new ProxyChunkCoverage(4, [2], 2, 0.5d))); + Assert.Empty(SegmentsOf(new ProxyChunkCoverage(4, [], 2, 0d))); + Assert.Empty(SegmentsOf(new ProxyChunkCoverage(4, [], 9, 0.5d))); + Assert.Equal([(0.5d, 0.25d)], SegmentsOf(new ProxyChunkCoverage(4, [], 2, 1.5d))); + } + + [Fact] + public void RendersGreenWhereChunksExist() + { + var pixels = RunSta(() => + { + var bar = new ChunkCoverageBar + { + Coverage = new ProxyChunkCoverage(4, [1], 2, 0.5d), + Fill = Brushes.Lime, + }; + bar.Measure(new Size(100d, 10d)); + bar.Arrange(new Rect(0d, 0d, 100d, 10d)); + bar.UpdateLayout(); + var bitmap = new RenderTargetBitmap(100, 10, 96d, 96d, PixelFormats.Pbgra32); + bitmap.Render(bar); + var buffer = new byte[100 * 10 * 4]; + bitmap.CopyPixels(buffer, 100 * 4, 0); + return buffer; + }); + + Assert.Equal(0, AlphaAt(pixels, 10)); + Assert.Equal(255, AlphaAt(pixels, 30)); + Assert.Equal(255, GreenAt(pixels, 30)); + Assert.Equal(0, RedAt(pixels, 30)); + Assert.Equal(255, AlphaAt(pixels, 55)); + Assert.Equal(0, AlphaAt(pixels, 70)); + Assert.Equal(0, AlphaAt(pixels, 90)); + } + + static byte AlphaAt(byte[] pixels, int x) => pixels[5 * 100 * 4 + x * 4 + 3]; + + static byte GreenAt(byte[] pixels, int x) => pixels[5 * 100 * 4 + x * 4 + 1]; + + static byte RedAt(byte[] pixels, int x) => pixels[5 * 100 * 4 + x * 4 + 2]; + + static T RunSta(Func action) + { + var result = default(T)!; + Exception? error = null; + var thread = new Thread(() => + { + try + { + result = action(); + } + catch (Exception exception) + { + error = exception; + } + }); + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + thread.Join(); + + if (error is not null) + throw new InvalidOperationException(error.Message, error); + return result; + } +} diff --git a/ProxyForge.Tests/ProxyGenerationQueueTests.cs b/ProxyForge.Tests/ProxyGenerationQueueTests.cs index d59b276..e7060ed 100644 --- a/ProxyForge.Tests/ProxyGenerationQueueTests.cs +++ b/ProxyForge.Tests/ProxyGenerationQueueTests.cs @@ -149,6 +149,7 @@ public sealed class ProxyGenerationQueueTests : IDisposable var started = new TaskCompletionSource(); var release = new TaskCompletionSource(); var progressed = new List(); + var covered = new List(); var queue = CreateQueue(async (_, _, output, progress, _) => { started.TrySetResult(); @@ -161,10 +162,13 @@ public sealed class ProxyGenerationQueueTests : IDisposable queue.TryEnqueue(source, 50); await started.Task; var item = TestUiThread.Read(() => Assert.Single(queue.Items)); + Assert.Equal(new ProxyChunkCoverage(2, [], 0, 0d), item.Coverage, CoverageComparer.Instance); item.PropertyChanged += (_, e) => { if (e.PropertyName == nameof(ProxyGenerationItem.Progress)) progressed.Add(item.Progress); + if (e.PropertyName == nameof(ProxyGenerationItem.Coverage)) + covered.Add(item.Coverage); }; Assert.Equal(source.Path, item.SourcePath); Assert.Equal("source.mp4", item.FileName); @@ -180,9 +184,22 @@ public sealed class ProxyGenerationQueueTests : IDisposable Assert.Equal(progressed.OrderBy(value => value), progressed); Assert.Contains(0.25d, progressed); Assert.Contains(0.5d, progressed); + Assert.Contains(new ProxyChunkCoverage(2, [], 0, 0.5d), covered, CoverageComparer.Instance); + Assert.Contains(new ProxyChunkCoverage(2, [0], 1, 0.5d), covered, CoverageComparer.Instance); + Assert.Equal(new ProxyChunkCoverage(2, [0, 1], null, 0d), item.Coverage, CoverageComparer.Instance); await WaitUntilAsync(() => queue.Items.Count == 0); } + sealed class CoverageComparer : IEqualityComparer + { + public static CoverageComparer Instance { get; } = new(); + + public bool Equals(ProxyChunkCoverage? x, ProxyChunkCoverage? y) + => x is not null && y is not null && x.ChunkCount == y.ChunkCount && x.Chunks.SequenceEqual(y.Chunks) && x.CurrentChunk == y.CurrentChunk && x.CurrentProgress == y.CurrentProgress; + + public int GetHashCode(ProxyChunkCoverage coverage) => HashCode.Combine(coverage.ChunkCount, coverage.Chunks.Count, coverage.CurrentChunk, coverage.CurrentProgress); + } + [Fact] public async Task ChunksStartAtTheFocusAndFinishTheRest() { diff --git a/ProxyForge.Tests/ViewModelTests.cs b/ProxyForge.Tests/ViewModelTests.cs index 14d826e..53065b5 100644 --- a/ProxyForge.Tests/ViewModelTests.cs +++ b/ProxyForge.Tests/ViewModelTests.cs @@ -135,6 +135,12 @@ public sealed class GenerationItemViewModelTests Assert.Equal(25d, viewModel.Percentage); Assert.Contains(nameof(GenerationItemViewModel.Percentage), raised); + Assert.Same(ProxyChunkCoverage.Empty, viewModel.Coverage); + var coverage = new ProxyChunkCoverage(4, [1], 2, 0.5d); + item.Coverage = coverage; + Assert.Same(coverage, viewModel.Coverage); + Assert.Contains(nameof(GenerationItemViewModel.Coverage), raised); + item.Status = ProxyGenerationStatus.Generating; Assert.False(viewModel.IsWaiting); Assert.Equal(Texts.StatusGenerating, viewModel.StatusText); diff --git a/ProxyForge/Encoding/ProxyChunkCoverage.cs b/ProxyForge/Encoding/ProxyChunkCoverage.cs new file mode 100644 index 0000000..734a884 --- /dev/null +++ b/ProxyForge/Encoding/ProxyChunkCoverage.cs @@ -0,0 +1,8 @@ +namespace ProxyForge.Encoding; + +internal sealed record ProxyChunkCoverage(int ChunkCount, IReadOnlyList Chunks, int? CurrentChunk, double CurrentProgress) +{ + public static ProxyChunkCoverage Empty { get; } = new(0, [], null, 0d); + + public bool Contains(int chunk) => Chunks.Contains(chunk); +} diff --git a/ProxyForge/Encoding/ProxyGenerationItem.cs b/ProxyForge/Encoding/ProxyGenerationItem.cs index adc2503..903d8c5 100644 --- a/ProxyForge/Encoding/ProxyGenerationItem.cs +++ b/ProxyForge/Encoding/ProxyGenerationItem.cs @@ -15,6 +15,7 @@ public enum ProxyGenerationStatus internal sealed class ProxyGenerationItem(string sourcePath, int scale) : Bindable { double progress; + ProxyChunkCoverage coverage = ProxyChunkCoverage.Empty; ProxyGenerationStatus status; ProxyEncodeFailure? failure; @@ -30,6 +31,12 @@ internal sealed class ProxyGenerationItem(string sourcePath, int scale) : Bindab set => Set(ref progress, value); } + public ProxyChunkCoverage Coverage + { + get => coverage; + set => Set(ref coverage, value); + } + public ProxyGenerationStatus Status { get => status; diff --git a/ProxyForge/Encoding/ProxyGenerationQueue.cs b/ProxyForge/Encoding/ProxyGenerationQueue.cs index 51859e7..7b2aaa1 100644 --- a/ProxyForge/Encoding/ProxyGenerationQueue.cs +++ b/ProxyForge/Encoding/ProxyGenerationQueue.cs @@ -22,9 +22,17 @@ internal sealed class ProxyGenerationQueue( readonly record struct Key(SourceIdentity Source, int Scale); - sealed class ChunkProgress(ProxyGenerationItem item, int completedChunks, int chunkCount, Action onUi) : IProgress + sealed class ChunkProgress(ProxyGenerationItem item, ProxyCacheEntry entry, int chunk, Action onUi) : IProgress { - public void Report(double value) => onUi(() => item.Progress = (completedChunks + Math.Clamp(value, 0d, 1d)) / chunkCount); + public void Report(double value) + { + var fraction = Math.Clamp(value, 0d, 1d); + onUi(() => + { + item.Progress = (entry.Chunks.Count + fraction) / entry.ChunkCount; + item.Coverage = new ProxyChunkCoverage(entry.ChunkCount, entry.Chunks, chunk, fraction); + }); + } } readonly Lock gate = new(); @@ -199,7 +207,7 @@ internal sealed class ProxyGenerationQueue( var analysis = session.Analysis; var entry = cache.Register(Describe(key, analysis, ProxyChunkPlan.LengthFor(analysis.FrameRate, current.ChunkSeconds))); var chunkCount = entry.ChunkCount; - onUi(() => item.Progress = (double)entry.Chunks.Count / chunkCount); + Publish(item, entry, null); while (ProxyChunkPlan.Next(entry.Chunks, chunkCount, entry.ChunkLength, focus.Get(key.Source)) is { } chunk) { @@ -211,7 +219,8 @@ internal sealed class ProxyGenerationQueue( ProxyCacheEntry? updated; try { - await session.EncodeAsync(firstFrame, frameCount, temporaryPath, new ChunkProgress(item, entry.Chunks.Count, chunkCount, onUi), token).ConfigureAwait(false); + Publish(item, entry, chunk); + await session.EncodeAsync(firstFrame, frameCount, temporaryPath, new ChunkProgress(item, entry, chunk, onUi), token).ConfigureAwait(false); if (SourceIdentity.Of(key.Source.Path) != key.Source) throw new ProxyEncodeException(ProxyEncodeFailure.SourceChanged, "The source file changed while the proxy was being generated."); @@ -227,11 +236,18 @@ internal sealed class ProxyGenerationQueue( throw new OperationCanceledException(); entry = updated; - onUi(() => item.Progress = (double)entry.Chunks.Count / chunkCount); + Publish(item, entry, null); ChunkCompleted?.Invoke(key.Source, key.Scale, entry); } } + void Publish(ProxyGenerationItem item, ProxyCacheEntry entry, int? chunk) + => onUi(() => + { + item.Progress = (double)entry.Chunks.Count / entry.ChunkCount; + item.Coverage = new ProxyChunkCoverage(entry.ChunkCount, entry.Chunks, chunk, 0d); + }); + static ProxyCacheEntry Describe(Key key, ProxyAnalysis analysis, int chunkLength) => new() { SourcePath = key.Source.Path, diff --git a/ProxyForge/ViewModels/GenerationItemViewModel.cs b/ProxyForge/ViewModels/GenerationItemViewModel.cs index 034826d..5a88132 100644 --- a/ProxyForge/ViewModels/GenerationItemViewModel.cs +++ b/ProxyForge/ViewModels/GenerationItemViewModel.cs @@ -21,6 +21,8 @@ internal sealed class GenerationItemViewModel : Bindable public double Percentage => Item.Progress * 100d; + public ProxyChunkCoverage Coverage => Item.Coverage; + public bool IsWaiting => Item.Status == ProxyGenerationStatus.Waiting; public string StatusText => Item.Status switch @@ -52,6 +54,9 @@ internal sealed class GenerationItemViewModel : Bindable case nameof(ProxyGenerationItem.Progress): OnPropertyChanged(nameof(Percentage)); break; + case nameof(ProxyGenerationItem.Coverage): + OnPropertyChanged(nameof(Coverage)); + break; case nameof(ProxyGenerationItem.Status): case nameof(ProxyGenerationItem.Failure): OnPropertyChanged(nameof(StatusText)); diff --git a/ProxyForge/Views/ChunkCoverageBar.cs b/ProxyForge/Views/ChunkCoverageBar.cs new file mode 100644 index 0000000..bbafa32 --- /dev/null +++ b/ProxyForge/Views/ChunkCoverageBar.cs @@ -0,0 +1,62 @@ +using System.Windows; +using System.Windows.Media; +using ProxyForge.Encoding; + +namespace ProxyForge.Views; + +internal sealed class ChunkCoverageBar : FrameworkElement +{ + public static readonly DependencyProperty CoverageProperty = DependencyProperty.Register( + nameof(Coverage), typeof(ProxyChunkCoverage), typeof(ChunkCoverageBar), + new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender)); + + public static readonly DependencyProperty FillProperty = DependencyProperty.Register( + nameof(Fill), typeof(Brush), typeof(ChunkCoverageBar), + new FrameworkPropertyMetadata(Brushes.LimeGreen, FrameworkPropertyMetadataOptions.AffectsRender)); + + public ProxyChunkCoverage? Coverage + { + get => (ProxyChunkCoverage?)GetValue(CoverageProperty); + set => SetValue(CoverageProperty, value); + } + + public Brush Fill + { + get => (Brush)GetValue(FillProperty); + set => SetValue(FillProperty, value); + } + + public static IEnumerable<(double Start, double Length)> Segments(ProxyChunkCoverage coverage) + { + if (coverage.ChunkCount <= 0) + yield break; + + double count = coverage.ChunkCount; + var chunks = coverage.Chunks.Where(chunk => chunk >= 0 && chunk < coverage.ChunkCount).Distinct().Order().ToArray(); + var index = 0; + while (index < chunks.Length) + { + var first = chunks[index]; + var last = first; + while (index + 1 < chunks.Length && chunks[index + 1] == last + 1) + last = chunks[++index]; + + yield return (first / count, (last - first + 1) / count); + index++; + } + + if (coverage.CurrentChunk is { } current && current >= 0 && current < coverage.ChunkCount && !coverage.Contains(current) && coverage.CurrentProgress > 0d) + yield return (current / count, Math.Min(coverage.CurrentProgress, 1d) / count); + } + + protected override void OnRender(DrawingContext drawingContext) + { + if (Coverage is not { } coverage) + return; + + var width = RenderSize.Width; + var height = RenderSize.Height; + foreach (var (start, length) in Segments(coverage)) + drawingContext.DrawRectangle(Fill, null, new Rect(start * width, 0d, length * width, height)); + } +} diff --git a/ProxyForge/Views/GenerationProgressWindow.xaml b/ProxyForge/Views/GenerationProgressWindow.xaml index fc134af..e9890ce 100644 --- a/ProxyForge/Views/GenerationProgressWindow.xaml +++ b/ProxyForge/Views/GenerationProgressWindow.xaml @@ -5,6 +5,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:root="clr-namespace:ProxyForge" xmlns:vm="clr-namespace:ProxyForge.ViewModels" + xmlns:views="clr-namespace:ProxyForge.Views" xmlns:host="clr-namespace:YukkuriMovieMaker.Resources.Localization;assembly=YukkuriMovieMaker.Plugin" mc:Ignorable="d" d:DataContext="{d:DesignInstance Type={x:Type vm:GenerationProgressViewModel}}" @@ -15,11 +16,11 @@