EGLImpl.java revision 8f0095cd33558e9cc8a440047908e53b68906f5f
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    public native boolean     eglChooseConfig(EGLDisplay display, int[] attrib_list, EGLConfig[] configs, int config_size, int[] num_config);
35    public native boolean     eglGetConfigAttrib(EGLDisplay display, EGLConfig config, int attribute, int[] value);
36    public native boolean     eglGetConfigs(EGLDisplay display, EGLConfig[] configs, int config_size, int[] num_config);
37    public native int         eglGetError();
38    public native boolean     eglDestroyContext(EGLDisplay display, EGLContext context);
39    public native boolean     eglDestroySurface(EGLDisplay display, EGLSurface surface);
40    public native boolean     eglMakeCurrent(EGLDisplay display, EGLSurface draw, EGLSurface read, EGLContext context);
41    public native String      eglQueryString(EGLDisplay display, int name);
42    public native boolean     eglSwapBuffers(EGLDisplay display, EGLSurface surface);
43    public native boolean     eglTerminate(EGLDisplay display);
44    public native boolean     eglCopyBuffers(EGLDisplay display, EGLSurface surface, Object native_pixmap);
45    public native boolean     eglWaitGL();
46    public native boolean     eglWaitNative(int engine, Object bindTarget);
47
48    public EGLContext eglCreateContext(EGLDisplay display, EGLConfig config, EGLContext share_context, int[] attrib_list) {
49        int eglContextId = _eglCreateContext(display, config, share_context, attrib_list);
50        if (eglContextId == 0) {
51            return EGL10.EGL_NO_CONTEXT;
52        }
53        return new EGLContextImpl( eglContextId );
54    }
55
56    public EGLSurface eglCreatePbufferSurface(EGLDisplay display, EGLConfig config, int[] attrib_list) {
57        int eglSurfaceId = _eglCreatePbufferSurface(display, config, attrib_list);
58        if (eglSurfaceId == 0) {
59            return EGL10.EGL_NO_SURFACE;
60        }
61        return new EGLSurfaceImpl( eglSurfaceId );
62    }
63
64    public EGLSurface eglCreatePixmapSurface(EGLDisplay display, EGLConfig config, Object native_pixmap, int[] attrib_list) {
65        EGLSurfaceImpl sur = new EGLSurfaceImpl();
66        _eglCreatePixmapSurface(sur, display, config, native_pixmap, attrib_list);
67        if (sur.mEGLSurface == 0) {
68            return EGL10.EGL_NO_SURFACE;
69        }
70        return sur;
71    }
72
73    public EGLSurface eglCreateWindowSurface(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list) {
74        Surface sur = null;
75        if (native_window instanceof SurfaceView) {
76            SurfaceView surfaceView = (SurfaceView)native_window;
77            sur = surfaceView.getHolder().getSurface();
78        } else if (native_window instanceof SurfaceHolder) {
79            SurfaceHolder holder = (SurfaceHolder)native_window;
80            sur = holder.getSurface();
81        }
82
83        int eglSurfaceId;
84        if (sur != null) {
85            eglSurfaceId = _eglCreateWindowSurface(display, config, sur, attrib_list);
86        } else if (native_window instanceof SurfaceTexture) {
87            eglSurfaceId = _eglCreateWindowSurfaceTexture(display, config,
88                    ((SurfaceTexture) native_window).mSurfaceTexture, attrib_list);
89        } else {
90            throw new java.lang.UnsupportedOperationException(
91                "eglCreateWindowSurface() can only be called with an instance of " +
92                "SurfaceView, SurfaceHolder or SurfaceTexture at the moment, " +
93                "this will be fixed later.");
94        }
95
96        if (eglSurfaceId == 0) {
97            return EGL10.EGL_NO_SURFACE;
98        }
99        return new EGLSurfaceImpl( eglSurfaceId );
100    }
101
102    public synchronized EGLDisplay eglGetDisplay(Object native_display) {
103        int value = _eglGetDisplay(native_display);
104        if (value == 0) {
105            return EGL10.EGL_NO_DISPLAY;
106        }
107        if (mDisplay.mEGLDisplay != value)
108            mDisplay = new EGLDisplayImpl(value);
109        return mDisplay;
110    }
111
112    public synchronized EGLContext eglGetCurrentContext() {
113        int value = _eglGetCurrentContext();
114        if (value == 0) {
115            return EGL10.EGL_NO_CONTEXT;
116        }
117        if (mContext.mEGLContext != value)
118            mContext = new EGLContextImpl(value);
119        return mContext;
120    }
121
122    public synchronized EGLDisplay eglGetCurrentDisplay() {
123        int value = _eglGetCurrentDisplay();
124        if (value == 0) {
125            return EGL10.EGL_NO_DISPLAY;
126        }
127        if (mDisplay.mEGLDisplay != value)
128            mDisplay = new EGLDisplayImpl(value);
129        return mDisplay;
130    }
131
132    public synchronized EGLSurface eglGetCurrentSurface(int readdraw) {
133        int value = _eglGetCurrentSurface(readdraw);
134        if (value == 0) {
135            return EGL10.EGL_NO_SURFACE;
136        }
137        if (mSurface.mEGLSurface != value)
138            mSurface = new EGLSurfaceImpl(value);
139        return mSurface;
140    }
141
142    private native int _eglCreateContext(EGLDisplay display, EGLConfig config, EGLContext share_context, int[] attrib_list);
143    private native int _eglCreatePbufferSurface(EGLDisplay display, EGLConfig config, int[] attrib_list);
144    private native void _eglCreatePixmapSurface(EGLSurface sur, EGLDisplay display, EGLConfig config, Object native_pixmap, int[] attrib_list);
145    private native int _eglCreateWindowSurface(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list);
146    private native int _eglCreateWindowSurfaceTexture(EGLDisplay display, EGLConfig config, int native_window, int[] attrib_list);
147    private native int _eglGetDisplay(Object native_display);
148    private native int _eglGetCurrentContext();
149    private native int _eglGetCurrentDisplay();
150    private native int _eglGetCurrentSurface(int readdraw);
151
152    native private static void _nativeClassInit();
153    static { _nativeClassInit(); }
154}
155