Copied GLSurfaceView from Android 1.6 - now it works with 1.6 but fails with 2.1

This commit is contained in:
pelya
2010-06-16 19:24:14 +03:00
parent 09afe74eed
commit 58926b8c02
3 changed files with 2677 additions and 385 deletions

View File

@@ -18,10 +18,11 @@
fixed with a hammer and rasp to work with libSDL port */
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package de.schwardtnet.alienblaster;
package com.demo.glxgears;
import java.io.Writer;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGL11;
@@ -148,11 +149,6 @@ import android.view.SurfaceView;
*
*/
public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Callback {
private final static boolean LOG_THREADS = false;
private final static boolean LOG_SURFACE = false;
private final static boolean LOG_RENDERER = false;
// Work-around for bug 2263168
private final static boolean DRAW_TWICE_AFTER_SIZE_CHANGED = true;
/**
* The renderer only renders
* when the surface is created, or when {@link #requestRender} is called.
@@ -212,6 +208,7 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
// underlying surface is created and destroyed
SurfaceHolder holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
}
/**
@@ -279,49 +276,17 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
* @param renderer the renderer to use to perform OpenGL drawing.
*/
public void setRenderer(Renderer renderer) {
checkRenderThreadState();
if (mGLThread != null) {
throw new IllegalStateException(
"setRenderer has already been called for this instance.");
}
if (mEGLConfigChooser == null) {
mEGLConfigChooser = new SimpleEGLConfigChooser(true);
}
if (mEGLContextFactory == null) {
mEGLContextFactory = new DefaultContextFactory();
}
if (mEGLWindowSurfaceFactory == null) {
mEGLWindowSurfaceFactory = new DefaultWindowSurfaceFactory();
}
mGLThread = new GLThread(renderer);
mGLThread.start();
}
/**
* Install a custom EGLContextFactory.
* <p>If this method is
* called, it must be called before {@link #setRenderer(Renderer)}
* is called.
* <p>
* If this method is not called, then by default
* a context will be created with no shared context and
* with a null attribute list.
*/
public void setEGLContextFactory(EGLContextFactory factory) {
checkRenderThreadState();
mEGLContextFactory = factory;
}
/**
* Install a custom EGLWindowSurfaceFactory.
* <p>If this method is
* called, it must be called before {@link #setRenderer(Renderer)}
* is called.
* <p>
* If this method is not called, then by default
* a window surface will be created with a null attribute list.
*/
public void setEGLWindowSurfaceFactory(EGLWindowSurfaceFactory factory) {
checkRenderThreadState();
mEGLWindowSurfaceFactory = factory;
}
/**
* Install a custom EGLConfigChooser.
* <p>If this method is
@@ -334,7 +299,10 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
* @param configChooser
*/
public void setEGLConfigChooser(EGLConfigChooser configChooser) {
checkRenderThreadState();
if (mGLThread != null) {
throw new IllegalStateException(
"setRenderer has already been called for this instance.");
}
mEGLConfigChooser = configChooser;
}
@@ -630,54 +598,6 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
private SwapBuffersCallback mSwapBuffersCallback = null;
}
/**
* An interface for customizing the eglCreateContext and eglDestroyContext calls.
* <p>
* This interface must be implemented by clients wishing to call
* {@link GLSurfaceView#setEGLContextFactory(EGLContextFactory)}
*/
public interface EGLContextFactory {
EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig);
void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context);
}
private static class DefaultContextFactory implements EGLContextFactory {
public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) {
return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, null);
}
public void destroyContext(EGL10 egl, EGLDisplay display,
EGLContext context) {
egl.eglDestroyContext(display, context);
}
}
/**
* An interface for customizing the eglCreateWindowSurface and eglDestroySurface calls.
* <p>
* This interface must be implemented by clients wishing to call
* {@link GLSurfaceView#setEGLWindowSurfaceFactory(EGLWindowSurfaceFactory)}
*/
public interface EGLWindowSurfaceFactory {
EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display, EGLConfig config,
Object nativeWindow);
void destroySurface(EGL10 egl, EGLDisplay display, EGLSurface surface);
}
private static class DefaultWindowSurfaceFactory implements EGLWindowSurfaceFactory {
public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display,
EGLConfig config, Object nativeWindow) {
return egl.eglCreateWindowSurface(display, config, nativeWindow, null);
}
public void destroySurface(EGL10 egl, EGLDisplay display,
EGLSurface surface) {
egl.eglDestroySurface(display, surface);
}
}
/**
* An interface for choosing an EGLConfig configuration from a list of
* potential configurations.
@@ -756,27 +676,25 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
EGLConfig closestConfig = null;
int closestDistance = 1000;
for(EGLConfig config : configs) {
int r = findConfigAttrib(egl, display, config,
EGL10.EGL_RED_SIZE, 0);
int g = findConfigAttrib(egl, display, config,
EGL10.EGL_GREEN_SIZE, 0);
int b = findConfigAttrib(egl, display, config,
EGL10.EGL_BLUE_SIZE, 0);
int a = findConfigAttrib(egl, display, config,
EGL10.EGL_ALPHA_SIZE, 0);
int d = findConfigAttrib(egl, display, config,
EGL10.EGL_DEPTH_SIZE, 0);
int s = findConfigAttrib(egl, display, config,
EGL10.EGL_STENCIL_SIZE, 0);
if (d >= mDepthSize && s>= mStencilSize) {
int r = findConfigAttrib(egl, display, config,
EGL10.EGL_RED_SIZE, 0);
int g = findConfigAttrib(egl, display, config,
EGL10.EGL_GREEN_SIZE, 0);
int b = findConfigAttrib(egl, display, config,
EGL10.EGL_BLUE_SIZE, 0);
int a = findConfigAttrib(egl, display, config,
EGL10.EGL_ALPHA_SIZE, 0);
int distance = Math.abs(r - mRedSize)
+ Math.abs(g - mGreenSize)
+ Math.abs(b - mBlueSize)
+ Math.abs(a - mAlphaSize);
if (distance < closestDistance) {
closestDistance = distance;
closestConfig = config;
}
int distance = Math.abs(r - mRedSize)
+ Math.abs(g - mGreenSize)
+ Math.abs(b - mBlueSize) + Math.abs(a - mAlphaSize)
+ Math.abs(d - mDepthSize) + Math.abs(s - mStencilSize);
if (distance < closestDistance) {
closestDistance = distance;
closestConfig = config;
}
}
return closestConfig;
@@ -852,10 +770,8 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
* Create an OpenGL ES context. This must be done only once, an
* OpenGL context is a somewhat heavy object.
*/
mEglContext = mEGLContextFactory.createContext(mEgl, mEglDisplay, mEglConfig);
if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
throw new RuntimeException("createContext failed");
}
mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig,
EGL10.EGL_NO_CONTEXT, null);
mEglSurface = null;
}
@@ -869,7 +785,7 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
* The window size has changed, so we need to create a new
* surface.
*/
if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
if (mEglSurface != null) {
/*
* Unbind and destroy the old EGL surface, if
@@ -877,26 +793,22 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
*/
mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface);
mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
}
/*
* Create an EGL surface we can render into.
*/
mEglSurface = mEGLWindowSurfaceFactory.createWindowSurface(mEgl,
mEglDisplay, mEglConfig, holder);
if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
throwEglException("createWindowSurface");
}
mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,
mEglConfig, holder, null);
/*
* Before we can issue GL commands, we need to make sure
* the context is current and bound to a surface.
*/
if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
throwEglException("eglMakeCurrent");
}
mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
mEglContext);
GL gl = mEglContext.getGL();
if (mGLWrapper != null) {
@@ -922,19 +834,16 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
return mEgl.eglGetError() != EGL11.EGL_CONTEXT_LOST;
}
public void destroySurface() {
if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
public void finish() {
if (mEglSurface != null) {
mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT);
mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface);
mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
mEglSurface = null;
}
}
public void finish() {
if (mEglContext != null) {
mEGLContextFactory.destroyContext(mEgl, mEglDisplay, mEglContext);
mEgl.eglDestroyContext(mEglDisplay, mEglContext);
mEglContext = null;
}
if (mEglDisplay != null) {
@@ -943,10 +852,6 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
}
}
private void throwEglException(String function) {
throw new RuntimeException(function + " failed: " + mEgl.eglGetError());
}
EGL10 mEgl;
EGLDisplay mEglDisplay;
EGLSurface mEglSurface;
@@ -959,181 +864,134 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
* to a Renderer instance to do the actual drawing. Can be configured to
* render continuously or on request.
*
* All potentially blocking synchronization is done through the
* sGLThreadManager object. This avoids multiple-lock ordering issues.
*
*/
class GLThread extends Thread implements SwapBuffersCallback {
GLThread(Renderer renderer) {
super();
mDone = false;
mWidth = 0;
mHeight = 0;
mRequestRender = true;
mRenderMode = RENDERMODE_CONTINUOUSLY;
mRenderer = renderer;
mRenderer.setSwapBuffersCallback(this);
setName("GLThread");
}
@Override
public void run() {
setName("GLThread " + getId());
if (LOG_THREADS) {
Log.i("GLThread", "starting tid=" + getId());
}
/*
* When the android framework launches a second instance of
* an activity, the new instance's onCreate() method may be
* called before the first instance returns from onDestroy().
*
* This semaphore ensures that only one instance at a time
* accesses EGL.
*/
try {
sEglSemaphore.acquire();
} catch (InterruptedException e) {
return;
}
mEglHelper = new EglHelper();
SwapBuffers();
SwapBuffers();
mEglHelper = new EglHelper();
// mEglHelper.start();
mNeedStart = true;
SwapBuffers();
SwapBuffers();
mRenderer.onDrawFrame(mGL);
mRenderer.onDrawFrame(mGL);
synchronized (sGLThreadManager) {
stopEglLocked();
}
sGLThreadManager.threadExiting(this);
}
/*
* This private method should only be called inside a
* synchronized(sGLThreadManager) block.
*/
private void stopEglLocked() {
if (mHaveEgl) {
mHaveEgl = false;
mEglHelper.destroySurface();
mEglHelper.finish();
sGLThreadManager.releaseEglSurfaceLocked(this);
}
/*
synchronized (sGLThreadManager) {
stopEglLocked();
}
sGLThreadManager.threadExiting(this);
*/
sEglSemaphore.release();
}
public boolean SwapBuffers() {
boolean tellRendererSurfaceCreated = false;
boolean tellRendererSurfaceChanged = false;
/*
* This is our main activity thread's loop, we go until
* asked to quit.
*/
try {
boolean createEglSurface = false;
boolean sizeChanged = false;
int w = 0;
int h = 0;
Runnable event = null;
synchronized (sGLThreadManager) {
while (true) {
if (mShouldExit) {
return false;
}
if (! mEventQueue.isEmpty()) {
event = mEventQueue.remove(0);
break;
}
// Do we need to release the EGL surface?
if (mHaveEgl && mPaused) {
if (LOG_SURFACE) {
Log.i("GLThread", "releasing EGL surface because paused tid=" + getId());
}
stopEglLocked();
}
// Have we lost the surface view surface?
if ((! mHasSurface) && (! mWaitingForSurface)) {
if (LOG_SURFACE) {
Log.i("GLThread", "noticed surfaceView surface lost tid=" + getId());
}
if (mHaveEgl) {
stopEglLocked();
}
mWaitingForSurface = true;
sGLThreadManager.notifyAll();
}
// Have we acquired the surface view surface?
if (mHasSurface && mWaitingForSurface) {
if (LOG_SURFACE) {
Log.i("GLThread", "noticed surfaceView surface acquired tid=" + getId());
}
mWaitingForSurface = false;
sGLThreadManager.notifyAll();
}
// Ready to draw?
if ((!mPaused) && mHasSurface
&& (mWidth > 0) && (mHeight > 0)
&& (mRequestRender || (mRenderMode == RENDERMODE_CONTINUOUSLY))) {
// If we don't have an egl surface, try to acquire one.
if ((! mHaveEgl) && sGLThreadManager.tryAcquireEglSurfaceLocked(this)) {
mHaveEgl = true;
mEglHelper.start();
createEglSurface = true;
sizeChanged = true;
sGLThreadManager.notifyAll();
}
if (mHaveEgl) {
if (mSizeChanged) {
sizeChanged = true;
w = mWidth;
h = mHeight;
if (DRAW_TWICE_AFTER_SIZE_CHANGED) {
// We keep mRequestRender true so that we draw twice after the size changes.
// (Once because of mSizeChanged, the second time because of mRequestRender.)
// This forces the updated graphics onto the screen.
} else {
mRequestRender = false;
}
mSizeChanged = false;
} else {
mRequestRender = false;
}
sGLThreadManager.notifyAll();
break;
}
}
// By design, this is the only place in a GLThread thread where we wait().
if (LOG_THREADS) {
Log.i("GLThread", "waiting tid=" + getId());
}
sGLThreadManager.wait();
}
} // end of synchronized(sGLThreadManager)
if (event != null) {
event.run();
event = null;
/*
* Update the asynchronous state (window size)
*/
int w, h;
boolean changed;
synchronized (this) {
Runnable r;
while ((r = getEvent()) != null) {
r.run();
}
if (createEglSurface) {
mGL = (GL10) mEglHelper.createSurface(getHolder());
if (LOG_RENDERER) {
Log.w("GLThread", "onSurfaceCreated");
}
mRenderer.onSurfaceCreated(mGL, mEglHelper.mEglConfig);
createEglSurface = false;
if (mPaused) {
mEglHelper.finish();
mNeedStart = true;
}
if (sizeChanged) {
if (LOG_RENDERER) {
Log.w("GLThread", "onSurfaceChanged(" + w + ", " + h + ")");
}
mRenderer.onSurfaceChanged(mGL, w, h);
sizeChanged = false;
while (needToWait()) {
wait();
}
if (LOG_RENDERER) {
Log.w("GLThread", "onDrawFrame");
}
if(!mEglHelper.swap()) {
if (LOG_SURFACE) {
Log.i("GLThread", "egl surface lost tid=" + getId());
return false;
}
if (mDone) {
return false;
}
changed = mSizeChanged;
w = mWidth;
h = mHeight;
mSizeChanged = false;
mRequestRender = false;
}
if (mNeedStart) {
mEglHelper.start();
tellRendererSurfaceCreated = true;
changed = true;
}
if (changed) {
mGL = (GL10) mEglHelper.createSurface(getHolder());
tellRendererSurfaceChanged = true;
}
if (tellRendererSurfaceCreated) {
mRenderer.onSurfaceCreated(mGL, mEglHelper.mEglConfig);
tellRendererSurfaceCreated = false;
}
if (tellRendererSurfaceChanged) {
mRenderer.onSurfaceChanged(mGL, w, h);
tellRendererSurfaceChanged = false;
}
/*
* Once we're done with GL, we need to call swapBuffers()
* to instruct the system to display the rendered frame
*/
return mEglHelper.swap();
} catch (InterruptedException e) {
} catch (java.lang.InterruptedException e) {
return false;
}
}
private boolean needToWait() {
if (mDone) {
return false;
}
if (mPaused || (! mHasSurface)) {
return true;
}
if ((mWidth > 0) && (mHeight > 0) && (mRequestRender || (mRenderMode == RENDERMODE_CONTINUOUSLY))) {
return false;
}
return true;
}
@@ -1141,90 +999,74 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
if ( !((RENDERMODE_WHEN_DIRTY <= renderMode) && (renderMode <= RENDERMODE_CONTINUOUSLY)) ) {
throw new IllegalArgumentException("renderMode");
}
synchronized(sGLThreadManager) {
synchronized(this) {
mRenderMode = renderMode;
sGLThreadManager.notifyAll();
if (renderMode == RENDERMODE_CONTINUOUSLY) {
notify();
}
}
}
public int getRenderMode() {
synchronized(sGLThreadManager) {
synchronized(this) {
return mRenderMode;
}
}
public void requestRender() {
synchronized(sGLThreadManager) {
synchronized(this) {
mRequestRender = true;
sGLThreadManager.notifyAll();
notify();
}
}
public void surfaceCreated() {
synchronized(sGLThreadManager) {
if (LOG_THREADS) {
Log.i("GLThread", "surfaceCreated tid=" + getId());
}
synchronized(this) {
mHasSurface = true;
sGLThreadManager.notifyAll();
notify();
}
}
public void surfaceDestroyed() {
synchronized(sGLThreadManager) {
if (LOG_THREADS) {
Log.i("GLThread", "surfaceDestroyed tid=" + getId());
}
synchronized(this) {
mHasSurface = false;
sGLThreadManager.notifyAll();
while((!mWaitingForSurface) && (!mExited)) {
try {
sGLThreadManager.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
notify();
}
}
public void onPause() {
synchronized (sGLThreadManager) {
synchronized (this) {
mPaused = true;
sGLThreadManager.notifyAll();
}
}
public void onResume() {
synchronized (sGLThreadManager) {
synchronized (this) {
mPaused = false;
mRequestRender = true;
sGLThreadManager.notifyAll();
notify();
}
}
public void onWindowResize(int w, int h) {
synchronized (sGLThreadManager) {
synchronized (this) {
mWidth = w;
mHeight = h;
mSizeChanged = true;
mRequestRender = true;
sGLThreadManager.notifyAll();
notify();
}
}
public void requestExitAndWait() {
// don't call this from GLThread thread or it is a guaranteed
// deadlock!
synchronized(sGLThreadManager) {
mShouldExit = true;
sGLThreadManager.notifyAll();
while (! mExited) {
try {
sGLThreadManager.wait();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
synchronized(this) {
mDone = true;
notify();
}
try {
join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
@@ -1233,33 +1075,33 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
* @param r the runnable to be run on the GL rendering thread.
*/
public void queueEvent(Runnable r) {
if (r == null) {
throw new IllegalArgumentException("r must not be null");
}
synchronized(sGLThreadManager) {
synchronized(this) {
mEventQueue.add(r);
sGLThreadManager.notifyAll();
}
}
// Once the thread is started, all accesses to the following member
// variables are protected by the sGLThreadManager monitor
private boolean mShouldExit;
private boolean mExited;
private Runnable getEvent() {
synchronized(this) {
if (mEventQueue.size() > 0) {
return mEventQueue.remove(0);
}
}
return null;
}
private boolean mDone;
private boolean mPaused;
private boolean mHasSurface;
private boolean mWaitingForSurface;
private boolean mHaveEgl;
private int mWidth;
private int mHeight;
private int mRenderMode;
private boolean mRequestRender;
private ArrayList<Runnable> mEventQueue = new ArrayList<Runnable>();
// End of member variables protected by the sGLThreadManager monitor.
private Renderer mRenderer;
private ArrayList<Runnable> mEventQueue = new ArrayList<Runnable>();
private EglHelper mEglHelper;
private GL10 mGL = null;
private boolean mNeedStart = false;
}
static class LogWriter extends Writer {
@@ -1294,62 +1136,11 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
private StringBuilder mBuilder = new StringBuilder();
}
private void checkRenderThreadState() {
if (mGLThread != null) {
throw new IllegalStateException(
"setRenderer has already been called for this instance.");
}
}
private static class GLThreadManager {
public synchronized void threadExiting(GLThread thread) {
if (LOG_THREADS) {
Log.i("GLThread", "exiting tid=" + thread.getId());
}
thread.mExited = true;
if (mEglOwner == thread) {
mEglOwner = null;
}
notifyAll();
}
/*
* Tries once to acquire the right to use an EGL
* surface. Does not block. Requires that we are already
* in the sGLThreadManager monitor when this is called.
* @return true if the right to use an EGL surface was acquired.
*/
public boolean tryAcquireEglSurfaceLocked(GLThread thread) {
if (mEglOwner == thread || mEglOwner == null) {
mEglOwner = thread;
notifyAll();
return true;
}
return false;
}
/*
* Releases the EGL surface. Requires that we are already in the
* sGLThreadManager monitor when this is called.
*/
public void releaseEglSurfaceLocked(GLThread thread) {
if (mEglOwner == thread) {
mEglOwner = null;
}
notifyAll();
}
private GLThread mEglOwner;
}
private static final GLThreadManager sGLThreadManager = new GLThreadManager();
private static final Semaphore sEglSemaphore = new Semaphore(1);
private boolean mSizeChanged = true;
private GLThread mGLThread;
private EGLConfigChooser mEGLConfigChooser;
private EGLContextFactory mEGLContextFactory;
private EGLWindowSurfaceFactory mEGLWindowSurfaceFactory;
private GLWrapper mGLWrapper;
private int mDebugFlags;
}