From 1e1e94f52edd5155be49c66870f36443ec2168af Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Wed, 7 Aug 2019 18:15:12 +0300 Subject: [PATCH] Various fixes to vtk swt components * Delete objects to prevent memory leaks * Added additional logic to SWT->AWT event conversion gitlab #22 Change-Id: I2ad4b61171cb7a97475196a9a3afc3130771f04c --- .../swt/vtkInternalSwtComponent.java | 34 ++++++++++++++++--- .../vtk/rendering/swt/vtkSwtComponent.java | 2 ++ .../vtkSwtInteractorForwarderDecorator.java | 27 ++++++++++++--- .../vtk/rendering/vtkAbstractComponent.java | 4 +++ .../vtk/rendering/vtkInteractorForwarder.java | 4 +-- 5 files changed, 61 insertions(+), 10 deletions(-) diff --git a/vtk.rendering/src/vtk/rendering/swt/vtkInternalSwtComponent.java b/vtk.rendering/src/vtk/rendering/swt/vtkInternalSwtComponent.java index 2d73b3d5..ce10c783 100644 --- a/vtk.rendering/src/vtk/rendering/swt/vtkInternalSwtComponent.java +++ b/vtk.rendering/src/vtk/rendering/swt/vtkInternalSwtComponent.java @@ -28,10 +28,29 @@ public class vtkInternalSwtComponent extends GLCanvas implements Listener { caps = new GLCapabilities(GLProfile.get(GLProfile.GL2GL3)); caps.setDoubleBuffered(true); caps.setHardwareAccelerated(true); - caps.setSampleBuffers(false); - caps.setNumSamples(4); + caps.setSampleBuffers(true); + caps.setRedBits(8); + caps.setGreenBits(8); + caps.setBlueBits(8); caps.setAlphaBits(8); + caps.setDepthBits(24); + caps.setStencilBits(8); + +// caps.setAccumRedBits(8); +// caps.setAccumGreenBits(8); +// caps.setAccumBlueBits(8); +// caps.setAccumAlphaBits(8); +// +// caps.setBitmap(true); +// caps.setOnscreen(true); + + + //caps.setPBuffer(true); + //caps.setFBO(true); + caps.setNumSamples(4); + + return caps; } @@ -97,10 +116,18 @@ public class vtkInternalSwtComponent extends GLCanvas implements Listener { this.removeListener(SWT.Close, this); this.removeListener(SWT.Dispose, this); this.removeListener(SWT.Resize, this); + + if (parent != null) { + parent.getRenderer().DrawOff(); + parent.Delete(); + vtkObject.JAVA_OBJECT_MANAGER.gc(false); + parent = null; + } - if (getContext().isCurrent()) { + if (getContext() != null && getContext().isCurrent()) { getContext().release(); } + super.dispose(); } @@ -117,7 +144,6 @@ public class vtkInternalSwtComponent extends GLCanvas implements Listener { vtkObject.JAVA_OBJECT_MANAGER.gc(false); break; case SWT.Close: - // System.out.println("closing"); break; case SWT.Resize: parent.setSize(getClientArea().width, getClientArea().height); diff --git a/vtk.rendering/src/vtk/rendering/swt/vtkSwtComponent.java b/vtk.rendering/src/vtk/rendering/swt/vtkSwtComponent.java index 7d79189d..bdc3c676 100644 --- a/vtk.rendering/src/vtk/rendering/swt/vtkSwtComponent.java +++ b/vtk.rendering/src/vtk/rendering/swt/vtkSwtComponent.java @@ -33,6 +33,7 @@ public class vtkSwtComponent extends vtkAbstractComponent { renderWindow.AddObserver("StartEvent", this, "startEvent"); renderWindow.AddObserver("EndEvent", this, "endEvent"); } + /** * Set the size of the VTK component @@ -96,6 +97,7 @@ public class vtkSwtComponent extends vtkAbstractComponent { this.lock.lock(); // We prevent any further rendering this.inRenderCall = true; + this.renderWindow.Delete(); this.renderWindow = null; super.Delete(); this.lock.unlock(); diff --git a/vtk.rendering/src/vtk/rendering/swt/vtkSwtInteractorForwarderDecorator.java b/vtk.rendering/src/vtk/rendering/swt/vtkSwtInteractorForwarderDecorator.java index 28178862..24ccabe1 100644 --- a/vtk.rendering/src/vtk/rendering/swt/vtkSwtInteractorForwarderDecorator.java +++ b/vtk.rendering/src/vtk/rendering/swt/vtkSwtInteractorForwarderDecorator.java @@ -35,16 +35,19 @@ implements MouseListener, MouseMoveListener, MouseTrackListener, MouseWheelListe this.forwarder = forwarder; } - public static int convertModifiers(int mods) { + public static int convertModifiers(int button , int mods) { int modifiers = 0; if ((mods & SWT.SHIFT) != 0) modifiers |= java.awt.Event.SHIFT_MASK; if ((mods & SWT.CTRL) != 0) modifiers |= java.awt.Event.CTRL_MASK; if ((mods & SWT.ALT) != 0) modifiers |= java.awt.Event.ALT_MASK; + if ((mods & SWT.BUTTON1) != 0 || button == 1) modifiers |= java.awt.event.InputEvent.BUTTON1_MASK; + if ((mods & SWT.BUTTON2) != 0 || button == 2) modifiers |= java.awt.event.InputEvent.BUTTON2_MASK; + if ((mods & SWT.BUTTON3) != 0 || button == 3) modifiers |= java.awt.event.InputEvent.BUTTON3_MASK; return modifiers; } public java.awt.event.KeyEvent convertKeyEvent(org.eclipse.swt.events.KeyEvent e) { - return new java.awt.event.KeyEvent(dummyComponent, 0, (long)e.time, convertModifiers(e.stateMask), e.keyCode, e.character); + return new java.awt.event.KeyEvent(dummyComponent, 0, (long)e.time, convertModifiers(0, e.stateMask), convertKeyCode(e.keyCode), e.character); } public java.awt.event.MouseEvent convertMouseEvent(org.eclipse.swt.events.MouseEvent e) { @@ -52,11 +55,18 @@ implements MouseListener, MouseMoveListener, MouseTrackListener, MouseWheelListe if ((e.button == 1) || (e.stateMask & SWT.BUTTON1) != 0) button = java.awt.event.MouseEvent.BUTTON1; else if ((e.button == 2) || (e.stateMask & SWT.BUTTON2) != 0) button = java.awt.event.MouseEvent.BUTTON2; else if ((e.button == 3) || (e.stateMask & SWT.BUTTON3) != 0) button = java.awt.event.MouseEvent.BUTTON3; - return new java.awt.event.MouseEvent(dummyComponent, 0, (long)e.time, convertModifiers(e.stateMask), e.x, e.y, e.count, false, button); + return new java.awt.event.MouseEvent(dummyComponent, 0, (long)e.time, convertModifiers(e.button,e.stateMask), e.x, e.y, e.count, false, button); + } + + public int convertKeyCode(int swtKeyCode) { + if (swtKeyCode >= 'a' && swtKeyCode <= 'z') { + return Character.toUpperCase(swtKeyCode); + } + return swtKeyCode; } public java.awt.event.MouseWheelEvent convertMouseWheelEvent(org.eclipse.swt.events.MouseEvent e) { - return new java.awt.event.MouseWheelEvent(dummyComponent, 0, e.time, convertModifiers(e.stateMask), e.x, e.y, 0, false, java.awt.event.MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, e.count); + return new java.awt.event.MouseWheelEvent(dummyComponent, 0, e.time, convertModifiers(e.button,e.stateMask), e.x, e.y, 0, false, java.awt.event.MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, e.count); } public void keyPressed(KeyEvent e) { @@ -85,12 +95,21 @@ implements MouseListener, MouseMoveListener, MouseTrackListener, MouseWheelListe } } + int x = 0; + int y = 0; + int d = 4; + public void mouseDown(MouseEvent e) { super.mousePressed(convertMouseEvent(e)); + x = e.x; + y = e.y; } public void mouseUp(MouseEvent e) { super.mouseReleased(convertMouseEvent(e)); + // Emulate AWT mouse clicks + if (Math.abs(x-e.x) < d && Math.abs(y-e.y) < d) + super.mouseClicked(convertMouseEvent(e)); } public void mouseScrolled(MouseEvent e) { diff --git a/vtk.rendering/src/vtk/rendering/vtkAbstractComponent.java b/vtk.rendering/src/vtk/rendering/vtkAbstractComponent.java index e68bd467..84f2fb41 100644 --- a/vtk.rendering/src/vtk/rendering/vtkAbstractComponent.java +++ b/vtk.rendering/src/vtk/rendering/vtkAbstractComponent.java @@ -144,8 +144,12 @@ public abstract class vtkAbstractComponent implements vtkComponent { public void Delete() { this.lock.lock(); + this.renderer.DrawOff(); + this.renderer.Delete(); this.renderer = null; + this.camera.Delete(); this.camera = null; + this.windowInteractor.Delete(); this.windowInteractor = null; // removing the renderWindow is let to the superclass // because in the very special case of an AWT component diff --git a/vtk.rendering/src/vtk/rendering/vtkInteractorForwarder.java b/vtk.rendering/src/vtk/rendering/vtkInteractorForwarder.java index af8a6e36..2c3bc67a 100644 --- a/vtk.rendering/src/vtk/rendering/vtkInteractorForwarder.java +++ b/vtk.rendering/src/vtk/rendering/vtkInteractorForwarder.java @@ -312,11 +312,11 @@ public class vtkInteractorForwarder implements MouseListener, MouseMotionListene shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0; if (e.getWheelRotation() > 0) { component.getRenderWindowInteractor().SetEventInformationFlipY(lastX, lastY, ctrlPressed, shiftPressed, '0', 0, "0"); - component.getRenderWindowInteractor().MouseWheelBackwardEvent(); + component.getRenderWindowInteractor().MouseWheelForwardEvent(); } else if (e.getWheelRotation() < 0) { component.getRenderWindowInteractor().SetEventInformationFlipY(lastX, lastY, ctrlPressed, shiftPressed, '0', 0, "0"); - component.getRenderWindowInteractor().MouseWheelForwardEvent(); + component.getRenderWindowInteractor().MouseWheelBackwardEvent(); } } catch (InterruptedException interupt) { // Nothing to do -- 2.47.1