2012-01-31 21:35:52 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011-2012
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Patrick Walton <pcwalton@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.SurfaceHolder;
|
|
|
|
import android.view.SurfaceView;
|
|
|
|
import javax.microedition.khronos.egl.EGL10;
|
|
|
|
import javax.microedition.khronos.egl.EGL11;
|
|
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
|
|
|
import javax.microedition.khronos.egl.EGLContext;
|
|
|
|
import javax.microedition.khronos.egl.EGLDisplay;
|
|
|
|
import javax.microedition.khronos.egl.EGLSurface;
|
|
|
|
import javax.microedition.khronos.opengles.GL;
|
|
|
|
import javax.microedition.khronos.opengles.GL10;
|
|
|
|
|
|
|
|
public class GLController {
|
|
|
|
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
|
|
|
|
private static final String LOGTAG = "GeckoGLController";
|
|
|
|
|
|
|
|
private FlexibleGLSurfaceView mView;
|
|
|
|
private int mGLVersion;
|
2012-04-02 12:43:21 -07:00
|
|
|
private boolean mSurfaceValid;
|
2012-01-31 21:35:52 -08:00
|
|
|
private int mWidth, mHeight;
|
|
|
|
|
|
|
|
private EGL10 mEGL;
|
|
|
|
private EGLDisplay mEGLDisplay;
|
|
|
|
private EGLConfig mEGLConfig;
|
|
|
|
private EGLContext mEGLContext;
|
|
|
|
private EGLSurface mEGLSurface;
|
|
|
|
|
|
|
|
private GL mGL;
|
|
|
|
|
2012-02-02 10:57:38 -08:00
|
|
|
private static final int LOCAL_EGL_OPENGL_ES2_BIT = 4;
|
|
|
|
|
2012-01-31 21:35:52 -08:00
|
|
|
private static final int[] CONFIG_SPEC = {
|
|
|
|
EGL10.EGL_RED_SIZE, 5,
|
|
|
|
EGL10.EGL_GREEN_SIZE, 6,
|
|
|
|
EGL10.EGL_BLUE_SIZE, 5,
|
2012-02-02 10:57:38 -08:00
|
|
|
EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT,
|
|
|
|
EGL10.EGL_RENDERABLE_TYPE, LOCAL_EGL_OPENGL_ES2_BIT,
|
2012-01-31 21:35:52 -08:00
|
|
|
EGL10.EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
public GLController(FlexibleGLSurfaceView view) {
|
|
|
|
mView = view;
|
2012-02-02 10:57:38 -08:00
|
|
|
mGLVersion = 2;
|
2012-04-02 12:43:21 -07:00
|
|
|
mSurfaceValid = false;
|
2012-01-31 21:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setGLVersion(int version) {
|
|
|
|
mGLVersion = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GL getGL() { return mEGLContext.getGL(); }
|
|
|
|
public EGLDisplay getEGLDisplay() { return mEGLDisplay; }
|
|
|
|
public EGLConfig getEGLConfig() { return mEGLConfig; }
|
|
|
|
public EGLContext getEGLContext() { return mEGLContext; }
|
|
|
|
public EGLSurface getEGLSurface() { return mEGLSurface; }
|
|
|
|
public FlexibleGLSurfaceView getView() { return mView; }
|
|
|
|
|
|
|
|
public boolean hasSurface() {
|
|
|
|
return mEGLSurface != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean swapBuffers() {
|
|
|
|
return mEGL.eglSwapBuffers(mEGLDisplay, mEGLSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean checkForLostContext() {
|
|
|
|
if (mEGL.eglGetError() != EGL11.EGL_CONTEXT_LOST) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mEGLDisplay = null;
|
|
|
|
mEGLConfig = null;
|
|
|
|
mEGLContext = null;
|
|
|
|
mEGLSurface = null;
|
|
|
|
mGL = null;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-02 12:43:21 -07:00
|
|
|
public synchronized void waitForValidSurface() {
|
|
|
|
while (!mSurfaceValid) {
|
|
|
|
try {
|
|
|
|
wait();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 21:35:52 -08:00
|
|
|
public synchronized int getWidth() {
|
|
|
|
return mWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized int getHeight() {
|
|
|
|
return mHeight;
|
|
|
|
}
|
|
|
|
|
2012-04-02 12:43:21 -07:00
|
|
|
synchronized void surfaceCreated() {
|
|
|
|
mSurfaceValid = true;
|
|
|
|
notifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized void surfaceDestroyed() {
|
|
|
|
mSurfaceValid = false;
|
|
|
|
notifyAll();
|
|
|
|
}
|
|
|
|
|
2012-01-31 21:35:52 -08:00
|
|
|
synchronized void sizeChanged(int newWidth, int newHeight) {
|
|
|
|
mWidth = newWidth;
|
|
|
|
mHeight = newHeight;
|
2012-03-30 10:50:54 -07:00
|
|
|
if (mGL != null) {
|
|
|
|
mView.getRenderer().onSurfaceChanged((GL10)mGL, mWidth, mHeight);
|
|
|
|
}
|
2012-01-31 21:35:52 -08:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:39:04 -08:00
|
|
|
private void initEGL() {
|
2012-01-31 21:35:52 -08:00
|
|
|
mEGL = (EGL10)EGLContext.getEGL();
|
|
|
|
|
|
|
|
mEGLDisplay = mEGL.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
|
|
|
|
if (mEGLDisplay == EGL10.EGL_NO_DISPLAY) {
|
|
|
|
throw new GLControllerException("eglGetDisplay() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
mEGLConfig = chooseConfig();
|
|
|
|
|
|
|
|
int[] attribList = { EGL_CONTEXT_CLIENT_VERSION, mGLVersion, EGL10.EGL_NONE };
|
|
|
|
mEGLContext = mEGL.eglCreateContext(mEGLDisplay, mEGLConfig, EGL10.EGL_NO_CONTEXT,
|
|
|
|
attribList);
|
|
|
|
if (mEGLContext == null || mEGLContext == EGL10.EGL_NO_CONTEXT) {
|
2012-03-19 10:35:13 -07:00
|
|
|
throw new GLControllerException("createContext() failed " +
|
|
|
|
getEGLError());
|
2012-01-31 21:35:52 -08:00
|
|
|
}
|
2012-03-30 10:50:54 -07:00
|
|
|
|
|
|
|
mGL = mEGLContext.getGL();
|
|
|
|
|
|
|
|
if (mView.getRenderer() != null) {
|
|
|
|
mView.getRenderer().onSurfaceCreated((GL10)mGL, mEGLConfig);
|
|
|
|
mView.getRenderer().onSurfaceChanged((GL10)mGL, mView.getWidth(), mView.getHeight());
|
|
|
|
}
|
2012-01-31 21:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private EGLConfig chooseConfig() {
|
|
|
|
int[] numConfigs = new int[1];
|
|
|
|
if (!mEGL.eglChooseConfig(mEGLDisplay, CONFIG_SPEC, null, 0, numConfigs) ||
|
|
|
|
numConfigs[0] <= 0) {
|
2012-03-19 10:35:13 -07:00
|
|
|
throw new GLControllerException("No available EGL configurations " +
|
|
|
|
getEGLError());
|
2012-01-31 21:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
EGLConfig[] configs = new EGLConfig[numConfigs[0]];
|
|
|
|
if (!mEGL.eglChooseConfig(mEGLDisplay, CONFIG_SPEC, configs, numConfigs[0], numConfigs)) {
|
2012-03-19 10:35:13 -07:00
|
|
|
throw new GLControllerException("No EGL configuration for that specification " +
|
|
|
|
getEGLError());
|
2012-01-31 21:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Select the first 565 RGB configuration.
|
|
|
|
int[] red = new int[1], green = new int[1], blue = new int[1];
|
|
|
|
for (EGLConfig config : configs) {
|
|
|
|
mEGL.eglGetConfigAttrib(mEGLDisplay, config, EGL10.EGL_RED_SIZE, red);
|
|
|
|
mEGL.eglGetConfigAttrib(mEGLDisplay, config, EGL10.EGL_GREEN_SIZE, green);
|
|
|
|
mEGL.eglGetConfigAttrib(mEGLDisplay, config, EGL10.EGL_BLUE_SIZE, blue);
|
|
|
|
if (red[0] == 5 && green[0] == 6 && blue[0] == 5) {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new GLControllerException("No suitable EGL configuration found");
|
|
|
|
}
|
|
|
|
|
2012-03-12 13:20:19 -07:00
|
|
|
/**
|
|
|
|
* Provides an EGLSurface without assuming ownership of this surface.
|
|
|
|
* This class does not keep a reference to the provided EGL surface; the
|
|
|
|
* caller assumes ownership of the surface once it is returned.
|
|
|
|
*/
|
2012-02-06 13:53:09 -08:00
|
|
|
private EGLSurface provideEGLSurface() {
|
|
|
|
if (mEGL == null) {
|
2012-02-09 14:39:04 -08:00
|
|
|
initEGL();
|
2012-02-06 13:53:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceHolder surfaceHolder = mView.getHolder();
|
2012-03-12 13:20:19 -07:00
|
|
|
EGLSurface surface = mEGL.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, surfaceHolder, null);
|
|
|
|
if (surface == null || surface == EGL10.EGL_NO_SURFACE) {
|
2012-03-19 10:35:13 -07:00
|
|
|
throw new GLControllerException("EGL window surface could not be created! " +
|
|
|
|
getEGLError());
|
2012-02-06 13:53:09 -08:00
|
|
|
}
|
|
|
|
|
2012-03-12 13:20:19 -07:00
|
|
|
return surface;
|
2012-02-06 13:53:09 -08:00
|
|
|
}
|
2012-01-31 21:35:52 -08:00
|
|
|
|
2012-03-19 10:35:13 -07:00
|
|
|
private String getEGLError() {
|
|
|
|
return "Error " + mEGL.eglGetError();
|
|
|
|
}
|
|
|
|
|
2012-01-31 21:35:52 -08:00
|
|
|
public static class GLControllerException extends RuntimeException {
|
|
|
|
public static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
GLControllerException(String e) {
|
|
|
|
super(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|