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/. */
6 package org
.mozilla
.gecko
.gfx
;
8 import javax
.microedition
.khronos
.egl
.EGL10
;
9 import javax
.microedition
.khronos
.egl
.EGL11
;
10 import javax
.microedition
.khronos
.egl
.EGLConfig
;
11 import javax
.microedition
.khronos
.egl
.EGLContext
;
12 import javax
.microedition
.khronos
.egl
.EGLDisplay
;
13 import javax
.microedition
.khronos
.egl
.EGLSurface
;
14 import javax
.microedition
.khronos
.opengles
.GL
;
15 import javax
.microedition
.khronos
.opengles
.GL10
;
17 public class GLController
{
18 private static final int EGL_CONTEXT_CLIENT_VERSION
= 0x3098;
19 private static final String LOGTAG
= "GeckoGLController";
21 private LayerView mView
;
22 private int mGLVersion
;
23 private boolean mSurfaceValid
;
24 private int mWidth
, mHeight
;
27 private EGLDisplay mEGLDisplay
;
28 private EGLConfig mEGLConfig
;
29 private EGLContext mEGLContext
;
30 private EGLSurface mEGLSurface
;
34 private static final int LOCAL_EGL_OPENGL_ES2_BIT
= 4;
36 private static final int[] CONFIG_SPEC
= {
37 EGL10
.EGL_RED_SIZE
, 5,
38 EGL10
.EGL_GREEN_SIZE
, 6,
39 EGL10
.EGL_BLUE_SIZE
, 5,
40 EGL10
.EGL_SURFACE_TYPE
, EGL10
.EGL_WINDOW_BIT
,
41 EGL10
.EGL_RENDERABLE_TYPE
, LOCAL_EGL_OPENGL_ES2_BIT
,
45 public GLController(LayerView view
) {
48 mSurfaceValid
= false;
51 public void setGLVersion(int version
) {
55 /** You must call this on the same thread you intend to use OpenGL on. */
56 public void initGLContext() {
61 public void disposeGLContext() {
66 if (!mEGL
.eglMakeCurrent(mEGLDisplay
, EGL10
.EGL_NO_SURFACE
, EGL10
.EGL_NO_SURFACE
,
67 EGL10
.EGL_NO_CONTEXT
)) {
68 throw new GLControllerException("EGL context could not be released! " +
72 if (mEGLSurface
!= null) {
73 if (!mEGL
.eglDestroySurface(mEGLDisplay
, mEGLSurface
)) {
74 throw new GLControllerException("EGL surface could not be destroyed! " +
81 if (mEGLContext
!= null) {
82 if (!mEGL
.eglDestroyContext(mEGLDisplay
, mEGLContext
)) {
83 throw new GLControllerException("EGL context could not be destroyed! " +
92 public GL
getGL() { return mEGLContext
.getGL(); }
93 public EGLDisplay
getEGLDisplay() { return mEGLDisplay
; }
94 public EGLConfig
getEGLConfig() { return mEGLConfig
; }
95 public EGLContext
getEGLContext() { return mEGLContext
; }
96 public EGLSurface
getEGLSurface() { return mEGLSurface
; }
97 public LayerView
getView() { return mView
; }
99 public boolean hasSurface() {
100 return mEGLSurface
!= null;
103 public boolean swapBuffers() {
104 return mEGL
.eglSwapBuffers(mEGLDisplay
, mEGLSurface
);
107 public boolean checkForLostContext() {
108 if (mEGL
.eglGetError() != EGL11
.EGL_CONTEXT_LOST
) {
120 // This function is invoked by JNI
121 public synchronized void resumeCompositorIfValid() {
123 mView
.getListener().compositionResumeRequested(mWidth
, mHeight
);
127 // Wait until we are allowed to use EGL functions on the Surface backing
128 // this window. This function is invoked by JNI
129 public synchronized void waitForValidSurface() {
130 while (!mSurfaceValid
) {
133 } catch (InterruptedException e
) {
134 throw new RuntimeException(e
);
139 public synchronized int getWidth() {
143 public synchronized int getHeight() {
147 synchronized void surfaceDestroyed() {
148 mSurfaceValid
= false;
152 synchronized void surfaceChanged(int newWidth
, int newHeight
) {
155 mSurfaceValid
= true;
159 private void initEGL() {
160 mEGL
= (EGL10
)EGLContext
.getEGL();
162 mEGLDisplay
= mEGL
.eglGetDisplay(EGL10
.EGL_DEFAULT_DISPLAY
);
163 if (mEGLDisplay
== EGL10
.EGL_NO_DISPLAY
) {
164 throw new GLControllerException("eglGetDisplay() failed");
167 int[] version
= new int[2];
168 if (!mEGL
.eglInitialize(mEGLDisplay
, version
)) {
169 throw new GLControllerException("eglInitialize() failed " + getEGLError());
172 mEGLConfig
= chooseConfig();
175 private void initEGLContext() {
178 int[] attribList
= { EGL_CONTEXT_CLIENT_VERSION
, mGLVersion
, EGL10
.EGL_NONE
};
179 mEGLContext
= mEGL
.eglCreateContext(mEGLDisplay
, mEGLConfig
, EGL10
.EGL_NO_CONTEXT
,
181 if (mEGLContext
== null || mEGLContext
== EGL10
.EGL_NO_CONTEXT
) {
182 throw new GLControllerException("createContext() failed " +
186 mGL
= mEGLContext
.getGL();
188 if (mView
.getRenderer() != null) {
189 mView
.getRenderer().onSurfaceCreated((GL10
)mGL
, mEGLConfig
);
190 mView
.getRenderer().onSurfaceChanged((GL10
)mGL
, mWidth
, mHeight
);
194 private EGLConfig
chooseConfig() {
195 int[] numConfigs
= new int[1];
196 if (!mEGL
.eglChooseConfig(mEGLDisplay
, CONFIG_SPEC
, null, 0, numConfigs
) ||
197 numConfigs
[0] <= 0) {
198 throw new GLControllerException("No available EGL configurations " +
202 EGLConfig
[] configs
= new EGLConfig
[numConfigs
[0]];
203 if (!mEGL
.eglChooseConfig(mEGLDisplay
, CONFIG_SPEC
, configs
, numConfigs
[0], numConfigs
)) {
204 throw new GLControllerException("No EGL configuration for that specification " +
208 // Select the first 565 RGB configuration.
209 int[] red
= new int[1], green
= new int[1], blue
= new int[1];
210 for (EGLConfig config
: configs
) {
211 mEGL
.eglGetConfigAttrib(mEGLDisplay
, config
, EGL10
.EGL_RED_SIZE
, red
);
212 mEGL
.eglGetConfigAttrib(mEGLDisplay
, config
, EGL10
.EGL_GREEN_SIZE
, green
);
213 mEGL
.eglGetConfigAttrib(mEGLDisplay
, config
, EGL10
.EGL_BLUE_SIZE
, blue
);
214 if (red
[0] == 5 && green
[0] == 6 && blue
[0] == 5) {
219 // if there's no 565 RGB configuration, select another one that fulfils the specification
223 private void createEGLSurface() {
224 Object window
= mView
.getNativeWindow();
225 mEGLSurface
= mEGL
.eglCreateWindowSurface(mEGLDisplay
, mEGLConfig
, window
, null);
226 if (mEGLSurface
== null || mEGLSurface
== EGL10
.EGL_NO_SURFACE
) {
227 throw new GLControllerException("EGL window surface could not be created! " +
231 if (!mEGL
.eglMakeCurrent(mEGLDisplay
, mEGLSurface
, mEGLSurface
, mEGLContext
)) {
232 throw new GLControllerException("EGL surface could not be made into the current " +
233 "surface! " + getEGLError());
236 mGL
= mEGLContext
.getGL();
238 if (mView
.getRenderer() != null) {
239 mView
.getRenderer().onSurfaceCreated((GL10
)mGL
, mEGLConfig
);
240 mView
.getRenderer().onSurfaceChanged((GL10
)mGL
, mView
.getWidth(), mView
.getHeight());
245 * Provides an EGLSurface without assuming ownership of this surface.
246 * This class does not keep a reference to the provided EGL surface; the
247 * caller assumes ownership of the surface once it is returned.
249 private EGLSurface
provideEGLSurface() {
254 Object window
= mView
.getNativeWindow();
255 EGLSurface surface
= mEGL
.eglCreateWindowSurface(mEGLDisplay
, mEGLConfig
, window
, null);
256 if (surface
== null || surface
== EGL10
.EGL_NO_SURFACE
) {
257 throw new GLControllerException("EGL window surface could not be created! " +
264 private String
getEGLError() {
265 return "Error " + mEGL
.eglGetError();
268 public static class GLControllerException
extends RuntimeException
{
269 public static final long serialVersionUID
= 1L;
271 GLControllerException(String e
) {