Skip to content

Commit

Permalink
Merge pull request #69 from bric3/refactor-frame-renderer
Browse files Browse the repository at this point in the history
Refactor flamegraph renderer to support custom font / color strategy.
  • Loading branch information
bric3 authored Jun 9, 2022
2 parents 61fcf31 + 432de76 commit 4f0fe24
Show file tree
Hide file tree
Showing 14 changed files with 1,832 additions and 1,392 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import io.github.bric3.fireplace.core.ui.Colors;
import io.github.bric3.fireplace.core.ui.Colors.Palette;
import io.github.bric3.fireplace.flamegraph.ColorMapper;
import io.github.bric3.fireplace.flamegraph.FlameGraph;
import io.github.bric3.fireplace.flamegraph.NodeDisplayStringProvider;
import io.github.bric3.fireplace.flamegraph.FlamegraphView;
import io.github.bric3.fireplace.flamegraph.FrameTextsProvider;
import io.github.bric3.fireplace.flamegraph.ZoomAnimation;
import org.openjdk.jmc.common.util.FormatToolkit;
import org.openjdk.jmc.flightrecorder.stacktrace.tree.Node;
Expand All @@ -36,46 +36,46 @@ public class FlameGraphTab extends JPanel {
private static final JfrFrameColorMode defaultFrameColorMode = JfrFrameColorMode.BY_PACKAGE;
private static final boolean defaultPaintFrameBorder = true;
private static final boolean defaultShowMinimap = true;
private FlameGraph<Node> jfrFlameGraph;
private Consumer<FlameGraph<Node>> dataApplier;
private FlamegraphView<Node> jfrFlamegraphView;
private Consumer<FlamegraphView<Node>> dataApplier;

public FlameGraphTab() {
super(new BorderLayout());

jfrFlameGraph = new FlameGraph<>();
jfrFlameGraph.showMinimap(defaultShowMinimap);
jfrFlameGraph.configureCanvas(ToolTipManager.sharedInstance()::registerComponent);
jfrFlameGraph.putClientProperty(FlameGraph.SHOW_STATS, true);
jfrFlamegraphView = new FlamegraphView<>();
jfrFlamegraphView.showMinimap(defaultShowMinimap);
jfrFlamegraphView.configureCanvas(ToolTipManager.sharedInstance()::registerComponent);
jfrFlamegraphView.putClientProperty(FlamegraphView.SHOW_STATS, true);
// jfrFlameGraph.setTooltipComponentSupplier(BalloonToolTip::new);
jfrFlameGraph.setMinimapShadeColorSupplier(() -> Colors.isDarkMode() ? Colors.translucent_black_40 : Colors.translucent_white_80);
jfrFlamegraphView.setMinimapShadeColorSupplier(() -> Colors.isDarkMode() ? Colors.translucent_black_40 : Colors.translucent_white_80);
var zoomAnimation = new ZoomAnimation();
zoomAnimation.install(jfrFlameGraph);
zoomAnimation.install(jfrFlamegraphView);

var colorPaletteJComboBox = new JComboBox<>(Palette.values());
colorPaletteJComboBox.setSelectedItem(defaultColorPalette);
var colorModeJComboBox = new JComboBox<>(JfrFrameColorMode.values());
colorModeJComboBox.setSelectedItem(defaultFrameColorMode);

ActionListener updateColorSettingsListener = e -> {
jfrFlameGraph.setColorFunction(
jfrFlamegraphView.setColorFunction(
((JfrFrameColorMode) colorModeJComboBox.getSelectedItem())
.colorMapperUsing(ColorMapper.ofObjectHashUsing(
((Palette) colorPaletteJComboBox.getSelectedItem()).colors())));
jfrFlameGraph.requestRepaint();
jfrFlamegraphView.requestRepaint();
};
colorPaletteJComboBox.addActionListener(updateColorSettingsListener);
colorModeJComboBox.addActionListener(updateColorSettingsListener);

var borderToggle = new JCheckBox("Border");
borderToggle.addActionListener(e -> {
jfrFlameGraph.setFrameGapEnabled(borderToggle.isSelected());
jfrFlameGraph.requestRepaint();
jfrFlamegraphView.setFrameGapEnabled(borderToggle.isSelected());
jfrFlamegraphView.requestRepaint();
});
borderToggle.setSelected(defaultPaintFrameBorder);

var minimapToggle = new JCheckBox("Minimap");
minimapToggle.addActionListener(e -> {
jfrFlameGraph.showMinimap(minimapToggle.isSelected());
jfrFlamegraphView.showMinimap(minimapToggle.isSelected());
});
minimapToggle.setSelected(defaultShowMinimap);

Expand All @@ -88,26 +88,26 @@ public FlameGraphTab() {

var wrapper = new JPanel(new BorderLayout());
{
var component = jfrFlameGraph.component;
var component = jfrFlamegraphView.component;
component.setBorder(null);
wrapper.add(component);
}

var timer = new Timer(2_000, e -> {
jfrFlameGraph = new FlameGraph<>();
jfrFlameGraph.showMinimap(defaultShowMinimap);
jfrFlameGraph.configureCanvas(ToolTipManager.sharedInstance()::registerComponent);
jfrFlameGraph.putClientProperty(FlameGraph.SHOW_STATS, true);
jfrFlameGraph.setMinimapShadeColorSupplier(() -> Colors.isDarkMode() ? Colors.translucent_black_40 : Colors.translucent_white_80);
zoomAnimation.install(jfrFlameGraph);
jfrFlamegraphView = new FlamegraphView<>();
jfrFlamegraphView.showMinimap(defaultShowMinimap);
jfrFlamegraphView.configureCanvas(ToolTipManager.sharedInstance()::registerComponent);
jfrFlamegraphView.putClientProperty(FlamegraphView.SHOW_STATS, true);
jfrFlamegraphView.setMinimapShadeColorSupplier(() -> Colors.isDarkMode() ? Colors.translucent_black_40 : Colors.translucent_white_80);
zoomAnimation.install(jfrFlamegraphView);
if (dataApplier != null) {
dataApplier.accept(jfrFlameGraph);
dataApplier.accept(jfrFlamegraphView);
}
updateColorSettingsListener.actionPerformed(null);

wrapper.removeAll();
{
var cmp = jfrFlameGraph.component;
var cmp = jfrFlamegraphView.component;
cmp.setBorder(null);
wrapper.add(cmp);
}
Expand All @@ -128,21 +128,21 @@ public FlameGraphTab() {

var resetZoom = new JButton("1:1");
resetZoom.addActionListener(e -> {
jfrFlameGraph.resetZoom();
jfrFlamegraphView.resetZoom();
});

var searchField = new JTextField("");
searchField.addActionListener(e -> {
var searched = searchField.getText();
if (searched.isEmpty()) {
jfrFlameGraph.highlightFrames(emptySet(), searched);
jfrFlamegraphView.highlightFrames(emptySet(), searched);
return;
}
CompletableFuture.runAsync(() -> {
try {
var matches = jfrFlameGraph.getFrames()
.stream()
.filter(frame -> {
var matches = jfrFlamegraphView.getFrames()
.stream()
.filter(frame -> {
var method = frame.actualNode.getFrame().getMethod();
return method.getMethodName().contains(searched)
|| method.getType().getTypeName().contains(searched)
Expand All @@ -151,8 +151,8 @@ public FlameGraphTab() {
|| method.getFormalDescriptor().replace('/', '.').contains(searched)
;
})
.collect(Collectors.toCollection(() -> Collections.newSetFromMap(new IdentityHashMap<>())));
jfrFlameGraph.highlightFrames(matches, searched);
.collect(Collectors.toCollection(() -> Collections.newSetFromMap(new IdentityHashMap<>())));
jfrFlamegraphView.highlightFrames(matches, searched);
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down Expand Up @@ -183,15 +183,15 @@ public FlameGraphTab(StacktraceTreeModel stacktraceTreeModel) {

public void setStacktraceTreeModel(StacktraceTreeModel stacktraceTreeModel) {
dataApplier = dataApplier(stacktraceTreeModel);
dataApplier.accept(jfrFlameGraph);
dataApplier.accept(jfrFlamegraphView);
}

private Consumer<FlameGraph<Node>> dataApplier(StacktraceTreeModel stacktraceTreeModel) {
private Consumer<FlamegraphView<Node>> dataApplier(StacktraceTreeModel stacktraceTreeModel) {
var flatFrameList = JfrFrameNodeConverter.convert(stacktraceTreeModel);
return (flameGraph) -> flameGraph.setConfigurationAndData(
flatFrameList,
NodeDisplayStringProvider.of(
(frame) -> {
FrameTextsProvider.of(
frame -> {
if (frame.isRoot()) {
var events = stacktraceTreeModel.getItems()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
package io.github.bric3.fireplace.flamegraph;

import io.github.bric3.fireplace.flamegraph.FlameGraph.FlameGraphCanvas;
import io.github.bric3.fireplace.flamegraph.FlameGraph.ZoomAction;
import io.github.bric3.fireplace.flamegraph.FlamegraphView.FlameGraphCanvas;
import io.github.bric3.fireplace.flamegraph.FlamegraphView.ZoomAction;
import org.pushingpixels.radiance.animation.api.Timeline;
import org.pushingpixels.radiance.animation.api.ease.Sine;
import org.pushingpixels.radiance.animation.api.swing.EventDispatchThreadTimelineCallbackAdapter;
Expand All @@ -37,7 +37,7 @@ public class ZoomAnimation implements ZoomAction {
*/
private boolean animateZoomTransitions = !Boolean.getBoolean(ZOOM_ANIMATION_DISABLED_KEY);

public <T> void install(final FlameGraph<T> flameGraph) {
public <T> void install(final FlamegraphView<T> flameGraph) {
flameGraph.overrideZoomAction(this);
}

Expand Down
Loading

0 comments on commit 4f0fe24

Please sign in to comment.