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        int 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        int 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        }
87
88        int eglSurfaceId;
89        if (sur != null) {
90            eglSurfaceId = _eglCreateWindowSurface(display, config, sur, attrib_list);
91        } else if (native_window instanceof SurfaceTexture) {
92            eglSurfaceId = _eglCreateWindowSurfaceTexture(display, config,
93                    native_window, attrib_list);
94        } else {
95            throw new java.lang.UnsupportedOperationException(
96                "eglCreateWindowSurface() can only be called with an instance of " +
97                "SurfaceView, SurfaceHolder or SurfaceTexture at the moment, " +
98                "this will be fixed later.");
99        }
100
101        if (eglSurfaceId == 0) {
102            return EGL10.EGL_NO_SURFACE;
103        }
104        return new EGLSurfaceImpl( eglSurfaceId );
105    }
106
107    public synchronized EGLDisplay eglGetDisplay(Object native_display) {
108        int value = _eglGetDisplay(native_display);
109        if (value == 0) {
110            return EGL10.EGL_NO_DISPLAY;
111        }
112        if (mDisplay.mEGLDisplay != value)
113            mDisplay = new EGLDisplayImpl(value);
114        return mDisplay;
115    }
116
117    public synchronized EGLContext eglGetCurrentContext() {
118        int value = _eglGetCurrentContext();
119        if (value == 0) {
120            return EGL10.EGL_NO_CONTEXT;
121        }
122        if (mContext.mEGLContext != value)
123            mContext = new EGLContextImpl(value);
124        return mContext;
125    }
126
127    public synchronized EGLDisplay eglGetCurrentDisplay() {
128        int value = _eglGetCurrentDisplay();
129        if (value == 0) {
130            return EGL10.EGL_NO_DISPLAY;
131        }
132        if (mDisplay.mEGLDisplay != value)
133            mDisplay = new EGLDisplayImpl(value);
134        return mDisplay;
135    }
136
137    public synchronized EGLSurface eglGetCurrentSurface(int readdraw) {
138        int value = _eglGetCurrentSurface(readdraw);
139        if (value == 0) {
140            return EGL10.EGL_NO_SURFACE;
141        }
142        if (mSurface.mEGLSurface != value)
143            mSurface = new EGLSurfaceImpl(value);
144        return mSurface;
145    }
146
147    private native int _eglCreateContext(EGLDisplay display, EGLConfig config, EGLContext share_context, int[] attrib_list);
148    private native int _eglCreatePbufferSurface(EGLDisplay display, EGLConfig config, int[] attrib_list);
149    private native void _eglCreatePixmapSurface(EGLSurface sur, EGLDisplay display, EGLConfig config, Object native_pixmap, int[] attrib_list);
150    private native int _eglCreateWindowSurface(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list);
151    private native int _eglCreateWindowSurfaceTexture(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list);
152    private native int _eglGetDisplay(Object native_display);
153    private native int _eglGetCurrentContext();
154    private native int _eglGetCurrentDisplay();
155    private native int _eglGetCurrentSurface(int readdraw);
156
157    native private static void _nativeClassInit();
158    static { _nativeClassInit(); }
159}
160