Skip to content

Commit

Permalink
Add flag to enable double-buffering when doing paint operations
Browse files Browse the repository at this point in the history
By using the `org.eclipse.draw2d.LightweightSystem.useDoubleBuffering`,
clients can enable a special functionality, that first paints all UI
updates to an internal buffer, before it is painted to the screen.

The hope of behavior is to increase the perceived performance of an
application by detaching rendering from the display.

eclipse#430
  • Loading branch information
ptziegler committed Apr 26, 2024
1 parent abd2af1 commit f3cb287
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# GEF Classic 3.20.0
# GEF Classic 3.20.0 (Eclipse 2024-03)

## Draw2d
- _Experimental:_ Clients can set the `org.eclipse.draw2d.LightweightSystem.useDoubleBuffering` system property to enable double buffering while performing paint operations.

## GEF
- Clients can now configure the accessible steps of tools through methods `accGetStep()`, `accStepIncrement` and `accStepReset` from `org.eclipse.gef.tools.AbstractTool`.
Expand Down
8 changes: 8 additions & 0 deletions org.eclipse.draw2d/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/draw2d/LightweightSystem.java" type="org.eclipse.draw2d.LightweightSystem">
<filter id="336658481">
<message_arguments>
<message_argument value="org.eclipse.draw2d.LightweightSystem"/>
<message_argument value="KEY_USE_DOUBLE_BUFFERING"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/draw2d/Orientable.java" type="org.eclipse.draw2d.Orientable">
<filter id="576720909">
<message_arguments>
Expand Down
39 changes: 38 additions & 1 deletion org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.swt.accessibility.AccessibleListener;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusEvent;
Expand All @@ -34,6 +35,7 @@
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
Expand All @@ -54,13 +56,23 @@
* </ol>
*/
public class LightweightSystem {
/**
* Indicates whether double-buffering should be used when painting the figures.
* This is an experimental feature that might be removed in a future release.
*
* @since 3.16
* @noreference This field is not intended to be referenced by clients.
*/
public static final String KEY_USE_DOUBLE_BUFFERING = "org.eclipse.draw2d.LightweightSystem.useDoubleBuffering"; //$NON-NLS-1$
private static final boolean USE_DOUBLE_BUFFERING = Boolean.valueOf(System.getProperty(KEY_USE_DOUBLE_BUFFERING));

private Canvas canvas;
IFigure contents;
private IFigure root;
private EventDispatcher dispatcher;
private UpdateManager manager = new DeferredUpdateManager();
private int ignoreResize;
private Image bufferedImage;

/**
* Constructs a LightweightSystem on Canvas <i>c</i>.
Expand Down Expand Up @@ -100,6 +112,17 @@ protected void addListeners() {
canvas.addFocusListener(handler);
canvas.addDisposeListener(handler);
canvas.addListener(SWT.MouseWheel, handler);
canvas.addDisposeListener(event -> {
if (bufferedImage != null) {
bufferedImage.dispose();
}
});
canvas.addControlListener(ControlListener.controlResizedAdapter(event -> {
if (bufferedImage != null) {
bufferedImage.dispose();
}
bufferedImage = null;
}));

canvas.addControlListener(new ControlAdapter() {
@Override
Expand Down Expand Up @@ -199,7 +222,21 @@ EventHandler internalCreateEventHandler() {
* @since 2.0
*/
public void paint(GC gc) {
getUpdateManager().paint(gc);
if (USE_DOUBLE_BUFFERING) {
if (bufferedImage == null) {
Rectangle bounds = getRootFigure().getBounds();
bufferedImage = new Image(null, bounds.width, bounds.height);
}
GC bufferedGC = new GC(bufferedImage);
try {
getUpdateManager().paint(bufferedGC);
gc.drawImage(bufferedImage, 0, 0);
} finally {
bufferedGC.dispose();
}
} else {
getUpdateManager().paint(gc);
}
}

/**
Expand Down

0 comments on commit f3cb287

Please sign in to comment.