From 7cb6cf2a9708d3f4e06f2215eb0d06b00fa6cd15 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Tue, 4 Dec 2012 07:10:59 +0100
Subject: SWT GLCanvas: Fix sporadic drop of redraw on X11 _and_ allow using
 custom GLCapabilities on X11 (feature complete)

To allow custom GLCapabilities, we had to use native parenting on X11 w/ a new child window.
The desired visualID chosen by the users GLCapabilities is passed to the new child window.

The redraw drops must be caused by the original GDK or the new child GDK window.
Now we use a plain X11 child window similar to NEWT's X11 window and NewtCanvasSWT,
which doesn't expose this bug.
(Note: SWTAccessor/GLCanvas still contains the uncommented GDK code path for further inspection, if desired)

Also added SWTNewtEventFactory to test event handling on the SWT GLCanvas w/ GearsES2.

TestSWTJOGLGLCanvas01GLn tests custom GLCapabilities now.

SWTEDTUtil has been moved to private: com.jogamp.newt.swt -> jogamp.newt.swt.
---
 .../classes/com/jogamp/newt/swt/NewtCanvasSWT.java |  82 +++---
 .../classes/com/jogamp/newt/swt/SWTEDTUtil.java    | 313 ---------------------
 2 files changed, 38 insertions(+), 357 deletions(-)
 delete mode 100644 src/newt/classes/com/jogamp/newt/swt/SWTEDTUtil.java

(limited to 'src/newt/classes/com')

diff --git a/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java b/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java
index 5af7afeb3..dbe7c0d98 100644
--- a/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java
+++ b/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java
@@ -26,7 +26,6 @@
  * or implied, of JogAmp Community.
  */
 
-
 package com.jogamp.newt.swt;
 
 import javax.media.nativewindow.AbstractGraphicsConfiguration;
@@ -44,20 +43,18 @@ import javax.media.nativewindow.WindowClosingProtocol;
 import javax.media.nativewindow.util.Insets;
 import javax.media.nativewindow.util.InsetsImmutable;
 import javax.media.nativewindow.util.Point;
+import javax.media.opengl.GLCapabilities;
 
 import jogamp.nativewindow.macosx.OSXUtil;
 import jogamp.newt.Debug;
+import jogamp.newt.swt.SWTEDTUtil;
 
 import org.eclipse.swt.SWT;
-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.PaintEvent;
-import org.eclipse.swt.events.PaintListener;
 import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.widgets.Canvas;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
 
 import com.jogamp.nativewindow.swt.SWTAccessor;
 import com.jogamp.newt.Display;
