aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/util/av/EGLMediaPlayerImpl.java
blob: ec375406d3304836b215892357ab76dadbc74a25 (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
/**
 * 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 jogamp.opengl.util.av;

import java.nio.Buffer;
import java.nio.IntBuffer;

import javax.media.opengl.GL;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureSequence;

import jogamp.opengl.egl.EGL;
import jogamp.opengl.egl.EGLContext;
import jogamp.opengl.egl.EGLDrawable;
import jogamp.opengl.egl.EGLExt;

public abstract class EGLMediaPlayerImpl extends GLMediaPlayerImpl {
    final protected TextureType texType;
    final protected boolean useKHRSync;
    
    public enum TextureType {
        GL(0), KHRImage(1); 
        
        public final int id;

        TextureType(int id){
            this.id = id;
        }
    }    
    
    public static class EGLTextureFrame extends TextureSequence.TextureFrame {
        
        public EGLTextureFrame(Buffer clientBuffer, Texture t, long khrImage, long khrSync) {
            super(t);
            this.clientBuffer = clientBuffer;
            this.image = khrImage;
            this.sync = khrSync;
        }
        
        public final Buffer getClientBuffer() { return clientBuffer; }
        public final long getImage() { return image; }        
        public final long getSync() { return sync; }
        
        public String toString() {
            return "EGLTextureFrame[pts " + pts + " ms, l " + duration + " ms, texID "+ texture.getTextureObject() + ", img "+ image + ", sync "+ sync+", clientBuffer "+clientBuffer+"]";
        }
        protected final Buffer clientBuffer;
        protected final long image;
        protected final long sync;
    }

    
    protected EGLMediaPlayerImpl(TextureType texType, boolean useKHRSync) {
        super();
        this.texType = texType;
        this.useKHRSync = useKHRSync;
    }

    @Override
    protected TextureSequence.TextureFrame createTexImage(GL gl, int texName) {
        final Texture texture = super.createTexImageImpl(gl, texName, width, height, false);
        final Buffer clientBuffer;
        final long image;
        final long sync;
        final boolean eglUsage = TextureType.KHRImage == texType || useKHRSync ; 
        final EGLContext eglCtx;
        final EGLExt eglExt;
        final EGLDrawable eglDrawable;
        
        if(eglUsage) {
            eglCtx = (EGLContext) gl.getContext();
            eglExt = eglCtx.getEGLExt();
            eglDrawable = (EGLDrawable) eglCtx.getGLDrawable();            
        } else {
            eglCtx = null;
            eglExt = null;
            eglDrawable = null;
        }
        
        if(TextureType.KHRImage == texType) {
            IntBuffer nioTmp = Buffers.newDirectIntBuffer(1);
            // create EGLImage from texture
            clientBuffer = null; // FIXME
            nioTmp.put(0, EGL.EGL_NONE);
            image =  eglExt.eglCreateImageKHR( eglDrawable.getNativeSurface().getDisplayHandle(), eglCtx.getHandle(),
                                               EGLExt.EGL_GL_TEXTURE_2D_KHR,
                                               clientBuffer, nioTmp);
            if (0==image) {
                throw new RuntimeException("EGLImage creation failed: "+EGL.eglGetError()+", ctx "+eglCtx+", tex "+texName+", err "+toHexString(EGL.eglGetError()));
            }
        } else {
            clientBuffer = null;
            image = 0;
        }

        if(useKHRSync) {
            IntBuffer tmp = Buffers.newDirectIntBuffer(1);
            // Create sync object so that we can be sure that gl has finished
            // rendering the EGLImage texture before we tell OpenMAX to fill
            // it with a new frame.
            tmp.put(0, EGL.EGL_NONE);
            sync = eglExt.eglCreateSyncKHR(eglDrawable.getNativeSurface().getDisplayHandle(), EGLExt.EGL_SYNC_FENCE_KHR, tmp);
            if (0==sync) {
                throw new RuntimeException("EGLSync creation failed: "+EGL.eglGetError()+", ctx "+eglCtx+", err "+toHexString(EGL.eglGetError()));
            }
        } else {
            sync = 0;
        }
        return new EGLTextureFrame(clientBuffer, texture, image, sync);
    }
    
    @Override
    protected void destroyTexFrame(GL gl, TextureSequence.TextureFrame frame) {
        final boolean eglUsage = TextureType.KHRImage == texType || useKHRSync ; 
        final EGLContext eglCtx;
        final EGLExt eglExt;
        final EGLDrawable eglDrawable;
        
        if(eglUsage) {
            eglCtx = (EGLContext) gl.getContext();
            eglExt = eglCtx.getEGLExt();
            eglDrawable = (EGLDrawable) eglCtx.getGLDrawable();            
        } else {
            eglCtx = null;
            eglExt = null;
            eglDrawable = null;
        }
        final EGLTextureFrame eglTex = (EGLTextureFrame) frame;
        
        if(0!=eglTex.getImage()) {
            eglExt.eglDestroyImageKHR(eglDrawable.getNativeSurface().getDisplayHandle(), eglTex.getImage());
        }
        if(0!=eglTex.getSync()) {
            eglExt.eglDestroySyncKHR(eglDrawable.getNativeSurface().getDisplayHandle(), eglTex.getSync());
        }
        super.destroyTexFrame(gl, frame);
    }
}