Skip to content

Commit

Permalink
Merge pull request #910 from b-editor/ram-preview
Browse files Browse the repository at this point in the history
描画したフレームのキャッシュ
  • Loading branch information
yuto-trd authored Feb 1, 2024
2 parents f56807e + c5a58f4 commit c1c2565
Show file tree
Hide file tree
Showing 40 changed files with 2,195 additions and 164 deletions.
123 changes: 122 additions & 1 deletion src/Beutl.Configuration/EditorConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Diagnostics;

using Beutl.Collections;
using Beutl.Serialization;
Expand All @@ -11,11 +12,33 @@ public enum LibraryTabDisplayMode
Hide
}

public enum FrameCacheConfigScale
{
Original,

FitToPreviewer,

Half,

Quarter,
}

public enum FrameCacheConfigColorType
{
RGBA,

YUV
}

public sealed class EditorConfig : ConfigurationBase
{
public static readonly CoreProperty<bool> AutoAdjustSceneDurationProperty;
public static readonly CoreProperty<bool> EnablePointerLockInPropertyProperty;
public static readonly CoreProperty<bool> IsAutoSaveEnabledProperty;
public static readonly CoreProperty<bool> IsFrameCacheEnabledProperty;
public static readonly CoreProperty<double> FrameCacheMaxSizeProperty;
public static readonly CoreProperty<FrameCacheConfigScale> FrameCacheScaleProperty;
public static readonly CoreProperty<FrameCacheConfigColorType> FrameCacheColorTypeProperty;

static EditorConfig()
{
Expand All @@ -30,6 +53,28 @@ static EditorConfig()
IsAutoSaveEnabledProperty = ConfigureProperty<bool, EditorConfig>(nameof(IsAutoSaveEnabled))
.DefaultValue(true)
.Register();

IsFrameCacheEnabledProperty = ConfigureProperty<bool, EditorConfig>(nameof(IsFrameCacheEnabled))
.DefaultValue(true)
.Register();

ulong memSize = OperatingSystem.IsWindows() ? GetWindowsMemoryCapacity()
: OperatingSystem.IsLinux() ? GetLinuxMemoryCapacity()
: 1024 * 1024 * 1024;
double memSizeInMG = memSize / (1024d * 1024d);

// デフォルトはメモリ容量の半分にする
FrameCacheMaxSizeProperty = ConfigureProperty<double, EditorConfig>(nameof(FrameCacheMaxSize))
.DefaultValue(memSizeInMG / 2)
.Register();

FrameCacheScaleProperty = ConfigureProperty<FrameCacheConfigScale, EditorConfig>(nameof(FrameCacheScale))
.DefaultValue(FrameCacheConfigScale.FitToPreviewer)
.Register();

FrameCacheColorTypeProperty = ConfigureProperty<FrameCacheConfigColorType, EditorConfig>(nameof(FrameCacheColorType))
.DefaultValue(FrameCacheConfigColorType.RGBA)
.Register();
}

public EditorConfig()
Expand All @@ -48,12 +93,36 @@ public bool EnablePointerLockInProperty
get => GetValue(EnablePointerLockInPropertyProperty);
set => SetValue(EnablePointerLockInPropertyProperty, value);
}

public bool IsAutoSaveEnabled
{
get => GetValue(IsAutoSaveEnabledProperty);
set => SetValue(IsAutoSaveEnabledProperty, value);
}

public bool IsFrameCacheEnabled
{
get => GetValue(IsFrameCacheEnabledProperty);
set => SetValue(IsFrameCacheEnabledProperty, value);
}

public double FrameCacheMaxSize
{
get => GetValue(FrameCacheMaxSizeProperty);
set => SetValue(FrameCacheMaxSizeProperty, value);
}

public FrameCacheConfigScale FrameCacheScale
{
get => GetValue(FrameCacheScaleProperty);
set => SetValue(FrameCacheScaleProperty, value);
}

public FrameCacheConfigColorType FrameCacheColorType
{
get => GetValue(FrameCacheColorTypeProperty);
set => SetValue(FrameCacheColorTypeProperty, value);
}

public CoreDictionary<string, LibraryTabDisplayMode> LibraryTabDisplayModes { get; } = new()
{
Expand Down Expand Up @@ -94,4 +163,56 @@ public override void Deserialize(ICoreSerializationContext context)
}
}
}

