| 1 |
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
| 2 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
package org.mozilla.gecko.gfx; |
| 7 |
|
| 8 |
import javax.microedition.khronos.egl.EGL10; |
| 9 |
import javax.microedition.khronos.egl.EGLConfig; |
| 10 |
import javax.microedition.khronos.egl.EGLContext; |
| 11 |
import javax.microedition.khronos.egl.EGLDisplay; |
| 12 |
import javax.microedition.khronos.egl.EGLSurface; |
| 13 |
import javax.microedition.khronos.opengles.GL10; |
| 14 |
|
| 15 |
public class GLController { |
| 16 |
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; |
| 17 |
private static final String LOGTAG = "GeckoGLController"; |
| 18 |
|
| 19 |
private final LayerView mView; |
| 20 |
private int mGLVersion; |
| 21 |
private int mWidth, mHeight; |
| 22 |
|
| 23 |
private EGL10 mEGL; |
| 24 |
private EGLDisplay mEGLDisplay; |
| 25 |
private EGLConfig mEGLConfig; |
| 26 |
private EGLContext mEGLContext; |
| 27 |
private EGLSurface mEGLSurface; |
| 28 |
|
| 29 |
private static final int LOCAL_EGL_OPENGL_ES2_BIT = 4; |
| 30 |
|
| 31 |
private static final int[] CONFIG_SPEC = { |
| 32 |
EGL10.EGL_RED_SIZE, 5, |
| 33 |
EGL10.EGL_GREEN_SIZE, 6, |
| 34 |
EGL10.EGL_BLUE_SIZE, 5, |
| 35 |
EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT, |
| 36 |
EGL10.EGL_RENDERABLE_TYPE, LOCAL_EGL_OPENGL_ES2_BIT, |
| 37 |
EGL10.EGL_NONE |
| 38 |
}; |
| 39 |
|
| 40 |
public GLController(LayerView view) { |
| 41 |
mView = view; |
| 42 |
mGLVersion = 2; |
| 43 |
} |
| 44 |
|
| 45 |
public void setGLVersion(int version) { |
| 46 |
mGLVersion = version; |
| 47 |
} |
| 48 |
|
| 49 |
/** You must call this on the same thread you intend to use OpenGL on. */ |
| 50 |
public void initGLContext() { |
| 51 |
initEGLContext(); |
| 52 |
createEGLSurface(); |
| 53 |
} |
| 54 |
|
| 55 |
public void disposeGLContext() { |
| 56 |
if (mEGL == null) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if (!mEGL.eglMakeCurrent(mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, |
| 61 |
EGL10.EGL_NO_CONTEXT)) { |
| 62 |
throw new GLControllerException("EGL context could not be released! " + |
| 63 |
getEGLError()); |
| 64 |
} |
| 65 |
|
| 66 |
if (mEGLSurface != null) { |
| 67 |
if (!mEGL.eglDestroySurface(mEGLDisplay, mEGLSurface)) { |
| 68 |
throw new GLControllerException("EGL surface could not be destroyed! " + |
| 69 |
getEGLError()); |
| 70 |
} |
| 71 |
|
| 72 |
mEGLSurface = null; |
| 73 |
} |
| 74 |
|
| 75 |
if (mEGLContext != null) { |
| 76 |
if (!mEGL.eglDestroyContext(mEGLDisplay, mEGLContext)) { |
| 77 |
throw new GLControllerException("EGL context could not be destroyed! " + |
| 78 |
getEGLError()); |
| 79 |
} |
| 80 |
|
| 81 |
mEGLContext = null; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public GL10 getGL() { return (GL10) mEGLContext.getGL(); } |
| 86 |
public EGLDisplay getEGLDisplay() { return mEGLDisplay; } |
| 87 |
public EGLConfig getEGLConfig() { return mEGLConfig; } |
| 88 |
public EGLContext getEGLContext() { return mEGLContext; } |
| 89 |
public EGLSurface getEGLSurface() { return mEGLSurface; } |
| 90 |
public LayerView getView() { return mView; } |
| 91 |
|
| 92 |
public boolean hasSurface() { |
| 93 |
return mEGLSurface != null; |
| 94 |
} |
| 95 |
|
| 96 |
public boolean swapBuffers() { |
| 97 |
return mEGL.eglSwapBuffers(mEGLDisplay, mEGLSurface); |
| 98 |
} |
| 99 |
|
| 100 |
public synchronized int getWidth() { |
| 101 |
return mWidth; |
| 102 |
} |
| 103 |
|
| 104 |
public synchronized int getHeight() { |
| 105 |
return mHeight; |
| 106 |
} |
| 107 |
|
| 108 |
synchronized void surfaceDestroyed() { |
| 109 |
notifyAll(); |
| 110 |
} |
| 111 |
|
| 112 |
synchronized void surfaceChanged(int newWidth, int newHeight) { |
| 113 |
mWidth = newWidth; |
| 114 |
mHeight = newHeight; |
| 115 |
notifyAll(); |
| 116 |
} |
| 117 |
|
| 118 |
private void initEGL() { |
| 119 |
mEGL = (EGL10)EGLContext.getEGL(); |
| 120 |
|
| 121 |
mEGLDisplay = mEGL.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); |
| 122 |
if (mEGLDisplay == EGL10.EGL_NO_DISPLAY) { |
| 123 |
throw new GLControllerException("eglGetDisplay() failed"); |
| 124 |
} |
| 125 |
|
| 126 |
int[] version = new int[2]; |
| 127 |
if (!mEGL.eglInitialize(mEGLDisplay, version)) { |
| 128 |
throw new GLControllerException("eglInitialize() failed " + getEGLError()); |
| 129 |
} |
| 130 |
|
| 131 |
mEGLConfig = chooseConfig(); |
| 132 |
} |
| 133 |
|
| 134 |
private void initEGLContext() { |
| 135 |
initEGL(); |
| 136 |
|
| 137 |
int[] attribList = { EGL_CONTEXT_CLIENT_VERSION, mGLVersion, EGL10.EGL_NONE }; |
| 138 |
mEGLContext = mEGL.eglCreateContext(mEGLDisplay, mEGLConfig, EGL10.EGL_NO_CONTEXT, |
| 139 |
attribList); |
| 140 |
if (mEGLContext == null || mEGLContext == EGL10.EGL_NO_CONTEXT) { |
| 141 |
throw new GLControllerException("createContext() failed " + |
| 142 |
getEGLError()); |
| 143 |
} |
| 144 |
|
| 145 |
if (mView.getRenderer() != null) { |
| 146 |
GL10 gl = (GL10) mEGLContext.getGL(); |
| 147 |
mView.getRenderer().onSurfaceCreated(gl, mEGLConfig); |
| 148 |
mView.getRenderer().onSurfaceChanged(gl, mWidth, mHeight); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
private EGLConfig chooseConfig() { |
| 153 |
int[] numConfigs = new int[1]; |
| 154 |
if (!mEGL.eglChooseConfig(mEGLDisplay, CONFIG_SPEC, null, 0, numConfigs) || |
| 155 |
numConfigs[0] <= 0) { |
| 156 |
throw new GLControllerException("No available EGL configurations " + |
| 157 |
getEGLError()); |
| 158 |
} |
| 159 |
|
| 160 |
EGLConfig[] configs = new EGLConfig[numConfigs[0]]; |
| 161 |
if (!mEGL.eglChooseConfig(mEGLDisplay, CONFIG_SPEC, configs, numConfigs[0], numConfigs)) { |
| 162 |
throw new GLControllerException("No EGL configuration for that specification " + |
| 163 |
getEGLError()); |
| 164 |
} |
| 165 |
|
| 166 |
// Select the first 565 RGB configuration. |
| 167 |
int[] red = new int[1], green = new int[1], blue = new int[1]; |
| 168 |
for (EGLConfig config : configs) { |
| 169 |
mEGL.eglGetConfigAttrib(mEGLDisplay, config, EGL10.EGL_RED_SIZE, red); |
| 170 |
mEGL.eglGetConfigAttrib(mEGLDisplay, config, EGL10.EGL_GREEN_SIZE, green); |
| 171 |
mEGL.eglGetConfigAttrib(mEGLDisplay, config, EGL10.EGL_BLUE_SIZE, blue); |
| 172 |
if (red[0] == 5 && green[0] == 6 && blue[0] == 5) { |
| 173 |
return config; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// if there's no 565 RGB configuration, select another one that fulfils the specification |
| 178 |
return configs[0]; |
| 179 |
} |
| 180 |
|
| 181 |
private void createEGLSurface() { |
| 182 |
Object window = mView.getNativeWindow(); |
| 183 |
mEGLSurface = mEGL.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, window, null); |
| 184 |
if (mEGLSurface == null || mEGLSurface == EGL10.EGL_NO_SURFACE) { |
| 185 |
throw new GLControllerException("EGL window surface could not be created! " + |
| 186 |
getEGLError()); |
| 187 |
} |
| 188 |
|
| 189 |
if (!mEGL.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext)) { |
| 190 |
throw new GLControllerException("EGL surface could not be made into the current " + |
| 191 |
"surface! " + getEGLError()); |
| 192 |
} |
| 193 |
|
| 194 |
if (mView.getRenderer() != null) { |
| 195 |
GL10 gl = (GL10) mEGLContext.getGL(); |
| 196 |
mView.getRenderer().onSurfaceCreated(gl, mEGLConfig); |
| 197 |
mView.getRenderer().onSurfaceChanged(gl, mView.getWidth(), mView.getHeight()); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
private String getEGLError() { |
| 202 |
return "Error " + mEGL.eglGetError(); |
| 203 |
} |
| 204 |
|
| 205 |
public static class GLControllerException extends RuntimeException { |
| 206 |
public static final long serialVersionUID = 1L; |
| 207 |
|
| 208 |
GLControllerException(String e) { |
| 209 |
super(e); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|