From 983e16b36a68292021833aa9b69305a37ffe2f91 Mon Sep 17 00:00:00 2001 From: dhewg Date: Sat, 26 Feb 2011 12:51:13 +0100 Subject: ANDROID: Formatting --- .../android/org/inodes/gus/scummvm/ScummVM.java | 128 ++++++++++++++------- 1 file changed, 86 insertions(+), 42 deletions(-) (limited to 'backends/platform/android/org/inodes/gus/scummvm/ScummVM.java') diff --git a/backends/platform/android/org/inodes/gus/scummvm/ScummVM.java b/backends/platform/android/org/inodes/gus/scummvm/ScummVM.java index 0e905f43a5..698e508605 100644 --- a/backends/platform/android/org/inodes/gus/scummvm/ScummVM.java +++ b/backends/platform/android/org/inodes/gus/scummvm/ScummVM.java @@ -35,16 +35,19 @@ import java.util.LinkedHashMap; public class ScummVM implements SurfaceHolder.Callback { protected final static String LOG_TAG = "ScummVM"; - private final int AUDIO_FRAME_SIZE = 2 * 2; // bytes. 16bit audio * stereo + // bytes. 16bit audio * stereo + private final int AUDIO_FRAME_SIZE = 2 * 2; public static class AudioSetupException extends Exception {} - private long nativeScummVM; // native code hangs itself here + // native code hangs itself here + private long nativeScummVM; boolean scummVMRunning = false; private native void create(AssetManager am); public ScummVM(Context context) { - create(context.getAssets()); // Init C++ code, set nativeScummVM + // Init C++ code, set nativeScummVM + create(context.getAssets()); } private native void nativeDestroy(); @@ -55,6 +58,7 @@ public class ScummVM implements SurfaceHolder.Callback { nativeScummVM = 0; } } + protected void finalize() { destroy(); } @@ -62,17 +66,17 @@ public class ScummVM implements SurfaceHolder.Callback { // Surface creation: // GUI thread: create surface, release lock // ScummVM thread: acquire lock (block), read surface - // + // Surface deletion: // GUI thread: post event, acquire lock (block), return // ScummVM thread: read event, free surface, release lock - // + // In other words, ScummVM thread does this: // acquire lock // setup surface // when SCREEN_CHANGED arrives: - // destroy surface - // release lock + // destroy surface + // release lock // back to acquire lock static final int configSpec[] = { EGL10.EGL_RED_SIZE, 5, @@ -82,6 +86,7 @@ public class ScummVM implements SurfaceHolder.Callback { EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT, EGL10.EGL_NONE, }; + EGL10 egl; EGLDisplay eglDisplay = EGL10.EGL_NO_DISPLAY; EGLConfig eglConfig; @@ -96,7 +101,7 @@ public class ScummVM implements SurfaceHolder.Callback { } public void surfaceChanged(SurfaceHolder holder, int format, - int width, int height) { + int width, int height) { // Disabled while I debug GL problems pushEvent(new Event(Event.EVENT_SCREEN_CHANGED)); } @@ -111,6 +116,7 @@ public class ScummVM implements SurfaceHolder.Callback { // For debugging private static final Map attribs; + static { attribs = new LinkedHashMap(); attribs.put("CONFIG_ID", EGL10.EGL_CONFIG_ID); @@ -141,11 +147,14 @@ public class ScummVM implements SurfaceHolder.Callback { attribs.put("TRANSPARENT_GREEN_VALUE", EGL10.EGL_TRANSPARENT_GREEN_VALUE); attribs.put("TRANSPARENT_BLUE_VALUE", EGL10.EGL_TRANSPARENT_BLUE_VALUE); } + private void dumpEglConfig(EGLConfig config) { int[] value = new int[1]; + for (Map.Entry entry : attribs.entrySet()) { egl.eglGetConfigAttrib(eglDisplay, config, - entry.getValue(), value); + entry.getValue(), value); + if (value[0] == EGL10.EGL_NONE) Log.d(LOG_TAG, entry.getKey() + ": NONE"); else @@ -157,12 +166,15 @@ public class ScummVM implements SurfaceHolder.Callback { private void createScummVMGLContext() { egl = (EGL10)EGLContext.getEGL(); eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); + int[] version = new int[2]; egl.eglInitialize(eglDisplay, version); + int[] num_config = new int[1]; egl.eglChooseConfig(eglDisplay, configSpec, null, 0, num_config); final int numConfigs = num_config[0]; + if (numConfigs <= 0) throw new IllegalArgumentException("No configs match configSpec"); @@ -179,13 +191,15 @@ public class ScummVM implements SurfaceHolder.Callback { // Android's eglChooseConfig is busted in several versions and // devices so we have to filter/rank the configs again ourselves. eglConfig = chooseEglConfig(configs); + if (false) { Log.d(LOG_TAG, String.format("Chose EGL config from %d possibilities.", numConfigs)); dumpEglConfig(eglConfig); } eglContext = egl.eglCreateContext(eglDisplay, eglConfig, - EGL10.EGL_NO_CONTEXT, null); + EGL10.EGL_NO_CONTEXT, null); + if (eglContext == EGL10.EGL_NO_CONTEXT) throw new RuntimeException("Failed to create context"); } @@ -198,32 +212,43 @@ public class ScummVM implements SurfaceHolder.Callback { for (int i = 0; i < configs.length; i++) { EGLConfig config = configs[i]; int score = 10000; + egl.eglGetConfigAttrib(eglDisplay, config, - EGL10.EGL_SURFACE_TYPE, value); + EGL10.EGL_SURFACE_TYPE, value); + + // must have if ((value[0] & EGL10.EGL_WINDOW_BIT) == 0) - continue; // must have + continue; egl.eglGetConfigAttrib(eglDisplay, config, - EGL10.EGL_CONFIG_CAVEAT, value); + EGL10.EGL_CONFIG_CAVEAT, value); + if (value[0] != EGL10.EGL_NONE) score -= 1000; // Must be at least 555, but then smaller is better - final int[] colorBits = {EGL10.EGL_RED_SIZE, - EGL10.EGL_GREEN_SIZE, - EGL10.EGL_BLUE_SIZE, - EGL10.EGL_ALPHA_SIZE}; + final int[] colorBits = { EGL10.EGL_RED_SIZE, + EGL10.EGL_GREEN_SIZE, + EGL10.EGL_BLUE_SIZE, + EGL10.EGL_ALPHA_SIZE + }; + for (int component : colorBits) { - egl.eglGetConfigAttrib(eglDisplay, config, - component, value); + egl.eglGetConfigAttrib(eglDisplay, config, component, value); + + // boost if >5 bits accuracy if (value[0] >= 5) - score += 10; // boost if >5 bits accuracy - score -= value[0]; // penalize for wasted bits + score += 10; + + // penalize for wasted bits + score -= value[0]; } egl.eglGetConfigAttrib(eglDisplay, config, - EGL10.EGL_DEPTH_SIZE, value); - score -= value[0]; // penalize for wasted bits + EGL10.EGL_DEPTH_SIZE, value); + + // penalize for wasted bits + score -= value[0]; if (score > bestScore) { best = i; @@ -248,28 +273,35 @@ public class ScummVM implements SurfaceHolder.Callback { Log.e(LOG_TAG, "Interrupted while waiting for surface lock", e); return; } + eglSurface = egl.eglCreateWindowSurface(eglDisplay, eglConfig, nativeSurface, null); + if (eglSurface == EGL10.EGL_NO_SURFACE) Log.e(LOG_TAG, "CreateWindowSurface failed!"); + egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext); GL10 gl = (GL10)eglContext.getGL(); if (_log_version) { Log.i(LOG_TAG, String.format("Using EGL %s (%s); GL %s/%s (%s)", - egl.eglQueryString(eglDisplay, EGL10.EGL_VERSION), - egl.eglQueryString(eglDisplay, EGL10.EGL_VENDOR), - gl.glGetString(GL10.GL_VERSION), - gl.glGetString(GL10.GL_RENDERER), - gl.glGetString(GL10.GL_VENDOR))); - _log_version = false; // only log this once + egl.eglQueryString(eglDisplay, EGL10.EGL_VERSION), + egl.eglQueryString(eglDisplay, EGL10.EGL_VENDOR), + gl.glGetString(GL10.GL_VERSION), + gl.glGetString(GL10.GL_RENDERER), + gl.glGetString(GL10.GL_VENDOR))); + + // only log this once + _log_version = false; } int[] value = new int[1]; egl.eglQuerySurface(eglDisplay, eglSurface, EGL10.EGL_WIDTH, value); + int width = value[0]; egl.eglQuerySurface(eglDisplay, eglSurface, EGL10.EGL_HEIGHT, value); + int height = value[0]; Log.i(LOG_TAG, String.format("New surface is %dx%d", width, height)); setSurfaceSize(width, height); @@ -279,7 +311,8 @@ public class ScummVM implements SurfaceHolder.Callback { protected void destroyScummVMSurface() { if (eglSurface != null) { egl.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE, - EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); + EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); + egl.eglDestroySurface(eglDisplay, eglSurface); eglSurface = EGL10.EGL_NO_SURFACE; } @@ -298,6 +331,7 @@ public class ScummVM implements SurfaceHolder.Callback { if (error == EGL11.EGL_CONTEXT_LOST) return false; } + return true; } @@ -324,6 +358,7 @@ public class ScummVM implements SurfaceHolder.Callback { protected void showVirtualKeyboard(boolean enable) {} protected String[] getSysArchives() { return new String[0]; } protected String[] getPluginDirectories() { return new String[0]; } + protected void initBackend() throws AudioSetupException { createScummVMGLContext(); initAudio(); @@ -375,17 +410,19 @@ public class ScummVM implements SurfaceHolder.Callback { scummvm.audioMixCallback(buf); offset = 0; } + int len = buf.length - offset; int ret = audio_track.write(buf, offset, len); if (ret < 0) { Log.w(LOG_TAG, String.format( - "AudioTrack.write(%dB) returned error %d", - buf.length, ret)); + "AudioTrack.write(%dB) returned error %d", + buf.length, ret)); break; } else if (ret != len) { Log.w(LOG_TAG, String.format( - "Short audio write. Wrote %dB, not %dB", - ret, buf.length)); + "Short audio write. Wrote %dB, not %dB", + ret, buf.length)); + // Buffer is full, so yield cpu for a while Thread.sleep(100); } @@ -409,21 +446,27 @@ public class ScummVM implements SurfaceHolder.Callback { AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT); if (buf_size < 0) { - int guess = AUDIO_FRAME_SIZE * sample_rate / 100; // 10ms of audio + // 10ms of audio + int guess = AUDIO_FRAME_SIZE * sample_rate / 100; + Log.w(LOG_TAG, String.format( "Unable to get min audio buffer size (error %d). Guessing %dB.", buf_size, guess)); + buf_size = guess; } + Log.d(LOG_TAG, String.format("Using %dB buffer for %dHZ audio", - buf_size, sample_rate)); + buf_size, sample_rate)); + AudioTrack audio_track = new AudioTrack(AudioManager.STREAM_MUSIC, - sample_rate, - AudioFormat.CHANNEL_CONFIGURATION_STEREO, - AudioFormat.ENCODING_PCM_16BIT, - buf_size, - AudioTrack.MODE_STREAM); + sample_rate, + AudioFormat.CHANNEL_CONFIGURATION_STEREO, + AudioFormat.ENCODING_PCM_16BIT, + buf_size, + AudioTrack.MODE_STREAM); + if (audio_track.getState() != AudioTrack.STATE_INITIALIZED) { Log.e(LOG_TAG, "Error initialising Android audio system."); throw new AudioSetupException(); @@ -448,7 +491,7 @@ public class ScummVM implements SurfaceHolder.Callback { final boolean sleep_for_debugger = false; if (sleep_for_debugger) { try { - Thread.sleep(20*1000); + Thread.sleep(20 * 1000); } catch (InterruptedException e) { } } @@ -460,3 +503,4 @@ public class ScummVM implements SurfaceHolder.Callback { System.load(libpath.getPath()); } } + -- cgit v1.2.3