Skip to content

Commit f63d421

Browse files
committed
AMD Oled Power Optimization clenaup #4977 #5048
1 parent 888f1ca commit f63d421

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

app/Display/AmdDisplay.cs

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,64 @@
11
using Microsoft.Win32;
22
using Ryzen;
3-
using System.Diagnostics;
43

5-
namespace GHelper.Display
4+
public static class AmdDisplay
65
{
7-
public static class AmdDisplay
6+
private const string DisplayPath0 =
7+
@"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0001\DAL2_DATA__2_0\DisplayPath_0";
8+
9+
private static string _cachedAdjustmentPath = null;
10+
private static bool _isPathSearched = false;
11+
12+
private static string GetAdjustmentPath()
813
{
9-
private const string DisplayPath0 =
10-
@"SYSTEM\CurrentControlSet\Control\Class\" +
11-
@"{4d36e968-e325-11ce-bfc1-08002be10318}\" +
12-
@"0001\DAL2_DATA__2_0\DisplayPath_0";
14+
if (_isPathSearched) return _cachedAdjustmentPath;
1315

14-
public static bool IsOledPowerOptimizationOnBattery()
16+
try
1517
{
16-
if (!AppConfig.IsOLED()) return false;
17-
//if (SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Offline) return false;
18-
if (!RyzenControl.IsAMD()) return false;
19-
20-
try
18+
using RegistryKey dp0 = Registry.LocalMachine.OpenSubKey(DisplayPath0, writable: false);
19+
if (dp0 != null)
2120
{
22-
using RegistryKey dp0 = Registry.LocalMachine.OpenSubKey(DisplayPath0, writable: false);
23-
if (dp0 == null) return false;
24-
25-
// Find the first EDID_* subkey — there's only one for the internal panel
2621
string edidKey = Array.Find(dp0.GetSubKeyNames(),
2722
name => name.StartsWith("EDID_", StringComparison.OrdinalIgnoreCase));
2823

29-
if (edidKey == null) return false;
24+
if (edidKey != null)
25+
{
26+
_cachedAdjustmentPath = $@"HKEY_LOCAL_MACHINE\{DisplayPath0}\{edidKey}\Adjustment";
27+
}
28+
}
29+
}
30+
catch
31+
{
32+
// Handle permissions or missing keys silently
33+
}
34+
finally
35+
{
36+
_isPathSearched = true;
37+
}
38+
39+
return _cachedAdjustmentPath;
40+
}
3041

31-
using RegistryKey adjustment = dp0.OpenSubKey($@"{edidKey}\Adjustment", writable: false);
42+
public static bool IsOledPowerOptimization()
43+
{
44+
if (!AppConfig.IsOLED() || !RyzenControl.IsAMD()) return false;
3245

33-
if (adjustment?.GetValue("DAL_SCE_Settings") is not byte[] data || data.Length < 5)
34-
return false;
46+
try
47+
{
48+
string path = GetAdjustmentPath();
49+
if (path == null) return false;
3550

36-
// Byte[4]: 0x02 = battery OLED optimization ON, 0x00 = OFF
37-
return (data[4] & 0x02) != 0;
38-
} catch
51+
object value = Registry.GetValue(path, "DAL_SCE_Settings", null);
52+
53+
if (value is byte[] data && data.Length >= 5)
3954
{
40-
return false;
55+
return (data[4] & 0x02) != 0;
4156
}
42-
57+
} catch
58+
{
59+
Logger.WriteLine("Can't check AMD OLED Optimization flag");
4360
}
61+
62+
return false;
4463
}
4564
}

app/Display/VisualControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public static void SetVisual(SplendidCommand mode = SplendidCommand.Default, int
274274
{
275275
Task.Run(async () =>
276276
{
277-
if (AmdDisplay.IsOledPowerOptimizationOnBattery()) Program.settingsForm.VisualiseAmdOled(true);
277+
if (AmdDisplay.IsOledPowerOptimization()) Program.settingsForm.VisualiseAmdOled(true);
278278
});
279279

280280
if (mode == SplendidCommand.None) return;
@@ -372,7 +372,7 @@ private static int RunSplendid(SplendidCommand command, int? param1 = null, int?
372372
bool isVivo = AppConfig.IsVivoZenPro();
373373
bool isSplenddid = File.Exists(splendidExe);
374374

375-
if (AmdDisplay.IsOledPowerOptimizationOnBattery())
375+
if (AmdDisplay.IsOledPowerOptimization())
376376
{
377377
Logger.WriteLine("Skipping command due to AMD OLED Power Optimization flag");
378378
Program.settingsForm.VisualiseAmdOled(true);

app/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ private void SettingsForm_Focused(object? sender, EventArgs e)
622622
if (activateCheck)
623623
{
624624
buttonEnergySaver.Visible = PowerNative.GetBatterySaverStatus();
625-
buttonAmdOled.Visible = AmdDisplay.IsOledPowerOptimizationOnBattery();
625+
buttonAmdOled.Visible = AmdDisplay.IsOledPowerOptimization();
626626
activateCheck = false;
627627
}
628628
}

0 commit comments

Comments
 (0)