diff --git a/cscore/src/main/java/edu/wpi/cscore/CameraServerCvJNI.java b/cscore/src/main/java/edu/wpi/cscore/CameraServerCvJNI.java index d10d5b0ed7..78b4ff8296 100644 --- a/cscore/src/main/java/edu/wpi/cscore/CameraServerCvJNI.java +++ b/cscore/src/main/java/edu/wpi/cscore/CameraServerCvJNI.java @@ -8,32 +8,56 @@ package edu.wpi.cscore; import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; import org.opencv.core.Core; import edu.wpi.first.wpiutil.RuntimeLoader; public class CameraServerCvJNI { - static boolean cvLibraryLoaded = false; + static boolean libraryLoaded = false; - static RuntimeLoader cvLoader = null; + static RuntimeLoader loader = null; + + public static class Helper { + private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); + + public static boolean getExtractOnStaticLoad() { + return extractOnStaticLoad.get(); + } + + public static void setExtractOnStaticLoad(boolean load) { + extractOnStaticLoad.set(load); + } + } static { String opencvName = Core.NATIVE_LIBRARY_NAME; - if (!cvLibraryLoaded) { - CameraServerJNI.forceLoad(); + if (Helper.getExtractOnStaticLoad()) { try { - cvLoader = new RuntimeLoader<>(opencvName, RuntimeLoader.getDefaultExtractionRoot(), Core.class); - cvLoader.loadLibraryHashed(); + CameraServerJNI.forceLoad(); + loader = new RuntimeLoader<>(opencvName, RuntimeLoader.getDefaultExtractionRoot(), Core.class); + loader.loadLibraryHashed(); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } - cvLibraryLoaded = true; + libraryLoaded = true; } } - public static void forceLoad() {} + /** + * Force load the library. + */ + public static synchronized void forceLoad() throws IOException { + if (libraryLoaded) { + return; + } + CameraServerJNI.forceLoad(); + loader = new RuntimeLoader<>(Core.NATIVE_LIBRARY_NAME, RuntimeLoader.getDefaultExtractionRoot(), Core.class); + loader.loadLibrary(); + libraryLoaded = true; + } public static native int createCvSource(String name, int pixelFormat, int width, int height, int fps); diff --git a/cscore/src/main/java/edu/wpi/cscore/CameraServerJNI.java b/cscore/src/main/java/edu/wpi/cscore/CameraServerJNI.java index 9f4fea6c36..ac18408fa2 100644 --- a/cscore/src/main/java/edu/wpi/cscore/CameraServerJNI.java +++ b/cscore/src/main/java/edu/wpi/cscore/CameraServerJNI.java @@ -9,6 +9,7 @@ package edu.wpi.cscore; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import edu.wpi.cscore.raw.RawFrame; @@ -19,8 +20,20 @@ public class CameraServerJNI { static RuntimeLoader loader = null; + public static class Helper { + private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); + + public static boolean getExtractOnStaticLoad() { + return extractOnStaticLoad.get(); + } + + public static void setExtractOnStaticLoad(boolean load) { + extractOnStaticLoad.set(load); + } + } + static { - if (!libraryLoaded) { + if (Helper.getExtractOnStaticLoad()) { try { loader = new RuntimeLoader<>("cscorejni", RuntimeLoader.getDefaultExtractionRoot(), CameraServerJNI.class); loader.loadLibrary(); @@ -32,7 +45,17 @@ public class CameraServerJNI { } } - public static void forceLoad() {} + /** + * Force load the library. + */ + public static synchronized void forceLoad() throws IOException { + if (libraryLoaded) { + return; + } + loader = new RuntimeLoader<>("cscorejni", RuntimeLoader.getDefaultExtractionRoot(), CameraServerJNI.class); + loader.loadLibrary(); + libraryLoaded = true; + } // // Property Functions diff --git a/hal/src/main/java/edu/wpi/first/hal/JNIWrapper.java b/hal/src/main/java/edu/wpi/first/hal/JNIWrapper.java index 21342bc9e5..63ad39fc31 100644 --- a/hal/src/main/java/edu/wpi/first/hal/JNIWrapper.java +++ b/hal/src/main/java/edu/wpi/first/hal/JNIWrapper.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -8,6 +8,7 @@ package edu.wpi.first.hal; import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; import edu.wpi.first.wpiutil.RuntimeLoader; @@ -18,8 +19,20 @@ public class JNIWrapper { static boolean libraryLoaded = false; static RuntimeLoader loader = null; + public static class Helper { + private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); + + public static boolean getExtractOnStaticLoad() { + return extractOnStaticLoad.get(); + } + + public static void setExtractOnStaticLoad(boolean load) { + extractOnStaticLoad.set(load); + } + } + static { - if (!libraryLoaded) { + if (Helper.getExtractOnStaticLoad()) { try { loader = new RuntimeLoader<>("wpiHaljni", RuntimeLoader.getDefaultExtractionRoot(), JNIWrapper.class); loader.loadLibrary(); @@ -28,7 +41,18 @@ public class JNIWrapper { System.exit(1); } libraryLoaded = true; - libraryLoaded = true; } } + + /** + * Force load the library. + */ + public static synchronized void forceLoad() throws IOException { + if (libraryLoaded) { + return; + } + loader = new RuntimeLoader<>("wpiHaljni", RuntimeLoader.getDefaultExtractionRoot(), JNIWrapper.class); + loader.loadLibrary(); + libraryLoaded = true; + } } diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java index 1d736fc14d..d24f06680b 100644 --- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java +++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -9,6 +9,7 @@ package edu.wpi.first.networktables; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.concurrent.atomic.AtomicBoolean; import edu.wpi.first.wpiutil.RuntimeLoader; @@ -16,8 +17,20 @@ public final class NetworkTablesJNI { static boolean libraryLoaded = false; static RuntimeLoader loader = null; + public static class Helper { + private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); + + public static boolean getExtractOnStaticLoad() { + return extractOnStaticLoad.get(); + } + + public static void setExtractOnStaticLoad(boolean load) { + extractOnStaticLoad.set(load); + } + } + static { - if (!libraryLoaded) { + if (Helper.getExtractOnStaticLoad()) { try { loader = new RuntimeLoader<>("ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class); loader.loadLibrary(); @@ -29,6 +42,18 @@ public final class NetworkTablesJNI { } } + /** + * Force load the library. + */ + public static synchronized void forceLoad() throws IOException { + if (libraryLoaded) { + return; + } + loader = new RuntimeLoader<>("ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class); + loader.loadLibrary(); + libraryLoaded = true; + } + public static native int getDefaultInstance(); public static native int createInstance(); public static native void destroyInstance(int inst);