Skip to content

Commit

Permalink
Add settings menu option to control whether GPU acceleration is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
emichael committed Jan 16, 2024
1 parent 6e5d68c commit 1da27bb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -91,12 +88,8 @@

public class DebuggerWindow extends JFrame {
static {
/*
* Try to enable GPU acceleration (doesn't seem to work very well) and disable UI scaling.
*
* Don't enable GPU acceleration in WSL, though. It does not like it.
*/
if (!runningInWSL()) {
// Try to enable GPU acceleration (doesn't seem to work very well) and disable UI scaling.
if (Utils.gpuAccelerationEnabled()) {
setProperty("sun.java2d.opengl", "true");
}
// Disable this for now since it doesn't seem to be useful
Expand All @@ -114,16 +107,6 @@ public class DebuggerWindow extends JFrame {
PlatformDefaults.setPlatform(PlatformDefaults.GNOME);
}

/** Try to detect whether the visual debugger is running under the Windows Subsystem for Linux. */
private static boolean runningInWSL() {
try {
String procVersion = Files.readString(Path.of("/proc/version"));
return procVersion.toLowerCase().contains("microsoft");
} catch (IOException e) {
return false;
}
}

/** Set a system property if it doesn't already have a value. */
private static void setProperty(String property, String value) {
if (System.getProperty(property) == null) {
Expand Down Expand Up @@ -257,6 +240,17 @@ public DebuggerWindow(final SearchState initialState, SearchSettings searchSetti

darkMode.addActionListener(e -> Utils.setupDarkTheme(true));
lightMode.addActionListener(e -> Utils.setupLightTheme(true));

settingsMenu.addSeparator();

JCheckBoxMenuItem gpuAcceleration =
new JCheckBoxMenuItem("GPU Acceleration", Utils.gpuAccelerationEnabled());
settingsMenu.add(gpuAcceleration);
gpuAcceleration.addActionListener(
e -> {
Utils.setGpuAccelerationEnabled(gpuAcceleration.getState());
});
gpuAcceleration.setToolTipText("Requires restart");
}
setJMenuBar(menuBar);

Expand Down
39 changes: 38 additions & 1 deletion framework/tst/dslabs/framework/testing/visualization/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import com.formdev.flatlaf.FlatLaf;
import java.awt.Color;
import java.awt.Component;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.Icon;
import javax.swing.JScrollPane;
Expand All @@ -37,7 +41,8 @@
import jiconfont.swing.IconFontSwing;
import lombok.NonNull;

abstract class Utils {
final class Utils {
// TODO: move preferences into their own utility file.
static final Preferences PREFERENCES = Preferences.userNodeForPackage(Utils.class);

static {
Expand All @@ -47,6 +52,7 @@ abstract class Utils {

private static final int ICON_SIZE = 18;
private static final String PREF_DARK_MODE = "dark_mode";
private static final String GPU_ACCEL = "gpu_acceleration";

private static Color iconColor() {
return UIManager.getColor("Tree.icon.expandedColor");
Expand Down Expand Up @@ -105,6 +111,35 @@ static void setupDarkTheme(boolean savePreference) {

private static void saveThemePreference(boolean darkMode) {
PREFERENCES.putBoolean(PREF_DARK_MODE, darkMode);
try {
PREFERENCES.flush();
} catch (BackingStoreException ignored) {
// TODO: print exception?
}
}

static boolean gpuAccelerationEnabled() {
// GPU acceleration is enabled by default. It's not in WSL, though; WSL does not like it.
return PREFERENCES.getBoolean(GPU_ACCEL, !runningInWSL());
}

static void setGpuAccelerationEnabled(boolean enabled) {
PREFERENCES.putBoolean(GPU_ACCEL, enabled);
try {
PREFERENCES.flush();
} catch (BackingStoreException ignored) {
// TODO: print exception?
}
}

/** Try to detect whether the visual debugger is running under the Windows Subsystem for Linux. */
private static boolean runningInWSL() {
try {
String procVersion = Files.readString(Path.of("/proc/version"));
return procVersion.toLowerCase().contains("microsoft");
} catch (IOException e) {
return false;
}
}

/**
Expand All @@ -121,4 +156,6 @@ static JScrollPane scrollPane(Component component) {
scrollPane.getHorizontalScrollBar().setUnitIncrement(10);
return scrollPane;
}

private Utils() {}
}

0 comments on commit 1da27bb

Please sign in to comment.