2012-02-01 05:35:52 +00:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-02-01 05:35:52 +00:00
|
|
|
|
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
|
|
|
import org.mozilla.gecko.GeckoEvent;
|
|
|
|
|
2012-02-01 05:35:52 +00:00
|
|
|
import javax.microedition.khronos.egl.EGL10;
|
|
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
|
|
|
import javax.microedition.khronos.egl.EGLContext;
|
|
|
|
import javax.microedition.khronos.egl.EGLDisplay;
|
|
|
|
import javax.microedition.khronos.egl.EGLSurface;
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
/**
|
|
|
|
* This class is a singleton that tracks EGL and compositor things over
|
|
|
|
* the lifetime of Fennec running.
|
|
|
|
* We only ever create one C++ compositor over Fennec's lifetime, but
|
|
|
|
* most of the Java-side objects (e.g. LayerView, GeckoLayerClient,
|
|
|
|
* LayerRenderer) can all get destroyed and re-created if the GeckoApp
|
|
|
|
* activity is destroyed. This GLController is never destroyed, so that
|
|
|
|
* the mCompositorCreated field and other state variables are always
|
|
|
|
* accurate.
|
|
|
|
*/
|
2012-02-01 05:35:52 +00:00
|
|
|
public class GLController {
|
|
|
|
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
|
|
|
|
private static final String LOGTAG = "GeckoGLController";
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
private static GLController sInstance;
|
|
|
|
|
2012-04-05 15:28:50 +00:00
|
|
|
private LayerView mView;
|
2012-04-02 19:43:21 +00:00
|
|
|
private boolean mSurfaceValid;
|
2012-02-01 05:35:52 +00:00
|
|
|
private int mWidth, mHeight;
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
/* This is written by the compositor thread (while the UI thread
|
|
|
|
* is blocked on it) and read by the UI thread. */
|
|
|
|
private volatile boolean mCompositorCreated;
|
|
|
|
|
2012-02-01 05:35:52 +00:00
|
|
|
private EGL10 mEGL;
|
|
|
|
private EGLDisplay mEGLDisplay;
|
|
|
|
private EGLConfig mEGLConfig;
|
|
|
|
|
2012-02-02 18:57:38 +00:00
|
|
|
private static final int LOCAL_EGL_OPENGL_ES2_BIT = 4;
|
|
|
|
|
2012-02-01 05:35:52 +00: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 18:57:38 +00:00
|
|
|
EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT,
|
|
|
|
EGL10.EGL_RENDERABLE_TYPE, LOCAL_EGL_OPENGL_ES2_BIT,
|
2012-02-01 05:35:52 +00:00
|
|
|
EGL10.EGL_NONE
|
|
|
|
};
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
private GLController() {
|
2012-02-01 05:35:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
static GLController getInstance(LayerView view) {
|
|
|
|
if (sInstance == null) {
|
|
|
|
sInstance = new GLController();
|
2012-07-13 14:19:46 +00:00
|
|
|
}
|
2013-02-28 18:28:23 +00:00
|
|
|
sInstance.mView = view;
|
|
|
|
return sInstance;
|
2012-07-13 14:19:46 +00:00
|
|
|
}
|
|
|
|
|
2012-12-21 16:32:52 +00:00
|
|
|
/* Wait until we are allowed to use EGL functions on the Surface backing
|
|
|
|
* this window.
|
|
|
|
* This function is invoked by JNI */
|
|
|
|
synchronized void waitForValidSurface() {
|
2012-04-02 19:43:21 +00:00
|
|
|
while (!mSurfaceValid) {
|
|
|
|
try {
|
|
|
|
wait();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized void surfaceDestroyed() {
|
|
|
|
mSurfaceValid = false;
|
|
|
|
notifyAll();
|
2013-02-28 18:28:23 +00:00
|
|
|
|
|
|
|
// We need to coordinate with Gecko when pausing composition, to ensure
|
|
|
|
// that Gecko never executes a draw event while the compositor is paused.
|
|
|
|
// This is sent synchronously to make sure that we don't attempt to use
|
|
|
|
// any outstanding Surfaces after we call this (such as from a
|
|
|
|
// surfaceDestroyed notification), and to make sure that any in-flight
|
|
|
|
// Gecko draw events have been processed. When this returns, composition is
|
|
|
|
// definitely paused -- it'll synchronize with the Gecko event loop, which
|
|
|
|
// in turn will synchronize with the compositor thread.
|
|
|
|
if (mCompositorCreated) {
|
|
|
|
GeckoAppShell.sendEventToGeckoSync(GeckoEvent.createCompositorPauseEvent());
|
|
|
|
}
|
2012-04-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
2012-05-18 19:58:46 +00:00
|
|
|
synchronized void surfaceChanged(int newWidth, int newHeight) {
|
2012-02-01 05:35:52 +00:00
|
|
|
mWidth = newWidth;
|
|
|
|
mHeight = newHeight;
|
2012-05-18 19:58:46 +00:00
|
|
|
mSurfaceValid = true;
|
|
|
|
notifyAll();
|
2012-02-01 05:35:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
void compositorCreated() {
|
|
|
|
mCompositorCreated = true;
|
|
|
|
}
|
|
|
|
|
2013-01-10 17:32:43 +00:00
|
|
|
public boolean hasValidSurface() {
|
|
|
|
return mSurfaceValid;
|
|
|
|
}
|
|
|
|
|
2012-02-09 22:39:04 +00:00
|
|
|
private void initEGL() {
|
2012-02-01 05:35:52 +00: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
private EGLConfig chooseConfig() {
|
|
|
|
int[] numConfigs = new int[1];
|
|
|
|
if (!mEGL.eglChooseConfig(mEGLDisplay, CONFIG_SPEC, null, 0, numConfigs) ||
|
|
|
|
numConfigs[0] <= 0) {
|
2012-03-19 17:35:13 +00:00
|
|
|
throw new GLControllerException("No available EGL configurations " +
|
|
|
|
getEGLError());
|
2012-02-01 05:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EGLConfig[] configs = new EGLConfig[numConfigs[0]];
|
|
|
|
if (!mEGL.eglChooseConfig(mEGLDisplay, CONFIG_SPEC, configs, numConfigs[0], numConfigs)) {
|
2012-03-19 17:35:13 +00:00
|
|
|
throw new GLControllerException("No EGL configuration for that specification " +
|
|
|
|
getEGLError());
|
2012-02-01 05:35:52 +00: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 20:20:19 +00: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-12-21 16:32:52 +00:00
|
|
|
* This function is invoked by JNI */
|
2012-02-06 21:53:09 +00:00
|
|
|
private EGLSurface provideEGLSurface() {
|
2013-01-10 16:21:10 +00:00
|
|
|
synchronized (this) {
|
|
|
|
if (!mSurfaceValid) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-06 21:53:09 +00:00
|
|
|
if (mEGL == null) {
|
2012-02-09 22:39:04 +00:00
|
|
|
initEGL();
|
2012-02-06 21:53:09 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 17:30:46 +00:00
|
|
|
Object window = mView.getNativeWindow();
|
|
|
|
EGLSurface surface = mEGL.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, window, null);
|
2012-03-12 20:20:19 +00:00
|
|
|
if (surface == null || surface == EGL10.EGL_NO_SURFACE) {
|
2012-03-19 17:35:13 +00:00
|
|
|
throw new GLControllerException("EGL window surface could not be created! " +
|
|
|
|
getEGLError());
|
2012-02-06 21:53:09 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 20:20:19 +00:00
|
|
|
return surface;
|
2012-02-06 21:53:09 +00:00
|
|
|
}
|
2012-02-01 05:35:52 +00:00
|
|
|
|
2012-03-19 17:35:13 +00:00
|
|
|
private String getEGLError() {
|
|
|
|
return "Error " + mEGL.eglGetError();
|
|
|
|
}
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
void resumeCompositor(int width, int height) {
|
|
|
|
// Asking Gecko to resume the compositor takes too long (see
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=735230#c23), so we
|
|
|
|
// resume the compositor directly. We still need to inform Gecko about
|
|
|
|
// the compositor resuming, so that Gecko knows that it can now draw.
|
|
|
|
// It is important to not notify Gecko until after the compositor has
|
|
|
|
// been resumed, otherwise Gecko may send updates that get dropped.
|
|
|
|
if (mCompositorCreated) {
|
|
|
|
GeckoAppShell.scheduleResumeComposition(width, height);
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createCompositorResumeEvent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 05:35:52 +00:00
|
|
|
public static class GLControllerException extends RuntimeException {
|
|
|
|
public static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
GLControllerException(String e) {
|
|
|
|
super(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|