From 976e89ff24da3b2cdf206e8ef8f222f54fb467de Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 23 Nov 2019 17:22:39 +0100 Subject: Bug 1156: GBM: Bring up incl GL rendering (TODO: GBM working page flip / sync) - EGLSurface: Factor out 'eglCreate[Platform]WindowSurface' NEWT egl.gbm.WindowDriver -- Properly use GBM fourcc format and use as visualID for GBM surface creation and EGL config selection -- Create eglSurface within this class -- Hook up GBM/DRM page flip (not working yet, no visible artifacts - no swap) - ProxySurfaceImpl.surfaceSwap() call upstreamSurface's implementation if available TODO: 'Permission denied' calling: - drmSetMaster (optional) - drmModeSetCrtc - drmModePageFlip --- make/build-newt.xml | 14 +- make/scripts/tests.sh | 5 +- src/jogl/classes/jogamp/opengl/egl/EGLSurface.java | 33 +-- .../jogamp/nativewindow/ProxySurfaceImpl.java | 4 + .../jogamp/newt/driver/android/WindowDriver.java | 5 +- .../jogamp/newt/driver/bcm/vc/iv/WindowDriver.java | 2 +- .../jogamp/newt/driver/egl/gbm/WindowDriver.java | 102 +++++++-- src/newt/native/egl_gbm.c | 174 ++++++++++------ src/newt/native/egl_gbm.h | 62 ++++++ src/newt/native/egl_gbm_drmflip.c | 230 +++++++++++++++++++++ 10 files changed, 522 insertions(+), 109 deletions(-) create mode 100644 src/newt/native/egl_gbm.h create mode 100644 src/newt/native/egl_gbm_drmflip.c diff --git a/make/build-newt.xml b/make/build-newt.xml index ac791add2..541077b9d 100644 --- a/make/build-newt.xml +++ b/make/build-newt.xml @@ -526,9 +526,10 @@ - - + + + @@ -556,8 +557,12 @@ - - + + @@ -897,6 +902,7 @@ + diff --git a/make/scripts/tests.sh b/make/scripts/tests.sh index 3d8ac7738..ddfa6c113 100644 --- a/make/scripts/tests.sh +++ b/make/scripts/tests.sh @@ -120,6 +120,8 @@ function jrun() { #D_ARGS="-Djogl.debug=all -Dnewt.debug=all" #D_ARGS="-Djogl.debug=all -Dnativewindow.debug=all" #D_ARGS="-Djogamp.debug=all -Dnativewindow.debug=all -Djogl.debug=all -Dnewt.debug=all" + #D_ARGS="-Djogamp.debug=all -Dnativewindow.debug=all -Djogl.debug=all -Dnewt.debug=all -Djogl.disable.opengldesktop" + D_ARGS="-Dnativewindow.debug=all -Djogl.debug=all -Dnewt.debug=all -Djogl.disable.opengldesktop" #D_ARGS="-Dnativewindow.debug.JAWT -Djogamp.debug.UnsafeUtil" #D_ARGS="-Dnativewindow.debug.OSXUtil -Dnativewindow.debug.JAWT -Djogl.debug.GLContext" @@ -367,8 +369,7 @@ function jrun() { export USE_CLASSPATH=$JOGAMP_ALL_NOAWT_CLASSPATH #export USE_CLASSPATH=$JOGAMP_MOBILE_CLASSPATH #export USE_CLASSPATH=.:$GLUEGEN_JAR:$JOGL_BUILDDIR/jar/atomic/jogl.jar:$JOGL_BUILDDIR/jar/atomic/jogl-gldesktop.jar:$JOGL_BUILDDIR/jar/atomic/jogl-os-x11.jar:$JOGL_BUILDDIR/jar/atomic/jogl-util.jar:$JOGL_BUILDDIR/jar/atomic/nativewindow.jar:$JOGL_BUILDDIR/jar/atomic/nativewindow-os-x11.jar:$JOGL_BUILDDIR/jar/atomic/newt.jar:$JOGL_BUILDDIR/jar/atomic/newt-driver-x11.jar:$JOGL_BUILDDIR/jar/atomic/newt-ogl.jar:$JOGL_BUILDDIR/jar/jogl-test.jar:$JUNIT_JAR:$ANT_JARS - #X_ARGS="-Djava.awt.headless=true $X_ARGS" - X_ARGS="-Djava.awt.headless=false $X_ARGS" + X_ARGS="-Djava.awt.headless=true $X_ARGS" fi if [ $USE_BUILDDIR -eq 1 ] ; then diff --git a/src/jogl/classes/jogamp/opengl/egl/EGLSurface.java b/src/jogl/classes/jogamp/opengl/egl/EGLSurface.java index 20e067eac..367f83295 100644 --- a/src/jogl/classes/jogamp/opengl/egl/EGLSurface.java +++ b/src/jogl/classes/jogamp/opengl/egl/EGLSurface.java @@ -36,7 +36,6 @@ import com.jogamp.nativewindow.ProxySurface; import com.jogamp.nativewindow.UpstreamSurfaceHook; import com.jogamp.opengl.GLCapabilitiesImmutable; import com.jogamp.opengl.GLException; -import com.jogamp.common.ExceptionUtils; import com.jogamp.common.nio.Buffers; import com.jogamp.nativewindow.GenericUpstreamSurfacelessHook; import com.jogamp.opengl.egl.EGL; @@ -93,6 +92,23 @@ public class EGLSurface extends WrappedSurface { } } + /** + * Entry point to C language function: + *
EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint * attrib_list)
Part of EGL_VERSION_1_0
+ *
or
+ *