@@ -67,6 +64,9 @@ import com.jogamp.newt.util.EDTUtil;
 
 /**
  * SWT {@link Canvas} containing a NEWT {@link Window} using native parenting.
+ * <p>
+ * Implementation allows use of custom {@link GLCapabilities}.
+ * </p>
  */
 public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol {
     private static final boolean DEBUG = Debug.debug("Window");
@@ -126,54 +126,44 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol {
         clientArea = getClientArea();
 
         final AbstractGraphicsDevice device = SWTAccessor.getDevice(this);
-        screen = SWTAccessor.getScreen(device, 0);
+        screen = SWTAccessor.getScreen(device, -1 /* default */);
         nativeWindow = null;
         
         if(null != child) {
             setNEWTChild(child);
         }
-        
-        /* Register SWT listeners (e.g. PaintListener) to render/resize GL surface. */
-        /* TODO: verify that these do not need to be manually de-registered when destroying the SWT component */
-        addPaintListener(new PaintListener() {
+
+        final Listener listener = new Listener () {
             @Override
-            public void paintControl(final PaintEvent arg0) {
-                if( null != nativeWindow || validateNative() ) {
-                    if( newtChildReady ) {
-                        if( postSetSize ) {
-                            newtChild.setSize(clientArea.width, clientArea.height);
-                            postSetSize = false;
+            public void handleEvent (Event event) {
+                switch (event.type) {
+                case SWT.Paint:
+                    if( null != nativeWindow || validateNative() ) {
+                        if( newtChildReady ) {
+                            if( postSetSize ) {
+                                newtChild.setSize(clientArea.width, clientArea.height);
+                                postSetSize = false;
+                            }
+                            newtChild.windowRepaint(0, 0, clientArea.width, clientArea.height);
                         }
-                        newtChild.windowRepaint(0, 0, clientArea.width, clientArea.height);
                     }
+                    break;
+                case SWT.Resize:
+                    updateSizeCheck();
+                    break;
+                case SWT.Dispose:
+                    NewtCanvasSWT.this.dispose();
+                    break;
                 }
             }
-        });
-
-        addControlListener(new ControlListener() {
-            @Override
-            public void controlMoved(ControlEvent e) {
-            }
-            @Override
-            public void controlResized(final ControlEvent arg0) {
-                updateSizeCheck();
-            }
-        });
-        
-        addDisposeListener(new DisposeListener() {
-            @Override
-            public void widgetDisposed(DisposeEvent e) {
-               NewtCanvasSWT.this.dispose();
-            }
-        });
-        
+        };
+        addListener (SWT.Resize, listener);
+        addListener (SWT.Paint, listener);
+        addListener (SWT.Dispose, listener);
     }
     
     /** assumes nativeWindow == null ! */
     protected final boolean validateNative() {
-        if( isDisposed() || !isVisible() ) {
-            return false;
-        }
         updateSizeCheck();
         final Rectangle nClientArea = clientArea;
         if(0 >= nClientArea.width || 0 >= nClientArea.height) {        
@@ -216,6 +206,10 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol {
              ( nClientArea.width != oClientArea.width || nClientArea.height != oClientArea.height )
            ) {
             clientArea = nClientArea; // write back new value
+            if(DEBUG) {
+                final long nsh = newtChildReady ? newtChild.getSurfaceHandle() : 0;
+                System.err.println("NewtCanvasSWT.sizeChanged: ("+Thread.currentThread().getName()+"): newtChildReady "+newtChildReady+", "+nClientArea.x+"/"+nClientArea.y+" "+nClientArea.width+"x"+nClientArea.height+" - surfaceHandle 0x"+Long.toHexString(nsh));
+            }
             if( newtChildReady ) {
                 newtChild.setSize(clientArea.width, clientArea.height);
             } else {
@@ -223,7 +217,7 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol {
             }
         }
     }
-
+    
     @Override
     public void update() {
         // don't paint background etc .. nop avoids flickering
@@ -261,11 +255,11 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol {
     public NativeWindow getNativeWindow() { return nativeWindow; }
     
     public WindowClosingMode getDefaultCloseOperation() {
-        return newtChildCloseOp; // FIXME
+        return newtChildCloseOp; // TODO: implement ?!
     }
 
     public WindowClosingMode setDefaultCloseOperation(WindowClosingMode op) {
-        return newtChildCloseOp = op; // FIXME
+        return newtChildCloseOp = op; // TODO: implement ?!
     }
 
 
diff --git a/src/newt/classes/com/jogamp/newt/swt/SWTEDTUtil.java b/src/newt/classes/com/jogamp/newt/swt/SWTEDTUtil.java
deleted file mode 100644
index 1c20fe524..000000000
--- a/src/newt/classes/com/jogamp/newt/swt/SWTEDTUtil.java
+++ /dev/null
@@ -1,313 +0,0 @@
-/**
- * Copyright 2012 JogAmp Community. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification, are
- * permitted provided that the following conditions are met:
- *
- *    1. Redistributions of source code must retain the above copyright notice, this list of
- *       conditions and the following disclaimer.
- *
- *    2. Redistributions in binary form must reproduce the above copyright notice, this list
- *       of conditions and the following disclaimer in the documentation and/or other materials
- *       provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * The views and conclusions contained in the software and documentation are those of the
- * authors and should not be interpreted as representing official policies, either expressed
- * or implied, of JogAmp Community.
- */
-package com.jogamp.newt.swt;
-
-import javax.media.nativewindow.NativeWindowException;
-
-import jogamp.newt.Debug;
-
-import com.jogamp.common.util.RunnableTask;
-import com.jogamp.newt.util.EDTUtil;
-
-/**
- * Simple {@link EDTUtil} implementation utilizing the SWT UI thread
- * of the given {@link Display}.
- */
-public class SWTEDTUtil implements EDTUtil {
-    public static final boolean DEBUG = Debug.debug("EDT");
-        
-    private final Object edtLock = new Object(); // locking the EDT start/stop state
-    private final ThreadGroup threadGroup; 
-    private final String name;
-    private final Runnable dispatchMessages;
-    private final org.eclipse.swt.widgets.Display swtDisplay;
-    private NewtEventDispatchThread nedt = null;
-    private int start_iter=0;
-    private static long pollPeriod = EDTUtil.defaultEDTPollPeriod;
-    
-    public SWTEDTUtil(final com.jogamp.newt.Display newtDisplay, org.eclipse.swt.widgets.Display swtDisplay) {
-        this.threadGroup = Thread.currentThread().getThreadGroup();
-        this.name=Thread.currentThread().getName()+"-SWTDisplay-"+newtDisplay.getFQName()+"-EDT-";
-        this.dispatchMessages = new Runnable() {
-            public void run() {
-                ((jogamp.newt.DisplayImpl) newtDisplay).dispatchMessages();
-            } };
-        this.swtDisplay = swtDisplay;
-        this.nedt = new NewtEventDispatchThread(threadGroup, name);
-        this.nedt.setDaemon(true); // don't stop JVM from shutdown ..
-    }
-    
-    public final org.eclipse.swt.widgets.Display getDisplay() {
-        return swtDisplay;
-    }
-    
-    @Override
-    public long getPollPeriod() {
-        return pollPeriod;
-    }
-
-    @Override
-    public void setPollPeriod(long ms) {
-        pollPeriod = ms;
-    }
-
-    @Override
-    public void reset() {
-        synchronized(edtLock) { 
-            waitUntilStopped();
-            if(DEBUG) {
-                System.err.println(Thread.currentThread()+": SWT-EDT reset - edt: "+nedt);
-            }
-            this.nedt = new NewtEventDispatchThread(threadGroup, name);
-            this.nedt.setDaemon(true); // don't stop JVM from shutdown ..
-        }
-    }
-
-    private final void startImpl() {
-        if(nedt.isAlive()) {
-            throw new RuntimeException("SWT-EDT Thread.isAlive(): true, isRunning: "+nedt.isRunning()+", edt: "+nedt);
-        }
-        start_iter++;
-        nedt.setName(name+start_iter);
-        nedt.shouldStop = false;
-        if(DEBUG) {
-            System.err.println(Thread.currentThread()+": SWT-EDT START - edt: "+nedt);
-            // Thread.dumpStack();
-        }
-        nedt.start();
-    }
-    
-    @Override
-    public boolean isCurrentThreadEDT() {
-        return swtDisplay.getThread() == Thread.currentThread();
-    }
-
-    @Override
-    public final boolean isCurrentThreadNEDT() {
-        return nedt == Thread.currentThread();
-    }
-    
-    @Override
-    public final boolean isCurrentThreadEDTorNEDT() {
-        final Thread ct = Thread.currentThread();
-        return ct == swtDisplay.getThread() || ct == nedt ;
-    }
-    
-    @Override
-    public boolean isRunning() {
-        return nedt.isRunning() ; // SWT is always running
-    }
-    
-    @Override
-    public final void invokeStop(Runnable task) {
-        invokeImpl(true, task, true);
-    }
-
-    @Override
-    public final void invoke(boolean wait, Runnable task) {
-        invokeImpl(wait, task, false);
-    }
-    
-    private void invokeImpl(boolean wait, Runnable task, boolean stop) {
-        Throwable throwable = null;
-        RunnableTask rTask = null;
-        Object rTaskLock = new Object();
-        synchronized(rTaskLock) { // lock the optional task execution
-            synchronized(edtLock) { // lock the EDT status
-                if( nedt.shouldStop ) {
-                    // drop task ..
-                    if(DEBUG) {
-                        System.err.println(Thread.currentThread()+": Warning: SWT-EDT about (1) to stop, won't enqueue new task: "+nedt);
-                        Thread.dumpStack();
-                    }
-                    return; 
-                }
-                // System.err.println(Thread.currentThread()+" XXX stop: "+stop+", tasks: "+edt.tasks.size()+", task: "+task);
-                // Thread.dumpStack();
-                if(stop) {
-                    synchronized(nedt.sync) {
-                        nedt.shouldStop = true;
-                        nedt.sync.notifyAll(); // stop immediate if waiting (poll freq)
-                    }
-                    if(DEBUG) {
-                        System.err.println(Thread.currentThread()+": SWT-EDT signal STOP (on edt: "+isCurrentThreadEDT()+") - "+nedt);
-                        // Thread.dumpStack();
-                    }
-                } else if( !nedt.isRunning() ) {
-                    // start if should not stop && not started yet
-                    startImpl();
-                }
-                if( null == task ) {
-                    wait = false;
-                } else if( isCurrentThreadEDT() ) {
-                    task.run();
-                    wait = false; // running in same thread (EDT) -> no wait
-                } else if( swtDisplay.isDisposed() ) {
-                    wait = false; // drop task, SWT disposed 
-                } else {
-                    rTask = new RunnableTask(task,
-                                             wait ? rTaskLock : null,
-                                             true /* always catch and report Exceptions, don't disturb EDT */);
-                    swtDisplay.asyncExec(rTask);
-                }
-            }
-            if( wait ) {
-                try {
-                    rTaskLock.wait(); // free lock, allow execution of rTask
-                } catch (InterruptedException ie) {
-                    throwable = ie;
-                }
-                if(null==throwable) {
-                    throwable = rTask.getThrowable();
-                }
-                if(null!=throwable) {
-                    if(throwable instanceof NativeWindowException) {
-                        throw (NativeWindowException)throwable;
-                    }
-                    throw new RuntimeException(throwable);
-                }
-            }
-        }
-    }    
-
-    @Override
-    final public void waitUntilIdle() {
-        final NewtEventDispatchThread _nedt;
-        synchronized(edtLock) {
-            _nedt = nedt;
-        }
-        final Thread ct = Thread.currentThread();
-        if(!_nedt.isRunning() || _nedt == ct || swtDisplay.getThread() == ct) {
-            return;
-        }
-        try {
-            swtDisplay.syncExec(new Runnable() {
-                public void run() { }
-            });
-        } catch (Exception e) { }
-    }
-
-    @Override
-    final public void waitUntilStopped() {
-        synchronized(edtLock) {
-            final Thread ct = Thread.currentThread();
-            if(nedt.isRunning() && nedt != ct && swtDisplay.getThread() != ct) {
-                while(nedt.isRunning()) {
-                    try {
-                        edtLock.wait();
-                    } catch (InterruptedException e) {
-                        e.printStackTrace();
-                    }
-                }
-            }
-        }
-    }
-    
-    class NewtEventDispatchThread extends Thread {
-        volatile boolean shouldStop = false;
-        volatile boolean isRunning = false;
-        Object sync = new Object();
-
-        public NewtEventDispatchThread(ThreadGroup tg, String name) {
-            super(tg, name);
-        }
-
-        final public boolean isRunning() {
-            return isRunning;
-        }
-
-        @Override
-        final public void start() throws IllegalThreadStateException {
-            isRunning = true;
-            super.start();
-        }
-
-        /** 
-         * Utilizing locking only on tasks and its execution,
-         * not for event dispatching.
-         */
-        @Override
-        final public void run() {
-            if(DEBUG) {
-                System.err.println(getName()+": SWT-EDT run() START "+ getName());
-            }
-            RuntimeException error = null;
-            try {
-                do {
-                    // event dispatch
-                    if(!shouldStop) {
-                        // EDT invoke thread is SWT-EDT,
-                        // hence dispatching is required to run on SWT-EDT as well.
-                        // Otherwise a deadlock may happen due to dispatched event's
-                        // triggering a locking action.                        
-                        if ( !swtDisplay.isDisposed() ) {
-                            swtDisplay.syncExec(dispatchMessages);
-                        } else {
-                            dispatchMessages.run();
-                        }
-                    }
-                    // wait
-                    synchronized(sync) {
-                        if(!shouldStop) {
-                            try {
-                                sync.wait(pollPeriod);
-                            } catch (InterruptedException e) {
-                                e.printStackTrace();
-                            }
-                        }
-                    }
-                } while(!shouldStop) ;
-            } catch (Throwable t) {
-                // handle errors ..
-                shouldStop = true;
-                if(t instanceof RuntimeException) {
-                    error = (RuntimeException) t;
-                } else {
-                    error = new RuntimeException("Within SWT-EDT", t);
-                }
-            } finally {
-                if(DEBUG) {
-                    System.err.println(getName()+": SWT-EDT run() END "+ getName()+", "+error); 
-                }
-                synchronized(edtLock) {
-                    isRunning = !shouldStop;
-                    if(!isRunning) {
-                        edtLock.notifyAll();
-                    }
-                }
-                if(DEBUG) {
-                    System.err.println(getName()+": SWT-EDT run() EXIT "+ getName()+", exception: "+error);
-                }
-                if(null!=error) {
-                    throw error;
-                }
-            } // finally
-        } // run()
-    } // EventDispatchThread
-    
-}
-- 
cgit v1.2.3