aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/com/jogamp/opengl/util/GLDrawableUtil.java
blob: cc81e48208a80dd3086f7cc1ae9d569d58327fe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
 * 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.opengl.util;

import java.util.ArrayList;
import java.util.List;

import javax.media.opengl.GLAnimatorControl;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLRunnable;

import jogamp.opengl.Debug;

/**
 * Providing utility functions dealing w/ {@link GLDrawable}s, {@link GLAutoDrawable} and their {@link GLEventListener}.
 */
public class GLDrawableUtil {
  protected static final boolean DEBUG = Debug.debug("GLDrawable");
  
  public static final boolean isAnimatorStartedOnOtherThread(GLAnimatorControl animatorCtrl) {
    return ( null != animatorCtrl ) ? animatorCtrl.isStarted() && animatorCtrl.getThread() != Thread.currentThread() : false ;
  }

  public static final boolean isAnimatorStarted(GLAnimatorControl animatorCtrl) {
    return ( null != animatorCtrl ) ? animatorCtrl.isStarted() : false ;
  }

  public static final boolean isAnimatorAnimatingOnOtherThread(GLAnimatorControl animatorCtrl) {
    return ( null != animatorCtrl ) ? animatorCtrl.isAnimating() && animatorCtrl.getThread() != Thread.currentThread() : false ;
  }
  
  public static final boolean isAnimatorAnimating(GLAnimatorControl animatorCtrl) {
    return ( null != animatorCtrl ) ? animatorCtrl.isAnimating() : false ;
  }
  
  /**
   * Moves the designated {@link GLEventListener} from {@link GLAutoDrawable} <code>src</code> to <code>dest</code>.
   * If <code>preserveInitState</code> is <code>true</code>, it's initialized state is preserved
   * and {@link GLEventListener#reshape(GLAutoDrawable, int, int, int, int) reshape(..)} issued w/ the next {@link GLAutoDrawable#display()} call.
   * <p>
   * Note that it is only legal to pass <code>preserveInitState := true</code>,
   * if the {@link GLContext} of both <code>src</code> and <code>dest</code> are shared, or has itself moved from <code>src</code> to <code>dest</code>.
   * </p>
   * <p>
   * Also note that the caller is encouraged to pause an attached {@link GLAnimatorControl}.
   * </p>
   * @param src
   * @param dest
   * @param listener
   * @param preserveInitState
   */
  public static final void moveGLEventListener(GLAutoDrawable src, GLAutoDrawable dest, GLEventListener listener, boolean preserveInitState) {
    final boolean initialized = src.getGLEventListenerInitState(listener);
    src.removeGLEventListener(listener);
    dest.addGLEventListener(listener);
    if(preserveInitState && initialized) {
        dest.setGLEventListenerInitState(listener, true);
        dest.invoke(false, new ReshapeGLEventListener(listener));
    } // else .. !init state is default
  }
  
  /**
   * Moves all {@link GLEventListener} from {@link GLAutoDrawable} <code>src</code> to <code>dest</code>.
   * If <code>preserveInitState</code> is <code>true</code>, it's initialized state is preserved
   * and {@link GLEventListener#reshape(GLAutoDrawable, int, int, int, int) reshape(..)} issued w/ the next {@link GLAutoDrawable#display()} call.
   * <p>
   * Note that it is only legal to pass <code>preserveInitState := true</code>,
   * if the {@link GLContext} of both <code>src</code> and <code>dest</code> are shared, or has itself moved from <code>src</code> to <code>dest</code>.
   * </p>
   * <p>
   * Also note that the caller is encouraged to pause an attached {@link GLAnimatorControl}.
   * </p>
   * @param src
   * @param dest
   * @param listener
   * @param preserveInitState
   */
  public static final void moveAllGLEventListener(GLAutoDrawable src, GLAutoDrawable dest, boolean preserveInitState) {
    for(int count = src.getGLEventListenerCount(); 0<count; count--) {
        final GLEventListener listener = src.getGLEventListener(0);
        moveGLEventListener(src, dest, listener, preserveInitState);
    }
  }

