1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.android.gles_jni;
18
19import javax.microedition.khronos.egl.*;
20
21import android.graphics.SurfaceTexture;
22import android.view.Surface;
23import android.view.SurfaceView;
24import android.view.SurfaceHolder;
25
26public class EGLImpl implements EGL10 {
27    private EGLContextImpl mContext = new EGLContextImpl(-1);
28    private EGLDisplayImpl mDisplay = new EGLDisplayImpl(-1);
29    private EGLSurfaceImpl mSurface = new EGLSurfaceImpl(-1);
30
31    public native boolean     eglInitialize(EGLDisplay display, int[] major_minor);
32    public native boolean     eglQueryContext(EGLDisplay display, EGLContext context, int attribute, int[] value);
33    public native boolean     eglQuerySurface(EGLDisplay display, EGLSurface surface, int attribute, int[] value);
34    /** @hide **/
35    public native boolean     eglReleaseThread();
36    public native boolean     eglChooseConfig(EGLDisplay display, int[] attrib_list, EGLConfig[] configs, int config_size, int[] num_config);
37    public native boolean     eglGetConfigAttrib(EGLDisplay display, EGLConfig config, int attribute, int[] value);
38    public native boolean     eglGetConfigs(EGLDisplay display, EGLConfig[] configs, int config_size, int[] num_config);
39    public native int         eglGetError();
40    public native boolean     eglDestroyContext(EGLDisplay display, EGLContext context);
41    public native boolean     eglDestroySurface(EGLDisplay display, EGLSurface surface);
42    public native boolean     eglMakeCurrent(EGLDisplay display, EGLSurface draw, EGLSurface read, EGLContext context);
43    public native String      eglQueryString(EGLDisplay display, int name);
44    public native boolean     eglSwapBuffers(EGLDisplay display, EGLSurface surface);
45    public native boolean     eglTerminate(EGLDisplay display);
46    public native boolean     eglCopyBuffers(EGLDisplay display, EGLSurface surface, Object native_pixmap);
47    public native boolean     eglWaitGL();
48    public native boolean     eglWaitNative(int engine, Object bindTarget);
49
50    /** @hide **/
51    public static native int  getInitCount(EGLDisplay display);
52
53    public EGLContext eglCreateContext(EGLDisplay display, EGLConfig config, EGLContext share_context, int[] attrib_list) {
54        long eglContextId = _eglCreateContext(display, config, share_context, attrib_list);
55        if (eglContextId == 0) {
56            return EGL10.EGL_NO_CONTEXT;
57        }
58        return new EGLContextImpl( eglContextId );
59    }
60
61    public EGLSurface eglCreatePbufferSurface(EGLDisplay display, EGLConfig config, int[] attrib_list) {
62        long eglSurfaceId = _eglCreatePbufferSurface(display, config, attrib_list);
63        if (eglSurfaceId == 0) {
64            return EGL10.EGL_NO_SURFACE;
65        }
66        return new EGLSurfaceImpl( eglSurfaceId );
67    }
68
69    public EGLSurface eglCreatePixmapSurface(EGLDisplay display, EGLConfig config, Object native_pixmap, int[] attrib_list) {
70        EGLSurfaceImpl sur = new EGLSurfaceImpl();
71        _eglCreatePixmapSurface(sur, display, config, native_pixmap, attrib_list);
72        if (sur.mEGLSurface == 0) {
73            return EGL10.EGL_NO_SURFACE;
74        }
75        return sur;
76    }
77
78    public EGLSurface eglCreateWindowSurface(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list) {
79        Surface sur = null;
80        if (native_window instanceof SurfaceView) {
81            SurfaceView surfaceView = (SurfaceView)native_window;
82            sur = surfaceView.getHolder().getSurface();
83        } else if (native_window instanceof SurfaceHolder) {
84            SurfaceHolder holder = (SurfaceHolder)native_window;
85            sur = holder.getSurface();
86        } else if (native_window instanceof Surface) {
87            sur = (Surface) native_window;
88        }
89
90        long eglSurfaceId;
91        if (sur != null) {
92            eglSurfaceId = _eglCreateWindowSurface(display, config, sur, attrib_list);
93        } else if (native_window instanceof SurfaceTexture) {
94            eglSurfaceId = _eglCreateWindowSurfaceTexture(display, config,
95                    native_window, attrib_list);
96        } else {
97            throw new java.lang.UnsupportedOperationException(
98                "eglCreateWindowSurface() can only be called with an instance of " +
99                "Surface, SurfaceView, SurfaceHolder or SurfaceTexture at the moment.");
100        }
101
102        if (eglSurfaceId == 0) {
103            return EGL10.EGL_NO_SURFACE;
104        }
105        return new EGLSurfaceImpl( eglSurfaceId );
106    }
107
108    public synchronized EGLDisplay eglGetDisplay(Object native_display) {
109        long value = _eglGetDisplay(native_display);
110        if (value == 0) {
111            return EGL10.EGL_NO_DISPLAY;
112        }
113        if (mDisplay.mEGLDisplay != value)
114            mDisplay = new EGLDisplayImpl(value);
115        return mDisplay;
116    }
117
118    public synchronized EGLContext eglGetCurrentContext() {
119        long value = _eglGetCurrentContext();
120        if (value == 0) {
121            return EGL10.EGL_NO_CONTEXT;
122        }
123        if (mContext.mEGLContext != value)
124            mContext = new EGLContextImpl(value);
125        return mContext;
126    }
127
128    public synchronized EGLDisplay eglGetCurrentDisplay() {
129        long value = _eglGetCurrentDisplay();
130        if (value == 0) {
131            return EGL10.EGL_NO_DISPLAY;
132        }
133        if (mDisplay.mEGLDisplay != value)
134            mDisplay = new EGLDisplayImpl(value);
135        return mDisplay;
136    }
137
138    public synchronized EGLSurface eglGetCurrentSurface(int readdraw) {
139        long value = _eglGetCurrentSurface(readdraw);
140        if (value == 0) {
141            return EGL10.EGL_NO_SURFACE;
142        }
143        if (mSurface.mEGLSurface != value)
144            mSurface = new EGLSurfaceImpl(value);
145        return mSurface;
146    }
147
148    private native long _eglCreateContext(EGLDisplay display, EGLConfig config, EGLContext share_context, int[] attrib_list);
149    private native long _eglCreatePbufferSurface(EGLDisplay display, EGLConfig config, int[] attrib_list);
150    private native void _eglCreatePixmapSurface(EGLSurface sur, EGLDisplay display, EGLConfig config, Object native_pixmap, int[] attrib_list);
151    private native long _eglCreateWindowSurface(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list);
152    private native long _eglCreateWindowSurfaceTexture(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list);
153    private native long _eglGetDisplay(Object native_display);
154    private native long _eglGetCurrentContext();
155    private native long _eglGetCurrentDisplay();
156    private native long _eglGetCurrentSurface(int readdraw);
157
158    native private static void _nativeClassInit();
159    static { _nativeClassInit(); }
160}
161