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;
|
2013-02-28 18:28:23 +00:00
|
|
|
import org.mozilla.gecko.GeckoThread;
|
2013-11-12 18:40:59 +00:00
|
|
|
import org.mozilla.gecko.mozglue.generatorannotations.WrapElementForJNI;
|
2013-03-15 10:52:52 +00:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2013-02-28 18:28:23 +00:00
|
|
|
|
|
|
|
import android.util.Log;
|
2013-02-28 18:28:23 +00:00
|
|
|
|
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-11-22 23:23:34 +00:00
|
|
|
/**
|
|
|
|
* EGLPreloadingThread is purely a preloading optimization, not something
|
|
|
|
* we rely on for anything else than performance. We will be initializing
|
|
|
|
* EGL in GLController::initEGL() when we need it, but having EGL initialization
|
|
|
|
* already previously done by EGLPreloadingThread::run() will make it much
|
|
|
|
* faster for GLController to do again.
|
|
|
|
*
|
|
|
|
* For example, here are some timings recorded on two devices:
|
|
|
|
*
|
|
|
|
* Device | EGLPreloadingThread::run() | GLController::initEGL()
|
|
|
|
* -----------------------+----------------------------+------------------------
|
|
|
|
* Nexus S (Android 2.3) | ~ 80 ms | < 0.1 ms
|
|
|
|
* Nexus 10 (Android 4.3) | ~ 35 ms | < 0.1 ms
|
|
|
|
*/
|
|
|
|
class EGLPreloadingThread extends Thread
|
|
|
|
{
|
|
|
|
private static final String LOGTAG = "EGLPreloadingThread";
|
|
|
|
private EGL10 mEGL;
|
|
|
|
private EGLDisplay mEGLDisplay;
|
|
|
|
|
|
|
|
public EGLPreloadingThread()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
mEGL = (EGL10)EGLContext.getEGL();
|
|
|
|
mEGLDisplay = mEGL.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
|
|
|
|
if (mEGLDisplay == EGL10.EGL_NO_DISPLAY) {
|
|
|
|
Log.w(LOGTAG, "Can't get EGL display!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int[] returnedVersion = new int[2];
|
|
|
|
if (!mEGL.eglInitialize(mEGLDisplay, returnedVersion)) {
|
|
|
|
Log.w(LOGTAG, "eglInitialize failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2013-11-18 22:57:59 +00:00
|
|
|
private boolean mServerSurfaceValid;
|
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;
|
2013-11-22 23:23:34 +00:00
|
|
|
private EGLPreloadingThread mEGLPreloadingThread;
|
2013-11-22 23:23:36 +00:00
|
|
|
private EGLSurface mEGLSurfaceForCompositor;
|
2012-02-01 05:35:52 +00:00
|
|
|
|
2012-02-02 18:57:38 +00:00
|
|
|
private static final int LOCAL_EGL_OPENGL_ES2_BIT = 4;
|
|
|
|
|
2013-07-04 13:53:25 +00:00
|
|
|
private static final int[] CONFIG_SPEC_16BPP = {
|
2012-02-01 05:35:52 +00:00
|
|
|
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-07-04 13:53:25 +00:00
|
|
|
private static final int[] CONFIG_SPEC_24BPP = {
|
|
|
|
EGL10.EGL_RED_SIZE, 8,
|
|
|
|
EGL10.EGL_GREEN_SIZE, 8,
|
|
|
|
EGL10.EGL_BLUE_SIZE, 8,
|
|
|
|
EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT,
|
|
|
|
EGL10.EGL_RENDERABLE_TYPE, LOCAL_EGL_OPENGL_ES2_BIT,
|
|
|
|
EGL10.EGL_NONE
|
|
|
|
};
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
private GLController() {
|
2013-11-22 23:23:34 +00:00
|
|
|
mEGLPreloadingThread = new EGLPreloadingThread();
|
|
|
|
mEGLPreloadingThread.start();
|
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
|
|
|
}
|
|
|
|
|
2013-11-15 16:28:52 +00:00
|
|
|
synchronized void serverSurfaceDestroyed() {
|
2013-03-15 10:52:52 +00:00
|
|
|
ThreadUtils.assertOnUiThread();
|
2013-02-28 18:28:23 +00:00
|
|
|
|
2013-11-18 22:57:59 +00:00
|
|
|
mServerSurfaceValid = false;
|
|
|
|
|
2013-11-22 23:23:36 +00:00
|
|
|
if (mEGLSurfaceForCompositor != null) {
|
|
|
|
mEGL.eglDestroySurface(mEGLDisplay, mEGLSurfaceForCompositor);
|
|
|
|
mEGLSurfaceForCompositor = null;
|
|
|
|
}
|
|
|
|
|
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
|
2013-11-15 16:28:52 +00:00
|
|
|
// serverSurfaceDestroyed notification), and to make sure that any in-flight
|
2013-02-28 18:28:23 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-11-15 16:28:52 +00:00
|
|
|
synchronized void serverSurfaceChanged(int newWidth, int newHeight) {
|
2013-03-15 10:52:52 +00:00
|
|
|
ThreadUtils.assertOnUiThread();
|
2013-02-28 18:28:23 +00:00
|
|
|
|
2012-02-01 05:35:52 +00:00
|
|
|
mWidth = newWidth;
|
|
|
|
mHeight = newHeight;
|
2013-11-18 22:57:59 +00:00
|
|
|
mServerSurfaceValid = true;
|
2013-02-28 18:28:23 +00:00
|
|
|
|
2013-11-18 22:57:47 +00:00
|
|
|
// we defer to a runnable the task of updating the compositor, because this is going to
|
|
|
|
// call back into createEGLSurfaceForCompositor, which will try to create an EGLSurface
|
|
|
|
// against mView, which we suspect might fail if called too early. By posting this to
|
|
|
|
// mView, we hope to ensure that it is deferred until mView is actually "ready" for some
|
|
|
|
// sense of "ready".
|
|
|
|
mView.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
updateCompositor();
|
|
|
|
}
|
|
|
|
});
|
2013-02-28 18:28:23 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 16:28:57 +00:00
|
|
|
void updateCompositor() {
|
2013-03-15 10:52:52 +00:00
|
|
|
ThreadUtils.assertOnUiThread();
|
2013-02-28 18:28:23 +00:00
|
|
|
|
|
|
|
if (mCompositorCreated) {
|
|
|
|
// If the compositor has already been created, just resume it instead. We don't need
|
|
|
|
// to block here because if the surface is destroyed before the compositor grabs it,
|
|
|
|
// we can handle that gracefully (i.e. the compositor will remain paused).
|
|
|
|
resumeCompositor(mWidth, mHeight);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-22 23:23:36 +00:00
|
|
|
if (!AttemptPreallocateEGLSurfaceForCompositor()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
// Only try to create the compositor if we have a valid surface and gecko is up. When these
|
|
|
|
// two conditions are satisfied, we can be relatively sure that the compositor creation will
|
|
|
|
// happen without needing to block anyhwere. Do it with a sync gecko event so that the
|
|
|
|
// android doesn't have a chance to destroy our surface in between.
|
2013-11-15 16:28:54 +00:00
|
|
|
if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) {
|
2013-03-20 22:45:07 +00:00
|
|
|
GeckoAppShell.sendEventToGeckoSync(GeckoEvent.createCompositorCreateEvent(mWidth, mHeight));
|
2013-02-28 18:28:23 +00:00
|
|
|
}
|
2012-02-01 05:35:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 18:28:23 +00:00
|
|
|
void compositorCreated() {
|
2013-02-28 18:28:23 +00:00
|
|
|
// This is invoked on the compositor thread, while the java UI thread
|
2013-11-15 16:28:57 +00:00
|
|
|
// is blocked on the gecko sync event in updateCompositor() above
|
2013-02-28 18:28:23 +00:00
|
|
|
mCompositorCreated = true;
|
|
|
|
}
|
|
|
|
|
2013-11-18 22:57:59 +00:00
|
|
|
public boolean isServerSurfaceValid() {
|
|
|
|
return mServerSurfaceValid;
|
|
|
|
}
|
|
|
|
|
2013-11-20 22:50:54 +00:00
|
|
|
public boolean isCompositorCreated() {
|
|
|
|
return mCompositorCreated;
|
|
|
|
}
|
|
|
|
|
2012-02-09 22:39:04 +00:00
|
|
|
private void initEGL() {
|
2013-11-15 16:28:56 +00:00
|
|
|
if (mEGL != null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-22 23:23:34 +00:00
|
|
|
|
|
|
|
// This join() should not be necessary, but makes this code a bit easier to think about.
|
|
|
|
// The EGLPreloadingThread should long be done by now, and even if it's not,
|
|
|
|
// it shouldn't be a problem to be initalizing EGL from two different threads.
|
|
|
|
// Still, having this join() here means that we don't have to wonder about what
|
|
|
|
// kind of caveats might exist with EGL initialization reentrancy on various drivers.
|
|
|
|
try {
|
|
|
|
mEGLPreloadingThread.join();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.w(LOGTAG, "EGLPreloadingThread interrupted", e);
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-11-15 16:28:56 +00:00
|
|
|
Log.w(LOGTAG, "Can't get EGL display!");
|
|
|
|
return;
|
2012-02-01 05:35:52 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 23:23:34 +00:00
|
|
|
// while calling eglInitialize here should not be necessary as it was already called
|
|
|
|
// by the EGLPreloadingThread, it really doesn't cost much to call it again here,
|
|
|
|
// and makes this code easier to think about: EGLPreloadingThread is only a
|
|
|
|
// preloading optimization, not something we rely on for anything else.
|
|
|
|
//
|
|
|
|
// Also note that while calling eglInitialize isn't necessary on Android 4.x
|
|
|
|
// (at least Android's HardwareRenderer does it for us already), it is necessary
|
|
|
|
// on Android 2.x.
|
|
|
|
int[] returnedVersion = new int[2];
|
|
|
|
if (!mEGL.eglInitialize(mEGLDisplay, returnedVersion)) {
|
|
|
|
Log.w(LOGTAG, "eglInitialize failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-01 05:35:52 +00:00
|
|
|
mEGLConfig = chooseConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
private EGLConfig chooseConfig() {
|
2013-07-04 13:53:25 +00:00
|
|
|
int[] desiredConfig;
|
|
|
|
int rSize, gSize, bSize;
|
2012-02-01 05:35:52 +00:00
|
|
|
int[] numConfigs = new int[1];
|
2013-07-04 13:53:25 +00:00
|
|
|
|
|
|
|
switch (GeckoAppShell.getScreenDepth()) {
|
|
|
|
case 24:
|
|
|
|
desiredConfig = CONFIG_SPEC_24BPP;
|
|
|
|
rSize = gSize = bSize = 8;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
default:
|
|
|
|
desiredConfig = CONFIG_SPEC_16BPP;
|
|
|
|
rSize = 5; gSize = 6; bSize = 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mEGL.eglChooseConfig(mEGLDisplay, desiredConfig, null, 0, numConfigs) ||
|
2012-02-01 05:35:52 +00:00
|
|
|
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]];
|
2013-07-04 13:53:25 +00:00
|
|
|
if (!mEGL.eglChooseConfig(mEGLDisplay, desiredConfig, 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
|
|
|
}
|
|
|
|
|
2013-07-04 13:53:25 +00:00
|
|
|
// Select the first configuration that matches the screen depth.
|
2012-02-01 05:35:52 +00:00
|
|
|
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);
|
2013-07-04 13:53:25 +00:00
|
|
|
if (red[0] == rSize && green[0] == gSize && blue[0] == bSize) {
|
2012-02-01 05:35:52 +00:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new GLControllerException("No suitable EGL configuration found");
|
|
|
|
}
|
|
|
|
|
2013-11-22 23:23:36 +00:00
|
|
|
private synchronized boolean AttemptPreallocateEGLSurfaceForCompositor() {
|
|
|
|
if (mEGLSurfaceForCompositor == null) {
|
|
|
|
initEGL();
|
|
|
|
try {
|
|
|
|
mEGLSurfaceForCompositor = mEGL.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mView.getNativeWindow(), null);
|
2013-12-18 20:49:11 +00:00
|
|
|
// In failure cases, eglCreateWindowSurface should return EGL_NO_SURFACE.
|
|
|
|
// We currently normalize this to null, and compare to null in all our checks.
|
|
|
|
if (mEGLSurfaceForCompositor == EGL10.EGL_NO_SURFACE) {
|
|
|
|
mEGLSurfaceForCompositor = null;
|
|
|
|
}
|
2013-11-22 23:23:36 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "eglCreateWindowSurface threw", e);
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 20:49:11 +00:00
|
|
|
if (mEGLSurfaceForCompositor == null) {
|
|
|
|
Log.w(LOGTAG, "eglCreateWindowSurface returned no surface!");
|
|
|
|
}
|
2013-11-22 23:23:36 +00:00
|
|
|
return mEGLSurfaceForCompositor != null;
|
|
|
|
}
|
|
|
|
|
2013-11-12 18:40:59 +00:00
|
|
|
@WrapElementForJNI(allowMultithread = true, stubName = "CreateEGLSurfaceForCompositorWrapper")
|
2013-11-22 23:23:36 +00:00
|
|
|
private synchronized EGLSurface createEGLSurfaceForCompositor() {
|
|
|
|
AttemptPreallocateEGLSurfaceForCompositor();
|
|
|
|
EGLSurface result = mEGLSurfaceForCompositor;
|
|
|
|
mEGLSurfaceForCompositor = null;
|
|
|
|
return result;
|
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() {
|
2013-06-05 16:07:04 +00:00
|
|
|
return "Error " + (mEGL == null ? "(no mEGL)" : mEGL.eglGetError());
|
2012-03-19 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|