  /**
   * Swaps the {@link GLContext} and all {@link GLEventListener} between {@link GLAutoDrawable} <code>a</code> and <code>b</code>,
   * while preserving it's initialized state, resets the GL-Viewport and issuing {@link GLEventListener#reshape(GLAutoDrawable, int, int, int, int) reshape(..)}.
   * <p>
   * If an {@link GLAnimatorControl} is being attached to {@link GLAutoDrawable} <code>a</code> or <code>b</code> 
   * and the current thread is different than {@link GLAnimatorControl#getThread() the animator's thread}, it is paused during the operation.
   * </p>
   * @param a
   * @param b
   */
  public static final void swapGLContextAndAllGLEventListener(GLAutoDrawable a, GLAutoDrawable b) {
    final List<GLRunnable> aGLCmds = new ArrayList<GLRunnable>();
    final List<GLRunnable> bGLCmds = new ArrayList<GLRunnable>();
    final GLAnimatorControl aAnim = a.getAnimator();
    final GLAnimatorControl bAnim = b.getAnimator();    
    final boolean aIsPaused = isAnimatorAnimatingOnOtherThread(aAnim) && aAnim.pause();
    final boolean bIsPaused = isAnimatorAnimatingOnOtherThread(bAnim) && bAnim.pause();
    
    //
    // remove and cache all GLEventListener and their init-state
    //
    final int aSz = a.getGLEventListenerCount();
    final GLEventListener[] aGLE = new GLEventListener[aSz];
    final boolean[] aInit = new boolean[aSz];
    for(int i=0; i<aSz; i++) {
        final GLEventListener l = a.getGLEventListener(0);
        aInit[i] = a.getGLEventListenerInitState(l);
        aGLE[i] = a.removeGLEventListener( l );
    }    
    final int bSz = b.getGLEventListenerCount();
    final GLEventListener[] bGLE = new GLEventListener[bSz];
    final boolean[] bInit = new boolean[bSz];
    for(int i=0; i<bSz; i++) {
        final GLEventListener l = b.getGLEventListener(0);
        bInit[i] = b.getGLEventListenerInitState(l);
        bGLE[i] = b.removeGLEventListener( l );
    }
    
    //
    // trigger glFinish to sync GL ctx
    //
    a.invoke(true, glFinish);
    b.invoke(true, glFinish);
    
    //
    // switch context and
    // trigger GL-Viewport reset and reshape of all initialized GLEventListeners
    //
    b.setContext( a.setContext( b.getContext() ) );
    aGLCmds.add(setViewport);
    bGLCmds.add(setViewport);
    for(int i=0; i<aSz; i++) {
        if( aInit[i] ) {
            bGLCmds.add(new ReshapeGLEventListener(aGLE[i]));
        }
    }    
    for(int i=0; i<bSz; i++) {
        if( bInit[i] ) {
            aGLCmds.add(new ReshapeGLEventListener(bGLE[i]));
        }
    }
    aGLCmds.add(glFinish);
    bGLCmds.add(glFinish);
    a.invoke(true, aGLCmds);
    b.invoke(true, bGLCmds);
    
    // add all cached GLEventListener to their destination and fix their init-state
    for(int i=0; i<bSz; i++) {
        final GLEventListener l = bGLE[i];
        a.addGLEventListener( l );
        if( bInit[i] ) {
            a.setGLEventListenerInitState(l, true);
        } // else uninitialized is default after add
    }    
    for(int i=0; i<aSz; i++) {
        final GLEventListener l = aGLE[i];
        b.addGLEventListener( l );
        if( aInit[i] ) {
            b.setGLEventListenerInitState(l, true);
        } // else uninitialized is default after add
    }
    
    if(aIsPaused) { aAnim.resume(); }
    if(bIsPaused) { bAnim.resume(); }
  }
  
  static GLRunnable setViewport = new GLRunnable() {
    @Override
    public boolean run(GLAutoDrawable drawable) {
        drawable.getGL().glViewport(0, 0, drawable.getWidth(), drawable.getHeight());
        return true;
    }            
  };
  static GLRunnable glFinish = new GLRunnable() {
    @Override
    public boolean run(GLAutoDrawable drawable) {
        drawable.getGL().glFinish();
        return true;
    }            
  };
  
  private static class ReshapeGLEventListener implements GLRunnable {
    private GLEventListener listener;
    ReshapeGLEventListener(GLEventListener listener) {
        this.listener = listener;
    }
    @Override
    public boolean run(GLAutoDrawable drawable) {
        listener.reshape(drawable, 0, 0, drawable.getWidth(), drawable.getHeight());
        return true;
    }      
  }

  /** 
   * Swaps the {@link GLContext} of given {@link GLAutoDrawable} 
   * and {@link GLAutoDrawable#disposeGLEventListener(GLEventListener, boolean) disposes} 
   * each {@link GLEventListener} w/o removing it.
   * <p>
   * The GL-Viewport is reset and {@link GLEventListener#reshape(GLAutoDrawable, int, int, int, int) reshape(..)} issued implicit.
   * </p> 
   * <p>
   * If an {@link GLAnimatorControl} is being attached to GLAutoDrawable src or dest and the current thread is different 
   * than {@link GLAnimatorControl#getThread() the animator's thread}, it is paused during the operation.
   * </p>
   * @param src
   * @param dest
   */
  public static final void swapGLContext(GLAutoDrawable src, GLAutoDrawable dest) {    
    final GLAnimatorControl aAnim = src.getAnimator();
    final GLAnimatorControl bAnim = dest.getAnimator();    
    final boolean aIsPaused = isAnimatorAnimatingOnOtherThread(aAnim) && aAnim.pause();
    final boolean bIsPaused = isAnimatorAnimatingOnOtherThread(bAnim) && bAnim.pause();
    
    for(int i = src.getGLEventListenerCount() - 1; 0 <= i; i--) {
        src.disposeGLEventListener(src.getGLEventListener(i), false);
    }
    for(int i = dest.getGLEventListenerCount() - 1; 0 <= i; i--) {
        dest.disposeGLEventListener(dest.getGLEventListener(i), false);
    }
    dest.setContext( src.setContext( dest.getContext() ) );
    
    src.invoke(true, setViewport);
    dest.invoke(true, setViewport);
    
    if(aIsPaused) { aAnim.resume(); }
    if(bIsPaused) { bAnim.resume(); }
  }

}