@Override protected ProxySurface createMutableSurfaceImpl( AbstractGraphicsDevice deviceReq, boolean createNewDevice, GLCapabilitiesImmutable capsChosen, GLCapabilitiesImmutable capsRequested, GLCapabilitiesChooser chooser, UpstreamSurfaceHook upstreamHook) { final boolean ownDevice; final EGLGraphicsDevice device; if (createNewDevice || !(deviceReq instanceof EGLGraphicsDevice)) { final long nativeDisplayID = (deviceReq instanceof EGLGraphicsDevice) ? ((EGLGraphicsDevice) deviceReq).getNativeDisplayID() : deviceReq.getHandle(); device = EGLDisplayUtil.eglCreateEGLGraphicsDevice( nativeDisplayID, deviceReq.getConnection(), deviceReq.getUnitID()); ownDevice = true; } else { device = (EGLGraphicsDevice) deviceReq; ownDevice = false; } final DefaultGraphicsScreen screen = new DefaultGraphicsScreen(device, 0); final EGLGraphicsConfiguration config = EGLGraphicsConfigurationFactory.chooseGraphicsConfigurationStatic( capsChosen, capsRequested, chooser, screen, VisualIDHolder.VID_UNDEFINED, false); if (null == config) { throw new GLException( "Choosing GraphicsConfiguration failed w/ " + capsChosen + " on " + screen); } return new WrappedSurface(config, 0, upstreamHook, ownDevice); }
private static List<GLCapabilitiesImmutable> getAvailableEGLConfigs( EGLGraphicsDevice eglDisplay, GLCapabilitiesImmutable caps) { final IntBuffer numConfigs = Buffers.newDirectIntBuffer(1); if (!EGL.eglGetConfigs(eglDisplay.getHandle(), null, 0, numConfigs)) { throw new GLException( "EGLDrawableFactory.getAvailableEGLConfigs: Get maxConfigs (eglGetConfigs) call failed, error " + EGLContext.toHexString(EGL.eglGetError())); } if (0 < numConfigs.get(0)) { final PointerBuffer configs = PointerBuffer.allocateDirect(numConfigs.get(0)); final IntBuffer attrs = EGLGraphicsConfiguration.GLCapabilities2AttribList(caps); final int winattrmask = GLGraphicsConfigurationUtil.getExclusiveWinAttributeBits(caps); if (EGL.eglChooseConfig( eglDisplay.getHandle(), attrs, configs, configs.capacity(), numConfigs) && numConfigs.get(0) > 0) { return EGLGraphicsConfigurationFactory.eglConfigs2GLCaps( eglDisplay, caps.getGLProfile(), configs, numConfigs.get(0), winattrmask, false /* forceTransparentFlag */); } } return new ArrayList<GLCapabilitiesImmutable>(0); }
@Override protected List<GLCapabilitiesImmutable> getAvailableCapabilitiesImpl( AbstractGraphicsDevice device) { if (null == sharedMap) { // null == eglES1DynamicLookupHelper && null == eglES2DynamicLookupHelper return new ArrayList<GLCapabilitiesImmutable>(); // null } return EGLGraphicsConfigurationFactory.getAvailableCapabilities(this, device); }
@Override protected ProxySurface createProxySurfaceImpl( AbstractGraphicsDevice deviceReq, int screenIdx, long windowHandle, GLCapabilitiesImmutable capsRequested, GLCapabilitiesChooser chooser, UpstreamSurfaceHook upstream) { final EGLGraphicsDevice eglDeviceReq = (EGLGraphicsDevice) deviceReq; final EGLGraphicsDevice device = EGLDisplayUtil.eglCreateEGLGraphicsDevice( eglDeviceReq.getNativeDisplayID(), deviceReq.getConnection(), deviceReq.getUnitID()); final DefaultGraphicsScreen screen = new DefaultGraphicsScreen(device, screenIdx); final EGLGraphicsConfiguration cfg = EGLGraphicsConfigurationFactory.chooseGraphicsConfigurationStatic( capsRequested, capsRequested, chooser, screen, VisualIDHolder.VID_UNDEFINED, false); return new WrappedSurface(cfg, windowHandle, upstream, true); }
@Override protected final void destroy() { if (null != sharedMap) { if (DEBUG) { System.err.println("EGLDrawableFactory.destroy() .. "); dumpMap(); } Collection<SharedResource> srl = sharedMap.values(); for (Iterator<SharedResource> sri = srl.iterator(); sri.hasNext(); ) { SharedResource sr = sri.next(); if (DEBUG) { System.err.println("EGLDrawableFactory.destroy(): " + sr.device.toString()); } sr.device.close(); } sharedMap.clear(); sharedMapCreateAttempt.clear(); sharedMap = null; sharedMapCreateAttempt = null; } if (null != defaultSharedResource) { defaultSharedResource = null; } if (null != defaultDevice) { defaultDevice.close(); defaultDevice = null; } /** Pulling away the native library may cause havoc .. */ if (null != eglES1DynamicLookupHelper) { // eglES1DynamicLookupHelper.destroy(); eglES1DynamicLookupHelper = null; } if (null != eglES2DynamicLookupHelper) { // eglES2DynamicLookupHelper.destroy(); eglES2DynamicLookupHelper = null; } EGLGraphicsConfigurationFactory.unregisterFactory(); EGLDisplayUtil.shutdown(DEBUG); }
private final void evalUpstreamSurface(String dbgPrefix, ProxySurface surface) { // // evaluate nature of upstreamSurface, may create EGL instances if required // boolean isEGLSurfaceValid = true; // assume yes final EGLGraphicsDevice eglDevice; final AbstractGraphicsConfiguration aConfig; { final AbstractGraphicsConfiguration surfaceConfig = surface.getGraphicsConfiguration(); final AbstractGraphicsDevice surfaceDevice = null != surfaceConfig ? surfaceConfig.getScreen().getDevice() : null; if (DEBUG) { System.err.println( dbgPrefix + "SurfaceDevice: " + surfaceDevice.getClass().getSimpleName() + ", hash 0x" + Integer.toHexString(surfaceDevice.hashCode()) + ", " + surfaceDevice); System.err.println( dbgPrefix + "SurfaceConfig: " + surfaceConfig.getClass().getSimpleName() + ", hash 0x" + Integer.toHexString(surfaceConfig.hashCode()) + ", " + surfaceConfig); } final AbstractGraphicsConfiguration upstreamConfig = upstreamSurface.getGraphicsConfiguration(); final AbstractGraphicsDevice upstreamDevice = upstreamConfig.getScreen().getDevice(); if (DEBUG) { System.err.println( dbgPrefix + "UpstreamDevice: " + upstreamDevice.getClass().getSimpleName() + ", hash 0x" + Integer.toHexString(upstreamDevice.hashCode()) + ", " + upstreamDevice); System.err.println( dbgPrefix + "UpstreamConfig: " + upstreamConfig.getClass().getSimpleName() + ", hash 0x" + Integer.toHexString(upstreamConfig.hashCode()) + ", " + upstreamConfig); } if (surfaceDevice instanceof EGLGraphicsDevice) { eglDevice = (EGLGraphicsDevice) surfaceDevice; aConfig = surfaceConfig; if (DEBUG) { System.err.println( dbgPrefix + "Reusing this eglDevice: " + eglDevice + ", using this config " + aConfig.getClass().getSimpleName() + " " + aConfig); } if (EGL.EGL_NO_DISPLAY == eglDevice.getHandle()) { eglDevice.open(); isEGLSurfaceValid = false; surface.addUpstreamOptionBits(ProxySurface.OPT_PROXY_OWNS_UPSTREAM_DEVICE); } } else if (upstreamDevice instanceof EGLGraphicsDevice) { eglDevice = (EGLGraphicsDevice) upstreamDevice; aConfig = upstreamConfig; if (DEBUG) { System.err.println( dbgPrefix + "Reusing upstream eglDevice: " + eglDevice + ", using upstream config " + aConfig.getClass().getSimpleName() + " " + aConfig); } if (EGL.EGL_NO_DISPLAY == eglDevice.getHandle()) { eglDevice.open(); isEGLSurfaceValid = false; surface.addUpstreamOptionBits(ProxySurface.OPT_PROXY_OWNS_UPSTREAM_DEVICE); } } else { eglDevice = EGLDisplayUtil.eglCreateEGLGraphicsDevice(upstreamSurface); eglDevice.open(); aConfig = upstreamConfig; isEGLSurfaceValid = false; surface.addUpstreamOptionBits(ProxySurface.OPT_PROXY_OWNS_UPSTREAM_DEVICE); } } final GLCapabilitiesImmutable capsRequested = (GLCapabilitiesImmutable) aConfig.getRequestedCapabilities(); final EGLGraphicsConfiguration eglConfig; if (aConfig instanceof EGLGraphicsConfiguration) { // Config is already in EGL type - reuse .. final EGLGLCapabilities capsChosen = (EGLGLCapabilities) aConfig.getChosenCapabilities(); if (!isEGLSurfaceValid || !EGLGraphicsConfiguration.isEGLConfigValid( eglDevice.getHandle(), capsChosen.getEGLConfig())) { // 'refresh' the native EGLConfig handle capsChosen.setEGLConfig( EGLGraphicsConfiguration.EGLConfigId2EGLConfig( eglDevice.getHandle(), capsChosen.getEGLConfigID())); if (0 == capsChosen.getEGLConfig()) { throw new GLException( "Refreshing native EGLConfig handle failed with error " + EGLContext.toHexString(EGL.eglGetError()) + ": " + eglDevice + ", " + capsChosen + " of " + aConfig); } final AbstractGraphicsScreen eglScreen = new DefaultGraphicsScreen(eglDevice, aConfig.getScreen().getIndex()); eglConfig = new EGLGraphicsConfiguration(eglScreen, capsChosen, capsRequested, null); if (DEBUG) { System.err.println(dbgPrefix + "Refreshing eglConfig: " + eglConfig); } isEGLSurfaceValid = false; } else { eglConfig = (EGLGraphicsConfiguration) aConfig; if (DEBUG) { System.err.println(dbgPrefix + "Reusing eglConfig: " + eglConfig); } } } else { final AbstractGraphicsScreen eglScreen = new DefaultGraphicsScreen(eglDevice, aConfig.getScreen().getIndex()); eglConfig = EGLGraphicsConfigurationFactory.chooseGraphicsConfigurationStatic( capsRequested, capsRequested, null, eglScreen, aConfig.getVisualID(VIDType.NATIVE), false /* forceTransparencyFlag */); if (null == eglConfig) { throw new GLException("Couldn't create EGLGraphicsConfiguration from " + eglScreen); } else if (DEBUG) { System.err.println(dbgPrefix + "Chosen eglConfig: " + eglConfig); } isEGLSurfaceValid = false; } surface.setGraphicsConfiguration(eglConfig); if (isEGLSurfaceValid) { isEGLSurfaceValid = EGLDrawable.isValidEGLSurface(eglDevice.getHandle(), upstreamSurface.getSurfaceHandle()); } if (isEGLSurfaceValid) { surface.setSurfaceHandle(upstreamSurface.getSurfaceHandle()); surface.clearUpstreamOptionBits(ProxySurface.OPT_PROXY_OWNS_UPSTREAM_SURFACE); if (DEBUG) { System.err.println( dbgPrefix + "Fin: Already valid EGL surface - use as-is: " + upstreamSurface); } } else { surface.setSurfaceHandle(EGL.EGL_NO_SURFACE); surface.addUpstreamOptionBits( ProxySurface.OPT_PROXY_OWNS_UPSTREAM_SURFACE); // create/destroy in EGLDrawable if (DEBUG) { System.err.println(dbgPrefix + "Fin: EGL surface n/a - TBD: " + upstreamSurface); } } }
private boolean mapAvailableEGLESConfig( AbstractGraphicsDevice adevice, int esProfile, boolean[] hasPBuffer, GLRendererQuirks[] rendererQuirks, int[] ctp) { final String profileString; switch (esProfile) { case 3: profileString = GLProfile.GLES3; break; case 2: profileString = GLProfile.GLES2; break; case 1: profileString = GLProfile.GLES1; break; default: throw new GLException("Invalid ES profile number " + esProfile); } if (!GLProfile.isAvailable(adevice, profileString)) { if (DEBUG) { System.err.println( "EGLDrawableFactory.mapAvailableEGLESConfig: " + profileString + " n/a on " + adevice); } return false; } final GLProfile glp = GLProfile.get(adevice, profileString); final GLDrawableFactoryImpl desktopFactory = (GLDrawableFactoryImpl) GLDrawableFactory.getDesktopFactory(); final boolean mapsADeviceToDefaultDevice = !QUERY_EGL_ES_NATIVE_TK || null == desktopFactory || adevice instanceof EGLGraphicsDevice; if (DEBUG) { System.err.println( "EGLDrawableFactory.mapAvailableEGLESConfig: " + profileString + " ( " + esProfile + " ), " + "defaultSharedResourceSet " + (null != defaultSharedResource) + ", mapsADeviceToDefaultDevice " + mapsADeviceToDefaultDevice + " (QUERY_EGL_ES_NATIVE_TK " + QUERY_EGL_ES_NATIVE_TK + ", hasDesktopFactory " + (null != desktopFactory) + ", isEGLGraphicsDevice " + (adevice instanceof EGLGraphicsDevice) + ")"); } EGLGraphicsDevice eglDevice = null; NativeSurface surface = null; ProxySurface upstreamSurface = null; // X11, GLX, .. boolean success = false; boolean deviceFromUpstreamSurface = false; try { final GLCapabilities reqCapsAny = new GLCapabilities(glp); reqCapsAny.setRedBits(5); reqCapsAny.setGreenBits(5); reqCapsAny.setBlueBits(5); reqCapsAny.setAlphaBits(0); reqCapsAny.setDoubleBuffered(false); if (mapsADeviceToDefaultDevice) { // In this branch, any non EGL device is mapped to EGL default shared resources (default // behavior). // Only one default shared resource instance is ever be created. final GLCapabilitiesImmutable reqCapsPBuffer = GLGraphicsConfigurationUtil.fixGLPBufferGLCapabilities(reqCapsAny); final List<GLCapabilitiesImmutable> availablePBufferCapsL = getAvailableEGLConfigs(defaultDevice, reqCapsPBuffer); hasPBuffer[0] = availablePBufferCapsL.size() > 0; // 1st case: adevice is not the EGL default device, map default shared resources if (adevice != defaultDevice) { if (null == defaultSharedResource) { return false; } switch (esProfile) { case 3: if (!defaultSharedResource.wasES3ContextCreated) { return false; } rendererQuirks[0] = defaultSharedResource.rendererQuirksES3ES2; ctp[0] = defaultSharedResource.ctpES3ES2; break; case 2: if (!defaultSharedResource.wasES2ContextCreated) { return false; } rendererQuirks[0] = defaultSharedResource.rendererQuirksES3ES2; ctp[0] = defaultSharedResource.ctpES3ES2; break; case 1: if (!defaultSharedResource.wasES1ContextCreated) { return false; } rendererQuirks[0] = defaultSharedResource.rendererQuirksES1; ctp[0] = defaultSharedResource.ctpES1; break; } EGLContext.mapStaticGLVersion(adevice, esProfile, 0, ctp[0]); return true; } // attempt to created the default shared resources .. eglDevice = defaultDevice; // reuse if (hasPBuffer[0]) { // 2nd case create defaultDevice shared resource using pbuffer surface surface = createDummySurfaceImpl( eglDevice, false, reqCapsPBuffer, reqCapsPBuffer, null, 64, 64); // egl pbuffer offscreen upstreamSurface = (ProxySurface) surface; upstreamSurface.createNotify(); deviceFromUpstreamSurface = false; } else { // 3rd case fake creation of defaultDevice shared resource, no pbuffer available final List<GLCapabilitiesImmutable> capsAnyL = getAvailableEGLConfigs(eglDevice, reqCapsAny); if (capsAnyL.size() > 0) { final GLCapabilitiesImmutable chosenCaps = capsAnyL.get(0); EGLContext.mapStaticGLESVersion(eglDevice, chosenCaps); success = true; } if (DEBUG) { System.err.println( "EGLDrawableFactory.mapAvailableEGLESConfig() no pbuffer config available, detected !pbuffer config: " + success); EGLGraphicsConfigurationFactory.printCaps("!PBufferCaps", capsAnyL, System.err); } } } else { // 4th case always creates a true mapping of given device to EGL surface = desktopFactory.createDummySurface( adevice, reqCapsAny, null, 64, 64); // X11, WGL, .. dummy window upstreamSurface = (surface instanceof ProxySurface) ? (ProxySurface) surface : null; if (null != upstreamSurface) { upstreamSurface.createNotify(); } eglDevice = EGLDisplayUtil.eglCreateEGLGraphicsDevice(surface); deviceFromUpstreamSurface = true; hasPBuffer[0] = true; } if (null != surface) { final EGLDrawable drawable = (EGLDrawable) createOnscreenDrawableImpl( surface); // works w/ implicit pbuffer surface via proxy-hook drawable.setRealized(true); final EGLContext context = (EGLContext) drawable.createContext(null); if (null != context) { try { context.makeCurrent(); // could cause exception if (context.isCurrent()) { final String glVersion = context.getGL().glGetString(GL.GL_VERSION); if (null != glVersion) { context.mapCurrentAvailableGLVersion(eglDevice); if (eglDevice != adevice) { context.mapCurrentAvailableGLVersion(adevice); } rendererQuirks[0] = context.getRendererQuirks(); ctp[0] = context.getContextOptions(); success = true; } else { // Oops .. something is wrong if (DEBUG) { System.err.println( "EGLDrawableFactory.mapAvailableEGLESConfig: " + eglDevice + ", " + context.getGLVersion() + " - VERSION is null, dropping availability!"); } } } } catch (GLException gle) { if (DEBUG) { System.err.println( "EGLDrawableFactory.mapAvailableEGLESConfig: INFO: context create/makeCurrent failed"); gle.printStackTrace(); } } finally { context.destroy(); } } drawable.setRealized(false); } } catch (Throwable t) { if (DEBUG) { System.err.println("Catched Exception on thread " + getThreadName()); t.printStackTrace(); } success = false; } finally { if (eglDevice == defaultDevice) { if (null != upstreamSurface) { upstreamSurface.destroyNotify(); } } else if (deviceFromUpstreamSurface) { if (null != eglDevice) { eglDevice.close(); } if (null != upstreamSurface) { upstreamSurface.destroyNotify(); } } else { if (null != upstreamSurface) { upstreamSurface.destroyNotify(); } if (null != eglDevice) { eglDevice.close(); } } } return success; }
public EGLDrawableFactory() { super(); // Register our GraphicsConfigurationFactory implementations // The act of constructing them causes them to be registered EGLGraphicsConfigurationFactory.registerFactory(); // Check for other underlying stuff .. if (NativeWindowFactory.TYPE_X11 == NativeWindowFactory.getNativeWindowType(true)) { hasX11 = true; try { ReflectionUtil.createInstance( "jogamp.opengl.x11.glx.X11GLXGraphicsConfigurationFactory", EGLDrawableFactory.class.getClassLoader()); } catch (Exception jre) { /* n/a .. */ } } // FIXME: Probably need to move EGL from a static model // to a dynamic one, where there can be 2 instances // for each ES profile with their own ProcAddressTable. synchronized (EGLDrawableFactory.class) { final boolean hasDesktopES2 = null != eglES2DynamicLookupHelper; if (!hasDesktopES2 && null == eglES1DynamicLookupHelper) { GLDynamicLookupHelper tmp = null; try { tmp = new GLDynamicLookupHelper(new EGLES1DynamicLibraryBundleInfo()); } catch (GLException gle) { if (DEBUG) { gle.printStackTrace(); } } if (null != tmp && tmp.isLibComplete()) { eglES1DynamicLookupHelper = tmp; EGL.resetProcAddressTable(eglES1DynamicLookupHelper); final boolean isANGLEES1 = isANGLE(eglES1DynamicLookupHelper); isANGLE |= isANGLEES1; if (DEBUG || GLProfile.DEBUG) { System.err.println("Info: EGLDrawableFactory: EGL ES1 - OK, isANGLE: " + isANGLEES1); } } else if (DEBUG || GLProfile.DEBUG) { System.err.println("Info: EGLDrawableFactory: EGL ES1 - NOPE (ES1 lib)"); } } if (!hasDesktopES2 && null == eglES2DynamicLookupHelper) { GLDynamicLookupHelper tmp = null; try { tmp = new GLDynamicLookupHelper(new EGLES2DynamicLibraryBundleInfo()); } catch (GLException gle) { if (DEBUG) { gle.printStackTrace(); } } if (null != tmp && tmp.isLibComplete()) { eglES2DynamicLookupHelper = tmp; EGL.resetProcAddressTable(eglES2DynamicLookupHelper); final boolean includesES1 = null == eglES1DynamicLookupHelper && includesES1(eglES2DynamicLookupHelper); if (includesES1) { eglES1DynamicLookupHelper = tmp; } final boolean isANGLEES2 = isANGLE(eglES2DynamicLookupHelper); isANGLE |= isANGLEES2; if (DEBUG || GLProfile.DEBUG) { System.err.println( "Info: EGLDrawableFactory: EGL ES2 - OK (includesES1 " + includesES1 + ", isANGLE: " + isANGLEES2 + ")"); if (includesES1) { System.err.println("Info: EGLDrawableFactory: EGL ES1 - OK (ES2 lib)"); } } } else if (DEBUG || GLProfile.DEBUG) { System.err.println("Info: EGLDrawableFactory: EGL ES2 - NOPE"); } } if (null != eglES2DynamicLookupHelper || null != eglES1DynamicLookupHelper) { if (isANGLE && !enableANGLE) { if (DEBUG || GLProfile.DEBUG) { System.err.println("Info: EGLDrawableFactory.init - EGL/ES2 ANGLE disabled"); } } else { if (isANGLE && (DEBUG || GLProfile.DEBUG)) { System.err.println("Info: EGLDrawableFactory.init - EGL/ES2 ANGLE enabled"); } sharedMap = new HashMap<String /*uniqueKey*/, SharedResource>(); sharedMapCreateAttempt = new HashSet<String>(); // FIXME: Following triggers eglInitialize(..) which crashed on Windows w/ Chrome/Angle, // FF/Angle! defaultDevice = EGLDisplayUtil.eglCreateEGLGraphicsDevice( EGL.EGL_DEFAULT_DISPLAY, AbstractGraphicsDevice.DEFAULT_CONNECTION, AbstractGraphicsDevice.DEFAULT_UNIT); } } } }