Skip to content

Modify forceLoad to return isLoaded #2022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public abstract class PhotonJNICommon {

protected static Logger logger = null;

protected static synchronized void forceLoad(
protected static synchronized boolean forceLoad(
PhotonJNICommon instance, Class<?> clazz, List<String> libraries) throws IOException {
if (instance.isLoaded()) return;
if (instance.isLoaded()) return true;
if (logger == null) logger = new Logger(clazz, LogGroup.Camera);

for (var libraryName : libraries) {
Expand All @@ -46,7 +46,8 @@ protected static synchronized void forceLoad(

if (in == null) {
instance.setLoaded(false);
return;
logger.error("Failed to find internal native library " + nativeLibName);
return false;
}

// It's important that we don't mangle the names of these files on Windows at least
Expand All @@ -70,14 +71,15 @@ protected static synchronized void forceLoad(
e.printStackTrace();
// logger.error(System.getProperty("java.library.path"));
instance.setLoaded(false);
return;
return false;
}
}
instance.setLoaded(true);
return instance.isLoaded();
}

protected static synchronized void forceLoad(
protected static synchronized boolean forceLoad(
PhotonJNICommon instance, Class<?> clazz, String libraryName) throws IOException {
forceLoad(instance, clazz, List.of(libraryName));
return forceLoad(instance, clazz, List.of(libraryName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public static RknnDetectorJNI getInstance() {
return instance;
}

public static synchronized void forceLoad() throws IOException {
public static synchronized boolean forceLoad() throws IOException {
TestUtils.loadLibraries();

forceLoad(getInstance(), RknnDetectorJNI.class, List.of("rga", "rknnrt", "rknn_jni"));
return forceLoad(getInstance(), RknnDetectorJNI.class, List.of("rga", "rknnrt", "rknn_jni"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public static synchronized MrCalJNILoader getInstance() {
return instance;
}

public static synchronized void forceLoad() throws IOException {
public static synchronized boolean forceLoad() throws IOException {
// Force load opencv
TestUtils.loadLibraries();

// Library naming is dumb and has "lib" appended for Windows when it ought not to
if (Platform.isWindows()) {
// Order is correct to match dependencies of libraries
forceLoad(
return forceLoad(
MrCalJNILoader.getInstance(),
MrCalJNILoader.class,
List.of(
Expand All @@ -62,11 +62,7 @@ public static synchronized void forceLoad() throws IOException {
"mrcal_jni"));
} else {
// Nothing else to do on linux
forceLoad(MrCalJNILoader.getInstance(), MrCalJNILoader.class, List.of("mrcal_jni"));
}

if (!MrCalJNILoader.getInstance().isLoaded()) {
throw new IOException("Unable to load mrcal JNI!");
return forceLoad(MrCalJNILoader.getInstance(), MrCalJNILoader.class, List.of("mrcal_jni"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class LibCameraJNILoader {
private static boolean libraryLoaded = false;
private static final Logger logger = new Logger(LibCameraJNILoader.class, LogGroup.Camera);

public static synchronized void forceLoad() throws IOException {
if (libraryLoaded) return;
public static synchronized boolean forceLoad() throws IOException {
if (libraryLoaded) return true;

var libraryName = "photonlibcamera";

Expand All @@ -46,7 +46,7 @@ public static synchronized void forceLoad() throws IOException {
if (in == null) {
logger.error("Failed to find internal native library at path " + resourcePath);
libraryLoaded = false;
return;
return false;
}

// It's important that we don't mangle the names of these files on Windows at least
Expand All @@ -71,6 +71,7 @@ public static synchronized void forceLoad() throws IOException {
// logger.error(System.getProperty("java.library.path"));
}
libraryLoaded = true;
return libraryLoaded;
}

public static boolean isSupported() {
Expand Down
18 changes: 15 additions & 3 deletions photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,34 @@ public static void main(String[] args) {

try {
if (Platform.isRaspberryPi()) {
LibCameraJNILoader.forceLoad();
if (LibCameraJNILoader.forceLoad()) {
logger.info("libcamera-JNI loaded successfully.");
} else {
logger.error("Failed to load libcamera-JNI!");
}
}
} catch (IOException e) {
logger.error("Failed to load libcamera-JNI!", e);
}
try {
if (Platform.isRK3588()) {
RknnDetectorJNI.forceLoad();
if (RknnDetectorJNI.forceLoad()) {
logger.info("RKNN-JNI loaded successfully.");
} else {
logger.error("Failed to load RKNN-JNI!");
}
} else {
logger.error("Platform does not support RKNN based machine learning!");
}
} catch (IOException e) {
logger.error("Failed to load rknn-JNI!", e);
}
try {
MrCalJNILoader.forceLoad();
if (MrCalJNILoader.forceLoad()) {
logger.info("mrcal-JNI loaded successfully.");
} else {
logger.error("Failed to load mrcal-JNI!");
}
} catch (IOException e) {
logger.warn(
"Failed to load mrcal-JNI! Camera calibration will fall back to opencv\n"
Expand Down
Loading