+ * EGLSurface eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config, void * native_window, const EGLAttrib * attrib_list)
Part of EGL_VERSION_1_5, EGL_EXT_platform_base
Alias for: eglCreatePlatformWindowSurfaceEXT + *

+ */ + public static long eglCreateWindowSurface(final long dpy, final long config, final long win) { + final int eglPlatform = EGLDisplayUtil.getEGLPlatformType(true); + if( 0 != eglPlatform ) { + return EGL.eglCreatePlatformWindowSurface(dpy, config, win, null); + } else { + return EGL.eglCreateWindowSurface(dpy, config, win, null); + } + } + public void setEGLSurfaceHandle() throws GLException { setSurfaceHandle( createEGLSurface() ); } @@ -138,24 +154,15 @@ public class EGLSurface extends WrappedSurface { if( isPBuffer ) { return EGLDrawableFactory.createPBufferSurfaceImpl(config, getSurfaceWidth(), getSurfaceHeight(), false); } else { - final int eglPlatform = EGLDisplayUtil.getEGLPlatformType(true); final long eglNativeWin = useNativeSurface ? nativeSurface.getSurfaceHandle() : ((NativeWindow)nativeSurface).getWindowHandle(); - final long eglSurface; - if( 0 != eglPlatform ) { - eglSurface = EGL.eglCreatePlatformWindowSurface(config.getScreen().getDevice().getHandle(), - config.getNativeConfig(), - eglNativeWin, null); - } else { - eglSurface = EGL.eglCreateWindowSurface(config.getScreen().getDevice().getHandle(), - config.getNativeConfig(), - eglNativeWin, null); - } + final long eglSurface = eglCreateWindowSurface(config.getScreen().getDevice().getHandle(), config.getNativeConfig(), eglNativeWin); if(DEBUG) { + final int eglPlatform = EGLDisplayUtil.getEGLPlatformType(true); System.err.println("EGLSurface.createEGLSurface.X: useNativeSurface "+useNativeSurface+ ", nativeWin "+EGLContext.toHexString(eglNativeWin)+") @ "+ eglPlatform+"/"+NativeWindowFactory.getNativeWindowType(true)+": "+ EGLContext.toHexString(eglSurface)+ - ", "+((EGL.EGL_NO_SURFACE != eglSurface)?"OK":"Failed")); + ", "+((EGL.EGL_NO_SURFACE != eglSurface)?"OK":"Failed")+" - with config "+config); } return eglSurface; } diff --git a/src/nativewindow/classes/jogamp/nativewindow/ProxySurfaceImpl.java b/src/nativewindow/classes/jogamp/nativewindow/ProxySurfaceImpl.java index 4c53ddb94..9f650f760 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/ProxySurfaceImpl.java +++ b/src/nativewindow/classes/jogamp/nativewindow/ProxySurfaceImpl.java @@ -163,6 +163,10 @@ public abstract class ProxySurfaceImpl implements ProxySurface { @Override public boolean surfaceSwap() { + final NativeSurface upstreamSurface = upstream.getUpstreamSurface(); + if( null != upstreamSurface ) { + return upstreamSurface.surfaceSwap(); + } return false; } diff --git a/src/newt/classes/jogamp/newt/driver/android/WindowDriver.java b/src/newt/classes/jogamp/newt/driver/android/WindowDriver.java index 890680e93..7bf2b6146 100644 --- a/src/newt/classes/jogamp/newt/driver/android/WindowDriver.java +++ b/src/newt/classes/jogamp/newt/driver/android/WindowDriver.java @@ -54,6 +54,7 @@ import com.jogamp.opengl.egl.EGL; import jogamp.opengl.egl.EGLDisplayUtil; import jogamp.opengl.egl.EGLGraphicsConfiguration; import jogamp.opengl.egl.EGLGraphicsConfigurationFactory; +import jogamp.opengl.egl.EGLSurface; import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -358,9 +359,9 @@ public class WindowDriver extends jogamp.newt.WindowImpl implements Callback2 { setSurfaceVisualID0(surfaceHandle, nativeVisualID); } - eglSurface = EGL.eglCreateWindowSurface(eglDevice.getHandle(), eglConfig.getNativeConfig(), surfaceHandle, null); + eglSurface = EGLSurface.eglCreateWindowSurface(eglDevice.getHandle(), eglConfig.getNativeConfig(), surfaceHandle); if (EGL.EGL_NO_SURFACE==eglSurface) { - throw new NativeWindowException("Creation of window surface failed: "+eglConfig+", surfaceHandle 0x"+Long.toHexString(surfaceHandle)+", error "+toHexString(EGL.eglGetError())); + throw new NativeWindowException("Creation of eglSurface failed: "+eglConfig+", surfaceHandle 0x"+Long.toHexString(surfaceHandle)+", error "+toHexString(EGL.eglGetError())); } // propagate data .. diff --git a/src/newt/classes/jogamp/newt/driver/bcm/vc/iv/WindowDriver.java b/src/newt/classes/jogamp/newt/driver/bcm/vc/iv/WindowDriver.java index b0a4ee34a..42ddb8e7f 100644 --- a/src/newt/classes/jogamp/newt/driver/bcm/vc/iv/WindowDriver.java +++ b/src/newt/classes/jogamp/newt/driver/bcm/vc/iv/WindowDriver.java @@ -190,7 +190,7 @@ public class WindowDriver extends WindowImpl { eglDevice.open(); final DefaultGraphicsScreen eglScreen = new DefaultGraphicsScreen(eglDevice, aScreen.getIndex()); - final AbstractGraphicsConfiguration cfg = GraphicsConfigurationFactory.getFactory(getScreen().getDisplay().getGraphicsDevice(), capsRequested).chooseGraphicsConfiguration( + final AbstractGraphicsConfiguration cfg = GraphicsConfigurationFactory.getFactory(display.getGraphicsDevice(), capsRequested).chooseGraphicsConfiguration( capsRequested, capsRequested, capabilitiesChooser, eglScreen, VisualIDHolder.VID_UNDEFINED); if (null == cfg) { throw new NativeWindowException("Error choosing GraphicsConfiguration creating window: "+this); diff --git a/src/newt/classes/jogamp/newt/driver/egl/gbm/WindowDriver.java b/src/newt/classes/jogamp/newt/driver/egl/gbm/WindowDriver.java index 79c0b0dac..8517a132f 100644 --- a/src/newt/classes/jogamp/newt/driver/egl/gbm/WindowDriver.java +++ b/src/newt/classes/jogamp/newt/driver/egl/gbm/WindowDriver.java @@ -27,20 +27,23 @@ */ package jogamp.newt.driver.egl.gbm; -import com.jogamp.nativewindow.AbstractGraphicsConfiguration; import com.jogamp.nativewindow.AbstractGraphicsScreen; -import com.jogamp.nativewindow.Capabilities; -import com.jogamp.nativewindow.GraphicsConfigurationFactory; import com.jogamp.nativewindow.NativeWindowException; -import com.jogamp.nativewindow.VisualIDHolder; import com.jogamp.nativewindow.egl.EGLGraphicsDevice; import com.jogamp.nativewindow.util.Point; import com.jogamp.nativewindow.util.Rectangle; import com.jogamp.nativewindow.util.RectangleImmutable; +import com.jogamp.opengl.GLCapabilitiesChooser; +import com.jogamp.opengl.GLCapabilitiesImmutable; +import com.jogamp.opengl.GLException; +import com.jogamp.opengl.egl.EGL; import jogamp.newt.WindowImpl; import jogamp.newt.driver.linux.LinuxEventDeviceTracker; import jogamp.newt.driver.linux.LinuxMouseTracker; +import jogamp.opengl.egl.EGLGraphicsConfiguration; +import jogamp.opengl.egl.EGLGraphicsConfigurationFactory; +import jogamp.opengl.egl.EGLSurface; public class WindowDriver extends WindowImpl { @@ -124,6 +127,15 @@ public class WindowDriver extends WindowImpl { return true; // default: always able to be created } + static int fourcc_code(final char a, final char b, final char c, final char d) { + // return ( (int)(a) | ((int)(b) << 8) | ((int)(c) << 16) | ((int)(d) << 24) ); + return ( (a) | ((b) << 8) | ((c) << 16) | ((d) << 24) ); + } + /** [31:0] x:R:G:B 8:8:8:8 little endian */ + static final int GBM_FORMAT_XRGB8888 = fourcc_code('X', 'R', '2', '4'); + /** [31:0] A:R:G:B 8:8:8:8 little endian */ + static final int GBM_FORMAT_ARGB8888 = fourcc_code('A', 'R', '2', '4'); + @Override protected void createNativeImpl() { if (0 != getParentWindowHandle()) { @@ -136,38 +148,49 @@ public class WindowDriver extends WindowImpl { // Create own screen/device resource instance allowing independent ownership, // while still utilizing shared EGL resources. final AbstractGraphicsScreen aScreen = screen.getGraphicsScreen(); - // final AbstractGraphicsDevice aDevice = display.getGraphicsDevice(); - // final EGLGraphicsDevice aDevice = (EGLGraphicsDevice) aScreen.getDevice(); + final int nativeVisualID = capsRequested.isBackgroundOpaque() ? GBM_FORMAT_XRGB8888 : GBM_FORMAT_ARGB8888; - final AbstractGraphicsConfiguration cfg = GraphicsConfigurationFactory.getFactory(getScreen().getDisplay().getGraphicsDevice(), capsRequested).chooseGraphicsConfiguration( - capsRequested, capsRequested, capabilitiesChooser, aScreen, VisualIDHolder.VID_UNDEFINED); - if (null == cfg) { + final EGLGraphicsConfiguration eglConfig = EGLGraphicsConfigurationFactory.chooseGraphicsConfigurationStatic( + (GLCapabilitiesImmutable)capsRequested, (GLCapabilitiesImmutable)capsRequested, (GLCapabilitiesChooser)capabilitiesChooser, + aScreen, nativeVisualID, !capsRequested.isBackgroundOpaque()); + if (eglConfig == null) { throw new NativeWindowException("Error choosing GraphicsConfiguration creating window: "+this); } - final Capabilities chosenCaps = (Capabilities) cfg.getChosenCapabilities(); - // FIXME: Pass along opaque flag, since EGL doesn't determine it - if(capsRequested.isBackgroundOpaque() != chosenCaps.isBackgroundOpaque()) { - chosenCaps.setBackgroundOpaque(capsRequested.isBackgroundOpaque()); - } - setGraphicsConfiguration(cfg); + setGraphicsConfiguration(eglConfig); final long nativeWindowHandle = CreateWindow0(DisplayDriver.getDrmHandle(), display.getGBMHandle(), - getX(), getY(), getWidth(), getHeight(), - chosenCaps.isBackgroundOpaque(), chosenCaps.getAlphaBits()); + getX(), getY(), getWidth(), getHeight(), nativeVisualID); if (nativeWindowHandle == 0) { - throw new NativeWindowException("Error creating egl window: "+cfg); + throw new NativeWindowException("Error creating egl window: "+eglConfig); } + setGraphicsConfiguration(eglConfig); setWindowHandle(nativeWindowHandle); if (0 == getWindowHandle()) { throw new NativeWindowException("Error native Window Handle is null"); } windowHandleClose = nativeWindowHandle; + eglSurface = EGLSurface.eglCreateWindowSurface(display.getHandle(), eglConfig.getNativeConfig(), nativeWindowHandle); + if (EGL.EGL_NO_SURFACE==eglSurface) { + throw new NativeWindowException("Creation of eglSurface failed: "+eglConfig+", windowHandle 0x"+Long.toHexString(nativeWindowHandle)+", error "+toHexString(EGL.eglGetError())); + } + + if(false) { + /** + if(!EGL.eglSwapBuffers(display.getHandle(), eglSurface)) { + throw new GLException("Error swapping buffers, eglError "+toHexString(EGL.eglGetError())+", "+this); + } */ + lastBO = FirstSwapSurface0(DisplayDriver.getDrmHandle(), nativeWindowHandle, display.getHandle(), eglSurface); + } else { + lastBO = 0; + } + if( null != linuxEventDeviceTracker ) { addWindowListener(linuxEventDeviceTracker); } if( null != linuxMouseTracker ) { addWindowListener(linuxMouseTracker); } + visibleChanged(true); focusChanged(false, true); } @@ -183,6 +206,18 @@ public class WindowDriver extends WindowImpl { removeWindowListener(linuxEventDeviceTracker); } + lastBO = 0; + if(0 != eglSurface) { + try { + if (!EGL.eglDestroySurface(eglDevice.getHandle(), eglSurface)) { + throw new GLException("Error destroying window surface (eglDestroySurface)"); + } + } catch (final Throwable t) { + t.printStackTrace(); + } finally { + eglSurface = 0; + } + } if( 0 != windowHandleClose ) { CloseWindow0(display.getGBMHandle(), windowHandleClose); windowHandleClose = 0; @@ -191,6 +226,30 @@ public class WindowDriver extends WindowImpl { eglDevice.close(); } + @Override + public final long getSurfaceHandle() { + return eglSurface; + } + + @Override + public boolean surfaceSwap() { + final DisplayDriver display = (DisplayDriver) getScreen().getDisplay(); + final long nativeWindowHandle = getWindowHandle(); + + if( 0 == lastBO ) { + /** if(!EGL.eglSwapBuffers(display.getHandle(), eglSurface)) { + throw new GLException("Error swapping buffers, eglError "+toHexString(EGL.eglGetError())+", "+this); + } */ + lastBO = FirstSwapSurface0(DisplayDriver.getDrmHandle(), nativeWindowHandle, display.getHandle(), eglSurface); + } + + /**if(!EGL.eglSwapBuffers(display.getHandle(), eglSurface)) { + throw new GLException("Error swapping buffers, eglError "+toHexString(EGL.eglGetError())+", "+this); + } */ + lastBO = NextSwapSurface0(DisplayDriver.getDrmHandle(), nativeWindowHandle, lastBO, display.getHandle(), eglSurface); + System.exit(1); + return true; // eglSwapBuffers done! + } @Override protected void requestFocusImpl(final boolean reparented) { @@ -232,10 +291,13 @@ public class WindowDriver extends WindowImpl { private final LinuxMouseTracker linuxMouseTracker; private final LinuxEventDeviceTracker linuxEventDeviceTracker; private long windowHandleClose; + private long eglSurface; + private long lastBO; protected static native boolean initIDs(); - private native long CreateWindow0(long drmHandle, long gbmHandle, int x, int y, int width, int height, boolean opaque, int alphaBits); + private native long CreateWindow0(long drmHandle, long gbmHandle, int x, int y, int width, int height, int nativeVisualID); private native void CloseWindow0(long gbmDisplay, long eglWindowHandle); private native void reconfigure0(long eglWindowHandle, int x, int y, int width, int height, int flags); - + private native long FirstSwapSurface0(long drmHandle, long gbmSurface, long eglDisplay, long eglSurface); + private native long NextSwapSurface0(long drmHandle, long gbmSurface, long lastBO, long eglDisplay, long eglSurface); } diff --git a/src/newt/native/egl_gbm.c b/src/newt/native/egl_gbm.c index d4b56378c..afbab9ef2 100644 --- a/src/newt/native/egl_gbm.c +++ b/src/newt/native/egl_gbm.c @@ -1,39 +1,12 @@ -#include -#include -#include - -#include "jogamp_newt_driver_egl_gbm_DisplayDriver.h" -#include "jogamp_newt_driver_egl_gbm_ScreenDriver.h" -#include "jogamp_newt_driver_egl_gbm_WindowDriver.h" - -#include -#include -#include - -#define VERBOSE_ON 1 - -#ifdef VERBOSE_ON - #define DBG_PRINT(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) -#else - #define DBG_PRINT(...) -#endif -#define ERR_PRINT(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) - -typedef struct { - int fd; // drmClose - drmModeRes *resources; // drmModeFreeResources - drmModeConnector *connector; // drmModeFreeConnector - drmModeEncoder *encoder; // drmModeFreeEncoder - drmModeModeInfo *current_mode; -} DRM_HANDLE; - -typedef struct { - struct gbm_bo *bo; - uint32_t fb_id; -} DRM_GBM_FB; +#include "egl_gbm.h" static jmethodID notifyScreenModeID = NULL; +static jmethodID sizeChangedID = NULL; +static jmethodID positionChangedID = NULL; +static jmethodID visibleChangedID = NULL; +static jmethodID windowDestroyNotifyID = NULL; + /** * Display */ @@ -48,10 +21,6 @@ static void freeDrm(DRM_HANDLE *drm) { drmModeFreeConnector(drm->connector); drm->connector = NULL; } - if( NULL != drm->resources ) { - drmModeFreeResources(drm->resources); - drm->resources = NULL; - } if( 0 <= drm->fd ) { drmClose(drm->fd); drm->fd = -1; @@ -70,23 +39,35 @@ JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initIDs JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initDrm (JNIEnv *env, jclass clazz, jboolean verbose) { + static const char *linux_dri_card0 = "/dev/dri/card0"; static const char *modules[] = { - "/dev/dri/card0", "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "msm" + "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "msm" }; int module_count = sizeof(modules) / sizeof(const char*); const char * module_used = NULL; - int i, j, area; + int ret, i, j, area; DRM_HANDLE *drm = calloc(1, sizeof(DRM_HANDLE)); + drmModeRes *resources = NULL; #ifdef VERBOSE_ON verbose = JNI_TRUE; #endif if( verbose ) { - ERR_PRINT( "EGL_GBM.Display initDrm start (testing %d modules)\n", module_count ); + ERR_PRINT( "EGL_GBM.Display initDrm start\n"); } - for (i = 0; i < module_count; i++) { + drm->fd = -1; + +#if 1 + // try linux_dri_card0 first + drm->fd = open(linux_dri_card0, O_RDWR); + ERR_PRINT("EGL_GBM.Display trying to open '%s': success %d\n", + linux_dri_card0, 0<=drm->fd); +#endif + + // try drm modules + for (i = 0; 0>drm->fd && i < module_count; i++) { if( verbose ) { ERR_PRINT("EGL_GBM.Display trying to load module[%d/%d] %s...", i, module_count, modules[i]); @@ -101,7 +82,6 @@ JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initDrm ERR_PRINT("success.\n"); } module_used = modules[i]; - break; } } if (drm->fd < 0) { @@ -109,27 +89,38 @@ JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initDrm goto error; } - drm->resources = drmModeGetResources(drm->fd); - if ( NULL == drm->resources ) { +#if 1 + ret = drmSetMaster(drm->fd); + if(ret) { + //drmDropMaster(int fd); + DBG_PRINT( "EGL_GBM.Display drmSetMaster fd %d: FAILED: %d %s\n", + drm->fd, ret, strerror(errno)); + } else { + DBG_PRINT( "EGL_GBM.Display drmSetMaster fd %d: OK\n", drm->fd); + } +#endif + + resources = drmModeGetResources(drm->fd); + if ( NULL == resources ) { ERR_PRINT("EGL_GBM.Display drmModeGetResources failed on module %s: %s\n", module_used, strerror(errno)); goto error; } if( verbose ) { - for (i = 0; i < drm->resources->count_connectors; i++) { - drmModeConnector * c = drmModeGetConnector(drm->fd, drm->resources->connectors[i]); + for (i = 0; i < resources->count_connectors; i++) { + drmModeConnector * c = drmModeGetConnector(drm->fd, resources->connectors[i]); int chosen = DRM_MODE_CONNECTED == c->connection; - ERR_PRINT( "EGL_GBM.Display Connector %d/%d chosen %d: id[con 0x%X, enc 0x%X], type %d[id 0x%X], connection %d, dim %dx%x mm, modes %d, encoders %d\n", - i, drm->resources->count_connectors, chosen, + ERR_PRINT( "EGL_GBM.Display Connector %d/%d chosen %d: id[con 0x%x, enc 0x%x], type %d[id 0x%x], connection %d, dim %dx%x mm, modes %d, encoders %d\n", + i, resources->count_connectors, chosen, c->connector_id, c->encoder_id, c->connector_type, c->connector_type_id, c->connection, c->mmWidth, c->mmHeight, c->count_modes, c->count_encoders); drmModeFreeConnector(c); } } /* find a connected connector: */ - for (i = 0; i < drm->resources->count_connectors; i++) { - drm->connector = drmModeGetConnector(drm->fd, drm->resources->connectors[i]); + for (i = 0; i < resources->count_connectors; i++) { + drm->connector = drmModeGetConnector(drm->fd, resources->connectors[i]); if( DRM_MODE_CONNECTED == drm->connector->connection ) { break; } else { @@ -137,12 +128,12 @@ JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initDrm drm->connector = NULL; } } - if( i >= drm->resources->count_connectors ) { + if( i >= resources->count_connectors ) { /* we could be fancy and listen for hotplug events and wait for * a connector.. */ ERR_PRINT("EGL_GBM.Display no connected connector (connector count %d, module %s)!\n", - drm->resources->count_connectors, module_used); + resources->count_connectors, module_used); goto error; } @@ -168,19 +159,19 @@ JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initDrm } if( verbose ) { - for (i = 0; i < drm->resources->count_encoders; i++) { - drmModeEncoder * e = drmModeGetEncoder(drm->fd, drm->resources->encoders[i]); + for (i = 0; i < resources->count_encoders; i++) { + drmModeEncoder * e = drmModeGetEncoder(drm->fd, resources->encoders[i]); int chosen = e->encoder_id == drm->connector->encoder_id; - ERR_PRINT( "EGL_GBM.Display Encoder %d/%d chosen %d: id 0x%X, type %d, crtc_id 0x%X, possible[crtcs %d, clones %d]\n", - i, drm->resources->count_encoders, chosen, + ERR_PRINT( "EGL_GBM.Display Encoder %d/%d chosen %d: id 0x%x, type %d, crtc_id 0x%x, possible[crtcs %d, clones %d]\n", + i, resources->count_encoders, chosen, e->encoder_id, e->encoder_type, e->crtc_id, e->possible_crtcs, e->possible_clones); drmModeFreeEncoder(e); } } /* find encoder: */ - for (i = 0; i < drm->resources->count_encoders; i++) { - drm->encoder = drmModeGetEncoder(drm->fd, drm->resources->encoders[i]); + for (i = 0; i < resources->count_encoders; i++) { + drm->encoder = drmModeGetEncoder(drm->fd, resources->encoders[i]); if( drm->encoder->encoder_id == drm->connector->encoder_id ) { break; } else { @@ -188,13 +179,23 @@ JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initDrm drm->encoder = NULL; } } - if ( i >= drm->resources->count_encoders ) { + if ( i >= resources->count_encoders ) { ERR_PRINT("EGL_GBM.Display no encoder (module %s)!\n", module_used); goto error; } + for (i = 0; i < resources->count_crtcs; i++) { + if (resources->crtcs[i] == drm->encoder->crtc_id) { + drm->crtc_index = i; + break; + } + } + + drmModeFreeResources(resources); + resources = NULL; if( verbose ) { - DBG_PRINT( "EGL_GBM.Display initDrm end.X0 OK\n"); + DBG_PRINT( "EGL_GBM.Display initDrm end.X0 OK: fd %d, enc_id 0x%x, crtc_id 0x%x, conn_id 0x%x, curMode %s\n", + drm->fd, drm->encoder->encoder_id, drm->encoder->crtc_id, drm->connector->connector_id, drm->current_mode->name); } // drm->crtc_id = encoder->crtc_id; @@ -205,6 +206,8 @@ error: if( verbose ) { DBG_PRINT( "EGL_GBM.Display initDrm end.X2 ERROR\n"); } + drmModeFreeResources(resources); + resources = NULL; freeDrm(drm); return 0; } @@ -219,6 +222,7 @@ JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_OpenGBMDis { DRM_HANDLE *drm = (DRM_HANDLE*) (intptr_t) jdrm; struct gbm_device * dev = gbm_create_device(drm->fd); + DBG_PRINT( "EGL_GBM.Display OpenGBMDisplay0 handle %p\n", dev); return (jlong) (intptr_t) dev; } @@ -226,6 +230,7 @@ JNIEXPORT void JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_CloseGBMDis (JNIEnv *env, jclass clazz, jlong jgbm) { struct gbm_device * dev = (struct gbm_device *) (intptr_t) jgbm; + DBG_PRINT( "EGL_GBM.Display CloseGBMDisplay0 handle %p\n", dev); gbm_device_destroy(dev); } @@ -283,24 +288,58 @@ JNIEXPORT void JNICALL Java_jogamp_newt_driver_egl_gbm_ScreenDriver_initNative JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_egl_gbm_WindowDriver_initIDs (JNIEnv *env, jclass clazz) { + sizeChangedID = (*env)->GetMethodID(env, clazz, "sizeChanged", "(ZIIZ)V"); + positionChangedID = (*env)->GetMethodID(env, clazz, "positionChanged", "(ZII)V"); + visibleChangedID = (*env)->GetMethodID(env, clazz, "visibleChanged", "(Z)V"); + windowDestroyNotifyID = (*env)->GetMethodID(env, clazz, "windowDestroyNotify", "(Z)Z"); + if (sizeChangedID == NULL || + positionChangedID == NULL || + visibleChangedID == NULL || + windowDestroyNotifyID == NULL) { + DBG_PRINT( "initIDs failed\n" ); + return JNI_FALSE; + } DBG_PRINT( "EGL_GBM.Window initIDs ok\n" ); return JNI_TRUE; } +#ifndef DRM_FORMAT_MOD_LINEAR + #define DRM_FORMAT_MOD_LINEAR 0 +#endif + +WEAK struct gbm_surface * +gbm_surface_create_with_modifiers(struct gbm_device *gbm, + uint32_t width, uint32_t height, + uint32_t format, + const uint64_t *modifiers, + const unsigned int count); + JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_WindowDriver_CreateWindow0 - (JNIEnv *env, jobject obj, jlong jdrm, jlong jgbm, jint x, jint y, jint width, jint height, jboolean opaque, jint alphaBits) + (JNIEnv *env, jobject obj, jlong jdrm, jlong jgbm, jint x, jint y, jint width, jint height, jint visual_id) { DRM_HANDLE *drm = (DRM_HANDLE*) (intptr_t) jdrm; struct gbm_device *dev = (struct gbm_device *) (intptr_t) jgbm; + uint64_t modifier = DRM_FORMAT_MOD_LINEAR; + struct gbm_surface *surface = NULL; - struct gbm_surface *surface = gbm_surface_create(dev, - drm->current_mode->hdisplay, drm->current_mode->vdisplay, - opaque ? GBM_FORMAT_XRGB8888 : GBM_BO_FORMAT_ARGB8888, - GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); + if( gbm_surface_create_with_modifiers ) { + surface = gbm_surface_create_with_modifiers(dev, width, height, visual_id, &modifier, 1); + } + if( NULL == surface ) { + if( gbm_surface_create_with_modifiers ) { + DBG_PRINT( "EGL_GBM.Window CreateWindow0 gbm_surface_create_with_modifiers failed\n"); + } + surface = gbm_surface_create(dev, width, height, visual_id, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); + } if ( NULL == surface ) { - DBG_PRINT("failed to create gbm surface\n"); - return -1; + DBG_PRINT( "EGL_GBM.Window CreateWindow0 gbm_surface_create failed\n"); + return 0; } + + // Done in Java code .. + // (*env)->CallVoidMethod(env, obj, visibleChangedID, JNI_TRUE); + + DBG_PRINT( "EGL_GBM.Window CreateWindow0 handle %p\n", surface); return (jlong) (intptr_t) surface; } @@ -308,6 +347,7 @@ JNIEXPORT void JNICALL Java_jogamp_newt_driver_egl_gbm_WindowDriver_CloseWindow0 (JNIEnv *env, jobject obj, jlong display, jlong window) { struct gbm_surface *surface = (struct gbm_surface *) (intptr_t) window; + DBG_PRINT( "EGL_GBM.Window CloseWindow0 handle %p\n", surface); gbm_surface_destroy(surface); } diff --git a/src/newt/native/egl_gbm.h b/src/newt/native/egl_gbm.h new file mode 100644 index 000000000..ae7c9121f --- /dev/null +++ b/src/newt/native/egl_gbm.h @@ -0,0 +1,62 @@ +/** + * Copyright 2019 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +#include +#include +#include +#include +#include + +#include "jogamp_newt_driver_egl_gbm_DisplayDriver.h" +#include "jogamp_newt_driver_egl_gbm_ScreenDriver.h" +#include "jogamp_newt_driver_egl_gbm_WindowDriver.h" + +#include +#include +#include + +#define WEAK __attribute__((weak)) + +#define VERBOSE_ON 1 + +#ifdef VERBOSE_ON + #define DBG_PRINT(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) +#else + #define DBG_PRINT(...) +#endif +#define ERR_PRINT(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) + +typedef struct { + int fd; // drmClose + drmModeConnector *connector; // drmModeFreeConnector + drmModeEncoder *encoder; // drmModeFreeEncoder + int crtc_index; + + drmModeModeInfo *current_mode; +} DRM_HANDLE; + diff --git a/src/newt/native/egl_gbm_drmflip.c b/src/newt/native/egl_gbm_drmflip.c new file mode 100644 index 000000000..4dda026a0 --- /dev/null +++ b/src/newt/native/egl_gbm_drmflip.c @@ -0,0 +1,230 @@ +#include "egl_gbm.h" + +#include + +WEAK uint64_t +gbm_bo_get_modifier(struct gbm_bo *bo); + +WEAK int +gbm_bo_get_plane_count(struct gbm_bo *bo); + +WEAK uint32_t +gbm_bo_get_stride_for_plane(struct gbm_bo *bo, int plane); + +WEAK uint32_t +gbm_bo_get_offset(struct gbm_bo *bo, int plane); + +typedef struct { + struct gbm_bo *bo; + uint32_t fb_id; +} DRM_FB; + +static void page_flip_handler(int fd, unsigned int frame, + unsigned int sec, unsigned int usec, void *data) +{ + /* suppress 'unused parameter' warnings */ + (void)fd, (void)frame, (void)sec, (void)usec; + + int *waiting_for_flip = data; + *waiting_for_flip = 0; +} + +static drmEventContext drm_event_ctx = { + .version = 2, + .page_flip_handler = page_flip_handler, + }; + +static void drm_fb_destroy_callback(struct gbm_bo *bo, void *data) +{ + struct gbm_device * gbmDev = gbm_bo_get_device(bo); + int drm_fd = gbm_device_get_fd(gbmDev); + DRM_FB *fb = data; + + if (fb->fb_id) + drmModeRmFB(drm_fd, fb->fb_id); + + free(fb); +} + +static DRM_FB * drm_fb_get_from_bo(struct gbm_bo *bo) +{ + struct gbm_device * gbmDev = gbm_bo_get_device(bo); + int drm_fd = gbm_device_get_fd(gbmDev); + DRM_FB *fb = gbm_bo_get_user_data(bo); + uint32_t width, height, format, + strides[4] = {0}, handles[4] = {0}, + offsets[4] = {0}, flags = 0; + int ret = -1; + + if (fb) + return fb; + + fb = calloc(1, sizeof *fb); + fb->bo = bo; + + width = gbm_bo_get_width(bo); + height = gbm_bo_get_height(bo); + format = gbm_bo_get_format(bo); + + if (gbm_bo_get_modifier && gbm_bo_get_plane_count && + gbm_bo_get_stride_for_plane && gbm_bo_get_offset) { + + uint64_t modifiers[4] = {0}; + modifiers[0] = gbm_bo_get_modifier(bo); + const int num_planes = gbm_bo_get_plane_count(bo); + for (int i = 0; i < num_planes; i++) { + strides[i] = gbm_bo_get_stride_for_plane(bo, i); + handles[i] = gbm_bo_get_handle(bo).u32; + offsets[i] = gbm_bo_get_offset(bo, i); + modifiers[i] = modifiers[0]; + } + + if (modifiers[0]) { + flags = DRM_MODE_FB_MODIFIERS; + DBG_PRINT("Using modifier %" PRIx64 "\n", modifiers[0]); + } + + ret = drmModeAddFB2WithModifiers(drm_fd, width, height, + format, handles, strides, offsets, + modifiers, &fb->fb_id, flags); + if(ret) { + ERR_PRINT("drmModeAddFB2WithModifiers failed!\n"); + } else { + ERR_PRINT("drmModeAddFB2WithModifiers OK!\n"); + } + } + + if (ret) { + memcpy(handles, (uint32_t [4]){gbm_bo_get_handle(bo).u32,0,0,0}, 16); + memcpy(strides, (uint32_t [4]){gbm_bo_get_stride(bo),0,0,0}, 16); + memset(offsets, 0, 16); + ret = drmModeAddFB2(drm_fd, width, height, format, + handles, strides, offsets, &fb->fb_id, 0); + if(ret) { + ERR_PRINT("drmModeAddFB2 failed!\n"); + } else { + ERR_PRINT("drmModeAddFB2 OK!\n"); + } + } + + if (ret) { + ERR_PRINT("failed to create fb: %s\n", strerror(errno)); + free(fb); + return NULL; + } + + gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback); + + return fb; +} + +JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_WindowDriver_FirstSwapSurface0 + (JNIEnv *env, jobject obj, jlong jdrm, jlong jgbmSurface, jlong jeglDisplay, jlong jeglSurface) +{ + DRM_HANDLE *drm = (DRM_HANDLE*) (intptr_t) jdrm; + struct gbm_surface *gbmSurface = (struct gbm_surface *) (intptr_t) jgbmSurface; + EGLDisplay eglDisplay = (EGLDisplay) (intptr_t) jeglDisplay; + EGLSurface eglSurface = (EGLSurface) (intptr_t) jeglSurface; + struct gbm_bo *nextBO = NULL; + DRM_FB *fb = NULL; + int ret; + + if ( EGL_TRUE != eglSwapBuffers(eglDisplay, eglSurface) ) { + EGLint err = eglGetError(); + ERR_PRINT("Failed eglSwapBuffers, err 0x%x\n", err); + return 0; + } + nextBO = gbm_surface_lock_front_buffer(gbmSurface); + fb = drm_fb_get_from_bo(nextBO); + if (!fb) { + ERR_PRINT("Failed to get a new framebuffer BO\n"); + return 0; + } + /* set mode: */ + ret = drmModeSetCrtc(drm->fd, drm->encoder->crtc_id, fb->fb_id, 0, 0, + &drm->connector->connector_id, 1, drm->current_mode); + /** + int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId, + uint32_t x, uint32_t y, uint32_t *connectors, int count, + drmModeModeInfoPtr mode); + */ + + if (ret) { + ERR_PRINT("drmModeSetCrtc failed to set mode: fd %d, enc_id 0x%x, crtc_id 0x%x, fb_id 0x%x, conn_id 0x%x, curMode %s: %d %s\n", + drm->fd, drm->encoder->encoder_id, drm->encoder->crtc_id, fb->fb_id, + drm->connector->connector_id, drm->current_mode->name, ret, strerror(errno)); + return 0; + } + DBG_PRINT( "EGL_GBM.Window FirstSwapSurface0 nextBO %p, fd %d, enc_id 0x%x, crtc_id 0x%x, fb_id 0x%x, conn_id 0x%x, curMode %s\n", + nextBO, + drm->fd, drm->encoder->encoder_id, drm->encoder->crtc_id, fb->fb_id, + drm->connector->connector_id, drm->current_mode->name); + return (jlong) (intptr_t) nextBO; +} + +JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_WindowDriver_NextSwapSurface0 + (JNIEnv *env, jobject obj, jlong jdrm, jlong jgbmSurface, jlong jlastBO, jlong jeglDisplay, jlong jeglSurface) +{ + DRM_HANDLE *drm = (DRM_HANDLE*) (intptr_t) jdrm; + struct gbm_surface *gbmSurface = (struct gbm_surface *) (intptr_t) jgbmSurface; + struct gbm_bo *lastBO = (struct gbm_bo*) (intptr_t) jlastBO, *nextBO = NULL; + EGLDisplay eglDisplay = (EGLDisplay) (intptr_t) jeglDisplay; + EGLSurface eglSurface = (EGLSurface) (intptr_t) jeglSurface; + DRM_FB *fb = NULL; + int ret, waiting_for_flip = 1; + fd_set fds; + + if ( EGL_TRUE != eglSwapBuffers(eglDisplay, eglSurface) ) { + EGLint err = eglGetError(); + ERR_PRINT("Failed eglSwapBuffers, err 0x%x\n", err); + return 0; + } + nextBO = gbm_surface_lock_front_buffer(gbmSurface); + fb = drm_fb_get_from_bo(nextBO); + if (!fb) { + ERR_PRINT("Failed to get a new framebuffer BO\n"); + return 0; + } + + /** + * Here you could also update drm plane layers if you want + * hw composition + */ + ret = drmModePageFlip(drm->fd, drm->encoder->crtc_id, fb->fb_id, + DRM_MODE_PAGE_FLIP_EVENT, &waiting_for_flip); + if (ret) { + ERR_PRINT("drmModePageFlip failed to queue page flip: fd %d, enc_id 0x%x, crtc_id 0x%x, fb_id 0x%x, conn_id 0x%x, curMode %s: %p -> %p: %d %s\n", + drm->fd, drm->encoder->encoder_id, drm->encoder->crtc_id, fb->fb_id, + drm->connector->connector_id, drm->current_mode->name, + lastBO, nextBO, ret, strerror(errno)); + return 0; + } + + while (waiting_for_flip) { + FD_ZERO(&fds); + FD_SET(0, &fds); + FD_SET(drm->fd, &fds); + + ret = select(drm->fd + 1, &fds, NULL, NULL, NULL); + if (ret < 0) { + ERR_PRINT("select err: %s\n", strerror(errno)); + return ret; + } else if (ret == 0) { + ERR_PRINT("select timeout!\n"); + return -1; + } else if (FD_ISSET(0, &fds)) { + ERR_PRINT("user interrupted!\n"); + return 0; + } + drmHandleEvent(drm->fd, &drm_event_ctx); + } + + /* release last buffer to render on again: */ + if( NULL != lastBO ) { + gbm_surface_release_buffer(gbmSurface, lastBO); + } + + DBG_PRINT( "EGL_GBM.Window NextSwapSurface0 %p -> %p\n", lastBO, nextBO); + return (jlong) (intptr_t) nextBO; +} + -- cgit v1.2.3