private static ulong GetWindowsMemoryCapacity()
{
// wmic memorychip get capacity
using var process = Process.Start(new ProcessStartInfo("wmic", "memorychip get capacity")
{
CreateNoWindow = true,
RedirectStandardOutput = true
});

if (process != null && process.WaitForExit(500))
{
ulong value = 0;
while (process.StandardOutput.ReadLine() is string line)
{
if (ulong.TryParse(line, out ulong v))
{
value += v;
}
}

return value;
}

// https://www.microsoft.com/ja-jp/windows/windows-11-specifications
return 4L * 1024 * 1024 * 1024;
}

private static ulong GetLinuxMemoryCapacity()
{
const string FileName = "/proc/meminfo";
// このifは多分無駄
if (File.Exists(FileName))
{
foreach (string item in File.ReadLines(FileName))
{
if (item.StartsWith("MemTotal:"))
{
string? s = item.Split(' ', StringSplitOptions.RemoveEmptyEntries).ElementAtOrDefault(1);
if (ulong.TryParse(s, out ulong v))
{
// kBで固定されている
// https://github.com/torvalds/linux/blob/3a5879d495b226d0404098e3564462d5f1daa33b/fs/proc/meminfo.c#L31
return v * 1024;
}
}
}
}

// https://help.ubuntu.com/community/Installation/SystemRequirements
return 4L * 1024 * 1024 * 1024;
}
}
4 changes: 2 additions & 2 deletions src/Beutl.Controls/Styles.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@
</ResourceDictionary>
</Styles.Resources>

<Style Selector="PopupRoot">
<!--<Style Selector="PopupRoot">
<Setter Property="TransparencyLevelHint" Value="AcrylicBlur" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="TransparencyBackgroundFallback" Value="Transparent" />
</Style>
</Style>-->
<Style Selector="MenuFlyoutPresenter /template/ Border#LayoutRoot">
<Setter Property="Background">
<SolidColorBrush Opacity="0.725" Color="{DynamicResource SolidBackgroundFillColorBase}" />
Expand Down
78 changes: 78 additions & 0 deletions src/Beutl.Core/Reactive/DisposableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,82 @@ public static T DisposeWith<T>(this T disposable, ICollection<IDisposable> list)
list.Add(disposable);
return disposable;
}

public static void DisposeAll<T1, T2>(in this ValueTuple<T1, T2> tuple)
where T1 : IDisposable
where T2 : IDisposable
{
tuple.Item1.Dispose();
tuple.Item2.Dispose();
}

public static void DisposeAll<T1, T2, T3>(in this ValueTuple<T1, T2, T3> tuple)
where T1 : IDisposable
where T2 : IDisposable
where T3 : IDisposable
{
tuple.Item1.Dispose();
tuple.Item2.Dispose();
tuple.Item3.Dispose();
}

public static void DisposeAll<T1, T2, T3, T4>(in this ValueTuple<T1, T2, T3, T4> tuple)
where T1 : IDisposable
where T2 : IDisposable
where T3 : IDisposable
where T4 : IDisposable
{
tuple.Item1.Dispose();
tuple.Item2.Dispose();
tuple.Item3.Dispose();
tuple.Item4.Dispose();
}

public static void DisposeAll<T1, T2, T3, T4, T5>(in this ValueTuple<T1, T2, T3, T4, T5> tuple)
where T1 : IDisposable
where T2 : IDisposable
where T3 : IDisposable
where T4 : IDisposable
where T5 : IDisposable
{
tuple.Item1.Dispose();
tuple.Item2.Dispose();
tuple.Item3.Dispose();
tuple.Item4.Dispose();
tuple.Item5.Dispose();
}

public static void DisposeAll<T1, T2, T3, T4, T5, T6>(in this ValueTuple<T1, T2, T3, T4, T5, T6> tuple)
where T1 : IDisposable
where T2 : IDisposable
where T3 : IDisposable
where T4 : IDisposable
where T5 : IDisposable
where T6 : IDisposable
{
tuple.Item1.Dispose();
tuple.Item2.Dispose();
tuple.Item3.Dispose();
tuple.Item4.Dispose();
tuple.Item5.Dispose();
tuple.Item6.Dispose();
}

public static void DisposeAll<T1, T2, T3, T4, T5, T6, T7>(in this ValueTuple<T1, T2, T3, T4, T5, T6, T7> tuple)
where T1 : IDisposable
where T2 : IDisposable
where T3 : IDisposable
where T4 : IDisposable
where T5 : IDisposable
where T6 : IDisposable
where T7 : IDisposable
{
tuple.Item1.Dispose();
tuple.Item2.Dispose();
tuple.Item3.Dispose();
tuple.Item4.Dispose();
tuple.Item5.Dispose();
tuple.Item6.Dispose();
tuple.Item7.Dispose();
}
}
90 changes: 90 additions & 0 deletions src/Beutl.Language/SettingsPage.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c1c2565

Please sign in to comment.