From 186b56cf961e5db4c3e1eeede1b3afc8b0d3a373 Mon Sep 17 00:00:00 2001 From: Sebastian Erives Date: Tue, 5 Nov 2024 15:32:55 -0600 Subject: [PATCH] TunableField caching & improvement of field context getting --- .../plugin/PaperVisionEOCVSimPlugin.kt | 44 ++++++++++++------- .../ipc/eocvsim/EOCVSimPrevizSession.kt | 10 +++-- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/EOCVSimPlugin/src/main/kotlin/io/github/deltacv/papervision/plugin/PaperVisionEOCVSimPlugin.kt b/EOCVSimPlugin/src/main/kotlin/io/github/deltacv/papervision/plugin/PaperVisionEOCVSimPlugin.kt index 68c7e73..e1744d7 100644 --- a/EOCVSimPlugin/src/main/kotlin/io/github/deltacv/papervision/plugin/PaperVisionEOCVSimPlugin.kt +++ b/EOCVSimPlugin/src/main/kotlin/io/github/deltacv/papervision/plugin/PaperVisionEOCVSimPlugin.kt @@ -23,6 +23,7 @@ import com.github.serivesmejia.eocvsim.EOCVSim import com.github.serivesmejia.eocvsim.gui.DialogFactory import com.github.serivesmejia.eocvsim.input.SourceType import com.github.serivesmejia.eocvsim.pipeline.PipelineSource +import com.github.serivesmejia.eocvsim.tuner.TunableField import com.github.serivesmejia.eocvsim.util.loggerForThis import io.github.deltacv.eocvsim.pipeline.StreamableOpenCvPipelineInstantiator import io.github.deltacv.eocvsim.plugin.EOCVSimPlugin @@ -50,10 +51,9 @@ import io.github.deltacv.papervision.util.replaceLast import io.github.deltacv.papervision.util.toValidIdentifier import io.javalin.Javalin import org.eclipse.jetty.server.Server -import org.eclipse.jetty.server.handler.HandlerWrapper -import org.eclipse.jetty.server.handler.StatisticsHandler import org.opencv.core.Size import java.io.File +import java.util.WeakHashMap import java.util.concurrent.Executors import javax.swing.JLabel import javax.swing.JOptionPane @@ -75,6 +75,8 @@ class PaperVisionEOCVSimPlugin : EOCVSimPlugin() { var currentPrevizSession: EOCVSimPrevizSession? = null + private val tunableFieldCache = WeakHashMap>() + /** * If the plugin comes from a file, we will just use the file classpath, since it's a single fat jar. * If the plugin comes from Maven, we will use the classpath of all the transitive dependencies. @@ -248,12 +250,35 @@ class PaperVisionEOCVSimPlugin : EOCVSimPlugin() { } override fun onEnable() { + fun tunableFieldOf(field: VirtualField): TunableField<*> { + if(tunableFieldCache.containsKey(field)) { + return tunableFieldCache[field]!! + } + + val tunableFieldClass = eocvSim.tunerManager.getTunableFieldOf(field) + + val tunableField = tunableFieldClass.getConstructor( + Object::class.java, + VirtualField::class.java, + EOCVSim::class.java + ).newInstance( + currentPrevizSession!!.latestPipeline, + field, + eocvSim + ) + + tunableFieldCache[field] = tunableField + return tunableField + } + engine.setMessageHandlerOf { eocvSim.onMainUpdate.doOnce { val field = currentPrevizSession?.latestVirtualReflect?.getLabeledField(message.label) if (field != null) { - field.set(message.value) + val tunableField = tunableFieldOf(field) + + tunableField.setFieldValue(0, message.value) logger.info("Received tuner change value message for ${message.label} to ${message.value} ") } @@ -266,24 +291,13 @@ class PaperVisionEOCVSimPlugin : EOCVSimPlugin() { val field = currentPrevizSession?.latestVirtualReflect?.getLabeledField(message.label) if(field != null) { - val tunableFieldClass = eocvSim.tunerManager.getTunableFieldOf(field) - - val tunableField = tunableFieldClass.getConstructor( - Object::class.java, - VirtualField::class.java, - EOCVSim::class.java - ).newInstance( - currentPrevizSession!!.latestPipeline, - field, - eocvSim - ) + val tunableField = tunableFieldOf(field) for (i in message.values.indices) { tunableField.setFieldValue(i, message.values[i]) } logger.info("Received tuner change values message for ${message.label} to ${message.values}") - } respond(OkResponse()) diff --git a/EOCVSimPlugin/src/main/kotlin/io/github/deltacv/papervision/plugin/ipc/eocvsim/EOCVSimPrevizSession.kt b/EOCVSimPlugin/src/main/kotlin/io/github/deltacv/papervision/plugin/ipc/eocvsim/EOCVSimPrevizSession.kt index 99eb44d..a616f77 100644 --- a/EOCVSimPlugin/src/main/kotlin/io/github/deltacv/papervision/plugin/ipc/eocvsim/EOCVSimPrevizSession.kt +++ b/EOCVSimPlugin/src/main/kotlin/io/github/deltacv/papervision/plugin/ipc/eocvsim/EOCVSimPrevizSession.kt @@ -23,7 +23,6 @@ import com.github.serivesmejia.eocvsim.pipeline.PipelineManager import com.github.serivesmejia.eocvsim.pipeline.PipelineSource import com.github.serivesmejia.eocvsim.util.loggerForThis import io.github.deltacv.eocvsim.pipeline.StreamableOpenCvPipeline -import io.github.deltacv.eocvsim.pipeline.StreamableOpenCvPipelineInstantiator import io.github.deltacv.eocvsim.stream.ImageStreamer import io.github.deltacv.eocvsim.virtualreflect.VirtualReflectContext import io.github.deltacv.eocvsim.virtualreflect.jvm.JvmVirtualReflection @@ -75,8 +74,13 @@ class EOCVSimPrevizSession( eocvSim.pipelineManager.forceChangePipeline(eocvSim.pipelineManager.getIndexOf(latestClass!!, PipelineSource.COMPILED_ON_RUNTIME)) - latestPipeline = eocvSim.pipelineManager.currentPipeline!! - latestVirtualReflect = JvmVirtualReflection.contextOf(latestPipeline!!) + latestPipeline = eocvSim.pipelineManager.currentPipeline + + latestVirtualReflect = if(latestPipeline != null) { + JvmVirtualReflection.contextOf(latestPipeline!!) + } else { + null + } refreshPrevizPipelineStreamer()