📄 nativepipeline.java
字号:
/* * $RCSfile: NativePipeline.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Use is subject to license terms. * * $Revision: 1.11 $ * $Date: 2007/05/31 20:55:44 $ * $State: Exp $ */package javax.media.j3d;import java.awt.GraphicsConfiguration;import java.awt.GraphicsDevice;import java.io.File;import java.lang.reflect.Method;import java.security.AccessController;import java.util.ArrayList;/** * Concrete implementation of Pipeline class for native OGL and D3D rendering * pipeline. */class NativePipeline extends Pipeline { // System properties containing the native library search PATH // The order listed is the order in which they will be searched private static final String[] systemPathProps = { "sun.boot.library.path", "java.library.path" }; // Prefix for native libraries private static final String libPrefix = "j3dcore"; // Boolean indicating whether we are using D3D or OGL private boolean isD3D; // Renderer name, either "ogl" or "d3d" private String rendererName; // Flags indicating whether the Cg or GLSL libraries are available. private boolean cgLibraryAvailable = false; private boolean glslLibraryAvailable = false; // Flag indicating that the ogl-chk library has been loaded private static boolean oglChkLibraryLoaded = false; // Method to return the vendor string for the native OpenGL pipeline. // If the native library cannot be loaded, or if GL_VERSION < 1.2 // then null is returned. static String getSupportedOglVendor() { if (!oglChkLibraryLoaded) { try { loadLibrary("j3dcore-ogl-chk", true); } catch (Exception ex) { MasterControl.getCoreLogger().severe(ex.toString()); return null; } catch (Error ex) { MasterControl.getCoreLogger().severe(ex.toString()); return null; } oglChkLibraryLoaded = true; } return getSupportedOglVendorNative(); } // Native method to return the vendor string private static native String getSupportedOglVendorNative(); /** * Constructor for singleton NativePipeline instance */ protected NativePipeline() { } /** * Initialize the pipeline */ void initialize(Pipeline.Type pipelineType) { super.initialize(pipelineType); // This works around a native load library bug try { java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit(); toolkit = null; // just making sure GC collects this } catch (java.awt.AWTError e) { } switch (pipelineType) { case NATIVE_OGL: isD3D = false; rendererName = "ogl"; break; case NATIVE_D3D: isD3D = true; rendererName = "d3d"; break; default: assert false; // Should never get here } // Create the singleton, OS-dependent NativeConfigTemplate3D and // NativeScreenInfo objects. NativeConfigTemplate3D.createNativeConfigTemplate3D(); NativeScreenInfo.createNativeScreenInfo(); } /** * Load all of the required libraries */ void loadLibraries(int globalShadingLanguage) { // Load the native JAWT library as a workaround for a problem whereby // the native libraries can't find it in some cases. // Issue 257: ignore UnsatisfiedLinkError exception as a result of // trying to load the library more than once. Try to continue in // all cases if the load library fails, since it is just a workaround // for a native bug. try { loadLibrary("jawt", false); } catch (UnsatisfiedLinkError ex) { if (ex.getMessage().indexOf("already loaded") == -1) { // If it failed for a reason other than already being // loaded, log the error. In either case, we will continue MasterControl.getCoreLogger().severe(ex.toString()); } } catch (Error ex) { MasterControl.getCoreLogger().severe(ex.toString()); } catch (Exception ex) { MasterControl.getCoreLogger().severe(ex.toString()); } // Load the native rendering library String libraryName = libPrefix + "-" + rendererName; loadLibrary(libraryName, true); // Check whether the Cg library is available if (globalShadingLanguage == Shader.SHADING_LANGUAGE_CG) { String cgLibraryName = libPrefix + "-" + rendererName + "-cg"; String[] libpath = setupLibPath(cgLibraryName); cgLibraryAvailable = loadNativeCgLibrary(libpath); } // Check whether the GLSL library is available if (globalShadingLanguage == Shader.SHADING_LANGUAGE_GLSL) { if (getPipelineType() == Pipeline.Type.NATIVE_OGL) { // No need to verify that GLSL is available, since GLSL is part // of OpenGL as an extension (or part of core in 2.0) glslLibraryAvailable = true; } } } /** * Returns true if the Cg library is loaded and available. Note that this * does not necessarily mean that Cg is supported by the graphics card. */ boolean isCgLibraryAvailable() { return cgLibraryAvailable; } /** * Returns true if the GLSL library is loaded and available. Note that this * does not necessarily mean that GLSL is supported by the graphics card. */ boolean isGLSLLibraryAvailable() { return glslLibraryAvailable; } /** * Load the specified native library. If we are running in "appletLauncher" * mode, we will use JNLPAppletLauncher to load the native library. * Otherwise, we will use System.loadLibrary(). */ private static void loadLibrary(final String libName, boolean allowAppletLauncher) { // Issue 257: Call JNLPAppletLauncher to load native libraries if needed final boolean useAppletLauncher = allowAppletLauncher && MasterControl.isAppletLauncher(); AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { try { if (useAppletLauncher) { Class jnlpAppletLauncherClass = Class.forName("org.jdesktop.applet.util.JNLPAppletLauncher"); Method loadLibraryMethod = jnlpAppletLauncherClass.getDeclaredMethod("loadLibrary", String.class); loadLibraryMethod.invoke(null, libName); } else { System.loadLibrary(libName); } } catch (Exception ex) { throw new RuntimeException(ex); } return null; } }); } /** * Parse the specified System properties containing a PATH and return an * array of Strings, where each element is an absolute filename consisting of * the individual component of the path concatenated with the (relative) * library file name. Only those absolute filenames that exist are included. * If no absolute filename is found, we will try the relative library name. */ private String[] setupLibPath(String libName) { final String libraryName = libName; return (String[]) AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { ArrayList pathList = new ArrayList(); String filename = System.mapLibraryName(libraryName); for (int n = 0; n < systemPathProps.length; n++) { String pathString = System.getProperty(systemPathProps[n]); boolean done = false; int posStart = 0; while (!done) { int posEnd = pathString.indexOf(File.pathSeparator, posStart); if (posEnd == -1) { posEnd = pathString.length(); done = true; } String pathDir = pathString.substring(posStart, posEnd); File pathFile = new File(pathDir, filename); if (pathFile.exists()) { pathList.add(pathFile.getAbsolutePath()); } posStart = posEnd + 1; } } // If no absolute path names exist, add in the relative library name if (pathList.size() == 0) { pathList.add(filename); } return (String[])pathList.toArray(new String[0]); } }); } // Method to verify whether the native Cg library is available private native boolean loadNativeCgLibrary(String[] libpath); // // Methods to box/unbox various native objects // private long unbox(Context ctx) { if (ctx == null) { return 0; } else { return ((NativeContext)ctx).getNativeCtx(); } } private Context boxContext(long nativeCtx) { if (nativeCtx == 0) { return null; } else { return new NativeContext(nativeCtx); } } private long unbox(Drawable drawable) { if (drawable == null) { return 0; } else { return ((NativeDrawable)drawable).getNativeDrawable(); } } private Drawable boxDrawable(long nativeDrawable) { if (nativeDrawable == 0) { return null; } else { return new NativeDrawable(nativeDrawable); } } private long unbox(ShaderProgramId shaderProgramId) { if (shaderProgramId == null) { return 0; } else { return ((NativeShaderObject)shaderProgramId).getNativeId(); } } private ShaderProgramId boxShaderProgramId(long nativeId) { if (nativeId == 0) { return null; } else { return new NativeShaderObject(nativeId); } } private long unbox(ShaderId shaderId) { if (shaderId == null) { return 0; } else { return ((NativeShaderObject)shaderId).getNativeId(); } } private ShaderId boxShaderId(long nativeId) { if (nativeId == 0) { return null; } else { return new NativeShaderObject(nativeId); } } private long unbox(ShaderAttrLoc attrLoc) { if (attrLoc == null) { return -1; } else { return ((NativeShaderObject)attrLoc).getNativeId(); } } private ShaderAttrLoc boxShaderAttrLoc(long nativeId) { if (nativeId == -1) { return null; } else { return new NativeShaderObject(nativeId); } } // --------------------------------------------------------------------- // // GeometryArrayRetained methods // // Used by D3D to free vertex buffer native void freeD3DArray(GeometryArrayRetained geo, boolean deleteVB); // used for GeometryArrays by Copy or interleaved native void execute(long ctx, GeometryArrayRetained geo, int geo_type, boolean isNonUniformScale, boolean useAlpha, boolean ignoreVertexColors, int startVIndex, int vcount, int vformat, int texCoordSetCount, int[] texCoordSetMap, int texCoordSetMapLen, int[] texCoordSetOffset, int numActiveTexUnitState, int vertexAttrCount, int[] vertexAttrSizes, float[] varray, float[] cdata, int cdirty); void execute(Context ctx, GeometryArrayRetained geo, int geo_type, boolean isNonUniformScale, boolean useAlpha, boolean ignoreVertexColors, int startVIndex, int vcount, int vformat, int texCoordSetCount, int[] texCoordSetMap, int texCoordSetMapLen, int[] texCoordSetOffset, int numActiveTexUnitState, int vertexAttrCount, int[] vertexAttrSizes, float[] varray, float[] cdata, int cdirty) { execute(unbox(ctx), geo, geo_type, isNonUniformScale, useAlpha, ignoreVertexColors, startVIndex, vcount, vformat, texCoordSetCount, texCoordSetMap, texCoordSetMapLen, texCoordSetOffset,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -