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
.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
;
15 public class GLController
{
16 private static final int EGL_CONTEXT_CLIENT_VERSION
= 0x3098;
17 private static final String LOGTAG
= "GeckoGLController";
19 private LayerView mView
;
20 private int mGLVersion
;
21 private int mWidth
, mHeight
;
24 private EGLDisplay mEGLDisplay
;
25 private EGLConfig mEGLConfig
;
26 private EGLContext mEGLContext
;
27 private EGLSurface mEGLSurface
;
29 private static final int LOCAL_EGL_OPENGL_ES2_BIT
= 4;
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
,
40 public GLController(LayerView view
) {
45 public void setGLVersion(int version
) {
49 /** You must call this on the same thread you intend to use OpenGL on. */
50 public void initGLContext() {
55 public void disposeGLContext() {
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! " +
66 if (mEGLSurface
!= null) {
67 if (!mEGL
.eglDestroySurface(mEGLDisplay
, mEGLSurface
)) {
68 throw new GLControllerException("EGL surface could not be destroyed! " +
75 if (mEGLContext
!= null) {
76 if (!mEGL
.eglDestroyContext(mEGLDisplay
, mEGLContext
)) {
77 throw new GLControllerException("EGL context could not be destroyed! " +
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
; }
92 public boolean hasSurface() {
93 return mEGLSurface
!= null;
96 public boolean swapBuffers() {
97 return mEGL
.eglSwapBuffers(mEGLDisplay
, mEGLSurface
);
100 public synchronized int getWidth() {
104 public synchronized int getHeight() {
108 synchronized void surfaceDestroyed() {
112 synchronized void surfaceChanged(int newWidth
, int newHeight
) {
118 private void initEGL() {
119 mEGL
= (EGL10
)EGLContext
.getEGL();
121 mEGLDisplay
= mEGL
.eglGetDisplay(EGL10
.EGL_DEFAULT_DISPLAY
);
122 if (mEGLDisplay
== EGL10
.EGL_NO_DISPLAY
) {
123 throw new GLControllerException("eglGetDisplay() failed");
126 int[] version
= new int[2];
127 if (!mEGL
.eglInitialize(mEGLDisplay
, version
)) {
128 throw new GLControllerException("eglInitialize() failed " + getEGLError());
131 mEGLConfig
= chooseConfig();
134 private void initEGLContext() {
137 int[] attribList
= { EGL_CONTEXT_CLIENT_VERSION
, mGLVersion
, EGL10
.EGL_NONE
};
138 mEGLContext
= mEGL
.eglCreateContext(mEGLDisplay
, mEGLConfig
, EGL10
.EGL_NO_CONTEXT
,
140 if (mEGLContext
== null || mEGLContext
== EGL10
.EGL_NO_CONTEXT
) {
141 throw new GLControllerException("createContext() failed " +
145 if (mView
.getRenderer() != null) {
146 GL10 gl
= (GL10
) mEGLContext
.getGL();
147 mView
.getRenderer().onSurfaceCreated(gl
, mEGLConfig
);
148 mView
.getRenderer().onSurfaceChanged(gl
, mWidth
, mHeight
);
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 " +
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 " +
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) {
177 // if there's no 565 RGB configuration, select another one that fulfils the specification
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! " +
189 if (!mEGL
.eglMakeCurrent(mEGLDisplay
, mEGLSurface
, mEGLSurface
, mEGLContext
)) {
190 throw new GLControllerException("EGL surface could not be made into the current " +
191 "surface! " + getEGLError());
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());
201 private String
getEGLError() {
202 return "Error " + mEGL
.eglGetError();
205 public static class GLControllerException
extends RuntimeException
{
206 public static final long serialVersionUID
= 1L;
208 GLControllerException(String e
) {