Copied GLSurfaceView from Android 1.6 - now it works with 1.6 but fails with 2.1
This commit is contained in:
@@ -18,10 +18,11 @@
|
|||||||
fixed with a hammer and rasp to work with libSDL port */
|
fixed with a hammer and rasp to work with libSDL port */
|
||||||
|
|
||||||
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
|
// 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.io.Writer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
import javax.microedition.khronos.egl.EGL10;
|
import javax.microedition.khronos.egl.EGL10;
|
||||||
import javax.microedition.khronos.egl.EGL11;
|
import javax.microedition.khronos.egl.EGL11;
|
||||||
@@ -148,11 +149,6 @@ import android.view.SurfaceView;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Callback {
|
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
|
* The renderer only renders
|
||||||
* when the surface is created, or when {@link #requestRender} is called.
|
* 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
|
// underlying surface is created and destroyed
|
||||||
SurfaceHolder holder = getHolder();
|
SurfaceHolder holder = getHolder();
|
||||||
holder.addCallback(this);
|
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.
|
* @param renderer the renderer to use to perform OpenGL drawing.
|
||||||
*/
|
*/
|
||||||
public void setRenderer(Renderer renderer) {
|
public void setRenderer(Renderer renderer) {
|
||||||
checkRenderThreadState();
|
if (mGLThread != null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"setRenderer has already been called for this instance.");
|
||||||
|
}
|
||||||
if (mEGLConfigChooser == null) {
|
if (mEGLConfigChooser == null) {
|
||||||
mEGLConfigChooser = new SimpleEGLConfigChooser(true);
|
mEGLConfigChooser = new SimpleEGLConfigChooser(true);
|
||||||
}
|
}
|
||||||
if (mEGLContextFactory == null) {
|
|
||||||
mEGLContextFactory = new DefaultContextFactory();
|
|
||||||
}
|
|
||||||
if (mEGLWindowSurfaceFactory == null) {
|
|
||||||
mEGLWindowSurfaceFactory = new DefaultWindowSurfaceFactory();
|
|
||||||
}
|
|
||||||
mGLThread = new GLThread(renderer);
|
mGLThread = new GLThread(renderer);
|
||||||
mGLThread.start();
|
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.
|
* Install a custom EGLConfigChooser.
|
||||||
* <p>If this method is
|
* <p>If this method is
|
||||||
@@ -334,7 +299,10 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
|
|||||||
* @param configChooser
|
* @param configChooser
|
||||||
*/
|
*/
|
||||||
public void setEGLConfigChooser(EGLConfigChooser configChooser) {
|
public void setEGLConfigChooser(EGLConfigChooser configChooser) {
|
||||||
checkRenderThreadState();
|
if (mGLThread != null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"setRenderer has already been called for this instance.");
|
||||||
|
}
|
||||||
mEGLConfigChooser = configChooser;
|
mEGLConfigChooser = configChooser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -630,54 +598,6 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
|
|||||||
private SwapBuffersCallback mSwapBuffersCallback = null;
|
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
|
* An interface for choosing an EGLConfig configuration from a list of
|
||||||
* potential configurations.
|
* potential configurations.
|
||||||
@@ -756,27 +676,25 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
|
|||||||
EGLConfig closestConfig = null;
|
EGLConfig closestConfig = null;
|
||||||
int closestDistance = 1000;
|
int closestDistance = 1000;
|
||||||
for(EGLConfig config : configs) {
|
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,
|
int d = findConfigAttrib(egl, display, config,
|
||||||
EGL10.EGL_DEPTH_SIZE, 0);
|
EGL10.EGL_DEPTH_SIZE, 0);
|
||||||
int s = findConfigAttrib(egl, display, config,
|
int s = findConfigAttrib(egl, display, config,
|
||||||
EGL10.EGL_STENCIL_SIZE, 0);
|
EGL10.EGL_STENCIL_SIZE, 0);
|
||||||
if (d >= mDepthSize && s>= mStencilSize) {
|
int distance = Math.abs(r - mRedSize)
|
||||||
int r = findConfigAttrib(egl, display, config,
|
+ Math.abs(g - mGreenSize)
|
||||||
EGL10.EGL_RED_SIZE, 0);
|
+ Math.abs(b - mBlueSize) + Math.abs(a - mAlphaSize)
|
||||||
int g = findConfigAttrib(egl, display, config,
|
+ Math.abs(d - mDepthSize) + Math.abs(s - mStencilSize);
|
||||||
EGL10.EGL_GREEN_SIZE, 0);
|
if (distance < closestDistance) {
|
||||||
int b = findConfigAttrib(egl, display, config,
|
closestDistance = distance;
|
||||||
EGL10.EGL_BLUE_SIZE, 0);
|
closestConfig = config;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return closestConfig;
|
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
|
* Create an OpenGL ES context. This must be done only once, an
|
||||||
* OpenGL context is a somewhat heavy object.
|
* OpenGL context is a somewhat heavy object.
|
||||||
*/
|
*/
|
||||||
mEglContext = mEGLContextFactory.createContext(mEgl, mEglDisplay, mEglConfig);
|
mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig,
|
||||||
if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
|
EGL10.EGL_NO_CONTEXT, null);
|
||||||
throw new RuntimeException("createContext failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
mEglSurface = 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
|
* The window size has changed, so we need to create a new
|
||||||
* surface.
|
* surface.
|
||||||
*/
|
*/
|
||||||
if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
|
if (mEglSurface != null) {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Unbind and destroy the old EGL surface, if
|
* 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,
|
mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
|
||||||
EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
|
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.
|
* Create an EGL surface we can render into.
|
||||||
*/
|
*/
|
||||||
mEglSurface = mEGLWindowSurfaceFactory.createWindowSurface(mEgl,
|
mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,
|
||||||
mEglDisplay, mEglConfig, holder);
|
mEglConfig, holder, null);
|
||||||
|
|
||||||
if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
|
|
||||||
throwEglException("createWindowSurface");
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Before we can issue GL commands, we need to make sure
|
* Before we can issue GL commands, we need to make sure
|
||||||
* the context is current and bound to a surface.
|
* the context is current and bound to a surface.
|
||||||
*/
|
*/
|
||||||
if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
|
mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
|
||||||
throwEglException("eglMakeCurrent");
|
mEglContext);
|
||||||
}
|
|
||||||
|
|
||||||
GL gl = mEglContext.getGL();
|
GL gl = mEglContext.getGL();
|
||||||
if (mGLWrapper != null) {
|
if (mGLWrapper != null) {
|
||||||
@@ -922,19 +834,16 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
|
|||||||
return mEgl.eglGetError() != EGL11.EGL_CONTEXT_LOST;
|
return mEgl.eglGetError() != EGL11.EGL_CONTEXT_LOST;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void destroySurface() {
|
public void finish() {
|
||||||
if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
|
if (mEglSurface != null) {
|
||||||
mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
|
mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
|
||||||
EGL10.EGL_NO_SURFACE,
|
EGL10.EGL_NO_SURFACE,
|
||||||
EGL10.EGL_NO_CONTEXT);
|
EGL10.EGL_NO_CONTEXT);
|
||||||
mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface);
|
mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
|
||||||
mEglSurface = null;
|
mEglSurface = null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void finish() {
|
|
||||||
if (mEglContext != null) {
|
if (mEglContext != null) {
|
||||||
mEGLContextFactory.destroyContext(mEgl, mEglDisplay, mEglContext);
|
mEgl.eglDestroyContext(mEglDisplay, mEglContext);
|
||||||
mEglContext = null;
|
mEglContext = null;
|
||||||
}
|
}
|
||||||
if (mEglDisplay != 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;
|
EGL10 mEgl;
|
||||||
EGLDisplay mEglDisplay;
|
EGLDisplay mEglDisplay;
|
||||||
EGLSurface mEglSurface;
|
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
|
* to a Renderer instance to do the actual drawing. Can be configured to
|
||||||
* render continuously or on request.
|
* 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 {
|
class GLThread extends Thread implements SwapBuffersCallback {
|
||||||
GLThread(Renderer renderer) {
|
GLThread(Renderer renderer) {
|
||||||
super();
|
super();
|
||||||
|
mDone = false;
|
||||||
mWidth = 0;
|
mWidth = 0;
|
||||||
mHeight = 0;
|
mHeight = 0;
|
||||||
mRequestRender = true;
|
mRequestRender = true;
|
||||||
mRenderMode = RENDERMODE_CONTINUOUSLY;
|
mRenderMode = RENDERMODE_CONTINUOUSLY;
|
||||||
mRenderer = renderer;
|
mRenderer = renderer;
|
||||||
mRenderer.setSwapBuffersCallback(this);
|
mRenderer.setSwapBuffersCallback(this);
|
||||||
|
setName("GLThread");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
setName("GLThread " + getId());
|
/*
|
||||||
if (LOG_THREADS) {
|
* When the android framework launches a second instance of
|
||||||
Log.i("GLThread", "starting tid=" + getId());
|
* 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();
|
mEglHelper = new EglHelper();
|
||||||
SwapBuffers();
|
// mEglHelper.start();
|
||||||
SwapBuffers();
|
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();
|
mEglHelper.finish();
|
||||||
sGLThreadManager.releaseEglSurfaceLocked(this);
|
|
||||||
}
|
/*
|
||||||
|
synchronized (sGLThreadManager) {
|
||||||
|
stopEglLocked();
|
||||||
|
}
|
||||||
|
sGLThreadManager.threadExiting(this);
|
||||||
|
*/
|
||||||
|
|
||||||
|
sEglSemaphore.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean SwapBuffers() {
|
public boolean SwapBuffers() {
|
||||||
|
|
||||||
|
boolean tellRendererSurfaceCreated = false;
|
||||||
|
boolean tellRendererSurfaceChanged = false;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is our main activity thread's loop, we go until
|
||||||
|
* asked to quit.
|
||||||
|
*/
|
||||||
try {
|
try {
|
||||||
|
|
||||||
boolean createEglSurface = false;
|
|
||||||
boolean sizeChanged = false;
|
|
||||||
int w = 0;
|
|
||||||
int h = 0;
|
|
||||||
Runnable event = null;
|
|
||||||
|
|
||||||
synchronized (sGLThreadManager) {
|
/*
|
||||||
while (true) {
|
* Update the asynchronous state (window size)
|
||||||
if (mShouldExit) {
|
*/
|
||||||
return false;
|
int w, h;
|
||||||
}
|
boolean changed;
|
||||||
|
synchronized (this) {
|
||||||
if (! mEventQueue.isEmpty()) {
|
Runnable r;
|
||||||
event = mEventQueue.remove(0);
|
while ((r = getEvent()) != null) {
|
||||||
break;
|
r.run();
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
if (mPaused) {
|
||||||
if (createEglSurface) {
|
mEglHelper.finish();
|
||||||
mGL = (GL10) mEglHelper.createSurface(getHolder());
|
mNeedStart = true;
|
||||||
if (LOG_RENDERER) {
|
|
||||||
Log.w("GLThread", "onSurfaceCreated");
|
|
||||||
}
|
|
||||||
mRenderer.onSurfaceCreated(mGL, mEglHelper.mEglConfig);
|
|
||||||
createEglSurface = false;
|
|
||||||
}
|
}
|
||||||
|
while (needToWait()) {
|
||||||
if (sizeChanged) {
|
wait();
|
||||||
if (LOG_RENDERER) {
|
|
||||||
Log.w("GLThread", "onSurfaceChanged(" + w + ", " + h + ")");
|
|
||||||
}
|
|
||||||
mRenderer.onSurfaceChanged(mGL, w, h);
|
|
||||||
sizeChanged = false;
|
|
||||||
}
|
}
|
||||||
|
if (mDone) {
|
||||||
if (LOG_RENDERER) {
|
return false;
|
||||||
Log.w("GLThread", "onDrawFrame");
|
|
||||||
}
|
|
||||||
if(!mEglHelper.swap()) {
|
|
||||||
if (LOG_SURFACE) {
|
|
||||||
Log.i("GLThread", "egl surface lost tid=" + getId());
|
|
||||||
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mPaused || (! mHasSurface)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((mWidth > 0) && (mHeight > 0) && (mRequestRender || (mRenderMode == RENDERMODE_CONTINUOUSLY))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1141,90 +999,74 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
|
|||||||
if ( !((RENDERMODE_WHEN_DIRTY <= renderMode) && (renderMode <= RENDERMODE_CONTINUOUSLY)) ) {
|
if ( !((RENDERMODE_WHEN_DIRTY <= renderMode) && (renderMode <= RENDERMODE_CONTINUOUSLY)) ) {
|
||||||
throw new IllegalArgumentException("renderMode");
|
throw new IllegalArgumentException("renderMode");
|
||||||
}
|
}
|
||||||
synchronized(sGLThreadManager) {
|
synchronized(this) {
|
||||||
mRenderMode = renderMode;
|
mRenderMode = renderMode;
|
||||||
sGLThreadManager.notifyAll();
|
if (renderMode == RENDERMODE_CONTINUOUSLY) {
|
||||||
|
notify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRenderMode() {
|
public int getRenderMode() {
|
||||||
synchronized(sGLThreadManager) {
|
synchronized(this) {
|
||||||
return mRenderMode;
|
return mRenderMode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestRender() {
|
public void requestRender() {
|
||||||
synchronized(sGLThreadManager) {
|
synchronized(this) {
|
||||||
mRequestRender = true;
|
mRequestRender = true;
|
||||||
sGLThreadManager.notifyAll();
|
notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated() {
|
public void surfaceCreated() {
|
||||||
synchronized(sGLThreadManager) {
|
synchronized(this) {
|
||||||
if (LOG_THREADS) {
|
|
||||||
Log.i("GLThread", "surfaceCreated tid=" + getId());
|
|
||||||
}
|
|
||||||
mHasSurface = true;
|
mHasSurface = true;
|
||||||
sGLThreadManager.notifyAll();
|
notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed() {
|
public void surfaceDestroyed() {
|
||||||
synchronized(sGLThreadManager) {
|
synchronized(this) {
|
||||||
if (LOG_THREADS) {
|
|
||||||
Log.i("GLThread", "surfaceDestroyed tid=" + getId());
|
|
||||||
}
|
|
||||||
mHasSurface = false;
|
mHasSurface = false;
|
||||||
sGLThreadManager.notifyAll();
|
notify();
|
||||||
while((!mWaitingForSurface) && (!mExited)) {
|
|
||||||
try {
|
|
||||||
sGLThreadManager.wait();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
synchronized (sGLThreadManager) {
|
synchronized (this) {
|
||||||
mPaused = true;
|
mPaused = true;
|
||||||
sGLThreadManager.notifyAll();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
synchronized (sGLThreadManager) {
|
synchronized (this) {
|
||||||
mPaused = false;
|
mPaused = false;
|
||||||
mRequestRender = true;
|
notify();
|
||||||
sGLThreadManager.notifyAll();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onWindowResize(int w, int h) {
|
public void onWindowResize(int w, int h) {
|
||||||
synchronized (sGLThreadManager) {
|
synchronized (this) {
|
||||||
mWidth = w;
|
mWidth = w;
|
||||||
mHeight = h;
|
mHeight = h;
|
||||||
mSizeChanged = true;
|
mSizeChanged = true;
|
||||||
mRequestRender = true;
|
notify();
|
||||||
sGLThreadManager.notifyAll();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestExitAndWait() {
|
public void requestExitAndWait() {
|
||||||
// don't call this from GLThread thread or it is a guaranteed
|
// don't call this from GLThread thread or it is a guaranteed
|
||||||
// deadlock!
|
// deadlock!
|
||||||
synchronized(sGLThreadManager) {
|
synchronized(this) {
|
||||||
mShouldExit = true;
|
mDone = true;
|
||||||
sGLThreadManager.notifyAll();
|
notify();
|
||||||
while (! mExited) {
|
}
|
||||||
try {
|
try {
|
||||||
sGLThreadManager.wait();
|
join();
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
Thread.currentThread().interrupt();
|
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.
|
* @param r the runnable to be run on the GL rendering thread.
|
||||||
*/
|
*/
|
||||||
public void queueEvent(Runnable r) {
|
public void queueEvent(Runnable r) {
|
||||||
if (r == null) {
|
synchronized(this) {
|
||||||
throw new IllegalArgumentException("r must not be null");
|
|
||||||
}
|
|
||||||
synchronized(sGLThreadManager) {
|
|
||||||
mEventQueue.add(r);
|
mEventQueue.add(r);
|
||||||
sGLThreadManager.notifyAll();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Once the thread is started, all accesses to the following member
|
private Runnable getEvent() {
|
||||||
// variables are protected by the sGLThreadManager monitor
|
synchronized(this) {
|
||||||
private boolean mShouldExit;
|
if (mEventQueue.size() > 0) {
|
||||||
private boolean mExited;
|
return mEventQueue.remove(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean mDone;
|
||||||
private boolean mPaused;
|
private boolean mPaused;
|
||||||
private boolean mHasSurface;
|
private boolean mHasSurface;
|
||||||
private boolean mWaitingForSurface;
|
|
||||||
private boolean mHaveEgl;
|
|
||||||
private int mWidth;
|
private int mWidth;
|
||||||
private int mHeight;
|
private int mHeight;
|
||||||
private int mRenderMode;
|
private int mRenderMode;
|
||||||
private boolean mRequestRender;
|
private boolean mRequestRender;
|
||||||
private ArrayList<Runnable> mEventQueue = new ArrayList<Runnable>();
|
|
||||||
// End of member variables protected by the sGLThreadManager monitor.
|
|
||||||
|
|
||||||
private Renderer mRenderer;
|
private Renderer mRenderer;
|
||||||
|
private ArrayList<Runnable> mEventQueue = new ArrayList<Runnable>();
|
||||||
private EglHelper mEglHelper;
|
private EglHelper mEglHelper;
|
||||||
private GL10 mGL = null;
|
private GL10 mGL = null;
|
||||||
|
private boolean mNeedStart = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class LogWriter extends Writer {
|
static class LogWriter extends Writer {
|
||||||
@@ -1294,62 +1136,11 @@ public class GLSurfaceView_SDL extends SurfaceView implements SurfaceHolder.Call
|
|||||||
private StringBuilder mBuilder = new StringBuilder();
|
private StringBuilder mBuilder = new StringBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Semaphore sEglSemaphore = new Semaphore(1);
|
||||||
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 boolean mSizeChanged = true;
|
private boolean mSizeChanged = true;
|
||||||
|
|
||||||
private GLThread mGLThread;
|
private GLThread mGLThread;
|
||||||
private EGLConfigChooser mEGLConfigChooser;
|
private EGLConfigChooser mEGLConfigChooser;
|
||||||
private EGLContextFactory mEGLContextFactory;
|
|
||||||
private EGLWindowSurfaceFactory mEGLWindowSurfaceFactory;
|
|
||||||
private GLWrapper mGLWrapper;
|
private GLWrapper mGLWrapper;
|
||||||
private int mDebugFlags;
|
private int mDebugFlags;
|
||||||
}
|
}
|
||||||
|
|||||||
1146
alienblaster/project/src_1.6/GLSurfaceView_SDL.java
Normal file
1146
alienblaster/project/src_1.6/GLSurfaceView_SDL.java
Normal file
File diff suppressed because it is too large
Load Diff
1355
alienblaster/project/src_2.1/GLSurfaceView_SDL.java
Normal file
1355
alienblaster/project/src_2.1/GLSurfaceView_SDL.java
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user