/*
* Copyright 2009 - 2010 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.
*/
package com.jogamp.opencl;
import com.jogamp.common.nio.Buffers;
import com.jogamp.common.os.DynamicLookupHelper;
import java.security.PrivilegedAction;
import com.jogamp.common.JogampRuntimeException;
import com.jogamp.common.os.NativeLibrary;
import com.jogamp.common.nio.NativeSizeBuffer;
import com.jogamp.gluegen.runtime.FunctionAddressResolver;
import com.jogamp.opencl.util.CLUtil;
import com.jogamp.opencl.impl.CLImpl;
import com.jogamp.opencl.impl.CLProcAddressTable;
import com.jogamp.opencl.util.Filter;
import com.jogamp.opencl.util.JOCLVersion;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import static java.security.AccessController.*;
import static com.jogamp.opencl.CLException.*;
import static com.jogamp.opencl.CL.*;
/**
* CLPlatfrorm representing a OpenCL implementation (e.g. graphics driver).
*
* optional eager initialization:
*
* try{
* CLPlatform.initialize();
* }catch(JogampRuntimeException ex) {
* throw new RuntimeException("could not load Java OpenCL Binding");
* }
*
* concurrency:
* CLPlatform is threadsafe.
*
* @author Michael Bien
* @see #initialize()
* @see #getDefault()
* @see #listCLPlatforms()
*/
public final class CLPlatform {
/**
* OpenCL platform id for this platform.
*/
public final long ID;
/**
* Version of this OpenCL platform.
*/
public final CLVersion version;
private static CL cl;
private Set extensions;
private CLPlatform(long id) {
this.ID = id;
this.version = new CLVersion(getInfoString(CL_PLATFORM_VERSION));
}
/**
* Eagerly initializes JOCL. Subsequent calls do nothing.
* @throws JogampRuntimeException if something went wrong in the initialization (e.g. OpenCL lib not found).
*/
public synchronized static void initialize() throws JogampRuntimeException {
if(cl != null) {
return;
}
try {
final CLProcAddressTable table = new CLProcAddressTable(new FunctionAddressResolver() {
public long resolve(String name, DynamicLookupHelper lookup) {
//FIXME workaround to fix a gluegen issue
if(name.endsWith("Impl")) {
name = name.substring(0, name.length() - "Impl".length());
}
if(name.endsWith("KHR") || name.endsWith("EXT")) {
long address = ((CLImpl) cl).clGetExtensionFunctionAddress(name);
if(address != 0) {
return address;
}
}
return lookup.dynamicLookupFunction(name);
}
});
cl = new CLImpl(table);
//load JOCL and init table
doPrivileged(new PrivilegedAction