From 26a187c320c52b9b66f64cdce8e71954d9475714 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 9 Aug 2011 20:20:04 +0200 Subject: NEWT Android Display/Screen driver implementation --- .../jogamp/newt/driver/android/AndroidDisplay.java | 51 ++++++++---- .../jogamp/newt/driver/android/AndroidScreen.java | 90 ++++++++++++++++++++-- 2 files changed, 117 insertions(+), 24 deletions(-) (limited to 'src/newt/classes/jogamp') diff --git a/src/newt/classes/jogamp/newt/driver/android/AndroidDisplay.java b/src/newt/classes/jogamp/newt/driver/android/AndroidDisplay.java index e1944276f..1ba40f189 100644 --- a/src/newt/classes/jogamp/newt/driver/android/AndroidDisplay.java +++ b/src/newt/classes/jogamp/newt/driver/android/AndroidDisplay.java @@ -30,19 +30,20 @@ package jogamp.newt.driver.android; import jogamp.newt.*; import jogamp.opengl.egl.*; + import javax.media.nativewindow.*; import javax.media.nativewindow.egl.*; -import android.content.Context; +import javax.media.opengl.GLException; -public class Display extends jogamp.newt.DisplayImpl { +import android.content.Context; +import android.view.Surface; - /*package*/ Context appContext; - +public class AndroidDisplay extends jogamp.newt.DisplayImpl { static { NEWTJNILibLoader.loadNEWT(); - if (!Window.initIDs()) { - throw new NativeWindowException("Failed to initialize BCEGL Window jmethodIDs"); + if (!AndroidWindow.initIDs()) { + throw new NativeWindowException("Failed to initialize Android NEWT Windowing library"); } } @@ -51,29 +52,47 @@ public class Display extends jogamp.newt.DisplayImpl { } - public Display() { + public AndroidDisplay() { } protected void createNativeImpl() { - long handle = CreateDisplay(Screen.fixedWidth, Screen.fixedHeight); - if (handle == EGL.EGL_NO_DISPLAY) { - throw new NativeWindowException("BC EGL CreateDisplay failed"); + // EGL Device + // final long eglDisplay = EGL.eglGetDisplay(EGL.EGL_DEFAULT_DISPLAY); + final long eglDisplay = EGL.eglGetDisplay(EGL.EGL_DEFAULT_DISPLAY); + if (eglDisplay == EGL.EGL_NO_DISPLAY) { + throw new GLException("Failed to created EGL default display: error 0x"+Integer.toHexString(EGL.eglGetError())); + } else if(DEBUG) { + System.err.println("Android Display.createNativeImpl: eglDisplay(EGL_DEFAULT_DISPLAY): 0x"+Long.toHexString(eglDisplay)); + } + if (!EGL.eglInitialize(eglDisplay, null, null)) { + throw new GLException("eglInitialize failed eglDisplay 0x"+Long.toHexString(eglDisplay)+", error 0x"+Integer.toHexString(EGL.eglGetError())); } - aDevice = new EGLGraphicsDevice(handle, AbstractGraphicsDevice.DEFAULT_CONNECTION, AbstractGraphicsDevice.DEFAULT_UNIT); + aDevice = new EGLGraphicsDevice(eglDisplay, AbstractGraphicsDevice.DEFAULT_CONNECTION, AbstractGraphicsDevice.DEFAULT_UNIT); } protected void closeNativeImpl() { if (aDevice.getHandle() != EGL.EGL_NO_DISPLAY) { - DestroyDisplay(aDevice.getHandle()); + EGL.eglTerminate(aDevice.getHandle()); } } protected void dispatchMessagesNative() { // n/a .. DispatchMessages(); } - - private native long CreateDisplay(int width, int height); - private native void DestroyDisplay(long dpy); - private native void DispatchMessages(); + + public synchronized boolean setAppContext(Context ctx) { + if(null == appContext) { + appContext = ctx; + return true; + } else if(appContext != ctx) { + throw new RuntimeException("AppContext already set to "+appContext+", can't override w/ "+ctx); + } + return false; + } + public synchronized Context getAppContext() { + return appContext; + } + + private Context appContext; } diff --git a/src/newt/classes/jogamp/newt/driver/android/AndroidScreen.java b/src/newt/classes/jogamp/newt/driver/android/AndroidScreen.java index 44c7ca95f..34092e885 100644 --- a/src/newt/classes/jogamp/newt/driver/android/AndroidScreen.java +++ b/src/newt/classes/jogamp/newt/driver/android/AndroidScreen.java @@ -29,31 +29,105 @@ package jogamp.newt.driver.android; import javax.media.nativewindow.*; +import javax.media.nativewindow.util.Dimension; +import javax.media.nativewindow.util.DimensionReadOnly; +import javax.media.nativewindow.util.SurfaceSize; + +import com.jogamp.newt.ScreenMode; +import com.jogamp.newt.util.MonitorMode; + import android.content.Context; +import android.graphics.PixelFormat; +import android.util.DisplayMetrics; +import android.view.Surface; +import android.view.WindowManager; -public class Screen extends jogamp.newt.ScreenImpl { +public class AndroidScreen extends jogamp.newt.ScreenImpl { static { - Display.initSingleton(); + AndroidDisplay.initSingleton(); } - - public Screen() { + public AndroidScreen() { } protected void createNativeImpl() { aScreen = new DefaultGraphicsScreen(getDisplay().getGraphicsDevice(), screen_idx); - // WINDOW_SERVICEUse with getSystemService(String) to retrieve a WindowManager for accessing - setScreenSize(fixedWidth, fixedHeight); } protected void closeNativeImpl() { } + public synchronized boolean setAppContext(Context ctx) { + if(!((AndroidDisplay) getDisplay()).setAppContext(ctx)) { + return false; + } + final WindowManager wmgr = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); + sm = getScreenMode(wmgr.getDefaultDisplay()); + setScreenSize(sm.getMonitorMode().getSurfaceSize().getResolution().getWidth(), + sm.getMonitorMode().getSurfaceSize().getResolution().getHeight()); + return true; + } + public synchronized Context getAppContext() { + return ((AndroidDisplay) getDisplay()).getAppContext(); + } + + protected ScreenMode getCurrentScreenModeImpl() { + return sm; + } + + ScreenMode sm = null; + //---------------------------------------------------------------------- // Internals only // + static DimensionReadOnly getScreenSize(DisplayMetrics outMetrics) { + return new Dimension(outMetrics.widthPixels, outMetrics.heightPixels); + } + static SurfaceSize getSurfaceSize(android.view.Display aDisplay, DimensionReadOnly dim) { + int bpp; + switch(aDisplay.getPixelFormat()) { + case PixelFormat.RGBA_8888: bpp=32; break; + case PixelFormat.RGBX_8888: bpp=32; break; + case PixelFormat.RGB_888: bpp=24; break; + case PixelFormat.RGB_565: bpp=16; break; + case PixelFormat.RGBA_5551: bpp=16; break; + case PixelFormat.RGBA_4444: bpp=16; break; + case PixelFormat.RGB_332: bpp= 8; break; + default: bpp=32; + } + return new SurfaceSize(dim, bpp); + } + static DimensionReadOnly getScreenSizeMM(DisplayMetrics outMetrics) { + final float iw = (float) outMetrics.widthPixels / outMetrics.xdpi; + final float ih = (float) outMetrics.heightPixels / outMetrics.xdpi; + final float mmpi = 25.4f; + return new Dimension((int) ((iw * mmpi)+0.5), (int) ((ih * mmpi)+0.5)); + } + static int getRotation(int androidRotation) { + int nrot; + switch(androidRotation) { + case Surface.ROTATION_270: nrot = ScreenMode.ROTATE_270; break; + case Surface.ROTATION_180: nrot = ScreenMode.ROTATE_180; break; + case Surface.ROTATION_90: nrot = ScreenMode.ROTATE_90; break; + case Surface.ROTATION_0: + default: nrot = ScreenMode.ROTATE_0; + } + return nrot; + } + static ScreenMode getScreenMode(android.view.Display aDisplay) { + final DisplayMetrics outMetrics = new DisplayMetrics(); + aDisplay.getMetrics(outMetrics); + + final DimensionReadOnly screenSize = getScreenSize(outMetrics); + final SurfaceSize surfaceSize = getSurfaceSize(aDisplay, screenSize); + final DimensionReadOnly screenSizeMM = getScreenSizeMM(outMetrics); + final int refreshRate = (int) aDisplay.getRefreshRate(); + final MonitorMode mm = new MonitorMode(surfaceSize, screenSizeMM, refreshRate); + + final int rotation = getRotation(aDisplay.getRotation()); + return new ScreenMode(mm, rotation); + } + - static final int fixedWidth = 1920; - static final int fixedHeight = 1080; } -- cgit v1.2.3