From 10c80da398ba0a6fdf62ea2b05c1e5f6488b0402 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Wed, 4 Nov 2026 16:12:52 +0100 Subject: [PATCH 01/49] Limit the public extension points to hardware plugins and Python processing --- CMakeLists.txt | 19 +- CONTRIBUTING.md | 18 +- README.md | 2 +- ScopeOneCore/CMakeLists.txt | 37 +- ScopeOneCore/DRIVERHOST_PROTOCOL.md | 132 +++ ScopeOneCore/README.md | 34 +- ScopeOneCore/cmake/ScopeOnePlugin.cmake | 8 +- ScopeOneCore/include/scopeone/DaqDevice.h | 12 - .../include/scopeone}/DhmReconstruction.h | 13 +- .../{PluginSDK.h => HardwarePluginSDK.h} | 10 +- ScopeOneCore/include/scopeone/ImageFrame.h | 80 -- .../include/scopeone/PluginManifest.h | 2 - ScopeOneCore/include/scopeone/SharedFrame.h | 88 ++ ScopeOneCore/include/scopeone/SignalSource.h | 12 - .../include/scopeone/SimulatorProvider.h | 101 --- .../include/scopeone/ToolFrameStream.h | 38 - ScopeOneCore/include/scopeone/ToolPlugin.h | 124 --- ScopeOneCore/include/scopeone/ToolTask.h | 37 - ScopeOneCore/internal/DaqDeviceManager.h | 20 +- .../internal/IscatModule.h | 6 +- ScopeOneCore/internal/NIDaqmx.h | 15 + ScopeOneCore/internal/PtuFile.h | 15 + .../internal/PythonProcessingModule.h | 16 + ScopeOneCore/internal/SharedFrameRing.h | 2 +- ScopeOneCore/internal/SignalSourceManager.h | 18 +- ScopeOneCore/internal/SimulatedPmt.h | 15 + .../internal}/SimulatedPmtSource.h | 2 +- .../scopeone/examples/processing_offset.py | 0 ScopeOneCore/src/DaqDeviceManager.cpp | 56 +- .../src}/DhmReconstruction.cpp | 54 +- ScopeOneCore/src/DriverHostMain.cpp | 2 +- .../src/IscatModule.cpp | 36 +- .../src/NIDaqmx.cpp | 8 +- ScopeOneCore/src/PluginManifest.cpp | 15 +- ScopeOneCore/src/ProcessingModuleRegistry.cpp | 22 +- .../src/PtuFile.cpp | 8 +- ScopeOneCore/src/PythonProcessingModule.cpp | 292 +++++++ ScopeOneCore/src/ScopeOneCore.cpp | 3 + ScopeOneCore/src/SignalSourceManager.cpp | 53 +- .../src/SimulatedPmt.cpp | 10 +- .../src}/SimulatedPmtSource.cpp | 4 +- ScopeOneCore/src/SimulatorProvider.cpp | 761 ------------------ ScopeOneCore/src/ToolFrameStream.cpp | 64 -- ScopeOneCore/src/ToolPlugin.cpp | 119 --- ScopeOneCore/src/ToolTask.cpp | 83 -- .../src/CudaFrequencyFilterModule.cpp | 2 +- ScopeOneCuda/src/CudaFrequencyFilterModule.h | 2 +- ScopeOneCuda/src/CudaGaussianBlurModule.cpp | 2 +- ScopeOneCuda/src/CudaGaussianBlurModule.h | 2 +- .../src/CudaProcessingRegistration.cpp | 2 +- plugins/CMakeLists.txt | 2 - plugins/README.md | 85 +- plugins/hardware/CMakeLists.txt | 5 +- plugins/hardware/ExampleCamera/CMakeLists.txt | 8 + .../ExampleCamera/ExampleCameraPlugin.cpp | 604 ++++++++++++++ .../hardware/ExampleCamera/TestPatterns.cpp | 329 ++++++++ plugins/hardware/ExampleCamera/TestPatterns.h | 51 ++ plugins/hardware/ExampleCamera/plugin.json | 9 + .../hardware/ExampleHardware/CMakeLists.txt | 6 - .../ExampleHardware/ExampleHardwarePlugin.cpp | 316 -------- plugins/hardware/ExampleHardware/plugin.json | 9 - plugins/hardware/NIDaqmx/CMakeLists.txt | 11 - plugins/hardware/NIDaqmx/NIDaqmxPlugin.h | 22 - plugins/hardware/NIDaqmx/plugin.json | 8 - plugins/hardware/PtuFile/CMakeLists.txt | 11 - plugins/hardware/PtuFile/PtuFilePlugin.h | 22 - plugins/hardware/PtuFile/plugin.json | 8 - plugins/hardware/SimulatedPmt/CMakeLists.txt | 14 - .../SimulatedPmt/SimulatedPmtPlugin.h | 22 - plugins/hardware/SimulatedPmt/plugin.json | 9 - plugins/processing/CMakeLists.txt | 6 - .../ExampleCudaProcessing/CMakeLists.txt | 16 - .../ExampleCudaKernel.cu | 52 -- .../ExampleCudaProcessingPlugin.cpp | 74 -- .../ExampleCudaProcessing/plugin.json | 7 - .../ExampleProcessing/CMakeLists.txt | 6 - .../ExampleProcessingPlugin.cpp | 49 -- .../processing/ExampleProcessing/plugin.json | 7 - .../processing/IscatProcessing/CMakeLists.txt | 18 - .../IscatProcessing/IscatProcessingPlugin.cpp | 124 --- .../processing/IscatProcessing/plugin.json | 7 - .../PythonProcessing/CMakeLists.txt | 7 - .../PythonProcessingPlugin.cpp | 301 ------- .../processing/PythonProcessing/plugin.json | 7 - .../BrillouinTool/BrillouinToolPlugin.cpp | 389 --------- plugins/tools/BrillouinTool/CMakeLists.txt | 11 - plugins/tools/BrillouinTool/plugin.json | 7 - plugins/tools/CMakeLists.txt | 4 - plugins/tools/DhmTool/CMakeLists.txt | 15 - plugins/tools/DhmTool/plugin.json | 7 - plugins/tools/ExampleTool/CMakeLists.txt | 6 - .../tools/ExampleTool/ExampleToolPlugin.cpp | 43 - plugins/tools/ExampleTool/plugin.json | 7 - plugins/tools/ScanningDaqTool/CMakeLists.txt | 14 - .../ScanningDaqTool/ScanningDaqToolPlugin.cpp | 49 -- plugins/tools/ScanningDaqTool/plugin.json | 7 - src/MainWindow.cpp | 39 +- src/MainWindow.h | 6 +- src/PluginManagerDialog.cpp | 8 +- src/ScopeOneLocalApiServer.cpp | 5 +- ...copeOneToolPlugin.cpp => ToolRegistry.cpp} | 115 +-- src/{ScopeOneToolPlugin.h => ToolRegistry.h} | 10 +- src/tools/ToolCatalog.cpp | 60 ++ src/tools/ToolContext.h | 78 ++ .../tools/daq}/DaqControlWidget.cpp | 24 +- .../tools/daq}/DaqControlWidget.h | 10 +- .../tools/daq}/SignalMonitorWidget.cpp | 30 +- .../tools/daq}/SignalMonitorWidget.h | 12 +- .../tools/dhm/DhmTool.cpp | 280 ++++--- src/{ => tools/mosaic}/StageMosaicDialog.cpp | 13 +- src/{ => tools/mosaic}/StageMosaicDialog.h | 0 .../particles}/ParticleDetectionDialog.cpp | 14 +- .../particles}/ParticleDetectionDialog.h | 0 src/{ => tools/scale}/CameraScaleDialog.cpp | 13 +- src/{ => tools/scale}/CameraScaleDialog.h | 0 115 files changed, 2272 insertions(+), 3712 deletions(-) create mode 100644 ScopeOneCore/DRIVERHOST_PROTOCOL.md rename {plugins/tools/DhmTool => ScopeOneCore/include/scopeone}/DhmReconstruction.h (83%) rename ScopeOneCore/include/scopeone/{PluginSDK.h => HardwarePluginSDK.h} (50%) delete mode 100644 ScopeOneCore/include/scopeone/SimulatorProvider.h delete mode 100644 ScopeOneCore/include/scopeone/ToolFrameStream.h delete mode 100644 ScopeOneCore/include/scopeone/ToolPlugin.h delete mode 100644 ScopeOneCore/include/scopeone/ToolTask.h rename plugins/processing/IscatProcessing/IscatProcessingModule.h => ScopeOneCore/internal/IscatModule.h (94%) create mode 100644 ScopeOneCore/internal/NIDaqmx.h create mode 100644 ScopeOneCore/internal/PtuFile.h create mode 100644 ScopeOneCore/internal/PythonProcessingModule.h create mode 100644 ScopeOneCore/internal/SimulatedPmt.h rename {plugins/hardware/SimulatedPmt => ScopeOneCore/internal}/SimulatedPmtSource.h (98%) rename plugins/processing/PythonProcessing/examples/offset.py => ScopeOneCore/python/scopeone/examples/processing_offset.py (100%) rename {plugins/tools/DhmTool => ScopeOneCore/src}/DhmReconstruction.cpp (98%) rename plugins/processing/IscatProcessing/IscatProcessingModule.cpp => ScopeOneCore/src/IscatModule.cpp (91%) rename plugins/hardware/NIDaqmx/NIDaqmxPlugin.cpp => ScopeOneCore/src/NIDaqmx.cpp (99%) rename plugins/hardware/PtuFile/PtuFilePlugin.cpp => ScopeOneCore/src/PtuFile.cpp (99%) create mode 100644 ScopeOneCore/src/PythonProcessingModule.cpp rename plugins/hardware/SimulatedPmt/SimulatedPmtPlugin.cpp => ScopeOneCore/src/SimulatedPmt.cpp (94%) rename {plugins/hardware/SimulatedPmt => ScopeOneCore/src}/SimulatedPmtSource.cpp (99%) delete mode 100644 ScopeOneCore/src/SimulatorProvider.cpp delete mode 100644 ScopeOneCore/src/ToolFrameStream.cpp delete mode 100644 ScopeOneCore/src/ToolPlugin.cpp delete mode 100644 ScopeOneCore/src/ToolTask.cpp create mode 100644 plugins/hardware/ExampleCamera/CMakeLists.txt create mode 100644 plugins/hardware/ExampleCamera/ExampleCameraPlugin.cpp create mode 100644 plugins/hardware/ExampleCamera/TestPatterns.cpp create mode 100644 plugins/hardware/ExampleCamera/TestPatterns.h create mode 100644 plugins/hardware/ExampleCamera/plugin.json delete mode 100644 plugins/hardware/ExampleHardware/CMakeLists.txt delete mode 100644 plugins/hardware/ExampleHardware/ExampleHardwarePlugin.cpp delete mode 100644 plugins/hardware/ExampleHardware/plugin.json delete mode 100644 plugins/hardware/NIDaqmx/CMakeLists.txt delete mode 100644 plugins/hardware/NIDaqmx/NIDaqmxPlugin.h delete mode 100644 plugins/hardware/NIDaqmx/plugin.json delete mode 100644 plugins/hardware/PtuFile/CMakeLists.txt delete mode 100644 plugins/hardware/PtuFile/PtuFilePlugin.h delete mode 100644 plugins/hardware/PtuFile/plugin.json delete mode 100644 plugins/hardware/SimulatedPmt/CMakeLists.txt delete mode 100644 plugins/hardware/SimulatedPmt/SimulatedPmtPlugin.h delete mode 100644 plugins/hardware/SimulatedPmt/plugin.json delete mode 100644 plugins/processing/CMakeLists.txt delete mode 100644 plugins/processing/ExampleCudaProcessing/CMakeLists.txt delete mode 100644 plugins/processing/ExampleCudaProcessing/ExampleCudaKernel.cu delete mode 100644 plugins/processing/ExampleCudaProcessing/ExampleCudaProcessingPlugin.cpp delete mode 100644 plugins/processing/ExampleCudaProcessing/plugin.json delete mode 100644 plugins/processing/ExampleProcessing/CMakeLists.txt delete mode 100644 plugins/processing/ExampleProcessing/ExampleProcessingPlugin.cpp delete mode 100644 plugins/processing/ExampleProcessing/plugin.json delete mode 100644 plugins/processing/IscatProcessing/CMakeLists.txt delete mode 100644 plugins/processing/IscatProcessing/IscatProcessingPlugin.cpp delete mode 100644 plugins/processing/IscatProcessing/plugin.json delete mode 100644 plugins/processing/PythonProcessing/CMakeLists.txt delete mode 100644 plugins/processing/PythonProcessing/PythonProcessingPlugin.cpp delete mode 100644 plugins/processing/PythonProcessing/plugin.json delete mode 100644 plugins/tools/BrillouinTool/BrillouinToolPlugin.cpp delete mode 100644 plugins/tools/BrillouinTool/CMakeLists.txt delete mode 100644 plugins/tools/BrillouinTool/plugin.json delete mode 100644 plugins/tools/CMakeLists.txt delete mode 100644 plugins/tools/DhmTool/CMakeLists.txt delete mode 100644 plugins/tools/DhmTool/plugin.json delete mode 100644 plugins/tools/ExampleTool/CMakeLists.txt delete mode 100644 plugins/tools/ExampleTool/ExampleToolPlugin.cpp delete mode 100644 plugins/tools/ExampleTool/plugin.json delete mode 100644 plugins/tools/ScanningDaqTool/CMakeLists.txt delete mode 100644 plugins/tools/ScanningDaqTool/ScanningDaqToolPlugin.cpp delete mode 100644 plugins/tools/ScanningDaqTool/plugin.json rename src/{ScopeOneToolPlugin.cpp => ToolRegistry.cpp} (50%) rename src/{ScopeOneToolPlugin.h => ToolRegistry.h} (63%) create mode 100644 src/tools/ToolCatalog.cpp create mode 100644 src/tools/ToolContext.h rename {plugins/tools/ScanningDaqTool => src/tools/daq}/DaqControlWidget.cpp (97%) rename {plugins/tools/ScanningDaqTool => src/tools/daq}/DaqControlWidget.h (92%) rename {plugins/tools/ScanningDaqTool => src/tools/daq}/SignalMonitorWidget.cpp (96%) rename {plugins/tools/ScanningDaqTool => src/tools/daq}/SignalMonitorWidget.h (90%) rename plugins/tools/DhmTool/DhmToolPlugin.cpp => src/tools/dhm/DhmTool.cpp (78%) rename src/{ => tools/mosaic}/StageMosaicDialog.cpp (95%) rename src/{ => tools/mosaic}/StageMosaicDialog.h (100%) rename src/{ => tools/particles}/ParticleDetectionDialog.cpp (95%) rename src/{ => tools/particles}/ParticleDetectionDialog.h (100%) rename src/{ => tools/scale}/CameraScaleDialog.cpp (90%) rename src/{ => tools/scale}/CameraScaleDialog.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8fb9c02..84f0673 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,16 +64,14 @@ endif () set(SCOPEONE_UI_SOURCES src/main.cpp - src/CameraScaleDialog.cpp src/ConfigPresetWidget.cpp src/DeviceControlWidget.cpp src/DevicePropertyWidget.cpp src/PositionListFile.cpp src/RecordingWidget.cpp - src/StageMosaicDialog.cpp src/PluginManagerDialog.cpp src/ScopeOneLocalApiServer.cpp - src/ScopeOneToolPlugin.cpp + src/ToolRegistry.cpp src/AboutDialog.cpp src/Appearance.cpp src/CollapsibleSection.cpp @@ -94,7 +92,6 @@ set(SCOPEONE_UI_SOURCES src/InspectWidget.cpp src/OrthogonalViewsDialog.cpp src/ParameterDialog.cpp - src/ParticleDetectionDialog.cpp src/PreviewWidget.cpp src/ProfilePlotWindow.cpp src/ResultsWindow.cpp @@ -102,17 +99,22 @@ set(SCOPEONE_UI_SOURCES ${CMAKE_SOURCE_DIR}/resources/resources.qrc ) +# Every tool in src/tools registers itself, so a new tool needs no change here +file(GLOB_RECURSE SCOPEONE_TOOL_FILES CONFIGURE_DEPENDS + "${CMAKE_SOURCE_DIR}/src/tools/*.cpp" + "${CMAKE_SOURCE_DIR}/src/tools/*.h" +) +list(APPEND SCOPEONE_UI_SOURCES ${SCOPEONE_TOOL_FILES}) + set(SCOPEONE_UI_HEADERS - src/CameraScaleDialog.h src/ConfigPresetWidget.h src/DeviceControlWidget.h src/DevicePropertyWidget.h src/PositionListFile.h src/RecordingWidget.h - src/StageMosaicDialog.h src/PluginManagerDialog.h src/ScopeOneLocalApiServer.h - src/ScopeOneToolPlugin.h + src/ToolRegistry.h src/AboutDialog.h src/Appearance.h src/CollapsibleSection.h @@ -133,7 +135,6 @@ set(SCOPEONE_UI_HEADERS src/InspectWidget.h src/OrthogonalViewsDialog.h src/ParameterDialog.h - src/ParticleDetectionDialog.h src/PreviewWidget.h src/ProfilePlotWindow.h src/ResultsWindow.h @@ -264,7 +265,7 @@ if (UNIX AND NOT APPLE) ) endif () -foreach (_plugin_kind IN ITEMS processing hardware tools) +foreach (_plugin_kind IN ITEMS processing hardware) set(_plugin_dir "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/plugins/${_plugin_kind}") if (EXISTS "${_plugin_dir}") add_custom_command(TARGET ScopeOne POST_BUILD diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 11d10aa..cf89f70 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,7 +13,7 @@ disagree in a specific case, follow the reason and say so in the review. | Core | `ScopeOneCore` | `scopeone::core`, `core::internal`, `core::imageops` | Qt Core, OpenCV, MMCore, ScopeWriter. No Qt Widgets. | | Application | `src` | `scopeone::ui` | Core, Qt Widgets | | MCP server | `src/ScopeOneMcpServer.cpp` | | Qt Core, Qt Network and the Local API protocol. Not Core. | -| Plugins | `plugins`, `ScopeOneCuda`, `ScopeOneInference` | their own | `scopeone::PluginSDK` | +| Plugins | `plugins`, `ScopeOneCuda`, `ScopeOneInference` | `scopeone::plugins::cuda` for CUDA, `scopeone::inference` for ONNX, their own for hardware plugins | `scopeone::HardwarePluginSDK` for hardware plugins, `scopeone::ScopeOneCore` for the CUDA and ONNX plugins | | Python package | `ScopeOneCore/python/scopeone` | `scopeone` | the Local API protocol | A lower layer never knows about a higher one. Core does not include from `src`, and plugins do @@ -21,7 +21,7 @@ not include Core internals. This keeps each part buildable and usable without th **Entry points.** Code outside Core enters through the public headers in `ScopeOneCore/include/scopeone` and the `ScopeOneCore` facade. `ScopeOneCore/internal` is for Core -alone. Plugins see only the SDK headers; a tool plugin gets `ScopeOneToolCore`, not the Core. +alone. Plugins see only the SDK headers. A narrow entry point is what lets Core change its insides without breaking callers. **Decide the layer first.** Before writing a feature, decide where it belongs. Anything that @@ -29,6 +29,20 @@ must work without a window (acquisition, hardware, processing, recording, data) and is reachable through the Local API before it gets a widget. Anything that only changes what is shown goes into `src`. Hardware and optional processing go into plugins. +**Tools.** A window of the Tools menu, with its own state and workflow, goes into its own folder +under `src/tools`. It uses only the public Core headers and `ScopeOneToolContext` from +`src/tools/ToolContext.h`, and registers itself with `SCOPEONE_REGISTER_TOOL` at the end of its +source file, so adding a tool changes neither `MainWindow` nor CMake. Its algorithm belongs in Core. +A command that takes an image and parameters and returns a result is not a tool; it belongs in the +image menus. + +**Extension boundary.** Hardware plugins and Python processing modules are the only public +extension points, and both run outside the ScopeOne process. The CUDA and ONNX processing plugins +exist only to keep CUDA and ONNX Runtime out of Core, and their interface is internal to this +repository. Everything without such a dependency belongs in Core, with its window in `src`. The +messages between ScopeOne and `ScopeOne_DriverHost` are internal too and are described in +`ScopeOneCore/DRIVERHOST_PROTOCOL.md`. + **Python API.** The `ScopeOne` client controls a running ScopeOne and moves frames in and out as numpy arrays. It does not reimplement analysis that numpy already does. `scopeone.processing` is the other direction: ScopeOne runs Python functions as pipeline modules. diff --git a/README.md b/README.md index 9a48877..7ea4a9e 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ ScopeOne/ ### Plugin layout -ScopeOne loads plugins from `plugins/processing`, `plugins/tools` and `plugins/hardware` beside the application, and Python processing modules from `plugins/processing/python`. All plugins build against the installed `scopeone::PluginSDK` target and the contracts in `ScopeOneCore/include/scopeone`. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files, while other native cameras use the `HardwareProvider` contract. [plugins/README.md](plugins/README.md) explains how to write, build and install a plugin. +ScopeOne has two public extension points, hardware plugins and Python processing modules, and both run outside the ScopeOne process. Hardware plugins are loaded from `plugins/hardware` by `ScopeOne_DriverHost`, one process per device, and Python modules from `plugins/processing/python`. The CUDA and ONNX processing plugins are built and released with ScopeOne. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [plugins/README.md](plugins/README.md) explains how to write, build and install an extension. **Windows Build Steps:** diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index 71330e3..3112e43 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -76,7 +76,6 @@ set(CORE_SOURCES src/PluginManifest.cpp src/HardwareRuntime.cpp src/MicroManagerProvider.cpp - src/SimulatorProvider.cpp src/ImageOps.cpp src/ImageSceneModel.cpp src/MMCoreManager.cpp @@ -85,9 +84,7 @@ set(CORE_SOURCES src/StageMosaicManager.cpp src/ImageProcessingFramework.cpp src/ProcessingPipeline.cpp - src/ToolTask.cpp - src/ToolFrameStream.cpp - src/ToolPlugin.cpp + src/DhmReconstruction.cpp src/ProcessingModuleRegistry.cpp src/SpatiotemporalBinningModule.cpp src/GaussianBlurModule.cpp @@ -98,6 +95,12 @@ set(CORE_SOURCES src/IFFTModule.cpp src/BackgroundCalibrationModule.cpp src/DifferentialRollingModule.cpp + src/IscatModule.cpp + src/PythonProcessingModule.cpp + src/NIDaqmx.cpp + src/PtuFile.cpp + src/SimulatedPmt.cpp + src/SimulatedPmtSource.cpp src/MDAManager.cpp src/RecordingManager.cpp src/CameraBackend.cpp @@ -119,18 +122,15 @@ set(CORE_HEADERS include/scopeone/HardwareTypes.h include/scopeone/ImageFrame.h include/scopeone/PluginManifest.h - include/scopeone/PluginSDK.h + include/scopeone/HardwarePluginSDK.h include/scopeone/ProcessingPlugin.h include/scopeone/ScanImageAssembler.h include/scopeone/scopeone_sdk_export.h include/scopeone/SharedFrame.h include/scopeone/SignalSource.h - include/scopeone/ToolFrameStream.h - include/scopeone/ToolPlugin.h - include/scopeone/ToolTask.h + include/scopeone/DhmReconstruction.h internal/AcquisitionEngine.h internal/CameraRuntimeControl.h - include/scopeone/SimulatorProvider.h include/scopeone/ImageOps.h include/scopeone/ImageSceneModel.h internal/HardwareRuntime.h @@ -157,6 +157,12 @@ set(CORE_HEADERS internal/IFFTModule.h internal/BackgroundCalibrationModule.h internal/DifferentialRollingModule.h + internal/IscatModule.h + internal/PythonProcessingModule.h + internal/NIDaqmx.h + internal/PtuFile.h + internal/SimulatedPmt.h + internal/SimulatedPmtSource.h internal/MDAManager.h internal/RecordingManager.h internal/CameraBackend.h @@ -168,8 +174,8 @@ set(CORE_HEADERS add_library(ScopeOneCore SHARED ${CORE_SOURCES} ${CORE_HEADERS}) add_library(scopeone::ScopeOneCore ALIAS ScopeOneCore) -add_library(ScopeOnePluginSDK INTERFACE) -add_library(scopeone::PluginSDK ALIAS ScopeOnePluginSDK) +add_library(ScopeOneHardwarePluginSDK INTERFACE) +add_library(scopeone::HardwarePluginSDK ALIAS ScopeOneHardwarePluginSDK) if (UNIX AND NOT APPLE) set_target_properties(ScopeWriter ScopeOneCore PROPERTIES BUILD_RPATH "$ORIGIN" @@ -177,9 +183,10 @@ if (UNIX AND NOT APPLE) INSTALL_RPATH_USE_LINK_PATH TRUE ) endif () -set_target_properties(ScopeOnePluginSDK PROPERTIES EXPORT_NAME PluginSDK) -target_link_libraries(ScopeOnePluginSDK INTERFACE ScopeOneCore Qt::Core Qt::Gui) -target_include_directories(ScopeOnePluginSDK INTERFACE +# The public target for hardware plugins outside this repository +set_target_properties(ScopeOneHardwarePluginSDK PROPERTIES EXPORT_NAME HardwarePluginSDK) +target_link_libraries(ScopeOneHardwarePluginSDK INTERFACE ScopeOneCore Qt::Core Qt::Gui) +target_include_directories(ScopeOneHardwarePluginSDK INTERFACE $ $ ) @@ -311,7 +318,7 @@ write_basic_package_version_file( COMPATIBILITY SameMajorVersion ) -install(TARGETS ScopeOneCore ScopeOnePluginSDK +install(TARGETS ScopeOneCore ScopeOneHardwarePluginSDK EXPORT ScopeOneCoreTargets RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" diff --git a/ScopeOneCore/DRIVERHOST_PROTOCOL.md b/ScopeOneCore/DRIVERHOST_PROTOCOL.md new file mode 100644 index 0000000..120d0a6 --- /dev/null +++ b/ScopeOneCore/DRIVERHOST_PROTOCOL.md @@ -0,0 +1,132 @@ +# DriverHost protocol + +ScopeOne talks to every hardware plugin through a `ScopeOne_DriverHost` process. This file +describes what crosses that process boundary: control messages on a local socket and camera +frames in shared memory. Both ends are part of ScopeOne, so the protocol is internal and changes +with it; hardware plugins are written against `scopeone::HardwarePluginSDK` instead. It is version +5, `kProtocolVersion` in `ScopeOneCore/internal/DriverHostProtocol.h`, which holds the names used +below. + +## Process + +ScopeOne starts one host for each device: + +```text +ScopeOne_DriverHost --provider --plugin --hostKey + --launcherPid [--option ] +``` + +`--option` carries the options from the Plugin Manager. The host loads the plugin, creates the +devices and listens on the local socket `ScopeOne.DriverHost..ctrl` (a named pipe on +Windows, a Unix domain socket elsewhere). ScopeOne connects to it, and the host exits when the +process `--launcherPid` is gone. + +## Messages + +Each message is a 4-byte little-endian length followed by that many bytes of compact UTF-8 JSON. +A message is at most 256 KiB. Every message is an object with + +| Field | Value | +|---|---| +| `kind` | `Request`, `Response` or `Event` | +| `version` | the protocol version | +| `type` | the command or event name | +| `requestId` | a decimal string, in requests and their responses | + +ScopeOne sends requests. The host answers each one with a response of the same `type` and +`requestId` that has `ok` and, when `ok` is false, `error`. Events come from the host at any time +and carry `providerId`. + +64-bit integers such as `requestId` and `frameIndex` are sent as decimal strings. + +## Describe and shutdown + +`Describe` returns `providerId`, `providerName`, `providerVersion` and `devices`, and for a provider +with stages `defaultXYStage` and `defaultZStage`. Each device is an object with + +| Field | Value | +|---|---| +| `deviceId` | the id ScopeOne uses in requests | +| `providerDeviceId`, `hardwareId`, `deviceName` | the plugin's own names | +| `deviceKind` | `Camera`, `XYStage`, `ZStage`, `Shutter`, `State`, `Hub`, `Serial`, `Generic`, ... | +| `deviceState` | the numeric `HardwareDeviceState` | +| `deviceProperties` | an object of fixed device information | +| `capabilities` | the capability names below that the device supports | +| `sharedMemoryKey` | cameras only, the key of the frame ring | + +`Shutdown` stops the devices, answers, and ends the host. + +## Commands + +Every command except the configuration ones names its device with `deviceId`. A command for a +capability the device lacks is answered with `ok` false. + +| Capability | Command | Request fields | Response fields | +|---|---|---|---| +| `Camera` | `StartPreview`, `StopPreview` | | | +| | `SetFrameDeliveryMode` | `mode`: `PreviewLatest`, `LatestOnly` or `AllFrames` | `mode` | +| | `GetExposure` | | `exposureMs` | +| | `SetExposure` | `value` in ms | `exposureMs`, the value the camera took | +| | `SetROI` | `x`, `y`, `width`, `height` | `x`, `y`, `width`, `height` | +| | `ClearROI`, `GetROI` | | `x`, `y`, `width`, `height` | +| | `CaptureEvent` | `timeoutMs`, default 1500 | `frameIndex` | +| `Properties` | `ListProperties` | | `properties` | +| | `GetProperty` | `name`, `fromCache` | `value`, `propertyType`, `readOnly`, `preInit`, `allowedValues`, `hasLimits`, `lowerLimit`, `upperLimit` | +| | `SetProperty` | `name`, `value` | | +| `Stage` | `GetXYPosition` | | `x`, `y` | +| | `GetZPosition` | | `z` | +| | `SetXYPosition`, `SetRelativeXYPosition` | `x`, `y` | | +| | `SetZPosition`, `SetRelativeZPosition` | `z` | | +| `Shutter` | `GetShutterOpen` | | `open` | +| | `SetShutterOpen` | `open` | | +| `State` | `GetState` | | `state`, `label` | +| | `SetState` | `state` | | +| | `GetStateLabel` | `state` | `label` | +| `Configuration` | `ListConfigGroups` | | `groups` | +| | `ListConfigs` | `group` | `configs` | +| | `GetCurrentConfig` | `group` | `config` | +| | `SetConfig` | `group`, `config` | | + +## Events + +| Event | Fields | Sent when | +|---|---|---| +| `Hello` | | a client has connected | +| `FrameAvailable` | `deviceId`, `frameIndex` | a frame is in the ring | +| `PreviewState` | `deviceId`, `running` | preview starts or stops | +| `DriverHostError` | `error` | the provider reports an error | + +## Camera frames + +Each camera has one ring in shared memory, created by the host under `sharedMemoryKey`. On Windows +the key is the name of a file mapping. On Linux and macOS it is the path of a file in the user's +runtime directory, which `QSharedMemory` turns into a System V key. + +The ring is a 64-byte control block followed by 32 slots. A slot is a 64-byte header and room for +`4 * 2048 * 2048` bytes of pixels. All fields are little-endian. + +| Offset | Control block field | +|---|---| +| 0 | `latestSlotIndex`, u32, the slot written last | + +| Offset | Slot header field | +|---|---| +| 0 | `state`, u32: 0 empty, 1 being written, 2 ready, 3 being read | +| 4 | `width`, u32 | +| 8 | `height`, u32 | +| 12 | `stride`, u32, bytes per row | +| 16 | `pixelFormat`, u32: 0 Mono8, 1 Mono16 | +| 20 | `bitsPerSample`, u16 | +| 22 | `channels`, u16, always 1 | +| 24 | `frameIndex`, u64 | +| 32 | `timestampNs`, u64 | +| 40 | `sourceRoiX`, `sourceRoiY`, `sourceRoiWidth`, `sourceRoiHeight`, i32 each | +| 56 | `sourceRoiValid`, u32 | + +The pixels start at offset 64 of the slot, `stride * height` bytes. + +The writer takes a slot whose `state` is 0 or 2 by changing it to 1 with a compare-and-swap, +fills the header and pixels, stores 2 with release order, then stores the slot index in +`latestSlotIndex` with release order and sends `FrameAvailable`. The reader takes a slot by +changing its `state` from 2 to 3, copies the frame, and stores 2 again. A slot being read is +skipped by the writer, so the reader never sees a frame change under it. diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 5153ca4..08647ee 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -8,7 +8,7 @@ The desktop app consumes `ScopeOneCore` through its installed CMake package. ```text ScopeOneCore/ -|-- include/scopeone/ Core facade and all public plugin contracts +|-- include/scopeone/ Core facade, public models and the hardware plugin contract |-- internal/ Private headers used only while building ScopeOneCore |-- src/ C++ implementations for both public and private types |-- python/scopeone/ External Python client for a running ScopeOne app @@ -18,7 +18,7 @@ ScopeOneCore/ `-- install/ Generated local installation consumed by the desktop app ``` -`include/scopeone` contains the Core facade, Core-owned public models and all plugin contracts: +`include/scopeone` contains the Core facade, Core-owned public models and the plugin contracts: ```cpp #include @@ -31,7 +31,7 @@ ScopeOneCore/ Use this placement rule: -- Put every public Core model, plugin contract and shared plugin data type in `ScopeOneCore/include/scopeone`. +- Put every public Core model and plugin contract in `ScopeOneCore/include/scopeone`. - Put a Core-only manager, algorithm or protocol detail in `internal`. - Put executable implementation in `src`. - Keep desktop widgets and Qt UI behavior in the top-level ScopeOne `src` directory, outside `ScopeOneCore`. @@ -43,10 +43,11 @@ Use this placement rule: | Namespace | Purpose | Examples | |---|---|---| | `scopeone::core` | Stable Core-facing types and public facades | `ScopeOneCore`, `ExperimentDocument`, `ImageSceneModel` | -| `scopeone::core` SDK contracts | Stable plugin-facing types and interfaces | `ImageFrame`, `CameraProvider`, `DaqDevice`, `SignalSource`, `ProcessingPlugin`, `ToolPlugin` | -| `scopeone::core::internal` | Core-only managers and processing implementations | `CameraManager`, `MMCoreManager`, `RecordingManager`, processing modules | +| `scopeone::core` hardware plugin contract | Stable types of `HardwarePluginSDK.h` | `ImageFrame`, `CameraProvider`, `HardwareProvider`, `DriverHostProviderPlugin` | +| `scopeone::core::internal` | Core-only managers, processing modules and built-in devices | `CameraManager`, `MMCoreManager`, `RecordingManager`, `IscatModule`, `NIDaqmxDeviceFactory` | | `scopeone::core::internal::driverhost` | Shared DriverHost message framing | Versioned request, response and event envelopes | -| `scopeone::ui` | Desktop application widgets and UI coordination outside this library | `MainWindow`, `PreviewWidget`, `InspectWidget` | +| `scopeone::ui` | Desktop application widgets and UI coordination outside this library | `MainWindow`, `PreviewWidget`, `DhmTool`, `ToolRegistry` | +| `scopeone::plugins::` | The CUDA processing plugin. The ONNX plugin stays in `scopeone::inference`, the namespace of its library | `scopeone::plugins::cuda` | Code in `src` that implements a public type remains in `scopeone::core`. Code that implements an `internal` header remains in `scopeone::core::internal`. The Python package named `scopeone` is an external client package and is not an embedded form of the C++ namespace. @@ -93,7 +94,7 @@ Outputs: - `build/ScopeOneCoreConfig.cmake` - `install/bin/ScopeOneCore.dll` - `install/bin/ScopeOne_DriverHost.exe` -- Hardware, DAQ and signal source plugins are installed under `plugins/hardware`. +- Hardware plugins are installed under `plugins/hardware`, and the CUDA and ONNX processing plugins under `plugins/processing`. - `install/lib/cmake/ScopeOneCore/ScopeOneCoreConfig.cmake` @@ -102,25 +103,24 @@ Outputs: The installed headers in `include/scopeone` are the source of truth for the public API: - `ScopeOneCore.h` provides the main hardware, acquisition, processing, recording and frame-graph facade. -- `SimulatorProvider.h`, `ProcessingPipeline.h`, `ExperimentDocument.h` and `ImageSceneModel.h` are Core-owned runtime models and services. -- The SDK provides `HardwareProvider.h`, `HardwareCapabilities.h`, `CameraProvider.h`, `DriverHostProviderPlugin.h`, `DaqDevice.h`, `SignalSource.h`, `ScanImageAssembler.h`, `ProcessingPlugin.h`, `ToolPlugin.h`, `PluginManifest.h`, `HardwareTypes.h`, `ImageFrame.h` and `SharedFrame.h`. +- `ProcessingPipeline.h`, `ExperimentDocument.h` and `ImageSceneModel.h` are Core-owned runtime models and services. +- `HardwarePluginSDK.h` includes the headers of the public hardware plugin contract: `CameraProvider.h`, `DriverHostProviderPlugin.h`, `HardwareCapabilities.h`, `HardwareProvider.h`, `HardwareTypes.h`, `ImageFrame.h` and `PluginManifest.h`. The `scopeone::HardwarePluginSDK` CMake target provides them. +- `ProcessingPlugin.h` is the interface of the CUDA and ONNX processing plugins. It is internal to this repository. +- `DaqDevice.h`, `SignalSource.h`, `ScanImageAssembler.h`, `SharedFrame.h` and `DhmReconstruction.h` serve the application and are not plugin contracts. - `scopeone_core_export.h` supplies DLL import and export declarations and is normally included indirectly. External code should enter through these headers and `scopeone::core::ScopeOneCore`. Internal managers are implementation details and must not become alternate access paths. -Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `CameraProvider::FrameSink`. Register in-process providers with `ScopeOneCore::registerHardwareProvider(...)`. Submit isolated module loading with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)` and observe `hardwareProviderRegistrationFinished` for the result. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider, using the native camera path for one camera and isolated DriverHost camera processes for multiple cameras. +Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `CameraProvider::FrameSink`. Core loads a hardware plugin with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider, using the native camera path for one camera and isolated DriverHost camera processes for multiple cameras. ## Plugin Boundaries - `plugins/hardware` contains native `HardwareProvider` modules. Each provider runs in an isolated DriverHost process. Micro-Manager Device Adapters remain under Micro-Manager and are not wrapped as ScopeOne plugins. -- `plugins/hardware` contains DAQ and signal source plugins alongside native `HardwareProvider` modules. The Core selects each plugin by its interface without linking DAQ vendor libraries into the application. -- `ScanImageAssembler` is implemented in Core as a provider-independent 1D-to-2D reconstruction algorithm, while its public contract is owned by the SDK. The Core publishes reconstructed frames through the shared frame graph and Gallery session path. -- `plugins/processing` contains `ProcessingPlugin` modules loaded by ScopeOneCore. A plugin publishes stable module IDs, parameter descriptors and factories. Built-in processing methods use the same registry. -- `plugins/tools` contains optional desktop `ScopeOneToolPlugin` modules. They use the public `ScopeOneCore` service for data and hardware operations, while `ScopeOneToolContext` supplies desktop-only layer presentation and status operations. Built-in Scale, Stage Mosaic and Particle Detection tools use the same registry. +- Python processing modules are scripts in the `python` folder of a processing plugin directory. Core runs each module of a pipeline in its own Python process. +- The CUDA and ONNX processing modules are processing plugins, because they link CUDA and ONNX Runtime, which Core does not. Every other processing method, the NI-DAQmx device, the PTU file and simulated PMT signal sources and the DHM reconstruction are part of Core. +- The tool windows (Scale, Stage Mosaic, Particle Detection, DHM Reconstruction, Scanning and DAQ Control, Signal Monitor) are part of the application. They use the `ScopeOneCore` facade, and `ScopeOneToolContext` in `src/tools/ToolContext.h` gives them the desktop operations such as showing layers. -External projects consume the exported `scopeone::PluginSDK` CMake target. Every plugin manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`; processing, tool and hardware contracts are versioned independently and incompatible manifests are rejected before the plugin instance is created. - -Hardware, processing, DAQ, signal-source and tool contracts are installed SDK APIs. Desktop tool plugins target the ScopeOne application UI contract exposed by the SDK. +Hardware plugins outside this repository consume the exported `scopeone::HardwarePluginSDK` CMake target. Every plugin manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`; the processing and hardware contracts are versioned independently and incompatible manifests are rejected before the plugin instance is created. ## Processing Data Flow diff --git a/ScopeOneCore/cmake/ScopeOnePlugin.cmake b/ScopeOneCore/cmake/ScopeOnePlugin.cmake index 3fe7e95..00e9b58 100644 --- a/ScopeOneCore/cmake/ScopeOnePlugin.cmake +++ b/ScopeOneCore/cmake/ScopeOnePlugin.cmake @@ -2,11 +2,11 @@ # # find_package(ScopeOneCore CONFIG REQUIRED) # add_library(MyPlugin MODULE MyPlugin.cpp plugin.json) -# target_link_libraries(MyPlugin PRIVATE scopeone::PluginSDK) -# scopeone_add_plugin(MyPlugin processing) +# target_link_libraries(MyPlugin PRIVATE scopeone::HardwarePluginSDK) +# scopeone_add_plugin(MyPlugin hardware) # # The plugin source names the generated manifest: -# Q_PLUGIN_METADATA(IID ScopeOneProcessingPlugin_iid FILE "scopeone_plugin.json") +# Q_PLUGIN_METADATA(IID ScopeOneDriverHostProviderPlugin_iid FILE "scopeone_plugin.json") # Writes scopeone_plugin.json from the plugin.json beside the target's CMakeLists, adding the # ScopeOneCore version the plugin is built against. ScopeOne loads only plugins built against @@ -21,7 +21,7 @@ function(scopeone_plugin_manifest target) target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") endfunction() -# Builds the target as a plugin of the given kind (processing, tools or hardware) and installs +# Builds the target as a plugin of the given kind (processing or hardware) and installs # it where ScopeOne looks for that kind function(scopeone_add_plugin target kind) scopeone_plugin_manifest(${target}) diff --git a/ScopeOneCore/include/scopeone/DaqDevice.h b/ScopeOneCore/include/scopeone/DaqDevice.h index f669265..59d8185 100644 --- a/ScopeOneCore/include/scopeone/DaqDevice.h +++ b/ScopeOneCore/include/scopeone/DaqDevice.h @@ -174,19 +174,7 @@ namespace scopeone::core void controllerError(const QString& errorMessage); void inputDataReady(const scopeone::core::DaqInputChunk& chunk); }; - - class DaqDevicePlugin - { - public: - virtual ~DaqDevicePlugin() = default; - virtual QList devices() const = 0; - virtual DaqController* createController(const QString& deviceId, - QObject* parent = nullptr) = 0; - }; } -#define SCOPEONE_DAQ_DEVICE_PLUGIN_IID "org.scopeone.DaqDevicePlugin/1.0" -Q_DECLARE_INTERFACE(scopeone::core::DaqDevicePlugin, SCOPEONE_DAQ_DEVICE_PLUGIN_IID) - Q_DECLARE_METATYPE(scopeone::core::DaqState) Q_DECLARE_METATYPE(scopeone::core::DaqInputChunk) diff --git a/plugins/tools/DhmTool/DhmReconstruction.h b/ScopeOneCore/include/scopeone/DhmReconstruction.h similarity index 83% rename from plugins/tools/DhmTool/DhmReconstruction.h rename to ScopeOneCore/include/scopeone/DhmReconstruction.h index a92833e..eb91900 100644 --- a/plugins/tools/DhmTool/DhmReconstruction.h +++ b/ScopeOneCore/include/scopeone/DhmReconstruction.h @@ -1,15 +1,13 @@ #pragma once #include "scopeone/ImageFrame.h" - -#include -#include +#include "scopeone/scopeone_core_export.h" #include #include #include -namespace scopeone::dhm +namespace scopeone::core { enum class DhmRoiMode { @@ -51,11 +49,10 @@ namespace scopeone::dhm int detectedSidebandY{0}; }; - DhmResult reconstruct( - const scopeone::core::ImageFrame& input, + // Reconstructs an off axis hologram. The output and the spectrum are 16-bit frames + SCOPEONE_CORE_EXPORT DhmResult reconstructDhm( + const ImageFrame& input, const DhmParameters& params, const std::atomic_bool& cancel, const std::function& progress); - - cv::Point autoDetectSideband(const cv::Mat& logMagSpectrum); } diff --git a/ScopeOneCore/include/scopeone/PluginSDK.h b/ScopeOneCore/include/scopeone/HardwarePluginSDK.h similarity index 50% rename from ScopeOneCore/include/scopeone/PluginSDK.h rename to ScopeOneCore/include/scopeone/HardwarePluginSDK.h index 4cdb85a..62b7473 100644 --- a/ScopeOneCore/include/scopeone/PluginSDK.h +++ b/ScopeOneCore/include/scopeone/HardwarePluginSDK.h @@ -1,15 +1,11 @@ #pragma once +// Everything a hardware plugin is written against. These headers are the public contract of +// ScopeOne for code outside this repository. +#include "scopeone/CameraProvider.h" #include "scopeone/DriverHostProviderPlugin.h" -#include "scopeone/DaqDevice.h" #include "scopeone/HardwareCapabilities.h" #include "scopeone/HardwareProvider.h" #include "scopeone/HardwareTypes.h" #include "scopeone/ImageFrame.h" #include "scopeone/PluginManifest.h" -#include "scopeone/ProcessingPlugin.h" -#include "scopeone/ScanImageAssembler.h" -#include "scopeone/SignalSource.h" -#include "scopeone/ToolFrameStream.h" -#include "scopeone/ToolPlugin.h" -#include "scopeone/ToolTask.h" diff --git a/ScopeOneCore/include/scopeone/ImageFrame.h b/ScopeOneCore/include/scopeone/ImageFrame.h index c2754b5..952c212 100644 --- a/ScopeOneCore/include/scopeone/ImageFrame.h +++ b/ScopeOneCore/include/scopeone/ImageFrame.h @@ -7,7 +7,6 @@ #include #include -#include "scopeone/SharedFrame.h" namespace scopeone::core { @@ -123,85 +122,6 @@ namespace scopeone::core && pixelFormat == other.pixelFormat; } - SharedFrameHeader toSharedFrameHeader() const - { - SharedFrameHeader header{}; - if (!isValid()) - { - return header; - } - - header.state = 2; - header.width = static_cast(width); - header.height = static_cast(height); - header.stride = static_cast(stride); - header.bitsPerSample = static_cast(bitsPerSample); - header.channels = 1; - header.frameIndex = frameIndex; - header.timestampNs = timestampNs; - header.pixelFormat = pixelFormat == ImagePixelFormat::Mono16 - ? static_cast(SharedPixelFormat::Mono16) - : static_cast(SharedPixelFormat::Mono8); - if (hasSourceRoi()) - { - setSharedFrameSourceRoi(header, sourceRoiX, sourceRoiY, sourceRoiWidth, sourceRoiHeight); - } - return header; - } - - static ImageFrame fromSharedFrame(const QString& cameraId, - const SharedFrameHeader& header, - const QByteArray& payload) - { - if (header.state != 2 - || header.channels != 1 - || header.width > static_cast((std::numeric_limits::max)()) - || header.height > static_cast((std::numeric_limits::max)()) - || header.stride > static_cast((std::numeric_limits::max)())) - { - return {}; - } - - ImageFrame frame; - frame.cameraId = cameraId.trimmed(); - frame.width = static_cast(header.width); - frame.height = static_cast(header.height); - frame.bitsPerSample = static_cast(header.bitsPerSample); - frame.frameIndex = header.frameIndex; - frame.timestampNs = header.timestampNs; - - if (header.pixelFormat == static_cast(SharedPixelFormat::Mono16)) - { - if (header.bitsPerSample == 0 || header.bitsPerSample > 16) - { - return {}; - } - frame.pixelFormat = ImagePixelFormat::Mono16; - } - else if (header.pixelFormat == static_cast(SharedPixelFormat::Mono8)) - { - if (header.bitsPerSample != 8) - { - return {}; - } - frame.pixelFormat = ImagePixelFormat::Mono8; - } - else - { - return {}; - } - - frame.stride = static_cast(header.stride); - frame.bytes = payload; - if (sharedFrameHasSourceRoi(header)) - { - frame.sourceRoiX = static_cast(header.sourceRoiX); - frame.sourceRoiY = static_cast(header.sourceRoiY); - frame.sourceRoiWidth = static_cast(header.sourceRoiWidth); - frame.sourceRoiHeight = static_cast(header.sourceRoiHeight); - } - return frame.isValid() ? frame : ImageFrame{}; - } }; } diff --git a/ScopeOneCore/include/scopeone/PluginManifest.h b/ScopeOneCore/include/scopeone/PluginManifest.h index e499258..d6caaf0 100644 --- a/ScopeOneCore/include/scopeone/PluginManifest.h +++ b/ScopeOneCore/include/scopeone/PluginManifest.h @@ -12,7 +12,6 @@ namespace scopeone::core enum class PluginKind { Processing, - Tool, Hardware }; @@ -37,7 +36,6 @@ namespace scopeone::core }; inline constexpr int ScopeOneProcessingPluginApiVersion = 2; - inline constexpr int ScopeOneToolPluginApiVersion = 3; inline constexpr int ScopeOneHardwarePluginApiVersion = 1; SCOPEONE_SDK_EXPORT QString pluginKindName(PluginKind kind); diff --git a/ScopeOneCore/include/scopeone/SharedFrame.h b/ScopeOneCore/include/scopeone/SharedFrame.h index eeadebb..ddd28c9 100644 --- a/ScopeOneCore/include/scopeone/SharedFrame.h +++ b/ScopeOneCore/include/scopeone/SharedFrame.h @@ -1,7 +1,13 @@ #pragma once +#include "scopeone/ImageFrame.h" + +#include +#include #include +#include + namespace scopeone::core { enum class SharedPixelFormat : quint32 @@ -81,4 +87,86 @@ namespace scopeone::core inline constexpr int kSharedMemoryControlSize = 64; inline constexpr int kSharedFrameHeaderSize = static_cast(sizeof(SharedFrameHeader)); inline constexpr int kSharedFrameSlotStride = kSharedFrameHeaderSize + kSharedFrameMaxBytes; + + // The header of a frame in the DriverHost frame ring + inline SharedFrameHeader toSharedFrameHeader(const ImageFrame& frame) + { + SharedFrameHeader header{}; + if (!frame.isValid()) + { + return header; + } + + header.state = 2; + header.width = static_cast(frame.width); + header.height = static_cast(frame.height); + header.stride = static_cast(frame.stride); + header.bitsPerSample = static_cast(frame.bitsPerSample); + header.channels = 1; + header.frameIndex = frame.frameIndex; + header.timestampNs = frame.timestampNs; + header.pixelFormat = frame.pixelFormat == ImagePixelFormat::Mono16 + ? static_cast(SharedPixelFormat::Mono16) + : static_cast(SharedPixelFormat::Mono8); + if (frame.hasSourceRoi()) + { + setSharedFrameSourceRoi(header, frame.sourceRoiX, frame.sourceRoiY, frame.sourceRoiWidth, frame.sourceRoiHeight); + } + return header; + } + + // A frame read back from the DriverHost frame ring, or an invalid frame when the header does not fit + inline ImageFrame imageFrameFromShared(const QString& cameraId, + const SharedFrameHeader& header, + const QByteArray& payload) + { + if (header.state != 2 + || header.channels != 1 + || header.width > static_cast((std::numeric_limits::max)()) + || header.height > static_cast((std::numeric_limits::max)()) + || header.stride > static_cast((std::numeric_limits::max)())) + { + return {}; + } + + ImageFrame frame; + frame.cameraId = cameraId.trimmed(); + frame.width = static_cast(header.width); + frame.height = static_cast(header.height); + frame.bitsPerSample = static_cast(header.bitsPerSample); + frame.frameIndex = header.frameIndex; + frame.timestampNs = header.timestampNs; + + if (header.pixelFormat == static_cast(SharedPixelFormat::Mono16)) + { + if (header.bitsPerSample == 0 || header.bitsPerSample > 16) + { + return {}; + } + frame.pixelFormat = ImagePixelFormat::Mono16; + } + else if (header.pixelFormat == static_cast(SharedPixelFormat::Mono8)) + { + if (header.bitsPerSample != 8) + { + return {}; + } + frame.pixelFormat = ImagePixelFormat::Mono8; + } + else + { + return {}; + } + + frame.stride = static_cast(header.stride); + frame.bytes = payload; + if (sharedFrameHasSourceRoi(header)) + { + frame.sourceRoiX = static_cast(header.sourceRoiX); + frame.sourceRoiY = static_cast(header.sourceRoiY); + frame.sourceRoiWidth = static_cast(header.sourceRoiWidth); + frame.sourceRoiHeight = static_cast(header.sourceRoiHeight); + } + return frame.isValid() ? frame : ImageFrame{}; + } } diff --git a/ScopeOneCore/include/scopeone/SignalSource.h b/ScopeOneCore/include/scopeone/SignalSource.h index 3a937d4..c765b0f 100644 --- a/ScopeOneCore/include/scopeone/SignalSource.h +++ b/ScopeOneCore/include/scopeone/SignalSource.h @@ -202,20 +202,8 @@ namespace scopeone::core const QString& message); void sourceError(const QString& errorMessage); }; - - class SignalSourcePlugin - { - public: - virtual ~SignalSourcePlugin() = default; - virtual QList signalSources() const = 0; - virtual SignalSource* createSignalSource(const QString& sourceId, - QObject* parent = nullptr) = 0; - }; } -#define SCOPEONE_SIGNAL_SOURCE_PLUGIN_IID "org.scopeone.SignalSourcePlugin/1.0" -Q_DECLARE_INTERFACE(scopeone::core::SignalSourcePlugin, SCOPEONE_SIGNAL_SOURCE_PLUGIN_IID) - Q_DECLARE_METATYPE(scopeone::core::SignalSourceState) Q_DECLARE_METATYPE(scopeone::core::SignalMarker) Q_DECLARE_METATYPE(scopeone::core::TimeSeriesChunk) diff --git a/ScopeOneCore/include/scopeone/SimulatorProvider.h b/ScopeOneCore/include/scopeone/SimulatorProvider.h deleted file mode 100644 index 1f41edd..0000000 --- a/ScopeOneCore/include/scopeone/SimulatorProvider.h +++ /dev/null @@ -1,101 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include "scopeone/CameraProvider.h" -#include "scopeone/HardwareProvider.h" -#include "scopeone/scopeone_core_export.h" - -namespace scopeone::core -{ - class SCOPEONE_CORE_EXPORT SimulatorProvider final - : public QObject, - public HardwareProvider, - public CameraProvider - { - public: - explicit SimulatorProvider(const QString& logicalCameraId = QStringLiteral("camera.simulator"), - int width = 512, - int height = 512, - const QString& providerId = {}); - - HardwareProviderDescriptor descriptor() const override; - QList devices() const override; - void setFrameSink(FrameSink sink) override; - void setPreviewStateSink(PreviewStateSink sink) override; - bool startPreview() override; - bool stopPreview() override; - bool startPreviewFor(const QString& cameraId) override; - bool stopPreviewFor(const QString& cameraId) override; - bool isPreviewRunning(const QString& cameraId) const override; - bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const override; - bool setExposure(const QString& cameraIdOrAll, double exposureMs) override; - QStringList listProperties(const QString& cameraId) override; - QString getProperty(const QString& cameraId, - const QString& name, - bool fromCache) override; - bool setProperty(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage) override; - QString getPropertyType(const QString& cameraId, const QString& name) override; - bool isPropertyReadOnly(const QString& cameraId, const QString& name) override; - bool isPropertyPreInit(const QString& cameraId, const QString& name) override; - QStringList getAllowedPropertyValues(const QString& cameraId, - const QString& name) override; - bool hasPropertyLimits(const QString& cameraId, const QString& name) override; - double getPropertyLowerLimit(const QString& cameraId, const QString& name) override; - double getPropertyUpperLimit(const QString& cameraId, const QString& name) override; - bool setROI(const QString& cameraId, int x, int y, int width, int height) override; - bool clearROI(const QString& cameraId) override; - bool getROI(const QString& cameraId, - int& x, - int& y, - int& width, - int& height) override; - bool captureEventFrame(const QString& cameraId, - ImageFrame& frame, - int timeoutMs) override; - - private: - enum class ImageMode - { - Gradient, - Hologram, - Beads, - RedBloodCells, - Brillouin - }; - - bool accepts(const QString& cameraIdOrAll) const; - ImageFrame makeFrame(); - void rebuildBeadsImage(); - void rebuildBloodCells(); - void updateTimerInterval(); - - QString m_providerId; - QString m_cameraId; - int m_sensorWidth{512}; - int m_sensorHeight{512}; - QRect m_roi; - double m_exposureMs{10.0}; - double m_gaussianNoiseSigma{0.0}; - ImageMode m_imageMode{ImageMode::Gradient}; - QByteArray m_beadsImage; - // Amplitude and phase of the red blood cells in the field, for the hologram of them - std::vector m_cellAmplitude; - std::vector m_cellPhase; - quint64 m_frameIndex{0}; - std::mt19937 m_randomGenerator{std::random_device{}()}; - FrameSink m_frameSink; - PreviewStateSink m_previewStateSink; - QTimer m_timer; - mutable QMutex m_mutex; - }; -} diff --git a/ScopeOneCore/include/scopeone/ToolFrameStream.h b/ScopeOneCore/include/scopeone/ToolFrameStream.h deleted file mode 100644 index e5feb91..0000000 --- a/ScopeOneCore/include/scopeone/ToolFrameStream.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include "scopeone/ImageFrame.h" -#include "scopeone/scopeone_sdk_export.h" - -#include -#include - -namespace scopeone::ui -{ - class ScopeOneToolCore; - - class SCOPEONE_SDK_EXPORT ScopeOneToolFrameStream final : public QObject - { - Q_OBJECT - - public: - explicit ScopeOneToolFrameStream(ScopeOneToolCore& core, - QObject* parent = nullptr); - - void setSourceId(const QString& cameraId); - - void setEnabled(bool enabled); - void setProcessing(bool processing); - void clearPendingFrame(); - - signals: - void frameReady(const scopeone::core::ImageFrame& frame); - - private: - void acceptFrame(const scopeone::core::ImageFrame& frame); - - QString m_sourceId; - scopeone::core::ImageFrame m_pendingFrame; - bool m_enabled{true}; - bool m_processing{false}; - }; -} diff --git a/ScopeOneCore/include/scopeone/ToolPlugin.h b/ScopeOneCore/include/scopeone/ToolPlugin.h deleted file mode 100644 index 0982be3..0000000 --- a/ScopeOneCore/include/scopeone/ToolPlugin.h +++ /dev/null @@ -1,124 +0,0 @@ -#pragma once - -#include "scopeone/DaqDevice.h" -#include "scopeone/ImageFrame.h" -#include "scopeone/RecordingSession.h" -#include "scopeone/SignalSource.h" -#include "scopeone/scopeone_sdk_export.h" - -#include -#include -#include -#include -#include -#include - -class QWidget; - -namespace scopeone::core -{ - class ImageSceneModel; - class ScopeOneCore; -} - -namespace scopeone::ui -{ - enum class ToolWindowMode - { - Modal, - ModelessSingleton - }; - - struct ToolDescriptor - { - QString id; - QString name; - QString category; - ToolWindowMode windowMode{ToolWindowMode::ModelessSingleton}; - bool requiresCamera{false}; - }; - - // The part of the Core a tool works with: layers and their images, signal sources and DAQ - // devices. Tools see only this, so the rest of the Core can change without breaking them. - class SCOPEONE_SDK_EXPORT ScopeOneToolCore final : public QObject - { - Q_OBJECT - - public: - explicit ScopeOneToolCore(scopeone::core::ScopeOneCore& core, QObject* parent = nullptr); - - static QString rawLayerKey(const QString& cameraId); - static QString toolLayerKey(const QString& sourceId); - static QString staticLayerKey(const QString& sourceId); - static QString sourceIdFromLayerKey(const QString& layerKey); - - QStringList cameraIds() const; - scopeone::core::ImageFrame graphFrame(const QString& layerKey) const; - scopeone::core::ImageSceneModel* imageSceneModel() const; - scopeone::core::ImageFrame publishToolStreamFrame(const QString& sourceId, - const scopeone::core::ImageFrame& frame, - const QString& displayName = QString()); - - QList signalSources() const; - bool startSignalTrace(const scopeone::core::SignalAcquisitionConfig& config, - QString* errorMessage = nullptr); - void stopSignalTrace(const QString& sourceId); - scopeone::core::SignalSourceState signalSourceState(const QString& sourceId) const; - QString signalSourceStateMessage(const QString& sourceId) const; - - QList daqDevices() const; - bool startDaqSession(const scopeone::core::DaqSessionConfig& config, - QString* errorMessage = nullptr); - void stopDaqSession(const QString& deviceId); - QString daqStateMessage(const QString& deviceId) const; - - signals: - void previewRawFrameReady(const scopeone::core::ImageFrame& frame); - void staticFramePublished(const QString& sourceId, - const QString& displayName, - const scopeone::core::ImageFrame& frame); - void signalTimeSeriesReady(const scopeone::core::TimeSeriesChunk& chunk); - void signalSourceStateChanged(const QString& sourceId, - scopeone::core::SignalSourceState state, - const QString& message); - void signalSourceError(const QString& sourceId, const QString& errorMessage); - void scanImageSessionReady( - const std::shared_ptr& session); - void daqStateChanged(const QString& deviceId, - scopeone::core::DaqState state, - const QString& message); - void daqError(const QString& deviceId, const QString& errorMessage); - void daqInputDataReady(const scopeone::core::DaqInputChunk& chunk); - - private: - scopeone::core::ScopeOneCore& m_core; - }; - - class ScopeOneToolContext - { - public: - virtual ~ScopeOneToolContext() = default; - - virtual ScopeOneToolCore& core() const = 0; - virtual QString currentLayerKey() const = 0; - virtual void showLayers(const QStringList& layerKeys, bool sideBySide = false) = 0; - virtual void showToolStatus(const QString& message, int timeoutMs = 5000) = 0; - virtual void presentSession( - const std::shared_ptr& session, - const QString& title) = 0; - }; - - class ScopeOneToolPlugin - { - public: - virtual ~ScopeOneToolPlugin() = default; - - virtual QList tools() const = 0; - virtual QWidget* createTool(const QString& toolId, - ScopeOneToolContext& context, - QWidget* parent) = 0; - }; -} - -#define ScopeOneToolPlugin_iid "org.scopeone.ToolPlugin/3.0" -Q_DECLARE_INTERFACE(scopeone::ui::ScopeOneToolPlugin, ScopeOneToolPlugin_iid) diff --git a/ScopeOneCore/include/scopeone/ToolTask.h b/ScopeOneCore/include/scopeone/ToolTask.h deleted file mode 100644 index fa2e131..0000000 --- a/ScopeOneCore/include/scopeone/ToolTask.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include "scopeone/scopeone_sdk_export.h" - -#include -#include -#include -#include -#include - -namespace scopeone::ui -{ - class SCOPEONE_SDK_EXPORT ScopeOneToolTask final : public QObject - { - Q_OBJECT - - public: - using Work = std::function&)>; - - explicit ScopeOneToolTask(Work work, QObject* parent = nullptr); - ~ScopeOneToolTask() override; - - void start(); - void cancel(); - - signals: - void progressChanged(int percent); - void finished(); - void canceled(); - void failed(const QString& message); - - private: - struct Impl; - std::unique_ptr m_impl; - }; -} diff --git a/ScopeOneCore/internal/DaqDeviceManager.h b/ScopeOneCore/internal/DaqDeviceManager.h index e8bbc99..c2aee49 100644 --- a/ScopeOneCore/internal/DaqDeviceManager.h +++ b/ScopeOneCore/internal/DaqDeviceManager.h @@ -9,10 +9,18 @@ #include "scopeone/DaqDevice.h" -class QPluginLoader; - namespace scopeone::core::internal { + // A kind of DAQ device built into Core, which lists the devices it finds and drives them + class DaqDeviceFactory + { + public: + virtual ~DaqDeviceFactory() = default; + virtual QList devices() const = 0; + virtual DaqController* createController(const QString& deviceId, + QObject* parent = nullptr) = 0; + }; + class DaqDeviceManager final : public QObject { Q_OBJECT @@ -36,13 +44,13 @@ namespace scopeone::core::internal void inputDataReady(const scopeone::core::DaqInputChunk& chunk); private: - void loadPlugins(); + void registerDevices(); DaqController* controller(const QString& deviceId, QString* errorMessage); - std::vector> m_loaders; - mutable bool m_pluginsLoaded{false}; - QHash m_plugins; + std::vector> m_factories; + mutable bool m_devicesRegistered{false}; + QHash m_factoryByDevice; QHash m_descriptors; QHash> m_controllers; QHash m_states; diff --git a/plugins/processing/IscatProcessing/IscatProcessingModule.h b/ScopeOneCore/internal/IscatModule.h similarity index 94% rename from plugins/processing/IscatProcessing/IscatProcessingModule.h rename to ScopeOneCore/internal/IscatModule.h index 9e18573..f4aaaf1 100644 --- a/plugins/processing/IscatProcessing/IscatProcessingModule.h +++ b/ScopeOneCore/internal/IscatModule.h @@ -1,13 +1,13 @@ #pragma once -#include "scopeone/ProcessingPlugin.h" +#include "internal/ProcessingModule.h" #include #include -namespace scopeone::iscat +namespace scopeone::core::internal { - class IscatProcessingModule final : public scopeone::core::ProcessingModule + class IscatModule final : public scopeone::core::ProcessingModule { public: QString id() const override; diff --git a/ScopeOneCore/internal/NIDaqmx.h b/ScopeOneCore/internal/NIDaqmx.h new file mode 100644 index 0000000..9348229 --- /dev/null +++ b/ScopeOneCore/internal/NIDaqmx.h @@ -0,0 +1,15 @@ +#pragma once + +#include "internal/DaqDeviceManager.h" + +namespace scopeone::core::internal +{ + class NIDaqmxDeviceFactory final : public DaqDeviceFactory + { + public: + QList devices() const override; + scopeone::core::DaqController* createController( + const QString& deviceId, + QObject* parent = nullptr) override; + }; +} diff --git a/ScopeOneCore/internal/PtuFile.h b/ScopeOneCore/internal/PtuFile.h new file mode 100644 index 0000000..44f800d --- /dev/null +++ b/ScopeOneCore/internal/PtuFile.h @@ -0,0 +1,15 @@ +#pragma once + +#include "internal/SignalSourceManager.h" + +namespace scopeone::core::internal +{ + class PtuFileSourceFactory final : public SignalSourceFactory + { + public: + QList signalSources() const override; + scopeone::core::SignalSource* createSignalSource( + const QString& sourceId, + QObject* parent = nullptr) override; + }; +} diff --git a/ScopeOneCore/internal/PythonProcessingModule.h b/ScopeOneCore/internal/PythonProcessingModule.h new file mode 100644 index 0000000..0bcb018 --- /dev/null +++ b/ScopeOneCore/internal/PythonProcessingModule.h @@ -0,0 +1,16 @@ +#pragma once + +#include "internal/ProcessingModule.h" + +#include + +namespace scopeone::core::internal +{ + class ProcessingModuleRegistry; + + // Registers the modules of every script in the python folder of a processing plugin + // directory. Each module of a pipeline then talks to its own Python process, one frame at a + // time, over stdin and stdout. Returns the scripts and modules that could not be registered. + QStringList registerPythonProcessingModules(ProcessingModuleRegistry& registry, + const QString& pluginDirectory); +} diff --git a/ScopeOneCore/internal/SharedFrameRing.h b/ScopeOneCore/internal/SharedFrameRing.h index f0baed1..80d115b 100644 --- a/ScopeOneCore/internal/SharedFrameRing.h +++ b/ScopeOneCore/internal/SharedFrameRing.h @@ -103,7 +103,7 @@ namespace scopeone::core::internal::sharedframe { QByteArray payload(static_cast(payloadSize(header)), Qt::Uninitialized); std::memcpy(payload.data(), slot + kSharedFrameHeaderSize, static_cast(payload.size())); - ImageFrame frame = ImageFrame::fromSharedFrame(cameraId, header, payload); + ImageFrame frame = imageFrameFromShared(cameraId, header, payload); if (!frame.isValid()) { return false; diff --git a/ScopeOneCore/internal/SignalSourceManager.h b/ScopeOneCore/internal/SignalSourceManager.h index c3f1f8f..0b6f5f8 100644 --- a/ScopeOneCore/internal/SignalSourceManager.h +++ b/ScopeOneCore/internal/SignalSourceManager.h @@ -9,10 +9,18 @@ #include "scopeone/SignalSource.h" -class QPluginLoader; - namespace scopeone::core::internal { + // A kind of signal source built into Core, which lists its sources and creates them + class SignalSourceFactory + { + public: + virtual ~SignalSourceFactory() = default; + virtual QList signalSources() const = 0; + virtual SignalSource* createSignalSource(const QString& sourceId, + QObject* parent = nullptr) = 0; + }; + class SignalSourceManager final : public QObject { Q_OBJECT @@ -37,12 +45,12 @@ namespace scopeone::core::internal void sourceError(const QString& sourceId, const QString& errorMessage); private: - void loadPlugins(); + void registerSources(); SignalSource* sourceInstance(const QString& sourceId, QString* errorMessage); - std::vector> m_loaders; - QHash m_plugins; + std::vector> m_factories; + QHash m_factoryBySource; QHash m_descriptors; QHash> m_sources; QHash m_states; diff --git a/ScopeOneCore/internal/SimulatedPmt.h b/ScopeOneCore/internal/SimulatedPmt.h new file mode 100644 index 0000000..c773905 --- /dev/null +++ b/ScopeOneCore/internal/SimulatedPmt.h @@ -0,0 +1,15 @@ +#pragma once + +#include "internal/SignalSourceManager.h" + +namespace scopeone::core::internal +{ + class SimulatedPmtSourceFactory final : public SignalSourceFactory + { + public: + QList signalSources() const override; + scopeone::core::SignalSource* createSignalSource( + const QString& sourceId, + QObject* parent = nullptr) override; + }; +} diff --git a/plugins/hardware/SimulatedPmt/SimulatedPmtSource.h b/ScopeOneCore/internal/SimulatedPmtSource.h similarity index 98% rename from plugins/hardware/SimulatedPmt/SimulatedPmtSource.h rename to ScopeOneCore/internal/SimulatedPmtSource.h index 9971363..4c0450e 100644 --- a/plugins/hardware/SimulatedPmt/SimulatedPmtSource.h +++ b/ScopeOneCore/internal/SimulatedPmtSource.h @@ -6,7 +6,7 @@ #include #include -namespace scopeone::plugins +namespace scopeone::core::internal { class SimulatedPmtSource final : public scopeone::core::SignalSource { diff --git a/plugins/processing/PythonProcessing/examples/offset.py b/ScopeOneCore/python/scopeone/examples/processing_offset.py similarity index 100% rename from plugins/processing/PythonProcessing/examples/offset.py rename to ScopeOneCore/python/scopeone/examples/processing_offset.py diff --git a/ScopeOneCore/src/DaqDeviceManager.cpp b/ScopeOneCore/src/DaqDeviceManager.cpp index b8f916e..c22e69a 100644 --- a/ScopeOneCore/src/DaqDeviceManager.cpp +++ b/ScopeOneCore/src/DaqDeviceManager.cpp @@ -1,9 +1,6 @@ #include "internal/DaqDeviceManager.h" -#include "scopeone/PluginManifest.h" -#include -#include -#include +#include "internal/NIDaqmx.h" #include #include @@ -94,14 +91,14 @@ namespace scopeone::core::internal delete controller.data(); } m_controllers.clear(); - m_loaders.clear(); + m_factories.clear(); } QList DaqDeviceManager::devices() const { - if (!m_pluginsLoaded) + if (!m_devicesRegistered) { - const_cast(this)->loadPlugins(); + const_cast(this)->registerDevices(); } QList result = m_descriptors.values(); std::sort(result.begin(), result.end(), @@ -116,9 +113,9 @@ namespace scopeone::core::internal bool DaqDeviceManager::start(const DaqSessionConfig& config, QString* errorMessage) { - if (!m_pluginsLoaded) + if (!m_devicesRegistered) { - loadPlugins(); + registerDevices(); } const QString deviceId = config.deviceId.trimmed(); if (!m_descriptors.contains(deviceId)) @@ -163,35 +160,13 @@ namespace scopeone::core::internal : m_messages.value(id, QStringLiteral("DAQ device is idle")); } - void DaqDeviceManager::loadPlugins() + void DaqDeviceManager::registerDevices() { - m_pluginsLoaded = true; - for (const DiscoveredPlugin& discovered : discoverPlugins(PluginKind::Hardware)) + m_devicesRegistered = true; + m_factories.push_back(std::make_unique()); + for (const std::unique_ptr& factory : m_factories) { - if (discovered.interfaceId != QStringLiteral(SCOPEONE_DAQ_DEVICE_PLUGIN_IID)) - { - continue; - } - const QFileInfo file(discovered.path); - if (!discovered.error.isEmpty()) - { - qWarning().noquote() << QStringLiteral("Failed to load DAQ plugin %1: %2") - .arg(file.fileName(), discovered.error); - continue; - } - if (!pluginEnabled(discovered.manifest)) - { - continue; - } - auto loader = std::make_unique(discovered.path); - auto* plugin = qobject_cast(loader->instance()); - if (!plugin) - { - qWarning().noquote() << QStringLiteral("Failed to load DAQ plugin %1: %2") - .arg(file.fileName(), loader->errorString()); - continue; - } - for (const DaqDeviceDescriptor& descriptor : plugin->devices()) + for (const DaqDeviceDescriptor& descriptor : factory->devices()) { const QString deviceId = descriptor.id.trimmed(); if (deviceId.isEmpty() || m_descriptors.contains(deviceId)) @@ -199,11 +174,10 @@ namespace scopeone::core::internal continue; } m_descriptors.insert(deviceId, descriptor); - m_plugins.insert(deviceId, plugin); + m_factoryByDevice.insert(deviceId, factory.get()); m_states.insert(deviceId, DaqState::Idle); m_messages.insert(deviceId, QStringLiteral("DAQ device is idle")); } - m_loaders.push_back(std::move(loader)); } } @@ -215,8 +189,8 @@ namespace scopeone::core::internal { return existing; } - DaqDevicePlugin* plugin = m_plugins.value(id); - if (!plugin) + DaqDeviceFactory* factory = m_factoryByDevice.value(id); + if (!factory) { if (errorMessage) { @@ -224,7 +198,7 @@ namespace scopeone::core::internal } return nullptr; } - DaqController* created = plugin->createController(id, this); + DaqController* created = factory->createController(id, this); if (!created) { if (errorMessage) diff --git a/plugins/tools/DhmTool/DhmReconstruction.cpp b/ScopeOneCore/src/DhmReconstruction.cpp similarity index 98% rename from plugins/tools/DhmTool/DhmReconstruction.cpp rename to ScopeOneCore/src/DhmReconstruction.cpp index 6f2a306..69ecadb 100644 --- a/plugins/tools/DhmTool/DhmReconstruction.cpp +++ b/ScopeOneCore/src/DhmReconstruction.cpp @@ -1,9 +1,11 @@ -#include "DhmReconstruction.h" +#include "scopeone/DhmReconstruction.h" +#include #include #include #include +#include #include #include @@ -14,7 +16,7 @@ namespace { using scopeone::core::ImageFrame; - using scopeone::dhm::DhmParameters; + using scopeone::core::DhmParameters; constexpr float Pi = 3.14159265358979323846f; constexpr float TwoPi = 2.0f * Pi; @@ -42,7 +44,7 @@ namespace cv::Mat applyRoi(const cv::Mat& input, const DhmParameters& params) { - if (params.roiMode == scopeone::dhm::DhmRoiMode::FullFrame) + if (params.roiMode == scopeone::core::DhmRoiMode::FullFrame) { return input.clone(); } @@ -133,9 +135,29 @@ namespace return frame; } + cv::Point autoDetectSideband(const cv::Mat& logMagSpectrum) + { + cv::Mat blurred; + cv::GaussianBlur(logMagSpectrum, blurred, cv::Size(0, 0), 2.0, 2.0); + + const cv::Point center(logMagSpectrum.cols / 2, logMagSpectrum.rows / 2); + const int dcRadius = static_cast( + 0.06 * std::min(logMagSpectrum.cols, logMagSpectrum.rows)); + cv::Mat mask = cv::Mat::zeros(logMagSpectrum.size(), CV_8U); + cv::rectangle(mask, + cv::Rect(0, 0, logMagSpectrum.cols, std::max(1, center.y)), + cv::Scalar(255), + cv::FILLED); + cv::circle(mask, center, dcRadius, cv::Scalar(0), cv::FILLED); + + cv::Point peak; + cv::minMaxLoc(blurred, nullptr, nullptr, nullptr, &peak, mask); + return peak; + } + QPoint detectedOffset(const cv::Mat& logMagnitude) { - const cv::Point point = scopeone::dhm::autoDetectSideband(logMagnitude); + const cv::Point point = autoDetectSideband(logMagnitude); return {point.x - logMagnitude.cols / 2, point.y - logMagnitude.rows / 2}; } @@ -390,29 +412,9 @@ namespace } } -namespace scopeone::dhm +namespace scopeone::core { - cv::Point autoDetectSideband(const cv::Mat& logMagSpectrum) - { - cv::Mat blurred; - cv::GaussianBlur(logMagSpectrum, blurred, cv::Size(0, 0), 2.0, 2.0); - - const cv::Point center(logMagSpectrum.cols / 2, logMagSpectrum.rows / 2); - const int dcRadius = static_cast( - 0.06 * std::min(logMagSpectrum.cols, logMagSpectrum.rows)); - cv::Mat mask = cv::Mat::zeros(logMagSpectrum.size(), CV_8U); - cv::rectangle(mask, - cv::Rect(0, 0, logMagSpectrum.cols, std::max(1, center.y)), - cv::Scalar(255), - cv::FILLED); - cv::circle(mask, center, dcRadius, cv::Scalar(0), cv::FILLED); - - cv::Point peak; - cv::minMaxLoc(blurred, nullptr, nullptr, nullptr, &peak, mask); - return peak; - } - - DhmResult reconstruct(const ImageFrame& input, + DhmResult reconstructDhm(const ImageFrame& input, const DhmParameters& params, const std::atomic_bool& cancel, const std::function& progress) diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index 818c932..dbfd82a 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -1835,7 +1835,7 @@ namespace scopeone::core::internal } QMutexLocker writeLocker(&transport.writeMutex); transport.frameIndex = (std::max)(transport.frameIndex + 1, frame.frameIndex); - SharedFrameHeader header = frame.toSharedFrameHeader(); + SharedFrameHeader header = toSharedFrameHeader(frame); header.frameIndex = transport.frameIndex; if (header.timestampNs == 0) { diff --git a/plugins/processing/IscatProcessing/IscatProcessingModule.cpp b/ScopeOneCore/src/IscatModule.cpp similarity index 91% rename from plugins/processing/IscatProcessing/IscatProcessingModule.cpp rename to ScopeOneCore/src/IscatModule.cpp index ac709a1..86e2d19 100644 --- a/plugins/processing/IscatProcessing/IscatProcessingModule.cpp +++ b/ScopeOneCore/src/IscatModule.cpp @@ -1,4 +1,4 @@ -#include "IscatProcessingModule.h" +#include "internal/IscatModule.h" #include #include @@ -7,7 +7,7 @@ #include #include -namespace scopeone::iscat +namespace scopeone::core::internal { namespace { @@ -37,17 +37,17 @@ namespace scopeone::iscat } } - QString IscatProcessingModule::id() const + QString IscatModule::id() const { return QStringLiteral("iscat.processing"); } - QString IscatProcessingModule::name() const + QString IscatModule::name() const { return QStringLiteral("iSCAT Processing"); } - QVariantMap IscatProcessingModule::parameters() const + QVariantMap IscatModule::parameters() const { return {{QStringLiteral("mode"), m_mode}, {QStringLiteral("blur_sigma"), m_blurSigma}, @@ -61,7 +61,7 @@ namespace scopeone::iscat {QStringLiteral("output_mode"), m_outputMode}}; } - void IscatProcessingModule::setParameters(const QVariantMap& parameters) + void IscatModule::setParameters(const QVariantMap& parameters) { const int previousMode = m_mode; const double previousBlurSigma = m_blurSigma; @@ -136,20 +136,20 @@ namespace scopeone::iscat } } - std::unique_ptr IscatProcessingModule::createRuntime() const + std::unique_ptr IscatModule::createRuntime() const { - auto module = std::make_unique(); + auto module = std::make_unique(); module->setParameters(parameters()); return module; } - bool IscatProcessingModule::resetState() + bool IscatModule::resetState() { clearState(); return true; } - void IscatProcessingModule::clearState() + void IscatModule::clearState() { m_medianBuffer.clear(); m_lockedMedianBackground.release(); @@ -161,7 +161,7 @@ namespace scopeone::iscat m_draSumB.release(); } - cv::Mat IscatProcessingModule::computePixelMedian(const std::deque& frames) const + cv::Mat IscatModule::computePixelMedian(const std::deque& frames) const { cv::Mat median(frames.front().size(), CV_32F); cv::parallel_for_(cv::Range(0, median.rows), @@ -189,7 +189,7 @@ namespace scopeone::iscat return median; } - scopeone::core::ProcessingResult IscatProcessingModule::finalizeContrastOutput( + scopeone::core::ProcessingResult IscatModule::finalizeContrastOutput( const cv::Mat& input, const cv::Mat& background, const scopeone::core::ImageFrame& frame) @@ -233,7 +233,7 @@ namespace scopeone::iscat return {makeMono16Output(frame, outputValues), {}}; } - scopeone::core::ProcessingResult IscatProcessingModule::processFlatField( + scopeone::core::ProcessingResult IscatModule::processFlatField( const cv::Mat& input, const scopeone::core::ImageFrame& frame) { @@ -245,7 +245,7 @@ namespace scopeone::iscat return finalizeContrastOutput(input, background, frame); } - scopeone::core::ProcessingResult IscatProcessingModule::processTemporalMedian( + scopeone::core::ProcessingResult IscatModule::processTemporalMedian( const cv::Mat& input, const scopeone::core::ImageFrame& frame) { @@ -275,7 +275,7 @@ namespace scopeone::iscat return finalizeContrastOutput(input, m_lockedMedianBackground, frame); } - scopeone::core::ProcessingResult IscatProcessingModule::processEma( + scopeone::core::ProcessingResult IscatModule::processEma( const cv::Mat& input, const scopeone::core::ImageFrame& frame) { @@ -297,7 +297,7 @@ namespace scopeone::iscat return finalizeContrastOutput(input, background, frame); } - scopeone::core::ProcessingResult IscatProcessingModule::processSnapshot( + scopeone::core::ProcessingResult IscatModule::processSnapshot( const cv::Mat& input, const scopeone::core::ImageFrame& frame) { @@ -312,7 +312,7 @@ namespace scopeone::iscat return finalizeContrastOutput(input, m_snapshotBackground, frame); } - scopeone::core::ProcessingResult IscatProcessingModule::processDra( + scopeone::core::ProcessingResult IscatModule::processDra( const cv::Mat& input, const scopeone::core::ImageFrame& frame) { @@ -365,7 +365,7 @@ namespace scopeone::iscat frame); } - scopeone::core::ProcessingResult IscatProcessingModule::process( + scopeone::core::ProcessingResult IscatModule::process( const scopeone::core::ImageFrame& frame, int) { diff --git a/plugins/hardware/NIDaqmx/NIDaqmxPlugin.cpp b/ScopeOneCore/src/NIDaqmx.cpp similarity index 99% rename from plugins/hardware/NIDaqmx/NIDaqmxPlugin.cpp rename to ScopeOneCore/src/NIDaqmx.cpp index bbc0f49..00f0cd9 100644 --- a/plugins/hardware/NIDaqmx/NIDaqmxPlugin.cpp +++ b/ScopeOneCore/src/NIDaqmx.cpp @@ -1,4 +1,4 @@ -#include "NIDaqmxPlugin.h" +#include "internal/NIDaqmx.h" #include #include @@ -13,7 +13,7 @@ #include #include -namespace scopeone::plugins +namespace scopeone::core::internal { using namespace scopeone::core; @@ -822,7 +822,7 @@ namespace scopeone::plugins } } - QList NIDaqmxPlugin::devices() const + QList NIDaqmxDeviceFactory::devices() const { DaqmxApi api; if (!api.load(false)) @@ -864,7 +864,7 @@ namespace scopeone::plugins return result; } - DaqController* NIDaqmxPlugin::createController(const QString& deviceId, + DaqController* NIDaqmxDeviceFactory::createController(const QString& deviceId, QObject* parent) { const QString device = nativeDeviceId(deviceId.trimmed()); diff --git a/ScopeOneCore/src/PluginManifest.cpp b/ScopeOneCore/src/PluginManifest.cpp index 891e49c..6ef8b83 100644 --- a/ScopeOneCore/src/PluginManifest.cpp +++ b/ScopeOneCore/src/PluginManifest.cpp @@ -1,10 +1,7 @@ #include "scopeone/PluginManifest.h" -#include "scopeone/DaqDevice.h" #include "scopeone/DriverHostProviderPlugin.h" #include "scopeone/ProcessingPlugin.h" -#include "scopeone/SignalSource.h" -#include "scopeone/ToolPlugin.h" #include #include @@ -23,8 +20,6 @@ namespace scopeone::core { case PluginKind::Processing: return QStringLiteral("processing"); - case PluginKind::Tool: - return QStringLiteral("tool"); case PluginKind::Hardware: return QStringLiteral("hardware"); } @@ -37,8 +32,6 @@ namespace scopeone::core { case PluginKind::Processing: return ScopeOneProcessingPluginApiVersion; - case PluginKind::Tool: - return ScopeOneToolPluginApiVersion; case PluginKind::Hardware: return ScopeOneHardwarePluginApiVersion; } @@ -47,7 +40,7 @@ namespace scopeone::core QString pluginDirectoryName(PluginKind kind) { - return kind == PluginKind::Tool ? QStringLiteral("tools") : pluginKindName(kind); + return pluginKindName(kind); } QStringList pluginInterfaceIds(PluginKind kind) @@ -56,12 +49,8 @@ namespace scopeone::core { case PluginKind::Processing: return {QStringLiteral(ScopeOneProcessingPlugin_iid)}; - case PluginKind::Tool: - return {QStringLiteral(ScopeOneToolPlugin_iid)}; case PluginKind::Hardware: - return {QStringLiteral(ScopeOneDriverHostProviderPlugin_iid), - QStringLiteral(SCOPEONE_DAQ_DEVICE_PLUGIN_IID), - QStringLiteral(SCOPEONE_SIGNAL_SOURCE_PLUGIN_IID)}; + return {QStringLiteral(ScopeOneDriverHostProviderPlugin_iid)}; } return {}; } diff --git a/ScopeOneCore/src/ProcessingModuleRegistry.cpp b/ScopeOneCore/src/ProcessingModuleRegistry.cpp index 79e55b5..2e3faea 100644 --- a/ScopeOneCore/src/ProcessingModuleRegistry.cpp +++ b/ScopeOneCore/src/ProcessingModuleRegistry.cpp @@ -6,6 +6,7 @@ #include "internal/FrequencyDomainFilterModule.h" #include "internal/MaskModule.h" #include "internal/GaussianBlurModule.h" +#include "internal/IscatModule.h" #include "internal/ImageOperationModules.h" #include "internal/IFFTModule.h" #include "internal/SpatiotemporalBinningModule.h" @@ -195,6 +196,26 @@ namespace scopeone::core::internal {"Subtract", "Add", "Multiply", "Divide"})}, true}); + registerBuiltIn( + registry, + {QStringLiteral("iscat.processing"), + QStringLiteral("iSCAT Processing"), + 1, + {choiceParameter("mode", "Background mode", 0, + {"Flat Field", "Temporal Median", "Dynamic EMA", + "Snapshot Reference", "Differential Rolling (DRA)"}), + realParameter("blur_sigma", "Blur sigma", 15.0, 1.0, 100.0, 0.5, 1), + integerParameter("median_window", "Median window", 31, 3, 101, 2), + realParameter("ema_alpha", "Smoothing factor", 0.05, 0.001, 0.5, 0.005, 3), + booleanParameter("capture_reference", "Capture Reference", false), + integerParameter("batch_size", "DRA batch size", 16, 1, 500, 1), + realParameter("contrast_gain", "Contrast gain", 20.0, 1.0, 500.0, 1.0, 1), + booleanParameter("enable_high_pass", "Enable High-Pass", false), + realParameter("high_pass_sigma", "High-Pass sigma", 30.0, 2.0, 200.0, 1.0, 1), + choiceParameter("output_mode", "Output mode", 0, + {"Contrast Centered", "Absolute Contrast", "Estimated Background"})}, + true}); + registerImageOperationModules(registry); } } @@ -274,7 +295,6 @@ namespace scopeone::core::internal const QList pluginDescriptors = plugin->processingModules(); QSet pluginIds; - // A plugin may offer no modules, as the Python plugin does without scripts bool valid = true; for (const ProcessingModuleDescriptor& descriptor : pluginDescriptors) { diff --git a/plugins/hardware/PtuFile/PtuFilePlugin.cpp b/ScopeOneCore/src/PtuFile.cpp similarity index 99% rename from plugins/hardware/PtuFile/PtuFilePlugin.cpp rename to ScopeOneCore/src/PtuFile.cpp index dc8fbbe..ead887c 100644 --- a/plugins/hardware/PtuFile/PtuFilePlugin.cpp +++ b/ScopeOneCore/src/PtuFile.cpp @@ -1,4 +1,4 @@ -#include "PtuFilePlugin.h" +#include "internal/PtuFile.h" #include #include @@ -12,7 +12,7 @@ #include #include -namespace scopeone::plugins +namespace scopeone::core::internal { using namespace scopeone::core; @@ -578,12 +578,12 @@ namespace scopeone::plugins }; } - QList PtuFilePlugin::signalSources() const + QList PtuFileSourceFactory::signalSources() const { return {sourceDescriptor()}; } - SignalSource* PtuFilePlugin::createSignalSource(const QString& sourceId, + SignalSource* PtuFileSourceFactory::createSignalSource(const QString& sourceId, QObject* parent) { return sourceId.trimmed() == QString::fromLatin1(kSourceId) diff --git a/ScopeOneCore/src/PythonProcessingModule.cpp b/ScopeOneCore/src/PythonProcessingModule.cpp new file mode 100644 index 0000000..374c2e0 --- /dev/null +++ b/ScopeOneCore/src/PythonProcessingModule.cpp @@ -0,0 +1,292 @@ +#include "internal/PythonProcessingModule.h" + +#include "internal/ProcessingModuleRegistry.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace scopeone::core::internal +{ + namespace + { + // SCOPEONE_PYTHON, else the interpreter of the Settings dialog, picks the interpreter, + // which needs numpy and the scopeone package + QString pythonExecutable() + { + const QString configured = qEnvironmentVariable("SCOPEONE_PYTHON"); + if (!configured.isEmpty()) + { + return configured; + } + const QString selected = QSettings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")) + .value(QStringLiteral("Python/Interpreter")).toString(); + if (!selected.isEmpty()) + { + return selected; + } +#ifdef Q_OS_WIN + return QStringLiteral("python"); +#else + return QStringLiteral("python3"); +#endif + } + + QStringList workerArguments(const QString& command, const QString& scriptPath) + { + return {QStringLiteral("-m"), QStringLiteral("scopeone.processing"), command, scriptPath}; + } + + ProcessingParameterDescriptor parameterFrom(const QJsonObject& object) + { + static const QHash types{ + {QStringLiteral("int"), ProcessingParameterType::Integer}, + {QStringLiteral("real"), ProcessingParameterType::Real}, + {QStringLiteral("bool"), ProcessingParameterType::Boolean}, + {QStringLiteral("choice"), ProcessingParameterType::Choice}}; + ProcessingParameterDescriptor parameter; + parameter.key = object.value(QStringLiteral("key")).toString(); + parameter.name = object.value(QStringLiteral("name")).toString(); + parameter.type = types.value(object.value(QStringLiteral("type")).toString()); + parameter.defaultValue = object.value(QStringLiteral("default")).toVariant(); + parameter.minimum = object.value(QStringLiteral("minimum")).toVariant(); + parameter.maximum = object.value(QStringLiteral("maximum")).toVariant(); + parameter.step = object.value(QStringLiteral("step")).toVariant(); + parameter.decimals = object.value(QStringLiteral("decimals")).toInt(); + for (const QJsonValue& choice : object.value(QStringLiteral("choices")).toArray()) + { + parameter.choices.append({choice[QStringLiteral("name")].toString(), + choice[QStringLiteral("value")].toVariant()}); + } + return parameter; + } + + struct PythonScriptModule + { + QString scriptPath; + ProcessingModuleDescriptor descriptor; + }; + + // The modules a script declares, as `python -m scopeone.processing describe` lists them + QList describeScript(const QString& scriptPath, QString& error) + { + QProcess process; + process.start(pythonExecutable(), workerArguments(QStringLiteral("describe"), scriptPath)); + if (!process.waitForStarted()) + { + error = QStringLiteral("cannot start %1").arg(pythonExecutable()); + return {}; + } + if (!process.waitForFinished(30000)) + { + process.kill(); + process.waitForFinished(); + error = QStringLiteral("describe timed out"); + return {}; + } + if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) + { + // A failure without stderr output left the error empty, so the script was skipped silently + error = QString::fromUtf8(process.readAllStandardError()).trimmed(); + if (error.isEmpty()) + { + error = process.exitStatus() != QProcess::NormalExit + ? QStringLiteral("describe crashed") + : QStringLiteral("describe exited with code %1").arg(process.exitCode()); + } + return {}; + } + + QList modules; + for (const QJsonValue& value : QJsonDocument::fromJson(process.readAllStandardOutput()).array()) + { + const QJsonObject object = value.toObject(); + PythonScriptModule module; + module.scriptPath = scriptPath; + module.descriptor.id = object.value(QStringLiteral("id")).toString(); + module.descriptor.name = object.value(QStringLiteral("name")).toString(); + for (const QJsonValue& parameter : object.value(QStringLiteral("parameters")).toArray()) + { + module.descriptor.parameters.append(parameterFrom(parameter.toObject())); + } + modules.append(module); + } + return modules; + } + + // One module of a pipeline. The pipeline calls process from changing pool threads, so the + // Python process lives on a thread of its own and every frame is handed to that thread. + class PythonProcessingModule final : public ProcessingModule + { + public: + explicit PythonProcessingModule(std::shared_ptr script) + : m_script(std::move(script)) + { + for (const ProcessingParameterDescriptor& parameter : m_script->descriptor.parameters) + { + m_parameters.insert(parameter.key, parameter.defaultValue); + } + } + + ~PythonProcessingModule() override + { + if (m_thread) + { + QMetaObject::invokeMethod(m_threadContext.get(), [this]() { m_process.reset(); }, + Qt::BlockingQueuedConnection); + m_thread->quit(); + m_thread->wait(); + } + } + + QString id() const override { return m_script->descriptor.id; } + QString name() const override { return m_script->descriptor.name; } + QVariantMap parameters() const override { return m_parameters; } + + void setParameters(const QVariantMap& parameters) override + { + for (auto it = parameters.constBegin(); it != parameters.constEnd(); ++it) + { + m_parameters.insert(it.key(), it.value()); + } + } + + std::unique_ptr createRuntime() const override + { + auto runtime = std::make_unique(m_script); + runtime->setParameters(m_parameters); + return runtime; + } + + ProcessingResult process(const ImageFrame& frame, int) override + { + if (!m_thread) + { + m_thread = std::make_unique(); + m_threadContext = std::make_unique(); + m_threadContext->moveToThread(m_thread.get()); + m_thread->start(); + } + ProcessingResult result; + QMetaObject::invokeMethod(m_threadContext.get(), [&]() { result = exchange(frame); }, + Qt::BlockingQueuedConnection); + return result; + } + + private: + ProcessingResult failure(const QString& message) + { + const QString log = m_process ? QString::fromUtf8(m_process->readAllStandardError()) : QString(); + m_process.reset(); + return ProcessingResult(ImageFrame{}, log.isEmpty() ? message : message + QLatin1Char('\n') + log); + } + + // Sends one frame and reads the frame that comes back, on the module's own thread + ProcessingResult exchange(const ImageFrame& frame) + { + if (!m_process) + { + m_process = std::make_unique(); + m_process->start(pythonExecutable(), + workerArguments(QStringLiteral("serve"), m_script->scriptPath)); + if (!m_process->waitForStarted(10000)) + { + return failure(QStringLiteral("Could not start %1").arg(pythonExecutable())); + } + } + + const QJsonObject request{ + {QStringLiteral("module"), m_script->descriptor.id}, + {QStringLiteral("width"), frame.width}, + {QStringLiteral("height"), frame.height}, + {QStringLiteral("stride"), frame.stride}, + {QStringLiteral("format"), frame.isMono16() ? QStringLiteral("mono16") : QStringLiteral("mono8")}, + {QStringLiteral("bits"), frame.isMono16() ? frame.bitsPerSample : 8}, + {QStringLiteral("parameters"), QJsonObject::fromVariantMap(m_parameters)}, + {QStringLiteral("bytes"), static_cast(frame.bytes.size())}}; + m_process->write(QJsonDocument(request).toJson(QJsonDocument::Compact) + '\n'); + m_process->write(frame.bytes); + + while (!m_process->canReadLine()) + { + if (!m_process->waitForReadyRead(30000)) + { + return failure(QStringLiteral("Python module %1 stopped answering").arg(id())); + } + } + const QJsonObject reply = QJsonDocument::fromJson(m_process->readLine()).object(); + const QByteArray printed = m_process->readAllStandardError(); + if (!printed.isEmpty()) + { + qInfo().noquote() << QStringLiteral("[%1] %2").arg(id(), QString::fromUtf8(printed).trimmed()); + } + if (reply.contains(QStringLiteral("error"))) + { + return ProcessingResult(ImageFrame{}, reply.value(QStringLiteral("error")).toString()); + } + + const qint64 byteCount = reply.value(QStringLiteral("bytes")).toInteger(); + while (m_process->bytesAvailable() < byteCount) + { + if (!m_process->waitForReadyRead(30000)) + { + return failure(QStringLiteral("Python module %1 stopped answering").arg(id())); + } + } + + ImageFrame output = frame; + const bool mono16 = reply.value(QStringLiteral("format")).toString() == QStringLiteral("mono16"); + output.pixelFormat = mono16 ? ImagePixelFormat::Mono16 : ImagePixelFormat::Mono8; + output.bitsPerSample = mono16 ? (frame.isMono16() ? frame.bitsPerSample : 16) : 8; + output.width = reply.value(QStringLiteral("width")).toInt(); + output.height = reply.value(QStringLiteral("height")).toInt(); + output.stride = output.width * (mono16 ? 2 : 1); + output.bytes = m_process->read(byteCount); + return ProcessingResult(output); + } + + std::shared_ptr m_script; + QVariantMap m_parameters; + std::unique_ptr m_thread; + std::unique_ptr m_threadContext; + std::unique_ptr m_process; + }; + } + + QStringList registerPythonProcessingModules(ProcessingModuleRegistry& registry, + const QString& pluginDirectory) + { + QStringList errors; + const QDir scripts(QDir(pluginDirectory).filePath(QStringLiteral("python"))); + for (const QString& name : scripts.entryList({QStringLiteral("*.py")}, QDir::Files, QDir::Name)) + { + QString error; + const QList modules = describeScript(scripts.filePath(name), error); + if (!error.isEmpty()) + { + errors.append(QStringLiteral("%1: %2").arg(name, error)); + continue; + } + for (const PythonScriptModule& module : modules) + { + const auto script = std::make_shared(module); + if (!registry.registerModule(script->descriptor, [script]() + { + return std::make_unique(script); + })) + { + errors.append(QStringLiteral("%1: invalid or duplicate processing module id %2") + .arg(name, script->descriptor.id)); + } + } + } + return errors; + } +} diff --git a/ScopeOneCore/src/ScopeOneCore.cpp b/ScopeOneCore/src/ScopeOneCore.cpp index 03b7b90..d00982f 100644 --- a/ScopeOneCore/src/ScopeOneCore.cpp +++ b/ScopeOneCore/src/ScopeOneCore.cpp @@ -7,6 +7,7 @@ #include "internal/FrameBufferUtils.h" #include "internal/ImageProcessingFramework.h" #include "internal/ProcessingModuleRegistry.h" +#include "internal/PythonProcessingModule.h" #include "internal/DriverHostProviderProxy.h" #include "internal/HardwareRuntime.h" #include "internal/MMCoreManager.h" @@ -1367,6 +1368,8 @@ namespace scopeone::core { allProcessingPluginErrors.append( m_managers->processingModuleRegistry->loadPlugins(directory)); + allProcessingPluginErrors.append(internal::registerPythonProcessingModules( + *m_managers->processingModuleRegistry, directory)); } for (const QString& error : allProcessingPluginErrors) { diff --git a/ScopeOneCore/src/SignalSourceManager.cpp b/ScopeOneCore/src/SignalSourceManager.cpp index 8d42eb3..a69e72f 100644 --- a/ScopeOneCore/src/SignalSourceManager.cpp +++ b/ScopeOneCore/src/SignalSourceManager.cpp @@ -1,9 +1,7 @@ #include "internal/SignalSourceManager.h" -#include "scopeone/PluginManifest.h" -#include -#include -#include +#include "internal/PtuFile.h" +#include "internal/SimulatedPmt.h" #include #include @@ -159,7 +157,7 @@ namespace scopeone::core::internal SignalSourceManager::SignalSourceManager(QObject* parent) : QObject(parent) { - loadPlugins(); + registerSources(); } SignalSourceManager::~SignalSourceManager() @@ -169,7 +167,7 @@ namespace scopeone::core::internal delete source.data(); } m_sources.clear(); - m_loaders.clear(); + m_factories.clear(); } QList SignalSourceManager::sources() const @@ -223,37 +221,13 @@ namespace scopeone::core::internal : m_messages.value(id, QStringLiteral("Signal source is idle")); } - void SignalSourceManager::loadPlugins() + void SignalSourceManager::registerSources() { - for (const DiscoveredPlugin& discovered : discoverPlugins(PluginKind::Hardware)) + m_factories.push_back(std::make_unique()); + m_factories.push_back(std::make_unique()); + for (const std::unique_ptr& factory : m_factories) { - if (discovered.interfaceId != QStringLiteral(SCOPEONE_SIGNAL_SOURCE_PLUGIN_IID)) - { - continue; - } - const QFileInfo file(discovered.path); - if (!discovered.error.isEmpty()) - { - qWarning().noquote() - << QStringLiteral("Failed to load signal source plugin %1: %2") - .arg(file.fileName(), discovered.error); - continue; - } - if (!pluginEnabled(discovered.manifest)) - { - continue; - } - auto loader = std::make_unique(discovered.path); - auto* plugin = qobject_cast(loader->instance()); - if (!plugin) - { - qWarning().noquote() - << QStringLiteral("Failed to load signal source plugin %1: %2") - .arg(file.fileName(), loader->errorString()); - continue; - } - - for (const SignalSourceDescriptor& descriptor : plugin->signalSources()) + for (const SignalSourceDescriptor& descriptor : factory->signalSources()) { const QString sourceId = descriptor.id.trimmed(); if (sourceId.isEmpty() || m_descriptors.contains(sourceId)) @@ -261,11 +235,10 @@ namespace scopeone::core::internal continue; } m_descriptors.insert(sourceId, descriptor); - m_plugins.insert(sourceId, plugin); + m_factoryBySource.insert(sourceId, factory.get()); m_states.insert(sourceId, SignalSourceState::Idle); m_messages.insert(sourceId, QStringLiteral("Signal source is idle")); } - m_loaders.push_back(std::move(loader)); } } @@ -277,8 +250,8 @@ namespace scopeone::core::internal { return existing; } - SignalSourcePlugin* plugin = m_plugins.value(id); - if (!plugin) + SignalSourceFactory* factory = m_factoryBySource.value(id); + if (!factory) { if (errorMessage) { @@ -286,7 +259,7 @@ namespace scopeone::core::internal } return nullptr; } - SignalSource* source = plugin->createSignalSource(id, this); + SignalSource* source = factory->createSignalSource(id, this); if (!source) { if (errorMessage) diff --git a/plugins/hardware/SimulatedPmt/SimulatedPmtPlugin.cpp b/ScopeOneCore/src/SimulatedPmt.cpp similarity index 94% rename from plugins/hardware/SimulatedPmt/SimulatedPmtPlugin.cpp rename to ScopeOneCore/src/SimulatedPmt.cpp index 83d2423..971c46c 100644 --- a/plugins/hardware/SimulatedPmt/SimulatedPmtPlugin.cpp +++ b/ScopeOneCore/src/SimulatedPmt.cpp @@ -1,8 +1,8 @@ -#include "SimulatedPmtPlugin.h" +#include "internal/SimulatedPmt.h" -#include "SimulatedPmtSource.h" +#include "internal/SimulatedPmtSource.h" -namespace scopeone::plugins +namespace scopeone::core::internal { namespace { @@ -125,12 +125,12 @@ namespace scopeone::plugins } } - QList SimulatedPmtPlugin::signalSources() const + QList SimulatedPmtSourceFactory::signalSources() const { return {sourceDescriptor()}; } - scopeone::core::SignalSource* SimulatedPmtPlugin::createSignalSource( + scopeone::core::SignalSource* SimulatedPmtSourceFactory::createSignalSource( const QString& sourceId, QObject* parent) { diff --git a/plugins/hardware/SimulatedPmt/SimulatedPmtSource.cpp b/ScopeOneCore/src/SimulatedPmtSource.cpp similarity index 99% rename from plugins/hardware/SimulatedPmt/SimulatedPmtSource.cpp rename to ScopeOneCore/src/SimulatedPmtSource.cpp index 3156a86..5978524 100644 --- a/plugins/hardware/SimulatedPmt/SimulatedPmtSource.cpp +++ b/ScopeOneCore/src/SimulatedPmtSource.cpp @@ -1,4 +1,4 @@ -#include "SimulatedPmtSource.h" +#include "internal/SimulatedPmtSource.h" #include #include @@ -7,7 +7,7 @@ #include #include -namespace scopeone::plugins +namespace scopeone::core::internal { using namespace scopeone::core; diff --git a/ScopeOneCore/src/SimulatorProvider.cpp b/ScopeOneCore/src/SimulatorProvider.cpp deleted file mode 100644 index 7a8861a..0000000 --- a/ScopeOneCore/src/SimulatorProvider.cpp +++ /dev/null @@ -1,761 +0,0 @@ -#include "scopeone/SimulatorProvider.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace scopeone::core -{ - namespace - { - struct Bead - { - double x; - double y; - double brightness; - }; - - constexpr std::array Beads{{ - {0.08, 0.10, 0.91}, {0.19, 0.08, 0.84}, {0.32, 0.12, 0.98}, - {0.46, 0.07, 0.88}, {0.62, 0.11, 0.94}, {0.77, 0.08, 0.82}, - {0.91, 0.13, 0.96}, {0.13, 0.25, 0.87}, {0.29, 0.29, 1.00}, - {0.48, 0.23, 0.83}, {0.69, 0.27, 0.92}, {0.87, 0.31, 0.86}, - {0.08, 0.43, 0.95}, {0.24, 0.47, 0.81}, {0.42, 0.39, 0.90}, - {0.61, 0.45, 0.97}, {0.80, 0.42, 0.85}, {0.94, 0.50, 0.93}, - {0.14, 0.63, 0.89}, {0.34, 0.58, 0.99}, {0.53, 0.66, 0.84}, - {0.72, 0.61, 0.96}, {0.89, 0.68, 0.80}, {0.07, 0.82, 0.92}, - {0.22, 0.88, 0.86}, {0.39, 0.79, 0.95}, {0.58, 0.86, 0.82}, - {0.76, 0.81, 0.98}, {0.92, 0.89, 0.88}, {0.47, 0.94, 0.91}, - {0.30, 0.70, 0.85}, {0.67, 0.75, 0.94} - }}; - - constexpr std::array BeadDoublets{{ - {0.175, 0.176, 1.00}, {0.191, 0.176, 0.92}, - {0.535, 0.335, 0.88}, {0.551, 0.335, 0.96}, - {0.365, 0.515, 0.98}, {0.381, 0.515, 0.86}, - {0.705, 0.925, 0.91}, {0.721, 0.925, 1.00} - }}; - } - - SimulatorProvider::SimulatorProvider(const QString& logicalCameraId, - int width, - int height, - const QString& providerId) - : m_providerId(providerId.trimmed().isEmpty() - ? QStringLiteral("simulator.%1").arg( - QUuid::createUuid().toString(QUuid::WithoutBraces)) - : providerId.trimmed()) - , m_cameraId(logicalCameraId.trimmed().isEmpty() - ? QStringLiteral("camera.simulator") - : logicalCameraId.trimmed()) - , m_sensorWidth((std::max)(1, width)) - , m_sensorHeight((std::max)(1, height)) - , m_roi(0, 0, m_sensorWidth, m_sensorHeight) - { - m_timer.setTimerType(Qt::PreciseTimer); - updateTimerInterval(); - connect(&m_timer, &QTimer::timeout, this, [this]() - { - FrameSink sink; - { - QMutexLocker locker(&m_mutex); - sink = m_frameSink; - } - if (sink) sink(makeFrame()); - }); - } - - HardwareProviderDescriptor SimulatorProvider::descriptor() const - { - return {m_providerId, QStringLiteral("ScopeOne Simulator"), QStringLiteral("1")}; - } - - QList SimulatorProvider::devices() const - { - HardwareDeviceDescriptor camera; - camera.logicalId = m_cameraId; - camera.providerId = m_providerId; - camera.providerDeviceId = m_cameraId; - camera.hardwareId = m_cameraId; - camera.name = QStringLiteral("Simulator Camera"); - camera.kind = HardwareDeviceKind::Camera; - camera.state = HardwareDeviceState::Initialized; - camera.endpoint = HardwareEndpointKind::InProcess; - return {camera}; - } - - void SimulatorProvider::setFrameSink(FrameSink sink) - { - QMutexLocker locker(&m_mutex); - m_frameSink = std::move(sink); - } - - void SimulatorProvider::setPreviewStateSink(PreviewStateSink sink) - { - QMutexLocker locker(&m_mutex); - m_previewStateSink = std::move(sink); - } - - bool SimulatorProvider::startPreview() - { - { - QMutexLocker locker(&m_mutex); - if (!m_frameSink) return false; - } - m_timer.start(); - PreviewStateSink sink; - { - QMutexLocker locker(&m_mutex); - sink = m_previewStateSink; - } - if (sink) sink(true); - return true; - } - - bool SimulatorProvider::stopPreview() - { - const bool wasRunning = m_timer.isActive(); - m_timer.stop(); - PreviewStateSink sink; - { - QMutexLocker locker(&m_mutex); - sink = m_previewStateSink; - } - if (wasRunning && sink) sink(false); - return true; - } - - bool SimulatorProvider::startPreviewFor(const QString& cameraId) - { - return accepts(cameraId) && startPreview(); - } - - bool SimulatorProvider::stopPreviewFor(const QString& cameraId) - { - return accepts(cameraId) && stopPreview(); - } - - bool SimulatorProvider::isPreviewRunning(const QString& cameraId) const - { - return accepts(cameraId) && m_timer.isActive(); - } - - bool SimulatorProvider::getExposure(const QString& cameraIdOrAll, double& exposureMs) const - { - if (!accepts(cameraIdOrAll)) - { - return false; - } - QMutexLocker locker(&m_mutex); - exposureMs = m_exposureMs; - return true; - } - - bool SimulatorProvider::setExposure(const QString& cameraIdOrAll, double exposureMs) - { - if (!accepts(cameraIdOrAll) || !std::isfinite(exposureMs) || exposureMs <= 0.0) - { - return false; - } - { - QMutexLocker locker(&m_mutex); - m_exposureMs = exposureMs; - } - if (QThread::currentThread() == thread()) - { - updateTimerInterval(); - } - else - { - QMetaObject::invokeMethod(this, - [this]() { updateTimerInterval(); }, - Qt::QueuedConnection); - } - return true; - } - - QStringList SimulatorProvider::listProperties(const QString& cameraId) - { - return accepts(cameraId) - ? QStringList{QStringLiteral("Exposure"), - QStringLiteral("ImageMode"), - QStringLiteral("Resolution"), - QStringLiteral("GaussianNoiseSigma"), - QStringLiteral("SensorWidth"), - QStringLiteral("SensorHeight")} - : QStringList{}; - } - - QString SimulatorProvider::getProperty(const QString& cameraId, - const QString& name, - bool) - { - if (!accepts(cameraId)) - { - return {}; - } - if (name == QStringLiteral("Exposure")) - { - QMutexLocker locker(&m_mutex); - return QString::number(m_exposureMs, 'g', 12); - } - if (name == QStringLiteral("ImageMode")) - { - QMutexLocker locker(&m_mutex); - switch (m_imageMode) - { - case ImageMode::Gradient: return QStringLiteral("Gradient"); - case ImageMode::Hologram: return QStringLiteral("Hologram"); - case ImageMode::Beads: return QStringLiteral("Fluorescent Beads"); - case ImageMode::RedBloodCells: return QStringLiteral("Red Blood Cells"); - case ImageMode::Brillouin: return QStringLiteral("Brillouin"); - } - } - if (name == QStringLiteral("Resolution")) - { - QMutexLocker locker(&m_mutex); - return QString::number(m_sensorWidth); - } - if (name == QStringLiteral("GaussianNoiseSigma")) - { - QMutexLocker locker(&m_mutex); - return QString::number(m_gaussianNoiseSigma, 'g', 12); - } - if (name == QStringLiteral("SensorWidth")) - { - QMutexLocker locker(&m_mutex); - return QString::number(m_sensorWidth); - } - if (name == QStringLiteral("SensorHeight")) - { - QMutexLocker locker(&m_mutex); - return QString::number(m_sensorHeight); - } - return {}; - } - - bool SimulatorProvider::setProperty(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage) - { - if (errorMessage) - { - errorMessage->clear(); - } - if (!accepts(cameraId)) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Property is not writable"); - } - return false; - } - if (name == QStringLiteral("ImageMode")) - { - const QString mode = value.trimmed(); - if (mode != QStringLiteral("Gradient") - && mode != QStringLiteral("Hologram") - && mode != QStringLiteral("Fluorescent Beads") - && mode != QStringLiteral("Red Blood Cells") - && mode != QStringLiteral("Brillouin")) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Invalid image mode"); - } - return false; - } - QMutexLocker locker(&m_mutex); - m_imageMode = mode == QStringLiteral("Hologram") ? ImageMode::Hologram - : mode == QStringLiteral("Fluorescent Beads") ? ImageMode::Beads - : mode == QStringLiteral("Red Blood Cells") ? ImageMode::RedBloodCells - : mode == QStringLiteral("Brillouin") ? ImageMode::Brillouin - : ImageMode::Gradient; - if (m_imageMode == ImageMode::Beads && m_beadsImage.isEmpty()) - { - rebuildBeadsImage(); - } - if ((m_imageMode == ImageMode::RedBloodCells || m_imageMode == ImageMode::Brillouin) && m_cellPhase.empty()) - { - rebuildBloodCells(); - } - return true; - } - if (name == QStringLiteral("Resolution")) - { - const int resolution = value.toInt(); - if (!getAllowedPropertyValues(cameraId, name).contains(QString::number(resolution))) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Invalid resolution"); - } - return false; - } - QMutexLocker locker(&m_mutex); - m_sensorWidth = resolution; - m_sensorHeight = resolution; - m_roi = QRect(0, 0, resolution, resolution); - m_beadsImage.clear(); - m_cellAmplitude.clear(); - m_cellPhase.clear(); - if (m_imageMode == ImageMode::Beads) - { - rebuildBeadsImage(); - } - if (m_imageMode == ImageMode::RedBloodCells || m_imageMode == ImageMode::Brillouin) - { - rebuildBloodCells(); - } - return true; - } - if (name == QStringLiteral("GaussianNoiseSigma")) - { - QMutexLocker locker(&m_mutex); - m_gaussianNoiseSigma = std::clamp(value.toDouble(), 0.0, 100.0); - return true; - } - if (name == QStringLiteral("Exposure")) - { - bool ok = false; - const double exposureMs = value.toDouble(&ok); - if (ok && setExposure(cameraId, exposureMs)) - { - return true; - } - if (errorMessage) - { - *errorMessage = QStringLiteral("Invalid exposure value"); - } - } - else if (errorMessage) - { - *errorMessage = QStringLiteral("Property is not writable"); - } - return false; - } - - QString SimulatorProvider::getPropertyType(const QString& cameraId, const QString& name) - { - return accepts(cameraId) && listProperties(cameraId).contains(name) - ? (name == QStringLiteral("Exposure") - || name == QStringLiteral("GaussianNoiseSigma") - ? QStringLiteral("Float") - : name == QStringLiteral("ImageMode") - ? QStringLiteral("String") - : QStringLiteral("Integer")) - : QStringLiteral("Unknown"); - } - - bool SimulatorProvider::isPropertyReadOnly(const QString& cameraId, const QString& name) - { - return !accepts(cameraId) - || (name != QStringLiteral("Exposure") - && name != QStringLiteral("ImageMode") - && name != QStringLiteral("Resolution") - && name != QStringLiteral("GaussianNoiseSigma")); - } - - bool SimulatorProvider::isPropertyPreInit(const QString&, const QString&) - { - return false; - } - - QStringList SimulatorProvider::getAllowedPropertyValues(const QString& cameraId, - const QString& name) - { - if (accepts(cameraId) && name == QStringLiteral("ImageMode")) - { - return {QStringLiteral("Gradient"), - QStringLiteral("Hologram"), - QStringLiteral("Red Blood Cells"), - QStringLiteral("Brillouin"), - QStringLiteral("Fluorescent Beads")}; - } - if (accepts(cameraId) && name == QStringLiteral("Resolution")) - { - return {QStringLiteral("256"), - QStringLiteral("512"), - QStringLiteral("1024"), - QStringLiteral("2048")}; - } - return {}; - } - - bool SimulatorProvider::hasPropertyLimits(const QString& cameraId, const QString& name) - { - return accepts(cameraId) - && (name == QStringLiteral("Exposure") - || name == QStringLiteral("GaussianNoiseSigma")); - } - - double SimulatorProvider::getPropertyLowerLimit(const QString& cameraId, const QString& name) - { - if (!accepts(cameraId)) - { - return 0.0; - } - return name == QStringLiteral("Exposure") ? 0.1 : 0.0; - } - - double SimulatorProvider::getPropertyUpperLimit(const QString& cameraId, const QString& name) - { - if (!accepts(cameraId)) - { - return 0.0; - } - return name == QStringLiteral("Exposure") ? 1000.0 - : name == QStringLiteral("GaussianNoiseSigma") ? 100.0 - : 0.0; - } - - bool SimulatorProvider::setROI(const QString& cameraId, - int x, - int y, - int width, - int height) - { - const QRect roi(x, y, width, height); - if (!accepts(cameraId) || width <= 0 || height <= 0) - { - return false; - } - QMutexLocker locker(&m_mutex); - if (!QRect(0, 0, m_sensorWidth, m_sensorHeight).contains(roi)) - { - return false; - } - m_roi = roi; - return true; - } - - bool SimulatorProvider::clearROI(const QString& cameraId) - { - if (!accepts(cameraId)) - { - return false; - } - QMutexLocker locker(&m_mutex); - m_roi = QRect(0, 0, m_sensorWidth, m_sensorHeight); - return true; - } - - bool SimulatorProvider::getROI(const QString& cameraId, - int& x, - int& y, - int& width, - int& height) - { - if (!accepts(cameraId)) - { - return false; - } - QMutexLocker locker(&m_mutex); - x = m_roi.x(); - y = m_roi.y(); - width = m_roi.width(); - height = m_roi.height(); - return true; - } - - bool SimulatorProvider::captureEventFrame(const QString& cameraId, - ImageFrame& frame, - int) - { - if (!accepts(cameraId)) - { - return false; - } - frame = makeFrame(); - return frame.isValid(); - } - - bool SimulatorProvider::accepts(const QString& cameraIdOrAll) const - { - const QString target = cameraIdOrAll.trimmed(); - return target == m_cameraId - || target.compare(QStringLiteral("All"), Qt::CaseInsensitive) == 0; - } - - ImageFrame SimulatorProvider::makeFrame() - { - QMutexLocker locker(&m_mutex); - ImageFrame frame; - frame.cameraId = m_cameraId; - frame.width = m_roi.width(); - frame.height = m_roi.height(); - frame.stride = frame.width; - frame.bitsPerSample = 8; - frame.pixelFormat = ImagePixelFormat::Mono8; - frame.frameIndex = ++m_frameIndex; - frame.timestampNs = static_cast(QDateTime::currentMSecsSinceEpoch()) * 1000000ull; - frame.sourceRoiX = m_roi.x(); - frame.sourceRoiY = m_roi.y(); - frame.sourceRoiWidth = m_roi.width(); - frame.sourceRoiHeight = m_roi.height(); - frame.bytes.resize(static_cast(frame.width) * frame.height); - std::normal_distribution gaussianNoise; - constexpr double twoPi = 6.28318530717958647692; - for (int y = 0; y < frame.height; ++y) - { - uchar* row = reinterpret_cast(frame.bytes.data()) - + static_cast(y) * frame.stride; - for (int x = 0; x < frame.width; ++x) - { - double intensity = 0.0; - if (m_imageMode == ImageMode::Gradient) - { - intensity = static_cast((x + y + frame.frameIndex) & 0xffu); - } - else if (m_imageMode == ImageMode::Hologram) - { - const int sensorX = m_roi.x() + x; - const int sensorY = m_roi.y() + y; - const double nx = (sensorX - 0.5 * m_sensorWidth) / m_sensorWidth; - const double ny = (sensorY - 0.5 * m_sensorHeight) / m_sensorHeight; - const double objectAmplitude = - 0.65 * std::exp(-35.0 * (nx * nx + ny * ny)) - + 0.35 * std::exp(-90.0 * ((nx - 0.18) * (nx - 0.18) - + (ny + 0.12) * (ny + 0.12))); - const double objectPhase = 18.0 * (nx * nx + ny * ny) - + 0.015 * static_cast(frame.frameIndex); - // The +1 order of this carrier lies in the upper half of the spectrum, at (48, -32), which is - // where the reconstruction looks for it, so that the phase comes out with the right sign - const double carrier = twoPi * (48.0 * sensorX / m_sensorWidth - - 32.0 * sensorY / m_sensorHeight); - intensity = 70.0 - + 55.0 * objectAmplitude * objectAmplitude - + 120.0 * objectAmplitude * std::cos(carrier + objectPhase); - } - else if (m_imageMode == ImageMode::RedBloodCells) - { - // An off axis hologram like the one above, of a thin layer of red blood cells. Besides - // bending the fringes, the cells take some of the light, which dims the fringes over them. - const int sensorX = m_roi.x() + x; - const int sensorY = m_roi.y() + y; - const std::size_t index = static_cast(sensorY) * m_sensorWidth + sensorX; - const double objectAmplitude = 0.8 * m_cellAmplitude[index]; - // The background is flat and the fringes stay where they are - const double objectPhase = m_cellPhase[index]; - // The +1 order of this carrier lies in the upper half of the spectrum, at (48, -32), which is - // where the reconstruction looks for it, so that the phase comes out with the right sign - const double carrier = twoPi * (48.0 * sensorX / m_sensorWidth - - 32.0 * sensorY / m_sensorHeight); - // The reference wave is much stronger than the light of the cells, as it is in a real setup, - // so the fringes have a contrast of about one half and the picture stays well inside the range - constexpr double kReferenceAmplitude = 11.8; - constexpr double kObjectScale = 4.1; - const double object = kObjectScale * objectAmplitude; - intensity = kReferenceAmplitude * kReferenceAmplitude - + object * object - + 2.0 * kReferenceAmplitude * object * std::cos(carrier + objectPhase); - } - else if (m_imageMode == ImageMode::Brillouin) - { - // The image of a spectrometer. It holds one line of the sample after another, each in a band of - // 32 rows. Along a band the columns are positions on the line, and across it the rows are the - // frequency. Every position gives the Stokes and the anti-Stokes peak of the Brillouin - // scattering, which lie 10 pixels from the center of the band for water, and further out where a - // cell is thick, as the cell is stiffer. - constexpr int kBandRows = 32; - constexpr double kWaterShift = 10.0; - constexpr double kCellShift = 2.4; - constexpr double kPeakPhase = 6.28318530717958647692 * 0.06 / 0.633 * 2.4; - constexpr double kWidth = 1.3; - const int sensorX = m_roi.x() + x; - const int sensorY = m_roi.y() + y; - const int band = sensorY / kBandRows; - const double offset = sensorY % kBandRows - 0.5 * (kBandRows - 1); - const int sampleY = (std::min)(m_sensorHeight - 1, band * kBandRows + kBandRows / 2); - const double thickness = std::clamp( - m_cellPhase[static_cast(sampleY) * m_sensorWidth + sensorX] / kPeakPhase, 0.0, 1.0); - const double shift = kWaterShift + kCellShift * thickness; - const double stokes = (offset + shift) / kWidth; - const double antiStokes = (offset - shift) / kWidth; - intensity = 8.0 + 180.0 * (1.0 / (1.0 + stokes * stokes) + 1.0 / (1.0 + antiStokes * antiStokes)); - } - else - { - const int sensorX = m_roi.x() + x; - const int sensorY = m_roi.y() + y; - intensity = static_cast(m_beadsImage.at( - sensorY * m_sensorWidth + sensorX)); - } - if (m_gaussianNoiseSigma > 0.0) - { - intensity += m_gaussianNoiseSigma * gaussianNoise(m_randomGenerator); - } - row[x] = static_cast(std::clamp(intensity, 0.0, 255.0)); - } - } - return frame; - } - - // Build a static field of Gaussian PSFs for fast live preview - // Lays out red blood cells that do not touch, each a biconcave disc, and works out the amplitude and the phase that - // a plane wave takes on passing through them. The thickness follows the shape of Evans and Fung, scaled to a rim of - // 2.4 micrometers and a dimple of about 0.8, and the phase is 2 pi times the difference of the refractive index of - // the cell and the medium (0.06) times the thickness, over the wavelength (0.633 micrometers). - void SimulatorProvider::rebuildBloodCells() - { - const std::size_t count = static_cast(m_sensorWidth) * m_sensorHeight; - m_cellAmplitude.assign(count, 1.0f); - m_cellPhase.assign(count, 0.0f); - - constexpr double kC0 = 0.207161; - constexpr double kC2 = 2.002558; - constexpr double kC4 = -1.122762; - const auto shape = [](double rho) - { - return std::sqrt((std::max)(0.0, 1.0 - rho * rho)) * (kC0 + kC2 * rho * rho + kC4 * rho * rho * rho * rho); - }; - double peak = 0.0; - for (int step = 0; step <= 1000; ++step) - { - peak = (std::max)(peak, shape(step / 1000.0)); - } - constexpr double kPeakThicknessUm = 2.4; - constexpr double kPhasePerMicrometer = 6.28318530717958647692 * 0.06 / 0.633; - - // The field is about 60 micrometers wide and a cell about 7.8 micrometers across - const double radiusBase = 0.065 * (std::min)(m_sensorWidth, m_sensorHeight); - struct Cell - { - double x; - double y; - double radius; - double squeeze; - double angle; - }; - std::vector cells; - std::mt19937 layoutGenerator(20240601u); - std::uniform_real_distribution unit(0.0, 1.0); - for (int attempt = 0; attempt < 4000 && cells.size() < 24; ++attempt) - { - Cell cell; - cell.radius = radiusBase * (0.9 + 0.2 * unit(layoutGenerator)); - cell.squeeze = 0.85 + 0.15 * unit(layoutGenerator); - cell.angle = 3.14159265358979323846 * unit(layoutGenerator); - cell.x = cell.radius + unit(layoutGenerator) * (m_sensorWidth - 2.0 * cell.radius); - cell.y = cell.radius + unit(layoutGenerator) * (m_sensorHeight - 2.0 * cell.radius); - bool free = true; - for (const Cell& other : cells) - { - const double distance = std::hypot(cell.x - other.x, cell.y - other.y); - free = free && distance > 1.08 * (cell.radius + other.radius); - } - if (free) - { - cells.push_back(cell); - } - } - - for (const Cell& cell : cells) - { - const int reach = static_cast(std::ceil(cell.radius * 1.05)); - const int minX = (std::max)(0, static_cast(cell.x) - reach); - const int maxX = (std::min)(m_sensorWidth - 1, static_cast(cell.x) + reach); - const int minY = (std::max)(0, static_cast(cell.y) - reach); - const int maxY = (std::min)(m_sensorHeight - 1, static_cast(cell.y) + reach); - const double cosine = std::cos(cell.angle); - const double sine = std::sin(cell.angle); - for (int y = minY; y <= maxY; ++y) - { - for (int x = minX; x <= maxX; ++x) - { - const double dx = x + 0.5 - cell.x; - const double dy = y + 0.5 - cell.y; - // The cell is a little flattened along one direction - const double u = (dx * cosine + dy * sine) / cell.radius; - const double v = (-dx * sine + dy * cosine) / (cell.radius * cell.squeeze); - const double rho = std::sqrt(u * u + v * v); - if (rho >= 1.0) - { - continue; - } - const double thicknessUm = kPeakThicknessUm * shape(rho) / peak; - const std::size_t index = static_cast(y) * m_sensorWidth + x; - m_cellPhase[index] = static_cast(kPhasePerMicrometer * thicknessUm); - // Hemoglobin takes light, more where the cell is thick, so the fringes lose contrast over a cell - // and the cells show in the hologram itself as dim discs with bent fringes - m_cellAmplitude[index] = static_cast(1.0 - 0.5 * thicknessUm / kPeakThicknessUm); - } - } - } - - // A microscope does not see the sharp edge of a cell: its resolution and the size of a pixel soften it. Without - // this the thickness drops to zero at the rim with an infinite slope, which spreads over the whole spectrum and - // comes back from the reconstruction as ripples around the cells. - const double blurSigma = (std::max)(1.0, 0.004 * (std::min)(m_sensorWidth, m_sensorHeight)); - cv::Mat phaseMap(m_sensorHeight, m_sensorWidth, CV_32F, m_cellPhase.data()); - cv::Mat amplitudeMap(m_sensorHeight, m_sensorWidth, CV_32F, m_cellAmplitude.data()); - cv::GaussianBlur(phaseMap, phaseMap, cv::Size(0, 0), blurSigma, blurSigma, cv::BORDER_REPLICATE); - cv::GaussianBlur(amplitudeMap, amplitudeMap, cv::Size(0, 0), blurSigma, blurSigma, cv::BORDER_REPLICATE); - } - - void SimulatorProvider::rebuildBeadsImage() - { - std::vector intensities( - static_cast(m_sensorWidth) * m_sensorHeight, 3.0f); - const double resolutionScale = (std::min)(m_sensorWidth, m_sensorHeight) / 256.0; - const double sigma = 4.0 * resolutionScale / 2.354820045; - const int radius = static_cast(std::ceil(4.0 * sigma)); - - const auto drawBead = [&](const Bead& bead) - { - const double centerX = bead.x * m_sensorWidth; - const double centerY = bead.y * m_sensorHeight; - const int minX = (std::max)(0, static_cast(std::floor(centerX)) - radius); - const int maxX = (std::min)(m_sensorWidth - 1, - static_cast(std::floor(centerX)) + radius); - const int minY = (std::max)(0, static_cast(std::floor(centerY)) - radius); - const int maxY = (std::min)(m_sensorHeight - 1, - static_cast(std::floor(centerY)) + radius); - const double denominator = 2.0 * sigma * sigma; - for (int y = minY; y <= maxY; ++y) - { - for (int x = minX; x <= maxX; ++x) - { - const double dx = x + 0.5 - centerX; - const double dy = y + 0.5 - centerY; - intensities[static_cast(y) * m_sensorWidth + x] - += static_cast(245.0 * bead.brightness - * std::exp(-(dx * dx + dy * dy) / denominator)); - } - } - }; - - for (const Bead& bead : Beads) - { - drawBead(bead); - } - for (const Bead& bead : BeadDoublets) - { - drawBead(bead); - } - - m_beadsImage.resize(m_sensorWidth * m_sensorHeight); - for (qsizetype i = 0; i < m_beadsImage.size(); ++i) - { - m_beadsImage[i] = static_cast(std::clamp(intensities[i], 0.0f, 255.0f)); - } - } - - void SimulatorProvider::updateTimerInterval() - { - double exposureMs = 0.0; - { - QMutexLocker locker(&m_mutex); - exposureMs = m_exposureMs; - } - m_timer.setInterval((std::max)(1, static_cast(std::ceil(exposureMs)))); - } -} diff --git a/ScopeOneCore/src/ToolFrameStream.cpp b/ScopeOneCore/src/ToolFrameStream.cpp deleted file mode 100644 index 6c7d06b..0000000 --- a/ScopeOneCore/src/ToolFrameStream.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "scopeone/ToolFrameStream.h" -#include "scopeone/ToolPlugin.h" - -namespace scopeone::ui -{ - ScopeOneToolFrameStream::ScopeOneToolFrameStream(ScopeOneToolCore& core, QObject* parent) - : QObject(parent) - { - connect(&core, &ScopeOneToolCore::previewRawFrameReady, - this, &ScopeOneToolFrameStream::acceptFrame); - } - - void ScopeOneToolFrameStream::setSourceId(const QString& cameraId) - { - m_sourceId = cameraId.trimmed(); - } - - void ScopeOneToolFrameStream::setEnabled(bool enabled) - { - m_enabled = enabled; - if (!m_enabled) - { - clearPendingFrame(); - } - } - - void ScopeOneToolFrameStream::setProcessing(bool processing) - { - if (m_processing == processing) - { - return; - } - m_processing = processing; - if (!m_processing && m_pendingFrame.isValid()) - { - const auto frame = m_pendingFrame; - m_pendingFrame = {}; - m_processing = true; - emit frameReady(frame); - } - } - - void ScopeOneToolFrameStream::clearPendingFrame() - { - m_pendingFrame = {}; - } - - void ScopeOneToolFrameStream::acceptFrame(const scopeone::core::ImageFrame& frame) - { - if (!m_enabled || !frame.isValid() - || (!m_sourceId.isEmpty() && frame.cameraId != m_sourceId)) - { - return; - } - if (m_processing) - { - m_pendingFrame = frame; - return; - } - m_processing = true; - emit frameReady(frame); - } - -} diff --git a/ScopeOneCore/src/ToolPlugin.cpp b/ScopeOneCore/src/ToolPlugin.cpp deleted file mode 100644 index f4702d6..0000000 --- a/ScopeOneCore/src/ToolPlugin.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "scopeone/ToolPlugin.h" -#include "scopeone/ScopeOneCore.h" - -namespace scopeone::ui -{ - using scopeone::core::ScopeOneCore; - - ScopeOneToolCore::ScopeOneToolCore(ScopeOneCore& core, QObject* parent) - : QObject(parent), m_core(core) - { - connect(&m_core, &ScopeOneCore::previewRawFrameReady, - this, &ScopeOneToolCore::previewRawFrameReady); - connect(&m_core, &ScopeOneCore::staticFramePublished, - this, &ScopeOneToolCore::staticFramePublished); - connect(&m_core, &ScopeOneCore::signalTimeSeriesReady, - this, &ScopeOneToolCore::signalTimeSeriesReady); - connect(&m_core, &ScopeOneCore::signalSourceStateChanged, - this, &ScopeOneToolCore::signalSourceStateChanged); - connect(&m_core, &ScopeOneCore::signalSourceError, - this, &ScopeOneToolCore::signalSourceError); - connect(&m_core, &ScopeOneCore::scanImageSessionReady, - this, &ScopeOneToolCore::scanImageSessionReady); - connect(&m_core, &ScopeOneCore::daqStateChanged, - this, &ScopeOneToolCore::daqStateChanged); - connect(&m_core, &ScopeOneCore::daqError, - this, &ScopeOneToolCore::daqError); - connect(&m_core, &ScopeOneCore::daqInputDataReady, - this, &ScopeOneToolCore::daqInputDataReady); - } - - QString ScopeOneToolCore::rawLayerKey(const QString& cameraId) - { - return ScopeOneCore::rawLayerKey(cameraId); - } - - QString ScopeOneToolCore::toolLayerKey(const QString& sourceId) - { - return ScopeOneCore::toolLayerKey(sourceId); - } - - QString ScopeOneToolCore::staticLayerKey(const QString& sourceId) - { - return ScopeOneCore::staticLayerKey(sourceId); - } - - QString ScopeOneToolCore::sourceIdFromLayerKey(const QString& layerKey) - { - return ScopeOneCore::sourceIdFromLayerKey(layerKey); - } - - QStringList ScopeOneToolCore::cameraIds() const - { - return m_core.cameraIds(); - } - - scopeone::core::ImageFrame ScopeOneToolCore::graphFrame(const QString& layerKey) const - { - return m_core.graphFrame(layerKey); - } - - scopeone::core::ImageSceneModel* ScopeOneToolCore::imageSceneModel() const - { - return m_core.imageSceneModel(); - } - - scopeone::core::ImageFrame ScopeOneToolCore::publishToolStreamFrame(const QString& sourceId, - const scopeone::core::ImageFrame& frame, - const QString& displayName) - { - return m_core.publishToolStreamFrame(sourceId, frame, displayName); - } - - QList ScopeOneToolCore::signalSources() const - { - return m_core.signalSources(); - } - - bool ScopeOneToolCore::startSignalTrace(const scopeone::core::SignalAcquisitionConfig& config, - QString* errorMessage) - { - return m_core.startSignalTrace(config, errorMessage); - } - - void ScopeOneToolCore::stopSignalTrace(const QString& sourceId) - { - m_core.stopSignalTrace(sourceId); - } - - scopeone::core::SignalSourceState ScopeOneToolCore::signalSourceState(const QString& sourceId) const - { - return m_core.signalSourceState(sourceId); - } - - QString ScopeOneToolCore::signalSourceStateMessage(const QString& sourceId) const - { - return m_core.signalSourceStateMessage(sourceId); - } - - QList ScopeOneToolCore::daqDevices() const - { - return m_core.daqDevices(); - } - - bool ScopeOneToolCore::startDaqSession(const scopeone::core::DaqSessionConfig& config, - QString* errorMessage) - { - return m_core.startDaqSession(config, errorMessage); - } - - void ScopeOneToolCore::stopDaqSession(const QString& deviceId) - { - m_core.stopDaqSession(deviceId); - } - - QString ScopeOneToolCore::daqStateMessage(const QString& deviceId) const - { - return m_core.daqStateMessage(deviceId); - } -} diff --git a/ScopeOneCore/src/ToolTask.cpp b/ScopeOneCore/src/ToolTask.cpp deleted file mode 100644 index 30085f7..0000000 --- a/ScopeOneCore/src/ToolTask.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "scopeone/ToolTask.h" - -#include -#include -#include -#include - -namespace scopeone::ui -{ - struct ScopeOneToolTask::Impl - { - Work work; - std::atomic_bool cancelRequested{false}; - bool running{false}; - QString error; - QFutureWatcher watcher; - }; - - ScopeOneToolTask::ScopeOneToolTask(Work work, QObject* parent) - : QObject(parent), m_impl(std::make_unique()) - { - m_impl->work = std::move(work); - connect(&m_impl->watcher, &QFutureWatcher::finished, this, [this]() - { - m_impl->running = false; - if (!m_impl->error.isEmpty()) - { - emit failed(m_impl->error); - return; - } - if (m_impl->cancelRequested) - { - emit canceled(); - } - else - { - emit finished(); - } - }); - } - - ScopeOneToolTask::~ScopeOneToolTask() - { - cancel(); - m_impl->watcher.waitForFinished(); - } - - void ScopeOneToolTask::start() - { - if (!m_impl->work || m_impl->running) - { - return; - } - m_impl->running = true; - m_impl->cancelRequested = false; - m_impl->error.clear(); - const Work work = m_impl->work; - m_impl->watcher.setFuture(QtConcurrent::run([this, work]() - { - try - { - work(m_impl->cancelRequested, [this](int percent) - { - emit progressChanged(qBound(0, percent, 100)); - }); - } - catch (const std::exception& exception) - { - m_impl->error = QString::fromLocal8Bit(exception.what()); - } - catch (...) - { - m_impl->error = QStringLiteral("Tool task failed"); - } - })); - } - - void ScopeOneToolTask::cancel() - { - m_impl->cancelRequested = true; - } - -} diff --git a/ScopeOneCuda/src/CudaFrequencyFilterModule.cpp b/ScopeOneCuda/src/CudaFrequencyFilterModule.cpp index 585995a..cef1dc1 100644 --- a/ScopeOneCuda/src/CudaFrequencyFilterModule.cpp +++ b/ScopeOneCuda/src/CudaFrequencyFilterModule.cpp @@ -5,7 +5,7 @@ #include #include -namespace scopeone::cuda_plugin +namespace scopeone::plugins::cuda { CudaFrequencyFilterModule::CudaFrequencyFilterModule() : CudaRealImageModule(scopeone::cuda::GpuMemoryLayout::Contiguous) diff --git a/ScopeOneCuda/src/CudaFrequencyFilterModule.h b/ScopeOneCuda/src/CudaFrequencyFilterModule.h index a04c419..58ebb73 100644 --- a/ScopeOneCuda/src/CudaFrequencyFilterModule.h +++ b/ScopeOneCuda/src/CudaFrequencyFilterModule.h @@ -5,7 +5,7 @@ #include -namespace scopeone::cuda_plugin +namespace scopeone::plugins::cuda { class CudaFrequencyFilterModule final : public scopeone::cuda::CudaRealImageModule { diff --git a/ScopeOneCuda/src/CudaGaussianBlurModule.cpp b/ScopeOneCuda/src/CudaGaussianBlurModule.cpp index f6dd972..4645bb8 100644 --- a/ScopeOneCuda/src/CudaGaussianBlurModule.cpp +++ b/ScopeOneCuda/src/CudaGaussianBlurModule.cpp @@ -2,7 +2,7 @@ #include "scopeone/cuda/CudaKernelLaunch.h" -namespace scopeone::cuda_plugin +namespace scopeone::plugins::cuda { CudaGaussianBlurModule::CudaGaussianBlurModule() : CudaRealImageModule(scopeone::cuda::GpuMemoryLayout::Pitched2D) diff --git a/ScopeOneCuda/src/CudaGaussianBlurModule.h b/ScopeOneCuda/src/CudaGaussianBlurModule.h index d016df0..896da52 100644 --- a/ScopeOneCuda/src/CudaGaussianBlurModule.h +++ b/ScopeOneCuda/src/CudaGaussianBlurModule.h @@ -2,7 +2,7 @@ #include "scopeone/cuda/CudaRealImageModule.h" -namespace scopeone::cuda_plugin +namespace scopeone::plugins::cuda { class CudaGaussianBlurModule final : public scopeone::cuda::CudaRealImageModule { diff --git a/ScopeOneCuda/src/CudaProcessingRegistration.cpp b/ScopeOneCuda/src/CudaProcessingRegistration.cpp index 7a38a8e..602a9c4 100644 --- a/ScopeOneCuda/src/CudaProcessingRegistration.cpp +++ b/ScopeOneCuda/src/CudaProcessingRegistration.cpp @@ -5,7 +5,7 @@ #include -namespace scopeone::cuda_plugin +namespace scopeone::plugins::cuda { class CudaProcessingPlugin final : public QObject, public core::ProcessingPlugin diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 1f1a8c4..7ce89e3 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -22,7 +22,6 @@ else () endif () add_subdirectory(hardware) -add_subdirectory(tools) if (SCOPEONE_ENABLE_INFERENCE) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../ScopeOneInference ScopeOneInference) endif () @@ -37,4 +36,3 @@ if (SCOPEONE_CUDA_NVCC_EXECUTABLE) find_package(CUDAToolkit REQUIRED) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../ScopeOneCuda ScopeOneCuda) endif () -add_subdirectory(processing) diff --git a/plugins/README.md b/plugins/README.md index 4572c8c..31459db 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -1,37 +1,42 @@ -# Writing ScopeOne plugins +# Extending ScopeOne -ScopeOne loads four kinds of extensions: +ScopeOne has two public extension points. Both run outside the ScopeOne process, so a crash in an +extension does not take ScopeOne down. -| Kind | Contract | Runs in | Example | +| Extension | You write | Runs in | Example | |---|---|---|---| -| Processing | `ProcessingPlugin` and `ProcessingModule` in `scopeone/ProcessingPlugin.h` | ScopeOne | `processing/ExampleProcessing` | -| Python processing | a decorated Python function, see below | its own Python process | `processing/PythonProcessing/examples` | -| Tool | `ScopeOneToolPlugin` in `scopeone/ToolPlugin.h` | ScopeOne, as a window | `tools/ExampleTool` | -| Hardware | `DriverHostProviderPlugin`, `DaqDevicePlugin`, `SignalSourcePlugin` | `ScopeOne_DriverHost`, one process per device | `hardware/ExampleHardware` | +| Hardware plugin | a `DriverHostProviderPlugin` in C++ against `scopeone::HardwarePluginSDK` | `ScopeOne_DriverHost`, one process per device | `hardware/ExampleCamera` | +| Python processing module | a decorated Python function | its own Python process | `ScopeOneCore/python/scopeone/examples/processing_offset.py` | -A processing module declares its parameters in a `ProcessingModuleDescriptor`. The pipeline -panel, the Local API and MCP build their controls from it, so a module needs no UI code. +The CUDA and ONNX processing modules in `ScopeOneCuda` and `ScopeOneInference` are built-in plugins, +built and released with ScopeOne, because they link CUDA and ONNX Runtime. Their interface, +`scopeone/ProcessingPlugin.h`, changes with ScopeOne and is not meant for code outside this +repository. Everything else, including the tool windows and the DAQ and signal source devices, is +part of ScopeOne itself. -A tool gets a `ScopeOneToolContext`. Its `core()` is a `ScopeOneToolCore`: the layers and their -images, signal sources and DAQ devices. Tools use only this part of the Core. +## Hardware plugins -## Building a C++ plugin +A hardware plugin implements `DriverHostProviderPlugin` and returns a `HardwareProvider` with the +capabilities its devices have (camera, stage, shutter, state, configuration). The headers it may +use are the ones `scopeone/HardwarePluginSDK.h` includes. ScopeOne starts one `ScopeOne_DriverHost` +for each device, the host loads the plugin library, and from then on ScopeOne only exchanges +messages and frames with the host. -Build and install ScopeOneCore first (`scripts/build-unix.sh` or the Windows script). A plugin -is then a separate CMake project: +Build and install ScopeOneCore first (`scripts/build-unix.sh` or `scripts\build.ps1`). A hardware +plugin is then a separate CMake project: ```cmake cmake_minimum_required(VERSION 3.23) -project(MyPlugin LANGUAGES CXX) +project(MyCamera LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_AUTOMOC ON) find_package(Qt6 REQUIRED COMPONENTS Core) find_package(ScopeOneCore CONFIG REQUIRED) -add_library(MyPlugin MODULE MyPlugin.cpp plugin.json) -target_link_libraries(MyPlugin PRIVATE scopeone::PluginSDK Qt6::Core) -scopeone_add_plugin(MyPlugin processing) # processing, tools or hardware +add_library(MyCamera MODULE MyCamera.cpp plugin.json) +target_link_libraries(MyCamera PRIVATE scopeone::HardwarePluginSDK Qt6::Core) +scopeone_add_plugin(MyCamera hardware) ``` Point `CMAKE_PREFIX_PATH` at `ScopeOneCore/install` and at the same Qt that ScopeOne uses @@ -41,33 +46,32 @@ Point `CMAKE_PREFIX_PATH` at `ScopeOneCore/install` and at the same Qt that Scop ```json { - "id": "my.plugin", - "name": "My Plugin", + "id": "my.camera", + "name": "My Camera", "version": "1.0.0", - "scopeOneApi": 2, - "kind": "processing" + "scopeOneApi": 1, + "kind": "hardware", + "providerId": "my.camera", + "autoLoad": false } ``` -`scopeOneApi` is the contract version of the kind (`ScopeOne*PluginApiVersion` in -`scopeone/PluginManifest.h`). `scopeone_add_plugin` writes `scopeone_plugin.json` from it with -the Core version added, and the plugin names that file: +`scopeOneApi` is `ScopeOneHardwarePluginApiVersion` in `scopeone/PluginManifest.h`. +`scopeone_add_plugin` writes `scopeone_plugin.json` from it with the Core version added, and the +plugin names that file: ```cpp -Q_PLUGIN_METADATA(IID ScopeOneProcessingPlugin_iid FILE "scopeone_plugin.json") +Q_PLUGIN_METADATA(IID ScopeOneDriverHostProviderPlugin_iid FILE "scopeone_plugin.json") ``` -Plugins share C++ types with ScopeOne, so they must be built with the same compiler, the same -Qt and the same Core release (`MAJOR.MINOR` of `VERSION`). ScopeOne refuses a plugin built -against another release and names the release it needs. Change `VERSION` when a public header -of the SDK changes. +The plugin library is loaded by `ScopeOne_DriverHost`, which links the same Core and Qt as +ScopeOne, so a hardware plugin is built with the same compiler, Qt and Core release +(`MAJOR.MINOR` of `VERSION`). ScopeOne refuses a plugin built against another release and names +the release it needs. -## Installing - -Copy the built library into `plugins//` beside the ScopeOne executable, or install it -with **Tools > Plugin Manager**, which puts it in the user's application data directory. -Hardware plugins are enabled and configured in the Plugin Manager and take effect after a -restart. +Copy the built library into `plugins/hardware/` beside the ScopeOne executable, or install it with +**Tools > Plugin Manager**, which puts it in the user's application data directory. Hardware +plugins are enabled and configured in the Plugin Manager and take effect after a restart. ## Python processing modules @@ -85,7 +89,7 @@ The function gets a 2D `uint8` or `uint16` array and the parameters by name, and the frame, 12 for a 12-bit camera in a `uint16` array, so `bits` is not a parameter key. Put the script in `plugins/processing/python/` beside ScopeOne or in the same folder of the user's plugin directory. The modules appear in the pipeline after a restart. ScopeOne ships no script -there; `processing/PythonProcessing/examples/offset.py` is one to copy. +there; `ScopeOneCore/python/scopeone/examples/processing_offset.py` is one to copy. Each module in a pipeline runs in its own Python process (`python -m scopeone.processing serve script.py`). That interpreter needs numpy and the `scopeone` package @@ -93,3 +97,10 @@ serve script.py`). That interpreter needs numpy and the `scopeone` package Interpreter**, or with `SCOPEONE_PYTHON`, which takes precedence; otherwise ScopeOne runs `python3` (`python` on Windows). Output printed by a module goes to the log, and an exception in it shows as the error of that pipeline step. + +ScopeOne first runs `python -m scopeone.processing describe script.py`, which prints one JSON +line with the modules of the script and their parameters. While the pipeline runs, each frame is +one request on stdin: a JSON line with `module`, `parameters`, `width`, `height`, `stride`, +`format` (`mono8` or `mono16`), `bits` and `bytes`, followed by that many pixel bytes. The reply +on stdout has the same form with `width`, `height`, `format` and `bytes`, or a JSON line with +`error`. diff --git a/plugins/hardware/CMakeLists.txt b/plugins/hardware/CMakeLists.txt index b050fd7..62317d1 100644 --- a/plugins/hardware/CMakeLists.txt +++ b/plugins/hardware/CMakeLists.txt @@ -1,4 +1 @@ -add_subdirectory(NIDaqmx) -add_subdirectory(PtuFile) -add_subdirectory(ExampleHardware) -add_subdirectory(SimulatedPmt) +add_subdirectory(ExampleCamera) diff --git a/plugins/hardware/ExampleCamera/CMakeLists.txt b/plugins/hardware/ExampleCamera/CMakeLists.txt new file mode 100644 index 0000000..8c28e7c --- /dev/null +++ b/plugins/hardware/ExampleCamera/CMakeLists.txt @@ -0,0 +1,8 @@ +add_library(ScopeOneExampleCamera MODULE + ExampleCameraPlugin.cpp + TestPatterns.cpp + TestPatterns.h + plugin.json +) +target_link_libraries(ScopeOneExampleCamera PRIVATE scopeone::HardwarePluginSDK Qt6::Core) +scopeone_add_plugin(ScopeOneExampleCamera hardware) diff --git a/plugins/hardware/ExampleCamera/ExampleCameraPlugin.cpp b/plugins/hardware/ExampleCamera/ExampleCameraPlugin.cpp new file mode 100644 index 0000000..78cb03d --- /dev/null +++ b/plugins/hardware/ExampleCamera/ExampleCameraPlugin.cpp @@ -0,0 +1,604 @@ +// A hardware plugin written the way a camera maker would write one. It uses only the headers of +// scopeone::HardwarePluginSDK. Where a real plugin calls its vendor SDK, this one renders the test +// patterns of TestPatterns.h on an acquisition thread, as a vendor SDK delivers frames from a +// thread of its own. +#include "TestPatterns.h" + +#include "scopeone/HardwarePluginSDK.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace example_camera +{ + using scopeone::core::CameraProvider; + using scopeone::core::HardwareDeviceDescriptor; + using scopeone::core::HardwareDeviceKind; + using scopeone::core::HardwareDeviceState; + using scopeone::core::HardwareProvider; + using scopeone::core::HardwareProviderDescriptor; + using scopeone::core::ImageFrame; + using scopeone::core::ImagePixelFormat; + + constexpr int kBitsPerSample = 12; + constexpr double kMinimumExposureMs = 1.0; + constexpr double kMaximumExposureMs = 1000.0; + + // One camera. It stands in for the handle a vendor SDK gives out for each camera it finds. + class Camera + { + public: + Camera(QString id, std::shared_ptr patterns, int sensorWidth, int sensorHeight) + : m_id(std::move(id)) + , m_patterns(std::move(patterns)) + , m_sensorWidth(sensorWidth) + , m_sensorHeight(sensorHeight) + , m_roiWidth(sensorWidth) + , m_roiHeight(sensorHeight) + { + } + + ~Camera() + { + stop(); + } + + const QString& id() const { return m_id; } + + void setFrameSink(CameraProvider::FrameSink sink) + { + QMutexLocker locker(&m_mutex); + m_sink = std::move(sink); + } + + bool start() + { + if (m_running.exchange(true)) + { + return true; + } + m_thread = std::thread([this]() + { + while (m_running.load()) + { + const ImageFrame frame = expose(); + CameraProvider::FrameSink sink; + { + QMutexLocker locker(&m_mutex); + sink = m_sink; + } + if (sink && m_running.load()) + { + sink(frame); + } + } + }); + return true; + } + + bool stop() + { + m_running.store(false); + if (m_thread.joinable()) + { + m_thread.join(); + } + return true; + } + + bool isRunning() const { return m_running.load(); } + + double exposureMs() const + { + QMutexLocker locker(&m_mutex); + return m_exposureMs; + } + + bool setExposureMs(double exposureMs) + { + if (exposureMs < kMinimumExposureMs || exposureMs > kMaximumExposureMs) + { + return false; + } + QMutexLocker locker(&m_mutex); + m_exposureMs = exposureMs; + return true; + } + + bool setRoi(int x, int y, int width, int height) + { + if (x < 0 || y < 0 || width <= 0 || height <= 0 + || x + width > m_sensorWidth || y + height > m_sensorHeight) + { + return false; + } + QMutexLocker locker(&m_mutex); + m_roiX = x; + m_roiY = y; + m_roiWidth = width; + m_roiHeight = height; + return true; + } + + void roi(int& x, int& y, int& width, int& height) const + { + QMutexLocker locker(&m_mutex); + x = m_roiX; + y = m_roiY; + width = m_roiWidth; + height = m_roiHeight; + } + + bool clearRoi() + { + return setRoi(0, 0, m_sensorWidth, m_sensorHeight); + } + + int gain() const + { + QMutexLocker locker(&m_mutex); + return m_gain; + } + + bool setGain(int gain) + { + if (gain < 1 || gain > 16) + { + return false; + } + QMutexLocker locker(&m_mutex); + m_gain = gain; + return true; + } + + TestPattern pattern() const + { + QMutexLocker locker(&m_mutex); + return m_pattern; + } + + void setPattern(TestPattern pattern) + { + QMutexLocker locker(&m_mutex); + m_pattern = pattern; + } + + // Waits one exposure and reads out the region of interest + ImageFrame expose() + { + double exposureMs = 0.0; + int roiX = 0; + int roiY = 0; + int roiWidth = 0; + int roiHeight = 0; + int gain = 1; + TestPattern pattern = TestPattern::MovingSpot; + { + QMutexLocker locker(&m_mutex); + pattern = m_pattern; + exposureMs = m_exposureMs; + roiX = m_roiX; + roiY = m_roiY; + roiWidth = m_roiWidth; + roiHeight = m_roiHeight; + gain = m_gain; + } + std::this_thread::sleep_for(std::chrono::duration(exposureMs)); + + // A single capture checked only that the acquisition was not running, so a preview started + // meanwhile read out on its thread at the same time and both wrote the buffers below + QMutexLocker readout(&m_readoutMutex); + ImageFrame frame; + frame.cameraId = m_id; + frame.width = roiWidth; + frame.height = roiHeight; + frame.stride = roiWidth * 2; + frame.bitsPerSample = kBitsPerSample; + frame.pixelFormat = ImagePixelFormat::Mono16; + frame.frameIndex = m_frameIndex++; + frame.timestampNs = static_cast(std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count()); + frame.sourceRoiX = roiX; + frame.sourceRoiY = roiY; + frame.sourceRoiWidth = roiWidth; + frame.sourceRoiHeight = roiHeight; + frame.bytes.resize(static_cast(frame.stride) * roiHeight); + + // The signal grows with the exposure and the gain, 16 counts of the 12-bit sensor for each level of + // the pattern at the default exposure, and the shot noise of the sensor is drawn around it + m_patterns->render(pattern, frame.frameIndex, roiX, roiY, roiWidth, roiHeight, m_intensities); + const double scale = 16.0 * gain * exposureMs / 10.0; + const double maximum = static_cast((1 << kBitsPerSample) - 1); + auto* pixels = reinterpret_cast(frame.bytes.data()); + for (std::size_t index = 0; index < m_intensities.size(); ++index) + { + const double counts = scale * m_intensities[index]; + std::normal_distribution noise(counts, std::sqrt(std::max(counts, 1.0))); + pixels[index] = static_cast(std::clamp(noise(m_random), 0.0, maximum)); + } + return frame; + } + + private: + const QString m_id; + const std::shared_ptr m_patterns; + const int m_sensorWidth; + const int m_sensorHeight; + mutable QMutex m_mutex; + QMutex m_readoutMutex; + CameraProvider::FrameSink m_sink; + double m_exposureMs{10.0}; + int m_gain{1}; + TestPattern m_pattern{TestPattern::MovingSpot}; + int m_roiX{0}; + int m_roiY{0}; + int m_roiWidth; + int m_roiHeight; + quint64 m_frameIndex{0}; + std::vector m_intensities; + std::minstd_rand m_random{12345}; + std::atomic_bool m_running{false}; + std::thread m_thread; + }; + + // One provider that offers several cameras, as the plugin of a camera maker does when its SDK + // finds several. ScopeOne addresses each camera by its id. + class MultiCameraProvider final : public HardwareProvider, public CameraProvider + { + public: + MultiCameraProvider(const QString& providerId, int cameraCount, int width, int height) + : m_providerId(providerId) + { + const auto patterns = std::make_shared(width, height); + for (int index = 0; index < cameraCount; ++index) + { + const QString id = cameraCount == 1 + ? QStringLiteral("camera.example") + : QStringLiteral("camera.example.%1").arg(index + 1); + m_cameras.push_back(std::make_unique(id, patterns, width, height)); + } + } + + HardwareProviderDescriptor descriptor() const override + { + return {m_providerId, QStringLiteral("Example Camera"), QStringLiteral("1")}; + } + + QList devices() const override + { + QList result; + for (size_t index = 0; index < m_cameras.size(); ++index) + { + HardwareDeviceDescriptor device; + device.logicalId = m_cameras[index]->id(); + device.providerId = m_providerId; + device.providerDeviceId = m_cameras[index]->id(); + device.hardwareId = QStringLiteral("EX-%1").arg(1000 + index); + device.name = QStringLiteral("Example Camera %1").arg(index + 1); + device.kind = HardwareDeviceKind::Camera; + device.state = HardwareDeviceState::Initialized; + result.append(device); + } + return result; + } + + // The frames carry the id of the camera that made them, so one sink serves all of them + void setFrameSink(FrameSink sink) override + { + for (auto& camera : m_cameras) + { + camera->setFrameSink(sink); + } + } + + void setPreviewStateSink(PreviewStateSink sink) override + { + QMutexLocker locker(&m_mutex); + m_previewStateSink = std::move(sink); + } + + bool startPreview() override + { + for (auto& camera : m_cameras) + { + camera->start(); + } + reportPreviewState(); + return true; + } + + bool stopPreview() override + { + for (auto& camera : m_cameras) + { + camera->stop(); + } + reportPreviewState(); + return true; + } + + bool startPreviewFor(const QString& cameraId) override + { + Camera* camera = find(cameraId); + if (!camera) + { + return false; + } + camera->start(); + reportPreviewState(); + return true; + } + + bool stopPreviewFor(const QString& cameraId) override + { + Camera* camera = find(cameraId); + if (!camera) + { + return false; + } + camera->stop(); + reportPreviewState(); + return true; + } + + bool isPreviewRunning(const QString& cameraId) const override + { + const Camera* camera = find(cameraId); + return camera && camera->isRunning(); + } + + // An id that is not a camera, such as "All", reads the first camera + bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const override + { + const Camera* camera = find(cameraIdOrAll); + if (!camera && !m_cameras.empty()) + { + camera = m_cameras.front().get(); + } + if (!camera) + { + return false; + } + exposureMs = camera->exposureMs(); + return true; + } + + // An id that is not a camera sets the exposure of every camera + bool setExposure(const QString& cameraIdOrAll, double exposureMs) override + { + if (Camera* camera = find(cameraIdOrAll)) + { + return camera->setExposureMs(exposureMs); + } + bool all = !m_cameras.empty(); + for (auto& camera : m_cameras) + { + all = camera->setExposureMs(exposureMs) && all; + } + return all; + } + + bool setROI(const QString& cameraId, int x, int y, int width, int height) override + { + Camera* camera = find(cameraId); + return camera && camera->setRoi(x, y, width, height); + } + + bool clearROI(const QString& cameraId) override + { + Camera* camera = find(cameraId); + return camera && camera->clearRoi(); + } + + bool getROI(const QString& cameraId, int& x, int& y, int& width, int& height) override + { + Camera* camera = find(cameraId); + if (!camera) + { + return false; + } + camera->roi(x, y, width, height); + return true; + } + + // A software trigger: one exposure, read out while the caller waits + bool captureEventFrame(const QString& cameraId, ImageFrame& frame, int timeoutMs) override + { + Camera* camera = find(cameraId); + if (!camera || camera->isRunning() || camera->exposureMs() > timeoutMs) + { + return false; + } + frame = camera->expose(); + return true; + } + + // Each camera has a gain and a test pattern the user can set, and a sensor temperature it reports + QStringList listProperties(const QString& deviceId) override + { + return find(deviceId) ? QStringList{QStringLiteral("Gain"), QStringLiteral("Pattern"), + QStringLiteral("SensorTemperature")} + : QStringList{}; + } + + QString getProperty(const QString& deviceId, const QString& name, bool) override + { + Camera* camera = find(deviceId); + if (!camera) + { + return {}; + } + if (name == QStringLiteral("Gain")) + { + return QString::number(camera->gain()); + } + if (name == QStringLiteral("Pattern")) + { + return testPatternName(camera->pattern()); + } + if (name == QStringLiteral("SensorTemperature")) + { + return QStringLiteral("-20.0"); + } + return {}; + } + + bool setProperty(const QString& deviceId, + const QString& name, + const QString& value, + QString* errorMessage) override + { + Camera* camera = find(deviceId); + TestPattern pattern = TestPattern::MovingSpot; + if (camera && name == QStringLiteral("Pattern") && testPatternFromName(value, pattern)) + { + camera->setPattern(pattern); + return true; + } + bool numeric = false; + const int gain = value.toInt(&numeric); + if (camera && name == QStringLiteral("Gain") && numeric && camera->setGain(gain)) + { + return true; + } + if (errorMessage) + { + *errorMessage = !camera ? QStringLiteral("Unknown camera") + : name == QStringLiteral("Gain") ? QStringLiteral("Gain must be an integer from 1 to 16") + : name == QStringLiteral("Pattern") ? QStringLiteral("Unknown pattern %1").arg(value) + : QStringLiteral("%1 cannot be set").arg(name); + } + return false; + } + + QString getPropertyType(const QString& deviceId, const QString& name) override + { + if (!find(deviceId)) + { + return {}; + } + return name == QStringLiteral("Gain") ? QStringLiteral("Integer") + : name == QStringLiteral("Pattern") ? QStringLiteral("String") + : QStringLiteral("Float"); + } + + bool isPropertyReadOnly(const QString&, const QString& name) override + { + return name == QStringLiteral("SensorTemperature"); + } + + bool isPropertyPreInit(const QString&, const QString&) override + { + return false; + } + + QStringList getAllowedPropertyValues(const QString&, const QString& name) override + { + return name == QStringLiteral("Pattern") ? testPatternNames() : QStringList{}; + } + + bool hasPropertyLimits(const QString&, const QString& name) override + { + return name == QStringLiteral("Gain"); + } + + double getPropertyLowerLimit(const QString&, const QString& name) override + { + return name == QStringLiteral("Gain") ? 1.0 : 0.0; + } + + double getPropertyUpperLimit(const QString&, const QString& name) override + { + return name == QStringLiteral("Gain") ? 16.0 : 0.0; + } + + private: + Camera* find(const QString& cameraId) const + { + for (const auto& camera : m_cameras) + { + if (camera->id() == cameraId) + { + return camera.get(); + } + } + return nullptr; + } + + // The provider is previewing while any of its cameras is + void reportPreviewState() + { + const bool running = std::any_of(m_cameras.begin(), m_cameras.end(), + [](const auto& camera) { return camera->isRunning(); }); + PreviewStateSink sink; + { + QMutexLocker locker(&m_mutex); + if (running == m_lastPreviewState) + { + return; + } + m_lastPreviewState = running; + sink = m_previewStateSink; + } + if (sink) + { + sink(running); + } + } + + QString m_providerId; + std::vector> m_cameras; + QMutex m_mutex; + PreviewStateSink m_previewStateSink; + bool m_lastPreviewState{false}; + }; + + class ExampleCameraPlugin final : public QObject, + public scopeone::core::DriverHostProviderPlugin + { + Q_OBJECT + Q_PLUGIN_METADATA(IID ScopeOneDriverHostProviderPlugin_iid FILE "scopeone_plugin.json") + Q_INTERFACES(scopeone::core::DriverHostProviderPlugin) + + public: + QString providerId() const override + { + return QStringLiteral("example.camera"); + } + + // Options: width and height of the sensor, and cameraCount, the number of cameras (2 by default) + scopeone::core::HardwareProviderPtr createProvider(const QJsonObject& options, + QString* errorMessage) override + { + if (errorMessage) + { + errorMessage->clear(); + } + const int width = options.value(QStringLiteral("width")).toInt(512); + const int height = options.value(QStringLiteral("height")).toInt(512); + const int cameraCount = options.value(QStringLiteral("cameraCount")).toInt(2); + if (width <= 0 || height <= 0 || cameraCount < 1 || cameraCount > 8) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("width and height must be positive and cameraCount from 1 to 8"); + } + return {}; + } + return std::make_shared(providerId(), cameraCount, width, height); + } + }; +} + +#include "ExampleCameraPlugin.moc" diff --git a/plugins/hardware/ExampleCamera/TestPatterns.cpp b/plugins/hardware/ExampleCamera/TestPatterns.cpp new file mode 100644 index 0000000..488bf40 --- /dev/null +++ b/plugins/hardware/ExampleCamera/TestPatterns.cpp @@ -0,0 +1,329 @@ +#include "TestPatterns.h" + +#include +#include +#include +#include + +namespace example_camera +{ + namespace + { + constexpr double kPi = 3.14159265358979323846; + constexpr double kTwoPi = 2.0 * kPi; + + struct Bead + { + double x; + double y; + double brightness; + }; + + constexpr std::array kBeads{{ + {0.08, 0.10, 0.91}, {0.19, 0.08, 0.84}, {0.32, 0.12, 0.98}, + {0.46, 0.07, 0.88}, {0.62, 0.11, 0.94}, {0.77, 0.08, 0.82}, + {0.91, 0.13, 0.96}, {0.13, 0.25, 0.87}, {0.29, 0.29, 1.00}, + {0.48, 0.23, 0.83}, {0.69, 0.27, 0.92}, {0.87, 0.31, 0.86}, + {0.08, 0.43, 0.95}, {0.24, 0.47, 0.81}, {0.42, 0.39, 0.90}, + {0.61, 0.45, 0.97}, {0.80, 0.42, 0.85}, {0.94, 0.50, 0.93}, + {0.14, 0.63, 0.89}, {0.34, 0.58, 0.99}, {0.53, 0.66, 0.84}, + {0.72, 0.61, 0.96}, {0.89, 0.68, 0.80}, {0.07, 0.82, 0.92}, + {0.22, 0.88, 0.86}, {0.39, 0.79, 0.95}, {0.58, 0.86, 0.82}, + {0.76, 0.81, 0.98}, {0.92, 0.89, 0.88}, {0.47, 0.94, 0.91}, + {0.30, 0.70, 0.85}, {0.67, 0.75, 0.94} + }}; + + // Pairs closer than the resolution, to judge how well an analysis separates them + constexpr std::array kBeadDoublets{{ + {0.175, 0.176, 1.00}, {0.191, 0.176, 0.92}, + {0.535, 0.335, 0.88}, {0.551, 0.335, 0.96}, + {0.365, 0.515, 0.98}, {0.381, 0.515, 0.86}, + {0.705, 0.925, 0.91}, {0.721, 0.925, 1.00} + }}; + + // A separable Gaussian blur that repeats the edge pixels beyond the border + void blur(std::vector& image, int width, int height, double sigma) + { + const int radius = static_cast(std::ceil(3.0 * sigma)); + std::vector kernel(static_cast(2 * radius + 1)); + float sum = 0.0f; + for (int offset = -radius; offset <= radius; ++offset) + { + const float weight = static_cast(std::exp(-offset * offset / (2.0 * sigma * sigma))); + kernel[static_cast(offset + radius)] = weight; + sum += weight; + } + for (float& weight : kernel) + { + weight /= sum; + } + + std::vector pass(image.size()); + for (int y = 0; y < height; ++y) + { + for (int x = 0; x < width; ++x) + { + float value = 0.0f; + for (int offset = -radius; offset <= radius; ++offset) + { + const int source = std::clamp(x + offset, 0, width - 1); + value += kernel[static_cast(offset + radius)] + * image[static_cast(y) * width + source]; + } + pass[static_cast(y) * width + x] = value; + } + } + for (int y = 0; y < height; ++y) + { + for (int x = 0; x < width; ++x) + { + float value = 0.0f; + for (int offset = -radius; offset <= radius; ++offset) + { + const int source = std::clamp(y + offset, 0, height - 1); + value += kernel[static_cast(offset + radius)] + * pass[static_cast(source) * width + x]; + } + image[static_cast(y) * width + x] = value; + } + } + } + } + + QStringList testPatternNames() + { + return {QStringLiteral("Moving Spot"), + QStringLiteral("Gradient"), + QStringLiteral("Hologram"), + QStringLiteral("Red Blood Cells"), + QStringLiteral("Fluorescent Beads")}; + } + + QString testPatternName(TestPattern pattern) + { + return testPatternNames().at(static_cast(pattern)); + } + + bool testPatternFromName(const QString& name, TestPattern& pattern) + { + const qsizetype index = testPatternNames().indexOf(name); + if (index < 0) + { + return false; + } + pattern = static_cast(index); + return true; + } + + TestPatterns::TestPatterns(int sensorWidth, int sensorHeight) + : m_sensorWidth(sensorWidth) + , m_sensorHeight(sensorHeight) + { + layOutBloodCells(); + layOutBeads(); + } + + void TestPatterns::render(TestPattern pattern, + quint64 frameIndex, + int roiX, + int roiY, + int roiWidth, + int roiHeight, + std::vector& intensities) const + { + intensities.resize(static_cast(roiWidth) * roiHeight); + const double angle = kTwoPi * static_cast(frameIndex % 200) / 200.0; + const double spotX = m_sensorWidth * (0.5 + 0.3 * std::cos(angle)); + const double spotY = m_sensorHeight * (0.5 + 0.3 * std::sin(angle)); + const double spotSigma = 0.04 * std::min(m_sensorWidth, m_sensorHeight); + for (int y = 0; y < roiHeight; ++y) + { + const int sensorY = roiY + y; + for (int x = 0; x < roiWidth; ++x) + { + const int sensorX = roiX + x; + const std::size_t sensorIndex = static_cast(sensorY) * m_sensorWidth + sensorX; + // The +1 order of the hologram carriers lies in the upper half of the spectrum, at (48, -32), where + // the DHM reconstruction looks for it, so that the phase comes out with the right sign + const double carrier = kTwoPi * (48.0 * sensorX / m_sensorWidth - 32.0 * sensorY / m_sensorHeight); + double intensity = 0.0; + switch (pattern) + { + case TestPattern::MovingSpot: + { + // A bright spot circles over a dim gradient + const double dx = sensorX - spotX; + const double dy = sensorY - spotY; + intensity = 10.0 + 20.0 * sensorX / m_sensorWidth + + 200.0 * std::exp(-(dx * dx + dy * dy) / (2.0 * spotSigma * spotSigma)); + break; + } + case TestPattern::Gradient: + intensity = static_cast((sensorX + sensorY + frameIndex) & 0xffu); + break; + case TestPattern::Hologram: + { + // An off axis hologram of two smooth objects whose phase drifts slowly + const double nx = (sensorX - 0.5 * m_sensorWidth) / m_sensorWidth; + const double ny = (sensorY - 0.5 * m_sensorHeight) / m_sensorHeight; + const double amplitude = 0.65 * std::exp(-35.0 * (nx * nx + ny * ny)) + + 0.35 * std::exp(-90.0 * ((nx - 0.18) * (nx - 0.18) + + (ny + 0.12) * (ny + 0.12))); + const double phase = 18.0 * (nx * nx + ny * ny) + 0.015 * static_cast(frameIndex); + intensity = 70.0 + 55.0 * amplitude * amplitude + 120.0 * amplitude * std::cos(carrier + phase); + break; + } + case TestPattern::RedBloodCells: + { + // An off axis hologram of a thin layer of red blood cells. The reference wave is much stronger + // than the light of the cells, as in a real setup, so the fringes have a contrast of about one + // half, and the cells take some of the light, which dims the fringes over them. + constexpr double kReferenceAmplitude = 11.8; + constexpr double kObjectScale = 4.1; + const double object = kObjectScale * 0.8 * m_cellAmplitude[sensorIndex]; + intensity = kReferenceAmplitude * kReferenceAmplitude + object * object + + 2.0 * kReferenceAmplitude * object * std::cos(carrier + m_cellPhase[sensorIndex]); + break; + } + case TestPattern::FluorescentBeads: + intensity = m_beads[sensorIndex]; + break; + } + intensities[static_cast(y) * roiWidth + x] = intensity; + } + } + } + + // Lays out red blood cells that do not touch, each a biconcave disc, and works out the amplitude and the phase + // that a plane wave takes on passing through them. The thickness follows the shape of Evans and Fung, scaled to a + // rim of 2.4 micrometers, and the phase is 2 pi times the difference of the refractive index of the cell and the + // medium (0.06) times the thickness, over the wavelength (0.633 micrometers). + void TestPatterns::layOutBloodCells() + { + const std::size_t count = static_cast(m_sensorWidth) * m_sensorHeight; + m_cellAmplitude.assign(count, 1.0f); + m_cellPhase.assign(count, 0.0f); + + constexpr double kC0 = 0.207161; + constexpr double kC2 = 2.002558; + constexpr double kC4 = -1.122762; + const auto shape = [](double rho) + { + return std::sqrt(std::max(0.0, 1.0 - rho * rho)) * (kC0 + kC2 * rho * rho + kC4 * rho * rho * rho * rho); + }; + double peak = 0.0; + for (int step = 0; step <= 1000; ++step) + { + peak = std::max(peak, shape(step / 1000.0)); + } + constexpr double kPeakThicknessUm = 2.4; + constexpr double kPhasePerMicrometer = kTwoPi * 0.06 / 0.633; + + // The field is about 60 micrometers wide and a cell about 7.8 micrometers across + const double radiusBase = 0.065 * std::min(m_sensorWidth, m_sensorHeight); + struct Cell + { + double x; + double y; + double radius; + double squeeze; + double angle; + }; + std::vector cells; + std::mt19937 layout(20240601u); + std::uniform_real_distribution unit(0.0, 1.0); + for (int attempt = 0; attempt < 4000 && cells.size() < 24; ++attempt) + { + Cell cell; + cell.radius = radiusBase * (0.9 + 0.2 * unit(layout)); + cell.squeeze = 0.85 + 0.15 * unit(layout); + cell.angle = kPi * unit(layout); + cell.x = cell.radius + unit(layout) * (m_sensorWidth - 2.0 * cell.radius); + cell.y = cell.radius + unit(layout) * (m_sensorHeight - 2.0 * cell.radius); + const bool free = std::none_of(cells.begin(), cells.end(), [&](const Cell& other) + { + return std::hypot(cell.x - other.x, cell.y - other.y) <= 1.08 * (cell.radius + other.radius); + }); + if (free) + { + cells.push_back(cell); + } + } + + for (const Cell& cell : cells) + { + const int reach = static_cast(std::ceil(cell.radius * 1.05)); + const int minX = std::max(0, static_cast(cell.x) - reach); + const int maxX = std::min(m_sensorWidth - 1, static_cast(cell.x) + reach); + const int minY = std::max(0, static_cast(cell.y) - reach); + const int maxY = std::min(m_sensorHeight - 1, static_cast(cell.y) + reach); + const double cosine = std::cos(cell.angle); + const double sine = std::sin(cell.angle); + for (int y = minY; y <= maxY; ++y) + { + for (int x = minX; x <= maxX; ++x) + { + const double dx = x + 0.5 - cell.x; + const double dy = y + 0.5 - cell.y; + // The cell is a little flattened along one direction + const double u = (dx * cosine + dy * sine) / cell.radius; + const double v = (-dx * sine + dy * cosine) / (cell.radius * cell.squeeze); + const double rho = std::sqrt(u * u + v * v); + if (rho >= 1.0) + { + continue; + } + const double thicknessUm = kPeakThicknessUm * shape(rho) / peak; + const std::size_t index = static_cast(y) * m_sensorWidth + x; + m_cellPhase[index] = static_cast(kPhasePerMicrometer * thicknessUm); + // Hemoglobin takes light, more where the cell is thick + m_cellAmplitude[index] = static_cast(1.0 - 0.5 * thicknessUm / kPeakThicknessUm); + } + } + } + + // A microscope does not see the sharp edge of a cell: its resolution and the size of a pixel soften it. + // Without this the thickness drops to zero at the rim with an infinite slope, which spreads over the whole + // spectrum and comes back from the reconstruction as ripples around the cells. + const double blurSigma = std::max(1.0, 0.004 * std::min(m_sensorWidth, m_sensorHeight)); + blur(m_cellPhase, m_sensorWidth, m_sensorHeight, blurSigma); + blur(m_cellAmplitude, m_sensorWidth, m_sensorHeight, blurSigma); + } + + // A still field of beads, each a Gaussian point spread function of about 4 pixels FWHM at 256 pixels + void TestPatterns::layOutBeads() + { + m_beads.assign(static_cast(m_sensorWidth) * m_sensorHeight, 3.0f); + const double resolutionScale = std::min(m_sensorWidth, m_sensorHeight) / 256.0; + const double sigma = 4.0 * resolutionScale / 2.354820045; + const int radius = static_cast(std::ceil(4.0 * sigma)); + const auto drawBead = [&](const Bead& bead) + { + const double centerX = bead.x * m_sensorWidth; + const double centerY = bead.y * m_sensorHeight; + const int minX = std::max(0, static_cast(std::floor(centerX)) - radius); + const int maxX = std::min(m_sensorWidth - 1, static_cast(std::floor(centerX)) + radius); + const int minY = std::max(0, static_cast(std::floor(centerY)) - radius); + const int maxY = std::min(m_sensorHeight - 1, static_cast(std::floor(centerY)) + radius); + for (int y = minY; y <= maxY; ++y) + { + for (int x = minX; x <= maxX; ++x) + { + const double dx = x + 0.5 - centerX; + const double dy = y + 0.5 - centerY; + m_beads[static_cast(y) * m_sensorWidth + x] + += static_cast(245.0 * bead.brightness * std::exp(-(dx * dx + dy * dy) + / (2.0 * sigma * sigma))); + } + } + }; + for (const Bead& bead : kBeads) + { + drawBead(bead); + } + for (const Bead& bead : kBeadDoublets) + { + drawBead(bead); + } + } +} diff --git a/plugins/hardware/ExampleCamera/TestPatterns.h b/plugins/hardware/ExampleCamera/TestPatterns.h new file mode 100644 index 0000000..4d8601f --- /dev/null +++ b/plugins/hardware/ExampleCamera/TestPatterns.h @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include + +#include + +namespace example_camera +{ + enum class TestPattern + { + MovingSpot, + Gradient, + Hologram, + RedBloodCells, + FluorescentBeads + }; + + QStringList testPatternNames(); + QString testPatternName(TestPattern pattern); + bool testPatternFromName(const QString& name, TestPattern& pattern); + + // The scenes the example camera can look at. A real plugin reads its images from the vendor SDK + // instead. The scenes are laid out once for the size of the sensor and read from any thread. + class TestPatterns + { + public: + TestPatterns(int sensorWidth, int sensorHeight); + + // The light on each pixel of the region, on a scale where 255 fills an 8-bit camera at the + // default exposure. Moving scenes advance with the frame index. + void render(TestPattern pattern, + quint64 frameIndex, + int roiX, + int roiY, + int roiWidth, + int roiHeight, + std::vector& intensities) const; + + private: + void layOutBloodCells(); + void layOutBeads(); + + int m_sensorWidth; + int m_sensorHeight; + std::vector m_cellAmplitude; + std::vector m_cellPhase; + std::vector m_beads; + }; +} diff --git a/plugins/hardware/ExampleCamera/plugin.json b/plugins/hardware/ExampleCamera/plugin.json new file mode 100644 index 0000000..3ecc972 --- /dev/null +++ b/plugins/hardware/ExampleCamera/plugin.json @@ -0,0 +1,9 @@ +{ + "id": "example.camera", + "name": "Example Camera", + "version": "1.0.0", + "scopeOneApi": 1, + "kind": "hardware", + "providerId": "example.camera", + "autoLoad": false +} diff --git a/plugins/hardware/ExampleHardware/CMakeLists.txt b/plugins/hardware/ExampleHardware/CMakeLists.txt deleted file mode 100644 index 7b56deb..0000000 --- a/plugins/hardware/ExampleHardware/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_library(ScopeOneExampleHardware MODULE - ExampleHardwarePlugin.cpp - plugin.json -) -target_link_libraries(ScopeOneExampleHardware PRIVATE scopeone::PluginSDK Qt6::Core) -scopeone_add_plugin(ScopeOneExampleHardware hardware) diff --git a/plugins/hardware/ExampleHardware/ExampleHardwarePlugin.cpp b/plugins/hardware/ExampleHardware/ExampleHardwarePlugin.cpp deleted file mode 100644 index 57060b8..0000000 --- a/plugins/hardware/ExampleHardware/ExampleHardwarePlugin.cpp +++ /dev/null @@ -1,316 +0,0 @@ -#include "scopeone/CameraProvider.h" -#include "scopeone/DriverHostProviderPlugin.h" -#include "scopeone/HardwareProvider.h" -#include "scopeone/SimulatorProvider.h" - -#include -#include -#include - -#include -#include - -namespace -{ - using scopeone::core::CameraProvider; - using scopeone::core::HardwareDeviceDescriptor; - using scopeone::core::HardwareProvider; - using scopeone::core::HardwareProviderDescriptor; - using scopeone::core::ImageFrame; - using scopeone::core::SimulatorProvider; - - // One provider that offers several cameras, as the driver of a real camera maker does when it finds - // more than one camera. Every camera has its own id, exposure, region of interest and preview, and - // the provider tells them apart by that id. Each camera here is a simulated one. - class MultiCameraProvider final : public HardwareProvider, public CameraProvider - { - public: - MultiCameraProvider(const QString& providerId, int cameraCount, int width, int height) - : m_providerId(providerId) - { - for (int index = 0; index < cameraCount; ++index) - { - // A single camera keeps the plain id that this example always had - const QString id = cameraCount == 1 - ? QStringLiteral("camera.example") - : QStringLiteral("camera.example.%1").arg(index + 1); - m_ids.append(id); - m_cameras.push_back(std::make_unique(id, width, height, providerId)); - } - } - - HardwareProviderDescriptor descriptor() const override - { - return {m_providerId, QStringLiteral("Example Hardware"), QStringLiteral("1")}; - } - - QList devices() const override - { - QList result; - for (size_t index = 0; index < m_cameras.size(); ++index) - { - for (HardwareDeviceDescriptor device : m_cameras[index]->devices()) - { - device.name = QStringLiteral("Example Camera %1").arg(index + 1); - result.append(device); - } - } - return result; - } - - void setFrameSink(FrameSink sink) override - { - // The frames carry the id of the camera that made them, so one sink serves all of them - for (auto& camera : m_cameras) - { - camera->setFrameSink(sink); - } - } - - // The provider is previewing while any of its cameras is - void setPreviewStateSink(PreviewStateSink sink) override - { - { - QMutexLocker locker(&m_mutex); - m_previewStateSink = std::move(sink); - } - for (auto& camera : m_cameras) - { - camera->setPreviewStateSink([this](bool) { reportPreviewState(); }); - } - } - - bool startPreview() override - { - bool all = true; - for (auto& camera : m_cameras) - { - all = camera->startPreview() && all; - } - return all; - } - - bool stopPreview() override - { - bool all = true; - for (auto& camera : m_cameras) - { - all = camera->stopPreview() && all; - } - return all; - } - - bool startPreviewFor(const QString& cameraId) override - { - SimulatorProvider* camera = owner(cameraId); - return camera && camera->startPreviewFor(cameraId); - } - - bool stopPreviewFor(const QString& cameraId) override - { - SimulatorProvider* camera = owner(cameraId); - return camera && camera->stopPreviewFor(cameraId); - } - - bool isPreviewRunning(const QString& cameraId) const override - { - const SimulatorProvider* camera = owner(cameraId); - return camera && camera->isPreviewRunning(cameraId); - } - - bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const override - { - if (const SimulatorProvider* camera = owner(cameraIdOrAll)) - { - return camera->getExposure(cameraIdOrAll, exposureMs); - } - return !m_cameras.empty() && m_cameras.front()->getExposure(m_ids.first(), exposureMs); - } - - // "All" sets the exposure of every camera - bool setExposure(const QString& cameraIdOrAll, double exposureMs) override - { - if (SimulatorProvider* camera = owner(cameraIdOrAll)) - { - return camera->setExposure(cameraIdOrAll, exposureMs); - } - bool any = false; - for (size_t index = 0; index < m_cameras.size(); ++index) - { - any = m_cameras[index]->setExposure(m_ids.at(static_cast(index)), exposureMs) || any; - } - return any; - } - - bool setROI(const QString& cameraId, int x, int y, int width, int height) override - { - SimulatorProvider* camera = owner(cameraId); - return camera && camera->setROI(cameraId, x, y, width, height); - } - - bool clearROI(const QString& cameraId) override - { - SimulatorProvider* camera = owner(cameraId); - return camera && camera->clearROI(cameraId); - } - - bool getROI(const QString& cameraId, int& x, int& y, int& width, int& height) override - { - SimulatorProvider* camera = owner(cameraId); - return camera && camera->getROI(cameraId, x, y, width, height); - } - - bool captureEventFrame(const QString& cameraId, ImageFrame& frame, int timeoutMs) override - { - SimulatorProvider* camera = owner(cameraId); - return camera && camera->captureEventFrame(cameraId, frame, timeoutMs); - } - - QStringList listProperties(const QString& deviceId) override - { - SimulatorProvider* camera = owner(deviceId); - return camera ? camera->listProperties(deviceId) : QStringList{}; - } - - QString getProperty(const QString& deviceId, const QString& name, bool fromCache) override - { - SimulatorProvider* camera = owner(deviceId); - return camera ? camera->getProperty(deviceId, name, fromCache) : QString{}; - } - - bool setProperty(const QString& deviceId, - const QString& name, - const QString& value, - QString* errorMessage) override - { - SimulatorProvider* camera = owner(deviceId); - if (!camera) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Unknown camera"); - } - return false; - } - return camera->setProperty(deviceId, name, value, errorMessage); - } - - QString getPropertyType(const QString& deviceId, const QString& name) override - { - SimulatorProvider* camera = owner(deviceId); - return camera ? camera->getPropertyType(deviceId, name) : QString{}; - } - - bool isPropertyReadOnly(const QString& deviceId, const QString& name) override - { - SimulatorProvider* camera = owner(deviceId); - return !camera || camera->isPropertyReadOnly(deviceId, name); - } - - bool isPropertyPreInit(const QString& deviceId, const QString& name) override - { - SimulatorProvider* camera = owner(deviceId); - return camera && camera->isPropertyPreInit(deviceId, name); - } - - QStringList getAllowedPropertyValues(const QString& deviceId, const QString& name) override - { - SimulatorProvider* camera = owner(deviceId); - return camera ? camera->getAllowedPropertyValues(deviceId, name) : QStringList{}; - } - - bool hasPropertyLimits(const QString& deviceId, const QString& name) override - { - SimulatorProvider* camera = owner(deviceId); - return camera && camera->hasPropertyLimits(deviceId, name); - } - - double getPropertyLowerLimit(const QString& deviceId, const QString& name) override - { - SimulatorProvider* camera = owner(deviceId); - return camera ? camera->getPropertyLowerLimit(deviceId, name) : 0.0; - } - - double getPropertyUpperLimit(const QString& deviceId, const QString& name) override - { - SimulatorProvider* camera = owner(deviceId); - return camera ? camera->getPropertyUpperLimit(deviceId, name) : 0.0; - } - - private: - // The camera with this id, or none for another id such as "all" - SimulatorProvider* owner(const QString& cameraId) const - { - const qsizetype index = m_ids.indexOf(cameraId); - return index >= 0 ? m_cameras[static_cast(index)].get() : nullptr; - } - - void reportPreviewState() - { - bool running = false; - for (size_t index = 0; index < m_cameras.size(); ++index) - { - running = running || m_cameras[index]->isPreviewRunning(m_ids.at(static_cast(index))); - } - PreviewStateSink sink; - { - QMutexLocker locker(&m_mutex); - if (running == m_lastPreviewState) - { - return; - } - m_lastPreviewState = running; - sink = m_previewStateSink; - } - if (sink) - { - sink(running); - } - } - - QString m_providerId; - QStringList m_ids; - std::vector> m_cameras; - QMutex m_mutex; - PreviewStateSink m_previewStateSink; - bool m_lastPreviewState{false}; - }; - - class ExampleHardwarePlugin final : public QObject, - public scopeone::core::DriverHostProviderPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneDriverHostProviderPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::DriverHostProviderPlugin) - - public: - QString providerId() const override - { - return QStringLiteral("example.hardware"); - } - - // Options: width and height of the images, and cameraCount, the number of cameras (2 by default) - scopeone::core::HardwareProviderPtr createProvider(const QJsonObject& options, - QString* errorMessage) override - { - if (errorMessage) - { - errorMessage->clear(); - } - const int width = options.value(QStringLiteral("width")).toInt(512); - const int height = options.value(QStringLiteral("height")).toInt(512); - const int cameraCount = options.value(QStringLiteral("cameraCount")).toInt(2); - if (width <= 0 || height <= 0 || cameraCount < 1 || cameraCount > 8) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("width and height must be positive and cameraCount from 1 to 8"); - } - return {}; - } - return std::make_shared(providerId(), cameraCount, width, height); - } - }; -} - -#include "ExampleHardwarePlugin.moc" diff --git a/plugins/hardware/ExampleHardware/plugin.json b/plugins/hardware/ExampleHardware/plugin.json deleted file mode 100644 index b4b0867..0000000 --- a/plugins/hardware/ExampleHardware/plugin.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "id": "example.hardware", - "name": "Example Hardware", - "version": "1.0.0", - "scopeOneApi": 1, - "kind": "hardware", - "providerId": "example.hardware", - "autoLoad": false -} diff --git a/plugins/hardware/NIDaqmx/CMakeLists.txt b/plugins/hardware/NIDaqmx/CMakeLists.txt deleted file mode 100644 index 92c4a2f..0000000 --- a/plugins/hardware/NIDaqmx/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -add_library(ScopeOneNIDaqmx MODULE - NIDaqmxPlugin.cpp - NIDaqmxPlugin.h - plugin.json -) -set_target_properties(ScopeOneNIDaqmx PROPERTIES OUTPUT_NAME "NIDaqmxPlugin") -target_link_libraries(ScopeOneNIDaqmx PRIVATE - scopeone::PluginSDK - Qt6::Core -) -scopeone_add_plugin(ScopeOneNIDaqmx hardware) diff --git a/plugins/hardware/NIDaqmx/NIDaqmxPlugin.h b/plugins/hardware/NIDaqmx/NIDaqmxPlugin.h deleted file mode 100644 index 4c74a8c..0000000 --- a/plugins/hardware/NIDaqmx/NIDaqmxPlugin.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "scopeone/DaqDevice.h" - -#include - -namespace scopeone::plugins -{ - class NIDaqmxPlugin final : public QObject, - public scopeone::core::DaqDevicePlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID SCOPEONE_DAQ_DEVICE_PLUGIN_IID FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::DaqDevicePlugin) - - public: - QList devices() const override; - scopeone::core::DaqController* createController( - const QString& deviceId, - QObject* parent = nullptr) override; - }; -} diff --git a/plugins/hardware/NIDaqmx/plugin.json b/plugins/hardware/NIDaqmx/plugin.json deleted file mode 100644 index 21abc3f..0000000 --- a/plugins/hardware/NIDaqmx/plugin.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "id": "scopeone.nidaqmx", - "name": "NI-DAQmx Device Plugin", - "version": "1.0.0", - "scopeOneApi": 1, - "kind": "hardware", - "autoLoad": true -} diff --git a/plugins/hardware/PtuFile/CMakeLists.txt b/plugins/hardware/PtuFile/CMakeLists.txt deleted file mode 100644 index eb7394a..0000000 --- a/plugins/hardware/PtuFile/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -add_library(ScopeOnePtuFile MODULE - PtuFilePlugin.cpp - PtuFilePlugin.h - plugin.json -) -set_target_properties(ScopeOnePtuFile PROPERTIES OUTPUT_NAME "PtuFilePlugin") -target_link_libraries(ScopeOnePtuFile PRIVATE - scopeone::PluginSDK - Qt6::Core -) -scopeone_add_plugin(ScopeOnePtuFile hardware) diff --git a/plugins/hardware/PtuFile/PtuFilePlugin.h b/plugins/hardware/PtuFile/PtuFilePlugin.h deleted file mode 100644 index b68130d..0000000 --- a/plugins/hardware/PtuFile/PtuFilePlugin.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "scopeone/SignalSource.h" - -#include - -namespace scopeone::plugins -{ - class PtuFilePlugin final : public QObject, - public scopeone::core::SignalSourcePlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID SCOPEONE_SIGNAL_SOURCE_PLUGIN_IID FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::SignalSourcePlugin) - - public: - QList signalSources() const override; - scopeone::core::SignalSource* createSignalSource( - const QString& sourceId, - QObject* parent = nullptr) override; - }; -} diff --git a/plugins/hardware/PtuFile/plugin.json b/plugins/hardware/PtuFile/plugin.json deleted file mode 100644 index 69533aa..0000000 --- a/plugins/hardware/PtuFile/plugin.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "id": "scopeone.ptu-file", - "name": "PicoQuant PTU Source", - "version": "1.0.0", - "scopeOneApi": 1, - "kind": "hardware", - "autoLoad": true -} diff --git a/plugins/hardware/SimulatedPmt/CMakeLists.txt b/plugins/hardware/SimulatedPmt/CMakeLists.txt deleted file mode 100644 index 850510a..0000000 --- a/plugins/hardware/SimulatedPmt/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -add_library(ScopeOneSimulatedPmt MODULE - SimulatedPmtPlugin.cpp - SimulatedPmtPlugin.h - SimulatedPmtSource.cpp - SimulatedPmtSource.h - plugin.json -) - -target_link_libraries(ScopeOneSimulatedPmt PRIVATE - scopeone::PluginSDK - Qt6::Core -) - -scopeone_add_plugin(ScopeOneSimulatedPmt hardware) diff --git a/plugins/hardware/SimulatedPmt/SimulatedPmtPlugin.h b/plugins/hardware/SimulatedPmt/SimulatedPmtPlugin.h deleted file mode 100644 index 270979b..0000000 --- a/plugins/hardware/SimulatedPmt/SimulatedPmtPlugin.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "scopeone/SignalSource.h" - -#include - -namespace scopeone::plugins -{ - class SimulatedPmtPlugin final : public QObject, - public scopeone::core::SignalSourcePlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID SCOPEONE_SIGNAL_SOURCE_PLUGIN_IID FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::SignalSourcePlugin) - - public: - QList signalSources() const override; - scopeone::core::SignalSource* createSignalSource( - const QString& sourceId, - QObject* parent = nullptr) override; - }; -} diff --git a/plugins/hardware/SimulatedPmt/plugin.json b/plugins/hardware/SimulatedPmt/plugin.json deleted file mode 100644 index a397c73..0000000 --- a/plugins/hardware/SimulatedPmt/plugin.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "id": "pmt.simulator", - "name": "Simulated PMT", - "version": "1.0.0", - "scopeOneApi": 1, - "kind": "hardware", - "providerId": "pmt.simulator", - "autoLoad": true -} diff --git a/plugins/processing/CMakeLists.txt b/plugins/processing/CMakeLists.txt deleted file mode 100644 index a71123c..0000000 --- a/plugins/processing/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_subdirectory(ExampleProcessing) -add_subdirectory(IscatProcessing) -add_subdirectory(PythonProcessing) -if (TARGET ScopeOneCuda) - add_subdirectory(ExampleCudaProcessing) -endif () diff --git a/plugins/processing/ExampleCudaProcessing/CMakeLists.txt b/plugins/processing/ExampleCudaProcessing/CMakeLists.txt deleted file mode 100644 index 6b8a356..0000000 --- a/plugins/processing/ExampleCudaProcessing/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -add_library(ScopeOneExampleCudaProcessing MODULE - ExampleCudaProcessingPlugin.cpp - ExampleCudaKernel.cu - plugin.json -) -target_link_libraries(ScopeOneExampleCudaProcessing PRIVATE - scopeone::Cuda - scopeone::PluginSDK - Qt6::Core - CUDA::cudart -) -set_target_properties(ScopeOneExampleCudaProcessing PROPERTIES - CUDA_ARCHITECTURES "${SCOPEONE_CUDA_ARCHITECTURES}" -) -set_target_properties(ScopeOneExampleCudaProcessing PROPERTIES OUTPUT_NAME "ExampleCudaProcessing") -scopeone_add_plugin(ScopeOneExampleCudaProcessing processing) diff --git a/plugins/processing/ExampleCudaProcessing/ExampleCudaKernel.cu b/plugins/processing/ExampleCudaProcessing/ExampleCudaKernel.cu deleted file mode 100644 index 873da03..0000000 --- a/plugins/processing/ExampleCudaProcessing/ExampleCudaKernel.cu +++ /dev/null @@ -1,52 +0,0 @@ -#include - -#include - -namespace -{ - __global__ void invertKernel(const float* input, - std::size_t inputPitchBytes, - float* output, - std::size_t outputPitchBytes, - int width, - int height) - { - const int x = blockIdx.x * blockDim.x + threadIdx.x; - const int y = blockIdx.y * blockDim.y + threadIdx.y; - if (x >= width || y >= height) - { - return; - } - - const float* inputRow = reinterpret_cast( - reinterpret_cast(input) - + static_cast(y) * inputPitchBytes); - float* outputRow = reinterpret_cast( - reinterpret_cast(output) - + static_cast(y) * outputPitchBytes); - outputRow[x] = 65535.0f - inputRow[x]; - } -} - -namespace example_cuda -{ - bool launchInvert(const void* input, - std::size_t inputPitchBytes, - void* output, - std::size_t outputPitchBytes, - int width, - int height) - { - const dim3 block(16, 16); - const dim3 grid((width + block.x - 1) / block.x, - (height + block.y - 1) / block.y); - invertKernel<<>>( - static_cast(input), - inputPitchBytes, - static_cast(output), - outputPitchBytes, - width, - height); - return cudaGetLastError() == cudaSuccess && cudaDeviceSynchronize() == cudaSuccess; - } -} diff --git a/plugins/processing/ExampleCudaProcessing/ExampleCudaProcessingPlugin.cpp b/plugins/processing/ExampleCudaProcessing/ExampleCudaProcessingPlugin.cpp deleted file mode 100644 index aa9204f..0000000 --- a/plugins/processing/ExampleCudaProcessing/ExampleCudaProcessingPlugin.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "scopeone/ProcessingPlugin.h" -#include "scopeone/cuda/CudaRealImageModule.h" - -#include - -namespace example_cuda -{ - bool launchInvert(const void* input, - std::size_t inputPitchBytes, - void* output, - std::size_t outputPitchBytes, - int width, - int height); -} - -namespace -{ - class InvertModule final : public scopeone::cuda::CudaRealImageModule - { - public: - InvertModule() - : CudaRealImageModule(scopeone::cuda::GpuMemoryLayout::Pitched2D) - { - } - - QString id() const override { return QStringLiteral("example.cuda_invert"); } - QString name() const override { return QStringLiteral("CUDA Example Invert"); } - QVariantMap parameters() const override { return {}; } - void setParameters(const QVariantMap&) override {} - - std::unique_ptr createRuntime() const override - { - return std::make_unique(); - } - - protected: - bool processDevice(const scopeone::cuda::GpuRealFrame& input, - scopeone::cuda::GpuRealFrame& output, - int) override - { - return example_cuda::launchInvert(input.data(), - input.pitchBytes(), - output.data(), - output.pitchBytes(), - input.width(), - input.height()); - } - }; - - class ExampleCudaProcessingPlugin final : public QObject, - public scopeone::core::ProcessingPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneProcessingPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::ProcessingPlugin) - - public: - QList processingModules() const override - { - return {{QStringLiteral("example.cuda_invert"), - QStringLiteral("CUDA Example Invert")}}; - } - - std::unique_ptr createProcessingModule( - const QString& moduleId) override - { - return moduleId == QStringLiteral("example.cuda_invert") - ? std::make_unique() - : nullptr; - } - }; -} - -#include "ExampleCudaProcessingPlugin.moc" diff --git a/plugins/processing/ExampleCudaProcessing/plugin.json b/plugins/processing/ExampleCudaProcessing/plugin.json deleted file mode 100644 index b317dd2..0000000 --- a/plugins/processing/ExampleCudaProcessing/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "example.cuda_processing", - "name": "Example CUDA Processing", - "version": "1.0.0", - "scopeOneApi": 2, - "kind": "processing" -} diff --git a/plugins/processing/ExampleProcessing/CMakeLists.txt b/plugins/processing/ExampleProcessing/CMakeLists.txt deleted file mode 100644 index 9d48301..0000000 --- a/plugins/processing/ExampleProcessing/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_library(ScopeOneExampleProcessing MODULE - ExampleProcessingPlugin.cpp - plugin.json -) -target_link_libraries(ScopeOneExampleProcessing PRIVATE scopeone::PluginSDK Qt6::Core) -scopeone_add_plugin(ScopeOneExampleProcessing processing) diff --git a/plugins/processing/ExampleProcessing/ExampleProcessingPlugin.cpp b/plugins/processing/ExampleProcessing/ExampleProcessingPlugin.cpp deleted file mode 100644 index c7324cd..0000000 --- a/plugins/processing/ExampleProcessing/ExampleProcessingPlugin.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "scopeone/ProcessingPlugin.h" - -#include - -namespace -{ - class PassthroughModule final : public scopeone::core::ProcessingModule - { - public: - QString id() const override { return QStringLiteral("example.passthrough"); } - QString name() const override { return QStringLiteral("Example Passthrough"); } - QVariantMap parameters() const override { return {}; } - void setParameters(const QVariantMap&) override {} - std::unique_ptr createRuntime() const override - { - return std::make_unique(); - } - scopeone::core::ProcessingResult process(const scopeone::core::ImageFrame& frame, - int) override - { - return {frame, {}}; - } - }; - - class ExampleProcessingPlugin final : public QObject, - public scopeone::core::ProcessingPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneProcessingPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::ProcessingPlugin) - - public: - QList processingModules() const override - { - return {{QStringLiteral("example.passthrough"), - QStringLiteral("Example Passthrough")}}; - } - - std::unique_ptr createProcessingModule( - const QString& moduleId) override - { - return moduleId == QStringLiteral("example.passthrough") - ? std::make_unique() - : nullptr; - } - }; -} - -#include "ExampleProcessingPlugin.moc" diff --git a/plugins/processing/ExampleProcessing/plugin.json b/plugins/processing/ExampleProcessing/plugin.json deleted file mode 100644 index 2401d28..0000000 --- a/plugins/processing/ExampleProcessing/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "example.processing", - "name": "Example Processing", - "version": "1.0.0", - "scopeOneApi": 2, - "kind": "processing" -} diff --git a/plugins/processing/IscatProcessing/CMakeLists.txt b/plugins/processing/IscatProcessing/CMakeLists.txt deleted file mode 100644 index 1a38a9a..0000000 --- a/plugins/processing/IscatProcessing/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -add_library(ScopeOneIscatProcessing MODULE - IscatProcessingPlugin.cpp - IscatProcessingModule.cpp - IscatProcessingModule.h - plugin.json -) - -target_compile_features(ScopeOneIscatProcessing PRIVATE cxx_std_20) -target_link_libraries(ScopeOneIscatProcessing PRIVATE - scopeone::PluginSDK - opencv_core - opencv_imgproc - Qt6::Core -) -set_target_properties(ScopeOneIscatProcessing PROPERTIES - OUTPUT_NAME "IscatProcessingPlugin" -) -scopeone_add_plugin(ScopeOneIscatProcessing processing) diff --git a/plugins/processing/IscatProcessing/IscatProcessingPlugin.cpp b/plugins/processing/IscatProcessing/IscatProcessingPlugin.cpp deleted file mode 100644 index 9a13950..0000000 --- a/plugins/processing/IscatProcessing/IscatProcessingPlugin.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "IscatProcessingModule.h" - -#include "scopeone/ProcessingPlugin.h" - -#include -#include - -namespace -{ - scopeone::core::ProcessingParameterDescriptor makeRealParameter( - const char* key, - const char* name, - double value, - double minimum, - double maximum, - double step, - int decimals) - { - scopeone::core::ProcessingParameterDescriptor descriptor{ - QString::fromLatin1(key), - QString::fromLatin1(name), - scopeone::core::ProcessingParameterType::Real, - value, - minimum, - maximum, - step}; - descriptor.decimals = decimals; - return descriptor; - } - - scopeone::core::ProcessingParameterDescriptor makeBooleanParameter( - const char* key, - const char* name, - bool value) - { - return {QString::fromLatin1(key), - QString::fromLatin1(name), - scopeone::core::ProcessingParameterType::Boolean, - value}; - } - - scopeone::core::ProcessingParameterDescriptor makeIntegerParameter( - const char* key, - const char* name, - int value, - int minimum, - int maximum, - int step) - { - return {QString::fromLatin1(key), - QString::fromLatin1(name), - scopeone::core::ProcessingParameterType::Integer, - value, - minimum, - maximum, - step}; - } - - scopeone::core::ProcessingParameterDescriptor makeChoiceParameter( - const char* key, - const char* name, - int value, - std::initializer_list choices) - { - scopeone::core::ProcessingParameterDescriptor descriptor{ - QString::fromLatin1(key), - QString::fromLatin1(name), - scopeone::core::ProcessingParameterType::Choice, - value}; - int index = 0; - for (const char* choice : choices) - { - descriptor.choices.append({QString::fromLatin1(choice), index++}); - } - return descriptor; - } - - class IscatProcessingPlugin final : public QObject, - public scopeone::core::ProcessingPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneProcessingPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::ProcessingPlugin) - - public: - QList processingModules() const override - { - return {{QStringLiteral("iscat.processing"), - QStringLiteral("iSCAT Processing"), - 1, - {makeChoiceParameter("mode", - "Background mode", - 0, - {"Flat Field", - "Temporal Median", - "Dynamic EMA", - "Snapshot Reference", - "Differential Rolling (DRA)"}), - makeRealParameter("blur_sigma", "Blur sigma", 15.0, 1.0, 100.0, 0.5, 1), - makeIntegerParameter("median_window", "Median window", 31, 3, 101, 2), - makeRealParameter("ema_alpha", "Smoothing factor", 0.05, 0.001, 0.5, 0.005, 3), - makeBooleanParameter("capture_reference", "Capture Reference", false), - makeIntegerParameter("batch_size", "DRA batch size", 16, 1, 500, 1), - makeRealParameter("contrast_gain", "Contrast gain", 20.0, 1.0, 500.0, 1.0, 1), - makeBooleanParameter("enable_high_pass", "Enable High-Pass", false), - makeRealParameter("high_pass_sigma", "High-Pass sigma", 30.0, 2.0, 200.0, 1.0, 1), - makeChoiceParameter("output_mode", - "Output mode", - 0, - {"Contrast Centered", "Absolute Contrast", "Estimated Background"})}, - true}}; - } - - std::unique_ptr createProcessingModule( - const QString& moduleId) override - { - return moduleId == QStringLiteral("iscat.processing") - ? std::make_unique() - : nullptr; - } - }; -} - -#include "IscatProcessingPlugin.moc" diff --git a/plugins/processing/IscatProcessing/plugin.json b/plugins/processing/IscatProcessing/plugin.json deleted file mode 100644 index f947c38..0000000 --- a/plugins/processing/IscatProcessing/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "iscat.processing", - "name": "iSCAT Processing", - "version": "1.0.0", - "scopeOneApi": 2, - "kind": "processing" -} diff --git a/plugins/processing/PythonProcessing/CMakeLists.txt b/plugins/processing/PythonProcessing/CMakeLists.txt deleted file mode 100644 index 11f2a60..0000000 --- a/plugins/processing/PythonProcessing/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -add_library(ScopeOnePythonProcessing MODULE - PythonProcessingPlugin.cpp - plugin.json -) -target_compile_features(ScopeOnePythonProcessing PRIVATE cxx_std_20) -target_link_libraries(ScopeOnePythonProcessing PRIVATE scopeone::PluginSDK Qt6::Core) -scopeone_add_plugin(ScopeOnePythonProcessing processing) diff --git a/plugins/processing/PythonProcessing/PythonProcessingPlugin.cpp b/plugins/processing/PythonProcessing/PythonProcessingPlugin.cpp deleted file mode 100644 index af1a002..0000000 --- a/plugins/processing/PythonProcessing/PythonProcessingPlugin.cpp +++ /dev/null @@ -1,301 +0,0 @@ -#include "scopeone/PluginManifest.h" -#include "scopeone/ProcessingPlugin.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -// Runs processing modules written in Python. Every script in the python folder of a processing -// plugin directory is asked for its modules once, and each module in a pipeline then talks to -// its own Python process, one frame at a time, over stdin and stdout. -namespace -{ - using scopeone::core::ImageFrame; - using scopeone::core::ImagePixelFormat; - using scopeone::core::ProcessingModule; - using scopeone::core::ProcessingModuleDescriptor; - using scopeone::core::ProcessingParameterChoice; - using scopeone::core::ProcessingParameterDescriptor; - using scopeone::core::ProcessingParameterType; - using scopeone::core::ProcessingResult; - - // SCOPEONE_PYTHON, else the interpreter of the Settings dialog, picks the interpreter, - // which needs numpy and the scopeone package - QString pythonExecutable() - { - const QString configured = qEnvironmentVariable("SCOPEONE_PYTHON"); - if (!configured.isEmpty()) - { - return configured; - } - const QString selected = QSettings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")) - .value(QStringLiteral("Python/Interpreter")).toString(); - if (!selected.isEmpty()) - { - return selected; - } -#ifdef Q_OS_WIN - return QStringLiteral("python"); -#else - return QStringLiteral("python3"); -#endif - } - - QStringList workerArguments(const QString& command, const QString& scriptPath) - { - return {QStringLiteral("-m"), QStringLiteral("scopeone.processing"), command, scriptPath}; - } - - ProcessingParameterDescriptor parameterFrom(const QJsonObject& object) - { - static const QHash types{ - {QStringLiteral("int"), ProcessingParameterType::Integer}, - {QStringLiteral("real"), ProcessingParameterType::Real}, - {QStringLiteral("bool"), ProcessingParameterType::Boolean}, - {QStringLiteral("choice"), ProcessingParameterType::Choice}}; - ProcessingParameterDescriptor parameter; - parameter.key = object.value(QStringLiteral("key")).toString(); - parameter.name = object.value(QStringLiteral("name")).toString(); - parameter.type = types.value(object.value(QStringLiteral("type")).toString()); - parameter.defaultValue = object.value(QStringLiteral("default")).toVariant(); - parameter.minimum = object.value(QStringLiteral("minimum")).toVariant(); - parameter.maximum = object.value(QStringLiteral("maximum")).toVariant(); - parameter.step = object.value(QStringLiteral("step")).toVariant(); - parameter.decimals = object.value(QStringLiteral("decimals")).toInt(); - for (const QJsonValue& choice : object.value(QStringLiteral("choices")).toArray()) - { - parameter.choices.append({choice[QStringLiteral("name")].toString(), - choice[QStringLiteral("value")].toVariant()}); - } - return parameter; - } - - struct PythonScriptModule - { - QString scriptPath; - ProcessingModuleDescriptor descriptor; - }; - - // The modules a script declares, as `python -m scopeone.processing describe` lists them - QList describeScript(const QString& scriptPath) - { - QProcess process; - process.start(pythonExecutable(), workerArguments(QStringLiteral("describe"), scriptPath)); - process.waitForFinished(30000); - if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) - { - qWarning().noquote() << QStringLiteral("Python processing script %1 failed: %2") - .arg(scriptPath, QString::fromUtf8(process.readAllStandardError())); - return {}; - } - - QList modules; - for (const QJsonValue& value : QJsonDocument::fromJson(process.readAllStandardOutput()).array()) - { - const QJsonObject object = value.toObject(); - PythonScriptModule module; - module.scriptPath = scriptPath; - module.descriptor.id = object.value(QStringLiteral("id")).toString(); - module.descriptor.name = object.value(QStringLiteral("name")).toString(); - for (const QJsonValue& parameter : object.value(QStringLiteral("parameters")).toArray()) - { - module.descriptor.parameters.append(parameterFrom(parameter.toObject())); - } - modules.append(module); - } - return modules; - } - - // One module of a pipeline. The pipeline calls process from changing pool threads, so the - // Python process lives on a thread of its own and every frame is handed to that thread. - class PythonProcessingModule final : public ProcessingModule - { - public: - explicit PythonProcessingModule(std::shared_ptr script) - : m_script(std::move(script)) - { - for (const ProcessingParameterDescriptor& parameter : m_script->descriptor.parameters) - { - m_parameters.insert(parameter.key, parameter.defaultValue); - } - } - - ~PythonProcessingModule() override - { - if (m_thread) - { - QMetaObject::invokeMethod(m_threadContext.get(), [this]() { m_process.reset(); }, - Qt::BlockingQueuedConnection); - m_thread->quit(); - m_thread->wait(); - } - } - - QString id() const override { return m_script->descriptor.id; } - QString name() const override { return m_script->descriptor.name; } - QVariantMap parameters() const override { return m_parameters; } - - void setParameters(const QVariantMap& parameters) override - { - for (auto it = parameters.constBegin(); it != parameters.constEnd(); ++it) - { - m_parameters.insert(it.key(), it.value()); - } - } - - std::unique_ptr createRuntime() const override - { - auto runtime = std::make_unique(m_script); - runtime->setParameters(m_parameters); - return runtime; - } - - ProcessingResult process(const ImageFrame& frame, int) override - { - if (!m_thread) - { - m_thread = std::make_unique(); - m_threadContext = std::make_unique(); - m_threadContext->moveToThread(m_thread.get()); - m_thread->start(); - } - ProcessingResult result; - QMetaObject::invokeMethod(m_threadContext.get(), [&]() { result = exchange(frame); }, - Qt::BlockingQueuedConnection); - return result; - } - - private: - ProcessingResult failure(const QString& message) - { - const QString log = m_process ? QString::fromUtf8(m_process->readAllStandardError()) : QString(); - m_process.reset(); - return ProcessingResult(ImageFrame{}, log.isEmpty() ? message : message + QLatin1Char('\n') + log); - } - - // Sends one frame and reads the frame that comes back, on the module's own thread - ProcessingResult exchange(const ImageFrame& frame) - { - if (!m_process) - { - m_process = std::make_unique(); - m_process->start(pythonExecutable(), - workerArguments(QStringLiteral("serve"), m_script->scriptPath)); - if (!m_process->waitForStarted(10000)) - { - return failure(QStringLiteral("Could not start %1").arg(pythonExecutable())); - } - } - - const QJsonObject request{ - {QStringLiteral("module"), m_script->descriptor.id}, - {QStringLiteral("width"), frame.width}, - {QStringLiteral("height"), frame.height}, - {QStringLiteral("stride"), frame.stride}, - {QStringLiteral("format"), frame.isMono16() ? QStringLiteral("mono16") : QStringLiteral("mono8")}, - {QStringLiteral("bits"), frame.isMono16() ? frame.bitsPerSample : 8}, - {QStringLiteral("parameters"), QJsonObject::fromVariantMap(m_parameters)}, - {QStringLiteral("bytes"), static_cast(frame.bytes.size())}}; - m_process->write(QJsonDocument(request).toJson(QJsonDocument::Compact) + '\n'); - m_process->write(frame.bytes); - - while (!m_process->canReadLine()) - { - if (!m_process->waitForReadyRead(30000)) - { - return failure(QStringLiteral("Python module %1 stopped answering").arg(id())); - } - } - const QJsonObject reply = QJsonDocument::fromJson(m_process->readLine()).object(); - const QByteArray printed = m_process->readAllStandardError(); - if (!printed.isEmpty()) - { - qInfo().noquote() << QStringLiteral("[%1] %2").arg(id(), QString::fromUtf8(printed).trimmed()); - } - if (reply.contains(QStringLiteral("error"))) - { - return ProcessingResult(ImageFrame{}, reply.value(QStringLiteral("error")).toString()); - } - - const qint64 byteCount = reply.value(QStringLiteral("bytes")).toInteger(); - while (m_process->bytesAvailable() < byteCount) - { - if (!m_process->waitForReadyRead(30000)) - { - return failure(QStringLiteral("Python module %1 stopped answering").arg(id())); - } - } - - ImageFrame output = frame; - const bool mono16 = reply.value(QStringLiteral("format")).toString() == QStringLiteral("mono16"); - output.pixelFormat = mono16 ? ImagePixelFormat::Mono16 : ImagePixelFormat::Mono8; - output.bitsPerSample = mono16 ? (frame.isMono16() ? frame.bitsPerSample : 16) : 8; - output.width = reply.value(QStringLiteral("width")).toInt(); - output.height = reply.value(QStringLiteral("height")).toInt(); - output.stride = output.width * (mono16 ? 2 : 1); - output.bytes = m_process->read(byteCount); - return ProcessingResult(output); - } - - std::shared_ptr m_script; - QVariantMap m_parameters; - std::unique_ptr m_thread; - std::unique_ptr m_threadContext; - std::unique_ptr m_process; - }; - - class PythonProcessingPlugin final : public QObject, - public scopeone::core::ProcessingPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneProcessingPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::ProcessingPlugin) - - public: - PythonProcessingPlugin() - { - for (const QString& directory : scopeone::core::pluginDirectories( - scopeone::core::PluginKind::Processing)) - { - const QDir scripts(QDir(directory).filePath(QStringLiteral("python"))); - for (const QString& name : scripts.entryList({QStringLiteral("*.py")}, QDir::Files, QDir::Name)) - { - for (const PythonScriptModule& module : describeScript(scripts.filePath(name))) - { - m_modules.insert(module.descriptor.id, - std::make_shared(module)); - } - } - } - } - - QList processingModules() const override - { - QList descriptors; - for (const auto& module : m_modules) - { - descriptors.append(module->descriptor); - } - return descriptors; - } - - std::unique_ptr createProcessingModule(const QString& moduleId) override - { - const auto module = m_modules.value(moduleId); - return module ? std::make_unique(module) : nullptr; - } - - private: - QHash> m_modules; - }; -} - -#include "PythonProcessingPlugin.moc" diff --git a/plugins/processing/PythonProcessing/plugin.json b/plugins/processing/PythonProcessing/plugin.json deleted file mode 100644 index 2825b3d..0000000 --- a/plugins/processing/PythonProcessing/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "python.processing", - "name": "Python Processing", - "version": "1.0.0", - "scopeOneApi": 2, - "kind": "processing" -} diff --git a/plugins/tools/BrillouinTool/BrillouinToolPlugin.cpp b/plugins/tools/BrillouinTool/BrillouinToolPlugin.cpp deleted file mode 100644 index fc6b2d8..0000000 --- a/plugins/tools/BrillouinTool/BrillouinToolPlugin.cpp +++ /dev/null @@ -1,389 +0,0 @@ -#include "scopeone/ToolFrameStream.h" -#include "scopeone/ToolPlugin.h" -#include "scopeone/ToolPlugin.h" -#include "scopeone/ImageSceneModel.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include - -namespace -{ - using scopeone::core::ImageFrame; - - struct BrillouinParameters - { - int bandRows{32}; - int binColumns{8}; - double ghzPerPixel{0.5}; - double minGhz{4.5}; - double maxGhz{6.5}; - }; - - // Position of a peak in a spectrum, as the centroid of the light around the strongest sample - double peakPosition(const std::vector& spectrum, int first, int last, float background) - { - int best = first; - for (int row = first; row < last; ++row) - { - if (spectrum[row] > spectrum[best]) - { - best = row; - } - } - double weight = 0.0; - double moment = 0.0; - for (int row = (std::max)(first, best - 3); row <= (std::min)(last - 1, best + 3); ++row) - { - const double value = (std::max)(0.0f, spectrum[row] - background); - weight += value; - moment += value * row; - } - return weight > 0.0 ? moment / weight : best; - } - - // The image of the spectrometer holds one line of the sample after another, each in a band of rows with the - // frequency across the band. The distance of the Stokes and the anti-Stokes peak of a position is twice its - // Brillouin shift, which is what the map shows, in the range given and as 16 bit. - ImageFrame brillouinMap(const ImageFrame& input, const BrillouinParameters& params) - { - if (!input.isValid() || params.bandRows < 8 || input.height < params.bandRows) - { - return {}; - } - const int depth = input.isMono16() ? CV_16UC1 : CV_8UC1; - const cv::Mat source(input.height, input.width, depth, - const_cast(input.bytes.constData()), static_cast(input.stride)); - cv::Mat image; - source.convertTo(image, CV_32F); - - const int lines = input.height / params.bandRows; - const int columns = input.width / (std::max)(1, params.binColumns); - const int half = params.bandRows / 2; - const double span = params.maxGhz - params.minGhz; - cv::Mat map(lines, columns, CV_16UC1); - std::vector spectrum(static_cast(params.bandRows)); - for (int line = 0; line < lines; ++line) - { - for (int column = 0; column < columns; ++column) - { - const cv::Rect cell(column * params.binColumns, line * params.bandRows, - params.binColumns, params.bandRows); - cv::Mat profile; - cv::reduce(image(cell), profile, 1, cv::REDUCE_AVG, CV_32F); - for (int row = 0; row < params.bandRows; ++row) - { - spectrum[static_cast(row)] = profile.at(row, 0); - } - const float background = *std::min_element(spectrum.begin(), spectrum.end()); - const double stokes = peakPosition(spectrum, 0, half, background); - const double antiStokes = peakPosition(spectrum, half, params.bandRows, background); - const double ghz = 0.5 * (antiStokes - stokes) * params.ghzPerPixel; - const double level = span > 0.0 ? (ghz - params.minGhz) / span : 0.0; - map.at(line, column) = static_cast(std::clamp(level, 0.0, 1.0) * 65535.0 + 0.5); - } - } - - ImageFrame frame; - frame.cameraId = input.cameraId; - frame.width = columns; - frame.height = lines; - frame.stride = columns * 2; - frame.bitsPerSample = 16; - frame.pixelFormat = scopeone::core::ImagePixelFormat::Mono16; - frame.frameIndex = input.frameIndex; - frame.timestampNs = input.timestampNs; - frame.sourceRoiWidth = columns; - frame.sourceRoiHeight = lines; - frame.bytes = QByteArray(reinterpret_cast(map.data), static_cast(map.total() * 2)); - return frame; - } - - class BrillouinTool final : public QWidget - { - public: - BrillouinTool(scopeone::ui::ScopeOneToolContext& context, QWidget* parent) - : QWidget(parent) - , m_context(context) - { - setWindowFlag(Qt::Window, true); - setWindowTitle(QStringLiteral("Brillouin Map")); - resize(1000, 640); - - auto* layout = new QVBoxLayout(this); - auto* previews = new QHBoxLayout(); - m_inputPreview = createPreview(QStringLiteral("Spectra")); - m_resultPreview = createPreview(QStringLiteral("Brillouin shift")); - previews->addWidget(m_inputPreview, 2); - previews->addWidget(m_resultPreview, 2); - layout->addLayout(previews, 1); - - auto* form = new QFormLayout(); - m_layerComboBox = new QComboBox(this); - m_bandRows = createSpinBox(8, 256, m_parameters.bandRows); - m_binColumns = createSpinBox(1, 64, m_parameters.binColumns); - m_ghzPerPixel = createDoubleSpinBox(0.001, 10.0, m_parameters.ghzPerPixel, 3); - m_minGhz = createDoubleSpinBox(0.0, 50.0, m_parameters.minGhz, 2); - m_maxGhz = createDoubleSpinBox(0.0, 50.0, m_parameters.maxGhz, 2); - form->addRow(QStringLiteral("Input"), m_layerComboBox); - form->addRow(QStringLiteral("Rows per line"), m_bandRows); - form->addRow(QStringLiteral("Columns per point"), m_binColumns); - form->addRow(QStringLiteral("GHz per pixel"), m_ghzPerPixel); - form->addRow(QStringLiteral("Map from (GHz)"), m_minGhz); - form->addRow(QStringLiteral("Map to (GHz)"), m_maxGhz); - layout->addLayout(form); - - m_liveCheckBox = new QCheckBox(QStringLiteral("Live"), this); - m_liveCheckBox->setChecked(true); - m_status = new QLabel(QStringLiteral("Ready"), this); - auto* bottom = new QHBoxLayout(); - bottom->addWidget(m_liveCheckBox); - bottom->addStretch(); - bottom->addWidget(m_status); - layout->addLayout(bottom); - - m_stream = new scopeone::ui::ScopeOneToolFrameStream(m_context.core(), this); - - refreshLayerComboBox(); - connect(m_context.core().imageSceneModel(), - &scopeone::core::ImageSceneModel::layersChanged, - this, - [this]() - { - const QString previous = m_layerComboBox->currentData().toString(); - refreshLayerComboBox(); - if (previous != m_layerComboBox->currentData().toString()) - { - selectSource(); - } - }); - connect(m_layerComboBox, &QComboBox::currentIndexChanged, this, [this]() { selectSource(); }); - for (QSpinBox* box : {m_bandRows, m_binColumns}) - { - connect(box, &QSpinBox::valueChanged, this, [this]() { rerun(); }); - } - for (QDoubleSpinBox* box : {m_ghzPerPixel, m_minGhz, m_maxGhz}) - { - connect(box, &QDoubleSpinBox::valueChanged, this, [this]() { rerun(); }); - } - connect(m_liveCheckBox, &QCheckBox::toggled, this, - [this](bool enabled) - { - m_stream->setEnabled(enabled); - m_status->setText(enabled ? QStringLiteral("Live") : QStringLiteral("Paused")); - if (enabled) - { - rerun(); - } - }); - connect(m_stream, &scopeone::ui::ScopeOneToolFrameStream::frameReady, this, - [this](const ImageFrame& frame) - { - if (m_liveCheckBox->isChecked()) - { - process(frame); - } - }); - selectSource(); - } - - private: - static QSpinBox* createSpinBox(int minimum, int maximum, int value) - { - auto* box = new QSpinBox(); - box->setRange(minimum, maximum); - box->setValue(value); - return box; - } - - static QDoubleSpinBox* createDoubleSpinBox(double minimum, double maximum, double value, int decimals) - { - auto* box = new QDoubleSpinBox(); - box->setRange(minimum, maximum); - box->setDecimals(decimals); - box->setValue(value); - return box; - } - - static QLabel* createPreview(const QString& title) - { - auto* preview = new QLabel(title); - preview->setAlignment(Qt::AlignCenter); - preview->setMinimumSize(240, 200); - preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - preview->setFrameShape(QFrame::Box); - return preview; - } - - static void showPreview(QLabel* preview, const ImageFrame& frame) - { - const QImage image(reinterpret_cast(frame.bytes.constData()), - frame.width, - frame.height, - frame.stride, - frame.isMono16() ? QImage::Format_Grayscale16 : QImage::Format_Grayscale8); - preview->setPixmap(QPixmap::fromImage(image).scaled( - preview->size(), Qt::KeepAspectRatio, Qt::FastTransformation)); - } - - BrillouinParameters parameters() const - { - BrillouinParameters params; - params.bandRows = m_bandRows->value(); - params.binColumns = m_binColumns->value(); - params.ghzPerPixel = m_ghzPerPixel->value(); - params.minGhz = m_minGhz->value(); - params.maxGhz = m_maxGhz->value(); - return params; - } - - void refreshLayerComboBox() - { - const QString current = m_layerComboBox->currentData().toString(); - const QString active = m_context.currentLayerKey(); - const QSignalBlocker blocker(m_layerComboBox); - m_layerComboBox->clear(); - for (const QString& layerKey : m_context.core().imageSceneModel()->layerIds()) - { - QString name = layerKey; - scopeone::core::DocumentLayer layer; - if (m_context.core().imageSceneModel()->findLayer(layerKey, layer) && !layer.name.isEmpty()) - { - name = layer.name; - } - m_layerComboBox->addItem(name, layerKey); - } - int index = m_layerComboBox->findData(current); - if (index < 0 && !active.isEmpty()) - { - index = m_layerComboBox->findData(active); - } - if (index >= 0) - { - m_layerComboBox->setCurrentIndex(index); - } - } - - QString layerKey() const - { - const QString key = m_layerComboBox->currentData().toString(); - return key.isEmpty() ? m_context.currentLayerKey() : key; - } - - void selectSource() - { - m_sourceId = scopeone::ui::ScopeOneToolCore::sourceIdFromLayerKey(layerKey()); - m_stream->setSourceId(m_sourceId); - rerun(); - } - - void rerun() - { - const ImageFrame frame = m_context.core().graphFrame(layerKey()); - if (frame.isValid()) - { - process(frame); - } - else - { - m_status->setText(QStringLiteral("No spectra layer available")); - } - } - - void process(const ImageFrame& input) - { - showPreview(m_inputPreview, input); - const ImageFrame map = brillouinMap(input, parameters()); - if (!map.isValid()) - { - m_status->setText(QStringLiteral("The image holds no line of spectra")); - return; - } - showPreview(m_resultPreview, map); - const ImageFrame stored = m_context.core().publishToolStreamFrame( - map.cameraId, map, QStringLiteral("Brillouin Shift")); - if (!stored.isValid()) - { - m_status->setText(QStringLiteral("Failed to publish the map")); - return; - } - const QString outputLayer = scopeone::ui::ScopeOneToolCore::toolLayerKey(stored.cameraId); - if (m_shownSourceId != m_sourceId || m_shownOutputLayerKey != outputLayer) - { - QStringList layers; - const QString rawLayer = scopeone::ui::ScopeOneToolCore::rawLayerKey(m_sourceId); - if (m_context.core().graphFrame(rawLayer).isValid()) - { - layers.append(rawLayer); - } - layers.append(outputLayer); - m_context.showLayers(layers, layers.size() > 1); - m_shownSourceId = m_sourceId; - m_shownOutputLayerKey = outputLayer; - } - m_status->setText(m_liveCheckBox->isChecked() ? QStringLiteral("Live") : QStringLiteral("Map ready")); - } - - scopeone::ui::ScopeOneToolContext& m_context; - BrillouinParameters m_parameters; - QLabel* m_inputPreview{nullptr}; - QLabel* m_resultPreview{nullptr}; - QComboBox* m_layerComboBox{nullptr}; - QSpinBox* m_bandRows{nullptr}; - QSpinBox* m_binColumns{nullptr}; - QDoubleSpinBox* m_ghzPerPixel{nullptr}; - QDoubleSpinBox* m_minGhz{nullptr}; - QDoubleSpinBox* m_maxGhz{nullptr}; - QCheckBox* m_liveCheckBox{nullptr}; - QLabel* m_status{nullptr}; - scopeone::ui::ScopeOneToolFrameStream* m_stream{nullptr}; - QString m_sourceId; - QString m_shownSourceId; - QString m_shownOutputLayerKey; - }; - - class BrillouinToolPlugin final : public QObject, - public scopeone::ui::ScopeOneToolPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneToolPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::ui::ScopeOneToolPlugin) - - public: - QList tools() const override - { - return {{QStringLiteral("scopeone.brillouin_map"), - QStringLiteral("Brillouin Map"), - QStringLiteral("Reconstruction"), - scopeone::ui::ToolWindowMode::ModelessSingleton, - false}}; - } - - QWidget* createTool(const QString& toolId, - scopeone::ui::ScopeOneToolContext& context, - QWidget* parent) override - { - return toolId == QStringLiteral("scopeone.brillouin_map") - ? new BrillouinTool(context, parent) - : nullptr; - } - }; -} - -#include "BrillouinToolPlugin.moc" diff --git a/plugins/tools/BrillouinTool/CMakeLists.txt b/plugins/tools/BrillouinTool/CMakeLists.txt deleted file mode 100644 index f225932..0000000 --- a/plugins/tools/BrillouinTool/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -add_library(ScopeOneBrillouinTool MODULE - BrillouinToolPlugin.cpp - plugin.json -) -target_compile_features(ScopeOneBrillouinTool PRIVATE cxx_std_20) -target_link_libraries(ScopeOneBrillouinTool PRIVATE - scopeone::PluginSDK - opencv_core - Qt6::Widgets -) -scopeone_add_plugin(ScopeOneBrillouinTool tools) diff --git a/plugins/tools/BrillouinTool/plugin.json b/plugins/tools/BrillouinTool/plugin.json deleted file mode 100644 index 191d815..0000000 --- a/plugins/tools/BrillouinTool/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "scopeone.brillouin.map", - "name": "Brillouin Map", - "version": "1.0.0", - "scopeOneApi": 3, - "kind": "tool" -} diff --git a/plugins/tools/CMakeLists.txt b/plugins/tools/CMakeLists.txt deleted file mode 100644 index 8fc817f..0000000 --- a/plugins/tools/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -add_subdirectory(DhmTool) -add_subdirectory(BrillouinTool) -add_subdirectory(ScanningDaqTool) -add_subdirectory(ExampleTool) diff --git a/plugins/tools/DhmTool/CMakeLists.txt b/plugins/tools/DhmTool/CMakeLists.txt deleted file mode 100644 index bce1ad7..0000000 --- a/plugins/tools/DhmTool/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -add_library(ScopeOneDhmTool MODULE - DhmToolPlugin.cpp - DhmReconstruction.cpp - DhmReconstruction.h - plugin.json -) -target_compile_features(ScopeOneDhmTool PRIVATE cxx_std_20) -target_link_libraries(ScopeOneDhmTool PRIVATE - scopeone::PluginSDK - opencv_core - opencv_imgcodecs - opencv_imgproc - Qt6::Widgets -) -scopeone_add_plugin(ScopeOneDhmTool tools) diff --git a/plugins/tools/DhmTool/plugin.json b/plugins/tools/DhmTool/plugin.json deleted file mode 100644 index 6def75f..0000000 --- a/plugins/tools/DhmTool/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "scopeone.dhm.reconstruction", - "name": "DHM Reconstruction", - "version": "1.0.0", - "scopeOneApi": 3, - "kind": "tool" -} diff --git a/plugins/tools/ExampleTool/CMakeLists.txt b/plugins/tools/ExampleTool/CMakeLists.txt deleted file mode 100644 index 78abc6b..0000000 --- a/plugins/tools/ExampleTool/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_library(ScopeOneExampleTool MODULE - ExampleToolPlugin.cpp - plugin.json -) -target_link_libraries(ScopeOneExampleTool PRIVATE scopeone::PluginSDK Qt6::Widgets) -scopeone_add_plugin(ScopeOneExampleTool tools) diff --git a/plugins/tools/ExampleTool/ExampleToolPlugin.cpp b/plugins/tools/ExampleTool/ExampleToolPlugin.cpp deleted file mode 100644 index c0581a9..0000000 --- a/plugins/tools/ExampleTool/ExampleToolPlugin.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "scopeone/ToolPlugin.h" - -#include -#include -#include -#include - -namespace -{ - class ExampleToolPlugin final : public QObject, - public scopeone::ui::ScopeOneToolPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneToolPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::ui::ScopeOneToolPlugin) - - public: - QList tools() const override - { - return {{QStringLiteral("example.tool.window"), - QStringLiteral("Example Tool"), - QStringLiteral("Examples")}}; - } - - QWidget* createTool(const QString& toolId, - scopeone::ui::ScopeOneToolContext& context, - QWidget* parent) override - { - if (toolId != QStringLiteral("example.tool.window")) - { - return nullptr; - } - auto* window = new QWidget(parent, Qt::Window); - window->setWindowTitle(QStringLiteral("Example Tool")); - auto* layout = new QVBoxLayout(window); - layout->addWidget(new QLabel( - QStringLiteral("Active layer: %1").arg(context.currentLayerKey()), window)); - return window; - } - }; -} - -#include "ExampleToolPlugin.moc" diff --git a/plugins/tools/ExampleTool/plugin.json b/plugins/tools/ExampleTool/plugin.json deleted file mode 100644 index de41723..0000000 --- a/plugins/tools/ExampleTool/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "example.tool", - "name": "Example Tool", - "version": "1.0.0", - "scopeOneApi": 3, - "kind": "tool" -} diff --git a/plugins/tools/ScanningDaqTool/CMakeLists.txt b/plugins/tools/ScanningDaqTool/CMakeLists.txt deleted file mode 100644 index 54ecbb4..0000000 --- a/plugins/tools/ScanningDaqTool/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -add_library(ScopeOneScanningDaqTool MODULE - DaqControlWidget.cpp - DaqControlWidget.h - SignalMonitorWidget.cpp - SignalMonitorWidget.h - ScanningDaqToolPlugin.cpp - plugin.json -) -set_target_properties(ScopeOneScanningDaqTool PROPERTIES OUTPUT_NAME "ScanningDaqTool") -target_link_libraries(ScopeOneScanningDaqTool PRIVATE - scopeone::PluginSDK - Qt6::Widgets -) -scopeone_add_plugin(ScopeOneScanningDaqTool tools) diff --git a/plugins/tools/ScanningDaqTool/ScanningDaqToolPlugin.cpp b/plugins/tools/ScanningDaqTool/ScanningDaqToolPlugin.cpp deleted file mode 100644 index 39dadac..0000000 --- a/plugins/tools/ScanningDaqTool/ScanningDaqToolPlugin.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "DaqControlWidget.h" -#include "SignalMonitorWidget.h" - -#include "scopeone/ToolPlugin.h" - -#include - -namespace scopeone::plugins -{ - class ScanningDaqToolPlugin final : public QObject, - public scopeone::ui::ScopeOneToolPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneToolPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::ui::ScopeOneToolPlugin) - - public: - QList tools() const override - { - return {{QStringLiteral("scopeone.daq_control"), - QStringLiteral("Scanning and DAQ Control"), - QStringLiteral("Acquisition"), - scopeone::ui::ToolWindowMode::ModelessSingleton, - false}, - {QStringLiteral("scopeone.signal_monitor"), - QStringLiteral("Signal Monitor"), - QStringLiteral("Acquisition"), - scopeone::ui::ToolWindowMode::ModelessSingleton, - false}}; - } - - QWidget* createTool(const QString& toolId, - scopeone::ui::ScopeOneToolContext& context, - QWidget* parent) override - { - if (toolId == QStringLiteral("scopeone.daq_control")) - { - return new DaqControlWidget(&context.core(), parent); - } - if (toolId == QStringLiteral("scopeone.signal_monitor")) - { - return new SignalMonitorWidget(&context.core(), context, parent); - } - return nullptr; - } - }; -} - -#include "ScanningDaqToolPlugin.moc" diff --git a/plugins/tools/ScanningDaqTool/plugin.json b/plugins/tools/ScanningDaqTool/plugin.json deleted file mode 100644 index ecf0fa6..0000000 --- a/plugins/tools/ScanningDaqTool/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "scopeone.scanning-daq-tool", - "name": "Scanning and DAQ Tools", - "version": "1.0.0", - "scopeOneApi": 3, - "kind": "tool" -} diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 78670a8..037cdc9 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -18,9 +18,6 @@ #include "scopeone/ImageSceneModel.h" #include "ImageGalleryWidget.h" #include "ImageWorkspace.h" -#include "CameraScaleDialog.h" -#include "ParticleDetectionDialog.h" -#include "StageMosaicDialog.h" #include "ImageProcessingWidget.h" #include "RecordingWidget.h" #include "SettingsDialog.h" @@ -1023,45 +1020,15 @@ namespace scopeone::ui rebuildWorkspacesMenu(); } - // Register built in tools and discover external tool plugins + // The tools in src/tools register themselves, so the menu lists whatever was built in void MainWindow::setupTools() { - m_toolCore = new ScopeOneToolCore(*m_scopeonecore, m_scopeonecore); m_toolRegistry = std::make_unique(*this); - m_toolRegistry->registerTool( - {QStringLiteral("scopeone.scale"), tr("&Scale..."), {}, ToolWindowMode::Modal, true}, - [this](ScopeOneToolContext&, QWidget* parent) - { - return new CameraScaleDialog(m_scopeonecore, parent); - }); - m_toolRegistry->registerTool( - {QStringLiteral("scopeone.stage_mosaic"), tr("Stage &Mosaic..."), {}, - ToolWindowMode::ModelessSingleton}, - [this](ScopeOneToolContext& context, QWidget* parent) - { - return new StageMosaicDialog(m_scopeonecore, context, parent); - }); - m_toolRegistry->registerTool( - {QStringLiteral("scopeone.particle_detection"), tr("&Particle Detection..."), {}, - ToolWindowMode::ModelessSingleton}, - [this](ScopeOneToolContext& context, QWidget* parent) - { - return new ParticleDetectionDialog(m_scopeonecore, context, parent); - }); - - for (const QString& directory : scopeone::core::pluginDirectories( - scopeone::core::PluginKind::Tool)) - { - for (const QString& error : m_toolRegistry->loadPlugins(directory)) - { - qWarning().noquote() << QStringLiteral("Failed to load tool plugin %1").arg(error); - } - } } - ScopeOneToolCore& MainWindow::core() const + scopeone::core::ScopeOneCore& MainWindow::core() const { - return *m_toolCore; + return *m_scopeonecore; } QString MainWindow::currentLayerKey() const diff --git a/src/MainWindow.h b/src/MainWindow.h index aa5e627..4ba638e 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -1,7 +1,7 @@ #pragma once #include "scopeone/ScopeOneCore.h" -#include "ScopeOneToolPlugin.h" +#include "ToolRegistry.h" #include #include @@ -54,7 +54,7 @@ namespace scopeone::ui explicit MainWindow(scopeone::core::ScopeOneCore* core, QWidget* parent = nullptr); ~MainWindow() override = default; - ScopeOneToolCore& core() const override; + scopeone::core::ScopeOneCore& core() const override; QString currentLayerKey() const override; void showLayers(const QStringList& layerKeys, bool sideBySide = false) override; void showToolStatus(const QString& message, int timeoutMs = 5000) override; @@ -207,8 +207,6 @@ namespace scopeone::ui QTimer* m_statusMessageTimer{nullptr}; QTimer* m_cursorRefreshTimer{nullptr}; scopeone::core::ScopeOneCore* m_scopeonecore{nullptr}; - // Owned by the Core, so it outlives the tool windows closed with this window - ScopeOneToolCore* m_toolCore{nullptr}; QString m_currentControlTarget{QStringLiteral("All")}; QPoint m_lastPreviewMousePos{-1, -1}; QList> m_closePendingSessions; diff --git a/src/PluginManagerDialog.cpp b/src/PluginManagerDialog.cpp index 3f4a342..9c4d799 100644 --- a/src/PluginManagerDialog.cpp +++ b/src/PluginManagerDialog.cpp @@ -6,7 +6,6 @@ #include "scopeone/ProcessingPlugin.h" #include "scopeone/SignalSource.h" #include "scopeone/ScopeOneCore.h" -#include "scopeone/ToolPlugin.h" #include #include @@ -41,11 +40,7 @@ namespace scopeone::ui { QList plugins = scopeone::core::discoverPlugins( scopeone::core::PluginKind::Processing); - for (const scopeone::core::PluginKind kind : - {scopeone::core::PluginKind::Tool, scopeone::core::PluginKind::Hardware}) - { - plugins.append(scopeone::core::discoverPlugins(kind)); - } + plugins.append(scopeone::core::discoverPlugins(scopeone::core::PluginKind::Hardware)); return plugins; } } @@ -235,7 +230,6 @@ namespace scopeone::ui QList candidates; scopeone::core::PluginKind kind = scopeone::core::PluginKind::Processing; for (const auto candidateKind : {scopeone::core::PluginKind::Processing, - scopeone::core::PluginKind::Tool, scopeone::core::PluginKind::Hardware}) { for (const auto& candidate : diff --git a/src/ScopeOneLocalApiServer.cpp b/src/ScopeOneLocalApiServer.cpp index 15b9d6c..792a99b 100644 --- a/src/ScopeOneLocalApiServer.cpp +++ b/src/ScopeOneLocalApiServer.cpp @@ -1,4 +1,5 @@ #include "ScopeOneLocalApiServer.h" +#include "scopeone/SharedFrame.h" #include "scopeone/ImageSceneModel.h" #include "PreviewWidget.h" @@ -4039,7 +4040,7 @@ namespace scopeone::ui return false; } - scopeone::core::SharedFrameHeader header = frame.toSharedFrameHeader(); + scopeone::core::SharedFrameHeader header = scopeone::core::toSharedFrameHeader(frame); exportedFrame = frame; exportedFrame.cameraId = frame.cameraId.trimmed(); exportedFrame.timestampNs = header.timestampNs; @@ -4095,7 +4096,7 @@ namespace scopeone::ui const QString resolvedCameraId = cameraId.trimmed().isEmpty() ? m_frameMappingCameraId : cameraId.trimmed(); - frame = scopeone::core::ImageFrame::fromSharedFrame(resolvedCameraId, header, payload); + frame = scopeone::core::imageFrameFromShared(resolvedCameraId, header, payload); if (!frame.isValid()) { errorMessage = QStringLiteral("Frame mapping metadata is invalid"); diff --git a/src/ScopeOneToolPlugin.cpp b/src/ToolRegistry.cpp similarity index 50% rename from src/ScopeOneToolPlugin.cpp rename to src/ToolRegistry.cpp index bfd603d..dfb4cef 100644 --- a/src/ScopeOneToolPlugin.cpp +++ b/src/ToolRegistry.cpp @@ -1,13 +1,12 @@ -#include "ScopeOneToolPlugin.h" -#include "scopeone/PluginManifest.h" +#include "ToolRegistry.h" + +#include "scopeone/ScopeOneCore.h" #include +#include #include -#include #include -#include #include -#include #include namespace scopeone::ui @@ -15,7 +14,7 @@ namespace scopeone::ui struct ToolRegistry::Entry { ToolDescriptor descriptor; - Factory factory; + ToolFactory factory; QAction* action{nullptr}; QPointer instance; }; @@ -23,106 +22,21 @@ namespace scopeone::ui ToolRegistry::ToolRegistry(ScopeOneToolContext& context) : m_context(context) { - } - - ToolRegistry::~ToolRegistry() - { - for (const auto& entry : m_entries) + for (ToolDefinition& tool : registeredTools()) { - delete entry->instance; + auto entry = std::make_unique(); + entry->descriptor = std::move(tool.descriptor); + entry->factory = std::move(tool.create); + m_entries.push_back(std::move(entry)); } } - bool ToolRegistry::registerTool(const ToolDescriptor& descriptor, Factory factory) + ToolRegistry::~ToolRegistry() { - const QString id = descriptor.id.trimmed(); for (const auto& entry : m_entries) { - if (entry->descriptor.id == id) - { - return false; - } - } - if (id.isEmpty() || descriptor.name.trimmed().isEmpty() || !factory) - { - return false; - } - - auto entry = std::make_unique(); - entry->descriptor = descriptor; - entry->descriptor.id = id; - entry->factory = std::move(factory); - m_entries.push_back(std::move(entry)); - return true; - } - - QStringList ToolRegistry::loadPlugins(const QString& directoryPath) - { - QStringList errors; - for (const scopeone::core::DiscoveredPlugin& discovered : - scopeone::core::discoverPlugins(scopeone::core::PluginKind::Tool, - {directoryPath})) - { - const QFileInfo file(discovered.path); - if (!discovered.error.isEmpty()) - { - errors.append(QStringLiteral("%1: %2").arg(file.fileName(), discovered.error)); - continue; - } - auto loader = std::make_unique(discovered.path); - auto* plugin = qobject_cast(loader->instance()); - if (!plugin) - { - errors.append(QStringLiteral("%1: %2").arg(file.fileName(), loader->errorString())); - continue; - } - - const QList descriptors = plugin->tools(); - QSet ids; - bool valid = !descriptors.isEmpty(); - for (const ToolDescriptor& descriptor : descriptors) - { - const QString id = descriptor.id.trimmed(); - if (id.isEmpty() - || descriptor.name.trimmed().isEmpty() - || ids.contains(id)) - { - valid = false; - break; - } - ids.insert(id); - } - if (!valid) - { - errors.append(QStringLiteral("%1: invalid or duplicate tool id").arg(file.fileName())); - loader->unload(); - continue; - } - - const size_t entryCount = m_entries.size(); - for (const ToolDescriptor& descriptor : descriptors) - { - const QString id = descriptor.id.trimmed(); - if (!registerTool(descriptor, [plugin, id](ScopeOneToolContext& context, - QWidget* parent) - { - return plugin->createTool(id, context, parent); - })) - { - m_entries.resize(entryCount); - errors.append(QStringLiteral("%1: tool id conflicts with an existing tool") - .arg(file.fileName())); - loader->unload(); - break; - } - } - if (m_entries.size() == entryCount) - { - continue; - } - m_pluginLoaders.push_back(std::move(loader)); + delete entry->instance; } - return errors; } void ToolRegistry::populateMenu(QMenu* menu, QWidget* parent) @@ -137,11 +51,12 @@ namespace scopeone::ui target = categories.value(category); if (!target) { - target = menu->addMenu(category); + target = menu->addMenu(QCoreApplication::translate("Tools", category.toUtf8().constData())); categories.insert(category, target); } } - entry->action = target->addAction(entry->descriptor.name); + entry->action = target->addAction( + QCoreApplication::translate("Tools", entry->descriptor.name.toUtf8().constData())); const QString id = entry->descriptor.id; QObject::connect(entry->action, &QAction::triggered, parent, [this, id, parent]() { openTool(id, parent); }); diff --git a/src/ScopeOneToolPlugin.h b/src/ToolRegistry.h similarity index 63% rename from src/ScopeOneToolPlugin.h rename to src/ToolRegistry.h index 3cc019a..9d9338c 100644 --- a/src/ScopeOneToolPlugin.h +++ b/src/ToolRegistry.h @@ -1,29 +1,24 @@ #pragma once -#include "scopeone/ToolPlugin.h" +#include "tools/ToolContext.h" -#include #include #include #include class QAction; class QMenu; -class QPluginLoader; class QWidget; namespace scopeone::ui { + // The Tools menu and the open tool windows, for the tools registered in src/tools class ToolRegistry { public: - using Factory = std::function; - explicit ToolRegistry(ScopeOneToolContext& context); ~ToolRegistry(); - bool registerTool(const ToolDescriptor& descriptor, Factory factory); - QStringList loadPlugins(const QString& directoryPath); void populateMenu(QMenu* menu, QWidget* parent); void setEnabled(bool enabled); void updateActions(); @@ -34,7 +29,6 @@ namespace scopeone::ui ScopeOneToolContext& m_context; std::vector> m_entries; - std::vector> m_pluginLoaders; bool m_enabled{true}; }; } diff --git a/src/tools/ToolCatalog.cpp b/src/tools/ToolCatalog.cpp new file mode 100644 index 0000000..c9704b9 --- /dev/null +++ b/src/tools/ToolCatalog.cpp @@ -0,0 +1,60 @@ +#include "tools/ToolContext.h" + +#include + +namespace scopeone::ui +{ + namespace + { + // Built on first use, so a tool registering from the static initialization of any source + // file finds it ready whatever the order of initialization is + std::vector& catalog() + { + static std::vector tools; + return tools; + } + + QString sortKey(const QString& text) + { + return QString(text).remove(QLatin1Char('&')); + } + } + + bool registerToolDefinition(ToolDefinition definition) + { + const QString id = definition.descriptor.id.trimmed(); + std::vector& tools = catalog(); + const bool duplicate = std::any_of(tools.begin(), tools.end(), [&id](const ToolDefinition& tool) + { + return tool.descriptor.id == id; + }); + if (id.isEmpty() || definition.descriptor.name.trimmed().isEmpty() || !definition.create || duplicate) + { + return false; + } + definition.descriptor.id = id; + tools.push_back(std::move(definition)); + return true; + } + + std::vector registeredTools() + { + std::vector tools = catalog(); + std::stable_sort(tools.begin(), tools.end(), [](const ToolDefinition& left, const ToolDefinition& right) + { + const QString leftCategory = sortKey(left.descriptor.category); + const QString rightCategory = sortKey(right.descriptor.category); + if (leftCategory.isEmpty() != rightCategory.isEmpty()) + { + return leftCategory.isEmpty(); + } + const int category = leftCategory.compare(rightCategory, Qt::CaseInsensitive); + if (category != 0) + { + return category < 0; + } + return sortKey(left.descriptor.name).compare(sortKey(right.descriptor.name), Qt::CaseInsensitive) < 0; + }); + return tools; + } +} diff --git a/src/tools/ToolContext.h b/src/tools/ToolContext.h new file mode 100644 index 0000000..b42c158 --- /dev/null +++ b/src/tools/ToolContext.h @@ -0,0 +1,78 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +class QWidget; + +namespace scopeone::core +{ + class ScopeOneCore; + class RecordingSessionData; +} + +// The tools of the Tools menu live in src/tools. A tool uses only the public Core headers and the +// context below, and registers itself with SCOPEONE_REGISTER_TOOL, so adding one changes no +// other file. +namespace scopeone::ui +{ + enum class ToolWindowMode + { + Modal, + ModelessSingleton + }; + + // The name and the category are untranslated source texts in the "Tools" context, marked with + // QT_TRANSLATE_NOOP, because tools register before the translations are loaded + struct ToolDescriptor + { + QString id; + QString name; + QString category; + ToolWindowMode windowMode{ToolWindowMode::ModelessSingleton}; + bool requiresCamera{false}; + }; + + // What the desktop offers a tool beside the Core + class ScopeOneToolContext + { + public: + virtual ~ScopeOneToolContext() = default; + + virtual scopeone::core::ScopeOneCore& core() const = 0; + virtual QString currentLayerKey() const = 0; + virtual void showLayers(const QStringList& layerKeys, bool sideBySide = false) = 0; + virtual void showToolStatus(const QString& message, int timeoutMs = 5000) = 0; + virtual void presentSession( + const std::shared_ptr& session, + const QString& title) = 0; + }; + + using ToolFactory = std::function; + + struct ToolDefinition + { + ToolDescriptor descriptor; + ToolFactory create; + }; + + // Every registered tool, in the order the menu lists them: tools without a category first, + // then by category and name + std::vector registeredTools(); + bool registerToolDefinition(ToolDefinition definition); +} + +// Registers the tool of a source file, when the program starts, with a descriptor and a factory: +// SCOPEONE_REGISTER_TOOL({id, name, category, mode, requiresCamera}, factory). Only these are +// stored then; the window is created when the user opens the tool. A file registers one tool. +#define SCOPEONE_REGISTER_TOOL(...) \ + namespace \ + { \ + [[maybe_unused]] const bool toolRegistered = \ + ::scopeone::ui::registerToolDefinition({__VA_ARGS__}); \ + } diff --git a/plugins/tools/ScanningDaqTool/DaqControlWidget.cpp b/src/tools/daq/DaqControlWidget.cpp similarity index 97% rename from plugins/tools/ScanningDaqTool/DaqControlWidget.cpp rename to src/tools/daq/DaqControlWidget.cpp index 2e32bd0..fb1c3ab 100644 --- a/plugins/tools/ScanningDaqTool/DaqControlWidget.cpp +++ b/src/tools/daq/DaqControlWidget.cpp @@ -1,6 +1,7 @@ #include "DaqControlWidget.h" -#include "scopeone/ToolPlugin.h" +#include "tools/ToolContext.h" +#include "scopeone/ScopeOneCore.h" #include #include @@ -27,7 +28,7 @@ #include #include -namespace scopeone::plugins +namespace scopeone::ui { namespace { @@ -70,7 +71,7 @@ namespace scopeone::plugins } } - DaqControlWidget::DaqControlWidget(scopeone::ui::ScopeOneToolCore* core, + DaqControlWidget::DaqControlWidget(scopeone::core::ScopeOneCore* core, QWidget* parent) : QWidget(parent) , m_core(core) @@ -235,14 +236,14 @@ namespace scopeone::plugins { m_core->stopDaqSession(m_activeDeviceId); }); - connect(m_core, &scopeone::ui::ScopeOneToolCore::daqStateChanged, + connect(m_core, &scopeone::core::ScopeOneCore::daqStateChanged, this, &DaqControlWidget::handleStateChanged); - connect(m_core, &scopeone::ui::ScopeOneToolCore::daqError, + connect(m_core, &scopeone::core::ScopeOneCore::daqError, this, [](const QString&, const QString& message) { qCritical().noquote() << message; }); - connect(m_core, &scopeone::ui::ScopeOneToolCore::daqInputDataReady, + connect(m_core, &scopeone::core::ScopeOneCore::daqInputDataReady, this, [this](const scopeone::core::DaqInputChunk& chunk) { if (chunk.deviceId != m_activeDeviceId) @@ -832,3 +833,14 @@ namespace scopeone::plugins } } } + +namespace scopeone::ui +{ + SCOPEONE_REGISTER_TOOL( + {QStringLiteral("scopeone.daq_control"), QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Scanning and DAQ Control")), + QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Acquisition")), ToolWindowMode::ModelessSingleton}, + [](ScopeOneToolContext& context, QWidget* parent) + { + return new DaqControlWidget(&context.core(), parent); + }) +} diff --git a/plugins/tools/ScanningDaqTool/DaqControlWidget.h b/src/tools/daq/DaqControlWidget.h similarity index 92% rename from plugins/tools/ScanningDaqTool/DaqControlWidget.h rename to src/tools/daq/DaqControlWidget.h index a1176d9..e1a071f 100644 --- a/plugins/tools/ScanningDaqTool/DaqControlWidget.h +++ b/src/tools/daq/DaqControlWidget.h @@ -15,19 +15,19 @@ class QSpinBox; class QTableWidget; class QWidget; -namespace scopeone::ui +namespace scopeone::core { - class ScopeOneToolCore; + class ScopeOneCore; } -namespace scopeone::plugins +namespace scopeone::ui { class DaqControlWidget final : public QWidget { Q_OBJECT public: - explicit DaqControlWidget(scopeone::ui::ScopeOneToolCore* core, + explicit DaqControlWidget(scopeone::core::ScopeOneCore* core, QWidget* parent = nullptr); private: @@ -45,7 +45,7 @@ namespace scopeone::plugins scopeone::core::DaqState state, const QString& message); - scopeone::ui::ScopeOneToolCore* m_core{nullptr}; + scopeone::core::ScopeOneCore* m_core{nullptr}; QComboBox* m_deviceCombo{nullptr}; QLabel* m_productLabel{nullptr}; QLabel* m_resourcesLabel{nullptr}; diff --git a/plugins/tools/ScanningDaqTool/SignalMonitorWidget.cpp b/src/tools/daq/SignalMonitorWidget.cpp similarity index 96% rename from plugins/tools/ScanningDaqTool/SignalMonitorWidget.cpp rename to src/tools/daq/SignalMonitorWidget.cpp index 83c66c6..4ff2215 100644 --- a/plugins/tools/ScanningDaqTool/SignalMonitorWidget.cpp +++ b/src/tools/daq/SignalMonitorWidget.cpp @@ -1,6 +1,7 @@ #include "SignalMonitorWidget.h" -#include "scopeone/ToolPlugin.h" +#include "tools/ToolContext.h" +#include "scopeone/ScopeOneCore.h" #include #include @@ -26,7 +27,7 @@ #include #include -namespace scopeone::plugins +namespace scopeone::ui { namespace { @@ -240,7 +241,7 @@ namespace scopeone::plugins double m_windowSeconds{10.0}; }; - SignalMonitorWidget::SignalMonitorWidget(scopeone::ui::ScopeOneToolCore* core, + SignalMonitorWidget::SignalMonitorWidget(scopeone::core::ScopeOneCore* core, scopeone::ui::ScopeOneToolContext& context, QWidget* parent) : QWidget(parent) @@ -359,16 +360,16 @@ namespace scopeone::plugins m_serpentineCheck->setEnabled(editable); m_mirrorHorizontalCheck->setEnabled(editable); }); - connect(m_core, &scopeone::ui::ScopeOneToolCore::signalTimeSeriesReady, + connect(m_core, &scopeone::core::ScopeOneCore::signalTimeSeriesReady, this, &SignalMonitorWidget::handleTimeSeries); - connect(m_core, &scopeone::ui::ScopeOneToolCore::signalSourceStateChanged, + connect(m_core, &scopeone::core::ScopeOneCore::signalSourceStateChanged, this, &SignalMonitorWidget::handleStateChanged); - connect(m_core, &scopeone::ui::ScopeOneToolCore::signalSourceError, + connect(m_core, &scopeone::core::ScopeOneCore::signalSourceError, this, [](const QString&, const QString& message) { qCritical().noquote() << message; }); - connect(m_core, &scopeone::ui::ScopeOneToolCore::staticFramePublished, + connect(m_core, &scopeone::core::ScopeOneCore::staticFramePublished, this, [this](const QString& sourceId, const QString&, @@ -382,9 +383,9 @@ namespace scopeone::plugins } m_scanLayerShown = true; m_context.showLayers({ - scopeone::ui::ScopeOneToolCore::staticLayerKey(sourceId)}); + scopeone::core::ScopeOneCore::staticLayerKey(sourceId)}); }); - connect(m_core, &scopeone::ui::ScopeOneToolCore::scanImageSessionReady, + connect(m_core, &scopeone::core::ScopeOneCore::scanImageSessionReady, this, [this](const std::shared_ptr& session) { m_statusLabel->setText( @@ -698,3 +699,14 @@ namespace scopeone::plugins return values; } } + +namespace scopeone::ui +{ + SCOPEONE_REGISTER_TOOL( + {QStringLiteral("scopeone.signal_monitor"), QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Signal Monitor")), + QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Acquisition")), ToolWindowMode::ModelessSingleton}, + [](ScopeOneToolContext& context, QWidget* parent) + { + return new SignalMonitorWidget(&context.core(), context, parent); + }) +} diff --git a/plugins/tools/ScanningDaqTool/SignalMonitorWidget.h b/src/tools/daq/SignalMonitorWidget.h similarity index 90% rename from plugins/tools/ScanningDaqTool/SignalMonitorWidget.h rename to src/tools/daq/SignalMonitorWidget.h index e4f43b3..5d2bb21 100644 --- a/plugins/tools/ScanningDaqTool/SignalMonitorWidget.h +++ b/src/tools/daq/SignalMonitorWidget.h @@ -1,7 +1,7 @@ #pragma once #include "scopeone/SignalSource.h" -#include "scopeone/ToolPlugin.h" +#include "tools/ToolContext.h" #include #include @@ -14,12 +14,12 @@ class QLabel; class QPushButton; class QSpinBox; -namespace scopeone::ui +namespace scopeone::core { - class ScopeOneToolCore; + class ScopeOneCore; } -namespace scopeone::plugins +namespace scopeone::ui { class SignalTracePlot; @@ -28,7 +28,7 @@ namespace scopeone::plugins Q_OBJECT public: - explicit SignalMonitorWidget(scopeone::ui::ScopeOneToolCore* core, + explicit SignalMonitorWidget(scopeone::core::ScopeOneCore* core, scopeone::ui::ScopeOneToolContext& context, QWidget* parent = nullptr); @@ -44,7 +44,7 @@ namespace scopeone::plugins scopeone::core::SignalSourceDescriptor currentDescriptor() const; QVariantMap sourceSettings() const; - scopeone::ui::ScopeOneToolCore* m_core{nullptr}; + scopeone::core::ScopeOneCore* m_core{nullptr}; scopeone::ui::ScopeOneToolContext& m_context; QComboBox* m_sourceCombo{nullptr}; QFormLayout* m_sourceForm{nullptr}; diff --git a/plugins/tools/DhmTool/DhmToolPlugin.cpp b/src/tools/dhm/DhmTool.cpp similarity index 78% rename from plugins/tools/DhmTool/DhmToolPlugin.cpp rename to src/tools/dhm/DhmTool.cpp index e42e17d..f6746f9 100644 --- a/plugins/tools/DhmTool/DhmToolPlugin.cpp +++ b/src/tools/dhm/DhmTool.cpp @@ -1,11 +1,14 @@ -#include "DhmReconstruction.h" +#include "scopeone/DhmReconstruction.h" -#include "scopeone/ToolFrameStream.h" -#include "scopeone/ToolPlugin.h" -#include "scopeone/ToolTask.h" -#include "scopeone/ToolPlugin.h" +#include "tools/ToolContext.h" +#include "scopeone/ScopeOneCore.h" #include "scopeone/ImageSceneModel.h" +#include +#include +#include +#include + #include #include #include @@ -24,10 +27,10 @@ #include #include -#include -#include #include +#include +#include #include #include #include @@ -35,9 +38,18 @@ namespace { using scopeone::core::ImageFrame; - using scopeone::dhm::DhmOutputMode; - using scopeone::dhm::DhmParameters; - using scopeone::dhm::DhmRoiMode; + using scopeone::core::DhmOutputMode; + using scopeone::core::DhmParameters; + using scopeone::core::DhmRoiMode; + + // The planar 256-entry ImageJ table the preview also uses + QByteArray jetLookupTable() + { + QFile file(QStringLiteral(":/luts/Jet.lut")); + QByteArray table = file.open(QIODevice::ReadOnly) ? file.readAll() : QByteArray(); + table.resize(3 * 256); + return table; + } class SpectrumView final : public QWidget { @@ -60,23 +72,21 @@ namespace return; } - const cv::Mat source(frame.height, - frame.width, - CV_16UC1, - const_cast(reinterpret_cast( - frame.bytes.constData())), - static_cast(frame.stride)); - cv::Mat eightBit; - source.convertTo(eightBit, CV_8U, 1.0 / 256.0); - cv::Mat colored; - cv::applyColorMap(eightBit, colored, cv::COLORMAP_JET); - cv::cvtColor(colored, colored, cv::COLOR_BGR2RGB); - m_image = QImage(colored.data, - colored.cols, - colored.rows, - static_cast(colored.step), - QImage::Format_RGB888) - .copy(); + // The spectrum is shown in the Jet lookup table of the preview, the top 8 bits of each sample + static const QByteArray lut = jetLookupTable(); + m_image = QImage(frame.width, frame.height, QImage::Format_RGB888); + for (int y = 0; y < frame.height; ++y) + { + const auto* source = reinterpret_cast(frame.bytes.constData() + y * frame.stride); + uchar* target = m_image.scanLine(y); + for (int x = 0; x < frame.width; ++x) + { + const int index = source[x] >> 8; + target[3 * x] = static_cast(lut[index]); + target[3 * x + 1] = static_cast(lut[256 + index]); + target[3 * x + 2] = static_cast(lut[512 + index]); + } + } update(); } @@ -187,6 +197,14 @@ namespace std::function m_selectionChanged; }; + // What a reconstruction on the thread pool hands back to the window + struct ReconstructionOutcome + { + scopeone::core::DhmResult result; + QString error; + bool canceled{false}; + }; + class DhmTool final : public QWidget { public: @@ -242,7 +260,19 @@ namespace statusLayout->addWidget(m_status); layout->addLayout(statusLayout); - m_stream = new scopeone::ui::ScopeOneToolFrameStream(m_context.core(), this); + connect(&m_watcher, &QFutureWatcher::progressValueChanged, this, + [this](int percent) + { + if (m_showProgress) + { + m_progress->setValue(percent); + m_status->setText(QStringLiteral("Reconstructing %1%").arg(percent)); + } + }); + connect(&m_watcher, &QFutureWatcher::finished, this, + [this]() { reconstructionFinished(); }); + connect(&m_context.core(), &scopeone::core::ScopeOneCore::previewRawFrameReady, this, + [this](const ImageFrame& frame) { acceptLiveFrame(frame); }); refreshLayerComboBox(); connect(m_context.core().imageSceneModel(), @@ -261,8 +291,7 @@ namespace [this]() { const QString layerKey = m_layerComboBox->currentData().toString(); - m_sourceId = scopeone::ui::ScopeOneToolCore::sourceIdFromLayerKey(layerKey); - m_stream->setSourceId(m_sourceId); + m_sourceId = scopeone::core::ScopeOneCore::sourceIdFromLayerKey(layerKey); startReconstruction(); }); @@ -282,16 +311,20 @@ namespace [this]() { m_liveCheckBox->setChecked(false); - m_task->cancel(); + m_pendingFrame = {}; + if (m_cancel) + { + m_cancel->store(true); + } }); connect(m_liveCheckBox, &QCheckBox::toggled, this, [this](bool enabled) { - m_stream->setEnabled(enabled); if (!enabled) { - m_status->setText(m_task ? QStringLiteral("Finishing current frame") - : QStringLiteral("Ready")); + m_pendingFrame = {}; + m_status->setText(m_running ? QStringLiteral("Finishing current frame") + : QStringLiteral("Ready")); return; } const QString layerKey = m_layerComboBox->currentData().toString(); @@ -301,18 +334,7 @@ namespace m_context.currentLayerKey()); if (frame.isValid()) { - m_sourceId = scopeone::ui::ScopeOneToolCore::sourceIdFromLayerKey(layerKey); - m_stream->setSourceId(m_sourceId); - queueFrame(frame); - } - }); - connect(m_stream, &scopeone::ui::ScopeOneToolFrameStream::frameReady, this, - [this](const ImageFrame& frame) - { - if (m_liveCheckBox->isChecked()) - { - m_sourceId = frame.cameraId; - m_stream->setSourceId(m_sourceId); + m_sourceId = scopeone::core::ScopeOneCore::sourceIdFromLayerKey(layerKey); queueFrame(frame); } }); @@ -321,12 +343,34 @@ namespace if (frame.isValid()) { m_sourceId = frame.cameraId; - m_stream->setSourceId(m_sourceId); queueFrame(frame); } } + // A reconstruction still running is canceled and awaited, so no result arrives after the window is gone + ~DhmTool() override + { + if (m_cancel) + { + m_cancel->store(true); + } + m_watcher.waitForFinished(); + } + private: + // Live frames of the shown source. While a frame is reconstructed only the newest one waits, so the + // reconstruction keeps up with the camera by dropping the frames in between. + void acceptLiveFrame(const ImageFrame& frame) + { + if (!m_liveCheckBox->isChecked() || !frame.isValid() + || (!m_sourceId.isEmpty() && frame.cameraId != m_sourceId)) + { + return; + } + m_sourceId = frame.cameraId; + queueFrame(frame); + } + QFormLayout* createSidebandForm() { auto* form = new QFormLayout(); @@ -509,8 +553,7 @@ namespace m_status->setText(QStringLiteral("No hologram layer available")); return; } - m_sourceId = scopeone::ui::ScopeOneToolCore::sourceIdFromLayerKey(layerKey); - m_stream->setSourceId(m_sourceId); + m_sourceId = scopeone::core::ScopeOneCore::sourceIdFromLayerKey(layerKey); queueFrame(input); } @@ -536,35 +579,45 @@ namespace void queueFrame(const ImageFrame& input) { + // Starting a new task while one ran replaced the watched future, so the old task ran on unseen and + // could no longer be canceled; a frame that arrives meanwhile waits until the current one finishes + if (m_processing) + { + m_pendingFrame = input; + return; + } showPreview(m_inputPreview, input); - m_stream->setProcessing(true); + m_processing = true; // While the reconstruction follows the stream the controls stay as they are, and the progress of // each frame is not shown, as both would flicker at the rate of the frames const bool live = m_liveCheckBox->isChecked(); const DhmParameters params = parameters(); - auto result = std::make_shared(); - m_task = new scopeone::ui::ScopeOneToolTask( - [input, params, result](const std::atomic_bool& cancel, - const std::function& progress) + m_cancel = std::make_shared(false); + m_showProgress = !live; + m_running = true; + const auto cancel = m_cancel; + m_watcher.setFuture(QtConcurrent::run( + [input, params, cancel](QPromise& promise) { - *result = scopeone::dhm::reconstruct(input, params, cancel, progress); - }, - this); - if (!live) - { - connect(m_task, &scopeone::ui::ScopeOneToolTask::progressChanged, this, - [this](int percent) - { - m_progress->setValue(percent); - m_status->setText(QStringLiteral("Reconstructing %1%%").arg(percent)); - }); - } - connect(m_task, &scopeone::ui::ScopeOneToolTask::finished, this, - [this, result]() { publishResult(*result); }); - connect(m_task, &scopeone::ui::ScopeOneToolTask::canceled, this, - [this]() { finishTask(QStringLiteral("Canceled")); }); - connect(m_task, &scopeone::ui::ScopeOneToolTask::failed, this, - [this](const QString& message) { finishTask(message); }); + promise.setProgressRange(0, 100); + ReconstructionOutcome outcome; + try + { + outcome.result = scopeone::core::reconstructDhm( + input, params, *cancel, + [&promise](int percent) { promise.setProgressValue(std::clamp(percent, 0, 100)); }); + } + catch (const std::exception& exception) + { + outcome.error = QString::fromLocal8Bit(exception.what()); + } + catch (...) + { + outcome.error = QStringLiteral("Reconstruction failed"); + } + outcome.canceled = cancel->load(); + promise.addResult(std::move(outcome)); + })); if (!live) { @@ -575,10 +628,28 @@ namespace m_status->setText(QStringLiteral("Reconstructing 0%")); } m_cancelButton->setEnabled(true); - m_task->start(); } - void publishResult(const scopeone::dhm::DhmResult& result) + void reconstructionFinished() + { + m_running = false; + const QFuture future = m_watcher.future(); + const ReconstructionOutcome outcome = future.resultCount() > 0 ? future.result() : ReconstructionOutcome{}; + if (!outcome.error.isEmpty()) + { + finishTask(outcome.error); + } + else if (outcome.canceled) + { + finishTask(QStringLiteral("Canceled")); + } + else + { + publishResult(outcome.result); + } + } + + void publishResult(const scopeone::core::DhmResult& result) { if (!result.outputFrame.isValid()) { @@ -609,11 +680,11 @@ namespace return; } - const QString outputLayer = scopeone::ui::ScopeOneToolCore::toolLayerKey(stored.cameraId); + const QString outputLayer = scopeone::core::ScopeOneCore::toolLayerKey(stored.cameraId); if (m_shownInputSourceId != m_sourceId || m_shownOutputLayerKey != outputLayer) { QStringList layers; - const QString rawLayer = scopeone::ui::ScopeOneToolCore::rawLayerKey(m_sourceId); + const QString rawLayer = scopeone::core::ScopeOneCore::rawLayerKey(m_sourceId); if (m_context.core().graphFrame(rawLayer).isValid()) { layers.append(rawLayer); @@ -651,16 +722,13 @@ namespace m_autoDetectButton->setEnabled(true); m_layerComboBox->setEnabled(true); m_cancelButton->setEnabled(false); - if (m_task) - { - m_task->deleteLater(); - m_task = nullptr; - } - if (!m_liveCheckBox->isChecked()) + m_processing = false; + if (m_pendingFrame.isValid()) { - m_stream->clearPendingFrame(); + const ImageFrame next = m_pendingFrame; + m_pendingFrame = {}; + queueFrame(next); } - m_stream->setProcessing(false); } scopeone::ui::ScopeOneToolContext& m_context; @@ -688,39 +756,25 @@ namespace QCheckBox* m_liveCheckBox{nullptr}; QProgressBar* m_progress{nullptr}; QLabel* m_status{nullptr}; - scopeone::ui::ScopeOneToolTask* m_task{nullptr}; - scopeone::ui::ScopeOneToolFrameStream* m_stream{nullptr}; + QFutureWatcher m_watcher; + std::shared_ptr m_cancel; + bool m_running{false}; + bool m_showProgress{false}; + bool m_processing{false}; + ImageFrame m_pendingFrame; QString m_sourceId; QString m_shownInputSourceId; QString m_shownOutputLayerKey; }; +} - class DhmToolPlugin final : public QObject, - public scopeone::ui::ScopeOneToolPlugin - { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneToolPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::ui::ScopeOneToolPlugin) - - public: - QList tools() const override - { - return {{QStringLiteral("scopeone.dhm_reconstruction"), - QStringLiteral("DHM Reconstruction"), - QStringLiteral("Reconstruction"), - scopeone::ui::ToolWindowMode::ModelessSingleton, - false}}; - } - - QWidget* createTool(const QString& toolId, - scopeone::ui::ScopeOneToolContext& context, - QWidget* parent) override +namespace scopeone::ui +{ + SCOPEONE_REGISTER_TOOL( + {QStringLiteral("scopeone.dhm_reconstruction"), QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "DHM Reconstruction")), + QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Reconstruction")), ToolWindowMode::ModelessSingleton}, + [](ScopeOneToolContext& context, QWidget* parent) { - return toolId == QStringLiteral("scopeone.dhm_reconstruction") - ? new DhmTool(context, parent) - : nullptr; - } - }; + return new DhmTool(context, parent); + }) } - -#include "DhmToolPlugin.moc" diff --git a/src/StageMosaicDialog.cpp b/src/tools/mosaic/StageMosaicDialog.cpp similarity index 95% rename from src/StageMosaicDialog.cpp rename to src/tools/mosaic/StageMosaicDialog.cpp index c4932d5..31a68bd 100644 --- a/src/StageMosaicDialog.cpp +++ b/src/tools/mosaic/StageMosaicDialog.cpp @@ -1,6 +1,6 @@ #include "StageMosaicDialog.h" -#include "ScopeOneToolPlugin.h" +#include "tools/ToolContext.h" #include "scopeone/ImageSceneModel.h" #include "scopeone/ScopeOneCore.h" @@ -255,3 +255,14 @@ namespace scopeone::ui m_returnToStartCheckBox->setEnabled(!running); } } + +namespace scopeone::ui +{ + SCOPEONE_REGISTER_TOOL( + {QStringLiteral("scopeone.stage_mosaic"), QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Stage &Mosaic...")), {}, + ToolWindowMode::ModelessSingleton}, + [](ScopeOneToolContext& context, QWidget* parent) + { + return new StageMosaicDialog(&context.core(), context, parent); + }) +} diff --git a/src/StageMosaicDialog.h b/src/tools/mosaic/StageMosaicDialog.h similarity index 100% rename from src/StageMosaicDialog.h rename to src/tools/mosaic/StageMosaicDialog.h diff --git a/src/ParticleDetectionDialog.cpp b/src/tools/particles/ParticleDetectionDialog.cpp similarity index 95% rename from src/ParticleDetectionDialog.cpp rename to src/tools/particles/ParticleDetectionDialog.cpp index 19e9595..a9197c3 100644 --- a/src/ParticleDetectionDialog.cpp +++ b/src/tools/particles/ParticleDetectionDialog.cpp @@ -1,6 +1,6 @@ #include "ParticleDetectionDialog.h" -#include "ScopeOneToolPlugin.h" +#include "tools/ToolContext.h" #include "scopeone/ImageSceneModel.h" #include "scopeone/ScopeOneCore.h" @@ -250,3 +250,15 @@ namespace scopeone::ui : tr("Detected %1 particle(s)").arg(result.particles.size())); } } + +namespace scopeone::ui +{ + SCOPEONE_REGISTER_TOOL( + {QStringLiteral("scopeone.particle_detection"), + QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "&Particle Detection...")), {}, + ToolWindowMode::ModelessSingleton}, + [](ScopeOneToolContext& context, QWidget* parent) + { + return new ParticleDetectionDialog(&context.core(), context, parent); + }) +} diff --git a/src/ParticleDetectionDialog.h b/src/tools/particles/ParticleDetectionDialog.h similarity index 100% rename from src/ParticleDetectionDialog.h rename to src/tools/particles/ParticleDetectionDialog.h diff --git a/src/CameraScaleDialog.cpp b/src/tools/scale/CameraScaleDialog.cpp similarity index 90% rename from src/CameraScaleDialog.cpp rename to src/tools/scale/CameraScaleDialog.cpp index fb68cd1..4b4ccef 100644 --- a/src/CameraScaleDialog.cpp +++ b/src/tools/scale/CameraScaleDialog.cpp @@ -1,6 +1,6 @@ #include "CameraScaleDialog.h" -#include "ScopeOneToolPlugin.h" +#include "tools/ToolContext.h" #include "scopeone/ImageSceneModel.h" #include "scopeone/ScopeOneCore.h" @@ -103,3 +103,14 @@ namespace scopeone::ui } } } + +namespace scopeone::ui +{ + SCOPEONE_REGISTER_TOOL( + {QStringLiteral("scopeone.scale"), QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "&Scale...")), {}, + ToolWindowMode::Modal, true}, + [](ScopeOneToolContext& context, QWidget* parent) + { + return new CameraScaleDialog(&context.core(), parent); + }) +} diff --git a/src/CameraScaleDialog.h b/src/tools/scale/CameraScaleDialog.h similarity index 100% rename from src/CameraScaleDialog.h rename to src/tools/scale/CameraScaleDialog.h From ad005e6d6aa413e2914ca7bf3acb56972b3c3a9b Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Wed, 4 Nov 2026 21:43:19 +0100 Subject: [PATCH 02/49] Move DAQ and PMT into drivers with a lossless stream ring and enforce the driver SDK boundary --- .gitignore | 2 + CMakeLists.txt | 10 +- CONTRIBUTING.md | 15 +- README.md | 4 +- ScopeOneCore/CMakeLists.txt | 88 ++- ScopeOneCore/DRIVERHOST_PROTOCOL.md | 60 +- ScopeOneCore/README.md | 36 +- ScopeOneCore/cmake/ScopeOnePlugin.cmake | 50 +- .../include/scopeone/PluginManifest.h | 27 +- .../include/scopeone/ProcessingPlugin.h | 4 +- .../include/scopeone/ScanImageAssembler.h | 4 +- ScopeOneCore/include/scopeone/ScopeOneCore.h | 3 + .../include/scopeone/scopeone_core_export.h | 6 +- ScopeOneCore/internal/DaqDeviceManager.h | 34 +- ScopeOneCore/internal/DriverHostProtocol.h | 28 +- ScopeOneCore/internal/HardwareStreamCodec.h | 30 + ScopeOneCore/internal/NIDaqmx.h | 15 - ScopeOneCore/internal/PtuFile.h | 2 +- ScopeOneCore/internal/SignalSourceManager.h | 40 +- ScopeOneCore/internal/SimulatedPmt.h | 15 - ScopeOneCore/internal/SimulatedPmtSource.h | 56 -- ScopeOneCore/internal/StreamRing.h | 83 +++ .../include/scopeone/CameraProvider.h | 0 .../{ => sdk}/include/scopeone/DaqDevice.h | 23 - .../sdk/include/scopeone/DaqProvider.h | 33 ++ .../include/scopeone/Driver.h} | 11 +- .../include/scopeone/DriverSDK.h} | 7 +- .../include/scopeone/HardwareCapabilities.h | 0 .../include/scopeone/HardwareProvider.h | 0 .../include/scopeone/HardwareTypes.h | 0 .../{ => sdk}/include/scopeone/ImageFrame.h | 0 .../{ => sdk}/include/scopeone/SignalSource.h | 24 - .../include/scopeone/SignalSourceProvider.h | 36 ++ .../include/scopeone/scopeone_sdk_export.h | 2 +- .../{ => sdk}/src/HardwareProvider.cpp | 4 + ScopeOneCore/sdk/src/SignalSource.cpp | 143 +++++ ScopeOneCore/src/DaqDeviceManager.cpp | 172 +++--- ScopeOneCore/src/DriverHostMain.cpp | 317 +++++++++- ScopeOneCore/src/DriverHostProviderProxy.cpp | 303 +++++++++- ScopeOneCore/src/HardwareStreamCodec.cpp | 546 ++++++++++++++++++ ScopeOneCore/src/PluginManifest.cpp | 21 +- ScopeOneCore/src/ScopeOneCore.cpp | 22 +- ScopeOneCore/src/SignalSourceManager.cpp | 280 +++++---- ScopeOneCore/src/SimulatedPmt.cpp | 141 ----- ScopeOneCore/src/StreamRing.cpp | 210 +++++++ drivers/CMakeLists.txt | 3 + drivers/ExampleCamera/CMakeLists.txt | 8 + .../ExampleCamera/ExampleCameraDriver.cpp | 18 +- .../ExampleCamera/TestPatterns.cpp | 0 .../ExampleCamera/TestPatterns.h | 0 .../ExampleCamera/driver.json | 2 +- drivers/ExamplePmt/CMakeLists.txt | 6 + .../ExamplePmt/ExamplePmtDriver.cpp | 427 ++++++++++---- drivers/ExamplePmt/driver.json | 9 + drivers/NIDaqmx/CMakeLists.txt | 6 + .../NIDaqmx/NIDaqmxDriver.cpp | 196 +++++-- drivers/NIDaqmx/driver.json | 9 + drivers/README.md | 93 +++ plugins/CMakeLists.txt | 2 +- plugins/README.md | 74 +-- plugins/hardware/CMakeLists.txt | 1 - plugins/hardware/ExampleCamera/CMakeLists.txt | 8 - src/MainWindow.cpp | 2 +- src/PluginManagerDialog.cpp | 82 ++- src/PluginManagerDialog.h | 4 +- src/main.cpp | 4 +- src/tools/daq/DaqControlWidget.cpp | 37 +- src/tools/daq/DaqControlWidget.h | 1 + src/tools/daq/SignalMonitorWidget.cpp | 2 + 69 files changed, 2939 insertions(+), 962 deletions(-) create mode 100644 ScopeOneCore/internal/HardwareStreamCodec.h delete mode 100644 ScopeOneCore/internal/NIDaqmx.h delete mode 100644 ScopeOneCore/internal/SimulatedPmt.h delete mode 100644 ScopeOneCore/internal/SimulatedPmtSource.h create mode 100644 ScopeOneCore/internal/StreamRing.h rename ScopeOneCore/{ => sdk}/include/scopeone/CameraProvider.h (100%) rename ScopeOneCore/{ => sdk}/include/scopeone/DaqDevice.h (82%) create mode 100644 ScopeOneCore/sdk/include/scopeone/DaqProvider.h rename ScopeOneCore/{include/scopeone/DriverHostProviderPlugin.h => sdk/include/scopeone/Driver.h} (51%) rename ScopeOneCore/{include/scopeone/HardwarePluginSDK.h => sdk/include/scopeone/DriverSDK.h} (56%) rename ScopeOneCore/{ => sdk}/include/scopeone/HardwareCapabilities.h (100%) rename ScopeOneCore/{ => sdk}/include/scopeone/HardwareProvider.h (100%) rename ScopeOneCore/{ => sdk}/include/scopeone/HardwareTypes.h (100%) rename ScopeOneCore/{ => sdk}/include/scopeone/ImageFrame.h (100%) rename ScopeOneCore/{ => sdk}/include/scopeone/SignalSource.h (85%) create mode 100644 ScopeOneCore/sdk/include/scopeone/SignalSourceProvider.h rename ScopeOneCore/{ => sdk}/include/scopeone/scopeone_sdk_export.h (90%) rename ScopeOneCore/{ => sdk}/src/HardwareProvider.cpp (74%) create mode 100644 ScopeOneCore/sdk/src/SignalSource.cpp create mode 100644 ScopeOneCore/src/HardwareStreamCodec.cpp delete mode 100644 ScopeOneCore/src/SimulatedPmt.cpp create mode 100644 ScopeOneCore/src/StreamRing.cpp create mode 100644 drivers/CMakeLists.txt create mode 100644 drivers/ExampleCamera/CMakeLists.txt rename plugins/hardware/ExampleCamera/ExampleCameraPlugin.cpp => drivers/ExampleCamera/ExampleCameraDriver.cpp (97%) rename {plugins/hardware => drivers}/ExampleCamera/TestPatterns.cpp (100%) rename {plugins/hardware => drivers}/ExampleCamera/TestPatterns.h (100%) rename plugins/hardware/ExampleCamera/plugin.json => drivers/ExampleCamera/driver.json (87%) create mode 100644 drivers/ExamplePmt/CMakeLists.txt rename ScopeOneCore/src/SimulatedPmtSource.cpp => drivers/ExamplePmt/ExamplePmtDriver.cpp (53%) create mode 100644 drivers/ExamplePmt/driver.json create mode 100644 drivers/NIDaqmx/CMakeLists.txt rename ScopeOneCore/src/NIDaqmx.cpp => drivers/NIDaqmx/NIDaqmxDriver.cpp (86%) create mode 100644 drivers/NIDaqmx/driver.json create mode 100644 drivers/README.md delete mode 100644 plugins/hardware/CMakeLists.txt delete mode 100644 plugins/hardware/ExampleCamera/CMakeLists.txt diff --git a/.gitignore b/.gitignore index 1507cac..3c7c50e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ !/ScopeOneCore/** !/plugins/ !/plugins/** +!/drivers/ +!/drivers/** !/ScopeOneCuda/ !/ScopeOneCuda/** !/ScopeOneInference/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 84f0673..ad163a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -236,6 +236,7 @@ else () LIST_DIRECTORIES false "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/ScopeOne_DriverHost" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore.so*" + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK.so*" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter.so*" ) endif () @@ -257,6 +258,7 @@ if (UNIX AND NOT APPLE) file(GLOB SCOPEONE_CORE_SHARED_LIBRARIES LIST_DIRECTORIES false "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore.so*" + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK.so*" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter.so*" ) install(FILES @@ -265,15 +267,15 @@ if (UNIX AND NOT APPLE) ) endif () -foreach (_plugin_kind IN ITEMS processing hardware) - set(_plugin_dir "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/plugins/${_plugin_kind}") +foreach (_plugin_kind IN ITEMS plugins/processing drivers) + set(_plugin_dir "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/${_plugin_kind}") if (EXISTS "${_plugin_dir}") add_custom_command(TARGET ScopeOne POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${_plugin_dir}" - "$/plugins/${_plugin_kind}" + "$/${_plugin_kind}" ) - install(DIRECTORY "${_plugin_dir}/" DESTINATION "plugins/${_plugin_kind}") + install(DIRECTORY "${_plugin_dir}/" DESTINATION "${_plugin_kind}") endif () endforeach () diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cf89f70..6b849fc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,21 +13,22 @@ disagree in a specific case, follow the reason and say so in the review. | Core | `ScopeOneCore` | `scopeone::core`, `core::internal`, `core::imageops` | Qt Core, OpenCV, MMCore, ScopeWriter. No Qt Widgets. | | Application | `src` | `scopeone::ui` | Core, Qt Widgets | | MCP server | `src/ScopeOneMcpServer.cpp` | | Qt Core, Qt Network and the Local API protocol. Not Core. | -| Plugins | `plugins`, `ScopeOneCuda`, `ScopeOneInference` | `scopeone::plugins::cuda` for CUDA, `scopeone::inference` for ONNX, their own for hardware plugins | `scopeone::HardwarePluginSDK` for hardware plugins, `scopeone::ScopeOneCore` for the CUDA and ONNX plugins | +| Drivers | `drivers` | their own | `scopeone::DriverSDK` | +| Plugins | `plugins`, `ScopeOneCuda`, `ScopeOneInference` | `scopeone::plugins::cuda` for CUDA, `scopeone::inference` for ONNX | `scopeone::ScopeOneCore` | | Python package | `ScopeOneCore/python/scopeone` | `scopeone` | the Local API protocol | -A lower layer never knows about a higher one. Core does not include from `src`, and plugins do +A lower layer never knows about a higher one. Core does not include from `src`, and drivers and plugins do not include Core internals. This keeps each part buildable and usable without the ones above it. **Entry points.** Code outside Core enters through the public headers in `ScopeOneCore/include/scopeone` and the `ScopeOneCore` facade. `ScopeOneCore/internal` is for Core -alone. Plugins see only the SDK headers. +alone. Drivers see only `ScopeOneCore/sdk`, enforced by the build. A narrow entry point is what lets Core change its insides without breaking callers. **Decide the layer first.** Before writing a feature, decide where it belongs. Anything that must work without a window (acquisition, hardware, processing, recording, data) goes into Core and is reachable through the Local API before it gets a widget. Anything that only changes what -is shown goes into `src`. Hardware and optional processing go into plugins. +is shown goes into `src`. Hardware goes into drivers and optional processing into plugins. **Tools.** A window of the Tools menu, with its own state and workflow, goes into its own folder under `src/tools`. It uses only the public Core headers and `ScopeOneToolContext` from @@ -36,7 +37,7 @@ source file, so adding a tool changes neither `MainWindow` nor CMake. Its algori A command that takes an image and parameters and returns a result is not a tool; it belongs in the image menus. -**Extension boundary.** Hardware plugins and Python processing modules are the only public +**Extension boundary.** Drivers and Python processing modules are the only public extension points, and both run outside the ScopeOne process. The CUDA and ONNX processing plugins exist only to keep CUDA and ONNX Runtime out of Core, and their interface is internal to this repository. Everything without such a dependency belongs in Core, with its window in `src`. The @@ -220,8 +221,8 @@ a pinned version in `vcpkg.json`. **Core.** The version is `MAJOR.MINOR.MMDD`: `MAJOR.MINOR` comes from `VERSION`, the last part is the date of the last commit. Raise `MINOR` whenever a public SDK header changes. ScopeOne loads -a plugin only when its Core `MAJOR.MINOR` matches, and each plugin kind has its own -`scopeOneApi` contract version. +a driver or plugin only when its Core `MAJOR.MINOR` matches, and drivers and processing plugins +each have their own `scopeOneApi` contract version. **Dependencies are pinned.** vcpkg packages, Python (`==3.14.*`) and Python packages (`numpy==2.5.3`) use exact versions. Upgrade them together, on purpose. diff --git a/README.md b/README.md index 7ea4a9e..eaacab1 100644 --- a/README.md +++ b/README.md @@ -79,9 +79,9 @@ ScopeOne/ ScopeWriter/ ``` -### Plugin layout +### Drivers and plugins -ScopeOne has two public extension points, hardware plugins and Python processing modules, and both run outside the ScopeOne process. Hardware plugins are loaded from `plugins/hardware` by `ScopeOne_DriverHost`, one process per device, and Python modules from `plugins/processing/python`. The CUDA and ONNX processing plugins are built and released with ScopeOne. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [plugins/README.md](plugins/README.md) explains how to write, build and install an extension. +ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `plugins/processing/python`. The CUDA and ONNX processing plugins are built and released with ScopeOne. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [plugins/README.md](plugins/README.md) how to write a Python processing module. **Windows Build Steps:** diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index 3112e43..cb6ecd6 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -72,7 +72,6 @@ set(CORE_SOURCES src/AcquisitionEngine.cpp src/DriverHostProviderProxy.cpp src/DaqDeviceManager.cpp - src/HardwareProvider.cpp src/PluginManifest.cpp src/HardwareRuntime.cpp src/MicroManagerProvider.cpp @@ -97,10 +96,9 @@ set(CORE_SOURCES src/DifferentialRollingModule.cpp src/IscatModule.cpp src/PythonProcessingModule.cpp - src/NIDaqmx.cpp src/PtuFile.cpp - src/SimulatedPmt.cpp - src/SimulatedPmtSource.cpp + src/HardwareStreamCodec.cpp + src/StreamRing.cpp src/MDAManager.cpp src/RecordingManager.cpp src/CameraBackend.cpp @@ -114,20 +112,10 @@ set(CORE_SOURCES set(CORE_HEADERS include/scopeone/ExperimentDocument.h - include/scopeone/CameraProvider.h - include/scopeone/DaqDevice.h - include/scopeone/DriverHostProviderPlugin.h - include/scopeone/HardwareCapabilities.h - include/scopeone/HardwareProvider.h - include/scopeone/HardwareTypes.h - include/scopeone/ImageFrame.h include/scopeone/PluginManifest.h - include/scopeone/HardwarePluginSDK.h include/scopeone/ProcessingPlugin.h include/scopeone/ScanImageAssembler.h - include/scopeone/scopeone_sdk_export.h include/scopeone/SharedFrame.h - include/scopeone/SignalSource.h include/scopeone/DhmReconstruction.h internal/AcquisitionEngine.h internal/CameraRuntimeControl.h @@ -145,6 +133,8 @@ set(CORE_HEADERS include/scopeone/ProcessingPipeline.h internal/DriverHostProtocol.h internal/SharedFrameRing.h + internal/HardwareStreamCodec.h + internal/StreamRing.h internal/DriverHostProviderProxy.h internal/DaqDeviceManager.h internal/SignalSourceManager.h @@ -159,10 +149,7 @@ set(CORE_HEADERS internal/DifferentialRollingModule.h internal/IscatModule.h internal/PythonProcessingModule.h - internal/NIDaqmx.h internal/PtuFile.h - internal/SimulatedPmt.h - internal/SimulatedPmtSource.h internal/MDAManager.h internal/RecordingManager.h internal/CameraBackend.h @@ -172,26 +159,48 @@ set(CORE_HEADERS include/scopeone/ScopeOneCore.h ) +# The driver SDK: the only headers and the only library a driver sees. It is a library of its +# own so a driver that reaches for anything else of Core does not compile or link. +set(SDK_HEADERS + sdk/include/scopeone/CameraProvider.h + sdk/include/scopeone/DaqDevice.h + sdk/include/scopeone/DaqProvider.h + sdk/include/scopeone/Driver.h + sdk/include/scopeone/HardwareCapabilities.h + sdk/include/scopeone/DriverSDK.h + sdk/include/scopeone/HardwareProvider.h + sdk/include/scopeone/HardwareTypes.h + sdk/include/scopeone/ImageFrame.h + sdk/include/scopeone/scopeone_sdk_export.h + sdk/include/scopeone/SignalSource.h + sdk/include/scopeone/SignalSourceProvider.h +) +add_library(ScopeOneDriverSDK SHARED + sdk/src/HardwareProvider.cpp + sdk/src/SignalSource.cpp + ${SDK_HEADERS} +) +add_library(scopeone::DriverSDK ALIAS ScopeOneDriverSDK) +set_target_properties(ScopeOneDriverSDK PROPERTIES EXPORT_NAME DriverSDK) +target_link_libraries(ScopeOneDriverSDK PUBLIC Qt::Core) +target_compile_definitions(ScopeOneDriverSDK PRIVATE SCOPEONE_SDK_EXPORTS) +target_include_directories(ScopeOneDriverSDK PUBLIC + $ + $ +) + add_library(ScopeOneCore SHARED ${CORE_SOURCES} ${CORE_HEADERS}) add_library(scopeone::ScopeOneCore ALIAS ScopeOneCore) -add_library(ScopeOneHardwarePluginSDK INTERFACE) -add_library(scopeone::HardwarePluginSDK ALIAS ScopeOneHardwarePluginSDK) if (UNIX AND NOT APPLE) - set_target_properties(ScopeWriter ScopeOneCore PROPERTIES + set_target_properties(ScopeWriter ScopeOneCore ScopeOneDriverSDK PROPERTIES BUILD_RPATH "$ORIGIN" INSTALL_RPATH "$ORIGIN" INSTALL_RPATH_USE_LINK_PATH TRUE ) endif () -# The public target for hardware plugins outside this repository -set_target_properties(ScopeOneHardwarePluginSDK PROPERTIES EXPORT_NAME HardwarePluginSDK) -target_link_libraries(ScopeOneHardwarePluginSDK INTERFACE ScopeOneCore Qt::Core Qt::Gui) -target_include_directories(ScopeOneHardwarePluginSDK INTERFACE - $ - $ -) target_link_libraries(ScopeOneCore PUBLIC + ScopeOneDriverSDK Qt::Core Qt::Gui PRIVATE @@ -247,6 +256,11 @@ target_precompile_headers(ScopeOneCore PRIVATE ) target_link_directories(ScopeOneCore PRIVATE ${MMCORE_BIN_DIR}) +# Keeps the symbols of the static libraries in Core, MMCore among them, out of its exports. Linux +# exported them all, so DriverHost had linked MMCore through Core without noticing. +if (UNIX AND NOT APPLE) + target_link_options(ScopeOneCore PRIVATE "LINKER:--exclude-libs,ALL") +endif () scopeone_add_runtime_copy_commands(ScopeOneCore $) add_custom_command(TARGET ScopeOneCore POST_BUILD @@ -255,8 +269,13 @@ add_custom_command(TARGET ScopeOneCore POST_BUILD $ ) +# DriverHost does not link Core, so a driver process loads only the SDK, Qt Core and Network and +# MMCore. It builds the few Core sources it needs itself. add_executable(ScopeOne_DriverHost src/DriverHostMain.cpp + src/HardwareStreamCodec.cpp + src/PluginManifest.cpp + src/StreamRing.cpp ) if (UNIX AND NOT APPLE) set_target_properties(ScopeOne_DriverHost PROPERTIES @@ -266,12 +285,16 @@ if (UNIX AND NOT APPLE) ) endif () target_link_libraries(ScopeOne_DriverHost - ScopeOneCore + ScopeOneDriverSDK MMCore Qt::Core Qt::Network ) -target_compile_definitions(ScopeOne_DriverHost PRIVATE MMDEVICE_CLIENT_BUILD) +target_compile_definitions(ScopeOne_DriverHost PRIVATE + MMDEVICE_CLIENT_BUILD + SCOPEONE_CORE_STATIC + SCOPEONE_CORE_VERSION_STRING="${PROJECT_VERSION}" +) target_include_directories(ScopeOne_DriverHost PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR} @@ -318,7 +341,7 @@ write_basic_package_version_file( COMPATIBILITY SameMajorVersion ) -install(TARGETS ScopeOneCore ScopeOneHardwarePluginSDK +install(TARGETS ScopeOneCore ScopeOneDriverSDK EXPORT ScopeOneCoreTargets RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" @@ -332,6 +355,11 @@ install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/" FILES_MATCHING PATTERN "*.h" ) +install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/sdk/include/" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/sdk" + FILES_MATCHING + PATTERN "*.h" +) install(EXPORT ScopeOneCoreTargets FILE ScopeOneCoreTargets.cmake NAMESPACE scopeone:: diff --git a/ScopeOneCore/DRIVERHOST_PROTOCOL.md b/ScopeOneCore/DRIVERHOST_PROTOCOL.md index 120d0a6..054dcf1 100644 --- a/ScopeOneCore/DRIVERHOST_PROTOCOL.md +++ b/ScopeOneCore/DRIVERHOST_PROTOCOL.md @@ -1,10 +1,10 @@ # DriverHost protocol -ScopeOne talks to every hardware plugin through a `ScopeOne_DriverHost` process. This file -describes what crosses that process boundary: control messages on a local socket and camera -frames in shared memory. Both ends are part of ScopeOne, so the protocol is internal and changes -with it; hardware plugins are written against `scopeone::HardwarePluginSDK` instead. It is version -5, `kProtocolVersion` in `ScopeOneCore/internal/DriverHostProtocol.h`, which holds the names used +ScopeOne talks to every driver through a `ScopeOne_DriverHost` process. This file +describes what crosses that process boundary: control messages on a local socket, camera frames +in shared memory, and the chunks of signal sources and DAQ devices in stream rings. Both ends are part of ScopeOne, so the protocol is internal and changes +with it; drivers are written against `scopeone::DriverSDK` instead. It is version +6, `kProtocolVersion` in `ScopeOneCore/internal/DriverHostProtocol.h`, which holds the names used below. ## Process @@ -12,11 +12,11 @@ below. ScopeOne starts one host for each device: ```text -ScopeOne_DriverHost --provider --plugin --hostKey +ScopeOne_DriverHost --provider --driver --hostKey --launcherPid [--option ] ``` -`--option` carries the options from the Plugin Manager. The host loads the plugin, creates the +`--option` carries the options from Drivers and Plugins. The host loads the driver, creates the devices and listens on the local socket `ScopeOne.DriverHost..ctrl` (a named pipe on Windows, a Unix domain socket elsewhere). ScopeOne connects to it, and the host exits when the process `--launcherPid` is gone. @@ -24,7 +24,7 @@ process `--launcherPid` is gone. ## Messages Each message is a 4-byte little-endian length followed by that many bytes of compact UTF-8 JSON. -A message is at most 256 KiB. Every message is an object with +A message is at most 64 MiB, because a DAQ session carries its output waveforms. Every message is an object with | Field | Value | |---|---| @@ -41,13 +41,15 @@ and carry `providerId`. ## Describe and shutdown -`Describe` returns `providerId`, `providerName`, `providerVersion` and `devices`, and for a provider -with stages `defaultXYStage` and `defaultZStage`. Each device is an object with +`Describe` returns `providerId`, `providerName`, `providerVersion`, `devices`, `signalSources` +and `daqDevices`, and for a provider with stages `defaultXYStage` and `defaultZStage`. +`signalSources` and `daqDevices` hold the JSON form of `SignalSourceDescriptor` and +`DaqDeviceDescriptor`, written by `ScopeOneCore/src/HardwareStreamCodec.cpp`. Each device is an object with | Field | Value | |---|---| | `deviceId` | the id ScopeOne uses in requests | -| `providerDeviceId`, `hardwareId`, `deviceName` | the plugin's own names | +| `providerDeviceId`, `hardwareId`, `deviceName` | the driver's own names | | `deviceKind` | `Camera`, `XYStage`, `ZStage`, `Shutter`, `State`, `Hub`, `Serial`, `Generic`, ... | | `deviceState` | the numeric `HardwareDeviceState` | | `deviceProperties` | an object of fixed device information | @@ -58,7 +60,7 @@ with stages `defaultXYStage` and `defaultZStage`. Each device is an object with ## Commands -Every command except the configuration ones names its device with `deviceId`. A command for a +Every command except the configuration, signal and DAQ ones names its device with `deviceId`. A command for a capability the device lacks is answered with `ok` false. | Capability | Command | Request fields | Response fields | @@ -82,6 +84,10 @@ capability the device lacks is answered with `ok` false. | `State` | `GetState` | | `state`, `label` | | | `SetState` | `state` | | | | `GetStateLabel` | `state` | `label` | +| `SignalSource` | `StartSignal` | `config`, a `SignalAcquisitionConfig` | `streamId`, `sharedMemoryKey` | +| | `StopSignal` | `sourceId` | | +| `Daq` | `StartDaq` | `config`, a `DaqSessionConfig` with its raster scans expanded | `streamId`, `sharedMemoryKey` | +| | `StopDaq` | `deviceId` | | | `Configuration` | `ListConfigGroups` | | `groups` | | | `ListConfigs` | `group` | `configs` | | | `GetCurrentConfig` | `group` | `config` | @@ -95,6 +101,9 @@ capability the device lacks is answered with `ok` false. | `FrameAvailable` | `deviceId`, `frameIndex` | a frame is in the ring | | `PreviewState` | `deviceId`, `running` | preview starts or stops | | `DriverHostError` | `error` | the provider reports an error | +| `StreamAvailable` | `streamId` | records are in a stream ring; one event can stand for several | +| `SignalState` | `sourceId`, `state`, `message` | a signal source changes its `SignalSourceState` | +| `DaqState` | `deviceId`, `state`, `message` | a DAQ device changes its `DaqState` | ## Camera frames @@ -130,3 +139,30 @@ fills the header and pixels, stores 2 with release order, then stores the slot i `latestSlotIndex` with release order and sends `FrameAvailable`. The reader takes a slot by changing its `state` from 2 to 3, copies the frame, and stores 2 again. A slot being read is skipped by the writer, so the reader never sees a frame change under it. + +## Stream rings + +Each `StartSignal` and `StartDaq` gets a new ring in shared memory under the `sharedMemoryKey` of +its response, sized for about two seconds of that acquisition. The host writes the chunks of the +source or session into it, and ScopeOne reads them after each `StreamAvailable`. The layout is +`ScopeOneCore/internal/StreamRing.h`. + +The ring begins with a control block of three 64-byte lines: `magic` u32 and `version` u32 and +`capacity` u64 in the first, `writeCursor` u64 in the second and `readCursor` u64 in the third. +The cursors count bytes from the start of the stream and never wrap; the data area of `capacity` +bytes follows the control block. Each record starts on 8 bytes with a 32-byte header + +| Offset | Record header field | +|---|---| +| 0 | `bytes`, u32, the record with its header and padding | +| 4 | `type`, u32: 0 skip, 1 time series, 2 timestamped events, 3 DAQ input | +| 8 | `sequence`, u64 | +| 16 | `droppedBefore`, u64, records dropped right before this one | +| 24 | `payloadBytes`, u64 | + +followed by the payload. A record that would cross the end of the data area is written at its +beginning after a skip record that fills the rest. The writer never overwrites unread bytes: when +the ring is full it drops the record and reports the count in `droppedBefore` of the next one. +It publishes a record by storing `writeCursor` with release order, and the reader frees it by +storing `readCursor` the same way. The payloads are the chunks in native byte order, as +`HardwareStreamCodec.cpp` writes them. diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 08647ee..cd64a93 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -8,7 +8,8 @@ The desktop app consumes `ScopeOneCore` through its installed CMake package. ```text ScopeOneCore/ -|-- include/scopeone/ Core facade, public models and the hardware plugin contract +|-- include/scopeone/ Core facade and public models used by the app and the built-in plugins +|-- sdk/ Driver SDK: its headers and the small library behind them |-- internal/ Private headers used only while building ScopeOneCore |-- src/ C++ implementations for both public and private types |-- python/scopeone/ External Python client for a running ScopeOne app @@ -18,20 +19,23 @@ ScopeOneCore/ `-- install/ Generated local installation consumed by the desktop app ``` -`include/scopeone` contains the Core facade, Core-owned public models and the plugin contracts: +`include/scopeone` contains the Core facade, Core-owned public models and the processing plugin contract: ```cpp #include -#include +#include ``` +`sdk/include/scopeone` contains the driver contract and the types it carries, such as `Driver`, `HardwareProvider`, `CameraProvider`, `SignalSourceProvider`, `DaqProvider` and `ImageFrame`. These headers keep the `scopeone/` include prefix. They build the `ScopeOneDriverSDK` library (`scopeone::DriverSDK`), which Core links publicly. A driver links only that target, so it sees neither the other Core headers nor the Core library, and on Linux it is linked with `--no-undefined`. An SDK header includes only Qt and other SDK headers. + `internal` contains implementation contracts between Core managers, processing modules and DriverHost. These headers are available to the `ScopeOneCore` target through a private include path, are not installed, and may change without preserving source compatibility. Code outside `ScopeOneCore` must not include them. `src` contains implementations. A public class such as `ScopeOneCore`, `ImageSceneModel` or `ExperimentDocument` still has its `.cpp` file in `src`; being public is determined by its header location and exported API, not by the location of its implementation. Use this placement rule: -- Put every public Core model and plugin contract in `ScopeOneCore/include/scopeone`. +- Put what drivers are written against in `ScopeOneCore/sdk`, and keep it small. +- Put every other public Core model and contract in `ScopeOneCore/include/scopeone`. - Put a Core-only manager, algorithm or protocol detail in `internal`. - Put executable implementation in `src`. - Keep desktop widgets and Qt UI behavior in the top-level ScopeOne `src` directory, outside `ScopeOneCore`. @@ -43,8 +47,8 @@ Use this placement rule: | Namespace | Purpose | Examples | |---|---|---| | `scopeone::core` | Stable Core-facing types and public facades | `ScopeOneCore`, `ExperimentDocument`, `ImageSceneModel` | -| `scopeone::core` hardware plugin contract | Stable types of `HardwarePluginSDK.h` | `ImageFrame`, `CameraProvider`, `HardwareProvider`, `DriverHostProviderPlugin` | -| `scopeone::core::internal` | Core-only managers, processing modules and built-in devices | `CameraManager`, `MMCoreManager`, `RecordingManager`, `IscatModule`, `NIDaqmxDeviceFactory` | +| `scopeone::core` driver contract | Stable types of `DriverSDK.h` | `Driver`, `HardwareProvider`, `CameraProvider`, `SignalSourceProvider`, `DaqProvider`, `ImageFrame` | +| `scopeone::core::internal` | Core-only managers, processing modules and built-in devices | `CameraManager`, `MMCoreManager`, `RecordingManager`, `IscatModule`, `SignalSourceManager` | | `scopeone::core::internal::driverhost` | Shared DriverHost message framing | Versioned request, response and event envelopes | | `scopeone::ui` | Desktop application widgets and UI coordination outside this library | `MainWindow`, `PreviewWidget`, `DhmTool`, `ToolRegistry` | | `scopeone::plugins::` | The CUDA processing plugin. The ONNX plugin stays in `scopeone::inference`, the namespace of its library | `scopeone::plugins::cuda` | @@ -90,37 +94,37 @@ Outputs: - `build/Release/ScopeOneCore.dll` - `build/Release/ScopeOneCore.lib` - `build/Release/ScopeOne_DriverHost.exe` -- External plugins are built under `plugins`. +- Drivers and the CUDA and ONNX plugins are built by the `plugins` project. - `build/ScopeOneCoreConfig.cmake` - `install/bin/ScopeOneCore.dll` - `install/bin/ScopeOne_DriverHost.exe` -- Hardware plugins are installed under `plugins/hardware`, and the CUDA and ONNX processing plugins under `plugins/processing`. +- Drivers are installed under `drivers`, and the CUDA and ONNX processing plugins under `plugins/processing`. - `install/lib/cmake/ScopeOneCore/ScopeOneCoreConfig.cmake` ## Core and SDK API -The installed headers in `include/scopeone` are the source of truth for the public API: +The installed headers in `include/scopeone` and `include/sdk/scopeone` are the source of truth for the public API: - `ScopeOneCore.h` provides the main hardware, acquisition, processing, recording and frame-graph facade. - `ProcessingPipeline.h`, `ExperimentDocument.h` and `ImageSceneModel.h` are Core-owned runtime models and services. -- `HardwarePluginSDK.h` includes the headers of the public hardware plugin contract: `CameraProvider.h`, `DriverHostProviderPlugin.h`, `HardwareCapabilities.h`, `HardwareProvider.h`, `HardwareTypes.h`, `ImageFrame.h` and `PluginManifest.h`. The `scopeone::HardwarePluginSDK` CMake target provides them. +- `DriverSDK.h` includes the headers of the public driver contract: `CameraProvider.h`, `DaqDevice.h`, `DaqProvider.h`, `Driver.h`, `HardwareCapabilities.h`, `HardwareProvider.h`, `HardwareTypes.h`, `ImageFrame.h`, `SignalSource.h` and `SignalSourceProvider.h`. The `scopeone::DriverSDK` CMake target provides them. - `ProcessingPlugin.h` is the interface of the CUDA and ONNX processing plugins. It is internal to this repository. -- `DaqDevice.h`, `SignalSource.h`, `ScanImageAssembler.h`, `SharedFrame.h` and `DhmReconstruction.h` serve the application and are not plugin contracts. +- `PluginManifest.h` is how ScopeOne discovers and enables drivers and plugins. `ScanImageAssembler.h`, `SharedFrame.h` and `DhmReconstruction.h` serve the application. None of them are driver contracts. - `scopeone_core_export.h` supplies DLL import and export declarations and is normally included indirectly. External code should enter through these headers and `scopeone::core::ScopeOneCore`. Internal managers are implementation details and must not become alternate access paths. -Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `CameraProvider::FrameSink`. Core loads a hardware plugin with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider, using the native camera path for one camera and isolated DriverHost camera processes for multiple cameras. +Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `CameraProvider::FrameSink`. Core loads a driver with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider, using the native camera path for one camera and isolated DriverHost camera processes for multiple cameras. -## Plugin Boundaries +## Driver and Plugin Boundaries -- `plugins/hardware` contains native `HardwareProvider` modules. Each provider runs in an isolated DriverHost process. Micro-Manager Device Adapters remain under Micro-Manager and are not wrapped as ScopeOne plugins. +- `drivers` contains the drivers, among them NI-DAQmx and the example camera and PMT. Each provider runs in an isolated DriverHost process, so a vendor library such as NI-DAQmx is never loaded into ScopeOne. Micro-Manager Device Adapters remain under Micro-Manager and are not wrapped as ScopeOne drivers. - Python processing modules are scripts in the `python` folder of a processing plugin directory. Core runs each module of a pipeline in its own Python process. -- The CUDA and ONNX processing modules are processing plugins, because they link CUDA and ONNX Runtime, which Core does not. Every other processing method, the NI-DAQmx device, the PTU file and simulated PMT signal sources and the DHM reconstruction are part of Core. +- The CUDA and ONNX processing modules are processing plugins, because they link CUDA and ONNX Runtime, which Core does not. Every other processing method, the PTU file signal source and the DHM reconstruction are part of Core. - The tool windows (Scale, Stage Mosaic, Particle Detection, DHM Reconstruction, Scanning and DAQ Control, Signal Monitor) are part of the application. They use the `ScopeOneCore` facade, and `ScopeOneToolContext` in `src/tools/ToolContext.h` gives them the desktop operations such as showing layers. -Hardware plugins outside this repository consume the exported `scopeone::HardwarePluginSDK` CMake target. Every plugin manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`; the processing and hardware contracts are versioned independently and incompatible manifests are rejected before the plugin instance is created. +Drivers outside this repository consume the exported `scopeone::DriverSDK` CMake target. Every driver and plugin manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`; the processing and driver contracts are versioned independently and incompatible manifests are rejected before the library instance is created. ## Processing Data Flow diff --git a/ScopeOneCore/cmake/ScopeOnePlugin.cmake b/ScopeOneCore/cmake/ScopeOnePlugin.cmake index 00e9b58..8261b54 100644 --- a/ScopeOneCore/cmake/ScopeOnePlugin.cmake +++ b/ScopeOneCore/cmake/ScopeOnePlugin.cmake @@ -1,19 +1,19 @@ -# Helpers for building ScopeOne plugins against an installed ScopeOneCore. +# Helpers for building ScopeOne drivers against an installed ScopeOneCore. # # find_package(ScopeOneCore CONFIG REQUIRED) -# add_library(MyPlugin MODULE MyPlugin.cpp plugin.json) -# target_link_libraries(MyPlugin PRIVATE scopeone::HardwarePluginSDK) -# scopeone_add_plugin(MyPlugin hardware) +# add_library(MyCamera MODULE MyCamera.cpp driver.json) +# target_link_libraries(MyCamera PRIVATE scopeone::DriverSDK) +# scopeone_add_driver(MyCamera) # -# The plugin source names the generated manifest: -# Q_PLUGIN_METADATA(IID ScopeOneDriverHostProviderPlugin_iid FILE "scopeone_plugin.json") +# The driver source names the generated manifest: +# Q_PLUGIN_METADATA(IID ScopeOneDriver_iid FILE "scopeone_driver.json") -# Writes scopeone_plugin.json from the plugin.json beside the target's CMakeLists, adding the -# ScopeOneCore version the plugin is built against. ScopeOne loads only plugins built against -# its own Core, because plugins share C++ types with it. -function(scopeone_plugin_manifest target) - set(_source "${CMAKE_CURRENT_SOURCE_DIR}/plugin.json") - set(_generated "${CMAKE_CURRENT_BINARY_DIR}/scopeone_plugin.json") +# Writes the generated manifest from the manifest beside the target's CMakeLists, adding the +# ScopeOneCore version the target is built against. ScopeOne loads only drivers and plugins built +# against its own Core, because they share C++ types with it. +function(scopeone_write_manifest target source generated) + set(_source "${CMAKE_CURRENT_SOURCE_DIR}/${source}") + set(_generated "${CMAKE_CURRENT_BINARY_DIR}/${generated}") file(READ "${_source}" _manifest) string(JSON _manifest SET "${_manifest}" scopeOneCore "\"${ScopeOneCore_VERSION}\"") file(CONFIGURE OUTPUT "${_generated}" CONTENT "${_manifest}\n" @ONLY) @@ -21,17 +21,27 @@ function(scopeone_plugin_manifest target) target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") endfunction() -# Builds the target as a plugin of the given kind (processing or hardware) and installs -# it where ScopeOne looks for that kind -function(scopeone_add_plugin target kind) - scopeone_plugin_manifest(${target}) +# The manifest of a built-in processing plugin: plugin.json to scopeone_plugin.json +function(scopeone_plugin_manifest target) + scopeone_write_manifest(${target} plugin.json scopeone_plugin.json) +endfunction() + +# Builds the target as a driver, from driver.json to scopeone_driver.json, and installs it in the +# drivers directory where ScopeOne looks for drivers +function(scopeone_add_driver target) + scopeone_write_manifest(${target} driver.json scopeone_driver.json) set_target_properties(${target} PROPERTIES PREFIX "" - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins/${kind}" - LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins/${kind}" + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/drivers" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/drivers" ) + # A driver links only what it names. Linux leaves undefined symbols of a module to load time, + # so a driver calling into Core past its SDK would link and fail only once loaded. + if (UNIX AND NOT APPLE) + target_link_options(${target} PRIVATE "LINKER:--no-undefined") + endif () install(TARGETS ${target} - RUNTIME DESTINATION "plugins/${kind}" - LIBRARY DESTINATION "plugins/${kind}" + RUNTIME DESTINATION "drivers" + LIBRARY DESTINATION "drivers" ) endfunction() diff --git a/ScopeOneCore/include/scopeone/PluginManifest.h b/ScopeOneCore/include/scopeone/PluginManifest.h index d6caaf0..630b563 100644 --- a/ScopeOneCore/include/scopeone/PluginManifest.h +++ b/ScopeOneCore/include/scopeone/PluginManifest.h @@ -1,6 +1,6 @@ #pragma once -#include "scopeone/scopeone_sdk_export.h" +#include "scopeone/scopeone_core_export.h" #include #include @@ -9,10 +9,12 @@ namespace scopeone::core { + // A library ScopeOne discovers and loads: a built-in processing plugin, or a driver that + // ScopeOne_DriverHost loads for its device enum class PluginKind { Processing, - Hardware + Driver }; struct PluginManifest @@ -36,20 +38,21 @@ namespace scopeone::core }; inline constexpr int ScopeOneProcessingPluginApiVersion = 2; - inline constexpr int ScopeOneHardwarePluginApiVersion = 1; + inline constexpr int ScopeOneDriverApiVersion = 1; - SCOPEONE_SDK_EXPORT QString pluginKindName(PluginKind kind); - SCOPEONE_SDK_EXPORT int pluginApiVersion(PluginKind kind); - SCOPEONE_SDK_EXPORT QString pluginDirectoryName(PluginKind kind); - SCOPEONE_SDK_EXPORT QStringList pluginInterfaceIds(PluginKind kind); - SCOPEONE_SDK_EXPORT QStringList pluginDirectories(PluginKind kind); - SCOPEONE_SDK_EXPORT QString pluginSettingsKey(const QString& pluginId, + SCOPEONE_CORE_EXPORT QString pluginKindName(PluginKind kind); + SCOPEONE_CORE_EXPORT int pluginApiVersion(PluginKind kind); + // The directory of the kind relative to the application or the user data directory + SCOPEONE_CORE_EXPORT QString pluginDirectoryName(PluginKind kind); + SCOPEONE_CORE_EXPORT QStringList pluginInterfaceIds(PluginKind kind); + SCOPEONE_CORE_EXPORT QStringList pluginDirectories(PluginKind kind); + SCOPEONE_CORE_EXPORT QString pluginSettingsKey(const QString& pluginId, const QString& name); - SCOPEONE_SDK_EXPORT bool pluginEnabled(const PluginManifest& manifest); - SCOPEONE_SDK_EXPORT QList discoverPlugins( + SCOPEONE_CORE_EXPORT bool pluginEnabled(const PluginManifest& manifest); + SCOPEONE_CORE_EXPORT QList discoverPlugins( PluginKind kind, const QStringList& directories = {}); - SCOPEONE_SDK_EXPORT bool parsePluginManifest(const QJsonObject& metadata, + SCOPEONE_CORE_EXPORT bool parsePluginManifest(const QJsonObject& metadata, PluginKind expectedKind, PluginManifest& manifest, QString* errorMessage = nullptr); diff --git a/ScopeOneCore/include/scopeone/ProcessingPlugin.h b/ScopeOneCore/include/scopeone/ProcessingPlugin.h index 6561fa7..7178f2c 100644 --- a/ScopeOneCore/include/scopeone/ProcessingPlugin.h +++ b/ScopeOneCore/include/scopeone/ProcessingPlugin.h @@ -1,7 +1,7 @@ #pragma once #include "scopeone/ImageFrame.h" -#include "scopeone/scopeone_sdk_export.h" +#include "scopeone/scopeone_core_export.h" #include #include @@ -116,7 +116,7 @@ namespace scopeone::core } }; - class SCOPEONE_SDK_EXPORT ProcessingModule + class SCOPEONE_CORE_EXPORT ProcessingModule { public: virtual ~ProcessingModule() = default; diff --git a/ScopeOneCore/include/scopeone/ScanImageAssembler.h b/ScopeOneCore/include/scopeone/ScanImageAssembler.h index a513681..3f6541d 100644 --- a/ScopeOneCore/include/scopeone/ScanImageAssembler.h +++ b/ScopeOneCore/include/scopeone/ScanImageAssembler.h @@ -2,14 +2,14 @@ #include "scopeone/ImageFrame.h" #include "scopeone/SignalSource.h" -#include "scopeone/scopeone_sdk_export.h" +#include "scopeone/scopeone_core_export.h" #include #include namespace scopeone::core { - class SCOPEONE_SDK_EXPORT ScanImageAssembler final + class SCOPEONE_CORE_EXPORT ScanImageAssembler final { public: explicit ScanImageAssembler(const QString& sourceId, diff --git a/ScopeOneCore/include/scopeone/ScopeOneCore.h b/ScopeOneCore/include/scopeone/ScopeOneCore.h index ad6fcc9..a89c02c 100644 --- a/ScopeOneCore/include/scopeone/ScopeOneCore.h +++ b/ScopeOneCore/include/scopeone/ScopeOneCore.h @@ -488,6 +488,9 @@ namespace scopeone::core void rawFramesAcquired(const QString& cameraId, quint64 frameCount); void previewRawFrameReady(const ImageFrame& frame); void previewStateChanged(bool running); + // The signal sources or DAQ devices changed as a driver was registered or removed + void signalSourcesChanged(); + void daqDevicesChanged(); void signalTimeSeriesReady(const TimeSeriesChunk& chunk); void signalSourceStateChanged(const QString& sourceId, SignalSourceState state, diff --git a/ScopeOneCore/include/scopeone/scopeone_core_export.h b/ScopeOneCore/include/scopeone/scopeone_core_export.h index 3d1ee5d..cb516bf 100644 --- a/ScopeOneCore/include/scopeone/scopeone_core_export.h +++ b/ScopeOneCore/include/scopeone/scopeone_core_export.h @@ -1,6 +1,10 @@ #pragma once -#if defined(_WIN32) || defined(__CYGWIN__) +// SCOPEONE_CORE_STATIC compiles a Core source into another binary, as DriverHost does with the +// plugin manifest, without importing it from the Core library +#if defined(SCOPEONE_CORE_STATIC) +# define SCOPEONE_CORE_EXPORT +#elif defined(_WIN32) || defined(__CYGWIN__) # if defined(SCOPEONE_CORE_EXPORTS) # define SCOPEONE_CORE_EXPORT __declspec(dllexport) # else diff --git a/ScopeOneCore/internal/DaqDeviceManager.h b/ScopeOneCore/internal/DaqDeviceManager.h index c2aee49..155b3f0 100644 --- a/ScopeOneCore/internal/DaqDeviceManager.h +++ b/ScopeOneCore/internal/DaqDeviceManager.h @@ -2,31 +2,23 @@ #include #include -#include - -#include -#include #include "scopeone/DaqDevice.h" +#include "scopeone/DaqProvider.h" +#include "scopeone/HardwareProvider.h" namespace scopeone::core::internal { - // A kind of DAQ device built into Core, which lists the devices it finds and drives them - class DaqDeviceFactory - { - public: - virtual ~DaqDeviceFactory() = default; - virtual QList devices() const = 0; - virtual DaqController* createController(const QString& deviceId, - QObject* parent = nullptr) = 0; - }; + class DeviceRegistry; + // The DAQ devices of the drivers. Their inputs and states arrive on a thread of the + // provider and are passed on in this object's thread. class DaqDeviceManager final : public QObject { Q_OBJECT public: - explicit DaqDeviceManager(QObject* parent = nullptr); + explicit DaqDeviceManager(DeviceRegistry* registry, QObject* parent = nullptr); ~DaqDeviceManager() override; QList devices() const; @@ -37,6 +29,7 @@ namespace scopeone::core::internal QString stateMessage(const QString& deviceId) const; signals: + void devicesChanged(); void stateChanged(const QString& deviceId, scopeone::core::DaqState state, const QString& message); @@ -44,15 +37,14 @@ namespace scopeone::core::internal void inputDataReady(const scopeone::core::DaqInputChunk& chunk); private: - void registerDevices(); - DaqController* controller(const QString& deviceId, - QString* errorMessage); + void refreshProviderDevices(); + void detachProvider(const HardwareProviderPtr& provider); + void updateState(const QString& deviceId, DaqState state, const QString& message); - std::vector> m_factories; - mutable bool m_devicesRegistered{false}; - QHash m_factoryByDevice; + DeviceRegistry* m_registry{nullptr}; + QHash m_attachedProviders; + QHash m_providerByDevice; QHash m_descriptors; - QHash> m_controllers; QHash m_states; QHash m_messages; }; diff --git a/ScopeOneCore/internal/DriverHostProtocol.h b/ScopeOneCore/internal/DriverHostProtocol.h index 9da9dab..c30aa28 100644 --- a/ScopeOneCore/internal/DriverHostProtocol.h +++ b/ScopeOneCore/internal/DriverHostProtocol.h @@ -17,7 +17,7 @@ namespace scopeone::core::internal::driverhost { - inline constexpr quint32 kProtocolVersion = 5; + inline constexpr quint32 kProtocolVersion = 6; // The names of the device kinds as the protocol sends them inline constexpr std::pair kDeviceKindNames[]{ @@ -61,7 +61,8 @@ namespace scopeone::core::internal::driverhost } return HardwareDeviceKind::Unknown; } - inline constexpr quint32 kMaxControlMessageBytes = 256 * 1024; + // A DAQ session carries its output waveforms in the control message + inline constexpr quint32 kMaxControlMessageBytes = 64 * 1024 * 1024; inline const QString kEnvelopeKindField = QStringLiteral("kind"); inline const QString kEnvelopeVersionField = QStringLiteral("version"); @@ -82,6 +83,13 @@ namespace scopeone::core::internal::driverhost inline const QString kSharedMemoryKeyField = QStringLiteral("sharedMemoryKey"); inline const QString kDefaultXYStageField = QStringLiteral("defaultXYStage"); inline const QString kDefaultZStageField = QStringLiteral("defaultZStage"); + inline const QString kSignalSourcesField = QStringLiteral("signalSources"); + inline const QString kDaqDevicesField = QStringLiteral("daqDevices"); + inline const QString kSourceIdField = QStringLiteral("sourceId"); + inline const QString kStreamIdField = QStringLiteral("streamId"); + inline const QString kConfigField = QStringLiteral("config"); + inline const QString kStateField = QStringLiteral("state"); + inline const QString kStateMessageField = QStringLiteral("message"); inline const QString kMessageKindRequest = QStringLiteral("Request"); inline const QString kMessageKindResponse = QStringLiteral("Response"); @@ -116,6 +124,10 @@ namespace scopeone::core::internal::driverhost inline const QString kCommandListConfigs = QStringLiteral("ListConfigs"); inline const QString kCommandGetCurrentConfig = QStringLiteral("GetCurrentConfig"); inline const QString kCommandSetConfig = QStringLiteral("SetConfig"); + inline const QString kCommandStartSignal = QStringLiteral("StartSignal"); + inline const QString kCommandStopSignal = QStringLiteral("StopSignal"); + inline const QString kCommandStartDaq = QStringLiteral("StartDaq"); + inline const QString kCommandStopDaq = QStringLiteral("StopDaq"); inline const QString kCapabilityCamera = QStringLiteral("Camera"); inline const QString kCapabilityProperties = QStringLiteral("Properties"); @@ -132,6 +144,10 @@ namespace scopeone::core::internal::driverhost inline const QString kEventFrameAvailable = QStringLiteral("FrameAvailable"); inline const QString kEventPreviewState = QStringLiteral("PreviewState"); inline const QString kEventDriverHostError = QStringLiteral("DriverHostError"); + // New records in the stream ring of a signal source or a DAQ device + inline const QString kEventStreamAvailable = QStringLiteral("StreamAvailable"); + inline const QString kEventSignalState = QStringLiteral("SignalState"); + inline const QString kEventDaqState = QStringLiteral("DaqState"); #ifdef Q_OS_WIN inline const QString kExecutableFileName = QStringLiteral("ScopeOne_DriverHost.exe"); @@ -168,6 +184,14 @@ namespace scopeone::core::internal::driverhost .arg(cameraIndex)); } + // Each start of a stream gets a ring of its own, sized for what that acquisition delivers + inline QString streamSharedMemoryKey(const QString& hostKey, quint64 streamIndex) + { + return sharedMemoryKeyPath(QStringLiteral("ScopeOne.DriverHost.%1.stream%2.shm") + .arg(hostKey) + .arg(streamIndex)); + } + inline QString encodeUInt64(quint64 value) { return QString::number(value); diff --git a/ScopeOneCore/internal/HardwareStreamCodec.h b/ScopeOneCore/internal/HardwareStreamCodec.h new file mode 100644 index 0000000..24aa6ed --- /dev/null +++ b/ScopeOneCore/internal/HardwareStreamCodec.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +#include "scopeone/DaqDevice.h" +#include "scopeone/SignalSource.h" + +// How signal sources and DAQ devices cross the DriverHost boundary: descriptors and +// configurations as JSON in the control messages, data chunks as binary records of the stream +// ring. Both ends run on the same machine, so the arrays are copied in native byte order. +namespace scopeone::core::internal::hardwarestream +{ + QJsonObject toJson(const SignalSourceDescriptor& descriptor); + SignalSourceDescriptor signalSourceDescriptorFromJson(const QJsonObject& object); + QJsonObject toJson(const SignalAcquisitionConfig& config); + SignalAcquisitionConfig signalAcquisitionConfigFromJson(const QJsonObject& object); + + QJsonObject toJson(const DaqDeviceDescriptor& descriptor); + DaqDeviceDescriptor daqDeviceDescriptorFromJson(const QJsonObject& object); + QJsonObject toJson(const DaqSessionConfig& config); + DaqSessionConfig daqSessionConfigFromJson(const QJsonObject& object); + + QByteArray encode(const TimeSeriesChunk& chunk); + QByteArray encode(const TimestampedEventChunk& chunk); + QByteArray encode(const DaqInputChunk& chunk); + bool decode(const QByteArray& payload, TimeSeriesChunk& chunk); + bool decode(const QByteArray& payload, TimestampedEventChunk& chunk); + bool decode(const QByteArray& payload, DaqInputChunk& chunk); +} diff --git a/ScopeOneCore/internal/NIDaqmx.h b/ScopeOneCore/internal/NIDaqmx.h deleted file mode 100644 index 9348229..0000000 --- a/ScopeOneCore/internal/NIDaqmx.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "internal/DaqDeviceManager.h" - -namespace scopeone::core::internal -{ - class NIDaqmxDeviceFactory final : public DaqDeviceFactory - { - public: - QList devices() const override; - scopeone::core::DaqController* createController( - const QString& deviceId, - QObject* parent = nullptr) override; - }; -} diff --git a/ScopeOneCore/internal/PtuFile.h b/ScopeOneCore/internal/PtuFile.h index 44f800d..2453818 100644 --- a/ScopeOneCore/internal/PtuFile.h +++ b/ScopeOneCore/internal/PtuFile.h @@ -8,7 +8,7 @@ namespace scopeone::core::internal { public: QList signalSources() const override; - scopeone::core::SignalSource* createSignalSource( + SignalSource* createSignalSource( const QString& sourceId, QObject* parent = nullptr) override; }; diff --git a/ScopeOneCore/internal/SignalSourceManager.h b/ScopeOneCore/internal/SignalSourceManager.h index 0b6f5f8..a1769fa 100644 --- a/ScopeOneCore/internal/SignalSourceManager.h +++ b/ScopeOneCore/internal/SignalSourceManager.h @@ -7,10 +7,37 @@ #include #include +#include "scopeone/HardwareProvider.h" #include "scopeone/SignalSource.h" +#include "scopeone/SignalSourceProvider.h" namespace scopeone::core::internal { + class DeviceRegistry; + + // A signal source built into Core, such as the reader of PTU files + class SignalSource : public QObject + { + Q_OBJECT + + public: + explicit SignalSource(QObject* parent = nullptr); + ~SignalSource() override; + + virtual bool start(const SignalAcquisitionConfig& config, + QString* errorMessage = nullptr) = 0; + virtual void stop() = 0; + virtual SignalSourceState state() const = 0; + virtual QString stateMessage() const = 0; + + signals: + void timeSeriesReady(const scopeone::core::TimeSeriesChunk& chunk); + void timestampedEventsReady(const scopeone::core::TimestampedEventChunk& chunk); + void stateChanged(scopeone::core::SignalSourceState state, + const QString& message); + void sourceError(const QString& errorMessage); + }; + // A kind of signal source built into Core, which lists its sources and creates them class SignalSourceFactory { @@ -21,12 +48,14 @@ namespace scopeone::core::internal QObject* parent = nullptr) = 0; }; + // The signal sources of Core and of the drivers under one list. The chunks of a + // plugin source arrive on a thread of its provider and are passed on in this object's thread. class SignalSourceManager final : public QObject { Q_OBJECT public: - explicit SignalSourceManager(QObject* parent = nullptr); + explicit SignalSourceManager(DeviceRegistry* registry, QObject* parent = nullptr); ~SignalSourceManager() override; QList sources() const; @@ -37,6 +66,7 @@ namespace scopeone::core::internal QString stateMessage(const QString& sourceId) const; signals: + void sourcesChanged(); void timeSeriesReady(const scopeone::core::TimeSeriesChunk& chunk); void timestampedEventsReady(const scopeone::core::TimestampedEventChunk& chunk); void sourceStateChanged(const QString& sourceId, @@ -45,12 +75,18 @@ namespace scopeone::core::internal void sourceError(const QString& sourceId, const QString& errorMessage); private: - void registerSources(); + void registerBuiltInSources(); + void refreshProviderSources(); + void detachProvider(const HardwareProviderPtr& provider); + void updateState(const QString& sourceId, SignalSourceState state, const QString& message); SignalSource* sourceInstance(const QString& sourceId, QString* errorMessage); + DeviceRegistry* m_registry{nullptr}; std::vector> m_factories; QHash m_factoryBySource; + QHash m_providerBySource; + QHash m_attachedProviders; QHash m_descriptors; QHash> m_sources; QHash m_states; diff --git a/ScopeOneCore/internal/SimulatedPmt.h b/ScopeOneCore/internal/SimulatedPmt.h deleted file mode 100644 index c773905..0000000 --- a/ScopeOneCore/internal/SimulatedPmt.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "internal/SignalSourceManager.h" - -namespace scopeone::core::internal -{ - class SimulatedPmtSourceFactory final : public SignalSourceFactory - { - public: - QList signalSources() const override; - scopeone::core::SignalSource* createSignalSource( - const QString& sourceId, - QObject* parent = nullptr) override; - }; -} diff --git a/ScopeOneCore/internal/SimulatedPmtSource.h b/ScopeOneCore/internal/SimulatedPmtSource.h deleted file mode 100644 index 4c0450e..0000000 --- a/ScopeOneCore/internal/SimulatedPmtSource.h +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once - -#include "scopeone/SignalSource.h" - -#include -#include -#include - -namespace scopeone::core::internal -{ - class SimulatedPmtSource final : public scopeone::core::SignalSource - { - public: - explicit SimulatedPmtSource(QObject* parent = nullptr); - ~SimulatedPmtSource() override; - - bool start(const scopeone::core::SignalAcquisitionConfig& config, - QString* errorMessage = nullptr) override; - void stop() override; - scopeone::core::SignalSourceState state() const override; - QString stateMessage() const override; - - private: - struct Settings - { - double voltage{1.0}; - double gain{1.0}; - double baseRate{1000000.0}; - double darkCountRate{500.0}; - double modulationFrequency{2.0}; - double scanFrameRate{2.0}; - QString waveform{QStringLiteral("constant")}; - QString noiseMode{QStringLiteral("poisson")}; - QString pattern{QStringLiteral("beads")}; - }; - - void run(std::stop_token stopToken, - const scopeone::core::SignalAcquisitionConfig& config, - const Settings& settings); - void runStream(std::stop_token stopToken, - const scopeone::core::SignalAcquisitionConfig& config, - const Settings& settings); - void runScan(std::stop_token stopToken, - const scopeone::core::SignalAcquisitionConfig& config, - const Settings& settings); - double rateAt(double timeSeconds, const Settings& settings) const; - void setState(scopeone::core::SignalSourceState state, const QString& message); - - mutable std::mutex m_stateMutex; - std::mutex m_threadMutex; - std::jthread m_worker; - std::atomic m_state{ - scopeone::core::SignalSourceState::Idle}; - QString m_stateMessage{QStringLiteral("Simulated PMT is idle")}; - }; -} diff --git a/ScopeOneCore/internal/StreamRing.h b/ScopeOneCore/internal/StreamRing.h new file mode 100644 index 0000000..3653e66 --- /dev/null +++ b/ScopeOneCore/internal/StreamRing.h @@ -0,0 +1,83 @@ +#pragma once + +#include +#include +#include +#include + +#include + +// A ring of records that one writer fills and one reader drains, in shared memory between +// DriverHost and ScopeOne or in the memory of one process. Unlike the camera frame ring it +// never overwrites a record the reader has not read: when the ring is full the writer drops +// the new record and the next record it writes carries the number it dropped, so every gap +// is reported. A record is stored in one piece; one that would cross the end of the ring +// starts again at its beginning after a skip record. +namespace scopeone::core::internal::streamring +{ + enum class RecordType : quint32 + { + Skip = 0, + TimeSeries = 1, + TimestampedEvents = 2, + DaqInput = 3 + }; + + struct Record + { + RecordType type{RecordType::Skip}; + quint64 sequence{0}; + // Records the writer dropped right before this one because the ring was full + quint64 droppedBefore{0}; + QByteArray payload; + }; + + // Bytes a ring of the given capacity takes, the control block included + qsizetype ringBytes(qsizetype capacity); + + // Lays out an empty ring in memory of ringBytes(capacity) bytes + void initializeRing(void* memory, qsizetype capacity); + + // Creates the shared memory of a ring of the given capacity under the native key, dropping + // a ring of that key a crashed DriverHost left behind + bool createSharedRing(QSharedMemory& shm, + const QString& nativeKey, + qsizetype capacity, + QString* errorMessage); + + // Attaches the shared memory of a ring another process created + bool attachSharedRing(QSharedMemory& shm, const QString& nativeKey, QString* errorMessage); + + class Writer + { + public: + Writer() = default; + explicit Writer(void* memory); + + bool isValid() const { return m_base != nullptr; } + + // Appends one record. False when the ring has no room, and the record is counted as dropped. + bool write(RecordType type, const void* payload, std::size_t bytes); + + private: + uchar* m_base{nullptr}; + quint64 m_sequence{0}; + quint64 m_dropped{0}; + }; + + class Reader + { + public: + Reader() = default; + explicit Reader(void* memory); + + bool isValid() const { return m_base != nullptr; } + + // Takes the oldest unread record. False when the ring is empty or holds a record that + // does not fit the ring, which only a writer of another layout leaves. + bool read(Record& record); + + private: + uchar* m_base{nullptr}; + }; +} diff --git a/ScopeOneCore/include/scopeone/CameraProvider.h b/ScopeOneCore/sdk/include/scopeone/CameraProvider.h similarity index 100% rename from ScopeOneCore/include/scopeone/CameraProvider.h rename to ScopeOneCore/sdk/include/scopeone/CameraProvider.h diff --git a/ScopeOneCore/include/scopeone/DaqDevice.h b/ScopeOneCore/sdk/include/scopeone/DaqDevice.h similarity index 82% rename from ScopeOneCore/include/scopeone/DaqDevice.h rename to ScopeOneCore/sdk/include/scopeone/DaqDevice.h index 59d8185..97c3611 100644 --- a/ScopeOneCore/include/scopeone/DaqDevice.h +++ b/ScopeOneCore/sdk/include/scopeone/DaqDevice.h @@ -2,11 +2,9 @@ #include #include -#include #include #include #include -#include #include "scopeone/scopeone_sdk_export.h" @@ -153,27 +151,6 @@ namespace scopeone::core QList digitalTasks; QList routes; }; - - class SCOPEONE_SDK_EXPORT DaqController : public QObject - { - Q_OBJECT - - public: - explicit DaqController(QObject* parent = nullptr); - ~DaqController() override; - - virtual bool start(const DaqSessionConfig& config, - QString* errorMessage = nullptr) = 0; - virtual void stop() = 0; - virtual DaqState state() const = 0; - virtual QString stateMessage() const = 0; - - signals: - void stateChanged(scopeone::core::DaqState state, - const QString& message); - void controllerError(const QString& errorMessage); - void inputDataReady(const scopeone::core::DaqInputChunk& chunk); - }; } Q_DECLARE_METATYPE(scopeone::core::DaqState) diff --git a/ScopeOneCore/sdk/include/scopeone/DaqProvider.h b/ScopeOneCore/sdk/include/scopeone/DaqProvider.h new file mode 100644 index 0000000..8b4cdb9 --- /dev/null +++ b/ScopeOneCore/sdk/include/scopeone/DaqProvider.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +#include + +#include "scopeone/DaqDevice.h" +#include "scopeone/scopeone_sdk_export.h" + +namespace scopeone::core +{ + // A hardware provider of data acquisition devices, which run timed analog, digital and + // counter tasks: they put out scan waveforms and clocks and read their inputs continuously + class SCOPEONE_SDK_EXPORT DaqProvider + { + public: + using InputSink = std::function; + using StateSink = std::function; + + virtual ~DaqProvider(); + + virtual QList daqDevices() const = 0; + // The sinks may be called from any thread, and the input sink in the order of the samples + virtual void setDaqSinks(InputSink input, StateSink state) = 0; + // Raster scans of the session are already expanded into its tasks + virtual bool startDaq(const DaqSessionConfig& config, + QString* errorMessage = nullptr) = 0; + virtual void stopDaq(const QString& deviceId) = 0; + }; +} diff --git a/ScopeOneCore/include/scopeone/DriverHostProviderPlugin.h b/ScopeOneCore/sdk/include/scopeone/Driver.h similarity index 51% rename from ScopeOneCore/include/scopeone/DriverHostProviderPlugin.h rename to ScopeOneCore/sdk/include/scopeone/Driver.h index 5e7caec..47a4a2b 100644 --- a/ScopeOneCore/include/scopeone/DriverHostProviderPlugin.h +++ b/ScopeOneCore/sdk/include/scopeone/Driver.h @@ -7,10 +7,12 @@ namespace scopeone::core { - class DriverHostProviderPlugin + // The entry point of a driver library. ScopeOne_DriverHost loads the library, gets this + // object from Qt's plugin loader and asks it for the provider of the devices. + class Driver { public: - virtual ~DriverHostProviderPlugin() = default; + virtual ~Driver() = default; virtual QString providerId() const = 0; virtual HardwareProviderPtr createProvider(const QJsonObject& options, @@ -18,6 +20,5 @@ namespace scopeone::core }; } -#define ScopeOneDriverHostProviderPlugin_iid "org.scopeone.DriverHostProviderPlugin/1.0" -Q_DECLARE_INTERFACE(scopeone::core::DriverHostProviderPlugin, - ScopeOneDriverHostProviderPlugin_iid) +#define ScopeOneDriver_iid "org.scopeone.Driver/1.0" +Q_DECLARE_INTERFACE(scopeone::core::Driver, ScopeOneDriver_iid) diff --git a/ScopeOneCore/include/scopeone/HardwarePluginSDK.h b/ScopeOneCore/sdk/include/scopeone/DriverSDK.h similarity index 56% rename from ScopeOneCore/include/scopeone/HardwarePluginSDK.h rename to ScopeOneCore/sdk/include/scopeone/DriverSDK.h index 62b7473..579dff0 100644 --- a/ScopeOneCore/include/scopeone/HardwarePluginSDK.h +++ b/ScopeOneCore/sdk/include/scopeone/DriverSDK.h @@ -1,11 +1,12 @@ #pragma once -// Everything a hardware plugin is written against. These headers are the public contract of +// Everything a driver is written against. These headers are the public contract of // ScopeOne for code outside this repository. #include "scopeone/CameraProvider.h" -#include "scopeone/DriverHostProviderPlugin.h" +#include "scopeone/DaqProvider.h" +#include "scopeone/Driver.h" #include "scopeone/HardwareCapabilities.h" #include "scopeone/HardwareProvider.h" #include "scopeone/HardwareTypes.h" #include "scopeone/ImageFrame.h" -#include "scopeone/PluginManifest.h" +#include "scopeone/SignalSourceProvider.h" diff --git a/ScopeOneCore/include/scopeone/HardwareCapabilities.h b/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h similarity index 100% rename from ScopeOneCore/include/scopeone/HardwareCapabilities.h rename to ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h diff --git a/ScopeOneCore/include/scopeone/HardwareProvider.h b/ScopeOneCore/sdk/include/scopeone/HardwareProvider.h similarity index 100% rename from ScopeOneCore/include/scopeone/HardwareProvider.h rename to ScopeOneCore/sdk/include/scopeone/HardwareProvider.h diff --git a/ScopeOneCore/include/scopeone/HardwareTypes.h b/ScopeOneCore/sdk/include/scopeone/HardwareTypes.h similarity index 100% rename from ScopeOneCore/include/scopeone/HardwareTypes.h rename to ScopeOneCore/sdk/include/scopeone/HardwareTypes.h diff --git a/ScopeOneCore/include/scopeone/ImageFrame.h b/ScopeOneCore/sdk/include/scopeone/ImageFrame.h similarity index 100% rename from ScopeOneCore/include/scopeone/ImageFrame.h rename to ScopeOneCore/sdk/include/scopeone/ImageFrame.h diff --git a/ScopeOneCore/include/scopeone/SignalSource.h b/ScopeOneCore/sdk/include/scopeone/SignalSource.h similarity index 85% rename from ScopeOneCore/include/scopeone/SignalSource.h rename to ScopeOneCore/sdk/include/scopeone/SignalSource.h index c765b0f..92472c7 100644 --- a/ScopeOneCore/include/scopeone/SignalSource.h +++ b/ScopeOneCore/sdk/include/scopeone/SignalSource.h @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -10,7 +9,6 @@ #include #include #include -#include #include "scopeone/scopeone_sdk_export.h" @@ -180,28 +178,6 @@ namespace scopeone::core QVector m_pendingMarkers; QList m_readyChunks; }; - - class SCOPEONE_SDK_EXPORT SignalSource : public QObject - { - Q_OBJECT - - public: - explicit SignalSource(QObject* parent = nullptr); - ~SignalSource() override; - - virtual bool start(const SignalAcquisitionConfig& config, - QString* errorMessage = nullptr) = 0; - virtual void stop() = 0; - virtual SignalSourceState state() const = 0; - virtual QString stateMessage() const = 0; - - signals: - void timeSeriesReady(const scopeone::core::TimeSeriesChunk& chunk); - void timestampedEventsReady(const scopeone::core::TimestampedEventChunk& chunk); - void stateChanged(scopeone::core::SignalSourceState state, - const QString& message); - void sourceError(const QString& errorMessage); - }; } Q_DECLARE_METATYPE(scopeone::core::SignalSourceState) diff --git a/ScopeOneCore/sdk/include/scopeone/SignalSourceProvider.h b/ScopeOneCore/sdk/include/scopeone/SignalSourceProvider.h new file mode 100644 index 0000000..e3f7d5b --- /dev/null +++ b/ScopeOneCore/sdk/include/scopeone/SignalSourceProvider.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#include + +#include "scopeone/SignalSource.h" +#include "scopeone/scopeone_sdk_export.h" + +namespace scopeone::core +{ + // A hardware provider whose devices put out one value over time, such as a PMT, an APD or a + // photon counter. It hands over the stream and the scan markers; ScopeOne plots the stream + // and assembles scan images from it. + class SCOPEONE_SDK_EXPORT SignalSourceProvider + { + public: + using TimeSeriesSink = std::function; + using TimestampedEventSink = std::function; + using StateSink = std::function; + + virtual ~SignalSourceProvider(); + + virtual QList signalSources() const = 0; + // The sinks may be called from any thread, and each in the order of the stream + virtual void setSignalSinks(TimeSeriesSink timeSeries, + TimestampedEventSink timestampedEvents, + StateSink state) = 0; + virtual bool startSignal(const SignalAcquisitionConfig& config, + QString* errorMessage = nullptr) = 0; + virtual void stopSignal(const QString& sourceId) = 0; + }; +} diff --git a/ScopeOneCore/include/scopeone/scopeone_sdk_export.h b/ScopeOneCore/sdk/include/scopeone/scopeone_sdk_export.h similarity index 90% rename from ScopeOneCore/include/scopeone/scopeone_sdk_export.h rename to ScopeOneCore/sdk/include/scopeone/scopeone_sdk_export.h index 6460fba..a96d046 100644 --- a/ScopeOneCore/include/scopeone/scopeone_sdk_export.h +++ b/ScopeOneCore/sdk/include/scopeone/scopeone_sdk_export.h @@ -1,7 +1,7 @@ #pragma once #if defined(_WIN32) || defined(__CYGWIN__) -# if defined(SCOPEONE_CORE_EXPORTS) +# if defined(SCOPEONE_SDK_EXPORTS) # define SCOPEONE_SDK_EXPORT __declspec(dllexport) # else # define SCOPEONE_SDK_EXPORT __declspec(dllimport) diff --git a/ScopeOneCore/src/HardwareProvider.cpp b/ScopeOneCore/sdk/src/HardwareProvider.cpp similarity index 74% rename from ScopeOneCore/src/HardwareProvider.cpp rename to ScopeOneCore/sdk/src/HardwareProvider.cpp index d1d1d02..9f2ef57 100644 --- a/ScopeOneCore/src/HardwareProvider.cpp +++ b/ScopeOneCore/sdk/src/HardwareProvider.cpp @@ -1,6 +1,8 @@ #include "scopeone/CameraProvider.h" #include "scopeone/HardwareCapabilities.h" +#include "scopeone/DaqProvider.h" #include "scopeone/HardwareProvider.h" +#include "scopeone/SignalSourceProvider.h" namespace scopeone::core { @@ -11,4 +13,6 @@ namespace scopeone::core StateProvider::~StateProvider() = default; ConfigurationProvider::~ConfigurationProvider() = default; CameraProvider::~CameraProvider() = default; + SignalSourceProvider::~SignalSourceProvider() = default; + DaqProvider::~DaqProvider() = default; } diff --git a/ScopeOneCore/sdk/src/SignalSource.cpp b/ScopeOneCore/sdk/src/SignalSource.cpp new file mode 100644 index 0000000..7cec66a --- /dev/null +++ b/ScopeOneCore/sdk/src/SignalSource.cpp @@ -0,0 +1,143 @@ +#include "scopeone/SignalSource.h" + +#include +#include +#include + +namespace scopeone::core +{ + EventCountBinner::EventCountBinner(const QString& sourceId, + const QString& quantity, + const QString& unit, + double tickPeriodSeconds, + double sampleIntervalSeconds) + : m_sourceId(sourceId) + , m_quantity(quantity) + , m_unit(unit) + , m_tickPeriodSeconds(tickPeriodSeconds) + , m_ticksPerSample(std::max( + 1, + static_cast(std::llround( + sampleIntervalSeconds / tickPeriodSeconds)))) + , m_nextSampleTick(m_ticksPerSample) + , m_sampleIntervalSeconds( + static_cast(m_ticksPerSample) * tickPeriodSeconds) + { + } + + void EventCountBinner::addEvent(quint64 tick) + { + const quint64 bin = binForTick(tick); + advanceToBin(bin); + if (bin == m_currentSample) + { + ++m_currentEventCount; + ++m_totalInputEvents; + } + } + + void EventCountBinner::addMarker(quint64 tick, quint32 code) + { + advanceToTick(tick); + m_pendingMarkers.append({static_cast(tick) * m_tickPeriodSeconds, code}); + ++m_totalMarkers; + } + + void EventCountBinner::advanceToTick(quint64 tick) + { + advanceToBin(binForTick(tick)); + } + + void EventCountBinner::advanceToElapsedSeconds(double elapsedSeconds) + { + if (elapsedSeconds > 0.0) + { + advanceToBin(static_cast(elapsedSeconds / m_sampleIntervalSeconds)); + } + } + + bool EventCountBinner::hasReadyChunks() const + { + return !m_readyChunks.isEmpty(); + } + + QList EventCountBinner::takeReadyChunks() + { + return std::exchange(m_readyChunks, QList{}); + } + + QList EventCountBinner::takeCompletedChunks() + { + finishChunk(); + return takeReadyChunks(); + } + + void EventCountBinner::appendCompletedValue(double value) + { + constexpr qsizetype kMaximumChunkSamples = 65536; + m_completedValues.append(value); + if (m_completedValues.size() >= kMaximumChunkSamples) + { + finishChunk(); + } + } + + void EventCountBinner::finishChunk() + { + if (m_completedValues.isEmpty()) + { + return; + } + TimeSeriesChunk chunk; + chunk.sourceId = m_sourceId; + chunk.quantity = m_quantity; + chunk.unit = m_unit; + chunk.startTimeSeconds = static_cast(m_firstCompletedSample) + * m_sampleIntervalSeconds; + chunk.sampleIntervalSeconds = m_sampleIntervalSeconds; + chunk.values.swap(m_completedValues); + const double chunkEndTime = static_cast( + m_firstCompletedSample + static_cast(chunk.values.size())) + * m_sampleIntervalSeconds; + int markerCount = 0; + while (markerCount < m_pendingMarkers.size() + && m_pendingMarkers[markerCount].timeSeconds < chunkEndTime) + { + ++markerCount; + } + if (markerCount > 0) + { + chunk.markers = m_pendingMarkers.mid(0, markerCount); + m_pendingMarkers.remove(0, markerCount); + } + chunk.totalInputEvents = m_totalInputEvents; + chunk.totalMarkers = m_totalMarkers; + m_firstCompletedSample += static_cast(chunk.values.size()); + m_readyChunks.append(std::move(chunk)); + } + + quint64 EventCountBinner::binForTick(quint64 tick) const + { + if (tick >= m_currentSampleTick && tick < m_nextSampleTick) + { + return m_currentSample; + } + return tick / m_ticksPerSample; + } + + void EventCountBinner::advanceToBin(quint64 targetBin) + { + if (targetBin <= m_currentSample) + { + return; + } + while (m_currentSample < targetBin) + { + appendCompletedValue(static_cast(m_currentEventCount)); + m_currentEventCount = 0; + ++m_currentSample; + } + m_currentSampleTick = m_currentSample * m_ticksPerSample; + m_nextSampleTick = m_currentSampleTick + m_ticksPerSample; + } +} diff --git a/ScopeOneCore/src/DaqDeviceManager.cpp b/ScopeOneCore/src/DaqDeviceManager.cpp index c22e69a..d96f1de 100644 --- a/ScopeOneCore/src/DaqDeviceManager.cpp +++ b/ScopeOneCore/src/DaqDeviceManager.cpp @@ -1,22 +1,15 @@ #include "internal/DaqDeviceManager.h" -#include "internal/NIDaqmx.h" +#include "internal/HardwareRuntime.h" + +#include +#include #include #include #include #include -namespace scopeone::core -{ - DaqController::DaqController(QObject* parent) - : QObject(parent) - { - } - - DaqController::~DaqController() = default; -} - namespace scopeone::core::internal { namespace @@ -79,27 +72,24 @@ namespace scopeone::core::internal } } - DaqDeviceManager::DaqDeviceManager(QObject* parent) + DaqDeviceManager::DaqDeviceManager(DeviceRegistry* registry, QObject* parent) : QObject(parent) + , m_registry(registry) { + connect(m_registry, &DeviceRegistry::changed, this, &DaqDeviceManager::refreshProviderDevices); + refreshProviderDevices(); } DaqDeviceManager::~DaqDeviceManager() { - for (const QPointer& controller : std::as_const(m_controllers)) + for (const HardwareProviderPtr& provider : std::as_const(m_attachedProviders)) { - delete controller.data(); + detachProvider(provider); } - m_controllers.clear(); - m_factories.clear(); } QList DaqDeviceManager::devices() const { - if (!m_devicesRegistered) - { - const_cast(this)->registerDevices(); - } QList result = m_descriptors.values(); std::sort(result.begin(), result.end(), [](const DaqDeviceDescriptor& left, @@ -113,12 +103,9 @@ namespace scopeone::core::internal bool DaqDeviceManager::start(const DaqSessionConfig& config, QString* errorMessage) { - if (!m_devicesRegistered) - { - registerDevices(); - } const QString deviceId = config.deviceId.trimmed(); - if (!m_descriptors.contains(deviceId)) + const HardwareProviderPtr provider = m_providerByDevice.value(deviceId); + if (!provider) { if (errorMessage) { @@ -133,96 +120,125 @@ namespace scopeone::core::internal { expandRasterScan(scan, expanded); } - DaqController* deviceController = controller(deviceId, errorMessage); - return deviceController && deviceController->start(expanded, errorMessage); + return dynamic_cast(provider.get())->startDaq(expanded, errorMessage); } void DaqDeviceManager::stop(const QString& deviceId) { - if (DaqController* deviceController = m_controllers.value(deviceId.trimmed())) + const QString id = deviceId.trimmed(); + if (const HardwareProviderPtr provider = m_providerByDevice.value(id)) { - deviceController->stop(); + dynamic_cast(provider.get())->stopDaq(id); } } DaqState DaqDeviceManager::state(const QString& deviceId) const { - const QString id = deviceId.trimmed(); - const QPointer controller = m_controllers.value(id); - return controller ? controller->state() : m_states.value(id, DaqState::Idle); + return m_states.value(deviceId.trimmed(), DaqState::Idle); } QString DaqDeviceManager::stateMessage(const QString& deviceId) const { - const QString id = deviceId.trimmed(); - const QPointer controller = m_controllers.value(id); - return controller ? controller->stateMessage() - : m_messages.value(id, QStringLiteral("DAQ device is idle")); + return m_messages.value(deviceId.trimmed(), QStringLiteral("DAQ device is idle")); } - void DaqDeviceManager::registerDevices() + // Follows the drivers as they are registered and removed, like the signal sources + void DaqDeviceManager::refreshProviderDevices() { - m_devicesRegistered = true; - m_factories.push_back(std::make_unique()); - for (const std::unique_ptr& factory : m_factories) + QHash current; + for (const HardwareProviderDescriptor& descriptor : m_registry->providers()) + { + const HardwareProviderPtr provider = m_registry->provider(descriptor.id); + if (dynamic_cast(provider.get())) + { + current.insert(descriptor.id, provider); + } + } + bool changed = false; + for (auto it = m_attachedProviders.begin(); it != m_attachedProviders.end();) + { + if (current.value(it.key()) == it.value()) + { + ++it; + continue; + } + detachProvider(it.value()); + it = m_attachedProviders.erase(it); + changed = true; + } + for (auto it = current.cbegin(); it != current.cend(); ++it) { - for (const DaqDeviceDescriptor& descriptor : factory->devices()) + if (m_attachedProviders.contains(it.key())) + { + continue; + } + auto* const daqProvider = dynamic_cast(it.value().get()); + const QPointer self(this); + daqProvider->setDaqSinks( + [self](const DaqInputChunk& chunk) + { + QMetaObject::invokeMethod(self, [self, chunk]() { if (self) emit self->inputDataReady(chunk); }, + Qt::QueuedConnection); + }, + [self](const QString& deviceId, DaqState state, const QString& message) + { + QMetaObject::invokeMethod(self, [self, deviceId, state, message]() + { + if (self) self->updateState(deviceId, state, message); + }, Qt::QueuedConnection); + }); + for (const DaqDeviceDescriptor& descriptor : daqProvider->daqDevices()) { const QString deviceId = descriptor.id.trimmed(); if (deviceId.isEmpty() || m_descriptors.contains(deviceId)) { + qWarning().noquote() << QStringLiteral("Hardware provider %1: invalid or duplicate DAQ device %2") + .arg(it.key(), descriptor.id); continue; } m_descriptors.insert(deviceId, descriptor); - m_factoryByDevice.insert(deviceId, factory.get()); + m_providerByDevice.insert(deviceId, it.value()); m_states.insert(deviceId, DaqState::Idle); m_messages.insert(deviceId, QStringLiteral("DAQ device is idle")); } + m_attachedProviders.insert(it.key(), it.value()); + changed = true; + } + if (changed) + { + emit devicesChanged(); } } - DaqController* DaqDeviceManager::controller(const QString& deviceId, - QString* errorMessage) + void DaqDeviceManager::detachProvider(const HardwareProviderPtr& provider) { - const QString id = deviceId.trimmed(); - if (DaqController* existing = m_controllers.value(id)) - { - return existing; - } - DaqDeviceFactory* factory = m_factoryByDevice.value(id); - if (!factory) + dynamic_cast(provider.get())->setDaqSinks({}, {}); + for (auto it = m_providerByDevice.begin(); it != m_providerByDevice.end();) { - if (errorMessage) + if (it.value() != provider) { - *errorMessage = QStringLiteral("Unknown DAQ device: %1").arg(id); + ++it; + continue; } - return nullptr; + m_descriptors.remove(it.key()); + m_states.remove(it.key()); + m_messages.remove(it.key()); + it = m_providerByDevice.erase(it); } - DaqController* created = factory->createController(id, this); - if (!created) + } + + void DaqDeviceManager::updateState(const QString& deviceId, DaqState state, const QString& message) + { + if (!m_providerByDevice.contains(deviceId)) { - if (errorMessage) - { - *errorMessage = QStringLiteral("Failed to create DAQ controller: %1").arg(id); - } - return nullptr; + return; + } + m_states.insert(deviceId, state); + m_messages.insert(deviceId, message); + emit stateChanged(deviceId, state, message); + if (state == DaqState::Error) + { + emit deviceError(deviceId, message); } - - connect(created, &DaqController::stateChanged, - this, [this, id](DaqState state, const QString& message) - { - m_states.insert(id, state); - m_messages.insert(id, message); - emit stateChanged(id, state, message); - }); - connect(created, &DaqController::controllerError, - this, [this, id](const QString& message) - { - emit deviceError(id, message); - }); - connect(created, &DaqController::inputDataReady, - this, &DaqDeviceManager::inputDataReady); - m_controllers.insert(id, created); - return created; } } diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index dbfd82a..4a13879 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -49,12 +50,16 @@ #include "MMCore.h" #include "internal/DriverHostProtocol.h" +#include "internal/HardwareStreamCodec.h" #include "internal/SharedFrameRing.h" +#include "internal/StreamRing.h" #include "scopeone/CameraProvider.h" -#include "scopeone/DriverHostProviderPlugin.h" +#include "scopeone/DaqProvider.h" +#include "scopeone/Driver.h" #include "scopeone/PluginManifest.h" #include "scopeone/HardwareCapabilities.h" #include "scopeone/SharedFrame.h" +#include "scopeone/SignalSourceProvider.h" namespace scopeone::core::internal { @@ -1490,6 +1495,30 @@ namespace scopeone::core::internal void emitPreviewStateEvent(const QString& deviceId, bool running); void emitFrameAvailableEvent(const QString& deviceId, quint64 frameIndex); + // The ring a signal source or a DAQ device writes its chunks to while it runs + struct StreamTransport + { + QString streamId; + QString shmKey; + QSharedMemory shm; + streamring::Writer writer; + QMutex writeMutex; + std::atomic_bool notifyQueued{false}; + }; + + void installStreamSinks(); + bool handleStreamRequest(quint64 requestId, + const QString& type, + const QJsonObject& message, + const std::function& reply); + std::shared_ptr openStream(const QString& streamId, + qsizetype capacity, + QString* errorMessage); + void closeStream(const QString& streamId); + void writeStream(const QString& streamId, streamring::RecordType type, const QByteArray& payload); + qsizetype signalStreamCapacity(const SignalAcquisitionConfig& config) const; + static qsizetype daqStreamCapacity(const DaqSessionConfig& config); + QString m_pluginPath; QString m_providerId; QString m_hostKey; @@ -1508,7 +1537,15 @@ namespace scopeone::core::internal ShutterProvider* m_shutter{nullptr}; StateProvider* m_state{nullptr}; ConfigurationProvider* m_configuration{nullptr}; + SignalSourceProvider* m_signals{nullptr}; + DaqProvider* m_daq{nullptr}; + QList m_signalSources; + QList m_daqDevices; + QHash> m_streams; + QMutex m_streamsMutex; + quint64 m_nextStreamIndex{0}; std::atomic_bool m_acceptFrames{false}; + std::atomic_bool m_acceptStreams{false}; }; bool ProviderDriverRuntime::initializeRuntime() @@ -1518,7 +1555,7 @@ namespace scopeone::core::internal PluginManifest manifest; if (!parsePluginManifest( m_pluginLoader->metaData().value(QStringLiteral("MetaData")).toObject(), - PluginKind::Hardware, + PluginKind::Driver, manifest, &m_lastError)) { @@ -1538,10 +1575,10 @@ namespace scopeone::core::internal return false; } - auto* const factory = qobject_cast(instance); + auto* const factory = qobject_cast(instance); if (!factory) { - m_lastError = QStringLiteral("Module does not implement DriverHostProviderPlugin"); + m_lastError = QStringLiteral("Module does not implement Driver"); return false; } if (factory->providerId() != m_providerId) @@ -1575,6 +1612,8 @@ namespace scopeone::core::internal m_shutter = dynamic_cast(provider); m_state = dynamic_cast(provider); m_configuration = dynamic_cast(provider); + m_signals = dynamic_cast(provider); + m_daq = dynamic_cast(provider); QSet logicalIds; int cameraIndex = 0; @@ -1625,6 +1664,227 @@ namespace scopeone::core::internal m_acceptFrames.store(true, std::memory_order_release); m_camera->setFrameSink([this](const ImageFrame& frame) { enqueueFrame(frame); }); } + installStreamSinks(); + return true; + } + + void ProviderDriverRuntime::installStreamSinks() + { + if (m_signals) + { + m_signalSources = m_signals->signalSources(); + m_signals->setSignalSinks( + [this](const TimeSeriesChunk& chunk) + { + writeStream(QStringLiteral("signal:") + chunk.sourceId, + streamring::RecordType::TimeSeries, hardwarestream::encode(chunk)); + }, + [this](const TimestampedEventChunk& chunk) + { + writeStream(QStringLiteral("signal:") + chunk.sourceId, + streamring::RecordType::TimestampedEvents, hardwarestream::encode(chunk)); + }, + [this](const QString& sourceId, SignalSourceState state, const QString& message) + { + QMetaObject::invokeMethod(this, [this, sourceId, state, message]() + { + QJsonObject event = makeEvent(driverhost::kEventSignalState); + event.insert(driverhost::kSourceIdField, sourceId); + event.insert(driverhost::kStateField, static_cast(state)); + event.insert(driverhost::kStateMessageField, message); + emit eventReady(event); + }, Qt::QueuedConnection); + }); + } + if (m_daq) + { + m_daqDevices = m_daq->daqDevices(); + m_daq->setDaqSinks( + [this](const DaqInputChunk& chunk) + { + writeStream(QStringLiteral("daq:") + chunk.deviceId, + streamring::RecordType::DaqInput, hardwarestream::encode(chunk)); + }, + [this](const QString& deviceId, DaqState state, const QString& message) + { + QMetaObject::invokeMethod(this, [this, deviceId, state, message]() + { + QJsonObject event = makeEvent(driverhost::kEventDaqState); + event.insert(driverhost::kDeviceIdField, deviceId); + event.insert(driverhost::kStateField, static_cast(state)); + event.insert(driverhost::kStateMessageField, message); + emit eventReady(event); + }, Qt::QueuedConnection); + }); + } + m_acceptStreams.store(m_signals || m_daq, std::memory_order_release); + } + + std::shared_ptr ProviderDriverRuntime::openStream( + const QString& streamId, + qsizetype capacity, + QString* errorMessage) + { + auto stream = std::make_shared(); + stream->streamId = streamId; + stream->shmKey = driverhost::streamSharedMemoryKey(m_hostKey, m_nextStreamIndex++); + if (!streamring::createSharedRing(stream->shm, stream->shmKey, capacity, errorMessage)) + { + return {}; + } + stream->writer = streamring::Writer(stream->shm.data()); + QMutexLocker locker(&m_streamsMutex); + m_streams.insert(streamId, stream); + return stream; + } + + void ProviderDriverRuntime::closeStream(const QString& streamId) + { + QMutexLocker locker(&m_streamsMutex); + m_streams.remove(streamId); + } + + void ProviderDriverRuntime::writeStream(const QString& streamId, + streamring::RecordType type, + const QByteArray& payload) + { + if (!m_acceptStreams.load(std::memory_order_acquire)) + { + return; + } + std::shared_ptr stream; + { + QMutexLocker locker(&m_streamsMutex); + stream = m_streams.value(streamId); + } + if (!stream) + { + return; + } + { + QMutexLocker locker(&stream->writeMutex); + stream->writer.write(type, payload.constData(), static_cast(payload.size())); + } + // One notice stands for every record written until it is sent, as the reader drains the ring + if (!stream->notifyQueued.exchange(true)) + { + QMetaObject::invokeMethod(this, [this, stream]() + { + stream->notifyQueued.store(false); + QJsonObject event = makeEvent(driverhost::kEventStreamAvailable); + event.insert(driverhost::kStreamIdField, stream->streamId); + emit eventReady(event); + }, Qt::QueuedConnection); + } + } + + // Two seconds of the stream. Photon events have no fixed rate, so their rings are sized for + // bright scans instead. + qsizetype ProviderDriverRuntime::signalStreamCapacity(const SignalAcquisitionConfig& config) const + { + constexpr qsizetype kMinimum = 4 * 1024 * 1024; + constexpr qsizetype kEvents = 64 * 1024 * 1024; + constexpr qsizetype kMaximum = 256 * 1024 * 1024; + const auto source = std::find_if(m_signalSources.cbegin(), m_signalSources.cend(), + [&config](const SignalSourceDescriptor& descriptor) + { + return descriptor.id == config.sourceId; + }); + if (config.scanImage.enabled || config.publishTimestampedEvents + || (source != m_signalSources.cend() && source->streamType == SignalStreamType::TimestampedEvents)) + { + return kEvents; + } + const double bytesPerSecond = sizeof(double) / (std::max)(config.sampleIntervalSeconds, 1.0e-9); + return std::clamp(static_cast(2.0 * std::min(bytesPerSecond, 1.0e12)), kMinimum, kMaximum); + } + + qsizetype ProviderDriverRuntime::daqStreamCapacity(const DaqSessionConfig& config) + { + constexpr qsizetype kMinimum = 4 * 1024 * 1024; + constexpr qsizetype kMaximum = 256 * 1024 * 1024; + double bytesPerSecond = 0.0; + for (const DaqAnalogTaskConfig& task : config.analogTasks) + { + if (task.direction == DaqTaskDirection::Input) + { + bytesPerSecond += task.timing.sampleRateHz * task.channels.size() * sizeof(double); + } + } + for (const DaqDigitalTaskConfig& task : config.digitalTasks) + { + if (task.direction == DaqTaskDirection::Input) + { + bytesPerSecond += task.timing.sampleRateHz * sizeof(quint32); + } + } + return std::clamp(static_cast(2.0 * std::min(bytesPerSecond, 1.0e12)), kMinimum, kMaximum); + } + + bool ProviderDriverRuntime::handleStreamRequest(quint64 requestId, + const QString& type, + const QJsonObject& message, + const std::function& reply) + { + const bool signalCommand = type == driverhost::kCommandStartSignal + || type == driverhost::kCommandStopSignal; + const bool daqCommand = type == driverhost::kCommandStartDaq || type == driverhost::kCommandStopDaq; + if (!signalCommand && !daqCommand) + { + return false; + } + if ((signalCommand && !m_signals) || (daqCommand && !m_daq)) + { + reply(makeErrorResponse(type, requestId, QStringLiteral("Provider does not support %1") + .arg(signalCommand ? QStringLiteral("signal sources") + : QStringLiteral("DAQ devices")))); + return true; + } + if (type == driverhost::kCommandStopSignal) + { + m_signals->stopSignal(message.value(driverhost::kSourceIdField).toString()); + reply(makeResponse(type, requestId, true)); + return true; + } + if (type == driverhost::kCommandStopDaq) + { + m_daq->stopDaq(message.value(driverhost::kDeviceIdField).toString()); + reply(makeResponse(type, requestId, true)); + return true; + } + + const QJsonObject configObject = message.value(driverhost::kConfigField).toObject(); + QString streamId; + qsizetype capacity = 0; + SignalAcquisitionConfig signalConfig; + DaqSessionConfig daqConfig; + if (signalCommand) + { + signalConfig = hardwarestream::signalAcquisitionConfigFromJson(configObject); + streamId = QStringLiteral("signal:") + signalConfig.sourceId; + capacity = signalStreamCapacity(signalConfig); + } + else + { + daqConfig = hardwarestream::daqSessionConfigFromJson(configObject); + streamId = QStringLiteral("daq:") + daqConfig.deviceId; + capacity = daqStreamCapacity(daqConfig); + } + QString error; + const auto stream = openStream(streamId, capacity, &error); + const bool started = stream + && (signalCommand ? m_signals->startSignal(signalConfig, &error) + : m_daq->startDaq(daqConfig, &error)); + if (!started) + { + closeStream(streamId); + reply(makeErrorResponse(type, requestId, error.isEmpty() ? QStringLiteral("Start failed") : error)); + return true; + } + QJsonObject response = makeResponse(type, requestId, true); + response.insert(driverhost::kStreamIdField, streamId); + response.insert(driverhost::kSharedMemoryKeyField, stream->shmKey); + reply(response); return true; } @@ -1901,6 +2161,18 @@ namespace scopeone::core::internal devices.append(deviceDescription(device)); } response.insert(driverhost::kDevicesField, devices); + QJsonArray signalSources; + for (const SignalSourceDescriptor& source : m_signalSources) + { + signalSources.append(hardwarestream::toJson(source)); + } + response.insert(driverhost::kSignalSourcesField, signalSources); + QJsonArray daqDevices; + for (const DaqDeviceDescriptor& daqDevice : m_daqDevices) + { + daqDevices.append(hardwarestream::toJson(daqDevice)); + } + response.insert(driverhost::kDaqDevicesField, daqDevices); if (m_stage) { response.insert(driverhost::kDefaultXYStageField, m_stage->defaultXYStage()); @@ -1921,6 +2193,10 @@ namespace scopeone::core::internal reply(makeErrorResponse(type, requestId, QStringLiteral("Provider is not initialized"))); return; } + if (handleStreamRequest(requestId, type, message, reply)) + { + return; + } const bool configurationCommand = type == driverhost::kCommandListConfigGroups @@ -2400,6 +2676,29 @@ namespace scopeone::core::internal void ProviderDriverRuntime::stopForExit() { m_acceptFrames.store(false, std::memory_order_release); + m_acceptStreams.store(false, std::memory_order_release); + if (m_signals) + { + for (const SignalSourceDescriptor& source : std::as_const(m_signalSources)) + { + m_signals->stopSignal(source.id); + } + m_signals->setSignalSinks({}, {}, {}); + } + if (m_daq) + { + for (const DaqDeviceDescriptor& daqDevice : std::as_const(m_daqDevices)) + { + m_daq->stopDaq(daqDevice.id); + } + m_daq->setDaqSinks({}, {}); + } + { + QMutexLocker locker(&m_streamsMutex); + m_streams.clear(); + } + m_signals = nullptr; + m_daq = nullptr; for (const auto& transport : m_cameraTransports) { if (transport && transport->previewTimer) @@ -2718,8 +3017,8 @@ int main(int argc, char* argv[]) QCommandLineOption optDeviceId(QStringLiteral("deviceId"), QStringLiteral("Provider device ID"), QStringLiteral("id")); - QCommandLineOption optPlugin(QStringLiteral("plugin"), - QStringLiteral("Provider module path"), + QCommandLineOption optDriver(QStringLiteral("driver"), + QStringLiteral("Driver library path"), QStringLiteral("path")); QCommandLineOption optHostKey(QStringLiteral("hostKey"), QStringLiteral("DriverHost instance key"), @@ -2753,7 +3052,7 @@ int main(int argc, char* argv[]) parser.addOption(optProvider); parser.addOption(optDeviceId); - parser.addOption(optPlugin); + parser.addOption(optDriver); parser.addOption(optHostKey); parser.addOption(optProviderOption); parser.addOption(optAdapter); @@ -2816,11 +3115,11 @@ int main(int argc, char* argv[]) } else { - const QString pluginPath = parser.value(optPlugin).trimmed(); + const QString pluginPath = parser.value(optDriver).trimmed(); const QString hostKey = parser.value(optHostKey).trimmed(); if (pluginPath.isEmpty() || hostKey.isEmpty()) { - qCritical().noquote() << "External providers require --plugin and --hostKey"; + qCritical().noquote() << "External providers require --driver and --hostKey"; return 2; } diff --git a/ScopeOneCore/src/DriverHostProviderProxy.cpp b/ScopeOneCore/src/DriverHostProviderProxy.cpp index 5a08df4..dba8ad5 100644 --- a/ScopeOneCore/src/DriverHostProviderProxy.cpp +++ b/ScopeOneCore/src/DriverHostProviderProxy.cpp @@ -2,8 +2,12 @@ #include "internal/CameraRuntimeControl.h" #include "internal/DriverHostProtocol.h" +#include "internal/HardwareStreamCodec.h" #include "internal/SharedFrameRing.h" +#include "internal/StreamRing.h" #include "scopeone/CameraProvider.h" +#include "scopeone/DaqProvider.h" +#include "scopeone/SignalSourceProvider.h" #include "scopeone/HardwareCapabilities.h" #include "scopeone/SharedFrame.h" @@ -65,23 +69,37 @@ namespace scopeone::core::internal using FrameHandler = std::function; using PreviewHandler = std::function; + // Where the chunks and states of the signal sources and DAQ devices go, on the transport thread + struct StreamHandlers + { + SignalSourceProvider::TimeSeriesSink timeSeries; + SignalSourceProvider::TimestampedEventSink timestampedEvents; + SignalSourceProvider::StateSink signalState; + DaqProvider::InputSink daqInput; + DaqProvider::StateSink daqState; + }; + DriverHostProviderTransport(QString providerId, QString pluginPath, QString hostKey, QJsonObject options, FrameHandler frameHandler, - PreviewHandler previewHandler) + PreviewHandler previewHandler, + StreamHandlers streamHandlers) : m_providerId(std::move(providerId)) , m_pluginPath(std::move(pluginPath)) , m_hostKey(std::move(hostKey)) , m_options(std::move(options)) , m_frameHandler(std::move(frameHandler)) , m_previewHandler(std::move(previewHandler)) + , m_streamHandlers(std::move(streamHandlers)) { } bool initialize(HardwareProviderDescriptor& descriptor, QList& devices, + QList& signalSources, + QList& daqDevices, QString& defaultXYStage, QString& defaultZStage, QString* errorMessage) @@ -138,12 +156,13 @@ namespace scopeone::core::internal if (m_previewHandler) m_previewHandler(cameraId, false); } m_runningCameras.clear(); + endActiveStreams(); finishWaitingRequest(); }); QStringList arguments; arguments << QStringLiteral("--provider") << m_providerId - << QStringLiteral("--plugin") << m_pluginPath + << QStringLiteral("--driver") << m_pluginPath << QStringLiteral("--hostKey") << m_hostKey << QStringLiteral("--launcherPid") << QString::number(QCoreApplication::applicationPid()); @@ -265,12 +284,64 @@ namespace scopeone::core::internal m_cameraIds.append(device.logicalId); } } + for (const QJsonValue& value : response.value(driverhost::kSignalSourcesField).toArray()) + { + signalSources.append(hardwarestream::signalSourceDescriptorFromJson(value.toObject())); + } + for (const QJsonValue& value : response.value(driverhost::kDaqDevicesField).toArray()) + { + daqDevices.append(hardwarestream::daqDeviceDescriptorFromJson(value.toObject())); + } defaultXYStage = response.value(driverhost::kDefaultXYStageField).toString(); defaultZStage = response.value(driverhost::kDefaultZStageField).toString(); if (errorMessage) errorMessage->clear(); return true; } + // Starts a signal source or a DAQ session and attaches the ring DriverHost made for it + bool startStream(const QString& command, const QJsonObject& config, QString* errorMessage) + { + QJsonObject request; + request.insert(driverhost::kMessageTypeField, command); + request.insert(driverhost::kConfigField, config); + QJsonObject response; + if (!sendRequest(request, response, kControlRequestTimeoutMs, errorMessage) + || !responseSucceeded(response, errorMessage)) + { + return false; + } + const bool signal = command == driverhost::kCommandStartSignal; + const QString id = signal ? config.value(QStringLiteral("sourceId")).toString() + : config.value(QStringLiteral("deviceId")).toString(); + auto stream = std::make_shared(); + stream->streamId = response.value(driverhost::kStreamIdField).toString(); + if (!streamring::attachSharedRing(stream->shm, + response.value(driverhost::kSharedMemoryKeyField).toString(), + errorMessage)) + { + stopStream(signal ? driverhost::kCommandStopSignal : driverhost::kCommandStopDaq, id, nullptr); + return false; + } + stream->reader = streamring::Reader(stream->shm.data()); + m_streams.insert(stream->streamId, stream); + (signal ? m_activeSignals : m_activeDaqDevices).insert(id); + // Chunks written before the ring was attached have no notice of their own + drainStream(stream->streamId); + return true; + } + + bool stopStream(const QString& command, const QString& id, QString* errorMessage) + { + QJsonObject request; + request.insert(driverhost::kMessageTypeField, command); + request.insert(command == driverhost::kCommandStopSignal ? driverhost::kSourceIdField + : driverhost::kDeviceIdField, + id); + QJsonObject response; + return sendRequest(request, response, kControlRequestTimeoutMs, errorMessage) + && responseSucceeded(response, errorMessage); + } + bool sendRequest(QJsonObject request, QJsonObject& response, int timeoutMs, @@ -444,6 +515,7 @@ namespace scopeone::core::internal m_socket->disconnectFromServer(); } m_readers.clear(); + m_streams.clear(); m_socket.reset(); if (m_process && m_process->state() != QProcess::NotRunning) { @@ -461,6 +533,71 @@ namespace scopeone::core::internal } private: + struct StreamReader + { + QString streamId; + QSharedMemory shm; + streamring::Reader reader; + }; + + void drainStream(const QString& streamId) + { + const auto it = m_streams.constFind(streamId); + if (it == m_streams.cend()) return; + const std::shared_ptr stream = it.value(); + streamring::Record record; + while (stream->reader.read(record)) + { + if (record.droppedBefore > 0) + { + qWarning().noquote() << QStringLiteral("DriverHost '%1' dropped %2 chunks of %3 because ScopeOne read too slowly") + .arg(m_providerId).arg(record.droppedBefore).arg(streamId); + } + switch (record.type) + { + case streamring::RecordType::TimeSeries: + { + TimeSeriesChunk chunk; + if (hardwarestream::decode(record.payload, chunk) && m_streamHandlers.timeSeries) + m_streamHandlers.timeSeries(chunk); + break; + } + case streamring::RecordType::TimestampedEvents: + { + TimestampedEventChunk chunk; + if (hardwarestream::decode(record.payload, chunk) && m_streamHandlers.timestampedEvents) + m_streamHandlers.timestampedEvents(chunk); + break; + } + case streamring::RecordType::DaqInput: + { + DaqInputChunk chunk; + if (hardwarestream::decode(record.payload, chunk) && m_streamHandlers.daqInput) + m_streamHandlers.daqInput(chunk); + break; + } + case streamring::RecordType::Skip: + break; + } + } + } + + // The sources and sessions that were running when DriverHost went away end with an error + void endActiveStreams() + { + const QString message = QStringLiteral("DriverHost process exited"); + for (const QString& sourceId : std::exchange(m_activeSignals, {})) + { + if (m_streamHandlers.signalState) + m_streamHandlers.signalState(sourceId, SignalSourceState::Error, message); + } + for (const QString& deviceId : std::exchange(m_activeDaqDevices, {})) + { + if (m_streamHandlers.daqState) m_streamHandlers.daqState(deviceId, DaqState::Error, message); + } + m_streams.clear(); + } + struct Reader { QString cameraId; @@ -542,6 +679,39 @@ namespace scopeone::core::internal else m_runningCameras.remove(deviceId); if (m_previewHandler) m_previewHandler(deviceId, running); } + else if (type == driverhost::kEventStreamAvailable) + { + drainStream(event.value(driverhost::kStreamIdField).toString()); + } + else if (type == driverhost::kEventSignalState) + { + const QString sourceId = event.value(driverhost::kSourceIdField).toString(); + const auto state = static_cast(event.value(driverhost::kStateField).toInt()); + // The last chunks reach the reader before the state that ends the stream + drainStream(QStringLiteral("signal:") + sourceId); + if (state == SignalSourceState::Idle || state == SignalSourceState::Error) + { + m_activeSignals.remove(sourceId); + } + if (m_streamHandlers.signalState) + { + m_streamHandlers.signalState(sourceId, state, + event.value(driverhost::kStateMessageField).toString()); + } + } + else if (type == driverhost::kEventDaqState) + { + const auto state = static_cast(event.value(driverhost::kStateField).toInt()); + drainStream(QStringLiteral("daq:") + deviceId); + if (state == DaqState::Idle || state == DaqState::Error) + { + m_activeDaqDevices.remove(deviceId); + } + if (m_streamHandlers.daqState) + { + m_streamHandlers.daqState(deviceId, state, event.value(driverhost::kStateMessageField).toString()); + } + } else if (type == driverhost::kEventDriverHostError) { qWarning().noquote() @@ -600,6 +770,10 @@ namespace scopeone::core::internal QJsonObject m_options; FrameHandler m_frameHandler; PreviewHandler m_previewHandler; + StreamHandlers m_streamHandlers; + QHash> m_streams; + QSet m_activeSignals; + QSet m_activeDaqDevices; std::unique_ptr m_process; std::unique_ptr m_socket; QByteArray m_readBuffer; @@ -624,6 +798,8 @@ namespace scopeone::core::internal public ShutterProvider, public StateProvider, public ConfigurationProvider, + public SignalSourceProvider, + public DaqProvider, public CameraRuntimeControl { public: @@ -674,7 +850,8 @@ namespace scopeone::core::internal sink = m_previewStateSink; } if (sink) sink(anyRunning); - }); + }, + streamHandlers()); m_worker->moveToThread(&m_workerThread); connect(&m_workerThread, &QThread::finished, m_worker, &QObject::deleteLater); @@ -691,6 +868,8 @@ namespace scopeone::core::internal { initialized = worker->initialize(m_descriptor, m_devices, + m_signalSources, + m_daqDevices, m_defaultXYStage, m_defaultZStage, &error); @@ -714,6 +893,47 @@ namespace scopeone::core::internal HardwareProviderDescriptor descriptor() const override { return m_descriptor; } QList devices() const override { return m_devices; } + QList signalSources() const override { return m_signalSources; } + + void setSignalSinks(TimeSeriesSink timeSeries, + TimestampedEventSink timestampedEvents, + SignalSourceProvider::StateSink state) override + { + QMutexLocker locker(&m_sinkMutex); + m_timeSeriesSink = std::move(timeSeries); + m_timestampedEventSink = std::move(timestampedEvents); + m_signalStateSink = std::move(state); + } + + bool startSignal(const SignalAcquisitionConfig& config, QString* errorMessage) override + { + return startStream(driverhost::kCommandStartSignal, hardwarestream::toJson(config), errorMessage); + } + + void stopSignal(const QString& sourceId) override + { + stopStream(driverhost::kCommandStopSignal, sourceId); + } + + QList daqDevices() const override { return m_daqDevices; } + + void setDaqSinks(InputSink input, DaqProvider::StateSink state) override + { + QMutexLocker locker(&m_sinkMutex); + m_daqInputSink = std::move(input); + m_daqStateSink = std::move(state); + } + + bool startDaq(const DaqSessionConfig& config, QString* errorMessage) override + { + return startStream(driverhost::kCommandStartDaq, hardwarestream::toJson(config), errorMessage); + } + + void stopDaq(const QString& deviceId) override + { + stopStream(driverhost::kCommandStopDaq, deviceId); + } + void setFrameSink(FrameSink sink) override { QMutexLocker locker(&m_sinkMutex); @@ -1348,6 +1568,76 @@ namespace scopeone::core::internal return request(command, deviceId, fields, response, errorMessage); } + bool startStream(const QString& command, const QJsonObject& config, QString* errorMessage) + { + QMutexLocker requestLocker(&m_requestMutex); + bool ok = false; + QString error; + DriverHostProviderTransport* const worker = m_worker; + runOnWorker([worker, &command, &config, &ok, &error]() { ok = worker->startStream(command, config, &error); }); + if (errorMessage) *errorMessage = error; + return ok; + } + + void stopStream(const QString& command, const QString& id) + { + QMutexLocker requestLocker(&m_requestMutex); + DriverHostProviderTransport* const worker = m_worker; + runOnWorker([worker, &command, &id]() { worker->stopStream(command, id, nullptr); }); + } + + // The transport calls these on its thread; each hands the chunk or state to the sink Core set + DriverHostProviderTransport::StreamHandlers streamHandlers() + { + DriverHostProviderTransport::StreamHandlers handlers; + handlers.timeSeries = [this](const TimeSeriesChunk& chunk) + { + TimeSeriesSink sink; + { + QMutexLocker locker(&m_sinkMutex); + sink = m_timeSeriesSink; + } + if (sink) sink(chunk); + }; + handlers.timestampedEvents = [this](const TimestampedEventChunk& chunk) + { + TimestampedEventSink sink; + { + QMutexLocker locker(&m_sinkMutex); + sink = m_timestampedEventSink; + } + if (sink) sink(chunk); + }; + handlers.signalState = [this](const QString& sourceId, SignalSourceState state, const QString& message) + { + SignalSourceProvider::StateSink sink; + { + QMutexLocker locker(&m_sinkMutex); + sink = m_signalStateSink; + } + if (sink) sink(sourceId, state, message); + }; + handlers.daqInput = [this](const DaqInputChunk& chunk) + { + InputSink sink; + { + QMutexLocker locker(&m_sinkMutex); + sink = m_daqInputSink; + } + if (sink) sink(chunk); + }; + handlers.daqState = [this](const QString& deviceId, DaqState state, const QString& message) + { + DaqProvider::StateSink sink; + { + QMutexLocker locker(&m_sinkMutex); + sink = m_daqStateSink; + } + if (sink) sink(deviceId, state, message); + }; + return handlers; + } + bool setDeliveryMode(const QString& mode) { QMutexLocker requestLocker(&m_requestMutex); @@ -1390,6 +1680,13 @@ namespace scopeone::core::internal mutable QMutex m_sinkMutex; FrameSink m_frameSink; PreviewStateSink m_previewStateSink; + TimeSeriesSink m_timeSeriesSink; + TimestampedEventSink m_timestampedEventSink; + SignalSourceProvider::StateSink m_signalStateSink; + InputSink m_daqInputSink; + DaqProvider::StateSink m_daqStateSink; + QList m_signalSources; + QList m_daqDevices; mutable QMutex m_stateMutex; QSet m_runningCameras; bool m_recordingDelivery{false}; diff --git a/ScopeOneCore/src/HardwareStreamCodec.cpp b/ScopeOneCore/src/HardwareStreamCodec.cpp new file mode 100644 index 0000000..dc77fa9 --- /dev/null +++ b/ScopeOneCore/src/HardwareStreamCodec.cpp @@ -0,0 +1,546 @@ +#include "internal/HardwareStreamCodec.h" + +#include + +#include +#include + +namespace scopeone::core::internal::hardwarestream +{ + namespace + { + QJsonArray toJsonArray(const QStringList& values) + { + return QJsonArray::fromStringList(values); + } + + QStringList stringList(const QJsonValue& value) + { + QStringList result; + for (const QJsonValue& item : value.toArray()) + { + result.append(item.toString()); + } + return result; + } + + QString encodeUInt64(quint64 value) + { + return QString::number(value); + } + + quint64 decodeUInt64(const QJsonValue& value) + { + return value.toString().toULongLong(); + } + + // Waveforms can hold many samples, so they travel as the bytes of the array + template + QString encodeSamples(const QVector& samples) + { + return QString::fromLatin1(QByteArray(reinterpret_cast(samples.constData()), + samples.size() * static_cast(sizeof(T))) + .toBase64()); + } + + template + QVector decodeSamples(const QJsonValue& value) + { + const QByteArray bytes = QByteArray::fromBase64(value.toString().toLatin1()); + QVector samples(bytes.size() / static_cast(sizeof(T))); + std::memcpy(samples.data(), bytes.constData(), + static_cast(samples.size()) * sizeof(T)); + return samples; + } + + QJsonObject toJson(const DaqTaskTiming& timing) + { + return {{QStringLiteral("sampleClock"), timing.sampleClock}, + {QStringLiteral("sampleRateHz"), timing.sampleRateHz}, + {QStringLiteral("sampleEdge"), static_cast(timing.sampleEdge)}, + {QStringLiteral("sampleMode"), static_cast(timing.sampleMode)}, + {QStringLiteral("samplesPerChannel"), encodeUInt64(timing.samplesPerChannel)}, + {QStringLiteral("startTrigger"), timing.startTrigger}, + {QStringLiteral("startEdge"), static_cast(timing.startEdge)}}; + } + + DaqTaskTiming timingFromJson(const QJsonObject& object) + { + DaqTaskTiming timing; + timing.sampleClock = object.value(QStringLiteral("sampleClock")).toString(); + timing.sampleRateHz = object.value(QStringLiteral("sampleRateHz")).toDouble(); + timing.sampleEdge = static_cast(object.value(QStringLiteral("sampleEdge")).toInt()); + timing.sampleMode = static_cast(object.value(QStringLiteral("sampleMode")).toInt()); + timing.samplesPerChannel = decodeUInt64(object.value(QStringLiteral("samplesPerChannel"))); + timing.startTrigger = object.value(QStringLiteral("startTrigger")).toString(); + timing.startEdge = static_cast(object.value(QStringLiteral("startEdge")).toInt()); + return timing; + } + + class Output + { + public: + template + void value(T item) + { + static_assert(std::is_trivially_copyable_v); + m_bytes.append(reinterpret_cast(&item), sizeof(T)); + } + + void string(const QString& text) + { + const QByteArray utf8 = text.toUtf8(); + value(static_cast(utf8.size())); + m_bytes.append(utf8); + } + + void strings(const QStringList& texts) + { + value(static_cast(texts.size())); + for (const QString& text : texts) + { + string(text); + } + } + + template + void array(const QVector& items) + { + value(static_cast(items.size())); + m_bytes.append(reinterpret_cast(items.constData()), + items.size() * static_cast(sizeof(T))); + } + + QByteArray bytes() const { return m_bytes; } + + private: + QByteArray m_bytes; + }; + + class Input + { + public: + explicit Input(const QByteArray& bytes) + : m_bytes(bytes) + { + } + + template + T value() + { + T item{}; + if (!take(&item, sizeof(T))) + { + return {}; + } + return item; + } + + QString string() + { + const quint32 size = value(); + if (!m_ok || size > remaining()) + { + m_ok = false; + return {}; + } + const QString text = QString::fromUtf8(m_bytes.constData() + m_position, + static_cast(size)); + m_position += size; + return text; + } + + QStringList strings() + { + const quint32 count = value(); + QStringList texts; + for (quint32 index = 0; m_ok && index < count; ++index) + { + texts.append(string()); + } + return texts; + } + + template + QVector array() + { + const quint64 count = value(); + if (!m_ok || count > remaining() / sizeof(T)) + { + m_ok = false; + return {}; + } + QVector items(static_cast(count)); + take(items.data(), static_cast(count) * sizeof(T)); + return items; + } + + bool ok() const { return m_ok; } + + private: + quint64 remaining() const + { + return static_cast(m_bytes.size() - m_position); + } + + bool take(void* target, std::size_t size) + { + if (!m_ok || size > remaining()) + { + m_ok = false; + return false; + } + std::memcpy(target, m_bytes.constData() + m_position, size); + m_position += static_cast(size); + return true; + } + + const QByteArray& m_bytes; + qsizetype m_position{0}; + bool m_ok{true}; + }; + } + + QJsonObject toJson(const SignalSourceDescriptor& descriptor) + { + QJsonArray parameters; + for (const SignalParameterDescriptor& parameter : descriptor.parameters) + { + parameters.append(QJsonObject{ + {QStringLiteral("key"), parameter.key}, + {QStringLiteral("name"), parameter.name}, + {QStringLiteral("type"), static_cast(parameter.type)}, + {QStringLiteral("defaultValue"), QJsonValue::fromVariant(parameter.defaultValue)}, + {QStringLiteral("hasRange"), parameter.hasRange}, + {QStringLiteral("minimum"), parameter.minimum}, + {QStringLiteral("maximum"), parameter.maximum}, + {QStringLiteral("suffix"), parameter.suffix}, + {QStringLiteral("choices"), QJsonArray::fromVariantList(parameter.choices)}, + {QStringLiteral("choiceNames"), toJsonArray(parameter.choiceNames)}, + {QStringLiteral("fileFilter"), parameter.fileFilter}}); + } + return {{QStringLiteral("id"), descriptor.id}, + {QStringLiteral("name"), descriptor.name}, + {QStringLiteral("provider"), descriptor.provider}, + {QStringLiteral("quantity"), descriptor.quantity}, + {QStringLiteral("unit"), descriptor.unit}, + {QStringLiteral("streamType"), static_cast(descriptor.streamType)}, + {QStringLiteral("parameters"), parameters}}; + } + + SignalSourceDescriptor signalSourceDescriptorFromJson(const QJsonObject& object) + { + SignalSourceDescriptor descriptor; + descriptor.id = object.value(QStringLiteral("id")).toString(); + descriptor.name = object.value(QStringLiteral("name")).toString(); + descriptor.provider = object.value(QStringLiteral("provider")).toString(); + descriptor.quantity = object.value(QStringLiteral("quantity")).toString(); + descriptor.unit = object.value(QStringLiteral("unit")).toString(); + descriptor.streamType = static_cast(object.value(QStringLiteral("streamType")).toInt()); + for (const QJsonValue& value : object.value(QStringLiteral("parameters")).toArray()) + { + const QJsonObject item = value.toObject(); + SignalParameterDescriptor parameter; + parameter.key = item.value(QStringLiteral("key")).toString(); + parameter.name = item.value(QStringLiteral("name")).toString(); + parameter.type = static_cast(item.value(QStringLiteral("type")).toInt()); + parameter.defaultValue = item.value(QStringLiteral("defaultValue")).toVariant(); + parameter.hasRange = item.value(QStringLiteral("hasRange")).toBool(); + parameter.minimum = item.value(QStringLiteral("minimum")).toDouble(); + parameter.maximum = item.value(QStringLiteral("maximum")).toDouble(); + parameter.suffix = item.value(QStringLiteral("suffix")).toString(); + parameter.choices = item.value(QStringLiteral("choices")).toArray().toVariantList(); + parameter.choiceNames = stringList(item.value(QStringLiteral("choiceNames"))); + parameter.fileFilter = item.value(QStringLiteral("fileFilter")).toString(); + descriptor.parameters.append(parameter); + } + return descriptor; + } + + QJsonObject toJson(const SignalAcquisitionConfig& config) + { + const ScanImageConfig& scan = config.scanImage; + return {{QStringLiteral("sourceId"), config.sourceId}, + {QStringLiteral("sampleIntervalSeconds"), config.sampleIntervalSeconds}, + {QStringLiteral("durationMs"), config.durationMs}, + {QStringLiteral("publishTimestampedEvents"), config.publishTimestampedEvents}, + {QStringLiteral("scanImage"), QJsonObject{ + {QStringLiteral("enabled"), scan.enabled}, + {QStringLiteral("width"), scan.width}, + {QStringLiteral("height"), scan.height}, + {QStringLiteral("gain"), static_cast(scan.gain)}, + {QStringLiteral("averageFrames"), scan.averageFrames}, + {QStringLiteral("frameStartMarker"), static_cast(scan.frameStartMarker)}, + {QStringLiteral("lineMarker"), static_cast(scan.lineMarker)}, + {QStringLiteral("frameEndMarker"), static_cast(scan.frameEndMarker)}, + {QStringLiteral("serpentine"), scan.serpentine}, + {QStringLiteral("mirrorHorizontal"), scan.mirrorHorizontal}}}, + {QStringLiteral("sourceSettings"), QJsonObject::fromVariantMap(config.sourceSettings)}}; + } + + SignalAcquisitionConfig signalAcquisitionConfigFromJson(const QJsonObject& object) + { + SignalAcquisitionConfig config; + config.sourceId = object.value(QStringLiteral("sourceId")).toString(); + config.sampleIntervalSeconds = object.value(QStringLiteral("sampleIntervalSeconds")).toDouble(); + config.durationMs = object.value(QStringLiteral("durationMs")).toInt(); + config.publishTimestampedEvents = object.value(QStringLiteral("publishTimestampedEvents")).toBool(); + const QJsonObject scan = object.value(QStringLiteral("scanImage")).toObject(); + config.scanImage.enabled = scan.value(QStringLiteral("enabled")).toBool(); + config.scanImage.width = scan.value(QStringLiteral("width")).toInt(); + config.scanImage.height = scan.value(QStringLiteral("height")).toInt(); + config.scanImage.gain = static_cast(scan.value(QStringLiteral("gain")).toInteger()); + config.scanImage.averageFrames = scan.value(QStringLiteral("averageFrames")).toInt(); + config.scanImage.frameStartMarker = + static_cast(scan.value(QStringLiteral("frameStartMarker")).toInteger()); + config.scanImage.lineMarker = static_cast(scan.value(QStringLiteral("lineMarker")).toInteger()); + config.scanImage.frameEndMarker = + static_cast(scan.value(QStringLiteral("frameEndMarker")).toInteger()); + config.scanImage.serpentine = scan.value(QStringLiteral("serpentine")).toBool(); + config.scanImage.mirrorHorizontal = scan.value(QStringLiteral("mirrorHorizontal")).toBool(); + config.sourceSettings = object.value(QStringLiteral("sourceSettings")).toObject().toVariantMap(); + return config; + } + + QJsonObject toJson(const DaqDeviceDescriptor& descriptor) + { + QJsonArray channels; + for (const DaqChannelDescriptor& channel : descriptor.channels) + { + channels.append(QJsonObject{{QStringLiteral("physicalName"), channel.physicalName}, + {QStringLiteral("type"), static_cast(channel.type)}}); + } + return {{QStringLiteral("id"), descriptor.id}, + {QStringLiteral("name"), descriptor.name}, + {QStringLiteral("provider"), descriptor.provider}, + {QStringLiteral("product"), descriptor.product}, + {QStringLiteral("channels"), channels}, + {QStringLiteral("terminals"), toJsonArray(descriptor.terminals)}}; + } + + DaqDeviceDescriptor daqDeviceDescriptorFromJson(const QJsonObject& object) + { + DaqDeviceDescriptor descriptor; + descriptor.id = object.value(QStringLiteral("id")).toString(); + descriptor.name = object.value(QStringLiteral("name")).toString(); + descriptor.provider = object.value(QStringLiteral("provider")).toString(); + descriptor.product = object.value(QStringLiteral("product")).toString(); + for (const QJsonValue& value : object.value(QStringLiteral("channels")).toArray()) + { + const QJsonObject channel = value.toObject(); + descriptor.channels.append({channel.value(QStringLiteral("physicalName")).toString(), + static_cast(channel.value(QStringLiteral("type")).toInt())}); + } + descriptor.terminals = stringList(object.value(QStringLiteral("terminals"))); + return descriptor; + } + + QJsonObject toJson(const DaqSessionConfig& config) + { + QJsonArray pulseTasks; + for (const DaqPulseTaskConfig& task : config.pulseTasks) + { + pulseTasks.append(QJsonObject{ + {QStringLiteral("name"), task.name}, + {QStringLiteral("counter"), task.counter}, + {QStringLiteral("outputTerminal"), task.outputTerminal}, + {QStringLiteral("frequencyHz"), task.frequencyHz}, + {QStringLiteral("dutyCycle"), task.dutyCycle}, + {QStringLiteral("initialDelaySeconds"), task.initialDelaySeconds}, + {QStringLiteral("startTrigger"), task.startTrigger}, + {QStringLiteral("startEdge"), static_cast(task.startEdge)}, + {QStringLiteral("timebaseSource"), task.timebaseSource}, + {QStringLiteral("initialDelayTicks"), static_cast(task.initialDelayTicks)}, + {QStringLiteral("lowTicks"), static_cast(task.lowTicks)}, + {QStringLiteral("highTicks"), static_cast(task.highTicks)}}); + } + QJsonArray analogTasks; + for (const DaqAnalogTaskConfig& task : config.analogTasks) + { + analogTasks.append(QJsonObject{ + {QStringLiteral("name"), task.name}, + {QStringLiteral("direction"), static_cast(task.direction)}, + {QStringLiteral("channels"), toJsonArray(task.channels)}, + {QStringLiteral("minimumVolts"), task.minimumVolts}, + {QStringLiteral("maximumVolts"), task.maximumVolts}, + {QStringLiteral("timing"), toJson(task.timing)}, + {QStringLiteral("outputSamplesByScan"), encodeSamples(task.outputSamplesByScan)}}); + } + QJsonArray digitalTasks; + for (const DaqDigitalTaskConfig& task : config.digitalTasks) + { + digitalTasks.append(QJsonObject{ + {QStringLiteral("name"), task.name}, + {QStringLiteral("direction"), static_cast(task.direction)}, + {QStringLiteral("lines"), toJsonArray(task.lines)}, + {QStringLiteral("timing"), toJson(task.timing)}, + {QStringLiteral("outputSamplesByScan"), encodeSamples(task.outputSamplesByScan)}}); + } + QJsonArray routes; + for (const DaqTerminalRoute& route : config.routes) + { + routes.append(QJsonObject{{QStringLiteral("source"), route.source}, + {QStringLiteral("destination"), route.destination}, + {QStringLiteral("inverted"), route.inverted}}); + } + return {{QStringLiteral("deviceId"), config.deviceId}, + {QStringLiteral("pulseTasks"), pulseTasks}, + {QStringLiteral("analogTasks"), analogTasks}, + {QStringLiteral("digitalTasks"), digitalTasks}, + {QStringLiteral("routes"), routes}}; + } + + DaqSessionConfig daqSessionConfigFromJson(const QJsonObject& object) + { + DaqSessionConfig config; + config.deviceId = object.value(QStringLiteral("deviceId")).toString(); + for (const QJsonValue& value : object.value(QStringLiteral("pulseTasks")).toArray()) + { + const QJsonObject item = value.toObject(); + DaqPulseTaskConfig task; + task.name = item.value(QStringLiteral("name")).toString(); + task.counter = item.value(QStringLiteral("counter")).toString(); + task.outputTerminal = item.value(QStringLiteral("outputTerminal")).toString(); + task.frequencyHz = item.value(QStringLiteral("frequencyHz")).toDouble(); + task.dutyCycle = item.value(QStringLiteral("dutyCycle")).toDouble(); + task.initialDelaySeconds = item.value(QStringLiteral("initialDelaySeconds")).toDouble(); + task.startTrigger = item.value(QStringLiteral("startTrigger")).toString(); + task.startEdge = static_cast(item.value(QStringLiteral("startEdge")).toInt()); + task.timebaseSource = item.value(QStringLiteral("timebaseSource")).toString(); + task.initialDelayTicks = static_cast(item.value(QStringLiteral("initialDelayTicks")).toInteger()); + task.lowTicks = static_cast(item.value(QStringLiteral("lowTicks")).toInteger()); + task.highTicks = static_cast(item.value(QStringLiteral("highTicks")).toInteger()); + config.pulseTasks.append(task); + } + for (const QJsonValue& value : object.value(QStringLiteral("analogTasks")).toArray()) + { + const QJsonObject item = value.toObject(); + DaqAnalogTaskConfig task; + task.name = item.value(QStringLiteral("name")).toString(); + task.direction = static_cast(item.value(QStringLiteral("direction")).toInt()); + task.channels = stringList(item.value(QStringLiteral("channels"))); + task.minimumVolts = item.value(QStringLiteral("minimumVolts")).toDouble(); + task.maximumVolts = item.value(QStringLiteral("maximumVolts")).toDouble(); + task.timing = timingFromJson(item.value(QStringLiteral("timing")).toObject()); + task.outputSamplesByScan = decodeSamples(item.value(QStringLiteral("outputSamplesByScan"))); + config.analogTasks.append(task); + } + for (const QJsonValue& value : object.value(QStringLiteral("digitalTasks")).toArray()) + { + const QJsonObject item = value.toObject(); + DaqDigitalTaskConfig task; + task.name = item.value(QStringLiteral("name")).toString(); + task.direction = static_cast(item.value(QStringLiteral("direction")).toInt()); + task.lines = stringList(item.value(QStringLiteral("lines"))); + task.timing = timingFromJson(item.value(QStringLiteral("timing")).toObject()); + task.outputSamplesByScan = decodeSamples(item.value(QStringLiteral("outputSamplesByScan"))); + config.digitalTasks.append(task); + } + for (const QJsonValue& value : object.value(QStringLiteral("routes")).toArray()) + { + const QJsonObject item = value.toObject(); + config.routes.append({item.value(QStringLiteral("source")).toString(), + item.value(QStringLiteral("destination")).toString(), + item.value(QStringLiteral("inverted")).toBool()}); + } + return config; + } + + QByteArray encode(const TimeSeriesChunk& chunk) + { + Output output; + output.string(chunk.sourceId); + output.string(chunk.quantity); + output.string(chunk.unit); + output.value(chunk.startTimeSeconds); + output.value(chunk.sampleIntervalSeconds); + output.value(chunk.totalInputEvents); + output.value(chunk.totalMarkers); + output.array(chunk.values); + QVector markerTimes; + QVector markerCodes; + for (const SignalMarker& marker : chunk.markers) + { + markerTimes.append(marker.timeSeconds); + markerCodes.append(marker.code); + } + output.array(markerTimes); + output.array(markerCodes); + return output.bytes(); + } + + QByteArray encode(const TimestampedEventChunk& chunk) + { + Output output; + output.string(chunk.sourceId); + output.value(chunk.tickPeriodSeconds); + output.array(chunk.eventTicks); + output.array(chunk.eventCodes); + output.array(chunk.markerTicks); + output.array(chunk.markerCodes); + return output.bytes(); + } + + QByteArray encode(const DaqInputChunk& chunk) + { + Output output; + output.string(chunk.deviceId); + output.string(chunk.taskName); + output.strings(chunk.channels); + output.value(chunk.firstSample); + output.value(chunk.nominalSampleRateHz); + output.array(chunk.analogSamplesByScan); + output.array(chunk.digitalSamplesByScan); + return output.bytes(); + } + + bool decode(const QByteArray& payload, TimeSeriesChunk& chunk) + { + Input input(payload); + chunk.sourceId = input.string(); + chunk.quantity = input.string(); + chunk.unit = input.string(); + chunk.startTimeSeconds = input.value(); + chunk.sampleIntervalSeconds = input.value(); + chunk.totalInputEvents = input.value(); + chunk.totalMarkers = input.value(); + chunk.values = input.array(); + const QVector markerTimes = input.array(); + const QVector markerCodes = input.array(); + if (!input.ok() || markerTimes.size() != markerCodes.size()) + { + return false; + } + chunk.markers.clear(); + for (qsizetype index = 0; index < markerTimes.size(); ++index) + { + chunk.markers.append({markerTimes[index], markerCodes[index]}); + } + return true; + } + + bool decode(const QByteArray& payload, TimestampedEventChunk& chunk) + { + Input input(payload); + chunk.sourceId = input.string(); + chunk.tickPeriodSeconds = input.value(); + chunk.eventTicks = input.array(); + chunk.eventCodes = input.array(); + chunk.markerTicks = input.array(); + chunk.markerCodes = input.array(); + return input.ok(); + } + + bool decode(const QByteArray& payload, DaqInputChunk& chunk) + { + Input input(payload); + chunk.deviceId = input.string(); + chunk.taskName = input.string(); + chunk.channels = input.strings(); + chunk.firstSample = input.value(); + chunk.nominalSampleRateHz = input.value(); + chunk.analogSamplesByScan = input.array(); + chunk.digitalSamplesByScan = input.array(); + return input.ok(); + } +} diff --git a/ScopeOneCore/src/PluginManifest.cpp b/ScopeOneCore/src/PluginManifest.cpp index 6ef8b83..d4cb69a 100644 --- a/ScopeOneCore/src/PluginManifest.cpp +++ b/ScopeOneCore/src/PluginManifest.cpp @@ -1,6 +1,6 @@ #include "scopeone/PluginManifest.h" -#include "scopeone/DriverHostProviderPlugin.h" +#include "scopeone/Driver.h" #include "scopeone/ProcessingPlugin.h" #include @@ -20,8 +20,8 @@ namespace scopeone::core { case PluginKind::Processing: return QStringLiteral("processing"); - case PluginKind::Hardware: - return QStringLiteral("hardware"); + case PluginKind::Driver: + return QStringLiteral("driver"); } return {}; } @@ -32,15 +32,16 @@ namespace scopeone::core { case PluginKind::Processing: return ScopeOneProcessingPluginApiVersion; - case PluginKind::Hardware: - return ScopeOneHardwarePluginApiVersion; + case PluginKind::Driver: + return ScopeOneDriverApiVersion; } return 0; } QString pluginDirectoryName(PluginKind kind) { - return pluginKindName(kind); + return kind == PluginKind::Driver ? QStringLiteral("drivers") + : QStringLiteral("plugins/%1").arg(pluginKindName(kind)); } QStringList pluginInterfaceIds(PluginKind kind) @@ -49,8 +50,8 @@ namespace scopeone::core { case PluginKind::Processing: return {QStringLiteral(ScopeOneProcessingPlugin_iid)}; - case PluginKind::Hardware: - return {QStringLiteral(ScopeOneDriverHostProviderPlugin_iid)}; + case PluginKind::Driver: + return {QStringLiteral(ScopeOneDriver_iid)}; } return {}; } @@ -60,9 +61,9 @@ namespace scopeone::core const QString directoryName = pluginDirectoryName(kind); return { QDir(QCoreApplication::applicationDirPath()) - .filePath(QStringLiteral("plugins/%1").arg(directoryName)), + .filePath(directoryName), QDir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)) - .filePath(QStringLiteral("plugins/%1").arg(directoryName))}; + .filePath(directoryName)}; } QString pluginSettingsKey(const QString& pluginId, const QString& name) diff --git a/ScopeOneCore/src/ScopeOneCore.cpp b/ScopeOneCore/src/ScopeOneCore.cpp index d00982f..4aa16db 100644 --- a/ScopeOneCore/src/ScopeOneCore.cpp +++ b/ScopeOneCore/src/ScopeOneCore.cpp @@ -1363,11 +1363,15 @@ namespace scopeone::core m_managers->processingModuleRegistry = std::make_unique(); QStringList allProcessingPluginErrors; - for (const QString& directory : - scopeone::core::pluginDirectories(scopeone::core::PluginKind::Processing)) + const QStringList processingDirectories = + scopeone::core::pluginDirectories(scopeone::core::PluginKind::Processing); + // Native processing plugins run inside ScopeOne and are only the built-in CUDA and ONNX + // ones beside the executable. Loading them from the user directory too let anyone add + // in-process C++ code; users extend processing with Python modules instead. + allProcessingPluginErrors.append( + m_managers->processingModuleRegistry->loadPlugins(processingDirectories.constFirst())); + for (const QString& directory : processingDirectories) { - allProcessingPluginErrors.append( - m_managers->processingModuleRegistry->loadPlugins(directory)); allProcessingPluginErrors.append(internal::registerPythonProcessingModules( *m_managers->processingModuleRegistry, directory)); } @@ -1418,8 +1422,14 @@ namespace scopeone::core this, &ScopeOneCore::previewStateChanged); m_managers->hardwareRuntime->registerProvider(m_managers->microManagerProvider); m_managers->recordingManager = new RecordingManager(this); - m_managers->signalSourceManager = new internal::SignalSourceManager(this); - m_managers->daqDeviceManager = new internal::DaqDeviceManager(this); + m_managers->signalSourceManager = + new internal::SignalSourceManager(m_managers->hardwareRuntime->deviceRegistry(), this); + m_managers->daqDeviceManager = + new internal::DaqDeviceManager(m_managers->hardwareRuntime->deviceRegistry(), this); + connect(m_managers->signalSourceManager, &internal::SignalSourceManager::sourcesChanged, + this, &ScopeOneCore::signalSourcesChanged); + connect(m_managers->daqDeviceManager, &internal::DaqDeviceManager::devicesChanged, + this, &ScopeOneCore::daqDevicesChanged); m_managers->imageProcessingManager = new ImageProcessingManager(this); m_managers->stageMosaicManager = new StageMosaicManager(this, this); m_managers->recordingWriterStatus.reset( diff --git a/ScopeOneCore/src/SignalSourceManager.cpp b/ScopeOneCore/src/SignalSourceManager.cpp index a69e72f..aa8becb 100644 --- a/ScopeOneCore/src/SignalSourceManager.cpp +++ b/ScopeOneCore/src/SignalSourceManager.cpp @@ -1,167 +1,38 @@ #include "internal/SignalSourceManager.h" +#include "internal/HardwareRuntime.h" #include "internal/PtuFile.h" -#include "internal/SimulatedPmt.h" + +#include #include #include #include -namespace scopeone::core +namespace scopeone::core::internal { - EventCountBinner::EventCountBinner(const QString& sourceId, - const QString& quantity, - const QString& unit, - double tickPeriodSeconds, - double sampleIntervalSeconds) - : m_sourceId(sourceId) - , m_quantity(quantity) - , m_unit(unit) - , m_tickPeriodSeconds(tickPeriodSeconds) - , m_ticksPerSample(std::max( - 1, - static_cast(std::llround( - sampleIntervalSeconds / tickPeriodSeconds)))) - , m_nextSampleTick(m_ticksPerSample) - , m_sampleIntervalSeconds( - static_cast(m_ticksPerSample) * tickPeriodSeconds) - { - } - - void EventCountBinner::addEvent(quint64 tick) - { - const quint64 bin = binForTick(tick); - advanceToBin(bin); - if (bin == m_currentSample) - { - ++m_currentEventCount; - ++m_totalInputEvents; - } - } - - void EventCountBinner::addMarker(quint64 tick, quint32 code) - { - advanceToTick(tick); - m_pendingMarkers.append({static_cast(tick) * m_tickPeriodSeconds, code}); - ++m_totalMarkers; - } - - void EventCountBinner::advanceToTick(quint64 tick) - { - advanceToBin(binForTick(tick)); - } - - void EventCountBinner::advanceToElapsedSeconds(double elapsedSeconds) - { - if (elapsedSeconds > 0.0) - { - advanceToBin(static_cast(elapsedSeconds / m_sampleIntervalSeconds)); - } - } - - bool EventCountBinner::hasReadyChunks() const - { - return !m_readyChunks.isEmpty(); - } - - QList EventCountBinner::takeReadyChunks() - { - return std::exchange(m_readyChunks, QList{}); - } - - QList EventCountBinner::takeCompletedChunks() - { - finishChunk(); - return takeReadyChunks(); - } - - void EventCountBinner::appendCompletedValue(double value) - { - constexpr qsizetype kMaximumChunkSamples = 65536; - m_completedValues.append(value); - if (m_completedValues.size() >= kMaximumChunkSamples) - { - finishChunk(); - } - } - - void EventCountBinner::finishChunk() - { - if (m_completedValues.isEmpty()) - { - return; - } - TimeSeriesChunk chunk; - chunk.sourceId = m_sourceId; - chunk.quantity = m_quantity; - chunk.unit = m_unit; - chunk.startTimeSeconds = static_cast(m_firstCompletedSample) - * m_sampleIntervalSeconds; - chunk.sampleIntervalSeconds = m_sampleIntervalSeconds; - chunk.values.swap(m_completedValues); - const double chunkEndTime = static_cast( - m_firstCompletedSample + static_cast(chunk.values.size())) - * m_sampleIntervalSeconds; - int markerCount = 0; - while (markerCount < m_pendingMarkers.size() - && m_pendingMarkers[markerCount].timeSeconds < chunkEndTime) - { - ++markerCount; - } - if (markerCount > 0) - { - chunk.markers = m_pendingMarkers.mid(0, markerCount); - m_pendingMarkers.remove(0, markerCount); - } - chunk.totalInputEvents = m_totalInputEvents; - chunk.totalMarkers = m_totalMarkers; - m_firstCompletedSample += static_cast(chunk.values.size()); - m_readyChunks.append(std::move(chunk)); - } - - quint64 EventCountBinner::binForTick(quint64 tick) const - { - if (tick >= m_currentSampleTick && tick < m_nextSampleTick) - { - return m_currentSample; - } - return tick / m_ticksPerSample; - } - - void EventCountBinner::advanceToBin(quint64 targetBin) - { - if (targetBin <= m_currentSample) - { - return; - } - while (m_currentSample < targetBin) - { - appendCompletedValue(static_cast(m_currentEventCount)); - m_currentEventCount = 0; - ++m_currentSample; - } - m_currentSampleTick = m_currentSample * m_ticksPerSample; - m_nextSampleTick = m_currentSampleTick + m_ticksPerSample; - } - SignalSource::SignalSource(QObject* parent) : QObject(parent) { } SignalSource::~SignalSource() = default; -} -namespace scopeone::core::internal -{ - SignalSourceManager::SignalSourceManager(QObject* parent) + SignalSourceManager::SignalSourceManager(DeviceRegistry* registry, QObject* parent) : QObject(parent) + , m_registry(registry) { - registerSources(); + registerBuiltInSources(); + connect(m_registry, &DeviceRegistry::changed, this, &SignalSourceManager::refreshProviderSources); + refreshProviderSources(); } SignalSourceManager::~SignalSourceManager() { + for (const HardwareProviderPtr& provider : std::as_const(m_attachedProviders)) + { + detachProvider(provider); + } for (const QPointer& source : std::as_const(m_sources)) { delete source.data(); @@ -194,13 +65,25 @@ namespace scopeone::core::internal } return false; } + if (const HardwareProviderPtr provider = m_providerBySource.value(sourceId)) + { + SignalAcquisitionConfig normalized = config; + normalized.sourceId = sourceId; + return dynamic_cast(provider.get())->startSignal(normalized, errorMessage); + } SignalSource* source = sourceInstance(sourceId, errorMessage); return source && source->start(config, errorMessage); } void SignalSourceManager::stopTrace(const QString& sourceId) { - if (SignalSource* source = m_sources.value(sourceId.trimmed())) + const QString id = sourceId.trimmed(); + if (const HardwareProviderPtr provider = m_providerBySource.value(id)) + { + dynamic_cast(provider.get())->stopSignal(id); + return; + } + if (SignalSource* source = m_sources.value(id)) { source->stop(); } @@ -221,10 +104,9 @@ namespace scopeone::core::internal : m_messages.value(id, QStringLiteral("Signal source is idle")); } - void SignalSourceManager::registerSources() + void SignalSourceManager::registerBuiltInSources() { m_factories.push_back(std::make_unique()); - m_factories.push_back(std::make_unique()); for (const std::unique_ptr& factory : m_factories) { for (const SignalSourceDescriptor& descriptor : factory->signalSources()) @@ -242,6 +124,114 @@ namespace scopeone::core::internal } } + // Follows the drivers as they are registered and removed: the sources of each + // plugin that provides signal sources join the list, and their chunks are passed on + void SignalSourceManager::refreshProviderSources() + { + QHash current; + for (const HardwareProviderDescriptor& descriptor : m_registry->providers()) + { + const HardwareProviderPtr provider = m_registry->provider(descriptor.id); + if (dynamic_cast(provider.get())) + { + current.insert(descriptor.id, provider); + } + } + bool changed = false; + for (auto it = m_attachedProviders.begin(); it != m_attachedProviders.end();) + { + if (current.value(it.key()) == it.value()) + { + ++it; + continue; + } + detachProvider(it.value()); + it = m_attachedProviders.erase(it); + changed = true; + } + for (auto it = current.cbegin(); it != current.cend(); ++it) + { + if (m_attachedProviders.contains(it.key())) + { + continue; + } + auto* const signalProvider = dynamic_cast(it.value().get()); + const QPointer self(this); + signalProvider->setSignalSinks( + [self](const TimeSeriesChunk& chunk) + { + QMetaObject::invokeMethod(self, [self, chunk]() { if (self) emit self->timeSeriesReady(chunk); }, + Qt::QueuedConnection); + }, + [self](const TimestampedEventChunk& chunk) + { + QMetaObject::invokeMethod(self, [self, chunk]() { if (self) emit self->timestampedEventsReady(chunk); }, + Qt::QueuedConnection); + }, + [self](const QString& sourceId, SignalSourceState state, const QString& message) + { + QMetaObject::invokeMethod(self, [self, sourceId, state, message]() + { + if (self) self->updateState(sourceId, state, message); + }, Qt::QueuedConnection); + }); + for (const SignalSourceDescriptor& descriptor : signalProvider->signalSources()) + { + const QString sourceId = descriptor.id.trimmed(); + if (sourceId.isEmpty() || m_descriptors.contains(sourceId)) + { + qWarning().noquote() << QStringLiteral("Hardware provider %1: invalid or duplicate signal source %2") + .arg(it.key(), descriptor.id); + continue; + } + m_descriptors.insert(sourceId, descriptor); + m_providerBySource.insert(sourceId, it.value()); + m_states.insert(sourceId, SignalSourceState::Idle); + m_messages.insert(sourceId, QStringLiteral("Signal source is idle")); + } + m_attachedProviders.insert(it.key(), it.value()); + changed = true; + } + if (changed) + { + emit sourcesChanged(); + } + } + + void SignalSourceManager::detachProvider(const HardwareProviderPtr& provider) + { + dynamic_cast(provider.get())->setSignalSinks({}, {}, {}); + for (auto it = m_providerBySource.begin(); it != m_providerBySource.end();) + { + if (it.value() != provider) + { + ++it; + continue; + } + m_descriptors.remove(it.key()); + m_states.remove(it.key()); + m_messages.remove(it.key()); + it = m_providerBySource.erase(it); + } + } + + void SignalSourceManager::updateState(const QString& sourceId, + SignalSourceState state, + const QString& message) + { + if (!m_providerBySource.contains(sourceId)) + { + return; + } + m_states.insert(sourceId, state); + m_messages.insert(sourceId, message); + emit sourceStateChanged(sourceId, state, message); + if (state == SignalSourceState::Error) + { + emit sourceError(sourceId, message); + } + } + SignalSource* SignalSourceManager::sourceInstance(const QString& sourceId, QString* errorMessage) { diff --git a/ScopeOneCore/src/SimulatedPmt.cpp b/ScopeOneCore/src/SimulatedPmt.cpp deleted file mode 100644 index 971c46c..0000000 --- a/ScopeOneCore/src/SimulatedPmt.cpp +++ /dev/null @@ -1,141 +0,0 @@ -#include "internal/SimulatedPmt.h" - -#include "internal/SimulatedPmtSource.h" - -namespace scopeone::core::internal -{ - namespace - { - constexpr auto kSourceId = "pmt:simulator"; - - scopeone::core::SignalParameterDescriptor realParameter( - const QString& key, - const QString& name, - double value, - double minimum, - double maximum, - const QString& suffix) - { - scopeone::core::SignalParameterDescriptor parameter; - parameter.key = key; - parameter.name = name; - parameter.type = scopeone::core::SignalParameterType::Real; - parameter.defaultValue = value; - parameter.hasRange = true; - parameter.minimum = minimum; - parameter.maximum = maximum; - parameter.suffix = suffix; - return parameter; - } - - scopeone::core::SignalParameterDescriptor choiceParameter( - const QString& key, - const QString& name, - const QStringList& values, - const QStringList& names, - const QString& value) - { - scopeone::core::SignalParameterDescriptor parameter; - parameter.key = key; - parameter.name = name; - parameter.type = scopeone::core::SignalParameterType::Choice; - parameter.defaultValue = value; - for (int index = 0; index < values.size(); ++index) - { - parameter.choices.append(values[index]); - parameter.choiceNames.append(names[index]); - } - return parameter; - } - - scopeone::core::SignalSourceDescriptor sourceDescriptor() - { - using namespace scopeone::core; - - SignalSourceDescriptor descriptor; - descriptor.id = QString::fromLatin1(kSourceId); - descriptor.name = QStringLiteral("Simulated PMT"); - descriptor.provider = QStringLiteral("ScopeOne"); - descriptor.quantity = QStringLiteral("Photon count rate"); - descriptor.unit = QStringLiteral("counts/s"); - descriptor.streamType = SignalStreamType::TimestampedEvents; - descriptor.parameters.append(realParameter( - QStringLiteral("voltage"), - QStringLiteral("Control voltage"), - 1.0, - 0.0, - 1.25, - QStringLiteral(" V"))); - descriptor.parameters.append(realParameter( - QStringLiteral("gain"), - QStringLiteral("Gain"), - 1.0, - 1.0, - 100.0, - QString())); - descriptor.parameters.append(realParameter( - QStringLiteral("baseRate"), - QStringLiteral("Base rate"), - 1000000.0, - 10000.0, - 1000000.0, - QStringLiteral(" counts/s"))); - descriptor.parameters.append(realParameter( - QStringLiteral("darkCountRate"), - QStringLiteral("Dark count rate"), - 500.0, - 0.0, - 5000.0, - QStringLiteral(" counts/s"))); - descriptor.parameters.append(realParameter( - QStringLiteral("modulationFrequency"), - QStringLiteral("Modulation frequency"), - 2.0, - 0.0, - 1000.0, - QStringLiteral(" Hz"))); - descriptor.parameters.append(realParameter( - QStringLiteral("scanFrameRate"), - QStringLiteral("Scan frame rate"), - 2.0, - 0.5, - 10.0, - QStringLiteral(" Hz"))); - descriptor.parameters.append(choiceParameter( - QStringLiteral("waveform"), - QStringLiteral("Waveform"), - {QStringLiteral("constant"), QStringLiteral("sine"), QStringLiteral("square")}, - {QStringLiteral("Constant"), QStringLiteral("Sine"), QStringLiteral("Square")}, - QStringLiteral("constant"))); - descriptor.parameters.append(choiceParameter( - QStringLiteral("noiseMode"), - QStringLiteral("Noise"), - {QStringLiteral("poisson"), QStringLiteral("gaussian"), QStringLiteral("none")}, - {QStringLiteral("Poisson"), QStringLiteral("Gaussian"), QStringLiteral("None")}, - QStringLiteral("poisson"))); - descriptor.parameters.append(choiceParameter( - QStringLiteral("pattern"), - QStringLiteral("Pattern"), - {QStringLiteral("beads"), QStringLiteral("usaf"), - QStringLiteral("grid"), QStringLiteral("cells")}, - {QStringLiteral("Fluorescent Beads"), QStringLiteral("USAF Target"), - QStringLiteral("Concentric Grid"), QStringLiteral("Cell Structure")}, - QStringLiteral("beads"))); - return descriptor; - } - } - - QList SimulatedPmtSourceFactory::signalSources() const - { - return {sourceDescriptor()}; - } - - scopeone::core::SignalSource* SimulatedPmtSourceFactory::createSignalSource( - const QString& sourceId, - QObject* parent) - { - return sourceId.trimmed() == QString::fromLatin1(kSourceId) - ? new SimulatedPmtSource(parent) - : nullptr; - } -} diff --git a/ScopeOneCore/src/StreamRing.cpp b/ScopeOneCore/src/StreamRing.cpp new file mode 100644 index 0000000..bacbf2f --- /dev/null +++ b/ScopeOneCore/src/StreamRing.cpp @@ -0,0 +1,210 @@ +#include "internal/StreamRing.h" + +#include +#include + +namespace scopeone::core::internal::streamring +{ + namespace + { + constexpr quint32 kMagic = 0x53524E47; // "SRNG" + constexpr quint32 kLayoutVersion = 1; + + // The cursors count bytes from the start of the stream and never wrap, so the bytes + // in use are writeCursor - readCursor. Each sits on a cache line of its own because + // the two processes write them. + struct alignas(64) Control + { + quint32 magic; + quint32 version; + quint64 capacity; + alignas(64) quint64 writeCursor; + alignas(64) quint64 readCursor; + }; + + struct RecordHeader + { + quint32 bytes; + quint32 type; + quint64 sequence; + quint64 droppedBefore; + quint64 payloadBytes; + }; + + // Every record starts on 8 bytes, so the first two words of any record, which hold its + // size and type, always fit before the end of the ring + constexpr quint64 kAlignment = 8; + static_assert(sizeof(RecordHeader) % kAlignment == 0); + static_assert(std::atomic_ref::is_always_lock_free, + "The stream ring requires lock-free 64-bit atomics"); + + quint64 aligned(quint64 bytes) + { + return (bytes + kAlignment - 1) / kAlignment * kAlignment; + } + + Control* control(uchar* base) + { + return reinterpret_cast(base); + } + + uchar* data(uchar* base) + { + return base + sizeof(Control); + } + } + + qsizetype ringBytes(qsizetype capacity) + { + return static_cast(sizeof(Control) + aligned(static_cast(capacity))); + } + + void initializeRing(void* memory, qsizetype capacity) + { + Control initial{}; + initial.magic = kMagic; + initial.version = kLayoutVersion; + initial.capacity = aligned(static_cast(capacity)); + std::memcpy(memory, &initial, sizeof(initial)); + } + + bool createSharedRing(QSharedMemory& shm, + const QString& nativeKey, + qsizetype capacity, + QString* errorMessage) + { + shm.setNativeKey(nativeKey); + const qsizetype bytes = ringBytes(capacity); + if (!shm.create(bytes)) + { + if (shm.attach()) + { + shm.detach(); + } + if (!shm.create(bytes)) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("Cannot create shared memory '%1': %2") + .arg(nativeKey, shm.errorString()); + } + return false; + } + } + initializeRing(shm.data(), capacity); + return true; + } + + bool attachSharedRing(QSharedMemory& shm, const QString& nativeKey, QString* errorMessage) + { + shm.setNativeKey(nativeKey); + if (!shm.attach(QSharedMemory::ReadWrite)) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("Cannot attach shared memory '%1': %2") + .arg(nativeKey, shm.errorString()); + } + return false; + } + const Control* const ring = control(static_cast(shm.data())); + if (shm.size() < static_cast(sizeof(Control)) + || ring->magic != kMagic || ring->version != kLayoutVersion + || shm.size() < ringBytes(static_cast(ring->capacity))) + { + shm.detach(); + if (errorMessage) + { + *errorMessage = QStringLiteral("Shared memory '%1' is not a stream ring").arg(nativeKey); + } + return false; + } + return true; + } + + Writer::Writer(void* memory) + : m_base(static_cast(memory)) + { + } + + bool Writer::write(RecordType type, const void* payload, std::size_t bytes) + { + Control* const ring = control(m_base); + const quint64 capacity = ring->capacity; + const quint64 need = aligned(sizeof(RecordHeader) + bytes); + const quint64 write = std::atomic_ref(ring->writeCursor).load(std::memory_order_relaxed); + const quint64 read = std::atomic_ref(ring->readCursor).load(std::memory_order_acquire); + const quint64 offset = write % capacity; + const quint64 tail = capacity - offset; + const quint64 skip = tail < need ? tail : 0; + if (need > capacity / 2 || write + skip + need - read > capacity) + { + ++m_dropped; + return false; + } + + uchar* const area = data(m_base); + if (skip > 0) + { + const quint32 skipHeader[2]{static_cast(skip), static_cast(RecordType::Skip)}; + std::memcpy(area + offset, skipHeader, sizeof(skipHeader)); + } + const quint64 start = (write + skip) % capacity; + const RecordHeader header{static_cast(need), static_cast(type), m_sequence++, m_dropped, + static_cast(bytes)}; + std::memcpy(area + start, &header, sizeof(header)); + if (bytes > 0) + { + std::memcpy(area + start + sizeof(header), payload, bytes); + } + m_dropped = 0; + std::atomic_ref(ring->writeCursor).store(write + skip + need, std::memory_order_release); + return true; + } + + Reader::Reader(void* memory) + : m_base(static_cast(memory)) + { + } + + bool Reader::read(Record& record) + { + Control* const ring = control(m_base); + const quint64 capacity = ring->capacity; + quint64 read = std::atomic_ref(ring->readCursor).load(std::memory_order_relaxed); + const quint64 write = std::atomic_ref(ring->writeCursor).load(std::memory_order_acquire); + const uchar* const area = data(m_base); + while (read < write) + { + const quint64 offset = read % capacity; + quint32 words[2]{}; + std::memcpy(words, area + offset, sizeof(words)); + const quint64 bytes = words[0]; + if (bytes < kAlignment || bytes % kAlignment != 0 || bytes > write - read + || offset + bytes > capacity) + { + return false; + } + if (static_cast(words[1]) == RecordType::Skip) + { + read += bytes; + continue; + } + RecordHeader header{}; + std::memcpy(&header, area + offset, sizeof(header)); + if (header.payloadBytes > bytes - sizeof(header)) + { + return false; + } + record.type = static_cast(header.type); + record.sequence = header.sequence; + record.droppedBefore = header.droppedBefore; + record.payload = QByteArray(reinterpret_cast(area + offset + sizeof(header)), + static_cast(header.payloadBytes)); + std::atomic_ref(ring->readCursor).store(read + bytes, std::memory_order_release); + return true; + } + std::atomic_ref(ring->readCursor).store(read, std::memory_order_release); + return false; + } +} diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt new file mode 100644 index 0000000..e4b436d --- /dev/null +++ b/drivers/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(ExampleCamera) +add_subdirectory(ExamplePmt) +add_subdirectory(NIDaqmx) diff --git a/drivers/ExampleCamera/CMakeLists.txt b/drivers/ExampleCamera/CMakeLists.txt new file mode 100644 index 0000000..80f4a54 --- /dev/null +++ b/drivers/ExampleCamera/CMakeLists.txt @@ -0,0 +1,8 @@ +add_library(ScopeOneExampleCamera MODULE + ExampleCameraDriver.cpp + TestPatterns.cpp + TestPatterns.h + driver.json +) +target_link_libraries(ScopeOneExampleCamera PRIVATE scopeone::DriverSDK Qt6::Core) +scopeone_add_driver(ScopeOneExampleCamera) diff --git a/plugins/hardware/ExampleCamera/ExampleCameraPlugin.cpp b/drivers/ExampleCamera/ExampleCameraDriver.cpp similarity index 97% rename from plugins/hardware/ExampleCamera/ExampleCameraPlugin.cpp rename to drivers/ExampleCamera/ExampleCameraDriver.cpp index 78cb03d..47d7516 100644 --- a/plugins/hardware/ExampleCamera/ExampleCameraPlugin.cpp +++ b/drivers/ExampleCamera/ExampleCameraDriver.cpp @@ -1,10 +1,10 @@ -// A hardware plugin written the way a camera maker would write one. It uses only the headers of -// scopeone::HardwarePluginSDK. Where a real plugin calls its vendor SDK, this one renders the test +// A driver written the way a camera maker would write one. It uses only the headers of +// scopeone::DriverSDK. Where a real driver calls its vendor SDK, this one renders the test // patterns of TestPatterns.h on an acquisition thread, as a vendor SDK delivers frames from a // thread of its own. #include "TestPatterns.h" -#include "scopeone/HardwarePluginSDK.h" +#include "scopeone/DriverSDK.h" #include #include @@ -252,7 +252,7 @@ namespace example_camera std::thread m_thread; }; - // One provider that offers several cameras, as the plugin of a camera maker does when its SDK + // One provider that offers several cameras, as the driver of a camera maker does when its SDK // finds several. ScopeOne addresses each camera by its id. class MultiCameraProvider final : public HardwareProvider, public CameraProvider { @@ -564,12 +564,12 @@ namespace example_camera bool m_lastPreviewState{false}; }; - class ExampleCameraPlugin final : public QObject, - public scopeone::core::DriverHostProviderPlugin + class ExampleCameraDriver final : public QObject, + public scopeone::core::Driver { Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneDriverHostProviderPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::DriverHostProviderPlugin) + Q_PLUGIN_METADATA(IID ScopeOneDriver_iid FILE "scopeone_driver.json") + Q_INTERFACES(scopeone::core::Driver) public: QString providerId() const override @@ -601,4 +601,4 @@ namespace example_camera }; } -#include "ExampleCameraPlugin.moc" +#include "ExampleCameraDriver.moc" diff --git a/plugins/hardware/ExampleCamera/TestPatterns.cpp b/drivers/ExampleCamera/TestPatterns.cpp similarity index 100% rename from plugins/hardware/ExampleCamera/TestPatterns.cpp rename to drivers/ExampleCamera/TestPatterns.cpp diff --git a/plugins/hardware/ExampleCamera/TestPatterns.h b/drivers/ExampleCamera/TestPatterns.h similarity index 100% rename from plugins/hardware/ExampleCamera/TestPatterns.h rename to drivers/ExampleCamera/TestPatterns.h diff --git a/plugins/hardware/ExampleCamera/plugin.json b/drivers/ExampleCamera/driver.json similarity index 87% rename from plugins/hardware/ExampleCamera/plugin.json rename to drivers/ExampleCamera/driver.json index 3ecc972..c249bde 100644 --- a/plugins/hardware/ExampleCamera/plugin.json +++ b/drivers/ExampleCamera/driver.json @@ -3,7 +3,7 @@ "name": "Example Camera", "version": "1.0.0", "scopeOneApi": 1, - "kind": "hardware", + "kind": "driver", "providerId": "example.camera", "autoLoad": false } diff --git a/drivers/ExamplePmt/CMakeLists.txt b/drivers/ExamplePmt/CMakeLists.txt new file mode 100644 index 0000000..ab45829 --- /dev/null +++ b/drivers/ExamplePmt/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library(ScopeOneExamplePmt MODULE + ExamplePmtDriver.cpp + driver.json +) +target_link_libraries(ScopeOneExamplePmt PRIVATE scopeone::DriverSDK Qt6::Core) +scopeone_add_driver(ScopeOneExamplePmt) diff --git a/ScopeOneCore/src/SimulatedPmtSource.cpp b/drivers/ExamplePmt/ExamplePmtDriver.cpp similarity index 53% rename from ScopeOneCore/src/SimulatedPmtSource.cpp rename to drivers/ExamplePmt/ExamplePmtDriver.cpp index 5978524..e824022 100644 --- a/ScopeOneCore/src/SimulatedPmtSource.cpp +++ b/drivers/ExamplePmt/ExamplePmtDriver.cpp @@ -1,19 +1,30 @@ -#include "internal/SimulatedPmtSource.h" +// A driver for a PMT, written the way a detector maker would write one. It uses only the +// headers of scopeone::DriverSDK. Where a real driver reads its counter or digitizer, this +// one draws photon counts on a thread of its own: a stream of count rates, or the photon events +// of a raster scan with its line and frame markers, from which ScopeOne assembles the image. +#include "scopeone/DriverSDK.h" + +#include +#include +#include #include #include +#include #include #include +#include #include +#include #include -namespace scopeone::core::internal +namespace example_pmt { using namespace scopeone::core; namespace { - constexpr auto kSourceId = "pmt:simulator"; + constexpr auto kSourceId = "example.pmt"; constexpr double kTickPeriodSeconds = 1.0e-9; constexpr double Pi = 3.14159265358979323846; constexpr quint32 kPhotonCode = 1; @@ -152,112 +163,251 @@ namespace scopeone::core::internal 0.5 + 0.5 * std::sin(2.0 * Pi * (4.0 * x + 2.0 * y))); return value; } - } - SimulatedPmtSource::SimulatedPmtSource(QObject* parent) - : SignalSource(parent) - { - } - - SimulatedPmtSource::~SimulatedPmtSource() - { - std::lock_guard lock(m_threadMutex); - if (m_worker.joinable()) + scopeone::core::SignalParameterDescriptor realParameter( + const QString& key, + const QString& name, + double value, + double minimum, + double maximum, + const QString& suffix) { - m_worker.request_stop(); - m_worker.join(); + scopeone::core::SignalParameterDescriptor parameter; + parameter.key = key; + parameter.name = name; + parameter.type = scopeone::core::SignalParameterType::Real; + parameter.defaultValue = value; + parameter.hasRange = true; + parameter.minimum = minimum; + parameter.maximum = maximum; + parameter.suffix = suffix; + return parameter; } - } - bool SimulatedPmtSource::start(const SignalAcquisitionConfig& config, - QString* errorMessage) - { - if (config.sourceId.trimmed() != QString::fromLatin1(kSourceId)) + scopeone::core::SignalParameterDescriptor choiceParameter( + const QString& key, + const QString& name, + const QStringList& values, + const QStringList& names, + const QString& value) { - if (errorMessage) + scopeone::core::SignalParameterDescriptor parameter; + parameter.key = key; + parameter.name = name; + parameter.type = scopeone::core::SignalParameterType::Choice; + parameter.defaultValue = value; + for (int index = 0; index < values.size(); ++index) { - *errorMessage = QStringLiteral("Unknown simulated PMT source"); + parameter.choices.append(values[index]); + parameter.choiceNames.append(names[index]); } - return false; + return parameter; } - std::lock_guard lock(m_threadMutex); - if (m_state == SignalSourceState::Starting - || m_state == SignalSourceState::Running - || m_state == SignalSourceState::Stopping) + scopeone::core::SignalSourceDescriptor sourceDescriptor() { - if (errorMessage) + SignalSourceDescriptor descriptor; + descriptor.id = QString::fromLatin1(kSourceId); + descriptor.name = QStringLiteral("Example PMT"); + descriptor.provider = QStringLiteral("Example PMT"); + descriptor.quantity = QStringLiteral("Photon count rate"); + descriptor.unit = QStringLiteral("counts/s"); + descriptor.streamType = SignalStreamType::TimestampedEvents; + descriptor.parameters.append(realParameter( + QStringLiteral("voltage"), + QStringLiteral("Control voltage"), + 1.0, + 0.0, + 1.25, + QStringLiteral(" V"))); + descriptor.parameters.append(realParameter( + QStringLiteral("gain"), + QStringLiteral("Gain"), + 1.0, + 1.0, + 100.0, + QString())); + descriptor.parameters.append(realParameter( + QStringLiteral("baseRate"), + QStringLiteral("Base rate"), + 1000000.0, + 10000.0, + 1000000.0, + QStringLiteral(" counts/s"))); + descriptor.parameters.append(realParameter( + QStringLiteral("darkCountRate"), + QStringLiteral("Dark count rate"), + 500.0, + 0.0, + 5000.0, + QStringLiteral(" counts/s"))); + descriptor.parameters.append(realParameter( + QStringLiteral("modulationFrequency"), + QStringLiteral("Modulation frequency"), + 2.0, + 0.0, + 1000.0, + QStringLiteral(" Hz"))); + descriptor.parameters.append(realParameter( + QStringLiteral("scanFrameRate"), + QStringLiteral("Scan frame rate"), + 2.0, + 0.5, + 10.0, + QStringLiteral(" Hz"))); + descriptor.parameters.append(choiceParameter( + QStringLiteral("waveform"), + QStringLiteral("Waveform"), + {QStringLiteral("constant"), QStringLiteral("sine"), QStringLiteral("square")}, + {QStringLiteral("Constant"), QStringLiteral("Sine"), QStringLiteral("Square")}, + QStringLiteral("constant"))); + descriptor.parameters.append(choiceParameter( + QStringLiteral("noiseMode"), + QStringLiteral("Noise"), + {QStringLiteral("poisson"), QStringLiteral("gaussian"), QStringLiteral("none")}, + {QStringLiteral("Poisson"), QStringLiteral("Gaussian"), QStringLiteral("None")}, + QStringLiteral("poisson"))); + descriptor.parameters.append(choiceParameter( + QStringLiteral("pattern"), + QStringLiteral("Pattern"), + {QStringLiteral("beads"), QStringLiteral("usaf"), + QStringLiteral("grid"), QStringLiteral("cells")}, + {QStringLiteral("Fluorescent Beads"), QStringLiteral("USAF Target"), + QStringLiteral("Concentric Grid"), QStringLiteral("Cell Structure")}, + QStringLiteral("beads"))); + return descriptor; + } + } + + // The PMT and the counter behind it. It reports its chunks and states through the sinks of the provider. + class PmtSource + { + public: + struct Settings + { + double voltage{1.0}; + double gain{1.0}; + double baseRate{1000000.0}; + double darkCountRate{500.0}; + double modulationFrequency{2.0}; + double scanFrameRate{2.0}; + QString waveform{QStringLiteral("constant")}; + QString noiseMode{QStringLiteral("poisson")}; + QString pattern{QStringLiteral("beads")}; + }; + + ~PmtSource() + { + stop(); + if (m_worker.joinable()) { - *errorMessage = QStringLiteral("Simulated PMT is already active"); + m_worker.join(); } - return false; } - if (m_worker.joinable()) + + void setSinks(SignalSourceProvider::TimeSeriesSink timeSeries, + SignalSourceProvider::TimestampedEventSink timestampedEvents, + SignalSourceProvider::StateSink state) { - m_worker.join(); + QMutexLocker locker(&m_sinkMutex); + m_timeSeriesSink = std::move(timeSeries); + m_timestampedEventSink = std::move(timestampedEvents); + m_stateSink = std::move(state); } - Settings settings; - settings.voltage = config.sourceSettings - .value(QStringLiteral("voltage"), settings.voltage).toDouble(); - settings.gain = config.sourceSettings - .value(QStringLiteral("gain"), settings.gain).toDouble(); - settings.baseRate = config.sourceSettings - .value(QStringLiteral("baseRate"), settings.baseRate).toDouble(); - settings.darkCountRate = config.sourceSettings - .value(QStringLiteral("darkCountRate"), settings.darkCountRate).toDouble(); - settings.modulationFrequency = config.sourceSettings - .value(QStringLiteral("modulationFrequency"), settings.modulationFrequency).toDouble(); - settings.scanFrameRate = config.sourceSettings - .value(QStringLiteral("scanFrameRate"), settings.scanFrameRate).toDouble(); - settings.waveform = config.sourceSettings - .value(QStringLiteral("waveform"), settings.waveform).toString(); - settings.noiseMode = config.sourceSettings - .value(QStringLiteral("noiseMode"), settings.noiseMode).toString(); - settings.pattern = config.sourceSettings - .value(QStringLiteral("pattern"), settings.pattern).toString(); - - setState(SignalSourceState::Starting, QStringLiteral("Starting simulated PMT")); - m_worker = std::jthread([this, config, settings](std::stop_token stopToken) + bool start(const SignalAcquisitionConfig& config, QString* errorMessage) { - run(stopToken, config, settings); - }); - return true; - } + if (m_running.load()) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("The PMT is already acquiring"); + } + return false; + } + if (m_worker.joinable()) + { + m_worker.join(); + } + Settings settings; + const QVariantMap& values = config.sourceSettings; + settings.voltage = values.value(QStringLiteral("voltage"), settings.voltage).toDouble(); + settings.gain = values.value(QStringLiteral("gain"), settings.gain).toDouble(); + settings.baseRate = values.value(QStringLiteral("baseRate"), settings.baseRate).toDouble(); + settings.darkCountRate = values.value(QStringLiteral("darkCountRate"), settings.darkCountRate).toDouble(); + settings.modulationFrequency = + values.value(QStringLiteral("modulationFrequency"), settings.modulationFrequency).toDouble(); + settings.scanFrameRate = values.value(QStringLiteral("scanFrameRate"), settings.scanFrameRate).toDouble(); + settings.waveform = values.value(QStringLiteral("waveform"), settings.waveform).toString(); + settings.noiseMode = values.value(QStringLiteral("noiseMode"), settings.noiseMode).toString(); + settings.pattern = values.value(QStringLiteral("pattern"), settings.pattern).toString(); + + m_running.store(true); + reportState(SignalSourceState::Running, QStringLiteral("The PMT is acquiring")); + m_worker = std::jthread([this, config, settings](std::stop_token stopToken) + { + if (config.scanImage.enabled) + { + runScan(stopToken, config, settings); + } + else + { + runStream(stopToken, config, settings); + } + m_running.store(false); + reportState(SignalSourceState::Idle, stopToken.stop_requested() + ? QStringLiteral("The PMT stopped") + : QStringLiteral("The PMT finished")); + }); + return true; + } - void SimulatedPmtSource::stop() - { - std::lock_guard lock(m_threadMutex); - if (m_state == SignalSourceState::Starting - || m_state == SignalSourceState::Running) + void stop() { - setState(SignalSourceState::Stopping, QStringLiteral("Stopping simulated PMT")); m_worker.request_stop(); } - } - SignalSourceState SimulatedPmtSource::state() const - { - return m_state.load(); - } + private: + double rateAt(double timeSeconds, const Settings& settings) const; + void runStream(std::stop_token stopToken, const SignalAcquisitionConfig& config, const Settings& settings); + void runScan(std::stop_token stopToken, const SignalAcquisitionConfig& config, const Settings& settings); - QString SimulatedPmtSource::stateMessage() const - { - std::lock_guard lock(m_stateMutex); - return m_stateMessage; - } + void publish(const TimeSeriesChunk& chunk) + { + QMutexLocker locker(&m_sinkMutex); + if (m_timeSeriesSink) + { + m_timeSeriesSink(chunk); + } + } - void SimulatedPmtSource::setState(SignalSourceState state, const QString& message) - { + void publish(const TimestampedEventChunk& chunk) { - std::lock_guard lock(m_stateMutex); - m_stateMessage = message; - m_state.store(state); + QMutexLocker locker(&m_sinkMutex); + if (m_timestampedEventSink) + { + m_timestampedEventSink(chunk); + } + } + + void reportState(SignalSourceState state, const QString& message) + { + QMutexLocker locker(&m_sinkMutex); + if (m_stateSink) + { + m_stateSink(QString::fromLatin1(kSourceId), state, message); + } } - emit stateChanged(state, message); - } - double SimulatedPmtSource::rateAt(double timeSeconds, const Settings& settings) const + QMutex m_sinkMutex; + SignalSourceProvider::TimeSeriesSink m_timeSeriesSink; + SignalSourceProvider::TimestampedEventSink m_timestampedEventSink; + SignalSourceProvider::StateSink m_stateSink; + std::atomic_bool m_running{false}; + std::jthread m_worker; + }; + + double PmtSource::rateAt(double timeSeconds, const Settings& settings) const { const double gain = settings.gain * std::pow(settings.voltage, 3.0); const double signalRate = settings.baseRate * gain + settings.darkCountRate; @@ -274,31 +424,7 @@ namespace scopeone::core::internal return signalRate; } - void SimulatedPmtSource::run(std::stop_token stopToken, - const SignalAcquisitionConfig& config, - const Settings& settings) - { - setState(SignalSourceState::Running, QStringLiteral("Simulated PMT is running")); - if (config.scanImage.enabled) - { - runScan(stopToken, config, settings); - } - else - { - runStream(stopToken, config, settings); - } - - if (stopToken.stop_requested()) - { - setState(SignalSourceState::Idle, QStringLiteral("Simulated PMT stopped")); - } - else - { - setState(SignalSourceState::Idle, QStringLiteral("Simulated PMT finished")); - } - } - - void SimulatedPmtSource::runStream(std::stop_token stopToken, + void PmtSource::runStream(std::stop_token stopToken, const SignalAcquisitionConfig& config, const Settings& settings) { @@ -351,10 +477,10 @@ namespace scopeone::core::internal } chunk.totalInputEvents = totalEvents; chunk.totalMarkers = 0; - emit timeSeriesReady(chunk); + publish(chunk); if (events.isValid()) { - emit timestampedEventsReady(events); + publish(events); } std::this_thread::sleep_until( startTime + std::chrono::duration_cast( @@ -362,7 +488,7 @@ namespace scopeone::core::internal } } - void SimulatedPmtSource::runScan(std::stop_token stopToken, + void PmtSource::runScan(std::stop_token stopToken, const SignalAcquisitionConfig& config, const Settings& settings) { @@ -440,7 +566,7 @@ namespace scopeone::core::internal binner.addMarker(lineEnd, scan.lineMarker); for (const TimeSeriesChunk& chunk : binner.takeReadyChunks()) { - emit timeSeriesReady(chunk); + publish(chunk); } } @@ -453,9 +579,9 @@ namespace scopeone::core::internal binner.advanceToElapsedSeconds((frameIndex + 1) * actualFrameDuration); for (const TimeSeriesChunk& chunk : binner.takeCompletedChunks()) { - emit timeSeriesReady(chunk); + publish(chunk); } - emit timestampedEventsReady(events); + publish(events); ++frameIndex; std::this_thread::sleep_until( @@ -463,4 +589,79 @@ namespace scopeone::core::internal std::chrono::duration(frameIndex * actualFrameDuration))); } } + + class PmtProvider final : public HardwareProvider, public SignalSourceProvider + { + public: + HardwareProviderDescriptor descriptor() const override + { + return {QString::fromLatin1(kSourceId), QStringLiteral("Example PMT"), QStringLiteral("1.0.0")}; + } + + // The PMT is listed as a signal source, not as a device with properties + QList devices() const override + { + return {}; + } + + QList signalSources() const override + { + return {sourceDescriptor()}; + } + + void setSignalSinks(TimeSeriesSink timeSeries, + TimestampedEventSink timestampedEvents, + StateSink state) override + { + m_pmt.setSinks(std::move(timeSeries), std::move(timestampedEvents), std::move(state)); + } + + bool startSignal(const SignalAcquisitionConfig& config, QString* errorMessage) override + { + if (config.sourceId != QString::fromLatin1(kSourceId)) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("Unknown signal source %1").arg(config.sourceId); + } + return false; + } + return m_pmt.start(config, errorMessage); + } + + void stopSignal(const QString& sourceId) override + { + if (sourceId == QString::fromLatin1(kSourceId)) + { + m_pmt.stop(); + } + } + + private: + PmtSource m_pmt; + }; + + class ExamplePmtDriver final : public QObject, public Driver + { + Q_OBJECT + Q_PLUGIN_METADATA(IID ScopeOneDriver_iid FILE "scopeone_driver.json") + Q_INTERFACES(scopeone::core::Driver) + + public: + QString providerId() const override + { + return QString::fromLatin1(kSourceId); + } + + HardwareProviderPtr createProvider(const QJsonObject&, QString* errorMessage) override + { + if (errorMessage) + { + errorMessage->clear(); + } + return std::make_shared(); + } + }; } + +#include "ExamplePmtDriver.moc" diff --git a/drivers/ExamplePmt/driver.json b/drivers/ExamplePmt/driver.json new file mode 100644 index 0000000..104ccbb --- /dev/null +++ b/drivers/ExamplePmt/driver.json @@ -0,0 +1,9 @@ +{ + "id": "example.pmt", + "name": "Example PMT", + "version": "1.0.0", + "scopeOneApi": 1, + "kind": "driver", + "providerId": "example.pmt", + "autoLoad": false +} diff --git a/drivers/NIDaqmx/CMakeLists.txt b/drivers/NIDaqmx/CMakeLists.txt new file mode 100644 index 0000000..ff978d7 --- /dev/null +++ b/drivers/NIDaqmx/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library(ScopeOneNIDaqmx MODULE + NIDaqmxDriver.cpp + driver.json +) +target_link_libraries(ScopeOneNIDaqmx PRIVATE scopeone::DriverSDK Qt6::Core) +scopeone_add_driver(ScopeOneNIDaqmx) diff --git a/ScopeOneCore/src/NIDaqmx.cpp b/drivers/NIDaqmx/NIDaqmxDriver.cpp similarity index 86% rename from ScopeOneCore/src/NIDaqmx.cpp rename to drivers/NIDaqmx/NIDaqmxDriver.cpp index 00f0cd9..84b7ed1 100644 --- a/ScopeOneCore/src/NIDaqmx.cpp +++ b/drivers/NIDaqmx/NIDaqmxDriver.cpp @@ -1,19 +1,28 @@ -#include "internal/NIDaqmx.h" +// The driver for National Instruments DAQ devices. It loads the NI-DAQmx runtime when +// DriverHost starts it, so ScopeOne itself never loads the vendor library, and it runs the +// timed tasks of a DAQ session: scan waveforms and clocks out, analog and digital inputs read +// continuously on a thread of their own. +#include "scopeone/DriverSDK.h" -#include +#include #include +#include +#include +#include #include #include #include #include #include +#include #include +#include #include #include #include -namespace scopeone::core::internal +namespace ni_daqmx { using namespace scopeone::core; @@ -298,22 +307,27 @@ namespace scopeone::core::internal : QStringLiteral("/%1/%2").arg(device, value); } - class NIDaqmxController final : public DaqController + // One DAQ device and the tasks of its session + class NIDaqmxController final { public: - explicit NIDaqmxController(QString device, QObject* parent) - : DaqController(parent) - , m_device(std::move(device)) + using InputSink = std::function; + using StateSink = std::function; + + NIDaqmxController(QString device, InputSink input, StateSink state) + : m_device(std::move(device)) + , m_input(std::move(input)) + , m_stateSink(std::move(state)) { } - ~NIDaqmxController() override + ~NIDaqmxController() { - stop(); + releaseHardware(); } bool start(const DaqSessionConfig& config, - QString* errorMessage) override + QString* errorMessage) { if (m_state == DaqState::Armed || m_state == DaqState::Running) { @@ -323,6 +337,8 @@ namespace scopeone::core::internal } return false; } + // A read error ends the reader and leaves the tasks of that session to be cleared here + releaseHardware(); QString loadError; if (!m_api.load(true, &loadError)) @@ -394,7 +410,7 @@ namespace scopeone::core::internal return true; } - void stop() override + void stop() { releaseHardware(); if (m_state != DaqState::Idle) @@ -403,16 +419,6 @@ namespace scopeone::core::internal } } - DaqState state() const override - { - return m_state.load(); - } - - QString stateMessage() const override - { - return m_message; - } - private: enum class InputKind { @@ -744,7 +750,7 @@ namespace scopeone::core::internal if (samplesRead > 0) { task.nextSample += static_cast(samplesRead); - emit inputDataReady(chunk); + m_input(chunk); } } std::this_thread::sleep_for(std::chrono::milliseconds(5)); @@ -771,15 +777,11 @@ namespace scopeone::core::internal m_routes.clear(); } + // Runs on the reader thread, which cannot join itself, so the tasks are cleared on the next start or stop void reportReadError(const QString& message) { - QMetaObject::invokeMethod(this, [this, message]() - { - if (m_state == DaqState::Armed || m_state == DaqState::Running) - { - fail(message, nullptr); - } - }, Qt::QueuedConnection); + m_reading.store(false); + emitState(DaqState::Error, message); } bool fail(const QString& message, QString* errorMessage) @@ -790,25 +792,24 @@ namespace scopeone::core::internal *errorMessage = message; } emitState(DaqState::Error, message); - emit controllerError(message); return false; } void emitState(DaqState state, const QString& message) { m_state = state; - m_message = message; - emit stateChanged(state, message); + m_stateSink(state, message); } QString m_device; + InputSink m_input; + StateSink m_stateSink; DaqmxApi m_api; std::vector m_tasks; std::vector> m_routes; std::atomic_bool m_reading{false}; std::thread m_reader; std::atomic m_state{DaqState::Idle}; - QString m_message{QStringLiteral("DAQ device is idle")}; }; void appendChannels(DaqDeviceDescriptor& descriptor, @@ -822,10 +823,10 @@ namespace scopeone::core::internal } } - QList NIDaqmxDeviceFactory::devices() const + QList findDevices(QString* errorMessage) { DaqmxApi api; - if (!api.load(false)) + if (!api.load(false, errorMessage)) { return {}; } @@ -864,10 +865,125 @@ namespace scopeone::core::internal return result; } - DaqController* NIDaqmxDeviceFactory::createController(const QString& deviceId, - QObject* parent) + + class NIDaqmxProvider final : public HardwareProvider, public DaqProvider { - const QString device = nativeDeviceId(deviceId.trimmed()); - return new NIDaqmxController(device, parent); - } + public: + explicit NIDaqmxProvider(QList devices) + : m_devices(std::move(devices)) + { + } + + HardwareProviderDescriptor descriptor() const override + { + return {QStringLiteral("scopeone.nidaqmx"), QStringLiteral("NI-DAQmx"), QStringLiteral("1.0.0")}; + } + + // The DAQ devices are listed as DAQ devices, not as devices with properties + QList devices() const override + { + return {}; + } + + QList daqDevices() const override + { + return m_devices; + } + + void setDaqSinks(InputSink input, StateSink state) override + { + QMutexLocker locker(&m_sinkMutex); + m_inputSink = std::move(input); + m_stateSink = std::move(state); + } + + bool startDaq(const DaqSessionConfig& config, QString* errorMessage) override + { + NIDaqmxController* const device = controller(config.deviceId); + if (!device) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("Unknown DAQ device %1").arg(config.deviceId); + } + return false; + } + return device->start(config, errorMessage); + } + + void stopDaq(const QString& deviceId) override + { + if (NIDaqmxController* const device = m_controllers.value(deviceId).get()) + { + device->stop(); + } + } + + private: + NIDaqmxController* controller(const QString& deviceId) + { + if (const auto existing = m_controllers.value(deviceId)) + { + return existing.get(); + } + const bool known = std::any_of(m_devices.cbegin(), m_devices.cend(), + [&deviceId](const DaqDeviceDescriptor& device) + { + return device.id == deviceId; + }); + if (!known) + { + return nullptr; + } + auto created = std::make_shared( + nativeDeviceId(deviceId), + [this](const DaqInputChunk& chunk) + { + QMutexLocker locker(&m_sinkMutex); + if (m_inputSink) m_inputSink(chunk); + }, + [this, deviceId](DaqState state, const QString& message) + { + QMutexLocker locker(&m_sinkMutex); + if (m_stateSink) m_stateSink(deviceId, state, message); + }); + m_controllers.insert(deviceId, created); + return created.get(); + } + + QList m_devices; + QHash> m_controllers; + QMutex m_sinkMutex; + InputSink m_inputSink; + StateSink m_stateSink; + }; + + class NIDaqmxDriver final : public QObject, public Driver + { + Q_OBJECT + Q_PLUGIN_METADATA(IID ScopeOneDriver_iid FILE "scopeone_driver.json") + Q_INTERFACES(scopeone::core::Driver) + + public: + QString providerId() const override + { + return QStringLiteral("scopeone.nidaqmx"); + } + + // Fails when the NI-DAQmx runtime is not installed, so the driver can be turned off under Drivers and Plugins + HardwareProviderPtr createProvider(const QJsonObject&, QString* errorMessage) override + { + QString error; + QList devices = findDevices(&error); + if (!error.isEmpty()) + { + if (errorMessage) *errorMessage = error; + return {}; + } + if (errorMessage) errorMessage->clear(); + return std::make_shared(std::move(devices)); + } + }; } + +#include "NIDaqmxDriver.moc" diff --git a/drivers/NIDaqmx/driver.json b/drivers/NIDaqmx/driver.json new file mode 100644 index 0000000..080b59b --- /dev/null +++ b/drivers/NIDaqmx/driver.json @@ -0,0 +1,9 @@ +{ + "id": "scopeone.nidaqmx", + "name": "NI-DAQmx", + "version": "1.0.0", + "scopeOneApi": 1, + "kind": "driver", + "providerId": "scopeone.nidaqmx", + "autoLoad": true +} diff --git a/drivers/README.md b/drivers/README.md new file mode 100644 index 0000000..a8819c7 --- /dev/null +++ b/drivers/README.md @@ -0,0 +1,93 @@ +# Drivers + +A driver makes a device usable in ScopeOne. It is a C++ library written against +`scopeone::DriverSDK`, and ScopeOne runs it in a `ScopeOne_DriverHost` process of its own, one per +device, so a crash in a driver or in a vendor library does not take ScopeOne down. + +| Driver | Folder | Device | +|---|---|---| +| Example Camera | `ExampleCamera` | a camera that renders test patterns, the template for camera drivers | +| Example PMT | `ExamplePmt` | a simulated PMT, the template for signal sources | +| NI-DAQmx | `NIDaqmx` | National Instruments DAQ devices, through the NI-DAQmx runtime | + +Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` +files. Those adapters are not ScopeOne drivers. + +## Writing a driver + +A driver has one entry class, a `Driver`. ScopeOne_DriverHost loads the library, gets that object +from Qt's plugin loader and calls `createProvider`, which returns a `HardwareProvider` with the +capabilities its devices have (camera, stage, shutter, state, configuration, signal source, DAQ). +A signal source, such as a PMT or a photon counter, puts out one value over time: +`SignalSourceProvider` hands over its chunks of samples or photon events with the scan markers, +and ScopeOne plots them in the Signal Monitor and assembles scan images from them. A +`DaqProvider` runs the timed tasks of a DAQ session and hands over the inputs it reads. From then +on ScopeOne only exchanges messages and data with the host. The host does not link ScopeOneCore, +so a driver process holds only the host, the SDK library, Qt Core and Network, and the driver. + +The `scopeone::DriverSDK` target gives a driver the SDK headers (`scopeone/DriverSDK.h` includes +them all) and the small SDK library, and nothing else of Core, so a driver that includes or calls +any other part of Core fails to build. + +Build and install ScopeOneCore first (`scripts/build-unix.sh` or `scripts\build.ps1`). A driver +is then a separate CMake project: + +```cmake +cmake_minimum_required(VERSION 3.23) +project(MyCamera LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_AUTOMOC ON) + +find_package(Qt6 REQUIRED COMPONENTS Core) +find_package(ScopeOneCore CONFIG REQUIRED) + +add_library(MyCamera MODULE MyCamera.cpp driver.json) +target_link_libraries(MyCamera PRIVATE scopeone::DriverSDK Qt6::Core) +scopeone_add_driver(MyCamera) +``` + +Point `CMAKE_PREFIX_PATH` at `ScopeOneCore/install` and at the same Qt that ScopeOne uses +(`vcpkg_installed/`). + +`driver.json` sits beside `CMakeLists.txt`: + +```json +{ + "id": "my.camera", + "name": "My Camera", + "version": "1.0.0", + "scopeOneApi": 1, + "kind": "driver", + "providerId": "my.camera", + "autoLoad": false +} +``` + +`scopeOneApi` is `ScopeOneDriverApiVersion` in `scopeone/PluginManifest.h`. +`scopeone_add_driver` writes `scopeone_driver.json` from it with the Core version added, and the +entry class names that file: + +```cpp +class MyCameraDriver final : public QObject, public scopeone::core::Driver +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID ScopeOneDriver_iid FILE "scopeone_driver.json") + Q_INTERFACES(scopeone::core::Driver) + +public: + QString providerId() const override; + scopeone::core::HardwareProviderPtr createProvider(const QJsonObject& options, + QString* errorMessage) override; +}; +``` + +The driver library is loaded by `ScopeOne_DriverHost`, which links the same SDK and Qt as +ScopeOne, so a driver is built with the same compiler, Qt and Core release (`MAJOR.MINOR` of +`VERSION`). ScopeOne refuses a driver built against another release and names the release it +needs. + +## Installing a driver + +Copy the built library into `drivers/` beside the ScopeOne executable, or install it with +**Tools > Drivers and Plugins**, which puts it in the user's application data directory. Drivers +are enabled and configured there and take effect after a restart. diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 7ce89e3..42df5c6 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -21,7 +21,7 @@ else () unset(SCOPEONE_CUDA_NVCC_EXECUTABLE CACHE) endif () -add_subdirectory(hardware) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../drivers drivers) if (SCOPEONE_ENABLE_INFERENCE) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../ScopeOneInference ScopeOneInference) endif () diff --git a/plugins/README.md b/plugins/README.md index 31459db..521f415 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -5,73 +5,17 @@ extension does not take ScopeOne down. | Extension | You write | Runs in | Example | |---|---|---|---| -| Hardware plugin | a `DriverHostProviderPlugin` in C++ against `scopeone::HardwarePluginSDK` | `ScopeOne_DriverHost`, one process per device | `hardware/ExampleCamera` | +| Driver | a `Driver` in C++ against `scopeone::DriverSDK` | `ScopeOne_DriverHost`, one process per device | `drivers/ExampleCamera`, `drivers/ExamplePmt` | | Python processing module | a decorated Python function | its own Python process | `ScopeOneCore/python/scopeone/examples/processing_offset.py` | -The CUDA and ONNX processing modules in `ScopeOneCuda` and `ScopeOneInference` are built-in plugins, -built and released with ScopeOne, because they link CUDA and ONNX Runtime. Their interface, -`scopeone/ProcessingPlugin.h`, changes with ScopeOne and is not meant for code outside this -repository. Everything else, including the tool windows and the DAQ and signal source devices, is -part of ScopeOne itself. - -## Hardware plugins - -A hardware plugin implements `DriverHostProviderPlugin` and returns a `HardwareProvider` with the -capabilities its devices have (camera, stage, shutter, state, configuration). The headers it may -use are the ones `scopeone/HardwarePluginSDK.h` includes. ScopeOne starts one `ScopeOne_DriverHost` -for each device, the host loads the plugin library, and from then on ScopeOne only exchanges -messages and frames with the host. - -Build and install ScopeOneCore first (`scripts/build-unix.sh` or `scripts\build.ps1`). A hardware -plugin is then a separate CMake project: - -```cmake -cmake_minimum_required(VERSION 3.23) -project(MyCamera LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_AUTOMOC ON) - -find_package(Qt6 REQUIRED COMPONENTS Core) -find_package(ScopeOneCore CONFIG REQUIRED) - -add_library(MyCamera MODULE MyCamera.cpp plugin.json) -target_link_libraries(MyCamera PRIVATE scopeone::HardwarePluginSDK Qt6::Core) -scopeone_add_plugin(MyCamera hardware) -``` - -Point `CMAKE_PREFIX_PATH` at `ScopeOneCore/install` and at the same Qt that ScopeOne uses -(`vcpkg_installed/`). - -`plugin.json` sits beside `CMakeLists.txt`: - -```json -{ - "id": "my.camera", - "name": "My Camera", - "version": "1.0.0", - "scopeOneApi": 1, - "kind": "hardware", - "providerId": "my.camera", - "autoLoad": false -} -``` - -`scopeOneApi` is `ScopeOneHardwarePluginApiVersion` in `scopeone/PluginManifest.h`. -`scopeone_add_plugin` writes `scopeone_plugin.json` from it with the Core version added, and the -plugin names that file: - -```cpp -Q_PLUGIN_METADATA(IID ScopeOneDriverHostProviderPlugin_iid FILE "scopeone_plugin.json") -``` - -The plugin library is loaded by `ScopeOne_DriverHost`, which links the same Core and Qt as -ScopeOne, so a hardware plugin is built with the same compiler, Qt and Core release -(`MAJOR.MINOR` of `VERSION`). ScopeOne refuses a plugin built against another release and names -the release it needs. - -Copy the built library into `plugins/hardware/` beside the ScopeOne executable, or install it with -**Tools > Plugin Manager**, which puts it in the user's application data directory. Hardware -plugins are enabled and configured in the Plugin Manager and take effect after a restart. +Drivers have their own folder and guide, [drivers/README.md](../drivers/README.md). This folder +builds the drivers together with the CUDA and ONNX processing modules in `ScopeOneCuda` and +`ScopeOneInference`. Those two are built-in plugins, built and released with ScopeOne, because +they link CUDA and ONNX Runtime. Their interface, `scopeone/ProcessingPlugin.h`, changes with +ScopeOne and is not meant for code outside this repository, and ScopeOne loads native +processing plugins only from `plugins/processing` beside the executable, never from the user's +directory. Everything else, including the tool +windows and the PTU file reader, is part of ScopeOne itself. ## Python processing modules diff --git a/plugins/hardware/CMakeLists.txt b/plugins/hardware/CMakeLists.txt deleted file mode 100644 index 62317d1..0000000 --- a/plugins/hardware/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(ExampleCamera) diff --git a/plugins/hardware/ExampleCamera/CMakeLists.txt b/plugins/hardware/ExampleCamera/CMakeLists.txt deleted file mode 100644 index 8c28e7c..0000000 --- a/plugins/hardware/ExampleCamera/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -add_library(ScopeOneExampleCamera MODULE - ExampleCameraPlugin.cpp - TestPatterns.cpp - TestPatterns.h - plugin.json -) -target_link_libraries(ScopeOneExampleCamera PRIVATE scopeone::HardwarePluginSDK Qt6::Core) -scopeone_add_plugin(ScopeOneExampleCamera hardware) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 037cdc9..71ecbcd 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1485,7 +1485,7 @@ namespace scopeone::ui }); m_toolRegistry->populateMenu(toolsMenu, this); toolsMenu->addSeparator(); - auto* pluginManagerAction = toolsMenu->addAction(tr("Plugin &Manager...")); + auto* pluginManagerAction = toolsMenu->addAction(tr("&Drivers and Plugins...")); connect(pluginManagerAction, &QAction::triggered, this, [this]() { PluginManagerDialog(this).exec(); diff --git a/src/PluginManagerDialog.cpp b/src/PluginManagerDialog.cpp index 9c4d799..c358102 100644 --- a/src/PluginManagerDialog.cpp +++ b/src/PluginManagerDialog.cpp @@ -2,7 +2,7 @@ #include "scopeone/PluginManifest.h" #include "scopeone/DaqDevice.h" -#include "scopeone/DriverHostProviderPlugin.h" +#include "scopeone/Driver.h" #include "scopeone/ProcessingPlugin.h" #include "scopeone/SignalSource.h" #include "scopeone/ScopeOneCore.h" @@ -40,22 +40,22 @@ namespace scopeone::ui { QList plugins = scopeone::core::discoverPlugins( scopeone::core::PluginKind::Processing); - plugins.append(scopeone::core::discoverPlugins(scopeone::core::PluginKind::Hardware)); + plugins.append(scopeone::core::discoverPlugins(scopeone::core::PluginKind::Driver)); return plugins; } } - QStringList loadConfiguredHardwarePlugins(scopeone::core::ScopeOneCore& core) + QStringList loadConfiguredDrivers(scopeone::core::ScopeOneCore& core) { QStringList errors; QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); for (const DiscoveredPlugin& plugin : discoverPlugins()) { - if (plugin.expectedKind != scopeone::core::PluginKind::Hardware) + if (plugin.expectedKind != scopeone::core::PluginKind::Driver) { continue; } - if (plugin.interfaceId != QStringLiteral(ScopeOneDriverHostProviderPlugin_iid)) + if (plugin.interfaceId != QStringLiteral(ScopeOneDriver_iid)) { continue; } @@ -77,7 +77,7 @@ namespace scopeone::ui .toString().trimmed(); if (providerId != id) { - errors.append(QStringLiteral("%1: providerId must match plugin id") + errors.append(QStringLiteral("%1: providerId must match the driver id") .arg(QFileInfo(plugin.path).fileName())); continue; } @@ -97,7 +97,7 @@ namespace scopeone::ui PluginManagerDialog::PluginManagerDialog(QWidget* parent) : QDialog(parent) { - setWindowTitle(tr("Plugin Manager")); + setWindowTitle(tr("Drivers and Plugins")); resize(900, 600); auto* layout = new QVBoxLayout(this); @@ -113,13 +113,13 @@ namespace scopeone::ui m_table->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch); layout->addWidget(m_table, 1); - layout->addWidget(new QLabel(tr("Plugin metadata and diagnostics"), this)); + layout->addWidget(new QLabel(tr("Metadata and diagnostics"), this)); m_detailsEdit = new QPlainTextEdit(this); m_detailsEdit->setReadOnly(true); m_detailsEdit->setMaximumHeight(160); layout->addWidget(m_detailsEdit); - layout->addWidget(new QLabel(tr("Hardware options (JSON)"), this)); + layout->addWidget(new QLabel(tr("Driver options (JSON)"), this)); m_optionsEdit = new QPlainTextEdit(this); m_optionsEdit->setMaximumHeight(100); m_optionsEdit->setPlaceholderText(QStringLiteral("{}")); @@ -127,10 +127,10 @@ namespace scopeone::ui auto* buttons = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Close, this); - auto* installButton = buttons->addButton(tr("Install..."), QDialogButtonBox::ActionRole); + auto* installButton = buttons->addButton(tr("Install Driver..."), QDialogButtonBox::ActionRole); layout->addWidget(buttons); connect(buttons->button(QDialogButtonBox::Save), &QPushButton::clicked, - this, &PluginManagerDialog::saveHardwareSettings); + this, &PluginManagerDialog::saveSettings); connect(installButton, &QPushButton::clicked, this, &PluginManagerDialog::installPlugin); connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); @@ -147,7 +147,7 @@ namespace scopeone::ui for (int row = 0; row < plugins.size(); ++row) { const DiscoveredPlugin& plugin = plugins.at(row); - const bool hardware = plugin.expectedKind == scopeone::core::PluginKind::Hardware; + const bool driver = plugin.expectedKind == scopeone::core::PluginKind::Driver; auto* enabled = new QTableWidgetItem(); enabled->setData(kIdRole, plugin.manifest.id); enabled->setData(kKindRole, static_cast(plugin.expectedKind)); @@ -156,7 +156,7 @@ namespace scopeone::ui enabled->setData( kMetadataRole, QString::fromUtf8(QJsonDocument(plugin.metadata).toJson(QJsonDocument::Indented))); - if (hardware && plugin.error.isEmpty()) + if (driver && plugin.error.isEmpty()) { enabled->setFlags(enabled->flags() | Qt::ItemIsUserCheckable); const QString key = scopeone::core::pluginSettingsKey( @@ -201,11 +201,11 @@ namespace scopeone::ui item->data(kMetadataRole).toString() + QStringLiteral("\n\n") + tr("Status: %1").arg(status)); - const bool hardware = kind == scopeone::core::PluginKind::Hardware + const bool driver = kind == scopeone::core::PluginKind::Driver && !id.isEmpty() && status == tr("Ready"); - m_optionsEdit->setEnabled(hardware); - if (!hardware) + m_optionsEdit->setEnabled(driver); + if (!driver) { m_optionsEdit->clear(); return; @@ -221,46 +221,42 @@ namespace scopeone::ui void PluginManagerDialog::installPlugin() { const QString sourceDirectory = QFileDialog::getExistingDirectory( - this, tr("Install Plugin Package")); + this, tr("Install Driver")); if (sourceDirectory.isEmpty()) { return; } + // Only drivers are installed here. Native processing plugins come with ScopeOne, and + // ScopeOne does not load them from the user directory. + const scopeone::core::PluginKind kind = scopeone::core::PluginKind::Driver; QList candidates; - scopeone::core::PluginKind kind = scopeone::core::PluginKind::Processing; - for (const auto candidateKind : {scopeone::core::PluginKind::Processing, - scopeone::core::PluginKind::Hardware}) + for (const auto& candidate : scopeone::core::discoverPlugins(kind, {sourceDirectory})) { - for (const auto& candidate : - scopeone::core::discoverPlugins(candidateKind, {sourceDirectory})) + if (candidate.error.isEmpty()) { - if (candidate.error.isEmpty()) - { - candidates.append(candidate); - kind = candidateKind; - } + candidates.append(candidate); } } if (candidates.size() != 1) { - QMessageBox::warning(this, tr("Plugin Manager"), - tr("A plugin package must contain one plugin library.")); + QMessageBox::warning(this, tr("Drivers and Plugins"), + tr("The folder must contain one driver library.")); return; } QDir userRoot(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)); const QString pluginDirectory = userRoot.filePath( - QStringLiteral("plugins/%1").arg(scopeone::core::pluginDirectoryName(kind))); + scopeone::core::pluginDirectoryName(kind)); if (!QDir().mkpath(pluginDirectory)) { - QMessageBox::warning(this, tr("Plugin Manager"), tr("The plugin directory could not be created.")); + QMessageBox::warning(this, tr("Drivers and Plugins"), tr("The install directory could not be created.")); return; } if (QFileInfo(sourceDirectory).canonicalFilePath() == QFileInfo(pluginDirectory).canonicalFilePath()) { - QMessageBox::information(this, tr("Plugin Manager"), tr("This plugin is already installed.")); + QMessageBox::information(this, tr("Drivers and Plugins"), tr("This is already installed.")); return; } for (const QFileInfo& source : QDir(sourceDirectory).entryInfoList(QDir::Files)) @@ -269,17 +265,17 @@ namespace scopeone::ui QFile::remove(destination); if (!QFile::copy(source.absoluteFilePath(), destination)) { - QMessageBox::warning(this, tr("Plugin Manager"), - tr("The plugin package could not be installed.")); + QMessageBox::warning(this, tr("Drivers and Plugins"), + tr("The files could not be installed.")); return; } } refreshPlugins(); - QMessageBox::information(this, tr("Plugin Manager"), - tr("The plugin will be available after ScopeOne restarts.")); + QMessageBox::information(this, tr("Drivers and Plugins"), + tr("It will be available after ScopeOne restarts.")); } - void PluginManagerDialog::saveHardwareSettings() + void PluginManagerDialog::saveSettings() { const int row = m_table->currentRow(); if (row < 0) @@ -290,7 +286,7 @@ namespace scopeone::ui const auto kind = static_cast(item->data(kKindRole).toInt()); const QString id = item->data(kIdRole).toString(); QVariantMap selectedOptions; - if (kind == scopeone::core::PluginKind::Hardware && !id.isEmpty()) + if (kind == scopeone::core::PluginKind::Driver && !id.isEmpty()) { QJsonParseError parseError; QByteArray optionJson = m_optionsEdit->toPlainText().trimmed().toUtf8(); @@ -301,7 +297,7 @@ namespace scopeone::ui const QJsonDocument options = QJsonDocument::fromJson(optionJson, &parseError); if (parseError.error != QJsonParseError::NoError || !options.isObject()) { - QMessageBox::warning(this, tr("Plugin Manager"), tr("Hardware options must be a JSON object.")); + QMessageBox::warning(this, tr("Drivers and Plugins"), tr("Driver options must be a JSON object.")); return; } selectedOptions = options.object().toVariantMap(); @@ -314,19 +310,19 @@ namespace scopeone::ui const auto pluginKind = static_cast( pluginItem->data(kKindRole).toInt()); const QString pluginId = pluginItem->data(kIdRole).toString(); - if (pluginKind == scopeone::core::PluginKind::Hardware && !pluginId.isEmpty()) + if (pluginKind == scopeone::core::PluginKind::Driver && !pluginId.isEmpty()) { settings.setValue(scopeone::core::pluginSettingsKey( pluginId, QStringLiteral("enabled")), pluginItem->checkState() == Qt::Checked); } } - if (kind == scopeone::core::PluginKind::Hardware && !id.isEmpty()) + if (kind == scopeone::core::PluginKind::Driver && !id.isEmpty()) { settings.setValue(scopeone::core::pluginSettingsKey( id, QStringLiteral("options")), selectedOptions); } - QMessageBox::information(this, tr("Plugin Manager"), - tr("Plugin settings will take effect after ScopeOne restarts.")); + QMessageBox::information(this, tr("Drivers and Plugins"), + tr("The settings will take effect after ScopeOne restarts.")); } } diff --git a/src/PluginManagerDialog.h b/src/PluginManagerDialog.h index 04acec6..7b833f8 100644 --- a/src/PluginManagerDialog.h +++ b/src/PluginManagerDialog.h @@ -13,7 +13,7 @@ namespace scopeone::core namespace scopeone::ui { - QStringList loadConfiguredHardwarePlugins(scopeone::core::ScopeOneCore& core); + QStringList loadConfiguredDrivers(scopeone::core::ScopeOneCore& core); class PluginManagerDialog final : public QDialog { @@ -26,7 +26,7 @@ namespace scopeone::ui void refreshPlugins(); void installPlugin(); void showSelectedOptions(); - void saveHardwareSettings(); + void saveSettings(); QTableWidget* m_table{nullptr}; QPlainTextEdit* m_detailsEdit{nullptr}; diff --git a/src/main.cpp b/src/main.cpp index e4955a3..5f2e813 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,9 +28,9 @@ int main(int argc, char *argv[]) scopeone::ui::ConsoleWidget::installQtMessageHandler(); auto scopeOneCore = std::make_unique(); - for (const QString& error : scopeone::ui::loadConfiguredHardwarePlugins(*scopeOneCore)) + for (const QString& error : scopeone::ui::loadConfiguredDrivers(*scopeOneCore)) { - qWarning().noquote() << QStringLiteral("Failed to load hardware plugin %1").arg(error); + qWarning().noquote() << QStringLiteral("Failed to load driver %1").arg(error); } scopeone::ui::MainWindow window(scopeOneCore.get()); window.show(); diff --git a/src/tools/daq/DaqControlWidget.cpp b/src/tools/daq/DaqControlWidget.cpp index fb1c3ab..6e0fec0 100644 --- a/src/tools/daq/DaqControlWidget.cpp +++ b/src/tools/daq/DaqControlWidget.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -199,14 +200,13 @@ namespace scopeone::ui m_statusLabel->setWordWrap(true); layout->addWidget(m_statusLabel); - for (const auto& device : m_core->daqDevices()) - { - m_deviceCombo->addItem(device.name, device.id); - m_deviceCombo->setItemData( - m_deviceCombo->count() - 1, - QStringLiteral("%1, %2").arg(device.provider, device.product), - Qt::ToolTipRole); - } + refreshDeviceList(); + connect(m_core, &scopeone::core::ScopeOneCore::daqDevicesChanged, + this, [this]() + { + refreshDeviceList(); + refreshDevice(); + }); connect(m_deviceCombo, &QComboBox::currentIndexChanged, this, &DaqControlWidget::refreshDevice); @@ -292,6 +292,27 @@ namespace scopeone::ui return choices; } + // DAQ devices come from drivers, which can be registered after the window opens + void DaqControlWidget::refreshDeviceList() + { + const QString selectedId = m_deviceCombo->currentData().toString(); + const QSignalBlocker blocker(m_deviceCombo); + m_deviceCombo->clear(); + for (const auto& device : m_core->daqDevices()) + { + m_deviceCombo->addItem(device.name, device.id); + m_deviceCombo->setItemData( + m_deviceCombo->count() - 1, + QStringLiteral("%1, %2").arg(device.provider, device.product), + Qt::ToolTipRole); + } + const int selected = m_deviceCombo->findData(selectedId); + if (selected >= 0) + { + m_deviceCombo->setCurrentIndex(selected); + } + } + void DaqControlWidget::refreshDevice() { m_pulseTable->setRowCount(0); diff --git a/src/tools/daq/DaqControlWidget.h b/src/tools/daq/DaqControlWidget.h index e1a071f..e28580f 100644 --- a/src/tools/daq/DaqControlWidget.h +++ b/src/tools/daq/DaqControlWidget.h @@ -33,6 +33,7 @@ namespace scopeone::ui private: scopeone::core::DaqDeviceDescriptor currentDevice() const; QStringList terminalChoices(const scopeone::core::DaqDeviceDescriptor& device) const; + void refreshDeviceList(); void refreshDevice(); void addPulseRow(); void addBufferedRow(); diff --git a/src/tools/daq/SignalMonitorWidget.cpp b/src/tools/daq/SignalMonitorWidget.cpp index 4ff2215..2a79760 100644 --- a/src/tools/daq/SignalMonitorWidget.cpp +++ b/src/tools/daq/SignalMonitorWidget.cpp @@ -360,6 +360,8 @@ namespace scopeone::ui m_serpentineCheck->setEnabled(editable); m_mirrorHorizontalCheck->setEnabled(editable); }); + connect(m_core, &scopeone::core::ScopeOneCore::signalSourcesChanged, + this, &SignalMonitorWidget::refreshSources); connect(m_core, &scopeone::core::ScopeOneCore::signalTimeSeriesReady, this, &SignalMonitorWidget::handleTimeSeries); connect(m_core, &scopeone::core::ScopeOneCore::signalSourceStateChanged, From 30c97d1178349a4347bcf3d30a0b6993c6092b36 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:25:08 +0100 Subject: [PATCH 03/49] Show the signal sources on an oscilloscope screen and read the NI analog inputs of a device in one task --- drivers/NIDaqmx/NIDaqmxDriver.cpp | 455 ++++++++++- drivers/README.md | 5 +- src/tools/daq/OscilloscopeView.cpp | 874 +++++++++++++++++++++ src/tools/daq/OscilloscopeView.h | 165 ++++ src/tools/daq/SignalMonitorWidget.cpp | 1001 ++++++++++++++----------- src/tools/daq/SignalMonitorWidget.h | 75 +- 6 files changed, 2099 insertions(+), 476 deletions(-) create mode 100644 src/tools/daq/OscilloscopeView.cpp create mode 100644 src/tools/daq/OscilloscopeView.h diff --git a/drivers/NIDaqmx/NIDaqmxDriver.cpp b/drivers/NIDaqmx/NIDaqmxDriver.cpp index 84b7ed1..bed60ff 100644 --- a/drivers/NIDaqmx/NIDaqmxDriver.cpp +++ b/drivers/NIDaqmx/NIDaqmxDriver.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -812,6 +813,353 @@ namespace ni_daqmx std::atomic m_state{DaqState::Idle}; }; + SignalParameterDescriptor voltsParameter(const QString& key, + const QString& name, + double value) + { + SignalParameterDescriptor parameter; + parameter.key = key; + parameter.name = name; + parameter.type = SignalParameterType::Real; + parameter.defaultValue = value; + parameter.hasRange = true; + parameter.minimum = -10.0; + parameter.maximum = 10.0; + parameter.suffix = QStringLiteral(" V"); + return parameter; + } + + // The analog inputs of one device that run as signal sources. A device runs one analog + // input task at a time, so all of them are read by one task on the sample clock of the + // first. Adding or removing a channel rebuilds the task, and the samples keep counting + // from the first start, so the channels share one time axis and a rebuild leaves a gap. + class AiTaskGroup final + { + public: + using ChunkSink = std::function; + using StateSink = std::function; + + AiTaskGroup(QString device, ChunkSink chunks, StateSink state) + : m_device(std::move(device)) + , m_chunks(std::move(chunks)) + , m_stateSink(std::move(state)) + { + } + + ~AiTaskGroup() + { + stopReader(); + } + + bool add(const QString& sourceId, + const QString& channel, + const SignalAcquisitionConfig& config, + QString* errorMessage) + { + // load resolves the function pointers again, so the reader that calls them stops first + stopReader(); + QString loadError; + if (!m_api.load(true, &loadError)) + { + return fail(sourceId, loadError, errorMessage); + } + quint64 offset = 0; + { + std::lock_guard lock(m_mutex); + for (const Member& member : m_members) + { + if (member.sourceId == sourceId) + { + restartLocked(); + return fail(sourceId, QStringLiteral("%1 is already acquiring").arg(channel), + errorMessage); + } + } + if (m_members.empty()) + { + m_interval = config.sampleIntervalSeconds; + m_origin = std::chrono::steady_clock::now(); + } + else if (std::abs(config.sampleIntervalSeconds - m_interval) > 1e-9 * m_interval) + { + restartLocked(); + return fail(sourceId, + QStringLiteral("%1 already samples its analog inputs every %2 s, so %3 " + "has to use the same interval") + .arg(m_device).arg(m_interval).arg(channel), + errorMessage); + } + offset = elapsedSamples(); + Member member; + member.sourceId = sourceId; + member.channel = channel; + member.minimum = config.sourceSettings.value(QStringLiteral("minimumVolts"), -10.0).toDouble(); + member.maximum = config.sourceSettings.value(QStringLiteral("maximumVolts"), 10.0).toDouble(); + member.endSample = offset + static_cast( + std::max(0.0, std::ceil(config.durationMs / 1000.0 / m_interval))); + m_members.push_back(member); + } + m_stateSink(sourceId, SignalSourceState::Starting, QStringLiteral("Configuring %1").arg(channel)); + QString error; + { + std::lock_guard lock(m_mutex); + error = restartLocked(); + } + if (!error.isEmpty()) + { + if (errorMessage) + { + *errorMessage = error; + } + return false; + } + m_stateSink(sourceId, SignalSourceState::Running, QStringLiteral("%1 is acquiring").arg(channel)); + return true; + } + + void remove(const QString& sourceId) + { + stopReader(); + QString channel; + { + std::lock_guard lock(m_mutex); + const auto found = std::find_if(m_members.begin(), m_members.end(), + [&sourceId](const Member& member) + { + return member.sourceId == sourceId; + }); + if (found != m_members.end()) + { + channel = found->channel; + m_members.erase(found); + } + restartLocked(); + } + if (!channel.isEmpty()) + { + m_stateSink(sourceId, SignalSourceState::Idle, QStringLiteral("%1 stopped").arg(channel)); + } + } + + private: + struct Member + { + QString sourceId; + QString channel; + double minimum{-10.0}; + double maximum{10.0}; + quint64 endSample{0}; + }; + + quint64 elapsedSamples() const + { + const double elapsed = std::chrono::duration( + std::chrono::steady_clock::now() - m_origin).count(); + return static_cast(std::llround(elapsed / m_interval)); + } + + // Builds and starts the task for the current members; on failure every member ends in + // the error, since the device then reads none of them + QString restartLocked() + { + if (m_members.empty()) + { + return {}; + } + const double rate = 1.0 / m_interval; + // The buffer of a continuous task holds two seconds, as the stream ring does + const auto bufferSamples = static_cast(std::max(1000.0, 2.0 * rate)); + TaskHandle handle = nullptr; + Int32 result = m_api.createTask("", &handle); + for (const Member& member : m_members) + { + if (result < 0) + { + break; + } + const QByteArray channel = member.channel.toLocal8Bit(); + result = m_api.createAiVoltage(handle, channel.constData(), "", + kDefaultTerminalConfiguration, + member.minimum, member.maximum, kVolts, nullptr); + } + if (result >= 0) + { + result = m_api.configureSampleClock(handle, "", rate, kRising, + kContinuousSamples, bufferSamples); + } + if (result >= 0) + { + result = m_api.startTask(handle); + } + if (result < 0) + { + const QString message = m_api.error(result); + if (handle) + { + m_api.clearTask(handle); + } + for (const Member& member : m_members) + { + m_stateSink(member.sourceId, SignalSourceState::Error, message); + } + m_members.clear(); + return message; + } + const quint64 offset = elapsedSamples(); + m_reading.store(true); + m_reader = std::thread([this, handle, members = m_members, offset]() + { + read(handle, members, offset); + }); + return {}; + } + + void stopReader() + { + m_reading.store(false); + if (m_reader.joinable()) + { + m_reader.join(); + } + } + + // Reports only how members end by themselves, finished or failed; a member that is + // stopped or a task that is rebuilt is reported by the caller + void read(TaskHandle handle, std::vector members, quint64 offset) + { + constexpr UInt32 kSamplesPerRead = 65536; + const auto channelCount = static_cast(members.size()); + std::vector finished(members.size(), false); + qsizetype running = channelCount; + QVector interleaved; + quint64 nextSample = offset; + QString error; + while (m_reading.load() && running > 0) + { + UInt32 available = 0; + Int32 result = m_api.availableSamples(handle, &available); + if (result >= 0 && available > 0) + { + const UInt32 wanted = std::min(available, kSamplesPerRead); + interleaved.resize(static_cast(wanted) * channelCount); + Int32 samplesRead = 0; + result = m_api.readAnalog(handle, static_cast(wanted), 0.0, + kGroupByScanNumber, interleaved.data(), + static_cast(interleaved.size()), + &samplesRead, nullptr); + if (result >= 0 && samplesRead > 0) + { + publish(members, finished, running, interleaved, nextSample, + static_cast(samplesRead)); + nextSample += static_cast(samplesRead); + } + } + if (result < 0) + { + error = m_api.error(result); + break; + } + if (available == 0) + { + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + } + m_api.stopTask(handle); + m_api.clearTask(handle); + if (error.isEmpty()) + { + return; + } + std::lock_guard lock(m_mutex); + for (std::size_t index = 0; index < members.size(); ++index) + { + if (!finished[index]) + { + dropMemberLocked(members[index].sourceId); + m_stateSink(members[index].sourceId, SignalSourceState::Error, error); + } + } + } + + void publish(const std::vector& members, + std::vector& finished, + qsizetype& running, + const QVector& interleaved, + quint64 firstSample, + quint64 count) + { + const auto channelCount = static_cast(members.size()); + for (qsizetype channel = 0; channel < channelCount; ++channel) + { + const Member& member = members[static_cast(channel)]; + if (finished[static_cast(channel)]) + { + continue; + } + const quint64 usable = std::min(count, member.endSample > firstSample + ? member.endSample - firstSample + : 0); + if (usable > 0) + { + TimeSeriesChunk chunk; + chunk.sourceId = member.sourceId; + chunk.quantity = QStringLiteral("Voltage"); + chunk.unit = QStringLiteral("V"); + chunk.startTimeSeconds = static_cast(firstSample) * m_interval; + chunk.sampleIntervalSeconds = m_interval; + chunk.values.resize(static_cast(usable)); + for (qsizetype sample = 0; sample < chunk.values.size(); ++sample) + { + chunk.values[sample] = interleaved[sample * channelCount + channel]; + } + m_chunks(chunk); + } + if (firstSample + count >= member.endSample) + { + finished[static_cast(channel)] = true; + --running; + { + std::lock_guard lock(m_mutex); + dropMemberLocked(member.sourceId); + } + m_stateSink(member.sourceId, SignalSourceState::Idle, + QStringLiteral("%1 finished").arg(member.channel)); + } + } + } + + void dropMemberLocked(const QString& sourceId) + { + m_members.erase(std::remove_if(m_members.begin(), m_members.end(), + [&sourceId](const Member& member) + { + return member.sourceId == sourceId; + }), + m_members.end()); + } + + bool fail(const QString& sourceId, const QString& message, QString* errorMessage) + { + if (errorMessage) + { + *errorMessage = message; + } + m_stateSink(sourceId, SignalSourceState::Error, message); + return false; + } + + QString m_device; + ChunkSink m_chunks; + StateSink m_stateSink; + DaqmxApi m_api; + std::mutex m_mutex; + std::vector m_members; + double m_interval{0.0}; + std::chrono::steady_clock::time_point m_origin; + std::atomic_bool m_reading{false}; + std::thread m_reader; + }; + void appendChannels(DaqDeviceDescriptor& descriptor, const QString& names, DaqChannelType type) @@ -866,14 +1214,43 @@ namespace ni_daqmx } - class NIDaqmxProvider final : public HardwareProvider, public DaqProvider + // The DAQ devices for timed sessions, and each of their analog inputs as a signal source of + // its own. The sources of one device run in one analog input task. A device runs one such task + // at a time, so its sources and a session that reads its analog inputs do not run together, + // and NI-DAQmx says so when the later one starts. + class NIDaqmxProvider final : public HardwareProvider, public DaqProvider, public SignalSourceProvider { public: explicit NIDaqmxProvider(QList devices) : m_devices(std::move(devices)) { + for (const DaqDeviceDescriptor& device : m_devices) + { + for (const DaqChannelDescriptor& channel : device.channels) + { + if (channel.type != DaqChannelType::AnalogInput) + { + continue; + } + SignalSourceDescriptor source; + source.id = QStringLiteral("ni:%1").arg(channel.physicalName); + source.name = QStringLiteral("%1 (%2)").arg(channel.physicalName, device.product); + source.provider = QStringLiteral("National Instruments"); + source.quantity = QStringLiteral("Voltage"); + source.unit = QStringLiteral("V"); + source.streamType = SignalStreamType::TimeSeries; + source.parameters.append(voltsParameter( + QStringLiteral("minimumVolts"), QStringLiteral("Minimum"), -10.0)); + source.parameters.append(voltsParameter( + QStringLiteral("maximumVolts"), QStringLiteral("Maximum"), 10.0)); + m_sources.append(source); + m_sourceChannels.insert(source.id, channel.physicalName); + m_sourceDevices.insert(source.id, device.name); + } + } } + HardwareProviderDescriptor descriptor() const override { return {QStringLiteral("scopeone.nidaqmx"), QStringLiteral("NI-DAQmx"), QStringLiteral("1.0.0")}; @@ -890,7 +1267,7 @@ namespace ni_daqmx return m_devices; } - void setDaqSinks(InputSink input, StateSink state) override + void setDaqSinks(InputSink input, DaqProvider::StateSink state) override { QMutexLocker locker(&m_sinkMutex); m_inputSink = std::move(input); @@ -919,7 +1296,70 @@ namespace ni_daqmx } } + QList signalSources() const override + { + return m_sources; + } + + void setSignalSinks(TimeSeriesSink timeSeries, + TimestampedEventSink, + SignalSourceProvider::StateSink state) override + { + QMutexLocker locker(&m_sinkMutex); + m_timeSeriesSink = std::move(timeSeries); + m_signalStateSink = std::move(state); + } + + bool startSignal(const SignalAcquisitionConfig& config, QString* errorMessage) override + { + AiTaskGroup* const group = aiGroup(config.sourceId); + if (!group) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("Unknown signal source %1").arg(config.sourceId); + } + return false; + } + return group->add(config.sourceId, m_sourceChannels.value(config.sourceId), config, errorMessage); + } + + void stopSignal(const QString& sourceId) override + { + if (AiTaskGroup* const group = m_aiGroups.value(m_sourceDevices.value(sourceId)).get()) + { + group->remove(sourceId); + } + } + private: + AiTaskGroup* aiGroup(const QString& sourceId) + { + const QString device = m_sourceDevices.value(sourceId); + if (device.isEmpty()) + { + return nullptr; + } + if (const auto existing = m_aiGroups.value(device)) + { + return existing.get(); + } + auto created = std::make_shared( + device, + [this](const TimeSeriesChunk& chunk) + { + QMutexLocker locker(&m_sinkMutex); + if (m_timeSeriesSink) m_timeSeriesSink(chunk); + }, + [this](const QString& id, SignalSourceState state, const QString& message) + { + QMutexLocker locker(&m_sinkMutex); + if (m_signalStateSink) m_signalStateSink(id, state, message); + }); + m_aiGroups.insert(device, created); + return created.get(); + } + NIDaqmxController* controller(const QString& deviceId) { if (const auto existing = m_controllers.value(deviceId)) @@ -952,10 +1392,17 @@ namespace ni_daqmx } QList m_devices; - QHash> m_controllers; + QList m_sources; + QHash m_sourceChannels; + QHash m_sourceDevices; QMutex m_sinkMutex; InputSink m_inputSink; - StateSink m_stateSink; + DaqProvider::StateSink m_stateSink; + TimeSeriesSink m_timeSeriesSink; + SignalSourceProvider::StateSink m_signalStateSink; + // Declared last so they are destroyed first: their readers call the sinks above until joined + QHash> m_controllers; + QHash> m_aiGroups; }; class NIDaqmxDriver final : public QObject, public Driver diff --git a/drivers/README.md b/drivers/README.md index a8819c7..e07f50c 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -8,7 +8,7 @@ device, so a crash in a driver or in a vendor library does not take ScopeOne dow |---|---|---| | Example Camera | `ExampleCamera` | a camera that renders test patterns, the template for camera drivers | | Example PMT | `ExamplePmt` | a simulated PMT, the template for signal sources | -| NI-DAQmx | `NIDaqmx` | National Instruments DAQ devices, through the NI-DAQmx runtime | +| NI-DAQmx | `NIDaqmx` | National Instruments DAQ devices, through the NI-DAQmx runtime, with each analog input also a signal source for the Signal Monitor; the inputs of one device run in one task on one sample clock | Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. Those adapters are not ScopeOne drivers. @@ -20,7 +20,8 @@ from Qt's plugin loader and calls `createProvider`, which returns a `HardwarePro capabilities its devices have (camera, stage, shutter, state, configuration, signal source, DAQ). A signal source, such as a PMT or a photon counter, puts out one value over time: `SignalSourceProvider` hands over its chunks of samples or photon events with the scan markers, -and ScopeOne plots them in the Signal Monitor and assembles scan images from them. A +and ScopeOne shows them on the oscilloscope screen of the Signal Monitor, up to four sources at +once on one time axis, and assembles scan images from them. A `DaqProvider` runs the timed tasks of a DAQ session and hands over the inputs it reads. From then on ScopeOne only exchanges messages and data with the host. The host does not link ScopeOneCore, so a driver process holds only the host, the SDK library, Qt Core and Network, and the driver. diff --git a/src/tools/daq/OscilloscopeView.cpp b/src/tools/daq/OscilloscopeView.cpp new file mode 100644 index 0000000..3653235 --- /dev/null +++ b/src/tools/daq/OscilloscopeView.cpp @@ -0,0 +1,874 @@ +#include "OscilloscopeView.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace scopeone::ui +{ + namespace + { + const QColor kBackground(16, 20, 24); + const QColor kGridColor(58, 68, 78); + const QColor kBorderColor(96, 108, 120); + const QColor kTextColor(210, 216, 222); + const QColor kTriggerColor(255, 159, 28); + const QColor kCursorColor(255, 159, 28); + const QColor kStoppedColor(235, 80, 70); + const QColor kRunningColor(95, 227, 95); + + // About two thousand screens of a fast timebase or a few minutes of a slow one per channel + constexpr qsizetype kMaximumSamples = 4'000'000; + constexpr int kRefreshMilliseconds = 33; + constexpr qint64 kAutoTriggerMilliseconds = 200; + } + + bool OscilloscopeView::Trace::empty() const + { + return begin >= times.size(); + } + + double OscilloscopeView::Trace::first() const + { + return times[begin]; + } + + double OscilloscopeView::Trace::last() const + { + return times.constLast(); + } + + qsizetype OscilloscopeView::Trace::indexAtOrAfter(double time) const + { + return std::lower_bound(times.cbegin() + begin, times.cend(), time) - times.cbegin(); + } + + void OscilloscopeView::Trace::append(const scopeone::core::TimeSeriesChunk& chunk) + { + const qsizetype count = chunk.values.size(); + times.reserve(times.size() + count); + values.reserve(values.size() + count); + for (qsizetype index = 0; index < count; ++index) + { + times.append(chunk.startTimeSeconds + static_cast(index) * chunk.sampleIntervalSeconds); + values.append(chunk.values[index]); + } + } + + void OscilloscopeView::Trace::trim(double cutoff, qsizetype maximumSamples) + { + if (empty()) + { + return; + } + begin = std::max(indexAtOrAfter(cutoff), times.size() - maximumSamples); + if (begin > 65536 && begin > times.size() / 2) + { + times.remove(0, begin); + values.remove(0, begin); + begin = 0; + } + } + + void OscilloscopeView::Trace::clear() + { + times.clear(); + values.clear(); + begin = 0; + } + + OscilloscopeView::OscilloscopeView(QWidget* parent) + : QWidget(parent) + { + setMinimumSize(480, 320); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + auto* timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, &OscilloscopeView::refresh); + timer->start(kRefreshMilliseconds); + m_sinceTrigger.start(); + } + + void OscilloscopeView::setChannel(int index, const ScopeChannel& channel) + { + if (m_channels[index].sourceId != channel.sourceId) + { + m_traces[index].clear(); + m_frame.times[index].clear(); + m_frame.values[index].clear(); + m_frame.measurements[index] = {}; + } + m_channels[index] = channel; + m_dirty = true; + update(); + } + + void OscilloscopeView::setTimebase(double secondsPerDivision) + { + if (m_timebase == secondsPerDivision) + { + return; + } + m_timebase = secondsPerDivision; + m_dirty = true; + update(); + } + + void OscilloscopeView::setTrigger(ScopeTriggerMode mode, + int channel, + ScopeTriggerSlope slope, + double level, + double position) + { + if (mode != m_mode || channel != m_triggerChannel || slope != m_slope) + { + m_triggerArmed = false; + } + m_mode = mode; + m_triggerChannel = channel; + m_slope = slope; + m_triggerLevel = level; + m_triggerPosition = position; + m_dirty = true; + update(); + } + + void OscilloscopeView::setCursors(bool visible, int channel) + { + m_cursorsVisible = visible; + m_cursorChannel = channel; + update(); + } + + void OscilloscopeView::setRunning(bool running) + { + if (!running) + { + m_singleArmed = false; + } + if (m_running == running) + { + return; + } + m_running = running; + m_dirty = true; + update(); + emit runningChanged(running); + } + + bool OscilloscopeView::isRunning() const + { + return m_running; + } + + void OscilloscopeView::armSingle() + { + // Only a trigger after the button counts, as on a scope + resetTriggerSearch(); + const double newest = newestTime(); + if (std::isfinite(newest)) + { + m_scanTime = newest; + } + m_singleArmed = true; + m_state = DisplayState::Ready; + if (!m_running) + { + m_running = true; + emit runningChanged(true); + } + m_dirty = true; + update(); + } + + void OscilloscopeView::appendChunk(const scopeone::core::TimeSeriesChunk& chunk) + { + const double retained = 2.0 * screenSeconds() + 0.1; + for (int index = 0; index < kChannelCount; ++index) + { + if (m_channels[index].sourceId != chunk.sourceId) + { + continue; + } + Trace& trace = m_traces[index]; + // A source that starts again counts its time from zero + if (!trace.empty() && chunk.startTimeSeconds < trace.last()) + { + trace.clear(); + if (index == m_triggerChannel) + { + resetTriggerSearch(); + } + } + trace.append(chunk); + trace.trim(trace.last() - retained, kMaximumSamples); + m_dirty = true; + } + } + + void OscilloscopeView::clear() + { + for (Trace& trace : m_traces) + { + trace.clear(); + } + m_frame = {}; + m_singleArmed = false; + m_state = m_mode == ScopeTriggerMode::Roll ? DisplayState::Roll : DisplayState::Ready; + resetTriggerSearch(); + update(); + } + + bool OscilloscopeView::displayedRange(int channel, double* minimum, double* maximum) const + { + const Measurement& measurement = m_frame.measurements[channel]; + if (!m_frame.valid || !measurement.valid) + { + return false; + } + *minimum = measurement.minimum; + *maximum = measurement.maximum; + return true; + } + + QColor OscilloscopeView::channelColor(int index) + { + static const std::array colors{ + QColor(245, 211, 63), QColor(57, 208, 240), QColor(240, 79, 208), QColor(95, 227, 95)}; + return colors[static_cast(index)]; + } + + QString OscilloscopeView::formatValue(double value, const QString& unit) + { + if (!std::isfinite(value)) + { + return QStringLiteral("-"); + } + if (value == 0.0) + { + return QStringLiteral("0 %1").arg(unit); + } + static const char* const prefixes[] = {"p", "n", "µ", "m", "", "k", "M", "G"}; + const int exponent = std::clamp( + static_cast(std::floor(std::log10(std::abs(value)) / 3.0)) * 3, -12, 9); + const double mantissa = value / std::pow(10.0, exponent); + return QStringLiteral("%1 %2%3") + .arg(QString::number(mantissa, 'g', 4), + QString::fromUtf8(prefixes[(exponent + 12) / 3]), + unit); + } + + double OscilloscopeView::screenSeconds() const + { + return kHorizontalDivisions * m_timebase; + } + + // The newest time every shown channel has reached, so a frame ends where all of them have data + double OscilloscopeView::newestTime() const + { + double newest = std::numeric_limits::infinity(); + bool any = false; + for (int index = 0; index < kChannelCount; ++index) + { + if (m_channels[index].enabled && !m_traces[index].empty()) + { + newest = std::min(newest, m_traces[index].last()); + any = true; + } + } + return any ? newest : std::numeric_limits::quiet_NaN(); + } + + bool OscilloscopeView::triggerShown() const + { + return m_mode != ScopeTriggerMode::Roll || m_singleArmed; + } + + void OscilloscopeView::refresh() + { + if (!m_dirty || !m_running) + { + return; + } + m_dirty = false; + if (triggerShown()) + { + processTrigger(); + } + else + { + const double newest = newestTime(); + if (std::isfinite(newest)) + { + buildFrame(newest - screenSeconds(), newest, std::numeric_limits::quiet_NaN()); + } + m_state = DisplayState::Roll; + } + update(); + } + + // Scans the trigger channel once over the samples that arrived, from where the last scan + // ended, and shows the latest edge whose whole screen has been received. The trigger re-arms + // only after the signal moves a tenth of a division past the level, so noise on a slow edge + // does not trigger twice. + void OscilloscopeView::processTrigger() + { + const double screen = screenSeconds(); + const double before = m_triggerPosition * screen; + const double after = screen - before; + const double newest = newestTime(); + if (!std::isfinite(newest)) + { + return; + } + + double crossing = std::numeric_limits::quiet_NaN(); + const Trace& trace = m_traces[m_triggerChannel]; + if (m_channels[m_triggerChannel].enabled && !trace.empty()) + { + const double limit = newest - after; + const double hysteresis = 0.1 * m_channels[m_triggerChannel].scale; + const bool rising = m_slope == ScopeTriggerSlope::Rising; + qsizetype index = trace.indexAtOrAfter(std::max(m_scanTime, trace.first() + before)); + if (index < trace.times.size() && trace.times[index] == m_scanTime) + { + ++index; + } + for (; index < trace.times.size() && trace.times[index] <= limit; ++index) + { + const double value = trace.values[index]; + const bool beyond = rising ? value >= m_triggerLevel : value <= m_triggerLevel; + const bool rearmed = rising ? value < m_triggerLevel - hysteresis + : value > m_triggerLevel + hysteresis; + if (rearmed) + { + m_triggerArmed = true; + } + else if (m_triggerArmed && beyond && index > trace.begin) + { + const double previousValue = trace.values[index - 1]; + const double previousTime = trace.times[index - 1]; + const double fraction = (m_triggerLevel - previousValue) / (value - previousValue); + crossing = previousTime + std::clamp(fraction, 0.0, 1.0) + * (trace.times[index] - previousTime); + m_triggerArmed = false; + } + m_scanTime = trace.times[index]; + } + } + + if (std::isfinite(crossing)) + { + buildFrame(crossing - before, crossing + after, crossing); + m_state = DisplayState::Triggered; + m_sinceTrigger.restart(); + if (m_singleArmed) + { + setRunning(false); + } + return; + } + if (m_mode == ScopeTriggerMode::Auto && !m_singleArmed + && m_sinceTrigger.elapsed() > kAutoTriggerMilliseconds) + { + buildFrame(newest - screen, newest, std::numeric_limits::quiet_NaN()); + m_state = DisplayState::Auto; + m_sinceTrigger.restart(); + return; + } + if (m_state != DisplayState::Triggered || m_singleArmed) + { + m_state = DisplayState::Ready; + } + } + + void OscilloscopeView::buildFrame(double start, double end, double triggerTime) + { + m_frame.valid = true; + m_frame.start = start; + m_frame.end = end; + m_frame.triggerTime = triggerTime; + for (int channel = 0; channel < kChannelCount; ++channel) + { + QVector& times = m_frame.times[channel]; + QVector& values = m_frame.values[channel]; + Measurement& measurement = m_frame.measurements[channel]; + times.clear(); + values.clear(); + measurement = {}; + const Trace& trace = m_traces[channel]; + if (!m_channels[channel].enabled || trace.empty()) + { + continue; + } + const qsizetype first = trace.indexAtOrAfter(start); + const qsizetype last = std::upper_bound(trace.times.cbegin() + first, trace.times.cend(), end) + - trace.times.cbegin(); + if (first >= last) + { + continue; + } + times = trace.times.mid(first, last - first); + values = trace.values.mid(first, last - first); + + double minimum = values.constFirst(); + double maximum = minimum; + double sum = 0.0; + double squares = 0.0; + for (const double value : values) + { + minimum = std::min(minimum, value); + maximum = std::max(maximum, value); + sum += value; + squares += value * value; + } + const double count = static_cast(values.size()); + measurement.valid = true; + measurement.minimum = minimum; + measurement.maximum = maximum; + measurement.mean = sum / count; + measurement.rms = std::sqrt(squares / count); + + // Rising crossings of the mean, re-armed a tenth of the peak to peak below it + const double hysteresis = 0.1 * (maximum - minimum); + bool armed = false; + int crossings = 0; + double firstCrossing = 0.0; + double lastCrossing = 0.0; + for (qsizetype index = 0; index < values.size() && hysteresis > 0.0; ++index) + { + if (values[index] < measurement.mean - hysteresis) + { + armed = true; + } + else if (armed && values[index] >= measurement.mean) + { + armed = false; + if (crossings == 0) + { + firstCrossing = times[index]; + } + lastCrossing = times[index]; + ++crossings; + } + } + if (crossings >= 2 && lastCrossing > firstCrossing) + { + measurement.frequency = (crossings - 1) / (lastCrossing - firstCrossing); + } + } + } + + void OscilloscopeView::resetTriggerSearch() + { + m_triggerArmed = false; + m_scanTime = -std::numeric_limits::infinity(); + } + + // The value of a channel in divisions above the centre line; with AC coupling the mean of + // the frame is taken off, while the trigger and the measurements keep the raw value + double OscilloscopeView::divisionsOf(int channel, double value) const + { + const ScopeChannel& settings = m_channels[channel]; + const double mean = settings.acCoupled && m_frame.measurements[channel].valid + ? m_frame.measurements[channel].mean + : 0.0; + return (value - mean + settings.offset) / settings.scale; + } + + void OscilloscopeView::paintEvent(QPaintEvent*) + { + QPainter painter(this); + painter.fillRect(rect(), kBackground); + painter.setFont(font()); + const QFontMetricsF metrics(font()); + const double lineHeight = metrics.height() + 2.0; + + int readoutLines = 1 + (m_cursorsVisible ? 1 : 0); + for (const ScopeChannel& channel : m_channels) + { + readoutLines += channel.enabled ? 1 : 0; + } + m_screen = QRectF(rect()).adjusted(32.0, 18.0, -32.0, -(readoutLines * lineHeight + 12.0)); + if (m_screen.width() < 40.0 || m_screen.height() < 40.0) + { + return; + } + + drawGrid(painter); + drawTraces(painter); + drawMarkers(painter); + if (m_cursorsVisible) + { + drawCursors(painter); + } + + QString stateText; + QColor stateColor = kRunningColor; + if (!m_running) + { + stateText = tr("Stop"); + stateColor = kStoppedColor; + } + else + { + switch (m_state) + { + case DisplayState::Roll: stateText = tr("Roll"); break; + case DisplayState::Triggered: stateText = tr("Trig'd"); break; + case DisplayState::Auto: stateText = tr("Auto"); break; + case DisplayState::Ready: + stateText = m_singleArmed ? tr("Single") : tr("Ready"); + stateColor = kTriggerColor; + break; + } + } + painter.setPen(stateColor); + painter.drawText(m_screen.adjusted(6.0, 4.0, -6.0, -4.0), Qt::AlignTop | Qt::AlignRight, stateText); + + if (!m_frame.valid) + { + painter.setPen(kTextColor); + painter.drawText(m_screen, Qt::AlignCenter, tr("No signal data")); + } + drawReadouts(painter, m_screen.bottom() + 8.0); + } + + void OscilloscopeView::drawGrid(QPainter& painter) const + { + const double divisionWidth = m_screen.width() / kHorizontalDivisions; + const double divisionHeight = m_screen.height() / kVerticalDivisions; + QPen gridPen(kGridColor, 1.0, Qt::DotLine); + painter.setPen(gridPen); + for (int division = 1; division < kHorizontalDivisions; ++division) + { + const double x = m_screen.left() + division * divisionWidth; + painter.drawLine(QPointF(x, m_screen.top()), QPointF(x, m_screen.bottom())); + } + for (int division = 1; division < kVerticalDivisions; ++division) + { + const double y = m_screen.top() + division * divisionHeight; + painter.drawLine(QPointF(m_screen.left(), y), QPointF(m_screen.right(), y)); + } + + // Ticks of a fifth of a division on the two centre lines + painter.setPen(QPen(kGridColor, 1.0)); + const QPointF centre = m_screen.center(); + for (int tick = 1; tick < kHorizontalDivisions * 5; ++tick) + { + const double x = m_screen.left() + tick * divisionWidth / 5.0; + painter.drawLine(QPointF(x, centre.y() - 3.0), QPointF(x, centre.y() + 3.0)); + } + for (int tick = 1; tick < kVerticalDivisions * 5; ++tick) + { + const double y = m_screen.top() + tick * divisionHeight / 5.0; + painter.drawLine(QPointF(centre.x() - 3.0, y), QPointF(centre.x() + 3.0, y)); + } + painter.setPen(QPen(kBorderColor, 1.0)); + painter.drawRect(m_screen); + } + + void OscilloscopeView::drawTraces(QPainter& painter) const + { + if (!m_frame.valid || m_frame.end <= m_frame.start) + { + return; + } + painter.save(); + painter.setClipRect(m_screen); + const double span = m_frame.end - m_frame.start; + const double divisionHeight = m_screen.height() / kVerticalDivisions; + const double centreY = m_screen.center().y(); + const int columns = std::max(1, static_cast(m_screen.width())); + for (int channel = kChannelCount - 1; channel >= 0; --channel) + { + const QVector& times = m_frame.times[channel]; + const QVector& values = m_frame.values[channel]; + if (!m_channels[channel].enabled || values.isEmpty()) + { + continue; + } + const auto mapX = [&](double time) + { + return m_screen.left() + (time - m_frame.start) / span * m_screen.width(); + }; + const auto mapY = [&](double value) + { + return centreY - divisionsOf(channel, value) * divisionHeight; + }; + painter.setPen(QPen(channelColor(channel), 1.2)); + if (values.size() <= 2 * columns) + { + painter.setRenderHint(QPainter::Antialiasing, true); + QPainterPath path; + path.moveTo(mapX(times.constFirst()), mapY(values.constFirst())); + for (qsizetype index = 1; index < values.size(); ++index) + { + path.lineTo(mapX(times[index]), mapY(values[index])); + } + painter.drawPath(path); + painter.setRenderHint(QPainter::Antialiasing, false); + continue; + } + + // Keep the extremes of each pixel column when samples outnumber the pixels + QVector minima(columns, std::numeric_limits::infinity()); + QVector maxima(columns, -std::numeric_limits::infinity()); + for (qsizetype index = 0; index < values.size(); ++index) + { + const int column = std::clamp( + static_cast((times[index] - m_frame.start) / span * columns), 0, columns - 1); + minima[column] = std::min(minima[column], values[index]); + maxima[column] = std::max(maxima[column], values[index]); + } + double previousLow = std::numeric_limits::quiet_NaN(); + double previousHigh = previousLow; + for (int column = 0; column < columns; ++column) + { + if (!std::isfinite(minima[column])) + { + previousLow = std::numeric_limits::quiet_NaN(); + continue; + } + const double x = m_screen.left() + column; + double low = mapY(minima[column]); + double high = mapY(maxima[column]); + // Join each column to the last one so a steep edge has no gap + if (std::isfinite(previousLow)) + { + low = std::max(low, previousHigh); + high = std::min(high, previousLow); + } + painter.drawLine(QPointF(x, low), QPointF(x, high)); + previousLow = mapY(minima[column]); + previousHigh = mapY(maxima[column]); + } + } + painter.restore(); + } + + void OscilloscopeView::drawMarkers(QPainter& painter) const + { + const double divisionHeight = m_screen.height() / kVerticalDivisions; + const double centreY = m_screen.center().y(); + painter.setRenderHint(QPainter::Antialiasing, true); + for (int channel = 0; channel < kChannelCount; ++channel) + { + if (!m_channels[channel].enabled) + { + continue; + } + // The level that zero, or with AC coupling the mean, is drawn at + const double y = std::clamp(centreY - m_channels[channel].offset / m_channels[channel].scale + * divisionHeight, + m_screen.top(), m_screen.bottom()); + const double right = m_screen.left() - 3.0; + const QPolygonF tag({QPointF(right - 24.0, y - 7.0), QPointF(right - 6.0, y - 7.0), + QPointF(right, y), QPointF(right - 6.0, y + 7.0), + QPointF(right - 24.0, y + 7.0)}); + painter.setPen(Qt::NoPen); + painter.setBrush(channelColor(channel)); + painter.drawPolygon(tag); + painter.setPen(kBackground); + painter.drawText(QRectF(right - 24.0, y - 7.0, 18.0, 14.0), Qt::AlignCenter, + QString::number(channel + 1)); + } + + if (triggerShown()) + { + const double level = centreY - divisionsOf(m_triggerChannel, m_triggerLevel) * divisionHeight; + const double y = std::clamp(level, m_screen.top(), m_screen.bottom()); + const double left = m_screen.right() + 3.0; + const QPolygonF tag({QPointF(left, y), QPointF(left + 6.0, y - 7.0), + QPointF(left + 24.0, y - 7.0), QPointF(left + 24.0, y + 7.0), + QPointF(left + 6.0, y + 7.0)}); + painter.setPen(Qt::NoPen); + painter.setBrush(channelColor(m_triggerChannel)); + painter.drawPolygon(tag); + painter.setPen(kBackground); + painter.drawText(QRectF(left + 6.0, y - 7.0, 18.0, 14.0), Qt::AlignCenter, QStringLiteral("T")); + + const double x = m_screen.left() + m_triggerPosition * m_screen.width(); + const QPolygonF arrow({QPointF(x - 6.0, m_screen.top() - 12.0), + QPointF(x + 6.0, m_screen.top() - 12.0), + QPointF(x, m_screen.top() - 2.0)}); + painter.setBrush(kTriggerColor); + painter.setPen(Qt::NoPen); + painter.drawPolygon(arrow); + } + painter.setBrush(Qt::NoBrush); + painter.setRenderHint(QPainter::Antialiasing, false); + } + + QPointF OscilloscopeView::cursorPoint(int cursor) const + { + const double divisionHeight = m_screen.height() / kVerticalDivisions; + if (cursor < 2) + { + return {m_screen.left() + m_cursors[cursor] * m_screen.width(), m_screen.top()}; + } + return {m_screen.left(), m_screen.center().y() - m_cursors[cursor] * divisionHeight}; + } + + void OscilloscopeView::drawCursors(QPainter& painter) const + { + painter.setPen(QPen(kCursorColor, 1.0, Qt::DashLine)); + for (int cursor = 0; cursor < 4; ++cursor) + { + const QPointF point = cursorPoint(cursor); + const QString label = cursor < 2 ? QStringLiteral("X%1").arg(cursor + 1) + : QStringLiteral("Y%1").arg(cursor - 1); + if (cursor < 2) + { + painter.drawLine(QPointF(point.x(), m_screen.top()), QPointF(point.x(), m_screen.bottom())); + painter.drawText(QPointF(point.x() + 3.0, m_screen.bottom() - 4.0), label); + } + else + { + painter.drawLine(QPointF(m_screen.left(), point.y()), QPointF(m_screen.right(), point.y())); + painter.drawText(QPointF(m_screen.left() + 4.0, point.y() - 3.0), label); + } + } + } + + void OscilloscopeView::drawReadouts(QPainter& painter, double top) const + { + const QFontMetricsF metrics(font()); + const double lineHeight = metrics.height() + 2.0; + const double spacing = metrics.horizontalAdvance(QStringLiteral(" ")); + double y = top + metrics.ascent(); + double x = m_screen.left(); + const auto write = [&](const QString& text, const QColor& color) + { + painter.setPen(color); + painter.drawText(QPointF(x, y), text); + x += metrics.horizontalAdvance(text) + spacing; + }; + + for (int channel = 0; channel < kChannelCount; ++channel) + { + const ScopeChannel& settings = m_channels[channel]; + if (settings.enabled) + { + write(QStringLiteral("CH%1 %2/div%3") + .arg(channel + 1) + .arg(formatValue(settings.scale, settings.unit), + settings.acCoupled ? QStringLiteral(" AC") : QString()), + channelColor(channel)); + } + } + write(QStringLiteral("%1/div").arg(formatValue(m_timebase, QStringLiteral("s"))), kTextColor); + if (triggerShown()) + { + write(QStringLiteral("T CH%1 %2 %3") + .arg(m_triggerChannel + 1) + .arg(m_slope == ScopeTriggerSlope::Rising ? QStringLiteral("↑") + : QStringLiteral("↓"), + formatValue(m_triggerLevel, m_channels[m_triggerChannel].unit)), + kTriggerColor); + } + + for (int channel = 0; channel < kChannelCount; ++channel) + { + const ScopeChannel& settings = m_channels[channel]; + if (!settings.enabled) + { + continue; + } + y += lineHeight; + x = m_screen.left(); + const Measurement& measurement = m_frame.measurements[channel]; + const QColor color = channelColor(channel); + write(QStringLiteral("CH%1").arg(channel + 1), color); + if (!measurement.valid) + { + write(tr("no data"), color); + continue; + } + write(tr("Vpp %1").arg(formatValue(measurement.maximum - measurement.minimum, settings.unit)), color); + write(tr("Min %1").arg(formatValue(measurement.minimum, settings.unit)), color); + write(tr("Max %1").arg(formatValue(measurement.maximum, settings.unit)), color); + write(tr("Mean %1").arg(formatValue(measurement.mean, settings.unit)), color); + write(tr("RMS %1").arg(formatValue(measurement.rms, settings.unit)), color); + write(tr("Freq %1").arg(formatValue(measurement.frequency, QStringLiteral("Hz"))), color); + } + + if (m_cursorsVisible) + { + y += lineHeight; + x = m_screen.left(); + const double span = m_frame.valid ? m_frame.end - m_frame.start : screenSeconds(); + const double deltaTime = std::abs(m_cursors[1] - m_cursors[0]) * span; + const ScopeChannel& settings = m_channels[m_cursorChannel]; + // A level cursor reads the value the cursor channel has at that height + const auto level = [&](double divisions) + { + return divisions * settings.scale - settings.offset; + }; + write(QStringLiteral("Δt %1").arg(formatValue(deltaTime, QStringLiteral("s"))), kCursorColor); + write(QStringLiteral("1/Δt %1") + .arg(formatValue(deltaTime > 0.0 ? 1.0 / deltaTime + : std::numeric_limits::quiet_NaN(), + QStringLiteral("Hz"))), + kCursorColor); + write(QStringLiteral("CH%1 Y1 %2").arg(m_cursorChannel + 1) + .arg(formatValue(level(m_cursors[2]), settings.unit)), kCursorColor); + write(QStringLiteral("Y2 %1").arg(formatValue(level(m_cursors[3]), settings.unit)), kCursorColor); + write(QStringLiteral("ΔY %1") + .arg(formatValue(std::abs(m_cursors[2] - m_cursors[3]) * settings.scale, settings.unit)), + kCursorColor); + } + } + + void OscilloscopeView::mousePressEvent(QMouseEvent* event) + { + m_dragCursor = -1; + if (!m_cursorsVisible || !m_screen.contains(event->position())) + { + return; + } + double nearest = 8.0; + for (int cursor = 0; cursor < 4; ++cursor) + { + const QPointF point = cursorPoint(cursor); + const double distance = cursor < 2 ? std::abs(event->position().x() - point.x()) + : std::abs(event->position().y() - point.y()); + if (distance < nearest) + { + nearest = distance; + m_dragCursor = cursor; + } + } + } + + void OscilloscopeView::mouseMoveEvent(QMouseEvent* event) + { + if (m_dragCursor < 0) + { + return; + } + if (m_dragCursor < 2) + { + m_cursors[m_dragCursor] = std::clamp( + (event->position().x() - m_screen.left()) / m_screen.width(), 0.0, 1.0); + } + else + { + const double divisionHeight = m_screen.height() / kVerticalDivisions; + m_cursors[m_dragCursor] = std::clamp( + (m_screen.center().y() - event->position().y()) / divisionHeight, + -kVerticalDivisions / 2.0, kVerticalDivisions / 2.0); + } + update(); + } + + void OscilloscopeView::mouseReleaseEvent(QMouseEvent*) + { + m_dragCursor = -1; + } +} diff --git a/src/tools/daq/OscilloscopeView.h b/src/tools/daq/OscilloscopeView.h new file mode 100644 index 0000000..e83f73e --- /dev/null +++ b/src/tools/daq/OscilloscopeView.h @@ -0,0 +1,165 @@ +#pragma once + +#include "scopeone/SignalSource.h" + +#include +#include +#include +#include +#include + +#include +#include + +namespace scopeone::ui +{ + enum class ScopeTriggerMode + { + Roll, + Auto, + Normal + }; + + enum class ScopeTriggerSlope + { + Rising, + Falling + }; + + struct ScopeChannel + { + bool enabled{false}; + QString sourceId; + QString unit; + double scale{1.0}; + double offset{0.0}; + bool acCoupled{false}; + }; + + // An oscilloscope screen for the signal sources: ten by eight divisions, up to four channels + // on one time axis, a software edge trigger on the received samples, cursors and the usual + // measurements. The display can be held while the sources keep acquiring. + class OscilloscopeView final : public QWidget + { + Q_OBJECT + + public: + static constexpr int kChannelCount = 4; + static constexpr int kHorizontalDivisions = 10; + static constexpr int kVerticalDivisions = 8; + + explicit OscilloscopeView(QWidget* parent = nullptr); + + void setChannel(int index, const ScopeChannel& channel); + void setTimebase(double secondsPerDivision); + void setTrigger(ScopeTriggerMode mode, + int channel, + ScopeTriggerSlope slope, + double level, + double position); + void setCursors(bool visible, int channel); + void setRunning(bool running); + bool isRunning() const; + void armSingle(); + void appendChunk(const scopeone::core::TimeSeriesChunk& chunk); + void clear(); + bool displayedRange(int channel, double* minimum, double* maximum) const; + + static QColor channelColor(int index); + static QString formatValue(double value, const QString& unit); + + signals: + void runningChanged(bool running); + + protected: + void paintEvent(QPaintEvent* event) override; + void mousePressEvent(QMouseEvent* event) override; + void mouseMoveEvent(QMouseEvent* event) override; + void mouseReleaseEvent(QMouseEvent* event) override; + + private: + enum class DisplayState + { + Roll, + Triggered, + Auto, + Ready + }; + + // The received samples of one channel. Old samples are dropped by moving the start, and + // the vectors are compacted only now and then, so trimming does not copy on every chunk. + struct Trace + { + QVector times; + QVector values; + qsizetype begin{0}; + + bool empty() const; + double first() const; + double last() const; + qsizetype indexAtOrAfter(double time) const; + void append(const scopeone::core::TimeSeriesChunk& chunk); + void trim(double cutoff, qsizetype maximumSamples); + void clear(); + }; + + struct Measurement + { + bool valid{false}; + double minimum{0.0}; + double maximum{0.0}; + double mean{0.0}; + double rms{0.0}; + double frequency{std::numeric_limits::quiet_NaN()}; + }; + + struct Frame + { + bool valid{false}; + double start{0.0}; + double end{0.0}; + double triggerTime{std::numeric_limits::quiet_NaN()}; + std::array, kChannelCount> times; + std::array, kChannelCount> values; + std::array measurements; + }; + + double screenSeconds() const; + double newestTime() const; + bool triggerShown() const; + void refresh(); + void processTrigger(); + void buildFrame(double start, double end, double triggerTime); + void resetTriggerSearch(); + double divisionsOf(int channel, double value) const; + void drawGrid(QPainter& painter) const; + void drawTraces(QPainter& painter) const; + void drawMarkers(QPainter& painter) const; + void drawCursors(QPainter& painter) const; + void drawReadouts(QPainter& painter, double top) const; + QPointF cursorPoint(int cursor) const; + + std::array m_channels; + std::array m_traces; + Frame m_frame; + double m_timebase{1e-3}; + ScopeTriggerMode m_mode{ScopeTriggerMode::Auto}; + int m_triggerChannel{0}; + ScopeTriggerSlope m_slope{ScopeTriggerSlope::Rising}; + double m_triggerLevel{0.0}; + double m_triggerPosition{0.5}; + bool m_running{true}; + bool m_singleArmed{false}; + bool m_dirty{false}; + bool m_triggerArmed{false}; + double m_scanTime{-std::numeric_limits::infinity()}; + QElapsedTimer m_sinceTrigger; + DisplayState m_state{DisplayState::Ready}; + bool m_cursorsVisible{false}; + int m_cursorChannel{0}; + // Two time cursors as fractions of the screen width, then two level cursors in divisions + std::array m_cursors{0.3, 0.7, 2.0, -2.0}; + int m_dragCursor{-1}; + QRectF m_screen; + }; +} diff --git a/src/tools/daq/SignalMonitorWidget.cpp b/src/tools/daq/SignalMonitorWidget.cpp index 2a79760..9a60ff1 100644 --- a/src/tools/daq/SignalMonitorWidget.cpp +++ b/src/tools/daq/SignalMonitorWidget.cpp @@ -11,12 +11,12 @@ #include #include #include +#include #include #include #include -#include -#include #include +#include #include #include #include @@ -25,221 +25,76 @@ #include #include -#include +#include namespace scopeone::ui { namespace { - QString formattedValue(double value, const QString& unit) - { - return QStringLiteral("%1 %2").arg(value, 0, 'g', 5).arg(unit); - } - - QString settingKey(const QString& sourceId, const QString& parameterKey) + QString parameterSettingKey(const QString& sourceId, const QString& parameterKey) { return QStringLiteral("signalSources/%1/%2").arg(sourceId, parameterKey); } - } - class SignalTracePlot final : public QWidget - { - public: - explicit SignalTracePlot(QWidget* parent = nullptr) - : QWidget(parent) + QString monitorKey(const QString& key) { - setMinimumHeight(220); - setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + return QStringLiteral("signalMonitor/%1").arg(key); } - double appendChunk(const scopeone::core::TimeSeriesChunk& chunk, - double windowSeconds) + QString channelKey(int index, const QString& key) { - m_windowSeconds = windowSeconds; - for (int index = 0; index < chunk.values.size(); ++index) - { - m_times.append(chunk.startTimeSeconds - + (static_cast(index) + 0.5) - * chunk.sampleIntervalSeconds); - m_values.append(chunk.values[index]); - } - trim(); - update(); - return m_values.constLast(); + return QStringLiteral("signalMonitor/channel%1/%2").arg(index + 1).arg(key); } - void setWindowSeconds(double windowSeconds) + // The 1, 2, 5 steps of a scope knob from the first to the last value + QVector knobSteps(int firstExponent, int lastExponent) { - m_windowSeconds = windowSeconds; - trim(); - update(); - } - - void clear() - { - m_times.clear(); - m_values.clear(); - update(); - } - - protected: - void paintEvent(QPaintEvent*) override - { - QPainter painter(this); - painter.fillRect(rect(), palette().color(QPalette::Base)); - - const QRectF plotRect = QRectF(rect()).adjusted(62.0, 12.0, -12.0, -30.0); - if (plotRect.width() <= 1.0 || plotRect.height() <= 1.0) - { - return; - } - painter.setPen(palette().color(QPalette::Mid)); - painter.drawLine(plotRect.bottomLeft(), plotRect.bottomRight()); - painter.drawLine(plotRect.bottomLeft(), plotRect.topLeft()); - - const QColor textColor = palette().color(QPalette::Text); - if (m_times.isEmpty() || m_values.isEmpty()) - { - painter.setPen(textColor); - painter.drawText(plotRect, Qt::AlignCenter, tr("No signal data")); - return; - } - - const double endTime = m_times.constLast(); - const double startTime = std::max(0.0, endTime - m_windowSeconds); - const double displayedDuration = std::max( - endTime - startTime, (std::numeric_limits::epsilon)()); - const auto first = std::lower_bound(m_times.cbegin(), m_times.cend(), startTime); - const int firstIndex = static_cast(std::distance(m_times.cbegin(), first)); - const int lastIndex = std::min(m_times.size(), m_values.size()); - if (firstIndex >= lastIndex) - { - return; - } - - double minimum = (std::numeric_limits::max)(); - double maximum = (std::numeric_limits::lowest)(); - for (int index = firstIndex; index < lastIndex; ++index) - { - minimum = std::min(minimum, m_values[index]); - maximum = std::max(maximum, m_values[index]); - } - const bool nonnegative = minimum >= 0.0; - const bool nonpositive = maximum <= 0.0; - if (nonnegative) - { - minimum = 0.0; - } - if (nonpositive) - { - maximum = 0.0; - } - if (minimum == maximum) - { - maximum = minimum + 1.0; - } - const double padding = (maximum - minimum) * 0.05; - if (!nonnegative) - { - minimum -= padding; - } - if (!nonpositive) - { - maximum += padding; - } - - const auto mapX = [&](double time) - { - return plotRect.left() - + (time - startTime) / displayedDuration * plotRect.width(); - }; - const auto mapY = [&](double value) - { - return plotRect.bottom() - - (value - minimum) / (maximum - minimum) * plotRect.height(); - }; - - painter.setPen(textColor); - painter.drawText(QRectF(0.0, plotRect.top() - 6.0, - plotRect.left() - 6.0, 20.0), - Qt::AlignRight | Qt::AlignVCenter, - QString::number(maximum, 'g', 5)); - painter.drawText(QRectF(0.0, plotRect.bottom() - 10.0, - plotRect.left() - 6.0, 20.0), - Qt::AlignRight | Qt::AlignVCenter, - QString::number(minimum, 'g', 5)); - painter.drawText(QRectF(plotRect.left(), plotRect.bottom() + 4.0, - plotRect.width(), 20.0), - Qt::AlignLeft | Qt::AlignVCenter, - QStringLiteral("%1 s").arg(startTime, 0, 'g', 4)); - painter.drawText(QRectF(plotRect.left(), plotRect.bottom() + 4.0, - plotRect.width(), 20.0), - Qt::AlignRight | Qt::AlignVCenter, - QStringLiteral("%1 s").arg(endTime, 0, 'g', 4)); - - painter.setPen(QPen(palette().color(QPalette::Highlight), 1.5)); - const int visiblePoints = lastIndex - firstIndex; - const int pixelColumns = std::max(1, static_cast(plotRect.width())); - if (visiblePoints <= pixelColumns * 2) + QVector steps; + for (int exponent = firstExponent; exponent <= lastExponent; ++exponent) { - painter.setRenderHint(QPainter::Antialiasing, true); - QPainterPath path; - path.moveTo(mapX(m_times[firstIndex]), mapY(m_values[firstIndex])); - for (int index = firstIndex + 1; index < lastIndex; ++index) + for (const double mantissa : {1.0, 2.0, 5.0}) { - path.lineTo(mapX(m_times[index]), mapY(m_values[index])); + steps.append(mantissa * std::pow(10.0, exponent)); } - painter.drawPath(path); - return; } + return steps; + } - QVector minima(pixelColumns, maximum); - QVector maxima(pixelColumns, minimum); - QVector populated(pixelColumns, false); - // Preserve extrema when samples outnumber horizontal pixels - for (int index = firstIndex; index < lastIndex; ++index) - { - const int column = std::clamp( - static_cast((m_times[index] - startTime) - / displayedDuration * pixelColumns), - 0, - pixelColumns - 1); - minima[column] = std::min(minima[column], m_values[index]); - maxima[column] = std::max(maxima[column], m_values[index]); - populated[column] = true; - } - for (int column = 0; column < pixelColumns; ++column) + void selectNearest(QComboBox* combo, double value) + { + int best = 0; + double bestDistance = std::numeric_limits::infinity(); + for (int index = 0; index < combo->count(); ++index) { - if (populated[column]) + const double distance = std::abs(std::log(combo->itemData(index).toDouble() / value)); + if (distance < bestDistance) { - const double x = plotRect.left() + column; - painter.drawLine(QPointF(x, mapY(minima[column])), - QPointF(x, mapY(maxima[column]))); + bestDistance = distance; + best = index; } } + combo->setCurrentIndex(best); } - private: - void trim() + QComboBox* channelCombo(QWidget* parent) { - if (m_times.isEmpty()) + auto* combo = new QComboBox(parent); + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) { - return; - } - const double cutoff = m_times.constLast() - m_windowSeconds; - const auto first = std::lower_bound(m_times.cbegin(), m_times.cend(), cutoff); - const int removeCount = static_cast(std::distance(m_times.cbegin(), first)); - if (removeCount > 0) - { - m_times.remove(0, removeCount); - m_values.remove(0, removeCount); + combo->addItem(QStringLiteral("CH%1").arg(index + 1), index); } + return combo; } - QVector m_times; - QVector m_values; - double m_windowSeconds{10.0}; - }; + QDoubleSpinBox* valueSpin(QWidget* parent) + { + auto* spin = new QDoubleSpinBox(parent); + spin->setRange(-1e6, 1e6); + spin->setDecimals(4); + spin->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType); + return spin; + } + } SignalMonitorWidget::SignalMonitorWidget(scopeone::core::ScopeOneCore* core, scopeone::ui::ScopeOneToolContext& context, @@ -248,55 +103,131 @@ namespace scopeone::ui , m_core(core) , m_context(context) { - auto* layout = new QVBoxLayout(this); - auto* sourceForm = new QFormLayout(); - m_sourceCombo = new QComboBox(this); - sourceForm->addRow(tr("Source"), m_sourceCombo); - layout->addLayout(sourceForm); - - m_sourceForm = new QFormLayout(); - layout->addLayout(m_sourceForm); - - auto* traceForm = new QFormLayout(); - m_sampleIntervalSpin = new QDoubleSpinBox(this); + auto* layout = new QHBoxLayout(this); + m_view = new OscilloscopeView(this); + layout->addWidget(m_view, 1); + + auto* panel = new QWidget(); + auto* panelLayout = new QVBoxLayout(panel); + auto* scroll = new QScrollArea(this); + scroll->setWidgetResizable(true); + scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + scroll->setMinimumWidth(320); + scroll->setWidget(panel); + layout->addWidget(scroll); + + auto* acquisitionBox = new QGroupBox(tr("Acquisition"), panel); + auto* acquisitionForm = new QFormLayout(acquisitionBox); + m_sampleIntervalSpin = new QDoubleSpinBox(acquisitionBox); m_sampleIntervalSpin->setRange(0.001, 10000.0); m_sampleIntervalSpin->setDecimals(3); m_sampleIntervalSpin->setSuffix(tr(" ms")); - m_sampleIntervalSpin->setValue(10.0); - traceForm->addRow(tr("Interval"), m_sampleIntervalSpin); - - m_windowDurationSpin = new QDoubleSpinBox(this); - m_windowDurationSpin->setRange(0.1, 60.0); - m_windowDurationSpin->setDecimals(1); - m_windowDurationSpin->setSuffix(tr(" s")); - m_windowDurationSpin->setValue(10.0); - traceForm->addRow(tr("Window"), m_windowDurationSpin); - - m_scanImageCheck = new QCheckBox(tr("Build scan image"), this); - traceForm->addRow(tr("Scan"), m_scanImageCheck); - m_scanWidthSpin = new QSpinBox(this); + m_sampleIntervalSpin->setValue(1.0); + m_sampleIntervalSpin->setToolTip( + tr("The sample clock of a time series such as a DAQ input, and the bin of an event source")); + acquisitionForm->addRow(tr("Interval"), m_sampleIntervalSpin); + auto* acquisitionButtons = new QHBoxLayout(); + m_startButton = new QPushButton(tr("Start"), acquisitionBox); + m_startButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); + m_stopButton = new QPushButton(tr("Stop"), acquisitionBox); + m_stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop)); + acquisitionButtons->addWidget(m_startButton); + acquisitionButtons->addWidget(m_stopButton); + acquisitionForm->addRow(acquisitionButtons); + m_statusLabel = new QLabel(tr("No signal sources available"), acquisitionBox); + m_statusLabel->setWordWrap(true); + acquisitionForm->addRow(m_statusLabel); + panelLayout->addWidget(acquisitionBox); + + auto* horizontalBox = new QGroupBox(tr("Horizontal"), panel); + auto* horizontalForm = new QFormLayout(horizontalBox); + m_timebaseCombo = new QComboBox(horizontalBox); + for (const double step : knobSteps(-6, 1)) + { + m_timebaseCombo->addItem( + QStringLiteral("%1/div").arg(OscilloscopeView::formatValue(step, QStringLiteral("s"))), step); + } + horizontalForm->addRow(tr("Timebase"), m_timebaseCombo); + m_triggerPositionSpin = new QDoubleSpinBox(horizontalBox); + m_triggerPositionSpin->setRange(0.0, 100.0); + m_triggerPositionSpin->setDecimals(0); + m_triggerPositionSpin->setSuffix(tr(" %")); + m_triggerPositionSpin->setValue(50.0); + m_triggerPositionSpin->setToolTip(tr("Where the trigger sits on the screen, from the left edge")); + horizontalForm->addRow(tr("Trigger position"), m_triggerPositionSpin); + panelLayout->addWidget(horizontalBox); + + auto* triggerBox = new QGroupBox(tr("Trigger"), panel); + auto* triggerForm = new QFormLayout(triggerBox); + m_triggerModeCombo = new QComboBox(triggerBox); + m_triggerModeCombo->addItem(tr("Roll"), static_cast(ScopeTriggerMode::Roll)); + m_triggerModeCombo->addItem(tr("Auto"), static_cast(ScopeTriggerMode::Auto)); + m_triggerModeCombo->addItem(tr("Normal"), static_cast(ScopeTriggerMode::Normal)); + m_triggerModeCombo->setToolTip( + tr("Roll shows the newest data scrolling in; Auto and Normal hold the screen on an edge, " + "and Auto also shows the signal when no edge comes")); + triggerForm->addRow(tr("Mode"), m_triggerModeCombo); + m_triggerChannelCombo = channelCombo(triggerBox); + triggerForm->addRow(tr("Source"), m_triggerChannelCombo); + m_triggerSlopeCombo = new QComboBox(triggerBox); + m_triggerSlopeCombo->addItem(tr("Rising"), static_cast(ScopeTriggerSlope::Rising)); + m_triggerSlopeCombo->addItem(tr("Falling"), static_cast(ScopeTriggerSlope::Falling)); + triggerForm->addRow(tr("Slope"), m_triggerSlopeCombo); + m_triggerLevelSpin = valueSpin(triggerBox); + triggerForm->addRow(tr("Level"), m_triggerLevelSpin); + auto* displayButtons = new QHBoxLayout(); + m_runButton = new QPushButton(tr("Stop"), triggerBox); + m_runButton->setToolTip(tr("Hold or resume the screen; the sources keep acquiring")); + m_singleButton = new QPushButton(tr("Single"), triggerBox); + m_singleButton->setToolTip(tr("Show the next edge and hold the screen")); + displayButtons->addWidget(m_runButton); + displayButtons->addWidget(m_singleButton); + triggerForm->addRow(displayButtons); + panelLayout->addWidget(triggerBox); + + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) + { + panelLayout->addWidget(createChannelBox(index)); + } + + auto* displayBox = new QGroupBox(tr("Display"), panel); + auto* displayForm = new QFormLayout(displayBox); + m_autoScaleButton = new QPushButton(tr("Auto Scale"), displayBox); + m_autoScaleButton->setToolTip(tr("Fit the scale and offset of each channel to the screen")); + displayForm->addRow(m_autoScaleButton); + m_cursorsCheck = new QCheckBox(tr("Show cursors"), displayBox); + m_cursorsCheck->setToolTip(tr("Drag the cursors on the screen")); + displayForm->addRow(tr("Cursors"), m_cursorsCheck); + m_cursorChannelCombo = channelCombo(displayBox); + displayForm->addRow(tr("Cursor channel"), m_cursorChannelCombo); + panelLayout->addWidget(displayBox); + + m_scanGroup = new QGroupBox(tr("Scan image from CH1"), panel); + m_scanGroup->setCheckable(true); + m_scanGroup->setChecked(false); + auto* scanForm = new QFormLayout(m_scanGroup); + m_scanWidthSpin = new QSpinBox(m_scanGroup); m_scanWidthSpin->setRange(1, 8192); m_scanWidthSpin->setValue(256); - traceForm->addRow(tr("Width"), m_scanWidthSpin); - m_scanHeightSpin = new QSpinBox(this); + scanForm->addRow(tr("Width"), m_scanWidthSpin); + m_scanHeightSpin = new QSpinBox(m_scanGroup); m_scanHeightSpin->setRange(1, 8192); m_scanHeightSpin->setValue(256); - traceForm->addRow(tr("Height"), m_scanHeightSpin); - m_scanGainSpin = new QSpinBox(this); + scanForm->addRow(tr("Height"), m_scanHeightSpin); + m_scanGainSpin = new QSpinBox(m_scanGroup); m_scanGainSpin->setRange(1, 65535); m_scanGainSpin->setValue(1000); m_scanGainSpin->setSuffix(tr(" x")); - traceForm->addRow(tr("Gain"), m_scanGainSpin); - - m_scanAverageFramesSpin = new QSpinBox(this); + scanForm->addRow(tr("Gain"), m_scanGainSpin); + m_scanAverageFramesSpin = new QSpinBox(m_scanGroup); m_scanAverageFramesSpin->setRange(1, 256); m_scanAverageFramesSpin->setValue(1); m_scanAverageFramesSpin->setToolTip( tr("Average this many complete scan images before publishing one result")); - traceForm->addRow(tr("Average frames"), m_scanAverageFramesSpin); + scanForm->addRow(tr("Average frames"), m_scanAverageFramesSpin); const auto markerCombo = [this](quint32 value) { - auto* combo = new QComboBox(this); + auto* combo = new QComboBox(m_scanGroup); combo->addItem(tr("Off"), 0U); combo->addItem(tr("Marker 1"), 1U); combo->addItem(tr("Marker 2"), 2U); @@ -306,60 +237,42 @@ namespace scopeone::ui return combo; }; m_frameStartMarkerCombo = markerCombo(1); - traceForm->addRow(tr("Frame start"), m_frameStartMarkerCombo); + scanForm->addRow(tr("Frame start"), m_frameStartMarkerCombo); m_lineMarkerCombo = markerCombo(2); - m_lineMarkerCombo->setToolTip( - tr("One marker completes one full back-and-forth scan line")); - traceForm->addRow(tr("Line marker"), m_lineMarkerCombo); + m_lineMarkerCombo->setToolTip(tr("One marker completes one full back-and-forth scan line")); + scanForm->addRow(tr("Line marker"), m_lineMarkerCombo); m_frameEndMarkerCombo = markerCombo(0); - traceForm->addRow(tr("Frame end"), m_frameEndMarkerCombo); - m_serpentineCheck = new QCheckBox(tr("Reverse alternate lines"), this); - traceForm->addRow(tr("Scan order"), m_serpentineCheck); - m_mirrorHorizontalCheck = new QCheckBox(tr("Mirror left/right"), this); - traceForm->addRow(tr("Image orientation"), m_mirrorHorizontalCheck); - layout->addLayout(traceForm); - - auto* buttonLayout = new QHBoxLayout(); - m_startButton = new QPushButton(tr("Start"), this); - m_startButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); - m_stopButton = new QPushButton(tr("Stop"), this); - m_stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop)); - m_stopButton->setEnabled(false); - buttonLayout->addWidget(m_startButton); - buttonLayout->addWidget(m_stopButton); - layout->addLayout(buttonLayout); - - m_valueLabel = new QLabel(tr("Value: -"), this); - m_statusLabel = new QLabel(tr("No signal sources available"), this); - m_statusLabel->setWordWrap(true); - layout->addWidget(m_valueLabel); - layout->addWidget(m_statusLabel); - m_plot = new SignalTracePlot(this); - layout->addWidget(m_plot, 1); - - connect(m_sourceCombo, &QComboBox::currentIndexChanged, - this, &SignalMonitorWidget::rebuildSourceParameters); - connect(m_startButton, &QPushButton::clicked, - this, &SignalMonitorWidget::startAcquisition); - connect(m_stopButton, &QPushButton::clicked, this, [this]() - { - m_core->stopSignalTrace(m_activeSourceId); + scanForm->addRow(tr("Frame end"), m_frameEndMarkerCombo); + m_serpentineCheck = new QCheckBox(tr("Reverse alternate lines"), m_scanGroup); + scanForm->addRow(tr("Scan order"), m_serpentineCheck); + m_mirrorHorizontalCheck = new QCheckBox(tr("Mirror left/right"), m_scanGroup); + scanForm->addRow(tr("Image orientation"), m_mirrorHorizontalCheck); + panelLayout->addWidget(m_scanGroup); + panelLayout->addStretch(1); + + loadSettings(); + + connect(m_startButton, &QPushButton::clicked, this, &SignalMonitorWidget::startAcquisition); + connect(m_stopButton, &QPushButton::clicked, this, &SignalMonitorWidget::stopAcquisition); + connect(m_autoScaleButton, &QPushButton::clicked, this, &SignalMonitorWidget::autoScale); + connect(m_runButton, &QPushButton::clicked, this, [this]() + { + m_view->setRunning(!m_view->isRunning()); }); - connect(m_windowDurationSpin, &QDoubleSpinBox::valueChanged, - m_plot, &SignalTracePlot::setWindowSeconds); - connect(m_scanImageCheck, &QCheckBox::toggled, this, [this](bool enabled) - { - const bool editable = enabled && m_scanImageCheck->isEnabled(); - m_scanWidthSpin->setEnabled(editable); - m_scanHeightSpin->setEnabled(editable); - m_scanGainSpin->setEnabled(editable); - m_scanAverageFramesSpin->setEnabled(editable); - m_frameStartMarkerCombo->setEnabled(editable); - m_lineMarkerCombo->setEnabled(editable); - m_frameEndMarkerCombo->setEnabled(editable); - m_serpentineCheck->setEnabled(editable); - m_mirrorHorizontalCheck->setEnabled(editable); + connect(m_singleButton, &QPushButton::clicked, m_view, &OscilloscopeView::armSingle); + connect(m_view, &OscilloscopeView::runningChanged, this, [this](bool running) + { + m_runButton->setText(running ? tr("Stop") : tr("Run")); }); + connect(m_timebaseCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); + connect(m_triggerPositionSpin, &QDoubleSpinBox::valueChanged, this, &SignalMonitorWidget::applyView); + connect(m_triggerModeCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); + connect(m_triggerChannelCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::updateUnits); + connect(m_triggerSlopeCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); + connect(m_triggerLevelSpin, &QDoubleSpinBox::valueChanged, this, &SignalMonitorWidget::applyView); + connect(m_cursorsCheck, &QCheckBox::toggled, this, &SignalMonitorWidget::applyView); + connect(m_cursorChannelCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); + connect(m_core, &scopeone::core::ScopeOneCore::signalSourcesChanged, this, &SignalMonitorWidget::refreshSources); connect(m_core, &scopeone::core::ScopeOneCore::signalTimeSeriesReady, @@ -373,109 +286,134 @@ namespace scopeone::ui }); connect(m_core, &scopeone::core::ScopeOneCore::staticFramePublished, this, - [this](const QString& sourceId, - const QString&, - const scopeone::core::ImageFrame&) + [this](const QString& sourceId, const QString&, const scopeone::core::ImageFrame&) { - if (!m_scanImageCheck->isChecked() + if (m_scanSourceId.isEmpty() || m_scanLayerShown - || sourceId != QStringLiteral("scan:%1").arg(m_activeSourceId)) + || sourceId != QStringLiteral("scan:%1").arg(m_scanSourceId)) { return; } m_scanLayerShown = true; - m_context.showLayers({ - scopeone::core::ScopeOneCore::staticLayerKey(sourceId)}); + m_context.showLayers({scopeone::core::ScopeOneCore::staticLayerKey(sourceId)}); }); connect(m_core, &scopeone::core::ScopeOneCore::scanImageSessionReady, this, [this](const std::shared_ptr& session) { - m_statusLabel->setText( - tr("Scan session ready: %1 frames") - .arg(session->recordedFrameCount())); + m_statusLabel->setText(tr("Scan session ready: %1 frames") + .arg(session->recordedFrameCount())); m_context.presentSession(session, tr("Reconstructed Scan")); }); - m_scanImageCheck->setChecked(false); - m_scanWidthSpin->setEnabled(false); - m_scanHeightSpin->setEnabled(false); - m_scanGainSpin->setEnabled(false); - m_scanAverageFramesSpin->setEnabled(false); - m_frameStartMarkerCombo->setEnabled(false); - m_lineMarkerCombo->setEnabled(false); - m_frameEndMarkerCombo->setEnabled(false); - m_serpentineCheck->setEnabled(false); - m_mirrorHorizontalCheck->setEnabled(false); - refreshSources(); } - void SignalMonitorWidget::refreshSources() + SignalMonitorWidget::~SignalMonitorWidget() + { + saveSettings(); + } + + QWidget* SignalMonitorWidget::createChannelBox(int index) { - const QString selectedId = m_sourceCombo->currentData().toString(); - const QList sources = m_core->signalSources(); + ChannelControls& channel = m_channels[index]; + channel.box = new QGroupBox(QStringLiteral("CH%1").arg(index + 1), this); + channel.box->setCheckable(true); + channel.box->setChecked(index == 0); + channel.box->setStyleSheet(QStringLiteral("QGroupBox::title { color: %1; }") + .arg(OscilloscopeView::channelColor(index).name())); + auto* form = new QFormLayout(channel.box); + channel.source = new QComboBox(channel.box); + form->addRow(tr("Source"), channel.source); + channel.scale = new QComboBox(channel.box); + for (const double step : knobSteps(-6, 5)) { - const QSignalBlocker blocker(m_sourceCombo); - m_sourceCombo->clear(); - for (const auto& source : sources) - { - m_sourceCombo->addItem(source.name, source.id); - m_sourceCombo->setItemData( - m_sourceCombo->count() - 1, - QStringLiteral("%1, %2 [%3]") - .arg(source.provider, source.quantity, source.unit), - Qt::ToolTipRole); - } - const int selectedIndex = m_sourceCombo->findData(selectedId); - if (selectedIndex >= 0) - { - m_sourceCombo->setCurrentIndex(selectedIndex); - } + channel.scale->addItem(QString(), step); } - const bool available = !sources.isEmpty(); - if (available) + selectNearest(channel.scale, 1.0); + form->addRow(tr("Scale"), channel.scale); + channel.offset = valueSpin(channel.box); + channel.offset->setToolTip(tr("Added to the signal before it is drawn")); + form->addRow(tr("Offset"), channel.offset); + channel.coupling = new QComboBox(channel.box); + channel.coupling->addItem(tr("DC"), false); + channel.coupling->addItem(tr("AC"), true); + channel.coupling->setToolTip(tr("AC draws the signal without its mean over the screen")); + form->addRow(tr("Coupling"), channel.coupling); + channel.parameterForm = new QFormLayout(); + form->addRow(channel.parameterForm); + + connect(channel.box, &QGroupBox::toggled, this, [this]() { - const QString sourceId = m_sourceCombo->currentData().toString(); - const auto state = m_core->signalSourceState(sourceId); - const bool active = state == scopeone::core::SignalSourceState::Starting - || state == scopeone::core::SignalSourceState::Running - || state == scopeone::core::SignalSourceState::Stopping; - m_startButton->setEnabled(!active); - m_stopButton->setEnabled(state == scopeone::core::SignalSourceState::Starting - || state == scopeone::core::SignalSourceState::Running); - m_statusLabel->setText(m_core->signalSourceStateMessage(sourceId)); - } - else + applyView(); + updateControls(); + }); + connect(channel.source, &QComboBox::currentIndexChanged, this, [this, index]() { - m_startButton->setEnabled(false); - m_stopButton->setEnabled(false); - m_statusLabel->setText(tr("No signal source plugins found")); + m_channels[index].preferredSource = channelSource(index); + rebuildParameters(index); + updateUnits(); + }); + connect(channel.scale, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); + connect(channel.offset, &QDoubleSpinBox::valueChanged, this, &SignalMonitorWidget::applyView); + connect(channel.coupling, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); + return channel.box; + } + + void SignalMonitorWidget::refreshSources() + { + m_sources = m_core->signalSources(); + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) + { + ChannelControls& channel = m_channels[index]; + { + const QSignalBlocker blocker(channel.source); + channel.source->clear(); + for (const auto& source : m_sources) + { + channel.source->addItem(source.name, source.id); + channel.source->setItemData(channel.source->count() - 1, + QStringLiteral("%1, %2 [%3]") + .arg(source.provider, source.quantity, source.unit), + Qt::ToolTipRole); + } + int selected = channel.source->findData(channel.preferredSource); + if (selected < 0 && !m_sources.isEmpty()) + { + // A new channel shows the next source, so the channels start on different ones + selected = std::min(index, m_sources.size() - 1); + } + channel.source->setCurrentIndex(selected); + } + rebuildParameters(index); } - rebuildSourceParameters(); + updateUnits(); + updateControls(); + updateStatus(); } - void SignalMonitorWidget::rebuildSourceParameters() + void SignalMonitorWidget::rebuildParameters(int index) { - while (m_sourceForm->rowCount() > 0) + ChannelControls& channel = m_channels[index]; + while (channel.parameterForm->rowCount() > 0) { - m_sourceForm->removeRow(0); + channel.parameterForm->removeRow(0); } - m_parameterEditors.clear(); + channel.editors.clear(); - const auto descriptor = currentDescriptor(); + const auto source = descriptor(channelSource(index)); QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - for (const auto& parameter : descriptor.parameters) + for (const auto& parameter : source.parameters) { const QVariant storedValue = settings.value( - settingKey(descriptor.id, parameter.key), parameter.defaultValue); + parameterSettingKey(source.id, parameter.key), parameter.defaultValue); QWidget* editor = nullptr; + QWidget* row = nullptr; if (parameter.type == scopeone::core::SignalParameterType::Integer) { - auto* spin = new QSpinBox(this); + auto* spin = new QSpinBox(channel.box); if (parameter.hasRange) { - spin->setRange(static_cast(parameter.minimum), - static_cast(parameter.maximum)); + spin->setRange(static_cast(parameter.minimum), static_cast(parameter.maximum)); } spin->setSuffix(parameter.suffix); spin->setValue(storedValue.toInt()); @@ -483,7 +421,7 @@ namespace scopeone::ui } else if (parameter.type == scopeone::core::SignalParameterType::Real) { - auto* spin = new QDoubleSpinBox(this); + auto* spin = new QDoubleSpinBox(channel.box); if (parameter.hasRange) { spin->setRange(parameter.minimum, parameter.maximum); @@ -494,24 +432,24 @@ namespace scopeone::ui } else if (parameter.type == scopeone::core::SignalParameterType::Choice) { - auto* combo = new QComboBox(this); - for (int index = 0; index < parameter.choices.size(); ++index) + auto* combo = new QComboBox(channel.box); + for (int choice = 0; choice < parameter.choices.size(); ++choice) { - const QString name = index < parameter.choiceNames.size() - ? parameter.choiceNames[index] - : parameter.choices[index].toString(); - combo->addItem(name, parameter.choices[index]); + const QString name = choice < parameter.choiceNames.size() + ? parameter.choiceNames[choice] + : parameter.choices[choice].toString(); + combo->addItem(name, parameter.choices[choice]); } combo->setCurrentIndex(std::max(0, combo->findData(storedValue))); editor = combo; } else { - auto* edit = new QLineEdit(storedValue.toString(), this); + auto* edit = new QLineEdit(storedValue.toString(), channel.box); editor = edit; if (parameter.type == scopeone::core::SignalParameterType::File) { - auto* row = new QWidget(this); + row = new QWidget(channel.box); auto* rowLayout = new QHBoxLayout(row); rowLayout->setContentsMargins(0, 0, 0, 0); rowLayout->setSpacing(4); @@ -520,180 +458,337 @@ namespace scopeone::ui browse->setToolTip(tr("Select file")); rowLayout->addWidget(edit, 1); rowLayout->addWidget(browse); - connect(browse, &QToolButton::clicked, this, - [this, edit, parameter]() - { - const QString path = QFileDialog::getOpenFileName( - this, - parameter.name, - QFileInfo(edit->text()).absolutePath(), - parameter.fileFilter); - if (!path.isEmpty()) - { - edit->setText(QDir::toNativeSeparators(path)); - } - }); - m_sourceForm->addRow(parameter.name, row); - m_parameterEditors.insert(parameter.key, edit); - continue; + connect(browse, &QToolButton::clicked, this, [this, edit, parameter]() + { + const QString path = QFileDialog::getOpenFileName( + this, parameter.name, QFileInfo(edit->text()).absolutePath(), parameter.fileFilter); + if (!path.isEmpty()) + { + edit->setText(QDir::toNativeSeparators(path)); + } + }); } } - m_sourceForm->addRow(parameter.name, editor); - m_parameterEditors.insert(parameter.key, editor); + channel.parameterForm->addRow(parameter.name, row ? row : editor); + channel.editors.insert(parameter.key, editor); + } + updateControls(); + } + + // The scale, offset and trigger level are in the unit of the source their channel shows + void SignalMonitorWidget::updateUnits() + { + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) + { + ChannelControls& channel = m_channels[index]; + const QString unit = descriptor(channelSource(index)).unit; + const QSignalBlocker blocker(channel.scale); + for (int step = 0; step < channel.scale->count(); ++step) + { + channel.scale->setItemText( + step, QStringLiteral("%1/div").arg( + OscilloscopeView::formatValue(channel.scale->itemData(step).toDouble(), unit))); + } + channel.offset->setSuffix(QStringLiteral(" %1").arg(unit)); } - m_sampleIntervalSpin->setEnabled( - descriptor.streamType == scopeone::core::SignalStreamType::TimestampedEvents); + m_triggerLevelSpin->setSuffix(QStringLiteral(" %1").arg( + descriptor(channelSource(m_triggerChannelCombo->currentIndex())).unit)); + applyView(); + } + + void SignalMonitorWidget::applyView() + { + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) + { + const ChannelControls& controls = m_channels[index]; + ScopeChannel channel; + channel.enabled = controls.box->isChecked(); + channel.sourceId = channelSource(index); + channel.unit = descriptor(channel.sourceId).unit; + channel.scale = controls.scale->currentData().toDouble(); + channel.offset = controls.offset->value(); + channel.acCoupled = controls.coupling->currentData().toBool(); + m_view->setChannel(index, channel); + } + m_view->setTimebase(m_timebaseCombo->currentData().toDouble()); + m_view->setTrigger(static_cast(m_triggerModeCombo->currentData().toInt()), + m_triggerChannelCombo->currentIndex(), + static_cast(m_triggerSlopeCombo->currentData().toInt()), + m_triggerLevelSpin->value(), + m_triggerPositionSpin->value() / 100.0); + m_view->setCursors(m_cursorsCheck->isChecked(), m_cursorChannelCombo->currentIndex()); } void SignalMonitorWidget::startAcquisition() { - const auto descriptor = currentDescriptor(); - if (descriptor.id.isEmpty()) + QStringList sources; + QHash firstChannel; + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) { + const QString sourceId = channelSource(index); + if (m_channels[index].box->isChecked() && !sourceId.isEmpty() && !sources.contains(sourceId)) + { + sources.append(sourceId); + firstChannel.insert(sourceId, index); + } + } + if (sources.isEmpty()) + { + m_statusLabel->setText(tr("Turn on a channel that shows a source")); return; } + saveSettings(); - scopeone::core::SignalAcquisitionConfig config; - config.sourceId = descriptor.id; - config.sampleIntervalSeconds = m_sampleIntervalSpin->value() / 1000.0; - config.scanImage.enabled = m_scanImageCheck->isChecked(); - config.scanImage.width = m_scanWidthSpin->value(); - config.scanImage.height = m_scanHeightSpin->value(); - config.scanImage.gain = static_cast(m_scanGainSpin->value()); - config.scanImage.averageFrames = m_scanAverageFramesSpin->value(); - config.scanImage.frameStartMarker = - m_frameStartMarkerCombo->currentData().toUInt(); - config.scanImage.lineMarker = - m_lineMarkerCombo->currentData().toUInt(); - config.scanImage.frameEndMarker = - m_frameEndMarkerCombo->currentData().toUInt(); - config.scanImage.serpentine = m_serpentineCheck->isChecked(); - config.scanImage.mirrorHorizontal = m_mirrorHorizontalCheck->isChecked(); - config.sourceSettings = sourceSettings(); + const bool scanImage = m_scanGroup->isChecked() && m_channels[0].box->isChecked(); + m_scanSourceId = scanImage ? channelSource(0) : QString(); + m_scanLayerShown = false; + m_sourceMessages.clear(); + m_view->clear(); + m_view->setRunning(true); QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - for (auto it = config.sourceSettings.constBegin(); - it != config.sourceSettings.constEnd(); ++it) + for (const QString& sourceId : sources) + { + scopeone::core::SignalAcquisitionConfig config; + config.sourceId = sourceId; + config.sampleIntervalSeconds = m_sampleIntervalSpin->value() / 1000.0; + config.scanImage.enabled = sourceId == m_scanSourceId; + config.scanImage.width = m_scanWidthSpin->value(); + config.scanImage.height = m_scanHeightSpin->value(); + config.scanImage.gain = static_cast(m_scanGainSpin->value()); + config.scanImage.averageFrames = m_scanAverageFramesSpin->value(); + config.scanImage.frameStartMarker = m_frameStartMarkerCombo->currentData().toUInt(); + config.scanImage.lineMarker = m_lineMarkerCombo->currentData().toUInt(); + config.scanImage.frameEndMarker = m_frameEndMarkerCombo->currentData().toUInt(); + config.scanImage.serpentine = m_serpentineCheck->isChecked(); + config.scanImage.mirrorHorizontal = m_mirrorHorizontalCheck->isChecked(); + config.sourceSettings = sourceSettings(firstChannel.value(sourceId)); + for (auto it = config.sourceSettings.constBegin(); it != config.sourceSettings.constEnd(); ++it) + { + settings.setValue(parameterSettingKey(sourceId, it.key()), it.value()); + } + + QString errorMessage; + if (!m_core->startSignalTrace(config, &errorMessage)) + { + qWarning().noquote() << errorMessage; + stopAcquisition(); + m_statusLabel->setText(errorMessage); + return; + } + if (!m_activeSources.contains(sourceId)) + { + m_activeSources.append(sourceId); + } + } + updateControls(); + } + + void SignalMonitorWidget::stopAcquisition() + { + for (const QString& sourceId : QStringList(m_activeSources)) { - settings.setValue(settingKey(descriptor.id, it.key()), it.value()); + m_core->stopSignalTrace(sourceId); } + } - m_plot->clear(); - m_valueLabel->setText(QStringLiteral("%1: -").arg(descriptor.quantity)); - QString errorMessage; - if (!m_core->startSignalTrace(config, &errorMessage)) + // Fits each shown channel into six of the eight divisions around the centre and puts the + // trigger level in the middle of its channel when it is outside the signal + void SignalMonitorWidget::autoScale() + { + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) { - m_statusLabel->setText(errorMessage); - qWarning().noquote() << errorMessage; - return; + ChannelControls& channel = m_channels[index]; + double minimum = 0.0; + double maximum = 0.0; + if (!channel.box->isChecked() || !m_view->displayedRange(index, &minimum, &maximum)) + { + continue; + } + const double span = maximum > minimum ? maximum - minimum : std::max(std::abs(maximum), 1e-3); + int step = channel.scale->count() - 1; + for (int candidate = 0; candidate < channel.scale->count(); ++candidate) + { + if (channel.scale->itemData(candidate).toDouble() * 6.0 >= span) + { + step = candidate; + break; + } + } + const QSignalBlocker scaleBlocker(channel.scale); + const QSignalBlocker offsetBlocker(channel.offset); + channel.scale->setCurrentIndex(step); + channel.offset->setValue(channel.coupling->currentData().toBool() ? 0.0 : -(minimum + maximum) / 2.0); + if (index == m_triggerChannelCombo->currentIndex() + && (m_triggerLevelSpin->value() <= minimum || m_triggerLevelSpin->value() >= maximum)) + { + const QSignalBlocker levelBlocker(m_triggerLevelSpin); + m_triggerLevelSpin->setValue((minimum + maximum) / 2.0); + } } - m_activeSourceId = descriptor.id; - m_scanLayerShown = false; + applyView(); } - void SignalMonitorWidget::handleTimeSeries( - const scopeone::core::TimeSeriesChunk& chunk) + void SignalMonitorWidget::handleTimeSeries(const scopeone::core::TimeSeriesChunk& chunk) { - if (!chunk.isValid() || chunk.sourceId != m_activeSourceId) + if (chunk.isValid()) { - return; + m_view->appendChunk(chunk); } - const double latestValue = m_plot->appendChunk( - chunk, m_windowDurationSpin->value()); - m_valueLabel->setText(QStringLiteral("%1: %2") - .arg(chunk.quantity, formattedValue(latestValue, chunk.unit))); - m_valueLabel->setToolTip(tr("Input events: %1\nMarkers: %2") - .arg(chunk.totalInputEvents) - .arg(chunk.totalMarkers)); } - void SignalMonitorWidget::handleStateChanged( - const QString& sourceId, - scopeone::core::SignalSourceState state, - const QString& message) + void SignalMonitorWidget::handleStateChanged(const QString& sourceId, + scopeone::core::SignalSourceState state, + const QString& message) { - if (sourceId != m_activeSourceId - && sourceId != m_sourceCombo->currentData().toString()) + bool shown = m_activeSources.contains(sourceId); + for (int index = 0; index < OscilloscopeView::kChannelCount && !shown; ++index) + { + shown = channelSource(index) == sourceId; + } + if (!shown) { return; } - m_statusLabel->setText(message); + m_sourceMessages.insert(sourceId, message); const bool active = state == scopeone::core::SignalSourceState::Starting || state == scopeone::core::SignalSourceState::Running || state == scopeone::core::SignalSourceState::Stopping; - setSourceControlsEnabled(!active); - m_startButton->setEnabled(!active); - m_stopButton->setEnabled(state == scopeone::core::SignalSourceState::Starting - || state == scopeone::core::SignalSourceState::Running); - if (active) + if (active && !m_activeSources.contains(sourceId)) { - m_activeSourceId = sourceId; + m_activeSources.append(sourceId); } - else if (m_activeSourceId == sourceId) + else if (!active) { - m_activeSourceId.clear(); + m_activeSources.removeAll(sourceId); } + updateControls(); + updateStatus(); } - void SignalMonitorWidget::setSourceControlsEnabled(bool enabled) + // The sources and their settings are fixed while they run; the screen stays adjustable + void SignalMonitorWidget::updateControls() { - m_sourceCombo->setEnabled(enabled); - for (QWidget* editor : m_parameterEditors) + const bool running = !m_activeSources.isEmpty(); + bool startable = false; + for (const ChannelControls& channel : m_channels) { - editor->setEnabled(enabled); - if (QWidget* row = editor->parentWidget(); row && row != this) + channel.source->setEnabled(!running); + for (QWidget* editor : channel.editors) { - row->setEnabled(enabled); + // A file parameter sits in a row with its browse button + QWidget* const row = editor->parentWidget(); + (row && row != channel.box ? row : editor)->setEnabled(!running); } + startable = startable || (channel.box->isChecked() && channel.source->currentIndex() >= 0); } - m_sampleIntervalSpin->setEnabled( - enabled && currentDescriptor().streamType - == scopeone::core::SignalStreamType::TimestampedEvents); - m_scanImageCheck->setEnabled(enabled); - const bool scanEditable = enabled && m_scanImageCheck->isChecked(); - m_scanWidthSpin->setEnabled(scanEditable); - m_scanHeightSpin->setEnabled(scanEditable); - m_scanGainSpin->setEnabled(scanEditable); - m_scanAverageFramesSpin->setEnabled(scanEditable); - m_frameStartMarkerCombo->setEnabled(scanEditable); - m_lineMarkerCombo->setEnabled(scanEditable); - m_frameEndMarkerCombo->setEnabled(scanEditable); - m_serpentineCheck->setEnabled(scanEditable); - m_mirrorHorizontalCheck->setEnabled(scanEditable); + m_sampleIntervalSpin->setEnabled(!running); + m_scanGroup->setEnabled(!running); + m_startButton->setEnabled(!running && startable); + m_stopButton->setEnabled(running); } - scopeone::core::SignalSourceDescriptor SignalMonitorWidget::currentDescriptor() const + void SignalMonitorWidget::updateStatus() { - const QString sourceId = m_sourceCombo->currentData().toString(); - for (const auto& descriptor : m_core->signalSources()) + if (m_sources.isEmpty()) + { + m_statusLabel->setText(tr("No signal sources available")); + return; + } + QStringList lines; + for (auto it = m_sourceMessages.constBegin(); it != m_sourceMessages.constEnd(); ++it) { - if (descriptor.id == sourceId) + const QString name = descriptor(it.key()).name; + lines.append(QStringLiteral("%1: %2").arg(name.isEmpty() ? it.key() : name, it.value())); + } + lines.sort(); + m_statusLabel->setText(lines.isEmpty() ? tr("Idle") : lines.join(QLatin1Char('\n'))); + } + + void SignalMonitorWidget::loadSettings() + { + QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); + m_sampleIntervalSpin->setValue(settings.value(monitorKey(QStringLiteral("intervalMs")), 1.0).toDouble()); + selectNearest(m_timebaseCombo, settings.value(monitorKey(QStringLiteral("timebase")), 1e-3).toDouble()); + m_triggerPositionSpin->setValue(settings.value(monitorKey(QStringLiteral("triggerPosition")), 50.0).toDouble()); + m_triggerModeCombo->setCurrentIndex(std::max(0, m_triggerModeCombo->findData( + settings.value(monitorKey(QStringLiteral("triggerMode")), static_cast(ScopeTriggerMode::Auto))))); + m_triggerChannelCombo->setCurrentIndex( + std::clamp(settings.value(monitorKey(QStringLiteral("triggerChannel")), 0).toInt(), + 0, OscilloscopeView::kChannelCount - 1)); + m_triggerSlopeCombo->setCurrentIndex(std::max(0, m_triggerSlopeCombo->findData( + settings.value(monitorKey(QStringLiteral("triggerSlope")), static_cast(ScopeTriggerSlope::Rising))))); + m_triggerLevelSpin->setValue(settings.value(monitorKey(QStringLiteral("triggerLevel")), 0.0).toDouble()); + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) + { + ChannelControls& channel = m_channels[index]; + channel.box->setChecked(settings.value(channelKey(index, QStringLiteral("enabled")), index == 0).toBool()); + channel.preferredSource = settings.value(channelKey(index, QStringLiteral("source"))).toString(); + selectNearest(channel.scale, settings.value(channelKey(index, QStringLiteral("scale")), 1.0).toDouble()); + channel.offset->setValue(settings.value(channelKey(index, QStringLiteral("offset")), 0.0).toDouble()); + channel.coupling->setCurrentIndex( + settings.value(channelKey(index, QStringLiteral("acCoupled")), false).toBool() ? 1 : 0); + } + } + + void SignalMonitorWidget::saveSettings() const + { + QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); + settings.setValue(monitorKey(QStringLiteral("intervalMs")), m_sampleIntervalSpin->value()); + settings.setValue(monitorKey(QStringLiteral("timebase")), m_timebaseCombo->currentData()); + settings.setValue(monitorKey(QStringLiteral("triggerPosition")), m_triggerPositionSpin->value()); + settings.setValue(monitorKey(QStringLiteral("triggerMode")), m_triggerModeCombo->currentData()); + settings.setValue(monitorKey(QStringLiteral("triggerChannel")), m_triggerChannelCombo->currentIndex()); + settings.setValue(monitorKey(QStringLiteral("triggerSlope")), m_triggerSlopeCombo->currentData()); + settings.setValue(monitorKey(QStringLiteral("triggerLevel")), m_triggerLevelSpin->value()); + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) + { + const ChannelControls& channel = m_channels[index]; + settings.setValue(channelKey(index, QStringLiteral("enabled")), channel.box->isChecked()); + settings.setValue(channelKey(index, QStringLiteral("source")), channel.preferredSource); + settings.setValue(channelKey(index, QStringLiteral("scale")), channel.scale->currentData()); + settings.setValue(channelKey(index, QStringLiteral("offset")), channel.offset->value()); + settings.setValue(channelKey(index, QStringLiteral("acCoupled")), channel.coupling->currentData()); + } + } + + scopeone::core::SignalSourceDescriptor SignalMonitorWidget::descriptor(const QString& sourceId) const + { + for (const auto& source : m_sources) + { + if (source.id == sourceId) { - return descriptor; + return source; } } return {}; } - QVariantMap SignalMonitorWidget::sourceSettings() const + QString SignalMonitorWidget::channelSource(int index) const + { + return m_channels[index].source->currentData().toString(); + } + + QVariantMap SignalMonitorWidget::sourceSettings(int index) const { QVariantMap values; - for (auto it = m_parameterEditors.constBegin(); - it != m_parameterEditors.constEnd(); ++it) + const ChannelControls& channel = m_channels[index]; + for (auto it = channel.editors.constBegin(); it != channel.editors.constEnd(); ++it) { - if (const auto* spin = qobject_cast(it.value())) + QWidget* const editor = it.value(); + if (const auto* spin = qobject_cast(editor)) { values.insert(it.key(), spin->value()); } - else if (const auto* spin = qobject_cast(it.value())) + else if (const auto* spin = qobject_cast(editor)) { values.insert(it.key(), spin->value()); } - else if (const auto* combo = qobject_cast(it.value())) + else if (const auto* combo = qobject_cast(editor)) { values.insert(it.key(), combo->currentData()); } - else if (const auto* edit = qobject_cast(it.value())) + else if (const auto* edit = qobject_cast(editor)) { values.insert(it.key(), edit->text().trimmed()); } diff --git a/src/tools/daq/SignalMonitorWidget.h b/src/tools/daq/SignalMonitorWidget.h index 5d2bb21..f8a67d5 100644 --- a/src/tools/daq/SignalMonitorWidget.h +++ b/src/tools/daq/SignalMonitorWidget.h @@ -1,15 +1,20 @@ #pragma once +#include "OscilloscopeView.h" #include "scopeone/SignalSource.h" #include "tools/ToolContext.h" #include +#include #include +#include + class QComboBox; class QCheckBox; class QDoubleSpinBox; class QFormLayout; +class QGroupBox; class QLabel; class QPushButton; class QSpinBox; @@ -21,8 +26,8 @@ namespace scopeone::core namespace scopeone::ui { - class SignalTracePlot; - + // The signal sources shown as an oscilloscope: each of the four channels shows one source, + // and Start runs the sources of the channels that are on class SignalMonitorWidget final : public QWidget { Q_OBJECT @@ -31,27 +36,65 @@ namespace scopeone::ui explicit SignalMonitorWidget(scopeone::core::ScopeOneCore* core, scopeone::ui::ScopeOneToolContext& context, QWidget* parent = nullptr); + ~SignalMonitorWidget() override; private: + struct ChannelControls + { + QGroupBox* box{nullptr}; + QComboBox* source{nullptr}; + QComboBox* scale{nullptr}; + QDoubleSpinBox* offset{nullptr}; + QComboBox* coupling{nullptr}; + QFormLayout* parameterForm{nullptr}; + QHash editors; + QString preferredSource; + }; + + QWidget* createChannelBox(int index); void refreshSources(); - void rebuildSourceParameters(); + void rebuildParameters(int index); + void updateUnits(); + void applyView(); void startAcquisition(); + void stopAcquisition(); + void autoScale(); void handleTimeSeries(const scopeone::core::TimeSeriesChunk& chunk); void handleStateChanged(const QString& sourceId, scopeone::core::SignalSourceState state, const QString& message); - void setSourceControlsEnabled(bool enabled); - scopeone::core::SignalSourceDescriptor currentDescriptor() const; - QVariantMap sourceSettings() const; + void updateControls(); + void updateStatus(); + void loadSettings(); + void saveSettings() const; + scopeone::core::SignalSourceDescriptor descriptor(const QString& sourceId) const; + QString channelSource(int index) const; + QVariantMap sourceSettings(int index) const; scopeone::core::ScopeOneCore* m_core{nullptr}; scopeone::ui::ScopeOneToolContext& m_context; - QComboBox* m_sourceCombo{nullptr}; - QFormLayout* m_sourceForm{nullptr}; - QHash m_parameterEditors; + OscilloscopeView* m_view{nullptr}; + std::array m_channels; + QList m_sources; + QDoubleSpinBox* m_sampleIntervalSpin{nullptr}; - QDoubleSpinBox* m_windowDurationSpin{nullptr}; - QCheckBox* m_scanImageCheck{nullptr}; + QPushButton* m_startButton{nullptr}; + QPushButton* m_stopButton{nullptr}; + QLabel* m_statusLabel{nullptr}; + + QComboBox* m_timebaseCombo{nullptr}; + QDoubleSpinBox* m_triggerPositionSpin{nullptr}; + QComboBox* m_triggerModeCombo{nullptr}; + QComboBox* m_triggerChannelCombo{nullptr}; + QComboBox* m_triggerSlopeCombo{nullptr}; + QDoubleSpinBox* m_triggerLevelSpin{nullptr}; + QPushButton* m_runButton{nullptr}; + QPushButton* m_singleButton{nullptr}; + QPushButton* m_autoScaleButton{nullptr}; + QCheckBox* m_cursorsCheck{nullptr}; + QComboBox* m_cursorChannelCombo{nullptr}; + + QGroupBox* m_scanGroup{nullptr}; QSpinBox* m_scanWidthSpin{nullptr}; QSpinBox* m_scanHeightSpin{nullptr}; QSpinBox* m_scanGainSpin{nullptr}; @@ -61,12 +104,10 @@ namespace scopeone::ui QComboBox* m_frameEndMarkerCombo{nullptr}; QCheckBox* m_serpentineCheck{nullptr}; QCheckBox* m_mirrorHorizontalCheck{nullptr}; - QPushButton* m_startButton{nullptr}; - QPushButton* m_stopButton{nullptr}; - QLabel* m_valueLabel{nullptr}; - QLabel* m_statusLabel{nullptr}; - SignalTracePlot* m_plot{nullptr}; - QString m_activeSourceId; + + QStringList m_activeSources; + QHash m_sourceMessages; + QString m_scanSourceId; bool m_scanLayerShown{false}; }; } From b3e83ab1975aab5188e92d55adec677204d80312 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 09:29:58 +0100 Subject: [PATCH 04/49] Bound the oscilloscope work per refresh with buckets and cheaper drawing --- src/tools/daq/OscilloscopeView.cpp | 314 +++++++++++++++++++++++------ src/tools/daq/OscilloscopeView.h | 41 +++- 2 files changed, 288 insertions(+), 67 deletions(-) diff --git a/src/tools/daq/OscilloscopeView.cpp b/src/tools/daq/OscilloscopeView.cpp index 3653235..f3eab16 100644 --- a/src/tools/daq/OscilloscopeView.cpp +++ b/src/tools/daq/OscilloscopeView.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -23,10 +22,17 @@ namespace scopeone::ui const QColor kStoppedColor(235, 80, 70); const QColor kRunningColor(95, 227, 95); - // About two thousand screens of a fast timebase or a few minutes of a slow one per channel - constexpr qsizetype kMaximumSamples = 4'000'000; + // The raw samples kept per channel, a second at 1 MHz + constexpr qsizetype kMaximumRawSamples = 1 << 20; + // A screen is cut into this many buckets, about one per pixel column of a wide screen + constexpr int kBucketsPerScreen = 2048; + // Above this many samples on the screen a channel is drawn and measured from its buckets + constexpr qsizetype kRawFrameSamples = 16384; + // The newest raw samples the frequency is counted on, so it stays exact on a long timebase + constexpr qsizetype kFrequencySamples = 65536; constexpr int kRefreshMilliseconds = 33; constexpr qint64 kAutoTriggerMilliseconds = 200; + constexpr qint64 kFrequencyMilliseconds = 250; } bool OscilloscopeView::Trace::empty() const @@ -49,20 +55,58 @@ namespace scopeone::ui return std::lower_bound(times.cbegin() + begin, times.cend(), time) - times.cbegin(); } - void OscilloscopeView::Trace::append(const scopeone::core::TimeSeriesChunk& chunk) + double OscilloscopeView::Trace::earliest(double bucketSeconds) const + { + double time = empty() ? std::numeric_limits::infinity() : first(); + if (!buckets.empty()) + { + time = std::min(time, static_cast(buckets.front().index) * bucketSeconds); + } + return time; + } + + void OscilloscopeView::Trace::append(const scopeone::core::TimeSeriesChunk& chunk, double bucketSeconds) { const qsizetype count = chunk.values.size(); times.reserve(times.size() + count); values.reserve(values.size() + count); for (qsizetype index = 0; index < count; ++index) { - times.append(chunk.startTimeSeconds + static_cast(index) * chunk.sampleIntervalSeconds); - values.append(chunk.values[index]); + const double time = chunk.startTimeSeconds + static_cast(index) * chunk.sampleIntervalSeconds; + const double value = chunk.values[index]; + times.append(time); + values.append(value); + addToBucket(time, value, bucketSeconds); } } - void OscilloscopeView::Trace::trim(double cutoff, qsizetype maximumSamples) + // Samples arrive in time order, so a sample never belongs to a bucket before the last one + void OscilloscopeView::Trace::addToBucket(double time, double value, double bucketSeconds) { + const auto index = static_cast(std::floor(time / bucketSeconds)); + if (buckets.empty() || index > buckets.back().index) + { + buckets.push_back({index, value, value, value, value * value, 1}); + return; + } + Bucket& bucket = buckets.back(); + bucket.minimum = std::min(bucket.minimum, value); + bucket.maximum = std::max(bucket.maximum, value); + bucket.sum += value; + bucket.squares += value * value; + ++bucket.count; + } + + void OscilloscopeView::Trace::trim(double cutoff, qsizetype maximumSamples, qint64 retainedBuckets) + { + if (!buckets.empty()) + { + const qint64 oldest = buckets.back().index - retainedBuckets; + while (buckets.front().index < oldest) + { + buckets.pop_front(); + } + } if (empty()) { return; @@ -76,10 +120,22 @@ namespace scopeone::ui } } + // A new timebase cuts the buckets anew from the raw samples, so the part of the history that + // only the old buckets held is gone + void OscilloscopeView::Trace::rebuildBuckets(double bucketSeconds) + { + buckets.clear(); + for (qsizetype index = begin; index < times.size(); ++index) + { + addToBucket(times[index], values[index], bucketSeconds); + } + } + void OscilloscopeView::Trace::clear() { times.clear(); values.clear(); + buckets.clear(); begin = 0; } @@ -92,6 +148,8 @@ namespace scopeone::ui connect(timer, &QTimer::timeout, this, &OscilloscopeView::refresh); timer->start(kRefreshMilliseconds); m_sinceTrigger.start(); + m_sinceFrequency.start(); + m_frequencies.fill(std::numeric_limits::quiet_NaN()); } void OscilloscopeView::setChannel(int index, const ScopeChannel& channel) @@ -100,8 +158,10 @@ namespace scopeone::ui { m_traces[index].clear(); m_frame.times[index].clear(); - m_frame.values[index].clear(); + m_frame.minima[index].clear(); + m_frame.maxima[index].clear(); m_frame.measurements[index] = {}; + m_frequencies[index] = std::numeric_limits::quiet_NaN(); } m_channels[index] = channel; m_dirty = true; @@ -115,6 +175,10 @@ namespace scopeone::ui return; } m_timebase = secondsPerDivision; + for (Trace& trace : m_traces) + { + trace.rebuildBuckets(bucketSeconds()); + } m_dirty = true; update(); } @@ -205,8 +269,8 @@ namespace scopeone::ui resetTriggerSearch(); } } - trace.append(chunk); - trace.trim(trace.last() - retained, kMaximumSamples); + trace.append(chunk, bucketSeconds()); + trace.trim(trace.last() - retained, kMaximumRawSamples, 2 * kBucketsPerScreen + 1); m_dirty = true; } } @@ -218,6 +282,7 @@ namespace scopeone::ui trace.clear(); } m_frame = {}; + m_frequencies.fill(std::numeric_limits::quiet_NaN()); m_singleArmed = false; m_state = m_mode == ScopeTriggerMode::Roll ? DisplayState::Roll : DisplayState::Ready; resetTriggerSearch(); @@ -268,6 +333,11 @@ namespace scopeone::ui return kHorizontalDivisions * m_timebase; } + double OscilloscopeView::bucketSeconds() const + { + return screenSeconds() / kBucketsPerScreen; + } + // The newest time every shown channel has reached, so a frame ends where all of them have data double OscilloscopeView::newestTime() const { @@ -291,7 +361,8 @@ namespace scopeone::ui void OscilloscopeView::refresh() { - if (!m_dirty || !m_running) + // A hidden or minimized screen waits; the samples keep arriving and it catches up when shown + if (!m_dirty || !m_running || !isVisible() || window()->isMinimized()) { return; } @@ -334,7 +405,9 @@ namespace scopeone::ui const double limit = newest - after; const double hysteresis = 0.1 * m_channels[m_triggerChannel].scale; const bool rising = m_slope == ScopeTriggerSlope::Rising; - qsizetype index = trace.indexAtOrAfter(std::max(m_scanTime, trace.first() + before)); + // The time before the edge may be held only by the buckets + qsizetype index = trace.indexAtOrAfter( + std::max(m_scanTime, trace.earliest(bucketSeconds()) + before)); if (index < trace.times.size() && trace.times[index] == m_scanTime) { ++index; @@ -387,81 +460,179 @@ namespace scopeone::ui } } + // Takes the screen from the raw samples when they reach back over it and are few enough to + // draw one by one, and from the buckets otherwise, so a frame holds at most a few thousand + // points per channel whatever the sample rate and the timebase void OscilloscopeView::buildFrame(double start, double end, double triggerTime) { m_frame.valid = true; m_frame.start = start; m_frame.end = end; m_frame.triggerTime = triggerTime; + const double width = bucketSeconds(); + const bool updateFrequency = m_singleArmed || m_sinceFrequency.elapsed() >= kFrequencyMilliseconds; + if (updateFrequency) + { + m_sinceFrequency.restart(); + } for (int channel = 0; channel < kChannelCount; ++channel) { QVector& times = m_frame.times[channel]; - QVector& values = m_frame.values[channel]; + QVector& minima = m_frame.minima[channel]; + QVector& maxima = m_frame.maxima[channel]; Measurement& measurement = m_frame.measurements[channel]; times.clear(); - values.clear(); + minima.clear(); + maxima.clear(); measurement = {}; const Trace& trace = m_traces[channel]; - if (!m_channels[channel].enabled || trace.empty()) - { - continue; - } - const qsizetype first = trace.indexAtOrAfter(start); - const qsizetype last = std::upper_bound(trace.times.cbegin() + first, trace.times.cend(), end) - - trace.times.cbegin(); - if (first >= last) + if (!m_channels[channel].enabled || (trace.empty() && trace.buckets.empty())) { continue; } - times = trace.times.mid(first, last - first); - values = trace.values.mid(first, last - first); - double minimum = values.constFirst(); - double maximum = minimum; + double minimum = std::numeric_limits::infinity(); + double maximum = -std::numeric_limits::infinity(); double sum = 0.0; double squares = 0.0; - for (const double value : values) + double count = 0.0; + qsizetype first = 0; + qsizetype last = 0; + if (!trace.empty()) { - minimum = std::min(minimum, value); - maximum = std::max(maximum, value); - sum += value; - squares += value * value; + first = trace.indexAtOrAfter(start); + last = std::upper_bound(trace.times.cbegin() + first, trace.times.cend(), end) + - trace.times.cbegin(); + } + const bool raw = !trace.empty() + && trace.first() <= std::max(start, trace.earliest(width)) + && last - first <= kRawFrameSamples; + m_frame.bucketed[channel] = !raw; + if (raw) + { + times = trace.times.mid(first, last - first); + minima = trace.values.mid(first, last - first); + maxima = minima; + for (const double value : minima) + { + minimum = std::min(minimum, value); + maximum = std::max(maximum, value); + sum += value; + squares += value * value; + } + count = static_cast(minima.size()); + } + else + { + const auto firstIndex = static_cast(std::floor(start / width)); + const auto lastIndex = static_cast(std::floor(end / width)); + auto bucket = std::lower_bound(trace.buckets.cbegin(), trace.buckets.cend(), firstIndex, + [](const Bucket& item, qint64 index) { return item.index < index; }); + for (; bucket != trace.buckets.cend() && bucket->index <= lastIndex; ++bucket) + { + times.append((static_cast(bucket->index) + 0.5) * width); + minima.append(bucket->minimum); + maxima.append(bucket->maximum); + minimum = std::min(minimum, bucket->minimum); + maximum = std::max(maximum, bucket->maximum); + sum += bucket->sum; + squares += bucket->squares; + count += static_cast(bucket->count); + } + } + if (count <= 0.0) + { + continue; } - const double count = static_cast(values.size()); measurement.valid = true; measurement.minimum = minimum; measurement.maximum = maximum; measurement.mean = sum / count; measurement.rms = std::sqrt(squares / count); + // The frequency is the one costly measurement and a number read by eye, so it follows a + // few times a second; a held screen always gets its own + measurement.frequency = updateFrequency + ? frequencyOf(channel, start, end, measurement) + : m_frequencies[channel]; + m_frequencies[channel] = measurement.frequency; + } + } + + // Counts rising crossings of the mean, re-armed a tenth of the peak to peak below it. The + // newest raw samples on the screen give the exact frequency of a fast signal; a signal too + // slow for them to hold three crossings is counted on the means of the buckets. + double OscilloscopeView::frequencyOf(int channel, + double start, + double end, + const Measurement& measurement) const + { + const double hysteresis = 0.1 * (measurement.maximum - measurement.minimum); + if (hysteresis <= 0.0) + { + return std::numeric_limits::quiet_NaN(); + } + struct Counter + { + double mean; + double hysteresis; + bool armed{false}; + int crossings{0}; + double first{0.0}; + double last{0.0}; - // Rising crossings of the mean, re-armed a tenth of the peak to peak below it - const double hysteresis = 0.1 * (maximum - minimum); - bool armed = false; - int crossings = 0; - double firstCrossing = 0.0; - double lastCrossing = 0.0; - for (qsizetype index = 0; index < values.size() && hysteresis > 0.0; ++index) + void add(double time, double value) { - if (values[index] < measurement.mean - hysteresis) + if (value < mean - hysteresis) { armed = true; } - else if (armed && values[index] >= measurement.mean) + else if (armed && value >= mean) { armed = false; if (crossings == 0) { - firstCrossing = times[index]; + first = time; } - lastCrossing = times[index]; + last = time; ++crossings; } } - if (crossings >= 2 && lastCrossing > firstCrossing) + + double frequency() const + { + return last > first ? (crossings - 1) / (last - first) : std::numeric_limits::quiet_NaN(); + } + }; + + const Trace& trace = m_traces[channel]; + if (!trace.empty()) + { + const qsizetype last = std::upper_bound(trace.times.cbegin() + trace.begin, trace.times.cend(), end) + - trace.times.cbegin(); + const qsizetype first = std::max(trace.indexAtOrAfter(start), last - kFrequencySamples); + Counter counter{measurement.mean, hysteresis}; + for (qsizetype index = first; index < last; ++index) + { + counter.add(trace.times[index], trace.values[index]); + } + if (counter.crossings >= 3 || !m_frame.bucketed[channel]) { - measurement.frequency = (crossings - 1) / (lastCrossing - firstCrossing); + return counter.crossings >= 2 ? counter.frequency() : std::numeric_limits::quiet_NaN(); } } + + const double width = bucketSeconds(); + const auto firstIndex = static_cast(std::floor(start / width)); + const auto lastIndex = static_cast(std::floor(end / width)); + auto bucket = std::lower_bound(trace.buckets.cbegin(), trace.buckets.cend(), firstIndex, + [](const Bucket& item, qint64 index) { return item.index < index; }); + Counter counter{measurement.mean, hysteresis}; + for (; bucket != trace.buckets.cend() && bucket->index <= lastIndex; ++bucket) + { + counter.add((static_cast(bucket->index) + 0.5) * width, + bucket->sum / static_cast(bucket->count)); + } + return counter.crossings >= 2 ? counter.frequency() : std::numeric_limits::quiet_NaN(); } void OscilloscopeView::resetTriggerSearch() @@ -484,7 +655,6 @@ namespace scopeone::ui void OscilloscopeView::paintEvent(QPaintEvent*) { QPainter painter(this); - painter.fillRect(rect(), kBackground); painter.setFont(font()); const QFontMetricsF metrics(font()); const double lineHeight = metrics.height() + 2.0; @@ -497,10 +667,23 @@ namespace scopeone::ui m_screen = QRectF(rect()).adjusted(32.0, 18.0, -32.0, -(readoutLines * lineHeight + 12.0)); if (m_screen.width() < 40.0 || m_screen.height() < 40.0) { + painter.fillRect(rect(), kBackground); return; } - drawGrid(painter); + // The background and the grid change only with the size, and the dotted grid is slow to + // draw, so they are drawn once into a pixmap + const qreal ratio = devicePixelRatioF(); + if (m_gridCache.size() != size() * ratio || m_gridScreen != m_screen) + { + m_gridCache = QPixmap(size() * ratio); + m_gridCache.setDevicePixelRatio(ratio); + m_gridCache.fill(kBackground); + QPainter gridPainter(&m_gridCache); + drawGrid(gridPainter); + m_gridScreen = m_screen; + } + painter.drawPixmap(0, 0, m_gridCache); drawTraces(painter); drawMarkers(painter); if (m_cursorsVisible) @@ -588,8 +771,9 @@ namespace scopeone::ui for (int channel = kChannelCount - 1; channel >= 0; --channel) { const QVector& times = m_frame.times[channel]; - const QVector& values = m_frame.values[channel]; - if (!m_channels[channel].enabled || values.isEmpty()) + const QVector& lows = m_frame.minima[channel]; + const QVector& highs = m_frame.maxima[channel]; + if (!m_channels[channel].enabled || times.isEmpty()) { continue; } @@ -601,31 +785,36 @@ namespace scopeone::ui { return centreY - divisionsOf(channel, value) * divisionHeight; }; - painter.setPen(QPen(channelColor(channel), 1.2)); - if (values.size() <= 2 * columns) + if (!m_frame.bucketed[channel] && times.size() <= 2 * columns) { - painter.setRenderHint(QPainter::Antialiasing, true); - QPainterPath path; - path.moveTo(mapX(times.constFirst()), mapY(values.constFirst())); - for (qsizetype index = 1; index < values.size(); ++index) + // Smoothing pays only while the samples are farther apart than the pixels + const bool smooth = times.size() <= columns; + painter.setRenderHint(QPainter::Antialiasing, smooth); + painter.setPen(QPen(channelColor(channel), smooth ? 1.2 : 0.0)); + QVector points(times.size()); + for (qsizetype index = 0; index < times.size(); ++index) { - path.lineTo(mapX(times[index]), mapY(values[index])); + points[index] = QPointF(mapX(times[index]), mapY(lows[index])); } - painter.drawPath(path); + painter.drawPolyline(points.constData(), static_cast(points.size())); painter.setRenderHint(QPainter::Antialiasing, false); continue; } + // A cosmetic pen of one pixel is the fastest the raster engine draws + painter.setPen(QPen(channelColor(channel), 0.0)); - // Keep the extremes of each pixel column when samples outnumber the pixels + // Keep the extremes of each pixel column when samples or buckets outnumber the pixels QVector minima(columns, std::numeric_limits::infinity()); QVector maxima(columns, -std::numeric_limits::infinity()); - for (qsizetype index = 0; index < values.size(); ++index) + for (qsizetype index = 0; index < times.size(); ++index) { const int column = std::clamp( static_cast((times[index] - m_frame.start) / span * columns), 0, columns - 1); - minima[column] = std::min(minima[column], values[index]); - maxima[column] = std::max(maxima[column], values[index]); + minima[column] = std::min(minima[column], lows[index]); + maxima[column] = std::max(maxima[column], highs[index]); } + QVector lines; + lines.reserve(columns); double previousLow = std::numeric_limits::quiet_NaN(); double previousHigh = previousLow; for (int column = 0; column < columns; ++column) @@ -644,10 +833,11 @@ namespace scopeone::ui low = std::max(low, previousHigh); high = std::min(high, previousLow); } - painter.drawLine(QPointF(x, low), QPointF(x, high)); + lines.append(QLineF(x, low, x, high)); previousLow = mapY(minima[column]); previousHigh = mapY(maxima[column]); } + painter.drawLines(lines); } painter.restore(); } diff --git a/src/tools/daq/OscilloscopeView.h b/src/tools/daq/OscilloscopeView.h index e83f73e..88d5a08 100644 --- a/src/tools/daq/OscilloscopeView.h +++ b/src/tools/daq/OscilloscopeView.h @@ -4,11 +4,13 @@ #include #include +#include #include #include #include #include +#include #include namespace scopeone::ui @@ -86,20 +88,39 @@ namespace scopeone::ui Ready }; - // The received samples of one channel. Old samples are dropped by moving the start, and - // the vectors are compacted only now and then, so trimming does not copy on every chunk. + // The extremes and sums of the samples in one slice of time, a fixed fraction of the screen + struct Bucket + { + qint64 index{0}; + double minimum{0.0}; + double maximum{0.0}; + double sum{0.0}; + double squares{0.0}; + qint64 count{0}; + }; + + // The received samples of one channel, kept twice. The raw samples cover only the last + // stretch, for the trigger, for exact frequencies and for screens with few samples. The + // buckets cover two screens whatever the sample rate, so a long timebase on a fast source + // draws and measures a few thousand buckets instead of millions of samples. + // Old raw samples are dropped by moving the start, and the vectors are compacted only now + // and then, so trimming does not copy on every chunk. struct Trace { QVector times; QVector values; qsizetype begin{0}; + std::deque buckets; bool empty() const; double first() const; double last() const; + double earliest(double bucketSeconds) const; qsizetype indexAtOrAfter(double time) const; - void append(const scopeone::core::TimeSeriesChunk& chunk); - void trim(double cutoff, qsizetype maximumSamples); + void append(const scopeone::core::TimeSeriesChunk& chunk, double bucketSeconds); + void addToBucket(double time, double value, double bucketSeconds); + void trim(double cutoff, qsizetype maximumSamples, qint64 retainedBuckets); + void rebuildBuckets(double bucketSeconds); void clear(); }; @@ -119,12 +140,18 @@ namespace scopeone::ui double start{0.0}; double end{0.0}; double triggerTime{std::numeric_limits::quiet_NaN()}; + // A raw channel has one sample per point, so its minima and maxima are the same; a + // bucketed one has one bucket per point std::array, kChannelCount> times; - std::array, kChannelCount> values; + std::array, kChannelCount> minima; + std::array, kChannelCount> maxima; + std::array bucketed{}; std::array measurements; }; double screenSeconds() const; + double bucketSeconds() const; + double frequencyOf(int channel, double start, double end, const Measurement& measurement) const; double newestTime() const; bool triggerShown() const; void refresh(); @@ -154,6 +181,10 @@ namespace scopeone::ui bool m_triggerArmed{false}; double m_scanTime{-std::numeric_limits::infinity()}; QElapsedTimer m_sinceTrigger; + QElapsedTimer m_sinceFrequency; + std::array m_frequencies{}; + QPixmap m_gridCache; + QRectF m_gridScreen; DisplayState m_state{DisplayState::Ready}; bool m_cursorsVisible{false}; int m_cursorChannel{0}; From 951533fa32d8258434861d2b0cabb4bb0e085775 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:00:09 +0100 Subject: [PATCH 05/49] Stop the signal sources when the Signal Monitor closes --- src/tools/daq/SignalMonitorWidget.cpp | 9 +++++++++ src/tools/daq/SignalMonitorWidget.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/tools/daq/SignalMonitorWidget.cpp b/src/tools/daq/SignalMonitorWidget.cpp index 9a60ff1..26616b0 100644 --- a/src/tools/daq/SignalMonitorWidget.cpp +++ b/src/tools/daq/SignalMonitorWidget.cpp @@ -4,6 +4,7 @@ #include "scopeone/ScopeOneCore.h" #include +#include #include #include #include @@ -313,6 +314,14 @@ namespace scopeone::ui saveSettings(); } + // The window is deleted on close, and the sources it started used to keep acquiring in their + // drivers with nothing left to show or stop them + void SignalMonitorWidget::closeEvent(QCloseEvent* event) + { + stopAcquisition(); + QWidget::closeEvent(event); + } + QWidget* SignalMonitorWidget::createChannelBox(int index) { ChannelControls& channel = m_channels[index]; diff --git a/src/tools/daq/SignalMonitorWidget.h b/src/tools/daq/SignalMonitorWidget.h index f8a67d5..0362b71 100644 --- a/src/tools/daq/SignalMonitorWidget.h +++ b/src/tools/daq/SignalMonitorWidget.h @@ -38,6 +38,9 @@ namespace scopeone::ui QWidget* parent = nullptr); ~SignalMonitorWidget() override; + protected: + void closeEvent(QCloseEvent* event) override; + private: struct ChannelControls { From 663c0a9f1f36766d37d81f67c744dd8263bf195e Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:07:54 +0100 Subject: [PATCH 06/49] Build the drivers as a project of their own --- .github/workflows/compile-check.yml | 2 +- .gitignore | 1 + drivers/CMakeLists.txt | 17 +++++++ drivers/README.md | 5 ++ plugins/CMakeLists.txt | 1 - plugins/README.md | 4 +- scripts/build-unix.sh | 7 +++ scripts/build.ps1 | 74 +++++++++++++++++++++++++++-- 8 files changed, 104 insertions(+), 7 deletions(-) diff --git a/.github/workflows/compile-check.yml b/.github/workflows/compile-check.yml index 072875f..affde88 100644 --- a/.github/workflows/compile-check.yml +++ b/.github/workflows/compile-check.yml @@ -158,7 +158,7 @@ jobs: if ($LASTEXITCODE -ne 0) { throw "Building MMCore failed." } if (-not (Test-Path "$mm\build\Release\x64\MMCore.lib")) { throw "MMCore.lib was not produced." } - - name: Compile Core, plugins and GUI + - name: Compile Core, plugins, drivers and GUI shell: pwsh run: | .\scripts\build.ps1 --configure ` diff --git a/.gitignore b/.gitignore index 3c7c50e..d7e556c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ !/ScopeOneInference/** /ScopeOneCore/install/ /ScopeOneCore/build/ +/drivers/build/ /ScopeOneCore/external/* !/ScopeOneCore/external/ScopeWriter/ !/ScopeOneCore/external/ScopeWriter/** diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index e4b436d..07bdd12 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -1,3 +1,20 @@ +cmake_minimum_required(VERSION 3.23) +project(ScopeOneDrivers LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_AUTOMOC ON) + +# Xcode's libc++ hides std::jthread and std::stop_token unless the experimental library is enabled +if (APPLE) + add_compile_options(-fexperimental-library) + add_link_options(-fexperimental-library) +endif () + +# A driver has the driver SDK and Qt Core and nothing else, so one that reaches for widgets, +# OpenCV or Core itself does not build +find_package(Qt6 REQUIRED COMPONENTS Core) +find_package(ScopeOneCore CONFIG REQUIRED) + add_subdirectory(ExampleCamera) add_subdirectory(ExamplePmt) add_subdirectory(NIDaqmx) diff --git a/drivers/README.md b/drivers/README.md index e07f50c..8f3d44b 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -50,6 +50,11 @@ scopeone_add_driver(MyCamera) Point `CMAKE_PREFIX_PATH` at `ScopeOneCore/install` and at the same Qt that ScopeOne uses (`vcpkg_installed/`). +The drivers in this folder are built the same way, as a project of their own that finds only Qt +Core and the installed ScopeOneCore. The build scripts build it into `drivers/build` and install +the drivers into `ScopeOneCore/install/bin/drivers`, from where the GUI build copies them +(`scripts\build.ps1 --target drivers` builds only them). + `driver.json` sits beside `CMakeLists.txt`: ```json diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 42df5c6..c66646f 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -21,7 +21,6 @@ else () unset(SCOPEONE_CUDA_NVCC_EXECUTABLE CACHE) endif () -add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../drivers drivers) if (SCOPEONE_ENABLE_INFERENCE) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../ScopeOneInference ScopeOneInference) endif () diff --git a/plugins/README.md b/plugins/README.md index 521f415..05ae72e 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -8,8 +8,8 @@ extension does not take ScopeOne down. | Driver | a `Driver` in C++ against `scopeone::DriverSDK` | `ScopeOne_DriverHost`, one process per device | `drivers/ExampleCamera`, `drivers/ExamplePmt` | | Python processing module | a decorated Python function | its own Python process | `ScopeOneCore/python/scopeone/examples/processing_offset.py` | -Drivers have their own folder and guide, [drivers/README.md](../drivers/README.md). This folder -builds the drivers together with the CUDA and ONNX processing modules in `ScopeOneCuda` and +Drivers have their own folder, CMake project and guide, [drivers/README.md](../drivers/README.md). +This folder builds the CUDA and ONNX processing modules in `ScopeOneCuda` and `ScopeOneInference`. Those two are built-in plugins, built and released with ScopeOne, because they link CUDA and ONNX Runtime. Their interface, `scopeone/ProcessingPlugin.h`, changes with ScopeOne and is not meant for code outside this repository, and ScopeOne loads native diff --git a/scripts/build-unix.sh b/scripts/build-unix.sh index ff09d96..8f605af 100755 --- a/scripts/build-unix.sh +++ b/scripts/build-unix.sh @@ -228,6 +228,13 @@ run cmake -S "$ROOT_DIR/plugins" -B "$ROOT_DIR/build/plugins" -DCMAKE_BUILD_TYPE run cmake --build "$ROOT_DIR/build/plugins" --parallel "$JOBS" run cmake --install "$ROOT_DIR/build/plugins" +step "Building and installing ScopeOne drivers" +run cmake -S "$ROOT_DIR/drivers" -B "$ROOT_DIR/drivers/build" -DCMAKE_BUILD_TYPE="$BUILD_TYPE" \ + -DScopeOneCore_ROOT="$CORE_DIR/install" -DCMAKE_PREFIX_PATH="$CORE_DIR/install" \ + -DCMAKE_INSTALL_PREFIX="$CORE_DIR/install/bin" "${VCPKG_ARGS[@]}" +run cmake --build "$ROOT_DIR/drivers/build" --parallel "$JOBS" +run cmake --install "$ROOT_DIR/drivers/build" + step "Building ScopeOne GUI" run cmake -S "$ROOT_DIR" -B "$ROOT_DIR/build" -DCMAKE_BUILD_TYPE="$BUILD_TYPE" "${VCPKG_ARGS[@]}" run cmake --build "$ROOT_DIR/build" --parallel "$JOBS" diff --git a/scripts/build.ps1 b/scripts/build.ps1 index c203016..49aa8a1 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -81,10 +81,10 @@ for ($i = 0; $i -lt $args.Count; $i++) { } } -if ($target -notin @("all", "core", "gui", "plugins", "scopewriter")) { - throw "Invalid target '$target'. Expected one of: all, core, gui, plugins, scopewriter." +if ($target -notin @("all", "core", "gui", "plugins", "drivers", "scopewriter")) { + throw "Invalid target '$target'. Expected one of: all, core, gui, plugins, drivers, scopewriter." } -if ($package -and $target -in @("core", "plugins", "scopewriter")) { +if ($package -and $target -in @("core", "plugins", "drivers", "scopewriter")) { throw "--package requires --target gui or --target all." } if ($run -and $target -eq "scopewriter") { @@ -245,10 +245,14 @@ $writerConsumerBuildDir = Join-Path $writerBuildRoot "consumer" $pluginSourceDir = Join-Path $repoRoot "plugins" $pluginBuildDir = Join-Path $repoRoot "build\plugins" $pluginInstallDir = Join-Path $coreInstallDir "bin" +# Inside drivers rather than build, whose drivers folder is where the GUI build puts the drivers it runs +$driverSourceDir = Join-Path $repoRoot "drivers" +$driverBuildDir = Join-Path $driverSourceDir "build" $config = "Release" $coreCachePath = Join-Path $coreBuildDir "CMakeCache.txt" $guiCachePath = Join-Path $guiBuildDir "CMakeCache.txt" $pluginCachePath = Join-Path $pluginBuildDir "CMakeCache.txt" +$driverCachePath = Join-Path $driverBuildDir "CMakeCache.txt" $vcpkgConfigureArgs = @() $writerVcpkgArgs = @() @@ -301,6 +305,10 @@ if ($clean) { Write-Step "Removing plugin build directory" Remove-Item -LiteralPath $pluginBuildDir -Recurse -Force } + if ($target -in @("all", "drivers") -and (Test-Path $driverBuildDir)) { + Write-Step "Removing driver build directory" + Remove-Item -LiteralPath $driverBuildDir -Recurse -Force + } if ($target -in @("all", "core") -and (Test-Path $coreBuildDir)) { Write-Step "Removing ScopeOneCore build directory" Remove-Item -LiteralPath $coreBuildDir -Recurse -Force @@ -336,6 +344,17 @@ if ($pluginCachedInstallPrefix -and $needPluginConfigure = $true } +$driverBuildFilesExist = (Test-Path (Join-Path $driverBuildDir "ALL_BUILD.vcxproj")) -or + (Test-Path (Join-Path $driverBuildDir "build.ninja")) -or + (Test-Path (Join-Path $driverBuildDir "Makefile")) +$needDriverConfigure = $configure -or -not (Test-Path $driverCachePath) -or -not $driverBuildFilesExist +$driverCachedInstallPrefix = Normalize-CMakePath ( + Get-CMakeCacheValue -CachePath $driverCachePath -Key "CMAKE_INSTALL_PREFIX") +if ($driverCachedInstallPrefix -and + $driverCachedInstallPrefix -ne (Normalize-CMakePath $pluginInstallDir)) { + $needDriverConfigure = $true +} + $installPrefixOverride = Find-ConfigureOverride -Options $coreConfigureOption -Prefix "-DCMAKE_INSTALL_PREFIX=" if ($target -eq "scopewriter") { @@ -517,6 +536,52 @@ if ($target -in @("all", "plugins")) { } } +if ($target -in @("all", "drivers")) { + $coreConfigFile = Join-Path $coreInstallDir "lib\cmake\ScopeOneCore\ScopeOneCoreConfig.cmake" + if (-not (Test-Path $coreConfigFile)) { + throw "ScopeOneCore is not installed at $coreInstallDir. Build the core first." + } + + if ($needDriverConfigure) { + Invoke-Step ` + -Label "Configuring ScopeOne drivers" ` + -FilePath $cmake ` + -Arguments (@( + "-S", $driverSourceDir, + "-B", $driverBuildDir, + "-DScopeOneCore_ROOT=$coreInstallDir", + "-DCMAKE_PREFIX_PATH=$coreInstallDir", + "-DCMAKE_INSTALL_PREFIX=$pluginInstallDir" + ) + $vcpkgConfigureArgs) ` + -WorkingDirectory $repoRoot + } + + Invoke-Step ` + -Label "Building ScopeOne drivers ($config)" ` + -FilePath $cmake ` + -Arguments @( + "--build", $driverBuildDir, + "--config", $config, + "--parallel" + ) ` + -WorkingDirectory $repoRoot + + Invoke-Step ` + -Label "Installing ScopeOne drivers" ` + -FilePath $cmake ` + -Arguments @( + "--install", $driverBuildDir, + "--config", $config + ) ` + -WorkingDirectory $repoRoot + + # The GUI decides at configure time whether there is a drivers folder to copy, so it is + # configured again once the drivers are installed + if ($target -eq "all") { + $needGuiConfigure = $true + } +} + if ($target -in @("all", "gui")) { if ($env:OS -eq "Windows_NT") { $guiBuildPrefix = [System.IO.Path]::GetFullPath($guiBuildDir).TrimEnd( @@ -591,6 +656,9 @@ else { if ($target -in @("all", "plugins")) { Write-Host "Plugins: $pluginBuildDir" } + if ($target -in @("all", "drivers")) { + Write-Host "Drivers: $driverBuildDir" + } } if ($target -ne "scopewriter") { if (Test-Path $guiExe) { From 832bfd3c5436bb8766fa03a9dae1577e57fbfe79 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:21:33 +0100 Subject: [PATCH 07/49] Run the Signal Monitor like a scope with a sample rate that follows the timebase --- src/tools/daq/OscilloscopeView.cpp | 70 ++++-- src/tools/daq/OscilloscopeView.h | 7 + src/tools/daq/SignalMonitorWidget.cpp | 294 ++++++++++++++++++++------ src/tools/daq/SignalMonitorWidget.h | 19 +- 4 files changed, 307 insertions(+), 83 deletions(-) diff --git a/src/tools/daq/OscilloscopeView.cpp b/src/tools/daq/OscilloscopeView.cpp index f3eab16..d2656ed 100644 --- a/src/tools/daq/OscilloscopeView.cpp +++ b/src/tools/daq/OscilloscopeView.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,8 @@ namespace scopeone::ui constexpr int kRefreshMilliseconds = 33; constexpr qint64 kAutoTriggerMilliseconds = 200; constexpr qint64 kFrequencyMilliseconds = 250; + // A triggered screen whose part after the edge takes longer than this is drawn as it fills + constexpr double kProgressiveSeconds = 0.1; } bool OscilloscopeView::Trace::empty() const @@ -67,9 +70,9 @@ namespace scopeone::ui void OscilloscopeView::Trace::append(const scopeone::core::TimeSeriesChunk& chunk, double bucketSeconds) { + // No reserve here: reserving the exact new size on every chunk made each chunk reallocate + // and copy the whole history, which froze the screen on a long timebase const qsizetype count = chunk.values.size(); - times.reserve(times.size() + count); - values.reserve(values.size() + count); for (qsizetype index = 0; index < count; ++index) { const double time = chunk.startTimeSeconds + static_cast(index) * chunk.sampleIntervalSeconds; @@ -192,6 +195,7 @@ namespace scopeone::ui if (mode != m_mode || channel != m_triggerChannel || slope != m_slope) { m_triggerArmed = false; + m_pendingTrigger = std::numeric_limits::quiet_NaN(); } m_mode = mode; m_triggerChannel = channel; @@ -230,6 +234,11 @@ namespace scopeone::ui return m_running; } + bool OscilloscopeView::isSingleArmed() const + { + return m_singleArmed; + } + void OscilloscopeView::armSingle() { // Only a trigger after the button counts, as on a scope @@ -384,9 +393,10 @@ namespace scopeone::ui } // Scans the trigger channel once over the samples that arrived, from where the last scan - // ended, and shows the latest edge whose whole screen has been received. The trigger re-arms - // only after the signal moves a tenth of a division past the level, so noise on a slow edge - // does not trigger twice. + // ended. The trigger re-arms only after the signal moves a tenth of a division past the level, + // so noise on a slow edge does not trigger twice. An edge whose screen has been received is + // shown; an edge still waiting for the part after it is kept and, on a slow timebase, drawn + // as the samples come in, as a scope does, and edges are not looked for until it is complete. void OscilloscopeView::processTrigger() { const double screen = screenSeconds(); @@ -398,11 +408,15 @@ namespace scopeone::ui return; } - double crossing = std::numeric_limits::quiet_NaN(); + double complete = std::numeric_limits::quiet_NaN(); + if (std::isfinite(m_pendingTrigger) && m_pendingTrigger + after <= newest) + { + complete = m_pendingTrigger; + m_pendingTrigger = std::numeric_limits::quiet_NaN(); + } const Trace& trace = m_traces[m_triggerChannel]; if (m_channels[m_triggerChannel].enabled && !trace.empty()) { - const double limit = newest - after; const double hysteresis = 0.1 * m_channels[m_triggerChannel].scale; const bool rising = m_slope == ScopeTriggerSlope::Rising; // The time before the edge may be held only by the buckets @@ -412,7 +426,7 @@ namespace scopeone::ui { ++index; } - for (; index < trace.times.size() && trace.times[index] <= limit; ++index) + for (; index < trace.times.size() && trace.times[index] <= newest; ++index) { const double value = trace.values[index]; const bool beyond = rising ? value >= m_triggerLevel : value <= m_triggerLevel; @@ -427,31 +441,47 @@ namespace scopeone::ui const double previousValue = trace.values[index - 1]; const double previousTime = trace.times[index - 1]; const double fraction = (m_triggerLevel - previousValue) / (value - previousValue); - crossing = previousTime + std::clamp(fraction, 0.0, 1.0) + const double crossing = previousTime + std::clamp(fraction, 0.0, 1.0) * (trace.times[index] - previousTime); m_triggerArmed = false; + if (crossing + after <= newest) + { + complete = crossing; + } + else if (!std::isfinite(m_pendingTrigger)) + { + m_pendingTrigger = crossing; + } } m_scanTime = trace.times[index]; } } - if (std::isfinite(crossing)) + if (std::isfinite(complete)) { - buildFrame(crossing - before, crossing + after, crossing); + buildFrame(complete - before, complete + after, complete); m_state = DisplayState::Triggered; m_sinceTrigger.restart(); if (m_singleArmed) { + m_pendingTrigger = std::numeric_limits::quiet_NaN(); setRunning(false); } return; } - if (m_mode == ScopeTriggerMode::Auto && !m_singleArmed + if (std::isfinite(m_pendingTrigger) && after > kProgressiveSeconds) + { + buildFrame(m_pendingTrigger - before, m_pendingTrigger + after, m_pendingTrigger); + m_state = DisplayState::Triggered; + m_sinceTrigger.restart(); + return; + } + // Without an edge Auto runs free on every refresh, so a slow screen still moves smoothly + if (m_mode == ScopeTriggerMode::Auto && !m_singleArmed && !std::isfinite(m_pendingTrigger) && m_sinceTrigger.elapsed() > kAutoTriggerMilliseconds) { buildFrame(newest - screen, newest, std::numeric_limits::quiet_NaN()); m_state = DisplayState::Auto; - m_sinceTrigger.restart(); return; } if (m_state != DisplayState::Triggered || m_singleArmed) @@ -638,6 +668,7 @@ namespace scopeone::ui void OscilloscopeView::resetTriggerSearch() { m_triggerArmed = false; + m_pendingTrigger = std::numeric_limits::quiet_NaN(); m_scanTime = -std::numeric_limits::infinity(); } @@ -1061,4 +1092,17 @@ namespace scopeone::ui { m_dragCursor = -1; } + + // Wheel up zooms in to a shorter timebase; a touchpad's small steps add up to whole notches + void OscilloscopeView::wheelEvent(QWheelEvent* event) + { + m_wheelAngle += event->angleDelta().y(); + const int notches = m_wheelAngle / 120; + if (notches != 0) + { + m_wheelAngle -= notches * 120; + emit timebaseStepRequested(-notches); + } + event->accept(); + } } diff --git a/src/tools/daq/OscilloscopeView.h b/src/tools/daq/OscilloscopeView.h index 88d5a08..105a835 100644 --- a/src/tools/daq/OscilloscopeView.h +++ b/src/tools/daq/OscilloscopeView.h @@ -62,6 +62,7 @@ namespace scopeone::ui void setCursors(bool visible, int channel); void setRunning(bool running); bool isRunning() const; + bool isSingleArmed() const; void armSingle(); void appendChunk(const scopeone::core::TimeSeriesChunk& chunk); void clear(); @@ -72,12 +73,15 @@ namespace scopeone::ui signals: void runningChanged(bool running); + // The wheel over the screen turns the timebase, positive steps to a longer one + void timebaseStepRequested(int steps); protected: void paintEvent(QPaintEvent* event) override; void mousePressEvent(QMouseEvent* event) override; void mouseMoveEvent(QMouseEvent* event) override; void mouseReleaseEvent(QMouseEvent* event) override; + void wheelEvent(QWheelEvent* event) override; private: enum class DisplayState @@ -179,6 +183,8 @@ namespace scopeone::ui bool m_singleArmed{false}; bool m_dirty{false}; bool m_triggerArmed{false}; + // An edge whose screen is still being received + double m_pendingTrigger{std::numeric_limits::quiet_NaN()}; double m_scanTime{-std::numeric_limits::infinity()}; QElapsedTimer m_sinceTrigger; QElapsedTimer m_sinceFrequency; @@ -191,6 +197,7 @@ namespace scopeone::ui // Two time cursors as fractions of the screen width, then two level cursors in divisions std::array m_cursors{0.3, 0.7, 2.0, -2.0}; int m_dragCursor{-1}; + int m_wheelAngle{0}; QRectF m_screen; }; } diff --git a/src/tools/daq/SignalMonitorWidget.cpp b/src/tools/daq/SignalMonitorWidget.cpp index 26616b0..58c8397 100644 --- a/src/tools/daq/SignalMonitorWidget.cpp +++ b/src/tools/daq/SignalMonitorWidget.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -32,6 +33,10 @@ namespace scopeone::ui { namespace { + constexpr double kMaximumSampleRate = 1e6; + constexpr int kAutoSamplesPerScreen = 5000; + constexpr int kRestartDelayMilliseconds = 300; + QString parameterSettingKey(const QString& sourceId, const QString& parameterKey) { return QStringLiteral("signalSources/%1/%2").arg(sourceId, parameterKey); @@ -117,28 +122,25 @@ namespace scopeone::ui scroll->setWidget(panel); layout->addWidget(scroll); - auto* acquisitionBox = new QGroupBox(tr("Acquisition"), panel); - auto* acquisitionForm = new QFormLayout(acquisitionBox); - m_sampleIntervalSpin = new QDoubleSpinBox(acquisitionBox); - m_sampleIntervalSpin->setRange(0.001, 10000.0); - m_sampleIntervalSpin->setDecimals(3); - m_sampleIntervalSpin->setSuffix(tr(" ms")); - m_sampleIntervalSpin->setValue(1.0); - m_sampleIntervalSpin->setToolTip( - tr("The sample clock of a time series such as a DAQ input, and the bin of an event source")); - acquisitionForm->addRow(tr("Interval"), m_sampleIntervalSpin); - auto* acquisitionButtons = new QHBoxLayout(); - m_startButton = new QPushButton(tr("Start"), acquisitionBox); - m_startButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); - m_stopButton = new QPushButton(tr("Stop"), acquisitionBox); - m_stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop)); - acquisitionButtons->addWidget(m_startButton); - acquisitionButtons->addWidget(m_stopButton); - acquisitionForm->addRow(acquisitionButtons); - m_statusLabel = new QLabel(tr("No signal sources available"), acquisitionBox); + // The front panel keys of a scope: Run/Stop starts and stops the sources of the channels + // that are on, Single takes one triggered screen and stops + auto* controlBox = new QGroupBox(tr("Control"), panel); + auto* controlLayout = new QVBoxLayout(controlBox); + auto* controlButtons = new QHBoxLayout(); + m_runButton = new QPushButton(tr("Run"), controlBox); + m_runButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); + m_singleButton = new QPushButton(tr("Single"), controlBox); + m_singleButton->setToolTip(tr("Take the next triggered screen and stop")); + m_autoScaleButton = new QPushButton(tr("Auto Scale"), controlBox); + m_autoScaleButton->setToolTip(tr("Fit the scale and offset of each channel to the screen")); + controlButtons->addWidget(m_runButton); + controlButtons->addWidget(m_singleButton); + controlButtons->addWidget(m_autoScaleButton); + controlLayout->addLayout(controlButtons); + m_statusLabel = new QLabel(tr("No signal sources available"), controlBox); m_statusLabel->setWordWrap(true); - acquisitionForm->addRow(m_statusLabel); - panelLayout->addWidget(acquisitionBox); + controlLayout->addWidget(m_statusLabel); + panelLayout->addWidget(controlBox); auto* horizontalBox = new QGroupBox(tr("Horizontal"), panel); auto* horizontalForm = new QFormLayout(horizontalBox); @@ -149,6 +151,20 @@ namespace scopeone::ui QStringLiteral("%1/div").arg(OscilloscopeView::formatValue(step, QStringLiteral("s"))), step); } horizontalForm->addRow(tr("Timebase"), m_timebaseCombo); + m_sampleRateCombo = new QComboBox(horizontalBox); + m_sampleRateCombo->addItem(tr("Auto"), 0.0); + for (const double step : knobSteps(0, 6)) + { + if (step <= kMaximumSampleRate) + { + m_sampleRateCombo->addItem(OscilloscopeView::formatValue(step, QStringLiteral("S/s")), step); + } + } + m_sampleRateCombo->setToolTip( + tr("Auto follows the timebase with about %1 samples on the screen; a fixed rate is the " + "sample clock of a DAQ input and the counting bin of a photon counter") + .arg(kAutoSamplesPerScreen)); + horizontalForm->addRow(tr("Sample rate"), m_sampleRateCombo); m_triggerPositionSpin = new QDoubleSpinBox(horizontalBox); m_triggerPositionSpin->setRange(0.0, 100.0); m_triggerPositionSpin->setDecimals(0); @@ -176,14 +192,6 @@ namespace scopeone::ui triggerForm->addRow(tr("Slope"), m_triggerSlopeCombo); m_triggerLevelSpin = valueSpin(triggerBox); triggerForm->addRow(tr("Level"), m_triggerLevelSpin); - auto* displayButtons = new QHBoxLayout(); - m_runButton = new QPushButton(tr("Stop"), triggerBox); - m_runButton->setToolTip(tr("Hold or resume the screen; the sources keep acquiring")); - m_singleButton = new QPushButton(tr("Single"), triggerBox); - m_singleButton->setToolTip(tr("Show the next edge and hold the screen")); - displayButtons->addWidget(m_runButton); - displayButtons->addWidget(m_singleButton); - triggerForm->addRow(displayButtons); panelLayout->addWidget(triggerBox); for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) @@ -191,16 +199,13 @@ namespace scopeone::ui panelLayout->addWidget(createChannelBox(index)); } - auto* displayBox = new QGroupBox(tr("Display"), panel); + auto* displayBox = new QGroupBox(tr("Cursors"), panel); auto* displayForm = new QFormLayout(displayBox); - m_autoScaleButton = new QPushButton(tr("Auto Scale"), displayBox); - m_autoScaleButton->setToolTip(tr("Fit the scale and offset of each channel to the screen")); - displayForm->addRow(m_autoScaleButton); m_cursorsCheck = new QCheckBox(tr("Show cursors"), displayBox); m_cursorsCheck->setToolTip(tr("Drag the cursors on the screen")); - displayForm->addRow(tr("Cursors"), m_cursorsCheck); + displayForm->addRow(m_cursorsCheck); m_cursorChannelCombo = channelCombo(displayBox); - displayForm->addRow(tr("Cursor channel"), m_cursorChannelCombo); + displayForm->addRow(tr("Channel"), m_cursorChannelCombo); panelLayout->addWidget(displayBox); m_scanGroup = new QGroupBox(tr("Scan image from CH1"), panel); @@ -253,19 +258,60 @@ namespace scopeone::ui loadSettings(); - connect(m_startButton, &QPushButton::clicked, this, &SignalMonitorWidget::startAcquisition); - connect(m_stopButton, &QPushButton::clicked, this, &SignalMonitorWidget::stopAcquisition); - connect(m_autoScaleButton, &QPushButton::clicked, this, &SignalMonitorWidget::autoScale); + m_restartTimer = new QTimer(this); + m_restartTimer->setSingleShot(true); + m_restartTimer->setInterval(kRestartDelayMilliseconds); + connect(m_restartTimer, &QTimer::timeout, this, &SignalMonitorWidget::restartAcquisition); + connect(m_runButton, &QPushButton::clicked, this, [this]() { - m_view->setRunning(!m_view->isRunning()); + if (m_acquiring) + { + stop(); + } + else + { + run(); + } }); - connect(m_singleButton, &QPushButton::clicked, m_view, &OscilloscopeView::armSingle); + connect(m_singleButton, &QPushButton::clicked, this, &SignalMonitorWidget::single); + connect(m_autoScaleButton, &QPushButton::clicked, this, &SignalMonitorWidget::autoScale); + // A single screen that was taken stops the acquisition, as on a scope connect(m_view, &OscilloscopeView::runningChanged, this, [this](bool running) { - m_runButton->setText(running ? tr("Stop") : tr("Run")); + if (!running && m_acquiring) + { + stop(); + } + }); + connect(m_view, &OscilloscopeView::timebaseStepRequested, this, [this](int steps) + { + m_timebaseCombo->setCurrentIndex( + std::clamp(m_timebaseCombo->currentIndex() + steps, 0, m_timebaseCombo->count() - 1)); }); - connect(m_timebaseCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); + connect(m_timebaseCombo, &QComboBox::currentIndexChanged, this, [this]() + { + applyView(); + updateSampleRateText(); + scheduleRestart(); + }); + connect(m_sampleRateCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::scheduleRestart); + for (QWidget* widget : m_scanGroup->findChildren()) + { + if (auto* spin = qobject_cast(widget)) + { + connect(spin, &QSpinBox::valueChanged, this, &SignalMonitorWidget::scheduleRestart); + } + else if (auto* combo = qobject_cast(widget)) + { + connect(combo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::scheduleRestart); + } + else if (auto* check = qobject_cast(widget)) + { + connect(check, &QCheckBox::toggled, this, &SignalMonitorWidget::scheduleRestart); + } + } + connect(m_scanGroup, &QGroupBox::toggled, this, &SignalMonitorWidget::scheduleRestart); connect(m_triggerPositionSpin, &QDoubleSpinBox::valueChanged, this, &SignalMonitorWidget::applyView); connect(m_triggerModeCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); connect(m_triggerChannelCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::updateUnits); @@ -318,7 +364,7 @@ namespace scopeone::ui // drivers with nothing left to show or stop them void SignalMonitorWidget::closeEvent(QCloseEvent* event) { - stopAcquisition(); + stop(); QWidget::closeEvent(event); } @@ -355,12 +401,14 @@ namespace scopeone::ui { applyView(); updateControls(); + scheduleRestart(); }); connect(channel.source, &QComboBox::currentIndexChanged, this, [this, index]() { m_channels[index].preferredSource = channelSource(index); rebuildParameters(index); updateUnits(); + scheduleRestart(); }); connect(channel.scale, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); connect(channel.offset, &QDoubleSpinBox::valueChanged, this, &SignalMonitorWidget::applyView); @@ -480,6 +528,22 @@ namespace scopeone::ui } channel.parameterForm->addRow(parameter.name, row ? row : editor); channel.editors.insert(parameter.key, editor); + if (auto* spin = qobject_cast(editor)) + { + connect(spin, &QSpinBox::valueChanged, this, &SignalMonitorWidget::scheduleRestart); + } + else if (auto* spin = qobject_cast(editor)) + { + connect(spin, &QDoubleSpinBox::valueChanged, this, &SignalMonitorWidget::scheduleRestart); + } + else if (auto* combo = qobject_cast(editor)) + { + connect(combo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::scheduleRestart); + } + else if (auto* edit = qobject_cast(editor)) + { + connect(edit, &QLineEdit::editingFinished, this, &SignalMonitorWidget::scheduleRestart); + } } updateControls(); } @@ -528,7 +592,7 @@ namespace scopeone::ui m_view->setCursors(m_cursorsCheck->isChecked(), m_cursorChannelCombo->currentIndex()); } - void SignalMonitorWidget::startAcquisition() + bool SignalMonitorWidget::startAcquisition() { QStringList sources; QHash firstChannel; @@ -544,7 +608,7 @@ namespace scopeone::ui if (sources.isEmpty()) { m_statusLabel->setText(tr("Turn on a channel that shows a source")); - return; + return false; } saveSettings(); @@ -560,7 +624,7 @@ namespace scopeone::ui { scopeone::core::SignalAcquisitionConfig config; config.sourceId = sourceId; - config.sampleIntervalSeconds = m_sampleIntervalSpin->value() / 1000.0; + config.sampleIntervalSeconds = 1.0 / sampleRate(); config.scanImage.enabled = sourceId == m_scanSourceId; config.scanImage.width = m_scanWidthSpin->value(); config.scanImage.height = m_scanHeightSpin->value(); @@ -583,24 +647,126 @@ namespace scopeone::ui qWarning().noquote() << errorMessage; stopAcquisition(); m_statusLabel->setText(errorMessage); - return; + return false; } if (!m_activeSources.contains(sourceId)) { m_activeSources.append(sourceId); } } - updateControls(); + return true; } + // Stops what was started and what the channels show, since during a restart the states of + // the stopped run can still be on their way void SignalMonitorWidget::stopAcquisition() { - for (const QString& sourceId : QStringList(m_activeSources)) + QStringList sources = m_activeSources; + for (int index = 0; index < OscilloscopeView::kChannelCount; ++index) { - m_core->stopSignalTrace(sourceId); + const QString sourceId = channelSource(index); + if (!sourceId.isEmpty() && !sources.contains(sourceId)) + { + sources.append(sourceId); + } + } + for (const QString& sourceId : sources) + { + if (m_core->signalSourceState(sourceId) != scopeone::core::SignalSourceState::Idle + || m_activeSources.contains(sourceId)) + { + m_core->stopSignalTrace(sourceId); + } } } + void SignalMonitorWidget::run() + { + m_restartTimer->stop(); + m_acquiring = startAcquisition(); + m_view->setRunning(m_acquiring); + updateControls(); + } + + void SignalMonitorWidget::stop() + { + m_restartTimer->stop(); + m_acquiring = false; + stopAcquisition(); + // The screen keeps the last frame, as a stopped scope does + m_view->setRunning(false); + updateControls(); + } + + void SignalMonitorWidget::single() + { + if (!m_acquiring) + { + run(); + } + if (m_acquiring) + { + m_view->armSingle(); + } + } + + // A change that reaches the sources takes effect at once while running, the way a scope + // re-arms when a knob turns; the delay gathers the steps of a turned wheel into one restart + void SignalMonitorWidget::scheduleRestart() + { + if (m_acquiring) + { + m_restartTimer->start(); + } + } + + void SignalMonitorWidget::restartAcquisition() + { + if (!m_acquiring) + { + return; + } + const bool singleArmed = m_view->isSingleArmed(); + stopAcquisition(); + m_acquiring = startAcquisition(); + m_view->setRunning(m_acquiring); + if (m_acquiring && singleArmed) + { + m_view->armSingle(); + } + updateControls(); + } + + // Auto puts about kAutoSamplesPerScreen samples on the screen, on the 1, 2, 5 steps, so the + // timebase alone sets the acquisition + double SignalMonitorWidget::sampleRate() const + { + const double fixed = m_sampleRateCombo->currentData().toDouble(); + return fixed > 0.0 ? fixed : autoSampleRate(); + } + + double SignalMonitorWidget::autoSampleRate() const + { + const double screen = OscilloscopeView::kHorizontalDivisions * m_timebaseCombo->currentData().toDouble(); + const double wanted = std::clamp(kAutoSamplesPerScreen / screen, 1.0, kMaximumSampleRate); + double rate = 1.0; + for (int index = 1; index < m_sampleRateCombo->count(); ++index) + { + const double step = m_sampleRateCombo->itemData(index).toDouble(); + if (step <= wanted * 1.0001) + { + rate = step; + } + } + return rate; + } + + void SignalMonitorWidget::updateSampleRateText() + { + m_sampleRateCombo->setItemText(0, tr("Auto (%1)").arg( + OscilloscopeView::formatValue(autoSampleRate(), QStringLiteral("S/s")))); + } + // Fits each shown channel into six of the eight divisions around the centre and puts the // trigger level in the middle of its channel when it is outside the signal void SignalMonitorWidget::autoScale() @@ -660,6 +826,10 @@ namespace scopeone::ui return; } m_sourceMessages.insert(sourceId, message); + if (state == scopeone::core::SignalSourceState::Error && m_acquiring) + { + stop(); + } const bool active = state == scopeone::core::SignalSourceState::Starting || state == scopeone::core::SignalSourceState::Running || state == scopeone::core::SignalSourceState::Stopping; @@ -675,26 +845,18 @@ namespace scopeone::ui updateStatus(); } - // The sources and their settings are fixed while they run; the screen stays adjustable + // Nothing is locked while running: a change to the sources restarts them void SignalMonitorWidget::updateControls() { - const bool running = !m_activeSources.isEmpty(); bool startable = false; for (const ChannelControls& channel : m_channels) { - channel.source->setEnabled(!running); - for (QWidget* editor : channel.editors) - { - // A file parameter sits in a row with its browse button - QWidget* const row = editor->parentWidget(); - (row && row != channel.box ? row : editor)->setEnabled(!running); - } startable = startable || (channel.box->isChecked() && channel.source->currentIndex() >= 0); } - m_sampleIntervalSpin->setEnabled(!running); - m_scanGroup->setEnabled(!running); - m_startButton->setEnabled(!running && startable); - m_stopButton->setEnabled(running); + m_runButton->setText(m_acquiring ? tr("Stop") : tr("Run")); + m_runButton->setIcon(style()->standardIcon(m_acquiring ? QStyle::SP_MediaStop : QStyle::SP_MediaPlay)); + m_runButton->setEnabled(m_acquiring || startable); + m_singleButton->setEnabled(startable); } void SignalMonitorWidget::updateStatus() @@ -717,8 +879,10 @@ namespace scopeone::ui void SignalMonitorWidget::loadSettings() { QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - m_sampleIntervalSpin->setValue(settings.value(monitorKey(QStringLiteral("intervalMs")), 1.0).toDouble()); selectNearest(m_timebaseCombo, settings.value(monitorKey(QStringLiteral("timebase")), 1e-3).toDouble()); + m_sampleRateCombo->setCurrentIndex(std::max(0, m_sampleRateCombo->findData( + settings.value(monitorKey(QStringLiteral("sampleRate")), 0.0).toDouble()))); + updateSampleRateText(); m_triggerPositionSpin->setValue(settings.value(monitorKey(QStringLiteral("triggerPosition")), 50.0).toDouble()); m_triggerModeCombo->setCurrentIndex(std::max(0, m_triggerModeCombo->findData( settings.value(monitorKey(QStringLiteral("triggerMode")), static_cast(ScopeTriggerMode::Auto))))); @@ -743,7 +907,7 @@ namespace scopeone::ui void SignalMonitorWidget::saveSettings() const { QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - settings.setValue(monitorKey(QStringLiteral("intervalMs")), m_sampleIntervalSpin->value()); + settings.setValue(monitorKey(QStringLiteral("sampleRate")), m_sampleRateCombo->currentData()); settings.setValue(monitorKey(QStringLiteral("timebase")), m_timebaseCombo->currentData()); settings.setValue(monitorKey(QStringLiteral("triggerPosition")), m_triggerPositionSpin->value()); settings.setValue(monitorKey(QStringLiteral("triggerMode")), m_triggerModeCombo->currentData()); diff --git a/src/tools/daq/SignalMonitorWidget.h b/src/tools/daq/SignalMonitorWidget.h index 0362b71..905a9a8 100644 --- a/src/tools/daq/SignalMonitorWidget.h +++ b/src/tools/daq/SignalMonitorWidget.h @@ -18,6 +18,7 @@ class QGroupBox; class QLabel; class QPushButton; class QSpinBox; +class QTimer; namespace scopeone::core { @@ -27,7 +28,7 @@ namespace scopeone::core namespace scopeone::ui { // The signal sources shown as an oscilloscope: each of the four channels shows one source, - // and Start runs the sources of the channels that are on + // and Run acquires the sources of the channels that are on at a rate that follows the timebase class SignalMonitorWidget final : public QWidget { Q_OBJECT @@ -59,8 +60,16 @@ namespace scopeone::ui void rebuildParameters(int index); void updateUnits(); void applyView(); - void startAcquisition(); + bool startAcquisition(); void stopAcquisition(); + void run(); + void stop(); + void single(); + void scheduleRestart(); + void restartAcquisition(); + double sampleRate() const; + double autoSampleRate() const; + void updateSampleRateText(); void autoScale(); void handleTimeSeries(const scopeone::core::TimeSeriesChunk& chunk); void handleStateChanged(const QString& sourceId, @@ -80,10 +89,10 @@ namespace scopeone::ui std::array m_channels; QList m_sources; - QDoubleSpinBox* m_sampleIntervalSpin{nullptr}; - QPushButton* m_startButton{nullptr}; - QPushButton* m_stopButton{nullptr}; QLabel* m_statusLabel{nullptr}; + QComboBox* m_sampleRateCombo{nullptr}; + QTimer* m_restartTimer{nullptr}; + bool m_acquiring{false}; QComboBox* m_timebaseCombo{nullptr}; QDoubleSpinBox* m_triggerPositionSpin{nullptr}; From ba23a9ce70960a37946887eb7d1e6932b15882f0 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:43:28 +0100 Subject: [PATCH 08/49] Keep the NI inputs running across timebase changes and rebuild their task once per change --- drivers/NIDaqmx/NIDaqmxDriver.cpp | 440 ++++++++++++++++---------- src/tools/daq/SignalMonitorWidget.cpp | 28 +- src/tools/daq/SignalMonitorWidget.h | 2 + 3 files changed, 297 insertions(+), 173 deletions(-) diff --git a/drivers/NIDaqmx/NIDaqmxDriver.cpp b/drivers/NIDaqmx/NIDaqmxDriver.cpp index bed60ff..172c469 100644 --- a/drivers/NIDaqmx/NIDaqmxDriver.cpp +++ b/drivers/NIDaqmx/NIDaqmxDriver.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -126,6 +127,9 @@ namespace ni_daqmx const UInt32*, Int32*, void*); + using GetDeviceRate = Int32 (SCOPEONE_DAQMX_CALL*)(const char*, double*); + using GetDeviceFlag = Int32 (SCOPEONE_DAQMX_CALL*)(const char*, Bool32*); + using GetTaskRate = Int32 (SCOPEONE_DAQMX_CALL*)(TaskHandle, double*); using AvailableSamples = Int32 (SCOPEONE_DAQMX_CALL*)(TaskHandle, UInt32*); using ReadAnalog = Int32 (SCOPEONE_DAQMX_CALL*)(TaskHandle, Int32, @@ -173,6 +177,10 @@ namespace ni_daqmx ConfigureSampleClock configureSampleClock{nullptr}; WriteAnalog writeAnalog{nullptr}; WriteDigital writeDigital{nullptr}; + GetDeviceRate getAiMaxSingleRate{nullptr}; + GetDeviceRate getAiMaxMultiRate{nullptr}; + GetDeviceFlag getAiSimultaneous{nullptr}; + GetTaskRate getSampleClockRate{nullptr}; AvailableSamples availableSamples{nullptr}; ReadAnalog readAnalog{nullptr}; ReadDigital readDigital{nullptr}; @@ -226,6 +234,10 @@ namespace ni_daqmx && resolve(configureSampleClock, "DAQmxCfgSampClkTiming") && resolve(writeAnalog, "DAQmxWriteAnalogF64") && resolve(writeDigital, "DAQmxWriteDigitalU32") + && resolve(getAiMaxSingleRate, "DAQmxGetDevAIMaxSingleChanRate") + && resolve(getAiMaxMultiRate, "DAQmxGetDevAIMaxMultiChanRate") + && resolve(getAiSimultaneous, "DAQmxGetDevAISimultaneousSamplingSupported") + && resolve(getSampleClockRate, "DAQmxGetSampClkRate") && resolve(availableSamples, "DAQmxGetReadAvailSampPerChan") && resolve(readAnalog, "DAQmxReadAnalogF64") && resolve(readDigital, "DAQmxReadDigitalU32"); @@ -830,9 +842,11 @@ namespace ni_daqmx } // The analog inputs of one device that run as signal sources. A device runs one analog - // input task at a time, so all of them are read by one task on the sample clock of the - // first. Adding or removing a channel rebuilds the task, and the samples keep counting - // from the first start, so the channels share one time axis and a rebuild leaves a gap. + // input task at a time, so all of them are read by one task, which a thread of the group + // builds, reads and rebuilds. Adding or removing a channel only records the change, and the + // thread rebuilds the task once the changes stop coming, so channels started or stopped + // together cost one rebuild. Sample times count from the first start, so the channels share + // one time axis and a rebuild leaves a gap. class AiTaskGroup final { public: @@ -848,7 +862,15 @@ namespace ni_daqmx ~AiTaskGroup() { - stopReader(); + { + std::lock_guard lock(m_mutex); + m_quit = true; + } + m_wake.notify_all(); + if (m_thread.joinable()) + { + m_thread.join(); + } } bool add(const QString& sourceId, @@ -856,71 +878,66 @@ namespace ni_daqmx const SignalAcquisitionConfig& config, QString* errorMessage) { - // load resolves the function pointers again, so the reader that calls them stops first - stopReader(); - QString loadError; - if (!m_api.load(true, &loadError)) + // The runtime is loaded once, before the thread that calls it starts; loading it on + // every start resolved the function pointers again while the reader called them + if (!m_thread.joinable()) { - return fail(sourceId, loadError, errorMessage); + QString loadError; + if (!m_api.load(true, &loadError)) + { + return fail(sourceId, loadError, errorMessage); + } + m_thread = std::thread([this]() { run(); }); } - quint64 offset = 0; + QString problem; { std::lock_guard lock(m_mutex); - for (const Member& member : m_members) + const bool acquiring = std::any_of(m_members.cbegin(), m_members.cend(), + [&sourceId](const Member& member) + { + return member.sourceId == sourceId; + }); + if (acquiring) { - if (member.sourceId == sourceId) - { - restartLocked(); - return fail(sourceId, QStringLiteral("%1 is already acquiring").arg(channel), - errorMessage); - } + problem = QStringLiteral("%1 is already acquiring").arg(channel); } - if (m_members.empty()) + else if (!m_members.empty() + && std::abs(config.sampleIntervalSeconds - m_interval) > 1e-9 * m_interval) { - m_interval = config.sampleIntervalSeconds; - m_origin = std::chrono::steady_clock::now(); + problem = QStringLiteral("%1 already samples its analog inputs every %2 s, so %3 " + "has to use the same interval") + .arg(m_device).arg(m_interval).arg(channel); } - else if (std::abs(config.sampleIntervalSeconds - m_interval) > 1e-9 * m_interval) + else { - restartLocked(); - return fail(sourceId, - QStringLiteral("%1 already samples its analog inputs every %2 s, so %3 " - "has to use the same interval") - .arg(m_device).arg(m_interval).arg(channel), - errorMessage); + if (m_members.empty()) + { + m_interval = config.sampleIntervalSeconds; + m_origin = std::chrono::steady_clock::now(); + } + Member member; + member.sourceId = sourceId; + member.channel = channel; + member.minimum = config.sourceSettings.value(QStringLiteral("minimumVolts"), -10.0).toDouble(); + member.maximum = config.sourceSettings.value(QStringLiteral("maximumVolts"), 10.0).toDouble(); + member.token = ++m_nextToken; + member.endSeconds = elapsedSecondsLocked() + config.durationMs / 1000.0; + m_members.push_back(member); + markChangedLocked(); + // Under the lock, so the thread cannot report Running before this + m_stateSink(sourceId, SignalSourceState::Starting, QStringLiteral("Configuring %1").arg(channel)); } - offset = elapsedSamples(); - Member member; - member.sourceId = sourceId; - member.channel = channel; - member.minimum = config.sourceSettings.value(QStringLiteral("minimumVolts"), -10.0).toDouble(); - member.maximum = config.sourceSettings.value(QStringLiteral("maximumVolts"), 10.0).toDouble(); - member.endSample = offset + static_cast( - std::max(0.0, std::ceil(config.durationMs / 1000.0 / m_interval))); - m_members.push_back(member); } - m_stateSink(sourceId, SignalSourceState::Starting, QStringLiteral("Configuring %1").arg(channel)); - QString error; + if (!problem.isEmpty()) { - std::lock_guard lock(m_mutex); - error = restartLocked(); + return fail(sourceId, problem, errorMessage); } - if (!error.isEmpty()) - { - if (errorMessage) - { - *errorMessage = error; - } - return false; - } - m_stateSink(sourceId, SignalSourceState::Running, QStringLiteral("%1 is acquiring").arg(channel)); + m_wake.notify_all(); return true; } void remove(const QString& sourceId) { - stopReader(); - QString channel; { std::lock_guard lock(m_mutex); const auto found = std::find_if(m_members.begin(), m_members.end(), @@ -928,17 +945,18 @@ namespace ni_daqmx { return member.sourceId == sourceId; }); - if (found != m_members.end()) + if (found == m_members.end()) { - channel = found->channel; - m_members.erase(found); + return; } - restartLocked(); - } - if (!channel.isEmpty()) - { + const QString channel = found->channel; + m_members.erase(found); + markChangedLocked(); + // Chunks are published under the lock for members still listed, so none of this + // source follows its Idle m_stateSink(sourceId, SignalSourceState::Idle, QStringLiteral("%1 stopped").arg(channel)); } + m_wake.notify_all(); } private: @@ -948,30 +966,173 @@ namespace ni_daqmx QString channel; double minimum{-10.0}; double maximum{10.0}; - quint64 endSample{0}; + quint64 token{0}; + double endSeconds{0.0}; }; - quint64 elapsedSamples() const + struct Task + { + TaskHandle handle{nullptr}; + std::vector members; + double interval{0.0}; + double startSeconds{0.0}; + quint64 sample{0}; + UInt32 blockSamples{1}; + }; + + static constexpr auto kSettle = std::chrono::milliseconds(50); + static constexpr auto kPoll = std::chrono::milliseconds(5); + static constexpr double kBlockSeconds = 0.02; + static constexpr UInt32 kSamplesPerRead = 65536; + + void markChangedLocked() + { + m_changed = true; + m_lastChange = std::chrono::steady_clock::now(); + } + + double elapsedSecondsLocked() const + { + return std::chrono::duration(std::chrono::steady_clock::now() - m_origin).count(); + } + + bool listedLocked(quint64 token) const + { + return std::any_of(m_members.cbegin(), m_members.cend(), + [token](const Member& member) { return member.token == token; }); + } + + void dropLocked(quint64 token) + { + m_members.erase(std::remove_if(m_members.begin(), m_members.end(), + [token](const Member& member) { return member.token == token; }), + m_members.end()); + } + + void run() + { + Task task; + std::vector interleaved; + std::unique_lock lock(m_mutex); + while (!m_quit) + { + if (m_changed) + { + // Waits until the channels stop changing, so one rebuild takes them all + if (m_wake.wait_until(lock, m_lastChange + kSettle, [this]() { return m_quit; })) + { + break; + } + if (std::chrono::steady_clock::now() < m_lastChange + kSettle) + { + continue; + } + m_changed = false; + const std::vector members = m_members; + const double interval = m_interval; + lock.unlock(); + closeTask(task); + const QString error = openTask(task, members, interval); + lock.lock(); + if (!error.isEmpty()) + { + failLocked(members, error); + continue; + } + task.startSeconds = elapsedSecondsLocked(); + for (const Member& member : task.members) + { + if (listedLocked(member.token)) + { + m_stateSink(member.sourceId, SignalSourceState::Running, + QStringLiteral("%1 is acquiring").arg(member.channel)); + } + } + continue; + } + if (!task.handle) + { + m_wake.wait(lock, [this]() { return m_quit || m_changed; }); + continue; + } + lock.unlock(); + // Reads blocks of about 20 ms; reading whatever had come every few milliseconds + // sent many small chunks, and each one costs as much on its way to the screen + const auto channelCount = static_cast(task.members.size()); + UInt32 available = 0; + Int32 samplesRead = 0; + Int32 result = m_api.availableSamples(task.handle, &available); + if (result >= 0 && available >= task.blockSamples) + { + const UInt32 wanted = std::min(available, kSamplesPerRead); + interleaved.resize(static_cast(wanted) * static_cast(channelCount)); + result = m_api.readAnalog(task.handle, static_cast(wanted), 0.0, + kGroupByScanNumber, interleaved.data(), + static_cast(interleaved.size()), + &samplesRead, nullptr); + } + const QString error = result < 0 ? m_api.error(result) : QString(); + lock.lock(); + if (!error.isEmpty()) + { + const std::vector members = task.members; + lock.unlock(); + closeTask(task); + lock.lock(); + failLocked(members, error); + continue; + } + if (samplesRead > 0) + { + publishLocked(task, interleaved, static_cast(samplesRead)); + } + else + { + m_wake.wait_for(lock, kPoll, [this]() { return m_quit || m_changed; }); + } + } + lock.unlock(); + closeTask(task); + } + + // Sampling faster than the device can made the task fail to start, which stopped the + // monitor with no signal; the rate is held to what the device reaches with this many + // channels, and the chunks carry the rate the clock really runs at + double maximumRate(std::size_t channelCount) { - const double elapsed = std::chrono::duration( - std::chrono::steady_clock::now() - m_origin).count(); - return static_cast(std::llround(elapsed / m_interval)); + const QByteArray device = m_device.toLocal8Bit(); + double rate = 0.0; + Int32 result = 0; + if (channelCount == 1) + { + result = m_api.getAiMaxSingleRate(device.constData(), &rate); + } + else + { + result = m_api.getAiMaxMultiRate(device.constData(), &rate); + Bool32 simultaneous = 0; + if (result >= 0 && m_api.getAiSimultaneous(device.constData(), &simultaneous) >= 0 + && !simultaneous) + { + rate /= static_cast(channelCount); + } + } + return result >= 0 && rate > 0.0 ? rate : std::numeric_limits::infinity(); } - // Builds and starts the task for the current members; on failure every member ends in - // the error, since the device then reads none of them - QString restartLocked() + QString openTask(Task& task, const std::vector& members, double interval) { - if (m_members.empty()) + task = {}; + if (members.empty()) { return {}; } - const double rate = 1.0 / m_interval; + double rate = std::min(1.0 / interval, maximumRate(members.size())); // The buffer of a continuous task holds two seconds, as the stream ring does const auto bufferSamples = static_cast(std::max(1000.0, 2.0 * rate)); TaskHandle handle = nullptr; Int32 result = m_api.createTask("", &handle); - for (const Member& member : m_members) + for (const Member& member : members) { if (result < 0) { @@ -989,6 +1150,11 @@ namespace ni_daqmx } if (result >= 0) { + double actual = 0.0; + if (m_api.getSampleClockRate(handle, &actual) >= 0 && actual > 0.0) + { + rate = actual; + } result = m_api.startTask(handle); } if (result < 0) @@ -998,144 +1164,76 @@ namespace ni_daqmx { m_api.clearTask(handle); } - for (const Member& member : m_members) - { - m_stateSink(member.sourceId, SignalSourceState::Error, message); - } - m_members.clear(); return message; } - const quint64 offset = elapsedSamples(); - m_reading.store(true); - m_reader = std::thread([this, handle, members = m_members, offset]() - { - read(handle, members, offset); - }); + task.handle = handle; + task.members = members; + task.interval = 1.0 / rate; + task.blockSamples = static_cast( + std::clamp(std::llround(rate * kBlockSeconds), 1LL, static_cast(kSamplesPerRead))); return {}; } - void stopReader() + void closeTask(Task& task) { - m_reading.store(false); - if (m_reader.joinable()) + if (task.handle) { - m_reader.join(); + m_api.stopTask(task.handle); + m_api.clearTask(task.handle); } + task = {}; } - // Reports only how members end by themselves, finished or failed; a member that is - // stopped or a task that is rebuilt is reported by the caller - void read(TaskHandle handle, std::vector members, quint64 offset) + // The members that are still listed end in the error, since the device reads none of them + void failLocked(const std::vector& members, const QString& error) { - constexpr UInt32 kSamplesPerRead = 65536; - const auto channelCount = static_cast(members.size()); - std::vector finished(members.size(), false); - qsizetype running = channelCount; - QVector interleaved; - quint64 nextSample = offset; - QString error; - while (m_reading.load() && running > 0) - { - UInt32 available = 0; - Int32 result = m_api.availableSamples(handle, &available); - if (result >= 0 && available > 0) - { - const UInt32 wanted = std::min(available, kSamplesPerRead); - interleaved.resize(static_cast(wanted) * channelCount); - Int32 samplesRead = 0; - result = m_api.readAnalog(handle, static_cast(wanted), 0.0, - kGroupByScanNumber, interleaved.data(), - static_cast(interleaved.size()), - &samplesRead, nullptr); - if (result >= 0 && samplesRead > 0) - { - publish(members, finished, running, interleaved, nextSample, - static_cast(samplesRead)); - nextSample += static_cast(samplesRead); - } - } - if (result < 0) - { - error = m_api.error(result); - break; - } - if (available == 0) - { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } - } - m_api.stopTask(handle); - m_api.clearTask(handle); - if (error.isEmpty()) + for (const Member& member : members) { - return; - } - std::lock_guard lock(m_mutex); - for (std::size_t index = 0; index < members.size(); ++index) - { - if (!finished[index]) + if (listedLocked(member.token)) { - dropMemberLocked(members[index].sourceId); - m_stateSink(members[index].sourceId, SignalSourceState::Error, error); + dropLocked(member.token); + m_stateSink(member.sourceId, SignalSourceState::Error, error); } } } - void publish(const std::vector& members, - std::vector& finished, - qsizetype& running, - const QVector& interleaved, - quint64 firstSample, - quint64 count) + void publishLocked(Task& task, const std::vector& interleaved, quint64 count) { - const auto channelCount = static_cast(members.size()); + const auto channelCount = static_cast(task.members.size()); + const double start = task.startSeconds + static_cast(task.sample) * task.interval; for (qsizetype channel = 0; channel < channelCount; ++channel) { - const Member& member = members[static_cast(channel)]; - if (finished[static_cast(channel)]) + const Member& member = task.members[static_cast(channel)]; + if (!listedLocked(member.token)) { continue; } - const quint64 usable = std::min(count, member.endSample > firstSample - ? member.endSample - firstSample - : 0); + const double left = std::ceil((member.endSeconds - start) / task.interval); + const auto usable = static_cast(std::clamp(left, 0.0, static_cast(count))); if (usable > 0) { TimeSeriesChunk chunk; chunk.sourceId = member.sourceId; chunk.quantity = QStringLiteral("Voltage"); chunk.unit = QStringLiteral("V"); - chunk.startTimeSeconds = static_cast(firstSample) * m_interval; - chunk.sampleIntervalSeconds = m_interval; + chunk.startTimeSeconds = start; + chunk.sampleIntervalSeconds = task.interval; chunk.values.resize(static_cast(usable)); for (qsizetype sample = 0; sample < chunk.values.size(); ++sample) { - chunk.values[sample] = interleaved[sample * channelCount + channel]; + chunk.values[sample] = interleaved[static_cast(sample * channelCount + channel)]; } m_chunks(chunk); } - if (firstSample + count >= member.endSample) + if (usable < count) { - finished[static_cast(channel)] = true; - --running; - { - std::lock_guard lock(m_mutex); - dropMemberLocked(member.sourceId); - } + dropLocked(member.token); + markChangedLocked(); m_stateSink(member.sourceId, SignalSourceState::Idle, QStringLiteral("%1 finished").arg(member.channel)); } } - } - - void dropMemberLocked(const QString& sourceId) - { - m_members.erase(std::remove_if(m_members.begin(), m_members.end(), - [&sourceId](const Member& member) - { - return member.sourceId == sourceId; - }), - m_members.end()); + task.sample += count; } bool fail(const QString& sourceId, const QString& message, QString* errorMessage) @@ -1153,11 +1251,15 @@ namespace ni_daqmx StateSink m_stateSink; DaqmxApi m_api; std::mutex m_mutex; + std::condition_variable m_wake; std::vector m_members; + quint64 m_nextToken{0}; double m_interval{0.0}; std::chrono::steady_clock::time_point m_origin; - std::atomic_bool m_reading{false}; - std::thread m_reader; + std::chrono::steady_clock::time_point m_lastChange; + bool m_changed{false}; + bool m_quit{false}; + std::thread m_thread; }; void appendChannels(DaqDeviceDescriptor& descriptor, diff --git a/src/tools/daq/SignalMonitorWidget.cpp b/src/tools/daq/SignalMonitorWidget.cpp index 58c8397..91ea917 100644 --- a/src/tools/daq/SignalMonitorWidget.cpp +++ b/src/tools/daq/SignalMonitorWidget.cpp @@ -161,8 +161,10 @@ namespace scopeone::ui } } m_sampleRateCombo->setToolTip( - tr("Auto follows the timebase with about %1 samples on the screen; a fixed rate is the " - "sample clock of a DAQ input and the counting bin of a photon counter") + tr("Auto takes the rate that puts about %1 samples on the screen when Run starts and keeps " + "it while the timebase gets slower; a fixed rate is the sample clock of a DAQ input " + "and the counting bin of a photon counter. A DAQ device samples at most as fast as it " + "can with the channels that are on") .arg(kAutoSamplesPerScreen)); horizontalForm->addRow(tr("Sample rate"), m_sampleRateCombo); m_triggerPositionSpin = new QDoubleSpinBox(horizontalBox); @@ -289,11 +291,13 @@ namespace scopeone::ui m_timebaseCombo->setCurrentIndex( std::clamp(m_timebaseCombo->currentIndex() + steps, 0, m_timebaseCombo->count() - 1)); }); + // Restarting on every timebase step emptied the screen and rebuilt the NI task each time, + // so turning the timebase left the screen without a signal; now it only changes the view connect(m_timebaseCombo, &QComboBox::currentIndexChanged, this, [this]() { applyView(); updateSampleRateText(); - scheduleRestart(); + followTimebase(); }); connect(m_sampleRateCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::scheduleRestart); for (QWidget* widget : m_scanGroup->findChildren()) @@ -618,13 +622,14 @@ namespace scopeone::ui m_sourceMessages.clear(); m_view->clear(); m_view->setRunning(true); + m_acquisitionRate = sampleRate(); QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); for (const QString& sourceId : sources) { scopeone::core::SignalAcquisitionConfig config; config.sourceId = sourceId; - config.sampleIntervalSeconds = 1.0 / sampleRate(); + config.sampleIntervalSeconds = 1.0 / m_acquisitionRate; config.scanImage.enabled = sourceId == m_scanSourceId; config.scanImage.width = m_scanWidthSpin->value(); config.scanImage.height = m_scanHeightSpin->value(); @@ -720,6 +725,21 @@ namespace scopeone::ui } } + // Auto keeps the rate it started with on a slower timebase, which the history still shows, and + // restarts only when the timebase got too fast for the rate to draw the screen + void SignalMonitorWidget::followTimebase() + { + if (!m_acquiring || m_sampleRateCombo->currentData().toDouble() > 0.0) + { + return; + } + const double screen = OscilloscopeView::kHorizontalDivisions * m_timebaseCombo->currentData().toDouble(); + if (m_acquisitionRate * screen < kAutoSamplesPerScreen / 10.0 && autoSampleRate() > m_acquisitionRate) + { + scheduleRestart(); + } + } + void SignalMonitorWidget::restartAcquisition() { if (!m_acquiring) diff --git a/src/tools/daq/SignalMonitorWidget.h b/src/tools/daq/SignalMonitorWidget.h index 905a9a8..a416605 100644 --- a/src/tools/daq/SignalMonitorWidget.h +++ b/src/tools/daq/SignalMonitorWidget.h @@ -66,6 +66,7 @@ namespace scopeone::ui void stop(); void single(); void scheduleRestart(); + void followTimebase(); void restartAcquisition(); double sampleRate() const; double autoSampleRate() const; @@ -93,6 +94,7 @@ namespace scopeone::ui QComboBox* m_sampleRateCombo{nullptr}; QTimer* m_restartTimer{nullptr}; bool m_acquiring{false}; + double m_acquisitionRate{0.0}; QComboBox* m_timebaseCombo{nullptr}; QDoubleSpinBox* m_triggerPositionSpin{nullptr}; From 77db8567659009ed5557fa9e4817a0d319f1fd21 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 12:19:58 +0100 Subject: [PATCH 09/49] Drag and turn the scope markers on the screen and trigger with a hysteresis that follows the noise --- drivers/NIDaqmx/NIDaqmxDriver.cpp | 29 +-- src/tools/daq/OscilloscopeView.cpp | 283 +++++++++++++++++++++----- src/tools/daq/OscilloscopeView.h | 35 +++- src/tools/daq/SignalMonitorWidget.cpp | 86 ++++++-- src/tools/daq/SignalMonitorWidget.h | 2 +- 5 files changed, 349 insertions(+), 86 deletions(-) diff --git a/drivers/NIDaqmx/NIDaqmxDriver.cpp b/drivers/NIDaqmx/NIDaqmxDriver.cpp index 172c469..007f2a2 100644 --- a/drivers/NIDaqmx/NIDaqmxDriver.cpp +++ b/drivers/NIDaqmx/NIDaqmxDriver.cpp @@ -825,21 +825,10 @@ namespace ni_daqmx std::atomic m_state{DaqState::Idle}; }; - SignalParameterDescriptor voltsParameter(const QString& key, - const QString& name, - double value) - { - SignalParameterDescriptor parameter; - parameter.key = key; - parameter.name = name; - parameter.type = SignalParameterType::Real; - parameter.defaultValue = value; - parameter.hasRange = true; - parameter.minimum = -10.0; - parameter.maximum = 10.0; - parameter.suffix = QStringLiteral(" V"); - return parameter; - } + // A signal source reads the widest input range, as the scale on the screen does the + // zooming; a range per channel was one more setting that restarted the task when changed + constexpr double kSourceMinimumVolts = -10.0; + constexpr double kSourceMaximumVolts = 10.0; // The analog inputs of one device that run as signal sources. A device runs one analog // input task at a time, so all of them are read by one task, which a thread of the group @@ -918,8 +907,6 @@ namespace ni_daqmx Member member; member.sourceId = sourceId; member.channel = channel; - member.minimum = config.sourceSettings.value(QStringLiteral("minimumVolts"), -10.0).toDouble(); - member.maximum = config.sourceSettings.value(QStringLiteral("maximumVolts"), 10.0).toDouble(); member.token = ++m_nextToken; member.endSeconds = elapsedSecondsLocked() + config.durationMs / 1000.0; m_members.push_back(member); @@ -964,8 +951,6 @@ namespace ni_daqmx { QString sourceId; QString channel; - double minimum{-10.0}; - double maximum{10.0}; quint64 token{0}; double endSeconds{0.0}; }; @@ -1141,7 +1126,7 @@ namespace ni_daqmx const QByteArray channel = member.channel.toLocal8Bit(); result = m_api.createAiVoltage(handle, channel.constData(), "", kDefaultTerminalConfiguration, - member.minimum, member.maximum, kVolts, nullptr); + kSourceMinimumVolts, kSourceMaximumVolts, kVolts, nullptr); } if (result >= 0) { @@ -1341,10 +1326,6 @@ namespace ni_daqmx source.quantity = QStringLiteral("Voltage"); source.unit = QStringLiteral("V"); source.streamType = SignalStreamType::TimeSeries; - source.parameters.append(voltsParameter( - QStringLiteral("minimumVolts"), QStringLiteral("Minimum"), -10.0)); - source.parameters.append(voltsParameter( - QStringLiteral("maximumVolts"), QStringLiteral("Maximum"), 10.0)); m_sources.append(source); m_sourceChannels.insert(source.id, channel.physicalName); m_sourceDevices.insert(source.id, device.name); diff --git a/src/tools/daq/OscilloscopeView.cpp b/src/tools/daq/OscilloscopeView.cpp index d2656ed..ad0c41e 100644 --- a/src/tools/daq/OscilloscopeView.cpp +++ b/src/tools/daq/OscilloscopeView.cpp @@ -9,6 +9,7 @@ #include #include +#include namespace scopeone::ui { @@ -34,6 +35,11 @@ namespace scopeone::ui constexpr int kRefreshMilliseconds = 33; constexpr qint64 kAutoTriggerMilliseconds = 200; constexpr qint64 kFrequencyMilliseconds = 250; + // The newest raw samples of the trigger channel its noise is measured on, a few times a second + constexpr qsizetype kNoiseSamples = 4096; + constexpr qint64 kHysteresisMilliseconds = 250; + // How far from a marker the mouse still takes it + constexpr double kReach = 9.0; // A triggered screen whose part after the edge takes longer than this is drawn as it fills constexpr double kProgressiveSeconds = 0.1; } @@ -73,6 +79,7 @@ namespace scopeone::ui // No reserve here: reserving the exact new size on every chunk made each chunk reallocate // and copy the whole history, which froze the screen on a long timebase const qsizetype count = chunk.values.size(); + interval = chunk.sampleIntervalSeconds; for (qsizetype index = 0; index < count; ++index) { const double time = chunk.startTimeSeconds + static_cast(index) * chunk.sampleIntervalSeconds; @@ -147,11 +154,13 @@ namespace scopeone::ui { setMinimumSize(480, 320); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + setMouseTracking(true); auto* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &OscilloscopeView::refresh); timer->start(kRefreshMilliseconds); m_sinceTrigger.start(); m_sinceFrequency.start(); + m_sinceHysteresis.start(); m_frequencies.fill(std::numeric_limits::quiet_NaN()); } @@ -196,6 +205,7 @@ namespace scopeone::ui { m_triggerArmed = false; m_pendingTrigger = std::numeric_limits::quiet_NaN(); + m_triggerHysteresis = std::numeric_limits::quiet_NaN(); } m_mode = mode; m_triggerChannel = channel; @@ -310,6 +320,18 @@ namespace scopeone::ui return true; } + double OscilloscopeView::displayedFrequency(int channel) const + { + const Measurement& measurement = m_frame.measurements[channel]; + return m_frame.valid && measurement.valid ? measurement.frequency : std::numeric_limits::quiet_NaN(); + } + + void OscilloscopeView::setMessage(const QString& message) + { + m_message = message; + update(); + } + QColor OscilloscopeView::channelColor(int index) { static const std::array colors{ @@ -393,8 +415,8 @@ namespace scopeone::ui } // Scans the trigger channel once over the samples that arrived, from where the last scan - // ended. The trigger re-arms only after the signal moves a tenth of a division past the level, - // so noise on a slow edge does not trigger twice. An edge whose screen has been received is + // ended. The trigger re-arms only after the signal moves the hysteresis past the level, so + // noise on a slow edge does not trigger twice. An edge whose screen has been received is // shown; an edge still waiting for the part after it is kept and, on a slow timebase, drawn // as the samples come in, as a scope does, and edges are not looked for until it is complete. void OscilloscopeView::processTrigger() @@ -417,7 +439,11 @@ namespace scopeone::ui const Trace& trace = m_traces[m_triggerChannel]; if (m_channels[m_triggerChannel].enabled && !trace.empty()) { - const double hysteresis = 0.1 * m_channels[m_triggerChannel].scale; + if (!std::isfinite(m_triggerHysteresis) || m_sinceHysteresis.elapsed() >= kHysteresisMilliseconds) + { + updateHysteresis(); + } + const double hysteresis = m_triggerHysteresis; const bool rising = m_slope == ScopeTriggerSlope::Rising; // The time before the edge may be held only by the buckets qsizetype index = trace.indexAtOrAfter( @@ -670,6 +696,121 @@ namespace scopeone::ui m_triggerArmed = false; m_pendingTrigger = std::numeric_limits::quiet_NaN(); m_scanTime = -std::numeric_limits::infinity(); + m_triggerHysteresis = std::numeric_limits::quiet_NaN(); + } + + // A fixed tenth of a division let the noise on a slow edge trigger on the other slope, so the + // picture jumped, and on a signal smaller than that never re-armed, so nothing was shown. The + // hysteresis now is six standard deviations of the noise, at most 40 % of the peak to peak so + // a real edge still re-arms. The noise is taken from the second difference, which cancels a + // slow signal, by its median absolute deviation, which ignores the edges. + void OscilloscopeView::updateHysteresis() + { + m_sinceHysteresis.restart(); + const Trace& trace = m_traces[m_triggerChannel]; + const double scale = m_channels[m_triggerChannel].scale; + const qsizetype count = std::min(kNoiseSamples, trace.times.size() - trace.begin); + if (count < 8) + { + m_triggerHysteresis = 0.1 * scale; + return; + } + const qsizetype first = trace.times.size() - count; + std::vector differences(static_cast(count - 2)); + double minimum = trace.values[first]; + double maximum = minimum; + for (qsizetype index = 0; index < count - 2; ++index) + { + const double* value = trace.values.constData() + first + index; + differences[static_cast(index)] = value[2] - 2.0 * value[1] + value[0]; + minimum = std::min({minimum, value[1], value[2]}); + maximum = std::max({maximum, value[1], value[2]}); + } + const auto middle = differences.begin() + static_cast(differences.size() / 2); + std::nth_element(differences.begin(), middle, differences.end()); + const double median = *middle; + for (double& difference : differences) + { + difference = std::abs(difference - median); + } + std::nth_element(differences.begin(), middle, differences.end()); + const double sigma = 1.4826 * *middle / std::sqrt(6.0); + m_triggerHysteresis = std::min(std::max(6.0 * sigma, 0.02 * scale), 0.4 * (maximum - minimum)); + } + + double OscilloscopeView::divisionsAt(double y) const + { + const double divisionHeight = m_screen.height() / kVerticalDivisions; + return std::clamp((m_screen.center().y() - y) / divisionHeight, + -kVerticalDivisions / 2.0, kVerticalDivisions / 2.0); + } + + double OscilloscopeView::triggerY() const + { + const double divisionHeight = m_screen.height() / kVerticalDivisions; + return std::clamp(m_screen.center().y() - divisionsOf(m_triggerChannel, m_triggerLevel) * divisionHeight, + m_screen.top(), m_screen.bottom()); + } + + // The level that zero, or with AC coupling the mean, is drawn at + double OscilloscopeView::channelTagY(int channel) const + { + const double divisionHeight = m_screen.height() / kVerticalDivisions; + return std::clamp(m_screen.center().y() + - m_channels[channel].offset / m_channels[channel].scale * divisionHeight, + m_screen.top(), m_screen.bottom()); + } + + // The trigger marker right of the screen, the trigger position above it, the channel markers + // left of it and the cursors on it + OscilloscopeView::Hit OscilloscopeView::hitTest(const QPointF& point) const + { + if (triggerShown()) + { + if (point.x() >= m_screen.right() && point.x() <= m_screen.right() + 30.0 + && std::abs(point.y() - triggerY()) <= kReach) + { + return {Handle::TriggerLevel, m_triggerChannel}; + } + const double x = m_screen.left() + m_triggerPosition * m_screen.width(); + if (point.y() <= m_screen.top() && point.y() >= m_screen.top() - 16.0 && std::abs(point.x() - x) <= kReach) + { + return {Handle::TriggerPosition, 0}; + } + } + if (point.x() <= m_screen.left() && point.x() >= m_screen.left() - 30.0) + { + Hit hit; + double nearest = kReach; + for (int channel = 0; channel < kChannelCount; ++channel) + { + const double distance = std::abs(point.y() - channelTagY(channel)); + if (m_channels[channel].enabled && distance <= nearest) + { + nearest = distance; + hit = {Handle::ChannelOffset, channel}; + } + } + return hit; + } + if (m_cursorsVisible && m_screen.contains(point)) + { + Hit hit; + double nearest = kReach; + for (int cursor = 0; cursor < 4; ++cursor) + { + const QPointF position = cursorPoint(cursor); + const double distance = cursor < 2 ? std::abs(point.x() - position.x()) + : std::abs(point.y() - position.y()); + if (distance < nearest) + { + nearest = distance; + hit = {Handle::Cursor, cursor}; + } + } + return hit; + } + return {}; } // The value of a channel in divisions above the centre line; with AC coupling the mean of @@ -743,12 +884,30 @@ namespace scopeone::ui } } painter.setPen(stateColor); - painter.drawText(m_screen.adjusted(6.0, 4.0, -6.0, -4.0), Qt::AlignTop | Qt::AlignRight, stateText); + const QRectF inside = m_screen.adjusted(6.0, 4.0, -6.0, -4.0); + painter.drawText(inside, Qt::AlignTop | Qt::AlignRight, stateText); + // The rate the samples really come at, which a DAQ device may hold below the one asked for + for (int channel = 0; channel < kChannelCount; ++channel) + { + if (m_channels[channel].enabled && m_traces[channel].interval > 0.0) + { + painter.setPen(kTextColor); + painter.drawText(inside, Qt::AlignTop | Qt::AlignLeft, + formatValue(1.0 / m_traces[channel].interval, QStringLiteral("S/s"))); + break; + } + } - if (!m_frame.valid) + if (!m_message.isEmpty()) + { + painter.setPen(kStoppedColor); + painter.drawText(inside, (m_frame.valid ? Qt::AlignBottom | Qt::AlignLeft : Qt::AlignCenter) + | Qt::TextWordWrap, m_message); + } + else if (!m_frame.valid) { painter.setPen(kTextColor); - painter.drawText(m_screen, Qt::AlignCenter, tr("No signal data")); + painter.drawText(m_screen, Qt::AlignCenter, m_running ? tr("Waiting for samples") : tr("Press Run")); } drawReadouts(painter, m_screen.bottom() + 8.0); } @@ -875,8 +1034,6 @@ namespace scopeone::ui void OscilloscopeView::drawMarkers(QPainter& painter) const { - const double divisionHeight = m_screen.height() / kVerticalDivisions; - const double centreY = m_screen.center().y(); painter.setRenderHint(QPainter::Antialiasing, true); for (int channel = 0; channel < kChannelCount; ++channel) { @@ -884,10 +1041,7 @@ namespace scopeone::ui { continue; } - // The level that zero, or with AC coupling the mean, is drawn at - const double y = std::clamp(centreY - m_channels[channel].offset / m_channels[channel].scale - * divisionHeight, - m_screen.top(), m_screen.bottom()); + const double y = channelTagY(channel); const double right = m_screen.left() - 3.0; const QPolygonF tag({QPointF(right - 24.0, y - 7.0), QPointF(right - 6.0, y - 7.0), QPointF(right, y), QPointF(right - 6.0, y + 7.0), @@ -902,8 +1056,7 @@ namespace scopeone::ui if (triggerShown()) { - const double level = centreY - divisionsOf(m_triggerChannel, m_triggerLevel) * divisionHeight; - const double y = std::clamp(level, m_screen.top(), m_screen.bottom()); + const double y = triggerY(); const double left = m_screen.right() + 3.0; const QPolygonF tag({QPointF(left, y), QPointF(left + 6.0, y - 7.0), QPointF(left + 24.0, y - 7.0), QPointF(left + 24.0, y + 7.0), @@ -1048,61 +1201,95 @@ namespace scopeone::ui void OscilloscopeView::mousePressEvent(QMouseEvent* event) { - m_dragCursor = -1; - if (!m_cursorsVisible || !m_screen.contains(event->position())) - { - return; - } - double nearest = 8.0; - for (int cursor = 0; cursor < 4; ++cursor) - { - const QPointF point = cursorPoint(cursor); - const double distance = cursor < 2 ? std::abs(event->position().x() - point.x()) - : std::abs(event->position().y() - point.y()); - if (distance < nearest) - { - nearest = distance; - m_dragCursor = cursor; - } - } + m_drag = event->button() == Qt::LeftButton ? hitTest(event->position()) : Hit{}; } void OscilloscopeView::mouseMoveEvent(QMouseEvent* event) { - if (m_dragCursor < 0) + const QPointF point = event->position(); + if (m_drag.handle == Handle::None) { + const Hit hit = hitTest(point); + const bool horizontal = hit.handle == Handle::TriggerPosition + || (hit.handle == Handle::Cursor && hit.index < 2); + if (hit.handle == Handle::None) + { + unsetCursor(); + } + else + { + setCursor(horizontal ? Qt::SizeHorCursor : Qt::SizeVerCursor); + } return; } - if (m_dragCursor < 2) - { - m_cursors[m_dragCursor] = std::clamp( - (event->position().x() - m_screen.left()) / m_screen.width(), 0.0, 1.0); + const double fraction = std::clamp((point.x() - m_screen.left()) / m_screen.width(), 0.0, 1.0); + const double divisions = divisionsAt(point.y()); + switch (m_drag.handle) + { + case Handle::Cursor: + m_cursors[m_drag.index] = m_drag.index < 2 ? fraction : divisions; + break; + case Handle::TriggerLevel: + { + const ScopeChannel& channel = m_channels[m_triggerChannel]; + const double mean = channel.acCoupled && m_frame.measurements[m_triggerChannel].valid + ? m_frame.measurements[m_triggerChannel].mean + : 0.0; + m_triggerLevel = divisions * channel.scale - channel.offset + mean; + m_dirty = true; + emit triggerLevelChanged(m_triggerLevel); + break; } - else - { - const double divisionHeight = m_screen.height() / kVerticalDivisions; - m_cursors[m_dragCursor] = std::clamp( - (m_screen.center().y() - event->position().y()) / divisionHeight, - -kVerticalDivisions / 2.0, kVerticalDivisions / 2.0); + case Handle::TriggerPosition: + m_triggerPosition = fraction; + m_dirty = true; + emit triggerPositionChanged(fraction); + break; + case Handle::ChannelOffset: + m_channels[m_drag.index].offset = divisions * m_channels[m_drag.index].scale; + emit channelOffsetChanged(m_drag.index, m_channels[m_drag.index].offset); + break; + case Handle::None: + break; } update(); } void OscilloscopeView::mouseReleaseEvent(QMouseEvent*) { - m_dragCursor = -1; + m_drag = {}; } - // Wheel up zooms in to a shorter timebase; a touchpad's small steps add up to whole notches + // Wheel up zooms in: over a channel marker to a smaller scale, elsewhere to a shorter + // timebase; with Shift or over the trigger marker it moves the trigger level a tenth of a + // division. A touchpad's small steps add up to whole notches. void OscilloscopeView::wheelEvent(QWheelEvent* event) { - m_wheelAngle += event->angleDelta().y(); + // Shift turns the wheel sideways on some systems + const QPoint angle = event->angleDelta(); + m_wheelAngle += angle.y() != 0 ? angle.y() : angle.x(); const int notches = m_wheelAngle / 120; - if (notches != 0) + event->accept(); + if (notches == 0) + { + return; + } + m_wheelAngle -= notches * 120; + const Hit hit = hitTest(event->position()); + if ((event->modifiers() & Qt::ShiftModifier) || hit.handle == Handle::TriggerLevel) + { + m_triggerLevel += notches * 0.1 * m_channels[m_triggerChannel].scale; + m_dirty = true; + update(); + emit triggerLevelChanged(m_triggerLevel); + } + else if (hit.handle == Handle::ChannelOffset) + { + emit channelScaleStepRequested(hit.index, -notches); + } + else { - m_wheelAngle -= notches * 120; emit timebaseStepRequested(-notches); } - event->accept(); } } diff --git a/src/tools/daq/OscilloscopeView.h b/src/tools/daq/OscilloscopeView.h index 105a835..cff5f08 100644 --- a/src/tools/daq/OscilloscopeView.h +++ b/src/tools/daq/OscilloscopeView.h @@ -67,6 +67,9 @@ namespace scopeone::ui void appendChunk(const scopeone::core::TimeSeriesChunk& chunk); void clear(); bool displayedRange(int channel, double* minimum, double* maximum) const; + double displayedFrequency(int channel) const; + // A message on the screen, such as the error that stopped a source; empty removes it + void setMessage(const QString& message); static QColor channelColor(int index); static QString formatValue(double value, const QString& unit); @@ -75,6 +78,12 @@ namespace scopeone::ui void runningChanged(bool running); // The wheel over the screen turns the timebase, positive steps to a longer one void timebaseStepRequested(int steps); + // The wheel over a channel marker turns its scale, positive steps to a larger one + void channelScaleStepRequested(int channel, int steps); + // The markers dragged on the screen, or the trigger level turned with Shift and the wheel + void triggerLevelChanged(double level); + void triggerPositionChanged(double position); + void channelOffsetChanged(int channel, double offset); protected: void paintEvent(QPaintEvent* event) override; @@ -92,6 +101,21 @@ namespace scopeone::ui Ready }; + enum class Handle + { + None, + Cursor, + TriggerLevel, + TriggerPosition, + ChannelOffset + }; + + struct Hit + { + Handle handle{Handle::None}; + int index{-1}; + }; + // The extremes and sums of the samples in one slice of time, a fixed fraction of the screen struct Bucket { @@ -114,6 +138,7 @@ namespace scopeone::ui QVector times; QVector values; qsizetype begin{0}; + double interval{0.0}; std::deque buckets; bool empty() const; @@ -162,6 +187,11 @@ namespace scopeone::ui void processTrigger(); void buildFrame(double start, double end, double triggerTime); void resetTriggerSearch(); + void updateHysteresis(); + double triggerY() const; + double channelTagY(int channel) const; + double divisionsAt(double y) const; + Hit hitTest(const QPointF& point) const; double divisionsOf(int channel, double value) const; void drawGrid(QPainter& painter) const; void drawTraces(QPainter& painter) const; @@ -188,6 +218,9 @@ namespace scopeone::ui double m_scanTime{-std::numeric_limits::infinity()}; QElapsedTimer m_sinceTrigger; QElapsedTimer m_sinceFrequency; + QElapsedTimer m_sinceHysteresis; + double m_triggerHysteresis{std::numeric_limits::quiet_NaN()}; + QString m_message; std::array m_frequencies{}; QPixmap m_gridCache; QRectF m_gridScreen; @@ -196,7 +229,7 @@ namespace scopeone::ui int m_cursorChannel{0}; // Two time cursors as fractions of the screen width, then two level cursors in divisions std::array m_cursors{0.3, 0.7, 2.0, -2.0}; - int m_dragCursor{-1}; + Hit m_drag; int m_wheelAngle{0}; QRectF m_screen; }; diff --git a/src/tools/daq/SignalMonitorWidget.cpp b/src/tools/daq/SignalMonitorWidget.cpp index 91ea917..62d4465 100644 --- a/src/tools/daq/SignalMonitorWidget.cpp +++ b/src/tools/daq/SignalMonitorWidget.cpp @@ -100,6 +100,21 @@ namespace scopeone::ui spin->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType); return spin; } + + // A checkable group that is off shows only its title, so the panel holds what is in use + void showContents(QGroupBox* box) + { + for (QWidget* child : box->findChildren(Qt::FindDirectChildrenOnly)) + { + child->setVisible(box->isChecked()); + } + } + + void collapseWhenOff(QGroupBox* box) + { + QObject::connect(box, &QGroupBox::toggled, box, [box]() { showContents(box); }); + showContents(box); + } } SignalMonitorWidget::SignalMonitorWidget(scopeone::core::ScopeOneCore* core, @@ -132,7 +147,9 @@ namespace scopeone::ui m_singleButton = new QPushButton(tr("Single"), controlBox); m_singleButton->setToolTip(tr("Take the next triggered screen and stop")); m_autoScaleButton = new QPushButton(tr("Auto Scale"), controlBox); - m_autoScaleButton->setToolTip(tr("Fit the scale and offset of each channel to the screen")); + m_autoScaleButton->setToolTip(tr("Fit the scale and offset of each channel to the screen, put " + "three periods of the trigger channel on it and trigger in " + "the middle of its signal")); controlButtons->addWidget(m_runButton); controlButtons->addWidget(m_singleButton); controlButtons->addWidget(m_autoScaleButton); @@ -201,14 +218,14 @@ namespace scopeone::ui panelLayout->addWidget(createChannelBox(index)); } - auto* displayBox = new QGroupBox(tr("Cursors"), panel); - auto* displayForm = new QFormLayout(displayBox); - m_cursorsCheck = new QCheckBox(tr("Show cursors"), displayBox); - m_cursorsCheck->setToolTip(tr("Drag the cursors on the screen")); - displayForm->addRow(m_cursorsCheck); - m_cursorChannelCombo = channelCombo(displayBox); - displayForm->addRow(tr("Channel"), m_cursorChannelCombo); - panelLayout->addWidget(displayBox); + m_cursorsGroup = new QGroupBox(tr("Cursors"), panel); + m_cursorsGroup->setCheckable(true); + m_cursorsGroup->setChecked(false); + m_cursorsGroup->setToolTip(tr("Drag the cursors on the screen")); + auto* cursorsForm = new QFormLayout(m_cursorsGroup); + m_cursorChannelCombo = channelCombo(m_cursorsGroup); + cursorsForm->addRow(tr("Channel"), m_cursorChannelCombo); + panelLayout->addWidget(m_cursorsGroup); m_scanGroup = new QGroupBox(tr("Scan image from CH1"), panel); m_scanGroup->setCheckable(true); @@ -259,6 +276,12 @@ namespace scopeone::ui panelLayout->addStretch(1); loadSettings(); + collapseWhenOff(m_cursorsGroup); + collapseWhenOff(m_scanGroup); + for (const ChannelControls& channel : m_channels) + { + collapseWhenOff(channel.box); + } m_restartTimer = new QTimer(this); m_restartTimer->setSingleShot(true); @@ -321,7 +344,29 @@ namespace scopeone::ui connect(m_triggerChannelCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::updateUnits); connect(m_triggerSlopeCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); connect(m_triggerLevelSpin, &QDoubleSpinBox::valueChanged, this, &SignalMonitorWidget::applyView); - connect(m_cursorsCheck, &QCheckBox::toggled, this, &SignalMonitorWidget::applyView); + connect(m_cursorsGroup, &QGroupBox::toggled, this, &SignalMonitorWidget::applyView); + // What is dragged or turned on the screen goes back to the controls, which the view + // already follows + connect(m_view, &OscilloscopeView::channelScaleStepRequested, this, [this](int index, int steps) + { + QComboBox* const scale = m_channels[index].scale; + scale->setCurrentIndex(std::clamp(scale->currentIndex() + steps, 0, scale->count() - 1)); + }); + connect(m_view, &OscilloscopeView::triggerLevelChanged, this, [this](double level) + { + const QSignalBlocker blocker(m_triggerLevelSpin); + m_triggerLevelSpin->setValue(level); + }); + connect(m_view, &OscilloscopeView::triggerPositionChanged, this, [this](double position) + { + const QSignalBlocker blocker(m_triggerPositionSpin); + m_triggerPositionSpin->setValue(position * 100.0); + }); + connect(m_view, &OscilloscopeView::channelOffsetChanged, this, [this](int index, double offset) + { + const QSignalBlocker blocker(m_channels[index].offset); + m_channels[index].offset->setValue(offset); + }); connect(m_cursorChannelCombo, &QComboBox::currentIndexChanged, this, &SignalMonitorWidget::applyView); connect(m_core, &scopeone::core::ScopeOneCore::signalSourcesChanged, @@ -549,6 +594,7 @@ namespace scopeone::ui connect(edit, &QLineEdit::editingFinished, this, &SignalMonitorWidget::scheduleRestart); } } + showContents(channel.box); updateControls(); } @@ -593,7 +639,7 @@ namespace scopeone::ui static_cast(m_triggerSlopeCombo->currentData().toInt()), m_triggerLevelSpin->value(), m_triggerPositionSpin->value() / 100.0); - m_view->setCursors(m_cursorsCheck->isChecked(), m_cursorChannelCombo->currentIndex()); + m_view->setCursors(m_cursorsGroup->isChecked(), m_cursorChannelCombo->currentIndex()); } bool SignalMonitorWidget::startAcquisition() @@ -652,6 +698,7 @@ namespace scopeone::ui qWarning().noquote() << errorMessage; stopAcquisition(); m_statusLabel->setText(errorMessage); + m_view->setMessage(errorMessage); return false; } if (!m_activeSources.contains(sourceId)) @@ -688,6 +735,7 @@ namespace scopeone::ui void SignalMonitorWidget::run() { m_restartTimer->stop(); + m_view->setMessage({}); m_acquiring = startAcquisition(); m_view->setRunning(m_acquiring); updateControls(); @@ -821,6 +869,17 @@ namespace scopeone::ui m_triggerLevelSpin->setValue((minimum + maximum) / 2.0); } } + // Three periods of the trigger channel on the screen, triggered in the middle, as the + // autoset of a scope; a signal with no period found keeps the timebase + const QSignalBlocker modeBlocker(m_triggerModeCombo); + const QSignalBlocker positionBlocker(m_triggerPositionSpin); + m_triggerModeCombo->setCurrentIndex(m_triggerModeCombo->findData(static_cast(ScopeTriggerMode::Auto))); + m_triggerPositionSpin->setValue(50.0); + const double frequency = m_view->displayedFrequency(m_triggerChannelCombo->currentIndex()); + if (std::isfinite(frequency) && frequency > 0.0) + { + selectNearest(m_timebaseCombo, 3.0 / frequency / OscilloscopeView::kHorizontalDivisions); + } applyView(); } @@ -846,8 +905,11 @@ namespace scopeone::ui return; } m_sourceMessages.insert(sourceId, message); + // The error that stops the run is shown on the screen, where it is not missed if (state == scopeone::core::SignalSourceState::Error && m_acquiring) { + const QString name = descriptor(sourceId).name; + m_view->setMessage(QStringLiteral("%1: %2").arg(name.isEmpty() ? sourceId : name, message)); stop(); } const bool active = state == scopeone::core::SignalSourceState::Starting @@ -994,7 +1056,7 @@ namespace scopeone::ui { SCOPEONE_REGISTER_TOOL( {QStringLiteral("scopeone.signal_monitor"), QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Signal Monitor")), - QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Acquisition")), ToolWindowMode::ModelessSingleton}, + {}, ToolWindowMode::ModelessSingleton}, [](ScopeOneToolContext& context, QWidget* parent) { return new SignalMonitorWidget(&context.core(), context, parent); diff --git a/src/tools/daq/SignalMonitorWidget.h b/src/tools/daq/SignalMonitorWidget.h index a416605..31d4320 100644 --- a/src/tools/daq/SignalMonitorWidget.h +++ b/src/tools/daq/SignalMonitorWidget.h @@ -105,7 +105,7 @@ namespace scopeone::ui QPushButton* m_runButton{nullptr}; QPushButton* m_singleButton{nullptr}; QPushButton* m_autoScaleButton{nullptr}; - QCheckBox* m_cursorsCheck{nullptr}; + QGroupBox* m_cursorsGroup{nullptr}; QComboBox* m_cursorChannelCombo{nullptr}; QGroupBox* m_scanGroup{nullptr}; From a263de8b42865ba4cb8e00817267ade1c368b0b3 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 12:23:26 +0100 Subject: [PATCH 10/49] List every tool directly in the Tools menu --- src/tools/daq/DaqControlWidget.cpp | 2 +- src/tools/dhm/DhmTool.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tools/daq/DaqControlWidget.cpp b/src/tools/daq/DaqControlWidget.cpp index 6e0fec0..f1c81c0 100644 --- a/src/tools/daq/DaqControlWidget.cpp +++ b/src/tools/daq/DaqControlWidget.cpp @@ -859,7 +859,7 @@ namespace scopeone::ui { SCOPEONE_REGISTER_TOOL( {QStringLiteral("scopeone.daq_control"), QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Scanning and DAQ Control")), - QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Acquisition")), ToolWindowMode::ModelessSingleton}, + {}, ToolWindowMode::ModelessSingleton}, [](ScopeOneToolContext& context, QWidget* parent) { return new DaqControlWidget(&context.core(), parent); diff --git a/src/tools/dhm/DhmTool.cpp b/src/tools/dhm/DhmTool.cpp index f6746f9..b2f14c1 100644 --- a/src/tools/dhm/DhmTool.cpp +++ b/src/tools/dhm/DhmTool.cpp @@ -772,7 +772,7 @@ namespace scopeone::ui { SCOPEONE_REGISTER_TOOL( {QStringLiteral("scopeone.dhm_reconstruction"), QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "DHM Reconstruction")), - QString::fromUtf8(QT_TRANSLATE_NOOP("Tools", "Reconstruction")), ToolWindowMode::ModelessSingleton}, + {}, ToolWindowMode::ModelessSingleton}, [](ScopeOneToolContext& context, QWidget* parent) { return new DhmTool(context, parent); From fabea2d7bfd88c63b6ef35b6410bc7fef123dd61 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:37:12 +0100 Subject: [PATCH 11/49] Build the CUDA and ONNX modules into Core, load only drivers and release stopped stream rings --- .github/workflows/compile-check.yml | 6 +- .github/workflows/windows-release.yml | 6 +- .gitignore | 6 - CMakeLists.txt | 36 ++-- CONTRIBUTING.md | 24 +-- README.md | 16 +- ScopeOneCore/CMakeLists.txt | 134 +++++++++++-- ScopeOneCore/DRIVERHOST_PROTOCOL.md | 9 +- ScopeOneCore/README.md | 24 +-- .../cmake/ScopeOneCoreConfig.cmake.in | 2 +- ...peOnePlugin.cmake => ScopeOneDriver.cmake} | 9 +- .../include/scopeone/DriverManifest.h | 42 ++++ .../include/scopeone/PluginManifest.h | 59 ------ .../include/scopeone/ProcessingPipeline.h | 2 +- .../{ProcessingPlugin.h => ProcessingTypes.h} | 13 -- ScopeOneCore/include/scopeone/ScopeOneCore.h | 2 +- .../include/scopeone/scopeone_core_export.h | 2 +- .../internal/DriverHostProviderProxy.h | 2 +- ScopeOneCore/internal/ProcessingModule.h | 2 +- .../internal/ProcessingModuleRegistry.h | 7 +- .../internal/PythonProcessingModule.h | 4 +- ScopeOneCore/internal/SignalSourceManager.h | 2 +- ScopeOneCore/internal/cuda/CudaDevice.h | 8 + .../cuda}/CudaFrequencyFilterModule.h | 6 +- .../internal/cuda}/CudaGaussianBlurModule.h | 4 +- .../internal}/cuda/CudaKernelLaunch.h | 9 +- .../cuda/CudaProcessingRegistration.h | 12 ++ .../internal}/cuda/CudaRealImageModule.h | 7 +- .../internal}/cuda/GpuComplexFrame.h | 3 +- .../internal}/cuda/GpuRealFrame.h | 3 +- .../internal}/inference/ExecutionProvider.h | 0 .../InferenceProcessingRegistration.h | 12 ++ .../internal/inference}/OnnxInferenceModule.h | 6 +- .../internal/inference}/OnnxInternal.h | 4 +- .../internal}/inference/OnnxRuntime.h | 5 +- .../internal}/inference/OnnxSession.h | 7 +- .../internal}/inference/TensorInfo.h | 0 ScopeOneCore/python/scopeone/README.md | 38 +++- .../scopeone/examples/processing_offset.py | 2 +- .../scopeone/src/scopeone/processing.py | 4 +- ScopeOneCore/src/DriverHostMain.cpp | 77 ++++++-- ScopeOneCore/src/DriverHostProviderProxy.cpp | 50 +++-- ...{PluginManifest.cpp => DriverManifest.cpp} | 111 +++-------- ScopeOneCore/src/ProcessingModuleRegistry.cpp | 75 ++------ ScopeOneCore/src/PythonProcessingModule.cpp | 4 +- ScopeOneCore/src/ScopeOneCore.cpp | 34 ++-- ScopeOneCore/src/SignalSourceManager.cpp | 2 +- .../src/cuda}/CudaDevice.cpp | 2 +- .../src/cuda}/CudaFrequencyFilterModule.cpp | 6 +- .../src/cuda}/CudaGaussianBlurModule.cpp | 6 +- .../src/cuda}/CudaKernels.cu | 2 +- .../src/cuda}/CudaProcessingRegistration.cpp | 37 ++-- .../src/cuda}/CudaRealImageModule.cpp | 4 +- .../src/cuda}/GpuComplexFrame.cpp | 2 +- .../src/cuda}/GpuRealFrame.cpp | 4 +- .../InferenceProcessingRegistration.cpp | 33 ++-- .../src/inference}/OnnxInferenceModule.cpp | 2 +- .../src/inference}/OnnxRuntime.cpp | 2 +- .../src/inference}/OnnxSession.cpp | 2 +- ScopeOneCuda/CMakeLists.txt | 100 ---------- .../cmake/ScopeOneCudaConfig.cmake.in | 7 - .../include/scopeone/cuda/CudaDevice.h | 9 - .../include/scopeone/cuda/CudaExport.h | 11 -- ScopeOneCuda/plugin.json | 7 - ScopeOneInference/CMakeLists.txt | 117 ------------ .../cmake/ScopeOneInferenceConfig.cmake.in | 6 - .../scopeone/inference/InferenceExport.h | 11 -- ScopeOneInference/plugin.json | 7 - drivers/CMakeLists.txt | 5 + drivers/ExampleCamera/TestPatterns.h | 2 +- drivers/NIDaqmx/NIDaqmxDriver.cpp | 2 +- drivers/README.md | 4 +- plugins/CMakeLists.txt | 37 ---- plugins/README.md | 50 ----- scripts/build-unix.sh | 20 +- scripts/build.ps1 | 127 ++++-------- ...ginManagerDialog.cpp => DriversDialog.cpp} | 180 +++++++----------- ...{PluginManagerDialog.h => DriversDialog.h} | 8 +- src/MainWindow.cpp | 9 +- src/main.cpp | 2 +- src/tools/daq/DaqControlWidget.cpp | 2 +- 81 files changed, 679 insertions(+), 1038 deletions(-) rename ScopeOneCore/cmake/{ScopeOnePlugin.cmake => ScopeOneDriver.cmake} (86%) create mode 100644 ScopeOneCore/include/scopeone/DriverManifest.h delete mode 100644 ScopeOneCore/include/scopeone/PluginManifest.h rename ScopeOneCore/include/scopeone/{ProcessingPlugin.h => ProcessingTypes.h} (90%) create mode 100644 ScopeOneCore/internal/cuda/CudaDevice.h rename {ScopeOneCuda/src => ScopeOneCore/internal/cuda}/CudaFrequencyFilterModule.h (90%) rename {ScopeOneCuda/src => ScopeOneCore/internal/cuda}/CudaGaussianBlurModule.h (89%) rename {ScopeOneCuda/include/scopeone => ScopeOneCore/internal}/cuda/CudaKernelLaunch.h (87%) create mode 100644 ScopeOneCore/internal/cuda/CudaProcessingRegistration.h rename {ScopeOneCuda/include/scopeone => ScopeOneCore/internal}/cuda/CudaRealImageModule.h (80%) rename {ScopeOneCuda/include/scopeone => ScopeOneCore/internal}/cuda/GpuComplexFrame.h (90%) rename {ScopeOneCuda/include/scopeone => ScopeOneCore/internal}/cuda/GpuRealFrame.h (94%) rename {ScopeOneInference/include/scopeone => ScopeOneCore/internal}/inference/ExecutionProvider.h (100%) create mode 100644 ScopeOneCore/internal/inference/InferenceProcessingRegistration.h rename {ScopeOneInference/src => ScopeOneCore/internal/inference}/OnnxInferenceModule.h (87%) rename {ScopeOneInference/src => ScopeOneCore/internal/inference}/OnnxInternal.h (66%) rename {ScopeOneInference/include/scopeone => ScopeOneCore/internal}/inference/OnnxRuntime.h (82%) rename {ScopeOneInference/include/scopeone => ScopeOneCore/internal}/inference/OnnxSession.h (83%) rename {ScopeOneInference/include/scopeone => ScopeOneCore/internal}/inference/TensorInfo.h (100%) rename ScopeOneCore/src/{PluginManifest.cpp => DriverManifest.cpp} (50%) rename {ScopeOneCuda/src => ScopeOneCore/src/cuda}/CudaDevice.cpp (87%) rename {ScopeOneCuda/src => ScopeOneCore/src/cuda}/CudaFrequencyFilterModule.cpp (97%) rename {ScopeOneCuda/src => ScopeOneCore/src/cuda}/CudaGaussianBlurModule.cpp (94%) rename {ScopeOneCuda/src => ScopeOneCore/src/cuda}/CudaKernels.cu (99%) rename {ScopeOneCuda/src => ScopeOneCore/src/cuda}/CudaProcessingRegistration.cpp (78%) rename {ScopeOneCuda/src => ScopeOneCore/src/cuda}/CudaRealImageModule.cpp (96%) rename {ScopeOneCuda/src => ScopeOneCore/src/cuda}/GpuComplexFrame.cpp (98%) rename {ScopeOneCuda/src => ScopeOneCore/src/cuda}/GpuRealFrame.cpp (98%) rename {ScopeOneInference/src => ScopeOneCore/src/inference}/InferenceProcessingRegistration.cpp (77%) rename {ScopeOneInference/src => ScopeOneCore/src/inference}/OnnxInferenceModule.cpp (99%) rename {ScopeOneInference/src => ScopeOneCore/src/inference}/OnnxRuntime.cpp (95%) rename {ScopeOneInference/src => ScopeOneCore/src/inference}/OnnxSession.cpp (99%) delete mode 100644 ScopeOneCuda/CMakeLists.txt delete mode 100644 ScopeOneCuda/cmake/ScopeOneCudaConfig.cmake.in delete mode 100644 ScopeOneCuda/include/scopeone/cuda/CudaDevice.h delete mode 100644 ScopeOneCuda/include/scopeone/cuda/CudaExport.h delete mode 100644 ScopeOneCuda/plugin.json delete mode 100644 ScopeOneInference/CMakeLists.txt delete mode 100644 ScopeOneInference/cmake/ScopeOneInferenceConfig.cmake.in delete mode 100644 ScopeOneInference/include/scopeone/inference/InferenceExport.h delete mode 100644 ScopeOneInference/plugin.json delete mode 100644 plugins/CMakeLists.txt delete mode 100644 plugins/README.md rename src/{PluginManagerDialog.cpp => DriversDialog.cpp} (53%) rename src/{PluginManagerDialog.h => DriversDialog.h} (74%) diff --git a/.github/workflows/compile-check.yml b/.github/workflows/compile-check.yml index affde88..d4dcde9 100644 --- a/.github/workflows/compile-check.yml +++ b/.github/workflows/compile-check.yml @@ -158,9 +158,9 @@ jobs: if ($LASTEXITCODE -ne 0) { throw "Building MMCore failed." } if (-not (Test-Path "$mm\build\Release\x64\MMCore.lib")) { throw "MMCore.lib was not produced." } - - name: Compile Core, plugins, drivers and GUI + - name: Compile Core, drivers and GUI shell: pwsh run: | .\scripts\build.ps1 --configure ` - --pluginConfigureOption -DSCOPEONE_ENABLE_CUDA=OFF ` - --pluginConfigureOption -DSCOPEONE_ENABLE_INFERENCE=OFF + --coreConfigureOption -DSCOPEONE_ENABLE_CUDA=OFF ` + --coreConfigureOption -DSCOPEONE_ENABLE_INFERENCE=OFF diff --git a/.github/workflows/windows-release.yml b/.github/workflows/windows-release.yml index cc9d2b7..2dc44d0 100644 --- a/.github/workflows/windows-release.yml +++ b/.github/workflows/windows-release.yml @@ -199,8 +199,8 @@ jobs: } else { $options += @( - "--pluginConfigureOption", "-DSCOPEONE_ENABLE_CUDA=OFF", - "--pluginConfigureOption", "-DSCOPEONE_ENABLE_INFERENCE=OFF" + "--coreConfigureOption", "-DSCOPEONE_ENABLE_CUDA=OFF", + "--coreConfigureOption", "-DSCOPEONE_ENABLE_INFERENCE=OFF" ) } .\scripts\build.ps1 --configure --package @options @@ -214,7 +214,7 @@ jobs: Get-Item $file | Select-Object Name, Length } $listing = 7z l "build\$name.zip" - $hasGpuFiles = [bool]($listing | Select-String -Pattern "cufft64|onnxruntime|ScopeOneInference") + $hasGpuFiles = [bool]($listing | Select-String -Pattern "cufft64|onnxruntime") if ("${{ matrix.gpu }}" -eq "true" -and -not $hasGpuFiles) { throw "The GPU package has no CUDA or ONNX Runtime files." } if ("${{ matrix.gpu }}" -ne "true" -and $hasGpuFiles) { throw "The standard package must not contain CUDA or ONNX Runtime files." } diff --git a/.gitignore b/.gitignore index d7e556c..74c17b7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,14 +3,8 @@ !/src/** !/ScopeOneCore/ !/ScopeOneCore/** -!/plugins/ -!/plugins/** !/drivers/ !/drivers/** -!/ScopeOneCuda/ -!/ScopeOneCuda/** -!/ScopeOneInference/ -!/ScopeOneInference/** /ScopeOneCore/install/ /ScopeOneCore/build/ /drivers/build/ diff --git a/CMakeLists.txt b/CMakeLists.txt index ad163a9..8135d10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,7 @@ set(SCOPEONE_UI_SOURCES src/DevicePropertyWidget.cpp src/PositionListFile.cpp src/RecordingWidget.cpp - src/PluginManagerDialog.cpp + src/DriversDialog.cpp src/ScopeOneLocalApiServer.cpp src/ToolRegistry.cpp src/AboutDialog.cpp @@ -112,7 +112,7 @@ set(SCOPEONE_UI_HEADERS src/DevicePropertyWidget.h src/PositionListFile.h src/RecordingWidget.h - src/PluginManagerDialog.h + src/DriversDialog.h src/ScopeOneLocalApiServer.h src/ToolRegistry.h src/AboutDialog.h @@ -267,17 +267,15 @@ if (UNIX AND NOT APPLE) ) endif () -foreach (_plugin_kind IN ITEMS plugins/processing drivers) - set(_plugin_dir "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/${_plugin_kind}") - if (EXISTS "${_plugin_dir}") - add_custom_command(TARGET ScopeOne POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory - "${_plugin_dir}" - "$/${_plugin_kind}" - ) - install(DIRECTORY "${_plugin_dir}/" DESTINATION "${_plugin_kind}") - endif () -endforeach () +set(_driver_dir "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/drivers") +if (EXISTS "${_driver_dir}") + add_custom_command(TARGET ScopeOne POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + "${_driver_dir}" + "$/drivers" + ) + install(DIRECTORY "${_driver_dir}/" DESTINATION "drivers") +endif () if (WIN32) if (WINDEPLOYQT_EXECUTABLE) @@ -295,12 +293,12 @@ if (WIN32) endif () endif () -# # Fast Release build -# if (MSVC) -# target_compile_options(ScopeOne PRIVATE -# $<$:/MP> -# ) -# endif () +# The Visual Studio generator compiles the sources of one project one after another unless the +# compiler runs them in parallel itself +if (MSVC) + target_compile_options(ScopeOne PRIVATE /MP) + target_compile_options(ScopeOneMcpServer PRIVATE /MP) +endif () # LTO Release build if (MSVC) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6b849fc..f6ca056 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,15 +10,14 @@ disagree in a specific case, follow the reason and say so in the review. | Part | Folder | Namespace | May use | |---|---|---|---| -| Core | `ScopeOneCore` | `scopeone::core`, `core::internal`, `core::imageops` | Qt Core, OpenCV, MMCore, ScopeWriter. No Qt Widgets. | +| Core | `ScopeOneCore` | `scopeone::core`, `core::internal`, `core::imageops`, `scopeone::cuda` and `scopeone::inference` for the CUDA and ONNX modules | Qt Core, OpenCV, MMCore, ScopeWriter, and CUDA and ONNX Runtime when they are built. No Qt Widgets. | | Application | `src` | `scopeone::ui` | Core, Qt Widgets | | MCP server | `src/ScopeOneMcpServer.cpp` | | Qt Core, Qt Network and the Local API protocol. Not Core. | | Drivers | `drivers` | their own | `scopeone::DriverSDK` | -| Plugins | `plugins`, `ScopeOneCuda`, `ScopeOneInference` | `scopeone::plugins::cuda` for CUDA, `scopeone::inference` for ONNX | `scopeone::ScopeOneCore` | | Python package | `ScopeOneCore/python/scopeone` | `scopeone` | the Local API protocol | -A lower layer never knows about a higher one. Core does not include from `src`, and drivers and plugins do -not include Core internals. This keeps each part buildable and usable without the ones above it. +A lower layer never knows about a higher one. Core does not include from `src`, and drivers do not include +Core internals. This keeps each part buildable and usable without the ones above it. **Entry points.** Code outside Core enters through the public headers in `ScopeOneCore/include/scopeone` and the `ScopeOneCore` facade. `ScopeOneCore/internal` is for Core @@ -28,7 +27,7 @@ A narrow entry point is what lets Core change its insides without breaking calle **Decide the layer first.** Before writing a feature, decide where it belongs. Anything that must work without a window (acquisition, hardware, processing, recording, data) goes into Core and is reachable through the Local API before it gets a widget. Anything that only changes what -is shown goes into `src`. Hardware goes into drivers and optional processing into plugins. +is shown goes into `src`. Hardware goes into drivers. **Tools.** A window of the Tools menu, with its own state and workflow, goes into its own folder under `src/tools`. It uses only the public Core headers and `ScopeOneToolContext` from @@ -38,9 +37,10 @@ A command that takes an image and parameters and returns a result is not a tool; image menus. **Extension boundary.** Drivers and Python processing modules are the only public -extension points, and both run outside the ScopeOne process. The CUDA and ONNX processing plugins -exist only to keep CUDA and ONNX Runtime out of Core, and their interface is internal to this -repository. Everything without such a dependency belongs in Core, with its window in `src`. The +extension points, and both run outside the ScopeOne process. Every other processing method +belongs in Core, with its window in `src`. The CUDA and ONNX modules are in Core too, under +`src/cuda` and `src/inference`, and `SCOPEONE_ENABLE_CUDA` and `SCOPEONE_ENABLE_INFERENCE` build +Core without them. The messages between ScopeOne and `ScopeOne_DriverHost` are internal too and are described in `ScopeOneCore/DRIVERHOST_PROTOCOL.md`. @@ -140,7 +140,7 @@ not send style-only changes. - Constant strings use `QStringLiteral`; text the user sees goes through `tr()`. - Our own code does not throw. MMCore, OpenCV and ONNX Runtime do, so catch their exceptions at the call site and turn them into an error value or a log message. -- Casts: `qobject_cast` for QObjects, `static_cast` otherwise. `dynamic_cast` only across plugin +- Casts: `qobject_cast` for QObjects, `static_cast` otherwise. `dynamic_cast` only across library interfaces. - Logging: `qWarning` for real problems. Do not leave `qDebug` behind. @@ -197,7 +197,7 @@ with a slash. and moving code are separate changes, because they hide the real change in review. Do not touch lines you are not changing. -**Build everything you touched.** Core, the application and the plugins all build before you +**Build everything you touched.** Core, the drivers and the application all build before you send a change, with no new warnings. A change in a header builds every part that includes it. **Run what you changed.** A change to behavior is run in the application, with a configuration @@ -221,8 +221,8 @@ a pinned version in `vcpkg.json`. **Core.** The version is `MAJOR.MINOR.MMDD`: `MAJOR.MINOR` comes from `VERSION`, the last part is the date of the last commit. Raise `MINOR` whenever a public SDK header changes. ScopeOne loads -a driver or plugin only when its Core `MAJOR.MINOR` matches, and drivers and processing plugins -each have their own `scopeOneApi` contract version. +a driver only when its Core `MAJOR.MINOR` matches, and drivers have their own `scopeOneApi` +contract version. **Dependencies are pinned.** vcpkg packages, Python (`==3.14.*`) and Python packages (`numpy==2.5.3`) use exact versions. Upgrade them together, on purpose. diff --git a/README.md b/README.md index eaacab1..c6f0ea3 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Deep learning is becoming an increasingly powerful tool for microscopy image res

### GPU-Accelerated Online Image Processing -Despite the deep learning, traditional image processing methods remain essential. GPU-accelerated image processing pipeline can be applied to live camera streams or recorded images, including filtering, background subtraction, FFT, and more. Users can customize image processing algorithms and parameters to suit their specific needs via the ScopeOne plugin system. +Despite the deep learning, traditional image processing methods remain essential. GPU-accelerated image processing pipeline can be applied to live camera streams or recorded images, including filtering, background subtraction, FFT, and more. Users can customize image processing algorithms and parameters to suit their specific needs with Python processing modules. ## 🚀 Quick Start ### For Users @@ -73,15 +73,15 @@ The expected layout is: ```text ScopeOne/ ScopeOneCore/ - include/scopeone/ Core API and external plugin contracts + include/scopeone/ Core API and the contracts of the application external/ mmCoreAndDevices/ ScopeWriter/ ``` -### Drivers and plugins +### Drivers and processing modules -ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `plugins/processing/python`. The CUDA and ONNX processing plugins are built and released with ScopeOne. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [plugins/README.md](plugins/README.md) how to write a Python processing module. +ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `processing/python`. The CUDA and ONNX processing modules are part of Core, which builds them when nvcc and ONNX Runtime are found. `-DSCOPEONE_ENABLE_CUDA=OFF` and `-DSCOPEONE_ENABLE_INFERENCE=OFF` build the standard Core without them. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [ScopeOneCore/python/scopeone/README.md](ScopeOneCore/python/scopeone/README.md#python-processing-modules) how to write a Python processing module. **Windows Build Steps:** @@ -93,13 +93,13 @@ C:/dev/vcpkg/bootstrap-vcpkg.bat $env:VCPKG_ROOT = "C:/dev/vcpkg" ``` -Build Core, plugins, and GUI with the repository script: +Build Core, drivers, and GUI with the repository script: ```powershell .\scripts\build.ps1 --configure ``` -Later builds only need `.\scripts\build.ps1`. Core, GUI, and plugins share `vcpkg_installed/`, and CMake installs the dependencies on the first configure. +Later builds only need `.\scripts\build.ps1`. Core, drivers, and GUI share `vcpkg_installed/`, and CMake installs the dependencies on the first configure. For a direct Core CMake configure: @@ -119,7 +119,7 @@ Run the built application: **Linux and macOS Build Steps:** -Micro-Manager is built from its own checkout because its native core and device adapters are not vcpkg dependencies. Everything else comes from vcpkg with the dynamic triplet of the host, so Qt is shared between the GUI, ScopeOneCore, and plugins. +Micro-Manager is built from its own checkout because its native core and device adapters are not vcpkg dependencies. Everything else comes from vcpkg with the dynamic triplet of the host, so Qt is shared between the GUI, ScopeOneCore and the drivers. 1. Install the system build tools. @@ -153,7 +153,7 @@ brew install \ ./scripts/build-unix.sh ``` -If vcpkg is missing, the script offers to download it into `$HOME/vcpkg` or a folder you type, and `--yes` takes the default folder without asking. It then builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the plugins and the GUI. Set `JOBS` to limit parallel jobs and `BUILD_TYPE` to change the CMake build type (default `Release`). Pass `--clean` to discard the previous build directories. +If vcpkg is missing, the script offers to download it into `$HOME/vcpkg` or a folder you type, and `--yes` takes the default folder without asking. It then builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. Set `JOBS` to limit parallel jobs and `BUILD_TYPE` to change the CMake build type (default `Release`). Pass `--clean` to discard the previous build directories. The executable is `build/ScopeOne`, and `config/MMConfig_demo.cfg` runs with the copied demo adapters. It loads Qt and OpenCV from `vcpkg_installed//lib`, so run it from the build tree. diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index cb6ecd6..df42463 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -72,7 +72,7 @@ set(CORE_SOURCES src/AcquisitionEngine.cpp src/DriverHostProviderProxy.cpp src/DaqDeviceManager.cpp - src/PluginManifest.cpp + src/DriverManifest.cpp src/HardwareRuntime.cpp src/MicroManagerProvider.cpp src/ImageOps.cpp @@ -112,8 +112,8 @@ set(CORE_SOURCES set(CORE_HEADERS include/scopeone/ExperimentDocument.h - include/scopeone/PluginManifest.h - include/scopeone/ProcessingPlugin.h + include/scopeone/DriverManifest.h + include/scopeone/ProcessingTypes.h include/scopeone/ScanImageAssembler.h include/scopeone/SharedFrame.h include/scopeone/DhmReconstruction.h @@ -217,6 +217,113 @@ target_compile_definitions(ScopeOneCore PRIVATE SCOPEONE_CORE_EXPORTS SCOPEONE_CORE_VERSION_STRING="${PROJECT_VERSION}" ) + +# The CUDA and ONNX Runtime processing modules are part of Core when their toolkits are found. +# Turning them off builds the plain CPU Core, which neither links nor ships CUDA or ONNX Runtime. +set(_with_cuda OFF) +set(_with_inference OFF) +option(SCOPEONE_ENABLE_CUDA "Build the CUDA processing modules into Core when nvcc is available" ON) +option(SCOPEONE_ENABLE_INFERENCE "Build the ONNX Runtime processing module into Core when ONNX Runtime is available" ON) +if (SCOPEONE_ENABLE_CUDA) + find_program(SCOPEONE_CUDA_NVCC_EXECUTABLE nvcc) +else () + unset(SCOPEONE_CUDA_NVCC_EXECUTABLE CACHE) +endif () +if (SCOPEONE_CUDA_NVCC_EXECUTABLE) + set(CMAKE_CUDA_COMPILER "${SCOPEONE_CUDA_NVCC_EXECUTABLE}" CACHE FILEPATH + "CUDA compiler selected by the environment" FORCE) + get_filename_component(_cuda_toolkit_root "${CMAKE_CUDA_COMPILER}" DIRECTORY) + get_filename_component(_cuda_toolkit_root "${_cuda_toolkit_root}" DIRECTORY) + set(CUDAToolkit_ROOT "${_cuda_toolkit_root}" CACHE PATH + "CUDA toolkit selected by the compiler" FORCE) + enable_language(CUDA) + find_package(CUDAToolkit REQUIRED) + set(SCOPEONE_CUDA_ARCHITECTURES "50;75;80;86;89" CACHE STRING "Target CUDA architectures") + target_sources(ScopeOneCore PRIVATE + src/cuda/CudaKernels.cu + src/cuda/CudaDevice.cpp + src/cuda/CudaFrequencyFilterModule.cpp + src/cuda/CudaGaussianBlurModule.cpp + src/cuda/CudaProcessingRegistration.cpp + src/cuda/CudaRealImageModule.cpp + src/cuda/GpuComplexFrame.cpp + src/cuda/GpuRealFrame.cpp + internal/cuda/CudaDevice.h + internal/cuda/CudaFrequencyFilterModule.h + internal/cuda/CudaGaussianBlurModule.h + internal/cuda/CudaKernelLaunch.h + internal/cuda/CudaProcessingRegistration.h + internal/cuda/CudaRealImageModule.h + internal/cuda/GpuComplexFrame.h + internal/cuda/GpuRealFrame.h + ) + set_source_files_properties(src/cuda/CudaKernels.cu PROPERTIES SKIP_PRECOMPILE_HEADERS ON) + set_target_properties(ScopeOneCore PROPERTIES + CUDA_ARCHITECTURES "${SCOPEONE_CUDA_ARCHITECTURES}" + CUDA_SEPARABLE_COMPILATION OFF + ) + target_link_libraries(ScopeOneCore PRIVATE CUDA::cudart CUDA::cufft) + target_compile_definitions(ScopeOneCore PRIVATE SCOPEONE_WITH_CUDA) + set(_with_cuda ON) + if (WIN32) + file(GLOB _cuda_runtime_dlls + "${CUDAToolkit_BIN_DIR}/cudart64_*.dll" + "${CUDAToolkit_BIN_DIR}/cufft64_*.dll" + ) + list(APPEND SCOPEONE_RUNTIME_DLLS ${_cuda_runtime_dlls}) + endif () +endif () + +if (SCOPEONE_ENABLE_INFERENCE) + set(ONNXRUNTIME_ROOT + "${CMAKE_CURRENT_SOURCE_DIR}/external/onnxruntime-win-x64-gpu_cuda12-1.30.0" + CACHE PATH "ONNX Runtime SDK root" + ) + find_path(ONNXRUNTIME_INCLUDE_DIR + NAMES onnxruntime_cxx_api.h + PATHS "${ONNXRUNTIME_ROOT}/include" + NO_DEFAULT_PATH + ) + find_library(ONNXRUNTIME_LIBRARY + NAMES onnxruntime + PATHS "${ONNXRUNTIME_ROOT}/lib" + NO_DEFAULT_PATH + ) +endif () +if (SCOPEONE_ENABLE_INFERENCE AND ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRARY) + target_sources(ScopeOneCore PRIVATE + src/inference/InferenceProcessingRegistration.cpp + src/inference/OnnxInferenceModule.cpp + src/inference/OnnxRuntime.cpp + src/inference/OnnxSession.cpp + internal/inference/ExecutionProvider.h + internal/inference/InferenceProcessingRegistration.h + internal/inference/OnnxInferenceModule.h + internal/inference/OnnxInternal.h + internal/inference/OnnxRuntime.h + internal/inference/OnnxSession.h + internal/inference/TensorInfo.h + ) + target_include_directories(ScopeOneCore PRIVATE "${ONNXRUNTIME_INCLUDE_DIR}") + target_link_libraries(ScopeOneCore PRIVATE "${ONNXRUNTIME_LIBRARY}") + target_compile_definitions(ScopeOneCore PRIVATE SCOPEONE_WITH_INFERENCE) + set(_with_inference ON) + if (WIN32) + file(GLOB _onnxruntime_dlls + "${ONNXRUNTIME_ROOT}/bin/onnxruntime*.dll" + "${ONNXRUNTIME_ROOT}/lib/onnxruntime*.dll" + ) + if (NOT _onnxruntime_dlls) + message(FATAL_ERROR "No ONNX Runtime DLLs found under ${ONNXRUNTIME_ROOT}") + endif () + list(APPEND SCOPEONE_RUNTIME_DLLS ${_onnxruntime_dlls}) + endif () +elseif (SCOPEONE_ENABLE_INFERENCE) + message(STATUS "ONNX Runtime was not found under ${ONNXRUNTIME_ROOT}; building Core without ONNX inference") +endif () +# What this configure built in, for the build scripts to report +set(SCOPEONE_BUILT_WITH_CUDA "${_with_cuda}" CACHE INTERNAL "") +set(SCOPEONE_BUILT_WITH_INFERENCE "${_with_inference}" CACHE INTERNAL "") target_include_directories(ScopeOneCore PUBLIC $ @@ -274,7 +381,7 @@ add_custom_command(TARGET ScopeOneCore POST_BUILD add_executable(ScopeOne_DriverHost src/DriverHostMain.cpp src/HardwareStreamCodec.cpp - src/PluginManifest.cpp + src/DriverManifest.cpp src/StreamRing.cpp ) if (UNIX AND NOT APPLE) @@ -305,17 +412,20 @@ target_link_directories(ScopeOne_DriverHost PRIVATE ${MMCORE_BIN_DIR}) scopeone_add_runtime_copy_commands(ScopeOne_DriverHost $) -# Fast Release build -# if (MSVC) -# target_compile_options(ScopeOneCore PRIVATE -# $<$:/MP> -# ) -# endif () +# The Visual Studio generator compiles the sources of one project one after another unless the +# compiler runs them in parallel itself. Only for C++, nvcc does not take /MP. +if (MSVC) + target_compile_options(ScopeOneCore PRIVATE $<$:/MP>) + target_compile_options(ScopeOne_DriverHost PRIVATE /MP) + target_compile_options(ScopeOneDriverSDK PRIVATE /MP) +endif () # LTO Release build if (MSVC) + # Only for C++, nvcc does not take these flags + set(_release_cxx "$,$>") target_compile_options(ScopeOneCore PRIVATE - $<$:/GL /fp:fast /arch:AVX2> + "$<${_release_cxx}:/GL>" "$<${_release_cxx}:/fp:fast>" "$<${_release_cxx}:/arch:AVX2>" ) target_link_options(ScopeOneCore PRIVATE $<$:/LTCG> @@ -368,7 +478,7 @@ install(EXPORT ScopeOneCoreTargets install(FILES "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneCoreConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneCoreConfigVersion.cmake" - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/ScopeOnePlugin.cmake" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/ScopeOneDriver.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeOneCore" ) diff --git a/ScopeOneCore/DRIVERHOST_PROTOCOL.md b/ScopeOneCore/DRIVERHOST_PROTOCOL.md index 054dcf1..e1a3c9d 100644 --- a/ScopeOneCore/DRIVERHOST_PROTOCOL.md +++ b/ScopeOneCore/DRIVERHOST_PROTOCOL.md @@ -16,7 +16,7 @@ ScopeOne_DriverHost --provider --driver --hostKey --launcherPid [--option ] ``` -`--option` carries the options from Drivers and Plugins. The host loads the driver, creates the +`--option` carries the options from the Drivers dialog. The host loads the driver, creates the devices and listens on the local socket `ScopeOne.DriverHost..ctrl` (a named pipe on Windows, a Unix domain socket elsewhere). ScopeOne connects to it, and the host exits when the process `--launcherPid` is gone. @@ -144,8 +144,11 @@ skipped by the writer, so the reader never sees a frame change under it. Each `StartSignal` and `StartDaq` gets a new ring in shared memory under the `sharedMemoryKey` of its response, sized for about two seconds of that acquisition. The host writes the chunks of the -source or session into it, and ScopeOne reads them after each `StreamAvailable`. The layout is -`ScopeOneCore/internal/StreamRing.h`. +source or session into it, and ScopeOne reads them after each `StreamAvailable`. After +`StopSignal` or `StopDaq`, both ends keep the ring until the `SignalState` or `DaqState` event +with `Idle` or `Error`, which follows the last record. ScopeOne reads what is left and both ends +then release the ring. A source that ends with `Error` releases its ring without a stop. The +layout is `ScopeOneCore/internal/StreamRing.h`. The ring begins with a control block of three 64-byte lines: `magic` u32 and `version` u32 and `capacity` u64 in the first, `writeCursor` u64 in the second and `readCursor` u64 in the third. diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index cd64a93..81004db 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -8,10 +8,10 @@ The desktop app consumes `ScopeOneCore` through its installed CMake package. ```text ScopeOneCore/ -|-- include/scopeone/ Core facade and public models used by the app and the built-in plugins +|-- include/scopeone/ Core facade and public models used by the app |-- sdk/ Driver SDK: its headers and the small library behind them |-- internal/ Private headers used only while building ScopeOneCore -|-- src/ C++ implementations for both public and private types +|-- src/ C++ implementations, with the CUDA and ONNX modules in src/cuda and src/inference |-- python/scopeone/ External Python client for a running ScopeOne app |-- cmake/ CMake package configuration templates |-- external/ Third-party dependencies and device adapter sources @@ -19,7 +19,7 @@ ScopeOneCore/ `-- install/ Generated local installation consumed by the desktop app ``` -`include/scopeone` contains the Core facade, Core-owned public models and the processing plugin contract: +`include/scopeone` contains the Core facade, Core-owned public models and the processing module types: ```cpp #include @@ -51,7 +51,7 @@ Use this placement rule: | `scopeone::core::internal` | Core-only managers, processing modules and built-in devices | `CameraManager`, `MMCoreManager`, `RecordingManager`, `IscatModule`, `SignalSourceManager` | | `scopeone::core::internal::driverhost` | Shared DriverHost message framing | Versioned request, response and event envelopes | | `scopeone::ui` | Desktop application widgets and UI coordination outside this library | `MainWindow`, `PreviewWidget`, `DhmTool`, `ToolRegistry` | -| `scopeone::plugins::` | The CUDA processing plugin. The ONNX plugin stays in `scopeone::inference`, the namespace of its library | `scopeone::plugins::cuda` | +| `scopeone::cuda`, `scopeone::inference` | The CUDA and ONNX processing modules in `src/cuda` and `src/inference` | `scopeone::cuda` | Code in `src` that implements a public type remains in `scopeone::core`. Code that implements an `internal` header remains in `scopeone::core::internal`. The Python package named `scopeone` is an external client package and is not an embedded form of the C++ namespace. @@ -94,11 +94,11 @@ Outputs: - `build/Release/ScopeOneCore.dll` - `build/Release/ScopeOneCore.lib` - `build/Release/ScopeOne_DriverHost.exe` -- Drivers and the CUDA and ONNX plugins are built by the `plugins` project. +- Drivers are built by the `drivers` project. - `build/ScopeOneCoreConfig.cmake` - `install/bin/ScopeOneCore.dll` - `install/bin/ScopeOne_DriverHost.exe` -- Drivers are installed under `drivers`, and the CUDA and ONNX processing plugins under `plugins/processing`. +- Drivers are installed under `drivers`. - `install/lib/cmake/ScopeOneCore/ScopeOneCoreConfig.cmake` @@ -109,22 +109,22 @@ The installed headers in `include/scopeone` and `include/sdk/scopeone` are the s - `ScopeOneCore.h` provides the main hardware, acquisition, processing, recording and frame-graph facade. - `ProcessingPipeline.h`, `ExperimentDocument.h` and `ImageSceneModel.h` are Core-owned runtime models and services. - `DriverSDK.h` includes the headers of the public driver contract: `CameraProvider.h`, `DaqDevice.h`, `DaqProvider.h`, `Driver.h`, `HardwareCapabilities.h`, `HardwareProvider.h`, `HardwareTypes.h`, `ImageFrame.h`, `SignalSource.h` and `SignalSourceProvider.h`. The `scopeone::DriverSDK` CMake target provides them. -- `ProcessingPlugin.h` is the interface of the CUDA and ONNX processing plugins. It is internal to this repository. -- `PluginManifest.h` is how ScopeOne discovers and enables drivers and plugins. `ScanImageAssembler.h`, `SharedFrame.h` and `DhmReconstruction.h` serve the application. None of them are driver contracts. +- `ProcessingTypes.h` holds the processing module base class and the descriptors of modules and their parameters. +- `DriverManifest.h` is how ScopeOne discovers and enables drivers. `ScanImageAssembler.h`, `SharedFrame.h` and `DhmReconstruction.h` serve the application. None of them are driver contracts. - `scopeone_core_export.h` supplies DLL import and export declarations and is normally included indirectly. External code should enter through these headers and `scopeone::core::ScopeOneCore`. Internal managers are implementation details and must not become alternate access paths. Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `CameraProvider::FrameSink`. Core loads a driver with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider, using the native camera path for one camera and isolated DriverHost camera processes for multiple cameras. -## Driver and Plugin Boundaries +## Driver and Processing Boundaries - `drivers` contains the drivers, among them NI-DAQmx and the example camera and PMT. Each provider runs in an isolated DriverHost process, so a vendor library such as NI-DAQmx is never loaded into ScopeOne. Micro-Manager Device Adapters remain under Micro-Manager and are not wrapped as ScopeOne drivers. -- Python processing modules are scripts in the `python` folder of a processing plugin directory. Core runs each module of a pipeline in its own Python process. -- The CUDA and ONNX processing modules are processing plugins, because they link CUDA and ONNX Runtime, which Core does not. Every other processing method, the PTU file signal source and the DHM reconstruction are part of Core. +- Python processing modules are scripts in `processing/python`, beside the executable or in the application data directory. Core runs each module of a pipeline in its own Python process. +- The CUDA and ONNX processing modules are part of Core when `SCOPEONE_ENABLE_CUDA` and `SCOPEONE_ENABLE_INFERENCE` are on and nvcc and ONNX Runtime are found. With both off, Core neither links nor ships CUDA or ONNX Runtime. Every processing method, the PTU file signal source and the DHM reconstruction are part of Core. - The tool windows (Scale, Stage Mosaic, Particle Detection, DHM Reconstruction, Scanning and DAQ Control, Signal Monitor) are part of the application. They use the `ScopeOneCore` facade, and `ScopeOneToolContext` in `src/tools/ToolContext.h` gives them the desktop operations such as showing layers. -Drivers outside this repository consume the exported `scopeone::DriverSDK` CMake target. Every driver and plugin manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`; the processing and driver contracts are versioned independently and incompatible manifests are rejected before the library instance is created. +Drivers outside this repository consume the exported `scopeone::DriverSDK` CMake target. Every driver manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`, and incompatible manifests are rejected before the library instance is created. ## Processing Data Flow diff --git a/ScopeOneCore/cmake/ScopeOneCoreConfig.cmake.in b/ScopeOneCore/cmake/ScopeOneCoreConfig.cmake.in index 45cdd07..3b241be 100644 --- a/ScopeOneCore/cmake/ScopeOneCoreConfig.cmake.in +++ b/ScopeOneCore/cmake/ScopeOneCoreConfig.cmake.in @@ -5,6 +5,6 @@ include(CMakeFindDependencyMacro) find_dependency(Qt6 REQUIRED COMPONENTS Core Gui) include("${CMAKE_CURRENT_LIST_DIR}/ScopeOneCoreTargets.cmake") -include("${CMAKE_CURRENT_LIST_DIR}/ScopeOnePlugin.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/ScopeOneDriver.cmake") check_required_components(ScopeOneCore) diff --git a/ScopeOneCore/cmake/ScopeOnePlugin.cmake b/ScopeOneCore/cmake/ScopeOneDriver.cmake similarity index 86% rename from ScopeOneCore/cmake/ScopeOnePlugin.cmake rename to ScopeOneCore/cmake/ScopeOneDriver.cmake index 8261b54..8246e81 100644 --- a/ScopeOneCore/cmake/ScopeOnePlugin.cmake +++ b/ScopeOneCore/cmake/ScopeOneDriver.cmake @@ -9,8 +9,8 @@ # Q_PLUGIN_METADATA(IID ScopeOneDriver_iid FILE "scopeone_driver.json") # Writes the generated manifest from the manifest beside the target's CMakeLists, adding the -# ScopeOneCore version the target is built against. ScopeOne loads only drivers and plugins built -# against its own Core, because they share C++ types with it. +# ScopeOneCore version the target is built against. ScopeOne loads only drivers built against its +# own Core, because they share C++ types with it. function(scopeone_write_manifest target source generated) set(_source "${CMAKE_CURRENT_SOURCE_DIR}/${source}") set(_generated "${CMAKE_CURRENT_BINARY_DIR}/${generated}") @@ -21,11 +21,6 @@ function(scopeone_write_manifest target source generated) target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") endfunction() -# The manifest of a built-in processing plugin: plugin.json to scopeone_plugin.json -function(scopeone_plugin_manifest target) - scopeone_write_manifest(${target} plugin.json scopeone_plugin.json) -endfunction() - # Builds the target as a driver, from driver.json to scopeone_driver.json, and installs it in the # drivers directory where ScopeOne looks for drivers function(scopeone_add_driver target) diff --git a/ScopeOneCore/include/scopeone/DriverManifest.h b/ScopeOneCore/include/scopeone/DriverManifest.h new file mode 100644 index 0000000..4ba27e9 --- /dev/null +++ b/ScopeOneCore/include/scopeone/DriverManifest.h @@ -0,0 +1,42 @@ +#pragma once + +#include "scopeone/scopeone_core_export.h" + +#include +#include +#include +#include + +namespace scopeone::core +{ + // The manifest of a driver, which ScopeOne reads from the library without loading it and + // ScopeOne_DriverHost loads for its device + struct DriverManifest + { + QString id; + QString name; + QString version; + bool autoLoad{false}; + QJsonObject metadata; + }; + + struct DiscoveredDriver + { + DriverManifest manifest; + QString path; + QString interfaceId; + QJsonObject metadata; + QString error; + }; + + inline constexpr int ScopeOneDriverApiVersion = 1; + + // The drivers directory beside the application and in the user data directory + SCOPEONE_CORE_EXPORT QStringList driverDirectories(); + SCOPEONE_CORE_EXPORT QString driverSettingsKey(const QString& driverId, const QString& name); + SCOPEONE_CORE_EXPORT bool driverEnabled(const DriverManifest& manifest); + SCOPEONE_CORE_EXPORT QList discoverDrivers(const QStringList& directories = {}); + SCOPEONE_CORE_EXPORT bool parseDriverManifest(const QJsonObject& metadata, + DriverManifest& manifest, + QString* errorMessage = nullptr); +} diff --git a/ScopeOneCore/include/scopeone/PluginManifest.h b/ScopeOneCore/include/scopeone/PluginManifest.h deleted file mode 100644 index 630b563..0000000 --- a/ScopeOneCore/include/scopeone/PluginManifest.h +++ /dev/null @@ -1,59 +0,0 @@ -#pragma once - -#include "scopeone/scopeone_core_export.h" - -#include -#include -#include -#include - -namespace scopeone::core -{ - // A library ScopeOne discovers and loads: a built-in processing plugin, or a driver that - // ScopeOne_DriverHost loads for its device - enum class PluginKind - { - Processing, - Driver - }; - - struct PluginManifest - { - QString id; - QString name; - QString version; - PluginKind kind{PluginKind::Processing}; - bool autoLoad{false}; - QJsonObject metadata; - }; - - struct DiscoveredPlugin - { - PluginManifest manifest; - PluginKind expectedKind{PluginKind::Processing}; - QString path; - QString interfaceId; - QJsonObject metadata; - QString error; - }; - - inline constexpr int ScopeOneProcessingPluginApiVersion = 2; - inline constexpr int ScopeOneDriverApiVersion = 1; - - SCOPEONE_CORE_EXPORT QString pluginKindName(PluginKind kind); - SCOPEONE_CORE_EXPORT int pluginApiVersion(PluginKind kind); - // The directory of the kind relative to the application or the user data directory - SCOPEONE_CORE_EXPORT QString pluginDirectoryName(PluginKind kind); - SCOPEONE_CORE_EXPORT QStringList pluginInterfaceIds(PluginKind kind); - SCOPEONE_CORE_EXPORT QStringList pluginDirectories(PluginKind kind); - SCOPEONE_CORE_EXPORT QString pluginSettingsKey(const QString& pluginId, - const QString& name); - SCOPEONE_CORE_EXPORT bool pluginEnabled(const PluginManifest& manifest); - SCOPEONE_CORE_EXPORT QList discoverPlugins( - PluginKind kind, - const QStringList& directories = {}); - SCOPEONE_CORE_EXPORT bool parsePluginManifest(const QJsonObject& metadata, - PluginKind expectedKind, - PluginManifest& manifest, - QString* errorMessage = nullptr); -} diff --git a/ScopeOneCore/include/scopeone/ProcessingPipeline.h b/ScopeOneCore/include/scopeone/ProcessingPipeline.h index 6e90b51..6960208 100644 --- a/ScopeOneCore/include/scopeone/ProcessingPipeline.h +++ b/ScopeOneCore/include/scopeone/ProcessingPipeline.h @@ -1,6 +1,6 @@ #pragma once -#include "scopeone/ProcessingPlugin.h" +#include "scopeone/ProcessingTypes.h" #include "scopeone/scopeone_core_export.h" #include diff --git a/ScopeOneCore/include/scopeone/ProcessingPlugin.h b/ScopeOneCore/include/scopeone/ProcessingTypes.h similarity index 90% rename from ScopeOneCore/include/scopeone/ProcessingPlugin.h rename to ScopeOneCore/include/scopeone/ProcessingTypes.h index 7178f2c..34f6b38 100644 --- a/ScopeOneCore/include/scopeone/ProcessingPlugin.h +++ b/ScopeOneCore/include/scopeone/ProcessingTypes.h @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -145,16 +144,4 @@ namespace scopeone::core private: bool m_enabled{true}; }; - - class ProcessingPlugin - { - public: - virtual ~ProcessingPlugin() = default; - - virtual QList processingModules() const = 0; - virtual std::unique_ptr createProcessingModule(const QString& moduleId) = 0; - }; } - -#define ScopeOneProcessingPlugin_iid "org.scopeone.ProcessingPlugin/2.0" -Q_DECLARE_INTERFACE(scopeone::core::ProcessingPlugin, ScopeOneProcessingPlugin_iid) diff --git a/ScopeOneCore/include/scopeone/ScopeOneCore.h b/ScopeOneCore/include/scopeone/ScopeOneCore.h index a89c02c..0d0b9e0 100644 --- a/ScopeOneCore/include/scopeone/ScopeOneCore.h +++ b/ScopeOneCore/include/scopeone/ScopeOneCore.h @@ -26,7 +26,7 @@ #include "scopeone/HardwareTypes.h" #include "scopeone/HardwareProvider.h" #include "scopeone/ImageFrame.h" -#include "scopeone/ProcessingPlugin.h" +#include "scopeone/ProcessingTypes.h" #include "scopeone/ProcessingPipeline.h" #include "scopeone/RecordingSession.h" #include "scopeone/SignalSource.h" diff --git a/ScopeOneCore/include/scopeone/scopeone_core_export.h b/ScopeOneCore/include/scopeone/scopeone_core_export.h index cb516bf..674e003 100644 --- a/ScopeOneCore/include/scopeone/scopeone_core_export.h +++ b/ScopeOneCore/include/scopeone/scopeone_core_export.h @@ -1,7 +1,7 @@ #pragma once // SCOPEONE_CORE_STATIC compiles a Core source into another binary, as DriverHost does with the -// plugin manifest, without importing it from the Core library +// driver manifest, without importing it from the Core library #if defined(SCOPEONE_CORE_STATIC) # define SCOPEONE_CORE_EXPORT #elif defined(_WIN32) || defined(__CYGWIN__) diff --git a/ScopeOneCore/internal/DriverHostProviderProxy.h b/ScopeOneCore/internal/DriverHostProviderProxy.h index d164a97..9219363 100644 --- a/ScopeOneCore/internal/DriverHostProviderProxy.h +++ b/ScopeOneCore/internal/DriverHostProviderProxy.h @@ -8,7 +8,7 @@ namespace scopeone::core::internal { HardwareProviderPtr createDriverHostProviderProxy(const QString& providerId, - const QString& pluginPath, + const QString& driverPath, const QJsonObject& options, QString* errorMessage = nullptr); } diff --git a/ScopeOneCore/internal/ProcessingModule.h b/ScopeOneCore/internal/ProcessingModule.h index 8d790c6..630aaa8 100644 --- a/ScopeOneCore/internal/ProcessingModule.h +++ b/ScopeOneCore/internal/ProcessingModule.h @@ -1,6 +1,6 @@ #pragma once -#include "scopeone/ProcessingPlugin.h" +#include "scopeone/ProcessingTypes.h" namespace scopeone::core::internal { diff --git a/ScopeOneCore/internal/ProcessingModuleRegistry.h b/ScopeOneCore/internal/ProcessingModuleRegistry.h index 7fd9198..3950c2c 100644 --- a/ScopeOneCore/internal/ProcessingModuleRegistry.h +++ b/ScopeOneCore/internal/ProcessingModuleRegistry.h @@ -1,14 +1,11 @@ #pragma once -#include "scopeone/ProcessingPlugin.h" +#include "scopeone/ProcessingTypes.h" #include #include #include #include -#include - -class QPluginLoader; namespace scopeone::core::internal { @@ -24,7 +21,6 @@ namespace scopeone::core::internal QList descriptors() const; ProcessingModuleDescriptor descriptor(const QString& moduleId) const; std::unique_ptr create(const QString& moduleId) const; - QStringList loadPlugins(const QString& directoryPath); private: struct Entry @@ -35,6 +31,5 @@ namespace scopeone::core::internal QHash m_entries; QStringList m_order; - std::vector> m_pluginLoaders; }; } diff --git a/ScopeOneCore/internal/PythonProcessingModule.h b/ScopeOneCore/internal/PythonProcessingModule.h index 0bcb018..4d34095 100644 --- a/ScopeOneCore/internal/PythonProcessingModule.h +++ b/ScopeOneCore/internal/PythonProcessingModule.h @@ -8,9 +8,9 @@ namespace scopeone::core::internal { class ProcessingModuleRegistry; - // Registers the modules of every script in the python folder of a processing plugin + // Registers the modules of every script in the python folder of a processing // directory. Each module of a pipeline then talks to its own Python process, one frame at a // time, over stdin and stdout. Returns the scripts and modules that could not be registered. QStringList registerPythonProcessingModules(ProcessingModuleRegistry& registry, - const QString& pluginDirectory); + const QString& processingDirectory); } diff --git a/ScopeOneCore/internal/SignalSourceManager.h b/ScopeOneCore/internal/SignalSourceManager.h index a1769fa..bdd728a 100644 --- a/ScopeOneCore/internal/SignalSourceManager.h +++ b/ScopeOneCore/internal/SignalSourceManager.h @@ -49,7 +49,7 @@ namespace scopeone::core::internal }; // The signal sources of Core and of the drivers under one list. The chunks of a - // plugin source arrive on a thread of its provider and are passed on in this object's thread. + // driver source arrive on a thread of its provider and are passed on in this object's thread. class SignalSourceManager final : public QObject { Q_OBJECT diff --git a/ScopeOneCore/internal/cuda/CudaDevice.h b/ScopeOneCore/internal/cuda/CudaDevice.h new file mode 100644 index 0000000..beb4675 --- /dev/null +++ b/ScopeOneCore/internal/cuda/CudaDevice.h @@ -0,0 +1,8 @@ +#pragma once + + +namespace scopeone::cuda +{ + bool isCudaDeviceAvailable(); + int cudaDeviceCount(); +} diff --git a/ScopeOneCuda/src/CudaFrequencyFilterModule.h b/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h similarity index 90% rename from ScopeOneCuda/src/CudaFrequencyFilterModule.h rename to ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h index 58ebb73..6a3045a 100644 --- a/ScopeOneCuda/src/CudaFrequencyFilterModule.h +++ b/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h @@ -1,11 +1,11 @@ #pragma once -#include "scopeone/cuda/CudaRealImageModule.h" -#include "scopeone/cuda/GpuComplexFrame.h" +#include "internal/cuda/CudaRealImageModule.h" +#include "internal/cuda/GpuComplexFrame.h" #include -namespace scopeone::plugins::cuda +namespace scopeone::cuda { class CudaFrequencyFilterModule final : public scopeone::cuda::CudaRealImageModule { diff --git a/ScopeOneCuda/src/CudaGaussianBlurModule.h b/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h similarity index 89% rename from ScopeOneCuda/src/CudaGaussianBlurModule.h rename to ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h index 896da52..de4ae6d 100644 --- a/ScopeOneCuda/src/CudaGaussianBlurModule.h +++ b/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h @@ -1,8 +1,8 @@ #pragma once -#include "scopeone/cuda/CudaRealImageModule.h" +#include "internal/cuda/CudaRealImageModule.h" -namespace scopeone::plugins::cuda +namespace scopeone::cuda { class CudaGaussianBlurModule final : public scopeone::cuda::CudaRealImageModule { diff --git a/ScopeOneCuda/include/scopeone/cuda/CudaKernelLaunch.h b/ScopeOneCore/internal/cuda/CudaKernelLaunch.h similarity index 87% rename from ScopeOneCuda/include/scopeone/cuda/CudaKernelLaunch.h rename to ScopeOneCore/internal/cuda/CudaKernelLaunch.h index 069aae9..e602957 100644 --- a/ScopeOneCuda/include/scopeone/cuda/CudaKernelLaunch.h +++ b/ScopeOneCore/internal/cuda/CudaKernelLaunch.h @@ -1,12 +1,11 @@ #pragma once -#include "scopeone/cuda/CudaExport.h" #include namespace scopeone::cuda::detail { - SCOPEONE_CUDA_EXPORT bool convertToFloat(const void* source, + bool convertToFloat(const void* source, int sourceStride, int width, int height, @@ -14,7 +13,7 @@ namespace scopeone::cuda::detail void* destination, std::size_t destinationPitchBytes); - SCOPEONE_CUDA_EXPORT bool convertFromFloat(const void* source, + bool convertFromFloat(const void* source, std::size_t sourcePitchBytes, void* destination, int destinationStride, @@ -22,7 +21,7 @@ namespace scopeone::cuda::detail int height, int destinationBytesPerPixel); - SCOPEONE_CUDA_EXPORT bool launchGaussian(const void* input, + bool launchGaussian(const void* input, std::size_t inputPitchBytes, void* output, std::size_t outputPitchBytes, @@ -31,7 +30,7 @@ namespace scopeone::cuda::detail int kernelSize, float sigma); - SCOPEONE_CUDA_EXPORT bool launchFrequencyFilter(const void* input, + bool launchFrequencyFilter(const void* input, void* output, void* spectrum, int width, diff --git a/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h b/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h new file mode 100644 index 0000000..fe8f73c --- /dev/null +++ b/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h @@ -0,0 +1,12 @@ +#pragma once + +namespace scopeone::core::internal +{ + class ProcessingModuleRegistry; +} + +namespace scopeone::cuda +{ + // Registers the CUDA processing modules, built into Core when SCOPEONE_ENABLE_CUDA is on + void registerCudaProcessingModules(core::internal::ProcessingModuleRegistry& registry); +} diff --git a/ScopeOneCuda/include/scopeone/cuda/CudaRealImageModule.h b/ScopeOneCore/internal/cuda/CudaRealImageModule.h similarity index 80% rename from ScopeOneCuda/include/scopeone/cuda/CudaRealImageModule.h rename to ScopeOneCore/internal/cuda/CudaRealImageModule.h index 96840e2..3f49d36 100644 --- a/ScopeOneCuda/include/scopeone/cuda/CudaRealImageModule.h +++ b/ScopeOneCore/internal/cuda/CudaRealImageModule.h @@ -1,12 +1,11 @@ #pragma once -#include "scopeone/ProcessingPlugin.h" -#include "scopeone/cuda/CudaExport.h" -#include "scopeone/cuda/GpuRealFrame.h" +#include "scopeone/ProcessingTypes.h" +#include "internal/cuda/GpuRealFrame.h" namespace scopeone::cuda { - class SCOPEONE_CUDA_EXPORT CudaRealImageModule + class CudaRealImageModule : public scopeone::core::ProcessingModule { public: diff --git a/ScopeOneCuda/include/scopeone/cuda/GpuComplexFrame.h b/ScopeOneCore/internal/cuda/GpuComplexFrame.h similarity index 90% rename from ScopeOneCuda/include/scopeone/cuda/GpuComplexFrame.h rename to ScopeOneCore/internal/cuda/GpuComplexFrame.h index fc1c04f..9a9af2d 100644 --- a/ScopeOneCuda/include/scopeone/cuda/GpuComplexFrame.h +++ b/ScopeOneCore/internal/cuda/GpuComplexFrame.h @@ -1,12 +1,11 @@ #pragma once -#include "scopeone/cuda/CudaExport.h" #include namespace scopeone::cuda { - class SCOPEONE_CUDA_EXPORT GpuComplexFrame + class GpuComplexFrame { public: GpuComplexFrame() = default; diff --git a/ScopeOneCuda/include/scopeone/cuda/GpuRealFrame.h b/ScopeOneCore/internal/cuda/GpuRealFrame.h similarity index 94% rename from ScopeOneCuda/include/scopeone/cuda/GpuRealFrame.h rename to ScopeOneCore/internal/cuda/GpuRealFrame.h index fefc115..a9d499b 100644 --- a/ScopeOneCuda/include/scopeone/cuda/GpuRealFrame.h +++ b/ScopeOneCore/internal/cuda/GpuRealFrame.h @@ -1,7 +1,6 @@ #pragma once #include "scopeone/ImageFrame.h" -#include "scopeone/cuda/CudaExport.h" #include @@ -13,7 +12,7 @@ namespace scopeone::cuda Contiguous }; - class SCOPEONE_CUDA_EXPORT GpuRealFrame + class GpuRealFrame { public: GpuRealFrame() = default; diff --git a/ScopeOneInference/include/scopeone/inference/ExecutionProvider.h b/ScopeOneCore/internal/inference/ExecutionProvider.h similarity index 100% rename from ScopeOneInference/include/scopeone/inference/ExecutionProvider.h rename to ScopeOneCore/internal/inference/ExecutionProvider.h diff --git a/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h b/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h new file mode 100644 index 0000000..ca86ead --- /dev/null +++ b/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h @@ -0,0 +1,12 @@ +#pragma once + +namespace scopeone::core::internal +{ + class ProcessingModuleRegistry; +} + +namespace scopeone::inference +{ + // Registers the ONNX Runtime processing modules, built into Core when SCOPEONE_ENABLE_INFERENCE is on + void registerInferenceProcessingModules(core::internal::ProcessingModuleRegistry& registry); +} diff --git a/ScopeOneInference/src/OnnxInferenceModule.h b/ScopeOneCore/internal/inference/OnnxInferenceModule.h similarity index 87% rename from ScopeOneInference/src/OnnxInferenceModule.h rename to ScopeOneCore/internal/inference/OnnxInferenceModule.h index e83ca94..06de074 100644 --- a/ScopeOneInference/src/OnnxInferenceModule.h +++ b/ScopeOneCore/internal/inference/OnnxInferenceModule.h @@ -1,8 +1,8 @@ #pragma once -#include "scopeone/ProcessingPlugin.h" -#include "scopeone/inference/OnnxRuntime.h" -#include "scopeone/inference/OnnxSession.h" +#include "scopeone/ProcessingTypes.h" +#include "internal/inference/OnnxRuntime.h" +#include "internal/inference/OnnxSession.h" #include diff --git a/ScopeOneInference/src/OnnxInternal.h b/ScopeOneCore/internal/inference/OnnxInternal.h similarity index 66% rename from ScopeOneInference/src/OnnxInternal.h rename to ScopeOneCore/internal/inference/OnnxInternal.h index 9c219e3..890e1da 100644 --- a/ScopeOneInference/src/OnnxInternal.h +++ b/ScopeOneCore/internal/inference/OnnxInternal.h @@ -1,7 +1,7 @@ #pragma once -#include "scopeone/inference/OnnxRuntime.h" -#include "scopeone/inference/OnnxSession.h" +#include "internal/inference/OnnxRuntime.h" +#include "internal/inference/OnnxSession.h" #include diff --git a/ScopeOneInference/include/scopeone/inference/OnnxRuntime.h b/ScopeOneCore/internal/inference/OnnxRuntime.h similarity index 82% rename from ScopeOneInference/include/scopeone/inference/OnnxRuntime.h rename to ScopeOneCore/internal/inference/OnnxRuntime.h index d11ce44..a88701d 100644 --- a/ScopeOneInference/include/scopeone/inference/OnnxRuntime.h +++ b/ScopeOneCore/internal/inference/OnnxRuntime.h @@ -1,7 +1,6 @@ #pragma once -#include "scopeone/inference/ExecutionProvider.h" -#include "scopeone/inference/InferenceExport.h" +#include "internal/inference/ExecutionProvider.h" #include #include @@ -11,7 +10,7 @@ namespace scopeone::inference { class OnnxSession; - class SCOPEONE_INFERENCE_EXPORT OnnxRuntime + class OnnxRuntime { public: OnnxRuntime(); diff --git a/ScopeOneInference/include/scopeone/inference/OnnxSession.h b/ScopeOneCore/internal/inference/OnnxSession.h similarity index 83% rename from ScopeOneInference/include/scopeone/inference/OnnxSession.h rename to ScopeOneCore/internal/inference/OnnxSession.h index b7d4ba3..7c235a8 100644 --- a/ScopeOneInference/include/scopeone/inference/OnnxSession.h +++ b/ScopeOneCore/internal/inference/OnnxSession.h @@ -1,8 +1,7 @@ #pragma once -#include "scopeone/inference/ExecutionProvider.h" -#include "scopeone/inference/InferenceExport.h" -#include "scopeone/inference/TensorInfo.h" +#include "internal/inference/ExecutionProvider.h" +#include "internal/inference/TensorInfo.h" #include #include @@ -18,7 +17,7 @@ namespace scopeone::inference int deviceId{0}; }; - class SCOPEONE_INFERENCE_EXPORT OnnxSession + class OnnxSession { public: OnnxSession(OnnxRuntime& runtime, diff --git a/ScopeOneInference/include/scopeone/inference/TensorInfo.h b/ScopeOneCore/internal/inference/TensorInfo.h similarity index 100% rename from ScopeOneInference/include/scopeone/inference/TensorInfo.h rename to ScopeOneCore/internal/inference/TensorInfo.h diff --git a/ScopeOneCore/python/scopeone/README.md b/ScopeOneCore/python/scopeone/README.md index 3040419..0a56d22 100644 --- a/ScopeOneCore/python/scopeone/README.md +++ b/ScopeOneCore/python/scopeone/README.md @@ -37,9 +37,41 @@ python -c "from scopeone import ScopeOne; print(ScopeOne().version())" - Python package project root: `ScopeOneCore/python/scopeone` - Package source: `ScopeOneCore/python/scopeone/src/scopeone` - `scopeone.ScopeOne`, `FrameResult`, `RecordingSession` and `ExperimentSession` (in `scopeone.client`) control a running ScopeOne and move frames in and out of it as numpy arrays -- `scopeone.processing` goes the other way: ScopeOne runs Python functions as pipeline modules, see [Writing ScopeOne plugins](../../../plugins/README.md#python-processing-modules) +- `scopeone.processing` goes the other way: ScopeOne runs Python functions as pipeline modules, see [Python processing modules](#python-processing-modules) - `scopeone._shm` reads and writes the shared frame layout and is internal +## Python processing modules + +```python +import numpy as np +from scopeone.processing import Integer, Real, Boolean, Choice, module + +@module("my.python.threshold", "My Threshold", Integer("level", "Level", 100, 0, 65535)) +def threshold(image, level, bits): + return np.where(image > level, (1 << bits) - 1, 0).astype(image.dtype) +``` + +The function gets a 2D `uint8` or `uint16` array and the parameters by name, and returns a 2D +`uint8` or `uint16` array. A function with a `bits` argument also gets the bits per sample of +the frame, 12 for a 12-bit camera in a `uint16` array, so `bits` is not a parameter key. Put +the script in `processing/python/` beside ScopeOne or in the same folder under the +user's application data directory. The modules appear in the pipeline after a restart. ScopeOne ships no script +there; `ScopeOneCore/python/scopeone/examples/processing_offset.py` is one to copy. + +Each module in a pipeline runs in its own Python process (`python -m scopeone.processing +serve script.py`). That interpreter needs numpy and the `scopeone` package +(`pip install ScopeOneCore/python/scopeone`). Choose it under **Settings > Python +Interpreter**, or with `SCOPEONE_PYTHON`, which takes precedence; otherwise ScopeOne runs +`python3` (`python` on Windows). Output printed by a module goes to the log, and an exception +in it shows as the error of that pipeline step. + +ScopeOne first runs `python -m scopeone.processing describe script.py`, which prints one JSON +line with the modules of the script and their parameters. While the pipeline runs, each frame is +one request on stdin: a JSON line with `module`, `parameters`, `width`, `height`, `stride`, +`format` (`mono8` or `mono16`), `bits` and `bytes`, followed by that many pixel bytes. The reply +on stdout has the same form with `width`, `height`, `format` and `bytes`, or a JSON line with +`error`. + ## Current API ```python @@ -345,7 +377,7 @@ ScopeOne uses one local control pipe for JSON commands and one shared-memory blo - `cancel_experiment`: field `experimentId`; requests cancellation and returns the current experiment status. - `record`: fields `frames`, `camera`, `timeoutMs`, `mdaIntervalMs`, `zPositions`, `positions`, `order`; response `sessionId`, `cameraIds`. - `session_info`: fields `sessionId`; response `cameraIds`, `frameCount`, `frameCounts`. -- `session_close`: fields `sessionId`; releases the recorded session held by the app. +- `session_close`: fields `sessionId`; releases the recorded session held by the app together with its experiment document, which then no longer appears among the retained experiments of `state_snapshot`. - `session_frame`: fields `sessionId`, `camera`, `index`; response `mappingName`, `mappingSize`, and frame metadata. - `latest_raw_frame`: fields `camera`; response `mappingName`, `mappingSize`, and frame metadata. - `layer_frame`: field `layerKey`; exports the current raw, processed, static, mosaic, or Gallery layer frame and returns shared-memory metadata. @@ -414,7 +446,7 @@ These plan fields are optional and follow the Micro-Manager acquisition engine: - `comment` is free text that is stored with the acquisition, as the OME image description and in the `scopeone.acquisition` metadata, which also holds the `AcquisitionOrder` of the axes from the slowest to the fastest. - Exposure, XY and Z are sent only when they change, and the stage positions, the channel preset and the shutter are restored when the run ends. -Processing module editing follows the desktop UI rules: stop real-time processing before changing bit depth, adding/removing modules, updating module parameters, or resetting module state. Pass `add_processing_module` a stable module ID returned in `processing_modules.availableModules`; this includes modules supplied by installed processing plugins. +Processing module editing follows the desktop UI rules: stop real-time processing before changing bit depth, adding/removing modules, updating module parameters, or resetting module state. Pass `add_processing_module` a stable module ID returned in `processing_modules.availableModules`; this includes the CUDA and ONNX processing modules and Python modules. ### Frame transfer diff --git a/ScopeOneCore/python/scopeone/examples/processing_offset.py b/ScopeOneCore/python/scopeone/examples/processing_offset.py index e78a414..61a12f9 100644 --- a/ScopeOneCore/python/scopeone/examples/processing_offset.py +++ b/ScopeOneCore/python/scopeone/examples/processing_offset.py @@ -1,4 +1,4 @@ -# A processing module in Python. Copy it into plugins/processing/python/ and ScopeOne lists it +# A processing module in Python. Copy it into processing/python/ and ScopeOne lists it # in the pipeline as "Python Offset". import numpy as np diff --git a/ScopeOneCore/python/scopeone/src/scopeone/processing.py b/ScopeOneCore/python/scopeone/src/scopeone/processing.py index 1a2cb6c..1ca7709 100644 --- a/ScopeOneCore/python/scopeone/src/scopeone/processing.py +++ b/ScopeOneCore/python/scopeone/src/scopeone/processing.py @@ -2,8 +2,8 @@ A module is a function that takes a 2D numpy array (uint8 or uint16) and its parameters and returns a 2D uint8 or uint16 array. A function with a ``bits`` argument also gets the bits per -sample of the frame, 12 for a 12-bit camera in a uint16 array. Put the script in the ``python`` -folder of a ScopeOne processing plugin directory and it shows up in the pipeline beside the C++ +sample of the frame, 12 for a 12-bit camera in a uint16 array. Put the script in +``processing/python`` beside ScopeOne or in the application data folder and it shows up in the pipeline beside the C++ modules:: import numpy as np diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index 4a13879..7eee3e7 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -56,7 +56,7 @@ #include "scopeone/CameraProvider.h" #include "scopeone/DaqProvider.h" #include "scopeone/Driver.h" -#include "scopeone/PluginManifest.h" +#include "scopeone/DriverManifest.h" #include "scopeone/HardwareCapabilities.h" #include "scopeone/SharedFrame.h" #include "scopeone/SignalSourceProvider.h" @@ -1423,13 +1423,13 @@ namespace scopeone::core::internal class ProviderDriverRuntime final : public DriverHostRuntime { public: - ProviderDriverRuntime(QString pluginPath, + ProviderDriverRuntime(QString driverPath, QString providerId, QString hostKey, QJsonObject options, QObject* parent = nullptr) : DriverHostRuntime(parent) - , m_pluginPath(std::move(pluginPath)) + , m_driverPath(std::move(driverPath)) , m_providerId(std::move(providerId)) , m_hostKey(std::move(hostKey)) , m_options(std::move(options)) @@ -1504,6 +1504,7 @@ namespace scopeone::core::internal streamring::Writer writer; QMutex writeMutex; std::atomic_bool notifyQueued{false}; + bool stopRequested{false}; }; void installStreamSinks(); @@ -1515,11 +1516,13 @@ namespace scopeone::core::internal qsizetype capacity, QString* errorMessage); void closeStream(const QString& streamId); + void markStreamStopped(const QString& streamId); + void releaseEndedStream(const QString& streamId, bool failed); void writeStream(const QString& streamId, streamring::RecordType type, const QByteArray& payload); qsizetype signalStreamCapacity(const SignalAcquisitionConfig& config) const; static qsizetype daqStreamCapacity(const DaqSessionConfig& config); - QString m_pluginPath; + QString m_driverPath; QString m_providerId; QString m_hostKey; QJsonObject m_options; @@ -1529,7 +1532,7 @@ namespace scopeone::core::internal QHash m_devicesById; QHash> m_cameraTransports; mutable QReadWriteLock m_cameraTransportsLock; - std::unique_ptr m_pluginLoader; + std::unique_ptr m_driverLoader; HardwareProviderPtr m_provider; CameraProvider* m_camera{nullptr}; DevicePropertyProvider* m_properties{nullptr}; @@ -1551,11 +1554,10 @@ namespace scopeone::core::internal bool ProviderDriverRuntime::initializeRuntime() { m_lastError.clear(); - m_pluginLoader = std::make_unique(m_pluginPath); - PluginManifest manifest; - if (!parsePluginManifest( - m_pluginLoader->metaData().value(QStringLiteral("MetaData")).toObject(), - PluginKind::Driver, + m_driverLoader = std::make_unique(m_driverPath); + DriverManifest manifest; + if (!parseDriverManifest( + m_driverLoader->metaData().value(QStringLiteral("MetaData")).toObject(), manifest, &m_lastError)) { @@ -1568,10 +1570,10 @@ namespace scopeone::core::internal m_lastError = QStringLiteral("Provider manifest identity mismatch"); return false; } - QObject* const instance = m_pluginLoader->instance(); + QObject* const instance = m_driverLoader->instance(); if (!instance) { - m_lastError = m_pluginLoader->errorString(); + m_lastError = m_driverLoader->errorString(); return false; } @@ -1693,6 +1695,11 @@ namespace scopeone::core::internal event.insert(driverhost::kStateField, static_cast(state)); event.insert(driverhost::kStateMessageField, message); emit eventReady(event); + if (state == SignalSourceState::Idle || state == SignalSourceState::Error) + { + releaseEndedStream(QStringLiteral("signal:") + sourceId, + state == SignalSourceState::Error); + } }, Qt::QueuedConnection); }); } @@ -1714,6 +1721,10 @@ namespace scopeone::core::internal event.insert(driverhost::kStateField, static_cast(state)); event.insert(driverhost::kStateMessageField, message); emit eventReady(event); + if (state == DaqState::Idle || state == DaqState::Error) + { + releaseEndedStream(QStringLiteral("daq:") + deviceId, state == DaqState::Error); + } }, Qt::QueuedConnection); }); } @@ -1744,6 +1755,28 @@ namespace scopeone::core::internal m_streams.remove(streamId); } + void ProviderDriverRuntime::markStreamStopped(const QString& streamId) + { + QMutexLocker locker(&m_streamsMutex); + if (const auto stream = m_streams.value(streamId)) + { + stream->stopRequested = true; + } + } + + // A stopped stream kept its ring until the next start of the same source, up to 256 MB in + // each process per source. The ring goes once the source has ended, and only the ring that + // was stopped, as the end of an earlier run can arrive after the next start. + void ProviderDriverRuntime::releaseEndedStream(const QString& streamId, bool failed) + { + QMutexLocker locker(&m_streamsMutex); + const auto stream = m_streams.value(streamId); + if (stream && (stream->stopRequested || failed)) + { + m_streams.remove(streamId); + } + } + void ProviderDriverRuntime::writeStream(const QString& streamId, streamring::RecordType type, const QByteArray& payload) @@ -1842,13 +1875,17 @@ namespace scopeone::core::internal } if (type == driverhost::kCommandStopSignal) { - m_signals->stopSignal(message.value(driverhost::kSourceIdField).toString()); + const QString sourceId = message.value(driverhost::kSourceIdField).toString(); + markStreamStopped(QStringLiteral("signal:") + sourceId); + m_signals->stopSignal(sourceId); reply(makeResponse(type, requestId, true)); return true; } if (type == driverhost::kCommandStopDaq) { - m_daq->stopDaq(message.value(driverhost::kDeviceIdField).toString()); + const QString deviceId = message.value(driverhost::kDeviceIdField).toString(); + markStreamStopped(QStringLiteral("daq:") + deviceId); + m_daq->stopDaq(deviceId); reply(makeResponse(type, requestId, true)); return true; } @@ -2739,10 +2776,10 @@ namespace scopeone::core::internal } m_devicesById.clear(); m_devices.clear(); - if (m_pluginLoader) + if (m_driverLoader) { - m_pluginLoader->unload(); - m_pluginLoader.reset(); + m_driverLoader->unload(); + m_driverLoader.reset(); } } @@ -3115,9 +3152,9 @@ int main(int argc, char* argv[]) } else { - const QString pluginPath = parser.value(optDriver).trimmed(); + const QString driverPath = parser.value(optDriver).trimmed(); const QString hostKey = parser.value(optHostKey).trimmed(); - if (pluginPath.isEmpty() || hostKey.isEmpty()) + if (driverPath.isEmpty() || hostKey.isEmpty()) { qCritical().noquote() << "External providers require --driver and --hostKey"; return 2; @@ -3143,7 +3180,7 @@ int main(int argc, char* argv[]) } runtime = new scopeone::core::internal::ProviderDriverRuntime( - pluginPath, + driverPath, providerId, hostKey, providerOptions); diff --git a/ScopeOneCore/src/DriverHostProviderProxy.cpp b/ScopeOneCore/src/DriverHostProviderProxy.cpp index dba8ad5..7e77b14 100644 --- a/ScopeOneCore/src/DriverHostProviderProxy.cpp +++ b/ScopeOneCore/src/DriverHostProviderProxy.cpp @@ -80,14 +80,14 @@ namespace scopeone::core::internal }; DriverHostProviderTransport(QString providerId, - QString pluginPath, + QString driverPath, QString hostKey, QJsonObject options, FrameHandler frameHandler, PreviewHandler previewHandler, StreamHandlers streamHandlers) : m_providerId(std::move(providerId)) - , m_pluginPath(std::move(pluginPath)) + , m_driverPath(std::move(driverPath)) , m_hostKey(std::move(hostKey)) , m_options(std::move(options)) , m_frameHandler(std::move(frameHandler)) @@ -124,12 +124,12 @@ namespace scopeone::core::internal } return false; } - if (!QFileInfo::exists(m_pluginPath)) + if (!QFileInfo::exists(m_driverPath)) { if (errorMessage) { *errorMessage = QStringLiteral("Provider module not found: %1") - .arg(m_pluginPath); + .arg(m_driverPath); } return false; } @@ -162,7 +162,7 @@ namespace scopeone::core::internal QStringList arguments; arguments << QStringLiteral("--provider") << m_providerId - << QStringLiteral("--driver") << m_pluginPath + << QStringLiteral("--driver") << m_driverPath << QStringLiteral("--hostKey") << m_hostKey << QStringLiteral("--launcherPid") << QString::number(QCoreApplication::applicationPid()); @@ -332,6 +332,13 @@ namespace scopeone::core::internal bool stopStream(const QString& command, const QString& id, QString* errorMessage) { + const QString streamId = (command == driverhost::kCommandStopSignal ? QStringLiteral("signal:") + : QStringLiteral("daq:")) + + id; + if (const auto stream = m_streams.value(streamId)) + { + stream->stopRequested = true; + } QJsonObject request; request.insert(driverhost::kMessageTypeField, command); request.insert(command == driverhost::kCommandStopSignal ? driverhost::kSourceIdField @@ -538,8 +545,21 @@ namespace scopeone::core::internal QString streamId; QSharedMemory shm; streamring::Reader reader; + bool stopRequested{false}; }; + // A stopped stream kept its ring attached until the next start of the same source. It is + // dropped once the source has ended, and only the ring that was stopped, as the end of an + // earlier run can arrive after the next start. + void releaseEndedStream(const QString& streamId, bool failed) + { + const auto it = m_streams.constFind(streamId); + if (it != m_streams.cend() && (it.value()->stopRequested || failed)) + { + m_streams.remove(streamId); + } + } + void drainStream(const QString& streamId) { const auto it = m_streams.constFind(streamId); @@ -692,6 +712,7 @@ namespace scopeone::core::internal if (state == SignalSourceState::Idle || state == SignalSourceState::Error) { m_activeSignals.remove(sourceId); + releaseEndedStream(QStringLiteral("signal:") + sourceId, state == SignalSourceState::Error); } if (m_streamHandlers.signalState) { @@ -706,6 +727,7 @@ namespace scopeone::core::internal if (state == DaqState::Idle || state == DaqState::Error) { m_activeDaqDevices.remove(deviceId); + releaseEndedStream(QStringLiteral("daq:") + deviceId, state == DaqState::Error); } if (m_streamHandlers.daqState) { @@ -765,7 +787,7 @@ namespace scopeone::core::internal } QString m_providerId; - QString m_pluginPath; + QString m_driverPath; QString m_hostKey; QJsonObject m_options; FrameHandler m_frameHandler; @@ -804,10 +826,10 @@ namespace scopeone::core::internal { public: DriverHostProviderProxy(QString providerId, - QString pluginPath, + QString driverPath, QJsonObject options) : m_expectedProviderId(std::move(providerId)) - , m_pluginPath(std::move(pluginPath)) + , m_driverPath(std::move(driverPath)) , m_options(std::move(options)) { } @@ -831,7 +853,7 @@ namespace scopeone::core::internal const QString hostKey = QUuid::createUuid().toString(QUuid::WithoutBraces); m_worker = new DriverHostProviderTransport( m_expectedProviderId, - m_pluginPath, + m_driverPath, hostKey, m_options, [this](const ImageFrame& frame) { deliverFrame(frame); }, @@ -1668,7 +1690,7 @@ namespace scopeone::core::internal } QString m_expectedProviderId; - QString m_pluginPath; + QString m_driverPath; QJsonObject m_options; HardwareProviderDescriptor m_descriptor; QList m_devices; @@ -1694,19 +1716,19 @@ namespace scopeone::core::internal }; HardwareProviderPtr createDriverHostProviderProxy(const QString& providerId, - const QString& pluginPath, + const QString& driverPath, const QJsonObject& options, QString* errorMessage) { const QString normalizedProviderId = providerId.trimmed(); - const QString normalizedPluginPath = QFileInfo(pluginPath).absoluteFilePath(); - if (normalizedProviderId.isEmpty() || pluginPath.trimmed().isEmpty()) + const QString normalizedDriverPath = QFileInfo(driverPath).absoluteFilePath(); + if (normalizedProviderId.isEmpty() || driverPath.trimmed().isEmpty()) { if (errorMessage) *errorMessage = QStringLiteral("Provider ID and module path are required"); return {}; } auto proxy = std::make_shared(normalizedProviderId, - normalizedPluginPath, + normalizedDriverPath, options); if (!proxy->initialize(errorMessage)) return {}; return proxy; diff --git a/ScopeOneCore/src/PluginManifest.cpp b/ScopeOneCore/src/DriverManifest.cpp similarity index 50% rename from ScopeOneCore/src/PluginManifest.cpp rename to ScopeOneCore/src/DriverManifest.cpp index d4cb69a..ffb8bea 100644 --- a/ScopeOneCore/src/PluginManifest.cpp +++ b/ScopeOneCore/src/DriverManifest.cpp @@ -1,7 +1,6 @@ -#include "scopeone/PluginManifest.h" +#include "scopeone/DriverManifest.h" #include "scopeone/Driver.h" -#include "scopeone/ProcessingPlugin.h" #include #include @@ -14,77 +13,30 @@ namespace scopeone::core { - QString pluginKindName(PluginKind kind) + QStringList driverDirectories() { - switch (kind) - { - case PluginKind::Processing: - return QStringLiteral("processing"); - case PluginKind::Driver: - return QStringLiteral("driver"); - } - return {}; - } - - int pluginApiVersion(PluginKind kind) - { - switch (kind) - { - case PluginKind::Processing: - return ScopeOneProcessingPluginApiVersion; - case PluginKind::Driver: - return ScopeOneDriverApiVersion; - } - return 0; - } - - QString pluginDirectoryName(PluginKind kind) - { - return kind == PluginKind::Driver ? QStringLiteral("drivers") - : QStringLiteral("plugins/%1").arg(pluginKindName(kind)); - } - - QStringList pluginInterfaceIds(PluginKind kind) - { - switch (kind) - { - case PluginKind::Processing: - return {QStringLiteral(ScopeOneProcessingPlugin_iid)}; - case PluginKind::Driver: - return {QStringLiteral(ScopeOneDriver_iid)}; - } - return {}; - } - - QStringList pluginDirectories(PluginKind kind) - { - const QString directoryName = pluginDirectoryName(kind); return { - QDir(QCoreApplication::applicationDirPath()) - .filePath(directoryName), + QDir(QCoreApplication::applicationDirPath()).filePath(QStringLiteral("drivers")), QDir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)) - .filePath(directoryName)}; + .filePath(QStringLiteral("drivers"))}; } - QString pluginSettingsKey(const QString& pluginId, const QString& name) + QString driverSettingsKey(const QString& driverId, const QString& name) { - return QStringLiteral("Plugins/%1/%2").arg(pluginId, name); + return QStringLiteral("Drivers/%1/%2").arg(driverId, name); } - bool pluginEnabled(const PluginManifest& manifest) + bool driverEnabled(const DriverManifest& manifest) { QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - const QString key = pluginSettingsKey(manifest.id, QStringLiteral("enabled")); + const QString key = driverSettingsKey(manifest.id, QStringLiteral("enabled")); return settings.contains(key) ? settings.value(key).toBool() : manifest.autoLoad; } - QList discoverPlugins(PluginKind kind, - const QStringList& directories) + QList discoverDrivers(const QStringList& directories) { - QList plugins; - const QStringList searchDirectories = directories.isEmpty() - ? pluginDirectories(kind) - : directories; + QList drivers; + const QStringList searchDirectories = directories.isEmpty() ? driverDirectories() : directories; for (const QString& directoryPath : searchDirectories) { const QDir directory(directoryPath); @@ -94,6 +46,7 @@ namespace scopeone::core { continue; } + // Reads the manifest Q_PLUGIN_METADATA put in the file, without loading the library QPluginLoader loader(file.absoluteFilePath()); const QJsonObject loaderMetadata = loader.metaData(); const QString interfaceId = loaderMetadata.value(QStringLiteral("IID")).toString(); @@ -102,25 +55,23 @@ namespace scopeone::core continue; } - DiscoveredPlugin plugin; - plugin.expectedKind = kind; - plugin.path = file.absoluteFilePath(); - plugin.interfaceId = interfaceId; - plugin.metadata = loaderMetadata.value(QStringLiteral("MetaData")).toObject(); - parsePluginManifest(plugin.metadata, kind, plugin.manifest, &plugin.error); - if (plugin.error.isEmpty() && !pluginInterfaceIds(kind).contains(interfaceId)) + DiscoveredDriver driver; + driver.path = file.absoluteFilePath(); + driver.interfaceId = interfaceId; + driver.metadata = loaderMetadata.value(QStringLiteral("MetaData")).toObject(); + parseDriverManifest(driver.metadata, driver.manifest, &driver.error); + if (driver.error.isEmpty() && interfaceId != QStringLiteral(ScopeOneDriver_iid)) { - plugin.error = QStringLiteral("plugin interface does not match its kind"); + driver.error = QStringLiteral("library is not a ScopeOne driver"); } - plugins.append(std::move(plugin)); + drivers.append(std::move(driver)); } } - return plugins; + return drivers; } - bool parsePluginManifest(const QJsonObject& metadata, - PluginKind expectedKind, - PluginManifest& manifest, + bool parseDriverManifest(const QJsonObject& metadata, + DriverManifest& manifest, QString* errorMessage) { const QString id = metadata.value(QStringLiteral("id")).toString().trimmed(); @@ -129,28 +80,27 @@ namespace scopeone::core const QString kind = metadata.value(QStringLiteral("kind")).toString().trimmed(); const int apiVersion = metadata.value(QStringLiteral("scopeOneApi")).toInt(); const QString coreVersion = metadata.value(QStringLiteral("scopeOneCore")).toString(); - const QString expectedKindName = pluginKindName(expectedKind); QString error; static const QRegularExpression idPattern( QStringLiteral("^[A-Za-z0-9][A-Za-z0-9._-]*$")); if (id.isEmpty() || name.isEmpty() || version.isEmpty()) { - error = QStringLiteral("plugin manifest requires id, name and version"); + error = QStringLiteral("manifest requires id, name and version"); } else if (!idPattern.match(id).hasMatch()) { - error = QStringLiteral("plugin id contains unsupported characters"); + error = QStringLiteral("id contains unsupported characters"); } - else if (kind != expectedKindName) + else if (kind != QStringLiteral("driver")) { - error = QStringLiteral("plugin kind must be '%1'").arg(expectedKindName); + error = QStringLiteral("kind must be 'driver'"); } - else if (apiVersion != pluginApiVersion(expectedKind)) + else if (apiVersion != ScopeOneDriverApiVersion) { - error = QStringLiteral("unsupported ScopeOne plugin API %1").arg(apiVersion); + error = QStringLiteral("unsupported ScopeOne API %1").arg(apiVersion); } - // Plugins share C++ types with the Core, so they load only into the MAJOR.MINOR release + // Drivers share C++ types with the Core, so they load only into the MAJOR.MINOR release // they were built against. The VERSION file changes that release when the SDK changes. else if (coreVersion.section(QLatin1Char('.'), 0, 1) != QStringLiteral(SCOPEONE_CORE_VERSION_STRING).section(QLatin1Char('.'), 0, 1)) @@ -172,7 +122,6 @@ namespace scopeone::core manifest.id = id; manifest.name = name; manifest.version = version; - manifest.kind = expectedKind; manifest.autoLoad = metadata.value(QStringLiteral("autoLoad")).toBool(); manifest.metadata = metadata; if (errorMessage) diff --git a/ScopeOneCore/src/ProcessingModuleRegistry.cpp b/ScopeOneCore/src/ProcessingModuleRegistry.cpp index 2e3faea..e6599a6 100644 --- a/ScopeOneCore/src/ProcessingModuleRegistry.cpp +++ b/ScopeOneCore/src/ProcessingModuleRegistry.cpp @@ -10,10 +10,14 @@ #include "internal/ImageOperationModules.h" #include "internal/IFFTModule.h" #include "internal/SpatiotemporalBinningModule.h" -#include "scopeone/PluginManifest.h" -#include -#include +#ifdef SCOPEONE_WITH_CUDA +#include "internal/cuda/CudaProcessingRegistration.h" +#endif +#ifdef SCOPEONE_WITH_INFERENCE +#include "internal/inference/InferenceProcessingRegistration.h" +#endif + #include namespace scopeone::core::internal @@ -223,6 +227,12 @@ namespace scopeone::core::internal ProcessingModuleRegistry::ProcessingModuleRegistry() { registerBuiltIns(*this); +#ifdef SCOPEONE_WITH_CUDA + cuda::registerCudaProcessingModules(*this); +#endif +#ifdef SCOPEONE_WITH_INFERENCE + inference::registerInferenceProcessingModules(*this); +#endif } ProcessingModuleRegistry::~ProcessingModuleRegistry() = default; @@ -269,63 +279,4 @@ namespace scopeone::core::internal std::unique_ptr module = it->factory(); return module && module->id() == it->descriptor.id ? std::move(module) : nullptr; } - - QStringList ProcessingModuleRegistry::loadPlugins(const QString& directoryPath) - { - QStringList errors; - for (const DiscoveredPlugin& discovered : - discoverPlugins(PluginKind::Processing, {directoryPath})) - { - const QFileInfo file(discovered.path); - if (!discovered.error.isEmpty()) - { - errors.append(QStringLiteral("%1: %2").arg(file.fileName(), discovered.error)); - continue; - } - - auto loader = std::make_unique(discovered.path); - QObject* instance = loader->instance(); - auto* plugin = qobject_cast(instance); - if (!plugin) - { - errors.append(QStringLiteral("%1: %2") - .arg(file.fileName(), loader->errorString())); - continue; - } - - const QList pluginDescriptors = plugin->processingModules(); - QSet pluginIds; - bool valid = true; - for (const ProcessingModuleDescriptor& descriptor : pluginDescriptors) - { - const QString id = descriptor.id.trimmed(); - if (!validDescriptor(descriptor) - || pluginIds.contains(id) - || m_entries.contains(id)) - { - valid = false; - break; - } - pluginIds.insert(id); - } - if (!valid) - { - errors.append(QStringLiteral("%1: invalid or duplicate processing module id") - .arg(file.fileName())); - loader->unload(); - continue; - } - - for (const ProcessingModuleDescriptor& descriptor : pluginDescriptors) - { - const QString id = descriptor.id.trimmed(); - registerModule(descriptor, [plugin, id]() - { - return plugin->createProcessingModule(id); - }); - } - m_pluginLoaders.push_back(std::move(loader)); - } - return errors; - } } diff --git a/ScopeOneCore/src/PythonProcessingModule.cpp b/ScopeOneCore/src/PythonProcessingModule.cpp index 374c2e0..df64159 100644 --- a/ScopeOneCore/src/PythonProcessingModule.cpp +++ b/ScopeOneCore/src/PythonProcessingModule.cpp @@ -261,10 +261,10 @@ namespace scopeone::core::internal } QStringList registerPythonProcessingModules(ProcessingModuleRegistry& registry, - const QString& pluginDirectory) + const QString& processingDirectory) { QStringList errors; - const QDir scripts(QDir(pluginDirectory).filePath(QStringLiteral("python"))); + const QDir scripts(QDir(processingDirectory).filePath(QStringLiteral("python"))); for (const QString& name : scripts.entryList({QStringLiteral("*.py")}, QDir::Files, QDir::Name)) { QString error; diff --git a/ScopeOneCore/src/ScopeOneCore.cpp b/ScopeOneCore/src/ScopeOneCore.cpp index 4aa16db..7b8568d 100644 --- a/ScopeOneCore/src/ScopeOneCore.cpp +++ b/ScopeOneCore/src/ScopeOneCore.cpp @@ -1,6 +1,5 @@ #include "scopeone/ScopeOneCore.h" #include "scopeone/ImageSceneModel.h" -#include "scopeone/PluginManifest.h" #include "scopeone/ScanImageAssembler.h" #include "internal/AcquisitionEngine.h" @@ -1362,22 +1361,19 @@ namespace scopeone::core "scopeone::core::ScanImageConfig"); m_managers->processingModuleRegistry = std::make_unique(); - QStringList allProcessingPluginErrors; - const QStringList processingDirectories = - scopeone::core::pluginDirectories(scopeone::core::PluginKind::Processing); - // Native processing plugins run inside ScopeOne and are only the built-in CUDA and ONNX - // ones beside the executable. Loading them from the user directory too let anyone add - // in-process C++ code; users extend processing with Python modules instead. - allProcessingPluginErrors.append( - m_managers->processingModuleRegistry->loadPlugins(processingDirectories.constFirst())); + // Python processing modules are scripts in processing/python beside the application and in + // the user data directory + const QStringList processingDirectories{ + QDir(QCoreApplication::applicationDirPath()).filePath(QStringLiteral("processing")), + QDir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)) + .filePath(QStringLiteral("processing"))}; for (const QString& directory : processingDirectories) { - allProcessingPluginErrors.append(internal::registerPythonProcessingModules( - *m_managers->processingModuleRegistry, directory)); - } - for (const QString& error : allProcessingPluginErrors) - { - qWarning().noquote() << QStringLiteral("Failed to load processing plugin %1").arg(error); + for (const QString& error : internal::registerPythonProcessingModules( + *m_managers->processingModuleRegistry, directory)) + { + qWarning().noquote() << QStringLiteral("Failed to load Python processing module %1").arg(error); + } } m_histogramThreadPool = std::make_unique(); m_histogramThreadPool->setMaxThreadCount(1); @@ -2366,7 +2362,7 @@ namespace scopeone::core return m_managers->cameraProvider->getROI(target, x, y, width, height); } - // List signal sources discovered through external source plugins + // List signal sources discovered through drivers QList ScopeOneCore::signalSources() const { return m_managers->signalSourceManager->sources(); @@ -5362,6 +5358,12 @@ namespace scopeone::core return false; } m_managers->sessions.remove(id); + // The experiment document stayed after its session was closed, so the event and frame + // records of every closed recording were kept until ScopeOne exited + if (id != m_managers->activeExperimentId) + { + m_managers->experiments.remove(id); + } emit recordingSessionClosed(id); emit recordingSessionsChanged(); return true; diff --git a/ScopeOneCore/src/SignalSourceManager.cpp b/ScopeOneCore/src/SignalSourceManager.cpp index aa8becb..8e0f4d6 100644 --- a/ScopeOneCore/src/SignalSourceManager.cpp +++ b/ScopeOneCore/src/SignalSourceManager.cpp @@ -125,7 +125,7 @@ namespace scopeone::core::internal } // Follows the drivers as they are registered and removed: the sources of each - // plugin that provides signal sources join the list, and their chunks are passed on + // driver that provides signal sources join the list, and their chunks are passed on void SignalSourceManager::refreshProviderSources() { QHash current; diff --git a/ScopeOneCuda/src/CudaDevice.cpp b/ScopeOneCore/src/cuda/CudaDevice.cpp similarity index 87% rename from ScopeOneCuda/src/CudaDevice.cpp rename to ScopeOneCore/src/cuda/CudaDevice.cpp index d01a988..382d9ec 100644 --- a/ScopeOneCuda/src/CudaDevice.cpp +++ b/ScopeOneCore/src/cuda/CudaDevice.cpp @@ -1,4 +1,4 @@ -#include "scopeone/cuda/CudaDevice.h" +#include "internal/cuda/CudaDevice.h" #include diff --git a/ScopeOneCuda/src/CudaFrequencyFilterModule.cpp b/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp similarity index 97% rename from ScopeOneCuda/src/CudaFrequencyFilterModule.cpp rename to ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp index cef1dc1..8ee7783 100644 --- a/ScopeOneCuda/src/CudaFrequencyFilterModule.cpp +++ b/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp @@ -1,11 +1,11 @@ -#include "CudaFrequencyFilterModule.h" +#include "internal/cuda/CudaFrequencyFilterModule.h" -#include "scopeone/cuda/CudaKernelLaunch.h" +#include "internal/cuda/CudaKernelLaunch.h" #include #include -namespace scopeone::plugins::cuda +namespace scopeone::cuda { CudaFrequencyFilterModule::CudaFrequencyFilterModule() : CudaRealImageModule(scopeone::cuda::GpuMemoryLayout::Contiguous) diff --git a/ScopeOneCuda/src/CudaGaussianBlurModule.cpp b/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp similarity index 94% rename from ScopeOneCuda/src/CudaGaussianBlurModule.cpp rename to ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp index 4645bb8..9eb0a94 100644 --- a/ScopeOneCuda/src/CudaGaussianBlurModule.cpp +++ b/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp @@ -1,8 +1,8 @@ -#include "CudaGaussianBlurModule.h" +#include "internal/cuda/CudaGaussianBlurModule.h" -#include "scopeone/cuda/CudaKernelLaunch.h" +#include "internal/cuda/CudaKernelLaunch.h" -namespace scopeone::plugins::cuda +namespace scopeone::cuda { CudaGaussianBlurModule::CudaGaussianBlurModule() : CudaRealImageModule(scopeone::cuda::GpuMemoryLayout::Pitched2D) diff --git a/ScopeOneCuda/src/CudaKernels.cu b/ScopeOneCore/src/cuda/CudaKernels.cu similarity index 99% rename from ScopeOneCuda/src/CudaKernels.cu rename to ScopeOneCore/src/cuda/CudaKernels.cu index 7709208..821d254 100644 --- a/ScopeOneCuda/src/CudaKernels.cu +++ b/ScopeOneCore/src/cuda/CudaKernels.cu @@ -1,4 +1,4 @@ -#include "scopeone/cuda/CudaKernelLaunch.h" +#include "internal/cuda/CudaKernelLaunch.h" #include #include diff --git a/ScopeOneCuda/src/CudaProcessingRegistration.cpp b/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp similarity index 78% rename from ScopeOneCuda/src/CudaProcessingRegistration.cpp rename to ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp index 602a9c4..816cd11 100644 --- a/ScopeOneCuda/src/CudaProcessingRegistration.cpp +++ b/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp @@ -1,21 +1,14 @@ -#include "CudaFrequencyFilterModule.h" -#include "CudaGaussianBlurModule.h" +#include "internal/cuda/CudaProcessingRegistration.h" +#include "internal/cuda/CudaFrequencyFilterModule.h" +#include "internal/cuda/CudaGaussianBlurModule.h" -#include "scopeone/ProcessingPlugin.h" +#include "internal/ProcessingModuleRegistry.h" -#include - -namespace scopeone::plugins::cuda +namespace scopeone::cuda { - class CudaProcessingPlugin final : public QObject, - public core::ProcessingPlugin + namespace { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneProcessingPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::ProcessingPlugin) - - public: - QList processingModules() const override + QList descriptors() { return {{QStringLiteral("cuda.gaussian_blur"), QStringLiteral("CUDA Gaussian Blur"), @@ -78,8 +71,7 @@ namespace scopeone::plugins::cuda {QStringLiteral("Hard"), 1}}}}}}; } - std::unique_ptr createProcessingModule( - const QString& moduleId) override + std::unique_ptr create(const QString& moduleId) { if (moduleId == QStringLiteral("cuda.gaussian_blur")) { @@ -91,7 +83,14 @@ namespace scopeone::plugins::cuda } return {}; } - }; -} + } -#include "CudaProcessingRegistration.moc" + void registerCudaProcessingModules(core::internal::ProcessingModuleRegistry& registry) + { + for (const core::ProcessingModuleDescriptor& descriptor : descriptors()) + { + const QString id = descriptor.id; + registry.registerModule(descriptor, [id]() { return create(id); }); + } + } +} diff --git a/ScopeOneCuda/src/CudaRealImageModule.cpp b/ScopeOneCore/src/cuda/CudaRealImageModule.cpp similarity index 96% rename from ScopeOneCuda/src/CudaRealImageModule.cpp rename to ScopeOneCore/src/cuda/CudaRealImageModule.cpp index cb26a0d..906a361 100644 --- a/ScopeOneCuda/src/CudaRealImageModule.cpp +++ b/ScopeOneCore/src/cuda/CudaRealImageModule.cpp @@ -1,6 +1,6 @@ -#include "scopeone/cuda/CudaRealImageModule.h" +#include "internal/cuda/CudaRealImageModule.h" -#include "scopeone/cuda/CudaDevice.h" +#include "internal/cuda/CudaDevice.h" #include diff --git a/ScopeOneCuda/src/GpuComplexFrame.cpp b/ScopeOneCore/src/cuda/GpuComplexFrame.cpp similarity index 98% rename from ScopeOneCuda/src/GpuComplexFrame.cpp rename to ScopeOneCore/src/cuda/GpuComplexFrame.cpp index 847d3d7..f8fceeb 100644 --- a/ScopeOneCuda/src/GpuComplexFrame.cpp +++ b/ScopeOneCore/src/cuda/GpuComplexFrame.cpp @@ -1,4 +1,4 @@ -#include "scopeone/cuda/GpuComplexFrame.h" +#include "internal/cuda/GpuComplexFrame.h" #include diff --git a/ScopeOneCuda/src/GpuRealFrame.cpp b/ScopeOneCore/src/cuda/GpuRealFrame.cpp similarity index 98% rename from ScopeOneCuda/src/GpuRealFrame.cpp rename to ScopeOneCore/src/cuda/GpuRealFrame.cpp index 6e833ec..c3f3b2f 100644 --- a/ScopeOneCuda/src/GpuRealFrame.cpp +++ b/ScopeOneCore/src/cuda/GpuRealFrame.cpp @@ -1,6 +1,6 @@ -#include "scopeone/cuda/GpuRealFrame.h" +#include "internal/cuda/GpuRealFrame.h" -#include "scopeone/cuda/CudaKernelLaunch.h" +#include "internal/cuda/CudaKernelLaunch.h" #include diff --git a/ScopeOneInference/src/InferenceProcessingRegistration.cpp b/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp similarity index 77% rename from ScopeOneInference/src/InferenceProcessingRegistration.cpp rename to ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp index 3c1ef1f..00ec172 100644 --- a/ScopeOneInference/src/InferenceProcessingRegistration.cpp +++ b/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp @@ -1,20 +1,13 @@ -#include "OnnxInferenceModule.h" +#include "internal/inference/InferenceProcessingRegistration.h" +#include "internal/inference/OnnxInferenceModule.h" -#include "scopeone/ProcessingPlugin.h" - -#include +#include "internal/ProcessingModuleRegistry.h" namespace scopeone::inference { - class InferenceProcessingPlugin final : public QObject, - public core::ProcessingPlugin + namespace { - Q_OBJECT - Q_PLUGIN_METADATA(IID ScopeOneProcessingPlugin_iid FILE "scopeone_plugin.json") - Q_INTERFACES(scopeone::core::ProcessingPlugin) - - public: - QList processingModules() const override + QList descriptors() { core::ProcessingParameterDescriptor modelPath{ QStringLiteral("model_path"), @@ -83,8 +76,7 @@ namespace scopeone::inference {QStringLiteral("Input - Output"), 1}}}}}}; } - std::unique_ptr createProcessingModule( - const QString& moduleId) override + std::unique_ptr create(const QString& moduleId) { if (moduleId == QStringLiteral("onnx_inference")) { @@ -92,7 +84,14 @@ namespace scopeone::inference } return {}; } - }; -} + } -#include "InferenceProcessingRegistration.moc" + void registerInferenceProcessingModules(core::internal::ProcessingModuleRegistry& registry) + { + for (const core::ProcessingModuleDescriptor& descriptor : descriptors()) + { + const QString id = descriptor.id; + registry.registerModule(descriptor, [id]() { return create(id); }); + } + } +} diff --git a/ScopeOneInference/src/OnnxInferenceModule.cpp b/ScopeOneCore/src/inference/OnnxInferenceModule.cpp similarity index 99% rename from ScopeOneInference/src/OnnxInferenceModule.cpp rename to ScopeOneCore/src/inference/OnnxInferenceModule.cpp index 73e0b13..343cf5b 100644 --- a/ScopeOneInference/src/OnnxInferenceModule.cpp +++ b/ScopeOneCore/src/inference/OnnxInferenceModule.cpp @@ -1,4 +1,4 @@ -#include "OnnxInferenceModule.h" +#include "internal/inference/OnnxInferenceModule.h" #include diff --git a/ScopeOneInference/src/OnnxRuntime.cpp b/ScopeOneCore/src/inference/OnnxRuntime.cpp similarity index 95% rename from ScopeOneInference/src/OnnxRuntime.cpp rename to ScopeOneCore/src/inference/OnnxRuntime.cpp index 1127195..caa80bf 100644 --- a/ScopeOneInference/src/OnnxRuntime.cpp +++ b/ScopeOneCore/src/inference/OnnxRuntime.cpp @@ -1,4 +1,4 @@ -#include "OnnxInternal.h" +#include "internal/inference/OnnxInternal.h" #include #include diff --git a/ScopeOneInference/src/OnnxSession.cpp b/ScopeOneCore/src/inference/OnnxSession.cpp similarity index 99% rename from ScopeOneInference/src/OnnxSession.cpp rename to ScopeOneCore/src/inference/OnnxSession.cpp index 8ef4c9b..8c22634 100644 --- a/ScopeOneInference/src/OnnxSession.cpp +++ b/ScopeOneCore/src/inference/OnnxSession.cpp @@ -1,4 +1,4 @@ -#include "OnnxInternal.h" +#include "internal/inference/OnnxInternal.h" #include #include diff --git a/ScopeOneCuda/CMakeLists.txt b/ScopeOneCuda/CMakeLists.txt deleted file mode 100644 index ab0b249..0000000 --- a/ScopeOneCuda/CMakeLists.txt +++ /dev/null @@ -1,100 +0,0 @@ -cmake_minimum_required(VERSION 3.23) - -include(GNUInstallDirs) -include(CMakePackageConfigHelpers) -set(CMAKE_AUTOMOC ON) - -set(SCOPEONE_CUDA_VERSION 1.0.0) - -add_library(ScopeOneCuda SHARED - src/CudaKernels.cu - src/CudaDevice.cpp - src/CudaFrequencyFilterModule.cpp - src/CudaFrequencyFilterModule.h - src/CudaGaussianBlurModule.cpp - src/CudaGaussianBlurModule.h - src/CudaProcessingRegistration.cpp - plugin.json - src/CudaRealImageModule.cpp - src/GpuComplexFrame.cpp - src/GpuRealFrame.cpp - include/scopeone/cuda/CudaDevice.h - include/scopeone/cuda/CudaRealImageModule.h - include/scopeone/cuda/CudaKernelLaunch.h - include/scopeone/cuda/GpuComplexFrame.h - include/scopeone/cuda/CudaExport.h - include/scopeone/cuda/GpuRealFrame.h -) -add_library(scopeone::Cuda ALIAS ScopeOneCuda) -scopeone_plugin_manifest(ScopeOneCuda) - -set(SCOPEONE_CUDA_ARCHITECTURES "50;75;80;86;89" CACHE STRING - "Target CUDA architectures") -set_target_properties(ScopeOneCuda PROPERTIES - EXPORT_NAME Cuda - PREFIX "" - CUDA_SEPARABLE_COMPILATION OFF - CUDA_ARCHITECTURES "${SCOPEONE_CUDA_ARCHITECTURES}" - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins/processing" - LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins/processing" -) - -target_compile_features(ScopeOneCuda PUBLIC cxx_std_20) -target_compile_definitions(ScopeOneCuda PRIVATE SCOPEONE_CUDA_EXPORTS) -target_include_directories(ScopeOneCuda - PUBLIC - $ - $ -) -target_link_libraries(ScopeOneCuda - PUBLIC - scopeone::ScopeOneCore - CUDA::cudart - CUDA::cufft -) - -file(GLOB SCOPEONE_CUDA_RUNTIME_DLLS - "${CUDAToolkit_BIN_DIR}/cudart64_*.dll" - "${CUDAToolkit_BIN_DIR}/cufft64_*.dll" -) -if (WIN32) - foreach (_dll IN LISTS SCOPEONE_CUDA_RUNTIME_DLLS) - add_custom_command(TARGET ScopeOneCuda POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - "${_dll}" - "${CMAKE_BINARY_DIR}/plugins/processing" - ) - endforeach () -endif () - -install(TARGETS ScopeOneCuda - EXPORT ScopeOneCudaTargets - RUNTIME DESTINATION "plugins/processing" - LIBRARY DESTINATION "plugins/processing" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" -) -install(DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") -if (SCOPEONE_CUDA_RUNTIME_DLLS) - install(FILES ${SCOPEONE_CUDA_RUNTIME_DLLS} DESTINATION "plugins/processing") -endif () -install(EXPORT ScopeOneCudaTargets - FILE ScopeOneCudaTargets.cmake - NAMESPACE scopeone:: - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeOneCuda" -) - -configure_package_config_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/ScopeOneCudaConfig.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneCudaConfig.cmake" - INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeOneCuda" -) -write_basic_package_version_file( - "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneCudaConfigVersion.cmake" - VERSION ${SCOPEONE_CUDA_VERSION} - COMPATIBILITY SameMajorVersion -) -install(FILES - "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneCudaConfig.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneCudaConfigVersion.cmake" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeOneCuda" -) diff --git a/ScopeOneCuda/cmake/ScopeOneCudaConfig.cmake.in b/ScopeOneCuda/cmake/ScopeOneCudaConfig.cmake.in deleted file mode 100644 index dae2453..0000000 --- a/ScopeOneCuda/cmake/ScopeOneCudaConfig.cmake.in +++ /dev/null @@ -1,7 +0,0 @@ -@PACKAGE_INIT@ - -include(CMakeFindDependencyMacro) -find_dependency(ScopeOneCore CONFIG REQUIRED) -find_dependency(CUDAToolkit REQUIRED) - -include("${CMAKE_CURRENT_LIST_DIR}/ScopeOneCudaTargets.cmake") diff --git a/ScopeOneCuda/include/scopeone/cuda/CudaDevice.h b/ScopeOneCuda/include/scopeone/cuda/CudaDevice.h deleted file mode 100644 index d7c2998..0000000 --- a/ScopeOneCuda/include/scopeone/cuda/CudaDevice.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include "scopeone/cuda/CudaExport.h" - -namespace scopeone::cuda -{ - SCOPEONE_CUDA_EXPORT bool isCudaDeviceAvailable(); - SCOPEONE_CUDA_EXPORT int cudaDeviceCount(); -} diff --git a/ScopeOneCuda/include/scopeone/cuda/CudaExport.h b/ScopeOneCuda/include/scopeone/cuda/CudaExport.h deleted file mode 100644 index 68baad4..0000000 --- a/ScopeOneCuda/include/scopeone/cuda/CudaExport.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#if defined(_WIN32) -# if defined(SCOPEONE_CUDA_EXPORTS) -# define SCOPEONE_CUDA_EXPORT __declspec(dllexport) -# else -# define SCOPEONE_CUDA_EXPORT __declspec(dllimport) -# endif -#else -# define SCOPEONE_CUDA_EXPORT __attribute__((visibility("default"))) -#endif diff --git a/ScopeOneCuda/plugin.json b/ScopeOneCuda/plugin.json deleted file mode 100644 index 100a469..0000000 --- a/ScopeOneCuda/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "scopeone.cuda", - "name": "ScopeOne CUDA Processing", - "version": "1.0.0", - "scopeOneApi": 2, - "kind": "processing" -} diff --git a/ScopeOneInference/CMakeLists.txt b/ScopeOneInference/CMakeLists.txt deleted file mode 100644 index 26333b3..0000000 --- a/ScopeOneInference/CMakeLists.txt +++ /dev/null @@ -1,117 +0,0 @@ -cmake_minimum_required(VERSION 3.23) - -if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) - project(ScopeOneInference VERSION 1.0.0 LANGUAGES CXX) - find_package(Qt6 REQUIRED COMPONENTS Core) - find_package(ScopeOneCore CONFIG REQUIRED) -endif () - -include(GNUInstallDirs) -include(CMakePackageConfigHelpers) -set(CMAKE_AUTOMOC ON) - -set(ONNXRUNTIME_ROOT - "${CMAKE_CURRENT_SOURCE_DIR}/../ScopeOneCore/external/onnxruntime-win-x64-gpu_cuda12-1.30.0" - CACHE PATH "ONNX Runtime SDK root" -) - -find_path(ONNXRUNTIME_INCLUDE_DIR - NAMES onnxruntime_cxx_api.h - PATHS "${ONNXRUNTIME_ROOT}/include" - NO_DEFAULT_PATH -) -find_library(ONNXRUNTIME_LIBRARY - NAMES onnxruntime - PATHS "${ONNXRUNTIME_ROOT}/lib" - NO_DEFAULT_PATH -) -if (NOT ONNXRUNTIME_INCLUDE_DIR OR NOT ONNXRUNTIME_LIBRARY) - message(STATUS "ONNX Runtime was not found under ${ONNXRUNTIME_ROOT}; skipping ScopeOneInference") - return() -endif () - -add_library(ScopeOneInference SHARED - src/OnnxInternal.h - src/OnnxRuntime.cpp - src/OnnxSession.cpp - src/OnnxInferenceModule.cpp - src/OnnxInferenceModule.h - src/InferenceProcessingRegistration.cpp - plugin.json - include/scopeone/inference/ExecutionProvider.h - include/scopeone/inference/InferenceExport.h - include/scopeone/inference/OnnxRuntime.h - include/scopeone/inference/OnnxSession.h - include/scopeone/inference/TensorInfo.h -) -add_library(scopeone::Inference ALIAS ScopeOneInference) -scopeone_plugin_manifest(ScopeOneInference) - -set_target_properties(ScopeOneInference PROPERTIES - EXPORT_NAME Inference - PREFIX "" - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins/processing" - LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins/processing" -) -target_compile_features(ScopeOneInference PUBLIC cxx_std_20) -target_compile_definitions(ScopeOneInference PRIVATE SCOPEONE_INFERENCE_EXPORTS) -target_include_directories(ScopeOneInference - PUBLIC - $ - $ - PRIVATE - "${ONNXRUNTIME_INCLUDE_DIR}" -) -target_link_libraries(ScopeOneInference PRIVATE - "${ONNXRUNTIME_LIBRARY}" - scopeone::ScopeOneCore - Qt6::Core -) - -if (WIN32) - file(GLOB ONNXRUNTIME_DLLS - "${ONNXRUNTIME_ROOT}/bin/onnxruntime*.dll" - "${ONNXRUNTIME_ROOT}/lib/onnxruntime*.dll" - ) - if (NOT ONNXRUNTIME_DLLS) - message(FATAL_ERROR "No ONNX Runtime DLLs found under ${ONNXRUNTIME_ROOT}") - endif () - add_custom_command(TARGET ScopeOneInference POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${ONNXRUNTIME_DLLS} - "${CMAKE_BINARY_DIR}/plugins/processing" - COMMAND_EXPAND_LISTS - ) -endif () - -install(TARGETS ScopeOneInference - EXPORT ScopeOneInferenceTargets - RUNTIME DESTINATION "plugins/processing" - LIBRARY DESTINATION "plugins/processing" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" -) -install(DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") -if (ONNXRUNTIME_DLLS) - install(FILES ${ONNXRUNTIME_DLLS} DESTINATION "plugins/processing") -endif () -install(EXPORT ScopeOneInferenceTargets - FILE ScopeOneInferenceTargets.cmake - NAMESPACE scopeone:: - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeOneInference" -) - -configure_package_config_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/ScopeOneInferenceConfig.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneInferenceConfig.cmake" - INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeOneInference" -) -write_basic_package_version_file( - "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneInferenceConfigVersion.cmake" - VERSION 1.0.0 - COMPATIBILITY SameMajorVersion -) -install(FILES - "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneInferenceConfig.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/ScopeOneInferenceConfigVersion.cmake" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeOneInference" -) diff --git a/ScopeOneInference/cmake/ScopeOneInferenceConfig.cmake.in b/ScopeOneInference/cmake/ScopeOneInferenceConfig.cmake.in deleted file mode 100644 index b2aada3..0000000 --- a/ScopeOneInference/cmake/ScopeOneInferenceConfig.cmake.in +++ /dev/null @@ -1,6 +0,0 @@ -@PACKAGE_INIT@ - -include(CMakeFindDependencyMacro) -find_dependency(ScopeOneCore CONFIG REQUIRED) - -include("${CMAKE_CURRENT_LIST_DIR}/ScopeOneInferenceTargets.cmake") diff --git a/ScopeOneInference/include/scopeone/inference/InferenceExport.h b/ScopeOneInference/include/scopeone/inference/InferenceExport.h deleted file mode 100644 index da8c8ef..0000000 --- a/ScopeOneInference/include/scopeone/inference/InferenceExport.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#if defined(_WIN32) -# if defined(SCOPEONE_INFERENCE_EXPORTS) -# define SCOPEONE_INFERENCE_EXPORT __declspec(dllexport) -# else -# define SCOPEONE_INFERENCE_EXPORT __declspec(dllimport) -# endif -#else -# define SCOPEONE_INFERENCE_EXPORT __attribute__((visibility("default"))) -#endif diff --git a/ScopeOneInference/plugin.json b/ScopeOneInference/plugin.json deleted file mode 100644 index 1b689ae..0000000 --- a/ScopeOneInference/plugin.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": "scopeone.inference", - "name": "ScopeOne Inference Processing", - "version": "1.0.0", - "scopeOneApi": 2, - "kind": "processing" -} diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 07bdd12..7ba4b30 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -9,6 +9,11 @@ if (APPLE) add_compile_options(-fexperimental-library) add_link_options(-fexperimental-library) endif () +# The Visual Studio generator compiles the sources of one project one after another unless the +# compiler runs them in parallel itself +if (MSVC) + add_compile_options(/MP) +endif () # A driver has the driver SDK and Qt Core and nothing else, so one that reaches for widgets, # OpenCV or Core itself does not build diff --git a/drivers/ExampleCamera/TestPatterns.h b/drivers/ExampleCamera/TestPatterns.h index 4d8601f..5370a51 100644 --- a/drivers/ExampleCamera/TestPatterns.h +++ b/drivers/ExampleCamera/TestPatterns.h @@ -21,7 +21,7 @@ namespace example_camera QString testPatternName(TestPattern pattern); bool testPatternFromName(const QString& name, TestPattern& pattern); - // The scenes the example camera can look at. A real plugin reads its images from the vendor SDK + // The scenes the example camera can look at. A real driver reads its images from the vendor SDK // instead. The scenes are laid out once for the size of the sensor and read from any thread. class TestPatterns { diff --git a/drivers/NIDaqmx/NIDaqmxDriver.cpp b/drivers/NIDaqmx/NIDaqmxDriver.cpp index 007f2a2..da62242 100644 --- a/drivers/NIDaqmx/NIDaqmxDriver.cpp +++ b/drivers/NIDaqmx/NIDaqmxDriver.cpp @@ -1500,7 +1500,7 @@ namespace ni_daqmx return QStringLiteral("scopeone.nidaqmx"); } - // Fails when the NI-DAQmx runtime is not installed, so the driver can be turned off under Drivers and Plugins + // Fails when the NI-DAQmx runtime is not installed, so the driver can be turned off under Tools > Drivers HardwareProviderPtr createProvider(const QJsonObject&, QString* errorMessage) override { QString error; diff --git a/drivers/README.md b/drivers/README.md index 8f3d44b..b6ce8ea 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -69,7 +69,7 @@ the drivers into `ScopeOneCore/install/bin/drivers`, from where the GUI build co } ``` -`scopeOneApi` is `ScopeOneDriverApiVersion` in `scopeone/PluginManifest.h`. +`scopeOneApi` is `ScopeOneDriverApiVersion` in `scopeone/DriverManifest.h`. `scopeone_add_driver` writes `scopeone_driver.json` from it with the Core version added, and the entry class names that file: @@ -95,5 +95,5 @@ needs. ## Installing a driver Copy the built library into `drivers/` beside the ScopeOne executable, or install it with -**Tools > Drivers and Plugins**, which puts it in the user's application data directory. Drivers +**Tools > Drivers**, which puts it in the user's application data directory. Drivers are enabled and configured there and take effect after a restart. diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt deleted file mode 100644 index c66646f..0000000 --- a/plugins/CMakeLists.txt +++ /dev/null @@ -1,37 +0,0 @@ -cmake_minimum_required(VERSION 3.23) -project(ScopeOnePlugins LANGUAGES CXX) - -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_AUTOMOC ON) - -# Xcode's libc++ hides std::jthread and std::stop_token unless the experimental library is enabled -if (APPLE) - add_compile_options(-fexperimental-library) - add_link_options(-fexperimental-library) -endif () - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) -find_package(ScopeOneCore CONFIG REQUIRED) -find_package(OpenCV CONFIG REQUIRED COMPONENTS core imgcodecs imgproc) -option(SCOPEONE_ENABLE_INFERENCE "Build the ONNX Runtime inference plugin when ONNX Runtime is available" ON) -option(SCOPEONE_ENABLE_CUDA "Build the CUDA plugins when nvcc is available" ON) -if (SCOPEONE_ENABLE_CUDA) - find_program(SCOPEONE_CUDA_NVCC_EXECUTABLE nvcc) -else () - unset(SCOPEONE_CUDA_NVCC_EXECUTABLE CACHE) -endif () - -if (SCOPEONE_ENABLE_INFERENCE) - add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../ScopeOneInference ScopeOneInference) -endif () -if (SCOPEONE_CUDA_NVCC_EXECUTABLE) - set(CMAKE_CUDA_COMPILER "${SCOPEONE_CUDA_NVCC_EXECUTABLE}" CACHE FILEPATH - "CUDA compiler selected by the environment" FORCE) - get_filename_component(_cuda_toolkit_root "${CMAKE_CUDA_COMPILER}" DIRECTORY) - get_filename_component(_cuda_toolkit_root "${_cuda_toolkit_root}" DIRECTORY) - set(CUDAToolkit_ROOT "${_cuda_toolkit_root}" CACHE PATH - "CUDA toolkit selected by the compiler" FORCE) - enable_language(CUDA) - find_package(CUDAToolkit REQUIRED) - add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../ScopeOneCuda ScopeOneCuda) -endif () diff --git a/plugins/README.md b/plugins/README.md deleted file mode 100644 index 05ae72e..0000000 --- a/plugins/README.md +++ /dev/null @@ -1,50 +0,0 @@ -# Extending ScopeOne - -ScopeOne has two public extension points. Both run outside the ScopeOne process, so a crash in an -extension does not take ScopeOne down. - -| Extension | You write | Runs in | Example | -|---|---|---|---| -| Driver | a `Driver` in C++ against `scopeone::DriverSDK` | `ScopeOne_DriverHost`, one process per device | `drivers/ExampleCamera`, `drivers/ExamplePmt` | -| Python processing module | a decorated Python function | its own Python process | `ScopeOneCore/python/scopeone/examples/processing_offset.py` | - -Drivers have their own folder, CMake project and guide, [drivers/README.md](../drivers/README.md). -This folder builds the CUDA and ONNX processing modules in `ScopeOneCuda` and -`ScopeOneInference`. Those two are built-in plugins, built and released with ScopeOne, because -they link CUDA and ONNX Runtime. Their interface, `scopeone/ProcessingPlugin.h`, changes with -ScopeOne and is not meant for code outside this repository, and ScopeOne loads native -processing plugins only from `plugins/processing` beside the executable, never from the user's -directory. Everything else, including the tool -windows and the PTU file reader, is part of ScopeOne itself. - -## Python processing modules - -```python -import numpy as np -from scopeone.processing import Integer, Real, Boolean, Choice, module - -@module("my.python.threshold", "My Threshold", Integer("level", "Level", 100, 0, 65535)) -def threshold(image, level, bits): - return np.where(image > level, (1 << bits) - 1, 0).astype(image.dtype) -``` - -The function gets a 2D `uint8` or `uint16` array and the parameters by name, and returns a 2D -`uint8` or `uint16` array. A function with a `bits` argument also gets the bits per sample of -the frame, 12 for a 12-bit camera in a `uint16` array, so `bits` is not a parameter key. Put -the script in `plugins/processing/python/` beside ScopeOne or in the same folder of the user's -plugin directory. The modules appear in the pipeline after a restart. ScopeOne ships no script -there; `ScopeOneCore/python/scopeone/examples/processing_offset.py` is one to copy. - -Each module in a pipeline runs in its own Python process (`python -m scopeone.processing -serve script.py`). That interpreter needs numpy and the `scopeone` package -(`pip install ScopeOneCore/python/scopeone`). Choose it under **Settings > Python -Interpreter**, or with `SCOPEONE_PYTHON`, which takes precedence; otherwise ScopeOne runs -`python3` (`python` on Windows). Output printed by a module goes to the log, and an exception -in it shows as the error of that pipeline step. - -ScopeOne first runs `python -m scopeone.processing describe script.py`, which prints one JSON -line with the modules of the script and their parameters. While the pipeline runs, each frame is -one request on stdin: a JSON line with `module`, `parameters`, `width`, `height`, `stride`, -`format` (`mono8` or `mono16`), `bits` and `bytes`, followed by that many pixel bytes. The reply -on stdout has the same form with `width`, `height`, `format` and `bytes`, or a JSON line with -`error`. diff --git a/scripts/build-unix.sh b/scripts/build-unix.sh index 8f605af..1e2d288 100755 --- a/scripts/build-unix.sh +++ b/scripts/build-unix.sh @@ -221,13 +221,6 @@ run cmake -S "$CORE_DIR" -B "$CORE_DIR/build" -DCMAKE_BUILD_TYPE="$BUILD_TYPE" " run cmake --build "$CORE_DIR/build" --parallel "$JOBS" run cmake --install "$CORE_DIR/build" -step "Building and installing ScopeOne plugins" -run cmake -S "$ROOT_DIR/plugins" -B "$ROOT_DIR/build/plugins" -DCMAKE_BUILD_TYPE="$BUILD_TYPE" \ - -DScopeOneCore_ROOT="$CORE_DIR/install" -DCMAKE_PREFIX_PATH="$CORE_DIR/install" \ - -DCMAKE_INSTALL_PREFIX="$CORE_DIR/install/bin" "${VCPKG_ARGS[@]}" -run cmake --build "$ROOT_DIR/build/plugins" --parallel "$JOBS" -run cmake --install "$ROOT_DIR/build/plugins" - step "Building and installing ScopeOne drivers" run cmake -S "$ROOT_DIR/drivers" -B "$ROOT_DIR/drivers/build" -DCMAKE_BUILD_TYPE="$BUILD_TYPE" \ -DScopeOneCore_ROOT="$CORE_DIR/install" -DCMAKE_PREFIX_PATH="$CORE_DIR/install" \ @@ -250,6 +243,19 @@ if [[ ! -f "$UTILITIES_ADAPTER" ]]; then fi run cp -L "$DEMO_ADAPTER" "$UTILITIES_ADAPTER" "$ROOT_DIR/build/" +# Core builds the CUDA and ONNX modules only when it finds their toolkits, so a missing one +# leaves them out without failing the build +core_module() { + local value + value="$(sed -n "s/^$1:INTERNAL=//p" "$CORE_DIR/build/CMakeCache.txt")" + if [[ "$value" == ON ]]; then + printf '%son%s' "$GREEN" "$RESET" + else + printf '%soff%s' "$YELLOW" "$RESET" + fi +} + step "Done" echo "GUI executable: $ROOT_DIR/build/ScopeOne" echo "Demo configuration: $ROOT_DIR/config/MMConfig_demo.cfg" +echo "Core modules: CUDA: $(core_module SCOPEONE_BUILT_WITH_CUDA), ONNX: $(core_module SCOPEONE_BUILT_WITH_INFERENCE)" diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 49aa8a1..ccd1629 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -3,7 +3,6 @@ $ErrorActionPreference = "Stop" $target = "all" $coreConfigureOption = @() $guiConfigureOption = @() -$pluginConfigureOption = @() $coreBuildOption = @() $guiBuildOption = @() $configure = $false @@ -43,10 +42,6 @@ for ($i = 0; $i -lt $args.Count; $i++) { $guiConfigureOption += Read-OptionValue -Arguments $args -Index ([ref]$i) -OptionName $arg continue } - { $_ -in @("--pluginConfigureOption", "-pluginConfigureOption") } { - $pluginConfigureOption += Read-OptionValue -Arguments $args -Index ([ref]$i) -OptionName $arg - continue - } { $_ -in @("--coreBuildOption", "-coreBuildOption") } { $coreBuildOption += Read-OptionValue -Arguments $args -Index ([ref]$i) -OptionName $arg continue @@ -81,10 +76,10 @@ for ($i = 0; $i -lt $args.Count; $i++) { } } -if ($target -notin @("all", "core", "gui", "plugins", "drivers", "scopewriter")) { - throw "Invalid target '$target'. Expected one of: all, core, gui, plugins, drivers, scopewriter." +if ($target -notin @("all", "core", "gui", "drivers", "scopewriter")) { + throw "Invalid target '$target'. Expected one of: all, core, gui, drivers, scopewriter." } -if ($package -and $target -in @("core", "plugins", "drivers", "scopewriter")) { +if ($package -and $target -in @("core", "drivers", "scopewriter")) { throw "--package requires --target gui or --target all." } if ($run -and $target -eq "scopewriter") { @@ -242,16 +237,13 @@ $writerBuildDir = Join-Path $writerBuildRoot "standalone" $writerInstallDir = Join-Path $writerSourceDir "install" $writerConsumerSourceDir = Join-Path $writerSourceDir "tests\consumer" $writerConsumerBuildDir = Join-Path $writerBuildRoot "consumer" -$pluginSourceDir = Join-Path $repoRoot "plugins" -$pluginBuildDir = Join-Path $repoRoot "build\plugins" -$pluginInstallDir = Join-Path $coreInstallDir "bin" +$driverInstallDir = Join-Path $coreInstallDir "bin" # Inside drivers rather than build, whose drivers folder is where the GUI build puts the drivers it runs $driverSourceDir = Join-Path $repoRoot "drivers" $driverBuildDir = Join-Path $driverSourceDir "build" $config = "Release" $coreCachePath = Join-Path $coreBuildDir "CMakeCache.txt" $guiCachePath = Join-Path $guiBuildDir "CMakeCache.txt" -$pluginCachePath = Join-Path $pluginBuildDir "CMakeCache.txt" $driverCachePath = Join-Path $driverBuildDir "CMakeCache.txt" $vcpkgConfigureArgs = @() @@ -301,10 +293,6 @@ if ($clean) { Write-Step "Removing GUI build directory" Remove-Item -LiteralPath $guiBuildDir -Recurse -Force } - if ($target -in @("all", "plugins") -and (Test-Path $pluginBuildDir)) { - Write-Step "Removing plugin build directory" - Remove-Item -LiteralPath $pluginBuildDir -Recurse -Force - } if ($target -in @("all", "drivers") -and (Test-Path $driverBuildDir)) { Write-Step "Removing driver build directory" Remove-Item -LiteralPath $driverBuildDir -Recurse -Force @@ -322,28 +310,6 @@ $guiConfigureOptionOverride = $guiConfigureOption.Count -gt 0 $needCoreConfigure = $configure -or $coreConfigureOptionOverride -or -not (Test-Path $coreCachePath) $needGuiConfigure = $configure -or $guiConfigureOptionOverride -or -not (Test-Path $guiCachePath) -$pluginBuildFilesExist = (Test-Path (Join-Path $pluginBuildDir "ALL_BUILD.vcxproj")) -or - (Test-Path (Join-Path $pluginBuildDir "build.ninja")) -or - (Test-Path (Join-Path $pluginBuildDir "Makefile")) -$needPluginConfigure = $configure -or $pluginConfigureOption.Count -gt 0 -or -not (Test-Path $pluginCachePath) -or - -not $pluginBuildFilesExist -$pluginCachedInstallPrefix = Normalize-CMakePath ( - Get-CMakeCacheValue -CachePath $pluginCachePath -Key "CMAKE_INSTALL_PREFIX") -$pluginCachedSourceDir = Normalize-CMakePath ( - Get-CMakeCacheValue -CachePath $pluginCachePath -Key "CMAKE_HOME_DIRECTORY") -if ($pluginCachedSourceDir -and - $pluginCachedSourceDir -ne (Normalize-CMakePath $pluginSourceDir)) { - $needPluginConfigure = $true - if (Test-Path $pluginBuildDir) { - Write-Step "Removing stale plugin build directory" - Remove-Item -LiteralPath $pluginBuildDir -Recurse -Force - } -} -if ($pluginCachedInstallPrefix -and - $pluginCachedInstallPrefix -ne (Normalize-CMakePath $pluginInstallDir)) { - $needPluginConfigure = $true -} - $driverBuildFilesExist = (Test-Path (Join-Path $driverBuildDir "ALL_BUILD.vcxproj")) -or (Test-Path (Join-Path $driverBuildDir "build.ninja")) -or (Test-Path (Join-Path $driverBuildDir "Makefile")) @@ -351,7 +317,7 @@ $needDriverConfigure = $configure -or -not (Test-Path $driverCachePath) -or -not $driverCachedInstallPrefix = Normalize-CMakePath ( Get-CMakeCacheValue -CachePath $driverCachePath -Key "CMAKE_INSTALL_PREFIX") if ($driverCachedInstallPrefix -and - $driverCachedInstallPrefix -ne (Normalize-CMakePath $pluginInstallDir)) { + $driverCachedInstallPrefix -ne (Normalize-CMakePath $driverInstallDir)) { $needDriverConfigure = $true } @@ -444,6 +410,17 @@ if (-not $needCoreConfigure -and $target -in @("all", "core")) { } if ($target -in @("all", "core")) { + # Core builds the CUDA processing modules, which need the CUDA toolset. A generator keeps the + # toolset it was first configured with, so a build directory made without it is configured + # again from scratch. + $coreToolset = if ($env:CUDA_PATH) { "cuda=$env:CUDA_PATH" } else { "" } + if ($coreToolset -and (Test-Path $coreCachePath) -and + (Get-CMakeCacheValue -CachePath $coreCachePath -Key "CMAKE_GENERATOR_TOOLSET") -ne $coreToolset) { + Write-Step "Removing ScopeOneCore build directory configured without the CUDA toolset" + Remove-Item -LiteralPath $coreBuildDir -Recurse -Force + $needCoreConfigure = $true + } + if ($needCoreConfigure) { $coreConfigureArgs = @( "-S", $coreSourceDir, @@ -455,6 +432,9 @@ if ($target -in @("all", "core")) { } $coreConfigureArgs += $vcpkgConfigureArgs + $coreConfigureOption + if ($coreToolset) { + $coreConfigureArgs += @("-T", $coreToolset) + } Invoke-Step ` -Label "Configuring ScopeOneCore" ` @@ -487,55 +467,6 @@ if ($target -in @("all", "core")) { } } -if ($target -in @("all", "plugins")) { - $coreConfigFile = Join-Path $coreInstallDir "lib\cmake\ScopeOneCore\ScopeOneCoreConfig.cmake" - if (-not (Test-Path $coreConfigFile)) { - throw "ScopeOneCore is not installed at $coreInstallDir. Build the core first." - } - - if ($needPluginConfigure) { - $pluginConfigureArgs = @( - "-S", $pluginSourceDir, - "-B", $pluginBuildDir, - "-DScopeOneCore_ROOT=$coreInstallDir", - "-DCMAKE_PREFIX_PATH=$coreInstallDir", - "-DCMAKE_INSTALL_PREFIX=$pluginInstallDir" - ) - $pluginConfigureArgs += $vcpkgConfigureArgs + $pluginConfigureOption - if ($env:CUDA_PATH) { - $pluginConfigureArgs += @("-T", "cuda=$env:CUDA_PATH") - } - Invoke-Step ` - -Label "Configuring ScopeOne plugins" ` - -FilePath $cmake ` - -Arguments $pluginConfigureArgs ` - -WorkingDirectory $repoRoot - } - - Invoke-Step ` - -Label "Building ScopeOne plugins ($config)" ` - -FilePath $cmake ` - -Arguments @( - "--build", $pluginBuildDir, - "--config", $config, - "--parallel" - ) ` - -WorkingDirectory $repoRoot - - Invoke-Step ` - -Label "Installing ScopeOne plugins" ` - -FilePath $cmake ` - -Arguments @( - "--install", $pluginBuildDir, - "--config", $config - ) ` - -WorkingDirectory $repoRoot - - if ($target -eq "all") { - $needGuiConfigure = $true - } -} - if ($target -in @("all", "drivers")) { $coreConfigFile = Join-Path $coreInstallDir "lib\cmake\ScopeOneCore\ScopeOneCoreConfig.cmake" if (-not (Test-Path $coreConfigFile)) { @@ -551,7 +482,7 @@ if ($target -in @("all", "drivers")) { "-B", $driverBuildDir, "-DScopeOneCore_ROOT=$coreInstallDir", "-DCMAKE_PREFIX_PATH=$coreInstallDir", - "-DCMAKE_INSTALL_PREFIX=$pluginInstallDir" + "-DCMAKE_INSTALL_PREFIX=$driverInstallDir" ) + $vcpkgConfigureArgs) ` -WorkingDirectory $repoRoot } @@ -653,8 +584,22 @@ if ($target -eq "scopewriter") { } else { Write-Host "ScopeOneCore install: $coreInstallDir" - if ($target -in @("all", "plugins")) { - Write-Host "Plugins: $pluginBuildDir" + # Core builds the CUDA and ONNX modules only when it finds their toolkits, so a missing one + # leaves them out without failing the build + if (Test-Path $coreCachePath) { + Write-Host -NoNewline "Core modules: CUDA: " + foreach ($module in @( + @{ Key = "SCOPEONE_BUILT_WITH_CUDA"; Next = ", ONNX: " }, + @{ Key = "SCOPEONE_BUILT_WITH_INFERENCE"; Next = "" })) { + if ((Get-CMakeCacheValue -CachePath $coreCachePath -Key $module.Key) -eq "ON") { + Write-Host -NoNewline "on" -ForegroundColor Green + } + else { + Write-Host -NoNewline "off" -ForegroundColor Yellow + } + Write-Host -NoNewline $module.Next + } + Write-Host "" } if ($target -in @("all", "drivers")) { Write-Host "Drivers: $driverBuildDir" diff --git a/src/PluginManagerDialog.cpp b/src/DriversDialog.cpp similarity index 53% rename from src/PluginManagerDialog.cpp rename to src/DriversDialog.cpp index c358102..2ff3d00 100644 --- a/src/PluginManagerDialog.cpp +++ b/src/DriversDialog.cpp @@ -1,10 +1,6 @@ -#include "PluginManagerDialog.h" +#include "DriversDialog.h" -#include "scopeone/PluginManifest.h" -#include "scopeone/DaqDevice.h" -#include "scopeone/Driver.h" -#include "scopeone/ProcessingPlugin.h" -#include "scopeone/SignalSource.h" +#include "scopeone/DriverManifest.h" #include "scopeone/ScopeOneCore.h" #include @@ -30,87 +26,72 @@ namespace scopeone::ui namespace { constexpr int kIdRole = Qt::UserRole; - constexpr int kKindRole = Qt::UserRole + 1; - constexpr int kStatusRole = Qt::UserRole + 2; - constexpr int kMetadataRole = Qt::UserRole + 3; + constexpr int kStatusRole = Qt::UserRole + 1; + constexpr int kMetadataRole = Qt::UserRole + 2; - using scopeone::core::DiscoveredPlugin; - - QList discoverPlugins() - { - QList plugins = scopeone::core::discoverPlugins( - scopeone::core::PluginKind::Processing); - plugins.append(scopeone::core::discoverPlugins(scopeone::core::PluginKind::Driver)); - return plugins; - } + using scopeone::core::DiscoveredDriver; + using scopeone::core::discoverDrivers; + using scopeone::core::driverSettingsKey; } QStringList loadConfiguredDrivers(scopeone::core::ScopeOneCore& core) { QStringList errors; QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - for (const DiscoveredPlugin& plugin : discoverPlugins()) + for (const DiscoveredDriver& driver : discoverDrivers()) { - if (plugin.expectedKind != scopeone::core::PluginKind::Driver) - { - continue; - } - if (plugin.interfaceId != QStringLiteral(ScopeOneDriver_iid)) - { - continue; - } - if (!plugin.error.isEmpty()) + if (!driver.error.isEmpty()) { errors.append(QStringLiteral("%1: %2") - .arg(QFileInfo(plugin.path).fileName(), plugin.error)); + .arg(QFileInfo(driver.path).fileName(), driver.error)); continue; } - const QString id = plugin.manifest.id; - if (!scopeone::core::pluginEnabled(plugin.manifest)) + const QString id = driver.manifest.id; + if (!scopeone::core::driverEnabled(driver.manifest)) { continue; } - const QString providerId = plugin.manifest.metadata + const QString providerId = driver.manifest.metadata .value(QStringLiteral("providerId")) .toString().trimmed(); if (providerId != id) { errors.append(QStringLiteral("%1: providerId must match the driver id") - .arg(QFileInfo(plugin.path).fileName())); + .arg(QFileInfo(driver.path).fileName())); continue; } const QVariantMap options = settings - .value(scopeone::core::pluginSettingsKey(id, QStringLiteral("options"))) + .value(driverSettingsKey(id, QStringLiteral("options"))) .toMap(); QString error; - if (!core.registerDriverHostProvider(providerId, plugin.path, options, &error)) + if (!core.registerDriverHostProvider(providerId, driver.path, options, &error)) { errors.append(QStringLiteral("%1: %2") - .arg(QFileInfo(plugin.path).fileName(), error)); + .arg(QFileInfo(driver.path).fileName(), error)); } } return errors; } - PluginManagerDialog::PluginManagerDialog(QWidget* parent) + DriversDialog::DriversDialog(QWidget* parent) : QDialog(parent) { - setWindowTitle(tr("Drivers and Plugins")); + setWindowTitle(tr("Drivers")); resize(900, 600); auto* layout = new QVBoxLayout(this); m_table = new QTableWidget(this); - m_table->setColumnCount(6); + m_table->setColumnCount(5); m_table->setHorizontalHeaderLabels( - {tr("Enabled"), tr("Name"), tr("Type"), tr("Version"), tr("Status"), tr("Location")}); + {tr("Enabled"), tr("Name"), tr("Version"), tr("Status"), tr("Location")}); m_table->setSelectionBehavior(QAbstractItemView::SelectRows); m_table->setSelectionMode(QAbstractItemView::SingleSelection); m_table->setEditTriggers(QAbstractItemView::NoEditTriggers); m_table->verticalHeader()->setVisible(false); m_table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); - m_table->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch); + m_table->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch); layout->addWidget(m_table, 1); layout->addWidget(new QLabel(tr("Metadata and diagnostics"), this)); @@ -130,53 +111,48 @@ namespace scopeone::ui auto* installButton = buttons->addButton(tr("Install Driver..."), QDialogButtonBox::ActionRole); layout->addWidget(buttons); connect(buttons->button(QDialogButtonBox::Save), &QPushButton::clicked, - this, &PluginManagerDialog::saveSettings); + this, &DriversDialog::saveSettings); connect(installButton, &QPushButton::clicked, - this, &PluginManagerDialog::installPlugin); + this, &DriversDialog::installDriver); connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(m_table, &QTableWidget::itemSelectionChanged, - this, &PluginManagerDialog::showSelectedOptions); - refreshPlugins(); + this, &DriversDialog::showSelectedOptions); + refreshDrivers(); } - void PluginManagerDialog::refreshPlugins() + void DriversDialog::refreshDrivers() { QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - const QList plugins = discoverPlugins(); - m_table->setRowCount(plugins.size()); - for (int row = 0; row < plugins.size(); ++row) + const QList drivers = discoverDrivers(); + m_table->setRowCount(drivers.size()); + for (int row = 0; row < drivers.size(); ++row) { - const DiscoveredPlugin& plugin = plugins.at(row); - const bool driver = plugin.expectedKind == scopeone::core::PluginKind::Driver; + const DiscoveredDriver& driver = drivers.at(row); auto* enabled = new QTableWidgetItem(); - enabled->setData(kIdRole, plugin.manifest.id); - enabled->setData(kKindRole, static_cast(plugin.expectedKind)); - const QString status = plugin.error.isEmpty() ? tr("Ready") : plugin.error; + enabled->setData(kIdRole, driver.manifest.id); + const QString status = driver.error.isEmpty() ? tr("Ready") : driver.error; enabled->setData(kStatusRole, status); enabled->setData( kMetadataRole, - QString::fromUtf8(QJsonDocument(plugin.metadata).toJson(QJsonDocument::Indented))); - if (driver && plugin.error.isEmpty()) + QString::fromUtf8(QJsonDocument(driver.metadata).toJson(QJsonDocument::Indented))); + if (driver.error.isEmpty()) { enabled->setFlags(enabled->flags() | Qt::ItemIsUserCheckable); - const QString key = scopeone::core::pluginSettingsKey( - plugin.manifest.id, QStringLiteral("enabled")); + const QString key = driverSettingsKey( + driver.manifest.id, QStringLiteral("enabled")); const bool checked = settings.contains(key) ? settings.value(key).toBool() - : plugin.manifest.autoLoad; + : driver.manifest.autoLoad; enabled->setCheckState(checked ? Qt::Checked : Qt::Unchecked); } m_table->setItem(row, 0, enabled); m_table->setItem(row, 1, new QTableWidgetItem( - plugin.manifest.name.isEmpty() - ? QFileInfo(plugin.path).completeBaseName() - : plugin.manifest.name)); - m_table->setItem(row, 2, new QTableWidgetItem( - scopeone::core::pluginKindName(plugin.expectedKind))); - m_table->setItem(row, 3, new QTableWidgetItem(plugin.manifest.version)); - m_table->setItem(row, 4, new QTableWidgetItem( - status)); - m_table->setItem(row, 5, new QTableWidgetItem(plugin.path)); + driver.manifest.name.isEmpty() + ? QFileInfo(driver.path).completeBaseName() + : driver.manifest.name)); + m_table->setItem(row, 2, new QTableWidgetItem(driver.manifest.version)); + m_table->setItem(row, 3, new QTableWidgetItem(status)); + m_table->setItem(row, 4, new QTableWidgetItem(driver.path)); } if (m_table->rowCount() > 0) { @@ -184,7 +160,7 @@ namespace scopeone::ui } } - void PluginManagerDialog::showSelectedOptions() + void DriversDialog::showSelectedOptions() { const int row = m_table->currentRow(); if (row < 0) @@ -194,16 +170,13 @@ namespace scopeone::ui return; } const QTableWidgetItem* item = m_table->item(row, 0); - const auto kind = static_cast(item->data(kKindRole).toInt()); const QString id = item->data(kIdRole).toString(); const QString status = item->data(kStatusRole).toString(); m_detailsEdit->setPlainText( item->data(kMetadataRole).toString() + QStringLiteral("\n\n") + tr("Status: %1").arg(status)); - const bool driver = kind == scopeone::core::PluginKind::Driver - && !id.isEmpty() - && status == tr("Ready"); + const bool driver = !id.isEmpty() && status == tr("Ready"); m_optionsEdit->setEnabled(driver); if (!driver) { @@ -212,13 +185,13 @@ namespace scopeone::ui } QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); const QJsonObject options = QJsonObject::fromVariantMap( - settings.value(scopeone::core::pluginSettingsKey( + settings.value(driverSettingsKey( id, QStringLiteral("options"))).toMap()); m_optionsEdit->setPlainText(QString::fromUtf8( QJsonDocument(options).toJson(QJsonDocument::Indented))); } - void PluginManagerDialog::installPlugin() + void DriversDialog::installDriver() { const QString sourceDirectory = QFileDialog::getExistingDirectory( this, tr("Install Driver")); @@ -227,11 +200,8 @@ namespace scopeone::ui return; } - // Only drivers are installed here. Native processing plugins come with ScopeOne, and - // ScopeOne does not load them from the user directory. - const scopeone::core::PluginKind kind = scopeone::core::PluginKind::Driver; - QList candidates; - for (const auto& candidate : scopeone::core::discoverPlugins(kind, {sourceDirectory})) + QList candidates; + for (const auto& candidate : discoverDrivers({sourceDirectory})) { if (candidate.error.isEmpty()) { @@ -240,42 +210,41 @@ namespace scopeone::ui } if (candidates.size() != 1) { - QMessageBox::warning(this, tr("Drivers and Plugins"), + QMessageBox::warning(this, tr("Drivers"), tr("The folder must contain one driver library.")); return; } QDir userRoot(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)); - const QString pluginDirectory = userRoot.filePath( - scopeone::core::pluginDirectoryName(kind)); - if (!QDir().mkpath(pluginDirectory)) + const QString driverDirectory = userRoot.filePath(QStringLiteral("drivers")); + if (!QDir().mkpath(driverDirectory)) { - QMessageBox::warning(this, tr("Drivers and Plugins"), tr("The install directory could not be created.")); + QMessageBox::warning(this, tr("Drivers"), tr("The install directory could not be created.")); return; } if (QFileInfo(sourceDirectory).canonicalFilePath() - == QFileInfo(pluginDirectory).canonicalFilePath()) + == QFileInfo(driverDirectory).canonicalFilePath()) { - QMessageBox::information(this, tr("Drivers and Plugins"), tr("This is already installed.")); + QMessageBox::information(this, tr("Drivers"), tr("This is already installed.")); return; } for (const QFileInfo& source : QDir(sourceDirectory).entryInfoList(QDir::Files)) { - const QString destination = QDir(pluginDirectory).filePath(source.fileName()); + const QString destination = QDir(driverDirectory).filePath(source.fileName()); QFile::remove(destination); if (!QFile::copy(source.absoluteFilePath(), destination)) { - QMessageBox::warning(this, tr("Drivers and Plugins"), + QMessageBox::warning(this, tr("Drivers"), tr("The files could not be installed.")); return; } } - refreshPlugins(); - QMessageBox::information(this, tr("Drivers and Plugins"), + refreshDrivers(); + QMessageBox::information(this, tr("Drivers"), tr("It will be available after ScopeOne restarts.")); } - void PluginManagerDialog::saveSettings() + void DriversDialog::saveSettings() { const int row = m_table->currentRow(); if (row < 0) @@ -283,10 +252,9 @@ namespace scopeone::ui return; } QTableWidgetItem* item = m_table->item(row, 0); - const auto kind = static_cast(item->data(kKindRole).toInt()); const QString id = item->data(kIdRole).toString(); QVariantMap selectedOptions; - if (kind == scopeone::core::PluginKind::Driver && !id.isEmpty()) + if (!id.isEmpty()) { QJsonParseError parseError; QByteArray optionJson = m_optionsEdit->toPlainText().trimmed().toUtf8(); @@ -297,32 +265,30 @@ namespace scopeone::ui const QJsonDocument options = QJsonDocument::fromJson(optionJson, &parseError); if (parseError.error != QJsonParseError::NoError || !options.isObject()) { - QMessageBox::warning(this, tr("Drivers and Plugins"), tr("Driver options must be a JSON object.")); + QMessageBox::warning(this, tr("Drivers"), tr("Driver options must be a JSON object.")); return; } selectedOptions = options.object().toVariantMap(); } QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - for (int pluginRow = 0; pluginRow < m_table->rowCount(); ++pluginRow) + for (int driverRow = 0; driverRow < m_table->rowCount(); ++driverRow) { - QTableWidgetItem* pluginItem = m_table->item(pluginRow, 0); - const auto pluginKind = static_cast( - pluginItem->data(kKindRole).toInt()); - const QString pluginId = pluginItem->data(kIdRole).toString(); - if (pluginKind == scopeone::core::PluginKind::Driver && !pluginId.isEmpty()) + QTableWidgetItem* driverItem = m_table->item(driverRow, 0); + const QString driverId = driverItem->data(kIdRole).toString(); + if (!driverId.isEmpty()) { - settings.setValue(scopeone::core::pluginSettingsKey( - pluginId, QStringLiteral("enabled")), - pluginItem->checkState() == Qt::Checked); + settings.setValue(driverSettingsKey( + driverId, QStringLiteral("enabled")), + driverItem->checkState() == Qt::Checked); } } - if (kind == scopeone::core::PluginKind::Driver && !id.isEmpty()) + if (!id.isEmpty()) { - settings.setValue(scopeone::core::pluginSettingsKey( + settings.setValue(driverSettingsKey( id, QStringLiteral("options")), selectedOptions); } - QMessageBox::information(this, tr("Drivers and Plugins"), + QMessageBox::information(this, tr("Drivers"), tr("The settings will take effect after ScopeOne restarts.")); } } diff --git a/src/PluginManagerDialog.h b/src/DriversDialog.h similarity index 74% rename from src/PluginManagerDialog.h rename to src/DriversDialog.h index 7b833f8..54b8cfb 100644 --- a/src/PluginManagerDialog.h +++ b/src/DriversDialog.h @@ -15,16 +15,16 @@ namespace scopeone::ui { QStringList loadConfiguredDrivers(scopeone::core::ScopeOneCore& core); - class PluginManagerDialog final : public QDialog + class DriversDialog final : public QDialog { Q_OBJECT public: - explicit PluginManagerDialog(QWidget* parent = nullptr); + explicit DriversDialog(QWidget* parent = nullptr); private: - void refreshPlugins(); - void installPlugin(); + void refreshDrivers(); + void installDriver(); void showSelectedOptions(); void saveSettings(); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 71ecbcd..238695e 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -6,7 +6,6 @@ #include "WelcomeOverlay.h" #include "scopeone/ScopeOneCore.h" -#include "scopeone/PluginManifest.h" #include "AboutDialog.h" #include "Appearance.h" #include "InspectWidget.h" @@ -21,7 +20,7 @@ #include "ImageProcessingWidget.h" #include "RecordingWidget.h" #include "SettingsDialog.h" -#include "PluginManagerDialog.h" +#include "DriversDialog.h" #include "ScopeOneLocalApiServer.h" #include @@ -1485,10 +1484,10 @@ namespace scopeone::ui }); m_toolRegistry->populateMenu(toolsMenu, this); toolsMenu->addSeparator(); - auto* pluginManagerAction = toolsMenu->addAction(tr("&Drivers and Plugins...")); - connect(pluginManagerAction, &QAction::triggered, this, [this]() + auto* driversAction = toolsMenu->addAction(tr("&Drivers...")); + connect(driversAction, &QAction::triggered, this, [this]() { - PluginManagerDialog(this).exec(); + DriversDialog(this).exec(); }); auto* settingsAction = toolsMenu->addAction(tr("&Settings...")); connect(settingsAction, &QAction::triggered, diff --git a/src/main.cpp b/src/main.cpp index 5f2e813..a8473cc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,7 @@ #include "ConsoleWidget.h" #include "Appearance.h" #include "MainWindow.h" -#include "PluginManagerDialog.h" +#include "DriversDialog.h" int main(int argc, char *argv[]) { diff --git a/src/tools/daq/DaqControlWidget.cpp b/src/tools/daq/DaqControlWidget.cpp index f1c81c0..6d9c5f5 100644 --- a/src/tools/daq/DaqControlWidget.cpp +++ b/src/tools/daq/DaqControlWidget.cpp @@ -372,7 +372,7 @@ namespace scopeone::ui } m_statusLabel->setText(available ? m_core->daqStateMessage(device.id) - : tr("Install a DAQ device plugin and driver")); + : tr("Install a DAQ driver")); setControlsEnabled(available); m_stopButton->setEnabled(false); } From 928cb79c98d46f76d5d09d974d2450e74d8507c1 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:48:50 +0100 Subject: [PATCH 12/49] Return the result of QHash::remove directly --- ScopeOneCore/src/ImageProcessingFramework.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ScopeOneCore/src/ImageProcessingFramework.cpp b/ScopeOneCore/src/ImageProcessingFramework.cpp index 91be9bf..a0ef803 100644 --- a/ScopeOneCore/src/ImageProcessingFramework.cpp +++ b/ScopeOneCore/src/ImageProcessingFramework.cpp @@ -261,7 +261,7 @@ namespace scopeone::core::internal bool ImageProcessingManager::removeCameraDefinition(const QString& cameraId) { QMutexLocker locker(&m_definitionsMutex); - return m_cameraDefinitions.remove(cameraId.trimmed()) > 0; + return m_cameraDefinitions.remove(cameraId.trimmed()); } QStringList ImageProcessingManager::cameraDefinitionIds() const From b96a90539c4bd754c63f5381decbb738a0ae55a4 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 15:09:03 +0100 Subject: [PATCH 13/49] Set up a new Windows machine with one build command --- .github/workflows/compile-check.yml | 18 +-- .github/workflows/windows-release.yml | 5 +- .gitignore | 1 + CONTRIBUTING.md | 5 +- README.md | 40 +++--- build.cmd | 5 + scripts/build.ps1 | 199 ++++++++++++++++++++++++-- 7 files changed, 220 insertions(+), 53 deletions(-) create mode 100644 build.cmd diff --git a/.github/workflows/compile-check.yml b/.github/workflows/compile-check.yml index d4dcde9..2c7ce31 100644 --- a/.github/workflows/compile-check.yml +++ b/.github/workflows/compile-check.yml @@ -107,10 +107,6 @@ jobs: runs-on: windows-2022 timeout-minutes: 180 - env: - # Keep in sync with windows-release.yml - MMCORE_COMMIT: 212cc416eec291634c916078a31efd4c5f914c64 - steps: - name: Check out ScopeOne uses: actions/checkout@v7 @@ -147,18 +143,8 @@ jobs: key: vcpkg-scopeone-x64-windows-${{ hashFiles('vcpkg.json') }} restore-keys: vcpkg-scopeone-x64-windows- - - name: Build MMCore - shell: pwsh - run: | - $mm = "$env:GITHUB_WORKSPACE\ScopeOneCore\external\mmCoreAndDevices" - git init $mm - git -C $mm fetch --depth 1 https://github.com/micro-manager/mmCoreAndDevices.git $env:MMCORE_COMMIT - git -C $mm checkout FETCH_HEAD - msbuild "$mm\MMCore\MMCore.vcxproj" /p:Configuration=Release /p:Platform=x64 "/p:SolutionDir=$mm\" /m /nologo /v:minimal - if ($LASTEXITCODE -ne 0) { throw "Building MMCore failed." } - if (-not (Test-Path "$mm\build\Release\x64\MMCore.lib")) { throw "MMCore.lib was not produced." } - - - name: Compile Core, drivers and GUI + # build.ps1 clones the latest mmCoreAndDevices and builds MMCore + - name: Compile MMCore, Core, drivers and GUI shell: pwsh run: | .\scripts\build.ps1 --configure ` diff --git a/.github/workflows/windows-release.yml b/.github/workflows/windows-release.yml index 2dc44d0..52853c3 100644 --- a/.github/workflows/windows-release.yml +++ b/.github/workflows/windows-release.yml @@ -16,7 +16,6 @@ concurrency: cancel-in-progress: false env: - MMCORE_COMMIT: 212cc416eec291634c916078a31efd4c5f914c64 BOOST_VERSION: 1.86.0 CUDA_VERSION: 12.8.1 ONNXRUNTIME_PACKAGE: onnxruntime-win-x64-gpu_cuda12-1.30.0 @@ -125,9 +124,7 @@ jobs: shell: pwsh run: | $mm = "$env:GITHUB_WORKSPACE\ScopeOneCore\external\mmCoreAndDevices" - git init $mm - git -C $mm fetch --depth 1 https://github.com/micro-manager/mmCoreAndDevices.git $env:MMCORE_COMMIT - git -C $mm checkout FETCH_HEAD + git clone --depth 1 https://github.com/micro-manager/mmCoreAndDevices.git $mm $boostRoot = "$env:RUNNER_TEMP\boost" $properties = @( "/p:Configuration=Release", diff --git a/.gitignore b/.gitignore index 74c17b7..7c298aa 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ !/config/MMConfig_demo_dual.cfg !/CMakeLists.txt +!/build.cmd !/README.md !/CONTRIBUTING.md !/LICENSE diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f6ca056..36f25dc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,7 +1,8 @@ # Development guidelines -These rules apply to everyone who changes ScopeOne. The README explains how to build; this file -explains how the code is meant to be written. Each rule says why. When a rule and its reason +These rules apply to everyone who changes ScopeOne. The README explains how to build: on Windows, +`.\build.cmd --run` after installing Git and the Visual Studio Build Tools, and on Linux and +macOS `./scripts/build-unix.sh`. This file explains how the code is meant to be written. Each rule says why. When a rule and its reason disagree in a specific case, follow the reason and say so in the review. ## Boundaries diff --git a/README.md b/README.md index c6f0ea3..908817c 100644 --- a/README.md +++ b/README.md @@ -55,12 +55,13 @@ Read [CONTRIBUTING.md](CONTRIBUTING.md) before changing the code. **Prerequisites:** -- [CMake](https://cmake.org/download/) 3.23 or newer -- A C++20 compiler: [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) (MSVC v143 toolset) on Windows, GCC or Clang on Linux and macOS (experimental) -- [vcpkg](https://github.com/microsoft/vcpkg) for Qt 6.11.2, OpenCV 4.14.0, and ScopeWriter's libtiff, zlib, zstd and crc32c -- Git, for the submodules (including mmCoreAndDevices) +- Git, for the ScopeWriter submodule and Micro-Manager +- A C++20 compiler: [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) or its Build Tools (MSVC v143 toolset) on Windows, GCC or Clang on Linux and macOS (experimental) +- [CMake](https://cmake.org/download/) 3.23 or newer, which comes with the C++ tools of Visual Studio on Windows - Python 3.14, only for the Python package and Python processing modules +The build scripts set up the rest. They download [vcpkg](https://github.com/microsoft/vcpkg) when `VCPKG_ROOT` does not name a checkout, and vcpkg builds Qt 6.11.2, OpenCV 4.14.0 and ScopeWriter's libtiff, zlib, zstd and crc32c inside the repository. They also fetch and build Micro-Manager's core with the DemoCamera and Utilities adapters. + Clone ScopeOne with its submodules, or initialize them in an existing checkout: ```powershell @@ -75,8 +76,8 @@ ScopeOne/ ScopeOneCore/ include/scopeone/ Core API and the contracts of the application external/ - mmCoreAndDevices/ - ScopeWriter/ + mmCoreAndDevices/ fetched by the build scripts + ScopeWriter/ submodule ``` ### Drivers and processing modules @@ -85,23 +86,28 @@ ScopeOne has two public extension points, drivers and Python processing modules, **Windows Build Steps:** -Set up vcpkg once outside the ScopeOne repository: +On a new Windows machine, install Git and the Visual Studio 2022 Build Tools with the C++ workload, which bring MSVC, MSBuild, the Windows SDK and CMake. An existing Visual Studio 2022 with "Desktop development with C++" works as well. ```powershell -git clone https://github.com/microsoft/vcpkg.git C:/dev/vcpkg -C:/dev/vcpkg/bootstrap-vcpkg.bat -$env:VCPKG_ROOT = "C:/dev/vcpkg" +winget install Git.Git +winget install Microsoft.VisualStudio.2022.BuildTools --override "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --passive --wait" ``` -Build Core, drivers, and GUI with the repository script: +`winget` is the package manager included in Windows 10 and 11. The Build Tools are Visual Studio without the IDE, free to use. `--override` passes its options to the Visual Studio installer: `--add ...VCTools` selects the C++ build tools, `--includeRecommended` adds the Windows SDK and CMake, `--passive` shows only a progress bar, and `--wait` returns once the installation has finished. The installer asks for administrator rights, downloads a few gigabytes and takes 10 to 20 minutes. + +Then clone ScopeOne and build and start it with one command, in a new terminal so Git is on the path: ```powershell -.\scripts\build.ps1 --configure +git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/ScopeOne.git +cd ScopeOne +.\build.cmd --run ``` -Later builds only need `.\scripts\build.ps1`. Core, drivers, and GUI share `vcpkg_installed/`, and CMake installs the dependencies on the first configure. +`build.cmd` runs `scripts\build.ps1` without changing the PowerShell execution policy, which blocks `.ps1` files on a new Windows installation. The script finds Visual Studio itself, so any terminal works. If `VCPKG_ROOT` is not set, it offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It clones the latest mmCoreAndDevices and builds MMCore with the DemoCamera and Utilities adapters, then ScopeOneCore, the drivers and the GUI. The first build compiles Qt and OpenCV with vcpkg and takes about an hour. vcpkg keeps the result in `vcpkg_installed\` and in its binary cache, so later builds only compile ScopeOne. + +Later builds need only `.\build.cmd`. `--run` starts ScopeOne after the build, `--configure` configures again, `--clean` discards the build directories, and `--target core`, `gui` or `drivers` builds one part. Open `config\MMConfig_demo.cfg` in ScopeOne to try it without hardware. The last lines of the build show whether Core was built with the CUDA and ONNX modules, which it does when the CUDA Toolkit is installed and ONNX Runtime is unpacked in `ScopeOneCore\external`. -For a direct Core CMake configure: +For a direct Core CMake configure, after the script has built MMCore once (`.\build.cmd --target core`): ```powershell cmake -S ScopeOneCore -B ScopeOneCore/build ` @@ -111,11 +117,7 @@ cmake -S ScopeOneCore -B ScopeOneCore/build ` -DVCPKG_TARGET_TRIPLET=x64-windows ``` -Run the built application: - -```powershell -.\build\Release\ScopeOne.exe -``` +The built application is `build\Release\ScopeOne.exe`. **Linux and macOS Build Steps:** diff --git a/build.cmd b/build.cmd new file mode 100644 index 0000000..cd296ab --- /dev/null +++ b/build.cmd @@ -0,0 +1,5 @@ +@echo off +rem Runs scripts\build.ps1 without changing the PowerShell execution policy of the machine, which +rem blocks .ps1 files on a new Windows installation. All arguments are passed on to the script. +powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0scripts\build.ps1" %* +exit /b %ERRORLEVEL% diff --git a/scripts/build.ps1 b/scripts/build.ps1 index ccd1629..64c131a 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -10,6 +10,7 @@ $clean = $false $run = $false $package = $false $skipCoreInstall = $false +$assumeYes = $false function Read-OptionValue { param( @@ -70,6 +71,10 @@ for ($i = 0; $i -lt $args.Count; $i++) { $skipCoreInstall = $true continue } + { $_ -in @("--yes", "-yes", "-y") } { + $assumeYes = $true + continue + } default { throw "Unknown option: $arg" } @@ -178,7 +183,7 @@ function Import-MsvcEnvironment { $vsWhere = Join-Path ${env:ProgramFiles(x86)} "Microsoft Visual Studio\Installer\vswhere.exe" if (-not (Test-Path $vsWhere)) { - throw "Visual Studio Installer was not found." + throw "Visual Studio 2022 with the C++ tools was not found.`n$visualStudioInstallHint" } $installationPath = & $vsWhere ` -latest ` @@ -186,7 +191,7 @@ function Import-MsvcEnvironment { -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ` -property installationPath if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($installationPath)) { - throw "A Visual Studio installation with the C++ toolchain was not found." + throw "Visual Studio is installed without the C++ tools.`n$visualStudioInstallHint" } $devShellModule = Join-Path $installationPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll" @@ -217,10 +222,178 @@ function Import-MsvcEnvironment { $env:CXX = "cl.exe" } -$scopeOneVcpkgRoot = $env:VCPKG_ROOT -Import-MsvcEnvironment +# The Build Tools bring MSVC, MSBuild, the Windows SDK and CMake, which is all the build needs +# besides Git +$visualStudioInstallHint = @" +Install the Visual Studio 2022 Build Tools with the C++ workload and run this script again: + + winget install Microsoft.VisualStudio.2022.BuildTools --override "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --passive --wait" + +An existing Visual Studio 2022 works too once "Desktop development with C++" is added in the Visual Studio Installer. +"@ + +# Clones vcpkg into the user's folder when VCPKG_ROOT does not name one, after asking, and +# bootstraps it. vcpkg builds Qt, OpenCV and the other libraries inside the repository. +function Resolve-Vcpkg { + $root = $env:VCPKG_ROOT + if (-not $root -or -not (Test-Path (Join-Path $root "scripts\buildsystems\vcpkg.cmake"))) { + $root = Join-Path $env:USERPROFILE "vcpkg" + if (-not (Test-Path (Join-Path $root "scripts\buildsystems\vcpkg.cmake"))) { + if (-not $assumeYes) { + if ([Console]::IsInputRedirected) { + throw "Set VCPKG_ROOT to a bootstrapped vcpkg checkout, or pass --yes to download vcpkg into $root." + } + Write-Host "vcpkg is a C++ package manager. ScopeOne uses it to download and build Qt, OpenCV and its other libraries, and nothing is installed system wide." + $answer = Read-Host "vcpkg was not found. Download it into $root? Press Enter to accept, type another folder, or n to cancel" + if ($answer -in @("n", "N", "no", "No")) { + throw "Set VCPKG_ROOT to a bootstrapped vcpkg checkout and run this script again." + } + if ($answer) { + $root = $answer + } + } + if (-not (Test-Path (Join-Path $root "scripts\buildsystems\vcpkg.cmake"))) { + Invoke-Step ` + -Label "Downloading vcpkg" ` + -FilePath $git ` + -Arguments @("clone", "https://github.com/microsoft/vcpkg.git", $root) ` + -WorkingDirectory $env:USERPROFILE + } + } + $env:VCPKG_ROOT = $root + [Environment]::SetEnvironmentVariable("VCPKG_ROOT", $root, "User") + Write-Host "Using vcpkg in $root, saved as VCPKG_ROOT for your user account." + } + if (-not (Test-Path (Join-Path $root "vcpkg.exe"))) { + Invoke-Step ` + -Label "Bootstrapping vcpkg" ` + -FilePath (Join-Path $root "bootstrap-vcpkg.bat") ` + -Arguments @("-disableMetrics") ` + -WorkingDirectory $root + } + return $root +} + +# Clones the latest mmCoreAndDevices and builds MMCore and the two adapters the demo +# configurations use. Core links MMCore.lib and copies the adapters next to +# ScopeOne. An existing checkout is used as it is, and nothing is rebuilt once its outputs exist. +function Ensure-MMCore { + $mm = Join-Path $coreSourceDir "external\mmCoreAndDevices" + $binDir = Join-Path $mm "build\Release\x64" + $missing = @(@("MMCore.lib", "mmgr_dal_DemoCamera.dll", "mmgr_dal_Utilities.dll") | + Where-Object { -not (Test-Path (Join-Path $binDir $_)) }) + if ($missing.Count -eq 0) { + return + } + if (-not (Test-Path (Join-Path $mm "MMCore\MMCore.vcxproj"))) { + Invoke-Step -Label "Downloading mmCoreAndDevices" -FilePath $git ` + -Arguments @("clone", "--depth", "1", + "https://github.com/micro-manager/mmCoreAndDevices.git", $mm) ` + -WorkingDirectory $repoRoot + } + $msbuild = (Get-Command msbuild -ErrorAction Stop).Source + foreach ($project in @( + "MMCore\MMCore.vcxproj", + "DeviceAdapters\DemoCamera\DemoCamera.vcxproj", + "DeviceAdapters\Utilities\Utilities.vcxproj")) { + Invoke-Step ` + -Label "Building $project" ` + -FilePath $msbuild ` + -Arguments @( + (Join-Path $mm $project), + "/p:Configuration=Release", + "/p:Platform=x64", + "/p:SolutionDir=$mm\", + "/m", + "/nologo", + "/v:minimal" + ) ` + -WorkingDirectory $mm + } +} + +# Prints one line per tool, a check mark or a cross with what it is for, and stops after the list +# with the install commands of everything missing. The marks are built from code points, as +# Windows PowerShell reads a script without a byte order mark in the legacy code page. +$checkMark = [string][char]0x2713 +$crossMark = [string][char]0x2717 +$missingTools = @() +function Write-ToolLine { + param([bool]$Found, [string]$Label, [string]$Detail) + + Write-Host " " -NoNewline + if ($Found) { + Write-Host $checkMark -ForegroundColor Green -NoNewline + } + else { + Write-Host $crossMark -ForegroundColor Red -NoNewline + } + Write-Host (" {0,-14} {1}" -f $Label, $Detail) +} + +Write-Step "Checking build tools" +$gitCommand = Get-Command git -ErrorAction SilentlyContinue +if ($gitCommand) { + $git = $gitCommand.Source + Write-ToolLine $true "Git" ((& $git --version) -join " ") +} +else { + Write-ToolLine $false "Git" "fetches the ScopeWriter submodule, vcpkg and Micro-Manager" + $missingTools += "Git" +} + +$msvcError = $null +try { + Import-MsvcEnvironment + Write-ToolLine $true "Visual Studio" ("MSVC $env:VCToolsVersion, Windows SDK $env:WindowsSDKVersion".TrimEnd("\")) +} +catch { + $msvcError = $_.Exception.Message + Write-ToolLine $false "Visual Studio" "compiles everything with MSVC" + $missingTools += "Visual Studio" +} + +$cmakeCommand = Get-Command cmake -ErrorAction SilentlyContinue +$msbuildCommand = Get-Command msbuild -ErrorAction SilentlyContinue +if ($cmakeCommand) { + $cmake = $cmakeCommand.Source + Write-ToolLine $true "CMake" ((& $cmake --version) | Select-Object -First 1) +} +else { + Write-ToolLine $false "CMake" "configures ScopeOne, part of the C++ tools of Visual Studio" + $missingTools += "CMake" +} +if ($msbuildCommand) { + Write-ToolLine $true "MSBuild" "builds Micro-Manager's core and adapters" +} +else { + Write-ToolLine $false "MSBuild" "builds Micro-Manager's core and adapters, part of Visual Studio" + $missingTools += "MSBuild" +} -$cmake = (Get-Command cmake -ErrorAction Stop).Source +if ($env:VCPKG_ROOT -and (Test-Path (Join-Path $env:VCPKG_ROOT "scripts\buildsystems\vcpkg.cmake"))) { + Write-ToolLine $true "vcpkg" $env:VCPKG_ROOT +} +else { + Write-Host " - " -ForegroundColor Yellow -NoNewline + Write-Host ("{0,-14} {1}" -f "vcpkg", "not set up yet, offered below") +} + +if ($missingTools.Count -gt 0) { + $hints = @() + if ("Git" -in $missingTools) { + $hints += "Install Git:`n`n winget install Git.Git`n" + } + if ($missingTools | Where-Object { $_ -ne "Git" }) { + if ($msvcError) { + $hints += $msvcError + } + else { + $hints += $visualStudioInstallHint + } + } + throw "Missing build tools: $($missingTools -join ', ')`n`n$($hints -join "`n")`nOpen a new terminal after installing, so the tools are on the path." +} $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $repoRoot = Split-Path -Parent $scriptDir @@ -249,13 +422,7 @@ $driverCachePath = Join-Path $driverBuildDir "CMakeCache.txt" $vcpkgConfigureArgs = @() $writerVcpkgArgs = @() if ($env:OS -eq "Windows_NT") { - if (-not $scopeOneVcpkgRoot) { - throw "Set VCPKG_ROOT to a bootstrapped vcpkg checkout." - } - $vcpkgToolchain = Join-Path $scopeOneVcpkgRoot "scripts\buildsystems\vcpkg.cmake" - if (-not (Test-Path $vcpkgToolchain)) { - throw "Set VCPKG_ROOT to a bootstrapped vcpkg checkout." - } + $vcpkgToolchain = Join-Path (Resolve-Vcpkg) "scripts\buildsystems\vcpkg.cmake" } if ($env:OS -eq "Windows_NT" -and $target -eq "scopewriter") { # Standalone ScopeWriter installs its own vcpkg.json dependencies and ships their DLLs @@ -410,6 +577,14 @@ if (-not $needCoreConfigure -and $target -in @("all", "core")) { } if ($target -in @("all", "core")) { + if ($env:OS -eq "Windows_NT") { + Ensure-MMCore + } + if (-not (Test-Path (Join-Path $repoRoot "vcpkg_installed\x64-windows"))) { + Write-Host "" + Write-Host "The first build compiles Qt and OpenCV with vcpkg, which takes about an hour. Later builds reuse the result." -ForegroundColor Yellow + } + # Core builds the CUDA processing modules, which need the CUDA toolset. A generator keeps the # toolset it was first configured with, so a build directory made without it is configured # again from scratch. From a513d664c5ffa8a25f58a32a80584776e08817fb Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 16:26:22 +0100 Subject: [PATCH 14/49] Run build.ps1 with a bypassed execution policy instead of build.cmd --- .gitignore | 1 - CONTRIBUTING.md | 5 +++-- README.md | 21 ++++++--------------- build.cmd | 5 ----- 4 files changed, 9 insertions(+), 23 deletions(-) delete mode 100644 build.cmd diff --git a/.gitignore b/.gitignore index 7c298aa..74c17b7 100644 --- a/.gitignore +++ b/.gitignore @@ -28,7 +28,6 @@ !/config/MMConfig_demo_dual.cfg !/CMakeLists.txt -!/build.cmd !/README.md !/CONTRIBUTING.md !/LICENSE diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 36f25dc..d54ca81 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,8 +1,9 @@ # Development guidelines These rules apply to everyone who changes ScopeOne. The README explains how to build: on Windows, -`.\build.cmd --run` after installing Git and the Visual Studio Build Tools, and on Linux and -macOS `./scripts/build-unix.sh`. This file explains how the code is meant to be written. Each rule says why. When a rule and its reason +`powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --run` after installing Git and the +Visual Studio Build Tools, and on Linux and macOS `./scripts/build-unix.sh`. This file explains +how the code is meant to be written. Each rule says why. When a rule and its reason disagree in a specific case, follow the reason and say so in the review. ## Boundaries diff --git a/README.md b/README.md index 908817c..e4ab8c9 100644 --- a/README.md +++ b/README.md @@ -69,17 +69,6 @@ git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/Sc git submodule update --init --recursive ``` -The expected layout is: - -```text -ScopeOne/ - ScopeOneCore/ - include/scopeone/ Core API and the contracts of the application - external/ - mmCoreAndDevices/ fetched by the build scripts - ScopeWriter/ submodule -``` - ### Drivers and processing modules ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `processing/python`. The CUDA and ONNX processing modules are part of Core, which builds them when nvcc and ONNX Runtime are found. `-DSCOPEONE_ENABLE_CUDA=OFF` and `-DSCOPEONE_ENABLE_INFERENCE=OFF` build the standard Core without them. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [ScopeOneCore/python/scopeone/README.md](ScopeOneCore/python/scopeone/README.md#python-processing-modules) how to write a Python processing module. @@ -100,14 +89,16 @@ Then clone ScopeOne and build and start it with one command, in a new terminal s ```powershell git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/ScopeOne.git cd ScopeOne -.\build.cmd --run +powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --run ``` -`build.cmd` runs `scripts\build.ps1` without changing the PowerShell execution policy, which blocks `.ps1` files on a new Windows installation. The script finds Visual Studio itself, so any terminal works. If `VCPKG_ROOT` is not set, it offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It clones the latest mmCoreAndDevices and builds MMCore with the DemoCamera and Utilities adapters, then ScopeOneCore, the drivers and the GUI. The first build compiles Qt and OpenCV with vcpkg and takes about an hour. vcpkg keeps the result in `vcpkg_installed\` and in its binary cache, so later builds only compile ScopeOne. +Windows does not run `.ps1` files by default. Its execution policy only guards against starting a script by accident, and `-ExecutionPolicy Bypass` lifts it for this one run without changing any setting of the machine. Use the same command for every build, or run `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned` once, which needs no administrator rights, and then start the script as `.\scripts\build.ps1`. + +The script finds Visual Studio itself, so any terminal works. If `VCPKG_ROOT` is not set, it offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It clones the latest mmCoreAndDevices and builds MMCore with the DemoCamera and Utilities adapters, then ScopeOneCore, the drivers and the GUI. The first build compiles Qt and OpenCV with vcpkg and takes about an hour. vcpkg keeps the result in `vcpkg_installed\` and in its binary cache, so later builds only compile ScopeOne. -Later builds need only `.\build.cmd`. `--run` starts ScopeOne after the build, `--configure` configures again, `--clean` discards the build directories, and `--target core`, `gui` or `drivers` builds one part. Open `config\MMConfig_demo.cfg` in ScopeOne to try it without hardware. The last lines of the build show whether Core was built with the CUDA and ONNX modules, which it does when the CUDA Toolkit is installed and ONNX Runtime is unpacked in `ScopeOneCore\external`. +Later builds run the same command without `--run`, or with it to start ScopeOne afterwards. `--run` starts ScopeOne after the build, `--configure` configures again, `--clean` discards the build directories, and `--target core`, `gui` or `drivers` builds one part. Open `config\MMConfig_demo.cfg` in ScopeOne to try it without hardware. The last lines of the build show whether Core was built with the CUDA and ONNX modules, which it does when the CUDA Toolkit is installed and ONNX Runtime is unpacked in `ScopeOneCore\external`. -For a direct Core CMake configure, after the script has built MMCore once (`.\build.cmd --target core`): +For a direct Core CMake configure, after the script has built MMCore once (`--target core`): ```powershell cmake -S ScopeOneCore -B ScopeOneCore/build ` diff --git a/build.cmd b/build.cmd deleted file mode 100644 index cd296ab..0000000 --- a/build.cmd +++ /dev/null @@ -1,5 +0,0 @@ -@echo off -rem Runs scripts\build.ps1 without changing the PowerShell execution policy of the machine, which -rem blocks .ps1 files on a new Windows installation. All arguments are passed on to the script. -powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0scripts\build.ps1" %* -exit /b %ERRORLEVEL% From b389b3a74234e78d01e47945e1926e1813ac7a21 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 16:34:13 +0100 Subject: [PATCH 15/49] Rename build-unix.sh to build.sh --- .github/workflows/compile-check.yml | 4 ++-- CONTRIBUTING.md | 2 +- README.md | 2 +- drivers/README.md | 2 +- scripts/{build-unix.sh => build.sh} | 0 5 files changed, 5 insertions(+), 5 deletions(-) rename scripts/{build-unix.sh => build.sh} (100%) diff --git a/.github/workflows/compile-check.yml b/.github/workflows/compile-check.yml index 2c7ce31..1735367 100644 --- a/.github/workflows/compile-check.yml +++ b/.github/workflows/compile-check.yml @@ -63,7 +63,7 @@ jobs: - name: Compile mmCoreAndDevices and ScopeOne shell: bash - run: ./scripts/build-unix.sh + run: ./scripts/build.sh compile-macos: name: Compile (macOS) @@ -100,7 +100,7 @@ jobs: - name: Compile mmCoreAndDevices and ScopeOne shell: bash - run: ./scripts/build-unix.sh + run: ./scripts/build.sh compile-windows: name: Compile (Windows) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d54ca81..ef76276 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,7 +2,7 @@ These rules apply to everyone who changes ScopeOne. The README explains how to build: on Windows, `powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --run` after installing Git and the -Visual Studio Build Tools, and on Linux and macOS `./scripts/build-unix.sh`. This file explains +Visual Studio Build Tools, and on Linux and macOS `./scripts/build.sh`. This file explains how the code is meant to be written. Each rule says why. When a rule and its reason disagree in a specific case, follow the reason and say so in the review. diff --git a/README.md b/README.md index e4ab8c9..415237a 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ brew install \ 2. Build from the ScopeOne repository root: ```bash -./scripts/build-unix.sh +./scripts/build.sh ``` If vcpkg is missing, the script offers to download it into `$HOME/vcpkg` or a folder you type, and `--yes` takes the default folder without asking. It then builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. Set `JOBS` to limit parallel jobs and `BUILD_TYPE` to change the CMake build type (default `Release`). Pass `--clean` to discard the previous build directories. diff --git a/drivers/README.md b/drivers/README.md index b6ce8ea..44e7991 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -30,7 +30,7 @@ The `scopeone::DriverSDK` target gives a driver the SDK headers (`scopeone/Drive them all) and the small SDK library, and nothing else of Core, so a driver that includes or calls any other part of Core fails to build. -Build and install ScopeOneCore first (`scripts/build-unix.sh` or `scripts\build.ps1`). A driver +Build and install ScopeOneCore first (`scripts/build.sh` or `scripts\build.ps1`). A driver is then a separate CMake project: ```cmake diff --git a/scripts/build-unix.sh b/scripts/build.sh similarity index 100% rename from scripts/build-unix.sh rename to scripts/build.sh From 9668772e46d93659d5c3d42f03759f03620f3a35 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 16:55:08 +0100 Subject: [PATCH 16/49] Keep the CUDA, ONNX and scan image code inside Core's internal namespace --- CONTRIBUTING.md | 2 +- ScopeOneCore/CMakeLists.txt | 2 +- ScopeOneCore/README.md | 4 ++-- .../scopeone => internal}/ScanImageAssembler.h | 5 ++--- ScopeOneCore/internal/cuda/CudaDevice.h | 2 +- ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h | 10 +++++----- ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h | 8 ++++---- ScopeOneCore/internal/cuda/CudaKernelLaunch.h | 2 +- .../internal/cuda/CudaProcessingRegistration.h | 2 +- ScopeOneCore/internal/cuda/CudaRealImageModule.h | 2 +- ScopeOneCore/internal/cuda/GpuComplexFrame.h | 2 +- ScopeOneCore/internal/cuda/GpuRealFrame.h | 2 +- ScopeOneCore/internal/inference/ExecutionProvider.h | 2 +- .../inference/InferenceProcessingRegistration.h | 2 +- ScopeOneCore/internal/inference/OnnxInferenceModule.h | 2 +- ScopeOneCore/internal/inference/OnnxInternal.h | 2 +- ScopeOneCore/internal/inference/OnnxRuntime.h | 2 +- ScopeOneCore/internal/inference/OnnxSession.h | 2 +- ScopeOneCore/internal/inference/TensorInfo.h | 2 +- ScopeOneCore/src/ScanImageAssembler.cpp | 4 ++-- ScopeOneCore/src/ScopeOneCore.cpp | 8 ++++---- ScopeOneCore/src/cuda/CudaDevice.cpp | 2 +- ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp | 10 +++++----- ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp | 10 +++++----- ScopeOneCore/src/cuda/CudaKernels.cu | 2 +- ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp | 2 +- ScopeOneCore/src/cuda/CudaRealImageModule.cpp | 2 +- ScopeOneCore/src/cuda/GpuComplexFrame.cpp | 2 +- ScopeOneCore/src/cuda/GpuRealFrame.cpp | 2 +- .../src/inference/InferenceProcessingRegistration.cpp | 2 +- ScopeOneCore/src/inference/OnnxInferenceModule.cpp | 8 ++++---- ScopeOneCore/src/inference/OnnxRuntime.cpp | 2 +- ScopeOneCore/src/inference/OnnxSession.cpp | 10 +++++----- drivers/README.md | 4 +++- 34 files changed, 64 insertions(+), 63 deletions(-) rename ScopeOneCore/{include/scopeone => internal}/ScanImageAssembler.h (91%) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ef76276..2224d03 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,7 +12,7 @@ disagree in a specific case, follow the reason and say so in the review. | Part | Folder | Namespace | May use | |---|---|---|---| -| Core | `ScopeOneCore` | `scopeone::core`, `core::internal`, `core::imageops`, `scopeone::cuda` and `scopeone::inference` for the CUDA and ONNX modules | Qt Core, OpenCV, MMCore, ScopeWriter, and CUDA and ONNX Runtime when they are built. No Qt Widgets. | +| Core | `ScopeOneCore` | `scopeone::core`, `core::internal`, `core::imageops`, `core::internal::cuda` and `core::internal::inference` for the CUDA and ONNX modules | Qt Core, OpenCV, MMCore, ScopeWriter, and CUDA and ONNX Runtime when they are built. No Qt Widgets. | | Application | `src` | `scopeone::ui` | Core, Qt Widgets | | MCP server | `src/ScopeOneMcpServer.cpp` | | Qt Core, Qt Network and the Local API protocol. Not Core. | | Drivers | `drivers` | their own | `scopeone::DriverSDK` | diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index df42463..1aa26ed 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -114,7 +114,7 @@ set(CORE_HEADERS include/scopeone/ExperimentDocument.h include/scopeone/DriverManifest.h include/scopeone/ProcessingTypes.h - include/scopeone/ScanImageAssembler.h + internal/ScanImageAssembler.h include/scopeone/SharedFrame.h include/scopeone/DhmReconstruction.h internal/AcquisitionEngine.h diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 81004db..34b0612 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -51,7 +51,7 @@ Use this placement rule: | `scopeone::core::internal` | Core-only managers, processing modules and built-in devices | `CameraManager`, `MMCoreManager`, `RecordingManager`, `IscatModule`, `SignalSourceManager` | | `scopeone::core::internal::driverhost` | Shared DriverHost message framing | Versioned request, response and event envelopes | | `scopeone::ui` | Desktop application widgets and UI coordination outside this library | `MainWindow`, `PreviewWidget`, `DhmTool`, `ToolRegistry` | -| `scopeone::cuda`, `scopeone::inference` | The CUDA and ONNX processing modules in `src/cuda` and `src/inference` | `scopeone::cuda` | +| `scopeone::core::internal::cuda`, `scopeone::core::internal::inference` | The CUDA and ONNX processing modules in `src/cuda` and `src/inference` | `core::internal::cuda` | Code in `src` that implements a public type remains in `scopeone::core`. Code that implements an `internal` header remains in `scopeone::core::internal`. The Python package named `scopeone` is an external client package and is not an embedded form of the C++ namespace. @@ -110,7 +110,7 @@ The installed headers in `include/scopeone` and `include/sdk/scopeone` are the s - `ProcessingPipeline.h`, `ExperimentDocument.h` and `ImageSceneModel.h` are Core-owned runtime models and services. - `DriverSDK.h` includes the headers of the public driver contract: `CameraProvider.h`, `DaqDevice.h`, `DaqProvider.h`, `Driver.h`, `HardwareCapabilities.h`, `HardwareProvider.h`, `HardwareTypes.h`, `ImageFrame.h`, `SignalSource.h` and `SignalSourceProvider.h`. The `scopeone::DriverSDK` CMake target provides them. - `ProcessingTypes.h` holds the processing module base class and the descriptors of modules and their parameters. -- `DriverManifest.h` is how ScopeOne discovers and enables drivers. `ScanImageAssembler.h`, `SharedFrame.h` and `DhmReconstruction.h` serve the application. None of them are driver contracts. +- `DriverManifest.h` is how ScopeOne discovers and enables drivers. `SharedFrame.h` and `DhmReconstruction.h` serve the application. None of them are driver contracts. - `scopeone_core_export.h` supplies DLL import and export declarations and is normally included indirectly. External code should enter through these headers and `scopeone::core::ScopeOneCore`. Internal managers are implementation details and must not become alternate access paths. diff --git a/ScopeOneCore/include/scopeone/ScanImageAssembler.h b/ScopeOneCore/internal/ScanImageAssembler.h similarity index 91% rename from ScopeOneCore/include/scopeone/ScanImageAssembler.h rename to ScopeOneCore/internal/ScanImageAssembler.h index 3f6541d..f0b2eec 100644 --- a/ScopeOneCore/include/scopeone/ScanImageAssembler.h +++ b/ScopeOneCore/internal/ScanImageAssembler.h @@ -2,14 +2,13 @@ #include "scopeone/ImageFrame.h" #include "scopeone/SignalSource.h" -#include "scopeone/scopeone_core_export.h" #include #include -namespace scopeone::core +namespace scopeone::core::internal { - class SCOPEONE_CORE_EXPORT ScanImageAssembler final + class ScanImageAssembler final { public: explicit ScanImageAssembler(const QString& sourceId, diff --git a/ScopeOneCore/internal/cuda/CudaDevice.h b/ScopeOneCore/internal/cuda/CudaDevice.h index beb4675..125dc65 100644 --- a/ScopeOneCore/internal/cuda/CudaDevice.h +++ b/ScopeOneCore/internal/cuda/CudaDevice.h @@ -1,7 +1,7 @@ #pragma once -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { bool isCudaDeviceAvailable(); int cudaDeviceCount(); diff --git a/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h b/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h index 6a3045a..80a23dd 100644 --- a/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h +++ b/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h @@ -5,9 +5,9 @@ #include -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { - class CudaFrequencyFilterModule final : public scopeone::cuda::CudaRealImageModule + class CudaFrequencyFilterModule final : public scopeone::core::internal::cuda::CudaRealImageModule { public: CudaFrequencyFilterModule(); @@ -21,15 +21,15 @@ namespace scopeone::cuda bool resetState() override; protected: - bool processDevice(const scopeone::cuda::GpuRealFrame& input, - scopeone::cuda::GpuRealFrame& output, + bool processDevice(const scopeone::core::internal::cuda::GpuRealFrame& input, + scopeone::core::internal::cuda::GpuRealFrame& output, int bitDepth) override; private: void destroyPlans(); void releaseScratch(); - scopeone::cuda::GpuComplexFrame m_spectrum; + scopeone::core::internal::cuda::GpuComplexFrame m_spectrum; cufftHandle m_forwardPlan{0}; cufftHandle m_inversePlan{0}; int m_planWidth{0}; diff --git a/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h b/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h index de4ae6d..46d0b6a 100644 --- a/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h +++ b/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h @@ -2,9 +2,9 @@ #include "internal/cuda/CudaRealImageModule.h" -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { - class CudaGaussianBlurModule final : public scopeone::cuda::CudaRealImageModule + class CudaGaussianBlurModule final : public scopeone::core::internal::cuda::CudaRealImageModule { public: CudaGaussianBlurModule(); @@ -16,8 +16,8 @@ namespace scopeone::cuda std::unique_ptr createRuntime() const override; protected: - bool processDevice(const scopeone::cuda::GpuRealFrame& input, - scopeone::cuda::GpuRealFrame& output, + bool processDevice(const scopeone::core::internal::cuda::GpuRealFrame& input, + scopeone::core::internal::cuda::GpuRealFrame& output, int bitDepth) override; private: diff --git a/ScopeOneCore/internal/cuda/CudaKernelLaunch.h b/ScopeOneCore/internal/cuda/CudaKernelLaunch.h index e602957..87c42f3 100644 --- a/ScopeOneCore/internal/cuda/CudaKernelLaunch.h +++ b/ScopeOneCore/internal/cuda/CudaKernelLaunch.h @@ -3,7 +3,7 @@ #include -namespace scopeone::cuda::detail +namespace scopeone::core::internal::cuda::detail { bool convertToFloat(const void* source, int sourceStride, diff --git a/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h b/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h index fe8f73c..0bae74e 100644 --- a/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h +++ b/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h @@ -5,7 +5,7 @@ namespace scopeone::core::internal class ProcessingModuleRegistry; } -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { // Registers the CUDA processing modules, built into Core when SCOPEONE_ENABLE_CUDA is on void registerCudaProcessingModules(core::internal::ProcessingModuleRegistry& registry); diff --git a/ScopeOneCore/internal/cuda/CudaRealImageModule.h b/ScopeOneCore/internal/cuda/CudaRealImageModule.h index 3f49d36..de66765 100644 --- a/ScopeOneCore/internal/cuda/CudaRealImageModule.h +++ b/ScopeOneCore/internal/cuda/CudaRealImageModule.h @@ -3,7 +3,7 @@ #include "scopeone/ProcessingTypes.h" #include "internal/cuda/GpuRealFrame.h" -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { class CudaRealImageModule : public scopeone::core::ProcessingModule diff --git a/ScopeOneCore/internal/cuda/GpuComplexFrame.h b/ScopeOneCore/internal/cuda/GpuComplexFrame.h index 9a9af2d..5d6d94f 100644 --- a/ScopeOneCore/internal/cuda/GpuComplexFrame.h +++ b/ScopeOneCore/internal/cuda/GpuComplexFrame.h @@ -3,7 +3,7 @@ #include -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { class GpuComplexFrame { diff --git a/ScopeOneCore/internal/cuda/GpuRealFrame.h b/ScopeOneCore/internal/cuda/GpuRealFrame.h index a9d499b..3923100 100644 --- a/ScopeOneCore/internal/cuda/GpuRealFrame.h +++ b/ScopeOneCore/internal/cuda/GpuRealFrame.h @@ -4,7 +4,7 @@ #include -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { enum class GpuMemoryLayout { diff --git a/ScopeOneCore/internal/inference/ExecutionProvider.h b/ScopeOneCore/internal/inference/ExecutionProvider.h index 4e1806c..5d62089 100644 --- a/ScopeOneCore/internal/inference/ExecutionProvider.h +++ b/ScopeOneCore/internal/inference/ExecutionProvider.h @@ -1,6 +1,6 @@ #pragma once -namespace scopeone::inference +namespace scopeone::core::internal::inference { enum class ExecutionProvider { diff --git a/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h b/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h index ca86ead..c9ac74a 100644 --- a/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h +++ b/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h @@ -5,7 +5,7 @@ namespace scopeone::core::internal class ProcessingModuleRegistry; } -namespace scopeone::inference +namespace scopeone::core::internal::inference { // Registers the ONNX Runtime processing modules, built into Core when SCOPEONE_ENABLE_INFERENCE is on void registerInferenceProcessingModules(core::internal::ProcessingModuleRegistry& registry); diff --git a/ScopeOneCore/internal/inference/OnnxInferenceModule.h b/ScopeOneCore/internal/inference/OnnxInferenceModule.h index 06de074..9daacc3 100644 --- a/ScopeOneCore/internal/inference/OnnxInferenceModule.h +++ b/ScopeOneCore/internal/inference/OnnxInferenceModule.h @@ -6,7 +6,7 @@ #include -namespace scopeone::inference +namespace scopeone::core::internal::inference { class OnnxInferenceModule final : public core::ProcessingModule { diff --git a/ScopeOneCore/internal/inference/OnnxInternal.h b/ScopeOneCore/internal/inference/OnnxInternal.h index 890e1da..a6d37cf 100644 --- a/ScopeOneCore/internal/inference/OnnxInternal.h +++ b/ScopeOneCore/internal/inference/OnnxInternal.h @@ -5,7 +5,7 @@ #include -namespace scopeone::inference +namespace scopeone::core::internal::inference { struct OnnxRuntime::Impl { diff --git a/ScopeOneCore/internal/inference/OnnxRuntime.h b/ScopeOneCore/internal/inference/OnnxRuntime.h index a88701d..7dc3f26 100644 --- a/ScopeOneCore/internal/inference/OnnxRuntime.h +++ b/ScopeOneCore/internal/inference/OnnxRuntime.h @@ -6,7 +6,7 @@ #include #include -namespace scopeone::inference +namespace scopeone::core::internal::inference { class OnnxSession; diff --git a/ScopeOneCore/internal/inference/OnnxSession.h b/ScopeOneCore/internal/inference/OnnxSession.h index 7c235a8..66b26ac 100644 --- a/ScopeOneCore/internal/inference/OnnxSession.h +++ b/ScopeOneCore/internal/inference/OnnxSession.h @@ -7,7 +7,7 @@ #include #include -namespace scopeone::inference +namespace scopeone::core::internal::inference { class OnnxRuntime; diff --git a/ScopeOneCore/internal/inference/TensorInfo.h b/ScopeOneCore/internal/inference/TensorInfo.h index d275feb..50d3c34 100644 --- a/ScopeOneCore/internal/inference/TensorInfo.h +++ b/ScopeOneCore/internal/inference/TensorInfo.h @@ -4,7 +4,7 @@ #include #include -namespace scopeone::inference +namespace scopeone::core::internal::inference { enum class TensorElementType { diff --git a/ScopeOneCore/src/ScanImageAssembler.cpp b/ScopeOneCore/src/ScanImageAssembler.cpp index acff2fc..a934a97 100644 --- a/ScopeOneCore/src/ScanImageAssembler.cpp +++ b/ScopeOneCore/src/ScanImageAssembler.cpp @@ -1,11 +1,11 @@ -#include "scopeone/ScanImageAssembler.h" +#include "internal/ScanImageAssembler.h" #include #include #include #include -namespace scopeone::core +namespace scopeone::core::internal { namespace { diff --git a/ScopeOneCore/src/ScopeOneCore.cpp b/ScopeOneCore/src/ScopeOneCore.cpp index 7b8568d..2c3813d 100644 --- a/ScopeOneCore/src/ScopeOneCore.cpp +++ b/ScopeOneCore/src/ScopeOneCore.cpp @@ -1,6 +1,6 @@ #include "scopeone/ScopeOneCore.h" #include "scopeone/ImageSceneModel.h" -#include "scopeone/ScanImageAssembler.h" +#include "internal/ScanImageAssembler.h" #include "internal/AcquisitionEngine.h" #include "internal/FrameBufferUtils.h" @@ -1149,7 +1149,7 @@ namespace scopeone::core QString baseName; quint64 lastTimestampNs{0}; }; - QHash> scanImageAssemblers; + QHash> scanImageAssemblers; QHash scanImageSessions; QHash experiments; QHash> sessions; @@ -2373,10 +2373,10 @@ namespace scopeone::core QString* errorMessage) { const QString sourceId = config.sourceId.trimmed(); - std::shared_ptr assembler; + std::shared_ptr assembler; if (config.scanImage.enabled) { - assembler = std::make_shared(sourceId, config.scanImage); + assembler = std::make_shared(sourceId, config.scanImage); if (!assembler->isValid()) { if (errorMessage) diff --git a/ScopeOneCore/src/cuda/CudaDevice.cpp b/ScopeOneCore/src/cuda/CudaDevice.cpp index 382d9ec..34e8e36 100644 --- a/ScopeOneCore/src/cuda/CudaDevice.cpp +++ b/ScopeOneCore/src/cuda/CudaDevice.cpp @@ -2,7 +2,7 @@ #include -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { bool isCudaDeviceAvailable() { diff --git a/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp b/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp index 8ee7783..0006939 100644 --- a/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp +++ b/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp @@ -5,10 +5,10 @@ #include #include -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { CudaFrequencyFilterModule::CudaFrequencyFilterModule() - : CudaRealImageModule(scopeone::cuda::GpuMemoryLayout::Contiguous) + : CudaRealImageModule(scopeone::core::internal::cuda::GpuMemoryLayout::Contiguous) { } @@ -98,8 +98,8 @@ namespace scopeone::cuda } bool CudaFrequencyFilterModule::processDevice( - const scopeone::cuda::GpuRealFrame& input, - scopeone::cuda::GpuRealFrame& output, + const scopeone::core::internal::cuda::GpuRealFrame& input, + scopeone::core::internal::cuda::GpuRealFrame& output, int bitDepth) { if (m_planWidth != input.width() || m_planHeight != input.height()) @@ -129,7 +129,7 @@ namespace scopeone::cuda { return false; } - return scopeone::cuda::detail::launchFrequencyFilter(input.data(), + return scopeone::core::internal::cuda::detail::launchFrequencyFilter(input.data(), output.data(), m_spectrum.data(), input.width(), diff --git a/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp b/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp index 9eb0a94..38e66b0 100644 --- a/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp +++ b/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp @@ -2,10 +2,10 @@ #include "internal/cuda/CudaKernelLaunch.h" -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { CudaGaussianBlurModule::CudaGaussianBlurModule() - : CudaRealImageModule(scopeone::cuda::GpuMemoryLayout::Pitched2D) + : CudaRealImageModule(scopeone::core::internal::cuda::GpuMemoryLayout::Pitched2D) { } @@ -50,11 +50,11 @@ namespace scopeone::cuda } bool CudaGaussianBlurModule::processDevice( - const scopeone::cuda::GpuRealFrame& input, - scopeone::cuda::GpuRealFrame& output, + const scopeone::core::internal::cuda::GpuRealFrame& input, + scopeone::core::internal::cuda::GpuRealFrame& output, int) { - return scopeone::cuda::detail::launchGaussian(input.data(), + return scopeone::core::internal::cuda::detail::launchGaussian(input.data(), input.pitchBytes(), output.data(), output.pitchBytes(), diff --git a/ScopeOneCore/src/cuda/CudaKernels.cu b/ScopeOneCore/src/cuda/CudaKernels.cu index 821d254..f783b0b 100644 --- a/ScopeOneCore/src/cuda/CudaKernels.cu +++ b/ScopeOneCore/src/cuda/CudaKernels.cu @@ -267,7 +267,7 @@ namespace } } -namespace scopeone::cuda::detail +namespace scopeone::core::internal::cuda::detail { bool convertToFloat(const void* source, int sourceStride, diff --git a/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp b/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp index 816cd11..5978b11 100644 --- a/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp +++ b/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp @@ -4,7 +4,7 @@ #include "internal/ProcessingModuleRegistry.h" -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { namespace { diff --git a/ScopeOneCore/src/cuda/CudaRealImageModule.cpp b/ScopeOneCore/src/cuda/CudaRealImageModule.cpp index 906a361..028bdb0 100644 --- a/ScopeOneCore/src/cuda/CudaRealImageModule.cpp +++ b/ScopeOneCore/src/cuda/CudaRealImageModule.cpp @@ -4,7 +4,7 @@ #include -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { CudaRealImageModule::CudaRealImageModule(GpuMemoryLayout layout) : m_layout(layout) diff --git a/ScopeOneCore/src/cuda/GpuComplexFrame.cpp b/ScopeOneCore/src/cuda/GpuComplexFrame.cpp index f8fceeb..60a823b 100644 --- a/ScopeOneCore/src/cuda/GpuComplexFrame.cpp +++ b/ScopeOneCore/src/cuda/GpuComplexFrame.cpp @@ -4,7 +4,7 @@ #include -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { GpuComplexFrame::~GpuComplexFrame() { diff --git a/ScopeOneCore/src/cuda/GpuRealFrame.cpp b/ScopeOneCore/src/cuda/GpuRealFrame.cpp index c3f3b2f..dbe8518 100644 --- a/ScopeOneCore/src/cuda/GpuRealFrame.cpp +++ b/ScopeOneCore/src/cuda/GpuRealFrame.cpp @@ -6,7 +6,7 @@ #include -namespace scopeone::cuda +namespace scopeone::core::internal::cuda { GpuRealFrame::GpuRealFrame(int width, int height, GpuMemoryLayout layout) { diff --git a/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp b/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp index 00ec172..1db2e05 100644 --- a/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp +++ b/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp @@ -3,7 +3,7 @@ #include "internal/ProcessingModuleRegistry.h" -namespace scopeone::inference +namespace scopeone::core::internal::inference { namespace { diff --git a/ScopeOneCore/src/inference/OnnxInferenceModule.cpp b/ScopeOneCore/src/inference/OnnxInferenceModule.cpp index 343cf5b..3fd98dd 100644 --- a/ScopeOneCore/src/inference/OnnxInferenceModule.cpp +++ b/ScopeOneCore/src/inference/OnnxInferenceModule.cpp @@ -59,7 +59,7 @@ namespace return target; } - TensorLayout resolveLayout(const scopeone::inference::TensorInfo& tensor, + TensorLayout resolveLayout(const scopeone::core::internal::inference::TensorInfo& tensor, int configuredLayout, TensorLayout fallback) { @@ -82,18 +82,18 @@ namespace return fallback; } - int tensorHeight(const scopeone::inference::TensorInfo& tensor, TensorLayout layout) + int tensorHeight(const scopeone::core::internal::inference::TensorInfo& tensor, TensorLayout layout) { return static_cast(tensor.shape[layout == TensorLayout::Nchw ? 2 : 1]); } - int tensorWidth(const scopeone::inference::TensorInfo& tensor, TensorLayout layout) + int tensorWidth(const scopeone::core::internal::inference::TensorInfo& tensor, TensorLayout layout) { return static_cast(tensor.shape[layout == TensorLayout::Nchw ? 3 : 2]); } } -namespace scopeone::inference +namespace scopeone::core::internal::inference { QString OnnxInferenceModule::id() const { diff --git a/ScopeOneCore/src/inference/OnnxRuntime.cpp b/ScopeOneCore/src/inference/OnnxRuntime.cpp index caa80bf..38c1c0b 100644 --- a/ScopeOneCore/src/inference/OnnxRuntime.cpp +++ b/ScopeOneCore/src/inference/OnnxRuntime.cpp @@ -3,7 +3,7 @@ #include #include -namespace scopeone::inference +namespace scopeone::core::internal::inference { OnnxRuntime::Impl::Impl() : environment(ORT_LOGGING_LEVEL_WARNING, "ScopeOneInference") diff --git a/ScopeOneCore/src/inference/OnnxSession.cpp b/ScopeOneCore/src/inference/OnnxSession.cpp index 8c22634..7fcdfa0 100644 --- a/ScopeOneCore/src/inference/OnnxSession.cpp +++ b/ScopeOneCore/src/inference/OnnxSession.cpp @@ -7,10 +7,10 @@ namespace { - scopeone::inference::TensorElementType tensorElementType( + scopeone::core::internal::inference::TensorElementType tensorElementType( ONNXTensorElementDataType type) { - using scopeone::inference::TensorElementType; + using scopeone::core::internal::inference::TensorElementType; switch (type) { case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT: return TensorElementType::Float32; @@ -42,13 +42,13 @@ namespace }); } - std::vector tensorMetadata( + std::vector tensorMetadata( Ort::Session& session, bool input) { const std::size_t count = input ? session.GetInputCount() : session.GetOutputCount(); Ort::AllocatorWithDefaultOptions allocator; - std::vector metadata; + std::vector metadata; metadata.reserve(count); for (std::size_t index = 0; index < count; ++index) { @@ -67,7 +67,7 @@ namespace } } -namespace scopeone::inference +namespace scopeone::core::internal::inference { struct OnnxSession::Impl { diff --git a/drivers/README.md b/drivers/README.md index 44e7991..0f85574 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -28,7 +28,9 @@ so a driver process holds only the host, the SDK library, Qt Core and Network, a The `scopeone::DriverSDK` target gives a driver the SDK headers (`scopeone/DriverSDK.h` includes them all) and the small SDK library, and nothing else of Core, so a driver that includes or calls -any other part of Core fails to build. +any other part of Core fails to build. The SDK types are in `scopeone::core` because Core uses the +same types; which of them a driver sees is decided by these headers and this target, not by the +namespace. Build and install ScopeOneCore first (`scripts/build.sh` or `scripts\build.ps1`). A driver is then a separate CMake project: From 3e2d079326759a42cabc63631e22edc312d10d28 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Thu, 5 Nov 2026 17:08:07 +0100 Subject: [PATCH 17/49] Build Core without CUDA when nvcc cannot build and report why a module was left out --- ScopeOneCore/CMakeLists.txt | 53 ++++++++++++++++++++++++++++++------- scripts/build.ps1 | 19 ++++++------- scripts/build.sh | 10 ++++--- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index 1aa26ed..e682a88 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -222,22 +222,52 @@ target_compile_definitions(ScopeOneCore PRIVATE # Turning them off builds the plain CPU Core, which neither links nor ships CUDA or ONNX Runtime. set(_with_cuda OFF) set(_with_inference OFF) +# Why a module that is turned on was left out, for the summary of the build scripts +set(_cuda_reason "") +set(_inference_reason "") option(SCOPEONE_ENABLE_CUDA "Build the CUDA processing modules into Core when nvcc is available" ON) option(SCOPEONE_ENABLE_INFERENCE "Build the ONNX Runtime processing module into Core when ONNX Runtime is available" ON) if (SCOPEONE_ENABLE_CUDA) + # An nvcc that was uninstalled since the last configure is looked up again + if (SCOPEONE_CUDA_NVCC_EXECUTABLE AND NOT EXISTS "${SCOPEONE_CUDA_NVCC_EXECUTABLE}") + unset(SCOPEONE_CUDA_NVCC_EXECUTABLE CACHE) + unset(CMAKE_CUDA_COMPILER CACHE) + endif () find_program(SCOPEONE_CUDA_NVCC_EXECUTABLE nvcc) + if (NOT SCOPEONE_CUDA_NVCC_EXECUTABLE) + set(_cuda_reason "nvcc was not found on the path") + message(STATUS "${_cuda_reason}; building Core without CUDA") + endif () else () unset(SCOPEONE_CUDA_NVCC_EXECUTABLE CACHE) + set(_cuda_reason "turned off with SCOPEONE_ENABLE_CUDA") endif () if (SCOPEONE_CUDA_NVCC_EXECUTABLE) - set(CMAKE_CUDA_COMPILER "${SCOPEONE_CUDA_NVCC_EXECUTABLE}" CACHE FILEPATH - "CUDA compiler selected by the environment" FORCE) - get_filename_component(_cuda_toolkit_root "${CMAKE_CUDA_COMPILER}" DIRECTORY) - get_filename_component(_cuda_toolkit_root "${_cuda_toolkit_root}" DIRECTORY) - set(CUDAToolkit_ROOT "${_cuda_toolkit_root}" CACHE PATH - "CUDA toolkit selected by the compiler" FORCE) - enable_language(CUDA) - find_package(CUDAToolkit REQUIRED) + # A CUDA that cannot build here, such as one installed before Visual Studio and so without its + # MSBuild integration, or one too old for the MSVC version, left the configure failing. + # check_language tries it in a separate project first, and CUDA is left out when it fails. + if (NOT CMAKE_CUDA_COMPILER) + set(ENV{CUDACXX} "${SCOPEONE_CUDA_NVCC_EXECUTABLE}") + include(CheckLanguage) + check_language(CUDA) + endif () + if (CMAKE_CUDA_COMPILER) + get_filename_component(_cuda_toolkit_root "${CMAKE_CUDA_COMPILER}" DIRECTORY) + get_filename_component(_cuda_toolkit_root "${_cuda_toolkit_root}" DIRECTORY) + set(CUDAToolkit_ROOT "${_cuda_toolkit_root}" CACHE PATH + "CUDA toolkit selected by the compiler" FORCE) + enable_language(CUDA) + find_package(CUDAToolkit) + if (NOT TARGET CUDA::cufft) + set(_cuda_reason "the CUDA toolkit at ${_cuda_toolkit_root} has no cuFFT") + message(STATUS "${_cuda_reason}; building Core without CUDA") + endif () + else () + set(_cuda_reason "nvcc at ${SCOPEONE_CUDA_NVCC_EXECUTABLE} cannot build with this compiler, often a CUDA installed before Visual Studio or too old for it") + message(STATUS "${_cuda_reason}; building Core without CUDA") + endif () +endif () +if (SCOPEONE_CUDA_NVCC_EXECUTABLE AND TARGET CUDA::cufft) set(SCOPEONE_CUDA_ARCHITECTURES "50;75;80;86;89" CACHE STRING "Target CUDA architectures") target_sources(ScopeOneCore PRIVATE src/cuda/CudaKernels.cu @@ -319,11 +349,16 @@ if (SCOPEONE_ENABLE_INFERENCE AND ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRAR list(APPEND SCOPEONE_RUNTIME_DLLS ${_onnxruntime_dlls}) endif () elseif (SCOPEONE_ENABLE_INFERENCE) - message(STATUS "ONNX Runtime was not found under ${ONNXRUNTIME_ROOT}; building Core without ONNX inference") + set(_inference_reason "ONNX Runtime was not found under ${ONNXRUNTIME_ROOT}") + message(STATUS "${_inference_reason}; building Core without ONNX inference") +else () + set(_inference_reason "turned off with SCOPEONE_ENABLE_INFERENCE") endif () # What this configure built in, for the build scripts to report set(SCOPEONE_BUILT_WITH_CUDA "${_with_cuda}" CACHE INTERNAL "") set(SCOPEONE_BUILT_WITH_INFERENCE "${_with_inference}" CACHE INTERNAL "") +set(SCOPEONE_CUDA_SKIP_REASON "${_cuda_reason}" CACHE INTERNAL "") +set(SCOPEONE_INFERENCE_SKIP_REASON "${_inference_reason}" CACHE INTERNAL "") target_include_directories(ScopeOneCore PUBLIC $ diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 64c131a..29c7434 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -762,19 +762,20 @@ else { # Core builds the CUDA and ONNX modules only when it finds their toolkits, so a missing one # leaves them out without failing the build if (Test-Path $coreCachePath) { - Write-Host -NoNewline "Core modules: CUDA: " - foreach ($module in @( - @{ Key = "SCOPEONE_BUILT_WITH_CUDA"; Next = ", ONNX: " }, - @{ Key = "SCOPEONE_BUILT_WITH_INFERENCE"; Next = "" })) { - if ((Get-CMakeCacheValue -CachePath $coreCachePath -Key $module.Key) -eq "ON") { - Write-Host -NoNewline "on" -ForegroundColor Green + foreach ($module in @(@{ Label = "CUDA"; Key = "CUDA" }, @{ Label = "ONNX"; Key = "INFERENCE" })) { + Write-Host -NoNewline "$($module.Label): " + $reason = Get-CMakeCacheValue -CachePath $coreCachePath -Key "SCOPEONE_$($module.Key)_SKIP_REASON" + if ((Get-CMakeCacheValue -CachePath $coreCachePath -Key "SCOPEONE_BUILT_WITH_$($module.Key)") -eq "ON") { + Write-Host "on" -ForegroundColor Green } - else { + elseif ($reason) { Write-Host -NoNewline "off" -ForegroundColor Yellow + Write-Host " ($reason)" + } + else { + Write-Host "off" -ForegroundColor Yellow } - Write-Host -NoNewline $module.Next } - Write-Host "" } if ($target -in @("all", "drivers")) { Write-Host "Drivers: $driverBuildDir" diff --git a/scripts/build.sh b/scripts/build.sh index 1e2d288..9fc42c6 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -246,10 +246,13 @@ run cp -L "$DEMO_ADAPTER" "$UTILITIES_ADAPTER" "$ROOT_DIR/build/" # Core builds the CUDA and ONNX modules only when it finds their toolkits, so a missing one # leaves them out without failing the build core_module() { - local value - value="$(sed -n "s/^$1:INTERNAL=//p" "$CORE_DIR/build/CMakeCache.txt")" + local value reason + value="$(sed -n "s/^SCOPEONE_BUILT_WITH_$1:INTERNAL=//p" "$CORE_DIR/build/CMakeCache.txt")" + reason="$(sed -n "s/^SCOPEONE_$1_SKIP_REASON:INTERNAL=//p" "$CORE_DIR/build/CMakeCache.txt")" if [[ "$value" == ON ]]; then printf '%son%s' "$GREEN" "$RESET" + elif [[ -n "$reason" ]]; then + printf '%soff%s (%s)' "$YELLOW" "$RESET" "$reason" else printf '%soff%s' "$YELLOW" "$RESET" fi @@ -258,4 +261,5 @@ core_module() { step "Done" echo "GUI executable: $ROOT_DIR/build/ScopeOne" echo "Demo configuration: $ROOT_DIR/config/MMConfig_demo.cfg" -echo "Core modules: CUDA: $(core_module SCOPEONE_BUILT_WITH_CUDA), ONNX: $(core_module SCOPEONE_BUILT_WITH_INFERENCE)" +echo "CUDA: $(core_module CUDA)" +echo "ONNX: $(core_module INFERENCE)" From bf85f65245c7ed01c668889e71d12fe82c160d8f Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 08:52:04 +0100 Subject: [PATCH 18/49] Give DriverHost and the Core libraries an rpath on macOS and copy their dylibs beside it --- CMakeLists.txt | 5 +++++ ScopeOneCore/CMakeLists.txt | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8135d10..cc783a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -238,6 +238,11 @@ else () "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore.so*" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK.so*" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter.so*" + # The copied DriverHost resolves its libraries beside itself, and the .so patterns + # alone left the macOS dylibs behind in the Core install + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore*.dylib" + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK*.dylib" + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter*.dylib" ) endif () diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index e682a88..c0df6e5 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -191,10 +191,17 @@ target_include_directories(ScopeOneDriverSDK PUBLIC add_library(ScopeOneCore SHARED ${CORE_SOURCES} ${CORE_HEADERS}) add_library(scopeone::ScopeOneCore ALIAS ScopeOneCore) -if (UNIX AND NOT APPLE) +# macOS has no $ORIGIN. Without an @loader_path entry the installed libraries and DriverHost had +# no LC_RPATH at all, so dyld could not resolve @rpath and the host died before loading a camera. +if (APPLE) + set(SCOPEONE_RPATH_ORIGIN "@loader_path") +elseif (UNIX) + set(SCOPEONE_RPATH_ORIGIN "$ORIGIN") +endif () +if (UNIX) set_target_properties(ScopeWriter ScopeOneCore ScopeOneDriverSDK PROPERTIES - BUILD_RPATH "$ORIGIN" - INSTALL_RPATH "$ORIGIN" + BUILD_RPATH "${SCOPEONE_RPATH_ORIGIN}" + INSTALL_RPATH "${SCOPEONE_RPATH_ORIGIN}" INSTALL_RPATH_USE_LINK_PATH TRUE ) endif () @@ -419,10 +426,10 @@ add_executable(ScopeOne_DriverHost src/DriverManifest.cpp src/StreamRing.cpp ) -if (UNIX AND NOT APPLE) +if (UNIX) set_target_properties(ScopeOne_DriverHost PROPERTIES - BUILD_RPATH "$ORIGIN" - INSTALL_RPATH "$ORIGIN/../lib;$ORIGIN" + BUILD_RPATH "${SCOPEONE_RPATH_ORIGIN}" + INSTALL_RPATH "${SCOPEONE_RPATH_ORIGIN}/../lib;${SCOPEONE_RPATH_ORIGIN}" INSTALL_RPATH_USE_LINK_PATH TRUE ) endif () From b550d9a0de020927d91028fb788937e84a6eca79 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:00:23 +0100 Subject: [PATCH 19/49] Offer the CUDA provider for ONNX inference only when ONNX Runtime has it --- .../inference/InferenceProcessingRegistration.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp b/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp index 1db2e05..40ea58e 100644 --- a/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp +++ b/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp @@ -3,6 +3,10 @@ #include "internal/ProcessingModuleRegistry.h" +#include + +#include + namespace scopeone::core::internal::inference { namespace @@ -16,6 +20,15 @@ namespace scopeone::core::internal::inference QString{}}; modelPath.fileFilter = QStringLiteral("ONNX models (*.onnx)"); + // The CPU package of ONNX Runtime has no CUDA provider, and offering CUDA there only + // failed when the model was loaded + QList providers{{QStringLiteral("CPU"), 0}}; + const std::vector available = Ort::GetAvailableProviders(); + if (std::find(available.begin(), available.end(), "CUDAExecutionProvider") != available.end()) + { + providers.append({QStringLiteral("CUDA"), 1}); + } + return {{QStringLiteral("onnx_inference"), QStringLiteral("ONNX Inference"), 2, @@ -28,7 +41,7 @@ namespace scopeone::core::internal::inference {}, {}, 0, - {{QStringLiteral("CPU"), 0}, {QStringLiteral("CUDA"), 1}}}, + providers}, {QStringLiteral("layout"), QStringLiteral("Layout"), scopeone::core::ProcessingParameterType::Choice, From 3c1c0940fa9a551e50a501d500e6e7ead9c561e9 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:00:23 +0100 Subject: [PATCH 20/49] Remove the drivers build and the Core install with --clean --- scripts/build.ps1 | 6 ++++++ scripts/build.sh | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 29c7434..70abe64 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -468,6 +468,12 @@ if ($clean) { Write-Step "Removing ScopeOneCore build directory" Remove-Item -LiteralPath $coreBuildDir -Recurse -Force } + # The GUI build copies the drivers and libraries of the Core install, and a removed driver or + # the CUDA provider of an earlier ONNX Runtime stayed there and kept being copied + if ($target -in @("all", "core") -and (Test-Path $coreInstallDir)) { + Write-Step "Removing ScopeOneCore install directory" + Remove-Item -LiteralPath $coreInstallDir -Recurse -Force + } } Ensure-Directory $repoRoot diff --git a/scripts/build.sh b/scripts/build.sh index 9fc42c6..a2f43ed 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -176,8 +176,11 @@ JOBS="${JOBS:-$DEFAULT_JOBS}" DEMO_ADAPTER="$MMCORE_LINK/DeviceAdapters/DemoCamera/.libs/$DEMO_ADAPTER_NAME" UTILITIES_ADAPTER="$MMCORE_LINK/DeviceAdapters/Utilities/.libs/$UTILITIES_ADAPTER_NAME" +# The drivers build moved out of build/ into drivers/build, and --clean had kept it. The Core +# install goes too, since the GUI build copies its drivers and libraries, and a removed driver or +# the CUDA provider of an earlier ONNX Runtime stayed there and kept being copied. if [[ "$CLEAN" == true ]]; then - rm -rf "$CORE_DIR/build" "$ROOT_DIR/build" + rm -rf "$CORE_DIR/build" "$CORE_DIR/install" "$ROOT_DIR/drivers/build" "$ROOT_DIR/build" fi step "Initializing ScopeOne submodules" From 0ca78def899e1673c8306e30d630dc631857a9d7 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:00:23 +0100 Subject: [PATCH 21/49] Download the ONNX Runtime package that matches the platform and CUDA and build ONNX inference into every Core --- .github/workflows/compile-check.yml | 3 +- .github/workflows/windows-release.yml | 27 +++----- CMakeLists.txt | 28 ++++++-- README.md | 24 +++++-- ScopeOneCore/CMakeLists.txt | 97 +++++++++++++++++++++++++-- ScopeOneCore/README.md | 2 +- scripts/CopyRuntimeFiles.cmake | 4 ++ 7 files changed, 146 insertions(+), 39 deletions(-) create mode 100644 scripts/CopyRuntimeFiles.cmake diff --git a/.github/workflows/compile-check.yml b/.github/workflows/compile-check.yml index 1735367..a672ae6 100644 --- a/.github/workflows/compile-check.yml +++ b/.github/workflows/compile-check.yml @@ -148,5 +148,4 @@ jobs: shell: pwsh run: | .\scripts\build.ps1 --configure ` - --coreConfigureOption -DSCOPEONE_ENABLE_CUDA=OFF ` - --coreConfigureOption -DSCOPEONE_ENABLE_INFERENCE=OFF + --coreConfigureOption -DSCOPEONE_ENABLE_CUDA=OFF diff --git a/.github/workflows/windows-release.yml b/.github/workflows/windows-release.yml index 52853c3..e3809e0 100644 --- a/.github/workflows/windows-release.yml +++ b/.github/workflows/windows-release.yml @@ -18,8 +18,6 @@ concurrency: env: BOOST_VERSION: 1.86.0 CUDA_VERSION: 12.8.1 - ONNXRUNTIME_PACKAGE: onnxruntime-win-x64-gpu_cuda12-1.30.0 - ONNXRUNTIME_VERSION: 1.30.0 jobs: package: @@ -171,14 +169,6 @@ jobs: "CUDA_PATH_V$($short -replace '\.', '_')=$cudaPath" >> $env:GITHUB_ENV "$cudaPath\bin" >> $env:GITHUB_PATH - - name: Download ONNX Runtime - if: matrix.gpu - shell: pwsh - run: | - $archive = "$env:RUNNER_TEMP\$env:ONNXRUNTIME_PACKAGE.zip" - Invoke-WebRequest "https://github.com/microsoft/onnxruntime/releases/download/v$env:ONNXRUNTIME_VERSION/$env:ONNXRUNTIME_PACKAGE.zip" -OutFile $archive - Expand-Archive $archive -DestinationPath "$env:GITHUB_WORKSPACE\ScopeOneCore\external" - - name: Set up NSIS shell: pwsh run: | @@ -195,10 +185,7 @@ jobs: $options += @("--guiConfigureOption", "-DSCOPEONE_PACKAGE_VARIANT=gpu") } else { - $options += @( - "--coreConfigureOption", "-DSCOPEONE_ENABLE_CUDA=OFF", - "--coreConfigureOption", "-DSCOPEONE_ENABLE_INFERENCE=OFF" - ) + $options += @("--coreConfigureOption", "-DSCOPEONE_ENABLE_CUDA=OFF") } .\scripts\build.ps1 --configure --package @options @@ -211,9 +198,11 @@ jobs: Get-Item $file | Select-Object Name, Length } $listing = 7z l "build\$name.zip" - $hasGpuFiles = [bool]($listing | Select-String -Pattern "cufft64|onnxruntime") - if ("${{ matrix.gpu }}" -eq "true" -and -not $hasGpuFiles) { throw "The GPU package has no CUDA or ONNX Runtime files." } - if ("${{ matrix.gpu }}" -ne "true" -and $hasGpuFiles) { throw "The standard package must not contain CUDA or ONNX Runtime files." } + # Both packages carry ONNX Runtime, and only the GPU one its CUDA provider and cuFFT + if (-not ($listing | Select-String -Pattern "onnxruntime\.dll")) { throw "The package has no ONNX Runtime." } + $hasGpuFiles = [bool]($listing | Select-String -Pattern "cufft64|onnxruntime_providers_cuda") + if ("${{ matrix.gpu }}" -eq "true" -and -not $hasGpuFiles) { throw "The GPU package has no CUDA files." } + if ("${{ matrix.gpu }}" -ne "true" -and $hasGpuFiles) { throw "The standard package must not contain CUDA files." } - name: Upload packages uses: actions/upload-artifact@v7 @@ -254,8 +243,8 @@ jobs: run: | ls -l packages cat > notes.md <<'NOTES' - - **Standard** (`win-x64`): live imaging, recording and image processing on CPU. - - **GPU** (`win-x64-gpu`): adds CUDA processing and ONNX inference. Needs an NVIDIA GPU. + - **Standard** (`win-x64`): live imaging, recording, image processing and inference on CPU. + - **GPU** (`win-x64-gpu`): adds CUDA processing and ONNX inference on CUDA. Needs an NVIDIA GPU. NOTES gh release create "v${{ steps.version.outputs.version }}" packages/* \ --target "$GITHUB_SHA" \ diff --git a/CMakeLists.txt b/CMakeLists.txt index cc783a0..bf313bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -243,6 +243,8 @@ else () "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore*.dylib" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK*.dylib" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter*.dylib" + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libonnxruntime*.so*" + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libonnxruntime*.dylib" ) endif () @@ -250,12 +252,25 @@ if (SCOPEONE_CORE_RUNTIME_FILES) # A target of its own that runs on every build. As a post-build step of ScopeOne it ran only # when the application itself was relinked, so a rebuilt core library was left behind next # to an older copy that the application kept loading. - add_custom_target(ScopeOneCoreRuntime ALL - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${SCOPEONE_CORE_RUNTIME_FILES} - "$" - COMMAND_EXPAND_LISTS - ) + if (WIN32) + add_custom_target(ScopeOneCoreRuntime ALL + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${SCOPEONE_CORE_RUNTIME_FILES} + "$" + COMMAND_EXPAND_LISTS + ) + else () + # A library comes as a file and its links, libonnxruntime.so, .so.1 and .so.1.30.0, and + # copying them as files put three full copies of ONNX Runtime beside ScopeOne + string(REPLACE ";" "|" _core_runtime_files "${SCOPEONE_CORE_RUNTIME_FILES}") + add_custom_target(ScopeOneCoreRuntime ALL + COMMAND ${CMAKE_COMMAND} + "-DFILES=${_core_runtime_files}" + "-DDEST=$" + -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/CopyRuntimeFiles.cmake" + VERBATIM + ) + endif () add_dependencies(ScopeOneCoreRuntime ScopeOne) endif () @@ -265,6 +280,7 @@ if (UNIX AND NOT APPLE) "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore.so*" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK.so*" "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter.so*" + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libonnxruntime*.so*" ) install(FILES ${SCOPEONE_CORE_SHARED_LIBRARIES} diff --git a/README.md b/README.md index 415237a..c6864b3 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,6 @@ Deep learning is becoming an increasingly powerful tool for microscopy image res Despite the deep learning, traditional image processing methods remain essential. GPU-accelerated image processing pipeline can be applied to live camera streams or recorded images, including filtering, background subtraction, FFT, and more. Users can customize image processing algorithms and parameters to suit their specific needs with Python processing modules. ## 🚀 Quick Start -### For Users - Download the latest release installer from the [Releases](https://github.com/Experimental-Microscopy-Lab/ScopeOne/releases) page. **System Requirements:** @@ -49,7 +47,7 @@ ScopeOne loads Micro-Manager configuration files (.cfg) directly and includes th There is an example dual-camera .cfg file in the config folder, just change the camera labels in Devices section to match your camera models. -### For Developers +## 🛠️ For Developers Read [CONTRIBUTING.md](CONTRIBUTING.md) before changing the code. @@ -71,7 +69,7 @@ git submodule update --init --recursive ### Drivers and processing modules -ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `processing/python`. The CUDA and ONNX processing modules are part of Core, which builds them when nvcc and ONNX Runtime are found. `-DSCOPEONE_ENABLE_CUDA=OFF` and `-DSCOPEONE_ENABLE_INFERENCE=OFF` build the standard Core without them. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [ScopeOneCore/python/scopeone/README.md](ScopeOneCore/python/scopeone/README.md#python-processing-modules) how to write a Python processing module. +ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `processing/python`. The CUDA and ONNX processing modules are part of Core. Core builds the CUDA modules when it finds nvcc, and ONNX inference always, with the GPU package of ONNX Runtime when CUDA is built and the CPU package otherwise. `-DSCOPEONE_ENABLE_CUDA=OFF` builds the standard Core and `-DSCOPEONE_ENABLE_INFERENCE=OFF` leaves ONNX out. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [ScopeOneCore/python/scopeone/README.md](ScopeOneCore/python/scopeone/README.md#python-processing-modules) how to write a Python processing module. **Windows Build Steps:** @@ -94,9 +92,9 @@ powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --run Windows does not run `.ps1` files by default. Its execution policy only guards against starting a script by accident, and `-ExecutionPolicy Bypass` lifts it for this one run without changing any setting of the machine. Use the same command for every build, or run `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned` once, which needs no administrator rights, and then start the script as `.\scripts\build.ps1`. -The script finds Visual Studio itself, so any terminal works. If `VCPKG_ROOT` is not set, it offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It clones the latest mmCoreAndDevices and builds MMCore with the DemoCamera and Utilities adapters, then ScopeOneCore, the drivers and the GUI. The first build compiles Qt and OpenCV with vcpkg and takes about an hour. vcpkg keeps the result in `vcpkg_installed\` and in its binary cache, so later builds only compile ScopeOne. +The script finds Visual Studio itself, so any terminal works. If `VCPKG_ROOT` is not set, it offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It clones the latest mmCoreAndDevices and builds MMCore with the DemoCamera and Utilities adapters, then ScopeOneCore, the drivers and the GUI. The first build compiles Qt, OpenCV and their dependencies with vcpkg, which takes about an hour on a four core machine, four fifths of it for Qt. vcpkg keeps the result in `vcpkg_installed\` and in its binary cache, so later builds only compile ScopeOne. -Later builds run the same command without `--run`, or with it to start ScopeOne afterwards. `--run` starts ScopeOne after the build, `--configure` configures again, `--clean` discards the build directories, and `--target core`, `gui` or `drivers` builds one part. Open `config\MMConfig_demo.cfg` in ScopeOne to try it without hardware. The last lines of the build show whether Core was built with the CUDA and ONNX modules, which it does when the CUDA Toolkit is installed and ONNX Runtime is unpacked in `ScopeOneCore\external`. +Later builds run the same command without `--run`, or with it to start ScopeOne afterwards. `--run` starts ScopeOne after the build, `--configure` configures again, `--clean` discards the build directories, and `--target core`, `gui` or `drivers` builds one part. Open `config\MMConfig_demo.cfg` in ScopeOne to try it without hardware. The first build downloads ONNX Runtime into `ScopeOneCore\external`, the GPU package when the CUDA Toolkit is installed. The last lines of the build show whether Core was built with the CUDA and ONNX modules. For a direct Core CMake configure, after the script has built MMCore once (`--target core`): @@ -146,7 +144,19 @@ brew install \ ./scripts/build.sh ``` -If vcpkg is missing, the script offers to download it into `$HOME/vcpkg` or a folder you type, and `--yes` takes the default folder without asking. It then builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. Set `JOBS` to limit parallel jobs and `BUILD_TYPE` to change the CMake build type (default `Release`). Pass `--clean` to discard the previous build directories. +If vcpkg is missing, the script offers to download it into `$HOME/vcpkg` or a folder you type, and `--yes` takes the default folder without asking. The first build compiles Qt, OpenCV and their dependencies with vcpkg, about 160 CPU minutes, 11 minutes on a 48 core Linux machine and about 40 on four cores, and later builds reuse them from the binary cache. It then builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. Set `JOBS` to limit parallel jobs and `BUILD_TYPE` to change the CMake build type (default `Release`). Pass `--clean` to discard the previous build directories. The first build downloads ONNX Runtime into `ScopeOneCore/external`. + +The vcpkg packages of a first build without the binary cache, measured on a 48 core Linux machine: + +| Package | Time | +|---|---| +| qtbase | 5.2 min | +| opencv4 | 2.2 min | +| qtsvg | 59 s | +| harfbuzz | 46 s | +| gperf, libb2, zstd, pcre2 | 10 to 22 s each | +| 29 others | under 10 s each | +| All 37 packages | 11 min 23 s | The executable is `build/ScopeOne`, and `config/MMConfig_demo.cfg` runs with the copied demo adapters. It loads Qt and OpenCV from `vcpkg_installed//lib`, so run it from the build tree. diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index c0df6e5..0216809 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -311,11 +311,86 @@ if (SCOPEONE_CUDA_NVCC_EXECUTABLE AND TARGET CUDA::cufft) endif () endif () +# ONNX Runtime is the official prebuilt package from the ONNX Runtime releases on GitHub, +# downloaded into external on the first configure. Core built with CUDA takes the GPU package and +# Core without it the CPU package, so CUDA alone decides between the GPU and the standard build. +# ONNXRUNTIME_ROOT names another SDK instead. +set(SCOPEONE_ONNXRUNTIME_VERSION 1.30.0) +set(ONNXRUNTIME_ROOT "" CACHE PATH "ONNX Runtime SDK root; empty selects the official package in external") +# Earlier configures cached the Windows GPU package as the default root, which then counted as a +# root set by hand and kept every other platform and the CPU package from being selected +if (ONNXRUNTIME_ROOT STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/external/onnxruntime-win-x64-gpu_cuda12-1.30.0") + set(ONNXRUNTIME_ROOT "" CACHE PATH "ONNX Runtime SDK root; empty selects the official package in external" FORCE) +endif () if (SCOPEONE_ENABLE_INFERENCE) - set(ONNXRUNTIME_ROOT - "${CMAKE_CURRENT_SOURCE_DIR}/external/onnxruntime-win-x64-gpu_cuda12-1.30.0" - CACHE PATH "ONNX Runtime SDK root" - ) + set(_ort_package "") + if (WIN32 AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64") + if (_with_cuda) + set(_ort_package "onnxruntime-win-x64-gpu_cuda12-${SCOPEONE_ONNXRUNTIME_VERSION}.zip") + set(_ort_sha256 d4667ea48eb0a10bc9b96b838f7b8975a6bf18de3bc5edd403a22e15c1458b23) + else () + set(_ort_package "onnxruntime-win-x64-${SCOPEONE_ONNXRUNTIME_VERSION}.zip") + set(_ort_sha256 c6ba983baf5681af108599675d2a89c2d145512d02de28aed0bff177cd0ba949) + endif () + elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") + if (_with_cuda) + set(_ort_package "onnxruntime-linux-x64-gpu_cuda12-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") + set(_ort_sha256 f9886932ee7bb0b4d3fcab736a392d4ff5efaa0672b47f19f0cec03437cf64f1) + else () + set(_ort_package "onnxruntime-linux-x64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") + set(_ort_sha256 a5ed5a3cac51fbb2e90da632ae43d19212faaa20e76484e62bcb7c23ddb3b3fd) + endif () + elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64") + set(_ort_package "onnxruntime-linux-aarch64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") + set(_ort_sha256 e16a27a8ed330bbc698df7330b0cf56e722f354e3bcc92118682c74ef3c3e3da) + elseif (APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") + # The macOS package has only the CPU provider + set(_ort_package "onnxruntime-osx-arm64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") + set(_ort_sha256 6ebb5062a934537c352937821f9fe9718e7de1a2db1122a93dd363ffd53a7012) + endif () + + set(_ort_download_error "") + if (ONNXRUNTIME_ROOT) + set(_ort_root "${ONNXRUNTIME_ROOT}") + elseif (_ort_package) + string(REGEX REPLACE "\\.(zip|tgz)$" "" _ort_name "${_ort_package}") + set(_ort_root "${CMAKE_CURRENT_SOURCE_DIR}/external/${_ort_name}") + set(_ort_url "https://github.com/microsoft/onnxruntime/releases/download/v${SCOPEONE_ONNXRUNTIME_VERSION}/${_ort_package}") + if (NOT EXISTS "${_ort_root}/include") + message(STATUS "Downloading ${_ort_url}") + set(_ort_archive "${CMAKE_CURRENT_BINARY_DIR}/${_ort_package}") + # EXPECTED_HASH turned a failed download into a configure error, so the hash is + # checked after it. Without the network Core is built without ONNX inference, and the + # next configure tries again. + file(DOWNLOAD "${_ort_url}" "${_ort_archive}" + SHOW_PROGRESS + STATUS _ort_download + ) + list(GET _ort_download 0 _ort_download_code) + list(GET _ort_download 1 _ort_download_error) + if (_ort_download_code EQUAL 0) + file(SHA256 "${_ort_archive}" _ort_actual_sha256) + if (_ort_actual_sha256 STREQUAL _ort_sha256) + set(_ort_download_error "") + file(ARCHIVE_EXTRACT INPUT "${_ort_archive}" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/external") + else () + set(_ort_download_error "SHA256 mismatch") + endif () + endif () + file(REMOVE "${_ort_archive}") + endif () + else () + set(_ort_root "") + endif () + + # The found paths are cached, so a root that changed, such as the GPU package taking over + # once CUDA is found, kept linking the package of the earlier configure + if (NOT "${SCOPEONE_ONNXRUNTIME_ROOT_USED}" STREQUAL "${_ort_root}") + unset(ONNXRUNTIME_INCLUDE_DIR CACHE) + unset(ONNXRUNTIME_LIBRARY CACHE) + endif () + set(SCOPEONE_ONNXRUNTIME_ROOT_USED "${_ort_root}" CACHE INTERNAL "") + set(ONNXRUNTIME_ROOT "${_ort_root}") find_path(ONNXRUNTIME_INCLUDE_DIR NAMES onnxruntime_cxx_api.h PATHS "${ONNXRUNTIME_ROOT}/include" @@ -354,7 +429,21 @@ if (SCOPEONE_ENABLE_INFERENCE AND ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRAR message(FATAL_ERROR "No ONNX Runtime DLLs found under ${ONNXRUNTIME_ROOT}") endif () list(APPEND SCOPEONE_RUNTIME_DLLS ${_onnxruntime_dlls}) + else () + # The installed Core resolves its libraries beside itself. Left in external, ONNX Runtime + # was not on its runpath and the GUI failed to link against the installed Core. + file(GLOB _onnxruntime_libs + "${ONNXRUNTIME_ROOT}/lib/libonnxruntime*.so*" + "${ONNXRUNTIME_ROOT}/lib/libonnxruntime*.dylib" + ) + install(FILES ${_onnxruntime_libs} DESTINATION "${CMAKE_INSTALL_LIBDIR}") endif () +elseif (SCOPEONE_ENABLE_INFERENCE AND NOT ONNXRUNTIME_ROOT) + set(_inference_reason "no official ONNX Runtime package for ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_PROCESSOR}; set ONNXRUNTIME_ROOT") + message(STATUS "${_inference_reason}; building Core without ONNX inference") +elseif (SCOPEONE_ENABLE_INFERENCE AND _ort_download_error) + set(_inference_reason "downloading ${_ort_url} failed (${_ort_download_error}); extract it into ${CMAKE_CURRENT_SOURCE_DIR}/external") + message(STATUS "${_inference_reason}; building Core without ONNX inference") elseif (SCOPEONE_ENABLE_INFERENCE) set(_inference_reason "ONNX Runtime was not found under ${ONNXRUNTIME_ROOT}") message(STATUS "${_inference_reason}; building Core without ONNX inference") diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 34b0612..30532b3 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -121,7 +121,7 @@ Providers use ScopeOne logical device IDs and publish `ImageFrame` objects throu - `drivers` contains the drivers, among them NI-DAQmx and the example camera and PMT. Each provider runs in an isolated DriverHost process, so a vendor library such as NI-DAQmx is never loaded into ScopeOne. Micro-Manager Device Adapters remain under Micro-Manager and are not wrapped as ScopeOne drivers. - Python processing modules are scripts in `processing/python`, beside the executable or in the application data directory. Core runs each module of a pipeline in its own Python process. -- The CUDA and ONNX processing modules are part of Core when `SCOPEONE_ENABLE_CUDA` and `SCOPEONE_ENABLE_INFERENCE` are on and nvcc and ONNX Runtime are found. With both off, Core neither links nor ships CUDA or ONNX Runtime. Every processing method, the PTU file signal source and the DHM reconstruction are part of Core. +- The CUDA and ONNX processing modules are part of Core. CUDA is built when nvcc is found and `SCOPEONE_ENABLE_CUDA` is on. ONNX inference is built when `SCOPEONE_ENABLE_INFERENCE` is on, with the official ONNX Runtime package that the configure downloads into `external`, the GPU package when CUDA is built and the CPU package otherwise. `ONNXRUNTIME_ROOT` points Core at another SDK, and with both switches off Core neither links nor ships CUDA or ONNX Runtime. Every processing method, the PTU file signal source and the DHM reconstruction are part of Core. - The tool windows (Scale, Stage Mosaic, Particle Detection, DHM Reconstruction, Scanning and DAQ Control, Signal Monitor) are part of the application. They use the `ScopeOneCore` facade, and `ScopeOneToolContext` in `src/tools/ToolContext.h` gives them the desktop operations such as showing layers. Drivers outside this repository consume the exported `scopeone::DriverSDK` CMake target. Every driver manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`, and incompatible manifests are rejected before the library instance is created. diff --git a/scripts/CopyRuntimeFiles.cmake b/scripts/CopyRuntimeFiles.cmake new file mode 100644 index 0000000..c08b387 --- /dev/null +++ b/scripts/CopyRuntimeFiles.cmake @@ -0,0 +1,4 @@ +# Copies the files in FILES (separated by |) into DEST. file(COPY) keeps symbolic links as links, +# where cmake -E copy_if_different copied each name of a library as a full copy of its file. +string(REPLACE "|" ";" _files "${FILES}") +file(COPY ${_files} DESTINATION "${DEST}") From 677a1ce383dcd9ac3636b2f0017e8fa909e17503 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 13:24:33 +0100 Subject: [PATCH 22/49] Build against an installed Qt 6.8 or later, simplify the build scripts and CMake, and rewrite the developer guide --- .github/workflows/compile-check.yml | 57 ++++ .github/workflows/windows-release.yml | 5 +- CMakeLists.txt | 77 ++--- CONTRIBUTING.md | 6 +- README.md | 216 +++++++++----- ScopeOneCore/CMakeLists.txt | 202 ++++++------- ScopeOneCore/README.md | 4 +- .../cmake/ScopeOneCoreConfig.cmake.in | 5 +- ScopeOneCore/cmake/ScopeOneQt.cmake | 24 ++ drivers/CMakeLists.txt | 3 +- drivers/README.md | 8 +- scripts/build.ps1 | 276 ++++++------------ scripts/build.sh | 43 ++- vcpkg.json | 52 ++-- 14 files changed, 516 insertions(+), 462 deletions(-) create mode 100644 ScopeOneCore/cmake/ScopeOneQt.cmake diff --git a/.github/workflows/compile-check.yml b/.github/workflows/compile-check.yml index a672ae6..abc507c 100644 --- a/.github/workflows/compile-check.yml +++ b/.github/workflows/compile-check.yml @@ -65,6 +65,63 @@ jobs: shell: bash run: ./scripts/build.sh + compile-installed-qt: + name: Compile (Linux, installed Qt 6.8) + runs-on: ubuntu-24.04 + timeout-minutes: 180 + + steps: + - name: Check out ScopeOne + uses: actions/checkout@v7 + + - name: Install build dependencies + shell: bash + run: | + sudo apt-get update + sudo apt-get install -y \ + git \ + build-essential \ + cmake \ + autoconf \ + automake \ + libtool \ + autoconf-archive \ + pkg-config \ + libboost-all-dev \ + ninja-build curl zip unzip \ + libegl-dev libopengl-dev libx11-xcb-dev libxrender-dev \ + libxcb-cursor-dev libxcb-icccm4-dev libxcb-util-dev libxcb-keysyms1-dev \ + libxcb-randr0-dev libxcb-sync-dev libxcb-xinput-dev libxcb-glx0-dev \ + libxkbcommon-x11-dev + + - name: Set up vcpkg + shell: bash + run: | + git clone https://github.com/microsoft/vcpkg.git "$RUNNER_TEMP/scopeone-vcpkg" + git -C "$RUNNER_TEMP/scopeone-vcpkg" checkout "$(jq -r '."builtin-baseline"' vcpkg.json)" + "$RUNNER_TEMP/scopeone-vcpkg/bootstrap-vcpkg.sh" -disableMetrics + mkdir -p "$RUNNER_TEMP/vcpkg-binary-cache" + echo "VCPKG_ROOT=$RUNNER_TEMP/scopeone-vcpkg" >> "$GITHUB_ENV" + echo "VCPKG_DEFAULT_BINARY_CACHE=$RUNNER_TEMP/vcpkg-binary-cache" >> "$GITHUB_ENV" + + - name: Cache vcpkg binaries + uses: actions/cache@v6 + with: + path: ${{ runner.temp }}/vcpkg-binary-cache + key: vcpkg-scopeone-ubuntu-24.04-installed-qt-${{ hashFiles('vcpkg.json') }} + restore-keys: vcpkg-scopeone-ubuntu-24.04-installed-qt- + + # The oldest Qt that ScopeOne supports, so a newer Qt API used by mistake fails here + - name: Install Qt + uses: jurplel/install-qt-action@v4 + with: + version: 6.8.0 + cache: true + + - name: Compile mmCoreAndDevices and ScopeOne + shell: bash + run: ./scripts/build.sh --qt "$QT_ROOT_DIR" + compile-macos: name: Compile (macOS) runs-on: macos-15 diff --git a/.github/workflows/windows-release.yml b/.github/workflows/windows-release.yml index e3809e0..d7fabbb 100644 --- a/.github/workflows/windows-release.yml +++ b/.github/workflows/windows-release.yml @@ -181,10 +181,7 @@ jobs: shell: pwsh run: | $options = @() - if ("${{ matrix.gpu }}" -eq "true") { - $options += @("--guiConfigureOption", "-DSCOPEONE_PACKAGE_VARIANT=gpu") - } - else { + if ("${{ matrix.gpu }}" -ne "true") { $options += @("--coreConfigureOption", "-DSCOPEONE_ENABLE_CUDA=OFF") } .\scripts\build.ps1 --configure --package @options diff --git a/CMakeLists.txt b/CMakeLists.txt index bf313bc..de382f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.23) include("${CMAKE_CURRENT_LIST_DIR}/ScopeOneCore/cmake/ScopeOneVersion.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/ScopeOneCore/cmake/ScopeOneQt.cmake") scopeone_resolve_version(SCOPEONE_VERSION "${CMAKE_CURRENT_LIST_DIR}") project(ScopeOne VERSION "${SCOPEONE_VERSION}" LANGUAGES CXX) @@ -37,7 +38,7 @@ configure_file( @ONLY ) -find_package(Qt6 COMPONENTS +find_package(Qt6 ${SCOPEONE_QT_MINIMUM_VERSION} COMPONENTS Core Gui Widgets @@ -47,6 +48,7 @@ find_package(Qt6 COMPONENTS Concurrent REQUIRED ) +message(STATUS "Using Qt ${Qt6_VERSION} from ${Qt6_DIR}") find_package(ScopeOneCore CONFIG REQUIRED) @@ -232,19 +234,17 @@ if (WIN32) "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/ScopeOne_DriverHost.exe" ) else () - file(GLOB SCOPEONE_CORE_RUNTIME_FILES + # The lib folder of the Core install holds only Core's libraries and ONNX Runtime, and the + # copied DriverHost resolves them beside itself. Named one by one, the list had missed the + # macOS dylibs. + file(GLOB SCOPEONE_CORE_LIBRARIES LIST_DIRECTORIES false + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/*.so*" + "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/*.dylib" + ) + set(SCOPEONE_CORE_RUNTIME_FILES "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/ScopeOne_DriverHost" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore.so*" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK.so*" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter.so*" - # The copied DriverHost resolves its libraries beside itself, and the .so patterns - # alone left the macOS dylibs behind in the Core install - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore*.dylib" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK*.dylib" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter*.dylib" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libonnxruntime*.so*" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libonnxruntime*.dylib" + ${SCOPEONE_CORE_LIBRARIES} ) endif () @@ -274,18 +274,10 @@ if (SCOPEONE_CORE_RUNTIME_FILES) add_dependencies(ScopeOneCoreRuntime ScopeOne) endif () -if (UNIX AND NOT APPLE) - file(GLOB SCOPEONE_CORE_SHARED_LIBRARIES - LIST_DIRECTORIES false - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneCore.so*" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeOneDriverSDK.so*" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libScopeWriter.so*" - "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_LIBDIR}/libonnxruntime*.so*" - ) - install(FILES - ${SCOPEONE_CORE_SHARED_LIBRARIES} - DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ) +if (WIN32) + install(FILES ${SCOPEONE_CORE_RUNTIME_FILES} DESTINATION .) +elseif (NOT APPLE) + install(FILES ${SCOPEONE_CORE_LIBRARIES} DESTINATION "${CMAKE_INSTALL_LIBDIR}") endif () set(_driver_dir "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/drivers") @@ -314,37 +306,14 @@ if (WIN32) endif () endif () -# The Visual Studio generator compiles the sources of one project one after another unless the -# compiler runs them in parallel itself +# /MP compiles the sources of one project in parallel, which the Visual Studio generator does not, +# and Release builds use LTO if (MSVC) - target_compile_options(ScopeOne PRIVATE /MP) + target_compile_options(ScopeOne PRIVATE /MP $<$:/GL /fp:fast /arch:AVX2>) + target_link_options(ScopeOne PRIVATE $<$:/LTCG>) target_compile_options(ScopeOneMcpServer PRIVATE /MP) endif () - -# LTO Release build -if (MSVC) - target_compile_options(ScopeOne PRIVATE - $<$:/GL /fp:fast /arch:AVX2> - ) - target_link_options(ScopeOne PRIVATE - $<$:/LTCG> - ) -endif () if (WIN32) - install(CODE " - file(GLOB _core_runtime_files - \"$/*.dll\" - \"$/ScopeOne_DriverHost.exe\" - ) - if (_core_runtime_files) - file(INSTALL - DESTINATION \"\${CMAKE_INSTALL_PREFIX}\" - TYPE FILE - FILES \${_core_runtime_files} - ) - endif() - ") - install(CODE " set(_runtime_dir \"${CMAKE_BINARY_DIR}/\${CMAKE_INSTALL_CONFIG_NAME}\") if (NOT EXISTS \"\${_runtime_dir}/$\") @@ -395,9 +364,9 @@ set(CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/Experimental-Microscopy-Lab/S set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE") set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF) set(CPACK_PACKAGE_DIRECTORY "${CMAKE_BINARY_DIR}") -set(SCOPEONE_PACKAGE_VARIANT "" CACHE STRING "Package variant suffix, for example gpu") -if (SCOPEONE_PACKAGE_VARIANT) - set(CPACK_PACKAGE_FILE_NAME "ScopeOne-${PROJECT_VERSION}-win-x64-${SCOPEONE_PACKAGE_VARIANT}") +# A Core built with CUDA makes the GPU package +if (ScopeOneCore_WITH_CUDA) + set(CPACK_PACKAGE_FILE_NAME "ScopeOne-${PROJECT_VERSION}-win-x64-gpu") else () set(CPACK_PACKAGE_FILE_NAME "ScopeOne-${PROJECT_VERSION}-win-x64") endif () diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2224d03..488e355 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -41,8 +41,7 @@ image menus. **Extension boundary.** Drivers and Python processing modules are the only public extension points, and both run outside the ScopeOne process. Every other processing method belongs in Core, with its window in `src`. The CUDA and ONNX modules are in Core too, under -`src/cuda` and `src/inference`, and `SCOPEONE_ENABLE_CUDA` and `SCOPEONE_ENABLE_INFERENCE` build -Core without them. The +`src/cuda` and `src/inference`, and `SCOPEONE_ENABLE_CUDA` builds Core without CUDA. The messages between ScopeOne and `ScopeOne_DriverHost` are internal too and are described in `ScopeOneCore/DRIVERHOST_PROTOCOL.md`. @@ -219,6 +218,9 @@ in only some of them is a bug. the existing dependencies cannot meet, a licence that allows redistribution with ScopeOne, and a pinned version in `vcpkg.json`. +**Qt version.** ScopeOne supports Qt 6.8 and later, so code does not use Qt API added after 6.8. +A newer API that is worth it is a reason to raise the minimum; open an issue to discuss it. + ## Versions **Core.** The version is `MAJOR.MINOR.MMDD`: `MAJOR.MINOR` comes from `VERSION`, the last part is diff --git a/README.md b/README.md index c6864b3..df60879 100644 --- a/README.md +++ b/README.md @@ -49,54 +49,110 @@ There is an example dual-camera .cfg file in the config folder, just change the ## 🛠️ For Developers -Read [CONTRIBUTING.md](CONTRIBUTING.md) before changing the code. +**TL;DR.** On a fresh machine, without spending time on setting up the C++ environment, run these commands. They install the tools, clone ScopeOne, and let the script build everything else, and start ScopeOne. The first build takes about an hour on four cores. -**Prerequisites:** +Windows: -- Git, for the ScopeWriter submodule and Micro-Manager -- A C++20 compiler: [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) or its Build Tools (MSVC v143 toolset) on Windows, GCC or Clang on Linux and macOS (experimental) -- [CMake](https://cmake.org/download/) 3.23 or newer, which comes with the C++ tools of Visual Studio on Windows -- Python 3.14, only for the Python package and Python processing modules +```powershell +winget install Microsoft.VisualStudio.2022.BuildTools --override "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --passive --wait" +git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/ScopeOne.git +cd ScopeOne +powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --run +``` -The build scripts set up the rest. They download [vcpkg](https://github.com/microsoft/vcpkg) when `VCPKG_ROOT` does not name a checkout, and vcpkg builds Qt 6.11.2, OpenCV 4.14.0 and ScopeWriter's libtiff, zlib, zstd and crc32c inside the repository. They also fetch and build Micro-Manager's core with the DemoCamera and Utilities adapters. +Linux (Debian, Ubuntu): -Clone ScopeOne with its submodules, or initialize them in an existing checkout: +```bash +sudo apt install \ + build-essential cmake autoconf automake libtool autoconf-archive \ + pkg-config ninja-build curl zip unzip libboost-all-dev +sudo apt install --no-upgrade \ + libegl-dev libopengl-dev libx11-xcb-dev libxrender-dev \ + libxcb-cursor-dev libxcb-icccm4-dev libxcb-util-dev libxcb-keysyms1-dev \ + libxcb-randr0-dev libxcb-sync-dev libxcb-xinput-dev libxcb-glx0-dev \ + libxkbcommon-x11-dev +git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/ScopeOne.git +cd ScopeOne +./scripts/build.sh --run +``` -```powershell +macOS: + +```bash +xcode-select --install +brew install cmake autoconf automake libtool autoconf-archive pkg-config ninja boost git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/ScopeOne.git -git submodule update --init --recursive +cd ScopeOne +./scripts/build.sh --run ``` -### Drivers and processing modules +A coding agent such as Claude Code or Codex can also do this: ask it to set up and build ScopeOne by following this README. -ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `processing/python`. The CUDA and ONNX processing modules are part of Core. Core builds the CUDA modules when it finds nvcc, and ONNX inference always, with the GPU package of ONNX Runtime when CUDA is built and the CPU package otherwise. `-DSCOPEONE_ENABLE_CUDA=OFF` builds the standard Core and `-DSCOPEONE_ENABLE_INFERENCE=OFF` leaves ONNX out. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [ScopeOneCore/python/scopeone/README.md](ScopeOneCore/python/scopeone/README.md#python-processing-modules) how to write a Python processing module. +The sections below explain each step, the options and how to use a Qt that is already installed. Read [CONTRIBUTING.md](CONTRIBUTING.md) before changing the code. -**Windows Build Steps:** +### Qt -On a new Windows machine, install Git and the Visual Studio 2022 Build Tools with the C++ workload, which bring MSVC, MSBuild, the Windows SDK and CMake. An existing Visual Studio 2022 with "Desktop development with C++" works as well. +ScopeOne needs Qt 6.8 or later. There are three ways to get it. -```powershell -winget install Git.Git -winget install Microsoft.VisualStudio.2022.BuildTools --override "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --passive --wait" -``` +- **Qt is already installed.** Pass its directory once and the script uses it instead of building Qt: -`winget` is the package manager included in Windows 10 and 11. The Build Tools are Visual Studio without the IDE, free to use. `--override` passes its options to the Visual Studio installer: `--add ...VCTools` selects the C++ build tools, `--includeRecommended` adds the Windows SDK and CMake, `--passive` shows only a progress bar, and `--wait` returns once the installation has finished. The installer asks for administrator rights, downloads a few gigabytes and takes 10 to 20 minutes. + ```powershell + # Windows + powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --qt C:\Qt\6.11.2\msvc2022_64 + # Linux and macOS + ./scripts/build.sh --qt ~/Qt/6.11.2/gcc_64 # macOS: ~/Qt/6.11.2/macos + ``` -Then clone ScopeOne and build and start it with one command, in a new terminal so Git is on the path: + On Windows the script saves the path as `SCOPEONE_QT_DIR` for your user account, and `--qt vcpkg` goes back to the Qt that vcpkg builds. On Linux and macOS, export `SCOPEONE_QT_DIR` with that path to keep it without `--qt`. -```powershell -git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/ScopeOne.git -cd ScopeOne -powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --run -``` +- **Install Qt yourself** with the [Qt Online Installer](https://www.qt.io/download-qt-installer-oss), then pass its directory as above. On Windows choose the MSVC 2022 64-bit build; MinGW does not work. + +- **Let the script build it.** Without `--qt`, vcpkg builds Qt on the first build. Qt is about four fifths of that build, and vcpkg keeps the result, so it is built once. -Windows does not run `.ps1` files by default. Its execution policy only guards against starting a script by accident, and `-ExecutionPolicy Bypass` lifts it for this one run without changing any setting of the machine. Use the same command for every build, or run `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned` once, which needs no administrator rights, and then start the script as `.\scripts\build.ps1`. +### Windows, step by step -The script finds Visual Studio itself, so any terminal works. If `VCPKG_ROOT` is not set, it offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It clones the latest mmCoreAndDevices and builds MMCore with the DemoCamera and Utilities adapters, then ScopeOneCore, the drivers and the GUI. The first build compiles Qt, OpenCV and their dependencies with vcpkg, which takes about an hour on a four core machine, four fifths of it for Qt. vcpkg keeps the result in `vcpkg_installed\` and in its binary cache, so later builds only compile ScopeOne. +1. **Install the C++ build tools.** In a terminal: -Later builds run the same command without `--run`, or with it to start ScopeOne afterwards. `--run` starts ScopeOne after the build, `--configure` configures again, `--clean` discards the build directories, and `--target core`, `gui` or `drivers` builds one part. Open `config\MMConfig_demo.cfg` in ScopeOne to try it without hardware. The first build downloads ONNX Runtime into `ScopeOneCore\external`, the GPU package when the CUDA Toolkit is installed. The last lines of the build show whether Core was built with the CUDA and ONNX modules. + ```powershell + winget install Microsoft.VisualStudio.2022.BuildTools --override "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --passive --wait" + ``` -For a direct Core CMake configure, after the script has built MMCore once (`--target core`): + `winget` comes with Windows 10 and 11. The command installs MSVC, the Windows SDK and CMake. It asks for administrator rights, downloads a few gigabytes and takes 10 to 20 minutes. An existing Visual Studio 2022 with "Desktop development with C++" works as well. + +2. **Clone ScopeOne.** + + ```powershell + git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/ScopeOne.git + cd ScopeOne + ``` + +3. **Build and start it.** + + ```powershell + powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --run + ``` + + `-ExecutionPolicy Bypass` lets this command run a `.ps1` file. After `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned` once, the script starts as `.\scripts\build.ps1`. + + The script finds Visual Studio itself. It offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It then builds MMCore with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI, and downloads ONNX Runtime into `ScopeOneCore\external`, the GPU package when the CUDA Toolkit is installed. + + Without an installed Qt, the first build compiles Qt, OpenCV and their dependencies with vcpkg, which takes about an hour on a four core machine, four fifths of it for Qt. vcpkg keeps the result, so later builds only compile ScopeOne. The last lines of the build show whether Core was built with the CUDA and ONNX modules. + +4. **Try it without hardware.** Open `config\MMConfig_demo.cfg` in ScopeOne. The application is `build\Release\ScopeOne.exe`. + +5. **Build again after a change** with the same command. The options are: + + | Option | Effect | + |---|---| + | `--run` | Start ScopeOne after the build | + | `--target core`, `gui` or `drivers` | Build one part | + | `--configure` | Configure again, for new source files or a newly installed CUDA | + | `--clean` | Discard the build directories and the Core install and build from scratch | + | `--package` | Build the installer and zip, named `-gpu` when Core was built with CUDA | + | `--qt ` | Use an installed Qt, see [Qt](#qt) | + | `--yes` | Accept the vcpkg download without asking | + +**CMake without the script.** After the script has built MMCore once (`--target core`), Core configures directly. Add `-DSCOPEONE_QT_DIR=` for an installed Qt: ```powershell cmake -S ScopeOneCore -B ScopeOneCore/build ` @@ -106,69 +162,80 @@ cmake -S ScopeOneCore -B ScopeOneCore/build ` -DVCPKG_TARGET_TRIPLET=x64-windows ``` -The built application is `build\Release\ScopeOne.exe`. +### Linux and macOS, step by step -**Linux and macOS Build Steps:** +macOS support is experimental. -Micro-Manager is built from its own checkout because its native core and device adapters are not vcpkg dependencies. Everything else comes from vcpkg with the dynamic triplet of the host, so Qt is shared between the GUI, ScopeOneCore and the drivers. +1. **Install the build tools.** -1. Install the system build tools. + Linux (Debian, Ubuntu): -Linux: + ```bash + sudo apt install \ + build-essential cmake autoconf automake libtool autoconf-archive \ + pkg-config ninja-build curl zip unzip libboost-all-dev + sudo apt install --no-upgrade \ + libegl-dev libopengl-dev libx11-xcb-dev libxrender-dev \ + libxcb-cursor-dev libxcb-icccm4-dev libxcb-util-dev libxcb-keysyms1-dev \ + libxcb-randr0-dev libxcb-sync-dev libxcb-xinput-dev libxcb-glx0-dev \ + libxkbcommon-x11-dev + ``` -```bash -sudo apt install \ - git build-essential cmake autoconf automake libtool autoconf-archive \ - pkg-config ninja-build curl zip unzip libboost-all-dev -sudo apt install --no-upgrade \ - libegl-dev libopengl-dev libx11-xcb-dev libxrender-dev \ - libxcb-cursor-dev libxcb-icccm4-dev libxcb-util-dev libxcb-keysyms1-dev \ - libxcb-randr0-dev libxcb-sync-dev libxcb-xinput-dev libxcb-glx0-dev \ - libxkbcommon-x11-dev -``` + The second command installs the X11 and OpenGL development files that vcpkg's Qt needs, and `--no-upgrade` keeps the installed Mesa drivers. The script lists the package names for Fedora when a tool is missing. -The second command installs the X11 and OpenGL development files that vcpkg's `qtbase` port expects from the system. It is not a system Qt installation, and `--no-upgrade` keeps it from upgrading the installed Mesa drivers. + macOS: -macOS: + ```bash + xcode-select --install + brew install \ + cmake autoconf automake libtool autoconf-archive \ + pkg-config ninja boost + ``` -```bash -xcode-select --install -brew install \ - git cmake autoconf automake libtool autoconf-archive \ - pkg-config ninja boost -``` +2. **Clone ScopeOne.** -2. Build from the ScopeOne repository root: + ```bash + git clone --recurse-submodules https://github.com/Experimental-Microscopy-Lab/ScopeOne.git + cd ScopeOne + ``` -```bash -./scripts/build.sh -``` +3. **Build and start it.** + + ```bash + ./scripts/build.sh --run + ``` + + The script checks the build tools, offers to download vcpkg into `$HOME/vcpkg` or a folder you type (`--yes` takes the default), and builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. It downloads ONNX Runtime into `ScopeOneCore/external`. -If vcpkg is missing, the script offers to download it into `$HOME/vcpkg` or a folder you type, and `--yes` takes the default folder without asking. The first build compiles Qt, OpenCV and their dependencies with vcpkg, about 160 CPU minutes, 11 minutes on a 48 core Linux machine and about 40 on four cores, and later builds reuse them from the binary cache. It then builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. Set `JOBS` to limit parallel jobs and `BUILD_TYPE` to change the CMake build type (default `Release`). Pass `--clean` to discard the previous build directories. The first build downloads ONNX Runtime into `ScopeOneCore/external`. + Without an installed Qt, the first build compiles Qt, OpenCV and their dependencies with vcpkg, about 160 CPU minutes, which is 11 minutes on a 48 core Linux machine and about 40 on four cores. Later builds reuse them from the binary cache. Measured without the cache on the 48 core machine: -The vcpkg packages of a first build without the binary cache, measured on a 48 core Linux machine: + | Package | Time | + |---|---| + | qtbase | 5.2 min | + | opencv4 | 2.2 min | + | qtsvg | 59 s | + | harfbuzz | 46 s | + | gperf, libb2, zstd, pcre2 | 10 to 22 s each | + | 29 others | under 10 s each | + | All 37 packages | 11 min 23 s | -| Package | Time | -|---|---| -| qtbase | 5.2 min | -| opencv4 | 2.2 min | -| qtsvg | 59 s | -| harfbuzz | 46 s | -| gperf, libb2, zstd, pcre2 | 10 to 22 s each | -| 29 others | under 10 s each | -| All 37 packages | 11 min 23 s | +4. **Try it without hardware.** The application is `build/ScopeOne`. Run it from the build tree and open `config/MMConfig_demo.cfg`. -The executable is `build/ScopeOne`, and `config/MMConfig_demo.cfg` runs with the copied demo adapters. It loads Qt and OpenCV from `vcpkg_installed//lib`, so run it from the build tree. +5. **Build again after a change** with the same command. `--clean` discards the build directories and the Core install, `--run` starts ScopeOne afterwards, `JOBS` limits the parallel jobs and `BUILD_TYPE` sets the CMake build type (default `Release`). + +### Drivers and processing modules + +ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `processing/python`. The CUDA and ONNX processing modules are part of Core. Core builds the CUDA modules when it finds nvcc, and ONNX inference always, with the GPU package of ONNX Runtime when CUDA is built and the CPU package otherwise. `-DSCOPEONE_ENABLE_CUDA=OFF` builds the standard Core. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [ScopeOneCore/python/scopeone/README.md](ScopeOneCore/python/scopeone/README.md#python-processing-modules) how to write a Python processing module. Python 3.14 is needed only for the Python package and Python processing modules. ## 🤖 Automation and AI Agents -The desktop app exposes a language-neutral local control API and shared-memory frame channel. An AI agent does not run inside ScopeOne or depend on Python. A tool adapter can discover supported operation groups with the `capabilities` request, read a structured observation with `state_snapshot`, and invoke the exposed camera, stage, mosaic, processing, image analysis, experiment, recording, layer, and markup operations. Requests may carry an ID that is echoed by the app for correlation. Recording save operations use `ome-tiff` by default and also accept `ome-zarr`, `tiff` and `binary`. +The desktop app exposes a language-neutral local control API and shared-memory frame channel. A tool adapter can discover supported operation groups with the `capabilities` request, read a structured observation with `state_snapshot`, and invoke the exposed camera, stage, mosaic, processing, image analysis, experiment, recording, layer, and markup operations. Requests may carry an ID that is echoed by the app for correlation. Recording save operations use `ome-tiff` by default and also accept `ome-zarr`, `tiff` and `binary`. The API reports which operations mutate hardware, write files, remove state, or may run for a long time. An agent adapter should request user confirmation before those operations and verify the result with the returned read-back value, experiment status, or a new state snapshot. The Python package is one optional client implementation. See the [Python client and Local API protocol guide](ScopeOneCore/python/scopeone/README.md) for protocol details and runnable examples. ### MCP adapter -`ScopeOneMcpServer` is a standalone C++ MCP server included with ScopeOne. It uses MCP protocol version `2025-06-18` over standard input and output and forwards validated tool calls to the running desktop app through the Local API. It does not embed a model, open a network port, or depend on Python. +`ScopeOneMcpServer` is a standalone C++ MCP server included with ScopeOne. It uses MCP protocol version `2025-06-18` over standard input and output and forwards validated tool calls to the running desktop app through the Local API. #### Claude Code plugin @@ -180,7 +247,7 @@ Install ScopeOne first, then run these commands inside Claude Code: /reload-plugins ``` -When prompted, select `ScopeOneMcpServer.exe` from the same directory as `ScopeOne.exe`. Start ScopeOne before asking Claude to inspect or control it. The plugin supplies the MCP configuration and agent operating guidance, so no Python installation or manual MCP server launch is required. +When prompted, select `ScopeOneMcpServer.exe` from the same directory as `ScopeOne.exe`. Start ScopeOne before asking Claude to inspect or control it. The plugin supplies the MCP configuration and the agent guidance. #### Other MCP hosts @@ -193,7 +260,7 @@ To use it: Use `scopeone` as the server name, `stdio` as the transport, the absolute path to `ScopeOneMcpServer.exe` as the command, and no command-line arguments. The exact configuration syntax depends on the agent host. -The MCP tool set mirrors the Local API operation catalog, including system state, configuration, preview layers, independent image windows, automatic display levels, source alignment, markups, device properties, exposure, ROI, stages, stage mosaics, processing, experiments, recording sessions, frame transfer, and analysis. Agents can list, open, activate, process, save, and close independent image windows, read the current frame of any image layer, monitor live acquisition and writer progress, and optionally export or display particle masks. ScopeOne remains the authority for parameter validation and hardware read-back, and MCP tool calls are visible in the desktop UI through the same application state used by manual controls. +The MCP tool set mirrors the Local API operation catalog, including system state, configuration, preview layers, independent image windows, automatic display levels, source alignment, markups, device properties, exposure, ROI, stages, stage mosaics, processing, experiments, recording sessions, frame transfer, and analysis. Agents can list, open, activate, process, save, and close independent image windows, read the current frame of any image layer, monitor live acquisition and writer progress, and optionally export or display particle masks. MCP tool calls are visible in the desktop UI through the same application state used by manual controls. Configuration loading and unloading report an explicit lifecycle state through the Local API and MCP. A configuration with non-camera initialization warnings is reported as `partially_loaded` with failed device labels; camera backend startup failures are cleaned up and reported as errors. During `loading` or `unloading`, hardware mutations are rejected until the operation finishes. @@ -203,7 +270,7 @@ Configuration loading and unloading report an explicit lifecycle state through t - Hamamatsu C13440 - Andor 897D -The current validation list is still short, but the codebase has been cleaned to remove early hard-coded device assumptions. In principle, ScopeOne should follow Micro-Manager device compatibility. +Other devices work through the same Micro-Manager device adapters. ## 🖥️ Tested System Configurations @@ -212,4 +279,5 @@ The current validation list is still short, but the codebase has been cleaned to - Fedora Linux 44 (Workstation Edition), Intel(R) Core(TM) i7-7700, 32 GB RAM - macOS 26.5.2, Apple M1 Pro, 16 GB RAM -We build and test ScopeOne on the above machines, which are comparatively older and weaker than many typical optical lab computers. However, ScopeOne still provides smooth real-time preview and processing on them. + +ScopeOne is built and tested on the above machines, which are comparatively older and weaker than many typical optical lab computers. However, ScopeOne still provides smooth real-time preview and processing on them, as an example, with a Hamamatsu C11440 camera at 2048 × 2048 and 100 fps, the live preview shows the overshoot of a PI M-112.1DG stage move: the stage passes the target and moves back. We have not seen this in the live preview of other camera control software. diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index 0216809..714e376 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.23) include("${CMAKE_CURRENT_LIST_DIR}/cmake/ScopeOneVersion.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/cmake/ScopeOneQt.cmake") scopeone_resolve_version(SCOPEONE_VERSION "${CMAKE_CURRENT_LIST_DIR}/..") project(ScopeOneCore VERSION "${SCOPEONE_VERSION}" LANGUAGES CXX) @@ -16,7 +17,8 @@ set(MMCORE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/mmCoreAndDevices") set(MMCORE_INCLUDE_DIR "${MMCORE_DIR}/MMCore") set(MMDEVICE_INCLUDE_DIR "${MMCORE_DIR}/MMDevice") -find_package(Qt6 COMPONENTS Core Gui Network Concurrent REQUIRED) +find_package(Qt6 ${SCOPEONE_QT_MINIMUM_VERSION} COMPONENTS Core Gui Network Concurrent REQUIRED) +message(STATUS "Using Qt ${Qt6_VERSION} from ${Qt6_DIR}") find_package(OpenCV CONFIG REQUIRED COMPONENTS core imgproc imgcodecs) set(SCOPEWRITER_BUILD_TESTS OFF) @@ -225,15 +227,14 @@ target_compile_definitions(ScopeOneCore PRIVATE SCOPEONE_CORE_VERSION_STRING="${PROJECT_VERSION}" ) -# The CUDA and ONNX Runtime processing modules are part of Core when their toolkits are found. -# Turning them off builds the plain CPU Core, which neither links nor ships CUDA or ONNX Runtime. +# The CUDA processing modules are part of Core when nvcc is found, and turning them off builds the +# standard Core. ONNX inference is always built, from the ONNX Runtime package that matches CUDA. set(_with_cuda OFF) set(_with_inference OFF) # Why a module that is turned on was left out, for the summary of the build scripts set(_cuda_reason "") set(_inference_reason "") option(SCOPEONE_ENABLE_CUDA "Build the CUDA processing modules into Core when nvcc is available" ON) -option(SCOPEONE_ENABLE_INFERENCE "Build the ONNX Runtime processing module into Core when ONNX Runtime is available" ON) if (SCOPEONE_ENABLE_CUDA) # An nvcc that was uninstalled since the last configure is looked up again if (SCOPEONE_CUDA_NVCC_EXECUTABLE AND NOT EXISTS "${SCOPEONE_CUDA_NVCC_EXECUTABLE}") @@ -317,92 +318,85 @@ endif () # ONNXRUNTIME_ROOT names another SDK instead. set(SCOPEONE_ONNXRUNTIME_VERSION 1.30.0) set(ONNXRUNTIME_ROOT "" CACHE PATH "ONNX Runtime SDK root; empty selects the official package in external") -# Earlier configures cached the Windows GPU package as the default root, which then counted as a -# root set by hand and kept every other platform and the CPU package from being selected -if (ONNXRUNTIME_ROOT STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/external/onnxruntime-win-x64-gpu_cuda12-1.30.0") - set(ONNXRUNTIME_ROOT "" CACHE PATH "ONNX Runtime SDK root; empty selects the official package in external" FORCE) -endif () -if (SCOPEONE_ENABLE_INFERENCE) - set(_ort_package "") - if (WIN32 AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64") - if (_with_cuda) - set(_ort_package "onnxruntime-win-x64-gpu_cuda12-${SCOPEONE_ONNXRUNTIME_VERSION}.zip") - set(_ort_sha256 d4667ea48eb0a10bc9b96b838f7b8975a6bf18de3bc5edd403a22e15c1458b23) - else () - set(_ort_package "onnxruntime-win-x64-${SCOPEONE_ONNXRUNTIME_VERSION}.zip") - set(_ort_sha256 c6ba983baf5681af108599675d2a89c2d145512d02de28aed0bff177cd0ba949) - endif () - elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") - if (_with_cuda) - set(_ort_package "onnxruntime-linux-x64-gpu_cuda12-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") - set(_ort_sha256 f9886932ee7bb0b4d3fcab736a392d4ff5efaa0672b47f19f0cec03437cf64f1) - else () - set(_ort_package "onnxruntime-linux-x64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") - set(_ort_sha256 a5ed5a3cac51fbb2e90da632ae43d19212faaa20e76484e62bcb7c23ddb3b3fd) - endif () - elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64") - set(_ort_package "onnxruntime-linux-aarch64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") - set(_ort_sha256 e16a27a8ed330bbc698df7330b0cf56e722f354e3bcc92118682c74ef3c3e3da) - elseif (APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") - # The macOS package has only the CPU provider - set(_ort_package "onnxruntime-osx-arm64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") - set(_ort_sha256 6ebb5062a934537c352937821f9fe9718e7de1a2db1122a93dd363ffd53a7012) +set(_ort_package "") +if (WIN32 AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64") + if (_with_cuda) + set(_ort_package "onnxruntime-win-x64-gpu_cuda12-${SCOPEONE_ONNXRUNTIME_VERSION}.zip") + set(_ort_sha256 d4667ea48eb0a10bc9b96b838f7b8975a6bf18de3bc5edd403a22e15c1458b23) + else () + set(_ort_package "onnxruntime-win-x64-${SCOPEONE_ONNXRUNTIME_VERSION}.zip") + set(_ort_sha256 c6ba983baf5681af108599675d2a89c2d145512d02de28aed0bff177cd0ba949) endif () +elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") + if (_with_cuda) + set(_ort_package "onnxruntime-linux-x64-gpu_cuda12-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") + set(_ort_sha256 f9886932ee7bb0b4d3fcab736a392d4ff5efaa0672b47f19f0cec03437cf64f1) + else () + set(_ort_package "onnxruntime-linux-x64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") + set(_ort_sha256 a5ed5a3cac51fbb2e90da632ae43d19212faaa20e76484e62bcb7c23ddb3b3fd) + endif () +elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64") + set(_ort_package "onnxruntime-linux-aarch64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") + set(_ort_sha256 e16a27a8ed330bbc698df7330b0cf56e722f354e3bcc92118682c74ef3c3e3da) +elseif (APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") + # The macOS package has only the CPU provider + set(_ort_package "onnxruntime-osx-arm64-${SCOPEONE_ONNXRUNTIME_VERSION}.tgz") + set(_ort_sha256 6ebb5062a934537c352937821f9fe9718e7de1a2db1122a93dd363ffd53a7012) +endif () - set(_ort_download_error "") - if (ONNXRUNTIME_ROOT) - set(_ort_root "${ONNXRUNTIME_ROOT}") - elseif (_ort_package) - string(REGEX REPLACE "\\.(zip|tgz)$" "" _ort_name "${_ort_package}") - set(_ort_root "${CMAKE_CURRENT_SOURCE_DIR}/external/${_ort_name}") - set(_ort_url "https://github.com/microsoft/onnxruntime/releases/download/v${SCOPEONE_ONNXRUNTIME_VERSION}/${_ort_package}") - if (NOT EXISTS "${_ort_root}/include") - message(STATUS "Downloading ${_ort_url}") - set(_ort_archive "${CMAKE_CURRENT_BINARY_DIR}/${_ort_package}") - # EXPECTED_HASH turned a failed download into a configure error, so the hash is - # checked after it. Without the network Core is built without ONNX inference, and the - # next configure tries again. - file(DOWNLOAD "${_ort_url}" "${_ort_archive}" - SHOW_PROGRESS - STATUS _ort_download - ) - list(GET _ort_download 0 _ort_download_code) - list(GET _ort_download 1 _ort_download_error) - if (_ort_download_code EQUAL 0) - file(SHA256 "${_ort_archive}" _ort_actual_sha256) - if (_ort_actual_sha256 STREQUAL _ort_sha256) - set(_ort_download_error "") - file(ARCHIVE_EXTRACT INPUT "${_ort_archive}" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/external") - else () - set(_ort_download_error "SHA256 mismatch") - endif () +set(_ort_download_error "") +if (ONNXRUNTIME_ROOT) + set(_ort_root "${ONNXRUNTIME_ROOT}") +elseif (_ort_package) + string(REGEX REPLACE "\\.(zip|tgz)$" "" _ort_name "${_ort_package}") + set(_ort_root "${CMAKE_CURRENT_SOURCE_DIR}/external/${_ort_name}") + set(_ort_url "https://github.com/microsoft/onnxruntime/releases/download/v${SCOPEONE_ONNXRUNTIME_VERSION}/${_ort_package}") + if (NOT EXISTS "${_ort_root}/include") + message(STATUS "Downloading ${_ort_url}") + set(_ort_archive "${CMAKE_CURRENT_BINARY_DIR}/${_ort_package}") + # EXPECTED_HASH turned a failed download into a configure error, so the hash is + # checked after it. Without the network Core is built without ONNX inference, and the + # next configure tries again. + file(DOWNLOAD "${_ort_url}" "${_ort_archive}" + SHOW_PROGRESS + STATUS _ort_download + ) + list(GET _ort_download 0 _ort_download_code) + list(GET _ort_download 1 _ort_download_error) + if (_ort_download_code EQUAL 0) + file(SHA256 "${_ort_archive}" _ort_actual_sha256) + if (_ort_actual_sha256 STREQUAL _ort_sha256) + set(_ort_download_error "") + file(ARCHIVE_EXTRACT INPUT "${_ort_archive}" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/external") + else () + set(_ort_download_error "SHA256 mismatch") endif () - file(REMOVE "${_ort_archive}") endif () - else () - set(_ort_root "") + file(REMOVE "${_ort_archive}") endif () +else () + set(_ort_root "") +endif () - # The found paths are cached, so a root that changed, such as the GPU package taking over - # once CUDA is found, kept linking the package of the earlier configure - if (NOT "${SCOPEONE_ONNXRUNTIME_ROOT_USED}" STREQUAL "${_ort_root}") - unset(ONNXRUNTIME_INCLUDE_DIR CACHE) - unset(ONNXRUNTIME_LIBRARY CACHE) - endif () - set(SCOPEONE_ONNXRUNTIME_ROOT_USED "${_ort_root}" CACHE INTERNAL "") - set(ONNXRUNTIME_ROOT "${_ort_root}") - find_path(ONNXRUNTIME_INCLUDE_DIR - NAMES onnxruntime_cxx_api.h - PATHS "${ONNXRUNTIME_ROOT}/include" - NO_DEFAULT_PATH - ) - find_library(ONNXRUNTIME_LIBRARY - NAMES onnxruntime - PATHS "${ONNXRUNTIME_ROOT}/lib" - NO_DEFAULT_PATH - ) +# The found paths are cached, so a root that changed, such as the GPU package taking over +# once CUDA is found, kept linking the package of the earlier configure +if (NOT "${SCOPEONE_ONNXRUNTIME_ROOT_USED}" STREQUAL "${_ort_root}") + unset(ONNXRUNTIME_INCLUDE_DIR CACHE) + unset(ONNXRUNTIME_LIBRARY CACHE) endif () -if (SCOPEONE_ENABLE_INFERENCE AND ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRARY) +set(SCOPEONE_ONNXRUNTIME_ROOT_USED "${_ort_root}" CACHE INTERNAL "") +set(ONNXRUNTIME_ROOT "${_ort_root}") +find_path(ONNXRUNTIME_INCLUDE_DIR + NAMES onnxruntime_cxx_api.h + PATHS "${ONNXRUNTIME_ROOT}/include" + NO_DEFAULT_PATH +) +find_library(ONNXRUNTIME_LIBRARY + NAMES onnxruntime + PATHS "${ONNXRUNTIME_ROOT}/lib" + NO_DEFAULT_PATH +) +if (ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRARY) target_sources(ScopeOneCore PRIVATE src/inference/InferenceProcessingRegistration.cpp src/inference/OnnxInferenceModule.cpp @@ -438,17 +432,15 @@ if (SCOPEONE_ENABLE_INFERENCE AND ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRAR ) install(FILES ${_onnxruntime_libs} DESTINATION "${CMAKE_INSTALL_LIBDIR}") endif () -elseif (SCOPEONE_ENABLE_INFERENCE AND NOT ONNXRUNTIME_ROOT) +elseif (NOT ONNXRUNTIME_ROOT) set(_inference_reason "no official ONNX Runtime package for ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_PROCESSOR}; set ONNXRUNTIME_ROOT") message(STATUS "${_inference_reason}; building Core without ONNX inference") -elseif (SCOPEONE_ENABLE_INFERENCE AND _ort_download_error) +elseif (_ort_download_error) set(_inference_reason "downloading ${_ort_url} failed (${_ort_download_error}); extract it into ${CMAKE_CURRENT_SOURCE_DIR}/external") message(STATUS "${_inference_reason}; building Core without ONNX inference") -elseif (SCOPEONE_ENABLE_INFERENCE) +else () set(_inference_reason "ONNX Runtime was not found under ${ONNXRUNTIME_ROOT}") message(STATUS "${_inference_reason}; building Core without ONNX inference") -else () - set(_inference_reason "turned off with SCOPEONE_ENABLE_INFERENCE") endif () # What this configure built in, for the build scripts to report set(SCOPEONE_BUILT_WITH_CUDA "${_with_cuda}" CACHE INTERNAL "") @@ -543,33 +535,21 @@ target_link_directories(ScopeOne_DriverHost PRIVATE ${MMCORE_BIN_DIR}) scopeone_add_runtime_copy_commands(ScopeOne_DriverHost $) -# The Visual Studio generator compiles the sources of one project one after another unless the -# compiler runs them in parallel itself. Only for C++, nvcc does not take /MP. +# /MP compiles the sources of one project in parallel, which the Visual Studio generator does not, +# and Release builds use LTO. Only for C++, nvcc takes neither. if (MSVC) - target_compile_options(ScopeOneCore PRIVATE $<$:/MP>) - target_compile_options(ScopeOne_DriverHost PRIVATE /MP) + set(_cxx "$") + set(_release_cxx "$,${_cxx}>") + foreach (_target IN ITEMS ScopeOneCore ScopeOne_DriverHost) + target_compile_options(${_target} PRIVATE + "$<${_cxx}:/MP>" + "$<${_release_cxx}:/GL>" "$<${_release_cxx}:/fp:fast>" "$<${_release_cxx}:/arch:AVX2>" + ) + target_link_options(${_target} PRIVATE $<$:/LTCG>) + endforeach () target_compile_options(ScopeOneDriverSDK PRIVATE /MP) endif () -# LTO Release build -if (MSVC) - # Only for C++, nvcc does not take these flags - set(_release_cxx "$,$>") - target_compile_options(ScopeOneCore PRIVATE - "$<${_release_cxx}:/GL>" "$<${_release_cxx}:/fp:fast>" "$<${_release_cxx}:/arch:AVX2>" - ) - target_link_options(ScopeOneCore PRIVATE - $<$:/LTCG> - ) - - target_compile_options(ScopeOne_DriverHost PRIVATE - $<$:/GL /fp:fast /arch:AVX2> - ) - target_link_options(ScopeOne_DriverHost PRIVATE - $<$:/LTCG> - ) -endif () - configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/ScopeOneCoreConfig.cmake.in" diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 30532b3..28b0d98 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -30,7 +30,7 @@ ScopeOneCore/ `internal` contains implementation contracts between Core managers, processing modules and DriverHost. These headers are available to the `ScopeOneCore` target through a private include path, are not installed, and may change without preserving source compatibility. Code outside `ScopeOneCore` must not include them. -`src` contains implementations. A public class such as `ScopeOneCore`, `ImageSceneModel` or `ExperimentDocument` still has its `.cpp` file in `src`; being public is determined by its header location and exported API, not by the location of its implementation. +`src` contains implementations. A public class such as `ScopeOneCore`, `ImageSceneModel` or `ExperimentDocument` has its `.cpp` file in `src` too. Use this placement rule: @@ -121,7 +121,7 @@ Providers use ScopeOne logical device IDs and publish `ImageFrame` objects throu - `drivers` contains the drivers, among them NI-DAQmx and the example camera and PMT. Each provider runs in an isolated DriverHost process, so a vendor library such as NI-DAQmx is never loaded into ScopeOne. Micro-Manager Device Adapters remain under Micro-Manager and are not wrapped as ScopeOne drivers. - Python processing modules are scripts in `processing/python`, beside the executable or in the application data directory. Core runs each module of a pipeline in its own Python process. -- The CUDA and ONNX processing modules are part of Core. CUDA is built when nvcc is found and `SCOPEONE_ENABLE_CUDA` is on. ONNX inference is built when `SCOPEONE_ENABLE_INFERENCE` is on, with the official ONNX Runtime package that the configure downloads into `external`, the GPU package when CUDA is built and the CPU package otherwise. `ONNXRUNTIME_ROOT` points Core at another SDK, and with both switches off Core neither links nor ships CUDA or ONNX Runtime. Every processing method, the PTU file signal source and the DHM reconstruction are part of Core. +- The CUDA and ONNX processing modules are part of Core. CUDA is built when nvcc is found and `SCOPEONE_ENABLE_CUDA` is on. ONNX inference is always built, with the official ONNX Runtime package that the configure downloads into `external`, the GPU package when CUDA is built and the CPU package otherwise. `ONNXRUNTIME_ROOT` points Core at another SDK. Every processing method, the PTU file signal source and the DHM reconstruction are part of Core. - The tool windows (Scale, Stage Mosaic, Particle Detection, DHM Reconstruction, Scanning and DAQ Control, Signal Monitor) are part of the application. They use the `ScopeOneCore` facade, and `ScopeOneToolContext` in `src/tools/ToolContext.h` gives them the desktop operations such as showing layers. Drivers outside this repository consume the exported `scopeone::DriverSDK` CMake target. Every driver manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`, and incompatible manifests are rejected before the library instance is created. diff --git a/ScopeOneCore/cmake/ScopeOneCoreConfig.cmake.in b/ScopeOneCore/cmake/ScopeOneCoreConfig.cmake.in index 3b241be..68ac268 100644 --- a/ScopeOneCore/cmake/ScopeOneCoreConfig.cmake.in +++ b/ScopeOneCore/cmake/ScopeOneCoreConfig.cmake.in @@ -2,7 +2,10 @@ include(CMakeFindDependencyMacro) -find_dependency(Qt6 REQUIRED COMPONENTS Core Gui) +# Whether this Core has the CUDA modules, which makes the GUI package the GPU package +set(ScopeOneCore_WITH_CUDA @SCOPEONE_BUILT_WITH_CUDA@) + +find_dependency(Qt6 @SCOPEONE_QT_MINIMUM_VERSION@ REQUIRED COMPONENTS Core Gui) include("${CMAKE_CURRENT_LIST_DIR}/ScopeOneCoreTargets.cmake") include("${CMAKE_CURRENT_LIST_DIR}/ScopeOneDriver.cmake") diff --git a/ScopeOneCore/cmake/ScopeOneQt.cmake b/ScopeOneCore/cmake/ScopeOneQt.cmake new file mode 100644 index 0000000..1a53f17 --- /dev/null +++ b/ScopeOneCore/cmake/ScopeOneQt.cmake @@ -0,0 +1,24 @@ +# Selects the Qt of a ScopeOne project. Included before project(), because vcpkg installs the +# manifest there. SCOPEONE_QT_DIR names an installed Qt 6.8 or later; empty builds Qt with vcpkg. +set(SCOPEONE_QT_DIR "" CACHE PATH "Installed Qt 6.8 or later, such as C:/Qt/6.11.2/msvc2022_64; empty builds Qt with vcpkg") + +if (SCOPEONE_QT_DIR) + # The qt feature of vcpkg.json is the only default feature, so vcpkg leaves Qt out and CMake + # can only find the installed one + set(VCPKG_MANIFEST_NO_DEFAULT_FEATURES ON) + list(PREPEND CMAKE_PREFIX_PATH "${SCOPEONE_QT_DIR}") +endif () + +# The found Qt is cached, so a project switched between vcpkg's Qt and an installed one kept the +# Qt of its earlier configure +if (NOT "${SCOPEONE_QT_DIR}" STREQUAL "${SCOPEONE_QT_DIR_USED}") + get_cmake_property(_scopeone_cache_variables CACHE_VARIABLES) + foreach (_scopeone_variable IN LISTS _scopeone_cache_variables) + if (_scopeone_variable MATCHES "^Qt6.*_DIR$") + unset(${_scopeone_variable} CACHE) + endif () + endforeach () +endif () +set(SCOPEONE_QT_DIR_USED "${SCOPEONE_QT_DIR}" CACHE INTERNAL "") + +set(SCOPEONE_QT_MINIMUM_VERSION 6.8) diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 7ba4b30..3347856 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -1,4 +1,5 @@ cmake_minimum_required(VERSION 3.23) +include("${CMAKE_CURRENT_LIST_DIR}/../ScopeOneCore/cmake/ScopeOneQt.cmake") project(ScopeOneDrivers LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) @@ -17,7 +18,7 @@ endif () # A driver has the driver SDK and Qt Core and nothing else, so one that reaches for widgets, # OpenCV or Core itself does not build -find_package(Qt6 REQUIRED COMPONENTS Core) +find_package(Qt6 ${SCOPEONE_QT_MINIMUM_VERSION} REQUIRED COMPONENTS Core) find_package(ScopeOneCore CONFIG REQUIRED) add_subdirectory(ExampleCamera) diff --git a/drivers/README.md b/drivers/README.md index 0f85574..b430cd6 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -28,9 +28,7 @@ so a driver process holds only the host, the SDK library, Qt Core and Network, a The `scopeone::DriverSDK` target gives a driver the SDK headers (`scopeone/DriverSDK.h` includes them all) and the small SDK library, and nothing else of Core, so a driver that includes or calls -any other part of Core fails to build. The SDK types are in `scopeone::core` because Core uses the -same types; which of them a driver sees is decided by these headers and this target, not by the -namespace. +any other part of Core fails to build. Build and install ScopeOneCore first (`scripts/build.sh` or `scripts\build.ps1`). A driver is then a separate CMake project: @@ -49,8 +47,8 @@ target_link_libraries(MyCamera PRIVATE scopeone::DriverSDK Qt6::Core) scopeone_add_driver(MyCamera) ``` -Point `CMAKE_PREFIX_PATH` at `ScopeOneCore/install` and at the same Qt that ScopeOne uses -(`vcpkg_installed/`). +Point `CMAKE_PREFIX_PATH` at `ScopeOneCore/install` and at the same Qt that ScopeOne uses, +`vcpkg_installed/` or the Qt passed to the build scripts with `--qt`. The drivers in this folder are built the same way, as a project of their own that finds only Qt Core and the installed ScopeOneCore. The build scripts build it into `drivers/build` and install diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 70abe64..8cc942b 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -2,15 +2,12 @@ $ErrorActionPreference = "Stop" $target = "all" $coreConfigureOption = @() -$guiConfigureOption = @() -$coreBuildOption = @() -$guiBuildOption = @() $configure = $false $clean = $false $run = $false $package = $false -$skipCoreInstall = $false $assumeYes = $false +$qtArgument = $null function Read-OptionValue { param( @@ -39,18 +36,6 @@ for ($i = 0; $i -lt $args.Count; $i++) { $coreConfigureOption += Read-OptionValue -Arguments $args -Index ([ref]$i) -OptionName $arg continue } - { $_ -in @("--guiConfigureOption", "-guiConfigureOption") } { - $guiConfigureOption += Read-OptionValue -Arguments $args -Index ([ref]$i) -OptionName $arg - continue - } - { $_ -in @("--coreBuildOption", "-coreBuildOption") } { - $coreBuildOption += Read-OptionValue -Arguments $args -Index ([ref]$i) -OptionName $arg - continue - } - { $_ -in @("--guiBuildOption", "-guiBuildOption") } { - $guiBuildOption += Read-OptionValue -Arguments $args -Index ([ref]$i) -OptionName $arg - continue - } { $_ -in @("--configure", "-configure") } { $configure = $true continue @@ -67,29 +52,26 @@ for ($i = 0; $i -lt $args.Count; $i++) { $package = $true continue } - { $_ -in @("--skipCoreInstall", "-skipCoreInstall") } { - $skipCoreInstall = $true - continue - } { $_ -in @("--yes", "-yes", "-y") } { $assumeYes = $true continue } + { $_ -in @("--qt", "-qt") } { + $qtArgument = Read-OptionValue -Arguments $args -Index ([ref]$i) -OptionName $arg + continue + } default { throw "Unknown option: $arg" } } } -if ($target -notin @("all", "core", "gui", "drivers", "scopewriter")) { - throw "Invalid target '$target'. Expected one of: all, core, gui, drivers, scopewriter." +if ($target -notin @("all", "core", "gui", "drivers")) { + throw "Invalid target '$target'. Expected one of: all, core, gui, drivers." } -if ($package -and $target -in @("core", "drivers", "scopewriter")) { +if ($package -and $target -in @("core", "drivers")) { throw "--package requires --target gui or --target all." } -if ($run -and $target -eq "scopewriter") { - throw "--run is not available for the scopewriter target." -} function Write-Step { param([string]$Message) @@ -404,12 +386,6 @@ $guiBuildDir = Join-Path $repoRoot "build" $coreSourceDir = Join-Path $repoRoot "ScopeOneCore" $coreBuildDir = Join-Path $coreSourceDir "build" $coreInstallDir = Join-Path $coreSourceDir "install" -$writerSourceDir = Join-Path $coreSourceDir "external\ScopeWriter" -$writerBuildRoot = Join-Path $writerSourceDir "build" -$writerBuildDir = Join-Path $writerBuildRoot "standalone" -$writerInstallDir = Join-Path $writerSourceDir "install" -$writerConsumerSourceDir = Join-Path $writerSourceDir "tests\consumer" -$writerConsumerBuildDir = Join-Path $writerBuildRoot "consumer" $driverInstallDir = Join-Path $coreInstallDir "bin" # Inside drivers rather than build, whose drivers folder is where the GUI build puts the drivers it runs $driverSourceDir = Join-Path $repoRoot "drivers" @@ -420,42 +396,45 @@ $guiCachePath = Join-Path $guiBuildDir "CMakeCache.txt" $driverCachePath = Join-Path $driverBuildDir "CMakeCache.txt" $vcpkgConfigureArgs = @() -$writerVcpkgArgs = @() if ($env:OS -eq "Windows_NT") { $vcpkgToolchain = Join-Path (Resolve-Vcpkg) "scripts\buildsystems\vcpkg.cmake" -} -if ($env:OS -eq "Windows_NT" -and $target -eq "scopewriter") { - # Standalone ScopeWriter installs its own vcpkg.json dependencies and ships their DLLs - $writerVcpkgArgs = @( - "-DCMAKE_TOOLCHAIN_FILE=$vcpkgToolchain", - "-DVCPKG_TARGET_TRIPLET=x64-windows", - "-DX_VCPKG_APPLOCAL_DEPS_INSTALL=ON" - ) -} -elseif ($env:OS -eq "Windows_NT") { $vcpkgConfigureArgs = @( "-DCMAKE_TOOLCHAIN_FILE=$vcpkgToolchain", "-DVCPKG_MANIFEST_DIR=$repoRoot", "-DVCPKG_INSTALLED_DIR=$(Join-Path $repoRoot 'vcpkg_installed')", - "-DVCPKG_TARGET_TRIPLET=x64-windows", - "-UTiff_DIR", - "-UOpenCV_DIR", - "-UQt6*_DIR", - "-UWINDEPLOYQT_EXECUTABLE" + "-DVCPKG_TARGET_TRIPLET=x64-windows" ) } -if ($clean) { - if ($target -eq "scopewriter") { - if (Test-Path $writerBuildRoot) { - Write-Step "Removing ScopeWriter build directory" - Remove-Item -LiteralPath $writerBuildRoot -Recurse -Force - } - if (Test-Path $writerInstallDir) { - Write-Step "Removing ScopeWriter install directory" - Remove-Item -LiteralPath $writerInstallDir -Recurse -Force - } +# An installed Qt comes from --qt or SCOPEONE_QT_DIR, and "vcpkg" selects the Qt that vcpkg builds. +# --qt is saved for the user account, so later builds keep the Qt without it. +$qtDir = if ($null -ne $qtArgument) { $qtArgument } elseif ($env:SCOPEONE_QT_DIR) { $env:SCOPEONE_QT_DIR } else { "" } +if ($qtDir -eq "vcpkg") { + $qtDir = "" +} +if ($qtDir) { + $qtDir = [System.IO.Path]::GetFullPath($qtDir).TrimEnd("\", "/") + if (-not (Test-Path (Join-Path $qtDir "lib\cmake\Qt6"))) { + throw "No Qt 6 in $qtDir (lib\cmake\Qt6 is missing). Pass the MSVC 64-bit Qt directory, such as C:\Qt\6.11.2\msvc2022_64." } +} +if ($null -ne $qtArgument) { + $savedQtDir = if ($qtDir) { $qtDir } else { $null } + $env:SCOPEONE_QT_DIR = $savedQtDir + [Environment]::SetEnvironmentVariable("SCOPEONE_QT_DIR", $savedQtDir, "User") + if ($qtDir) { + Write-Host "Using the Qt in $qtDir, saved as SCOPEONE_QT_DIR for your user account." + } + else { + Write-Host "Using the Qt that vcpkg builds, and SCOPEONE_QT_DIR is removed." + } +} +# Every project takes the Qt from SCOPEONE_QT_DIR, which leaves Qt out of vcpkg when it is set +if ($env:OS -eq "Windows_NT") { + $vcpkgConfigureArgs += "-DSCOPEONE_QT_DIR=$qtDir" +} + +if ($clean) { if ($target -in @("all", "gui") -and (Test-Path $guiBuildDir)) { Write-Step "Removing GUI build directory" Remove-Item -LiteralPath $guiBuildDir -Recurse -Force @@ -479,10 +458,9 @@ if ($clean) { Ensure-Directory $repoRoot $coreConfigureOptionOverride = $coreConfigureOption.Count -gt 0 -$guiConfigureOptionOverride = $guiConfigureOption.Count -gt 0 $needCoreConfigure = $configure -or $coreConfigureOptionOverride -or -not (Test-Path $coreCachePath) -$needGuiConfigure = $configure -or $guiConfigureOptionOverride -or -not (Test-Path $guiCachePath) +$needGuiConfigure = $configure -or -not (Test-Path $guiCachePath) $driverBuildFilesExist = (Test-Path (Join-Path $driverBuildDir "ALL_BUILD.vcxproj")) -or (Test-Path (Join-Path $driverBuildDir "build.ninja")) -or (Test-Path (Join-Path $driverBuildDir "Makefile")) @@ -496,81 +474,19 @@ if ($driverCachedInstallPrefix -and $installPrefixOverride = Find-ConfigureOverride -Options $coreConfigureOption -Prefix "-DCMAKE_INSTALL_PREFIX=" -if ($target -eq "scopewriter") { - $writerGeneratorArgs = @() - if ($env:OS -eq "Windows_NT") { - $writerGeneratorArgs = @("-G", "Visual Studio 17 2022", "-A", "x64") - } - Invoke-Step ` - -Label "Configuring standalone ScopeWriter" ` - -FilePath $cmake ` - -Arguments (@( - "-S", $writerSourceDir, - "-B", $writerBuildDir, - "-DCMAKE_INSTALL_PREFIX=$writerInstallDir", - "-DSCOPEWRITER_BUILD_TESTS=ON" - ) + $writerVcpkgArgs + $writerGeneratorArgs) ` - -WorkingDirectory $repoRoot - - Invoke-Step ` - -Label "Building standalone ScopeWriter ($config)" ` - -FilePath $cmake ` - -Arguments @( - "--build", $writerBuildDir, - "--config", $config, - "--parallel" - ) ` - -WorkingDirectory $repoRoot - - $ctest = (Get-Command ctest -ErrorAction Stop).Source - Invoke-Step ` - -Label "Testing standalone ScopeWriter ($config)" ` - -FilePath $ctest ` - -Arguments @( - "--test-dir", $writerBuildDir, - "-C", $config, - "--output-on-failure" - ) ` - -WorkingDirectory $repoRoot - - Invoke-Step ` - -Label "Installing standalone ScopeWriter" ` - -FilePath $cmake ` - -Arguments @( - "--install", $writerBuildDir, - "--config", $config - ) ` - -WorkingDirectory $repoRoot - - Invoke-Step ` - -Label "Configuring installed ScopeWriter consumer" ` - -FilePath $cmake ` - -Arguments (@( - "-S", $writerConsumerSourceDir, - "-B", $writerConsumerBuildDir, - "-DCMAKE_PREFIX_PATH=$writerInstallDir" - ) + $writerGeneratorArgs) ` - -WorkingDirectory $repoRoot - - Invoke-Step ` - -Label "Building installed ScopeWriter consumer ($config)" ` - -FilePath $cmake ` - -Arguments @( - "--build", $writerConsumerBuildDir, - "--config", $config, - "--parallel" - ) ` - -WorkingDirectory $repoRoot - - Invoke-Step ` - -Label "Testing installed ScopeWriter consumer ($config)" ` - -FilePath $ctest ` - -Arguments @( - "--test-dir", $writerConsumerBuildDir, - "-C", $config, - "--output-on-failure" - ) ` - -WorkingDirectory $repoRoot +# A build directory configured with the other Qt is configured again, or one part would keep it +$qtDirNormalized = [string](Normalize-CMakePath $qtDir) +if ((Test-Path $coreCachePath) -and + [string](Normalize-CMakePath (Get-CMakeCacheValue -CachePath $coreCachePath -Key "SCOPEONE_QT_DIR")) -ne $qtDirNormalized) { + $needCoreConfigure = $true +} +if ((Test-Path $driverCachePath) -and + [string](Normalize-CMakePath (Get-CMakeCacheValue -CachePath $driverCachePath -Key "SCOPEONE_QT_DIR")) -ne $qtDirNormalized) { + $needDriverConfigure = $true +} +if ((Test-Path $guiCachePath) -and + [string](Normalize-CMakePath (Get-CMakeCacheValue -CachePath $guiCachePath -Key "SCOPEONE_QT_DIR")) -ne $qtDirNormalized) { + $needGuiConfigure = $true } if (-not $needCoreConfigure -and $target -in @("all", "core")) { @@ -588,7 +504,12 @@ if ($target -in @("all", "core")) { } if (-not (Test-Path (Join-Path $repoRoot "vcpkg_installed\x64-windows"))) { Write-Host "" - Write-Host "The first build compiles Qt and OpenCV with vcpkg, which takes about an hour. Later builds reuse the result." -ForegroundColor Yellow + if ($qtDir) { + Write-Host "The first build compiles OpenCV with vcpkg, which takes 10 to 15 minutes. Later builds reuse the result." -ForegroundColor Yellow + } + else { + Write-Host "The first build compiles Qt and OpenCV with vcpkg, which takes about an hour. Pass --qt with an installed MSVC Qt to skip Qt. Later builds reuse the result." -ForegroundColor Yellow + } } # Core builds the CUDA processing modules, which need the CUDA toolset. A generator keeps the @@ -628,7 +549,7 @@ if ($target -in @("all", "core")) { "--build", $coreBuildDir, "--config", $config, "--parallel" - ) + $coreBuildOption + ) Invoke-Step ` -Label "Building ScopeOneCore ($config)" ` @@ -636,16 +557,14 @@ if ($target -in @("all", "core")) { -Arguments $coreBuildArgs ` -WorkingDirectory $repoRoot - if (-not $skipCoreInstall) { - Invoke-Step ` - -Label "Installing ScopeOneCore into local prefix" ` - -FilePath $cmake ` - -Arguments @( - "--install", $coreBuildDir, - "--config", $config - ) ` - -WorkingDirectory $repoRoot - } + Invoke-Step ` + -Label "Installing ScopeOneCore into local prefix" ` + -FilePath $cmake ` + -Arguments @( + "--install", $coreBuildDir, + "--config", $config + ) ` + -WorkingDirectory $repoRoot } if ($target -in @("all", "drivers")) { @@ -717,7 +636,7 @@ if ($target -in @("all", "gui")) { "-S", $guiSourceDir, "-B", $guiBuildDir, "-DScopeOneCore_ROOT=$coreInstallDir" - ) + $vcpkgConfigureArgs + $guiConfigureOption + ) + $vcpkgConfigureArgs Invoke-Step ` -Label "Configuring ScopeOne GUI" ` @@ -730,7 +649,7 @@ if ($target -in @("all", "gui")) { "--build", $guiBuildDir, "--config", $config, "--parallel" - ) + $guiBuildOption + ) Invoke-Step ` -Label "Building ScopeOne GUI ($config)" ` @@ -760,42 +679,35 @@ $packageCandidates = Get-ChildItem -LiteralPath $guiBuildDir -File -ErrorAction Write-Step "Summary" Write-Host "Target: $target" Write-Host "Config: $config" -if ($target -eq "scopewriter") { - Write-Host "ScopeWriter install: $writerInstallDir" -} -else { - Write-Host "ScopeOneCore install: $coreInstallDir" - # Core builds the CUDA and ONNX modules only when it finds their toolkits, so a missing one - # leaves them out without failing the build - if (Test-Path $coreCachePath) { - foreach ($module in @(@{ Label = "CUDA"; Key = "CUDA" }, @{ Label = "ONNX"; Key = "INFERENCE" })) { - Write-Host -NoNewline "$($module.Label): " - $reason = Get-CMakeCacheValue -CachePath $coreCachePath -Key "SCOPEONE_$($module.Key)_SKIP_REASON" - if ((Get-CMakeCacheValue -CachePath $coreCachePath -Key "SCOPEONE_BUILT_WITH_$($module.Key)") -eq "ON") { - Write-Host "on" -ForegroundColor Green - } - elseif ($reason) { - Write-Host -NoNewline "off" -ForegroundColor Yellow - Write-Host " ($reason)" - } - else { - Write-Host "off" -ForegroundColor Yellow - } +Write-Host "ScopeOneCore install: $coreInstallDir" +# Core builds the CUDA and ONNX modules only when it finds their toolkits, so a missing one +# leaves them out without failing the build +if (Test-Path $coreCachePath) { + foreach ($module in @(@{ Label = "CUDA"; Key = "CUDA" }, @{ Label = "ONNX"; Key = "INFERENCE" })) { + Write-Host -NoNewline "$($module.Label): " + $reason = Get-CMakeCacheValue -CachePath $coreCachePath -Key "SCOPEONE_$($module.Key)_SKIP_REASON" + if ((Get-CMakeCacheValue -CachePath $coreCachePath -Key "SCOPEONE_BUILT_WITH_$($module.Key)") -eq "ON") { + Write-Host "on" -ForegroundColor Green + } + elseif ($reason) { + Write-Host -NoNewline "off" -ForegroundColor Yellow + Write-Host " ($reason)" + } + else { + Write-Host "off" -ForegroundColor Yellow } - } - if ($target -in @("all", "drivers")) { - Write-Host "Drivers: $driverBuildDir" } } -if ($target -ne "scopewriter") { - if (Test-Path $guiExe) { - Write-Host "GUI executable: $guiExe" - } - if ($packageCandidates) { - Write-Host "Packages:" - foreach ($candidate in $packageCandidates) { - Write-Host " $($candidate.FullName)" - } +if ($target -in @("all", "drivers")) { + Write-Host "Drivers: $driverBuildDir" +} +if (Test-Path $guiExe) { + Write-Host "GUI executable: $guiExe" +} +if ($packageCandidates) { + Write-Host "Packages:" + foreach ($candidate in $packageCandidates) { + Write-Host " $($candidate.FullName)" } } diff --git a/scripts/build.sh b/scripts/build.sh index a2f43ed..9698587 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -8,6 +8,7 @@ MM_REPO_DIR="$EXTERNAL_DIR/micro-manager" MMCORE_LINK="$EXTERNAL_DIR/mmCoreAndDevices" BUILD_TYPE="${BUILD_TYPE:-Release}" CLEAN=false +RUN=false ASSUME_YES=false # Colors are used only on a terminal, and NO_COLOR turns them off @@ -29,13 +30,29 @@ error() { printf '%s%s%s\n' "$RED" "$1" "$RESET" >&2 } -for argument in "$@"; do - case "$argument" in +# An installed Qt comes from --qt or SCOPEONE_QT_DIR, and "vcpkg" selects the Qt that vcpkg builds +QT_DIR="${SCOPEONE_QT_DIR:-}" +while (( $# > 0 )); do + case "$1" in --clean) CLEAN=true ;; + --run) RUN=true ;; --yes|-y) ASSUME_YES=true ;; - *) error "Unknown option: $argument"; exit 1 ;; + --qt) + if (( $# < 2 )); then + error "--qt needs the Qt directory, such as ~/Qt/6.11.2/gcc_64, or vcpkg" + exit 1 + fi + QT_DIR="$2" + shift + ;; + --qt=*) QT_DIR="${1#--qt=}" ;; + *) error "Unknown option: $1"; exit 1 ;; esac + shift done +if [[ "$QT_DIR" == vcpkg ]]; then + QT_DIR="" +fi case "$(uname -s)" in Linux*) @@ -171,6 +188,16 @@ VCPKG_ARGS=( "-DVCPKG_TARGET_TRIPLET=$VCPKG_TRIPLET" "-DVCPKG_HOST_TRIPLET=$VCPKG_TRIPLET" ) +if [[ -n "$QT_DIR" ]]; then + QT_DIR="$(cd "$QT_DIR" 2>/dev/null && pwd || echo "$QT_DIR")" + if [[ ! -d "$QT_DIR/lib/cmake/Qt6" ]]; then + error "No Qt 6 in $QT_DIR (lib/cmake/Qt6 is missing)." + error "Pass the directory that holds bin, lib and include, such as ~/Qt/6.11.2/gcc_64 or ~/Qt/6.11.2/macos." + exit 1 + fi +fi +# Every project takes the Qt from SCOPEONE_QT_DIR, which leaves Qt out of vcpkg when it is set +VCPKG_ARGS+=("-DSCOPEONE_QT_DIR=$QT_DIR") JOBS="${JOBS:-$DEFAULT_JOBS}" DEMO_ADAPTER="$MMCORE_LINK/DeviceAdapters/DemoCamera/.libs/$DEMO_ADAPTER_NAME" @@ -217,7 +244,9 @@ step "Building required Micro-Manager components" ) step "Building and installing ScopeOneCore" -if [[ ! -d "$ROOT_DIR/vcpkg_installed/$VCPKG_TRIPLET" ]]; then +if [[ -n "$QT_DIR" ]]; then + echo "Using the Qt in $QT_DIR. Export SCOPEONE_QT_DIR=$QT_DIR to keep it without --qt." +elif [[ ! -d "$ROOT_DIR/vcpkg_installed/$VCPKG_TRIPLET" ]]; then echo "The first build compiles Qt and OpenCV with vcpkg and can take a long time. Later builds reuse the result." fi run cmake -S "$CORE_DIR" -B "$CORE_DIR/build" -DCMAKE_BUILD_TYPE="$BUILD_TYPE" "${VCPKG_ARGS[@]}" @@ -266,3 +295,9 @@ echo "GUI executable: $ROOT_DIR/build/ScopeOne" echo "Demo configuration: $ROOT_DIR/config/MMConfig_demo.cfg" echo "CUDA: $(core_module CUDA)" echo "ONNX: $(core_module INFERENCE)" + +if [[ "$RUN" == true ]]; then + step "Starting ScopeOne" + cd "$ROOT_DIR/build" + exec ./ScopeOne +fi diff --git a/vcpkg.json b/vcpkg.json index 7f27d78..a136dcc 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -8,35 +8,43 @@ "default-features": false, "features": ["fs", "intrinsics", "jpeg", "png", "thread", "tiff", "webp"] }, - { - "name": "qtbase", - "default-features": false, - "features": ["concurrent", "dnslookup", "gui", "harfbuzz", "jpeg", "network", "opengl", "png", "thread", "widgets"] - }, - { - "name": "qtsvg", - "default-features": false - }, { "name": "tiff", "default-features": false, "features": ["zip"] }, "zlib", - "zstd", - { - "name": "qtbase", - "default-features": false, - "features": ["windeployqt"], - "platform": "windows" - }, - { - "name": "qtbase", - "default-features": false, - "features": ["egl", "fontconfig", "xcb", "xcb-xlib", "xrender"], - "platform": "linux" - } + "zstd" ], + "default-features": ["qt"], + "features": { + "qt": { + "description": "Qt built by vcpkg. Turning the default features off uses an installed Qt instead.", + "dependencies": [ + { + "name": "qtbase", + "default-features": false, + "features": ["concurrent", "dnslookup", "gui", "harfbuzz", "jpeg", "network", "opengl", "png", "thread", "widgets"] + }, + { + "name": "qtsvg", + "default-features": false + }, + { + "name": "qtbase", + "default-features": false, + "features": ["windeployqt"], + "platform": "windows" + }, + { + "name": "qtbase", + "default-features": false, + "features": ["egl", "fontconfig", "xcb", "xcb-xlib", "xrender"], + "platform": "linux" + } + ] + } + }, "overrides": [ { "name": "opencv4", From b29d73102e05a616b9801a75b436bd75bcd3a6ca Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 13:53:07 +0100 Subject: [PATCH 23/49] Fetch the ScopeWriter submodule in build.ps1 and add measured build times to the README --- README.md | 52 ++++++++++++++++++++++++++++++++++------------- scripts/build.ps1 | 9 +++++++- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index df60879..30566f2 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ There is an example dual-camera .cfg file in the config folder, just change the ## 🛠️ For Developers -**TL;DR.** On a fresh machine, without spending time on setting up the C++ environment, run these commands. They install the tools, clone ScopeOne, and let the script build everything else, and start ScopeOne. The first build takes about an hour on four cores. +**TL;DR.** On a fresh machine, without spending time on setting up the C++ environment, run these commands. They install the tools, clone ScopeOne, and let the script build everything else, and start ScopeOne. The first build takes long, see [Build times](#build-times). Windows: @@ -107,7 +107,7 @@ ScopeOne needs Qt 6.8 or later. There are three ways to get it. - **Install Qt yourself** with the [Qt Online Installer](https://www.qt.io/download-qt-installer-oss), then pass its directory as above. On Windows choose the MSVC 2022 64-bit build; MinGW does not work. -- **Let the script build it.** Without `--qt`, vcpkg builds Qt on the first build. Qt is about four fifths of that build, and vcpkg keeps the result, so it is built once. +- **Let the script build it.** Without `--qt`, vcpkg builds Qt on the first build. Qt is the largest part of that build, and vcpkg keeps the result, so it is built once (see [Build times](#build-times)). ### Windows, step by step @@ -136,7 +136,7 @@ ScopeOne needs Qt 6.8 or later. There are three ways to get it. The script finds Visual Studio itself. It offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It then builds MMCore with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI, and downloads ONNX Runtime into `ScopeOneCore\external`, the GPU package when the CUDA Toolkit is installed. - Without an installed Qt, the first build compiles Qt, OpenCV and their dependencies with vcpkg, which takes about an hour on a four core machine, four fifths of it for Qt. vcpkg keeps the result, so later builds only compile ScopeOne. The last lines of the build show whether Core was built with the CUDA and ONNX modules. + The first build takes long, see [Build times](#build-times). The last lines of the build show whether Core was built with the CUDA and ONNX modules. 4. **Try it without hardware.** Open `config\MMConfig_demo.cfg` in ScopeOne. The application is `build\Release\ScopeOne.exe`. @@ -207,17 +207,7 @@ macOS support is experimental. The script checks the build tools, offers to download vcpkg into `$HOME/vcpkg` or a folder you type (`--yes` takes the default), and builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. It downloads ONNX Runtime into `ScopeOneCore/external`. - Without an installed Qt, the first build compiles Qt, OpenCV and their dependencies with vcpkg, about 160 CPU minutes, which is 11 minutes on a 48 core Linux machine and about 40 on four cores. Later builds reuse them from the binary cache. Measured without the cache on the 48 core machine: - - | Package | Time | - |---|---| - | qtbase | 5.2 min | - | opencv4 | 2.2 min | - | qtsvg | 59 s | - | harfbuzz | 46 s | - | gperf, libb2, zstd, pcre2 | 10 to 22 s each | - | 29 others | under 10 s each | - | All 37 packages | 11 min 23 s | + The first build takes long, see [Build times](#build-times). 4. **Try it without hardware.** The application is `build/ScopeOne`. Run it from the build tree and open `config/MMConfig_demo.cfg`. @@ -227,6 +217,40 @@ macOS support is experimental. ScopeOne has two public extension points, drivers and Python processing modules, and both run outside the ScopeOne process. Drivers are loaded from `drivers` by `ScopeOne_DriverHost`, one process per device, and Python modules from `processing/python`. The CUDA and ONNX processing modules are part of Core. Core builds the CUDA modules when it finds nvcc, and ONNX inference always, with the GPU package of ONNX Runtime when CUDA is built and the CPU package otherwise. `-DSCOPEONE_ENABLE_CUDA=OFF` builds the standard Core. Micro-Manager remains the built-in camera provider and loads its device adapters from `.cfg` files. [drivers/README.md](drivers/README.md) explains how to write a driver and [ScopeOneCore/python/scopeone/README.md](ScopeOneCore/python/scopeone/README.md#python-processing-modules) how to write a Python processing module. Python 3.14 is needed only for the Python package and Python processing modules. +### Build times + +The first build is the long one. vcpkg compiles Qt, OpenCV and their dependencies from source, about 160 CPU minutes, 11 minutes on a 48 core Linux machine, and Qt is the largest part of it. An installed Qt (see [Qt](#qt)) leaves most of it out. vcpkg keeps the compiled packages in its binary cache, so this happens once per machine, and builds after a change take seconds. + +The first build without the cache, per vcpkg package, on the 48 core Linux machine: + +| Package | Time | +|---|---| +| qtbase | 5.2 min | +| opencv4 | 2.2 min | +| qtsvg | 59 s | +| harfbuzz | 46 s | +| gperf, libb2, zstd, pcre2 | 10 to 22 s each | +| 29 others | under 10 s each | +| All 37 packages | 11 min 23 s | + +A fresh clone with the binary cache filled, on the same machine, about 5 minutes in all: + +| Step | Time | +|---|---| +| Clone Micro-Manager | 71 s | +| Configure Core: restore the 37 packages from the cache and download ONNX Runtime | 2.6 min | +| Build Core | 28 s | +| Configure and build the drivers | 11 s | +| Configure and build the GUI | 27 s | + +Builds after that: + +| Change | Time | +|---|---| +| Nothing changed | under 1 s | +| One GUI file | 8 s | +| One Core file | 5 s | + ## 🤖 Automation and AI Agents The desktop app exposes a language-neutral local control API and shared-memory frame channel. A tool adapter can discover supported operation groups with the `capabilities` request, read a structured observation with `state_snapshot`, and invoke the exposed camera, stage, mosaic, processing, image analysis, experiment, recording, layer, and markup operations. Requests may carry an ID that is echoed by the app for correlation. Recording save operations use `ome-tiff` by default and also accept `ome-zarr`, `tiff` and `binary`. diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 8cc942b..a691d46 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -499,6 +499,13 @@ if (-not $needCoreConfigure -and $target -in @("all", "core")) { } if ($target -in @("all", "core")) { + # A clone without --recurse-submodules has an empty ScopeWriter folder and Core failed to + # configure. Only an empty checkout is filled, so commits made inside ScopeWriter are kept. + if (-not (Test-Path (Join-Path $coreSourceDir "external\ScopeWriter\CMakeLists.txt"))) { + Invoke-Step -Label "Fetching the ScopeWriter submodule" -FilePath $git ` + -Arguments @("submodule", "update", "--init", "--recursive") ` + -WorkingDirectory $repoRoot + } if ($env:OS -eq "Windows_NT") { Ensure-MMCore } @@ -508,7 +515,7 @@ if ($target -in @("all", "core")) { Write-Host "The first build compiles OpenCV with vcpkg, which takes 10 to 15 minutes. Later builds reuse the result." -ForegroundColor Yellow } else { - Write-Host "The first build compiles Qt and OpenCV with vcpkg, which takes about an hour. Pass --qt with an installed MSVC Qt to skip Qt. Later builds reuse the result." -ForegroundColor Yellow + Write-Host "The first build compiles Qt and OpenCV with vcpkg, which takes a long time. Pass --qt with an installed MSVC Qt to skip Qt. Later builds reuse the result." -ForegroundColor Yellow } } From b11398d656780508d1e173268295012571adfae7 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:00:06 +0100 Subject: [PATCH 24/49] Trim the developer guide --- README.md | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 30566f2..a25c9a0 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ cd ScopeOne A coding agent such as Claude Code or Codex can also do this: ask it to set up and build ScopeOne by following this README. -The sections below explain each step, the options and how to use a Qt that is already installed. Read [CONTRIBUTING.md](CONTRIBUTING.md) before changing the code. +Read [CONTRIBUTING.md](CONTRIBUTING.md) before changing the code. ### Qt @@ -107,7 +107,7 @@ ScopeOne needs Qt 6.8 or later. There are three ways to get it. - **Install Qt yourself** with the [Qt Online Installer](https://www.qt.io/download-qt-installer-oss), then pass its directory as above. On Windows choose the MSVC 2022 64-bit build; MinGW does not work. -- **Let the script build it.** Without `--qt`, vcpkg builds Qt on the first build. Qt is the largest part of that build, and vcpkg keeps the result, so it is built once (see [Build times](#build-times)). +- **Let the script build it.** Without `--qt`, vcpkg builds Qt on the first build, see [Build times](#build-times). ### Windows, step by step @@ -132,9 +132,9 @@ ScopeOne needs Qt 6.8 or later. There are three ways to get it. powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 --run ``` - `-ExecutionPolicy Bypass` lets this command run a `.ps1` file. After `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned` once, the script starts as `.\scripts\build.ps1`. + Windows blocks `.ps1` scripts by default, and `-ExecutionPolicy Bypass` lifts that for this one command. To lift it for good, run `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned` once; the script then starts as `.\scripts\build.ps1 --run`. - The script finds Visual Studio itself. It offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It then builds MMCore with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI, and downloads ONNX Runtime into `ScopeOneCore\external`, the GPU package when the CUDA Toolkit is installed. + The script finds Visual Studio itself. It offers to download vcpkg into `%USERPROFILE%\vcpkg` and saves `VCPKG_ROOT` for your user account, and `--yes` accepts without asking. It then builds MMCore with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. The first build takes long, see [Build times](#build-times). The last lines of the build show whether Core was built with the CUDA and ONNX modules. @@ -152,16 +152,6 @@ ScopeOne needs Qt 6.8 or later. There are three ways to get it. | `--qt ` | Use an installed Qt, see [Qt](#qt) | | `--yes` | Accept the vcpkg download without asking | -**CMake without the script.** After the script has built MMCore once (`--target core`), Core configures directly. Add `-DSCOPEONE_QT_DIR=` for an installed Qt: - -```powershell -cmake -S ScopeOneCore -B ScopeOneCore/build ` - "-DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" ` - "-DVCPKG_MANIFEST_DIR=$PWD" ` - "-DVCPKG_INSTALLED_DIR=$PWD/vcpkg_installed" ` - -DVCPKG_TARGET_TRIPLET=x64-windows -``` - ### Linux and macOS, step by step macOS support is experimental. @@ -181,7 +171,7 @@ macOS support is experimental. libxkbcommon-x11-dev ``` - The second command installs the X11 and OpenGL development files that vcpkg's Qt needs, and `--no-upgrade` keeps the installed Mesa drivers. The script lists the package names for Fedora when a tool is missing. + The second command installs the X11 and OpenGL development files that vcpkg's Qt needs, and `--no-upgrade` keeps the installed Mesa drivers. macOS: @@ -205,7 +195,7 @@ macOS support is experimental. ./scripts/build.sh --run ``` - The script checks the build tools, offers to download vcpkg into `$HOME/vcpkg` or a folder you type (`--yes` takes the default), and builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. It downloads ONNX Runtime into `ScopeOneCore/external`. + The script checks the build tools, offers to download vcpkg into `$HOME/vcpkg` or a folder you type (`--yes` takes the default), and builds Micro-Manager's core with the DemoCamera and Utilities adapters, ScopeOneCore, the drivers and the GUI. The first build takes long, see [Build times](#build-times). @@ -229,8 +219,7 @@ The first build without the cache, per vcpkg package, on the 48 core Linux machi | opencv4 | 2.2 min | | qtsvg | 59 s | | harfbuzz | 46 s | -| gperf, libb2, zstd, pcre2 | 10 to 22 s each | -| 29 others | under 10 s each | +| 33 others | under 25 s each | | All 37 packages | 11 min 23 s | A fresh clone with the binary cache filled, on the same machine, about 5 minutes in all: From 30f6dab5d43553ffa0d77c53c05dbaa5347e776b Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:28:20 +0100 Subject: [PATCH 25/49] Register every processing module in its own source file and make CUDA modules self-contained --- ScopeOneCore/CMakeLists.txt | 28 +- ScopeOneCore/README.md | 129 +++++++ ScopeOneCore/internal/ImageOperationModules.h | 2 - .../internal/ProcessingModuleRegistry.h | 31 +- ScopeOneCore/internal/ProcessingParameters.h | 75 ++++ .../cuda/CudaFrequencyFilterKernels.h | 20 ++ .../internal/cuda/CudaFrequencyFilterModule.h | 11 +- .../internal/cuda/CudaGaussianBlurKernels.h | 15 + .../internal/cuda/CudaGaussianBlurModule.h | 8 +- ScopeOneCore/internal/cuda/CudaKernelLaunch.h | 28 +- ScopeOneCore/internal/cuda/CudaModule.h | 56 +++ .../cuda/CudaProcessingRegistration.h | 12 - .../internal/cuda/CudaRealImageModule.h | 28 -- ScopeOneCore/internal/cuda/GpuComplexFrame.h | 35 -- .../cuda/{GpuRealFrame.h => GpuImage.h} | 19 +- .../InferenceProcessingRegistration.h | 12 - .../src/BackgroundCalibrationModule.cpp | 19 + .../src/DifferentialRollingModule.cpp | 15 + ScopeOneCore/src/FFTModule.cpp | 10 + .../src/FrequencyDomainFilterModule.cpp | 20 ++ ScopeOneCore/src/GaussianBlurModule.cpp | 14 + ScopeOneCore/src/IFFTModule.cpp | 10 + ScopeOneCore/src/ImageOperationModules.cpp | 29 +- ScopeOneCore/src/IscatModule.cpp | 26 ++ ScopeOneCore/src/MaskModule.cpp | 21 ++ ScopeOneCore/src/ProcessingModuleRegistry.cpp | 243 ++----------- .../src/SpatiotemporalBinningModule.cpp | 20 ++ .../src/cuda/CudaFrequencyFilterKernels.cu | 243 +++++++++++++ .../src/cuda/CudaFrequencyFilterModule.cpp | 43 ++- .../src/cuda/CudaGaussianBlurKernels.cu | 95 +++++ .../src/cuda/CudaGaussianBlurModule.cpp | 20 +- ScopeOneCore/src/cuda/CudaKernels.cu | 331 +----------------- ...CudaRealImageModule.cpp => CudaModule.cpp} | 47 ++- .../src/cuda/CudaProcessingRegistration.cpp | 96 ----- ScopeOneCore/src/cuda/GpuComplexFrame.cpp | 102 ------ .../cuda/{GpuRealFrame.cpp => GpuImage.cpp} | 38 +- .../InferenceProcessingRegistration.cpp | 18 +- src/AppVersion.h.in | 6 - src/ImageProcessingWidget.cpp | 35 +- 39 files changed, 1028 insertions(+), 982 deletions(-) create mode 100644 ScopeOneCore/internal/ProcessingParameters.h create mode 100644 ScopeOneCore/internal/cuda/CudaFrequencyFilterKernels.h create mode 100644 ScopeOneCore/internal/cuda/CudaGaussianBlurKernels.h create mode 100644 ScopeOneCore/internal/cuda/CudaModule.h delete mode 100644 ScopeOneCore/internal/cuda/CudaProcessingRegistration.h delete mode 100644 ScopeOneCore/internal/cuda/CudaRealImageModule.h delete mode 100644 ScopeOneCore/internal/cuda/GpuComplexFrame.h rename ScopeOneCore/internal/cuda/{GpuRealFrame.h => GpuImage.h} (68%) delete mode 100644 ScopeOneCore/internal/inference/InferenceProcessingRegistration.h create mode 100644 ScopeOneCore/src/cuda/CudaFrequencyFilterKernels.cu create mode 100644 ScopeOneCore/src/cuda/CudaGaussianBlurKernels.cu rename ScopeOneCore/src/cuda/{CudaRealImageModule.cpp => CudaModule.cpp} (55%) delete mode 100644 ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp delete mode 100644 ScopeOneCore/src/cuda/GpuComplexFrame.cpp rename ScopeOneCore/src/cuda/{GpuRealFrame.cpp => GpuImage.cpp} (86%) diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index 714e376..bcf63ff 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -132,6 +132,7 @@ set(CORE_HEADERS internal/ImageProcessingFramework.h internal/ProcessingModule.h internal/ProcessingModuleRegistry.h + internal/ProcessingParameters.h include/scopeone/ProcessingPipeline.h internal/DriverHostProtocol.h internal/SharedFrameRing.h @@ -277,31 +278,16 @@ if (SCOPEONE_CUDA_NVCC_EXECUTABLE) endif () if (SCOPEONE_CUDA_NVCC_EXECUTABLE AND TARGET CUDA::cufft) set(SCOPEONE_CUDA_ARCHITECTURES "50;75;80;86;89" CACHE STRING "Target CUDA architectures") - target_sources(ScopeOneCore PRIVATE - src/cuda/CudaKernels.cu - src/cuda/CudaDevice.cpp - src/cuda/CudaFrequencyFilterModule.cpp - src/cuda/CudaGaussianBlurModule.cpp - src/cuda/CudaProcessingRegistration.cpp - src/cuda/CudaRealImageModule.cpp - src/cuda/GpuComplexFrame.cpp - src/cuda/GpuRealFrame.cpp - internal/cuda/CudaDevice.h - internal/cuda/CudaFrequencyFilterModule.h - internal/cuda/CudaGaussianBlurModule.h - internal/cuda/CudaKernelLaunch.h - internal/cuda/CudaProcessingRegistration.h - internal/cuda/CudaRealImageModule.h - internal/cuda/GpuComplexFrame.h - internal/cuda/GpuRealFrame.h - ) - set_source_files_properties(src/cuda/CudaKernels.cu PROPERTIES SKIP_PRECOMPILE_HEADERS ON) + # A new CUDA module is a new file in src/cuda and internal/cuda, picked up without editing this list + file(GLOB _cuda_sources CONFIGURE_DEPENDS src/cuda/*.cpp internal/cuda/*.h) + file(GLOB _cuda_kernels CONFIGURE_DEPENDS src/cuda/*.cu) + target_sources(ScopeOneCore PRIVATE ${_cuda_sources} ${_cuda_kernels}) + set_source_files_properties(${_cuda_kernels} PROPERTIES SKIP_PRECOMPILE_HEADERS ON) set_target_properties(ScopeOneCore PROPERTIES CUDA_ARCHITECTURES "${SCOPEONE_CUDA_ARCHITECTURES}" CUDA_SEPARABLE_COMPILATION OFF ) target_link_libraries(ScopeOneCore PRIVATE CUDA::cudart CUDA::cufft) - target_compile_definitions(ScopeOneCore PRIVATE SCOPEONE_WITH_CUDA) set(_with_cuda ON) if (WIN32) file(GLOB _cuda_runtime_dlls @@ -403,7 +389,6 @@ if (ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRARY) src/inference/OnnxRuntime.cpp src/inference/OnnxSession.cpp internal/inference/ExecutionProvider.h - internal/inference/InferenceProcessingRegistration.h internal/inference/OnnxInferenceModule.h internal/inference/OnnxInternal.h internal/inference/OnnxRuntime.h @@ -412,7 +397,6 @@ if (ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRARY) ) target_include_directories(ScopeOneCore PRIVATE "${ONNXRUNTIME_INCLUDE_DIR}") target_link_libraries(ScopeOneCore PRIVATE "${ONNXRUNTIME_LIBRARY}") - target_compile_definitions(ScopeOneCore PRIVATE SCOPEONE_WITH_INFERENCE) set(_with_inference ON) if (WIN32) file(GLOB _onnxruntime_dlls diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 28b0d98..50b3392 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -126,6 +126,135 @@ Providers use ScopeOne logical device IDs and publish `ImageFrame` objects throu Drivers outside this repository consume the exported `scopeone::DriverSDK` CMake target. Every driver manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`, and incompatible manifests are rejected before the library instance is created. +## Processing Modules + +Every processing method of Core is a `ProcessingModule` that registers itself at the end of its own source file: + +```cpp +SCOPEONE_REGISTER_PROCESSING_MODULE( + GaussianBlurModule, + QStringLiteral("gaussian_blur"), + QStringLiteral("Gaussian Blur"), + 1, + {integerParameter("kernel_size", "Kernel size", 3, 1, 99, 2), + realParameter("sigma", "Sigma", 0.0, 0.0, 100.0, 0.1, 2)}) +``` + +The arguments after the class are its `ProcessingModuleDescriptor`: the id that recipes store, the name shown in the processing list, the schema version and the parameters, built with `integerParameter`, `realParameter`, `booleanParameter` and `choiceParameter` from `internal/ProcessingParameters.h`. A file that builds several modules from one table, or a descriptor that depends on the machine such as the ONNX providers, passes a function to `addProcessingModuleRegistration` instead, as `ImageOperationModules.cpp` does. Each file registers once. The registry and the processing list need no change, and the list is sorted by name. + +## CUDA Modules + +A CUDA module is the GPU version of a processing method. It is built into Core when CUDA is built, works on one frame at a time like any other module, and is named after the CPU method with "(CUDA)", so the two stand together in the list. Its id starts with `cuda.` and it uses the parameter keys of the CPU method. + +`CudaModule` does the frame handling. It uploads the frame into a float image in GPU memory, calls `processDevice`, and converts the result back to an 8 or 16 bit frame. A module writes `processDevice` and its kernels: + +```cpp +// internal/cuda/CudaInvertModule.h +#pragma once + +#include "internal/cuda/CudaModule.h" + +namespace scopeone::core::internal::cuda +{ + class CudaInvertModule : public CudaModule + { + public: + QString id() const override { return QStringLiteral("cuda.invert"); } + QString name() const override { return QStringLiteral("Invert (CUDA)"); } + QVariantMap parameters() const override { return {}; } + void setParameters(const QVariantMap&) override {} + std::unique_ptr createRuntime() const override + { + return std::make_unique(); + } + + protected: + bool processDevice(const GpuImage& input, GpuImage& output, int bitDepth) override; + }; +} +``` + +```cpp +// src/cuda/CudaInvertModule.cpp +#include "internal/cuda/CudaInvertModule.h" + +#include "internal/cuda/CudaInvertKernels.h" +#include "internal/ProcessingModuleRegistry.h" + +namespace scopeone::core::internal::cuda +{ + bool CudaInvertModule::processDevice(const GpuImage& input, GpuImage& output, int bitDepth) + { + return detail::launchInvert(input.data(), input.pitchBytes(), output.data(), output.pitchBytes(), + input.width(), input.height(), bitDepth >= 16 ? 65535.0f : 255.0f); + } +} + +SCOPEONE_REGISTER_PROCESSING_MODULE(scopeone::core::internal::cuda::CudaInvertModule, + QStringLiteral("cuda.invert"), QStringLiteral("Invert (CUDA)"), 1, {}) +``` + +```cpp +// internal/cuda/CudaInvertKernels.h +#pragma once + +#include + +namespace scopeone::core::internal::cuda::detail +{ + bool launchInvert(const void* input, std::size_t inputPitch, void* output, std::size_t outputPitch, + int width, int height, float maxValue); +} +``` + +```cpp +// src/cuda/CudaInvertKernels.cu +#include "internal/cuda/CudaInvertKernels.h" +#include "internal/cuda/CudaKernelLaunch.h" + +#include + +namespace +{ + __global__ void invertKernel(const float* input, std::size_t inputPitch, float* output, + std::size_t outputPitch, int width, int height, float maxValue) + { + const int x = blockIdx.x * blockDim.x + threadIdx.x; + const int y = blockIdx.y * blockDim.y + threadIdx.y; + if (x >= width || y >= height) + { + return; + } + const auto* in = reinterpret_cast(reinterpret_cast(input) + y * inputPitch); + auto* out = reinterpret_cast(reinterpret_cast(output) + y * outputPitch); + out[x] = maxValue - in[x]; + } +} + +namespace scopeone::core::internal::cuda::detail +{ + bool launchInvert(const void* input, std::size_t inputPitch, void* output, std::size_t outputPitch, + int width, int height, float maxValue) + { + const dim3 block(16, 16); + const dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y); + invertKernel<<>>(static_cast(input), inputPitch, + static_cast(output), outputPitch, width, height, maxValue); + return synchronizeKernel(); + } +} +``` + +The four files are the whole change, as CMake builds every file in `src/cuda` and `internal/cuda`. + +- The kernels are launched through plain functions declared in the module's own kernels header, which has no Qt and no CUDA types, so the module sources compile without nvcc and no shared file changes. `CudaKernelLaunch.h` holds what the kernel files share, such as `synchronizeKernel`. +- The frames are pitched 2D float images by default. `GpuMemoryLayout::Contiguous` in the constructor gives a contiguous buffer for libraries that need one, such as cuFFT. +- The output has the size of the input. A method that bins, crops or scales overrides `outputSize` and gets an output image of that size. +- The output pixel is `value * scale + offset`, clamped to the processing bit depth, with a scale of 1 and an offset of 0 by default. A method with a signed result, such as a difference of Gaussians or a background subtraction, overrides `outputMapping` and returns an offset of half the maximum value, so zero lands in the middle of the range. +- cuFFT is linked. `CudaFrequencyFilterModule` shows how a module keeps its FFT plans and scratch buffers and recreates them when the frame size changes. +- NPP comes with the CUDA Toolkit. A module that uses it adds the library it needs, such as `CUDA::nppif` for filters, to `target_link_libraries` beside `CUDA::cufft`, and its DLL to the `_cuda_runtime_dlls` glob for the Windows package. NPP takes the frame's pointer, pitch and size directly. +- A failing CUDA call returns `false` from `processDevice`, and the module reports "CUDA processing failed" for that frame. + ## Processing Data Flow `ImageFrame` is the frame model used by preview, processing, recording, gallery and the local API. Processing recipes persist stable module IDs rather than registry positions. Available modules and their parameter descriptors come from the processing registry. Use `processFrameThrough(...)` to stop at one pipeline stage and `processFrameFrom(...)` to continue from a later module after an edited frame is written back. Saved OME-TIFF, OME-Zarr, TIFF and binary recording outputs are read back asynchronously through `ScopeOneCore::requestRecordingSessionFrame(...)`. diff --git a/ScopeOneCore/internal/ImageOperationModules.h b/ScopeOneCore/internal/ImageOperationModules.h index bb2c8c2..0fed3f2 100644 --- a/ScopeOneCore/internal/ImageOperationModules.h +++ b/ScopeOneCore/internal/ImageOperationModules.h @@ -1,7 +1,6 @@ #pragma once #include "internal/ProcessingModule.h" -#include "internal/ProcessingModuleRegistry.h" #include #include @@ -36,5 +35,4 @@ namespace scopeone::core::internal int m_windowType{0}; }; - void registerImageOperationModules(ProcessingModuleRegistry& registry); } // namespace scopeone::core::internal diff --git a/ScopeOneCore/internal/ProcessingModuleRegistry.h b/ScopeOneCore/internal/ProcessingModuleRegistry.h index 3950c2c..4df118b 100644 --- a/ScopeOneCore/internal/ProcessingModuleRegistry.h +++ b/ScopeOneCore/internal/ProcessingModuleRegistry.h @@ -3,7 +3,6 @@ #include "scopeone/ProcessingTypes.h" #include -#include #include #include @@ -14,10 +13,15 @@ namespace scopeone::core::internal public: using Factory = std::function()>; + // Registers every module that its source file added with SCOPEONE_REGISTER_PROCESSING_MODULE ProcessingModuleRegistry(); ~ProcessingModuleRegistry(); bool registerModule(const ProcessingModuleDescriptor& descriptor, Factory factory); + // For the modules built into Core, whose id and descriptor the program guarantees. + // Stops with the module's id when one is registered twice or its descriptor is invalid. + void registerBuiltIn(const ProcessingModuleDescriptor& descriptor, Factory factory); + // Sorted by name, so a CPU module and its "(CUDA)" version are listed together QList descriptors() const; ProcessingModuleDescriptor descriptor(const QString& moduleId) const; std::unique_ptr create(const QString& moduleId) const; @@ -30,6 +34,29 @@ namespace scopeone::core::internal }; QHash m_entries; - QStringList m_order; }; + + using ProcessingModuleRegistration = void (*)(ProcessingModuleRegistry& registry); + + // Called while Core is loaded, before any registry exists. Every registry runs the + // registrations when it is built, so a descriptor that depends on the machine, such as the + // ONNX providers, is made then and not at load time. + bool addProcessingModuleRegistration(ProcessingModuleRegistration registration); } + +// Registers one module with its descriptor. Placed once at the end of the module's source file, +// so the registry and the processing list need no change. +#define SCOPEONE_REGISTER_PROCESSING_MODULE(Module, ...) \ + namespace \ + { \ + [[maybe_unused]] const bool processingModuleRegistered = \ + ::scopeone::core::internal::addProcessingModuleRegistration( \ + [](::scopeone::core::internal::ProcessingModuleRegistry& registry) \ + { \ + registry.registerBuiltIn( \ + ::scopeone::core::ProcessingModuleDescriptor{__VA_ARGS__}, \ + []() -> std::unique_ptr<::scopeone::core::ProcessingModule> \ + { return std::make_unique(); }); \ + }); \ + } + diff --git a/ScopeOneCore/internal/ProcessingParameters.h b/ScopeOneCore/internal/ProcessingParameters.h new file mode 100644 index 0000000..2d9169e --- /dev/null +++ b/ScopeOneCore/internal/ProcessingParameters.h @@ -0,0 +1,75 @@ +#pragma once + +#include "scopeone/ProcessingTypes.h" + +#include + +namespace scopeone::core::internal +{ + // Builders for the parameter descriptors of a module, shared by the built-in and the CUDA + // modules so that every descriptor is written the same way. + inline ProcessingParameterDescriptor integerParameter(const char* key, + const char* name, + int value, + int minimum, + int maximum, + int step = 1) + { + return {QString::fromLatin1(key), + QString::fromLatin1(name), + ProcessingParameterType::Integer, + value, + minimum, + maximum, + step}; + } + + inline ProcessingParameterDescriptor realParameter(const char* key, + const char* name, + double value, + double minimum, + double maximum, + double step, + int decimals) + { + ProcessingParameterDescriptor descriptor{ + QString::fromLatin1(key), + QString::fromLatin1(name), + ProcessingParameterType::Real, + value, + minimum, + maximum, + step}; + descriptor.decimals = decimals; + return descriptor; + } + + inline ProcessingParameterDescriptor booleanParameter(const char* key, + const char* name, + bool value) + { + return {QString::fromLatin1(key), + QString::fromLatin1(name), + ProcessingParameterType::Boolean, + value}; + } + + // The choices take the values 0, 1, 2 ... in the order given + inline ProcessingParameterDescriptor choiceParameter(const char* key, + const char* name, + int value, + std::initializer_list choices) + { + ProcessingParameterDescriptor descriptor{ + QString::fromLatin1(key), + QString::fromLatin1(name), + ProcessingParameterType::Choice, + value}; + int index = 0; + for (const char* choice : choices) + { + descriptor.choices.append({QString::fromLatin1(choice), index++}); + } + return descriptor; + } +} diff --git a/ScopeOneCore/internal/cuda/CudaFrequencyFilterKernels.h b/ScopeOneCore/internal/cuda/CudaFrequencyFilterKernels.h new file mode 100644 index 0000000..04cd9e5 --- /dev/null +++ b/ScopeOneCore/internal/cuda/CudaFrequencyFilterKernels.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +namespace scopeone::core::internal::cuda::detail +{ + bool launchFrequencyFilter(const void* input, + void* output, + void* spectrum, + int width, + int height, + float minFeatureSize, + float maxFeatureSize, + int filterKind, + int outputMode, + int forwardPlan, + int inversePlan, + void* minMaxScratch, + float maxValue); +} diff --git a/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h b/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h index 80a23dd..f548854 100644 --- a/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h +++ b/ScopeOneCore/internal/cuda/CudaFrequencyFilterModule.h @@ -1,13 +1,12 @@ #pragma once -#include "internal/cuda/CudaRealImageModule.h" -#include "internal/cuda/GpuComplexFrame.h" +#include "internal/cuda/CudaModule.h" #include namespace scopeone::core::internal::cuda { - class CudaFrequencyFilterModule final : public scopeone::core::internal::cuda::CudaRealImageModule + class CudaFrequencyFilterModule final : public scopeone::core::internal::cuda::CudaModule { public: CudaFrequencyFilterModule(); @@ -21,15 +20,15 @@ namespace scopeone::core::internal::cuda bool resetState() override; protected: - bool processDevice(const scopeone::core::internal::cuda::GpuRealFrame& input, - scopeone::core::internal::cuda::GpuRealFrame& output, + bool processDevice(const scopeone::core::internal::cuda::GpuImage& input, + scopeone::core::internal::cuda::GpuImage& output, int bitDepth) override; private: void destroyPlans(); void releaseScratch(); - scopeone::core::internal::cuda::GpuComplexFrame m_spectrum; + void* m_spectrum{nullptr}; cufftHandle m_forwardPlan{0}; cufftHandle m_inversePlan{0}; int m_planWidth{0}; diff --git a/ScopeOneCore/internal/cuda/CudaGaussianBlurKernels.h b/ScopeOneCore/internal/cuda/CudaGaussianBlurKernels.h new file mode 100644 index 0000000..a0e5571 --- /dev/null +++ b/ScopeOneCore/internal/cuda/CudaGaussianBlurKernels.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace scopeone::core::internal::cuda::detail +{ + bool launchGaussian(const void* input, + std::size_t inputPitchBytes, + void* output, + std::size_t outputPitchBytes, + int width, + int height, + int kernelSize, + float sigma); +} diff --git a/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h b/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h index 46d0b6a..a210bf2 100644 --- a/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h +++ b/ScopeOneCore/internal/cuda/CudaGaussianBlurModule.h @@ -1,10 +1,10 @@ #pragma once -#include "internal/cuda/CudaRealImageModule.h" +#include "internal/cuda/CudaModule.h" namespace scopeone::core::internal::cuda { - class CudaGaussianBlurModule final : public scopeone::core::internal::cuda::CudaRealImageModule + class CudaGaussianBlurModule final : public scopeone::core::internal::cuda::CudaModule { public: CudaGaussianBlurModule(); @@ -16,8 +16,8 @@ namespace scopeone::core::internal::cuda std::unique_ptr createRuntime() const override; protected: - bool processDevice(const scopeone::core::internal::cuda::GpuRealFrame& input, - scopeone::core::internal::cuda::GpuRealFrame& output, + bool processDevice(const scopeone::core::internal::cuda::GpuImage& input, + scopeone::core::internal::cuda::GpuImage& output, int bitDepth) override; private: diff --git a/ScopeOneCore/internal/cuda/CudaKernelLaunch.h b/ScopeOneCore/internal/cuda/CudaKernelLaunch.h index 87c42f3..403a6ed 100644 --- a/ScopeOneCore/internal/cuda/CudaKernelLaunch.h +++ b/ScopeOneCore/internal/cuda/CudaKernelLaunch.h @@ -19,28 +19,10 @@ namespace scopeone::core::internal::cuda::detail int destinationStride, int width, int height, - int destinationBytesPerPixel); + int destinationBytesPerPixel, + float scale, + float offset); - bool launchGaussian(const void* input, - std::size_t inputPitchBytes, - void* output, - std::size_t outputPitchBytes, - int width, - int height, - int kernelSize, - float sigma); - - bool launchFrequencyFilter(const void* input, - void* output, - void* spectrum, - int width, - int height, - float minFeatureSize, - float maxFeatureSize, - int filterKind, - int outputMode, - int forwardPlan, - int inversePlan, - void* minMaxScratch, - float maxValue); + // Checks the last launch and waits for the device, so a failed kernel is reported at once + bool synchronizeKernel(); } diff --git a/ScopeOneCore/internal/cuda/CudaModule.h b/ScopeOneCore/internal/cuda/CudaModule.h new file mode 100644 index 0000000..2ed64d6 --- /dev/null +++ b/ScopeOneCore/internal/cuda/CudaModule.h @@ -0,0 +1,56 @@ +#pragma once + +#include "scopeone/ProcessingTypes.h" +#include "internal/cuda/GpuImage.h" + +namespace scopeone::core::internal::cuda +{ + struct GpuImageSize + { + int width{0}; + int height{0}; + }; + + // The output pixel is value * scale + offset, clamped to the processing bit depth + struct GpuOutputMapping + { + float scale{1.0f}; + float offset{0.0f}; + }; + + class CudaModule + : public scopeone::core::ProcessingModule + { + public: + explicit CudaModule(GpuMemoryLayout layout = GpuMemoryLayout::Pitched2D); + ~CudaModule() override; + + scopeone::core::ProcessingResult process( + const scopeone::core::ImageFrame& frame, + int processingBitDepth) final override; + + protected: + // The output image is allocated with outputSize and has the input's layout + virtual bool processDevice(const GpuImage& input, + GpuImage& output, + int bitDepth) = 0; + + // A method that bins, crops or scales returns the size of its result + virtual GpuImageSize outputSize(int inputWidth, int inputHeight) const + { + return {inputWidth, inputHeight}; + } + + // A method with a signed result, such as a difference of Gaussians, moves zero to the + // middle of the range with an offset of half the maximum value + virtual GpuOutputMapping outputMapping(int bitDepth) const + { + Q_UNUSED(bitDepth); + return {}; + } + + GpuImage m_inputBuffer; + GpuImage m_outputBuffer; + GpuMemoryLayout m_layout; + }; +} diff --git a/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h b/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h deleted file mode 100644 index 0bae74e..0000000 --- a/ScopeOneCore/internal/cuda/CudaProcessingRegistration.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -namespace scopeone::core::internal -{ - class ProcessingModuleRegistry; -} - -namespace scopeone::core::internal::cuda -{ - // Registers the CUDA processing modules, built into Core when SCOPEONE_ENABLE_CUDA is on - void registerCudaProcessingModules(core::internal::ProcessingModuleRegistry& registry); -} diff --git a/ScopeOneCore/internal/cuda/CudaRealImageModule.h b/ScopeOneCore/internal/cuda/CudaRealImageModule.h deleted file mode 100644 index de66765..0000000 --- a/ScopeOneCore/internal/cuda/CudaRealImageModule.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include "scopeone/ProcessingTypes.h" -#include "internal/cuda/GpuRealFrame.h" - -namespace scopeone::core::internal::cuda -{ - class CudaRealImageModule - : public scopeone::core::ProcessingModule - { - public: - explicit CudaRealImageModule(GpuMemoryLayout layout = GpuMemoryLayout::Pitched2D); - ~CudaRealImageModule() override; - - scopeone::core::ProcessingResult process( - const scopeone::core::ImageFrame& frame, - int processingBitDepth) final override; - - protected: - virtual bool processDevice(const GpuRealFrame& input, - GpuRealFrame& output, - int bitDepth) = 0; - - GpuRealFrame m_inputBuffer; - GpuRealFrame m_outputBuffer; - GpuMemoryLayout m_layout; - }; -} diff --git a/ScopeOneCore/internal/cuda/GpuComplexFrame.h b/ScopeOneCore/internal/cuda/GpuComplexFrame.h deleted file mode 100644 index 5d6d94f..0000000 --- a/ScopeOneCore/internal/cuda/GpuComplexFrame.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - - -#include - -namespace scopeone::core::internal::cuda -{ - class GpuComplexFrame - { - public: - GpuComplexFrame() = default; - ~GpuComplexFrame(); - - GpuComplexFrame(const GpuComplexFrame&) = delete; - GpuComplexFrame& operator=(const GpuComplexFrame&) = delete; - GpuComplexFrame(GpuComplexFrame&& other) noexcept; - GpuComplexFrame& operator=(GpuComplexFrame&& other) noexcept; - - bool allocateForRealImage(int width, int height); - void release(); - bool isValid() const; - - void* data() const; - int realWidth() const; - int spectrumWidth() const; - int height() const; - std::size_t elementCount() const; - - private: - void* m_data{nullptr}; - int m_realWidth{0}; - int m_spectrumWidth{0}; - int m_height{0}; - }; -} diff --git a/ScopeOneCore/internal/cuda/GpuRealFrame.h b/ScopeOneCore/internal/cuda/GpuImage.h similarity index 68% rename from ScopeOneCore/internal/cuda/GpuRealFrame.h rename to ScopeOneCore/internal/cuda/GpuImage.h index 3923100..fa9848c 100644 --- a/ScopeOneCore/internal/cuda/GpuRealFrame.h +++ b/ScopeOneCore/internal/cuda/GpuImage.h @@ -12,17 +12,17 @@ namespace scopeone::core::internal::cuda Contiguous }; - class GpuRealFrame + class GpuImage { public: - GpuRealFrame() = default; - GpuRealFrame(int width, int height, GpuMemoryLayout layout); - ~GpuRealFrame(); + GpuImage() = default; + GpuImage(int width, int height, GpuMemoryLayout layout); + ~GpuImage(); - GpuRealFrame(const GpuRealFrame&) = delete; - GpuRealFrame& operator=(const GpuRealFrame&) = delete; - GpuRealFrame(GpuRealFrame&& other) noexcept; - GpuRealFrame& operator=(GpuRealFrame&& other) noexcept; + GpuImage(const GpuImage&) = delete; + GpuImage& operator=(const GpuImage&) = delete; + GpuImage(GpuImage&& other) noexcept; + GpuImage& operator=(GpuImage&& other) noexcept; bool allocate(int width, int height, GpuMemoryLayout layout); void release(); @@ -35,7 +35,8 @@ namespace scopeone::core::internal::cuda GpuMemoryLayout layout() const; bool upload(const scopeone::core::ImageFrame& frame); - bool download(scopeone::core::ImageFrame& frame) const; + // Writes value * scale + offset, clamped to the frame's bit depth + bool download(scopeone::core::ImageFrame& frame, float scale, float offset) const; private: bool ensureUploadBuffer(std::size_t bytes); diff --git a/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h b/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h deleted file mode 100644 index c9ac74a..0000000 --- a/ScopeOneCore/internal/inference/InferenceProcessingRegistration.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -namespace scopeone::core::internal -{ - class ProcessingModuleRegistry; -} - -namespace scopeone::core::internal::inference -{ - // Registers the ONNX Runtime processing modules, built into Core when SCOPEONE_ENABLE_INFERENCE is on - void registerInferenceProcessingModules(core::internal::ProcessingModuleRegistry& registry); -} diff --git a/ScopeOneCore/src/BackgroundCalibrationModule.cpp b/ScopeOneCore/src/BackgroundCalibrationModule.cpp index 6b82098..e51801c 100644 --- a/ScopeOneCore/src/BackgroundCalibrationModule.cpp +++ b/ScopeOneCore/src/BackgroundCalibrationModule.cpp @@ -1,4 +1,7 @@ #include "internal/BackgroundCalibrationModule.h" + +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" #include "internal/FrameBufferUtils.h" #include #include @@ -475,3 +478,19 @@ namespace scopeone::core::internal } } } // namespace scopeone::core::internal + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + BackgroundCalibrationModule, + QStringLiteral("background_calibration"), + QStringLiteral("Background Calibration"), + 1, + {integerParameter("calibration_frames", "Frames", 101, 3, 1001, 2), + choiceParameter("mode", "Mode", 0, {"Snapshot", "Running"}), + choiceParameter("method", "Method", 0, + {"Median", "Mean", "Maximum", "Minimum"}), + choiceParameter("operation", "Operation", 0, + {"Subtract", "Add", "Multiply", "Divide"})}, + true) +} diff --git a/ScopeOneCore/src/DifferentialRollingModule.cpp b/ScopeOneCore/src/DifferentialRollingModule.cpp index f31f992..ccba617 100644 --- a/ScopeOneCore/src/DifferentialRollingModule.cpp +++ b/ScopeOneCore/src/DifferentialRollingModule.cpp @@ -1,5 +1,8 @@ #include "internal/DifferentialRollingModule.h" +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" + #include "internal/FrameBufferUtils.h" #include @@ -324,3 +327,15 @@ namespace scopeone::core::internal return true; } } // namespace scopeone::core::internal + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + DifferentialRollingModule, + QStringLiteral("differential_rolling"), + QStringLiteral("Differential Rolling"), + 1, + {integerParameter("batch_size", "Batch size", 16, 1, 256), + booleanParameter("normalize", "Normalize by batch 1", true)}, + true) +} diff --git a/ScopeOneCore/src/FFTModule.cpp b/ScopeOneCore/src/FFTModule.cpp index 0ad4be4..f30f5bf 100644 --- a/ScopeOneCore/src/FFTModule.cpp +++ b/ScopeOneCore/src/FFTModule.cpp @@ -1,5 +1,8 @@ #include "internal/FFTModule.h" +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" + #include "internal/FrameBufferUtils.h" #include @@ -132,3 +135,10 @@ namespace scopeone::core::internal { } } // namespace scopeone::core::internal + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + FFTModule, + QStringLiteral("fft"), QStringLiteral("FFT"), 1, {}) +} diff --git a/ScopeOneCore/src/FrequencyDomainFilterModule.cpp b/ScopeOneCore/src/FrequencyDomainFilterModule.cpp index e4efeb1..916ddb6 100644 --- a/ScopeOneCore/src/FrequencyDomainFilterModule.cpp +++ b/ScopeOneCore/src/FrequencyDomainFilterModule.cpp @@ -1,5 +1,8 @@ #include "internal/FrequencyDomainFilterModule.h" +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" + #include "internal/FrameBufferUtils.h" #include @@ -235,3 +238,20 @@ namespace scopeone::core::internal } } } // namespace scopeone::core::internal + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + FrequencyDomainFilterModule, + QStringLiteral("frequency_domain_filter"), + QStringLiteral("Frequency Domain Filter"), + 1, + {choiceParameter("output_mode", "Output", 2, + {"Spectrum", "Filtered spectrum", "Filtered image"}), + realParameter("min_feature_size", "Min feature size", 2.0, + 0.0, 1000.0, 0.1, 2), + realParameter("max_feature_size", "Max feature size", 10.0, + 0.0, 1000.0, 0.1, 2), + choiceParameter("filter_kind", "Filter kind", 0, + {"Smooth", "Hard"})}) +} diff --git a/ScopeOneCore/src/GaussianBlurModule.cpp b/ScopeOneCore/src/GaussianBlurModule.cpp index 275e8a7..81e3dc3 100644 --- a/ScopeOneCore/src/GaussianBlurModule.cpp +++ b/ScopeOneCore/src/GaussianBlurModule.cpp @@ -1,4 +1,7 @@ #include "internal/GaussianBlurModule.h" + +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" #include "internal/FrameBufferUtils.h" #include @@ -82,3 +85,14 @@ namespace scopeone::core::internal } } } // namespace scopeone::core::internal + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + GaussianBlurModule, + QStringLiteral("gaussian_blur"), + QStringLiteral("Gaussian Blur"), + 1, + {integerParameter("kernel_size", "Kernel size", 3, 1, 99, 2), + realParameter("sigma", "Sigma", 0.0, 0.0, 100.0, 0.1, 2)}) +} diff --git a/ScopeOneCore/src/IFFTModule.cpp b/ScopeOneCore/src/IFFTModule.cpp index 01fa723..be4efac 100644 --- a/ScopeOneCore/src/IFFTModule.cpp +++ b/ScopeOneCore/src/IFFTModule.cpp @@ -1,5 +1,8 @@ #include "internal/IFFTModule.h" +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" + #include "internal/FrameBufferUtils.h" #include @@ -79,3 +82,10 @@ namespace scopeone::core::internal } } } + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + IFFTModule, + QStringLiteral("ifft"), QStringLiteral("IFFT"), 1, {}) +} diff --git a/ScopeOneCore/src/ImageOperationModules.cpp b/ScopeOneCore/src/ImageOperationModules.cpp index 5c916ce..6f08308 100644 --- a/ScopeOneCore/src/ImageOperationModules.cpp +++ b/ScopeOneCore/src/ImageOperationModules.cpp @@ -759,22 +759,27 @@ namespace scopeone::core::internal } } - void registerImageOperationModules(ProcessingModuleRegistry& registry) + namespace { - for (const Spec& spec : specs()) + void registerImageOperationModules(ProcessingModuleRegistry& registry) { - QVariantMap defaults; - for (const ProcessingParameterDescriptor& parameter : spec.parameters) + for (const Spec& spec : specs()) { - defaults.insert(parameter.key, parameter.defaultValue); + QVariantMap defaults; + for (const ProcessingParameterDescriptor& parameter : spec.parameters) + { + defaults.insert(parameter.key, parameter.defaultValue); + } + const QString id = QString::fromLatin1(spec.id); + const QString name = QString::fromLatin1(spec.name); + registry.registerBuiltIn({id, name, 1, spec.parameters, spec.resettable}, + [id, name, defaults]() + { + return std::make_unique(id, name, defaults); + }); } - const QString id = QString::fromLatin1(spec.id); - const QString name = QString::fromLatin1(spec.name); - registry.registerModule({id, name, 1, spec.parameters, spec.resettable}, - [id, name, defaults]() - { - return std::make_unique(id, name, defaults); - }); } + + [[maybe_unused]] const bool registered = addProcessingModuleRegistration(registerImageOperationModules); } } // namespace scopeone::core::internal diff --git a/ScopeOneCore/src/IscatModule.cpp b/ScopeOneCore/src/IscatModule.cpp index 86e2d19..d1f7887 100644 --- a/ScopeOneCore/src/IscatModule.cpp +++ b/ScopeOneCore/src/IscatModule.cpp @@ -1,5 +1,8 @@ #include "internal/IscatModule.h" +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" + #include #include #include @@ -404,3 +407,26 @@ namespace scopeone::core::internal return {}; } } + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + IscatModule, + QStringLiteral("iscat.processing"), + QStringLiteral("iSCAT Processing"), + 1, + {choiceParameter("mode", "Background mode", 0, + {"Flat Field", "Temporal Median", "Dynamic EMA", + "Snapshot Reference", "Differential Rolling (DRA)"}), + realParameter("blur_sigma", "Blur sigma", 15.0, 1.0, 100.0, 0.5, 1), + integerParameter("median_window", "Median window", 31, 3, 101, 2), + realParameter("ema_alpha", "Smoothing factor", 0.05, 0.001, 0.5, 0.005, 3), + booleanParameter("capture_reference", "Capture Reference", false), + integerParameter("batch_size", "DRA batch size", 16, 1, 500, 1), + realParameter("contrast_gain", "Contrast gain", 20.0, 1.0, 500.0, 1.0, 1), + booleanParameter("enable_high_pass", "Enable High-Pass", false), + realParameter("high_pass_sigma", "High-Pass sigma", 30.0, 2.0, 200.0, 1.0, 1), + choiceParameter("output_mode", "Output mode", 0, + {"Contrast Centered", "Absolute Contrast", "Estimated Background"})}, + true) +} diff --git a/ScopeOneCore/src/MaskModule.cpp b/ScopeOneCore/src/MaskModule.cpp index 92da108..8067e39 100644 --- a/ScopeOneCore/src/MaskModule.cpp +++ b/ScopeOneCore/src/MaskModule.cpp @@ -1,5 +1,8 @@ #include "internal/MaskModule.h" +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" + #include "internal/FrameBufferUtils.h" #include @@ -286,3 +289,21 @@ namespace scopeone::core::internal } } } + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + MaskModule, + QStringLiteral("mask"), + QStringLiteral("Mask"), + 1, + {choiceParameter("shape", "Shape", 0, {"Ellipse", "Rectangle", "Annulus"}), + realParameter("center_x", "Center X", 0.0, -0.5, 0.5, 0.001, 3), + realParameter("center_y", "Center Y", 0.0, -0.5, 0.5, 0.001, 3), + realParameter("size_x", "Size X", 0.1, 0.001, 1.0, 0.001, 3), + realParameter("size_y", "Size Y", 0.1, 0.001, 1.0, 0.001, 3), + realParameter("inner_size", "Inner size", 0.0, 0.0, 1.0, 0.001, 3), + realParameter("rotation", "Rotation", 0.0, -180.0, 180.0, 0.1, 1), + realParameter("edge_width", "Edge width", 0.0, 0.0, 0.5, 0.001, 3), + booleanParameter("invert", "Invert", false)}) +} diff --git a/ScopeOneCore/src/ProcessingModuleRegistry.cpp b/ScopeOneCore/src/ProcessingModuleRegistry.cpp index e6599a6..419c15f 100644 --- a/ScopeOneCore/src/ProcessingModuleRegistry.cpp +++ b/ScopeOneCore/src/ProcessingModuleRegistry.cpp @@ -1,25 +1,10 @@ #include "internal/ProcessingModuleRegistry.h" -#include "internal/BackgroundCalibrationModule.h" -#include "internal/DifferentialRollingModule.h" -#include "internal/FFTModule.h" -#include "internal/FrequencyDomainFilterModule.h" -#include "internal/MaskModule.h" -#include "internal/GaussianBlurModule.h" -#include "internal/IscatModule.h" -#include "internal/ImageOperationModules.h" -#include "internal/IFFTModule.h" -#include "internal/SpatiotemporalBinningModule.h" - -#ifdef SCOPEONE_WITH_CUDA -#include "internal/cuda/CudaProcessingRegistration.h" -#endif -#ifdef SCOPEONE_WITH_INFERENCE -#include "internal/inference/InferenceProcessingRegistration.h" -#endif - #include +#include +#include + namespace scopeone::core::internal { namespace @@ -42,197 +27,27 @@ namespace scopeone::core::internal && !descriptor.name.trimmed().isEmpty(); } - ProcessingParameterDescriptor integerParameter(const char* key, - const char* name, - int value, - int minimum, - int maximum, - int step = 1) - { - return {QString::fromLatin1(key), - QString::fromLatin1(name), - ProcessingParameterType::Integer, - value, - minimum, - maximum, - step}; - } - - ProcessingParameterDescriptor realParameter(const char* key, - const char* name, - double value, - double minimum, - double maximum, - double step, - int decimals) - { - ProcessingParameterDescriptor descriptor{ - QString::fromLatin1(key), - QString::fromLatin1(name), - ProcessingParameterType::Real, - value, - minimum, - maximum, - step}; - descriptor.decimals = decimals; - return descriptor; - } - - ProcessingParameterDescriptor booleanParameter(const char* key, - const char* name, - bool value) - { - return {QString::fromLatin1(key), - QString::fromLatin1(name), - ProcessingParameterType::Boolean, - value}; - } - - ProcessingParameterDescriptor choiceParameter( - const char* key, - const char* name, - int value, - std::initializer_list choices) - { - ProcessingParameterDescriptor descriptor{ - QString::fromLatin1(key), - QString::fromLatin1(name), - ProcessingParameterType::Choice, - value}; - int index = 0; - for (const char* choice : choices) - { - descriptor.choices.append({QString::fromLatin1(choice), index++}); - } - return descriptor; - } - - template - bool registerBuiltIn(ProcessingModuleRegistry& registry, - ProcessingModuleDescriptor descriptor) + // A function-local list, so the registrations of other source files can add to it during + // static initialization in any order + std::vector& registrations() { - return registry.registerModule(descriptor, []() - { - return std::make_unique(); - }); + static std::vector list; + return list; } + } - void registerBuiltIns(ProcessingModuleRegistry& registry) - { - registerBuiltIn( - registry, - {QStringLiteral("spatiotemporal_binning"), - QStringLiteral("Spatiotemporal Binning"), - 1, - {integerParameter("spatial_bin_x", "Spatial X", 1, 1, 64), - integerParameter("spatial_bin_y", "Spatial Y", 1, 1, 64), - integerParameter("temporal_bin", "Temporal", 1, 1, 256), - choiceParameter("spatial_mode", "Spatial mode", 0, - {"Mean", "Sum", "Minimum", "Maximum", "Skip"}), - choiceParameter("temporal_mode", "Temporal mode", 0, - {"Mean", "Sum", "Minimum", "Maximum", "Skip"})}, - true}); - - registerBuiltIn( - registry, - {QStringLiteral("gaussian_blur"), - QStringLiteral("Gaussian Blur"), - 1, - {integerParameter("kernel_size", "Kernel size", 3, 1, 99, 2), - realParameter("sigma", "Sigma", 0.0, 0.0, 100.0, 0.1, 2)}}); - - registerBuiltIn( - registry, - {QStringLiteral("fft"), QStringLiteral("FFT"), 1, {}}); - - registerBuiltIn( - registry, - {QStringLiteral("frequency_domain_filter"), - QStringLiteral("Frequency Domain Filter"), - 1, - {choiceParameter("output_mode", "Output", 2, - {"Spectrum", "Filtered spectrum", "Filtered image"}), - realParameter("min_feature_size", "Min feature size", 2.0, - 0.0, 1000.0, 0.1, 2), - realParameter("max_feature_size", "Max feature size", 10.0, - 0.0, 1000.0, 0.1, 2), - choiceParameter("filter_kind", "Filter kind", 0, - {"Smooth", "Hard"})}}); - - registerBuiltIn( - registry, - {QStringLiteral("mask"), - QStringLiteral("Mask"), - 1, - {choiceParameter("shape", "Shape", 0, {"Ellipse", "Rectangle", "Annulus"}), - realParameter("center_x", "Center X", 0.0, -0.5, 0.5, 0.001, 3), - realParameter("center_y", "Center Y", 0.0, -0.5, 0.5, 0.001, 3), - realParameter("size_x", "Size X", 0.1, 0.001, 1.0, 0.001, 3), - realParameter("size_y", "Size Y", 0.1, 0.001, 1.0, 0.001, 3), - realParameter("inner_size", "Inner size", 0.0, 0.0, 1.0, 0.001, 3), - realParameter("rotation", "Rotation", 0.0, -180.0, 180.0, 0.1, 1), - realParameter("edge_width", "Edge width", 0.0, 0.0, 0.5, 0.001, 3), - booleanParameter("invert", "Invert", false)}}); - - registerBuiltIn( - registry, - {QStringLiteral("ifft"), QStringLiteral("IFFT"), 1, {}}); - - registerBuiltIn( - registry, - {QStringLiteral("differential_rolling"), - QStringLiteral("Differential Rolling"), - 1, - {integerParameter("batch_size", "Batch size", 16, 1, 256), - booleanParameter("normalize", "Normalize by batch 1", true)}, - true}); - - registerBuiltIn( - registry, - {QStringLiteral("background_calibration"), - QStringLiteral("Background Calibration"), - 1, - {integerParameter("calibration_frames", "Frames", 101, 3, 1001, 2), - choiceParameter("mode", "Mode", 0, {"Snapshot", "Running"}), - choiceParameter("method", "Method", 0, - {"Median", "Mean", "Maximum", "Minimum"}), - choiceParameter("operation", "Operation", 0, - {"Subtract", "Add", "Multiply", "Divide"})}, - true}); - - registerBuiltIn( - registry, - {QStringLiteral("iscat.processing"), - QStringLiteral("iSCAT Processing"), - 1, - {choiceParameter("mode", "Background mode", 0, - {"Flat Field", "Temporal Median", "Dynamic EMA", - "Snapshot Reference", "Differential Rolling (DRA)"}), - realParameter("blur_sigma", "Blur sigma", 15.0, 1.0, 100.0, 0.5, 1), - integerParameter("median_window", "Median window", 31, 3, 101, 2), - realParameter("ema_alpha", "Smoothing factor", 0.05, 0.001, 0.5, 0.005, 3), - booleanParameter("capture_reference", "Capture Reference", false), - integerParameter("batch_size", "DRA batch size", 16, 1, 500, 1), - realParameter("contrast_gain", "Contrast gain", 20.0, 1.0, 500.0, 1.0, 1), - booleanParameter("enable_high_pass", "Enable High-Pass", false), - realParameter("high_pass_sigma", "High-Pass sigma", 30.0, 2.0, 200.0, 1.0, 1), - choiceParameter("output_mode", "Output mode", 0, - {"Contrast Centered", "Absolute Contrast", "Estimated Background"})}, - true}); - - registerImageOperationModules(registry); - } + bool addProcessingModuleRegistration(ProcessingModuleRegistration registration) + { + registrations().push_back(registration); + return true; } ProcessingModuleRegistry::ProcessingModuleRegistry() { - registerBuiltIns(*this); -#ifdef SCOPEONE_WITH_CUDA - cuda::registerCudaProcessingModules(*this); -#endif -#ifdef SCOPEONE_WITH_INFERENCE - inference::registerInferenceProcessingModules(*this); -#endif + for (const ProcessingModuleRegistration registration : registrations()) + { + registration(*this); + } } ProcessingModuleRegistry::~ProcessingModuleRegistry() = default; @@ -249,18 +64,34 @@ namespace scopeone::core::internal ProcessingModuleDescriptor normalized = descriptor; normalized.id = id; m_entries.insert(id, {std::move(normalized), std::move(factory)}); - m_order.append(id); return true; } + void ProcessingModuleRegistry::registerBuiltIn(const ProcessingModuleDescriptor& descriptor, + Factory factory) + { + // Builds are Release, where Q_ASSERT does nothing, and a module with a repeated id was + // dropped without a word. Stopping at start up shows it to whoever added the module. + if (!registerModule(descriptor, std::move(factory))) + { + qFatal("Processing module %s is registered twice or has an invalid descriptor", + qPrintable(descriptor.id)); + } + } + QList ProcessingModuleRegistry::descriptors() const { QList result; - result.reserve(m_order.size()); - for (const QString& id : m_order) + result.reserve(m_entries.size()); + for (const Entry& entry : m_entries) { - result.append(m_entries.value(id).descriptor); + result.append(entry.descriptor); } + std::sort(result.begin(), result.end(), + [](const ProcessingModuleDescriptor& a, const ProcessingModuleDescriptor& b) + { + return a.name.compare(b.name, Qt::CaseInsensitive) < 0; + }); return result; } diff --git a/ScopeOneCore/src/SpatiotemporalBinningModule.cpp b/ScopeOneCore/src/SpatiotemporalBinningModule.cpp index 50986f8..8491437 100644 --- a/ScopeOneCore/src/SpatiotemporalBinningModule.cpp +++ b/ScopeOneCore/src/SpatiotemporalBinningModule.cpp @@ -1,4 +1,7 @@ #include "internal/SpatiotemporalBinningModule.h" + +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" #include "internal/FrameBufferUtils.h" #include @@ -493,3 +496,20 @@ namespace scopeone::core::internal return true; } } // namespace scopeone::core::internal + +namespace scopeone::core::internal +{ + SCOPEONE_REGISTER_PROCESSING_MODULE( + SpatiotemporalBinningModule, + QStringLiteral("spatiotemporal_binning"), + QStringLiteral("Spatiotemporal Binning"), + 1, + {integerParameter("spatial_bin_x", "Spatial X", 1, 1, 64), + integerParameter("spatial_bin_y", "Spatial Y", 1, 1, 64), + integerParameter("temporal_bin", "Temporal", 1, 1, 256), + choiceParameter("spatial_mode", "Spatial mode", 0, + {"Mean", "Sum", "Minimum", "Maximum", "Skip"}), + choiceParameter("temporal_mode", "Temporal mode", 0, + {"Mean", "Sum", "Minimum", "Maximum", "Skip"})}, + true) +} diff --git a/ScopeOneCore/src/cuda/CudaFrequencyFilterKernels.cu b/ScopeOneCore/src/cuda/CudaFrequencyFilterKernels.cu new file mode 100644 index 0000000..3348945 --- /dev/null +++ b/ScopeOneCore/src/cuda/CudaFrequencyFilterKernels.cu @@ -0,0 +1,243 @@ +#include "internal/cuda/CudaFrequencyFilterKernels.h" +#include "internal/cuda/CudaKernelLaunch.h" + +#include +#include + +#include +#include +#include + +namespace +{ + using scopeone::core::internal::cuda::detail::synchronizeKernel; + + __global__ void frequencyMaskKernel(float2* spectrum, + int spectrumWidth, + int height, + int realWidth, + float minFeatureSize, + float maxFeatureSize, + int filterKind) + { + const int x = blockIdx.x * blockDim.x + threadIdx.x; + const int y = blockIdx.y * blockDim.y + threadIdx.y; + if (x >= spectrumWidth || y >= height) + { + return; + } + + constexpr float twoPi = 6.28318530717958647692f; + const float fx = twoPi * static_cast(x) / static_cast(realWidth); + const float fy = static_cast(y <= height / 2 ? y : y - height) + * twoPi / static_cast(height); + const float radiusSquared = fx * fx + fy * fy; + const float mask = filterKind == 1 + ? (radiusSquared * maxFeatureSize * maxFeatureSize > 1.0f + && radiusSquared * minFeatureSize * minFeatureSize < 1.0f + ? 1.0f + : 0.0f) + : expf(-radiusSquared * minFeatureSize * minFeatureSize * 0.5f) + - expf(-radiusSquared * maxFeatureSize * maxFeatureSize * 0.5f); + spectrum[y * spectrumWidth + x].x *= mask; + spectrum[y * spectrumWidth + x].y *= mask; + } + + __global__ void renderSpectrumKernel(const float2* spectrum, + int spectrumWidth, + int width, + int height, + float* output) + { + const int x = blockIdx.x * blockDim.x + threadIdx.x; + const int y = blockIdx.y * blockDim.y + threadIdx.y; + if (x >= width || y >= height) + { + return; + } + const int sourceX = (x + width / 2) % width; + const int sourceY = (y + height / 2) % height; + float2 value; + if (sourceX <= width / 2) + { + value = spectrum[sourceY * spectrumWidth + sourceX]; + } + else + { + const float2 conjugate = spectrum[((height - sourceY) % height) * spectrumWidth + + width - sourceX]; + value = make_float2(conjugate.x, -conjugate.y); + } + output[y * width + x] = log1pf(hypotf(value.x, value.y)); + } + + __global__ void scaleKernel(float* output, int count, float scale) + { + const int index = blockIdx.x * blockDim.x + threadIdx.x; + if (index < count) + { + output[index] *= scale; + } + } + + __device__ unsigned int orderedFloat(float value) + { + const unsigned int bits = __float_as_uint(value); + return (bits & 0x80000000U) == 0 ? bits | 0x80000000U : ~bits; + } + + __device__ float floatFromOrdered(unsigned int value) + { + const unsigned int bits = (value & 0x80000000U) == 0 ? ~value : value & ~0x80000000U; + return __uint_as_float(bits); + } + + __global__ void reduceMinMaxKernel(const float* input, int count, unsigned int* limits) + { + __shared__ unsigned int blockMinimum; + __shared__ unsigned int blockMaximum; + if (threadIdx.x == 0) + { + blockMinimum = 0xFF800000U; + blockMaximum = 0x007FFFFFU; + } + __syncthreads(); + const int index = blockIdx.x * blockDim.x + threadIdx.x; + if (index < count) + { + const unsigned int value = orderedFloat(input[index]); + atomicMin(&blockMinimum, value); + atomicMax(&blockMaximum, value); + } + __syncthreads(); + if (threadIdx.x == 0) + { + atomicMin(&limits[0], blockMinimum); + atomicMax(&limits[1], blockMaximum); + } + } + + __global__ void minMaxNormalizeKernel(float* output, + int count, + const unsigned int* limits, + float maxValue) + { + const int index = blockIdx.x * blockDim.x + threadIdx.x; + if (index < count) + { + const float minimum = floatFromOrdered(limits[0]); + const float maximum = floatFromOrdered(limits[1]); + output[index] = maximum == minimum + ? 0.0f + : (output[index] - minimum) * maxValue / (maximum - minimum); + } + } + + bool normalizeMinMax(float* output, int count, void* scratch, float maxValue) + { + const unsigned int limits[] = {0xFF800000U, 0x007FFFFFU}; + if (cudaMemcpy(scratch, limits, sizeof(limits), cudaMemcpyHostToDevice) != cudaSuccess) + { + return false; + } + constexpr int blockSize = 256; + reduceMinMaxKernel<<<(count + blockSize - 1) / blockSize, blockSize>>>( + output, + count, + static_cast(scratch)); + minMaxNormalizeKernel<<<(count + blockSize - 1) / blockSize, blockSize>>>( + output, + count, + static_cast(scratch), + maxValue); + return synchronizeKernel(); + } +} + +namespace scopeone::core::internal::cuda::detail +{ + bool launchFrequencyFilter(const void* input, + void* output, + void* spectrum, + int width, + int height, + float minFeatureSize, + float maxFeatureSize, + int filterKind, + int outputMode, + int forwardPlan, + int inversePlan, + void* minMaxScratch, + float maxValue) + { + if (cufftExecR2C(forwardPlan, + static_cast(const_cast(input)), + static_cast(spectrum)) != CUFFT_SUCCESS) + { + return false; + } + + const dim3 block(16, 16); + const dim3 imageGrid((width + block.x - 1) / block.x, + (height + block.y - 1) / block.y); + const int spectrumWidth = width / 2 + 1; + const dim3 grid((spectrumWidth + block.x - 1) / block.x, + (height + block.y - 1) / block.y); + if (outputMode == 0) + { + renderSpectrumKernel<<>>(static_cast(spectrum), + spectrumWidth, + width, + height, + static_cast(output)); + return synchronizeKernel() + && normalizeMinMax(static_cast(output), + width * height, + minMaxScratch, + maxValue); + } + frequencyMaskKernel<<>>( + static_cast(spectrum), + spectrumWidth, + height, + width, + minFeatureSize, + maxFeatureSize, + filterKind); + if (!synchronizeKernel()) + { + return false; + } + + if (outputMode == 1) + { + renderSpectrumKernel<<>>(static_cast(spectrum), + spectrumWidth, + width, + height, + static_cast(output)); + return synchronizeKernel() + && normalizeMinMax(static_cast(output), + width * height, + minMaxScratch, + maxValue); + } + + if (cufftExecC2R(inversePlan, + static_cast(spectrum), + static_cast(output)) != CUFFT_SUCCESS) + { + return false; + } + constexpr int blockSize = 256; + scaleKernel<<<(width * height + blockSize - 1) / blockSize, blockSize>>>( + static_cast(output), + width * height, + 1.0f / static_cast(width * height)); + return synchronizeKernel() + && normalizeMinMax(static_cast(output), + width * height, + minMaxScratch, + maxValue); + } +} diff --git a/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp b/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp index 0006939..514d567 100644 --- a/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp +++ b/ScopeOneCore/src/cuda/CudaFrequencyFilterModule.cpp @@ -1,6 +1,8 @@ #include "internal/cuda/CudaFrequencyFilterModule.h" -#include "internal/cuda/CudaKernelLaunch.h" +#include "internal/cuda/CudaFrequencyFilterKernels.h" +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" #include #include @@ -8,7 +10,7 @@ namespace scopeone::core::internal::cuda { CudaFrequencyFilterModule::CudaFrequencyFilterModule() - : CudaRealImageModule(scopeone::core::internal::cuda::GpuMemoryLayout::Contiguous) + : CudaModule(scopeone::core::internal::cuda::GpuMemoryLayout::Contiguous) { } @@ -25,7 +27,7 @@ namespace scopeone::core::internal::cuda QString CudaFrequencyFilterModule::name() const { - return QStringLiteral("CUDA Frequency Filter"); + return QStringLiteral("Frequency Domain Filter (CUDA)"); } QVariantMap CudaFrequencyFilterModule::parameters() const @@ -67,7 +69,6 @@ namespace scopeone::core::internal::cuda bool CudaFrequencyFilterModule::resetState() { destroyPlans(); - m_spectrum.release(); releaseScratch(); return true; } @@ -90,6 +91,11 @@ namespace scopeone::core::internal::cuda void CudaFrequencyFilterModule::releaseScratch() { + if (m_spectrum) + { + cudaFree(m_spectrum); + m_spectrum = nullptr; + } if (m_minMaxScratch) { cudaFree(m_minMaxScratch); @@ -98,14 +104,22 @@ namespace scopeone::core::internal::cuda } bool CudaFrequencyFilterModule::processDevice( - const scopeone::core::internal::cuda::GpuRealFrame& input, - scopeone::core::internal::cuda::GpuRealFrame& output, + const scopeone::core::internal::cuda::GpuImage& input, + scopeone::core::internal::cuda::GpuImage& output, int bitDepth) { if (m_planWidth != input.width() || m_planHeight != input.height()) { destroyPlans(); - if (!m_spectrum.allocateForRealImage(input.width(), input.height()) + if (m_spectrum) + { + cudaFree(m_spectrum); + m_spectrum = nullptr; + } + // The R2C spectrum of a width x height image holds (width / 2 + 1) x height values + const std::size_t spectrumBytes = static_cast(input.width() / 2 + 1) + * static_cast(input.height()) * sizeof(cufftComplex); + if (cudaMalloc(&m_spectrum, spectrumBytes) != cudaSuccess || cufftPlan2d(&m_forwardPlan, input.height(), input.width(), @@ -131,7 +145,7 @@ namespace scopeone::core::internal::cuda } return scopeone::core::internal::cuda::detail::launchFrequencyFilter(input.data(), output.data(), - m_spectrum.data(), + m_spectrum, input.width(), input.height(), m_minFeatureSize, @@ -144,3 +158,16 @@ namespace scopeone::core::internal::cuda bitDepth >= 16 ? 65535.0f : 255.0f); } } + +SCOPEONE_REGISTER_PROCESSING_MODULE( + scopeone::core::internal::cuda::CudaFrequencyFilterModule, + QStringLiteral("cuda.frequency_filter"), + QStringLiteral("Frequency Domain Filter (CUDA)"), + 1, + {scopeone::core::internal::choiceParameter( + "output_mode", "Output", 2, {"Spectrum", "Filtered spectrum", "Filtered image"}), + scopeone::core::internal::realParameter( + "min_feature_size", "Min feature size", 2.0, 0.0, 1000.0, 0.1, 2), + scopeone::core::internal::realParameter( + "max_feature_size", "Max feature size", 10.0, 0.0, 1000.0, 0.1, 2), + scopeone::core::internal::choiceParameter("filter_kind", "Filter kind", 0, {"Smooth", "Hard"})}) diff --git a/ScopeOneCore/src/cuda/CudaGaussianBlurKernels.cu b/ScopeOneCore/src/cuda/CudaGaussianBlurKernels.cu new file mode 100644 index 0000000..408c897 --- /dev/null +++ b/ScopeOneCore/src/cuda/CudaGaussianBlurKernels.cu @@ -0,0 +1,95 @@ +#include "internal/cuda/CudaGaussianBlurKernels.h" +#include "internal/cuda/CudaKernelLaunch.h" + +#include + +#include +#include + +namespace +{ + __device__ int reflect101(int value, int size) + { + while (value < 0 || value >= size) + { + value = value < 0 ? -value : 2 * size - value - 2; + } + return value; + } + + __global__ void gaussianKernel(const float* input, + std::size_t inputPitchBytes, + float* output, + std::size_t outputPitchBytes, + int width, + int height, + int radius, + float sigma) + { + const int x = blockIdx.x * blockDim.x + threadIdx.x; + const int y = blockIdx.y * blockDim.y + threadIdx.y; + if (x >= width || y >= height) + { + return; + } + + float sum = 0.0f; + float weightSum = 0.0f; + for (int dy = -radius; dy <= radius; ++dy) + { + const int sampleY = reflect101(y + dy, height); + const float* inputRow = reinterpret_cast( + reinterpret_cast(input) + + static_cast(sampleY) * inputPitchBytes); + for (int dx = -radius; dx <= radius; ++dx) + { + const int sampleX = reflect101(x + dx, width); + const float distanceSquared = static_cast(dx * dx + dy * dy); + const float weight = expf(-distanceSquared / (2.0f * sigma * sigma)); + sum += inputRow[sampleX] * weight; + weightSum += weight; + } + } + + float* outputRow = reinterpret_cast( + reinterpret_cast(output) + + static_cast(y) * outputPitchBytes); + outputRow[x] = sum / weightSum; + } +} + +namespace scopeone::core::internal::cuda::detail +{ + bool launchGaussian(const void* input, + std::size_t inputPitchBytes, + void* output, + std::size_t outputPitchBytes, + int width, + int height, + int kernelSize, + float sigma) + { + kernelSize = std::max(1, kernelSize); + if ((kernelSize % 2) == 0) + { + ++kernelSize; + } + const float effectiveSigma = sigma > 0.0f + ? sigma + : 0.3f * ((kernelSize - 1) * 0.5f - 1.0f) + 0.8f; + const int radius = kernelSize / 2; + const dim3 block(16, 16); + const dim3 grid((width + block.x - 1) / block.x, + (height + block.y - 1) / block.y); + gaussianKernel<<>>( + static_cast(input), + inputPitchBytes, + static_cast(output), + outputPitchBytes, + width, + height, + radius, + effectiveSigma); + return synchronizeKernel(); + } +} diff --git a/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp b/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp index 38e66b0..522f5c8 100644 --- a/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp +++ b/ScopeOneCore/src/cuda/CudaGaussianBlurModule.cpp @@ -1,11 +1,13 @@ #include "internal/cuda/CudaGaussianBlurModule.h" -#include "internal/cuda/CudaKernelLaunch.h" +#include "internal/cuda/CudaGaussianBlurKernels.h" +#include "internal/ProcessingModuleRegistry.h" +#include "internal/ProcessingParameters.h" namespace scopeone::core::internal::cuda { CudaGaussianBlurModule::CudaGaussianBlurModule() - : CudaRealImageModule(scopeone::core::internal::cuda::GpuMemoryLayout::Pitched2D) + : CudaModule(scopeone::core::internal::cuda::GpuMemoryLayout::Pitched2D) { } @@ -16,7 +18,7 @@ namespace scopeone::core::internal::cuda QString CudaGaussianBlurModule::name() const { - return QStringLiteral("CUDA Gaussian Blur"); + return QStringLiteral("Gaussian Blur (CUDA)"); } QVariantMap CudaGaussianBlurModule::parameters() const @@ -50,8 +52,8 @@ namespace scopeone::core::internal::cuda } bool CudaGaussianBlurModule::processDevice( - const scopeone::core::internal::cuda::GpuRealFrame& input, - scopeone::core::internal::cuda::GpuRealFrame& output, + const scopeone::core::internal::cuda::GpuImage& input, + scopeone::core::internal::cuda::GpuImage& output, int) { return scopeone::core::internal::cuda::detail::launchGaussian(input.data(), @@ -64,3 +66,11 @@ namespace scopeone::core::internal::cuda m_sigma); } } + +SCOPEONE_REGISTER_PROCESSING_MODULE( + scopeone::core::internal::cuda::CudaGaussianBlurModule, + QStringLiteral("cuda.gaussian_blur"), + QStringLiteral("Gaussian Blur (CUDA)"), + 1, + {scopeone::core::internal::integerParameter("kernel_size", "Kernel size", 3, 1, 99, 2), + scopeone::core::internal::realParameter("sigma", "Sigma", 0.0, 0.0, 100.0, 0.1, 2)}) diff --git a/ScopeOneCore/src/cuda/CudaKernels.cu b/ScopeOneCore/src/cuda/CudaKernels.cu index f783b0b..0ac783b 100644 --- a/ScopeOneCore/src/cuda/CudaKernels.cu +++ b/ScopeOneCore/src/cuda/CudaKernels.cu @@ -1,10 +1,7 @@ #include "internal/cuda/CudaKernelLaunch.h" #include -#include -#include -#include #include namespace @@ -45,7 +42,9 @@ namespace int destinationStride, int width, int height, - int destinationBytesPerPixel) + int destinationBytesPerPixel, + float scale, + float offset) { const int x = blockIdx.x * blockDim.x + threadIdx.x; const int y = blockIdx.y * blockDim.y + threadIdx.y; @@ -59,7 +58,7 @@ namespace + static_cast(y) * sourcePitchBytes); unsigned char* destinationRow = destination + static_cast(y) * destinationStride; - const float value = fminf(fmaxf(sourceRow[x], 0.0f), 65535.0f); + const float value = fminf(fmaxf(sourceRow[x] * scale + offset, 0.0f), 65535.0f); if (destinationBytesPerPixel == 1) { destinationRow[x] = static_cast(fminf(value, 255.0f) + 0.5f); @@ -70,205 +69,15 @@ namespace static_cast(value + 0.5f); } } +} - __device__ int reflect101(int value, int size) - { - while (value < 0 || value >= size) - { - value = value < 0 ? -value : 2 * size - value - 2; - } - return value; - } - - __global__ void gaussianKernel(const float* input, - std::size_t inputPitchBytes, - float* output, - std::size_t outputPitchBytes, - int width, - int height, - int radius, - float sigma) - { - const int x = blockIdx.x * blockDim.x + threadIdx.x; - const int y = blockIdx.y * blockDim.y + threadIdx.y; - if (x >= width || y >= height) - { - return; - } - - float sum = 0.0f; - float weightSum = 0.0f; - for (int dy = -radius; dy <= radius; ++dy) - { - const int sampleY = reflect101(y + dy, height); - const float* inputRow = reinterpret_cast( - reinterpret_cast(input) - + static_cast(sampleY) * inputPitchBytes); - for (int dx = -radius; dx <= radius; ++dx) - { - const int sampleX = reflect101(x + dx, width); - const float distanceSquared = static_cast(dx * dx + dy * dy); - const float weight = expf(-distanceSquared / (2.0f * sigma * sigma)); - sum += inputRow[sampleX] * weight; - weightSum += weight; - } - } - - float* outputRow = reinterpret_cast( - reinterpret_cast(output) - + static_cast(y) * outputPitchBytes); - outputRow[x] = sum / weightSum; - } - - __global__ void frequencyMaskKernel(float2* spectrum, - int spectrumWidth, - int height, - int realWidth, - float minFeatureSize, - float maxFeatureSize, - int filterKind) - { - const int x = blockIdx.x * blockDim.x + threadIdx.x; - const int y = blockIdx.y * blockDim.y + threadIdx.y; - if (x >= spectrumWidth || y >= height) - { - return; - } - - constexpr float twoPi = 6.28318530717958647692f; - const float fx = twoPi * static_cast(x) / static_cast(realWidth); - const float fy = static_cast(y <= height / 2 ? y : y - height) - * twoPi / static_cast(height); - const float radiusSquared = fx * fx + fy * fy; - const float mask = filterKind == 1 - ? (radiusSquared * maxFeatureSize * maxFeatureSize > 1.0f - && radiusSquared * minFeatureSize * minFeatureSize < 1.0f - ? 1.0f - : 0.0f) - : expf(-radiusSquared * minFeatureSize * minFeatureSize * 0.5f) - - expf(-radiusSquared * maxFeatureSize * maxFeatureSize * 0.5f); - spectrum[y * spectrumWidth + x].x *= mask; - spectrum[y * spectrumWidth + x].y *= mask; - } - - __global__ void renderSpectrumKernel(const float2* spectrum, - int spectrumWidth, - int width, - int height, - float* output) - { - const int x = blockIdx.x * blockDim.x + threadIdx.x; - const int y = blockIdx.y * blockDim.y + threadIdx.y; - if (x >= width || y >= height) - { - return; - } - const int sourceX = (x + width / 2) % width; - const int sourceY = (y + height / 2) % height; - float2 value; - if (sourceX <= width / 2) - { - value = spectrum[sourceY * spectrumWidth + sourceX]; - } - else - { - const float2 conjugate = spectrum[((height - sourceY) % height) * spectrumWidth - + width - sourceX]; - value = make_float2(conjugate.x, -conjugate.y); - } - output[y * width + x] = log1pf(hypotf(value.x, value.y)); - } - - __global__ void scaleKernel(float* output, int count, float scale) - { - const int index = blockIdx.x * blockDim.x + threadIdx.x; - if (index < count) - { - output[index] *= scale; - } - } - - __device__ unsigned int orderedFloat(float value) - { - const unsigned int bits = __float_as_uint(value); - return (bits & 0x80000000U) == 0 ? bits | 0x80000000U : ~bits; - } - - __device__ float floatFromOrdered(unsigned int value) - { - const unsigned int bits = (value & 0x80000000U) == 0 ? ~value : value & ~0x80000000U; - return __uint_as_float(bits); - } - - __global__ void reduceMinMaxKernel(const float* input, int count, unsigned int* limits) - { - __shared__ unsigned int blockMinimum; - __shared__ unsigned int blockMaximum; - if (threadIdx.x == 0) - { - blockMinimum = 0xFF800000U; - blockMaximum = 0x007FFFFFU; - } - __syncthreads(); - const int index = blockIdx.x * blockDim.x + threadIdx.x; - if (index < count) - { - const unsigned int value = orderedFloat(input[index]); - atomicMin(&blockMinimum, value); - atomicMax(&blockMaximum, value); - } - __syncthreads(); - if (threadIdx.x == 0) - { - atomicMin(&limits[0], blockMinimum); - atomicMax(&limits[1], blockMaximum); - } - } - - __global__ void minMaxNormalizeKernel(float* output, - int count, - const unsigned int* limits, - float maxValue) - { - const int index = blockIdx.x * blockDim.x + threadIdx.x; - if (index < count) - { - const float minimum = floatFromOrdered(limits[0]); - const float maximum = floatFromOrdered(limits[1]); - output[index] = maximum == minimum - ? 0.0f - : (output[index] - minimum) * maxValue / (maximum - minimum); - } - } - +namespace scopeone::core::internal::cuda::detail +{ bool synchronizeKernel() { return cudaGetLastError() == cudaSuccess && cudaDeviceSynchronize() == cudaSuccess; } - bool normalizeMinMax(float* output, int count, void* scratch, float maxValue) - { - const unsigned int limits[] = {0xFF800000U, 0x007FFFFFU}; - if (cudaMemcpy(scratch, limits, sizeof(limits), cudaMemcpyHostToDevice) != cudaSuccess) - { - return false; - } - constexpr int blockSize = 256; - reduceMinMaxKernel<<<(count + blockSize - 1) / blockSize, blockSize>>>( - output, - count, - static_cast(scratch)); - minMaxNormalizeKernel<<<(count + blockSize - 1) / blockSize, blockSize>>>( - output, - count, - static_cast(scratch), - maxValue); - return synchronizeKernel(); - } -} - -namespace scopeone::core::internal::cuda::detail -{ bool convertToFloat(const void* source, int sourceStride, int width, @@ -297,7 +106,9 @@ namespace scopeone::core::internal::cuda::detail int destinationStride, int width, int height, - int destinationBytesPerPixel) + int destinationBytesPerPixel, + float scale, + float offset) { const dim3 block(16, 16); const dim3 grid((width + block.x - 1) / block.x, @@ -309,125 +120,9 @@ namespace scopeone::core::internal::cuda::detail destinationStride, width, height, - destinationBytesPerPixel); + destinationBytesPerPixel, + scale, + offset); return synchronizeKernel(); } - - bool launchGaussian(const void* input, - std::size_t inputPitchBytes, - void* output, - std::size_t outputPitchBytes, - int width, - int height, - int kernelSize, - float sigma) - { - kernelSize = std::max(1, kernelSize); - if ((kernelSize % 2) == 0) - { - ++kernelSize; - } - const float effectiveSigma = sigma > 0.0f - ? sigma - : 0.3f * ((kernelSize - 1) * 0.5f - 1.0f) + 0.8f; - const int radius = kernelSize / 2; - const dim3 block(16, 16); - const dim3 grid((width + block.x - 1) / block.x, - (height + block.y - 1) / block.y); - gaussianKernel<<>>( - static_cast(input), - inputPitchBytes, - static_cast(output), - outputPitchBytes, - width, - height, - radius, - effectiveSigma); - return synchronizeKernel(); - } - - bool launchFrequencyFilter(const void* input, - void* output, - void* spectrum, - int width, - int height, - float minFeatureSize, - float maxFeatureSize, - int filterKind, - int outputMode, - int forwardPlan, - int inversePlan, - void* minMaxScratch, - float maxValue) - { - if (cufftExecR2C(forwardPlan, - static_cast(const_cast(input)), - static_cast(spectrum)) != CUFFT_SUCCESS) - { - return false; - } - - const dim3 block(16, 16); - const dim3 imageGrid((width + block.x - 1) / block.x, - (height + block.y - 1) / block.y); - const int spectrumWidth = width / 2 + 1; - const dim3 grid((spectrumWidth + block.x - 1) / block.x, - (height + block.y - 1) / block.y); - if (outputMode == 0) - { - renderSpectrumKernel<<>>(static_cast(spectrum), - spectrumWidth, - width, - height, - static_cast(output)); - return synchronizeKernel() - && normalizeMinMax(static_cast(output), - width * height, - minMaxScratch, - maxValue); - } - frequencyMaskKernel<<>>( - static_cast(spectrum), - spectrumWidth, - height, - width, - minFeatureSize, - maxFeatureSize, - filterKind); - if (!synchronizeKernel()) - { - return false; - } - - if (outputMode == 1) - { - renderSpectrumKernel<<>>(static_cast(spectrum), - spectrumWidth, - width, - height, - static_cast(output)); - return synchronizeKernel() - && normalizeMinMax(static_cast(output), - width * height, - minMaxScratch, - maxValue); - } - - if (cufftExecC2R(inversePlan, - static_cast(spectrum), - static_cast(output)) != CUFFT_SUCCESS) - { - return false; - } - constexpr int blockSize = 256; - scaleKernel<<<(width * height + blockSize - 1) / blockSize, blockSize>>>( - static_cast(output), - width * height, - 1.0f / static_cast(width * height)); - return synchronizeKernel() - && normalizeMinMax(static_cast(output), - width * height, - minMaxScratch, - maxValue); - } } diff --git a/ScopeOneCore/src/cuda/CudaRealImageModule.cpp b/ScopeOneCore/src/cuda/CudaModule.cpp similarity index 55% rename from ScopeOneCore/src/cuda/CudaRealImageModule.cpp rename to ScopeOneCore/src/cuda/CudaModule.cpp index 028bdb0..cf189d0 100644 --- a/ScopeOneCore/src/cuda/CudaRealImageModule.cpp +++ b/ScopeOneCore/src/cuda/CudaModule.cpp @@ -1,4 +1,4 @@ -#include "internal/cuda/CudaRealImageModule.h" +#include "internal/cuda/CudaModule.h" #include "internal/cuda/CudaDevice.h" @@ -6,14 +6,14 @@ namespace scopeone::core::internal::cuda { - CudaRealImageModule::CudaRealImageModule(GpuMemoryLayout layout) + CudaModule::CudaModule(GpuMemoryLayout layout) : m_layout(layout) { } - CudaRealImageModule::~CudaRealImageModule() = default; + CudaModule::~CudaModule() = default; - scopeone::core::ProcessingResult CudaRealImageModule::process( + scopeone::core::ProcessingResult CudaModule::process( const scopeone::core::ImageFrame& frame, int processingBitDepth) { @@ -25,17 +25,29 @@ namespace scopeone::core::internal::cuda { return {scopeone::core::ImageFrame{}, QStringLiteral("CUDA device unavailable")}; } - if (!m_inputBuffer.isValid() - || m_inputBuffer.width() != frame.width - || m_inputBuffer.height() != frame.height - || m_inputBuffer.layout() != m_layout) + const GpuImageSize size = outputSize(frame.width, frame.height); + if (size.width <= 0 || size.height <= 0) { - if (!m_inputBuffer.allocate(frame.width, frame.height, m_layout) - || !m_outputBuffer.allocate(frame.width, frame.height, m_layout)) - { - return {scopeone::core::ImageFrame{}, - QStringLiteral("CUDA frame allocation failed")}; - } + return {scopeone::core::ImageFrame{}, + QStringLiteral("Frame too small for this module")}; + } + if ((!m_inputBuffer.isValid() + || m_inputBuffer.width() != frame.width + || m_inputBuffer.height() != frame.height + || m_inputBuffer.layout() != m_layout) + && !m_inputBuffer.allocate(frame.width, frame.height, m_layout)) + { + return {scopeone::core::ImageFrame{}, + QStringLiteral("CUDA frame allocation failed")}; + } + if ((!m_outputBuffer.isValid() + || m_outputBuffer.width() != size.width + || m_outputBuffer.height() != size.height + || m_outputBuffer.layout() != m_layout) + && !m_outputBuffer.allocate(size.width, size.height, m_layout)) + { + return {scopeone::core::ImageFrame{}, + QStringLiteral("CUDA frame allocation failed")}; } if (!m_inputBuffer.upload(frame)) { @@ -50,8 +62,8 @@ namespace scopeone::core::internal::cuda scopeone::core::ImageFrame output; output.cameraId = frame.cameraId; - output.width = frame.width; - output.height = frame.height; + output.width = size.width; + output.height = size.height; output.pixelFormat = processingBitDepth >= 16 ? scopeone::core::ImagePixelFormat::Mono16 : scopeone::core::ImagePixelFormat::Mono8; @@ -64,7 +76,8 @@ namespace scopeone::core::internal::cuda output.sourceRoiWidth = frame.sourceRoiWidth; output.sourceRoiHeight = frame.sourceRoiHeight; output.bytes.resize(static_cast(output.payloadByteCount())); - if (!m_outputBuffer.download(output)) + const GpuOutputMapping mapping = outputMapping(processingBitDepth); + if (!m_outputBuffer.download(output, mapping.scale, mapping.offset)) { return {scopeone::core::ImageFrame{}, QStringLiteral("CUDA output download failed")}; diff --git a/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp b/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp deleted file mode 100644 index 5978b11..0000000 --- a/ScopeOneCore/src/cuda/CudaProcessingRegistration.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "internal/cuda/CudaProcessingRegistration.h" -#include "internal/cuda/CudaFrequencyFilterModule.h" -#include "internal/cuda/CudaGaussianBlurModule.h" - -#include "internal/ProcessingModuleRegistry.h" - -namespace scopeone::core::internal::cuda -{ - namespace - { - QList descriptors() - { - return {{QStringLiteral("cuda.gaussian_blur"), - QStringLiteral("CUDA Gaussian Blur"), - 1, - {{QStringLiteral("kernel_size"), - QStringLiteral("Kernel size"), - scopeone::core::ProcessingParameterType::Integer, - 3, - 1, - 99, - 2, - 0}, - {QStringLiteral("sigma"), - QStringLiteral("Sigma"), - scopeone::core::ProcessingParameterType::Real, - 0.0, - 0.0, - 100.0, - 0.1, - 2}}}, - {QStringLiteral("cuda.frequency_filter"), - QStringLiteral("CUDA Frequency Filter"), - 1, - {{QStringLiteral("output_mode"), - QStringLiteral("Output"), - core::ProcessingParameterType::Choice, - 2, - 0, - 2, - 1, - 0, - {{QStringLiteral("Spectrum"), 0}, - {QStringLiteral("Filtered spectrum"), 1}, - {QStringLiteral("Filtered image"), 2}}}, - {QStringLiteral("min_feature_size"), - QStringLiteral("Min feature size"), - core::ProcessingParameterType::Real, - 2.0, - 0.0, - 1000.0, - 0.1, - 2}, - {QStringLiteral("max_feature_size"), - QStringLiteral("Max feature size"), - core::ProcessingParameterType::Real, - 10.0, - 0.0, - 1000.0, - 0.1, - 2}, - {QStringLiteral("filter_kind"), - QStringLiteral("Filter kind"), - core::ProcessingParameterType::Choice, - 0, - 0, - 1, - 1, - 0, - {{QStringLiteral("Smooth"), 0}, - {QStringLiteral("Hard"), 1}}}}}}; - } - - std::unique_ptr create(const QString& moduleId) - { - if (moduleId == QStringLiteral("cuda.gaussian_blur")) - { - return std::make_unique(); - } - if (moduleId == QStringLiteral("cuda.frequency_filter")) - { - return std::make_unique(); - } - return {}; - } - } - - void registerCudaProcessingModules(core::internal::ProcessingModuleRegistry& registry) - { - for (const core::ProcessingModuleDescriptor& descriptor : descriptors()) - { - const QString id = descriptor.id; - registry.registerModule(descriptor, [id]() { return create(id); }); - } - } -} diff --git a/ScopeOneCore/src/cuda/GpuComplexFrame.cpp b/ScopeOneCore/src/cuda/GpuComplexFrame.cpp deleted file mode 100644 index 60a823b..0000000 --- a/ScopeOneCore/src/cuda/GpuComplexFrame.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "internal/cuda/GpuComplexFrame.h" - -#include - -#include - -namespace scopeone::core::internal::cuda -{ - GpuComplexFrame::~GpuComplexFrame() - { - release(); - } - - GpuComplexFrame::GpuComplexFrame(GpuComplexFrame&& other) noexcept - : m_data(std::exchange(other.m_data, nullptr)) - , m_realWidth(std::exchange(other.m_realWidth, 0)) - , m_spectrumWidth(std::exchange(other.m_spectrumWidth, 0)) - , m_height(std::exchange(other.m_height, 0)) - { - } - - GpuComplexFrame& GpuComplexFrame::operator=(GpuComplexFrame&& other) noexcept - { - if (this != &other) - { - release(); - m_data = std::exchange(other.m_data, nullptr); - m_realWidth = std::exchange(other.m_realWidth, 0); - m_spectrumWidth = std::exchange(other.m_spectrumWidth, 0); - m_height = std::exchange(other.m_height, 0); - } - return *this; - } - - bool GpuComplexFrame::allocateForRealImage(int width, int height) - { - if (isValid() && m_realWidth == width && m_height == height) - { - return true; - } - - release(); - m_realWidth = width; - m_spectrumWidth = width / 2 + 1; - m_height = height; - const std::size_t bytes = static_cast(m_spectrumWidth) - * static_cast(m_height) - * sizeof(float2); - if (cudaMalloc(&m_data, bytes) != cudaSuccess) - { - release(); - return false; - } - return true; - } - - void GpuComplexFrame::release() - { - if (m_data) - { - cudaFree(m_data); - } - m_data = nullptr; - m_realWidth = 0; - m_spectrumWidth = 0; - m_height = 0; - } - - bool GpuComplexFrame::isValid() const - { - return m_data != nullptr - && m_realWidth > 0 - && m_spectrumWidth > 0 - && m_height > 0; - } - - void* GpuComplexFrame::data() const - { - return m_data; - } - - int GpuComplexFrame::realWidth() const - { - return m_realWidth; - } - - int GpuComplexFrame::spectrumWidth() const - { - return m_spectrumWidth; - } - - int GpuComplexFrame::height() const - { - return m_height; - } - - std::size_t GpuComplexFrame::elementCount() const - { - return static_cast(m_spectrumWidth) - * static_cast(m_height); - } -} diff --git a/ScopeOneCore/src/cuda/GpuRealFrame.cpp b/ScopeOneCore/src/cuda/GpuImage.cpp similarity index 86% rename from ScopeOneCore/src/cuda/GpuRealFrame.cpp rename to ScopeOneCore/src/cuda/GpuImage.cpp index dbe8518..a4d0278 100644 --- a/ScopeOneCore/src/cuda/GpuRealFrame.cpp +++ b/ScopeOneCore/src/cuda/GpuImage.cpp @@ -1,4 +1,4 @@ -#include "internal/cuda/GpuRealFrame.h" +#include "internal/cuda/GpuImage.h" #include "internal/cuda/CudaKernelLaunch.h" @@ -8,17 +8,17 @@ namespace scopeone::core::internal::cuda { - GpuRealFrame::GpuRealFrame(int width, int height, GpuMemoryLayout layout) + GpuImage::GpuImage(int width, int height, GpuMemoryLayout layout) { allocate(width, height, layout); } - GpuRealFrame::~GpuRealFrame() + GpuImage::~GpuImage() { release(); } - GpuRealFrame::GpuRealFrame(GpuRealFrame&& other) noexcept + GpuImage::GpuImage(GpuImage&& other) noexcept : m_data(std::exchange(other.m_data, nullptr)) , m_pitchBytes(std::exchange(other.m_pitchBytes, 0)) , m_width(std::exchange(other.m_width, 0)) @@ -31,7 +31,7 @@ namespace scopeone::core::internal::cuda { } - GpuRealFrame& GpuRealFrame::operator=(GpuRealFrame&& other) noexcept + GpuImage& GpuImage::operator=(GpuImage&& other) noexcept { if (this != &other) { @@ -49,7 +49,7 @@ namespace scopeone::core::internal::cuda return *this; } - bool GpuRealFrame::allocate(int width, int height, GpuMemoryLayout layout) + bool GpuImage::allocate(int width, int height, GpuMemoryLayout layout) { if (isValid() && m_width == width && m_height == height && m_layout == layout) { @@ -84,7 +84,7 @@ namespace scopeone::core::internal::cuda return true; } - void GpuRealFrame::release() + void GpuImage::release() { if (m_data) { @@ -108,37 +108,37 @@ namespace scopeone::core::internal::cuda m_downloadCapacity = 0; } - bool GpuRealFrame::isValid() const + bool GpuImage::isValid() const { return m_data != nullptr && m_width > 0 && m_height > 0 && m_pitchBytes > 0; } - void* GpuRealFrame::data() const + void* GpuImage::data() const { return m_data; } - std::size_t GpuRealFrame::pitchBytes() const + std::size_t GpuImage::pitchBytes() const { return m_pitchBytes; } - int GpuRealFrame::width() const + int GpuImage::width() const { return m_width; } - int GpuRealFrame::height() const + int GpuImage::height() const { return m_height; } - GpuMemoryLayout GpuRealFrame::layout() const + GpuMemoryLayout GpuImage::layout() const { return m_layout; } - bool GpuRealFrame::ensureUploadBuffer(std::size_t bytes) + bool GpuImage::ensureUploadBuffer(std::size_t bytes) { if (m_uploadCapacity >= bytes) { @@ -158,7 +158,7 @@ namespace scopeone::core::internal::cuda return true; } - bool GpuRealFrame::ensureDownloadBuffer(std::size_t bytes) const + bool GpuImage::ensureDownloadBuffer(std::size_t bytes) const { if (m_downloadCapacity >= bytes) { @@ -178,7 +178,7 @@ namespace scopeone::core::internal::cuda return true; } - bool GpuRealFrame::upload(const scopeone::core::ImageFrame& frame) + bool GpuImage::upload(const scopeone::core::ImageFrame& frame) { if (!frame.isValid()) { @@ -216,7 +216,7 @@ namespace scopeone::core::internal::cuda m_pitchBytes); } - bool GpuRealFrame::download(scopeone::core::ImageFrame& frame) const + bool GpuImage::download(scopeone::core::ImageFrame& frame, float scale, float offset) const { if (!isValid() || frame.width != m_width @@ -236,7 +236,9 @@ namespace scopeone::core::internal::cuda frame.stride, frame.width, frame.height, - frame.bytesPerPixel())) + frame.bytesPerPixel(), + scale, + offset)) { return false; } diff --git a/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp b/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp index 40ea58e..6974f09 100644 --- a/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp +++ b/ScopeOneCore/src/inference/InferenceProcessingRegistration.cpp @@ -1,4 +1,3 @@ -#include "internal/inference/InferenceProcessingRegistration.h" #include "internal/inference/OnnxInferenceModule.h" #include "internal/ProcessingModuleRegistry.h" @@ -97,14 +96,19 @@ namespace scopeone::core::internal::inference } return {}; } - } - void registerInferenceProcessingModules(core::internal::ProcessingModuleRegistry& registry) - { - for (const core::ProcessingModuleDescriptor& descriptor : descriptors()) + // The descriptor asks ONNX Runtime for its providers, so it is made when a registry is + // built and not while Core is loaded + void registerInferenceProcessingModules(core::internal::ProcessingModuleRegistry& registry) { - const QString id = descriptor.id; - registry.registerModule(descriptor, [id]() { return create(id); }); + for (const core::ProcessingModuleDescriptor& descriptor : descriptors()) + { + const QString id = descriptor.id; + registry.registerBuiltIn(descriptor, [id]() { return create(id); }); + } } + + [[maybe_unused]] const bool registered = + core::internal::addProcessingModuleRegistration(registerInferenceProcessingModules); } } diff --git a/src/AppVersion.h.in b/src/AppVersion.h.in index f983e60..cb93adf 100644 --- a/src/AppVersion.h.in +++ b/src/AppVersion.h.in @@ -1,10 +1,4 @@ #pragma once - - - -#define SCOPEONE_APP_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ -#define SCOPEONE_APP_VERSION_MINOR @PROJECT_VERSION_MINOR@ -#define SCOPEONE_APP_VERSION_PATCH @PROJECT_VERSION_PATCH@ #define SCOPEONE_APP_VERSION_STRING "@PROJECT_VERSION@" #define SCOPEONE_APP_NAME "@PROJECT_NAME@" diff --git a/src/ImageProcessingWidget.cpp b/src/ImageProcessingWidget.cpp index 85906c5..4751b38 100644 --- a/src/ImageProcessingWidget.cpp +++ b/src/ImageProcessingWidget.cpp @@ -799,39 +799,10 @@ namespace scopeone::ui listRow->addWidget(m_moduleList, 1); auto* controls = new QVBoxLayout; m_moduleTypeCombo = new QComboBox(group); - // The steps are listed by group, and the box finds them by any part of the group or the name - static const QList> groups{ - {QStringLiteral("Filters"), {QStringLiteral("gaussian_blur"), QStringLiteral("filter"), QStringLiteral("sharpen"), - QStringLiteral("find_edges"), QStringLiteral("unsharp_mask"), QStringLiteral("top_hat")}}, - {QStringLiteral("Intensity"), {QStringLiteral("math"), QStringLiteral("invert"), QStringLiteral("threshold"), - QStringLiteral("clahe"), QStringLiteral("equalize"), - QStringLiteral("background_calibration")}}, - {QStringLiteral("Binary"), {QStringLiteral("binary"), QStringLiteral("distance_map"), QStringLiteral("watershed"), - QStringLiteral("mask")}}, - {QStringLiteral("Geometry"), {QStringLiteral("flip"), QStringLiteral("rotate_90"), - QStringLiteral("spatiotemporal_binning")}}, - {QStringLiteral("Stack"), {QStringLiteral("rolling_projection"), QStringLiteral("differential_rolling")}}, - {QStringLiteral("Fourier"), {QStringLiteral("fft"), QStringLiteral("frequency_domain_filter"), - QStringLiteral("ifft")}}}; - QList remaining = m_scopeonecore->availableProcessingModules(); - for (const auto& group : groups) - { - for (const QString& id : group.second) - { - for (qsizetype index = 0; index < remaining.size(); ++index) - { - if (remaining.at(index).id == id) - { - m_moduleTypeCombo->addItem(tr("%1 / %2").arg(group.first, remaining.at(index).name), id); - remaining.removeAt(index); - break; - } - } - } - } - for (const ProcessingModuleDescriptor& descriptor : remaining) + // The steps are listed by name as Core sorts them, and the box finds them by any part of the name + for (const ProcessingModuleDescriptor& descriptor : m_scopeonecore->availableProcessingModules()) { - m_moduleTypeCombo->addItem(tr("Other / %1").arg(descriptor.name), descriptor.id); + m_moduleTypeCombo->addItem(descriptor.name, descriptor.id); } m_moduleTypeCombo->setEditable(true); m_moduleTypeCombo->setInsertPolicy(QComboBox::NoInsert); From 3296fa9005080d73761deb5502c55eee1896be27 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:37:27 +0100 Subject: [PATCH 26/49] Pick up Core sources from their folders so a new module needs no CMake change --- ScopeOneCore/CMakeLists.txt | 112 +++--------------------------------- ScopeOneCore/README.md | 4 +- 2 files changed, 9 insertions(+), 107 deletions(-) diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index bcf63ff..a99d23b 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -69,98 +69,11 @@ function(scopeone_add_runtime_copy_commands target destination) endforeach () endfunction() -set(CORE_SOURCES - src/ExperimentDocument.cpp - src/AcquisitionEngine.cpp - src/DriverHostProviderProxy.cpp - src/DaqDeviceManager.cpp - src/DriverManifest.cpp - src/HardwareRuntime.cpp - src/MicroManagerProvider.cpp - src/ImageOps.cpp - src/ImageSceneModel.cpp - src/MMCoreManager.cpp - src/FrameBufferUtils.cpp - src/ParticleAnalysis.cpp - src/StageMosaicManager.cpp - src/ImageProcessingFramework.cpp - src/ProcessingPipeline.cpp - src/DhmReconstruction.cpp - src/ProcessingModuleRegistry.cpp - src/SpatiotemporalBinningModule.cpp - src/GaussianBlurModule.cpp - src/ImageOperationModules.cpp - src/FFTModule.cpp - src/FrequencyDomainFilterModule.cpp - src/MaskModule.cpp - src/IFFTModule.cpp - src/BackgroundCalibrationModule.cpp - src/DifferentialRollingModule.cpp - src/IscatModule.cpp - src/PythonProcessingModule.cpp - src/PtuFile.cpp - src/HardwareStreamCodec.cpp - src/StreamRing.cpp - src/MDAManager.cpp - src/RecordingManager.cpp - src/CameraBackend.cpp - src/CameraManager.cpp - src/NativeCameraBackend.cpp - src/DriverHostCameraBackend.cpp - src/ScopeOneCore.cpp - src/ScanImageAssembler.cpp - src/SignalSourceManager.cpp -) - -set(CORE_HEADERS - include/scopeone/ExperimentDocument.h - include/scopeone/DriverManifest.h - include/scopeone/ProcessingTypes.h - internal/ScanImageAssembler.h - include/scopeone/SharedFrame.h - include/scopeone/DhmReconstruction.h - internal/AcquisitionEngine.h - internal/CameraRuntimeControl.h - include/scopeone/ImageOps.h - include/scopeone/ImageSceneModel.h - internal/HardwareRuntime.h - internal/MicroManagerProvider.h - internal/MMCoreManager.h - internal/FrameBufferUtils.h - internal/ParticleAnalysis.h - internal/StageMosaicManager.h - internal/ImageProcessingFramework.h - internal/ProcessingModule.h - internal/ProcessingModuleRegistry.h - internal/ProcessingParameters.h - include/scopeone/ProcessingPipeline.h - internal/DriverHostProtocol.h - internal/SharedFrameRing.h - internal/HardwareStreamCodec.h - internal/StreamRing.h - internal/DriverHostProviderProxy.h - internal/DaqDeviceManager.h - internal/SignalSourceManager.h - internal/SpatiotemporalBinningModule.h - internal/GaussianBlurModule.h - internal/ImageOperationModules.h - internal/FFTModule.h - internal/FrequencyDomainFilterModule.h - internal/MaskModule.h - internal/IFFTModule.h - internal/BackgroundCalibrationModule.h - internal/DifferentialRollingModule.h - internal/IscatModule.h - internal/PythonProcessingModule.h - internal/PtuFile.h - internal/MDAManager.h - internal/RecordingManager.h - internal/CameraBackend.h - internal/CameraManager.h - include/scopeone/scopeone_core_export.h - include/scopeone/RecordingSession.h - include/scopeone/ScopeOneCore.h -) +# Every source of Core is picked up from its folder, so a new file, such as a processing module that +# registers itself, needs no change here. DriverHostMain.cpp belongs to ScopeOne_DriverHost. +file(GLOB CORE_SOURCES CONFIGURE_DEPENDS src/*.cpp) +list(REMOVE_ITEM CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/DriverHostMain.cpp") +file(GLOB CORE_HEADERS CONFIGURE_DEPENDS internal/*.h include/scopeone/*.h) # The driver SDK: the only headers and the only library a driver sees. It is a library of its # own so a driver that reaches for anything else of Core does not compile or link. @@ -278,7 +191,6 @@ if (SCOPEONE_CUDA_NVCC_EXECUTABLE) endif () if (SCOPEONE_CUDA_NVCC_EXECUTABLE AND TARGET CUDA::cufft) set(SCOPEONE_CUDA_ARCHITECTURES "50;75;80;86;89" CACHE STRING "Target CUDA architectures") - # A new CUDA module is a new file in src/cuda and internal/cuda, picked up without editing this list file(GLOB _cuda_sources CONFIGURE_DEPENDS src/cuda/*.cpp internal/cuda/*.h) file(GLOB _cuda_kernels CONFIGURE_DEPENDS src/cuda/*.cu) target_sources(ScopeOneCore PRIVATE ${_cuda_sources} ${_cuda_kernels}) @@ -383,18 +295,8 @@ find_library(ONNXRUNTIME_LIBRARY NO_DEFAULT_PATH ) if (ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRARY) - target_sources(ScopeOneCore PRIVATE - src/inference/InferenceProcessingRegistration.cpp - src/inference/OnnxInferenceModule.cpp - src/inference/OnnxRuntime.cpp - src/inference/OnnxSession.cpp - internal/inference/ExecutionProvider.h - internal/inference/OnnxInferenceModule.h - internal/inference/OnnxInternal.h - internal/inference/OnnxRuntime.h - internal/inference/OnnxSession.h - internal/inference/TensorInfo.h - ) + file(GLOB _inference_sources CONFIGURE_DEPENDS src/inference/*.cpp internal/inference/*.h) + target_sources(ScopeOneCore PRIVATE ${_inference_sources}) target_include_directories(ScopeOneCore PRIVATE "${ONNXRUNTIME_INCLUDE_DIR}") target_link_libraries(ScopeOneCore PRIVATE "${ONNXRUNTIME_LIBRARY}") set(_with_inference ON) diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 50b3392..4d0cb57 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -140,7 +140,7 @@ SCOPEONE_REGISTER_PROCESSING_MODULE( realParameter("sigma", "Sigma", 0.0, 0.0, 100.0, 0.1, 2)}) ``` -The arguments after the class are its `ProcessingModuleDescriptor`: the id that recipes store, the name shown in the processing list, the schema version and the parameters, built with `integerParameter`, `realParameter`, `booleanParameter` and `choiceParameter` from `internal/ProcessingParameters.h`. A file that builds several modules from one table, or a descriptor that depends on the machine such as the ONNX providers, passes a function to `addProcessingModuleRegistration` instead, as `ImageOperationModules.cpp` does. Each file registers once. The registry and the processing list need no change, and the list is sorted by name. +The arguments after the class are its `ProcessingModuleDescriptor`: the id that recipes store, the name shown in the processing list, the schema version and the parameters, built with `integerParameter`, `realParameter`, `booleanParameter` and `choiceParameter` from `internal/ProcessingParameters.h`. A file that builds several modules from one table, or a descriptor that depends on the machine such as the ONNX providers, passes a function to `addProcessingModuleRegistration` instead, as `ImageOperationModules.cpp` does. Each file registers once. CMake picks up every source file of Core from its folder, so a new module is a new file and nothing else changes. The processing list is sorted by name. ## CUDA Modules @@ -245,7 +245,7 @@ namespace scopeone::core::internal::cuda::detail } ``` -The four files are the whole change, as CMake builds every file in `src/cuda` and `internal/cuda`. +The four files are the whole change. - The kernels are launched through plain functions declared in the module's own kernels header, which has no Qt and no CUDA types, so the module sources compile without nvcc and no shared file changes. `CudaKernelLaunch.h` holds what the kernel files share, such as `synchronizeKernel`. - The frames are pitched 2D float images by default. `GpuMemoryLayout::Contiguous` in the constructor gives a contiguous buffer for libraries that need one, such as cuFFT. From 4330eef04880fe6360f776f25cbac570eac3d0fc Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:16:27 +0100 Subject: [PATCH 27/49] Run every camera through one provider interface, put each camera of a multi-camera configuration in its own DriverHost and reach hardware only through HardwareRuntime --- CMakeLists.txt | 5 +- CONTRIBUTING.md | 16 + README.md | 2 +- ScopeOneCore/CMakeLists.txt | 1 + ScopeOneCore/DRIVERHOST_PROTOCOL.md | 51 +- ScopeOneCore/README.md | 9 +- .../include/scopeone/DriverManifest.h | 4 +- ScopeOneCore/include/scopeone/ScopeOneCore.h | 8 +- ScopeOneCore/internal/AcquisitionHardware.h | 53 - ScopeOneCore/internal/CameraBackend.h | 191 -- ScopeOneCore/internal/CameraFrameDelivery.h | 96 + ScopeOneCore/internal/CameraManager.h | 108 - ScopeOneCore/internal/CameraRuntimeControl.h | 23 - ScopeOneCore/internal/DriverHostClient.h | 28 + ScopeOneCore/internal/DriverHostProtocol.h | 40 +- .../internal/DriverHostProviderProxy.h | 14 - ScopeOneCore/internal/FrameDeliveryMode.h | 18 + ScopeOneCore/internal/HardwareRuntime.h | 198 +- ScopeOneCore/internal/MDAManager.h | 14 +- ScopeOneCore/internal/MMCoreManager.h | 64 - ScopeOneCore/internal/MicroManagerCalls.h | 125 ++ ScopeOneCore/internal/MicroManagerCamera.h | 133 ++ ScopeOneCore/internal/MicroManagerProvider.h | 134 +- ScopeOneCore/internal/PtuFile.h | 15 - ScopeOneCore/internal/RecordingManager.h | 18 +- ScopeOneCore/internal/SignalSourceManager.h | 47 +- ScopeOneCore/python/scopeone/README.md | 2 +- .../sdk/include/scopeone/CameraProvider.h | 35 +- .../include/scopeone/HardwareCapabilities.h | 76 + ScopeOneCore/sdk/src/HardwareProvider.cpp | 1 + ScopeOneCore/src/CameraBackend.cpp | 480 ---- ScopeOneCore/src/CameraFrameDelivery.cpp | 322 +++ ScopeOneCore/src/CameraManager.cpp | 453 ---- ScopeOneCore/src/DriverHostCameraBackend.cpp | 1862 ---------------- ...ProviderProxy.cpp => DriverHostClient.cpp} | 763 ++++--- ScopeOneCore/src/DriverHostMain.cpp | 1986 ++++------------- ScopeOneCore/src/HardwareRuntime.cpp | 473 ++-- ScopeOneCore/src/MDAManager.cpp | 107 +- ScopeOneCore/src/MicroManagerCamera.cpp | 1032 +++++++++ ...ager.cpp => MicroManagerConfiguration.cpp} | 138 +- ScopeOneCore/src/MicroManagerProvider.cpp | 512 +++-- ScopeOneCore/src/NativeCameraBackend.cpp | 1180 ---------- ScopeOneCore/src/PtuFile.cpp | 593 ----- ScopeOneCore/src/RecordingManager.cpp | 28 +- ScopeOneCore/src/ScopeOneCore.cpp | 310 +-- ScopeOneCore/src/SignalSourceManager.cpp | 109 +- drivers/ExampleCamera/ExampleCameraDriver.cpp | 44 +- drivers/ExampleCamera/driver.json | 2 +- drivers/ExamplePmt/driver.json | 2 +- drivers/NIDaqmx/driver.json | 2 +- drivers/README.md | 2 +- 51 files changed, 3966 insertions(+), 7963 deletions(-) delete mode 100644 ScopeOneCore/internal/AcquisitionHardware.h delete mode 100644 ScopeOneCore/internal/CameraBackend.h create mode 100644 ScopeOneCore/internal/CameraFrameDelivery.h delete mode 100644 ScopeOneCore/internal/CameraManager.h delete mode 100644 ScopeOneCore/internal/CameraRuntimeControl.h create mode 100644 ScopeOneCore/internal/DriverHostClient.h delete mode 100644 ScopeOneCore/internal/DriverHostProviderProxy.h create mode 100644 ScopeOneCore/internal/FrameDeliveryMode.h delete mode 100644 ScopeOneCore/internal/MMCoreManager.h create mode 100644 ScopeOneCore/internal/MicroManagerCalls.h create mode 100644 ScopeOneCore/internal/MicroManagerCamera.h delete mode 100644 ScopeOneCore/internal/PtuFile.h delete mode 100644 ScopeOneCore/src/CameraBackend.cpp create mode 100644 ScopeOneCore/src/CameraFrameDelivery.cpp delete mode 100644 ScopeOneCore/src/CameraManager.cpp delete mode 100644 ScopeOneCore/src/DriverHostCameraBackend.cpp rename ScopeOneCore/src/{DriverHostProviderProxy.cpp => DriverHostClient.cpp} (71%) create mode 100644 ScopeOneCore/src/MicroManagerCamera.cpp rename ScopeOneCore/src/{MMCoreManager.cpp => MicroManagerConfiguration.cpp} (82%) delete mode 100644 ScopeOneCore/src/NativeCameraBackend.cpp delete mode 100644 ScopeOneCore/src/PtuFile.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index de382f2..2114f31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -282,11 +282,14 @@ endif () set(_driver_dir "${ScopeOneCore_ROOT}/${CMAKE_INSTALL_BINDIR}/drivers") if (EXISTS "${_driver_dir}") - add_custom_command(TARGET ScopeOne POST_BUILD + # Copied on every build. As a step after linking ScopeOne it ran only when ScopeOne itself was + # relinked, so rebuilt drivers stayed old beside the application and crashed a newer DriverHost. + add_custom_target(ScopeOneDrivers ALL COMMAND ${CMAKE_COMMAND} -E copy_directory "${_driver_dir}" "$/drivers" ) + add_dependencies(ScopeOneDrivers ScopeOne) install(DIRECTORY "${_driver_dir}/" DESTINATION "drivers") endif () diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 488e355..6321cd8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -6,6 +6,22 @@ Visual Studio Build Tools, and on Linux and macOS `./scripts/build.sh`. This fil how the code is meant to be written. Each rule says why. When a rule and its reason disagree in a specific case, follow the reason and say so in the review. +## Philosophy + +**C++ and Python.** The parts that drive the hardware and handle every frame are written in C++: +acquisition, recording, display, and processing that has to keep up with the camera. They have to be +fast and predictable, and Qt lets the same code run on Windows, Linux and macOS. Python is for the +methods researchers write themselves: a processing step, an analysis of recorded data, or a script +that runs an experiment through the Local API. Each Python module runs in its own process, so a slow +or broken script cannot hold up the microscope. When a Python method turns out to be needed at the +full frame rate, it is rewritten in C++. + +**One place for processing.** Frames from every camera end up in the same pipeline, whatever the +platform and whatever the hardware. A processing step can be a C++ module on the CPU, a CUDA module +on an NVIDIA GPU, an ONNX model, or a Python function, and it plugs in the same way. Whoever adds a +method gets the live view, recording and the Local API with it, and the person at the microscope +just picks the method. + ## Boundaries **Layers.** Dependencies point one way: diff --git a/README.md b/README.md index a25c9a0..5e97ff0 100644 --- a/README.md +++ b/README.md @@ -275,7 +275,7 @@ Use `scopeone` as the server name, `stdio` as the transport, the absolute path t The MCP tool set mirrors the Local API operation catalog, including system state, configuration, preview layers, independent image windows, automatic display levels, source alignment, markups, device properties, exposure, ROI, stages, stage mosaics, processing, experiments, recording sessions, frame transfer, and analysis. Agents can list, open, activate, process, save, and close independent image windows, read the current frame of any image layer, monitor live acquisition and writer progress, and optionally export or display particle masks. MCP tool calls are visible in the desktop UI through the same application state used by manual controls. -Configuration loading and unloading report an explicit lifecycle state through the Local API and MCP. A configuration with non-camera initialization warnings is reported as `partially_loaded` with failed device labels; camera backend startup failures are cleaned up and reported as errors. During `loading` or `unloading`, hardware mutations are rejected until the operation finishes. +Configuration loading and unloading report an explicit lifecycle state through the Local API and MCP. A configuration with non-camera initialization warnings is reported as `partially_loaded` with failed device labels; cameras that fail to start are cleaned up and reported as errors. During `loading` or `unloading`, hardware mutations are rejected until the operation finishes. ## 🔬 Tested Devices diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index a99d23b..ec161fa 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -391,6 +391,7 @@ add_executable(ScopeOne_DriverHost src/DriverHostMain.cpp src/HardwareStreamCodec.cpp src/DriverManifest.cpp + src/MicroManagerCamera.cpp src/StreamRing.cpp ) if (UNIX) diff --git a/ScopeOneCore/DRIVERHOST_PROTOCOL.md b/ScopeOneCore/DRIVERHOST_PROTOCOL.md index e1a3c9d..f935ba6 100644 --- a/ScopeOneCore/DRIVERHOST_PROTOCOL.md +++ b/ScopeOneCore/DRIVERHOST_PROTOCOL.md @@ -1,24 +1,33 @@ # DriverHost protocol -ScopeOne talks to every driver through a `ScopeOne_DriverHost` process. This file -describes what crosses that process boundary: control messages on a local socket, camera frames +ScopeOne talks to every driver, and to each camera of a Micro-Manager configuration with more +than one camera, through a `ScopeOne_DriverHost` process. This file describes what crosses that +process boundary: control messages on a local socket, camera frames in shared memory, and the chunks of signal sources and DAQ devices in stream rings. Both ends are part of ScopeOne, so the protocol is internal and changes with it; drivers are written against `scopeone::DriverSDK` instead. It is version -6, `kProtocolVersion` in `ScopeOneCore/internal/DriverHostProtocol.h`, which holds the names used +7, `kProtocolVersion` in `ScopeOneCore/internal/DriverHostProtocol.h`, which holds the names used below. ## Process -ScopeOne starts one host for each device: +ScopeOne starts one host for each driver and for each camera of a Micro-Manager configuration +with more than one camera: ```text ScopeOne_DriverHost --provider --driver --hostKey --launcherPid [--option ] +ScopeOne_DriverHost --provider micro-manager --micromanagerCamera --hostKey + --launcherPid ``` -`--option` carries the options from the Drivers dialog. The host loads the driver, creates the -devices and listens on the local socket `ScopeOne.DriverHost..ctrl` (a named pipe on -Windows, a Unix domain socket elsewhere). ScopeOne connects to it, and the host exits when the +`--option` carries the options from the Drivers dialog. `--micromanagerCamera` carries +`cameraId`, `adapter`, `device`, `exposureMs`, and `preInitProperties` and `properties`, arrays of +`{"name", "value"}` objects. The host loads the camera on a core of its own, as two cameras of the +same model in one process would share the state of one vendor library. The host loads the +driver or the camera, creates the devices and listens on the local socket +`ScopeOne..ctrl`, where `` is the first 16 hexadecimal digits of the SHA-1 of the host +key, as a Unix socket path on macOS holds at most 104 characters (a named pipe on Windows, a Unix +domain socket elsewhere). ScopeOne connects to it, and the host exits when the process `--launcherPid` is gone. ## Messages @@ -42,7 +51,9 @@ and carry `providerId`. ## Describe and shutdown `Describe` returns `providerId`, `providerName`, `providerVersion`, `devices`, `signalSources` -and `daqDevices`, and for a provider with stages `defaultXYStage` and `defaultZStage`. +and `daqDevices`, for a provider with stages `defaultXYStage` and `defaultZStage`, for one with +shutters `defaultShutter`, the shutter that lights the sample for its cameras, and for one with an +autofocus `defaultAutofocus`. `signalSources` and `daqDevices` hold the JSON form of `SignalSourceDescriptor` and `DaqDeviceDescriptor`, written by `ScopeOneCore/src/HardwareStreamCodec.cpp`. Each device is an object with @@ -55,32 +66,48 @@ and `daqDevices`, and for a provider with stages `defaultXYStage` and `defaultZS | `deviceProperties` | an object of fixed device information | | `capabilities` | the capability names below that the device supports | | `sharedMemoryKey` | cameras only, the key of the frame ring | +| `eventSequence` | cameras only, whether the camera takes a series of images in one sequence acquisition | +| `pixelSizeUm` | cameras only, the size of a pixel in the sample, 0 when unknown; `GetPixelSize` reads it again after a setting changed | `Shutdown` stops the devices, answers, and ends the host. ## Commands -Every command except the configuration, signal and DAQ ones names its device with `deviceId`. A command for a +Every command except the configuration, automatic shutter, signal and DAQ ones names its device +with `deviceId`. A command for a capability the device lacks is answered with `ok` false. | Capability | Command | Request fields | Response fields | |---|---|---|---| | `Camera` | `StartPreview`, `StopPreview` | | | | | `SetFrameDeliveryMode` | `mode`: `PreviewLatest`, `LatestOnly` or `AllFrames` | `mode` | +| | `SetFrameDeliveryPaused` | `paused`; the host writes no frames while paused, and a Micro-Manager camera starts again the live acquisition an event capture stopped | | | | `GetExposure` | | `exposureMs` | | | `SetExposure` | `value` in ms | `exposureMs`, the value the camera took | | | `SetROI` | `x`, `y`, `width`, `height` | `x`, `y`, `width`, `height` | | | `ClearROI`, `GetROI` | | `x`, `y`, `width`, `height` | | | `CaptureEvent` | `timeoutMs`, default 1500 | `frameIndex` | +| | `CaptureEventSequence` | `count`, `timeoutMs`; the images wait in the host | | +| | `SendEventFrames` | `first`, `count`; writes these images of the last sequence to the frame ring, as it holds fewer than a sequence can have | | +| | `GetPixelSize` | | `pixelSizeUm`, 0 when unknown | | `Properties` | `ListProperties` | | `properties` | | | `GetProperty` | `name`, `fromCache` | `value`, `propertyType`, `readOnly`, `preInit`, `allowedValues`, `hasLimits`, `lowerLimit`, `upperLimit` | | | `SetProperty` | `name`, `value` | | +| | `GetPropertySequenceCapacity` | `name` | `capacity`, 0 when it cannot be sequenced | +| | `StartPropertySequence` | `name`, `values` | | +| | `StopPropertySequence` | `name` | | | `Stage` | `GetXYPosition` | | `x`, `y` | | | `GetZPosition` | | `z` | | | `SetXYPosition`, `SetRelativeXYPosition` | `x`, `y` | | | | `SetZPosition`, `SetRelativeZPosition` | `z` | | +| | `GetStageSequenceCapacity` | | `capacity`, 0 when it cannot be sequenced | +| | `StartStageSequence` | `positions` | | +| | `StopStageSequence` | | | | `Shutter` | `GetShutterOpen` | | `open` | | | `SetShutterOpen` | `open` | | +| | `GetAutoShutter`, provider wide | | `enabled` | +| | `SetAutoShutter`, provider wide | `enabled` | | +| `Autofocus` | `RunAutofocus` | | | | `State` | `GetState` | | `state`, `label` | | | `SetState` | `state` | | | | `GetStateLabel` | `state` | `label` | @@ -92,6 +119,7 @@ capability the device lacks is answered with `ok` false. | | `ListConfigs` | `group` | `configs` | | | `GetCurrentConfig` | `group` | `config` | | | `SetConfig` | `group`, `config` | | +| | `GetConfigSettings` | `group`, `config` | `settings`, an array of `device`, `property`, `value` | ## Events @@ -136,7 +164,10 @@ The pixels start at offset 64 of the slot, `stride * height` bytes. The writer takes a slot whose `state` is 0 or 2 by changing it to 1 with a compare-and-swap, fills the header and pixels, stores 2 with release order, then stores the slot index in -`latestSlotIndex` with release order and sends `FrameAvailable`. The reader takes a slot by +`latestSlotIndex` with release order and sends `FrameAvailable`. The host writes a frame while the +frame sink of the driver is called, so a driver may hand over a frame that points into its own +buffer. In `PreviewLatest` mode it writes every frame but sends `FrameAvailable` at most every +16 ms, and a frame that came in between is announced when the 16 ms are up. The reader takes a slot by changing its `state` from 2 to 3, copies the frame, and stores 2 again. A slot being read is skipped by the writer, so the reader never sees a frame change under it. diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 4d0cb57..c68eda4 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -48,7 +48,7 @@ Use this placement rule: |---|---|---| | `scopeone::core` | Stable Core-facing types and public facades | `ScopeOneCore`, `ExperimentDocument`, `ImageSceneModel` | | `scopeone::core` driver contract | Stable types of `DriverSDK.h` | `Driver`, `HardwareProvider`, `CameraProvider`, `SignalSourceProvider`, `DaqProvider`, `ImageFrame` | -| `scopeone::core::internal` | Core-only managers, processing modules and built-in devices | `CameraManager`, `MMCoreManager`, `RecordingManager`, `IscatModule`, `SignalSourceManager` | +| `scopeone::core::internal` | Core-only managers, processing modules and built-in devices | `HardwareRuntime`, `MicroManagerProvider`, `RecordingManager`, `IscatModule`, `SignalSourceManager` | | `scopeone::core::internal::driverhost` | Shared DriverHost message framing | Versioned request, response and event envelopes | | `scopeone::ui` | Desktop application widgets and UI coordination outside this library | `MainWindow`, `PreviewWidget`, `DhmTool`, `ToolRegistry` | | `scopeone::core::internal::cuda`, `scopeone::core::internal::inference` | The CUDA and ONNX processing modules in `src/cuda` and `src/inference` | `core::internal::cuda` | @@ -115,13 +115,16 @@ The installed headers in `include/scopeone` and `include/sdk/scopeone` are the s External code should enter through these headers and `scopeone::core::ScopeOneCore`. Internal managers are implementation details and must not become alternate access paths. -Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `CameraProvider::FrameSink`. Core loads a driver with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider, using the native camera path for one camera and isolated DriverHost camera processes for multiple cameras. +Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `CameraProvider::FrameSink`. The sink copies a frame before it returns, so a frame can point into the buffer of the camera SDK through `QByteArray::fromRawData` instead of being copied by the driver first. Core loads a driver with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider. It runs the camera of a configuration with one camera in ScopeOne, and each camera of a configuration with more in a DriverHost of its own, so two cameras of the same model do not share one vendor library. + +Inside Core, the rest of Core reaches hardware only through `HardwareRuntime`. It routes each call to the provider of the device and answers the calls that span providers itself, such as all cameras, the default stages and shutter, and the frame delivery to the live view, live processing and recording. Every provider, `MicroManagerProvider` as well as each driver, offers its devices through the same interfaces of the driver SDK, so a driver can do what Micro-Manager does here, from hardware sequences to autofocus. How +frames are delivered and paused is ScopeOne's own policy and stays out of the driver SDK. ## Driver and Processing Boundaries - `drivers` contains the drivers, among them NI-DAQmx and the example camera and PMT. Each provider runs in an isolated DriverHost process, so a vendor library such as NI-DAQmx is never loaded into ScopeOne. Micro-Manager Device Adapters remain under Micro-Manager and are not wrapped as ScopeOne drivers. - Python processing modules are scripts in `processing/python`, beside the executable or in the application data directory. Core runs each module of a pipeline in its own Python process. -- The CUDA and ONNX processing modules are part of Core. CUDA is built when nvcc is found and `SCOPEONE_ENABLE_CUDA` is on. ONNX inference is always built, with the official ONNX Runtime package that the configure downloads into `external`, the GPU package when CUDA is built and the CPU package otherwise. `ONNXRUNTIME_ROOT` points Core at another SDK. Every processing method, the PTU file signal source and the DHM reconstruction are part of Core. +- The CUDA and ONNX processing modules are part of Core. CUDA is built when nvcc is found and `SCOPEONE_ENABLE_CUDA` is on. ONNX inference is always built, with the official ONNX Runtime package that the configure downloads into `external`, the GPU package when CUDA is built and the CPU package otherwise. `ONNXRUNTIME_ROOT` points Core at another SDK. Every processing method and the DHM reconstruction are part of Core. - The tool windows (Scale, Stage Mosaic, Particle Detection, DHM Reconstruction, Scanning and DAQ Control, Signal Monitor) are part of the application. They use the `ScopeOneCore` facade, and `ScopeOneToolContext` in `src/tools/ToolContext.h` gives them the desktop operations such as showing layers. Drivers outside this repository consume the exported `scopeone::DriverSDK` CMake target. Every driver manifest declares `id`, `name`, `version`, `scopeOneApi`, and `kind`, and incompatible manifests are rejected before the library instance is created. diff --git a/ScopeOneCore/include/scopeone/DriverManifest.h b/ScopeOneCore/include/scopeone/DriverManifest.h index 4ba27e9..990b244 100644 --- a/ScopeOneCore/include/scopeone/DriverManifest.h +++ b/ScopeOneCore/include/scopeone/DriverManifest.h @@ -29,7 +29,9 @@ namespace scopeone::core QString error; }; - inline constexpr int ScopeOneDriverApiVersion = 1; + // Raised whenever an interface of the driver SDK changes its virtual functions. A driver built + // against other ones calls the wrong functions and crashes DriverHost, so it is rejected. + inline constexpr int ScopeOneDriverApiVersion = 2; // The drivers directory beside the application and in the user data directory SCOPEONE_CORE_EXPORT QStringList driverDirectories(); diff --git a/ScopeOneCore/include/scopeone/ScopeOneCore.h b/ScopeOneCore/include/scopeone/ScopeOneCore.h index 0d0b9e0..96e90de 100644 --- a/ScopeOneCore/include/scopeone/ScopeOneCore.h +++ b/ScopeOneCore/include/scopeone/ScopeOneCore.h @@ -32,7 +32,6 @@ #include "scopeone/SignalSource.h" #include "scopeone/scopeone_core_export.h" -class CMMCore; class QThreadPool; class QTimer; @@ -505,7 +504,6 @@ namespace scopeone::core void daqError(const QString& deviceId, const QString& errorMessage); void daqInputDataReady(const DaqInputChunk& chunk); - void driverHostControlServerListening(const QString& cameraId, const QString& serverName); void previewProcessedFrameReady(const ImageFrame& frame); void staticFramePublished(const QString& sourceId, const QString& displayName, @@ -639,7 +637,10 @@ namespace scopeone::core void synchronizeCameraIdsFromRegistry(); void finishConfigurationLoadFailure(const LoadConfigResult& result, const QString& errorMessage); - void clearConfigurationRuntime(bool notify, bool shutdownCameraBackend); + void clearConfigurationRuntime(bool notify, bool stopCameras); + // Stops the Micro-Manager cameras on the hardware thread, as their DriverHosts can take a + // second to exit, and then calls next + void stopMicroManagerCameras(std::function next); void startConfigurationLoadTask(const QString& configPath); void startConfigurationLoadCleanupTask(const LoadConfigResult& result, const QString& errorMessage); @@ -653,7 +654,6 @@ namespace scopeone::core quint64 queueStageMove( const QString& deviceLabel, std::function command); - std::shared_ptr core() const; bool isPropertyPreInit(const QString& deviceLabel, const QString& name) const; void ensureSceneLayer(const QString& layerKey, const QString& sourceId, diff --git a/ScopeOneCore/internal/AcquisitionHardware.h b/ScopeOneCore/internal/AcquisitionHardware.h deleted file mode 100644 index e2879a4..0000000 --- a/ScopeOneCore/internal/AcquisitionHardware.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include - -#include - -namespace scopeone::core::internal -{ - // Hardware functions of an acquisition that act on the core of Micro-Manager rather than on - // one device: the shutter that the core opens for every image, and the autofocus device - // The values that one device property takes on successive triggers - struct PropertySequence - { - QString device; - QString property; - std::vector values; - }; - - class AcquisitionHardware - { - public: - virtual ~AcquisitionHardware() = default; - - // Whether the core opens the shutter around each image by itself - virtual bool autoShutter(bool& enabled, QString* errorMessage = nullptr) const = 0; - virtual bool setAutoShutter(bool enabled, QString* errorMessage = nullptr) = 0; - // Opens or closes the shutter that the core currently uses - virtual bool setCoreShutterOpen(bool open, QString* errorMessage = nullptr) = 0; - - // Autofocus device that the core currently uses - virtual bool hasAutofocus() const = 0; - virtual bool runAutofocus(QString* errorMessage = nullptr) = 0; - - // Number of positions that the focus device can step through by itself on each - // trigger, or 0 when it has no hardware sequence - virtual long focusSequenceCapacity(const QString& focusDevice) const = 0; - // Loads the positions into the focus device and arms it, so that every trigger of the - // camera moves it to the next one - virtual bool startFocusSequence(const QString& focusDevice, - const std::vector& positions, - QString* errorMessage = nullptr) = 0; - virtual void stopFocusSequence(const QString& focusDevice) = 0; - - // Number of values that a device property can step through by itself on each - // trigger, or 0 when it has no hardware sequence - virtual long propertySequenceCapacity(const QString& device, const QString& property) const = 0; - // Loads the values of the properties and arms them, so that every trigger of the - // camera sets the next value. Nothing stays armed when it fails. - virtual bool startPropertySequences(const std::vector& sequences, - QString* errorMessage = nullptr) = 0; - virtual void stopPropertySequences(const std::vector& sequences) = 0; - }; -} diff --git a/ScopeOneCore/internal/CameraBackend.h b/ScopeOneCore/internal/CameraBackend.h deleted file mode 100644 index 47b695b..0000000 --- a/ScopeOneCore/internal/CameraBackend.h +++ /dev/null @@ -1,191 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "scopeone/ImageFrame.h" - -class CMMCore; - -namespace scopeone::core::internal -{ - class ProcessingFrameGate - { - public: - void setEnabled(bool enabled); - quint64 tryAcquire(const QString& cameraId); - bool isCurrent(const QString& cameraId, quint64 token); - void release(const QString& cameraId, quint64 token); - - private: - QMutex m_mutex; - QHash m_inFlightTokens; - quint64 m_nextToken{0}; - bool m_enabled{false}; - }; - - struct CameraPropertyReadback - { - QString value; - QString type{QStringLiteral("Unknown")}; - bool readOnly{true}; - bool preInit{false}; - QStringList allowedValues; - bool hasLimits{false}; - double lowerLimit{0.0}; - double upperLimit{0.0}; - }; - - class CameraBackend : public QObject - { - Q_OBJECT - - public: - enum class Kind - { - Native, - DriverHost - }; - - explicit CameraBackend(ProcessingFrameGate& processingFrameGate, - QObject* parent = nullptr); - ~CameraBackend() override; - - virtual Kind kind() const = 0; - virtual void shutdown(std::function completion) - { - if (completion) - { - completion({}); - } - } - - virtual bool configureNativeCamera(const std::shared_ptr& core, - const QString& cameraId, - double exposureMs); - virtual bool addDriverHostCamera(const QString& cameraId, - const QString& adapter, - const QString& device, - const QStringList& preInitProperties, - const QStringList& properties, - double exposureMs); - - virtual bool startPreview() = 0; - virtual bool stopPreview() = 0; - virtual bool startPreviewFor(const QString& cameraId) = 0; - virtual bool stopPreviewFor(const QString& cameraId) = 0; - virtual bool isPreviewRunning(const QString& cameraId) const = 0; - virtual void setFrameDeliveryPaused(bool paused) = 0; - virtual bool captureEventFrame(const QString& cameraId, - scopeone::core::ImageFrame& frame, - int timeoutMs); - virtual bool supportsEventSequence(const QString& cameraId) const - { - (void)cameraId; - return false; - } - virtual bool captureEventSequence(const QString& cameraId, - int count, - std::vector& frames, - int timeoutMs) - { - (void)cameraId; - (void)count; - (void)frames; - (void)timeoutMs; - return false; - } - - virtual bool setRecordingFrameDeliveryEnabled(bool enabled); - virtual bool setHighRateFrameDeliveryEnabled(bool enabled); - - bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const; - bool setExposure(const QString& cameraIdOrAll, double exposureMs); - QStringList listProperties(const QString& cameraId); - bool readPropertyDetails(const QString& cameraId, - const QString& name, - bool fromCache, - CameraPropertyReadback& readback); - bool setProperty(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage); - bool setROI(const QString& cameraId, int x, int y, int width, int height); - bool clearROI(const QString& cameraId); - bool getROI(const QString& cameraId, int& x, int& y, int& width, int& height); - - signals: - void rawFrameReady(const scopeone::core::ImageFrame& frame); - void processingFrameReady(const scopeone::core::ImageFrame& frame, quint64 token); - void rawFramesAcquired(const QString& cameraId, quint64 frameCount); - void recordingFramesReady(const QList& frames); - void frameDeliveryFailed(const QString& errorMessage, quint64 droppedFrames); - void previewStateChanged(bool running); - void driverHostControlServerListening(const QString& cameraId, const QString& serverName); - - protected: - bool applyWithPreviewRestart(const QString& cameraId, const std::function& operation); - void notifyPreviewStarted(const QString& cameraId); - void notifyPreviewStopped(); - void submitFrames(const QList& frames, - quint64 acquiredFrameCount, - quint64 recordingToken); - void submitProcessingFrame(const scopeone::core::ImageFrame& frame, quint64 token); - quint64 tryAcquireProcessingFrame(const QString& cameraId); - void releaseProcessingFrame(const QString& cameraId, quint64 token); - void discardPendingPreviewFrames(); - bool recordingFrameDeliveryEnabled() const; - quint64 recordingFrameDeliveryToken() const; - bool highRateFrameDeliveryEnabled() const; - - virtual bool hasRunningCamera() const = 0; - virtual bool resolvePrimaryCameraId(const QString& cameraIdOrAll, QString& cameraId) const = 0; - virtual QStringList resolveTargetCameraIds(const QString& cameraIdOrAll) const = 0; - virtual bool readExposureFor(const QString& cameraId, double& exposureMs) const = 0; - virtual bool writeExposureFor(const QString& cameraId, double exposureMs) = 0; - virtual QStringList listPropertiesFor(const QString& cameraId) = 0; - virtual bool readPropertyDetailsFor(const QString& cameraId, - const QString& name, - bool fromCache, - CameraPropertyReadback& readback) = 0; - virtual bool setPropertyFor(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage) = 0; - virtual bool setROIFor(const QString& cameraId, int x, int y, int width, int height) = 0; - virtual bool clearROIFor(const QString& cameraId) = 0; - virtual bool getROIFor(const QString& cameraId, - int& x, - int& y, - int& width, - int& height) = 0; - - private: - void flushPendingFrames(); - - mutable QMutex m_frameDeliveryMutex; - QHash m_pendingLatestFrames; - QHash m_pendingAcquiredFrameCounts; - std::deque m_pendingRecordingFrames; - qint64 m_pendingRecordingBytes{0}; - quint64 m_pendingDroppedRecordingFrames{0}; - QString m_pendingDeliveryError; - std::atomic m_recordingFrameDeliveryToken{0}; - quint64 m_nextRecordingFrameDeliveryToken{0}; - std::atomic_bool m_highRateFrameDeliveryEnabled{false}; - ProcessingFrameGate& m_processingFrameGate; - bool m_frameFlushQueued{false}; - }; - - std::unique_ptr createNativeCameraBackend(ProcessingFrameGate& processingFrameGate); - std::unique_ptr createDriverHostCameraBackend(ProcessingFrameGate& processingFrameGate); -} diff --git a/ScopeOneCore/internal/CameraFrameDelivery.h b/ScopeOneCore/internal/CameraFrameDelivery.h new file mode 100644 index 0000000..e45b912 --- /dev/null +++ b/ScopeOneCore/internal/CameraFrameDelivery.h @@ -0,0 +1,96 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "internal/FrameDeliveryMode.h" +#include "scopeone/ImageFrame.h" + +namespace scopeone::core::internal +{ + // Hands the frames of every camera, whichever provider runs it, to the live view, to live + // processing and to recording. The live view gets the newest frame of each camera once per pass + // of the event loop, processing gets one frame of a camera at a time, and recording gets every + // frame in order, in bounded batches. + class CameraFrameDelivery final : public QObject + { + Q_OBJECT + + public: + explicit CameraFrameDelivery(QObject* parent = nullptr); + + // Each camera is recorded or processed on its own, as others may keep their live view + void setRecordingEnabled(const QStringList& cameraIds, bool enabled); + bool recordingEnabled(const QString& cameraId) const; + void setHighRateEnabled(const QStringList& cameraIds, bool enabled); + bool highRateEnabled(const QString& cameraId) const; + // The mode the cameras run in for what is enabled for them + FrameDeliveryMode mode(const QString& cameraId) const; + // Forgets the cameras, as the ids of a new configuration may name other cameras + void removeCameras(const QStringList& cameraIds); + + // Whether the newest frame of a camera is worth reading now. Without recording or live + // processing every newest frame is, as the live view takes it. + bool wantsLatestFrame(const QString& cameraId); + // Frames of one camera in order. acquiredFrameCount counts every frame the camera took + // since the last call, including those that were not read. + void deliverFrames(const QList& frames, quint64 acquiredFrameCount); + + bool isProcessingFrameCurrent(const QString& cameraId, quint64 token); + void finishProcessingFrame(const QString& cameraId, quint64 token); + + // Ends recording delivery of every camera after one failed while it recorded + void fail(const QString& message); + void discardPendingFrames(); + + signals: + void frameReady(const scopeone::core::ImageFrame& frame); + // Emitted on the thread that delivered the frame + void processingFrameReady(const scopeone::core::ImageFrame& frame, quint64 token); + void framesAcquired(const QString& cameraId, quint64 frameCount); + void recordingFramesReady(const QList& frames); + void deliveryFailed(const QString& errorMessage, quint64 droppedFrames); + + private: + quint64 tryAcquireProcessingFrame(const QString& cameraId); + bool previewDue(const QString& cameraId) const; + void flushPendingFrames(); + + mutable QMutex m_mutex; + QHash m_pendingLatestFrames; + QHash m_pendingAcquiredFrameCounts; + QHash m_previewTimers; + std::deque m_pendingRecordingFrames; + qint64 m_pendingRecordingBytes{0}; + quint64 m_pendingDroppedRecordingFrames{0}; + QString m_pendingDeliveryError; + QSet m_recordingCameras; + bool m_frameFlushQueued{false}; + + mutable QMutex m_processingMutex; + QSet m_highRateCameras; + QHash m_processingTokens; + quint64 m_nextProcessingToken{0}; + }; + + // What a provider that runs cameras in ScopeOne offers HardwareRuntime, so their frames reach + // the one CameraFrameDelivery + class CameraFrameSource + { + public: + virtual ~CameraFrameSource() = default; + + virtual void setFrameDelivery(CameraFrameDelivery* delivery) = 0; + virtual bool setFrameDeliveryMode(const QStringList& cameraIds, FrameDeliveryMode mode) = 0; + virtual void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) = 0; + }; +} diff --git a/ScopeOneCore/internal/CameraManager.h b/ScopeOneCore/internal/CameraManager.h deleted file mode 100644 index 3adf310..0000000 --- a/ScopeOneCore/internal/CameraManager.h +++ /dev/null @@ -1,108 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include "internal/CameraBackend.h" -#include "scopeone/CameraProvider.h" -#include "internal/CameraRuntimeControl.h" - -class CMMCore; - -namespace scopeone::core::internal -{ - class CameraManager : public QObject, public CameraProvider, public CameraRuntimeControl - { - Q_OBJECT - - public: - explicit CameraManager(QObject* parent = nullptr); - ~CameraManager() override; - - void setFrameSink(FrameSink sink) override; - void setPreviewStateSink(PreviewStateSink sink) override; - - bool configureNativeCamera(const std::shared_ptr& core, - const QString& cameraId, - double exposureMs = 0.0); - bool addDriverHostCamera(const QString& cameraId, - const QString& adapter, - const QString& device, - const QStringList& preInitProperties = QStringList(), - const QStringList& properties = QStringList(), - double exposureMs = 0.0); - void shutdownNow(); - void shutdown(std::function completion); - - bool startPreview() override; - bool stopPreview() override; - bool usesDriverHostBackend() const; - bool startPreviewFor(const QString& cameraId) override; - bool stopPreviewFor(const QString& cameraId) override; - bool isPreviewRunning(const QString& cameraId) const override; - void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) override; - bool setRecordingFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) override; - bool setHighRateFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) override; - bool isProcessingFrameTokenCurrent(const QString& cameraId, quint64 token) override; - void finishProcessingFrame(const QString& cameraId, quint64 token) override; - - bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const override; - bool setExposure(const QString& cameraIdOrAll, double exposureMs) override; - QStringList listProperties(const QString& cameraId) override; - QString getProperty(const QString& cameraId, - const QString& name, - bool fromCache = false) override; - bool setProperty(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage = nullptr) override; - QString getPropertyType(const QString& cameraId, const QString& name) override; - bool isPropertyReadOnly(const QString& cameraId, const QString& name) override; - bool isPropertyPreInit(const QString& cameraId, const QString& name) override; - QStringList getAllowedPropertyValues(const QString& cameraId, const QString& name) override; - bool hasPropertyLimits(const QString& cameraId, const QString& name) override; - double getPropertyLowerLimit(const QString& cameraId, const QString& name) override; - double getPropertyUpperLimit(const QString& cameraId, const QString& name) override; - - bool setROI(const QString& cameraId, int x, int y, int width, int height) override; - bool clearROI(const QString& cameraId) override; - bool getROI(const QString& cameraId, int& x, int& y, int& width, int& height) override; - bool captureEventFrame(const QString& cameraId, - scopeone::core::ImageFrame& frame, - int timeoutMs = 1500) override; - bool supportsEventSequence(const QString& cameraId) const override; - bool captureEventSequence(const QString& cameraId, - int count, - std::vector& frames, - int timeoutMs = 10000) override; - - signals: - void processingFrameReady(const scopeone::core::ImageFrame& frame, quint64 token); - void rawFramesAcquired(const QString& cameraId, quint64 frameCount); - void recordingFramesReady(const QList& frames); - void frameDeliveryFailed(const QString& errorMessage, quint64 droppedFrames); - void previewStateChanged(bool running); - void driverHostControlServerListening(const QString& cameraId, const QString& serverName); - - private: - bool activateBackend(CameraBackend::Kind kind); - // Reads one property into the cache when it is not there yet - CameraPropertyReadback propertyDetails(const QString& cameraId, const QString& name); - - ProcessingFrameGate m_processingFrameGate; - std::unique_ptr m_backend; - FrameSink m_frameSink; - PreviewStateSink m_previewStateSink; - bool m_recordingFrameDeliveryEnabled{false}; - bool m_highRateFrameDeliveryEnabled{false}; - QMap m_propertyNamesCache; - QMap m_propertyDetailsCache; - }; -} diff --git a/ScopeOneCore/internal/CameraRuntimeControl.h b/ScopeOneCore/internal/CameraRuntimeControl.h deleted file mode 100644 index 7eab518..0000000 --- a/ScopeOneCore/internal/CameraRuntimeControl.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace scopeone::core::internal -{ - class CameraRuntimeControl - { - public: - virtual ~CameraRuntimeControl() = default; - - virtual void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) = 0; - virtual bool setRecordingFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) = 0; - virtual bool setHighRateFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) = 0; - virtual bool isProcessingFrameTokenCurrent(const QString& cameraId, - quint64 token) = 0; - virtual void finishProcessingFrame(const QString& cameraId, quint64 token) = 0; - }; -} diff --git a/ScopeOneCore/internal/DriverHostClient.h b/ScopeOneCore/internal/DriverHostClient.h new file mode 100644 index 0000000..96a165f --- /dev/null +++ b/ScopeOneCore/internal/DriverHostClient.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +#include + +#include "scopeone/HardwareProvider.h" + +namespace scopeone::core::internal +{ + // A DriverHost is a process of its own that runs hardware for ScopeOne, so a vendor library that + // crashes or holds global state stays out of ScopeOne. The provider these functions return + // stands for what the DriverHost runs. It also implements CameraFrameSource. + + // A DriverHost that runs what the arguments name, such as one camera of a Micro-Manager + // configuration for MicroManagerProvider + HardwareProviderPtr startDriverHost(const QString& providerId, + const QStringList& arguments, + QString* errorMessage = nullptr); + + // The devices of one ScopeOne driver. Each driver is a provider of its own, as each camera of a + // Micro-Manager configuration is a device of MicroManagerProvider. + HardwareProviderPtr createScopeOneProvider(const QString& providerId, + const QString& driverPath, + const QJsonObject& options, + QString* errorMessage = nullptr); +} diff --git a/ScopeOneCore/internal/DriverHostProtocol.h b/ScopeOneCore/internal/DriverHostProtocol.h index c30aa28..ca53968 100644 --- a/ScopeOneCore/internal/DriverHostProtocol.h +++ b/ScopeOneCore/internal/DriverHostProtocol.h @@ -3,7 +3,7 @@ #include "scopeone/HardwareTypes.h" #include -#include +#include #include #include #include @@ -17,7 +17,7 @@ namespace scopeone::core::internal::driverhost { - inline constexpr quint32 kProtocolVersion = 6; + inline constexpr quint32 kProtocolVersion = 7; // The names of the device kinds as the protocol sends them inline constexpr std::pair kDeviceKindNames[]{ @@ -83,6 +83,11 @@ namespace scopeone::core::internal::driverhost inline const QString kSharedMemoryKeyField = QStringLiteral("sharedMemoryKey"); inline const QString kDefaultXYStageField = QStringLiteral("defaultXYStage"); inline const QString kDefaultZStageField = QStringLiteral("defaultZStage"); + inline const QString kDefaultShutterField = QStringLiteral("defaultShutter"); + inline const QString kDefaultAutofocusField = QStringLiteral("defaultAutofocus"); + // Whether a camera takes a series of images in one sequence acquisition + inline const QString kEventSequenceField = QStringLiteral("eventSequence"); + inline const QString kPixelSizeField = QStringLiteral("pixelSizeUm"); inline const QString kSignalSourcesField = QStringLiteral("signalSources"); inline const QString kDaqDevicesField = QStringLiteral("daqDevices"); inline const QString kSourceIdField = QStringLiteral("sourceId"); @@ -100,12 +105,18 @@ namespace scopeone::core::internal::driverhost inline const QString kCommandStartPreview = QStringLiteral("StartPreview"); inline const QString kCommandStopPreview = QStringLiteral("StopPreview"); inline const QString kCommandSetFrameDeliveryMode = QStringLiteral("SetFrameDeliveryMode"); + inline const QString kCommandSetFrameDeliveryPaused = QStringLiteral("SetFrameDeliveryPaused"); inline const QString kCommandGetExposure = QStringLiteral("GetExposure"); inline const QString kCommandSetExposure = QStringLiteral("SetExposure"); inline const QString kCommandListProperties = QStringLiteral("ListProperties"); inline const QString kCommandGetProperty = QStringLiteral("GetProperty"); inline const QString kCommandSetProperty = QStringLiteral("SetProperty"); inline const QString kCommandCaptureEvent = QStringLiteral("CaptureEvent"); + // The images of a sequence wait in DriverHost, and SendEventFrames writes them to the frame + // ring a part at a time, as the ring holds fewer images than a sequence can have + inline const QString kCommandCaptureEventSequence = QStringLiteral("CaptureEventSequence"); + inline const QString kCommandSendEventFrames = QStringLiteral("SendEventFrames"); + inline const QString kCommandGetPixelSize = QStringLiteral("GetPixelSize"); inline const QString kCommandSetRoi = QStringLiteral("SetROI"); inline const QString kCommandClearRoi = QStringLiteral("ClearROI"); inline const QString kCommandGetRoi = QStringLiteral("GetROI"); @@ -115,6 +126,15 @@ namespace scopeone::core::internal::driverhost inline const QString kCommandSetRelativeZPosition = QStringLiteral("SetRelativeZPosition"); inline const QString kCommandSetXYPosition = QStringLiteral("SetXYPosition"); inline const QString kCommandSetZPosition = QStringLiteral("SetZPosition"); + inline const QString kCommandGetStageSequenceCapacity = QStringLiteral("GetStageSequenceCapacity"); + inline const QString kCommandStartStageSequence = QStringLiteral("StartStageSequence"); + inline const QString kCommandStopStageSequence = QStringLiteral("StopStageSequence"); + inline const QString kCommandGetPropertySequenceCapacity = QStringLiteral("GetPropertySequenceCapacity"); + inline const QString kCommandStartPropertySequence = QStringLiteral("StartPropertySequence"); + inline const QString kCommandStopPropertySequence = QStringLiteral("StopPropertySequence"); + inline const QString kCommandGetAutoShutter = QStringLiteral("GetAutoShutter"); + inline const QString kCommandSetAutoShutter = QStringLiteral("SetAutoShutter"); + inline const QString kCommandRunAutofocus = QStringLiteral("RunAutofocus"); inline const QString kCommandGetShutterOpen = QStringLiteral("GetShutterOpen"); inline const QString kCommandSetShutterOpen = QStringLiteral("SetShutterOpen"); inline const QString kCommandGetState = QStringLiteral("GetState"); @@ -124,6 +144,7 @@ namespace scopeone::core::internal::driverhost inline const QString kCommandListConfigs = QStringLiteral("ListConfigs"); inline const QString kCommandGetCurrentConfig = QStringLiteral("GetCurrentConfig"); inline const QString kCommandSetConfig = QStringLiteral("SetConfig"); + inline const QString kCommandGetConfigSettings = QStringLiteral("GetConfigSettings"); inline const QString kCommandStartSignal = QStringLiteral("StartSignal"); inline const QString kCommandStopSignal = QStringLiteral("StopSignal"); inline const QString kCommandStartDaq = QStringLiteral("StartDaq"); @@ -135,6 +156,7 @@ namespace scopeone::core::internal::driverhost inline const QString kCapabilityShutter = QStringLiteral("Shutter"); inline const QString kCapabilityState = QStringLiteral("State"); inline const QString kCapabilityConfiguration = QStringLiteral("Configuration"); + inline const QString kCapabilityAutofocus = QStringLiteral("Autofocus"); inline const QString kFrameDeliveryModePreviewLatest = QStringLiteral("PreviewLatest"); inline const QString kFrameDeliveryModeLatestOnly = QStringLiteral("LatestOnly"); @@ -155,9 +177,14 @@ namespace scopeone::core::internal::driverhost inline const QString kExecutableFileName = QStringLiteral("ScopeOne_DriverHost"); #endif - inline QString controlServerName(const QString& deviceId) + // A unix socket path holds at most 104 characters on macOS, where the temporary folder takes + // about half of them, and a name with the 36 character host key made the server fail to + // listen. The key is hashed to a name of fixed length. + inline QString controlServerName(const QString& hostKey) { - return QStringLiteral("ScopeOne.DriverHost.%1.ctrl").arg(deviceId); + const QByteArray hash = + QCryptographicHash::hash(hostKey.toUtf8(), QCryptographicHash::Sha1).toHex().left(16); + return QStringLiteral("ScopeOne.%1.ctrl").arg(QString::fromLatin1(hash)); } // On Unix the System V key is a file that ftok reads, so keep it in the @@ -172,11 +199,6 @@ namespace scopeone::core::internal::driverhost #endif } - inline QString sharedMemoryKey(const QString& deviceId) - { - return sharedMemoryKeyPath(QStringLiteral("ScopeOne.DriverHost.%1.shm").arg(deviceId)); - } - inline QString sharedMemoryKey(const QString& hostKey, int cameraIndex) { return sharedMemoryKeyPath(QStringLiteral("ScopeOne.DriverHost.%1.%2.shm") diff --git a/ScopeOneCore/internal/DriverHostProviderProxy.h b/ScopeOneCore/internal/DriverHostProviderProxy.h deleted file mode 100644 index 9219363..0000000 --- a/ScopeOneCore/internal/DriverHostProviderProxy.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include - -#include "scopeone/HardwareProvider.h" - -namespace scopeone::core::internal -{ - HardwareProviderPtr createDriverHostProviderProxy(const QString& providerId, - const QString& driverPath, - const QJsonObject& options, - QString* errorMessage = nullptr); -} diff --git a/ScopeOneCore/internal/FrameDeliveryMode.h b/ScopeOneCore/internal/FrameDeliveryMode.h new file mode 100644 index 0000000..5d95bdc --- /dev/null +++ b/ScopeOneCore/internal/FrameDeliveryMode.h @@ -0,0 +1,18 @@ +#pragma once + +namespace scopeone::core::internal +{ + // The live view shows at most one frame of a camera in this interval, about 60 a second + inline constexpr int kPreviewFrameIntervalMs = 16; + + // Which frames a running camera hands on + enum class FrameDeliveryMode + { + // The newest frame, at most about 60 times a second, for the live view + PreviewLatest, + // The newest frame each time one arrives, for live processing + LatestOnly, + // Every frame in order, for recording + AllFrames + }; +} diff --git a/ScopeOneCore/internal/HardwareRuntime.h b/ScopeOneCore/internal/HardwareRuntime.h index f0fba94..899442a 100644 --- a/ScopeOneCore/internal/HardwareRuntime.h +++ b/ScopeOneCore/internal/HardwareRuntime.h @@ -2,20 +2,32 @@ #include #include +#include #include #include #include #include -#include "internal/AcquisitionHardware.h" #include "scopeone/HardwareProvider.h" #include "scopeone/CameraProvider.h" #include "internal/AcquisitionEngine.h" -#include "internal/CameraRuntimeControl.h" +#include "internal/CameraFrameDelivery.h" +#include "internal/MicroManagerProvider.h" namespace scopeone::core::internal { + class DaqDeviceManager; + class SignalSourceManager; + + // The values that one device property takes on successive triggers of the camera + struct PropertySequence + { + QString device; + QString property; + std::vector values; + }; + class DeviceRegistry : public QObject { Q_OBJECT @@ -49,134 +61,159 @@ namespace scopeone::core::internal QHash m_providers; }; - class HardwareRuntime : public QObject, - public CameraProvider, - public StageProvider, - public ShutterProvider, - public StateProvider, - public ConfigurationProvider, - public AcquisitionHardware, - public CameraRuntimeControl + // The hardware of every provider, for the parts of ScopeOne above the providers, which reach + // the hardware only through it. It routes each call to the provider of the device, and the + // calls that span providers, such as "All" cameras or the default stages, it answers itself. + // Every provider reaches it through the same interfaces, those of the driver SDK. + class HardwareRuntime : public QObject { Q_OBJECT public: explicit HardwareRuntime(QObject* parent = nullptr); - void setFrameSink(FrameSink sink) override; - void setPreviewStateSink(PreviewStateSink sink) override; - bool startPreview() override; - bool stopPreview() override; - bool startPreviewFor(const QString& cameraId) override; - bool stopPreviewFor(const QString& cameraId) override; - bool isPreviewRunning(const QString& cameraId) const override; - bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const override; - bool setExposure(const QString& cameraIdOrAll, double exposureMs) override; - QStringList listProperties(const QString& cameraId) override; + bool startPreview(); + bool stopPreview(); + bool startPreviewFor(const QString& cameraId); + bool stopPreviewFor(const QString& cameraId); + bool isPreviewRunning(const QString& cameraId) const; + bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const; + bool setExposure(const QString& cameraIdOrAll, double exposureMs); + QStringList listProperties(const QString& cameraId); QString getProperty(const QString& cameraId, const QString& name, - bool fromCache) override; + bool fromCache = false); bool setProperty(const QString& cameraId, const QString& name, const QString& value, - QString* errorMessage) override; - QString getPropertyType(const QString& cameraId, const QString& name) override; - bool isPropertyReadOnly(const QString& cameraId, const QString& name) override; - bool isPropertyPreInit(const QString& cameraId, const QString& name) override; + QString* errorMessage = nullptr); + QString getPropertyType(const QString& cameraId, const QString& name); + bool isPropertyReadOnly(const QString& cameraId, const QString& name); + bool isPropertyPreInit(const QString& cameraId, const QString& name); QStringList getAllowedPropertyValues(const QString& cameraId, - const QString& name) override; - bool hasPropertyLimits(const QString& cameraId, const QString& name) override; - double getPropertyLowerLimit(const QString& cameraId, const QString& name) override; - double getPropertyUpperLimit(const QString& cameraId, const QString& name) override; - bool setROI(const QString& cameraId, int x, int y, int width, int height) override; - bool clearROI(const QString& cameraId) override; + const QString& name); + bool hasPropertyLimits(const QString& cameraId, const QString& name); + double getPropertyLowerLimit(const QString& cameraId, const QString& name); + double getPropertyUpperLimit(const QString& cameraId, const QString& name); + bool setROI(const QString& cameraId, int x, int y, int width, int height); + bool clearROI(const QString& cameraId); bool getROI(const QString& cameraId, int& x, int& y, int& width, - int& height) override; + int& height); bool captureEventFrame(const QString& cameraId, ImageFrame& frame, - int timeoutMs) override; - bool supportsEventSequence(const QString& cameraId) const override; + int timeoutMs = 1500); + bool supportsEventSequence(const QString& cameraId) const; bool captureEventSequence(const QString& cameraId, int count, std::vector& frames, - int timeoutMs) override; - void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) override; + int timeoutMs = 10000); + void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused); bool setRecordingFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) override; + bool enabled); bool setHighRateFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) override; - bool isProcessingFrameTokenCurrent(const QString& cameraId, quint64 token) override; - void finishProcessingFrame(const QString& cameraId, quint64 token) override; - QString defaultXYStage() const override; - QString defaultZStage() const override; + bool enabled); + bool isProcessingFrameTokenCurrent(const QString& cameraId, quint64 token); + void finishProcessingFrame(const QString& cameraId, quint64 token); + QString defaultXYStage() const; + QString defaultZStage() const; bool getXYPosition(const QString& deviceId, double& x, double& y, - QString* errorMessage) const override; + QString* errorMessage = nullptr) const; bool getZPosition(const QString& deviceId, double& z, - QString* errorMessage) const override; + QString* errorMessage = nullptr) const; bool setRelativeXYPosition(const QString& deviceId, double dx, double dy, - QString* errorMessage) override; + QString* errorMessage = nullptr); bool setRelativeZPosition(const QString& deviceId, double dz, - QString* errorMessage) override; + QString* errorMessage = nullptr); bool setXYPosition(const QString& deviceId, double x, double y, - QString* errorMessage) override; + QString* errorMessage = nullptr); bool setZPosition(const QString& deviceId, double z, - QString* errorMessage) override; + QString* errorMessage = nullptr); bool isShutterOpen(const QString& deviceId, bool& open, - QString* errorMessage) const override; + QString* errorMessage = nullptr) const; bool setShutterOpen(const QString& deviceId, bool open, - QString* errorMessage) override; + QString* errorMessage = nullptr); bool getState(const QString& deviceId, long& state, - QString* errorMessage) const override; + QString* errorMessage = nullptr) const; bool setState(const QString& deviceId, long state, - QString* errorMessage) override; - QString stateLabel(const QString& deviceId, long state) const override; - QStringList availableConfigGroups() const override; - QStringList availableConfigs(const QString& groupName) const override; - QString currentConfig(const QString& groupName) const override; + QString* errorMessage = nullptr); + QString stateLabel(const QString& deviceId, long state) const; + QStringList availableConfigGroups() const; + QStringList availableConfigs(const QString& groupName) const; + QString currentConfig(const QString& groupName) const; std::vector configSettings( - const QString& groupName, const QString& configName) const override; + const QString& groupName, const QString& configName) const; bool setConfig(const QString& groupName, const QString& configName, - QString* errorMessage) override; - - bool autoShutter(bool& enabled, QString* errorMessage = nullptr) const override; - bool setAutoShutter(bool enabled, QString* errorMessage = nullptr) override; - bool setCoreShutterOpen(bool open, QString* errorMessage = nullptr) override; - bool hasAutofocus() const override; - bool runAutofocus(QString* errorMessage = nullptr) override; - long focusSequenceCapacity(const QString& focusDevice) const override; + QString* errorMessage = nullptr); + + bool autoShutter(bool& enabled, QString* errorMessage = nullptr) const; + bool setAutoShutter(bool enabled, QString* errorMessage = nullptr); + // Opens or closes the shutter that lights the sample for the cameras + bool setDefaultShutterOpen(bool open, QString* errorMessage = nullptr); + bool hasAutofocus() const; + bool runAutofocus(QString* errorMessage = nullptr); + long focusSequenceCapacity(const QString& focusDevice) const; bool startFocusSequence(const QString& focusDevice, const std::vector& positions, - QString* errorMessage = nullptr) override; - void stopFocusSequence(const QString& focusDevice) override; - long propertySequenceCapacity(const QString& device, const QString& property) const override; + QString* errorMessage = nullptr); + void stopFocusSequence(const QString& focusDevice); + long propertySequenceCapacity(const QString& device, const QString& property) const; bool startPropertySequences(const std::vector& sequences, - QString* errorMessage = nullptr) override; - void stopPropertySequences(const std::vector& sequences) override; - - DeviceRegistry* deviceRegistry() { return &m_registry; } - const DeviceRegistry* deviceRegistry() const { return &m_registry; } + QString* errorMessage = nullptr); + void stopPropertySequences(const std::vector& sequences); + + // Where the frames of every camera arrive + CameraFrameDelivery* frameDelivery() { return &m_frameDelivery; } + SignalSourceManager* signalSourceManager() const { return m_signalSourceManager; } + DaqDeviceManager* daqDeviceManager() const { return m_daqDeviceManager; } + double pixelSizeUm(const QString& cameraId) const; + + // A driver in a DriverHost of its own. Safe to call from any thread, and the provider is + // registered afterwards on the thread of the runtime. + static HardwareProviderPtr startScopeOneProvider(const QString& providerId, + const QString& driverPath, + const QJsonObject& options, + QString* errorMessage); + + // The Micro-Manager configuration. Loading and unloading block, so they run on a worker + // thread, while the cameras are started and stopped on the thread of the runtime. + QStringList microManagerAdapterSearchPaths() const; + void setMicroManagerAdapterSearchPaths(const QStringList& paths); + bool loadMicroManagerConfiguration(const QString& configPath, + MicroManagerProvider::LoadResult& result, + QString& errorMessage); + QStringList startMicroManagerCameras(const QList& cameras); + // The cameras, to be destroyed on a worker thread, as their DriverHosts take time to exit + QList takeMicroManagerCameras(); + // Registers the devices again, false when one conflicts with the devices of a driver + bool setMicroManagerDevices(const QList& devices); + void stopMicroManagerPreview(); + bool unloadMicroManagerDevices(QString* errorMessage = nullptr); + void applyMicroManagerShutdownPreset(); + QList devices() const { return m_registry.devices(); } + bool hasProvider(const QString& providerId) const { return m_registry.provider(providerId) != nullptr; } + static QString microManagerVersion(); void clear(); bool registerProvider(const HardwareProviderPtr& provider); void unregisterProvider(const QString& providerId); bool refreshProvider(const QString& providerId); - bool stopPreviewForProvider(const QString& providerId); + void stopPreviewForProvider(const QString& providerId); signals: void frameReady(const ImageFrame& frame); @@ -190,15 +227,16 @@ namespace scopeone::core::internal ShutterProvider* shutterProviderForDevice(const QString& logicalId) const; StateProvider* stateProviderForDevice(const QString& logicalId) const; ConfigurationProvider* configurationProviderForGroup(const QString& groupName) const; - QList cameraProviders() const; - QList> runtimeControlsFor( - const QStringList& cameraIds) const; + QList> frameSourcesFor(const QStringList& cameraIds) const; + bool applyFrameDeliveryModes(const QHash& modes); + DeviceRegistry m_registry; - AcquisitionHardware* acquisitionHardware() const; + std::shared_ptr m_microManager; + SignalSourceManager* m_signalSourceManager{nullptr}; + DaqDeviceManager* m_daqDeviceManager{nullptr}; AcquisitionEngine m_acquisitionEngine; - FrameSink m_frameSink; - PreviewStateSink m_previewStateSink; + CameraFrameDelivery m_frameDelivery; }; } diff --git a/ScopeOneCore/internal/MDAManager.h b/ScopeOneCore/internal/MDAManager.h index 57fb81f..f820143 100644 --- a/ScopeOneCore/internal/MDAManager.h +++ b/ScopeOneCore/internal/MDAManager.h @@ -10,10 +10,8 @@ #include #include #include -#include "internal/AcquisitionHardware.h" +#include "internal/HardwareRuntime.h" #include "scopeone/ExperimentDocument.h" -#include "scopeone/CameraProvider.h" -#include "scopeone/HardwareCapabilities.h" namespace scopeone::core::internal { @@ -37,10 +35,7 @@ namespace scopeone::core::internal bool isRunning() const { return m_running.load(); } - void setCameraProvider(CameraProvider* cameraProvider); - void setStageProvider(StageProvider* stageProvider); - void setConfigurationProvider(ConfigurationProvider* configurationProvider); - void setAcquisitionHardware(AcquisitionHardware* hardware); + void setHardwareRuntime(HardwareRuntime* hardwareRuntime); bool start(const QList& events); void requestCancel(); void cancelAndWait(); @@ -141,10 +136,7 @@ namespace scopeone::core::internal bool moveZ(double z, QString* errorMessage); void runSequence(QList events); - CameraProvider* m_cameraProvider{nullptr}; - StageProvider* m_stageProvider{nullptr}; - ConfigurationProvider* m_configurationProvider{nullptr}; - AcquisitionHardware* m_acquisitionHardware{nullptr}; + HardwareRuntime* m_hardwareRuntime{nullptr}; QThreadPool m_threadPool; std::atomic m_running{false}; std::atomic m_cancelRequested{false}; diff --git a/ScopeOneCore/internal/MMCoreManager.h b/ScopeOneCore/internal/MMCoreManager.h deleted file mode 100644 index 0f384c1..0000000 --- a/ScopeOneCore/internal/MMCoreManager.h +++ /dev/null @@ -1,64 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include "MMCore.h" -#include "scopeone/HardwareTypes.h" - -namespace scopeone::core::internal -{ - class CameraManager; - - class MMCoreManager : public QObject - { - Q_OBJECT - - public: - struct CameraLoadInfo - { - QString label; - QString adapter; - QString device; - QStringList preInitProperties; - QStringList properties; - double exposureMs{10.0}; - }; - - struct LoadConfigResult - { - QStringList cameraIds; - QList cameras; - QStringList failedDevices; - int successCount{0}; - int failCount{0}; - int skippedCameraCount{0}; - bool foundCamera{false}; - bool useSingleCamera{false}; - QList devices; - }; - - explicit MMCoreManager(QObject* parent = nullptr); - ~MMCoreManager() override = default; - - std::shared_ptr getCore() const { return m_mmcore; } - const QStringList& additionalDeviceAdapterSearchPaths() const - { - return m_additionalDeviceAdapterSearchPaths; - } - void setAdditionalDeviceAdapterSearchPaths(const QStringList& paths) - { - m_additionalDeviceAdapterSearchPaths = paths; - } - - bool loadConfigurationDevices(const QString& configPath, - LoadConfigResult& result, - QString& errorMessage); - bool startCameraBackends(CameraManager& cameraManager, - LoadConfigResult& result); - private: - std::shared_ptr m_mmcore; - QStringList m_additionalDeviceAdapterSearchPaths; - }; -} diff --git a/ScopeOneCore/internal/MicroManagerCalls.h b/ScopeOneCore/internal/MicroManagerCalls.h new file mode 100644 index 0000000..977539d --- /dev/null +++ b/ScopeOneCore/internal/MicroManagerCalls.h @@ -0,0 +1,125 @@ +#pragma once + +#include +#include + +#include +#include + +#include "MMCore.h" +#include "scopeone/HardwareTypes.h" + +// Helpers for the code that calls MMCore, MicroManagerProvider and MicroManagerCamera +namespace scopeone::core::internal::micromanager +{ + inline HardwareProviderDescriptor descriptor() + { + return {QStringLiteral("micro-manager"), QStringLiteral("Micro-Manager"), QStringLiteral("MMCore")}; + } + + // MMCore takes device and property names as trimmed narrow strings + inline std::string name8(const QString& name) + { + return name.trimmed().toStdString(); + } + + inline QStringList toQStringList(const std::vector& values) + { + QStringList result; + result.reserve(static_cast(values.size())); + for (const std::string& value : values) + { + result.append(QString::fromStdString(value)); + } + return result; + } + + // Runs an MMCore query and returns the fallback when the core reports an error + template + T orElse(T fallback, Call&& call) + { + try + { + return call(); + } + catch (const CMMError&) + { + return fallback; + } + } + + // Runs an MMCore command and reports a core error through errorMessage + template + bool succeeds(QString* errorMessage, Call&& call) + { + if (errorMessage) + { + errorMessage->clear(); + } + try + { + call(); + return true; + } + catch (const CMMError& error) + { + if (errorMessage) + { + *errorMessage = QString::fromStdString(error.getMsg()); + } + return false; + } + } + + inline QString propertyType(CMMCore& core, const QString& device, const QString& property) + { + return orElse(QStringLiteral("Unknown"), [&] + { + switch (core.getPropertyType(name8(device).c_str(), name8(property).c_str())) + { + case MM::String: return QStringLiteral("String"); + case MM::Float: return QStringLiteral("Float"); + case MM::Integer: return QStringLiteral("Integer"); + default: return QStringLiteral("Unknown"); + } + }); + } + + inline long propertySequenceCapacity(CMMCore& core, const QString& device, const QString& property) + { + return orElse(0L, [&] + { + const std::string label = name8(device); + const std::string name = name8(property); + return core.isPropertySequenceable(label.c_str(), name.c_str()) + ? core.getPropertySequenceMaxLength(label.c_str(), name.c_str()) + : 0L; + }); + } + + inline bool startPropertySequence(CMMCore& core, + const QString& device, + const QString& property, + const QStringList& values, + QString* errorMessage) + { + return succeeds(errorMessage, [&] + { + const std::string label = name8(device); + const std::string name = name8(property); + std::vector sequence; + sequence.reserve(static_cast(values.size())); + for (const QString& value : values) + { + sequence.push_back(value.toStdString()); + } + core.loadPropertySequence(label.c_str(), name.c_str(), sequence); + core.startPropertySequence(label.c_str(), name.c_str()); + }); + } + + inline void stopPropertySequence(CMMCore& core, const QString& device, const QString& property) + { + succeeds(nullptr, [&] { core.stopPropertySequence(name8(device).c_str(), name8(property).c_str()); }); + } +} diff --git a/ScopeOneCore/internal/MicroManagerCamera.h b/ScopeOneCore/internal/MicroManagerCamera.h new file mode 100644 index 0000000..5b86bfa --- /dev/null +++ b/ScopeOneCore/internal/MicroManagerCamera.h @@ -0,0 +1,133 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "internal/FrameDeliveryMode.h" +#include "scopeone/CameraProvider.h" +#include "scopeone/HardwareProvider.h" + +class CMMCore; + +namespace scopeone::core::internal +{ + // What a camera of a Micro-Manager configuration needs to be loaded again on a core of its own + struct MicroManagerCameraSetup + { + QString cameraId; + QString adapter; + QString device; + // {"name", "value"} objects, applied before and after the camera is initialized + QJsonArray preInitProperties; + QJsonArray properties; + double exposureMs{0.0}; + + QJsonObject toJson() const; + static MicroManagerCameraSetup fromJson(const QJsonObject& object); + }; + + class MicroManagerCameraPoller; + + // One Micro-Manager camera on a CMMCore. A configuration with one camera runs it on the core of + // ScopeOne. A configuration with more runs each camera in a DriverHost of its own, on a core that + // holds only that camera, because a vendor library loaded twice in one process shares its state + // and two cameras of the same model would stop each other. + class MicroManagerCamera final : public HardwareProvider, public CameraProvider + { + public: + // A camera that lends its frames hands out frames that point into the circular buffer of + // the core. Such a frame is valid only during the call of the frame sink. + MicroManagerCamera(std::shared_ptr core, QString cameraId, bool lendFrames); + ~MicroManagerCamera() override; + + // Loads the camera on a new core that holds only this camera + static std::shared_ptr load(const MicroManagerCameraSetup& setup, + bool lendFrames, + QString* errorMessage); + + QString cameraId() const { return m_cameraId; } + + HardwareProviderDescriptor descriptor() const override; + QList devices() const override; + + void setFrameSink(FrameSink sink) override; + void setPreviewStateSink(PreviewStateSink sink) override; + // Called with the error when the running acquisition fails + void setErrorSink(std::function sink); + // Asked in LatestOnly mode before the newest frame of a poll is copied + void setFrameFilter(std::function filter); + void setFrameDeliveryMode(FrameDeliveryMode mode); + // A pause stops reading the circular buffer, and its end restarts a live acquisition that + // an event capture stopped + void setFrameDeliveryPaused(bool paused); + + bool startPreviewFor(const QString& cameraId) override; + bool stopPreviewFor(const QString& cameraId) override; + bool isPreviewRunning(const QString& cameraId) const override; + bool getExposure(const QString& cameraId, double& exposureMs) const override; + bool setExposure(const QString& cameraId, double exposureMs) override; + bool setROI(const QString& cameraId, int x, int y, int width, int height) override; + bool clearROI(const QString& cameraId) override; + bool getROI(const QString& cameraId, int& x, int& y, int& width, int& height) override; + bool captureEventFrame(const QString& cameraId, ImageFrame& frame, int timeoutMs) override; + bool supportsEventSequence(const QString& cameraId) const override; + bool captureEventSequence(const QString& cameraId, + int count, + std::vector& frames, + int timeoutMs) override; + + QStringList listProperties(const QString& deviceId) override; + QString getProperty(const QString& deviceId, const QString& name, bool fromCache) override; + bool setProperty(const QString& deviceId, + const QString& name, + const QString& value, + QString* errorMessage) override; + QString getPropertyType(const QString& deviceId, const QString& name) override; + bool isPropertyReadOnly(const QString& deviceId, const QString& name) override; + bool isPropertyPreInit(const QString& deviceId, const QString& name) override; + QStringList getAllowedPropertyValues(const QString& deviceId, const QString& name) override; + bool hasPropertyLimits(const QString& deviceId, const QString& name) override; + double getPropertyLowerLimit(const QString& deviceId, const QString& name) override; + double getPropertyUpperLimit(const QString& deviceId, const QString& name) override; + long propertySequenceCapacity(const QString& deviceId, const QString& name) override; + bool startPropertySequence(const QString& deviceId, + const QString& name, + const QStringList& values, + QString* errorMessage) override; + void stopPropertySequence(const QString& deviceId, const QString& name) override; + + private: + friend class MicroManagerCameraPoller; + + bool matches(const QString& cameraId) const; + bool startAcquisition(); + void stopAcquisition(); + void setPreviewRunning(bool running); + // Stops the live acquisition before the core takes an image of its own + void suspendAcquisition(); + bool makeFrame(const void* pixels, ImageFrame& frame); + void handleAcquisitionFailure(const QString& error); + + std::shared_ptr m_core; + QString m_cameraId; + bool m_lendFrames{false}; + std::atomic m_frameIndex{0}; + std::atomic_bool m_running{false}; + bool m_paused{false}; + std::atomic_bool m_suspended{false}; + mutable QMutex m_sinkMutex; + FrameSink m_frameSink; + PreviewStateSink m_previewStateSink; + std::function m_errorSink; + std::function m_frameFilter; + QThread m_pollThread; + MicroManagerCameraPoller* m_poller{nullptr}; + }; +} diff --git a/ScopeOneCore/internal/MicroManagerProvider.h b/ScopeOneCore/internal/MicroManagerProvider.h index 43bb47a..22020fe 100644 --- a/ScopeOneCore/internal/MicroManagerProvider.h +++ b/ScopeOneCore/internal/MicroManagerProvider.h @@ -4,42 +4,80 @@ #include -#include "internal/AcquisitionHardware.h" +#include +#include + +#include + +#include "internal/CameraFrameDelivery.h" +#include "internal/MicroManagerCamera.h" #include "scopeone/HardwareProvider.h" #include "scopeone/CameraProvider.h" -#include "internal/CameraRuntimeControl.h" class CMMCore; namespace scopeone::core::internal { + // The devices of the loaded Micro-Manager configuration. They run on the core of ScopeOne, + // except the cameras of a configuration with more than one camera: each of those runs in a + // DriverHost of its own, so two cameras of the same model do not share one vendor library. class MicroManagerProvider final : public HardwareProvider, public CameraProvider, public StageProvider, public ShutterProvider, + public AutofocusProvider, public StateProvider, public ConfigurationProvider, - public AcquisitionHardware, - public CameraRuntimeControl + public CameraFrameSource { public: - MicroManagerProvider(std::shared_ptr core, - CameraProvider* cameraProvider, - CameraRuntimeControl* cameraRuntimeControl); + struct LoadResult + { + QStringList cameraIds; + QList cameras; + QStringList failedDevices; + int successCount{0}; + int failCount{0}; + int skippedCameraCount{0}; + bool foundCamera{false}; + bool useSingleCamera{false}; + QList devices; + }; + + MicroManagerProvider(); + ~MicroManagerProvider() override; + + static QString coreVersion(); + + QStringList adapterSearchPaths() const { return m_adapterSearchPaths; } + void setAdapterSearchPaths(const QStringList& paths) { m_adapterSearchPaths = paths; } + // Blocks while the devices initialize + bool loadConfiguration(const QString& configPath, LoadResult& result, QString& errorMessage); + bool unloadDevices(QString* errorMessage); + void applyShutdownPreset(); HardwareProviderDescriptor descriptor() const override; QList devices() const override; void setDevices(const QList& devices); + // Starts the cameras of a loaded configuration and returns the ones that failed. A single + // camera has to be loaded on the core already, while more are loaded again in their + // DriverHosts. + QStringList startCameras(const QList& cameras); + // Hands over the cameras, so they can be stopped away from the calling thread + QList takeCameras(); + + void setFrameDelivery(CameraFrameDelivery* delivery) override; + bool setFrameDeliveryMode(const QStringList& cameraIds, FrameDeliveryMode mode) override; + void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) override; + void setFrameSink(FrameSink sink) override; void setPreviewStateSink(PreviewStateSink sink) override; - bool startPreview() override; - bool stopPreview() override; bool startPreviewFor(const QString& cameraId) override; bool stopPreviewFor(const QString& cameraId) override; bool isPreviewRunning(const QString& cameraId) const override; - bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const override; - bool setExposure(const QString& cameraIdOrAll, double exposureMs) override; + bool getExposure(const QString& cameraId, double& exposureMs) const override; + bool setExposure(const QString& cameraId, double exposureMs) override; QStringList listProperties(const QString& cameraId) override; QString getProperty(const QString& cameraId, const QString& name, @@ -71,13 +109,6 @@ namespace scopeone::core::internal int count, std::vector& frames, int timeoutMs) override; - void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) override; - bool setRecordingFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) override; - bool setHighRateFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) override; - bool isProcessingFrameTokenCurrent(const QString& cameraId, quint64 token) override; - void finishProcessingFrame(const QString& cameraId, quint64 token) override; QString defaultXYStage() const override; QString defaultZStage() const override; bool getXYPosition(const QString& deviceId, @@ -123,27 +154,62 @@ namespace scopeone::core::internal const QString& configName, QString* errorMessage) override; - bool autoShutter(bool& enabled, QString* errorMessage = nullptr) const override; - bool setAutoShutter(bool enabled, QString* errorMessage = nullptr) override; - bool setCoreShutterOpen(bool open, QString* errorMessage = nullptr) override; - bool hasAutofocus() const override; - bool runAutofocus(QString* errorMessage = nullptr) override; - long focusSequenceCapacity(const QString& focusDevice) const override; - bool startFocusSequence(const QString& focusDevice, + QString defaultShutter() const override; + bool autoShutter() const override; + bool setAutoShutter(bool enabled, QString* errorMessage) override; + QString defaultAutofocus() const override; + bool runAutofocus(const QString& deviceId, QString* errorMessage) override; + long stageSequenceCapacity(const QString& deviceId) const override; + bool startStageSequence(const QString& deviceId, const std::vector& positions, - QString* errorMessage = nullptr) override; - void stopFocusSequence(const QString& focusDevice) override; - long propertySequenceCapacity(const QString& device, const QString& property) const override; - bool startPropertySequences(const std::vector& sequences, - QString* errorMessage = nullptr) override; - void stopPropertySequences(const std::vector& sequences) override; + QString* errorMessage) override; + void stopStageSequence(const QString& deviceId) override; + long propertySequenceCapacity(const QString& deviceId, const QString& name) override; + bool startPropertySequence(const QString& deviceId, + const QString& name, + const QStringList& values, + QString* errorMessage) override; + void stopPropertySequence(const QString& deviceId, const QString& name) override; + double pixelSizeUm(const QString& cameraId) const override; private: - bool isCamera(const QString& deviceId) const; + struct Camera + { + // A MicroManagerCamera on the core of ScopeOne, or the DriverHost of the camera + HardwareProviderPtr provider; + CameraProvider* camera{nullptr}; + CameraFrameSource* frameSource{nullptr}; + std::shared_ptr local; + // The index of the last frame the local camera delivered, 0 after a change of mode + std::shared_ptr> lastFrameIndex; + }; + + // A camera held while a call uses it, so unloading the configuration on another thread + // cannot free it meanwhile + struct CameraRef + { + HardwareProviderPtr owner; + CameraProvider* camera{nullptr}; + explicit operator bool() const { return camera != nullptr; } + CameraProvider* operator->() const { return camera; } + }; + + bool startLocalCamera(const QString& cameraId); + CameraRef camera(const QString& cameraId) const; + Camera cameraEntry(const QString& cameraId) const; + void readBinning(const QString& cameraId); + QList cameras() const; std::shared_ptr m_core; - CameraProvider* m_cameraProvider{nullptr}; - CameraRuntimeControl* m_cameraRuntimeControl{nullptr}; + QStringList m_adapterSearchPaths; QList m_devices; + // Written when a configuration loads or unloads, read by calls on any thread + mutable QReadWriteLock m_camerasLock; + QMap m_cameras; + // The binning of each camera in a DriverHost, read when it changes, as the core of + // ScopeOne does not hold these cameras to apply it to the pixel size + QHash m_binnings; + std::atomic m_delivery{nullptr}; + PreviewStateSink m_previewStateSink; }; } diff --git a/ScopeOneCore/internal/PtuFile.h b/ScopeOneCore/internal/PtuFile.h deleted file mode 100644 index 2453818..0000000 --- a/ScopeOneCore/internal/PtuFile.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "internal/SignalSourceManager.h" - -namespace scopeone::core::internal -{ - class PtuFileSourceFactory final : public SignalSourceFactory - { - public: - QList signalSources() const override; - SignalSource* createSignalSource( - const QString& sourceId, - QObject* parent = nullptr) override; - }; -} diff --git a/ScopeOneCore/internal/RecordingManager.h b/ScopeOneCore/internal/RecordingManager.h index 4480552..b81ce27 100644 --- a/ScopeOneCore/internal/RecordingManager.h +++ b/ScopeOneCore/internal/RecordingManager.h @@ -1,10 +1,7 @@ #pragma once #include "scopeone/ScopeOneCore.h" -#include "scopeone/CameraProvider.h" -#include "scopeone/HardwareCapabilities.h" #include "internal/MDAManager.h" -#include "internal/CameraRuntimeControl.h" #include #include #include @@ -34,14 +31,7 @@ namespace scopeone::core::internal explicit RecordingManager(QObject* parent = nullptr); ~RecordingManager() override; - void setCameraProvider(CameraProvider* cameraProvider) { m_cameraProvider = cameraProvider; } - void setStageProvider(StageProvider* stageProvider) { m_stageProvider = stageProvider; } - void setConfigurationProvider(ConfigurationProvider* provider) { m_configurationProvider = provider; } - void setAcquisitionHardware(AcquisitionHardware* hardware) { m_acquisitionHardware = hardware; } - void setCameraRuntimeControl(CameraRuntimeControl* cameraRuntimeControl) - { - m_cameraRuntimeControl = cameraRuntimeControl; - } + void setHardwareRuntime(HardwareRuntime* hardwareRuntime) { m_hardwareRuntime = hardwareRuntime; } void setLatestFrameFetcher(std::function fetcher) { m_latestFrameFetcher = std::move(fetcher); @@ -213,11 +203,7 @@ namespace scopeone::core::internal bool allCamerasReachedTarget() const; void advanceBurstStateIfNeeded(); - CameraProvider* m_cameraProvider{nullptr}; - StageProvider* m_stageProvider{nullptr}; - ConfigurationProvider* m_configurationProvider{nullptr}; - AcquisitionHardware* m_acquisitionHardware{nullptr}; - CameraRuntimeControl* m_cameraRuntimeControl{nullptr}; + HardwareRuntime* m_hardwareRuntime{nullptr}; std::function m_latestFrameFetcher; std::function m_sessionPreparationCallback; diff --git a/ScopeOneCore/internal/SignalSourceManager.h b/ScopeOneCore/internal/SignalSourceManager.h index bdd728a..d6726bd 100644 --- a/ScopeOneCore/internal/SignalSourceManager.h +++ b/ScopeOneCore/internal/SignalSourceManager.h @@ -2,10 +2,6 @@ #include #include -#include - -#include -#include #include "scopeone/HardwareProvider.h" #include "scopeone/SignalSource.h" @@ -15,41 +11,8 @@ namespace scopeone::core::internal { class DeviceRegistry; - // A signal source built into Core, such as the reader of PTU files - class SignalSource : public QObject - { - Q_OBJECT - - public: - explicit SignalSource(QObject* parent = nullptr); - ~SignalSource() override; - - virtual bool start(const SignalAcquisitionConfig& config, - QString* errorMessage = nullptr) = 0; - virtual void stop() = 0; - virtual SignalSourceState state() const = 0; - virtual QString stateMessage() const = 0; - - signals: - void timeSeriesReady(const scopeone::core::TimeSeriesChunk& chunk); - void timestampedEventsReady(const scopeone::core::TimestampedEventChunk& chunk); - void stateChanged(scopeone::core::SignalSourceState state, - const QString& message); - void sourceError(const QString& errorMessage); - }; - - // A kind of signal source built into Core, which lists its sources and creates them - class SignalSourceFactory - { - public: - virtual ~SignalSourceFactory() = default; - virtual QList signalSources() const = 0; - virtual SignalSource* createSignalSource(const QString& sourceId, - QObject* parent = nullptr) = 0; - }; - - // The signal sources of Core and of the drivers under one list. The chunks of a - // driver source arrive on a thread of its provider and are passed on in this object's thread. + // The signal sources of the drivers under one list. Their chunks arrive on a thread of + // the provider and are passed on in this object's thread. class SignalSourceManager final : public QObject { Q_OBJECT @@ -75,20 +38,14 @@ namespace scopeone::core::internal void sourceError(const QString& sourceId, const QString& errorMessage); private: - void registerBuiltInSources(); void refreshProviderSources(); void detachProvider(const HardwareProviderPtr& provider); void updateState(const QString& sourceId, SignalSourceState state, const QString& message); - SignalSource* sourceInstance(const QString& sourceId, - QString* errorMessage); DeviceRegistry* m_registry{nullptr}; - std::vector> m_factories; - QHash m_factoryBySource; QHash m_providerBySource; QHash m_attachedProviders; QHash m_descriptors; - QHash> m_sources; QHash m_states; QHash m_messages; }; diff --git a/ScopeOneCore/python/scopeone/README.md b/ScopeOneCore/python/scopeone/README.md index 0a56d22..937d5bb 100644 --- a/ScopeOneCore/python/scopeone/README.md +++ b/ScopeOneCore/python/scopeone/README.md @@ -298,7 +298,7 @@ ScopeOne uses one local control pipe for JSON commands and one shared-memory blo - `capabilities`: response `capabilities` with operation groups and hardware, filesystem, destructive, and long-running operation classifications. - `state_snapshot`: response `snapshot` with application and Core versions, configuration, hardware inventory, preview, processing, scene, live acquisition and writer progress, experiment, and session state. - `frame_mapping_info`: response `mappingName`, `mappingSize`, `headerBytes`, `maxPayloadBytes`, and supported `pixelFormats`. -- `load_config`: fields `configPath`; response `state`, `complete`, `cameraIds`, `failedDevices`, `successCount`, `failCount`, and `skippedCameraCount`. A valid configuration can finish as `partially_loaded` when non-camera devices fail; camera backend startup failures are cleaned up and returned as an error. +- `load_config`: fields `configPath`; response `state`, `complete`, `cameraIds`, `failedDevices`, `successCount`, `failCount`, and `skippedCameraCount`. A valid configuration can finish as `partially_loaded` when non-camera devices fail; cameras that fail to start are cleaned up and returned as an error. - `unload_config`: unload current Micro-Manager config and return `state` and `complete`. A failed unload returns `state: "failed"` and an error while preserving the actual hardware inventory for recovery. - `camera_ids`: response `cameraIds`. - `loaded_devices`: response `devices`. diff --git a/ScopeOneCore/sdk/include/scopeone/CameraProvider.h b/ScopeOneCore/sdk/include/scopeone/CameraProvider.h index 71cfa34..cd3e6d3 100644 --- a/ScopeOneCore/sdk/include/scopeone/CameraProvider.h +++ b/ScopeOneCore/sdk/include/scopeone/CameraProvider.h @@ -11,23 +11,39 @@ namespace scopeone::core { + // A provider whose devices include cameras. Every provider implements this interface, the + // drivers as well as Micro-Manager inside ScopeOne, and every call names one camera by its id. + // + // Threads: DriverHost calls the methods of a driver one at a time, from one thread, so they + // need no locks against each other. The frame sink may be called from any thread, such as a + // callback thread of the camera SDK, while a method runs. + // + // Frames: Mono8 or Mono16, at most 16 MB each; a larger frame is not delivered. frameIndex + // counts the images the camera took, from 1, including those the provider did not hand on, + // so ScopeOne can tell the frame rate and see frames that were lost. timestampNs is the time + // the image was taken, in nanoseconds since the Unix epoch, or 0 to let ScopeOne stamp it. class SCOPEONE_SDK_EXPORT CameraProvider : public DevicePropertyProvider { public: + // ScopeOne copies the frame before the sink returns, so the bytes of a frame may point + // into a buffer of the camera, for example through QByteArray::fromRawData, and need to + // stay valid only during the call. The copy takes about a millisecond for 8 MB. using FrameSink = std::function; + // Called with whether any camera of the provider is running, whenever that may change using PreviewStateSink = std::function; ~CameraProvider() override; + // Once a new sink is set, the old one is no longer called from any thread virtual void setFrameSink(FrameSink sink) = 0; virtual void setPreviewStateSink(PreviewStateSink sink) = 0; - virtual bool startPreview() = 0; - virtual bool stopPreview() = 0; + // Starts the continuous acquisition of a camera. Starting a running camera succeeds. virtual bool startPreviewFor(const QString& cameraId) = 0; + // Once it returns, the camera hands no further frame to the sink virtual bool stopPreviewFor(const QString& cameraId) = 0; virtual bool isPreviewRunning(const QString& cameraId) const = 0; - virtual bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const = 0; - virtual bool setExposure(const QString& cameraIdOrAll, double exposureMs) = 0; + virtual bool getExposure(const QString& cameraId, double& exposureMs) const = 0; + virtual bool setExposure(const QString& cameraId, double exposureMs) = 0; virtual bool setROI(const QString& cameraId, int x, int y, int width, int height) = 0; virtual bool clearROI(const QString& cameraId) = 0; virtual bool getROI(const QString& cameraId, @@ -35,9 +51,20 @@ namespace scopeone::core int& y, int& width, int& height) = 0; + // Takes one new image with the current settings, started after the call, and returns it + // instead of handing it to the sink. A running acquisition may be stopped for it. + // timeoutMs is how long ScopeOne waits for the image; a provider that waits for the camera + // itself should give up after it. virtual bool captureEventFrame(const QString& cameraId, ImageFrame& frame, int timeoutMs = 1500) = 0; + // The size of a pixel in the sample in micrometers, with the current objective and + // binning, or 0 when the provider does not know it + virtual double pixelSizeUm(const QString& cameraId) const + { + (void)cameraId; + return 0.0; + } // Whether the camera can take a series of images in one sequence acquisition, which // lets the hardware time the images, for example while a stage steps through a // sequence of positions diff --git a/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h b/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h index faa4827..f789d57 100644 --- a/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h +++ b/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h @@ -30,6 +30,33 @@ namespace scopeone::core virtual bool hasPropertyLimits(const QString& deviceId, const QString& name) = 0; virtual double getPropertyLowerLimit(const QString& deviceId, const QString& name) = 0; virtual double getPropertyUpperLimit(const QString& deviceId, const QString& name) = 0; + + // A property sequence steps a property through a list of values, one value on each + // trigger of the camera, without a call per value. The capacity is the longest list the + // device takes, 0 when the property cannot be sequenced. + virtual long propertySequenceCapacity(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return 0; + } + // Loads the values and arms the property for the next triggers + virtual bool startPropertySequence(const QString& deviceId, + const QString& name, + const QStringList& values, + QString* errorMessage = nullptr) + { + (void)deviceId; + (void)name; + (void)values; + if (errorMessage) *errorMessage = QStringLiteral("The property cannot be sequenced"); + return false; + } + virtual void stopPropertySequence(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + } }; class SCOPEONE_SDK_EXPORT StageProvider @@ -60,6 +87,29 @@ namespace scopeone::core virtual bool setZPosition(const QString& deviceId, double z, QString* errorMessage = nullptr) = 0; + + // A stage sequence steps a Z stage through a list of positions, one position on each + // trigger of the camera. The capacity is the longest list the stage takes, 0 when it + // cannot be sequenced. + virtual long stageSequenceCapacity(const QString& deviceId) const + { + (void)deviceId; + return 0; + } + // Loads the positions and arms the stage for the next triggers + virtual bool startStageSequence(const QString& deviceId, + const std::vector& positions, + QString* errorMessage = nullptr) + { + (void)deviceId; + (void)positions; + if (errorMessage) *errorMessage = QStringLiteral("The stage cannot be sequenced"); + return false; + } + virtual void stopStageSequence(const QString& deviceId) + { + (void)deviceId; + } }; class SCOPEONE_SDK_EXPORT ShutterProvider @@ -73,6 +123,32 @@ namespace scopeone::core virtual bool setShutterOpen(const QString& deviceId, bool open, QString* errorMessage = nullptr) = 0; + + // The shutter that lights the sample while a camera of the provider takes an image + virtual QString defaultShutter() const + { + return {}; + } + // Whether the provider opens the default shutter around each image by itself + virtual bool autoShutter() const + { + return false; + } + virtual bool setAutoShutter(bool enabled, QString* errorMessage = nullptr) + { + if (errorMessage && enabled) *errorMessage = QStringLiteral("The provider has no automatic shutter"); + return !enabled; + } + }; + + class SCOPEONE_SDK_EXPORT AutofocusProvider + { + public: + virtual ~AutofocusProvider(); + + virtual QString defaultAutofocus() const = 0; + // Focuses with the autofocus device and returns once the focus has settled + virtual bool runAutofocus(const QString& deviceId, QString* errorMessage = nullptr) = 0; }; class SCOPEONE_SDK_EXPORT StateProvider diff --git a/ScopeOneCore/sdk/src/HardwareProvider.cpp b/ScopeOneCore/sdk/src/HardwareProvider.cpp index 9f2ef57..17191f6 100644 --- a/ScopeOneCore/sdk/src/HardwareProvider.cpp +++ b/ScopeOneCore/sdk/src/HardwareProvider.cpp @@ -10,6 +10,7 @@ namespace scopeone::core DevicePropertyProvider::~DevicePropertyProvider() = default; StageProvider::~StageProvider() = default; ShutterProvider::~ShutterProvider() = default; + AutofocusProvider::~AutofocusProvider() = default; StateProvider::~StateProvider() = default; ConfigurationProvider::~ConfigurationProvider() = default; CameraProvider::~CameraProvider() = default; diff --git a/ScopeOneCore/src/CameraBackend.cpp b/ScopeOneCore/src/CameraBackend.cpp deleted file mode 100644 index b7b3877..0000000 --- a/ScopeOneCore/src/CameraBackend.cpp +++ /dev/null @@ -1,480 +0,0 @@ -#include "internal/CameraBackend.h" - -#include -#include -#include -#include -#include - -namespace scopeone::core::internal -{ - namespace - { - constexpr qint64 kMaximumPendingRecordingBytes = 256ll * 1024 * 1024; - constexpr qint64 kMaximumRecordingFlushBytes = 16ll * 1024 * 1024; - constexpr qsizetype kMaximumRecordingFlushFrames = 16; - - // Releases discarded frame buffers away from the backend thread - void releaseFramesAsync(std::deque&& frames) - { - if (frames.empty()) - { - return; - } - QThreadPool::globalInstance()->start( - [frames = std::move(frames)]() mutable { frames.clear(); }); - } - } - - // Enables processing admission and invalidates outstanding permits - void ProcessingFrameGate::setEnabled(bool enabled) - { - QMutexLocker lock(&m_mutex); - if (m_enabled == enabled) - { - return; - } - m_enabled = enabled; - m_inFlightTokens.clear(); - } - - // Grants one processing permit per camera - quint64 ProcessingFrameGate::tryAcquire(const QString& cameraId) - { - QMutexLocker lock(&m_mutex); - if (!m_enabled || cameraId.isEmpty() || m_inFlightTokens.contains(cameraId)) - { - return 0; - } - ++m_nextToken; - m_inFlightTokens.insert(cameraId, m_nextToken); - return m_nextToken; - } - - // Checks whether a permit still belongs to the active request - bool ProcessingFrameGate::isCurrent(const QString& cameraId, quint64 token) - { - QMutexLocker lock(&m_mutex); - return m_enabled && m_inFlightTokens.value(cameraId) == token; - } - - // Releases a permit only when its generation still matches - void ProcessingFrameGate::release(const QString& cameraId, quint64 token) - { - QMutexLocker lock(&m_mutex); - const auto it = m_inFlightTokens.constFind(cameraId); - if (it != m_inFlightTokens.constEnd() && it.value() == token) - { - m_inFlightTokens.remove(cameraId); - } - } - - CameraBackend::CameraBackend(ProcessingFrameGate& processingFrameGate, - QObject* parent) - : QObject(parent) - , m_processingFrameGate(processingFrameGate) - { - } - - CameraBackend::~CameraBackend() = default; - - bool CameraBackend::configureNativeCamera(const std::shared_ptr&, - const QString&, - double) - { - return false; - } - - bool CameraBackend::addDriverHostCamera(const QString&, - const QString&, - const QString&, - const QStringList&, - const QStringList&, - double) - { - return false; - } - - bool CameraBackend::captureEventFrame(const QString&, ImageFrame&, int) - { - return false; - } - - // Starts or invalidates one generation of lossless recording delivery - bool CameraBackend::setRecordingFrameDeliveryEnabled(bool enabled) - { - std::deque discardedFrames; - { - QMutexLocker lock(&m_frameDeliveryMutex); - if (enabled && m_recordingFrameDeliveryToken.load(std::memory_order_relaxed) != 0) - { - return true; - } - - const quint64 token = enabled ? ++m_nextRecordingFrameDeliveryToken : 0; - m_recordingFrameDeliveryToken.store(token, std::memory_order_relaxed); - discardedFrames.swap(m_pendingRecordingFrames); - m_pendingRecordingBytes = 0; - m_pendingDroppedRecordingFrames = 0; - m_pendingDeliveryError.clear(); - } - releaseFramesAsync(std::move(discardedFrames)); - return true; - } - - // Selects full speed latest frame delivery for live processing consumers - bool CameraBackend::setHighRateFrameDeliveryEnabled(bool enabled) - { - m_processingFrameGate.setEnabled(enabled); - m_highRateFrameDeliveryEnabled.store(enabled, std::memory_order_relaxed); - return true; - } - - bool CameraBackend::getExposure(const QString& cameraIdOrAll, double& exposureMs) const - { - QString cameraId; - if (!resolvePrimaryCameraId(cameraIdOrAll, cameraId)) - { - return false; - } - return readExposureFor(cameraId, exposureMs); - } - - bool CameraBackend::setExposure(const QString& cameraIdOrAll, double exposureMs) - { - const QStringList cameraIds = resolveTargetCameraIds(cameraIdOrAll); - if (cameraIds.isEmpty()) - { - return false; - } - - for (const QString& cameraId : cameraIds) - { - if (!applyWithPreviewRestart(cameraId, - [this, cameraId, exposureMs]() - { - return writeExposureFor(cameraId, exposureMs); - })) - { - return false; - } - } - return true; - } - - QStringList CameraBackend::listProperties(const QString& cameraId) - { - QString resolvedCameraId; - if (!resolvePrimaryCameraId(cameraId, resolvedCameraId)) - { - return {}; - } - return listPropertiesFor(resolvedCameraId); - } - - bool CameraBackend::readPropertyDetails(const QString& cameraId, - const QString& name, - bool fromCache, - CameraPropertyReadback& readback) - { - QString resolvedCameraId; - if (!resolvePrimaryCameraId(cameraId, resolvedCameraId)) - { - return false; - } - return readPropertyDetailsFor(resolvedCameraId, name, fromCache, readback); - } - - bool CameraBackend::setProperty(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage) - { - QString resolvedCameraId; - if (!resolvePrimaryCameraId(cameraId, resolvedCameraId)) - { - return false; - } - return applyWithPreviewRestart( - resolvedCameraId, - [this, resolvedCameraId, name, value, errorMessage]() - { - return setPropertyFor(resolvedCameraId, name, value, errorMessage); - }); - } - - bool CameraBackend::setROI(const QString& cameraId, int x, int y, int width, int height) - { - QString resolvedCameraId; - if (!resolvePrimaryCameraId(cameraId, resolvedCameraId)) - { - return false; - } - return applyWithPreviewRestart( - resolvedCameraId, - [this, resolvedCameraId, x, y, width, height]() - { - return setROIFor(resolvedCameraId, x, y, width, height); - }); - } - - bool CameraBackend::clearROI(const QString& cameraId) - { - QString resolvedCameraId; - if (!resolvePrimaryCameraId(cameraId, resolvedCameraId)) - { - return false; - } - return applyWithPreviewRestart( - resolvedCameraId, - [this, resolvedCameraId]() { return clearROIFor(resolvedCameraId); }); - } - - bool CameraBackend::getROI(const QString& cameraId, int& x, int& y, int& width, int& height) - { - QString resolvedCameraId; - if (!resolvePrimaryCameraId(cameraId, resolvedCameraId)) - { - return false; - } - return getROIFor(resolvedCameraId, x, y, width, height); - } - - bool CameraBackend::applyWithPreviewRestart(const QString& cameraId, - const std::function& operation) - { - const bool wasRunning = isPreviewRunning(cameraId); - if (wasRunning && !stopPreviewFor(cameraId)) - { - return false; - } - - const bool ok = operation(); - if (wasRunning && !startPreviewFor(cameraId)) - { - return false; - } - return ok; - } - - void CameraBackend::notifyPreviewStarted(const QString& cameraId) - { - emit previewStateChanged(true); - qInfo().noquote() << QString("Preview started for camera '%1'").arg(cameraId); - } - - void CameraBackend::notifyPreviewStopped() - { - if (!hasRunningCamera()) - { - emit previewStateChanged(false); - qInfo().noquote() << "All cameras stopped"; - } - } - - // Routes one producer batch to processing, preview, and recording consumers - void CameraBackend::submitFrames(const QList& frames, - quint64 acquiredFrameCount, - quint64 recordingToken) - { - bool queueFlush = false; - std::deque discardedFrames; - { - QMutexLocker lock(&m_frameDeliveryMutex); - bool recordingDeliveryEnabled = - recordingToken != 0 - && recordingToken == m_recordingFrameDeliveryToken.load(std::memory_order_relaxed); - const bool recordingWasEnabled = recordingDeliveryEnabled; - QString acquiredCameraId; - quint64 validFrameCount = 0; - quint64 queuedFrameCount = 0; - for (const ImageFrame& frame : frames) - { - const QString cameraId = frame.cameraId.trimmed(); - if (!frame.isValid() || cameraId.isEmpty()) - { - continue; - } - - ImageFrame normalizedFrame(frame); - normalizedFrame.cameraId = cameraId; - acquiredCameraId = cameraId; - ++validFrameCount; - m_pendingLatestFrames.insert(cameraId, normalizedFrame); - if (!recordingDeliveryEnabled) - { - continue; - } - - const qint64 frameBytes = normalizedFrame.payloadByteCount(); - if (frameBytes > kMaximumPendingRecordingBytes - m_pendingRecordingBytes) - { - if (m_pendingDeliveryError.isEmpty()) - { - m_pendingDeliveryError = QStringLiteral( - "Recording frame delivery exceeded the 256 MiB pending limit"); - } - recordingDeliveryEnabled = false; - m_recordingFrameDeliveryToken.store(0, std::memory_order_relaxed); - continue; - } - - m_pendingRecordingFrames.push_back(std::move(normalizedFrame)); - m_pendingRecordingBytes += frameBytes; - ++queuedFrameCount; - } - - if (!acquiredCameraId.isEmpty() && acquiredFrameCount > 0) - { - m_pendingAcquiredFrameCounts[acquiredCameraId] += acquiredFrameCount; - } - if (recordingDeliveryEnabled - && acquiredFrameCount > validFrameCount - && m_pendingDeliveryError.isEmpty()) - { - m_pendingDeliveryError = QStringLiteral( - "Recording frame delivery detected dropped camera frames"); - m_recordingFrameDeliveryToken.store(0, std::memory_order_relaxed); - } - if (recordingWasEnabled && !m_pendingDeliveryError.isEmpty()) - { - const quint64 submittedFrameCount = acquiredFrameCount > 0 - ? acquiredFrameCount - : validFrameCount; - if (submittedFrameCount > queuedFrameCount) - { - m_pendingDroppedRecordingFrames += submittedFrameCount - queuedFrameCount; - } - m_pendingDroppedRecordingFrames += - static_cast(m_pendingRecordingFrames.size()); - discardedFrames.swap(m_pendingRecordingFrames); - m_pendingRecordingBytes = 0; - } - - if ((!m_pendingLatestFrames.isEmpty() - || !m_pendingAcquiredFrameCounts.isEmpty() - || !m_pendingRecordingFrames.empty() - || !m_pendingDeliveryError.isEmpty()) - && !m_frameFlushQueued) - { - m_frameFlushQueued = true; - queueFlush = true; - } - } - - releaseFramesAsync(std::move(discardedFrames)); - - if (queueFlush) - { - QMetaObject::invokeMethod( - this, - [this]() { flushPendingFrames(); }, - Qt::QueuedConnection); - } - } - - // Publishes one admitted frame to the processing pipeline - void CameraBackend::submitProcessingFrame(const ImageFrame& frame, quint64 token) - { - emit processingFrameReady(frame, token); - } - - // Requests one processing permit for a camera - quint64 CameraBackend::tryAcquireProcessingFrame(const QString& cameraId) - { - return m_processingFrameGate.tryAcquire(cameraId); - } - - // Returns one processing permit to the shared gate - void CameraBackend::releaseProcessingFrame(const QString& cameraId, quint64 token) - { - m_processingFrameGate.release(cameraId, token); - } - - // Drops coalesced preview frames after preview state changes - void CameraBackend::discardPendingPreviewFrames() - { - QMutexLocker lock(&m_frameDeliveryMutex); - m_pendingLatestFrames.clear(); - m_pendingAcquiredFrameCounts.clear(); - } - - // Reports whether a recording delivery generation is active - bool CameraBackend::recordingFrameDeliveryEnabled() const - { - return recordingFrameDeliveryToken() != 0; - } - - // Returns the generation accepted by the recording consumer - quint64 CameraBackend::recordingFrameDeliveryToken() const - { - return m_recordingFrameDeliveryToken.load(std::memory_order_relaxed); - } - - // Reports whether processing consumes frames independently of display rate - bool CameraBackend::highRateFrameDeliveryEnabled() const - { - return m_highRateFrameDeliveryEnabled.load(std::memory_order_relaxed); - } - - // Delivers one bounded batch without monopolizing the backend thread - void CameraBackend::flushPendingFrames() - { - QHash latestFrames; - QHash acquiredFrameCounts; - QList recordingFrames; - QString deliveryError; - quint64 droppedRecordingFrames = 0; - bool queueNextFlush = false; - { - QMutexLocker lock(&m_frameDeliveryMutex); - latestFrames.swap(m_pendingLatestFrames); - acquiredFrameCounts.swap(m_pendingAcquiredFrameCounts); - deliveryError.swap(m_pendingDeliveryError); - droppedRecordingFrames = m_pendingDroppedRecordingFrames; - m_pendingDroppedRecordingFrames = 0; - qint64 flushBytes = 0; - while (!m_pendingRecordingFrames.empty() - && recordingFrames.size() < kMaximumRecordingFlushFrames) - { - const qint64 frameBytes = m_pendingRecordingFrames.front().payloadByteCount(); - if (!recordingFrames.isEmpty() - && flushBytes + frameBytes > kMaximumRecordingFlushBytes) - { - break; - } - flushBytes += frameBytes; - recordingFrames.append(std::move(m_pendingRecordingFrames.front())); - m_pendingRecordingFrames.pop_front(); - } - m_pendingRecordingBytes -= flushBytes; - queueNextFlush = !m_pendingRecordingFrames.empty(); - m_frameFlushQueued = queueNextFlush; - } - - for (auto it = acquiredFrameCounts.constBegin(); it != acquiredFrameCounts.constEnd(); ++it) - { - emit rawFramesAcquired(it.key(), it.value()); - } - for (auto it = latestFrames.constBegin(); it != latestFrames.constEnd(); ++it) - { - emit rawFrameReady(it.value()); - } - if (!deliveryError.isEmpty()) - { - recordingFrames.clear(); - emit frameDeliveryFailed(deliveryError, droppedRecordingFrames); - } - else if (!recordingFrames.isEmpty()) - { - emit recordingFramesReady(recordingFrames); - } - if (queueNextFlush) - { - QMetaObject::invokeMethod( - this, - [this]() { flushPendingFrames(); }, - Qt::QueuedConnection); - } - } -} diff --git a/ScopeOneCore/src/CameraFrameDelivery.cpp b/ScopeOneCore/src/CameraFrameDelivery.cpp new file mode 100644 index 0000000..ffaa3cf --- /dev/null +++ b/ScopeOneCore/src/CameraFrameDelivery.cpp @@ -0,0 +1,322 @@ +#include "internal/CameraFrameDelivery.h" + +#include +#include +#include + +#include +#include + +namespace scopeone::core::internal +{ + namespace + { + constexpr qint64 kMaximumPendingRecordingBytes = 256ll * 1024 * 1024; + constexpr qint64 kMaximumRecordingFlushBytes = 16ll * 1024 * 1024; + constexpr qsizetype kMaximumRecordingFlushFrames = 16; + + // Releases discarded frame buffers away from the thread that delivers frames + void releaseFramesAsync(std::deque&& frames) + { + if (frames.empty()) + { + return; + } + QThreadPool::globalInstance()->start([frames = std::move(frames)]() mutable { frames.clear(); }); + } + } + + CameraFrameDelivery::CameraFrameDelivery(QObject* parent) + : QObject(parent) + { + qRegisterMetaType("scopeone::core::ImageFrame"); + } + + // The queue starts empty with the first recorded camera and is dropped with the last + void CameraFrameDelivery::setRecordingEnabled(const QStringList& cameraIds, bool enabled) + { + std::deque discardedFrames; + { + QMutexLocker lock(&m_mutex); + const bool wasRecording = !m_recordingCameras.isEmpty(); + for (const QString& cameraId : cameraIds) + { + if (enabled) m_recordingCameras.insert(cameraId); + else m_recordingCameras.remove(cameraId); + } + if (wasRecording == m_recordingCameras.isEmpty()) + { + discardedFrames.swap(m_pendingRecordingFrames); + m_pendingRecordingBytes = 0; + m_pendingDroppedRecordingFrames = 0; + m_pendingDeliveryError.clear(); + } + } + releaseFramesAsync(std::move(discardedFrames)); + } + + bool CameraFrameDelivery::recordingEnabled(const QString& cameraId) const + { + QMutexLocker lock(&m_mutex); + return m_recordingCameras.contains(cameraId); + } + + // Changing the mode of a camera invalidates its processing frame still in flight + void CameraFrameDelivery::setHighRateEnabled(const QStringList& cameraIds, bool enabled) + { + QMutexLocker lock(&m_processingMutex); + for (const QString& cameraId : cameraIds) + { + if (enabled) m_highRateCameras.insert(cameraId); + else m_highRateCameras.remove(cameraId); + m_processingTokens.remove(cameraId); + } + } + + bool CameraFrameDelivery::highRateEnabled(const QString& cameraId) const + { + QMutexLocker lock(&m_processingMutex); + return m_highRateCameras.contains(cameraId); + } + + FrameDeliveryMode CameraFrameDelivery::mode(const QString& cameraId) const + { + return recordingEnabled(cameraId) ? FrameDeliveryMode::AllFrames + : highRateEnabled(cameraId) ? FrameDeliveryMode::LatestOnly + : FrameDeliveryMode::PreviewLatest; + } + + void CameraFrameDelivery::removeCameras(const QStringList& cameraIds) + { + setRecordingEnabled(cameraIds, false); + setHighRateEnabled(cameraIds, false); + } + + bool CameraFrameDelivery::previewDue(const QString& cameraId) const + { + const auto it = m_previewTimers.constFind(cameraId); + return it == m_previewTimers.cend() + || !it->isValid() + || it->elapsed() >= kPreviewFrameIntervalMs; + } + + bool CameraFrameDelivery::wantsLatestFrame(const QString& cameraId) + { + { + QMutexLocker lock(&m_processingMutex); + if (!m_highRateCameras.contains(cameraId) || !m_processingTokens.contains(cameraId)) + { + return true; + } + } + QMutexLocker lock(&m_mutex); + return m_recordingCameras.contains(cameraId) || previewDue(cameraId); + } + + quint64 CameraFrameDelivery::tryAcquireProcessingFrame(const QString& cameraId) + { + QMutexLocker lock(&m_processingMutex); + if (!m_highRateCameras.contains(cameraId) || m_processingTokens.contains(cameraId)) + { + return 0; + } + m_processingTokens.insert(cameraId, ++m_nextProcessingToken); + return m_nextProcessingToken; + } + + bool CameraFrameDelivery::isProcessingFrameCurrent(const QString& cameraId, quint64 token) + { + QMutexLocker lock(&m_processingMutex); + return m_highRateCameras.contains(cameraId) && m_processingTokens.value(cameraId) == token; + } + + void CameraFrameDelivery::finishProcessingFrame(const QString& cameraId, quint64 token) + { + QMutexLocker lock(&m_processingMutex); + const auto it = m_processingTokens.constFind(cameraId); + if (it != m_processingTokens.cend() && it.value() == token) + { + m_processingTokens.remove(cameraId); + } + } + + void CameraFrameDelivery::deliverFrames(const QList& frames, quint64 acquiredFrameCount) + { + if (frames.isEmpty() || !frames.constLast().isValid()) + { + return; + } + const ImageFrame& latestFrame = frames.constLast(); + const QString cameraId = latestFrame.cameraId; + const bool highRate = highRateEnabled(cameraId); + if (const quint64 token = tryAcquireProcessingFrame(cameraId)) + { + emit processingFrameReady(latestFrame, token); + } + + bool queueFlush = false; + std::deque discardedFrames; + { + QMutexLocker lock(&m_mutex); + // A camera that is not recorded skips frames for its live view, which are not lost + bool recording = m_recordingCameras.contains(cameraId); + const bool recordingWasEnabled = recording; + quint64 validFrameCount = 0; + quint64 queuedFrameCount = 0; + for (const ImageFrame& frame : frames) + { + if (!frame.isValid()) + { + continue; + } + ++validFrameCount; + if (!recording) + { + continue; + } + const qint64 frameBytes = frame.payloadByteCount(); + if (frameBytes > kMaximumPendingRecordingBytes - m_pendingRecordingBytes) + { + if (m_pendingDeliveryError.isEmpty()) + { + m_pendingDeliveryError = + QStringLiteral("Recording frame delivery exceeded the 256 MiB pending limit"); + } + recording = false; + m_recordingCameras.clear(); + continue; + } + m_pendingRecordingFrames.push_back(frame); + m_pendingRecordingBytes += frameBytes; + ++queuedFrameCount; + } + + // Live processing reads frames faster than the view shows them + if (recordingWasEnabled || !highRate || previewDue(cameraId)) + { + m_pendingLatestFrames.insert(cameraId, latestFrame); + m_previewTimers[cameraId].start(); + } + if (acquiredFrameCount > 0) + { + m_pendingAcquiredFrameCounts[cameraId] += acquiredFrameCount; + } + if (recording && acquiredFrameCount > validFrameCount && m_pendingDeliveryError.isEmpty()) + { + m_pendingDeliveryError = QStringLiteral("Recording frame delivery detected dropped camera frames"); + m_recordingCameras.clear(); + } + if (recordingWasEnabled && !m_pendingDeliveryError.isEmpty()) + { + const quint64 submittedFrameCount = (std::max)(acquiredFrameCount, validFrameCount); + m_pendingDroppedRecordingFrames += submittedFrameCount - queuedFrameCount; + m_pendingDroppedRecordingFrames += static_cast(m_pendingRecordingFrames.size()); + discardedFrames.swap(m_pendingRecordingFrames); + m_pendingRecordingBytes = 0; + } + if (!m_frameFlushQueued + && (!m_pendingLatestFrames.isEmpty() + || !m_pendingAcquiredFrameCounts.isEmpty() + || !m_pendingRecordingFrames.empty() + || !m_pendingDeliveryError.isEmpty())) + { + m_frameFlushQueued = true; + queueFlush = true; + } + } + + releaseFramesAsync(std::move(discardedFrames)); + if (queueFlush) + { + QMetaObject::invokeMethod(this, [this]() { flushPendingFrames(); }, Qt::QueuedConnection); + } + } + + void CameraFrameDelivery::fail(const QString& message) + { + std::deque discardedFrames; + bool queueFlush = false; + { + QMutexLocker lock(&m_mutex); + if (m_recordingCameras.isEmpty()) + { + return; + } + m_recordingCameras.clear(); + if (m_pendingDeliveryError.isEmpty()) + { + m_pendingDeliveryError = message; + } + m_pendingDroppedRecordingFrames += static_cast(m_pendingRecordingFrames.size()); + discardedFrames.swap(m_pendingRecordingFrames); + m_pendingRecordingBytes = 0; + queueFlush = !std::exchange(m_frameFlushQueued, true); + } + releaseFramesAsync(std::move(discardedFrames)); + if (queueFlush) + { + QMetaObject::invokeMethod(this, [this]() { flushPendingFrames(); }, Qt::QueuedConnection); + } + } + + void CameraFrameDelivery::discardPendingFrames() + { + QMutexLocker lock(&m_mutex); + m_pendingLatestFrames.clear(); + m_pendingAcquiredFrameCounts.clear(); + } + + // Delivers one bounded batch without holding the thread for long + void CameraFrameDelivery::flushPendingFrames() + { + QHash latestFrames; + QHash acquiredFrameCounts; + QList recordingFrames; + QString deliveryError; + quint64 droppedRecordingFrames = 0; + bool queueNextFlush = false; + { + QMutexLocker lock(&m_mutex); + latestFrames.swap(m_pendingLatestFrames); + acquiredFrameCounts.swap(m_pendingAcquiredFrameCounts); + deliveryError.swap(m_pendingDeliveryError); + droppedRecordingFrames = std::exchange(m_pendingDroppedRecordingFrames, 0); + qint64 flushBytes = 0; + while (!m_pendingRecordingFrames.empty() && recordingFrames.size() < kMaximumRecordingFlushFrames) + { + const qint64 frameBytes = m_pendingRecordingFrames.front().payloadByteCount(); + if (!recordingFrames.isEmpty() && flushBytes + frameBytes > kMaximumRecordingFlushBytes) + { + break; + } + flushBytes += frameBytes; + recordingFrames.append(std::move(m_pendingRecordingFrames.front())); + m_pendingRecordingFrames.pop_front(); + } + m_pendingRecordingBytes -= flushBytes; + queueNextFlush = !m_pendingRecordingFrames.empty(); + m_frameFlushQueued = queueNextFlush; + } + + for (auto it = acquiredFrameCounts.cbegin(); it != acquiredFrameCounts.cend(); ++it) + { + emit framesAcquired(it.key(), it.value()); + } + for (auto it = latestFrames.cbegin(); it != latestFrames.cend(); ++it) + { + emit frameReady(it.value()); + } + if (!deliveryError.isEmpty()) + { + emit deliveryFailed(deliveryError, droppedRecordingFrames); + } + else if (!recordingFrames.isEmpty()) + { + emit recordingFramesReady(recordingFrames); + } + if (queueNextFlush) + { + QMetaObject::invokeMethod(this, [this]() { flushPendingFrames(); }, Qt::QueuedConnection); + } + } +} diff --git a/ScopeOneCore/src/CameraManager.cpp b/ScopeOneCore/src/CameraManager.cpp deleted file mode 100644 index f28a61e..0000000 --- a/ScopeOneCore/src/CameraManager.cpp +++ /dev/null @@ -1,453 +0,0 @@ -#include "internal/CameraManager.h" - -#include -#include - -namespace scopeone::core::internal -{ - namespace - { - QString normalizedCameraId(const QString& cameraId) - { - return cameraId.trimmed(); - } - - QString propertyCacheKey(const QString& cameraId, const QString& name) - { - return QStringLiteral("%1:%2").arg(cameraId, name); - } - } - - CameraManager::CameraManager(QObject* parent) - : QObject(parent) - { - qRegisterMetaType("scopeone::core::ImageFrame"); - } - - CameraManager::~CameraManager() - { - shutdownNow(); - } - - void CameraManager::setFrameSink(FrameSink sink) - { - m_frameSink = std::move(sink); - } - - void CameraManager::setPreviewStateSink(PreviewStateSink sink) - { - m_previewStateSink = std::move(sink); - } - - // Selects one camera backend and forwards its runtime signals - bool CameraManager::activateBackend(CameraBackend::Kind kind) - { - if (m_backend && m_backend->kind() == kind) - { - return true; - } - - shutdownNow(); - m_backend = kind == CameraBackend::Kind::Native - ? createNativeCameraBackend(m_processingFrameGate) - : createDriverHostCameraBackend(m_processingFrameGate); - if (!m_backend) - { - return false; - } - - connect(m_backend.get(), &CameraBackend::rawFrameReady, - this, [this](const scopeone::core::ImageFrame& frame) - { - if (m_frameSink) - { - m_frameSink(frame); - } - }); - // Keeps processing input on the producer thread - connect(m_backend.get(), &CameraBackend::processingFrameReady, - this, &CameraManager::processingFrameReady, - Qt::DirectConnection); - connect(m_backend.get(), &CameraBackend::rawFramesAcquired, - this, &CameraManager::rawFramesAcquired); - connect(m_backend.get(), &CameraBackend::recordingFramesReady, - this, &CameraManager::recordingFramesReady); - connect(m_backend.get(), &CameraBackend::frameDeliveryFailed, - this, &CameraManager::frameDeliveryFailed); - connect(m_backend.get(), &CameraBackend::previewStateChanged, - this, [this](bool running) - { - if (m_previewStateSink) m_previewStateSink(running); - emit previewStateChanged(running); - }); - connect(m_backend.get(), &CameraBackend::driverHostControlServerListening, - this, &CameraManager::driverHostControlServerListening); - if (!m_backend->setHighRateFrameDeliveryEnabled(m_highRateFrameDeliveryEnabled) - || !m_backend->setRecordingFrameDeliveryEnabled(m_recordingFrameDeliveryEnabled)) - { - m_backend.reset(); - return false; - } - return true; - } - - // Configures the in process backend for one MMCore camera - bool CameraManager::configureNativeCamera(const std::shared_ptr& core, - const QString& cameraId, - double exposureMs) - { - const QString normalizedId = normalizedCameraId(cameraId); - const bool configured = core - && !normalizedId.isEmpty() - && activateBackend(CameraBackend::Kind::Native) - && m_backend->configureNativeCamera(core, normalizedId, exposureMs); - if (configured) - { - m_propertyNamesCache.remove(normalizedId); - m_propertyDetailsCache.clear(); - } - return configured; - } - - // Add one process isolated camera to the DriverHost backend - bool CameraManager::addDriverHostCamera(const QString& cameraId, - const QString& adapter, - const QString& device, - const QStringList& preInitProperties, - const QStringList& properties, - double exposureMs) - { - const QString normalizedId = normalizedCameraId(cameraId); - const bool configured = !normalizedId.isEmpty() - && activateBackend(CameraBackend::Kind::DriverHost) - && m_backend->addDriverHostCamera(normalizedId, - adapter, - device, - preInitProperties, - properties, - exposureMs); - if (configured) - { - m_propertyNamesCache.remove(normalizedId); - m_propertyDetailsCache.clear(); - } - return configured; - } - - // Tears down the active backend synchronously - void CameraManager::shutdownNow() - { - m_processingFrameGate.setEnabled(false); - m_backend.reset(); - m_recordingFrameDeliveryEnabled = false; - m_highRateFrameDeliveryEnabled = false; - m_propertyNamesCache.clear(); - m_propertyDetailsCache.clear(); - } - - // Releases the active backend after its asynchronous teardown completes - void CameraManager::shutdown(std::function completion) - { - m_processingFrameGate.setEnabled(false); - if (!m_backend) - { - m_recordingFrameDeliveryEnabled = false; - m_highRateFrameDeliveryEnabled = false; - m_propertyNamesCache.clear(); - m_propertyDetailsCache.clear(); - if (completion) - { - completion({}); - } - return; - } - - CameraBackend* const backend = m_backend.get(); - backend->shutdown([this, backend, completion = std::move(completion)]( - const QString& errorMessage) mutable - { - QTimer::singleShot(0, this, [this, - backend, - completion = std::move(completion), - errorMessage]() mutable - { - if (errorMessage.isEmpty() && m_backend.get() == backend) - { - m_backend.reset(); - } - if (errorMessage.isEmpty()) - { - m_recordingFrameDeliveryEnabled = false; - m_highRateFrameDeliveryEnabled = false; - m_propertyNamesCache.clear(); - m_propertyDetailsCache.clear(); - } - if (completion) - { - completion(errorMessage); - } - }); - }); - } - - bool CameraManager::startPreview() - { - return m_backend && m_backend->startPreview(); - } - - bool CameraManager::stopPreview() - { - return m_backend && m_backend->stopPreview(); - } - - // Report whether cameras are isolated in DriverHost processes - bool CameraManager::usesDriverHostBackend() const - { - return m_backend && m_backend->kind() == CameraBackend::Kind::DriverHost; - } - - bool CameraManager::startPreviewFor(const QString& cameraId) - { - const QString normalizedId = normalizedCameraId(cameraId); - return !normalizedId.isEmpty() && m_backend && m_backend->startPreviewFor(normalizedId); - } - - bool CameraManager::stopPreviewFor(const QString& cameraId) - { - const QString normalizedId = normalizedCameraId(cameraId); - return !normalizedId.isEmpty() && m_backend && m_backend->stopPreviewFor(normalizedId); - } - - bool CameraManager::isPreviewRunning(const QString& cameraId) const - { - const QString normalizedId = normalizedCameraId(cameraId); - return !normalizedId.isEmpty() && m_backend && m_backend->isPreviewRunning(normalizedId); - } - - void CameraManager::setFrameDeliveryPaused(const QStringList&, bool paused) - { - if (m_backend) - { - m_backend->setFrameDeliveryPaused(paused); - } - } - - // Switches the active backend between preview and lossless recording delivery - bool CameraManager::setRecordingFrameDeliveryEnabled(const QStringList&, bool enabled) - { - const bool ok = !m_backend || m_backend->setRecordingFrameDeliveryEnabled(enabled); - if (ok || !enabled) - { - m_recordingFrameDeliveryEnabled = enabled; - } - return ok; - } - - // Switches processing delivery independently of preview display rate - bool CameraManager::setHighRateFrameDeliveryEnabled(const QStringList&, bool enabled) - { - const bool ok = !m_backend || m_backend->setHighRateFrameDeliveryEnabled(enabled); - if (ok) - { - m_highRateFrameDeliveryEnabled = enabled; - } - return ok; - } - - // Checks whether a completed processing task still owns its permit - bool CameraManager::isProcessingFrameTokenCurrent(const QString& cameraId, quint64 token) - { - return m_processingFrameGate.isCurrent(cameraId, token); - } - - // Releases the permit held by one completed processing task - void CameraManager::finishProcessingFrame(const QString& cameraId, quint64 token) - { - m_processingFrameGate.release(cameraId, token); - } - - bool CameraManager::getExposure(const QString& cameraIdOrAll, double& exposureMs) const - { - exposureMs = 0.0; - const QString target = cameraIdOrAll.trimmed(); - return !target.isEmpty() && m_backend && m_backend->getExposure(target, exposureMs); - } - - bool CameraManager::setExposure(const QString& cameraIdOrAll, double exposureMs) - { - const QString target = cameraIdOrAll.trimmed(); - const bool ok = !target.isEmpty() && m_backend && m_backend->setExposure(target, exposureMs); - if (ok) - { - m_propertyDetailsCache.clear(); - } - return ok; - } - - QStringList CameraManager::listProperties(const QString& cameraId) - { - const QString normalizedId = normalizedCameraId(cameraId); - if (normalizedId.isEmpty() || !m_backend) - { - return {}; - } - const auto cached = m_propertyNamesCache.constFind(normalizedId); - if (cached != m_propertyNamesCache.constEnd()) - { - return cached.value(); - } - const QStringList names = m_backend->listProperties(normalizedId); - if (!names.isEmpty()) - { - m_propertyNamesCache.insert(normalizedId, names); - } - return names; - } - - QString CameraManager::getProperty(const QString& cameraId, - const QString& name, - bool fromCache) - { - const QString normalizedId = normalizedCameraId(cameraId); - const QString cacheKey = propertyCacheKey(normalizedId, name); - if (fromCache) - { - const auto cached = m_propertyDetailsCache.constFind(cacheKey); - if (cached != m_propertyDetailsCache.constEnd()) - { - return cached.value().value; - } - } - CameraPropertyReadback readback; - if (normalizedId.isEmpty() - || !m_backend - || !m_backend->readPropertyDetails(normalizedId, name, fromCache, readback)) - { - m_propertyDetailsCache.remove(cacheKey); - return {}; - } - - m_propertyDetailsCache.insert(cacheKey, readback); - return readback.value; - } - - bool CameraManager::setProperty(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage) - { - const QString normalizedId = normalizedCameraId(cameraId); - if (normalizedId.isEmpty() || !m_backend) - { - return false; - } - const bool ok = m_backend->setProperty(normalizedId, name, value, errorMessage); - if (ok) - { - m_propertyDetailsCache.clear(); - } - return ok; - } - - CameraPropertyReadback CameraManager::propertyDetails(const QString& cameraId, const QString& name) - { - const QString normalizedId = normalizedCameraId(cameraId); - const QString cacheKey = propertyCacheKey(normalizedId, name); - if (!m_propertyDetailsCache.contains(cacheKey)) - { - getProperty(normalizedId, name); - } - return m_propertyDetailsCache.value(cacheKey); - } - - QString CameraManager::getPropertyType(const QString& cameraId, const QString& name) - { - return propertyDetails(cameraId, name).type; - } - - bool CameraManager::isPropertyReadOnly(const QString& cameraId, const QString& name) - { - return propertyDetails(cameraId, name).readOnly; - } - - bool CameraManager::isPropertyPreInit(const QString& cameraId, const QString& name) - { - return propertyDetails(cameraId, name).preInit; - } - - QStringList CameraManager::getAllowedPropertyValues(const QString& cameraId, const QString& name) - { - return propertyDetails(cameraId, name).allowedValues; - } - - bool CameraManager::hasPropertyLimits(const QString& cameraId, const QString& name) - { - return propertyDetails(cameraId, name).hasLimits; - } - - double CameraManager::getPropertyLowerLimit(const QString& cameraId, const QString& name) - { - return propertyDetails(cameraId, name).lowerLimit; - } - - double CameraManager::getPropertyUpperLimit(const QString& cameraId, const QString& name) - { - return propertyDetails(cameraId, name).upperLimit; - } - - bool CameraManager::setROI(const QString& cameraId, int x, int y, int width, int height) - { - const QString normalizedId = normalizedCameraId(cameraId); - const bool ok = !normalizedId.isEmpty() - && m_backend - && m_backend->setROI(normalizedId, x, y, width, height); - if (ok) - { - m_propertyDetailsCache.clear(); - } - return ok; - } - - bool CameraManager::clearROI(const QString& cameraId) - { - const QString normalizedId = normalizedCameraId(cameraId); - const bool ok = !normalizedId.isEmpty() && m_backend && m_backend->clearROI(normalizedId); - if (ok) - { - m_propertyDetailsCache.clear(); - } - return ok; - } - - bool CameraManager::getROI(const QString& cameraId, int& x, int& y, int& width, int& height) - { - const QString normalizedId = normalizedCameraId(cameraId); - return !normalizedId.isEmpty() && m_backend && m_backend->getROI(normalizedId, x, y, width, height); - } - - bool CameraManager::captureEventFrame(const QString& cameraId, ImageFrame& frame, int timeoutMs) - { - const QString normalizedId = normalizedCameraId(cameraId); - return !normalizedId.isEmpty() - && m_backend - && m_backend->captureEventFrame(normalizedId, frame, timeoutMs); - } - - bool CameraManager::supportsEventSequence(const QString& cameraId) const - { - const QString normalizedId = normalizedCameraId(cameraId); - return !normalizedId.isEmpty() && m_backend && m_backend->supportsEventSequence(normalizedId); - } - - bool CameraManager::captureEventSequence(const QString& cameraId, - int count, - std::vector& frames, - int timeoutMs) - { - const QString normalizedId = normalizedCameraId(cameraId); - return !normalizedId.isEmpty() - && m_backend - && m_backend->captureEventSequence(normalizedId, count, frames, timeoutMs); - } - -} diff --git a/ScopeOneCore/src/DriverHostCameraBackend.cpp b/ScopeOneCore/src/DriverHostCameraBackend.cpp deleted file mode 100644 index 416708b..0000000 --- a/ScopeOneCore/src/DriverHostCameraBackend.cpp +++ /dev/null @@ -1,1862 +0,0 @@ -#include "internal/CameraBackend.h" -#include "internal/DriverHostProtocol.h" -#include "internal/SharedFrameRing.h" -#include "scopeone/SharedFrame.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace scopeone::core::internal -{ - using scopeone::core::ImageFrame; - using scopeone::core::SharedFrameHeader; - using scopeone::core::SharedMemoryControl; - using scopeone::core::SharedPixelFormat; - using scopeone::core::kSharedFrameHeaderSize; - using scopeone::core::kSharedFrameMaxBytes; - using scopeone::core::kSharedFrameNumSlots; - using scopeone::core::kSharedFrameSlotStride; - using scopeone::core::kSharedMemoryControlSize; - - namespace - { - constexpr int kDriverHostControlReadyTimeoutMs = 15000; - // Starting a sequence after the image size changed reallocates the 2 GB circular buffer - // of the DriverHost MMCore, which takes over a second - constexpr int kStartPreviewTimeoutMs = 10000; - constexpr int kPreviewFrameDeliveryIntervalMs = 16; - - enum class DriverHostFrameDeliveryMode - { - PreviewLatest, - LatestOnly, - AllFrames - }; - - // Normalizes camera ids before they become backend keys - QString normalizedCameraId(const QString& cameraId) - { - return cameraId.trimmed(); - } - - const QString& frameDeliveryModeName(DriverHostFrameDeliveryMode mode) - { - switch (mode) - { - case DriverHostFrameDeliveryMode::PreviewLatest: - return driverhost::kFrameDeliveryModePreviewLatest; - case DriverHostFrameDeliveryMode::LatestOnly: - return driverhost::kFrameDeliveryModeLatestOnly; - case DriverHostFrameDeliveryMode::AllFrames: - return driverhost::kFrameDeliveryModeAllFrames; - } - return driverhost::kFrameDeliveryModePreviewLatest; - } - - DriverHostFrameDeliveryMode nonRecordingDeliveryMode(bool highRate) - { - return highRate - ? DriverHostFrameDeliveryMode::LatestOnly - : DriverHostFrameDeliveryMode::PreviewLatest; - } - - } // namespace - - struct DriverHostControlSession final : QObject - { - struct PendingRequest - { - QByteArray encoded; - QTimer* timer{nullptr}; - std::function completion; - }; - - explicit DriverHostControlSession(const QString& providerId, - const QString& cameraId, - const QString& serverName, - QObject* parent = nullptr) - : QObject(parent) - , m_providerId(providerId) - , m_cameraId(cameraId) - , m_serverName(serverName) - { - m_reconnectTimer.setSingleShot(true); - connect(&m_reconnectTimer, &QTimer::timeout, this, [this]() { ensureConnected(); }); - - connect(&m_socket, &QLocalSocket::connected, this, [this]() { flushPendingWrites(); }); - connect(&m_socket, &QLocalSocket::disconnected, this, [this]() - { - m_readBuffer.clear(); - setReady(false); - failAll(QStringLiteral("Control session disconnected")); - if (!m_closing) - { - m_reconnectTimer.start(100); - } - }); - connect(&m_socket, &QLocalSocket::readyRead, this, [this]() { handleReadyRead(); }); - connect(&m_socket, &QLocalSocket::errorOccurred, this, - [this](QLocalSocket::LocalSocketError) - { - if (m_closing) - { - return; - } - setReady(false); - failAll(QStringLiteral("Control socket error for '%1'").arg(m_cameraId)); - // A failed connect reports its error while the socket is still connecting - if (m_socket.state() != QLocalSocket::ConnectedState) - { - m_reconnectTimer.start(100); - } - }); - } - - void start() - { - ensureConnected(); - } - - void stop() - { - m_closing = true; - m_reconnectTimer.stop(); - m_readBuffer.clear(); - setReady(false); - failAll(QStringLiteral("Control session closed")); - if (m_socket.state() != QLocalSocket::UnconnectedState) - { - m_socket.abort(); - } - } - - bool isReady() const - { - return m_ready && m_socket.state() == QLocalSocket::ConnectedState; - } - - bool waitForReady(int timeoutMs) - { - if (isReady()) - { - return true; - } - - ensureConnected(); - - QEventLoop loop; - QTimer watchdog; - watchdog.setSingleShot(true); - watchdog.setInterval((std::max)(1, timeoutMs)); - - const auto quitIfReady = [this, &loop]() - { - if (isReady()) - { - loop.quit(); - } - }; - - connect(&m_socket, &QLocalSocket::connected, &loop, quitIfReady); - connect(&m_socket, &QLocalSocket::readyRead, &loop, quitIfReady); - connect(&m_socket, &QLocalSocket::disconnected, &loop, quitIfReady); - connect(&m_socket, &QLocalSocket::errorOccurred, &loop, - [quitIfReady](QLocalSocket::LocalSocketError) - { - quitIfReady(); - }); - connect(&watchdog, &QTimer::timeout, &loop, &QEventLoop::quit); - - watchdog.start(); - if (!isReady()) - { - loop.exec(); - } - return isReady(); - } - - void addReadyHandler(std::function handler) - { - m_readyHandlers.push_back(std::move(handler)); - } - - void addEventHandler(std::function handler) - { - m_eventHandlers.push_back(std::move(handler)); - } - - void sendRequest(const QJsonObject& request, - int timeoutMs, - std::function completion) - { - const quint64 requestId = m_nextRequestId++; - QJsonObject envelope = request; - envelope.insert(driverhost::kEnvelopeKindField, driverhost::kMessageKindRequest); - envelope.insert(driverhost::kEnvelopeVersionField, static_cast(driverhost::kProtocolVersion)); - envelope.insert(driverhost::kEnvelopeRequestIdField, driverhost::encodeUInt64(requestId)); - - PendingRequest pending; - pending.encoded = driverhost::encodeMessage(envelope); - pending.completion = std::move(completion); - pending.timer = new QTimer(this); - pending.timer->setSingleShot(true); - connect(pending.timer, &QTimer::timeout, this, [this, requestId]() - { - completeRequest(requestId, false, QJsonObject{}, QStringLiteral("Control request timed out")); - }); - pending.timer->start((std::max)(1, timeoutMs)); - - m_pending.insert(requestId, pending); - m_sendQueue.push_back(requestId); - ensureConnected(); - flushPendingWrites(); - } - - private: - void ensureConnected() - { - if (m_closing) - { - return; - } - if (m_socket.state() == QLocalSocket::ConnectedState - || m_socket.state() == QLocalSocket::ConnectingState) - { - return; - } - m_socket.connectToServer(m_serverName); - } - - void setReady(bool ready) - { - if (m_ready == ready) - { - return; - } - m_ready = ready; - for (const auto& handler : m_readyHandlers) - { - handler(m_ready); - } - } - - void flushPendingWrites() - { - if (m_socket.state() != QLocalSocket::ConnectedState) - { - return; - } - - while (!m_sendQueue.isEmpty()) - { - const quint64 requestId = m_sendQueue.front(); - m_sendQueue.pop_front(); - - auto it = m_pending.find(requestId); - if (it == m_pending.end()) - { - continue; - } - m_socket.write(it->encoded); - } - m_socket.flush(); - } - - void handleReadyRead() - { - m_readBuffer += m_socket.readAll(); - while (true) - { - QJsonObject message; - QString error; - const driverhost::DecodeResult result = - driverhost::tryDecodeMessage(m_readBuffer, message, &error); - if (result == driverhost::DecodeResult::Incomplete) - { - return; - } - if (result == driverhost::DecodeResult::Error) - { - resetWithError(error); - return; - } - - if (message.value(driverhost::kEnvelopeVersionField).toInt(0) - != static_cast(driverhost::kProtocolVersion)) - { - resetWithError(QStringLiteral("Control protocol version mismatch")); - return; - } - - const QString kind = message.value(driverhost::kEnvelopeKindField).toString(); - if (kind == driverhost::kMessageKindResponse) - { - const quint64 requestId = - driverhost::decodeUInt64(message.value(driverhost::kEnvelopeRequestIdField)); - if (requestId == 0) - { - continue; - } - completeRequest(requestId, true, message, QString{}); - continue; - } - - if (kind == driverhost::kMessageKindEvent) - { - const QString type = message.value(driverhost::kMessageTypeField).toString(); - if (type == driverhost::kEventHello) - { - const QJsonArray capabilities = - message.value(driverhost::kCapabilitiesField).toArray(); - if (message.value(driverhost::kProviderIdField).toString() - != m_providerId - || message.value(driverhost::kDeviceIdField).toString() != m_cameraId - || !capabilities.contains(driverhost::kCapabilityCamera)) - { - resetWithError(QStringLiteral("DriverHost identity mismatch")); - return; - } - setReady(true); - } - for (const auto& handler : m_eventHandlers) - { - handler(message); - } - } - } - } - - void completeRequest(quint64 requestId, - bool ok, - const QJsonObject& response, - const QString& error) - { - auto it = m_pending.find(requestId); - if (it == m_pending.end()) - { - return; - } - - PendingRequest pending = it.value(); - m_pending.erase(it); - - if (pending.timer) - { - pending.timer->stop(); - pending.timer->deleteLater(); - } - if (pending.completion) - { - pending.completion(ok, response, error); - } - } - - void failAll(const QString& error) - { - const QList requestIds = m_pending.keys(); - for (quint64 requestId : requestIds) - { - completeRequest(requestId, false, QJsonObject{}, error); - } - m_sendQueue.clear(); - } - - void resetWithError(const QString& error) - { - m_readBuffer.clear(); - setReady(false); - failAll(error); - if (m_socket.state() != QLocalSocket::UnconnectedState) - { - m_socket.abort(); - } - if (!m_closing) - { - m_reconnectTimer.start(100); - } - } - - QString m_providerId; - QString m_cameraId; - QString m_serverName; - QLocalSocket m_socket; - QTimer m_reconnectTimer; - QByteArray m_readBuffer; - QMap m_pending; - QList m_sendQueue; - QList> m_readyHandlers; - QList> m_eventHandlers; - quint64 m_nextRequestId{1}; - bool m_ready{false}; - bool m_closing{false}; - }; - - class DriverHostCameraBackend; - - class DriverHostFrameWorker final : public QObject - { - public: - explicit DriverHostFrameWorker(DriverHostCameraBackend* owner) - : m_owner(owner) - { - } - - void addCamera(const QString& cameraId, const QString& shmKey); - void removeCamera(const QString& cameraId); - void clear(); - void resetPreviewDeliveryState(const QString& cameraId); - bool ensureSharedMemory(const QString& cameraId); - ImageFrame consumeFrames(const QString& cameraId, bool publishFrames); - void consumeFramesAsync(const QString& cameraId); - - private: - struct ReaderSlot - { - QString cameraId; - QString shmKey; - std::unique_ptr shm; - quint64 lastFrameIndex{0}; - quint64 pendingAcquiredFrameCount{0}; - ImageFrame latestFrame; - QElapsedTimer previewDeliveryTimer; - }; - - bool ensureSharedMemory(ReaderSlot& slot); - bool readLatestFrame(ReaderSlot& slot, - QList& frames, - quint64& acquiredFrameCount); - bool readAllFrames(ReaderSlot& slot, - QList& frames, - quint64& acquiredFrameCount); - - DriverHostCameraBackend* const m_owner; - QMap> m_readers; - }; - - struct DriverHostCameraSlot - { - QString cameraId; - QString shmKey; - std::shared_ptr process; - std::shared_ptr control; - bool isRunning{false}; - double exposureMs{10.0}; - bool frameReadQueued{false}; - bool frameReadRequested{false}; - }; - - class DriverHostCameraBackend final : public CameraBackend - { - public: - explicit DriverHostCameraBackend(ProcessingFrameGate& processingFrameGate); - ~DriverHostCameraBackend() override; - - Kind kind() const override { return Kind::DriverHost; } - bool addDriverHostCamera(const QString& cameraId, - const QString& adapter, - const QString& device, - const QStringList& preInitProperties, - const QStringList& properties, - double exposureMs) override; - void removeDriverHostCamera(const QString& cameraId); - bool isPreviewRunning(const QString& cameraId) const override - { - const auto it = m_cameras.constFind(cameraId); - return it != m_cameras.constEnd() && it.value()->isRunning; - } - void setFrameDeliveryPaused(bool paused) override; - bool setRecordingFrameDeliveryEnabled(bool enabled) override; - bool setHighRateFrameDeliveryEnabled(bool enabled) override; - bool captureEventFrame(const QString& cameraId, - ImageFrame& frame, - int timeoutMs) override; - - void shutdownNow(); - void shutdown(std::function completion) override; - - bool startPreview() override - { - if (m_cameras.isEmpty()) - { - return false; - } - - QStringList startedCameraIds; - for (auto it = m_cameras.begin(); it != m_cameras.end(); ++it) - { - const QString cameraId = it.key(); - if (it.value()->isRunning) - { - continue; - } - if (!startPreviewFor(cameraId)) - { - for (const QString& startedCameraId : startedCameraIds) - { - stopPreviewFor(startedCameraId); - } - return false; - } - startedCameraIds.append(cameraId); - } - qInfo().noquote() << "DriverHost preview started"; - return true; - } - - bool stopPreview() override - { - bool ok = true; - for (auto it = m_cameras.begin(); it != m_cameras.end(); ++it) - { - ok = stopPreviewFor(it.key()) && ok; - } - if (ok) - { - qInfo().noquote() << "DriverHost preview stopped"; - } - return ok; - } - - bool startPreviewFor(const QString& cameraId) override - { - if (!m_cameras.contains(cameraId)) - { - qWarning().noquote() << QString("Camera '%1' not found").arg(cameraId); - return false; - } - - DriverHostCameraSlot& slot = *m_cameras[cameraId]; - if (slot.isRunning) - { - return true; - } - DriverHostFrameWorker* const worker = m_frameWorker; - QMetaObject::invokeMethod( - worker, - [worker, cameraId]() { worker->resetPreviewDeliveryState(cameraId); }, - Qt::BlockingQueuedConnection); - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandStartPreview); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, kStartPreviewTimeoutMs)) - { - qWarning().noquote() << QString("Failed to connect to camera '%1'").arg(cameraId); - return false; - } - if (!resp.value(QStringLiteral("ok")).toBool(false)) - { - qWarning().noquote() << QString("DriverHost refused to start preview for '%1'").arg(cameraId); - return false; - } - - bool sharedMemoryReady = false; - for (int attempt = 0; attempt < 20 && !sharedMemoryReady; ++attempt) - { - sharedMemoryReady = prepareFrameReader(cameraId); - if (!sharedMemoryReady) - { - QThread::msleep(50); - } - } - if (!sharedMemoryReady) - { - qWarning().noquote() << QString("Shared memory unavailable for '%1'").arg(cameraId); - QJsonObject stopRequest; - stopRequest.insert(driverhost::kMessageTypeField, driverhost::kCommandStopPreview); - sendControlCommand(cameraId, stopRequest, nullptr, 1200); - return false; - } - - slot.isRunning = true; - notifyPreviewStarted(cameraId); - return true; - } - - bool stopPreviewFor(const QString& cameraId) override - { - const auto it = m_cameras.find(cameraId); - if (it == m_cameras.end()) - { - return false; - } - if (!it.value()->isRunning) - { - return true; - } - - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandStopPreview); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, 1200)) - { - return false; - } - if (!resp.value(QStringLiteral("ok")).toBool(false)) - { - return false; - } - - it.value()->isRunning = false; - notifyPreviewStopped(); - return true; - } - - protected: - bool hasRunningCamera() const override - { - for (auto it = m_cameras.constBegin(); it != m_cameras.constEnd(); ++it) - { - if (it.value()->isRunning) - { - return true; - } - } - return false; - } - - bool resolvePrimaryCameraId(const QString& cameraIdOrAll, QString& cameraId) const override - { - if (m_cameras.isEmpty()) - { - return false; - } - - const QString target = - cameraIdOrAll.compare(QStringLiteral("All"), Qt::CaseInsensitive) == 0 - ? m_cameras.firstKey() - : cameraIdOrAll; - if (!m_cameras.contains(target)) - { - return false; - } - cameraId = target; - return true; - } - - QStringList resolveTargetCameraIds(const QString& cameraIdOrAll) const override - { - if (cameraIdOrAll.compare(QStringLiteral("All"), Qt::CaseInsensitive) == 0) - { - return m_cameras.keys(); - } - return m_cameras.contains(cameraIdOrAll) ? QStringList{cameraIdOrAll} : QStringList{}; - } - - bool readExposureFor(const QString& cameraId, double& exposureMs) const override - { - QJsonObject request; - request.insert(driverhost::kMessageTypeField, driverhost::kCommandGetExposure); - QJsonObject response; - if (!sendControlCommand(cameraId, request, &response, 1200) - || !response.value(QStringLiteral("ok")).toBool(false)) - { - return false; - } - exposureMs = response.value(QStringLiteral("exposureMs")).toDouble(0.0); - return exposureMs > 0.0; - } - - bool writeExposureFor(const QString& cameraId, double exposureMs) override - { - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandSetExposure); - req.insert(QStringLiteral("value"), exposureMs); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, 1200) - || !resp.value(QStringLiteral("ok")).toBool(false)) - { - return false; - } - const double actualExposureMs = resp.value(QStringLiteral("exposureMs")).toDouble(exposureMs); - m_cameras[cameraId]->exposureMs = actualExposureMs; - return true; - } - - QStringList listPropertiesFor(const QString& cameraId) override - { - QStringList out; - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandListProperties); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, 4000)) - { - return out; - } - if (!resp.value(QStringLiteral("ok")).toBool(false)) - { - return out; - } - const QJsonArray properties = resp.value(QStringLiteral("properties")).toArray(); - for (const auto& value : properties) - { - out << value.toString(); - } - return out; - } - - bool readPropertyDetailsFor(const QString& cameraId, - const QString& name, - bool fromCache, - CameraPropertyReadback& readback) override - { - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandGetProperty); - req.insert(QStringLiteral("name"), name); - req.insert(QStringLiteral("fromCache"), fromCache); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, 4000)) - { - return false; - } - if (!resp.value(QStringLiteral("ok")).toBool(false)) - { - return false; - } - - readback.value = resp.value(QStringLiteral("value")).toString(); - readback.type = resp.value(QStringLiteral("propertyType")).toString(QStringLiteral("Unknown")); - readback.readOnly = resp.value(QStringLiteral("readOnly")).toBool(true); - readback.preInit = resp.value(QStringLiteral("preInit")).toBool(false); - readback.hasLimits = resp.value(QStringLiteral("hasLimits")).toBool(false); - readback.lowerLimit = resp.value(QStringLiteral("lowerLimit")).toDouble(0.0); - readback.upperLimit = resp.value(QStringLiteral("upperLimit")).toDouble(0.0); - - const QJsonArray allowedValues = resp.value(QStringLiteral("allowedValues")).toArray(); - for (const auto& value : allowedValues) - { - readback.allowedValues << value.toString(); - } - return true; - } - - bool setPropertyFor(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage) override - { - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandSetProperty); - req.insert(QStringLiteral("name"), name); - req.insert(QStringLiteral("value"), value); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, 4000)) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("DriverHost control request failed"); - } - return false; - } - if (!resp.value(QStringLiteral("ok")).toBool(false)) - { - if (errorMessage) - { - *errorMessage = resp.value(QStringLiteral("error")).toString(); - } - return false; - } - return true; - } - - bool setROIFor(const QString& cameraId, int x, int y, int width, int height) override - { - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandSetRoi); - req.insert(QStringLiteral("x"), x); - req.insert(QStringLiteral("y"), y); - req.insert(QStringLiteral("width"), width); - req.insert(QStringLiteral("height"), height); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, 1200)) - { - qWarning().noquote() << QString("Failed to connect to camera '%1'").arg(cameraId); - return false; - } - const bool ok = resp.value(QStringLiteral("ok")).toBool(false); - if (!ok) - { - qWarning().noquote() << QString("Failed to set ROI for '%1': %2") - .arg(cameraId, resp.value(QStringLiteral("error")).toString(QStringLiteral("Unknown error"))); - } - return ok; - } - - bool clearROIFor(const QString& cameraId) override - { - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandClearRoi); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, 1200)) - { - qWarning().noquote() << QString("Failed to connect to camera '%1'").arg(cameraId); - return false; - } - const bool ok = resp.value(QStringLiteral("ok")).toBool(false); - if (!ok) - { - qWarning().noquote() << QString("Failed to clear ROI for '%1': %2") - .arg(cameraId, resp.value(QStringLiteral("error")).toString(QStringLiteral("Unknown error"))); - } - return ok; - } - - bool getROIFor(const QString& cameraId, int& x, int& y, int& width, int& height) override - { - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandGetRoi); - QJsonObject resp; - if (!sendControlCommand(cameraId, req, &resp, 1200)) - { - qWarning().noquote() << QString("Failed to connect to camera '%1'").arg(cameraId); - return false; - } - const bool ok = resp.value(QStringLiteral("ok")).toBool(false); - if (!ok) - { - qWarning().noquote() << QString("Failed to get ROI for '%1': %2") - .arg(cameraId, resp.value(QStringLiteral("error")).toString(QStringLiteral("Unknown error"))); - return false; - } - - x = resp.value(QStringLiteral("x")).toInt(0); - y = resp.value(QStringLiteral("y")).toInt(0); - width = resp.value(QStringLiteral("width")).toInt(0); - height = resp.value(QStringLiteral("height")).toInt(0); - return true; - } - private: - friend class DriverHostFrameWorker; - - bool waitForControlReady(DriverHostCameraSlot& slot, int timeoutMs); - bool addFrameReader(const QString& cameraId, const QString& shmKey); - void removeFrameReader(const QString& cameraId); - bool prepareFrameReader(const QString& cameraId); - ImageFrame consumeFrameNow(const QString& cameraId); - void scheduleFrameRead(const QString& cameraId); - void completeFrameRead(const QString& cameraId); - bool sendControlCommand(const QString& cameraId, - const QJsonObject& request, - QJsonObject* response, - int timeoutMs) const; - bool sendFrameDeliveryModes(const QStringList& cameraIds, - DriverHostFrameDeliveryMode mode); - void stopDriverHostProcessesAsync(); - void completeAsyncShutdown(); - - QMap> m_cameras; - std::atomic_bool m_frameDeliveryPaused{false}; - bool m_shuttingDown{false}; - bool m_shutdownHadRunningCamera{false}; - int m_shutdownProcessesRemaining{0}; - int m_shutdownKillPollsRemaining{0}; - QString m_shutdownError; - std::function m_shutdownCompletion; - QThread m_frameThread; - DriverHostFrameWorker* m_frameWorker{nullptr}; - }; - - DriverHostCameraBackend::DriverHostCameraBackend(ProcessingFrameGate& processingFrameGate) - : CameraBackend(processingFrameGate) - { - m_frameThread.setObjectName(QStringLiteral("ScopeOneDriverHostFrameReader")); - m_frameWorker = new DriverHostFrameWorker(this); - m_frameWorker->moveToThread(&m_frameThread); - QObject::connect(&m_frameThread, &QThread::finished, - m_frameWorker, &QObject::deleteLater); - m_frameThread.start(); - } - - DriverHostCameraBackend::~DriverHostCameraBackend() - { - shutdownNow(); - m_frameThread.quit(); - m_frameThread.wait(); - m_frameWorker = nullptr; - } - - void DriverHostCameraBackend::shutdownNow() - { - if (m_shuttingDown && m_cameras.isEmpty()) - { - return; - } - m_shuttingDown = true; - m_shutdownCompletion = {}; - CameraBackend::setRecordingFrameDeliveryEnabled(false); - - const bool hadRunningCamera = hasRunningCamera(); - for (auto& slot : m_cameras) - { - slot->isRunning = false; - } - DriverHostFrameWorker* const worker = m_frameWorker; - QMetaObject::invokeMethod(worker, - [worker]() { worker->clear(); }, - Qt::BlockingQueuedConnection); - for (auto& slot : m_cameras) - { - QJsonObject request; - request.insert(driverhost::kMessageTypeField, driverhost::kCommandShutdown); - sendControlCommand(slot->cameraId, request, nullptr, 800); - slot->control->stop(); - slot->process->terminate(); - if (!slot->process->waitForFinished(1500)) - { - slot->process->kill(); - slot->process->waitForFinished(1000); - } - } - m_cameras.clear(); - discardPendingPreviewFrames(); - if (hadRunningCamera) - { - notifyPreviewStopped(); - } - } - - // Stop DriverHost processes without waiting on the facade thread - void DriverHostCameraBackend::shutdown(std::function completion) - { - if (m_shuttingDown) - { - if (!m_shutdownCompletion && !m_shutdownError.isEmpty()) - { - completeAsyncShutdown(); - if (!m_shuttingDown) - { - shutdown(std::move(completion)); - return; - } - } - if (completion) - { - completion(m_cameras.isEmpty() - ? QString() - : m_shutdownError.isEmpty() - ? QStringLiteral("Camera DriverHost shutdown is already in progress") - : m_shutdownError); - } - return; - } - - m_shuttingDown = true; - m_shutdownError.clear(); - m_shutdownCompletion = std::move(completion); - m_shutdownHadRunningCamera = hasRunningCamera(); - m_shutdownProcessesRemaining = 0; - CameraBackend::setRecordingFrameDeliveryEnabled(false); - for (auto& slot : m_cameras) - { - slot->isRunning = false; - } - DriverHostFrameWorker* const worker = m_frameWorker; - const QPointer guardedThis(this); - QMetaObject::invokeMethod( - worker, - [guardedThis, worker]() - { - worker->clear(); - if (guardedThis) - { - QMetaObject::invokeMethod( - guardedThis.data(), - [guardedThis]() - { - if (guardedThis) - { - guardedThis->stopDriverHostProcessesAsync(); - } - }, - Qt::QueuedConnection); - } - }, - Qt::QueuedConnection); - } - - // Request DriverHost exit and fall back to bounded process termination - void DriverHostCameraBackend::stopDriverHostProcessesAsync() - { - if (!m_shutdownCompletion) - { - return; - } - - for (auto& slot : m_cameras) - { - if (slot->process->state() == QProcess::NotRunning) - { - slot->control->stop(); - continue; - } - - ++m_shutdownProcessesRemaining; - QProcess* const process = slot->process.get(); - connect(process, &QProcess::finished, this, - [this](int, QProcess::ExitStatus) - { - if (m_shutdownProcessesRemaining > 0) - { - --m_shutdownProcessesRemaining; - } - if (m_shutdownProcessesRemaining == 0) - { - QTimer::singleShot(0, this, [this]() - { - if (m_shutdownProcessesRemaining == 0) - { - completeAsyncShutdown(); - } - }); - } - }); - - QJsonObject request; - request.insert(driverhost::kMessageTypeField, driverhost::kCommandShutdown); - const QPointer guardedProcess(process); - slot->control->sendRequest( - request, - 800, - [guardedProcess](bool success, const QJsonObject&, const QString&) - { - if (!success - && guardedProcess - && guardedProcess->state() != QProcess::NotRunning) - { - guardedProcess->terminate(); - } - }); - } - - if (m_shutdownProcessesRemaining == 0) - { - completeAsyncShutdown(); - return; - } - - QTimer::singleShot(1500, this, [this]() - { - if (m_shutdownProcessesRemaining == 0) - { - return; - } - for (const auto& slot : m_cameras) - { - if (slot->process->state() != QProcess::NotRunning) - { - slot->process->kill(); - } - } - m_shutdownKillPollsRemaining = 50; - QTimer::singleShot(100, this, [this]() { completeAsyncShutdown(); }); - }); - } - - // Release DriverHost state and complete the owning manager callback - void DriverHostCameraBackend::completeAsyncShutdown() - { - if (!m_shutdownCompletion && m_shutdownError.isEmpty()) - { - return; - } - for (const auto& slot : m_cameras) - { - if (slot->process->state() != QProcess::NotRunning) - { - if (!m_shutdownCompletion) - { - return; - } - if (m_shutdownKillPollsRemaining <= 0) - { - m_shutdownError = QStringLiteral("Camera DriverHost process did not stop after termination"); - auto completion = std::move(m_shutdownCompletion); - completion(m_shutdownError); - return; - } - --m_shutdownKillPollsRemaining; - slot->process->kill(); - QTimer::singleShot(100, this, [this]() { completeAsyncShutdown(); }); - return; - } - } - m_shutdownProcessesRemaining = 0; - m_cameras.clear(); - discardPendingPreviewFrames(); - if (m_shutdownHadRunningCamera) - { - notifyPreviewStopped(); - } - m_shutdownHadRunningCamera = false; - m_shuttingDown = false; - m_shutdownError.clear(); - auto completion = std::move(m_shutdownCompletion); - if (completion) - { - completion({}); - } - } - - // Registers one camera shared memory reader on the worker thread - void DriverHostFrameWorker::addCamera(const QString& cameraId, const QString& shmKey) - { - removeCamera(cameraId); - auto slot = std::make_shared(); - slot->cameraId = cameraId; - slot->shmKey = shmKey; - slot->shm = std::make_unique(); - slot->shm->setNativeKey(shmKey); - m_readers.insert(cameraId, std::move(slot)); - } - - // Removes one camera reader and releases its shared memory mapping - void DriverHostFrameWorker::removeCamera(const QString& cameraId) - { - m_readers.remove(cameraId); - } - - // Releases every shared memory reader owned by the worker - void DriverHostFrameWorker::clear() - { - m_readers.clear(); - } - - // Reset preview coalescing after a DriverHost delivery mode change - void DriverHostFrameWorker::resetPreviewDeliveryState(const QString& cameraId) - { - const auto it = m_readers.find(cameraId); - if (it == m_readers.end()) - { - return; - } - it.value()->pendingAcquiredFrameCount = 0; - it.value()->previewDeliveryTimer.invalidate(); - } - - // Attaches the named camera reader when its mapping is unavailable - bool DriverHostFrameWorker::ensureSharedMemory(const QString& cameraId) - { - const auto it = m_readers.find(cameraId); - return it != m_readers.end() && ensureSharedMemory(*it.value()); - } - - // Attaches one shared memory reader slot - bool DriverHostFrameWorker::ensureSharedMemory(ReaderSlot& slot) - { - const bool wasAttached = slot.shm->isAttached(); - if (!sharedframe::attachRing(*slot.shm)) - { - qWarning().noquote() - << QString("SHM unavailable for %1 (key=%2)").arg(slot.cameraId, slot.shmKey); - return false; - } - if (!wasAttached) - { - qInfo().noquote() - << QString("SHM attached for %1 (key=%2)").arg(slot.cameraId, slot.shmKey); - } - return true; - } - - // Wait for one DriverHost control channel to become ready - bool DriverHostCameraBackend::waitForControlReady(DriverHostCameraSlot& slot, int timeoutMs) - { - return slot.control->waitForReady(timeoutMs); - } - - // Creates one shared memory reader on the dedicated frame thread - bool DriverHostCameraBackend::addFrameReader(const QString& cameraId, const QString& shmKey) - { - DriverHostFrameWorker* const worker = m_frameWorker; - return QMetaObject::invokeMethod( - worker, - [worker, cameraId, shmKey]() { worker->addCamera(cameraId, shmKey); }, - Qt::BlockingQueuedConnection); - } - - // Removes one shared memory reader on the dedicated frame thread - void DriverHostCameraBackend::removeFrameReader(const QString& cameraId) - { - DriverHostFrameWorker* const worker = m_frameWorker; - QMetaObject::invokeMethod( - worker, - [worker, cameraId]() { worker->removeCamera(cameraId); }, - Qt::BlockingQueuedConnection); - } - - // Attaches one reader before preview begins - bool DriverHostCameraBackend::prepareFrameReader(const QString& cameraId) - { - bool attached = false; - DriverHostFrameWorker* const worker = m_frameWorker; - const bool invoked = QMetaObject::invokeMethod( - worker, - [worker, cameraId, &attached]() { attached = worker->ensureSharedMemory(cameraId); }, - Qt::BlockingQueuedConnection); - return invoked && attached; - } - - // Send one JSON command to a DriverHost control channel - bool DriverHostCameraBackend::sendControlCommand(const QString& cameraId, - const QJsonObject& request, - QJsonObject* response, - int timeoutMs) const - { - const QString normalizedId = normalizedCameraId(cameraId); - const auto it = m_cameras.constFind(normalizedId); - if (it == m_cameras.constEnd()) - { - return false; - } - - bool completed = false; - bool ok = false; - QJsonObject capturedResponse; - QEventLoop loop; - QTimer watchdog; - watchdog.setSingleShot(true); - watchdog.setInterval((std::max)(1, timeoutMs + 250)); - connect(&watchdog, &QTimer::timeout, &loop, [&]() - { - if (!completed) - { - completed = true; - ok = false; - } - loop.quit(); - }); - - it.value()->control->sendRequest( - request, - (std::max)(1, timeoutMs), - [&](bool requestOk, const QJsonObject& requestResponse, const QString&) - { - if (completed) - { - return; - } - completed = true; - ok = requestOk; - capturedResponse = requestResponse; - loop.quit(); - }); - - watchdog.start(); - if (!completed) - { - loop.exec(); - } - - if (!ok) - { - return false; - } - if (response) - { - *response = capturedResponse; - } - return true; - } - - // Switch multiple DriverHost producers within one bounded wait - bool DriverHostCameraBackend::sendFrameDeliveryModes(const QStringList& cameraIds, - DriverHostFrameDeliveryMode mode) - { - QJsonObject request; - request.insert(driverhost::kMessageTypeField, driverhost::kCommandSetFrameDeliveryMode); - request.insert(QStringLiteral("mode"), frameDeliveryModeName(mode)); - - struct BatchState - { - int pendingRequests{0}; - bool allSucceeded{true}; - QPointer loop; - }; - auto state = std::make_shared(); - QEventLoop loop; - state->loop = &loop; - for (const QString& cameraId : cameraIds) - { - const auto it = m_cameras.constFind(cameraId); - if (it == m_cameras.constEnd()) - { - state->allSucceeded = false; - continue; - } - - ++state->pendingRequests; - it.value()->control->sendRequest( - request, - 1200, - [state](bool requestOk, - const QJsonObject& response, - const QString&) - { - if (!requestOk || !response.value(QStringLiteral("ok")).toBool(false)) - { - state->allSucceeded = false; - } - --state->pendingRequests; - if (state->pendingRequests == 0 && state->loop) - { - state->loop->quit(); - } - }); - } - - if (state->pendingRequests > 0) - { - loop.exec(); - } - state->loop.clear(); - return state->allSucceeded; - } - - // Reads only the newest ready frame for responsive preview delivery - bool DriverHostFrameWorker::readLatestFrame(ReaderSlot& slot, - QList& frames, - quint64& acquiredFrameCount) - { - const quint64 previousFrameIndex = slot.lastFrameIndex; - if (!sharedframe::readLatestFrame(static_cast(slot.shm->data()), - slot.cameraId, slot.lastFrameIndex, frames)) - { - return false; - } - slot.latestFrame = frames.constLast(); - acquiredFrameCount = slot.lastFrameIndex - previousFrameIndex; - return true; - } - - // Reads every retained shared memory frame in order for recording delivery - bool DriverHostFrameWorker::readAllFrames(ReaderSlot& slot, - QList& frames, - quint64& acquiredFrameCount) - { - const quint64 previousFrameIndex = slot.lastFrameIndex; - if (!sharedframe::readNewFrames(static_cast(slot.shm->data()), - slot.cameraId, slot.lastFrameIndex, frames)) - { - return false; - } - slot.latestFrame = frames.constLast(); - acquiredFrameCount = slot.lastFrameIndex - previousFrameIndex; - return true; - } - - // Reads frames on the worker thread and optionally publishes the batch - ImageFrame DriverHostFrameWorker::consumeFrames(const QString& cameraId, bool publishFrames) - { - const auto it = m_readers.find(cameraId); - if (it == m_readers.end()) - { - return {}; - } - - ReaderSlot& slot = *it.value(); - QList frames; - quint64 acquiredFrameCount = 0; - const quint64 recordingToken = m_owner->recordingFrameDeliveryToken(); - const bool recording = recordingToken != 0; - const bool highRate = m_owner->highRateFrameDeliveryEnabled(); - const quint64 processingToken = publishFrames && highRate - ? m_owner->tryAcquireProcessingFrame(cameraId) - : 0; - const bool processingFrameSelected = processingToken != 0; - const bool previewFrameSelected = publishFrames - && (!slot.previewDeliveryTimer.isValid() - || slot.previewDeliveryTimer.elapsed() >= kPreviewFrameDeliveryIntervalMs); - if (ensureSharedMemory(slot)) - { - if (recording && publishFrames) - { - readAllFrames(slot, frames, acquiredFrameCount); - } - else if (!publishFrames - || !highRate - || processingFrameSelected - || previewFrameSelected) - { - readLatestFrame(slot, frames, acquiredFrameCount); - } - } - - if (processingFrameSelected) - { - if (frames.isEmpty()) - { - m_owner->releaseProcessingFrame(cameraId, processingToken); - } - else - { - m_owner->submitProcessingFrame(frames.constLast(), processingToken); - } - } - if (publishFrames) - { - slot.pendingAcquiredFrameCount += acquiredFrameCount; - } - else - { - slot.pendingAcquiredFrameCount = 0; - } - if (publishFrames - && !frames.isEmpty() - && (recording || !highRate || previewFrameSelected)) - { - m_owner->submitFrames(frames, - slot.pendingAcquiredFrameCount, - recordingToken); - slot.pendingAcquiredFrameCount = 0; - slot.previewDeliveryTimer.restart(); - } - return slot.latestFrame; - } - - // Consume one DriverHost batch and report completion on the backend thread - void DriverHostFrameWorker::consumeFramesAsync(const QString& cameraId) - { - if (!m_owner->m_frameDeliveryPaused.load(std::memory_order_relaxed)) - { - consumeFrames(cameraId, true); - } - DriverHostCameraBackend* const owner = m_owner; - QMetaObject::invokeMethod(owner, - [owner, cameraId]() { owner->completeFrameRead(cameraId); }, - Qt::QueuedConnection); - } - - // Reads the newest frame synchronously for event capture - ImageFrame DriverHostCameraBackend::consumeFrameNow(const QString& cameraId) - { - ImageFrame frame; - DriverHostFrameWorker* const worker = m_frameWorker; - const bool invoked = QMetaObject::invokeMethod( - worker, - [worker, cameraId, &frame]() { frame = worker->consumeFrames(cameraId, false); }, - Qt::BlockingQueuedConnection); - return invoked ? frame : ImageFrame{}; - } - - // Coalesces frame notifications into one worker request per camera - void DriverHostCameraBackend::scheduleFrameRead(const QString& cameraId) - { - const auto it = m_cameras.find(cameraId); - if (it == m_cameras.end() || m_shuttingDown) - { - return; - } - DriverHostCameraSlot& slot = *it.value(); - if (!slot.isRunning || m_frameDeliveryPaused.load(std::memory_order_relaxed)) - { - return; - } - if (slot.frameReadQueued) - { - slot.frameReadRequested = true; - return; - } - - slot.frameReadQueued = true; - DriverHostFrameWorker* const worker = m_frameWorker; - if (!QMetaObject::invokeMethod( - worker, - [worker, cameraId]() { worker->consumeFramesAsync(cameraId); }, - Qt::QueuedConnection)) - { - slot.frameReadQueued = false; - } - } - - // Schedules one deferred read when notifications arrived during a request - void DriverHostCameraBackend::completeFrameRead(const QString& cameraId) - { - const auto it = m_cameras.find(cameraId); - if (it == m_cameras.end()) - { - return; - } - DriverHostCameraSlot& slot = *it.value(); - slot.frameReadQueued = false; - if (!slot.frameReadRequested) - { - return; - } - - slot.frameReadRequested = false; - scheduleFrameRead(cameraId); - } - - // Triggers one camera and waits for its event frame - bool DriverHostCameraBackend::captureEventFrame(const QString& cameraId, - ImageFrame& frame, - int timeoutMs) - { - const QString normalizedId = normalizedCameraId(cameraId); - const auto cameraIt = m_cameras.constFind(normalizedId); - if (cameraIt == m_cameras.constEnd() || !prepareFrameReader(normalizedId)) - { - return false; - } - - const ImageFrame previousFrame = consumeFrameNow(normalizedId); - const quint64 previousFrameIndex = previousFrame.isValid() - ? previousFrame.frameIndex - : 0; - - QJsonObject req; - req.insert(driverhost::kMessageTypeField, driverhost::kCommandCaptureEvent); - QJsonObject resp; - const int waitMs = (timeoutMs > 0) ? timeoutMs : 1500; - if (!sendControlCommand(normalizedId, req, &resp, waitMs + 1000)) - { - return false; - } - if (!resp.value("ok").toBool(false)) - { - return false; - } - - const quint64 targetFrameIndex = - driverhost::decodeUInt64(resp.value(QStringLiteral("frameIndex"))); - QElapsedTimer timer; - timer.start(); - - while (timer.elapsed() <= waitMs) - { - const ImageFrame candidate = consumeFrameNow(normalizedId); - if (candidate.isValid() - && candidate.frameIndex > previousFrameIndex - && (targetFrameIndex == 0 || candidate.frameIndex >= targetFrameIndex)) - { - frame = candidate; - return true; - } - QCoreApplication::processEvents(QEventLoop::AllEvents, 5); - QThread::msleep(1); - } - - return false; - } - - // Pauses or resumes polling while another workflow controls capture - void DriverHostCameraBackend::setFrameDeliveryPaused(bool paused) - { - if (m_frameDeliveryPaused.exchange(paused, std::memory_order_relaxed) == paused) - { - return; - } - if (!paused) - { - for (auto it = m_cameras.begin(); it != m_cameras.end(); ++it) - { - if (it.value()->isRunning) - { - scheduleFrameRead(it.key()); - } - } - } - } - - // Switch DriverHost producers before changing the local recording consumer mode - bool DriverHostCameraBackend::setRecordingFrameDeliveryEnabled(bool enabled) - { - if (enabled && recordingFrameDeliveryEnabled()) - { - return true; - } - - const DriverHostFrameDeliveryMode previewMode = - nonRecordingDeliveryMode(highRateFrameDeliveryEnabled()); - if (!enabled) - { - CameraBackend::setRecordingFrameDeliveryEnabled(false); - return sendFrameDeliveryModes(m_cameras.keys(), previewMode); - } - - const QStringList cameraIds = m_cameras.keys(); - if (!sendFrameDeliveryModes(cameraIds, DriverHostFrameDeliveryMode::AllFrames)) - { - sendFrameDeliveryModes(cameraIds, previewMode); - return false; - } - - for (const QString& cameraId : cameraIds) - { - if (!prepareFrameReader(cameraId)) - { - sendFrameDeliveryModes(cameraIds, previewMode); - return false; - } - consumeFrameNow(cameraId); - } - return CameraBackend::setRecordingFrameDeliveryEnabled(true); - } - - // Switches preview producers between display-rate and processing-rate delivery - bool DriverHostCameraBackend::setHighRateFrameDeliveryEnabled(bool enabled) - { - if (highRateFrameDeliveryEnabled() == enabled) - { - return true; - } - - if (recordingFrameDeliveryEnabled()) - { - return CameraBackend::setHighRateFrameDeliveryEnabled(enabled); - } - - const DriverHostFrameDeliveryMode targetMode = nonRecordingDeliveryMode(enabled); - const DriverHostFrameDeliveryMode rollbackMode = - nonRecordingDeliveryMode(highRateFrameDeliveryEnabled()); - const QStringList cameraIds = m_cameras.keys(); - if (!sendFrameDeliveryModes(cameraIds, targetMode)) - { - sendFrameDeliveryModes(cameraIds, rollbackMode); - return false; - } - return CameraBackend::setHighRateFrameDeliveryEnabled(enabled); - } - - // Start one camera DriverHost process and connect its control channel - bool DriverHostCameraBackend::addDriverHostCamera(const QString& cameraId, - const QString& adapter, - const QString& device, - const QStringList& preInitProperties, - const QStringList& properties, - double exposureMs) - { - const QString normalizedId = normalizedCameraId(cameraId); - const QString adapterName = adapter.trimmed(); - const QString deviceName = device.trimmed(); - if (normalizedId.isEmpty() || adapterName.isEmpty() || deviceName.isEmpty()) - { - return false; - } - if (m_cameras.contains(normalizedId)) - { - return true; - } - auto slot = std::make_shared(); - slot->cameraId = normalizedId; - slot->shmKey = driverhost::sharedMemoryKey(normalizedId); - slot->process = std::make_shared(); - slot->control = std::make_shared( - QStringLiteral("micro-manager"), - normalizedId, - driverhost::controlServerName(normalizedId)); - slot->exposureMs = exposureMs; - const QString driverHostPath = - QDir(QCoreApplication::applicationDirPath()).filePath(driverhost::kExecutableFileName); - if (!QFileInfo::exists(driverHostPath)) - { - qWarning().noquote() << QString("DriverHost executable not found: %1").arg(driverHostPath); - return false; - } - QStringList args; - args << "--provider" << QStringLiteral("micro-manager") - << "--deviceId" << normalizedId - << "--adapter" << adapterName - << "--device" << deviceName - << "--shm" << slot->shmKey - << "--launcherPid" << QString::number(QCoreApplication::applicationPid()); - if (exposureMs > 0.0) - { - args << "--exposure" << QString::number(exposureMs, 'f', 4); - } - for (const QString& encodedProperty : preInitProperties) - { - if (!encodedProperty.isEmpty()) - { - args << "--preinit" << encodedProperty; - } - } - for (const QString& encodedProperty : properties) - { - if (!encodedProperty.isEmpty()) - { - args << "--property" << encodedProperty; - } - } - slot->process->setProgram(driverHostPath); - slot->process->setArguments(args); - slot->process->setProcessChannelMode(QProcess::MergedChannels); - - QProcess* const process = slot->process.get(); - connect(process, &QProcess::readyReadStandardOutput, this, [normalizedId, process]() - { - const QByteArray output = process->readAllStandardOutput(); - if (!output.isEmpty()) - { - QStringList lines = QString::fromUtf8(output).split('\n', Qt::SkipEmptyParts); - for (const QString& line : lines) - { - const QString trimmed = line.trimmed(); - qInfo().noquote() << QString("[DriverHost %1] %2").arg(normalizedId, trimmed); - } - } - }); - connect(process, &QProcess::finished, this, - [this, normalizedId](int, QProcess::ExitStatus) - { - const auto it = m_cameras.find(normalizedId); - if (it == m_cameras.end()) - { - return; - } - const bool wasRunning = it.value()->isRunning; - it.value()->isRunning = false; - const bool wasRecording = recordingFrameDeliveryEnabled(); - if (wasRecording) - { - CameraBackend::setRecordingFrameDeliveryEnabled(false); - } - if (wasRunning) - { - notifyPreviewStopped(); - } - if (wasRecording) - { - emit frameDeliveryFailed( - QStringLiteral("Camera DriverHost exited for '%1'").arg(normalizedId), - 0); - } - }); - - slot->control->addReadyHandler([this, normalizedId](bool ready) - { - if (ready) - { - emit driverHostControlServerListening(normalizedId, driverhost::controlServerName(normalizedId)); - } - }); - slot->control->addEventHandler([this, normalizedId](const QJsonObject& event) - { - const auto it = m_cameras.find(normalizedId); - if (it == m_cameras.end()) - { - return; - } - DriverHostCameraSlot& cameraSlot = *it.value(); - const QString type = event.value(driverhost::kMessageTypeField).toString(); - if (type == driverhost::kEventFrameAvailable) - { - scheduleFrameRead(normalizedId); - return; - } - if (type == driverhost::kEventPreviewState) - { - const bool wasRunning = cameraSlot.isRunning; - cameraSlot.isRunning = event.value(QStringLiteral("running")).toBool(cameraSlot.isRunning); - if (wasRunning && !cameraSlot.isRunning) - { - notifyPreviewStopped(); - } - return; - } - if (type == driverhost::kEventDriverHostError) - { - const QString error = QStringLiteral("DriverHost '%1' error: %2") - .arg(cameraSlot.cameraId, - event.value(QStringLiteral("error")).toString()); - qWarning().noquote() << error; - if (recordingFrameDeliveryEnabled()) - { - CameraBackend::setRecordingFrameDeliveryEnabled(false); - emit frameDeliveryFailed(error, 0); - } - return; - } - }); - - slot->process->start(); - if (!slot->process->waitForStarted(3000)) - { - qWarning().noquote() << QString("Failed to start DriverHost for %1").arg(normalizedId); - return false; - } - m_cameras.insert(normalizedId, slot); - slot->control->start(); - if (!waitForControlReady(*slot, kDriverHostControlReadyTimeoutMs)) - { - qWarning().noquote() - << QString("DriverHost control session did not become ready for %1 within %2 ms") - .arg(normalizedId) - .arg(kDriverHostControlReadyTimeoutMs); - removeDriverHostCamera(normalizedId); - return false; - } - if (!addFrameReader(normalizedId, slot->shmKey)) - { - removeDriverHostCamera(normalizedId); - return false; - } - const DriverHostFrameDeliveryMode deliveryMode = recordingFrameDeliveryEnabled() - ? DriverHostFrameDeliveryMode::AllFrames - : nonRecordingDeliveryMode( - highRateFrameDeliveryEnabled()); - if (!sendFrameDeliveryModes(QStringList{normalizedId}, deliveryMode)) - { - removeDriverHostCamera(normalizedId); - return false; - } - return true; - } - - // Stop one camera DriverHost process and release its resources - void DriverHostCameraBackend::removeDriverHostCamera(const QString& cameraId) - { - const QString normalizedId = normalizedCameraId(cameraId); - if (!m_cameras.contains(normalizedId)) - { - return; - } - const auto slot = m_cameras.value(normalizedId); - const bool wasRunning = slot->isRunning; - slot->isRunning = false; - removeFrameReader(normalizedId); - QJsonObject request; - request.insert(driverhost::kMessageTypeField, driverhost::kCommandShutdown); - sendControlCommand(normalizedId, request, nullptr, 800); - m_cameras.remove(normalizedId); - slot->control->stop(); - slot->process->terminate(); - if (!slot->process->waitForFinished(1500)) - { - slot->process->kill(); - slot->process->waitForFinished(1000); - } - if (wasRunning) - { - notifyPreviewStopped(); - } - } - - std::unique_ptr createDriverHostCameraBackend( - ProcessingFrameGate& processingFrameGate) - { - return std::make_unique(processingFrameGate); - } -} diff --git a/ScopeOneCore/src/DriverHostProviderProxy.cpp b/ScopeOneCore/src/DriverHostClient.cpp similarity index 71% rename from ScopeOneCore/src/DriverHostProviderProxy.cpp rename to ScopeOneCore/src/DriverHostClient.cpp index 7e77b14..8c80edc 100644 --- a/ScopeOneCore/src/DriverHostProviderProxy.cpp +++ b/ScopeOneCore/src/DriverHostClient.cpp @@ -1,6 +1,6 @@ -#include "internal/DriverHostProviderProxy.h" +#include "internal/DriverHostClient.h" -#include "internal/CameraRuntimeControl.h" +#include "internal/CameraFrameDelivery.h" #include "internal/DriverHostProtocol.h" #include "internal/HardwareStreamCodec.h" #include "internal/SharedFrameRing.h" @@ -61,12 +61,25 @@ namespace scopeone::core::internal return false; } + const QString& frameDeliveryModeName(FrameDeliveryMode mode) + { + switch (mode) + { + case FrameDeliveryMode::LatestOnly: + return driverhost::kFrameDeliveryModeLatestOnly; + case FrameDeliveryMode::AllFrames: + return driverhost::kFrameDeliveryModeAllFrames; + case FrameDeliveryMode::PreviewLatest: + break; + } + return driverhost::kFrameDeliveryModePreviewLatest; + } } - class DriverHostProviderTransport final : public QObject + // Talks to one DriverHost on a thread of its own and reads the frames and streams it writes + class DriverHostTransport final : public QObject { public: - using FrameHandler = std::function; using PreviewHandler = std::function; // Where the chunks and states of the signal sources and DAQ devices go, on the transport thread @@ -79,29 +92,29 @@ namespace scopeone::core::internal DaqProvider::StateSink daqState; }; - DriverHostProviderTransport(QString providerId, - QString driverPath, - QString hostKey, - QJsonObject options, - FrameHandler frameHandler, - PreviewHandler previewHandler, - StreamHandlers streamHandlers) + DriverHostTransport(QString providerId, + QStringList arguments, + QString hostKey, + PreviewHandler previewHandler, + StreamHandlers streamHandlers) : m_providerId(std::move(providerId)) - , m_driverPath(std::move(driverPath)) + , m_arguments(std::move(arguments)) , m_hostKey(std::move(hostKey)) - , m_options(std::move(options)) - , m_frameHandler(std::move(frameHandler)) , m_previewHandler(std::move(previewHandler)) , m_streamHandlers(std::move(streamHandlers)) { } + void setFrameDelivery(CameraFrameDelivery* delivery) + { + m_delivery = delivery; + } + bool initialize(HardwareProviderDescriptor& descriptor, QList& devices, QList& signalSources, QList& daqDevices, - QString& defaultXYStage, - QString& defaultZStage, + QJsonObject& description, QString* errorMessage) { m_socket = std::make_unique(); @@ -124,15 +137,6 @@ namespace scopeone::core::internal } return false; } - if (!QFileInfo::exists(m_driverPath)) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Provider module not found: %1") - .arg(m_driverPath); - } - return false; - } m_process = std::make_unique(); m_process->setProcessChannelMode(QProcess::MergedChannels); @@ -155,23 +159,20 @@ namespace scopeone::core::internal { if (m_previewHandler) m_previewHandler(cameraId, false); } + if (!m_runningCameras.isEmpty() && m_delivery) + { + m_delivery->fail(QStringLiteral("DriverHost of '%1' exited").arg(m_providerId)); + } m_runningCameras.clear(); endActiveStreams(); finishWaitingRequest(); }); - QStringList arguments; + QStringList arguments = m_arguments; arguments << QStringLiteral("--provider") << m_providerId - << QStringLiteral("--driver") << m_driverPath << QStringLiteral("--hostKey") << m_hostKey << QStringLiteral("--launcherPid") << QString::number(QCoreApplication::applicationPid()); - if (!m_options.isEmpty()) - { - arguments << QStringLiteral("--option") - << QString::fromUtf8( - QJsonDocument(m_options).toJson(QJsonDocument::Compact)); - } m_process->setProgram(driverHostPath); m_process->setArguments(arguments); m_process->start(); @@ -292,12 +293,58 @@ namespace scopeone::core::internal { daqDevices.append(hardwarestream::daqDeviceDescriptorFromJson(value.toObject())); } - defaultXYStage = response.value(driverhost::kDefaultXYStageField).toString(); - defaultZStage = response.value(driverhost::kDefaultZStageField).toString(); + description = response; if (errorMessage) errorMessage->clear(); return true; } + // The images wait in DriverHost and come over a part at a time, as the frame ring holds + // fewer images than a sequence can have + bool captureEventSequence(const QString& cameraId, + int count, + std::vector& frames, + int timeoutMs) + { + constexpr int kImagesPerPart = 16; + auto it = m_readers.find(cameraId); + if (it == m_readers.end() || count < 1 || !ensureSharedMemory(*it.value())) + { + return false; + } + QList discarded; + readLatest(*it.value(), discarded); + QJsonObject request{{driverhost::kMessageTypeField, driverhost::kCommandCaptureEventSequence}, + {driverhost::kDeviceIdField, cameraId}, + {QStringLiteral("count"), count}, + {QStringLiteral("timeoutMs"), timeoutMs}}; + QJsonObject response; + if (!sendRequest(request, response, timeoutMs + kControlRequestTimeoutMs, nullptr) + || !responseSucceeded(response, nullptr)) + { + return false; + } + frames.clear(); + frames.reserve(static_cast(count)); + for (int first = 0; first < count; first += kImagesPerPart) + { + const int partCount = (std::min)(kImagesPerPart, count - first); + QJsonObject part{{driverhost::kMessageTypeField, driverhost::kCommandSendEventFrames}, + {driverhost::kDeviceIdField, cameraId}, + {QStringLiteral("first"), first}, + {QStringLiteral("count"), partCount}}; + QList partFrames; + if (!sendRequest(part, response, kControlRequestTimeoutMs, nullptr) + || !responseSucceeded(response, nullptr) + || !readAll(*it.value(), partFrames) + || partFrames.size() != partCount) + { + return false; + } + frames.insert(frames.end(), partFrames.cbegin(), partFrames.cend()); + } + return true; + } + // Starts a signal source or a DAQ session and attaches the ring DriverHost made for it bool startStream(const QString& command, const QJsonObject& config, QString* errorMessage) { @@ -409,48 +456,68 @@ namespace scopeone::core::internal return true; } - bool setFrameDeliveryMode(const QString& mode, QString* errorMessage) + // Only the cameras named change, as the others of the DriverHost may keep their live view + bool setFrameDeliveryMode(const QStringList& cameraIds, FrameDeliveryMode mode) { - const QString previousMode = m_deliveryMode; - QStringList changed; - for (const QString& cameraId : m_cameraIds) + const auto send = [this](const QString& cameraId, FrameDeliveryMode deliveryMode) { QJsonObject request; - request.insert(driverhost::kMessageTypeField, - driverhost::kCommandSetFrameDeliveryMode); + request.insert(driverhost::kMessageTypeField, driverhost::kCommandSetFrameDeliveryMode); request.insert(driverhost::kDeviceIdField, cameraId); - request.insert(QStringLiteral("mode"), mode); + request.insert(QStringLiteral("mode"), frameDeliveryModeName(deliveryMode)); QJsonObject response; - if (!sendRequest(request, response, kControlRequestTimeoutMs, errorMessage) - || !responseSucceeded(response, errorMessage)) + return sendRequest(request, response, kControlRequestTimeoutMs, nullptr) + && responseSucceeded(response, nullptr); + }; + QStringList changed; + for (const QString& cameraId : cameraIds) + { + if (!send(cameraId, mode)) { for (const QString& changedCameraId : changed) { - QJsonObject rollback; - rollback.insert(driverhost::kMessageTypeField, - driverhost::kCommandSetFrameDeliveryMode); - rollback.insert(driverhost::kDeviceIdField, changedCameraId); - rollback.insert(QStringLiteral("mode"), previousMode); - QJsonObject ignored; - sendRequest(rollback, ignored, kControlRequestTimeoutMs, nullptr); + send(changedCameraId, m_deliveryModes.value(changedCameraId)); } return false; } changed.append(cameraId); } - m_deliveryMode = mode; - m_readAllFrames = mode == driverhost::kFrameDeliveryModeAllFrames; + for (const QString& cameraId : cameraIds) + { + m_deliveryModes.insert(cameraId, mode); + const auto reader = m_readers.value(cameraId); + if (!reader) + { + continue; + } + // Frames written before the switch belong to the live view, not to the recording + if (mode == FrameDeliveryMode::AllFrames) + { + QList discarded; + readLatest(*reader, discarded); + } + reader->resync = true; + } return true; } - void setFrameDeliveryPaused(bool paused) + // The DriverHost stops writing frames too, and a Micro-Manager camera that an event + // capture stopped starts its live acquisition again when the pause ends + void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) { - m_frameDeliveryPaused = paused; - if (!paused) + for (const QString& cameraId : cameraIds) { - for (const QString& cameraId : m_runningCameras) + if (paused) m_pausedCameras.insert(cameraId); + else m_pausedCameras.remove(cameraId); + QJsonObject request; + request.insert(driverhost::kMessageTypeField, driverhost::kCommandSetFrameDeliveryPaused); + request.insert(driverhost::kDeviceIdField, cameraId); + request.insert(QStringLiteral("paused"), paused); + QJsonObject response; + sendRequest(request, response, kControlRequestTimeoutMs, nullptr); + if (!paused && m_runningCameras.contains(cameraId)) { - deliverAvailableFrames(cameraId, false); + deliverAvailableFrames(cameraId); } } } @@ -625,6 +692,8 @@ namespace scopeone::core::internal std::unique_ptr shm; quint64 lastFrameIndex{0}; ImageFrame latestFrame; + // The frames a camera skipped before its delivery mode changed are not counted again + bool resync{false}; }; void finishWaitingRequest() @@ -687,10 +756,10 @@ namespace scopeone::core::internal const QString type = event.value(driverhost::kMessageTypeField).toString(); const QString deviceId = event.value(driverhost::kDeviceIdField).toString(); if (type == driverhost::kEventFrameAvailable - && !m_frameDeliveryPaused + && !m_pausedCameras.contains(deviceId) && m_runningCameras.contains(deviceId)) { - deliverAvailableFrames(deviceId, m_readAllFrames); + deliverAvailableFrames(deviceId); } else if (type == driverhost::kEventPreviewState) { @@ -736,10 +805,13 @@ namespace scopeone::core::internal } else if (type == driverhost::kEventDriverHostError) { - qWarning().noquote() - << QString("DriverHost '%1' error: %2") - .arg(m_providerId, - event.value(QStringLiteral("error")).toString()); + const QString error = QStringLiteral("DriverHost '%1' error: %2") + .arg(m_providerId, event.value(QStringLiteral("error")).toString()); + qWarning().noquote() << error; + if (m_delivery) + { + m_delivery->fail(error); + } } } @@ -772,25 +844,39 @@ namespace scopeone::core::internal return true; } - void deliverAvailableFrames(const QString& cameraId, bool allFrames) + // A frame that is skipped stays counted, as the frame index of the next one tells how + // many the camera took in between + void deliverAvailableFrames(const QString& cameraId) { auto it = m_readers.find(cameraId); - if (it == m_readers.end()) return; + CameraFrameDelivery* const delivery = m_delivery; + if (it == m_readers.end() || !delivery) + { + return; + } + const bool allFrames = m_deliveryModes.value(cameraId) == FrameDeliveryMode::AllFrames; + if (!allFrames && !delivery->wantsLatestFrame(cameraId)) + { + return; + } + Reader& reader = *it.value(); + const quint64 previousFrameIndex = reader.lastFrameIndex; QList frames; - if (allFrames) readAll(*it.value(), frames); - else readLatest(*it.value(), frames); - if (!m_frameHandler) return; - for (const ImageFrame& frame : frames) + const bool read = allFrames ? readAll(reader, frames) : readLatest(reader, frames); + if (read) { - m_frameHandler(frame); + const quint64 acquiredFrameCount = std::exchange(reader.resync, false) + ? static_cast(frames.size()) + : reader.lastFrameIndex - previousFrameIndex; + delivery->deliverFrames(frames, acquiredFrameCount); } } QString m_providerId; - QString m_driverPath; + QStringList m_arguments; QString m_hostKey; - QJsonObject m_options; - FrameHandler m_frameHandler; + // Set and read on the transport thread only + CameraFrameDelivery* m_delivery{nullptr}; PreviewHandler m_previewHandler; StreamHandlers m_streamHandlers; QHash> m_streams; @@ -802,43 +888,42 @@ namespace scopeone::core::internal QHash> m_readers; QStringList m_cameraIds; QSet m_runningCameras; - QString m_deliveryMode{driverhost::kFrameDeliveryModePreviewLatest}; + QHash m_deliveryModes; + QSet m_pausedCameras; QEventLoop* m_waitingLoop{nullptr}; QJsonObject m_waitingResponse; QString m_lastError; quint64 m_nextRequestId{1}; quint64 m_waitingRequestId{0}; - bool m_frameDeliveryPaused{false}; - bool m_readAllFrames{false}; bool m_cleanedUp{false}; }; - class DriverHostProviderProxy final : public QObject, - public HardwareProvider, - public CameraProvider, - public StageProvider, - public ShutterProvider, - public StateProvider, - public ConfigurationProvider, - public SignalSourceProvider, - public DaqProvider, - public CameraRuntimeControl + // Stands in ScopeOne for what one DriverHost runs. Every call goes to the DriverHost as a + // request on the transport thread. + class DriverHostClient final : public QObject, + public HardwareProvider, + public CameraProvider, + public StageProvider, + public ShutterProvider, + public AutofocusProvider, + public StateProvider, + public ConfigurationProvider, + public SignalSourceProvider, + public DaqProvider, + public CameraFrameSource { public: - DriverHostProviderProxy(QString providerId, - QString driverPath, - QJsonObject options) + DriverHostClient(QString providerId, QStringList arguments) : m_expectedProviderId(std::move(providerId)) - , m_driverPath(std::move(driverPath)) - , m_options(std::move(options)) + , m_arguments(std::move(arguments)) { } - ~DriverHostProviderProxy() override + ~DriverHostClient() override { if (m_worker && m_workerThread.isRunning()) { - DriverHostProviderTransport* const worker = m_worker; + DriverHostTransport* const worker = m_worker; QMetaObject::invokeMethod(worker, [worker]() { worker->cleanup(); }, Qt::BlockingQueuedConnection); @@ -851,12 +936,10 @@ namespace scopeone::core::internal bool initialize(QString* errorMessage) { const QString hostKey = QUuid::createUuid().toString(QUuid::WithoutBraces); - m_worker = new DriverHostProviderTransport( + m_worker = new DriverHostTransport( m_expectedProviderId, - m_driverPath, + m_arguments, hostKey, - m_options, - [this](const ImageFrame& frame) { deliverFrame(frame); }, [this](const QString& cameraId, bool running) { bool anyRunning = false; @@ -883,18 +966,35 @@ namespace scopeone::core::internal bool initialized = false; QString error; - DriverHostProviderTransport* const worker = m_worker; + DriverHostTransport* const worker = m_worker; QMetaObject::invokeMethod( worker, [this, worker, &initialized, &error]() { + QJsonObject description; initialized = worker->initialize(m_descriptor, m_devices, m_signalSources, m_daqDevices, - m_defaultXYStage, - m_defaultZStage, + description, &error); + m_defaultXYStage = description.value(driverhost::kDefaultXYStageField).toString(); + m_defaultZStage = description.value(driverhost::kDefaultZStageField).toString(); + m_defaultShutter = description.value(driverhost::kDefaultShutterField).toString(); + m_defaultAutofocus = description.value(driverhost::kDefaultAutofocusField).toString(); + for (const QJsonValue& value : description.value(driverhost::kDevicesField).toArray()) + { + const QJsonObject device = value.toObject(); + const QString deviceId = device.value(driverhost::kDeviceIdField).toString(); + if (device.value(driverhost::kEventSequenceField).toBool()) + { + m_sequenceCameras.insert(deviceId); + } + if (device.contains(driverhost::kPixelSizeField)) + { + m_pixelSizes.insert(deviceId, device.value(driverhost::kPixelSizeField).toDouble()); + } + } }, Qt::BlockingQueuedConnection); if (!initialized) @@ -956,41 +1056,37 @@ namespace scopeone::core::internal stopStream(driverhost::kCommandStopDaq, deviceId); } - void setFrameSink(FrameSink sink) override + // The frames go to the CameraFrameDelivery instead + void setFrameSink(FrameSink) override { - QMutexLocker locker(&m_sinkMutex); - m_frameSink = std::move(sink); } - void setPreviewStateSink(PreviewStateSink sink) override + void setFrameDelivery(CameraFrameDelivery* delivery) override { - QMutexLocker locker(&m_sinkMutex); - m_previewStateSink = std::move(sink); + DriverHostTransport* const worker = m_worker; + runOnWorker([worker, delivery]() { worker->setFrameDelivery(delivery); }); } - bool startPreview() override + bool setFrameDeliveryMode(const QStringList& cameraIds, FrameDeliveryMode mode) override { - const QStringList ids = cameraIds(); - QStringList started; - for (const QString& cameraId : ids) - { - const bool wasRunning = isPreviewRunning(cameraId); - if (!startPreviewFor(cameraId)) - { - for (const QString& startedId : started) stopPreviewFor(startedId); - return false; - } - if (!wasRunning) started.append(cameraId); - } - return !ids.isEmpty(); + QMutexLocker requestLocker(&m_requestMutex); + bool ok = false; + DriverHostTransport* const worker = m_worker; + runOnWorker([worker, cameraIds, mode, &ok]() { ok = worker->setFrameDeliveryMode(cameraIds, mode); }); + return ok; } - bool stopPreview() override + void setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) override { - bool ok = true; - const QStringList ids = cameraIds(); - for (const QString& cameraId : ids) ok = stopPreviewFor(cameraId) && ok; - return ok; + QMutexLocker requestLocker(&m_requestMutex); + DriverHostTransport* const worker = m_worker; + runOnWorker([worker, cameraIds, paused]() { worker->setFrameDeliveryPaused(cameraIds, paused); }); + } + + void setPreviewStateSink(PreviewStateSink sink) override + { + QMutexLocker locker(&m_sinkMutex); + m_previewStateSink = std::move(sink); } bool startPreviewFor(const QString& cameraId) override @@ -1027,50 +1123,25 @@ namespace scopeone::core::internal return m_runningCameras.contains(cameraId.trimmed()); } - bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const override + bool getExposure(const QString& cameraId, double& exposureMs) const override { - const QStringList targets = targetCameras(cameraIdOrAll); - bool found = false; - double common = 0.0; - for (const QString& cameraId : targets) + QJsonObject response; + if (!isCamera(cameraId) || !request(driverhost::kCommandGetExposure, cameraId, {}, response, nullptr)) { - QJsonObject response; - if (!request(driverhost::kCommandGetExposure, - cameraId, - {}, - response, - nullptr)) - { - return false; - } - const double value = response.value(QStringLiteral("exposureMs")).toDouble(); - if (found && !qFuzzyCompare(common + 1.0, value + 1.0)) return false; - common = value; - found = true; + return false; } - if (found) exposureMs = common; - return found; + exposureMs = response.value(QStringLiteral("exposureMs")).toDouble(); + return true; } - bool setExposure(const QString& cameraIdOrAll, double exposureMs) override + bool setExposure(const QString& cameraId, double exposureMs) override { - const QStringList targets = targetCameras(cameraIdOrAll); - if (targets.isEmpty() || exposureMs <= 0.0) return false; - for (const QString& cameraId : targets) - { - QJsonObject fields; - fields.insert(QStringLiteral("value"), exposureMs); - QJsonObject response; - if (!request(driverhost::kCommandSetExposure, - cameraId, - fields, - response, - nullptr)) - { - return false; - } - } - return true; + QJsonObject fields; + fields.insert(QStringLiteral("value"), exposureMs); + QJsonObject response; + clearPropertyDetails(); + return isCamera(cameraId) && exposureMs > 0.0 + && request(driverhost::kCommandSetExposure, cameraId, fields, response, nullptr); } QStringList listProperties(const QString& deviceId) override @@ -1118,11 +1189,13 @@ namespace scopeone::core::internal fields.insert(QStringLiteral("name"), name); fields.insert(QStringLiteral("value"), value); QJsonObject response; - return request(driverhost::kCommandSetProperty, - deviceId, - fields, - response, - errorMessage); + clearPropertyDetails(); + const bool ok = request(driverhost::kCommandSetProperty, deviceId, fields, response, errorMessage); + if (ok && isCamera(deviceId)) + { + refreshPixelSizes({deviceId.trimmed()}); + } + return ok; } QString getPropertyType(const QString& deviceId, const QString& name) override @@ -1176,21 +1249,23 @@ namespace scopeone::core::internal fields.insert(QStringLiteral("width"), width); fields.insert(QStringLiteral("height"), height); QJsonObject response; - return request(driverhost::kCommandSetRoi, - cameraId, - fields, - response, - nullptr); + const bool ok = request(driverhost::kCommandSetRoi, cameraId, fields, response, nullptr); + if (ok) + { + refreshPixelSizes({cameraId.trimmed()}); + } + return ok; } bool clearROI(const QString& cameraId) override { QJsonObject response; - return request(driverhost::kCommandClearRoi, - cameraId, - {}, - response, - nullptr); + const bool ok = request(driverhost::kCommandClearRoi, cameraId, {}, response, nullptr); + if (ok) + { + refreshPixelSizes({cameraId.trimmed()}); + } + return ok; } bool getROI(const QString& cameraId, @@ -1221,7 +1296,7 @@ namespace scopeone::core::internal { QMutexLocker requestLocker(&m_requestMutex); bool ok = false; - DriverHostProviderTransport* const worker = m_worker; + DriverHostTransport* const worker = m_worker; runOnWorker([worker, &ok, &frame, cameraId, timeoutMs]() { ok = worker->captureEventFrame(cameraId, frame, (std::max)(1, timeoutMs), nullptr); @@ -1229,45 +1304,141 @@ namespace scopeone::core::internal return ok; } - void setFrameDeliveryPaused(const QStringList&, bool paused) override + bool supportsEventSequence(const QString& cameraId) const override { - DriverHostProviderTransport* const worker = m_worker; - runOnWorker([worker, paused]() { worker->setFrameDeliveryPaused(paused); }); + return m_sequenceCameras.contains(cameraId.trimmed()); } - bool setRecordingFrameDeliveryEnabled(const QStringList&, bool enabled) override + bool captureEventSequence(const QString& cameraId, + int count, + std::vector& frames, + int timeoutMs) override { - if (m_recordingDelivery == enabled) return true; - const QString mode = enabled - ? driverhost::kFrameDeliveryModeAllFrames - : m_highRateDelivery - ? driverhost::kFrameDeliveryModeLatestOnly - : driverhost::kFrameDeliveryModePreviewLatest; - if (!setDeliveryMode(mode)) return false; - m_recordingDelivery = enabled; - return true; + QMutexLocker requestLocker(&m_requestMutex); + bool ok = false; + DriverHostTransport* const worker = m_worker; + const QString id = cameraId.trimmed(); + runOnWorker([worker, &ok, &frames, id, count, timeoutMs]() + { + ok = worker->captureEventSequence(id, count, frames, (std::max)(1, timeoutMs)); + }); + return ok; } - bool setHighRateFrameDeliveryEnabled(const QStringList&, bool enabled) override + // Read on the GUI thread whenever the scale bar is drawn, so it never waits for DriverHost + double pixelSizeUm(const QString& cameraId) const override { - if (m_highRateDelivery == enabled) return true; - if (!m_recordingDelivery) - { - const QString mode = enabled - ? driverhost::kFrameDeliveryModeLatestOnly - : driverhost::kFrameDeliveryModePreviewLatest; - if (!setDeliveryMode(mode)) return false; - } - m_highRateDelivery = enabled; - return true; + QMutexLocker locker(&m_stateMutex); + return m_pixelSizes.value(cameraId.trimmed()); } - bool isProcessingFrameTokenCurrent(const QString&, quint64) override { return false; } - void finishProcessingFrame(const QString&, quint64) override {} + long propertySequenceCapacity(const QString& deviceId, const QString& name) override + { + QJsonObject response; + return request(driverhost::kCommandGetPropertySequenceCapacity, deviceId, + {{QStringLiteral("name"), name}}, response, nullptr) + ? static_cast(response.value(QStringLiteral("capacity")).toDouble()) + : 0; + } + + bool startPropertySequence(const QString& deviceId, + const QString& name, + const QStringList& values, + QString* errorMessage) override + { + QJsonObject response; + return request(driverhost::kCommandStartPropertySequence, deviceId, + {{QStringLiteral("name"), name}, + {QStringLiteral("values"), QJsonArray::fromStringList(values)}}, + response, errorMessage); + } + + void stopPropertySequence(const QString& deviceId, const QString& name) override + { + QJsonObject response; + request(driverhost::kCommandStopPropertySequence, deviceId, + {{QStringLiteral("name"), name}}, response, nullptr); + } QString defaultXYStage() const override { return m_defaultXYStage; } QString defaultZStage() const override { return m_defaultZStage; } + long stageSequenceCapacity(const QString& deviceId) const override + { + QJsonObject response; + return request(driverhost::kCommandGetStageSequenceCapacity, deviceId, {}, response, nullptr) + ? static_cast(response.value(QStringLiteral("capacity")).toDouble()) + : 0; + } + + bool startStageSequence(const QString& deviceId, + const std::vector& positions, + QString* errorMessage) override + { + QJsonArray values; + for (const double position : positions) + { + values.append(position); + } + QJsonObject response; + return request(driverhost::kCommandStartStageSequence, deviceId, + {{QStringLiteral("positions"), values}}, response, errorMessage); + } + + void stopStageSequence(const QString& deviceId) override + { + QJsonObject response; + request(driverhost::kCommandStopStageSequence, deviceId, {}, response, nullptr); + } + + QString defaultShutter() const override { return m_defaultShutter; } + + bool autoShutter() const override + { + QJsonObject response; + return request(driverhost::kCommandGetAutoShutter, {}, {}, response, nullptr) + && response.value(QStringLiteral("enabled")).toBool(); + } + + bool setAutoShutter(bool enabled, QString* errorMessage) override + { + QJsonObject response; + return request(driverhost::kCommandSetAutoShutter, {}, + {{QStringLiteral("enabled"), enabled}}, response, errorMessage); + } + + QString defaultAutofocus() const override { return m_defaultAutofocus; } + + // An autofocus can search for longer than a usual request may take + bool runAutofocus(const QString& deviceId, QString* errorMessage) override + { + constexpr int kAutofocusTimeoutMs = 120000; + QJsonObject response; + return request(driverhost::kCommandRunAutofocus, deviceId, {}, response, errorMessage, + kAutofocusTimeoutMs); + } + + std::vector configSettings(const QString& groupName, + const QString& configName) const override + { + QJsonObject response; + std::vector settings; + if (!request(driverhost::kCommandGetConfigSettings, {}, + {{QStringLiteral("group"), groupName}, {QStringLiteral("config"), configName}}, + response, nullptr)) + { + return settings; + } + for (const QJsonValue& value : response.value(QStringLiteral("settings")).toArray()) + { + const QJsonObject setting = value.toObject(); + settings.push_back({setting.value(QStringLiteral("device")).toString(), + setting.value(QStringLiteral("property")).toString(), + setting.value(QStringLiteral("value")).toString()}); + } + return settings; + } + bool getXYPosition(const QString& deviceId, double& x, double& y, @@ -1411,11 +1582,12 @@ namespace scopeone::core::internal QJsonObject fields; fields.insert(QStringLiteral("state"), static_cast(state)); QJsonObject response; - return request(driverhost::kCommandSetState, - deviceId, - fields, - response, - errorMessage); + const bool ok = request(driverhost::kCommandSetState, deviceId, fields, response, errorMessage); + if (ok) + { + refreshPixelSizes(cameraIds()); + } + return ok; } QString stateLabel(const QString& deviceId, long state) const override @@ -1482,34 +1654,16 @@ namespace scopeone::core::internal fields.insert(QStringLiteral("group"), groupName); fields.insert(QStringLiteral("config"), configName); QJsonObject response; - return request(driverhost::kCommandSetConfig, - {}, - fields, - response, - errorMessage); - } - - private: - void deliverFrame(const ImageFrame& frame) - { - FrameSink sink; + clearPropertyDetails(); + const bool ok = request(driverhost::kCommandSetConfig, {}, fields, response, errorMessage); + if (ok) { - QMutexLocker locker(&m_sinkMutex); - sink = m_frameSink; + refreshPixelSizes(cameraIds()); } - if (sink) sink(frame); - } - - QStringList cameraIds() const - { - QStringList result; - for (const HardwareDeviceDescriptor& device : m_devices) - { - if (device.kind == HardwareDeviceKind::Camera) result.append(device.logicalId); - } - return result; + return ok; } + private: bool isCamera(const QString& cameraId) const { const QString normalized = cameraId.trimmed(); @@ -1521,21 +1675,12 @@ namespace scopeone::core::internal }); } - QStringList targetCameras(const QString& cameraIdOrAll) const - { - const QString target = cameraIdOrAll.trimmed(); - if (target.compare(QStringLiteral("All"), Qt::CaseInsensitive) == 0) - { - return cameraIds(); - } - return isCamera(target) ? QStringList{target} : QStringList{}; - } - bool request(const QString& type, const QString& deviceId, const QJsonObject& fields, QJsonObject& response, - QString* errorMessage) const + QString* errorMessage, + int timeoutMs = kControlRequestTimeoutMs) const { QMutexLocker requestLocker(&m_requestMutex); QJsonObject request = fields; @@ -1546,12 +1691,12 @@ namespace scopeone::core::internal } bool transported = false; QString transportError; - DriverHostProviderTransport* const worker = m_worker; - runOnWorker([worker, request, &response, &transported, &transportError]() + DriverHostTransport* const worker = m_worker; + runOnWorker([worker, request, &response, &transported, &transportError, timeoutMs]() { transported = worker->sendRequest(request, response, - kControlRequestTimeoutMs, + timeoutMs, &transportError); }); if (!transported) @@ -1562,19 +1707,61 @@ namespace scopeone::core::internal return responseSucceeded(response, errorMessage); } + // The type, limits and allowed values of a property, kept until a setting changes, as a + // property browser asks for each of them separately and every request crosses processes QJsonObject propertyDetails(const QString& deviceId, const QString& name) { + const QString key = deviceId.trimmed() + QChar(0x1f) + name; + { + QMutexLocker locker(&m_stateMutex); + const auto cached = m_propertyDetails.constFind(key); + if (cached != m_propertyDetails.cend()) + { + return cached.value(); + } + } QJsonObject fields; fields.insert(QStringLiteral("name"), name); fields.insert(QStringLiteral("fromCache"), true); QJsonObject response; - return request(driverhost::kCommandGetProperty, - deviceId, - fields, - response, - nullptr) - ? response - : QJsonObject{}; + if (!request(driverhost::kCommandGetProperty, deviceId, fields, response, nullptr)) + { + return {}; + } + QMutexLocker locker(&m_stateMutex); + m_propertyDetails.insert(key, response); + return response; + } + + QStringList cameraIds() const + { + QStringList result; + for (const HardwareDeviceDescriptor& device : m_devices) + { + if (device.kind == HardwareDeviceKind::Camera) result.append(device.logicalId); + } + return result; + } + + // A setting such as the binning or the objective changes the pixel size, which is read + // again after it + void refreshPixelSizes(const QStringList& cameraIds) + { + for (const QString& cameraId : cameraIds) + { + QJsonObject response; + if (request(driverhost::kCommandGetPixelSize, cameraId, {}, response, nullptr)) + { + QMutexLocker locker(&m_stateMutex); + m_pixelSizes.insert(cameraId, response.value(driverhost::kPixelSizeField).toDouble()); + } + } + } + + void clearPropertyDetails() + { + QMutexLocker locker(&m_stateMutex); + m_propertyDetails.clear(); } bool stageWrite(const QString& command, @@ -1595,7 +1782,7 @@ namespace scopeone::core::internal QMutexLocker requestLocker(&m_requestMutex); bool ok = false; QString error; - DriverHostProviderTransport* const worker = m_worker; + DriverHostTransport* const worker = m_worker; runOnWorker([worker, &command, &config, &ok, &error]() { ok = worker->startStream(command, config, &error); }); if (errorMessage) *errorMessage = error; return ok; @@ -1604,14 +1791,14 @@ namespace scopeone::core::internal void stopStream(const QString& command, const QString& id) { QMutexLocker requestLocker(&m_requestMutex); - DriverHostProviderTransport* const worker = m_worker; + DriverHostTransport* const worker = m_worker; runOnWorker([worker, &command, &id]() { worker->stopStream(command, id, nullptr); }); } // The transport calls these on its thread; each hands the chunk or state to the sink Core set - DriverHostProviderTransport::StreamHandlers streamHandlers() + DriverHostTransport::StreamHandlers streamHandlers() { - DriverHostProviderTransport::StreamHandlers handlers; + DriverHostTransport::StreamHandlers handlers; handlers.timeSeries = [this](const TimeSeriesChunk& chunk) { TimeSeriesSink sink; @@ -1660,15 +1847,6 @@ namespace scopeone::core::internal return handlers; } - bool setDeliveryMode(const QString& mode) - { - QMutexLocker requestLocker(&m_requestMutex); - bool ok = false; - DriverHostProviderTransport* const worker = m_worker; - runOnWorker([worker, mode, &ok]() { ok = worker->setFrameDeliveryMode(mode, nullptr); }); - return ok; - } - // Runs a call on the transport thread and waits for it. Frame and preview callbacks // already run there, so a call from them runs directly instead of waiting on itself. template @@ -1690,17 +1868,18 @@ namespace scopeone::core::internal } QString m_expectedProviderId; - QString m_driverPath; - QJsonObject m_options; + QStringList m_arguments; HardwareProviderDescriptor m_descriptor; QList m_devices; QString m_defaultXYStage; QString m_defaultZStage; + QString m_defaultShutter; + QString m_defaultAutofocus; + QSet m_sequenceCameras; QThread m_workerThread; - DriverHostProviderTransport* m_worker{nullptr}; + DriverHostTransport* m_worker{nullptr}; mutable QMutex m_requestMutex; mutable QMutex m_sinkMutex; - FrameSink m_frameSink; PreviewStateSink m_previewStateSink; TimeSeriesSink m_timeSeriesSink; TimestampedEventSink m_timestampedEventSink; @@ -1711,14 +1890,14 @@ namespace scopeone::core::internal QList m_daqDevices; mutable QMutex m_stateMutex; QSet m_runningCameras; - bool m_recordingDelivery{false}; - bool m_highRateDelivery{false}; + QHash m_propertyDetails; + QHash m_pixelSizes; }; - HardwareProviderPtr createDriverHostProviderProxy(const QString& providerId, - const QString& driverPath, - const QJsonObject& options, - QString* errorMessage) + HardwareProviderPtr createScopeOneProvider(const QString& providerId, + const QString& driverPath, + const QJsonObject& options, + QString* errorMessage) { const QString normalizedProviderId = providerId.trimmed(); const QString normalizedDriverPath = QFileInfo(driverPath).absoluteFilePath(); @@ -1727,10 +1906,26 @@ namespace scopeone::core::internal if (errorMessage) *errorMessage = QStringLiteral("Provider ID and module path are required"); return {}; } - auto proxy = std::make_shared(normalizedProviderId, - normalizedDriverPath, - options); - if (!proxy->initialize(errorMessage)) return {}; - return proxy; + if (!QFileInfo::exists(normalizedDriverPath)) + { + if (errorMessage) *errorMessage = QStringLiteral("Driver not found: %1").arg(normalizedDriverPath); + return {}; + } + QStringList arguments{QStringLiteral("--driver"), normalizedDriverPath}; + if (!options.isEmpty()) + { + arguments << QStringLiteral("--option") + << QString::fromUtf8(QJsonDocument(options).toJson(QJsonDocument::Compact)); + } + return startDriverHost(normalizedProviderId, arguments, errorMessage); + } + + HardwareProviderPtr startDriverHost(const QString& providerId, + const QStringList& arguments, + QString* errorMessage) + { + auto client = std::make_shared(providerId, arguments); + if (!client->initialize(errorMessage)) return {}; + return client; } } diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index 7eee3e7..a9d0724 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -45,11 +45,12 @@ #include #include #include +#include #include #include -#include "MMCore.h" #include "internal/DriverHostProtocol.h" +#include "internal/MicroManagerCamera.h" #include "internal/HardwareStreamCodec.h" #include "internal/SharedFrameRing.h" #include "internal/StreamRing.h" @@ -64,45 +65,7 @@ namespace scopeone::core::internal { using scopeone::core::SharedFrameHeader; - using scopeone::core::SharedMemoryControl; - using scopeone::core::SharedPixelFormat; - using scopeone::core::computeMaxFrameBytes; - using scopeone::core::kSharedFrameHeaderSize; using scopeone::core::kSharedFrameMaxBytes; - using scopeone::core::kSharedFrameNumSlots; - using scopeone::core::kSharedFrameSlotStride; - using scopeone::core::kSharedMemoryControlSize; - - constexpr int kMinimumPollIntervalMs = 1; - constexpr int kMaximumPollIntervalMs = 50; - constexpr int kPreviewFrameDeliveryIntervalMs = 16; - - int pollingIntervalFor(double frameIntervalMs) - { - if (!std::isfinite(frameIntervalMs) || frameIntervalMs <= 0.0) - { - return kMinimumPollIntervalMs; - } - return static_cast(std::clamp(frameIntervalMs / 4.0, - static_cast(kMinimumPollIntervalMs), - static_cast(kMaximumPollIntervalMs))); - } - - // Normalize frame bit depth before publishing shared frame metadata - static quint16 normalizedSharedBitDepth(SharedPixelFormat format, int bitsPerSample) - { - if (format == SharedPixelFormat::Mono8) - { - return 8; - } - if (format == SharedPixelFormat::Mono16) - { - return bitsPerSample >= 1 && bitsPerSample <= 16 - ? static_cast(bitsPerSample) - : 16; - } - return 0; - } class ControlConnection final : public QObject { @@ -197,1266 +160,87 @@ namespace scopeone::core::internal const QString type = message.value(driverhost::kMessageTypeField).toString(); if (requestId == 0 || type.isEmpty()) { - qWarning().noquote() - << QString("Ignoring malformed request on connection %1") - .arg(m_connectionId); - continue; - } - - emit requestReceived(m_connectionId, requestId, type, message); - } - } - - // Notify DriverHost when this connection closes - void onDisconnected() - { - emit connectionClosed(m_connectionId); - deleteLater(); - } - - private: - quint64 m_connectionId{0}; - QLocalSocket* m_socket{nullptr}; - QByteArray m_readBuffer; - }; - - class DriverHostRuntime : public QObject - { - Q_OBJECT - - public: - using QObject::QObject; - virtual bool initializeRuntime() = 0; - virtual QString lastError() const = 0; - virtual void publishHello() = 0; - virtual void handleRequest(quint64 connectionId, - quint64 requestId, - const QString& type, - const QJsonObject& message) = 0; - virtual void stopForExit() = 0; - - signals: - void responseReady(quint64 connectionId, const QJsonObject& response); - void eventReady(const QJsonObject& event); - void shutdownRequested(); - - protected: - static QJsonObject makeResponse(const QString& type, quint64 requestId, bool ok) - { - QJsonObject response = - driverhost::makeEnvelope(driverhost::kMessageKindResponse, type, requestId); - response.insert(QStringLiteral("ok"), ok); - return response; - } - - static QJsonObject makeErrorResponse(const QString& type, quint64 requestId, const QString& error) - { - QJsonObject response = makeResponse(type, requestId, false); - response.insert(QStringLiteral("error"), error); - return response; - } - }; - - class MicroManagerDriverRuntime final : public DriverHostRuntime - { - Q_OBJECT - - public: - // Store launch settings for one camera runtime - MicroManagerDriverRuntime(QString providerId, - QString cameraId, - QString adapter, - QString device, - QString shmKey, - QStringList preInitProperties, - QStringList properties, - double exposureMs, - bool autoPreview, - QObject* parent = nullptr) - : DriverHostRuntime(parent) - , m_providerId(std::move(providerId)) - , m_cameraId(std::move(cameraId)) - , m_adapter(std::move(adapter)) - , m_device(std::move(device)) - , m_shmKey(std::move(shmKey)) - , m_preInitProperties(std::move(preInitProperties)) - , m_properties(std::move(properties)) - , m_exposureMs(exposureMs) - , m_autoPreview(autoPreview) - { - } - - QString lastError() const override - { - return m_lastError; - } - - // Initialize MMCore camera state and shared memory transport - bool initializeRuntime() override - { - m_lastError.clear(); - setState(State::Starting); - - m_timer = new QTimer(this); - m_timer->setTimerType(Qt::PreciseTimer); - connect(m_timer, &QTimer::timeout, - this, &MicroManagerDriverRuntime::pollAndWrite); - - try - { - m_mmcore = std::make_unique(); - const std::string label = m_cameraId.toStdString(); - const std::string adapter = m_adapter.toStdString(); - const std::string device = m_device.toStdString(); - - m_mmcore->loadDevice(label.c_str(), adapter.c_str(), device.c_str()); - QString preInitError; - if (!applyProperties(m_preInitProperties, label, QStringLiteral("pre-init property"), &preInitError)) - { - m_lastError = preInitError; - setState(State::Error, m_lastError); - return false; - } - m_mmcore->initializeDevice(label.c_str()); - m_mmcore->setCameraDevice(label.c_str()); - - QString propertyError; - if (!applyProperties(m_properties, label, QStringLiteral("property"), &propertyError)) - { - m_lastError = propertyError; - setState(State::Error, m_lastError); - return false; - } - - try - { - m_mmcore->setCircularBufferMemoryFootprint(2048); - } - catch (const CMMError&) - { - } - - double finalExposure = 0.0; - if (m_exposureMs > 0.0) - { - m_mmcore->setExposure(m_exposureMs); - } - m_mmcore->waitForDevice(label.c_str()); - finalExposure = m_mmcore->getExposure(); - if (finalExposure <= 0.0) - { - m_lastError = QStringLiteral("Camera reported an invalid exposure"); - setState(State::Error, m_lastError); - return false; - } - m_exposureMs = finalExposure; - } - catch (const CMMError& error) - { - m_lastError = QString::fromStdString(error.getMsg()); - setState(State::Error, m_lastError); - return false; - } - - m_shm = std::make_unique(); - m_shm->setNativeKey(m_shmKey); - QString shmError; - if (!sharedframe::createRing(*m_shm, &shmError)) - { - m_lastError = shmError; - setState(State::Error, m_lastError); - return false; - } - - if (m_autoPreview) - { - QString error; - if (!startPreviewInternal(&error)) - { - m_lastError = error; - setState(State::Error, m_lastError); - return false; - } - } - else - { - setState(State::Idle); - } - - return true; - } - - void publishHello() override; - void handleRequest(quint64 connectionId, - quint64 requestId, - const QString& type, - const QJsonObject& message) override; - void stopForExit() override; - - private: - // Runtime state is mirrored to control clients - enum class State - { - Starting, - Idle, - Previewing, - Error, - ShuttingDown - }; - - enum class FrameDeliveryMode - { - PreviewLatest, - LatestOnly, - AllFrames - }; - - // Frame layout describes one shared memory payload shape - struct FrameLayout - { - unsigned width{0}; - unsigned height{0}; - unsigned bytesPerPixel{0}; - SharedPixelFormat format{SharedPixelFormat::Mono8}; - unsigned stride{0}; - quint64 byteCount{0}; - quint16 bitDepth{8}; - quint16 channels{1}; - }; - - // Answers one request with what fill adds to a success response, or with the error - // MMCore throws while it runs - template - void answer(quint64 connectionId, const QString& type, quint64 requestId, Fill&& fill) - { - try - { - QJsonObject response = makeResponse(type, requestId, true); - fill(response); - emit responseReady(connectionId, response); - } - catch (const CMMError& error) - { - emit responseReady(connectionId, - makeErrorResponse(type, requestId, QString::fromStdString(error.getMsg()))); - } - } - - bool previewRunning() const; - void setState(State state, const QString& error = QString()); - QJsonObject makeEvent(const QString& type) const; - bool applyProperties(const QStringList& encodedProperties, - const std::string& label, - const QString& propertyKind, - QString* errorMessage); - void emitPreviewStateEvent(); - void emitDriverHostErrorEvent(const QString& error); - void emitFrameAvailableEvent(quint64 frameIndex); - bool startPreviewInternal(QString* errorMessage); - bool stopPreviewInternal(QString* errorMessage); - bool captureEventFrameInternal(quint64& frameIndex, QString* errorMessage); - bool writeFrameToSharedMemory(const void* pixels, - quint64 frameAdvance = 1, - quint64* frameIndexOut = nullptr); - void pollAndWrite(); - void updatePollingInterval(quint64 frameCount); - bool ensureFrameLayout(unsigned width, unsigned height, unsigned bytesPerPixel); - void refreshSourceRoi(); - - QString m_providerId; - QString m_cameraId; - QString m_adapter; - QString m_device; - QString m_shmKey; - QStringList m_preInitProperties; - QStringList m_properties; - double m_exposureMs{10.0}; - bool m_autoPreview{false}; - - QString m_lastError; - State m_state{State::Starting}; - - std::unique_ptr m_mmcore; - std::unique_ptr m_shm; - QTimer* m_timer{nullptr}; - QElapsedTimer m_frameIntervalTimer; - QElapsedTimer m_deliveryTimer; - double m_observedFrameIntervalMs{0.0}; - - FrameLayout m_frameLayout{}; - bool m_frameLayoutValid{false}; - bool m_loggedOversizedFrame{false}; - bool m_loggedUnsupportedFormat{false}; - int m_sourceRoiX{0}; - int m_sourceRoiY{0}; - int m_sourceRoiWidth{0}; - int m_sourceRoiHeight{0}; - - quint64 m_frameIndex{0}; - quint64 m_pendingFrameAdvance{0}; - int m_nextWriteSlot{0}; - FrameDeliveryMode m_frameDeliveryMode{FrameDeliveryMode::PreviewLatest}; - }; - - // Publish the initial hello event to connected clients - void MicroManagerDriverRuntime::publishHello() - { - QJsonObject event = makeEvent(driverhost::kEventHello); - event.insert(driverhost::kProviderIdField, m_providerId); - event.insert(driverhost::kDeviceIdField, m_cameraId); - event.insert(driverhost::kDeviceKindField, driverhost::kCapabilityCamera); - event.insert(driverhost::kCapabilitiesField, - QJsonArray{driverhost::kCapabilityCamera, - driverhost::kCapabilityProperties}); - emit eventReady(event); - } - - // Replay encoded cfg properties into the DriverHost MMCore instance - bool MicroManagerDriverRuntime::applyProperties(const QStringList& encodedProperties, - const std::string& label, - const QString& propertyKind, - QString* errorMessage) - { - for (const QString& encodedProperty : encodedProperties) - { - if (encodedProperty.isEmpty()) - { - continue; - } - - QJsonParseError parseError; - const QJsonDocument doc = QJsonDocument::fromJson(encodedProperty.toUtf8(), &parseError); - if (parseError.error != QJsonParseError::NoError || !doc.isObject()) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Invalid %1 payload for '%2'") - .arg(propertyKind, m_cameraId); - } - return false; - } - - const QJsonObject property = doc.object(); - const QString propertyName = property.value(QStringLiteral("name")).toString().trimmed(); - const QString propertyValue = property.value(QStringLiteral("value")).toString(); - if (propertyName.isEmpty()) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Missing %1 name for '%2'") - .arg(propertyKind, m_cameraId); - } - return false; - } - - try - { - m_mmcore->setProperty(label.c_str(), - propertyName.toStdString().c_str(), - propertyValue.toStdString().c_str()); - } - catch (const CMMError& mmError) - { - if (errorMessage) - { - *errorMessage = QString("Failed to apply %1 '%2': %3") - .arg(propertyKind, propertyName, QString::fromStdString(mmError.getMsg())); - } - return false; - } - } - - return true; - } - - // Dispatch one control request and emit a matching response - void MicroManagerDriverRuntime::handleRequest(quint64 connectionId, - quint64 requestId, - const QString& type, - const QJsonObject& message) - { - if (requestId == 0 || type.isEmpty()) - { - return; - } - - if (!m_mmcore && type != driverhost::kCommandShutdown) - { - emit responseReady(connectionId, - makeErrorResponse(type, - requestId, - QStringLiteral("DriverHost runtime not initialized"))); - return; - } - - if (type == driverhost::kCommandDescribe) - { - QJsonObject response = makeResponse(type, requestId, true); - response.insert(driverhost::kProviderIdField, m_providerId); - response.insert(driverhost::kDeviceIdField, m_cameraId); - response.insert(driverhost::kDeviceKindField, driverhost::kCapabilityCamera); - response.insert(driverhost::kCapabilitiesField, - QJsonArray{driverhost::kCapabilityCamera, - driverhost::kCapabilityProperties}); - emit responseReady(connectionId, response); - return; - } - - if (type == driverhost::kCommandStartPreview) - { - QString error; - const bool ok = startPreviewInternal(&error); - emit responseReady(connectionId, - ok - ? makeResponse(type, requestId, true) - : makeErrorResponse(type, requestId, error)); - return; - } - - if (type == driverhost::kCommandStopPreview) - { - QString error; - const bool ok = stopPreviewInternal(&error); - emit responseReady(connectionId, - ok - ? makeResponse(type, requestId, true) - : makeErrorResponse(type, requestId, error)); - return; - } - - if (type == driverhost::kCommandSetFrameDeliveryMode) - { - const QString mode = message.value(QStringLiteral("mode")).toString(); - if (mode == driverhost::kFrameDeliveryModePreviewLatest) - { - m_frameDeliveryMode = FrameDeliveryMode::PreviewLatest; - m_deliveryTimer.invalidate(); - } - else if (mode == driverhost::kFrameDeliveryModeLatestOnly) - { - m_frameDeliveryMode = FrameDeliveryMode::LatestOnly; - } - else if (mode == driverhost::kFrameDeliveryModeAllFrames) - { - m_frameDeliveryMode = FrameDeliveryMode::AllFrames; - m_pendingFrameAdvance = 0; - m_deliveryTimer.invalidate(); - } - else - { - emit responseReady(connectionId, - makeErrorResponse(type, - requestId, - QStringLiteral("Unknown frame delivery mode"))); - return; - } - - QJsonObject response = makeResponse(type, requestId, true); - response.insert(QStringLiteral("mode"), mode); - emit responseReady(connectionId, response); - return; - } - - const std::string camera = m_cameraId.toStdString(); - - if (type == driverhost::kCommandGetExposure) - { - answer(connectionId, type, requestId, [this](QJsonObject& response) - { - m_exposureMs = m_mmcore->getExposure(); - response.insert(QStringLiteral("exposureMs"), m_exposureMs); - }); - return; - } - - if (type == driverhost::kCommandSetExposure) - { - const double exposureMs = message.value(QStringLiteral("value")).toDouble(-1.0); - if (exposureMs <= 0.0) - { - emit responseReady(connectionId, - makeErrorResponse(type, requestId, QStringLiteral("Invalid exposure value"))); - return; - } - answer(connectionId, type, requestId, [this, exposureMs](QJsonObject& response) - { - m_mmcore->setExposure(exposureMs); - m_exposureMs = m_mmcore->getExposure(); - response.insert(QStringLiteral("exposureMs"), m_exposureMs); - }); - return; - } - - if (type == driverhost::kCommandListProperties) - { - answer(connectionId, type, requestId, [this, &camera](QJsonObject& response) - { - QJsonArray properties; - for (const auto& name : m_mmcore->getDevicePropertyNames(camera.c_str())) - { - properties.append(QString::fromStdString(name)); - } - response.insert(QStringLiteral("properties"), properties); - }); - return; - } - - if (type == driverhost::kCommandGetProperty) - { - const QString name = message.value(QStringLiteral("name")).toString(); - const bool fromCache = message.value(QStringLiteral("fromCache")).toBool(false); - const std::string property = name.toStdString(); - QString value; - QString propertyType = QStringLiteral("Unknown"); - bool readOnly = true; - bool preInit = false; - QJsonArray allowedValues; - bool hasLimits = false; - double lowerLimit = 0.0; - double upperLimit = 0.0; - QString error; - bool ok = true; - - try - { - value = QString::fromStdString( - fromCache - ? m_mmcore->getPropertyFromCache(camera.c_str(), property.c_str()) - : m_mmcore->getProperty(camera.c_str(), property.c_str())); - - try - { - switch (m_mmcore->getPropertyType(camera.c_str(), property.c_str())) - { - case MM::String: - propertyType = QStringLiteral("String"); - break; - case MM::Float: - propertyType = QStringLiteral("Float"); - break; - case MM::Integer: - propertyType = QStringLiteral("Integer"); - break; - default: - propertyType = QStringLiteral("Unknown"); - break; - } - } - catch (const CMMError&) - { - } - - try - { - preInit = m_mmcore->isPropertyPreInit(camera.c_str(), property.c_str()); - } - catch (const CMMError&) - { - } - - try - { - readOnly = m_mmcore->isPropertyReadOnly(camera.c_str(), property.c_str()); - } - catch (const CMMError&) - { - } - - try - { - const auto values = - m_mmcore->getAllowedPropertyValues(camera.c_str(), property.c_str()); - for (const auto& allowedValue : values) - { - allowedValues.append(QString::fromStdString(allowedValue)); - } - } - catch (const CMMError&) - { - } - - try - { - hasLimits = m_mmcore->hasPropertyLimits(camera.c_str(), property.c_str()); - if (hasLimits) - { - lowerLimit = m_mmcore->getPropertyLowerLimit(camera.c_str(), property.c_str()); - upperLimit = m_mmcore->getPropertyUpperLimit(camera.c_str(), property.c_str()); - } - } - catch (const CMMError&) - { - hasLimits = false; - } - } - catch (const CMMError& mmError) - { - ok = false; - error = QString::fromStdString(mmError.getMsg()); - } - - if (ok) - { - QJsonObject response = makeResponse(type, requestId, true); - response.insert(QStringLiteral("value"), value); - response.insert(QStringLiteral("propertyType"), propertyType); - response.insert(QStringLiteral("readOnly"), readOnly); - response.insert(QStringLiteral("preInit"), preInit); - response.insert(QStringLiteral("allowedValues"), allowedValues); - response.insert(QStringLiteral("hasLimits"), hasLimits); - response.insert(QStringLiteral("lowerLimit"), lowerLimit); - response.insert(QStringLiteral("upperLimit"), upperLimit); - emit responseReady(connectionId, response); - } - else - { - emit responseReady(connectionId, makeErrorResponse(type, requestId, error)); - } - return; - } - - if (type == driverhost::kCommandSetProperty) - { - const std::string name = message.value(QStringLiteral("name")).toString().toStdString(); - const std::string value = message.value(QStringLiteral("value")).toString().toStdString(); - answer(connectionId, type, requestId, [this, &camera, &name, &value](QJsonObject&) - { - m_mmcore->setProperty(camera.c_str(), name.c_str(), value.c_str()); - m_mmcore->waitForDevice(camera.c_str()); - }); - return; - } - - if (type == driverhost::kCommandSetRoi) - { - const int x = message.value(QStringLiteral("x")).toInt(0); - const int y = message.value(QStringLiteral("y")).toInt(0); - const int width = message.value(QStringLiteral("width")).toInt(0); - const int height = message.value(QStringLiteral("height")).toInt(0); - answer(connectionId, type, requestId, [this, &camera, x, y, width, height](QJsonObject&) - { - m_mmcore->setROI(camera.c_str(), x, y, width, height); - m_mmcore->waitForDevice(camera.c_str()); - refreshSourceRoi(); - }); - return; - } - - if (type == driverhost::kCommandClearRoi) - { - answer(connectionId, type, requestId, [this, &camera](QJsonObject&) - { - m_mmcore->setCameraDevice(camera.c_str()); - m_mmcore->clearROI(); - m_mmcore->waitForDevice(camera.c_str()); - refreshSourceRoi(); - }); - return; - } - - if (type == driverhost::kCommandGetRoi) - { - answer(connectionId, type, requestId, [this, &camera](QJsonObject& response) - { - int x = 0; - int y = 0; - int width = 0; - int height = 0; - m_mmcore->getROI(camera.c_str(), x, y, width, height); - response.insert(QStringLiteral("x"), x); - response.insert(QStringLiteral("y"), y); - response.insert(QStringLiteral("width"), width); - response.insert(QStringLiteral("height"), height); - }); - return; - } - - if (type == driverhost::kCommandCaptureEvent) - { - QString error; - quint64 frameIndex = 0; - const bool ok = captureEventFrameInternal(frameIndex, &error); - if (ok) - { - QJsonObject response = makeResponse(type, requestId, true); - response.insert(QStringLiteral("frameIndex"), driverhost::encodeUInt64(frameIndex)); - emit responseReady(connectionId, response); - } - else - { - emit responseReady(connectionId, makeErrorResponse(type, requestId, error)); - } - return; - } - - if (type == driverhost::kCommandShutdown) - { - stopPreviewInternal(nullptr); - setState(State::ShuttingDown); - emit responseReady(connectionId, makeResponse(type, requestId, true)); - QTimer::singleShot(50, this, [this]() { emit shutdownRequested(); }); - return; - } - - emit responseReady(connectionId, - makeErrorResponse(type, - requestId, - QStringLiteral("Unknown control command"))); - } - - // Stop preview before the runtime thread exits - void MicroManagerDriverRuntime::stopForExit() - { - stopPreviewInternal(nullptr); - setState(State::ShuttingDown); - } - - // Check whether the runtime is currently previewing - bool MicroManagerDriverRuntime::previewRunning() const - { - return m_state == State::Previewing; - } - - // Update runtime state and publish state changes - void MicroManagerDriverRuntime::setState(State state, const QString& error) - { - const bool changed = (m_state != state); - m_state = state; - if (!error.isEmpty()) - { - m_lastError = error; - } - if (changed && (state == State::Previewing - || state == State::Idle - || state == State::Error - || state == State::ShuttingDown)) - { - emitPreviewStateEvent(); - } - if (!error.isEmpty()) - { - emitDriverHostErrorEvent(error); - } - } - - // Build a protocol event envelope - QJsonObject MicroManagerDriverRuntime::makeEvent(const QString& type) const - { - return driverhost::makeEnvelope(driverhost::kMessageKindEvent, type); - } - - // Publish current preview state to clients - void MicroManagerDriverRuntime::emitPreviewStateEvent() - { - QJsonObject event = makeEvent(driverhost::kEventPreviewState); - event.insert(QStringLiteral("running"), previewRunning()); - emit eventReady(event); - } - - // Publish a DriverHost error event to clients - void MicroManagerDriverRuntime::emitDriverHostErrorEvent(const QString& error) - { - QJsonObject event = makeEvent(driverhost::kEventDriverHostError); - event.insert(QStringLiteral("error"), error); - emit eventReady(event); - } - - // Publish the newest shared memory frame index - void MicroManagerDriverRuntime::emitFrameAvailableEvent(quint64 frameIndex) - { - QJsonObject event = makeEvent(driverhost::kEventFrameAvailable); - event.insert(QStringLiteral("frameIndex"), driverhost::encodeUInt64(frameIndex)); - emit eventReady(event); - } - - // Start continuous acquisition and polling - bool MicroManagerDriverRuntime::startPreviewInternal(QString* errorMessage) - { - if (m_state == State::ShuttingDown || m_state == State::Error) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("DriverHost is not in a runnable state"); - } - return false; - } - if (previewRunning()) - { - return true; - } - - try - { - while (m_mmcore->getRemainingImageCount() > 0) - { - m_mmcore->popNextImage(); - } - refreshSourceRoi(); - if (!ensureFrameLayout(m_mmcore->getImageWidth(), - m_mmcore->getImageHeight(), - m_mmcore->getBytesPerPixel())) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Unsupported frame format"); - } - return false; - } - m_mmcore->startContinuousSequenceAcquisition(0.0); - m_observedFrameIntervalMs = 0.0; - m_pendingFrameAdvance = 0; - m_frameIntervalTimer.restart(); - m_deliveryTimer.invalidate(); - m_timer->setInterval(pollingIntervalFor(m_exposureMs)); - m_timer->start(); - setState(State::Previewing); - return true; - } - catch (const CMMError& error) - { - if (errorMessage) - { - *errorMessage = QString::fromStdString(error.getMsg()); - } - return false; - } - } - - // Stop continuous acquisition and polling - bool MicroManagerDriverRuntime::stopPreviewInternal(QString* errorMessage) - { - if (!m_mmcore) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("MMCore not available"); - } - return false; - } - - try - { - if (m_mmcore->isSequenceRunning()) - { - m_mmcore->stopSequenceAcquisition(); - } - if (m_timer) - { - m_timer->stop(); - } - m_frameIntervalTimer.invalidate(); - m_deliveryTimer.invalidate(); - m_observedFrameIntervalMs = 0.0; - m_pendingFrameAdvance = 0; - if (m_state != State::ShuttingDown && m_state != State::Error) - { - setState(State::Idle); - } - return true; - } - catch (const CMMError& error) - { - if (errorMessage) - { - *errorMessage = QString::fromStdString(error.getMsg()); - } - return false; - } - } - - // Capture one frame for recording or API requests - bool MicroManagerDriverRuntime::captureEventFrameInternal(quint64& frameIndex, QString* errorMessage) - { - frameIndex = 0; - try - { - const void* pixels = nullptr; - if (previewRunning()) - { - QElapsedTimer waitTimer; - waitTimer.start(); - while (m_mmcore->getRemainingImageCount() <= 0 && waitTimer.elapsed() < 2000) - { - QThread::msleep(1); - } - if (m_mmcore->getRemainingImageCount() > 0) - { - pixels = m_mmcore->popNextImage(); - } - else if (errorMessage) - { - *errorMessage = QStringLiteral("No frame available from running sequence"); - } - } - else - { - m_mmcore->snapImage(); - pixels = m_mmcore->getImage(); - } - - if (!pixels) - { - if (errorMessage && errorMessage->isEmpty()) - { - *errorMessage = QStringLiteral("Empty image buffer"); - } - return false; - } - - const unsigned width = m_mmcore->getImageWidth(); - const unsigned height = m_mmcore->getImageHeight(); - const unsigned bytesPerPixel = m_mmcore->getBytesPerPixel(); - if (!ensureFrameLayout(width, height, bytesPerPixel)) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Unsupported frame format"); - } - return false; - } - - const quint64 frameAdvance = m_pendingFrameAdvance + 1; - m_pendingFrameAdvance = 0; - if (!writeFrameToSharedMemory(pixels, frameAdvance, &frameIndex)) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Shared memory unavailable"); - } - return false; - } - - if (m_frameDeliveryMode == FrameDeliveryMode::PreviewLatest) - { - m_deliveryTimer.restart(); - } - - emitFrameAvailableEvent(frameIndex); - return true; - } - catch (const CMMError& error) - { - if (errorMessage) - { - *errorMessage = QString::fromStdString(error.getMsg()); - } - return false; - } - } - - // Copy one camera frame into the shared memory ring buffer - bool MicroManagerDriverRuntime::writeFrameToSharedMemory(const void* pixels, - quint64 frameAdvance, - quint64* frameIndexOut) - { - // A frame that finds no free slot still counts, so readers see it as dropped - m_frameIndex += (std::max)(quint64{1}, frameAdvance); - if (frameIndexOut) - { - *frameIndexOut = m_frameIndex; - } - SharedFrameHeader header{}; - header.width = m_frameLayout.width; - header.height = m_frameLayout.height; - header.stride = m_frameLayout.stride; - header.pixelFormat = static_cast(m_frameLayout.format); - header.bitsPerSample = m_frameLayout.bitDepth; - header.channels = m_frameLayout.channels; - header.frameIndex = m_frameIndex; - header.timestampNs = - static_cast(QDateTime::currentMSecsSinceEpoch()) * 1000000ull; - setSharedFrameSourceRoi(header, - m_sourceRoiX, - m_sourceRoiY, - m_sourceRoiWidth, - m_sourceRoiHeight); - return sharedframe::writeFrame(static_cast(m_shm->data()), - m_nextWriteSlot, - header, - pixels, - static_cast(m_frameLayout.byteCount)); - } - - // Drain camera frames into shared memory and publish the newest index - void MicroManagerDriverRuntime::pollAndWrite() - { - try - { - long remaining = m_mmcore->getRemainingImageCount(); - - if (remaining <= 0) - { - return; - } - - quint64 newestFrameIndex = 0; - quint64 acquiredFrameCount = 0; - if (m_frameDeliveryMode != FrameDeliveryMode::AllFrames) - { - const bool previewRateLimited = - m_frameDeliveryMode == FrameDeliveryMode::PreviewLatest; - const bool publishFrame = !previewRateLimited - || !m_deliveryTimer.isValid() - || m_deliveryTimer.elapsed() >= kPreviewFrameDeliveryIntervalMs; - while (remaining-- > 0) - { - const void* pixels = m_mmcore->popNextImage(); - if (!pixels) - { - break; - } - ++acquiredFrameCount; - ++m_pendingFrameAdvance; - if (remaining > 0) - { - continue; - } - if (publishFrame && m_frameLayoutValid) - { - quint64 writtenFrameIndex = 0; - const bool written = writeFrameToSharedMemory( - pixels, - m_pendingFrameAdvance, - &writtenFrameIndex); - m_pendingFrameAdvance = 0; - if (previewRateLimited) - { - m_deliveryTimer.restart(); - } - if (written) - { - newestFrameIndex = writtenFrameIndex; - } - } - } - } - else - { - while (remaining-- > 0) - { - const void* pixels = m_mmcore->popNextImage(); - if (!pixels) - { - break; - } - ++acquiredFrameCount; - - if (!m_frameLayoutValid) - { - break; - } - - quint64 writtenFrameIndex = 0; - if (!writeFrameToSharedMemory(pixels, 1, &writtenFrameIndex)) - { - break; - } - newestFrameIndex = writtenFrameIndex; - } - } - - if (newestFrameIndex != 0) - { - emitFrameAvailableEvent(newestFrameIndex); - } - updatePollingInterval(acquiredFrameCount); - } - catch (const CMMError& error) - { - const QString message = - QStringLiteral("DriverHost capture error: %1") - .arg(QString::fromStdString(error.getMsg())); - setState(State::Error, message); - } - } - - // Adapts MMCore polling to the observed camera frame interval - void MicroManagerDriverRuntime::updatePollingInterval(quint64 frameCount) - { - if (frameCount == 0 || !m_frameIntervalTimer.isValid()) - { - return; - } - - const double elapsedMs = static_cast(m_frameIntervalTimer.nsecsElapsed()) / 1000000.0; - m_frameIntervalTimer.restart(); - const double measuredIntervalMs = elapsedMs / static_cast(frameCount); - if (!std::isfinite(measuredIntervalMs) || measuredIntervalMs <= 0.0) - { - return; - } - - m_observedFrameIntervalMs = m_observedFrameIntervalMs > 0.0 - ? 0.75 * m_observedFrameIntervalMs - + 0.25 * measuredIntervalMs - : measuredIntervalMs; - const int intervalMs = pollingIntervalFor(m_observedFrameIntervalMs); - if (m_timer->interval() != intervalMs) - { - m_timer->setInterval(intervalMs); - } - } - - // Validate and cache the current frame memory layout - bool MicroManagerDriverRuntime::ensureFrameLayout(unsigned width, unsigned height, unsigned bytesPerPixel) - { - const auto logOversized = [this](quint64 byteCount) - { - if (!m_loggedOversizedFrame) - { - qWarning().noquote() - << QString("[DriverHost %1] Frame payload (%2 bytes) exceeds shared memory slot capacity (%3 bytes)") - .arg(m_cameraId) - .arg(static_cast(byteCount)) - .arg(kSharedFrameMaxBytes); - m_loggedOversizedFrame = true; - } - }; - - if (bytesPerPixel != 1 && bytesPerPixel != 2) - { - if (!m_loggedUnsupportedFormat) - { - qWarning().noquote() - << QString("[DriverHost %1] Unsupported bytes-per-pixel (%2). Only Mono8/Mono16 are supported.") - .arg(m_cameraId) - .arg(bytesPerPixel); - m_loggedUnsupportedFormat = true; - } - return false; - } - m_loggedUnsupportedFormat = false; - - const bool geometryChanged = - !m_frameLayoutValid - || m_frameLayout.width != width - || m_frameLayout.height != height - || m_frameLayout.bytesPerPixel != bytesPerPixel; - - if (!geometryChanged) - { - if (m_frameLayout.byteCount == 0 - || m_frameLayout.byteCount > static_cast(kSharedFrameMaxBytes)) - { - logOversized(m_frameLayout.byteCount); - return false; - } - if (m_sourceRoiWidth <= 0 || m_sourceRoiHeight <= 0) - { - refreshSourceRoi(); - } - m_loggedOversizedFrame = false; - return true; - } - - m_frameLayoutValid = false; - - FrameLayout updated; - updated.width = width; - updated.height = height; - updated.bytesPerPixel = bytesPerPixel; - updated.format = - (bytesPerPixel == 2) ? SharedPixelFormat::Mono16 : SharedPixelFormat::Mono8; - updated.channels = 1; - const quint64 stride = static_cast(width) * bytesPerPixel; - if (stride == 0 || stride > (std::numeric_limits::max)()) - { - logOversized(stride); - return false; - } - updated.stride = static_cast(stride); - updated.byteCount = computeMaxFrameBytes(width, height, updated.format); - - if (updated.byteCount == 0 - || updated.byteCount > static_cast(kSharedFrameMaxBytes)) - { - logOversized(updated.byteCount); - return false; - } - - int bitDepth = updated.format == SharedPixelFormat::Mono16 ? 16 : 8; - try - { - bitDepth = static_cast(m_mmcore->getImageBitDepth()); - } - catch (const CMMError&) - { - } - updated.bitDepth = normalizedSharedBitDepth(updated.format, bitDepth); - - m_frameLayout = updated; - m_frameLayoutValid = true; - refreshSourceRoi(); - m_loggedOversizedFrame = false; - return true; - } + qWarning().noquote() + << QString("Ignoring malformed request on connection %1") + .arg(m_connectionId); + continue; + } - // Read the active camera ROI used by following frame metadata - void MicroManagerDriverRuntime::refreshSourceRoi() - { - int x = 0; - int y = 0; - int width = 0; - int height = 0; - try - { - m_mmcore->getROI(m_cameraId.toStdString().c_str(), x, y, width, height); - } - catch (const CMMError&) - { - width = static_cast(m_frameLayout.width); - height = static_cast(m_frameLayout.height); + emit requestReceived(m_connectionId, requestId, type, message); + } } - if (width <= 0 || height <= 0) + // Notify DriverHost when this connection closes + void onDisconnected() { - width = static_cast(m_frameLayout.width); - height = static_cast(m_frameLayout.height); + emit connectionClosed(m_connectionId); + deleteLater(); } - m_sourceRoiX = x; - m_sourceRoiY = y; - m_sourceRoiWidth = width; - m_sourceRoiHeight = height; - } + private: + quint64 m_connectionId{0}; + QLocalSocket* m_socket{nullptr}; + QByteArray m_readBuffer; + }; - class ProviderDriverRuntime final : public DriverHostRuntime + // Runs one provider, a driver or one Micro-Manager camera, and answers the requests of + // ScopeOne on a thread of its own + class DriverRuntime final : public QObject { + Q_OBJECT + public: - ProviderDriverRuntime(QString driverPath, - QString providerId, - QString hostKey, - QJsonObject options, - QObject* parent = nullptr) - : DriverHostRuntime(parent) + // A Micro-Manager camera is run when cameraSetup is set, the driver at driverPath otherwise + DriverRuntime(QString driverPath, + std::optional cameraSetup, + QString providerId, + QString hostKey, + QJsonObject options, + QObject* parent = nullptr) + : QObject(parent) , m_driverPath(std::move(driverPath)) + , m_cameraSetup(std::move(cameraSetup)) , m_providerId(std::move(providerId)) , m_hostKey(std::move(hostKey)) , m_options(std::move(options)) { } - ~ProviderDriverRuntime() override + ~DriverRuntime() override { stopForExit(); } - bool initializeRuntime() override; - QString lastError() const override { return m_lastError; } - void publishHello() override; + bool initializeRuntime(); + QString lastError() const { return m_lastError; } + void publishHello(); void handleRequest(quint64 connectionId, quint64 requestId, const QString& type, - const QJsonObject& message) override; - void stopForExit() override; + const QJsonObject& message); + void stopForExit(); + + signals: + void responseReady(quint64 connectionId, const QJsonObject& response); + void eventReady(const QJsonObject& event); + void shutdownRequested(); private: - enum class FrameDeliveryMode + static QJsonObject makeResponse(const QString& type, quint64 requestId, bool ok) { - PreviewLatest, - LatestOnly, - AllFrames - }; + QJsonObject response = driverhost::makeEnvelope(driverhost::kMessageKindResponse, type, requestId); + response.insert(QStringLiteral("ok"), ok); + return response; + } + + static QJsonObject makeErrorResponse(const QString& type, quint64 requestId, const QString& error) + { + QJsonObject response = makeResponse(type, requestId, false); + response.insert(QStringLiteral("error"), error); + return response; + } + + bool createProvider(); struct CameraTransport { @@ -1464,13 +248,13 @@ namespace scopeone::core::internal QString shmKey; std::unique_ptr shm; std::atomic deliveryMode{FrameDeliveryMode::PreviewLatest}; - QMutex frameMutex; + std::atomic_bool paused{false}; QMutex writeMutex; - ImageFrame latestFrame; - bool latestDispatchQueued{false}; - ImageFrame previewFrame; - QTimer* previewTimer{nullptr}; QElapsedTimer deliveryTimer; + // The newest frame the live view has not been told about, and the timer that tells it + quint64 pendingNotice{0}; + QTimer* noticeTimer{nullptr}; + std::atomic_bool noticeQueued{false}; quint64 frameIndex{0}; int nextWriteSlot{0}; bool previewRunning{false}; @@ -1484,11 +268,8 @@ namespace scopeone::core::internal const HardwareDeviceDescriptor* requestedDevice(const QJsonObject& message) const; std::shared_ptr cameraTransport(const QString& deviceId) const; bool createSharedMemory(CameraTransport& transport); - void enqueueFrame(const ImageFrame& frame); - void dispatchLatestFrame(const QString& deviceId); - void publishAllFrame(const QString& deviceId, const ImageFrame& frame); - void publishFrame(const QString& deviceId, const ImageFrame& frame); - void flushPreviewFrame(const QString& deviceId); + void writeCameraFrame(const ImageFrame& frame); + void sendPendingNotice(const QString& deviceId); bool writeFrame(CameraTransport& transport, const ImageFrame& frame, quint64* frameIndexOut = nullptr); @@ -1523,6 +304,7 @@ namespace scopeone::core::internal static qsizetype daqStreamCapacity(const DaqSessionConfig& config); QString m_driverPath; + std::optional m_cameraSetup; QString m_providerId; QString m_hostKey; QJsonObject m_options; @@ -1534,10 +316,12 @@ namespace scopeone::core::internal mutable QReadWriteLock m_cameraTransportsLock; std::unique_ptr m_driverLoader; HardwareProviderPtr m_provider; + std::shared_ptr m_microManagerCamera; CameraProvider* m_camera{nullptr}; DevicePropertyProvider* m_properties{nullptr}; StageProvider* m_stage{nullptr}; ShutterProvider* m_shutter{nullptr}; + AutofocusProvider* m_autofocus{nullptr}; StateProvider* m_state{nullptr}; ConfigurationProvider* m_configuration{nullptr}; SignalSourceProvider* m_signals{nullptr}; @@ -1547,13 +331,21 @@ namespace scopeone::core::internal QHash> m_streams; QMutex m_streamsMutex; quint64 m_nextStreamIndex{0}; + // The images of the last sequence of each camera, until SendEventFrames has written them + QHash> m_eventSequences; std::atomic_bool m_acceptFrames{false}; std::atomic_bool m_acceptStreams{false}; }; - bool ProviderDriverRuntime::initializeRuntime() + // A Micro-Manager camera is built into DriverHost, a driver comes from its library + bool DriverRuntime::createProvider() { - m_lastError.clear(); + if (m_cameraSetup) + { + m_microManagerCamera = MicroManagerCamera::load(*m_cameraSetup, true, &m_lastError); + m_provider = m_microManagerCamera; + return m_provider != nullptr; + } m_driverLoader = std::make_unique(m_driverPath); DriverManifest manifest; if (!parseDriverManifest( @@ -1599,6 +391,16 @@ namespace scopeone::core::internal } return false; } + return true; + } + + bool DriverRuntime::initializeRuntime() + { + m_lastError.clear(); + if (!createProvider()) + { + return false; + } m_descriptor = m_provider->descriptor(); if (m_descriptor.id != m_providerId || m_descriptor.id != m_descriptor.id.trimmed()) @@ -1612,6 +414,7 @@ namespace scopeone::core::internal m_properties = dynamic_cast(provider); m_stage = dynamic_cast(provider); m_shutter = dynamic_cast(provider); + m_autofocus = dynamic_cast(provider); m_state = dynamic_cast(provider); m_configuration = dynamic_cast(provider); m_signals = dynamic_cast(provider); @@ -1628,7 +431,8 @@ namespace scopeone::core::internal && device.kind != HardwareDeviceKind::ZStage) || m_stage) && (device.kind != HardwareDeviceKind::Shutter || m_shutter) - && (device.kind != HardwareDeviceKind::State || m_state); + && (device.kind != HardwareDeviceKind::State || m_state) + && (device.kind != HardwareDeviceKind::AutoFocus || m_autofocus); if (logicalId.isEmpty() || device.logicalId != logicalId || device.providerId != m_providerId @@ -1648,11 +452,11 @@ namespace scopeone::core::internal auto transport = std::make_shared(); transport->deviceId = logicalId; transport->shmKey = driverhost::sharedMemoryKey(m_hostKey, cameraIndex++); - transport->previewTimer = new QTimer(this); - transport->previewTimer->setSingleShot(true); - transport->previewTimer->setTimerType(Qt::PreciseTimer); - connect(transport->previewTimer, &QTimer::timeout, this, - [this, logicalId]() { flushPreviewFrame(logicalId); }); + transport->noticeTimer = new QTimer(this); + transport->noticeTimer->setSingleShot(true); + transport->noticeTimer->setTimerType(Qt::PreciseTimer); + connect(transport->noticeTimer, &QTimer::timeout, this, + [this, logicalId]() { sendPendingNotice(logicalId); }); if (!createSharedMemory(*transport)) { return false; @@ -1664,13 +468,31 @@ namespace scopeone::core::internal if (m_camera && !m_cameraTransports.isEmpty()) { m_acceptFrames.store(true, std::memory_order_release); - m_camera->setFrameSink([this](const ImageFrame& frame) { enqueueFrame(frame); }); + m_camera->setFrameSink([this](const ImageFrame& frame) { writeCameraFrame(frame); }); + } + if (m_microManagerCamera) + { + const QString cameraId = m_microManagerCamera->cameraId(); + m_microManagerCamera->setErrorSink([this, cameraId](const QString& error) + { + QMetaObject::invokeMethod(this, [this, cameraId, error]() + { + if (const auto transport = cameraTransport(cameraId)) + { + transport->previewRunning = false; + } + emitPreviewStateEvent(cameraId, false); + QJsonObject event = makeEvent(driverhost::kEventDriverHostError); + event.insert(QStringLiteral("error"), error); + emit eventReady(event); + }, Qt::QueuedConnection); + }); } installStreamSinks(); return true; } - void ProviderDriverRuntime::installStreamSinks() + void DriverRuntime::installStreamSinks() { if (m_signals) { @@ -1731,7 +553,7 @@ namespace scopeone::core::internal m_acceptStreams.store(m_signals || m_daq, std::memory_order_release); } - std::shared_ptr ProviderDriverRuntime::openStream( + std::shared_ptr DriverRuntime::openStream( const QString& streamId, qsizetype capacity, QString* errorMessage) @@ -1749,13 +571,13 @@ namespace scopeone::core::internal return stream; } - void ProviderDriverRuntime::closeStream(const QString& streamId) + void DriverRuntime::closeStream(const QString& streamId) { QMutexLocker locker(&m_streamsMutex); m_streams.remove(streamId); } - void ProviderDriverRuntime::markStreamStopped(const QString& streamId) + void DriverRuntime::markStreamStopped(const QString& streamId) { QMutexLocker locker(&m_streamsMutex); if (const auto stream = m_streams.value(streamId)) @@ -1767,7 +589,7 @@ namespace scopeone::core::internal // A stopped stream kept its ring until the next start of the same source, up to 256 MB in // each process per source. The ring goes once the source has ended, and only the ring that // was stopped, as the end of an earlier run can arrive after the next start. - void ProviderDriverRuntime::releaseEndedStream(const QString& streamId, bool failed) + void DriverRuntime::releaseEndedStream(const QString& streamId, bool failed) { QMutexLocker locker(&m_streamsMutex); const auto stream = m_streams.value(streamId); @@ -1777,7 +599,7 @@ namespace scopeone::core::internal } } - void ProviderDriverRuntime::writeStream(const QString& streamId, + void DriverRuntime::writeStream(const QString& streamId, streamring::RecordType type, const QByteArray& payload) { @@ -1813,7 +635,7 @@ namespace scopeone::core::internal // Two seconds of the stream. Photon events have no fixed rate, so their rings are sized for // bright scans instead. - qsizetype ProviderDriverRuntime::signalStreamCapacity(const SignalAcquisitionConfig& config) const + qsizetype DriverRuntime::signalStreamCapacity(const SignalAcquisitionConfig& config) const { constexpr qsizetype kMinimum = 4 * 1024 * 1024; constexpr qsizetype kEvents = 64 * 1024 * 1024; @@ -1832,7 +654,7 @@ namespace scopeone::core::internal return std::clamp(static_cast(2.0 * std::min(bytesPerSecond, 1.0e12)), kMinimum, kMaximum); } - qsizetype ProviderDriverRuntime::daqStreamCapacity(const DaqSessionConfig& config) + qsizetype DriverRuntime::daqStreamCapacity(const DaqSessionConfig& config) { constexpr qsizetype kMinimum = 4 * 1024 * 1024; constexpr qsizetype kMaximum = 256 * 1024 * 1024; @@ -1854,7 +676,7 @@ namespace scopeone::core::internal return std::clamp(static_cast(2.0 * std::min(bytesPerSecond, 1.0e12)), kMinimum, kMaximum); } - bool ProviderDriverRuntime::handleStreamRequest(quint64 requestId, + bool DriverRuntime::handleStreamRequest(quint64 requestId, const QString& type, const QJsonObject& message, const std::function& reply) @@ -1925,14 +747,14 @@ namespace scopeone::core::internal return true; } - QJsonObject ProviderDriverRuntime::makeEvent(const QString& type) const + QJsonObject DriverRuntime::makeEvent(const QString& type) const { QJsonObject event = driverhost::makeEnvelope(driverhost::kMessageKindEvent, type); event.insert(driverhost::kProviderIdField, m_providerId); return event; } - QJsonArray ProviderDriverRuntime::capabilities( + QJsonArray DriverRuntime::capabilities( const HardwareDeviceDescriptor& device) const { QJsonArray result; @@ -1955,18 +777,22 @@ namespace scopeone::core::internal { result.append(driverhost::kCapabilityState); } + if (m_autofocus && device.kind == HardwareDeviceKind::AutoFocus) + { + result.append(driverhost::kCapabilityAutofocus); + } if (m_configuration) result.append(driverhost::kCapabilityConfiguration); return result; } - bool ProviderDriverRuntime::supportsCapability( + bool DriverRuntime::supportsCapability( const HardwareDeviceDescriptor& device, const QString& capability) const { return capabilities(device).contains(QJsonValue(capability)); } - QJsonObject ProviderDriverRuntime::deviceDescription( + QJsonObject DriverRuntime::deviceDescription( const HardwareDeviceDescriptor& device) const { QJsonObject object; @@ -1982,11 +808,13 @@ namespace scopeone::core::internal if (const auto transport = cameraTransport(device.logicalId)) { object.insert(driverhost::kSharedMemoryKeyField, transport->shmKey); + object.insert(driverhost::kEventSequenceField, m_camera->supportsEventSequence(device.logicalId)); + object.insert(driverhost::kPixelSizeField, m_camera->pixelSizeUm(device.logicalId)); } return object; } - const HardwareDeviceDescriptor* ProviderDriverRuntime::requestedDevice( + const HardwareDeviceDescriptor* DriverRuntime::requestedDevice( const QJsonObject& message) const { const QString deviceId = message.value(driverhost::kDeviceIdField) @@ -1995,26 +823,29 @@ namespace scopeone::core::internal return it == m_devicesById.cend() ? nullptr : &it.value(); } - std::shared_ptr - ProviderDriverRuntime::cameraTransport(const QString& deviceId) const + std::shared_ptr + DriverRuntime::cameraTransport(const QString& deviceId) const { QReadLocker locker(&m_cameraTransportsLock); return m_cameraTransports.value(deviceId.trimmed()); } - void ProviderDriverRuntime::publishHello() + void DriverRuntime::publishHello() { emit eventReady(makeEvent(driverhost::kEventHello)); } - bool ProviderDriverRuntime::createSharedMemory(CameraTransport& transport) + bool DriverRuntime::createSharedMemory(CameraTransport& transport) { transport.shm = std::make_unique(); transport.shm->setNativeKey(transport.shmKey); return sharedframe::createRing(*transport.shm, &m_lastError); } - void ProviderDriverRuntime::enqueueFrame(const ImageFrame& frame) + // A frame may point into a buffer of the driver, so it is written to shared memory before the + // call returns. The live view is told about new frames at most every 16 ms, and the notice it + // would miss comes from a timer, so the last frame of a burst is shown too. + void DriverRuntime::writeCameraFrame(const ImageFrame& frame) { if (!m_acceptFrames.load(std::memory_order_acquire)) { @@ -2022,105 +853,70 @@ namespace scopeone::core::internal } const QString deviceId = frame.cameraId.trimmed(); const auto transport = cameraTransport(deviceId); - if (!transport) - { - return; - } - if (transport->deliveryMode.load(std::memory_order_relaxed) - == FrameDeliveryMode::AllFrames) + if (!transport || transport->paused.load(std::memory_order_relaxed)) { - publishAllFrame(deviceId, frame); return; } - + quint64 frameIndex = 0; + int noticeDelayMs = 0; { - QMutexLocker locker(&transport->frameMutex); - transport->latestFrame = frame; - if (transport->latestDispatchQueued) + QMutexLocker locker(&transport->writeMutex); + if (!writeFrame(*transport, frame, &frameIndex)) { return; } - transport->latestDispatchQueued = true; + if (transport->deliveryMode.load(std::memory_order_relaxed) == FrameDeliveryMode::PreviewLatest + && transport->deliveryTimer.isValid()) + { + noticeDelayMs = kPreviewFrameIntervalMs - static_cast(transport->deliveryTimer.elapsed()); + } + if (noticeDelayMs > 0) + { + transport->pendingNotice = frameIndex; + } + else + { + transport->pendingNotice = 0; + transport->deliveryTimer.restart(); + } } - QMetaObject::invokeMethod(this, - [this, deviceId]() { dispatchLatestFrame(deviceId); }, - Qt::QueuedConnection); - } - - void ProviderDriverRuntime::dispatchLatestFrame(const QString& deviceId) - { - const auto transport = cameraTransport(deviceId); - if (!transport) return; - ImageFrame frame; + if (noticeDelayMs <= 0) { - QMutexLocker locker(&transport->frameMutex); - frame = transport->latestFrame; - transport->latestDispatchQueued = false; + emitFrameAvailableEvent(deviceId, frameIndex); + } + else if (!transport->noticeQueued.exchange(true)) + { + QTimer* const timer = transport->noticeTimer; + QMetaObject::invokeMethod(timer, [timer, noticeDelayMs]() { timer->start(noticeDelayMs); }, + Qt::QueuedConnection); } - publishFrame(deviceId, frame); - } - - void ProviderDriverRuntime::publishAllFrame(const QString& deviceId, - const ImageFrame& frame) - { - const auto transport = cameraTransport(deviceId); - if (!transport) return; - quint64 frameIndex = 0; - if (!writeFrame(*transport, frame, &frameIndex)) return; - - emitFrameAvailableEvent(deviceId, frameIndex); } - void ProviderDriverRuntime::publishFrame(const QString& deviceId, - const ImageFrame& frame) + void DriverRuntime::sendPendingNotice(const QString& deviceId) { const auto transport = cameraTransport(deviceId); - if (!transport) return; - if (transport->deliveryMode.load(std::memory_order_relaxed) - == FrameDeliveryMode::PreviewLatest) - { - transport->previewFrame = frame; - const int remaining = transport->deliveryTimer.isValid() - ? kPreviewFrameDeliveryIntervalMs - - static_cast(transport->deliveryTimer.elapsed()) - : 0; - if (remaining > 0) - { - if (!transport->previewTimer->isActive()) - { - transport->previewTimer->start(remaining); - } - return; - } + if (!transport) + { + return; } - + transport->noticeQueued = false; quint64 frameIndex = 0; - if (writeFrame(*transport, frame, &frameIndex)) { - if (transport->deliveryMode.load(std::memory_order_relaxed) - == FrameDeliveryMode::PreviewLatest) + QMutexLocker locker(&transport->writeMutex); + frameIndex = std::exchange(transport->pendingNotice, 0); + if (frameIndex != 0) { transport->deliveryTimer.restart(); } - emitFrameAvailableEvent(deviceId, frameIndex); } - } - - void ProviderDriverRuntime::flushPreviewFrame(const QString& deviceId) - { - const auto transport = cameraTransport(deviceId); - if (!transport) return; - const ImageFrame frame = transport->previewFrame; - transport->previewFrame = {}; - quint64 frameIndex = 0; - if (writeFrame(*transport, frame, &frameIndex)) + if (frameIndex != 0) { - transport->deliveryTimer.restart(); emitFrameAvailableEvent(deviceId, frameIndex); } } - bool ProviderDriverRuntime::writeFrame(CameraTransport& transport, + // The caller holds the write mutex of the transport + bool DriverRuntime::writeFrame(CameraTransport& transport, const ImageFrame& frame, quint64* frameIndexOut) { @@ -2130,7 +926,6 @@ namespace scopeone::core::internal { return false; } - QMutexLocker writeLocker(&transport.writeMutex); transport.frameIndex = (std::max)(transport.frameIndex + 1, frame.frameIndex); SharedFrameHeader header = toSharedFrameHeader(frame); header.frameIndex = transport.frameIndex; @@ -2154,7 +949,7 @@ namespace scopeone::core::internal return true; } - void ProviderDriverRuntime::emitFrameAvailableEvent(const QString& deviceId, quint64 frameIndex) + void DriverRuntime::emitFrameAvailableEvent(const QString& deviceId, quint64 frameIndex) { QJsonObject event = makeEvent(driverhost::kEventFrameAvailable); event.insert(driverhost::kDeviceIdField, deviceId); @@ -2162,7 +957,7 @@ namespace scopeone::core::internal emit eventReady(event); } - void ProviderDriverRuntime::emitPreviewStateEvent(const QString& deviceId, bool running) + void DriverRuntime::emitPreviewStateEvent(const QString& deviceId, bool running) { QJsonObject event = makeEvent(driverhost::kEventPreviewState); event.insert(driverhost::kDeviceIdField, deviceId); @@ -2170,7 +965,7 @@ namespace scopeone::core::internal emit eventReady(event); } - void ProviderDriverRuntime::handleRequest(quint64 connectionId, + void DriverRuntime::handleRequest(quint64 connectionId, quint64 requestId, const QString& type, const QJsonObject& message) @@ -2215,6 +1010,14 @@ namespace scopeone::core::internal response.insert(driverhost::kDefaultXYStageField, m_stage->defaultXYStage()); response.insert(driverhost::kDefaultZStageField, m_stage->defaultZStage()); } + if (m_shutter) + { + response.insert(driverhost::kDefaultShutterField, m_shutter->defaultShutter()); + } + if (m_autofocus) + { + response.insert(driverhost::kDefaultAutofocusField, m_autofocus->defaultAutofocus()); + } reply(response); return; } @@ -2235,11 +1038,32 @@ namespace scopeone::core::internal return; } + if (type == driverhost::kCommandGetAutoShutter || type == driverhost::kCommandSetAutoShutter) + { + if (!m_shutter) + { + unsupported(driverhost::kCapabilityShutter); + return; + } + if (type == driverhost::kCommandGetAutoShutter) + { + QJsonObject response = makeResponse(type, requestId, true); + response.insert(QStringLiteral("enabled"), m_shutter->autoShutter()); + reply(response); + return; + } + QString error; + const bool ok = m_shutter->setAutoShutter(message.value(QStringLiteral("enabled")).toBool(), &error); + reply(ok ? makeResponse(type, requestId, true) : makeErrorResponse(type, requestId, error)); + return; + } + const bool configurationCommand = type == driverhost::kCommandListConfigGroups || type == driverhost::kCommandListConfigs || type == driverhost::kCommandGetCurrentConfig - || type == driverhost::kCommandSetConfig; + || type == driverhost::kCommandSetConfig + || type == driverhost::kCommandGetConfigSettings; const HardwareDeviceDescriptor* const device = configurationCommand ? nullptr : requestedDevice(message); if (!configurationCommand && !device) @@ -2264,38 +1088,51 @@ namespace scopeone::core::internal return; } const QString mode = message.value(QStringLiteral("mode")).toString(); - if (mode == driverhost::kFrameDeliveryModePreviewLatest) - { - transport->deliveryMode.store(FrameDeliveryMode::PreviewLatest, - std::memory_order_relaxed); - } - else if (mode == driverhost::kFrameDeliveryModeLatestOnly) + FrameDeliveryMode deliveryMode = FrameDeliveryMode::PreviewLatest; + if (mode == driverhost::kFrameDeliveryModeLatestOnly) { - transport->deliveryMode.store(FrameDeliveryMode::LatestOnly, - std::memory_order_relaxed); - transport->previewTimer->stop(); - transport->previewFrame = {}; + deliveryMode = FrameDeliveryMode::LatestOnly; } else if (mode == driverhost::kFrameDeliveryModeAllFrames) { - transport->deliveryMode.store(FrameDeliveryMode::AllFrames, - std::memory_order_relaxed); - transport->previewTimer->stop(); - transport->previewFrame = {}; + deliveryMode = FrameDeliveryMode::AllFrames; } - else + else if (mode != driverhost::kFrameDeliveryModePreviewLatest) { reply(makeErrorResponse(type, requestId, QStringLiteral("Unknown frame delivery mode"))); return; } + transport->deliveryMode.store(deliveryMode, std::memory_order_relaxed); + if (m_microManagerCamera) + { + m_microManagerCamera->setFrameDeliveryMode(deliveryMode); + } QJsonObject response = makeResponse(type, requestId, true); response.insert(QStringLiteral("mode"), mode); reply(response); return; } + if (type == driverhost::kCommandSetFrameDeliveryPaused) + { + const auto transport = cameraTransport(deviceId); + if (!transport) + { + unsupported(driverhost::kCapabilityCamera); + return; + } + const bool paused = message.value(QStringLiteral("paused")).toBool(); + transport->paused.store(paused, std::memory_order_relaxed); + if (m_microManagerCamera) + { + m_microManagerCamera->setFrameDeliveryPaused(paused); + } + reply(makeResponse(type, requestId, true)); + return; + } + if (type == driverhost::kCommandStartPreview || type == driverhost::kCommandStopPreview) { @@ -2305,6 +1142,8 @@ namespace scopeone::core::internal return; } const bool start = type == driverhost::kCommandStartPreview; + // Images of a sequence the client gave up on would otherwise stay in memory + m_eventSequences.remove(deviceId); const bool ok = start ? m_camera->startPreviewFor(deviceId) : m_camera->stopPreviewFor(deviceId); @@ -2423,6 +1262,7 @@ namespace scopeone::core::internal if (type == driverhost::kCommandCaptureEvent) { + m_eventSequences.remove(deviceId); if (!supportsCapability(*device, driverhost::kCapabilityCamera)) { unsupported(driverhost::kCapabilityCamera); @@ -2433,9 +1273,14 @@ namespace scopeone::core::internal const auto transport = cameraTransport(deviceId); const int timeoutMs = (std::max)(1, message.value(QStringLiteral("timeoutMs")).toInt(1500)); + const auto writeEventFrame = [&]() + { + QMutexLocker locker(&transport->writeMutex); + return writeFrame(*transport, frame, &frameIndex); + }; if (!transport || !m_camera->captureEventFrame(deviceId, frame, timeoutMs) - || !writeFrame(*transport, frame, &frameIndex)) + || !writeEventFrame()) { reply(makeErrorResponse(type, requestId, @@ -2449,6 +1294,151 @@ namespace scopeone::core::internal return; } + if (type == driverhost::kCommandCaptureEventSequence + || type == driverhost::kCommandSendEventFrames + || type == driverhost::kCommandGetPixelSize) + { + const auto transport = cameraTransport(deviceId); + if (!transport || !supportsCapability(*device, driverhost::kCapabilityCamera)) + { + unsupported(driverhost::kCapabilityCamera); + return; + } + if (type == driverhost::kCommandGetPixelSize) + { + QJsonObject response = makeResponse(type, requestId, true); + response.insert(driverhost::kPixelSizeField, m_camera->pixelSizeUm(deviceId)); + reply(response); + return; + } + if (type == driverhost::kCommandCaptureEventSequence) + { + std::vector frames; + const int count = message.value(QStringLiteral("count")).toInt(); + const int timeoutMs = (std::max)(1, message.value(QStringLiteral("timeoutMs")).toInt(10000)); + if (!m_camera->captureEventSequence(deviceId, count, frames, timeoutMs) + || static_cast(frames.size()) != count) + { + reply(makeErrorResponse(type, requestId, + QStringLiteral("Provider failed to capture an image sequence"))); + return; + } + m_eventSequences.insert(deviceId, std::move(frames)); + reply(makeResponse(type, requestId, true)); + return; + } + const int first = message.value(QStringLiteral("first")).toInt(); + const int count = message.value(QStringLiteral("count")).toInt(); + const auto frames = m_eventSequences.find(deviceId); + if (frames == m_eventSequences.end() || first < 0 || count < 1 + || first + count > static_cast(frames->size())) + { + reply(makeErrorResponse(type, requestId, QStringLiteral("No such images of the sequence"))); + return; + } + { + QMutexLocker locker(&transport->writeMutex); + for (int index = first; index < first + count; ++index) + { + quint64 frameIndex = 0; + if (!writeFrame(*transport, (*frames)[static_cast(index)], &frameIndex)) + { + reply(makeErrorResponse(type, requestId, + QStringLiteral("Failed to write an image of the sequence"))); + return; + } + } + } + if (first + count == static_cast(frames->size())) + { + m_eventSequences.erase(frames); + } + reply(makeResponse(type, requestId, true)); + return; + } + + if (type == driverhost::kCommandGetPropertySequenceCapacity + || type == driverhost::kCommandStartPropertySequence + || type == driverhost::kCommandStopPropertySequence) + { + if (!supportsCapability(*device, driverhost::kCapabilityProperties)) + { + unsupported(driverhost::kCapabilityProperties); + return; + } + const QString name = message.value(QStringLiteral("name")).toString(); + if (type == driverhost::kCommandGetPropertySequenceCapacity) + { + QJsonObject response = makeResponse(type, requestId, true); + response.insert(QStringLiteral("capacity"), + static_cast(m_properties->propertySequenceCapacity(deviceId, name))); + reply(response); + return; + } + if (type == driverhost::kCommandStopPropertySequence) + { + m_properties->stopPropertySequence(deviceId, name); + reply(makeResponse(type, requestId, true)); + return; + } + QStringList values; + for (const QJsonValue& value : message.value(QStringLiteral("values")).toArray()) + { + values.append(value.toString()); + } + QString error; + const bool ok = m_properties->startPropertySequence(deviceId, name, values, &error); + reply(ok ? makeResponse(type, requestId, true) : makeErrorResponse(type, requestId, error)); + return; + } + + if (type == driverhost::kCommandGetStageSequenceCapacity + || type == driverhost::kCommandStartStageSequence + || type == driverhost::kCommandStopStageSequence) + { + if (!supportsCapability(*device, driverhost::kCapabilityStage)) + { + unsupported(driverhost::kCapabilityStage); + return; + } + if (type == driverhost::kCommandGetStageSequenceCapacity) + { + QJsonObject response = makeResponse(type, requestId, true); + response.insert(QStringLiteral("capacity"), + static_cast(m_stage->stageSequenceCapacity(deviceId))); + reply(response); + return; + } + if (type == driverhost::kCommandStopStageSequence) + { + m_stage->stopStageSequence(deviceId); + reply(makeResponse(type, requestId, true)); + return; + } + std::vector positions; + for (const QJsonValue& value : message.value(QStringLiteral("positions")).toArray()) + { + positions.push_back(value.toDouble()); + } + QString error; + const bool ok = m_stage->startStageSequence(deviceId, positions, &error); + reply(ok ? makeResponse(type, requestId, true) : makeErrorResponse(type, requestId, error)); + return; + } + + if (type == driverhost::kCommandRunAutofocus) + { + if (!supportsCapability(*device, driverhost::kCapabilityAutofocus)) + { + unsupported(driverhost::kCapabilityAutofocus); + return; + } + QString error; + const bool ok = m_autofocus->runAutofocus(deviceId, &error); + reply(ok ? makeResponse(type, requestId, true) : makeErrorResponse(type, requestId, error)); + return; + } + if (type == driverhost::kCommandListProperties || type == driverhost::kCommandGetProperty || type == driverhost::kCommandSetProperty) @@ -2660,10 +1650,9 @@ namespace scopeone::core::internal return; } - if (type == driverhost::kCommandListConfigGroups - || type == driverhost::kCommandListConfigs - || type == driverhost::kCommandGetCurrentConfig - || type == driverhost::kCommandSetConfig) + // The same list decides the commands that need no device, so a configuration command is + // never accepted there and then missed here + if (configurationCommand) { if (!m_configuration) { @@ -2680,6 +1669,21 @@ namespace scopeone::core::internal return; } const QString group = message.value(QStringLiteral("group")).toString(); + if (type == driverhost::kCommandGetConfigSettings) + { + QJsonArray settings; + for (const ConfigSetting& setting : + m_configuration->configSettings(group, message.value(QStringLiteral("config")).toString())) + { + settings.append(QJsonObject{{QStringLiteral("device"), setting.device}, + {QStringLiteral("property"), setting.property}, + {QStringLiteral("value"), setting.value}}); + } + QJsonObject response = makeResponse(type, requestId, true); + response.insert(QStringLiteral("settings"), settings); + reply(response); + return; + } if (type == driverhost::kCommandListConfigs) { QJsonObject response = makeResponse(type, requestId, true); @@ -2710,7 +1714,7 @@ namespace scopeone::core::internal reply(makeErrorResponse(type, requestId, QStringLiteral("Unknown control command"))); } - void ProviderDriverRuntime::stopForExit() + void DriverRuntime::stopForExit() { m_acceptFrames.store(false, std::memory_order_release); m_acceptStreams.store(false, std::memory_order_release); @@ -2736,13 +1740,6 @@ namespace scopeone::core::internal } m_signals = nullptr; m_daq = nullptr; - for (const auto& transport : m_cameraTransports) - { - if (transport && transport->previewTimer) - { - transport->previewTimer->stop(); - } - } if (m_camera) { for (const auto& transport : m_cameraTransports) @@ -2755,18 +1752,12 @@ namespace scopeone::core::internal } m_camera->setFrameSink({}); } - for (const auto& transport : m_cameraTransports) - { - if (!transport) continue; - QMutexLocker locker(&transport->frameMutex); - transport->latestFrame = {}; - transport->latestDispatchQueued = false; - transport->previewFrame = {}; - } m_camera = nullptr; + m_microManagerCamera.reset(); m_properties = nullptr; m_stage = nullptr; m_shutter = nullptr; + m_autofocus = nullptr; m_state = nullptr; m_configuration = nullptr; m_provider.reset(); @@ -2790,7 +1781,7 @@ namespace scopeone::core::internal public: // Create the control server wrapper for one isolated runtime DriverHost(QString hostKey, - DriverHostRuntime* runtime, + DriverRuntime* runtime, QObject* parent = nullptr) : QObject(parent) , m_hostKey(std::move(hostKey)) @@ -2823,7 +1814,7 @@ namespace scopeone::core::internal std::unique_ptr m_ctrlServer; QThread m_runtimeThread; - DriverHostRuntime* m_runtime{nullptr}; + DriverRuntime* m_runtime{nullptr}; quint64 m_nextConnectionId{1}; QHash m_connections; }; @@ -2838,11 +1829,11 @@ namespace scopeone::core::internal m_runtime->moveToThread(&m_runtimeThread); connect(&m_runtimeThread, &QThread::finished, m_runtime, &QObject::deleteLater); - connect(m_runtime, &DriverHostRuntime::responseReady, + connect(m_runtime, &DriverRuntime::responseReady, this, &DriverHost::onRuntimeResponse); - connect(m_runtime, &DriverHostRuntime::eventReady, + connect(m_runtime, &DriverRuntime::eventReady, this, &DriverHost::broadcastEvent); - connect(m_runtime, &DriverHostRuntime::shutdownRequested, this, + connect(m_runtime, &DriverRuntime::shutdownRequested, this, []() { QCoreApplication::quit(); }); m_runtimeThread.start(); @@ -2901,12 +1892,12 @@ namespace scopeone::core::internal m_connections.insert(connectionId, connection); connect(connection, &ControlConnection::requestReceived, - m_runtime, &DriverHostRuntime::handleRequest, + m_runtime, &DriverRuntime::handleRequest, Qt::QueuedConnection); connect(connection, &ControlConnection::connectionClosed, this, &DriverHost::onConnectionClosed); - DriverHostRuntime* const runtime = m_runtime; + DriverRuntime* const runtime = m_runtime; QMetaObject::invokeMethod(runtime, [runtime]() { runtime->publishHello(); }, Qt::QueuedConnection); @@ -2950,7 +1941,7 @@ namespace scopeone::core::internal return; } - DriverHostRuntime* const runtime = m_runtime; + DriverRuntime* const runtime = m_runtime; QMetaObject::invokeMethod(runtime, [runtime]() { runtime->stopForExit(); }, Qt::BlockingQueuedConnection); @@ -3039,7 +2030,7 @@ namespace scopeone::core::internal } } // namespace scopeone::core::internal -// Launch one provider in an isolated DriverHost process +// Runs one provider in a process of its own: a driver, or one camera of a Micro-Manager configuration int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); @@ -3051,54 +2042,27 @@ int main(int argc, char* argv[]) QCommandLineOption optProvider(QStringLiteral("provider"), QStringLiteral("Hardware provider ID"), QStringLiteral("id")); - QCommandLineOption optDeviceId(QStringLiteral("deviceId"), - QStringLiteral("Provider device ID"), - QStringLiteral("id")); QCommandLineOption optDriver(QStringLiteral("driver"), QStringLiteral("Driver library path"), QStringLiteral("path")); + QCommandLineOption optCamera(QStringLiteral("micromanagerCamera"), + QStringLiteral("JSON setup of a Micro-Manager camera"), + QStringLiteral("json")); QCommandLineOption optHostKey(QStringLiteral("hostKey"), QStringLiteral("DriverHost instance key"), QStringLiteral("key")); QCommandLineOption optProviderOption(QStringLiteral("option"), QStringLiteral("JSON provider options"), QStringLiteral("json")); - QCommandLineOption optAdapter(QStringLiteral("adapter"), - QStringLiteral("MM adapter"), - QStringLiteral("adapter")); - QCommandLineOption optDevice(QStringLiteral("device"), - QStringLiteral("MM device"), - QStringLiteral("device")); - QCommandLineOption optShm(QStringLiteral("shm"), - QStringLiteral("Shared memory key"), - QStringLiteral("key")); - QCommandLineOption optExp(QStringLiteral("exposure"), - QStringLiteral("Exposure ms"), - QStringLiteral("ms")); - QCommandLineOption optPreInit(QStringLiteral("preinit"), - QStringLiteral("JSON-encoded pre-init property"), - QStringLiteral("json")); - QCommandLineOption optProperty(QStringLiteral("property"), - QStringLiteral("JSON-encoded initialized property"), - QStringLiteral("json")); - QCommandLineOption optAuto(QStringLiteral("autoPreview"), - QStringLiteral("Start preview immediately")); QCommandLineOption optLauncherPid(QStringLiteral("launcherPid"), QStringLiteral("Process ID of the launching app"), QStringLiteral("pid")); parser.addOption(optProvider); - parser.addOption(optDeviceId); parser.addOption(optDriver); + parser.addOption(optCamera); parser.addOption(optHostKey); parser.addOption(optProviderOption); - parser.addOption(optAdapter); - parser.addOption(optDevice); - parser.addOption(optShm); - parser.addOption(optExp); - parser.addOption(optPreInit); - parser.addOption(optProperty); - parser.addOption(optAuto); parser.addOption(optLauncherPid); parser.process(app); @@ -3109,87 +2073,55 @@ int main(int argc, char* argv[]) return 2; } - if (!parser.isSet(optProvider)) + const QString providerId = parser.value(optProvider).trimmed(); + const QString hostKey = parser.value(optHostKey).trimmed(); + const QString driverPath = parser.value(optDriver).trimmed(); + if (providerId.isEmpty() || hostKey.isEmpty() || driverPath.isEmpty() == !parser.isSet(optCamera)) { - qCritical().noquote() - << "Missing required argument: --provider"; + qCritical().noquote() << "DriverHost requires --provider, --hostKey and either --driver or --micromanagerCamera"; return 2; } - const QString providerId = parser.value(optProvider).trimmed(); - const QString deviceId = parser.value(optDeviceId).trimmed(); - if (providerId.isEmpty()) + const auto parseObject = [](const QString& text, QJsonObject& object) { - qCritical().noquote() << "Provider ID cannot be empty"; - return 2; - } + QJsonParseError parseError; + const QJsonDocument document = QJsonDocument::fromJson(text.toUtf8(), &parseError); + if (parseError.error != QJsonParseError::NoError || !document.isObject()) + { + qCritical().noquote() << QStringLiteral("Invalid JSON argument: %1").arg(parseError.errorString()); + return false; + } + object = document.object(); + return true; + }; - const double exposureMs = parser.isSet(optExp) - ? parser.value(optExp).toDouble() - : 0.0; - scopeone::core::internal::DriverHostRuntime* runtime = nullptr; - if (providerId == QStringLiteral("micro-manager")) + std::optional cameraSetup; + if (parser.isSet(optCamera)) { - if (deviceId.isEmpty() - || !parser.isSet(optAdapter) - || !parser.isSet(optDevice) - || !parser.isSet(optShm)) + QJsonObject object; + if (!parseObject(parser.value(optCamera), object)) { - qCritical().noquote() - << "Micro-Manager requires --deviceId, --adapter, --device and --shm"; return 2; } - runtime = new scopeone::core::internal::MicroManagerDriverRuntime( - providerId, - deviceId, - parser.value(optAdapter), - parser.value(optDevice), - parser.value(optShm), - parser.values(optPreInit), - parser.values(optProperty), - exposureMs, - parser.isSet(optAuto)); + cameraSetup = scopeone::core::internal::MicroManagerCameraSetup::fromJson(object); } - else + QJsonObject providerOptions; + for (const QString& encodedOptions : parser.values(optProviderOption)) { - const QString driverPath = parser.value(optDriver).trimmed(); - const QString hostKey = parser.value(optHostKey).trimmed(); - if (driverPath.isEmpty() || hostKey.isEmpty()) + QJsonObject object; + if (!parseObject(encodedOptions, object)) { - qCritical().noquote() << "External providers require --driver and --hostKey"; return 2; } - - QJsonObject providerOptions; - for (const QString& encodedOptions : parser.values(optProviderOption)) + for (auto it = object.constBegin(); it != object.constEnd(); ++it) { - QJsonParseError parseError; - const QJsonDocument document = - QJsonDocument::fromJson(encodedOptions.toUtf8(), &parseError); - if (parseError.error != QJsonParseError::NoError || !document.isObject()) - { - qCritical().noquote() << QStringLiteral("Invalid --option JSON: %1") - .arg(parseError.errorString()); - return 2; - } - const QJsonObject object = document.object(); - for (auto it = object.constBegin(); it != object.constEnd(); ++it) - { - providerOptions.insert(it.key(), it.value()); - } + providerOptions.insert(it.key(), it.value()); } - - runtime = new scopeone::core::internal::ProviderDriverRuntime( - driverPath, - providerId, - hostKey, - providerOptions); } - const QString serverKey = providerId == QStringLiteral("micro-manager") - ? deviceId - : parser.value(optHostKey).trimmed(); - scopeone::core::internal::DriverHost driverHost(serverKey, runtime); + auto* runtime = new scopeone::core::internal::DriverRuntime( + driverPath, std::move(cameraSetup), providerId, hostKey, providerOptions); + scopeone::core::internal::DriverHost driverHost(hostKey, runtime); if (!driverHost.start()) { return 2; diff --git a/ScopeOneCore/src/HardwareRuntime.cpp b/ScopeOneCore/src/HardwareRuntime.cpp index 936533c..009dcc1 100644 --- a/ScopeOneCore/src/HardwareRuntime.cpp +++ b/ScopeOneCore/src/HardwareRuntime.cpp @@ -1,9 +1,13 @@ #include "internal/HardwareRuntime.h" +#include "internal/DaqDeviceManager.h" +#include "internal/DriverHostClient.h" +#include "internal/SignalSourceManager.h" #include "scopeone/CameraProvider.h" #include #include +#include #include #include #include @@ -27,63 +31,75 @@ namespace scopeone::core::internal return call(*provider); } - // The stage that exactly one provider names as its default, or none when they disagree - QString uniqueDefaultStage(const DeviceRegistry& registry, - QString (StageProvider::*defaultStage)() const, - HardwareDeviceKind kind) + // The device of the given kind that exactly one provider names as its default, such as + // the focus stage or the shutter, and that provider. None when two providers disagree. + template + Provider* uniqueDefault(const DeviceRegistry& registry, + QString (Provider::*defaultDevice)() const, + HardwareDeviceKind kind, + QString* deviceId) { - QString match; + Provider* match = nullptr; + QString matchId; for (const HardwareProviderDescriptor& descriptor : registry.providers()) { - const HardwareProviderPtr provider = registry.provider(descriptor.id); - auto* stageProvider = dynamic_cast(provider.get()); - if (!stageProvider) + auto* provider = dynamic_cast(registry.provider(descriptor.id).get()); + const QString id = provider ? (provider->*defaultDevice)().trimmed() : QString{}; + const HardwareDeviceDescriptor device = registry.device(id); + if (id.isEmpty() || device.providerId != descriptor.id || device.kind != kind) { continue; } - const QString deviceId = (stageProvider->*defaultStage)().trimmed(); - const HardwareDeviceDescriptor device = registry.device(deviceId); - if (device.providerId == descriptor.id && device.kind == kind) + if (match) { - if (!match.isEmpty() && match != deviceId) - { - return {}; - } - match = deviceId; + return nullptr; } + match = provider; + matchId = id; } + *deviceId = matchId; return match; } - // Applies one delivery switch to every provider, and undoes it when one provider refuses - bool setDeliveryEnabled(const QList>& controls, - bool (CameraRuntimeControl::*setter)(const QStringList&, bool), - bool enabled) + ShutterProvider* defaultShutter(const DeviceRegistry& registry, QString* shutter) + { + return uniqueDefault(registry, &ShutterProvider::defaultShutter, HardwareDeviceKind::Shutter, shutter); + } + + AutofocusProvider* defaultAutofocus(const DeviceRegistry& registry, QString* device) { - QList> changed; - for (const auto& entry : controls) + return uniqueDefault(registry, &AutofocusProvider::defaultAutofocus, HardwareDeviceKind::AutoFocus, device); + } + + void stopCameras(const DeviceRegistry& registry, const QString& providerId) + { + const HardwareProviderPtr provider = registry.provider(providerId); + auto* cameraProvider = dynamic_cast(provider.get()); + if (!cameraProvider) + { + return; + } + for (const HardwareDeviceDescriptor& device : registry.devices()) { - if (!(entry.first->*setter)(entry.second, enabled)) + if (device.providerId == providerId && device.kind == HardwareDeviceKind::Camera) { - for (const auto& previous : changed) - { - (previous.first->*setter)(previous.second, !enabled); - } - return false; + cameraProvider->stopPreviewFor(device.logicalId); } - changed.append(entry); } - return true; } - void detachCameraProvider(const HardwareProviderPtr& provider) + void detachCameraProvider(const DeviceRegistry& registry, const QString& providerId) { + stopCameras(registry, providerId); + const HardwareProviderPtr provider = registry.provider(providerId); if (auto* cameraProvider = dynamic_cast(provider.get())) { - cameraProvider->stopPreview(); - cameraProvider->setFrameSink({}); cameraProvider->setPreviewStateSink({}); } + if (auto* frameSource = dynamic_cast(provider.get())) + { + frameSource->setFrameDelivery(nullptr); + } } } @@ -250,20 +266,16 @@ namespace scopeone::core::internal HardwareRuntime::HardwareRuntime(QObject* parent) : QObject(parent) , m_registry(this) + , m_microManager(std::make_shared()) , m_acquisitionEngine(m_registry) { connect(&m_registry, &DeviceRegistry::changed, this, &HardwareRuntime::devicesChanged); - } - - void HardwareRuntime::setFrameSink(FrameSink sink) - { - m_frameSink = std::move(sink); - } - - void HardwareRuntime::setPreviewStateSink(PreviewStateSink sink) - { - m_previewStateSink = std::move(sink); + connect(&m_frameDelivery, &CameraFrameDelivery::frameReady, + this, &HardwareRuntime::frameReady); + m_signalSourceManager = new SignalSourceManager(&m_registry, this); + m_daqDeviceManager = new DaqDeviceManager(&m_registry, this); + registerProvider(m_microManager); } CameraProvider* HardwareRuntime::cameraProviderForDevice(const QString& logicalId) const @@ -324,27 +336,6 @@ namespace scopeone::core::internal return match; } - QList HardwareRuntime::cameraProviders() const - { - QList result; - QSet visited; - for (const HardwareDeviceDescriptor& device : m_registry.devices()) - { - if (device.kind != HardwareDeviceKind::Camera) - { - continue; - } - const HardwareProviderPtr provider = m_registry.provider(device.providerId); - auto* cameraProvider = dynamic_cast(provider.get()); - if (cameraProvider && !visited.contains(cameraProvider)) - { - visited.insert(cameraProvider); - result.append(cameraProvider); - } - } - return result; - } - bool HardwareRuntime::startPreview() { return m_acquisitionEngine.start(QStringLiteral("All")); @@ -415,13 +406,19 @@ namespace scopeone::core::internal CameraProvider* provider = cameraProviderForDevice(target); return provider && provider->setExposure(target, exposureMs); } - const QList providers = cameraProviders(); - bool ok = !providers.isEmpty(); - for (CameraProvider* provider : providers) + bool ok = false; + bool all = true; + for (const HardwareDeviceDescriptor& device : m_registry.devices()) { - ok = provider->setExposure(QStringLiteral("All"), exposureMs) && ok; + if (device.kind != HardwareDeviceKind::Camera) + { + continue; + } + CameraProvider* provider = cameraProviderForDevice(device.logicalId); + ok = true; + all = provider && provider->setExposure(device.logicalId, exposureMs) && all; } - return ok; + return ok && all; } QStringList HardwareRuntime::listProperties(const QString& cameraId) @@ -539,25 +536,24 @@ namespace scopeone::core::internal return provider && provider->captureEventSequence(cameraId, count, frames, timeoutMs); } - QList> HardwareRuntime::runtimeControlsFor( + QList> HardwareRuntime::frameSourcesFor( const QStringList& cameraIds) const { - QHash grouped; + QHash grouped; for (const QString& cameraId : cameraIds) { const QString normalizedId = cameraId.trimmed(); const HardwareProviderPtr provider = m_registry.providerForDevice(normalizedId); - if (auto* control = dynamic_cast(provider.get())) + if (auto* source = dynamic_cast(provider.get())) { - QStringList& providerCameraIds = grouped[control]; + QStringList& providerCameraIds = grouped[source]; if (!providerCameraIds.contains(normalizedId)) { providerCameraIds.append(normalizedId); } } } - QList> result; - result.reserve(grouped.size()); + QList> result; for (auto it = grouped.cbegin(); it != grouped.cend(); ++it) { result.append({it.key(), it.value()}); @@ -565,56 +561,112 @@ namespace scopeone::core::internal return result; } + // Switches each camera to its mode, and every camera back when one provider refuses + bool HardwareRuntime::applyFrameDeliveryModes(const QHash& modes) + { + const auto apply = [this](const QStringList& cameraIds, + const std::function& modeOf) + { + for (const auto& [source, providerCameraIds] : frameSourcesFor(cameraIds)) + { + for (const FrameDeliveryMode mode : {FrameDeliveryMode::PreviewLatest, + FrameDeliveryMode::LatestOnly, + FrameDeliveryMode::AllFrames}) + { + QStringList ids; + for (const QString& cameraId : providerCameraIds) + { + if (modeOf(cameraId) == mode) ids.append(cameraId); + } + if (!ids.isEmpty() && !source->setFrameDeliveryMode(ids, mode)) + { + return false; + } + } + } + return true; + }; + if (apply(modes.keys(), [&modes](const QString& cameraId) { return modes.value(cameraId); })) + { + return true; + } + apply(modes.keys(), [this](const QString& cameraId) { return m_frameDelivery.mode(cameraId); }); + return false; + } + void HardwareRuntime::setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) { - for (const auto& [control, providerCameraIds] : runtimeControlsFor(cameraIds)) + for (const auto& [source, providerCameraIds] : frameSourcesFor(cameraIds)) { - control->setFrameDeliveryPaused(providerCameraIds, paused); + source->setFrameDeliveryPaused(providerCameraIds, paused); } } - bool HardwareRuntime::setRecordingFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) + // The cameras deliver every frame before recording takes them, and recording lets go of + // them before the cameras skip frames again + bool HardwareRuntime::setRecordingFrameDeliveryEnabled(const QStringList& cameraIds, bool enabled) { - return setDeliveryEnabled(runtimeControlsFor(cameraIds), - &CameraRuntimeControl::setRecordingFrameDeliveryEnabled, - enabled); + QHash modes; + for (const QString& cameraId : cameraIds) + { + modes.insert(cameraId, enabled ? FrameDeliveryMode::AllFrames + : m_frameDelivery.highRateEnabled(cameraId) + ? FrameDeliveryMode::LatestOnly + : FrameDeliveryMode::PreviewLatest); + } + if (enabled) + { + if (!applyFrameDeliveryModes(modes)) + { + return false; + } + m_frameDelivery.setRecordingEnabled(cameraIds, true); + return true; + } + m_frameDelivery.setRecordingEnabled(cameraIds, false); + applyFrameDeliveryModes(modes); + return true; } - bool HardwareRuntime::setHighRateFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) + bool HardwareRuntime::setHighRateFrameDeliveryEnabled(const QStringList& cameraIds, bool enabled) { - return setDeliveryEnabled(runtimeControlsFor(cameraIds), - &CameraRuntimeControl::setHighRateFrameDeliveryEnabled, - enabled); + QHash modes; + for (const QString& cameraId : cameraIds) + { + modes.insert(cameraId, m_frameDelivery.recordingEnabled(cameraId) + ? FrameDeliveryMode::AllFrames + : enabled ? FrameDeliveryMode::LatestOnly : FrameDeliveryMode::PreviewLatest); + } + if (!applyFrameDeliveryModes(modes)) + { + return false; + } + m_frameDelivery.setHighRateEnabled(cameraIds, enabled); + return true; } bool HardwareRuntime::isProcessingFrameTokenCurrent(const QString& cameraId, quint64 token) { - const HardwareProviderPtr provider = m_registry.providerForDevice(cameraId); - auto* control = dynamic_cast(provider.get()); - return control && control->isProcessingFrameTokenCurrent(cameraId, token); + return m_frameDelivery.isProcessingFrameCurrent(cameraId, token); } void HardwareRuntime::finishProcessingFrame(const QString& cameraId, quint64 token) { - const HardwareProviderPtr provider = m_registry.providerForDevice(cameraId); - if (auto* control = dynamic_cast(provider.get())) - { - control->finishProcessingFrame(cameraId, token); - } + m_frameDelivery.finishProcessingFrame(cameraId, token); } QString HardwareRuntime::defaultXYStage() const { - return uniqueDefaultStage(m_registry, &StageProvider::defaultXYStage, - HardwareDeviceKind::XYStage); + QString stage; + uniqueDefault(m_registry, &StageProvider::defaultXYStage, HardwareDeviceKind::XYStage, &stage); + return stage; } QString HardwareRuntime::defaultZStage() const { - return uniqueDefaultStage(m_registry, &StageProvider::defaultZStage, - HardwareDeviceKind::ZStage); + QString stage; + uniqueDefault(m_registry, &StageProvider::defaultZStage, HardwareDeviceKind::ZStage, &stage); + return stage; } bool HardwareRuntime::getXYPosition(const QString& deviceId, @@ -803,118 +855,185 @@ namespace scopeone::core::internal }); } - // The provider that has the hardware functions of the Micro-Manager core, if there is one - AcquisitionHardware* HardwareRuntime::acquisitionHardware() const + bool HardwareRuntime::autoShutter(bool& enabled, QString* errorMessage) const { - for (const HardwareProviderDescriptor& descriptor : m_registry.providers()) + QString shutter; + ShutterProvider* const provider = defaultShutter(m_registry, &shutter); + enabled = provider && provider->autoShutter(); + if (!provider && errorMessage) { - const HardwareProviderPtr provider = m_registry.provider(descriptor.id); - if (auto* hardware = dynamic_cast(provider.get())) - { - return hardware; - } + *errorMessage = QStringLiteral("No shutter lights the cameras"); } - return nullptr; + return provider != nullptr; } - bool HardwareRuntime::autoShutter(bool& enabled, QString* errorMessage) const + bool HardwareRuntime::setAutoShutter(bool enabled, QString* errorMessage) { - return forward(acquisitionHardware(), "Shutter control is not available", errorMessage, - [&](AcquisitionHardware& hardware) - { - return hardware.autoShutter(enabled, errorMessage); - }); + QString shutter; + return forward(defaultShutter(m_registry, &shutter), "No shutter lights the cameras", errorMessage, + [&](ShutterProvider& provider) { return provider.setAutoShutter(enabled, errorMessage); }); } - bool HardwareRuntime::setAutoShutter(bool enabled, QString* errorMessage) + bool HardwareRuntime::setDefaultShutterOpen(bool open, QString* errorMessage) { - return forward(acquisitionHardware(), "Shutter control is not available", errorMessage, - [&](AcquisitionHardware& hardware) - { - return hardware.setAutoShutter(enabled, errorMessage); - }); + QString shutter; + return forward(defaultShutter(m_registry, &shutter), "No shutter lights the cameras", errorMessage, + [&](ShutterProvider& provider) { return provider.setShutterOpen(shutter, open, errorMessage); }); + } + + bool HardwareRuntime::hasAutofocus() const + { + QString device; + return defaultAutofocus(m_registry, &device) != nullptr; + } + + bool HardwareRuntime::runAutofocus(QString* errorMessage) + { + QString device; + return forward(defaultAutofocus(m_registry, &device), "Autofocus is not available", errorMessage, + [&](AutofocusProvider& provider) { return provider.runAutofocus(device, errorMessage); }); } - bool HardwareRuntime::setCoreShutterOpen(bool open, QString* errorMessage) + long HardwareRuntime::focusSequenceCapacity(const QString& focusDevice) const { - return forward(acquisitionHardware(), "Shutter control is not available", errorMessage, - [&](AcquisitionHardware& hardware) + StageProvider* provider = stageProviderForDevice(focusDevice); + return provider ? provider->stageSequenceCapacity(focusDevice) : 0; + } + + bool HardwareRuntime::startFocusSequence(const QString& focusDevice, + const std::vector& positions, + QString* errorMessage) + { + return forward(stageProviderForDevice(focusDevice), "Stage provider not available", errorMessage, + [&](StageProvider& provider) { - return hardware.setCoreShutterOpen(open, errorMessage); + return provider.startStageSequence(focusDevice, positions, errorMessage); }); } - bool HardwareRuntime::hasAutofocus() const + void HardwareRuntime::stopFocusSequence(const QString& focusDevice) { - AcquisitionHardware* hardware = acquisitionHardware(); - return hardware && hardware->hasAutofocus(); + if (StageProvider* provider = stageProviderForDevice(focusDevice)) + { + provider->stopStageSequence(focusDevice); + } } long HardwareRuntime::propertySequenceCapacity(const QString& device, const QString& property) const { - AcquisitionHardware* hardware = acquisitionHardware(); - return hardware ? hardware->propertySequenceCapacity(device, property) : 0; + DevicePropertyProvider* provider = propertyProviderForDevice(device); + return provider ? provider->propertySequenceCapacity(device, property) : 0; } + // Nothing stays armed when one of the sequences fails bool HardwareRuntime::startPropertySequences(const std::vector& sequences, QString* errorMessage) { - return forward(acquisitionHardware(), "Acquisition hardware not available", errorMessage, - [&](AcquisitionHardware& hardware) - { - return hardware.startPropertySequences(sequences, errorMessage); - }); + for (std::size_t index = 0; index < sequences.size(); ++index) + { + const PropertySequence& sequence = sequences[index]; + DevicePropertyProvider* provider = propertyProviderForDevice(sequence.device); + const QStringList values(sequence.values.begin(), sequence.values.end()); + if (!provider || !provider->startPropertySequence(sequence.device, sequence.property, values, errorMessage)) + { + stopPropertySequences({sequences.begin(), sequences.begin() + static_cast(index)}); + return false; + } + } + return true; } void HardwareRuntime::stopPropertySequences(const std::vector& sequences) { - if (AcquisitionHardware* hardware = acquisitionHardware()) + for (const PropertySequence& sequence : sequences) { - hardware->stopPropertySequences(sequences); + if (DevicePropertyProvider* provider = propertyProviderForDevice(sequence.device)) + { + provider->stopPropertySequence(sequence.device, sequence.property); + } } } - long HardwareRuntime::focusSequenceCapacity(const QString& focusDevice) const + QString HardwareRuntime::microManagerVersion() { - AcquisitionHardware* hardware = acquisitionHardware(); - return hardware ? hardware->focusSequenceCapacity(focusDevice) : 0; + return MicroManagerProvider::coreVersion(); } - bool HardwareRuntime::startFocusSequence(const QString& focusDevice, - const std::vector& positions, - QString* errorMessage) + double HardwareRuntime::pixelSizeUm(const QString& cameraId) const { - return forward(acquisitionHardware(), "Acquisition hardware not available", errorMessage, - [&](AcquisitionHardware& hardware) - { - return hardware.startFocusSequence(focusDevice, positions, errorMessage); - }); + CameraProvider* provider = cameraProviderForDevice(cameraId); + return provider ? provider->pixelSizeUm(cameraId) : 0.0; } - void HardwareRuntime::stopFocusSequence(const QString& focusDevice) + HardwareProviderPtr HardwareRuntime::startScopeOneProvider(const QString& providerId, + const QString& driverPath, + const QJsonObject& options, + QString* errorMessage) { - if (AcquisitionHardware* hardware = acquisitionHardware()) - { - hardware->stopFocusSequence(focusDevice); - } + return createScopeOneProvider(providerId, driverPath, options, errorMessage); } - bool HardwareRuntime::runAutofocus(QString* errorMessage) + QStringList HardwareRuntime::microManagerAdapterSearchPaths() const { - return forward(acquisitionHardware(), "Autofocus is not available", errorMessage, - [&](AcquisitionHardware& hardware) - { - return hardware.runAutofocus(errorMessage); - }); + return m_microManager->adapterSearchPaths(); + } + + void HardwareRuntime::setMicroManagerAdapterSearchPaths(const QStringList& paths) + { + m_microManager->setAdapterSearchPaths(paths); + } + + bool HardwareRuntime::loadMicroManagerConfiguration(const QString& configPath, + MicroManagerProvider::LoadResult& result, + QString& errorMessage) + { + return m_microManager->loadConfiguration(configPath, result, errorMessage); + } + + QStringList HardwareRuntime::startMicroManagerCameras(const QList& cameras) + { + return m_microManager->startCameras(cameras); + } + + QList HardwareRuntime::takeMicroManagerCameras() + { + return m_microManager->takeCameras(); + } + + bool HardwareRuntime::setMicroManagerDevices(const QList& devices) + { + m_microManager->setDevices(devices); + return registerProvider(m_microManager); + } + + void HardwareRuntime::stopMicroManagerPreview() + { + stopPreviewForProvider(m_microManager->descriptor().id); + } + + bool HardwareRuntime::unloadMicroManagerDevices(QString* errorMessage) + { + return m_microManager->unloadDevices(errorMessage); } + void HardwareRuntime::applyMicroManagerShutdownPreset() + { + stopPreviewForProvider(m_microManager->descriptor().id); + m_microManager->applyShutdownPreset(); + } + + // Removes the drivers. Micro-Manager stays registered, as the runtime owns it for its lifetime. void HardwareRuntime::clear() { for (const HardwareProviderDescriptor& descriptor : m_registry.providers()) { - detachCameraProvider(m_registry.provider(descriptor.id)); + if (descriptor.id == m_microManager->descriptor().id) + { + stopCameras(m_registry, descriptor.id); + continue; + } + unregisterProvider(descriptor.id); } - m_registry.clear(); } bool HardwareRuntime::registerProvider(const HardwareProviderPtr& provider) @@ -942,7 +1061,9 @@ namespace scopeone::core::internal && device.kind != HardwareDeviceKind::ZStage) || stageProvider) && (device.kind != HardwareDeviceKind::Shutter || shutterProvider) - && (device.kind != HardwareDeviceKind::State || stateProvider); + && (device.kind != HardwareDeviceKind::State || stateProvider) + && (device.kind != HardwareDeviceKind::AutoFocus + || dynamic_cast(provider.get())); if (!supported) { return false; @@ -952,16 +1073,22 @@ namespace scopeone::core::internal { return false; } - if (cameraProvider) + if (auto* frameSource = dynamic_cast(provider.get())) { - cameraProvider->setFrameSink([this](const ImageFrame& frame) + // The cameras of a newly registered provider start in the live view mode + QStringList cameraIds; + for (const HardwareDeviceDescriptor& device : providerDevices) { - if (m_frameSink) + if (device.kind == HardwareDeviceKind::Camera) { - m_frameSink(frame); + cameraIds.append(device.logicalId); } - emit frameReady(frame); - }); + } + m_frameDelivery.removeCameras(cameraIds); + frameSource->setFrameDelivery(&m_frameDelivery); + } + if (cameraProvider) + { cameraProvider->setPreviewStateSink([this](bool) { const auto publishState = [this]() @@ -976,10 +1103,6 @@ namespace scopeone::core::internal break; } } - if (m_previewStateSink) - { - m_previewStateSink(running); - } emit previewStateChanged(running); }; if (QThread::currentThread() == thread()) publishState(); @@ -991,7 +1114,7 @@ namespace scopeone::core::internal void HardwareRuntime::unregisterProvider(const QString& providerId) { - detachCameraProvider(m_registry.provider(providerId)); + detachCameraProvider(m_registry, providerId); m_registry.unregisterProvider(providerId); } @@ -1001,10 +1124,8 @@ namespace scopeone::core::internal return provider && registerProvider(provider); } - bool HardwareRuntime::stopPreviewForProvider(const QString& providerId) + void HardwareRuntime::stopPreviewForProvider(const QString& providerId) { - const HardwareProviderPtr provider = m_registry.provider(providerId); - auto* cameraProvider = dynamic_cast(provider.get()); - return cameraProvider && cameraProvider->stopPreview(); + stopCameras(m_registry, providerId); } } diff --git a/ScopeOneCore/src/MDAManager.cpp b/ScopeOneCore/src/MDAManager.cpp index 20ad64c..e6333b3 100644 --- a/ScopeOneCore/src/MDAManager.cpp +++ b/ScopeOneCore/src/MDAManager.cpp @@ -1,6 +1,6 @@ #include "internal/MDAManager.h" -#include "scopeone/CameraProvider.h" +#include "internal/HardwareRuntime.h" #include "scopeone/HardwareCapabilities.h" #include @@ -37,25 +37,10 @@ namespace scopeone::core::internal cancelAndWait(); } - // Connects MDA capture to the active camera backend - void MDAManager::setCameraProvider(CameraProvider* cameraProvider) + // Connects MDA capture to the cameras + void MDAManager::setHardwareRuntime(HardwareRuntime* hardwareRuntime) { - m_cameraProvider = cameraProvider; - } - - void MDAManager::setStageProvider(StageProvider* stageProvider) - { - m_stageProvider = stageProvider; - } - - void MDAManager::setConfigurationProvider(ConfigurationProvider* configurationProvider) - { - m_configurationProvider = configurationProvider; - } - - void MDAManager::setAcquisitionHardware(AcquisitionHardware* hardware) - { - m_acquisitionHardware = hardware; + m_hardwareRuntime = hardwareRuntime; } // Starts one immutable acquisition event sequence @@ -209,21 +194,21 @@ namespace scopeone::core::internal } if (usesXY) { - const QString stage = m_stageProvider->defaultXYStage(); + const QString stage = m_hardwareRuntime->defaultXYStage(); state.hasInitialXY = !stage.isEmpty() - && m_stageProvider->getXYPosition(stage, state.initialX, state.initialY); + && m_hardwareRuntime->getXYPosition(stage, state.initialX, state.initialY); } if (usesZ) { - const QString focus = m_stageProvider->defaultZStage(); + const QString focus = m_hardwareRuntime->defaultZStage(); state.hasInitialZ = !focus.isEmpty() - && m_stageProvider->getZPosition(focus, state.initialZ); + && m_hardwareRuntime->getZPosition(focus, state.initialZ); state.zReference = state.initialZ; } if (!channelGroup.isEmpty()) { state.initialChannelGroup = channelGroup; - state.initialChannelPreset = m_configurationProvider->currentConfig(channelGroup); + state.initialChannelPreset = m_hardwareRuntime->currentConfig(channelGroup); } // Like Micro-Manager, only take over the shutter when the core controls it const bool wantsShutter = std::any_of(events.begin(), events.end(), @@ -233,7 +218,7 @@ namespace scopeone::core::internal }); bool autoShutterOn = false; state.holdShutter = wantsShutter - && m_acquisitionHardware->autoShutter(autoShutterOn) && autoShutterOn; + && m_hardwareRuntime->autoShutter(autoShutterOn) && autoShutterOn; } // Opens the shutter at the start of a block of events and keeps it open until the block ends @@ -246,14 +231,14 @@ namespace scopeone::core::internal return true; } QString error; - if (!m_acquisitionHardware->setAutoShutter(false, &error) - || !m_acquisitionHardware->setCoreShutterOpen(true, &error)) + if (!m_hardwareRuntime->setAutoShutter(false, &error) + || !m_hardwareRuntime->setDefaultShutterOpen(true, &error)) { if (errorMessage) { *errorMessage = QStringLiteral("Failed to open the shutter: %1").arg(error); } - m_acquisitionHardware->setAutoShutter(true); + m_hardwareRuntime->setAutoShutter(true); return false; } state.shutterOpen = true; @@ -268,11 +253,11 @@ namespace scopeone::core::internal return; } QString error; - if (!m_acquisitionHardware->setCoreShutterOpen(false, &error)) + if (!m_hardwareRuntime->setDefaultShutterOpen(false, &error)) { qWarning().noquote() << "Failed to close the shutter:" << error; } - m_acquisitionHardware->setAutoShutter(true); + m_hardwareRuntime->setAutoShutter(true); state.shutterOpen = false; } @@ -282,30 +267,30 @@ namespace scopeone::core::internal QString error; if (state.holdShutter && state.shutterOpen) { - m_acquisitionHardware->setCoreShutterOpen(false); - m_acquisitionHardware->setAutoShutter(true); + m_hardwareRuntime->setDefaultShutterOpen(false); + m_hardwareRuntime->setAutoShutter(true); } if (state.hasInitialXY) { - const QString stage = m_stageProvider->defaultXYStage(); + const QString stage = m_hardwareRuntime->defaultXYStage(); if (!stage.isEmpty() - && !m_stageProvider->setXYPosition(stage, state.initialX, state.initialY, &error)) + && !m_hardwareRuntime->setXYPosition(stage, state.initialX, state.initialY, &error)) { qWarning().noquote() << "Failed to restore the XY position:" << error; } } if (state.hasInitialZ) { - const QString focus = m_stageProvider->defaultZStage(); + const QString focus = m_hardwareRuntime->defaultZStage(); if (!focus.isEmpty() - && !m_stageProvider->setZPosition(focus, state.initialZ, &error)) + && !m_hardwareRuntime->setZPosition(focus, state.initialZ, &error)) { qWarning().noquote() << "Failed to restore the Z position:" << error; } } if (!state.initialChannelPreset.isEmpty() && state.initialChannelPreset != state.channelPreset - && !m_configurationProvider->setConfig(state.initialChannelGroup, + && !m_hardwareRuntime->setConfig(state.initialChannelGroup, state.initialChannelPreset, &error)) { @@ -388,7 +373,7 @@ namespace scopeone::core::internal return true; } QString error; - if (!m_configurationProvider->setConfig(event.channelGroup, event.channelPreset, &error)) + if (!m_hardwareRuntime->setConfig(event.channelGroup, event.channelPreset, &error)) { if (errorMessage) { @@ -435,12 +420,12 @@ namespace scopeone::core::internal SequenceState& state, QString* errorMessage) { - if (!m_acquisitionHardware->hasAutofocus()) + if (!m_hardwareRuntime->hasAutofocus()) { if (errorMessage) *errorMessage = QStringLiteral("No autofocus device is available"); return false; } - const QString focus = m_stageProvider->defaultZStage(); + const QString focus = m_hardwareRuntime->defaultZStage(); if (focus.isEmpty()) { if (errorMessage) *errorMessage = QStringLiteral("No focus device configured"); @@ -456,19 +441,19 @@ namespace scopeone::core::internal return false; } } - else if (!m_stageProvider->getZPosition(focus, before, errorMessage)) + else if (!m_hardwareRuntime->getZPosition(focus, before, errorMessage)) { return false; } QString error; - if (!m_acquisitionHardware->runAutofocus(&error)) + if (!m_hardwareRuntime->runAutofocus(&error)) { if (errorMessage) *errorMessage = QStringLiteral("Autofocus failed: %1").arg(error); return false; } double after = 0.0; - if (!m_stageProvider->getZPosition(focus, after, errorMessage)) + if (!m_hardwareRuntime->getZPosition(focus, after, errorMessage)) { return false; } @@ -495,7 +480,7 @@ namespace scopeone::core::internal const AcquisitionEvent& head = events.at(first); if (!head.hardwareSequencing || !head.hasZ || head.autofocusBefore || head.cameraIds.size() != 1 - || !m_cameraProvider->supportsEventSequence(head.cameraIds.first())) + || !m_hardwareRuntime->supportsEventSequence(head.cameraIds.first())) { return 1; } @@ -519,8 +504,8 @@ namespace scopeone::core::internal { return 1; } - const QString focus = m_stageProvider->defaultZStage(); - const long capacity = focus.isEmpty() ? 0 : m_acquisitionHardware->focusSequenceCapacity(focus); + const QString focus = m_hardwareRuntime->defaultZStage(); + const long capacity = focus.isEmpty() ? 0 : m_hardwareRuntime->focusSequenceCapacity(focus); return capacity >= length ? length : 1; } @@ -534,7 +519,7 @@ namespace scopeone::core::internal QString* errorMessage) { const AcquisitionEvent& head = events.at(first); - const QString focus = m_stageProvider->defaultZStage(); + const QString focus = m_hardwareRuntime->defaultZStage(); // Moves everything but the slices into place, and the focus to the first slice if (!setupEvent(head, state, errorMessage)) @@ -555,9 +540,9 @@ namespace scopeone::core::internal events, first, count, state, outputs, [&](QString* error) { - return m_acquisitionHardware->startFocusSequence(focus, positions, error); + return m_hardwareRuntime->startFocusSequence(focus, positions, error); }, - [&]() { m_acquisitionHardware->stopFocusSequence(focus); }, + [&]() { m_hardwareRuntime->stopFocusSequence(focus); }, errorMessage); if (!captured) { @@ -582,7 +567,7 @@ namespace scopeone::core::internal const AcquisitionEvent& head = events.at(first); if (!head.hardwareSequencing || head.channelPreset.isEmpty() || head.autofocusBefore || head.cameraIds.size() != 1 - || !m_cameraProvider->supportsEventSequence(head.cameraIds.first())) + || !m_hardwareRuntime->supportsEventSequence(head.cameraIds.first())) { return 1; } @@ -612,7 +597,7 @@ namespace scopeone::core::internal std::vector> presets; for (int index = 0; index < length; ++index) { - presets.push_back(m_configurationProvider->configSettings( + presets.push_back(m_hardwareRuntime->configSettings( head.channelGroup, events.at(first + index).channelPreset)); if (presets.back().size() != presets.front().size()) { @@ -645,7 +630,7 @@ namespace scopeone::core::internal } // The core and the camera cannot be stepped by triggers if (reference.device == QLatin1String("Core") - || m_acquisitionHardware->propertySequenceCapacity(reference.device, reference.property) < length) + || m_hardwareRuntime->propertySequenceCapacity(reference.device, reference.property) < length) { return 1; } @@ -674,8 +659,8 @@ namespace scopeone::core::internal } const bool captured = captureSequencedBlock( events, first, count, state, outputs, - [&](QString* error) { return m_acquisitionHardware->startPropertySequences(sequences, error); }, - [&]() { m_acquisitionHardware->stopPropertySequences(sequences); }, + [&](QString* error) { return m_hardwareRuntime->startPropertySequences(sequences, error); }, + [&]() { m_hardwareRuntime->stopPropertySequences(sequences); }, errorMessage); // The properties are left on the last channel or have wrapped around, so the next // event applies its preset again @@ -715,7 +700,7 @@ namespace scopeone::core::internal } std::vector frames; const int timeoutMs = static_cast((std::max)(10000.0, head.exposureMs * count * 4.0 + 5000.0)); - const bool captured = m_cameraProvider->captureEventSequence(cameraId, count, frames, timeoutMs) + const bool captured = m_hardwareRuntime->captureEventSequence(cameraId, count, frames, timeoutMs) && static_cast(frames.size()) == count; disarm(); closeShutter(last, state); @@ -777,7 +762,7 @@ namespace scopeone::core::internal { CaptureResult result; result.cameraId = cameraId; - if (!m_cameraProvider->captureEventFrame(cameraId, result.frame, captureTimeoutMs)) + if (!m_hardwareRuntime->captureEventFrame(cameraId, result.frame, captureTimeoutMs)) { result.error = QStringLiteral("Failed to capture frame from camera: %1").arg(cameraId); return result; @@ -813,7 +798,7 @@ namespace scopeone::core::internal { for (const QString& cameraId : cameraIds) { - if (!m_cameraProvider->setExposure(cameraId, exposureMs)) + if (!m_hardwareRuntime->setExposure(cameraId, exposureMs)) { if (errorMessage) { @@ -829,24 +814,24 @@ namespace scopeone::core::internal // Moves the active XY stage to an event position bool MDAManager::moveXY(double x, double y, QString* errorMessage) { - const QString stage = m_stageProvider->defaultXYStage(); + const QString stage = m_hardwareRuntime->defaultXYStage(); if (stage.isEmpty()) { if (errorMessage) *errorMessage = QStringLiteral("No XY stage device configured"); return false; } - return m_stageProvider->setXYPosition(stage, x, y, errorMessage); + return m_hardwareRuntime->setXYPosition(stage, x, y, errorMessage); } // Moves the active focus device to an event position bool MDAManager::moveZ(double z, QString* errorMessage) { - const QString focus = m_stageProvider->defaultZStage(); + const QString focus = m_hardwareRuntime->defaultZStage(); if (focus.isEmpty()) { if (errorMessage) *errorMessage = QStringLiteral("No focus device configured"); return false; } - return m_stageProvider->setZPosition(focus, z, errorMessage); + return m_hardwareRuntime->setZPosition(focus, z, errorMessage); } } // namespace scopeone::core::internal diff --git a/ScopeOneCore/src/MicroManagerCamera.cpp b/ScopeOneCore/src/MicroManagerCamera.cpp new file mode 100644 index 0000000..737164b --- /dev/null +++ b/ScopeOneCore/src/MicroManagerCamera.cpp @@ -0,0 +1,1032 @@ +#include "internal/MicroManagerCamera.h" + +#include "internal/MicroManagerCalls.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace scopeone::core::internal +{ + using micromanager::orElse; + + namespace + { + constexpr int kMinimumPollIntervalMs = 1; + constexpr int kMaximumPollIntervalMs = 50; + + int pollingIntervalFor(double frameIntervalMs) + { + if (!std::isfinite(frameIntervalMs) || frameIntervalMs <= 0.0) + { + return kMinimumPollIntervalMs; + } + return static_cast(std::clamp(frameIntervalMs / 4.0, + static_cast(kMinimumPollIntervalMs), + static_cast(kMaximumPollIntervalMs))); + } + + bool applyProperties(CMMCore& core, + const std::string& label, + const QJsonArray& properties, + QString* errorMessage) + { + for (const QJsonValue& value : properties) + { + const QJsonObject property = value.toObject(); + const QString name = property.value(QStringLiteral("name")).toString().trimmed(); + const QString setting = property.value(QStringLiteral("value")).toString(); + try + { + core.setProperty(label.c_str(), name.toStdString().c_str(), setting.toStdString().c_str()); + } + catch (const CMMError& error) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("Failed to apply property '%1': %2") + .arg(name, QString::fromStdString(error.getMsg())); + } + return false; + } + } + return true; + } + + // The pixel layout of the frames one acquisition delivers + struct FrameLayout + { + QString cameraId; + int width{0}; + int height{0}; + int stride{0}; + int bitsPerSample{0}; + ImagePixelFormat pixelFormat{ImagePixelFormat::Invalid}; + int sourceRoiX{0}; + int sourceRoiY{0}; + int sourceRoiWidth{0}; + int sourceRoiHeight{0}; + double expectedIntervalMs{0.0}; + + qsizetype byteCount() const { return static_cast(stride) * height; } + }; + + // Reads the layout of the images the camera currently takes + bool readFrameLayout(CMMCore& core, const QString& cameraId, FrameLayout& layout) + { + const std::string camera = cameraId.toStdString(); + const unsigned width = core.getImageWidth(); + const unsigned height = core.getImageHeight(); + const unsigned bytesPerPixel = core.getBytesPerPixel(); + const qint64 stride = static_cast(width) * bytesPerPixel; + if ((bytesPerPixel != 1 && bytesPerPixel != 2) + || width == 0 + || height == 0 + || width > static_cast((std::numeric_limits::max)()) + || height > static_cast((std::numeric_limits::max)()) + || stride * height > (std::numeric_limits::max)()) + { + return false; + } + layout.cameraId = cameraId; + layout.width = static_cast(width); + layout.height = static_cast(height); + layout.stride = static_cast(stride); + layout.pixelFormat = bytesPerPixel == 2 ? ImagePixelFormat::Mono16 : ImagePixelFormat::Mono8; + layout.bitsPerSample = ImageFrame::normalizedBitsPerSample( + layout.pixelFormat, + orElse(static_cast(bytesPerPixel) * 8, + [&] { return static_cast(core.getImageBitDepth()); })); + layout.sourceRoiWidth = layout.width; + layout.sourceRoiHeight = layout.height; + try + { + core.getROI(camera.c_str(), layout.sourceRoiX, layout.sourceRoiY, + layout.sourceRoiWidth, layout.sourceRoiHeight); + } + catch (const CMMError&) + { + layout.sourceRoiX = 0; + layout.sourceRoiY = 0; + layout.sourceRoiWidth = layout.width; + layout.sourceRoiHeight = layout.height; + } + return true; + } + + ImageFrame frameWithLayout(const FrameLayout& layout, quint64 frameIndex) + { + ImageFrame frame; + frame.cameraId = layout.cameraId; + frame.width = layout.width; + frame.height = layout.height; + frame.stride = layout.stride; + frame.bitsPerSample = layout.bitsPerSample; + frame.pixelFormat = layout.pixelFormat; + frame.frameIndex = frameIndex; + frame.timestampNs = static_cast(QDateTime::currentMSecsSinceEpoch()) * 1000000ull; + frame.sourceRoiX = layout.sourceRoiX; + frame.sourceRoiY = layout.sourceRoiY; + frame.sourceRoiWidth = layout.sourceRoiWidth; + frame.sourceRoiHeight = layout.sourceRoiHeight; + return frame; + } + } + + QJsonObject MicroManagerCameraSetup::toJson() const + { + return QJsonObject{{QStringLiteral("cameraId"), cameraId}, + {QStringLiteral("adapter"), adapter}, + {QStringLiteral("device"), device}, + {QStringLiteral("preInitProperties"), preInitProperties}, + {QStringLiteral("properties"), properties}, + {QStringLiteral("exposureMs"), exposureMs}}; + } + + MicroManagerCameraSetup MicroManagerCameraSetup::fromJson(const QJsonObject& object) + { + MicroManagerCameraSetup setup; + setup.cameraId = object.value(QStringLiteral("cameraId")).toString(); + setup.adapter = object.value(QStringLiteral("adapter")).toString(); + setup.device = object.value(QStringLiteral("device")).toString(); + setup.preInitProperties = object.value(QStringLiteral("preInitProperties")).toArray(); + setup.properties = object.value(QStringLiteral("properties")).toArray(); + setup.exposureMs = object.value(QStringLiteral("exposureMs")).toDouble(); + return setup; + } + + // Reads the circular buffer of the core on a thread of its own and hands the frames that the + // delivery mode asks for to the frame sink + class MicroManagerCameraPoller final : public QObject + { + public: + explicit MicroManagerCameraPoller(MicroManagerCamera* owner) + : m_owner(owner) + { + } + + void start(const FrameLayout& layout) + { + m_layout = layout; + m_observedIntervalMs = layout.expectedIntervalMs; + m_deliveryTimer.invalidate(); + m_frameIntervalTimer.start(); + if (!m_timer) + { + m_timer = new QTimer(this); + m_timer->setTimerType(Qt::PreciseTimer); + connect(m_timer, &QTimer::timeout, this, [this]() { poll(); }); + } + m_timer->setInterval(pollingIntervalFor(m_observedIntervalMs)); + m_active = true; + if (!m_paused) + { + m_timer->start(); + } + } + + void stop() + { + m_active = false; + if (m_timer) + { + m_timer->stop(); + } + m_frameIntervalTimer.invalidate(); + m_deliveryTimer.invalidate(); + } + + void setPaused(bool paused) + { + m_paused = paused; + if (!m_active || !m_timer) + { + return; + } + if (paused) + { + m_timer->stop(); + return; + } + m_frameIntervalTimer.restart(); + m_timer->start(); + } + + void setMode(FrameDeliveryMode mode) + { + m_mode = mode; + m_deliveryTimer.invalidate(); + } + + private: + void poll() + { + CMMCore& core = *m_owner->m_core; + try + { + long remaining = core.getRemainingImageCount(); + if (remaining <= 0) + { + return; + } + + MicroManagerCamera::FrameSink sink; + std::function filter; + { + QMutexLocker locker(&m_owner->m_sinkMutex); + sink = m_owner->m_frameSink; + filter = m_owner->m_frameFilter; + } + const bool allFrames = m_mode == FrameDeliveryMode::AllFrames; + const bool deliverNewest = + m_mode == FrameDeliveryMode::LatestOnly + ? !filter || filter() + : !m_deliveryTimer.isValid() + || m_deliveryTimer.elapsed() >= kPreviewFrameIntervalMs; + const qsizetype byteCount = m_layout.byteCount(); + int frameCount = 0; + bool delivered = false; + while (remaining-- > 0) + { + const void* pixels = core.popNextImage(); + if (!pixels) + { + break; + } + const quint64 frameIndex = ++m_owner->m_frameIndex; + ++frameCount; + if (!sink || (!allFrames && (remaining > 0 || !deliverNewest))) + { + continue; + } + ImageFrame frame = frameWithLayout(m_layout, frameIndex); + frame.bytes = m_owner->m_lendFrames + ? QByteArray::fromRawData(static_cast(pixels), byteCount) + : QByteArray(static_cast(pixels), byteCount); + sink(frame); + delivered = true; + } + if (delivered && m_mode == FrameDeliveryMode::PreviewLatest) + { + m_deliveryTimer.restart(); + } + updatePollingInterval(frameCount); + } + catch (const CMMError& error) + { + stop(); + m_owner->handleAcquisitionFailure(QString::fromStdString(error.getMsg())); + } + } + + // Follows the observed frame interval, so a slow camera is not read in a busy loop + void updatePollingInterval(int frameCount) + { + if (frameCount <= 0 || !m_frameIntervalTimer.isValid()) + { + return; + } + const double elapsedMs = static_cast(m_frameIntervalTimer.nsecsElapsed()) / 1000000.0; + m_frameIntervalTimer.restart(); + const double measuredIntervalMs = elapsedMs / frameCount; + if (!std::isfinite(measuredIntervalMs) || measuredIntervalMs <= 0.0) + { + return; + } + m_observedIntervalMs = m_observedIntervalMs > 0.0 + ? 0.75 * m_observedIntervalMs + 0.25 * measuredIntervalMs + : measuredIntervalMs; + const int intervalMs = pollingIntervalFor(m_observedIntervalMs); + if (m_timer->interval() != intervalMs) + { + m_timer->setInterval(intervalMs); + } + } + + MicroManagerCamera* const m_owner; + QTimer* m_timer{nullptr}; + FrameLayout m_layout; + FrameDeliveryMode m_mode{FrameDeliveryMode::PreviewLatest}; + QElapsedTimer m_frameIntervalTimer; + QElapsedTimer m_deliveryTimer; + double m_observedIntervalMs{0.0}; + bool m_active{false}; + bool m_paused{false}; + }; + + MicroManagerCamera::MicroManagerCamera(std::shared_ptr core, QString cameraId, bool lendFrames) + : m_core(std::move(core)) + , m_cameraId(std::move(cameraId)) + , m_lendFrames(lendFrames) + { + m_pollThread.setObjectName(QStringLiteral("ScopeOneCamera_%1").arg(m_cameraId)); + m_poller = new MicroManagerCameraPoller(this); + m_poller->moveToThread(&m_pollThread); + QObject::connect(&m_pollThread, &QThread::finished, m_poller, &QObject::deleteLater); + m_pollThread.start(); + } + + MicroManagerCamera::~MicroManagerCamera() + { + if (m_running) + { + stopAcquisition(); + } + m_pollThread.quit(); + m_pollThread.wait(); + } + + std::shared_ptr MicroManagerCamera::load(const MicroManagerCameraSetup& setup, + bool lendFrames, + QString* errorMessage) + { + auto core = std::make_shared(); + const std::string label = setup.cameraId.toStdString(); + try + { + core->loadDevice(label.c_str(), setup.adapter.toStdString().c_str(), setup.device.toStdString().c_str()); + if (!applyProperties(*core, label, setup.preInitProperties, errorMessage)) + { + return {}; + } + core->initializeDevice(label.c_str()); + core->setCameraDevice(label.c_str()); + if (!applyProperties(*core, label, setup.properties, errorMessage)) + { + return {}; + } + // The core holds only this camera, so its circular buffer can take 2 GB. A machine that + // cannot reserve that much keeps the default buffer instead of failing the camera. + micromanager::succeeds(nullptr, [&] { core->setCircularBufferMemoryFootprint(2048); }); + if (setup.exposureMs > 0.0) + { + core->setExposure(setup.exposureMs); + } + core->waitForDevice(label.c_str()); + } + catch (const CMMError& error) + { + if (errorMessage) + { + *errorMessage = QString::fromStdString(error.getMsg()); + } + return {}; + } + return std::make_shared(std::move(core), setup.cameraId, lendFrames); + } + + HardwareProviderDescriptor MicroManagerCamera::descriptor() const + { + return micromanager::descriptor(); + } + + QList MicroManagerCamera::devices() const + { + HardwareDeviceDescriptor device; + device.logicalId = m_cameraId; + device.providerId = QStringLiteral("micro-manager"); + device.providerDeviceId = m_cameraId; + device.name = m_cameraId; + device.kind = HardwareDeviceKind::Camera; + device.state = HardwareDeviceState::Initialized; + device.hardwareId = orElse(QString{}, [&] + { + return QString::fromStdString(m_core->getDeviceName(m_cameraId.toStdString().c_str())); + }); + return {device}; + } + + void MicroManagerCamera::setFrameSink(FrameSink sink) + { + QMutexLocker locker(&m_sinkMutex); + m_frameSink = std::move(sink); + } + + void MicroManagerCamera::setPreviewStateSink(PreviewStateSink sink) + { + QMutexLocker locker(&m_sinkMutex); + m_previewStateSink = std::move(sink); + } + + void MicroManagerCamera::setErrorSink(std::function sink) + { + QMutexLocker locker(&m_sinkMutex); + m_errorSink = std::move(sink); + } + + void MicroManagerCamera::setFrameFilter(std::function filter) + { + QMutexLocker locker(&m_sinkMutex); + m_frameFilter = std::move(filter); + } + + // Runs on the poll thread, so a lent frame of the old mode is no longer in use when it returns + void MicroManagerCamera::setFrameDeliveryMode(FrameDeliveryMode mode) + { + MicroManagerCameraPoller* const poller = m_poller; + QMetaObject::invokeMethod(poller, [poller, mode]() { poller->setMode(mode); }, + Qt::BlockingQueuedConnection); + } + + void MicroManagerCamera::setFrameDeliveryPaused(bool paused) + { + if (m_paused == paused) + { + return; + } + m_paused = paused; + if (!paused && m_suspended.exchange(false) && m_running) + { + try + { + if (!m_core->isSequenceRunning()) + { + m_core->setCameraDevice(m_cameraId.toStdString().c_str()); + m_core->startContinuousSequenceAcquisition(0.0); + } + } + catch (const CMMError& error) + { + qWarning().noquote() << QStringLiteral("Failed to resume the live view of '%1': %2") + .arg(m_cameraId, QString::fromStdString(error.getMsg())); + } + } + MicroManagerCameraPoller* const poller = m_poller; + QMetaObject::invokeMethod(poller, [poller, paused]() { poller->setPaused(paused); }, + Qt::BlockingQueuedConnection); + } + + bool MicroManagerCamera::matches(const QString& cameraId) const + { + return cameraId.trimmed() == m_cameraId; + } + + bool MicroManagerCamera::startPreviewFor(const QString& cameraId) + { + if (!matches(cameraId)) + { + return false; + } + if (m_running) + { + return true; + } + if (!startAcquisition()) + { + return false; + } + setPreviewRunning(true); + return true; + } + + bool MicroManagerCamera::stopPreviewFor(const QString& cameraId) + { + if (!matches(cameraId)) + { + return false; + } + if (!m_running) + { + return true; + } + stopAcquisition(); + setPreviewRunning(false); + return true; + } + + bool MicroManagerCamera::isPreviewRunning(const QString& cameraId) const + { + return matches(cameraId) && m_running; + } + + void MicroManagerCamera::setPreviewRunning(bool running) + { + m_running = running; + PreviewStateSink sink; + { + QMutexLocker locker(&m_sinkMutex); + sink = m_previewStateSink; + } + if (sink) + { + sink(running); + } + } + + bool MicroManagerCamera::startAcquisition() + { + FrameLayout layout; + try + { + const std::string camera = m_cameraId.toStdString(); + m_core->setCameraDevice(camera.c_str()); + if (m_core->isSequenceRunning()) + { + m_core->stopSequenceAcquisition(); + } + if (!readFrameLayout(*m_core, m_cameraId, layout)) + { + qWarning().noquote() << QStringLiteral("Camera '%1' delivers a pixel format other than Mono8 or Mono16") + .arg(m_cameraId); + return false; + } + layout.expectedIntervalMs = orElse(0.0, [&] { return m_core->getExposure(); }); + const double actualIntervalMs = orElse(0.0, [&] + { + if (!m_core->hasProperty(camera.c_str(), MM::g_Keyword_ActualInterval_ms)) + { + return 0.0; + } + return std::strtod(m_core->getProperty(camera.c_str(), MM::g_Keyword_ActualInterval_ms).c_str(), + nullptr); + }); + if (std::isfinite(actualIntervalMs) && actualIntervalMs > layout.expectedIntervalMs) + { + layout.expectedIntervalMs = actualIntervalMs; + } + m_suspended = false; + m_core->startContinuousSequenceAcquisition(0.0); + } + catch (const CMMError& error) + { + qWarning().noquote() << QStringLiteral("Failed to start the live view of '%1': %2") + .arg(m_cameraId, QString::fromStdString(error.getMsg())); + return false; + } + MicroManagerCameraPoller* const poller = m_poller; + QMetaObject::invokeMethod(poller, [poller, layout]() { poller->start(layout); }, + Qt::BlockingQueuedConnection); + return true; + } + + void MicroManagerCamera::stopAcquisition() + { + MicroManagerCameraPoller* const poller = m_poller; + QMetaObject::invokeMethod(poller, [poller]() { poller->stop(); }, Qt::BlockingQueuedConnection); + m_suspended = false; + try + { + if (m_core->isSequenceRunning()) + { + m_core->stopSequenceAcquisition(); + } + } + catch (const CMMError&) + { + } + } + + // Called on the poll thread, which has already stopped polling + void MicroManagerCamera::handleAcquisitionFailure(const QString& error) + { + const QString message = QStringLiteral("The live view of '%1' failed: %2").arg(m_cameraId, error); + qWarning().noquote() << message; + try + { + if (m_core->isSequenceRunning()) + { + m_core->stopSequenceAcquisition(); + } + } + catch (const CMMError&) + { + } + setPreviewRunning(false); + std::function sink; + { + QMutexLocker locker(&m_sinkMutex); + sink = m_errorSink; + } + if (sink) + { + sink(message); + } + } + + // A running live acquisition holds images taken before the settings of an event were applied, + // and the camera cannot be selected while it runs + void MicroManagerCamera::suspendAcquisition() + { + if (m_core->isSequenceRunning()) + { + m_core->stopSequenceAcquisition(); + m_suspended = true; + } + m_core->setCameraDevice(m_cameraId.toStdString().c_str()); + } + + bool MicroManagerCamera::makeFrame(const void* pixels, ImageFrame& frame) + { + FrameLayout layout; + if (!pixels || !readFrameLayout(*m_core, m_cameraId, layout)) + { + return false; + } + frame = frameWithLayout(layout, ++m_frameIndex); + frame.bytes = QByteArray(static_cast(pixels), layout.byteCount()); + return frame.isValid(); + } + + bool MicroManagerCamera::captureEventFrame(const QString& cameraId, ImageFrame& frame, int) + { + if (!matches(cameraId)) + { + return false; + } + try + { + suspendAcquisition(); + m_core->snapImage(); + return makeFrame(m_core->getImage(), frame); + } + catch (const CMMError& error) + { + qWarning().noquote() << QStringLiteral("Failed to capture an event frame from '%1': %2") + .arg(m_cameraId, QString::fromStdString(error.getMsg())); + return false; + } + } + + bool MicroManagerCamera::supportsEventSequence(const QString& cameraId) const + { + return matches(cameraId); + } + + // Takes a series of images in one sequence acquisition of the camera. The images come from the + // circular buffer of the core, so the camera times them itself and can trigger a stage that + // steps through a sequence of positions. + bool MicroManagerCamera::captureEventSequence(const QString& cameraId, + int count, + std::vector& frames, + int timeoutMs) + { + frames.clear(); + if (!matches(cameraId) || count < 1) + { + return false; + } + const std::string camera = m_cameraId.toStdString(); + bool started = false; + const auto stop = [&]() + { + try + { + if (started && m_core->isSequenceRunning(camera.c_str())) + { + m_core->stopSequenceAcquisition(camera.c_str()); + } + } + catch (const CMMError&) + { + } + }; + const auto popFrame = [&]() + { + ImageFrame frame; + if (!makeFrame(m_core->popNextImage(), frame)) + { + return false; + } + frames.push_back(std::move(frame)); + return true; + }; + try + { + suspendAcquisition(); + m_core->initializeCircularBuffer(); + m_core->clearCircularBuffer(); + m_core->startSequenceAcquisition(count, 0.0, true); + started = true; + + QElapsedTimer timer; + timer.start(); + frames.reserve(static_cast(count)); + while (static_cast(frames.size()) < count) + { + if (m_core->getRemainingImageCount() > 0) + { + if (!popFrame()) + { + stop(); + return false; + } + continue; + } + if (!m_core->isSequenceRunning(camera.c_str())) + { + break; + } + if (timer.elapsed() > timeoutMs) + { + qWarning().noquote() << QStringLiteral("Timed out waiting for the image sequence of '%1'") + .arg(m_cameraId); + stop(); + return false; + } + QThread::msleep(1); + } + // The last images may arrive between the last check and the end of the sequence + while (static_cast(frames.size()) < count && m_core->getRemainingImageCount() > 0) + { + if (!popFrame()) + { + stop(); + return false; + } + } + stop(); + return static_cast(frames.size()) == count; + } + catch (const CMMError& error) + { + qWarning().noquote() << QStringLiteral("Failed to capture an image sequence from '%1': %2") + .arg(m_cameraId, QString::fromStdString(error.getMsg())); + stop(); + return false; + } + } + + bool MicroManagerCamera::getExposure(const QString& cameraId, double& exposureMs) const + { + if (!matches(cameraId)) + { + return false; + } + try + { + exposureMs = m_core->getExposure(m_cameraId.toStdString().c_str()); + return exposureMs > 0.0; + } + catch (const CMMError&) + { + return false; + } + } + + // A camera setting changes the images of a running acquisition, so the acquisition is stopped + // around it and started again + template + static bool applyStopped(MicroManagerCamera& camera, Change&& change) + { + const bool wasRunning = camera.isPreviewRunning(camera.cameraId()); + if (wasRunning && !camera.stopPreviewFor(camera.cameraId())) + { + return false; + } + const bool ok = change(); + if (wasRunning && !camera.startPreviewFor(camera.cameraId())) + { + return false; + } + return ok; + } + + bool MicroManagerCamera::setExposure(const QString& cameraId, double exposureMs) + { + if (!matches(cameraId) || exposureMs <= 0.0) + { + return false; + } + return applyStopped(*this, [&]() + { + try + { + const std::string camera = m_cameraId.toStdString(); + m_core->setExposure(camera.c_str(), exposureMs); + m_core->waitForDevice(camera.c_str()); + return true; + } + catch (const CMMError&) + { + return false; + } + }); + } + + bool MicroManagerCamera::setROI(const QString& cameraId, int x, int y, int width, int height) + { + if (!matches(cameraId)) + { + return false; + } + return applyStopped(*this, [&]() + { + try + { + const std::string camera = m_cameraId.toStdString(); + m_core->setROI(camera.c_str(), x, y, width, height); + m_core->waitForDevice(camera.c_str()); + return true; + } + catch (const CMMError& error) + { + qWarning().noquote() << QStringLiteral("Failed to set the ROI of '%1': %2") + .arg(m_cameraId, QString::fromStdString(error.getMsg())); + return false; + } + }); + } + + bool MicroManagerCamera::clearROI(const QString& cameraId) + { + if (!matches(cameraId)) + { + return false; + } + return applyStopped(*this, [&]() + { + try + { + const std::string camera = m_cameraId.toStdString(); + m_core->setCameraDevice(camera.c_str()); + m_core->clearROI(); + m_core->waitForDevice(camera.c_str()); + return true; + } + catch (const CMMError& error) + { + qWarning().noquote() << QStringLiteral("Failed to clear the ROI of '%1': %2") + .arg(m_cameraId, QString::fromStdString(error.getMsg())); + return false; + } + }); + } + + bool MicroManagerCamera::getROI(const QString& cameraId, int& x, int& y, int& width, int& height) + { + if (!matches(cameraId)) + { + return false; + } + try + { + m_core->getROI(m_cameraId.toStdString().c_str(), x, y, width, height); + return true; + } + catch (const CMMError&) + { + return false; + } + } + + QStringList MicroManagerCamera::listProperties(const QString& deviceId) + { + if (!matches(deviceId)) + { + return {}; + } + return orElse(QStringList{}, [&] + { + QStringList names; + for (const std::string& name : m_core->getDevicePropertyNames(m_cameraId.toStdString().c_str())) + { + names.append(QString::fromStdString(name)); + } + return names; + }); + } + + QString MicroManagerCamera::getProperty(const QString& deviceId, const QString& name, bool fromCache) + { + if (!matches(deviceId)) + { + return {}; + } + return orElse(QString{}, [&] + { + const std::string camera = m_cameraId.toStdString(); + const std::string property = name.trimmed().toStdString(); + return QString::fromStdString(fromCache + ? m_core->getPropertyFromCache(camera.c_str(), property.c_str()) + : m_core->getProperty(camera.c_str(), property.c_str())); + }); + } + + bool MicroManagerCamera::setProperty(const QString& deviceId, + const QString& name, + const QString& value, + QString* errorMessage) + { + if (!matches(deviceId)) + { + return false; + } + return applyStopped(*this, [&]() + { + try + { + const std::string camera = m_cameraId.toStdString(); + m_core->setProperty(camera.c_str(), name.trimmed().toStdString().c_str(), value.toStdString().c_str()); + m_core->waitForDevice(camera.c_str()); + return true; + } + catch (const CMMError& error) + { + if (errorMessage) + { + *errorMessage = QString::fromStdString(error.getMsg()); + } + return false; + } + }); + } + + long MicroManagerCamera::propertySequenceCapacity(const QString& deviceId, const QString& name) + { + return matches(deviceId) ? micromanager::propertySequenceCapacity(*m_core, m_cameraId, name) : 0; + } + + bool MicroManagerCamera::startPropertySequence(const QString& deviceId, + const QString& name, + const QStringList& values, + QString* errorMessage) + { + return matches(deviceId) + && micromanager::startPropertySequence(*m_core, m_cameraId, name, values, errorMessage); + } + + void MicroManagerCamera::stopPropertySequence(const QString& deviceId, const QString& name) + { + if (matches(deviceId)) + { + micromanager::stopPropertySequence(*m_core, m_cameraId, name); + } + } + + QString MicroManagerCamera::getPropertyType(const QString& deviceId, const QString& name) + { + if (!matches(deviceId)) + { + return QStringLiteral("Unknown"); + } + return micromanager::propertyType(*m_core, m_cameraId, name); + } + + bool MicroManagerCamera::isPropertyReadOnly(const QString& deviceId, const QString& name) + { + return !matches(deviceId) || orElse(true, [&] + { + return m_core->isPropertyReadOnly(m_cameraId.toStdString().c_str(), name.trimmed().toStdString().c_str()); + }); + } + + bool MicroManagerCamera::isPropertyPreInit(const QString& deviceId, const QString& name) + { + return matches(deviceId) && orElse(false, [&] + { + return m_core->isPropertyPreInit(m_cameraId.toStdString().c_str(), name.trimmed().toStdString().c_str()); + }); + } + + QStringList MicroManagerCamera::getAllowedPropertyValues(const QString& deviceId, const QString& name) + { + if (!matches(deviceId)) + { + return {}; + } + return orElse(QStringList{}, [&] + { + QStringList values; + for (const std::string& value : m_core->getAllowedPropertyValues( + m_cameraId.toStdString().c_str(), name.trimmed().toStdString().c_str())) + { + values.append(QString::fromStdString(value)); + } + return values; + }); + } + + bool MicroManagerCamera::hasPropertyLimits(const QString& deviceId, const QString& name) + { + return matches(deviceId) && orElse(false, [&] + { + return m_core->hasPropertyLimits(m_cameraId.toStdString().c_str(), name.trimmed().toStdString().c_str()); + }); + } + + double MicroManagerCamera::getPropertyLowerLimit(const QString& deviceId, const QString& name) + { + return !matches(deviceId) ? 0.0 : orElse(0.0, [&] + { + return m_core->getPropertyLowerLimit(m_cameraId.toStdString().c_str(), name.trimmed().toStdString().c_str()); + }); + } + + double MicroManagerCamera::getPropertyUpperLimit(const QString& deviceId, const QString& name) + { + return !matches(deviceId) ? 0.0 : orElse(0.0, [&] + { + return m_core->getPropertyUpperLimit(m_cameraId.toStdString().c_str(), name.trimmed().toStdString().c_str()); + }); + } +} diff --git a/ScopeOneCore/src/MMCoreManager.cpp b/ScopeOneCore/src/MicroManagerConfiguration.cpp similarity index 82% rename from ScopeOneCore/src/MMCoreManager.cpp rename to ScopeOneCore/src/MicroManagerConfiguration.cpp index 59c637f..3ccba0e 100644 --- a/ScopeOneCore/src/MMCoreManager.cpp +++ b/ScopeOneCore/src/MicroManagerConfiguration.cpp @@ -1,10 +1,13 @@ -#include "internal/MMCoreManager.h" -#include "internal/CameraManager.h" +// The loading of a Micro-Manager configuration by MicroManagerProvider +#include "internal/MicroManagerProvider.h" + +#include "MMCore.h" #include #include #include #include #include +#include #include #include #include @@ -69,8 +72,8 @@ namespace scopeone::core::internal struct DevicePropertyState { - QStringList preInitProperties; - QStringList properties; + QJsonArray preInitProperties; + QJsonArray properties; }; struct ConfigProperty @@ -147,13 +150,9 @@ namespace scopeone::core::internal core.setDeviceAdapterSearchPaths(searchPaths); } - // Encodes one property replay entry as compact JSON - QString encodePropertyPayload(const QString& name, const QString& value) + QJsonObject propertySetting(const QString& name, const QString& value) { - QJsonObject property; - property.insert(QStringLiteral("name"), name); - property.insert(QStringLiteral("value"), value); - return QString::fromUtf8(QJsonDocument(property).toJson(QJsonDocument::Compact)); + return QJsonObject{{QStringLiteral("name"), name}, {QStringLiteral("value"), value}}; } // Splits one configuration line while preserving commas in the final field @@ -279,7 +278,7 @@ namespace scopeone::core::internal continue; } - const QString encodedProperty = encodePropertyPayload(propertyName, propertyValue); + const QJsonObject encodedProperty = propertySetting(propertyName, propertyValue); if (preInit) { state.preInitProperties.append(encodedProperty); @@ -299,7 +298,6 @@ namespace scopeone::core::internal return state; } - } // namespace // Loads one config file into MMCore bool loadConfigurationFile(CMMCore& core, @@ -356,12 +354,12 @@ namespace scopeone::core::internal } // Reads camera metadata before backend startup - QList loadedCameraInfos( + QList loadedCameraInfos( CMMCore& core, const QStringList& loadedDevices, const ConfigPropertyReplay& replay) { - QList cameras; + QList cameras; cameras.reserve(loadedDevices.size()); for (const QString& deviceName : loadedDevices) { @@ -373,8 +371,8 @@ namespace scopeone::core::internal continue; } - MMCoreManager::CameraLoadInfo info; - info.label = deviceName; + MicroManagerCameraSetup info; + info.cameraId = deviceName; try { info.adapter = QString::fromStdString(core.getDeviceLibrary(deviceName.toStdString().c_str())); @@ -409,7 +407,7 @@ namespace scopeone::core::internal info.properties = propertyState.properties; for (const ConfigProperty& property : replay.startupProperties.value(deviceName)) { - info.properties.append(encodePropertyPayload(property.name, property.value)); + info.properties.append(propertySetting(property.name, property.value)); } cameras.append(std::move(info)); @@ -420,36 +418,34 @@ namespace scopeone::core::internal } return cameras; } + } // namespace - // Creates the MMCore manager and backing core instance - MMCoreManager::MMCoreManager(QObject* parent) - : QObject(parent) - , m_mmcore(std::make_shared()) - { - } - - // Loads and initializes devices without creating Qt camera backends - bool MMCoreManager::loadConfigurationDevices(const QString& configPath, - LoadConfigResult& result, + // Loads and initializes the devices, but does not start the cameras. The devices of an + // earlier configuration are unloaded first. + bool MicroManagerProvider::loadConfiguration(const QString& configPath, + LoadResult& result, QString& errorMessage) { - result = LoadConfigResult{}; - if (!loadConfigurationFile( - *m_mmcore, configPath, m_additionalDeviceAdapterSearchPaths, &errorMessage)) + result = LoadResult{}; + if (!unloadDevices(&errorMessage)) + { + return false; + } + if (!loadConfigurationFile(*m_core, configPath, m_adapterSearchPaths, &errorMessage)) { return false; } QString listError; - const QStringList loadedDevices = loadedDeviceLabels(*m_mmcore, &listError); + const QStringList loadedDevices = loadedDeviceLabels(*m_core, &listError); if (!listError.isEmpty()) { errorMessage = QString("Failed to query loaded devices: %1").arg(listError); return false; } const ConfigPropertyReplay propertyReplay = configPropertyReplay(configPath); - const QList cameraInfos = - loadedCameraInfos(*m_mmcore, loadedDevices, propertyReplay); + const QList cameraInfos = + loadedCameraInfos(*m_core, loadedDevices, propertyReplay); const bool useSingleCamera = cameraInfos.size() == 1; int successCount = 0; @@ -460,7 +456,7 @@ namespace scopeone::core::internal try { const std::string label = deviceName.toStdString(); - const MM::DeviceType deviceType = m_mmcore->getDeviceType(label.c_str()); + const MM::DeviceType deviceType = m_core->getDeviceType(label.c_str()); if (deviceType == MM::CoreDevice) { continue; @@ -479,7 +475,7 @@ namespace scopeone::core::internal try { descriptor.hardwareId = QString::fromStdString( - m_mmcore->getDeviceName(label.c_str())); + m_core->getDeviceName(label.c_str())); } catch (const CMMError&) { @@ -492,12 +488,12 @@ namespace scopeone::core::internal continue; } - const DeviceInitializationState state = m_mmcore->getDeviceInitializationState(label.c_str()); + const DeviceInitializationState state = m_core->getDeviceInitializationState(label.c_str()); if (state != InitializedSuccessfully) { try { - m_mmcore->initializeDevice(label.c_str()); + m_core->initializeDevice(label.c_str()); descriptor.state = scopeone::core::HardwareDeviceState::Initialized; successCount++; } @@ -534,12 +530,12 @@ namespace scopeone::core::internal { try { - const std::vector current = m_mmcore->getLoadedDevices(); + const std::vector current = m_core->getLoadedDevices(); const bool stillLoaded = - std::find(current.begin(), current.end(), ci.label.toStdString()) != current.end(); + std::find(current.begin(), current.end(), ci.cameraId.toStdString()) != current.end(); if (stillLoaded) { - m_mmcore->unloadDevice(ci.label.toStdString().c_str()); + m_core->unloadDevice(ci.cameraId.toStdString().c_str()); } } catch (const CMMError&) @@ -557,49 +553,41 @@ namespace scopeone::core::internal return true; } - // Creates camera backends on the owning Qt thread - bool MMCoreManager::startCameraBackends(CameraManager& cameraManager, - LoadConfigResult& result) + bool MicroManagerProvider::unloadDevices(QString* errorMessage) { - result.cameraIds.clear(); - for (const CameraLoadInfo& camera : result.cameras) + try { - const bool started = result.useSingleCamera - ? cameraManager.configureNativeCamera(m_mmcore, camera.label, camera.exposureMs) - : cameraManager.addDriverHostCamera(camera.label, - camera.adapter, - camera.device, - camera.preInitProperties, - camera.properties, - camera.exposureMs); - if (!started) + m_core->unloadAllDevices(); + return true; + } + catch (const CMMError& error) + { + if (errorMessage) { - for (auto& device : result.devices) - { - if (device.logicalId == camera.label) - { - device.state = scopeone::core::HardwareDeviceState::Faulted; - break; - } - } - ++result.failCount; - result.failedDevices.append(camera.label); - result.failedDevices.removeDuplicates(); - result.cameraIds.clear(); - return false; + *errorMessage = QString::fromStdString(error.getMsg()); } + return false; + } + } - for (auto& device : result.devices) + // Applies the System/Shutdown preset of the configuration, which puts the hardware in a + // safe state before the devices are released + void MicroManagerProvider::applyShutdownPreset() + { + try + { + if (!m_core->isConfigDefined(MM::g_CFGGroup_System, MM::g_CFGGroup_System_Shutdown)) { - if (device.logicalId == camera.label) - { - device.state = scopeone::core::HardwareDeviceState::Initialized; - break; - } + return; } - result.cameraIds.append(camera.label); } - return true; + catch (const CMMError& error) + { + qWarning().noquote() << QStringLiteral("Failed to inspect System/Shutdown preset: %1") + .arg(QString::fromStdString(error.getMsg())); + return; + } + setConfig(MM::g_CFGGroup_System, MM::g_CFGGroup_System_Shutdown, nullptr); } } // namespace scopeone::core::internal diff --git a/ScopeOneCore/src/MicroManagerProvider.cpp b/ScopeOneCore/src/MicroManagerProvider.cpp index f24e7c1..9d308c8 100644 --- a/ScopeOneCore/src/MicroManagerProvider.cpp +++ b/ScopeOneCore/src/MicroManagerProvider.cpp @@ -1,7 +1,13 @@ #include "internal/MicroManagerProvider.h" -#include "MMCore.h" +#include "internal/DriverHostClient.h" +#include "internal/MicroManagerCalls.h" + +#include +#include +#include +#include #include #include @@ -11,152 +17,257 @@ namespace scopeone::core::internal { - namespace + using namespace micromanager; + + MicroManagerProvider::MicroManagerProvider() + : m_core(std::make_shared()) + { + } + + MicroManagerProvider::~MicroManagerProvider() = default; + + QString MicroManagerProvider::coreVersion() + { + return QStringLiteral("%1.%2.%3") + .arg(CMMCore::getMMCoreVersionMajor()) + .arg(CMMCore::getMMCoreVersionMinor()) + .arg(CMMCore::getMMCoreVersionPatch()); + } + + HardwareProviderDescriptor MicroManagerProvider::descriptor() const + { + return micromanager::descriptor(); + } + + QList MicroManagerProvider::devices() const + { + return m_devices; + } + + void MicroManagerProvider::setDevices(const QList& devices) { - QStringList toQStringList(const std::vector& values) + m_devices = devices; + } + + QStringList MicroManagerProvider::startCameras(const QList& cameras) + { + if (cameras.size() == 1) { - QStringList result; - result.reserve(static_cast(values.size())); - for (const std::string& value : values) - { - result.append(QString::fromStdString(value)); - } - return result; + return startLocalCamera(cameras.first().cameraId) ? QStringList{} : QStringList{cameras.first().cameraId}; } - - // MMCore takes device and property names as trimmed narrow strings - std::string name8(const QString& name) + // Each DriverHost loads its camera on its own, which can take seconds, so they start + // together + QList> hosts; + for (const MicroManagerCameraSetup& setup : cameras) { - return name.trimmed().toStdString(); + hosts.append(QtConcurrent::run([setup]() + { + QString error; + const QStringList arguments{ + QStringLiteral("--micromanagerCamera"), + QString::fromUtf8(QJsonDocument(setup.toJson()).toJson(QJsonDocument::Compact))}; + HardwareProviderPtr host = startDriverHost(micromanager::descriptor().id, arguments, &error); + if (!host) + { + qWarning().noquote() << QStringLiteral("Failed to start the DriverHost of camera '%1': %2") + .arg(setup.cameraId, error); + } + return host; + })); } - - // Runs an MMCore query and returns the fallback when the core reports an error - template - T orElse(T fallback, Call&& call) + QStringList failed; + for (qsizetype index = 0; index < cameras.size(); ++index) { - try + const HardwareProviderPtr host = hosts[index].result(); + if (!host) { - return call(); + failed.append(cameras[index].cameraId); + continue; } - catch (const CMMError&) + Camera entry; + entry.provider = host; + entry.camera = dynamic_cast(host.get()); + entry.frameSource = dynamic_cast(host.get()); + entry.camera->setPreviewStateSink(m_previewStateSink); + entry.frameSource->setFrameDelivery(m_delivery); { - return fallback; + QWriteLocker locker(&m_camerasLock); + m_cameras.insert(cameras[index].cameraId, entry); } + readBinning(cameras[index].cameraId); } + return failed; + } - // Runs an MMCore command and reports a core error through errorMessage - template - bool succeeds(QString* errorMessage, Call&& call) + bool MicroManagerProvider::startLocalCamera(const QString& cameraId) + { + auto camera = std::make_shared(m_core, cameraId, false); + auto lastFrameIndex = std::make_shared>(0); + camera->setFrameSink([this, lastFrameIndex](const ImageFrame& frame) { - if (errorMessage) - { - errorMessage->clear(); - } - try + CameraFrameDelivery* const delivery = m_delivery; + if (!delivery) { - call(); - return true; + return; } - catch (const CMMError& error) + // The camera skips frames outside AllFrames mode, and their indices tell how many + const quint64 previous = lastFrameIndex->exchange(frame.frameIndex); + const quint64 acquired = previous == 0 || frame.frameIndex <= previous ? 1 : frame.frameIndex - previous; + delivery->deliverFrames({frame}, acquired); + }); + camera->setFrameFilter([this, cameraId]() + { + CameraFrameDelivery* const delivery = m_delivery; + return !delivery || delivery->wantsLatestFrame(cameraId); + }); + camera->setErrorSink([this](const QString& error) + { + if (CameraFrameDelivery* const delivery = m_delivery) { - if (errorMessage) - { - *errorMessage = QString::fromStdString(error.getMsg()); - } - return false; + delivery->fail(error); } - } + }); + camera->setPreviewStateSink(m_previewStateSink); + Camera entry; + entry.provider = camera; + entry.camera = camera.get(); + entry.local = camera; + entry.lastFrameIndex = lastFrameIndex; + QWriteLocker locker(&m_camerasLock); + m_cameras.insert(cameraId, entry); + return true; } - MicroManagerProvider::MicroManagerProvider(std::shared_ptr core, - CameraProvider* cameraProvider, - CameraRuntimeControl* cameraRuntimeControl) - : m_core(std::move(core)) - , m_cameraProvider(cameraProvider) - , m_cameraRuntimeControl(cameraRuntimeControl) + QList MicroManagerProvider::takeCameras() { + QMap taken; + { + QWriteLocker locker(&m_camerasLock); + taken.swap(m_cameras); + m_binnings.clear(); + } + QList providers; + for (const Camera& entry : std::as_const(taken)) + { + entry.camera->setPreviewStateSink({}); + providers.append(entry.provider); + } + return providers; } - HardwareProviderDescriptor MicroManagerProvider::descriptor() const + MicroManagerProvider::CameraRef MicroManagerProvider::camera(const QString& cameraId) const { - return { - QStringLiteral("micro-manager"), - QStringLiteral("Micro-Manager"), - QStringLiteral("MMCore") - }; + const Camera entry = cameraEntry(cameraId); + return {entry.provider, entry.camera}; } - QList MicroManagerProvider::devices() const + MicroManagerProvider::Camera MicroManagerProvider::cameraEntry(const QString& cameraId) const { - return m_devices; + QReadLocker locker(&m_camerasLock); + return m_cameras.value(cameraId.trimmed()); } - void MicroManagerProvider::setDevices(const QList& devices) + QList MicroManagerProvider::cameras() const { - m_devices = devices; + QReadLocker locker(&m_camerasLock); + return m_cameras.values(); } - bool MicroManagerProvider::isCamera(const QString& deviceId) const + void MicroManagerProvider::setFrameDelivery(CameraFrameDelivery* delivery) { - const QString normalizedId = deviceId.trimmed(); - return std::any_of(m_devices.cbegin(), m_devices.cend(), - [&normalizedId](const HardwareDeviceDescriptor& device) - { - return device.logicalId == normalizedId - && device.kind == HardwareDeviceKind::Camera; - }); + m_delivery = delivery; + for (const Camera& entry : cameras()) + { + if (entry.frameSource) + { + entry.frameSource->setFrameDelivery(delivery); + } + } } - void MicroManagerProvider::setFrameSink(FrameSink sink) + bool MicroManagerProvider::setFrameDeliveryMode(const QStringList& cameraIds, FrameDeliveryMode mode) { - m_cameraProvider->setFrameSink(std::move(sink)); + for (const QString& cameraId : cameraIds) + { + const Camera entry = cameraEntry(cameraId); + if (entry.local) + { + entry.local->setFrameDeliveryMode(mode); + entry.lastFrameIndex->store(0); + } + else if (entry.frameSource && !entry.frameSource->setFrameDeliveryMode({cameraId}, mode)) + { + return false; + } + } + return true; } - void MicroManagerProvider::setPreviewStateSink(PreviewStateSink sink) + void MicroManagerProvider::setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) { - m_cameraProvider->setPreviewStateSink(std::move(sink)); + for (const QString& cameraId : cameraIds) + { + const Camera entry = cameraEntry(cameraId); + if (entry.local) + { + entry.local->setFrameDeliveryPaused(paused); + } + else if (entry.frameSource) + { + entry.frameSource->setFrameDeliveryPaused({cameraId}, paused); + } + } } - bool MicroManagerProvider::startPreview() + // The frames go to the CameraFrameDelivery instead + void MicroManagerProvider::setFrameSink(FrameSink) { - return m_cameraProvider->startPreview(); } - bool MicroManagerProvider::stopPreview() + void MicroManagerProvider::setPreviewStateSink(PreviewStateSink sink) { - return m_cameraProvider->stopPreview(); + m_previewStateSink = std::move(sink); + for (const Camera& entry : cameras()) + { + entry.camera->setPreviewStateSink(m_previewStateSink); + } } bool MicroManagerProvider::startPreviewFor(const QString& cameraId) { - return m_cameraProvider->startPreviewFor(cameraId); + const CameraRef provider = camera(cameraId); + return provider && provider->startPreviewFor(cameraId.trimmed()); } bool MicroManagerProvider::stopPreviewFor(const QString& cameraId) { - return m_cameraProvider->stopPreviewFor(cameraId); + const CameraRef provider = camera(cameraId); + return provider && provider->stopPreviewFor(cameraId.trimmed()); } bool MicroManagerProvider::isPreviewRunning(const QString& cameraId) const { - return m_cameraProvider->isPreviewRunning(cameraId); + const CameraRef provider = camera(cameraId); + return provider && provider->isPreviewRunning(cameraId.trimmed()); } - bool MicroManagerProvider::getExposure(const QString& cameraIdOrAll, double& exposureMs) const + bool MicroManagerProvider::getExposure(const QString& cameraId, double& exposureMs) const { - return m_cameraProvider->getExposure(cameraIdOrAll, exposureMs); + const CameraRef provider = camera(cameraId); + return provider && provider->getExposure(cameraId.trimmed(), exposureMs); } - bool MicroManagerProvider::setExposure(const QString& cameraIdOrAll, double exposureMs) + bool MicroManagerProvider::setExposure(const QString& cameraId, double exposureMs) { - return m_cameraProvider->setExposure(cameraIdOrAll, exposureMs); + const CameraRef provider = camera(cameraId); + return provider && provider->setExposure(cameraId.trimmed(), exposureMs); } QStringList MicroManagerProvider::listProperties(const QString& cameraId) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->listProperties(cameraId); + return provider->listProperties(cameraId.trimmed()); } return orElse(QStringList{}, [&] { @@ -168,9 +279,9 @@ namespace scopeone::core::internal const QString& name, bool fromCache) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->getProperty(cameraId, name, fromCache); + return provider->getProperty(cameraId.trimmed(), name, fromCache); } return orElse(QString{}, [&] { @@ -188,9 +299,14 @@ namespace scopeone::core::internal const QString& value, QString* errorMessage) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->setProperty(cameraId, name, value, errorMessage); + const bool ok = provider->setProperty(cameraId.trimmed(), name, value, errorMessage); + if (ok && name.trimmed() == QLatin1String(MM::g_Keyword_Binning)) + { + readBinning(cameraId.trimmed()); + } + return ok; } return succeeds(errorMessage, [&] { @@ -203,27 +319,18 @@ namespace scopeone::core::internal QString MicroManagerProvider::getPropertyType(const QString& cameraId, const QString& name) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->getPropertyType(cameraId, name); + return provider->getPropertyType(cameraId.trimmed(), name); } - return orElse(QStringLiteral("Unknown"), [&] - { - switch (m_core->getPropertyType(name8(cameraId).c_str(), name8(name).c_str())) - { - case MM::String: return QStringLiteral("String"); - case MM::Float: return QStringLiteral("Float"); - case MM::Integer: return QStringLiteral("Integer"); - default: return QStringLiteral("Unknown"); - } - }); + return micromanager::propertyType(*m_core, cameraId, name); } bool MicroManagerProvider::isPropertyReadOnly(const QString& cameraId, const QString& name) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->isPropertyReadOnly(cameraId, name); + return provider->isPropertyReadOnly(cameraId.trimmed(), name); } return orElse(true, [&] { @@ -233,9 +340,9 @@ namespace scopeone::core::internal bool MicroManagerProvider::isPropertyPreInit(const QString& cameraId, const QString& name) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->isPropertyPreInit(cameraId, name); + return provider->isPropertyPreInit(cameraId.trimmed(), name); } return orElse(false, [&] { @@ -246,9 +353,9 @@ namespace scopeone::core::internal QStringList MicroManagerProvider::getAllowedPropertyValues(const QString& cameraId, const QString& name) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->getAllowedPropertyValues(cameraId, name); + return provider->getAllowedPropertyValues(cameraId.trimmed(), name); } return orElse(QStringList{}, [&] { @@ -259,9 +366,9 @@ namespace scopeone::core::internal bool MicroManagerProvider::hasPropertyLimits(const QString& cameraId, const QString& name) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->hasPropertyLimits(cameraId, name); + return provider->hasPropertyLimits(cameraId.trimmed(), name); } return orElse(false, [&] { @@ -271,9 +378,9 @@ namespace scopeone::core::internal double MicroManagerProvider::getPropertyLowerLimit(const QString& cameraId, const QString& name) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->getPropertyLowerLimit(cameraId, name); + return provider->getPropertyLowerLimit(cameraId.trimmed(), name); } return orElse(0.0, [&] { @@ -283,9 +390,9 @@ namespace scopeone::core::internal double MicroManagerProvider::getPropertyUpperLimit(const QString& cameraId, const QString& name) { - if (isCamera(cameraId)) + if (const CameraRef provider = camera(cameraId)) { - return m_cameraProvider->getPropertyUpperLimit(cameraId, name); + return provider->getPropertyUpperLimit(cameraId.trimmed(), name); } return orElse(0.0, [&] { @@ -299,12 +406,14 @@ namespace scopeone::core::internal int width, int height) { - return m_cameraProvider->setROI(cameraId, x, y, width, height); + const CameraRef provider = camera(cameraId); + return provider && provider->setROI(cameraId.trimmed(), x, y, width, height); } bool MicroManagerProvider::clearROI(const QString& cameraId) { - return m_cameraProvider->clearROI(cameraId); + const CameraRef provider = camera(cameraId); + return provider && provider->clearROI(cameraId.trimmed()); } bool MicroManagerProvider::getROI(const QString& cameraId, @@ -313,19 +422,22 @@ namespace scopeone::core::internal int& width, int& height) { - return m_cameraProvider->getROI(cameraId, x, y, width, height); + const CameraRef provider = camera(cameraId); + return provider && provider->getROI(cameraId.trimmed(), x, y, width, height); } bool MicroManagerProvider::captureEventFrame(const QString& cameraId, scopeone::core::ImageFrame& frame, int timeoutMs) { - return m_cameraProvider->captureEventFrame(cameraId, frame, timeoutMs); + const CameraRef provider = camera(cameraId); + return provider && provider->captureEventFrame(cameraId.trimmed(), frame, timeoutMs); } bool MicroManagerProvider::supportsEventSequence(const QString& cameraId) const { - return m_cameraProvider->supportsEventSequence(cameraId); + const CameraRef provider = camera(cameraId); + return provider && provider->supportsEventSequence(cameraId.trimmed()); } bool MicroManagerProvider::captureEventSequence(const QString& cameraId, @@ -333,35 +445,8 @@ namespace scopeone::core::internal std::vector& frames, int timeoutMs) { - return m_cameraProvider->captureEventSequence(cameraId, count, frames, timeoutMs); - } - - void MicroManagerProvider::setFrameDeliveryPaused(const QStringList& cameraIds, bool paused) - { - m_cameraRuntimeControl->setFrameDeliveryPaused(cameraIds, paused); - } - - bool MicroManagerProvider::setRecordingFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) - { - return m_cameraRuntimeControl->setRecordingFrameDeliveryEnabled(cameraIds, enabled); - } - - bool MicroManagerProvider::setHighRateFrameDeliveryEnabled(const QStringList& cameraIds, - bool enabled) - { - return m_cameraRuntimeControl->setHighRateFrameDeliveryEnabled(cameraIds, enabled); - } - - bool MicroManagerProvider::isProcessingFrameTokenCurrent(const QString& cameraId, - quint64 token) - { - return m_cameraRuntimeControl->isProcessingFrameTokenCurrent(cameraId, token); - } - - void MicroManagerProvider::finishProcessingFrame(const QString& cameraId, quint64 token) - { - m_cameraRuntimeControl->finishProcessingFrame(cameraId, token); + const CameraRef provider = camera(cameraId); + return provider && provider->captureEventSequence(cameraId.trimmed(), count, frames, timeoutMs); } QString MicroManagerProvider::defaultXYStage() const @@ -635,9 +720,14 @@ namespace scopeone::core::internal return ok; } - bool MicroManagerProvider::autoShutter(bool& enabled, QString* errorMessage) const + QString MicroManagerProvider::defaultShutter() const + { + return orElse(QString{}, [&] { return QString::fromStdString(m_core->getShutterDevice()); }); + } + + bool MicroManagerProvider::autoShutter() const { - return succeeds(errorMessage, [&] { enabled = m_core->getAutoShutter(); }); + return orElse(false, [&] { return m_core->getAutoShutter(); }); } bool MicroManagerProvider::setAutoShutter(bool enabled, QString* errorMessage) @@ -645,110 +735,108 @@ namespace scopeone::core::internal return succeeds(errorMessage, [&] { m_core->setAutoShutter(enabled); }); } - bool MicroManagerProvider::setCoreShutterOpen(bool open, QString* errorMessage) + QString MicroManagerProvider::defaultAutofocus() const { - return succeeds(errorMessage, [&] { m_core->setShutterOpen(open); }); + return orElse(QString{}, [&] { return QString::fromStdString(m_core->getAutoFocusDevice()); }); } - bool MicroManagerProvider::hasAutofocus() const + // The core focuses with the autofocus device it currently uses + bool MicroManagerProvider::runAutofocus(const QString& deviceId, QString* errorMessage) { - return orElse(false, [&] { return !m_core->getAutoFocusDevice().empty(); }); + return succeeds(errorMessage, [&] + { + const std::string device = name8(deviceId); + if (m_core->getAutoFocusDevice() != device) + { + m_core->setAutoFocusDevice(device.c_str()); + } + m_core->fullFocus(); + }); } - long MicroManagerProvider::propertySequenceCapacity(const QString& device, const QString& property) const + long MicroManagerProvider::stageSequenceCapacity(const QString& deviceId) const { - if (device.isEmpty() || property.isEmpty()) - { - return 0; - } return orElse(0L, [&] { - const std::string label = device.toStdString(); - const std::string name = property.toStdString(); - return m_core->isPropertySequenceable(label.c_str(), name.c_str()) - ? m_core->getPropertySequenceMaxLength(label.c_str(), name.c_str()) - : 0; + const std::string device = name8(deviceId); + return m_core->isStageSequenceable(device.c_str()) ? m_core->getStageSequenceMaxLength(device.c_str()) : 0; }); } - bool MicroManagerProvider::startPropertySequences(const std::vector& sequences, - QString* errorMessage) + bool MicroManagerProvider::startStageSequence(const QString& deviceId, + const std::vector& positions, + QString* errorMessage) { - std::size_t started = 0; - const bool ok = succeeds(errorMessage, [&] + return succeeds(errorMessage, [&] { - for (const PropertySequence& sequence : sequences) - { - const std::string label = sequence.device.toStdString(); - const std::string name = sequence.property.toStdString(); - std::vector values; - values.reserve(sequence.values.size()); - for (const QString& value : sequence.values) - { - values.push_back(value.toStdString()); - } - m_core->loadPropertySequence(label.c_str(), name.c_str(), values); - m_core->startPropertySequence(label.c_str(), name.c_str()); - ++started; - } + const std::string device = name8(deviceId); + m_core->loadStageSequence(device.c_str(), positions); + m_core->startStageSequence(device.c_str()); }); - if (!ok) - { - // Do not leave the properties that were armed running - stopPropertySequences({sequences.begin(), sequences.begin() + started}); - } - return ok; } - void MicroManagerProvider::stopPropertySequences(const std::vector& sequences) + void MicroManagerProvider::stopStageSequence(const QString& deviceId) + { + succeeds(nullptr, [&] { m_core->stopStageSequence(name8(deviceId).c_str()); }); + } + + long MicroManagerProvider::propertySequenceCapacity(const QString& deviceId, const QString& name) { - for (const PropertySequence& sequence : sequences) + if (const CameraRef provider = camera(deviceId)) { - succeeds(nullptr, [&] - { - m_core->stopPropertySequence(sequence.device.toStdString().c_str(), - sequence.property.toStdString().c_str()); - }); + return provider->propertySequenceCapacity(deviceId.trimmed(), name); } + return micromanager::propertySequenceCapacity(*m_core, deviceId, name); } - long MicroManagerProvider::focusSequenceCapacity(const QString& focusDevice) const + bool MicroManagerProvider::startPropertySequence(const QString& deviceId, + const QString& name, + const QStringList& values, + QString* errorMessage) { - if (focusDevice.isEmpty()) + if (const CameraRef provider = camera(deviceId)) { - return 0; + return provider->startPropertySequence(deviceId.trimmed(), name, values, errorMessage); } - return orElse(0L, [&] - { - const std::string label = focusDevice.toStdString(); - return m_core->isStageSequenceable(label.c_str()) - ? m_core->getStageSequenceMaxLength(label.c_str()) - : 0; - }); + return micromanager::startPropertySequence(*m_core, deviceId, name, values, errorMessage); } - bool MicroManagerProvider::startFocusSequence(const QString& focusDevice, - const std::vector& positions, - QString* errorMessage) + void MicroManagerProvider::stopPropertySequence(const QString& deviceId, const QString& name) { - return succeeds(errorMessage, [&] + if (const CameraRef provider = camera(deviceId)) { - const std::string label = focusDevice.toStdString(); - m_core->loadStageSequence(label.c_str(), positions); - m_core->startStageSequence(label.c_str()); - }); + provider->stopPropertySequence(deviceId.trimmed(), name); + return; + } + micromanager::stopPropertySequence(*m_core, deviceId, name); } - void MicroManagerProvider::stopFocusSequence(const QString& focusDevice) + // The pixel size configurations are on the core of ScopeOne, also for a camera in a DriverHost. + // The core scales them by the binning of the camera it holds, as MMCore does, so a camera in + // a DriverHost is scaled by its own binning here. + double MicroManagerProvider::pixelSizeUm(const QString& cameraId) const { - if (!focusDevice.isEmpty()) + const Camera entry = cameraEntry(cameraId); + if (!entry.camera) + { + return 0.0; + } + const double pixelSizeUm = orElse(0.0, [&] { return m_core->getPixelSizeUm(); }); + if (entry.local) { - succeeds(nullptr, [&] { m_core->stopStageSequence(focusDevice.toStdString().c_str()); }); + return pixelSizeUm; } + QReadLocker locker(&m_camerasLock); + return pixelSizeUm * m_binnings.value(cameraId.trimmed(), 1); } - bool MicroManagerProvider::runAutofocus(QString* errorMessage) + void MicroManagerProvider::readBinning(const QString& cameraId) { - return succeeds(errorMessage, [&] { m_core->fullFocus(); }); + const CameraRef provider = camera(cameraId); + // Cameras name their binning "2" or "2x2" + const QString value = provider->getProperty(cameraId, QLatin1String(MM::g_Keyword_Binning), false); + const int binning = (std::max)(1, value.section(QLatin1Char('x'), 0, 0).toInt()); + QWriteLocker locker(&m_camerasLock); + m_binnings.insert(cameraId, binning); } } diff --git a/ScopeOneCore/src/NativeCameraBackend.cpp b/ScopeOneCore/src/NativeCameraBackend.cpp deleted file mode 100644 index 4eaeecd..0000000 --- a/ScopeOneCore/src/NativeCameraBackend.cpp +++ /dev/null @@ -1,1180 +0,0 @@ -#include "internal/CameraBackend.h" - -#include "MMCore.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace scopeone::core::internal -{ - namespace - { - constexpr int kMinimumPollIntervalMs = 1; - constexpr int kMaximumPollIntervalMs = 50; - constexpr int kPreviewFrameDeliveryIntervalMs = 16; - - int pollingIntervalFor(double frameIntervalMs) - { - if (!std::isfinite(frameIntervalMs) || frameIntervalMs <= 0.0) - { - return kMinimumPollIntervalMs; - } - const double intervalMs = std::clamp(frameIntervalMs / 4.0, - static_cast(kMinimumPollIntervalMs), - static_cast(kMaximumPollIntervalMs)); - return static_cast(intervalMs); - } - - struct NativeStreamConfiguration - { - std::shared_ptr core; - QString cameraId; - int width{0}; - int height{0}; - int stride{0}; - int bitsPerSample{0}; - ImagePixelFormat pixelFormat{ImagePixelFormat::Invalid}; - int sourceRoiX{0}; - int sourceRoiY{0}; - int sourceRoiWidth{0}; - int sourceRoiHeight{0}; - quint64 firstFrameIndex{0}; - quint64 generation{0}; - double expectedIntervalMs{0.0}; - - bool isValid() const - { - const qint64 payloadBytes = static_cast(stride) * height; - const int bytesPerPixel = pixelFormat == ImagePixelFormat::Mono16 - ? 2 - : (pixelFormat == ImagePixelFormat::Mono8 ? 1 : 0); - const qint64 minimumStride = static_cast(width) * bytesPerPixel; - return core - && !cameraId.trimmed().isEmpty() - && width > 0 - && height > 0 - && bytesPerPixel > 0 - && stride >= minimumStride - && (stride % bytesPerPixel) == 0 - && bitsPerSample == ImageFrame::normalizedBitsPerSample(pixelFormat, bitsPerSample) - && payloadBytes > 0 - && generation > 0; - } - }; - - class NativeFrameWorker; - - class NativeCameraBackend final : public CameraBackend - { - public: - explicit NativeCameraBackend(ProcessingFrameGate& processingFrameGate); - ~NativeCameraBackend() override; - - Kind kind() const override { return Kind::Native; } - void shutdownNow(); - bool configureNativeCamera(const std::shared_ptr& core, - const QString& cameraId, - double exposureMs) override; - - bool startPreview() override; - bool stopPreview() override; - bool startPreviewFor(const QString& cameraId) override; - bool stopPreviewFor(const QString& cameraId) override; - bool isPreviewRunning(const QString& cameraId) const override; - void setFrameDeliveryPaused(bool paused) override; - bool captureEventFrame(const QString& cameraId, ImageFrame& frame, int timeoutMs) override; - bool supportsEventSequence(const QString& cameraId) const override; - bool captureEventSequence(const QString& cameraId, - int count, - std::vector& frames, - int timeoutMs) override; - - protected: - bool hasRunningCamera() const override { return m_running; } - bool resolvePrimaryCameraId(const QString& cameraIdOrAll, QString& cameraId) const override; - QStringList resolveTargetCameraIds(const QString& cameraIdOrAll) const override; - bool readExposureFor(const QString& cameraId, double& exposureMs) const override; - bool writeExposureFor(const QString& cameraId, double exposureMs) override; - QStringList listPropertiesFor(const QString& cameraId) override; - bool readPropertyDetailsFor(const QString& cameraId, - const QString& name, - bool fromCache, - CameraPropertyReadback& readback) override; - bool setPropertyFor(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage) override; - bool setROIFor(const QString& cameraId, int x, int y, int width, int height) override; - bool clearROIFor(const QString& cameraId) override; - bool getROIFor(const QString& cameraId, - int& x, - int& y, - int& width, - int& height) override; - - private: - friend class NativeFrameWorker; - - bool startNativeStream(); - bool stopNativeStream(); - void setWorkerPaused(bool paused); - void handleStreamFailure(quint64 generation, - const QString& cameraId, - const QString& error); - bool matchesCamera(const QString& cameraId) const; - - std::shared_ptr m_core; - QString m_cameraId; - double m_exposureMs{10.0}; - bool m_running{false}; - bool m_frameDeliveryPaused{false}; - // The live sequence was stopped to capture an acquisition event - // Builds a frame from the pixels of the current image buffer of the core - bool makeEventFrame(const void* pixels, ImageFrame& frame); - - std::atomic m_sequenceSuspended{false}; - std::atomic m_lastFrameIndex{0}; - quint64 m_streamGeneration{0}; - quint64 m_activeGeneration{0}; - QThread m_streamThread; - NativeFrameWorker* m_worker{nullptr}; - }; - - class NativeFrameWorker final : public QObject - { - public: - explicit NativeFrameWorker(NativeCameraBackend* owner) - : m_owner(owner) - { - } - - bool start(const NativeStreamConfiguration& configuration) - { - stop(); - if (!configuration.isValid()) - { - return false; - } - - m_configuration = configuration; - m_configuration.cameraId = m_configuration.cameraId.trimmed(); - m_frameIndex = configuration.firstFrameIndex; - m_observedIntervalMs = configuration.expectedIntervalMs; - m_pendingAcquiredFrameCount = 0; - m_paused = false; - m_frameIntervalTimer.start(); - m_deliveryTimer.invalidate(); - - if (!m_timer) - { - m_timer = new QTimer(this); - m_timer->setTimerType(Qt::PreciseTimer); - connect(m_timer, &QTimer::timeout, this, [this]() { poll(); }); - } - m_timer->setInterval(pollingIntervalFor(configuration.expectedIntervalMs)); - m_timer->start(); - return true; - } - - void stop() - { - if (m_timer) - { - m_timer->stop(); - } - m_configuration = NativeStreamConfiguration{}; - m_frameIntervalTimer.invalidate(); - m_deliveryTimer.invalidate(); - m_frameIndex = 0; - m_pendingAcquiredFrameCount = 0; - m_observedIntervalMs = 0.0; - m_paused = false; - } - - void setPaused(bool paused) - { - if (m_paused == paused || !m_configuration.isValid()) - { - return; - } - m_paused = paused; - if (m_paused) - { - m_timer->stop(); - return; - } - m_frameIntervalTimer.restart(); - m_timer->start(); - } - - private: - void poll() - { - if (m_paused || !m_configuration.isValid()) - { - return; - } - - try - { - long remaining = m_configuration.core->getRemainingImageCount(); - if (remaining <= 0) - { - return; - } - - const qint64 payloadByteCount = static_cast(m_configuration.stride) - * m_configuration.height; - const QPointer owner = m_owner; - if (!owner) - { - return; - } - - // Copy all recording frames but admit only one live processing frame at a time - const quint64 recordingToken = owner->recordingFrameDeliveryToken(); - const bool requiresAllFrames = recordingToken != 0; - const bool requiresHighRateFrames = owner->highRateFrameDeliveryEnabled(); - const bool deliverLatest = requiresAllFrames - || !m_deliveryTimer.isValid() - || m_deliveryTimer.elapsed() >= kPreviewFrameDeliveryIntervalMs; - QList frames; - int frameCount = 0; - while (remaining-- > 0) - { - const void* pixels = m_configuration.core->popNextImage(); - if (!pixels) - { - break; - } - const quint64 frameIndex = ++m_frameIndex; - ++frameCount; - const bool latestFrame = remaining == 0; - const quint64 processingToken = requiresHighRateFrames && latestFrame - ? owner->tryAcquireProcessingFrame(m_configuration.cameraId) - : 0; - const bool processingFrameSelected = processingToken != 0; - const bool previewFrameSelected = latestFrame && deliverLatest; - if (!requiresAllFrames - && !processingFrameSelected - && !previewFrameSelected) - { - continue; - } - - ImageFrame frame; - frame.cameraId = m_configuration.cameraId; - frame.width = m_configuration.width; - frame.height = m_configuration.height; - frame.stride = m_configuration.stride; - frame.bitsPerSample = m_configuration.bitsPerSample; - frame.pixelFormat = m_configuration.pixelFormat; - frame.frameIndex = frameIndex; - frame.timestampNs = static_cast(QDateTime::currentMSecsSinceEpoch()) * 1000000ull; - frame.sourceRoiX = m_configuration.sourceRoiX; - frame.sourceRoiY = m_configuration.sourceRoiY; - frame.sourceRoiWidth = m_configuration.sourceRoiWidth; - frame.sourceRoiHeight = m_configuration.sourceRoiHeight; - frame.bytes.resize(static_cast(payloadByteCount)); - memcpy(frame.bytes.data(), pixels, static_cast(payloadByteCount)); - if (processingFrameSelected) - { - owner->submitProcessingFrame(frame, processingToken); - } - if (requiresAllFrames || previewFrameSelected) - { - frames.append(std::move(frame)); - } - } - owner->m_lastFrameIndex.store(m_frameIndex, std::memory_order_relaxed); - if (requiresAllFrames) - { - m_pendingAcquiredFrameCount = 0; - if (!frames.isEmpty()) - { - owner->submitFrames(frames, - static_cast(frameCount), - recordingToken); - } - } - else - { - m_pendingAcquiredFrameCount += static_cast(frameCount); - if (!frames.isEmpty()) - { - const quint64 acquiredFrameCount = m_pendingAcquiredFrameCount; - m_pendingAcquiredFrameCount = 0; - m_deliveryTimer.restart(); - owner->submitFrames(frames, - acquiredFrameCount, - recordingToken); - } - } - updatePollingInterval(frameCount); - } - catch (const CMMError& error) - { - m_timer->stop(); - postFailure(m_configuration.generation, - m_configuration.cameraId, - QString::fromStdString(error.getMsg())); - } - } - - void postFailure(quint64 generation, const QString& cameraId, const QString& error) - { - const QPointer owner = m_owner; - if (!owner) - { - return; - } - QMetaObject::invokeMethod( - owner.data(), - [owner, generation, cameraId, error]() - { - if (owner) - { - owner->handleStreamFailure(generation, cameraId, error); - } - }, - Qt::QueuedConnection); - } - - void updatePollingInterval(int frameCount) - { - if (frameCount <= 0 || !m_frameIntervalTimer.isValid()) - { - return; - } - - const double elapsedMs = static_cast(m_frameIntervalTimer.nsecsElapsed()) / 1000000.0; - m_frameIntervalTimer.restart(); - const double measuredIntervalMs = elapsedMs / frameCount; - if (!std::isfinite(measuredIntervalMs) || measuredIntervalMs <= 0.0) - { - return; - } - - m_observedIntervalMs = m_observedIntervalMs > 0.0 - ? 0.75 * m_observedIntervalMs + 0.25 * measuredIntervalMs - : measuredIntervalMs; - const int intervalMs = pollingIntervalFor(m_observedIntervalMs); - if (m_timer->interval() != intervalMs) - { - m_timer->setInterval(intervalMs); - } - } - - QPointer m_owner; - QTimer* m_timer{nullptr}; - NativeStreamConfiguration m_configuration; - QElapsedTimer m_frameIntervalTimer; - QElapsedTimer m_deliveryTimer; - quint64 m_frameIndex{0}; - quint64 m_pendingAcquiredFrameCount{0}; - double m_observedIntervalMs{0.0}; - bool m_paused{false}; - }; - - NativeCameraBackend::NativeCameraBackend(ProcessingFrameGate& processingFrameGate) - : CameraBackend(processingFrameGate) - { - m_streamThread.setObjectName(QStringLiteral("ScopeOneNativeFrameWorker")); - m_worker = new NativeFrameWorker(this); - m_worker->moveToThread(&m_streamThread); - connect(&m_streamThread, &QThread::finished, m_worker, &QObject::deleteLater); - m_streamThread.start(); - } - - NativeCameraBackend::~NativeCameraBackend() - { - shutdownNow(); - m_streamThread.quit(); - m_streamThread.wait(); - m_worker = nullptr; - } - - void NativeCameraBackend::shutdownNow() - { - if (m_running) - { - stopNativeStream(); - } - m_core.reset(); - m_cameraId.clear(); - m_exposureMs = 10.0; - m_lastFrameIndex.store(0, std::memory_order_relaxed); - m_activeGeneration = 0; - m_frameDeliveryPaused = false; - } - - bool NativeCameraBackend::configureNativeCamera(const std::shared_ptr& core, - const QString& cameraId, - double exposureMs) - { - const QString normalizedId = cameraId.trimmed(); - if (!core || normalizedId.isEmpty()) - { - return false; - } - shutdownNow(); - m_core = core; - m_cameraId = normalizedId; - m_exposureMs = exposureMs > 0.0 ? exposureMs : 10.0; - return true; - } - - bool NativeCameraBackend::startPreview() - { - return startPreviewFor(m_cameraId); - } - - bool NativeCameraBackend::stopPreview() - { - return stopPreviewFor(m_cameraId); - } - - bool NativeCameraBackend::startPreviewFor(const QString& cameraId) - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - if (m_running) - { - return true; - } - if (!startNativeStream()) - { - return false; - } - notifyPreviewStarted(m_cameraId); - return true; - } - - bool NativeCameraBackend::stopPreviewFor(const QString& cameraId) - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - if (!m_running) - { - return true; - } - const bool stopped = stopNativeStream(); - notifyPreviewStopped(); - return stopped; - } - - bool NativeCameraBackend::isPreviewRunning(const QString& cameraId) const - { - return matchesCamera(cameraId) && m_running; - } - - void NativeCameraBackend::setFrameDeliveryPaused(bool paused) - { - if (m_frameDeliveryPaused == paused) - { - return; - } - m_frameDeliveryPaused = paused; - if (!paused && m_sequenceSuspended.exchange(false) && m_running && m_core) - { - try - { - if (!m_core->isSequenceRunning()) - { - m_core->setCameraDevice(m_cameraId.toStdString().c_str()); - m_core->startContinuousSequenceAcquisition(0.0); - } - } - catch (const CMMError& error) - { - qWarning().noquote() - << QString("Failed to resume the live view of '%1': %2") - .arg(m_cameraId, QString::fromStdString(error.getMsg())); - } - } - setWorkerPaused(paused); - } - - bool NativeCameraBackend::makeEventFrame(const void* pixels, ImageFrame& frame) - { - const std::string camera = m_cameraId.toStdString(); - const unsigned width = m_core->getImageWidth(); - const unsigned height = m_core->getImageHeight(); - const unsigned bytesPerPixel = m_core->getBytesPerPixel(); - const qint64 stride = static_cast(width) * bytesPerPixel; - const qint64 payloadByteCount = stride * height; - if (!pixels - || (bytesPerPixel != 1 && bytesPerPixel != 2) - || width > static_cast((std::numeric_limits::max)()) - || height > static_cast((std::numeric_limits::max)()) - || stride > (std::numeric_limits::max)() - || payloadByteCount <= 0) - { - return false; - } - - ImageFrame captured; - captured.cameraId = m_cameraId; - captured.width = static_cast(width); - captured.height = static_cast(height); - captured.stride = static_cast(stride); - captured.pixelFormat = bytesPerPixel == 2 ? ImagePixelFormat::Mono16 - : ImagePixelFormat::Mono8; - int bitsPerSample = static_cast(bytesPerPixel) * 8; - try - { - bitsPerSample = static_cast(m_core->getImageBitDepth()); - } - catch (const CMMError&) - { - } - captured.bitsPerSample = - ImageFrame::normalizedBitsPerSample(captured.pixelFormat, bitsPerSample); - captured.sourceRoiWidth = captured.width; - captured.sourceRoiHeight = captured.height; - try - { - m_core->getROI(camera.c_str(), - captured.sourceRoiX, - captured.sourceRoiY, - captured.sourceRoiWidth, - captured.sourceRoiHeight); - } - catch (const CMMError&) - { - captured.sourceRoiX = 0; - captured.sourceRoiY = 0; - captured.sourceRoiWidth = captured.width; - captured.sourceRoiHeight = captured.height; - } - captured.frameIndex = m_lastFrameIndex.fetch_add(1, std::memory_order_relaxed) + 1; - captured.timestampNs = - static_cast(QDateTime::currentMSecsSinceEpoch()) * 1000000ull; - captured.bytes = QByteArray(reinterpret_cast(pixels), - static_cast(payloadByteCount)); - if (!captured.isValid()) - { - return false; - } - frame = std::move(captured); - return true; - } - - bool NativeCameraBackend::supportsEventSequence(const QString& cameraId) const - { - return m_core && matchesCamera(cameraId); - } - - // Takes a series of images in one sequence acquisition of the camera. The images come - // from the circular buffer of the core, so the camera times them itself and can - // trigger a stage that steps through a sequence of positions. - bool NativeCameraBackend::captureEventSequence(const QString& cameraId, - int count, - std::vector& frames, - int timeoutMs) - { - frames.clear(); - if (!m_core || !matchesCamera(cameraId) || count < 1) - { - return false; - } - const std::string camera = m_cameraId.toStdString(); - bool started = false; - const auto stop = [&]() - { - try - { - if (started && m_core->isSequenceRunning(camera.c_str())) - { - m_core->stopSequenceAcquisition(camera.c_str()); - } - } - catch (const CMMError&) - { - } - }; - try - { - if (m_core->isSequenceRunning()) - { - m_core->stopSequenceAcquisition(); - m_sequenceSuspended.store(true); - } - m_core->setCameraDevice(camera.c_str()); - m_core->initializeCircularBuffer(); - m_core->clearCircularBuffer(); - m_core->startSequenceAcquisition(count, 0.0, true); - started = true; - - QElapsedTimer timer; - timer.start(); - frames.reserve(static_cast(count)); - while (static_cast(frames.size()) < count) - { - if (m_core->getRemainingImageCount() > 0) - { - const void* pixels = m_core->popNextImage(); - ImageFrame frame; - if (!makeEventFrame(pixels, frame)) - { - stop(); - return false; - } - frames.push_back(std::move(frame)); - continue; - } - if (!m_core->isSequenceRunning(camera.c_str())) - { - break; - } - if (timer.elapsed() > timeoutMs) - { - qWarning().noquote() - << QString("Timed out waiting for the image sequence of '%1'").arg(m_cameraId); - stop(); - return false; - } - QThread::msleep(1); - } - // The last images may arrive between the last check and the end of the sequence - while (static_cast(frames.size()) < count && m_core->getRemainingImageCount() > 0) - { - ImageFrame frame; - if (!makeEventFrame(m_core->popNextImage(), frame)) - { - stop(); - return false; - } - frames.push_back(std::move(frame)); - } - stop(); - return static_cast(frames.size()) == count; - } - catch (const CMMError& error) - { - qWarning().noquote() - << QString("Failed to capture an image sequence from '%1': %2") - .arg(m_cameraId, QString::fromStdString(error.getMsg())); - stop(); - return false; - } - } - - // Takes one image for an acquisition event. A running live sequence is stopped first, - // because its buffer holds images taken before the settings of the event were applied. - bool NativeCameraBackend::captureEventFrame(const QString& cameraId, - ImageFrame& frame, - int timeoutMs) - { - (void)timeoutMs; - if (!m_core || !matchesCamera(cameraId)) - { - return false; - } - try - { - const std::string camera = m_cameraId.toStdString(); - // The camera device cannot be selected while a sequence is running - if (m_core->isSequenceRunning()) - { - m_core->stopSequenceAcquisition(); - m_sequenceSuspended.store(true); - } - m_core->setCameraDevice(camera.c_str()); - m_core->snapImage(); - const void* pixels = m_core->getImage(); - return makeEventFrame(pixels, frame); - } - catch (const CMMError& error) - { - qWarning().noquote() - << QString("Failed to capture an event frame from '%1': %2") - .arg(m_cameraId, QString::fromStdString(error.getMsg())); - return false; - } - } - - bool NativeCameraBackend::resolvePrimaryCameraId(const QString& cameraIdOrAll, QString& cameraId) const - { - const QString target = cameraIdOrAll.compare(QStringLiteral("All"), Qt::CaseInsensitive) == 0 - ? m_cameraId - : cameraIdOrAll; - if (!matchesCamera(target)) - { - return false; - } - cameraId = m_cameraId; - return true; - } - - QStringList NativeCameraBackend::resolveTargetCameraIds(const QString& cameraIdOrAll) const - { - QString cameraId; - return resolvePrimaryCameraId(cameraIdOrAll, cameraId) ? QStringList{cameraId} : QStringList{}; - } - - bool NativeCameraBackend::readExposureFor(const QString& cameraId, double& exposureMs) const - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - try - { - exposureMs = m_core->getExposure(cameraId.toStdString().c_str()); - return true; - } - catch (const CMMError&) - { - exposureMs = m_exposureMs; - return exposureMs > 0.0; - } - } - - bool NativeCameraBackend::writeExposureFor(const QString& cameraId, double exposureMs) - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - try - { - m_core->setCameraDevice(cameraId.toStdString().c_str()); - m_core->setExposure(exposureMs); - m_core->waitForDevice(cameraId.toStdString().c_str()); - double actualExposureMs = exposureMs; - try - { - actualExposureMs = m_core->getExposure(); - } - catch (const CMMError&) - { - } - m_exposureMs = actualExposureMs; - return true; - } - catch (const CMMError&) - { - return false; - } - } - - QStringList NativeCameraBackend::listPropertiesFor(const QString& cameraId) - { - if (!matchesCamera(cameraId) || !m_core) - { - return {}; - } - try - { - QStringList properties; - for (const std::string& name : m_core->getDevicePropertyNames(cameraId.toStdString().c_str())) - { - properties << QString::fromStdString(name); - } - return properties; - } - catch (const CMMError&) - { - return {}; - } - } - - bool NativeCameraBackend::readPropertyDetailsFor(const QString& cameraId, - const QString& name, - bool fromCache, - CameraPropertyReadback& readback) - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - - const std::string camera = cameraId.toStdString(); - const std::string property = name.toStdString(); - try - { - readback.value = QString::fromStdString( - fromCache - ? m_core->getPropertyFromCache(camera.c_str(), property.c_str()) - : m_core->getProperty(camera.c_str(), property.c_str())); - try - { - switch (m_core->getPropertyType(camera.c_str(), property.c_str())) - { - case MM::String: readback.type = QStringLiteral("String"); break; - case MM::Float: readback.type = QStringLiteral("Float"); break; - case MM::Integer: readback.type = QStringLiteral("Integer"); break; - default: readback.type = QStringLiteral("Unknown"); break; - } - } - catch (const CMMError&) - { - } - try - { - readback.readOnly = m_core->isPropertyReadOnly(camera.c_str(), property.c_str()); - } - catch (const CMMError&) - { - } - try - { - readback.preInit = m_core->isPropertyPreInit(camera.c_str(), property.c_str()); - } - catch (const CMMError&) - { - } - try - { - for (const std::string& value : m_core->getAllowedPropertyValues(camera.c_str(), property.c_str())) - { - readback.allowedValues << QString::fromStdString(value); - } - } - catch (const CMMError&) - { - } - try - { - readback.hasLimits = m_core->hasPropertyLimits(camera.c_str(), property.c_str()); - if (readback.hasLimits) - { - readback.lowerLimit = m_core->getPropertyLowerLimit(camera.c_str(), property.c_str()); - readback.upperLimit = m_core->getPropertyUpperLimit(camera.c_str(), property.c_str()); - } - } - catch (const CMMError&) - { - } - return true; - } - catch (const CMMError&) - { - return false; - } - } - - bool NativeCameraBackend::setPropertyFor(const QString& cameraId, - const QString& name, - const QString& value, - QString* errorMessage) - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - try - { - m_core->setCameraDevice(cameraId.toStdString().c_str()); - m_core->setProperty(cameraId.toStdString().c_str(), - name.toStdString().c_str(), - value.toStdString().c_str()); - m_core->waitForDevice(cameraId.toStdString().c_str()); - return true; - } - catch (const CMMError& error) - { - if (errorMessage) - { - *errorMessage = QString::fromStdString(error.getMsg()); - } - return false; - } - } - - bool NativeCameraBackend::setROIFor(const QString& cameraId, int x, int y, int width, int height) - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - try - { - m_core->setROI(cameraId.toStdString().c_str(), x, y, width, height); - m_core->waitForDevice(cameraId.toStdString().c_str()); - return true; - } - catch (const CMMError& error) - { - qWarning().noquote() - << QString("Failed to set ROI for '%1': %2") - .arg(cameraId, QString::fromStdString(error.getMsg())); - return false; - } - } - - bool NativeCameraBackend::clearROIFor(const QString& cameraId) - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - try - { - m_core->setCameraDevice(cameraId.toStdString().c_str()); - m_core->clearROI(); - m_core->waitForDevice(cameraId.toStdString().c_str()); - return true; - } - catch (const CMMError& error) - { - qWarning().noquote() - << QString("Failed to clear ROI for '%1': %2") - .arg(cameraId, QString::fromStdString(error.getMsg())); - return false; - } - } - - bool NativeCameraBackend::getROIFor(const QString& cameraId, - int& x, - int& y, - int& width, - int& height) - { - if (!matchesCamera(cameraId) || !m_core) - { - return false; - } - try - { - m_core->getROI(cameraId.toStdString().c_str(), x, y, width, height); - return true; - } - catch (const CMMError& error) - { - qWarning().noquote() - << QString("Failed to get ROI for '%1': %2") - .arg(cameraId, QString::fromStdString(error.getMsg())); - return false; - } - } - - // Starts continuous MMCore acquisition and the native frame worker - bool NativeCameraBackend::startNativeStream() - { - try - { - m_core->setCameraDevice(m_cameraId.toStdString().c_str()); - if (m_core->isSequenceRunning()) - { - m_core->stopSequenceAcquisition(); - } - - const unsigned width = m_core->getImageWidth(); - const unsigned height = m_core->getImageHeight(); - const unsigned bytesPerPixel = m_core->getBytesPerPixel(); - const qint64 stride = static_cast(width) * bytesPerPixel; - const qint64 payloadByteCount = stride * height; - if ((bytesPerPixel != 1 && bytesPerPixel != 2) - || width > static_cast((std::numeric_limits::max)()) - || height > static_cast((std::numeric_limits::max)()) - || stride > (std::numeric_limits::max)() - || payloadByteCount <= 0) - { - return false; - } - - NativeStreamConfiguration configuration; - configuration.core = m_core; - configuration.cameraId = m_cameraId; - configuration.width = static_cast(width); - configuration.height = static_cast(height); - configuration.stride = static_cast(stride); - configuration.pixelFormat = bytesPerPixel == 2 - ? ImagePixelFormat::Mono16 - : ImagePixelFormat::Mono8; - int bitsPerSample = bytesPerPixel == 2 ? 16 : 8; - try - { - bitsPerSample = static_cast(m_core->getImageBitDepth()); - } - catch (const CMMError&) - { - } - configuration.bitsPerSample = ImageFrame::normalizedBitsPerSample( - configuration.pixelFormat, - bitsPerSample); - configuration.sourceRoiWidth = configuration.width; - configuration.sourceRoiHeight = configuration.height; - try - { - m_core->getROI(m_cameraId.toStdString().c_str(), - configuration.sourceRoiX, - configuration.sourceRoiY, - configuration.sourceRoiWidth, - configuration.sourceRoiHeight); - } - catch (const CMMError&) - { - configuration.sourceRoiX = 0; - configuration.sourceRoiY = 0; - configuration.sourceRoiWidth = configuration.width; - configuration.sourceRoiHeight = configuration.height; - } - - try - { - const double hardwareExposureMs = m_core->getExposure(); - if (hardwareExposureMs > 0.0) - { - m_exposureMs = hardwareExposureMs; - } - } - catch (const CMMError&) - { - } - configuration.expectedIntervalMs = m_exposureMs; - try - { - const std::string camera = m_cameraId.toStdString(); - if (m_core->hasProperty(camera.c_str(), MM::g_Keyword_ActualInterval_ms)) - { - const std::string value = - m_core->getProperty(camera.c_str(), MM::g_Keyword_ActualInterval_ms); - char* end = nullptr; - const double actualIntervalMs = std::strtod(value.c_str(), &end); - if (end != value.c_str() - && std::isfinite(actualIntervalMs) - && actualIntervalMs > 0.0) - { - configuration.expectedIntervalMs = - (std::max)(configuration.expectedIntervalMs, actualIntervalMs); - } - } - } - catch (const CMMError&) - { - } - - configuration.firstFrameIndex = m_lastFrameIndex.load(std::memory_order_relaxed); - configuration.generation = ++m_streamGeneration; - m_activeGeneration = configuration.generation; - - m_core->startContinuousSequenceAcquisition(0.0); - - bool workerStarted = false; - NativeFrameWorker* const worker = m_worker; - const bool paused = m_frameDeliveryPaused; - const bool invoked = QMetaObject::invokeMethod( - worker, - [worker, configuration, paused, &workerStarted]() - { - workerStarted = worker->start(configuration); - if (workerStarted && paused) - { - worker->setPaused(true); - } - }, - Qt::BlockingQueuedConnection); - if (!invoked || !workerStarted) - { - stopNativeStream(); - return false; - } - - m_running = true; - return true; - } - catch (const CMMError& error) - { - qWarning().noquote() - << QString("Failed to start native camera stream for '%1': %2") - .arg(m_cameraId, QString::fromStdString(error.getMsg())); - stopNativeStream(); - return false; - } - } - - // Stops the frame worker before ending MMCore acquisition - bool NativeCameraBackend::stopNativeStream() - { - ++m_streamGeneration; - m_activeGeneration = 0; - m_sequenceSuspended.store(false); - - NativeFrameWorker* const worker = m_worker; - const bool workerStopped = QMetaObject::invokeMethod( - worker, - [worker]() { worker->stop(); }, - Qt::BlockingQueuedConnection); - discardPendingPreviewFrames(); - - bool sequenceStopped = true; - try - { - if (m_core && m_core->isSequenceRunning()) - { - m_core->stopSequenceAcquisition(); - } - } - catch (const CMMError&) - { - sequenceStopped = false; - } - m_running = false; - return workerStopped && sequenceStopped; - } - - // Applies capture suspension on the native frame worker thread - void NativeCameraBackend::setWorkerPaused(bool paused) - { - NativeFrameWorker* const worker = m_worker; - QMetaObject::invokeMethod( - worker, - [worker, paused]() { worker->setPaused(paused); }, - Qt::BlockingQueuedConnection); - } - - // Handles failures only for the currently active stream generation - void NativeCameraBackend::handleStreamFailure(quint64 generation, - const QString& cameraId, - const QString& error) - { - if (generation != m_activeGeneration || cameraId != m_cameraId) - { - return; - } - const QString message = QStringLiteral("Native camera stream failed for '%1': %2") - .arg(cameraId, error); - const bool wasRecording = recordingFrameDeliveryEnabled(); - if (wasRecording) - { - CameraBackend::setRecordingFrameDeliveryEnabled(false); - } - qWarning().noquote() << message; - stopNativeStream(); - notifyPreviewStopped(); - if (wasRecording) - { - emit frameDeliveryFailed(message, 0); - } - } - - bool NativeCameraBackend::matchesCamera(const QString& cameraId) const - { - return !m_cameraId.isEmpty() && cameraId.trimmed() == m_cameraId; - } - } - - std::unique_ptr createNativeCameraBackend( - ProcessingFrameGate& processingFrameGate) - { - return std::make_unique(processingFrameGate); - } -} diff --git a/ScopeOneCore/src/PtuFile.cpp b/ScopeOneCore/src/PtuFile.cpp deleted file mode 100644 index ead887c..0000000 --- a/ScopeOneCore/src/PtuFile.cpp +++ /dev/null @@ -1,593 +0,0 @@ -#include "internal/PtuFile.h" - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace scopeone::core::internal -{ - using namespace scopeone::core; - - namespace - { - constexpr auto kSourceId = "ptu:file"; - constexpr qsizetype kTagSize = 48; - constexpr qsizetype kRecordsPerChunk = 65536; - - constexpr quint32 kTagEmpty = 0xffff0008U; - constexpr quint32 kTagBool = 0x00000008U; - constexpr quint32 kTagInt = 0x10000008U; - constexpr quint32 kTagBitSet = 0x11000008U; - constexpr quint32 kTagColor = 0x12000008U; - constexpr quint32 kTagFloat = 0x20000008U; - constexpr quint32 kTagDateTime = 0x21000008U; - constexpr quint32 kTagFloatArray = 0x2001ffffU; - constexpr quint32 kTagAnsiString = 0x4001ffffU; - constexpr quint32 kTagWideString = 0x4002ffffU; - constexpr quint32 kTagBinaryBlob = 0xffffffffU; - - constexpr quint32 kPicoHarpT3 = 0x00010303U; - constexpr quint32 kPicoHarpT2 = 0x00010203U; - constexpr quint32 kHydraHarpT3 = 0x00010304U; - constexpr quint32 kHydraHarpT2 = 0x00010204U; - constexpr quint32 kHydraHarp2T3 = 0x01010304U; - constexpr quint32 kHydraHarp2T2 = 0x01010204U; - constexpr quint32 kTimeHarp260NT3 = 0x00010305U; - constexpr quint32 kTimeHarp260NT2 = 0x00010205U; - constexpr quint32 kTimeHarp260PT3 = 0x00010306U; - constexpr quint32 kTimeHarp260PT2 = 0x00010206U; - constexpr quint32 kGenericT3 = 0x00010307U; - constexpr quint32 kGenericT2 = 0x00010207U; - - enum class Decoder - { - PicoT2, - PicoT3, - HydraT2V1, - HydraT3V1, - HydraT2V2, - HydraT3V2 - }; - - struct PtuHeader - { - quint64 recordCount{0}; - quint32 recordType{0}; - double globalResolutionSeconds{0.0}; - Decoder decoder{Decoder::PicoT2}; - }; - - struct PtuSettings - { - QString filePath; - int detectorChannel{1}; - double sampleIntervalSeconds{0.01}; - bool publishEvents{false}; - }; - - QString tagIdentifier(const QByteArray& bytes) - { - const int terminator = bytes.indexOf('\0'); - return QString::fromLatin1(terminator >= 0 ? bytes.first(terminator) : bytes); - } - - double tagDouble(qint64 value) - { - const quint64 bits = static_cast(value); - double result = 0.0; - std::memcpy(&result, &bits, sizeof(result)); - return result; - } - - bool decoderForRecordType(quint32 recordType, Decoder& decoder) - { - switch (recordType) - { - case kPicoHarpT2: - decoder = Decoder::PicoT2; - return true; - case kPicoHarpT3: - decoder = Decoder::PicoT3; - return true; - case kHydraHarpT2: - decoder = Decoder::HydraT2V1; - return true; - case kHydraHarpT3: - decoder = Decoder::HydraT3V1; - return true; - case kHydraHarp2T2: - case kTimeHarp260NT2: - case kTimeHarp260PT2: - case kGenericT2: - decoder = Decoder::HydraT2V2; - return true; - case kHydraHarp2T3: - case kTimeHarp260NT3: - case kTimeHarp260PT3: - case kGenericT3: - decoder = Decoder::HydraT3V2; - return true; - default: - return false; - } - } - - bool skipTagPayload(QFile& file, qint64 byteCount, QString& errorMessage) - { - if (byteCount < 0 || byteCount > file.size() - file.pos() - || !file.seek(file.pos() + byteCount)) - { - errorMessage = QStringLiteral("Invalid PTU tag payload length"); - return false; - } - return true; - } - - bool readHeader(QFile& file, PtuHeader& header, QString& errorMessage) - { - const QByteArray magic = file.read(8); - const QByteArray version = file.read(8); - if (magic.size() != 8 || version.size() != 8 - || !magic.startsWith("PQTTTR")) - { - errorMessage = QStringLiteral("The selected file is not a PTU file"); - return false; - } - - qint64 recordCount = -1; - qint64 recordType = -1; - double globalResolution = 0.0; - while (true) - { - const QByteArray rawTag = file.read(kTagSize); - if (rawTag.size() != kTagSize) - { - errorMessage = QStringLiteral("Incomplete PTU header"); - return false; - } - const auto* bytes = reinterpret_cast(rawTag.constData()); - const QString identifier = tagIdentifier(rawTag.first(32)); - const quint32 type = qFromLittleEndian(bytes + 36); - const qint64 value = qFromLittleEndian(bytes + 40); - if (identifier == QStringLiteral("TTResult_NumberOfRecords")) - { - recordCount = value; - } - else if (identifier == QStringLiteral("TTResultFormat_TTTRRecType")) - { - recordType = value; - } - else if (identifier == QStringLiteral("MeasDesc_GlobalResolution")) - { - globalResolution = tagDouble(value); - } - - if (type == kTagFloatArray || type == kTagAnsiString - || type == kTagWideString || type == kTagBinaryBlob) - { - if (!skipTagPayload(file, value, errorMessage)) - { - return false; - } - } - else if (type != kTagEmpty && type != kTagBool && type != kTagInt - && type != kTagBitSet && type != kTagColor - && type != kTagFloat && type != kTagDateTime) - { - errorMessage = QStringLiteral("Unsupported PTU tag type 0x%1") - .arg(type, 8, 16, QLatin1Char('0')); - return false; - } - if (identifier == QStringLiteral("Header_End")) - { - break; - } - } - - Decoder decoder; - if (recordCount < 0 || recordType < 0 - || !std::isfinite(globalResolution) - || globalResolution <= 0.0 - || !decoderForRecordType(static_cast(recordType), decoder)) - { - errorMessage = QStringLiteral("Unsupported or incomplete PTU measurement header"); - return false; - } - header.recordCount = static_cast(recordCount); - header.recordType = static_cast(recordType); - header.globalResolutionSeconds = globalResolution; - header.decoder = decoder; - return true; - } - - SignalSourceDescriptor sourceDescriptor() - { - SignalSourceDescriptor descriptor; - descriptor.id = QString::fromLatin1(kSourceId); - descriptor.name = QStringLiteral("PTU File"); - descriptor.provider = QStringLiteral("PicoQuant"); - descriptor.quantity = QStringLiteral("Photon count"); - descriptor.unit = QStringLiteral("photons"); - descriptor.streamType = SignalStreamType::TimestampedEvents; - - SignalParameterDescriptor file; - file.key = QStringLiteral("filePath"); - file.name = QStringLiteral("PTU file"); - file.type = SignalParameterType::File; - file.fileFilter = QStringLiteral("PicoQuant PTU files (*.ptu);;All files (*)"); - descriptor.parameters.append(file); - - SignalParameterDescriptor channel; - channel.key = QStringLiteral("detectorChannel"); - channel.name = QStringLiteral("Detector channel"); - channel.type = SignalParameterType::Integer; - channel.defaultValue = 1; - channel.hasRange = true; - channel.minimum = 0; - channel.maximum = 64; - descriptor.parameters.append(channel); - return descriptor; - } - - class PtuFileSource final : public SignalSource - { - public: - explicit PtuFileSource(QObject* parent = nullptr) - : SignalSource(parent) - { - } - - ~PtuFileSource() override - { - std::lock_guard lock(m_threadMutex); - if (m_worker.joinable()) - { - m_worker.request_stop(); - m_worker.join(); - } - } - - bool start(const SignalAcquisitionConfig& config, - QString* errorMessage) override - { - const QString filePath = config.sourceSettings - .value(QStringLiteral("filePath")).toString().trimmed(); - if (config.sourceId.trimmed() != QString::fromLatin1(kSourceId) - || !QFileInfo::exists(filePath) - || !QFileInfo(filePath).isFile()) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Select an existing PTU file"); - } - return false; - } - - std::lock_guard lock(m_threadMutex); - if (m_state == SignalSourceState::Starting - || m_state == SignalSourceState::Running - || m_state == SignalSourceState::Stopping) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Signal source is already active"); - } - return false; - } - if (m_worker.joinable()) - { - m_worker.join(); - } - - PtuSettings settings; - settings.filePath = filePath; - settings.detectorChannel = config.sourceSettings - .value(QStringLiteral("detectorChannel"), 1).toInt(); - settings.sampleIntervalSeconds = config.sampleIntervalSeconds; - settings.publishEvents = config.publishTimestampedEvents - || config.scanImage.enabled; - setState(SignalSourceState::Starting, QStringLiteral("Opening PTU file")); - m_worker = std::jthread([this, settings](std::stop_token stopToken) - { - run(stopToken, settings); - }); - return true; - } - - void stop() override - { - std::lock_guard lock(m_threadMutex); - if (m_state == SignalSourceState::Starting - || m_state == SignalSourceState::Running) - { - setState(SignalSourceState::Stopping, QStringLiteral("Stopping PTU read")); - m_worker.request_stop(); - } - } - - SignalSourceState state() const override - { - return m_state.load(); - } - - QString stateMessage() const override - { - std::lock_guard lock(m_stateMutex); - return m_stateMessage; - } - - private: - void setState(SignalSourceState state, const QString& message) - { - { - std::lock_guard lock(m_stateMutex); - m_stateMessage = message; - m_state.store(state); - } - emit stateChanged(state, message); - } - - void fail(const QString& message) - { - setState(SignalSourceState::Error, message); - emit sourceError(message); - } - - void run(std::stop_token stopToken, const PtuSettings& settings) - { - QFile file(settings.filePath); - if (!file.open(QIODevice::ReadOnly)) - { - fail(QStringLiteral("Failed to open PTU file: %1").arg(file.errorString())); - return; - } - - PtuHeader header; - QString errorMessage; - if (!readHeader(file, header, errorMessage)) - { - fail(errorMessage); - return; - } - setState(SignalSourceState::Running, - QStringLiteral("Reading %1 PTU records").arg(header.recordCount)); - - EventCountBinner binner(QString::fromLatin1(kSourceId), - QStringLiteral("Photon count"), - QStringLiteral("photons"), - header.globalResolutionSeconds, - settings.sampleIntervalSeconds); - quint64 overflowCorrection = 0; - quint64 processedRecords = 0; - quint64 photonCount = 0; - quint64 markerCount = 0; - quint64 lastTick = 0; - bool hasTick = false; - - while (processedRecords < header.recordCount - && !stopToken.stop_requested()) - { - const qsizetype count = static_cast(std::min( - kRecordsPerChunk, header.recordCount - processedRecords)); - const QByteArray rawRecords = file.read(count * 4); - if (rawRecords.size() != count * 4) - { - fail(QStringLiteral("Unexpected end of PTU record data")); - return; - } - - TimestampedEventChunk events; - if (settings.publishEvents) - { - events.sourceId = QString::fromLatin1(kSourceId); - events.tickPeriodSeconds = header.globalResolutionSeconds; - events.eventTicks.reserve(count); - events.eventCodes.reserve(count); - } - - const auto addPhoton = [&](quint64 tick, quint32 channel) - { - lastTick = tick; - hasTick = true; - if (static_cast(channel) == settings.detectorChannel) - { - binner.addEvent(tick); - ++photonCount; - if (settings.publishEvents) - { - events.eventTicks.append(tick); - events.eventCodes.append(channel); - } - } - else - { - binner.advanceToTick(tick); - } - }; - const auto addMarker = [&](quint64 tick, quint32 code) - { - lastTick = tick; - hasTick = true; - binner.addMarker(tick, code); - ++markerCount; - if (settings.publishEvents) - { - events.markerTicks.append(tick); - events.markerCodes.append(code); - } - }; - const auto advanceOverflow = [&](quint64 tick) - { - lastTick = tick; - hasTick = true; - binner.advanceToTick(tick); - }; - - const auto* bytes = reinterpret_cast(rawRecords.constData()); - for (qsizetype index = 0; index < count; ++index) - { - const quint32 record = qFromLittleEndian(bytes + index * 4); - switch (header.decoder) - { - case Decoder::PicoT2: - { - constexpr quint64 wraparound = 210698240ULL; - const quint32 channel = record >> 28; - const quint32 time = record & 0x0fffffffU; - if (channel != 0x0fU) - { - addPhoton(overflowCorrection + time, channel); - } - else if ((time & 0x0fU) == 0) - { - overflowCorrection += wraparound; - advanceOverflow(overflowCorrection); - } - else - { - addMarker(overflowCorrection + (time & 0x0ffffff0U), - time & 0x0fU); - } - break; - } - case Decoder::PicoT3: - { - constexpr quint64 wraparound = 65536ULL; - const quint32 channel = record >> 28; - const quint32 nsync = record & 0x0000ffffU; - const quint32 dtime = (record >> 16) & 0x00000fffU; - if (channel != 0x0fU) - { - addPhoton(overflowCorrection + nsync, channel); - } - else if (dtime == 0) - { - overflowCorrection += wraparound; - advanceOverflow(overflowCorrection); - } - else - { - addMarker(overflowCorrection + nsync, dtime); - } - break; - } - case Decoder::HydraT2V1: - case Decoder::HydraT2V2: - { - constexpr quint64 wraparoundV1 = 33552000ULL; - constexpr quint64 wraparoundV2 = 33554432ULL; - const bool version1 = header.decoder == Decoder::HydraT2V1; - const bool special = (record & 0x80000000U) != 0; - const quint32 channel = (record >> 25) & 0x3fU; - const quint32 time = record & 0x01ffffffU; - if (!special) - { - addPhoton(overflowCorrection + time, channel + 1); - } - else if (channel == 0x3fU) - { - const quint64 overflows = version1 || time == 0 ? 1 : time; - overflowCorrection += overflows - * (version1 ? wraparoundV1 : wraparoundV2); - advanceOverflow(overflowCorrection); - } - else if (channel >= 1 && channel <= 15) - { - addMarker(overflowCorrection + time, channel); - } - else if (channel == 0) - { - addPhoton(overflowCorrection + time, 0); - } - break; - } - case Decoder::HydraT3V1: - case Decoder::HydraT3V2: - { - constexpr quint64 wraparound = 1024ULL; - const bool version1 = header.decoder == Decoder::HydraT3V1; - const bool special = (record & 0x80000000U) != 0; - const quint32 channel = (record >> 25) & 0x3fU; - const quint32 nsync = record & 0x000003ffU; - if (!special) - { - addPhoton(overflowCorrection + nsync, channel); - } - else if (channel == 0x3fU) - { - const quint64 overflows = version1 || nsync == 0 ? 1 : nsync; - overflowCorrection += overflows * wraparound; - advanceOverflow(overflowCorrection); - } - else if (channel >= 1 && channel <= 15) - { - addMarker(overflowCorrection + nsync, channel); - } - break; - } - } - if (binner.hasReadyChunks()) - { - for (const TimeSeriesChunk& chunk : binner.takeReadyChunks()) - { - emit timeSeriesReady(chunk); - } - } - } - processedRecords += static_cast(count); - if (events.isValid()) - { - emit timestampedEventsReady(events); - } - } - - if (stopToken.stop_requested()) - { - setState(SignalSourceState::Idle, QStringLiteral("PTU read stopped")); - return; - } - if (hasTick) - { - binner.advanceToElapsedSeconds( - static_cast(lastTick) * header.globalResolutionSeconds - + settings.sampleIntervalSeconds); - } - for (const TimeSeriesChunk& chunk : binner.takeCompletedChunks()) - { - emit timeSeriesReady(chunk); - } - setState(SignalSourceState::Idle, - QStringLiteral("PTU read complete: %1 photons, %2 markers") - .arg(photonCount) - .arg(markerCount)); - } - - mutable std::mutex m_stateMutex; - std::mutex m_threadMutex; - std::jthread m_worker; - std::atomic m_state{SignalSourceState::Idle}; - QString m_stateMessage{QStringLiteral("PTU file source is idle")}; - }; - } - - QList PtuFileSourceFactory::signalSources() const - { - return {sourceDescriptor()}; - } - - SignalSource* PtuFileSourceFactory::createSignalSource(const QString& sourceId, - QObject* parent) - { - return sourceId.trimmed() == QString::fromLatin1(kSourceId) - ? new PtuFileSource(parent) - : nullptr; - } -} diff --git a/ScopeOneCore/src/RecordingManager.cpp b/ScopeOneCore/src/RecordingManager.cpp index 7748bbc..22ac8b1 100644 --- a/ScopeOneCore/src/RecordingManager.cpp +++ b/ScopeOneCore/src/RecordingManager.cpp @@ -1,6 +1,5 @@ #include "internal/RecordingManager.h" -#include "scopeone/CameraProvider.h" #include #include @@ -1384,7 +1383,7 @@ namespace scopeone::core::internal for (ChannelSetting& channel : plan.channels) { - channel.presetSettings = m_configurationProvider->configSettings(plan.channelGroup, + channel.presetSettings = m_hardwareRuntime->configSettings(plan.channelGroup, channel.preset); } @@ -1410,8 +1409,8 @@ namespace scopeone::core::internal if (!usesMda) { primeLastFrameIndices(); - if (m_cameraRuntimeControl - && !m_cameraRuntimeControl->setRecordingFrameDeliveryEnabled( + if (m_hardwareRuntime + && !m_hardwareRuntime->setRecordingFrameDeliveryEnabled( m_captureState.activeCameraIds, true)) { if (plan.streamToDisk) @@ -1441,9 +1440,9 @@ namespace scopeone::core::internal { if (!startStreamingOutputs(plan)) { - if (m_cameraRuntimeControl) + if (m_hardwareRuntime) { - m_cameraRuntimeControl->setRecordingFrameDeliveryEnabled( + m_hardwareRuntime->setRecordingFrameDeliveryEnabled( m_captureState.activeCameraIds, false); } const QString writerError = writerErrorSnapshot(); @@ -1513,17 +1512,17 @@ namespace scopeone::core::internal emit recordingStateChanged(false); emitProgress(true); - if (m_cameraRuntimeControl) + if (m_hardwareRuntime) { - m_cameraRuntimeControl->setRecordingFrameDeliveryEnabled(activeCameraIds, false); + m_hardwareRuntime->setRecordingFrameDeliveryEnabled(activeCameraIds, false); } if (m_mdaState.usingMda && m_mdaState.manager && m_mdaState.manager->isRunning()) { m_mdaState.manager->requestCancel(); } - if (m_cameraRuntimeControl) + if (m_hardwareRuntime) { - m_cameraRuntimeControl->setFrameDeliveryPaused(activeCameraIds, false); + m_hardwareRuntime->setFrameDeliveryPaused(activeCameraIds, false); } qInfo().noquote() << "Recording stopped"; @@ -2081,14 +2080,11 @@ namespace scopeone::core::internal { m_mdaState.manager = new MDAManager(this); } - m_mdaState.manager->setCameraProvider(m_cameraProvider); - m_mdaState.manager->setStageProvider(m_stageProvider); - m_mdaState.manager->setConfigurationProvider(m_configurationProvider); - m_mdaState.manager->setAcquisitionHardware(m_acquisitionHardware); + m_mdaState.manager->setHardwareRuntime(m_hardwareRuntime); - if (m_cameraRuntimeControl) + if (m_hardwareRuntime) { - m_cameraRuntimeControl->setFrameDeliveryPaused( + m_hardwareRuntime->setFrameDeliveryPaused( m_captureState.activeCameraIds, true); } diff --git a/ScopeOneCore/src/ScopeOneCore.cpp b/ScopeOneCore/src/ScopeOneCore.cpp index 2c3813d..34f7e81 100644 --- a/ScopeOneCore/src/ScopeOneCore.cpp +++ b/ScopeOneCore/src/ScopeOneCore.cpp @@ -7,17 +7,15 @@ #include "internal/ImageProcessingFramework.h" #include "internal/ProcessingModuleRegistry.h" #include "internal/PythonProcessingModule.h" -#include "internal/DriverHostProviderProxy.h" +#include "internal/DriverHostClient.h" #include "internal/HardwareRuntime.h" -#include "internal/MMCoreManager.h" #include "internal/MicroManagerProvider.h" -#include "internal/CameraManager.h" +#include "internal/CameraFrameDelivery.h" #include "internal/ParticleAnalysis.h" #include "internal/RecordingManager.h" #include "internal/StageMosaicManager.h" #include "internal/DaqDeviceManager.h" #include "internal/SignalSourceManager.h" -#include "MMCore.h" #include #include #include @@ -635,7 +633,7 @@ namespace // Translate manager load results into the public facade result scopeone::core::ScopeOneCore::LoadConfigResult toFacadeLoadConfigResult( - const scopeone::core::internal::MMCoreManager::LoadConfigResult& result) + const scopeone::core::internal::MicroManagerProvider::LoadResult& result) { scopeone::core::ScopeOneCore::LoadConfigResult facade; facade.cameraIds = result.cameraIds; @@ -651,7 +649,8 @@ namespace struct ConfigurationTaskResult { bool success{false}; - scopeone::core::internal::MMCoreManager::LoadConfigResult loadResult; + scopeone::core::internal::MicroManagerProvider::LoadResult loadResult; + QStringList failedCameras; QString errorMessage; }; @@ -727,9 +726,7 @@ namespace scopeone::core { using scopeone::core::internal::ImageProcessingManager; using scopeone::core::internal::HardwareRuntime; - using scopeone::core::internal::MMCoreManager; - using scopeone::core::internal::CameraManager; - using scopeone::core::internal::CameraRuntimeControl; + using scopeone::core::internal::MicroManagerCameraSetup; using scopeone::core::internal::MicroManagerProvider; using scopeone::core::internal::ProcessingModule; using scopeone::core::internal::ProcessingPipelineDefinition; @@ -1132,11 +1129,6 @@ namespace scopeone::core }; HardwareRuntime* hardwareRuntime{nullptr}; - CameraProvider* cameraProvider{nullptr}; - CameraRuntimeControl* cameraRuntimeControl{nullptr}; - std::shared_ptr microManagerProvider; - MMCoreManager* mmcoreManager{nullptr}; - CameraManager* cameraManager{nullptr}; RecordingManager* recordingManager{nullptr}; ImageProcessingManager* imageProcessingManager{nullptr}; std::unique_ptr processingModuleRegistry; @@ -1173,7 +1165,7 @@ namespace scopeone::core // Return the unified device catalog owned by the hardware runtime QList ScopeOneCore::hardwareDevices() const { - return m_managers->hardwareRuntime->deviceRegistry()->devices(); + return m_managers->hardwareRuntime->devices(); } bool ScopeOneCore::registerDriverHostProvider(const QString& providerId, @@ -1184,13 +1176,12 @@ namespace scopeone::core if (errorMessage) errorMessage->clear(); const QString normalizedId = providerId.trimmed(); if (normalizedId.isEmpty() - || normalizedId == QStringLiteral("micro-manager") || m_configurationOperationRunning || m_pendingStageCommands > 0 || !m_managers->activeExperimentId.isEmpty() || isRecording() || m_pendingProviderRegistrations.contains(normalizedId) - || m_managers->hardwareRuntime->deviceRegistry()->provider(normalizedId)) + || m_managers->hardwareRuntime->hasProvider(normalizedId)) { if (errorMessage) { @@ -1224,7 +1215,7 @@ namespace scopeone::core [normalizedId, modulePath, providerOptions]() { ProviderRegistrationResult result; - result.provider = internal::createDriverHostProviderProxy( + result.provider = HardwareRuntime::startScopeOneProvider( normalizedId, modulePath, providerOptions, &result.errorMessage); return result; })); @@ -1234,10 +1225,7 @@ namespace scopeone::core // Return the linked MMCore version QString ScopeOneCore::getMMCoreVersion() { - return QStringLiteral("%1.%2.%3") - .arg(CMMCore::getMMCoreVersionMajor()) - .arg(CMMCore::getMMCoreVersionMinor()) - .arg(CMMCore::getMMCoreVersionPatch()); + return HardwareRuntime::microManagerVersion(); } // Return the compiled OpenCV version @@ -1399,15 +1387,7 @@ namespace scopeone::core m_imageSceneModel = new ImageSceneModel(this); connect(m_imageSceneModel, &ImageSceneModel::markupsChanged, this, &ScopeOneCore::syncLineProfileFromScene); - m_managers->mmcoreManager = new MMCoreManager(this); - m_managers->cameraManager = new CameraManager(this); - m_managers->microManagerProvider = - std::make_shared(m_managers->mmcoreManager->getCore(), - m_managers->cameraManager, - m_managers->cameraManager); m_managers->hardwareRuntime = new HardwareRuntime(this); - m_managers->cameraProvider = m_managers->hardwareRuntime; - m_managers->cameraRuntimeControl = m_managers->hardwareRuntime; connect(m_managers->hardwareRuntime, &HardwareRuntime::devicesChanged, this, [this]() { @@ -1416,12 +1396,9 @@ namespace scopeone::core }); connect(m_managers->hardwareRuntime, &HardwareRuntime::previewStateChanged, this, &ScopeOneCore::previewStateChanged); - m_managers->hardwareRuntime->registerProvider(m_managers->microManagerProvider); m_managers->recordingManager = new RecordingManager(this); - m_managers->signalSourceManager = - new internal::SignalSourceManager(m_managers->hardwareRuntime->deviceRegistry(), this); - m_managers->daqDeviceManager = - new internal::DaqDeviceManager(m_managers->hardwareRuntime->deviceRegistry(), this); + m_managers->signalSourceManager = m_managers->hardwareRuntime->signalSourceManager(); + m_managers->daqDeviceManager = m_managers->hardwareRuntime->daqDeviceManager(); connect(m_managers->signalSourceManager, &internal::SignalSourceManager::sourcesChanged, this, &ScopeOneCore::signalSourcesChanged); connect(m_managers->daqDeviceManager, &internal::DaqDeviceManager::devicesChanged, @@ -1430,11 +1407,7 @@ namespace scopeone::core m_managers->stageMosaicManager = new StageMosaicManager(this, this); m_managers->recordingWriterStatus.reset( m_managers->recordingManager->recordedMaxBytes()); - m_managers->recordingManager->setCameraProvider(m_managers->cameraProvider); - m_managers->recordingManager->setStageProvider(m_managers->hardwareRuntime); - m_managers->recordingManager->setConfigurationProvider(m_managers->hardwareRuntime); - m_managers->recordingManager->setAcquisitionHardware(m_managers->hardwareRuntime); - m_managers->recordingManager->setCameraRuntimeControl(m_managers->cameraRuntimeControl); + m_managers->recordingManager->setHardwareRuntime(m_managers->hardwareRuntime); m_managers->recordingManager->setLatestFrameFetcher( [this](const QString& cameraId, ImageFrame& frame) { @@ -1494,37 +1467,23 @@ namespace scopeone::core this, &ScopeOneCore::daqInputDataReady); + internal::CameraFrameDelivery* const frameDelivery = m_managers->hardwareRuntime->frameDelivery(); connect(m_managers->hardwareRuntime, &HardwareRuntime::frameReady, this, &ScopeOneCore::handleIncomingRawFrame); - connect(m_managers->hardwareRuntime, &HardwareRuntime::frameReady, - this, [this](const ImageFrame& frame) - { - const HardwareDeviceDescriptor device = - m_managers->hardwareRuntime->deviceRegistry()->device(frame.cameraId); - if (device.providerId == QStringLiteral("micro-manager")) - { - return; - } - recordLayerFrame(rawLayerKey(frame.cameraId)); - emit rawFramesAcquired(frame.cameraId, 1); - submitProcessingFrame(frame); - m_managers->recordingManager->onRawFramesReady(QList{frame}); - }); - connect(m_managers->cameraManager, &CameraManager::processingFrameReady, + // Keeps processing input on the thread that delivered the frame + connect(frameDelivery, &internal::CameraFrameDelivery::processingFrameReady, this, &ScopeOneCore::submitProcessingFrame, Qt::DirectConnection); - connect(m_managers->cameraManager, &CameraManager::rawFramesAcquired, + connect(frameDelivery, &internal::CameraFrameDelivery::framesAcquired, this, [this](const QString& cameraId, quint64 frameCount) { recordLayerFrame(rawLayerKey(cameraId), frameCount); emit rawFramesAcquired(cameraId, frameCount); }); - connect(m_managers->cameraManager, &CameraManager::recordingFramesReady, + connect(frameDelivery, &internal::CameraFrameDelivery::recordingFramesReady, m_managers->recordingManager, &RecordingManager::onRawFramesReady); - connect(m_managers->cameraManager, &CameraManager::frameDeliveryFailed, + connect(frameDelivery, &internal::CameraFrameDelivery::deliveryFailed, m_managers->recordingManager, &RecordingManager::onFrameDeliveryFailed); - connect(m_managers->cameraManager, &CameraManager::driverHostControlServerListening, - this, &ScopeOneCore::driverHostControlServerListening); connect(m_managers->recordingManager, &RecordingManager::mdaRawFrameReady, this, [this](const ImageFrame& frame) @@ -1665,18 +1624,13 @@ namespace scopeone::core } // Expose the native MMCore handle for low level callers - std::shared_ptr ScopeOneCore::core() const - { - return m_managers->mmcoreManager->getCore(); - } - // Collect camera ids that currently have active previews QStringList ScopeOneCore::runningPreviewCameraIds() const { QStringList running; for (const QString& cameraId : m_cameraIds) { - if (m_managers->cameraProvider->isPreviewRunning(cameraId)) + if (m_managers->hardwareRuntime->isPreviewRunning(cameraId)) { running.append(cameraId); } @@ -1693,16 +1647,7 @@ namespace scopeone::core { return overrideIt.value(); } - const HardwareDeviceDescriptor device = - m_managers->hardwareRuntime->deviceRegistry()->device(camera); - if (device.providerId == QStringLiteral("micro-manager") - && !m_managers->cameraManager->usesDriverHostBackend() - && m_cameraIds.size() == 1 - && m_cameraIds.first() == camera) - { - return core()->getPixelSizeUm(); - } - return 0.0; + return m_managers->hardwareRuntime->pixelSizeUm(camera); } // The scales of the cameras that have one @@ -1742,7 +1687,7 @@ namespace scopeone::core // Return external device adapter directories searched after the application directory QStringList ScopeOneCore::additionalDeviceAdapterSearchPaths() const { - return m_managers->mmcoreManager->additionalDeviceAdapterSearchPaths(); + return m_managers->hardwareRuntime->microManagerAdapterSearchPaths(); } // Set external device adapter directories for subsequent configuration loads @@ -1766,7 +1711,7 @@ namespace scopeone::core normalizedPaths.append(normalizedPath); } } - m_managers->mmcoreManager->setAdditionalDeviceAdapterSearchPaths(normalizedPaths); + m_managers->hardwareRuntime->setMicroManagerAdapterSearchPaths(normalizedPaths); return true; } @@ -1774,11 +1719,9 @@ namespace scopeone::core bool ScopeOneCore::applyLoadedConfiguration(const QString& configPath, const LoadConfigResult& result) { - m_managers->microManagerProvider->setDevices(result.devices); - if (!m_managers->hardwareRuntime->refreshProvider(QStringLiteral("micro-manager"))) + if (!m_managers->hardwareRuntime->setMicroManagerDevices(result.devices)) { - m_managers->microManagerProvider->setDevices({}); - m_managers->hardwareRuntime->refreshProvider(QStringLiteral("micro-manager")); + m_managers->hardwareRuntime->setMicroManagerDevices({}); return false; } m_configurationFailedDevices = result.failedDevices; @@ -1804,7 +1747,7 @@ namespace scopeone::core { QStringList nextCameraIds; for (const HardwareDeviceDescriptor& device - : m_managers->hardwareRuntime->deviceRegistry()->devices()) + : m_managers->hardwareRuntime->devices()) { const QString logicalId = device.logicalId.trimmed(); if (device.kind == HardwareDeviceKind::Camera @@ -1864,52 +1807,28 @@ namespace scopeone::core { applySystemShutdownPreset(); clearConfigurationRuntime(false, true); - auto handle = core(); - try - { - handle->unloadAllDevices(); - } - catch (const CMMError&) - { - } + m_managers->hardwareRuntime->unloadMicroManagerDevices(); m_managers->hardwareRuntime->clear(); } // Apply the configured hardware shutdown state before releasing devices void ScopeOneCore::applySystemShutdownPreset() { - m_managers->hardwareRuntime->stopPreviewForProvider(QStringLiteral("micro-manager")); - auto handle = core(); - try - { - if (!handle->isConfigDefined( - MM::g_CFGGroup_System, MM::g_CFGGroup_System_Shutdown)) - { - return; - } - m_managers->microManagerProvider->setConfig( - MM::g_CFGGroup_System, MM::g_CFGGroup_System_Shutdown, nullptr); - } - catch (const CMMError& error) - { - qWarning().noquote() - << QString("Failed to inspect System/Shutdown preset: %1") - .arg(QString::fromStdString(error.getMsg())); - } + m_managers->hardwareRuntime->applyMicroManagerShutdownPreset(); } // Stops camera runtime and clears state owned by the facade thread - void ScopeOneCore::clearConfigurationRuntime(bool notify, bool shutdownCameraBackend) + void ScopeOneCore::clearConfigurationRuntime(bool notify, bool stopCameras) { ++m_analysisGeneration; m_managers->stageMosaicManager->cancel(); const bool processingWasEnabled = isRealTimeProcessingEnabled(); m_managers->imageProcessingManager->enableRealTimeProcessing(false); const QStringList cameraIds = m_cameraIds; - m_managers->hardwareRuntime->stopPreviewForProvider(QStringLiteral("micro-manager")); - if (shutdownCameraBackend) + m_managers->hardwareRuntime->stopMicroManagerPreview(); + if (stopCameras) { - m_managers->cameraManager->shutdownNow(); + m_managers->hardwareRuntime->takeMicroManagerCameras(); } for (const QString& cameraId : cameraIds) @@ -1928,8 +1847,7 @@ namespace scopeone::core m_latestHistogramStats.clear(); m_activeHistogramLayerKey.clear(); m_imageSceneModel->reset(); - m_managers->microManagerProvider->setDevices(QList{}); - m_managers->hardwareRuntime->refreshProvider(QStringLiteral("micro-manager")); + m_managers->hardwareRuntime->setMicroManagerDevices(QList{}); if (notify && processingWasEnabled) { emit processingSettingsChanged(); @@ -1991,16 +1909,7 @@ namespace scopeone::core applySystemShutdownPreset(); m_configurationOperationRunning = true; clearConfigurationRuntime(true, false); - m_managers->cameraManager->shutdown( - [this, path](const QString& errorMessage) - { - if (!errorMessage.isEmpty()) - { - finishConfigurationLoadFailure({}, errorMessage); - return; - } - startConfigurationLoadTask(path); - }); + stopMicroManagerCameras([this, path]() { startConfigurationLoadTask(path); }); return true; } @@ -2015,14 +1924,30 @@ namespace scopeone::core LoadConfigResult result; if (task.success) { - const bool backendsStarted = m_managers->mmcoreManager->startCameraBackends( - *m_managers->cameraManager, task.loadResult); + const QStringList failedCameras = task.failedCameras; + for (const MicroManagerCameraSetup& camera : std::as_const(task.loadResult.cameras)) + { + const bool started = !failedCameras.contains(camera.cameraId); + for (HardwareDeviceDescriptor& device : task.loadResult.devices) + { + if (device.logicalId == camera.cameraId) + { + device.state = started ? HardwareDeviceState::Initialized : HardwareDeviceState::Faulted; + } + } + if (started) + { + task.loadResult.cameraIds.append(camera.cameraId); + } + } + task.loadResult.failCount += static_cast(failedCameras.size()); + task.loadResult.failedDevices.append(failedCameras); result = toFacadeLoadConfigResult(task.loadResult); - if (!backendsStarted) + if (!failedCameras.isEmpty()) { const QString errorMessage = result.failedDevices.isEmpty() - ? QStringLiteral("Failed to initialize camera backend") - : QStringLiteral("Failed to initialize camera backend: %1") + ? QStringLiteral("Failed to start the cameras") + : QStringLiteral("Failed to start the cameras: %1") .arg(result.failedDevices.join(QStringLiteral(", "))); startConfigurationLoadCleanupTask(result, errorMessage); watcher->deleteLater(); @@ -2048,58 +1973,49 @@ namespace scopeone::core watcher->deleteLater(); }); - auto* manager = m_managers->mmcoreManager; - const auto future = QtConcurrent::run(m_hardwareThreadPool.get(), [manager, path]() + HardwareRuntime* const runtime = m_managers->hardwareRuntime; + const auto future = QtConcurrent::run(m_hardwareThreadPool.get(), [runtime, path]() { ConfigurationTaskResult task; - auto handle = manager->getCore(); - try - { - handle->unloadAllDevices(); - } - catch (const CMMError& error) + task.success = runtime->loadMicroManagerConfiguration(path, task.loadResult, task.errorMessage); + // The DriverHosts of the cameras take seconds to start, which the GUI thread does not wait for + if (task.success) { - task.errorMessage = QString::fromStdString(error.getMsg()); - return task; + task.failedCameras = runtime->startMicroManagerCameras(task.loadResult.cameras); } - - task.success = manager->loadConfigurationDevices( - path, task.loadResult, task.errorMessage); - if (!task.success) + QString cleanupError; + if (!task.success && !runtime->unloadMicroManagerDevices(&cleanupError)) { - try - { - handle->unloadAllDevices(); - } - catch (const CMMError& error) - { - task.errorMessage = QStringLiteral("%1; cleanup failed: %2") - .arg(task.errorMessage, - QString::fromStdString(error.getMsg())); - } + task.errorMessage = QStringLiteral("%1; cleanup failed: %2").arg(task.errorMessage, cleanupError); } return task; }); watcher->setFuture(future); } + void ScopeOneCore::stopMicroManagerCameras(std::function next) + { + auto* watcher = new QFutureWatcher(this); + connect(watcher, &QFutureWatcher::finished, this, [watcher, next = std::move(next)]() + { + watcher->deleteLater(); + next(); + }); + watcher->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), + [cameras = m_managers->hardwareRuntime->takeMicroManagerCameras()]() mutable + { + cameras.clear(); + })); + } + // Stops the cameras and removes the devices after a failed configuration load void ScopeOneCore::startConfigurationLoadCleanupTask(const LoadConfigResult& result, const QString& errorMessage) { - m_managers->cameraManager->shutdown( - [this, result, errorMessage](const QString& shutdownError) - { - if (!shutdownError.isEmpty()) - { - finishConfigurationLoadFailure( - result, - QStringLiteral("%1; camera cleanup failed: %2") - .arg(errorMessage, shutdownError)); - return; - } - startDeviceUnloadAfterFailedLoad(result, errorMessage); - }); + stopMicroManagerCameras([this, result, errorMessage]() + { + startDeviceUnloadAfterFailedLoad(result, errorMessage); + }); } // Removes the devices of a configuration that failed to load @@ -2120,18 +2036,11 @@ namespace scopeone::core watcher->deleteLater(); }); - const auto handle = core(); - watcher->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), [handle]() + HardwareRuntime* const runtime = m_managers->hardwareRuntime; + watcher->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), [runtime]() { StageTaskResult task; - try - { - handle->unloadAllDevices(); - } - catch (const CMMError& error) - { - task.errorMessage = QString::fromStdString(error.getMsg()); - } + runtime->unloadMicroManagerDevices(&task.errorMessage); return task; })); } @@ -2150,20 +2059,7 @@ namespace scopeone::core applySystemShutdownPreset(); m_configurationOperationRunning = true; clearConfigurationRuntime(true, false); - m_managers->cameraManager->shutdown( - [this](const QString& errorMessage) - { - if (!errorMessage.isEmpty()) - { - m_configurationState = ConfigurationState::Failed; - m_configurationError = errorMessage; - m_configurationOperationRunning = false; - emit hardwareConfigurationChanged(); - emit configurationUnloadFinished(false, errorMessage); - return; - } - startConfigurationUnloadTask(); - }); + stopMicroManagerCameras([this]() { startConfigurationUnloadTask(); }); return true; } @@ -2193,19 +2089,11 @@ namespace scopeone::core watcher->deleteLater(); }); - const auto handle = core(); - watcher->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), [handle]() + HardwareRuntime* const runtime = m_managers->hardwareRuntime; + watcher->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), [runtime]() { StageTaskResult task; - try - { - handle->unloadAllDevices(); - task.success = true; - } - catch (const CMMError& error) - { - task.errorMessage = QString::fromStdString(error.getMsg()); - } + task.success = runtime->unloadMicroManagerDevices(&task.errorMessage); return task; })); } @@ -2257,7 +2145,7 @@ namespace scopeone::core { return false; } - const bool ok = m_managers->cameraProvider->setExposure(target, exposureMs); + const bool ok = m_managers->hardwareRuntime->setExposure(target, exposureMs); if (ok) { emit deviceStateChanged(); @@ -2273,7 +2161,7 @@ namespace scopeone::core { return false; } - const bool ok = m_managers->cameraProvider->setROI(target, x, y, width, height); + const bool ok = m_managers->hardwareRuntime->setROI(target, x, y, width, height); if (ok) { clearLiveFrames(target); @@ -2336,7 +2224,7 @@ namespace scopeone::core bool changed = false; for (const QString& cameraId : targets) { - if (!m_managers->cameraProvider->clearROI(cameraId)) + if (!m_managers->hardwareRuntime->clearROI(cameraId)) { ok = false; continue; @@ -2359,7 +2247,7 @@ namespace scopeone::core { return false; } - return m_managers->cameraProvider->getROI(target, x, y, width, height); + return m_managers->hardwareRuntime->getROI(target, x, y, width, height); } // List signal sources discovered through drivers @@ -2712,7 +2600,7 @@ namespace scopeone::core processingToken, [this, cameraId, processingToken]() { - return m_managers->cameraRuntimeControl->isProcessingFrameTokenCurrent( + return m_managers->hardwareRuntime->isProcessingFrameTokenCurrent( cameraId, processingToken); }); @@ -4051,7 +3939,7 @@ namespace scopeone::core return false; } - return m_managers->cameraProvider->getExposure(target, exposureMs); + return m_managers->hardwareRuntime->getExposure(target, exposureMs); } // List devices from the unified registry @@ -4236,7 +4124,7 @@ namespace scopeone::core } if (m_managers->imageProcessingManager->isRealTimeProcessingEnabled() == enabled) { - if (!m_managers->cameraRuntimeControl->setHighRateFrameDeliveryEnabled( + if (!m_managers->hardwareRuntime->setHighRateFrameDeliveryEnabled( m_cameraIds, enabled)) { return false; @@ -4250,7 +4138,7 @@ namespace scopeone::core if (enabled) { m_managers->imageProcessingManager->enableRealTimeProcessing(true); - if (!m_managers->cameraRuntimeControl->setHighRateFrameDeliveryEnabled( + if (!m_managers->hardwareRuntime->setHighRateFrameDeliveryEnabled( m_cameraIds, true)) { m_managers->imageProcessingManager->enableRealTimeProcessing(false); @@ -4259,7 +4147,7 @@ namespace scopeone::core } else { - if (!m_managers->cameraRuntimeControl->setHighRateFrameDeliveryEnabled( + if (!m_managers->hardwareRuntime->setHighRateFrameDeliveryEnabled( m_cameraIds, false)) { return false; diff --git a/ScopeOneCore/src/SignalSourceManager.cpp b/ScopeOneCore/src/SignalSourceManager.cpp index 8e0f4d6..364d96e 100644 --- a/ScopeOneCore/src/SignalSourceManager.cpp +++ b/ScopeOneCore/src/SignalSourceManager.cpp @@ -1,9 +1,9 @@ #include "internal/SignalSourceManager.h" #include "internal/HardwareRuntime.h" -#include "internal/PtuFile.h" #include +#include #include #include @@ -11,18 +11,10 @@ namespace scopeone::core::internal { - SignalSource::SignalSource(QObject* parent) - : QObject(parent) - { - } - - SignalSource::~SignalSource() = default; - SignalSourceManager::SignalSourceManager(DeviceRegistry* registry, QObject* parent) : QObject(parent) , m_registry(registry) { - registerBuiltInSources(); connect(m_registry, &DeviceRegistry::changed, this, &SignalSourceManager::refreshProviderSources); refreshProviderSources(); } @@ -33,12 +25,6 @@ namespace scopeone::core::internal { detachProvider(provider); } - for (const QPointer& source : std::as_const(m_sources)) - { - delete source.data(); - } - m_sources.clear(); - m_factories.clear(); } QList SignalSourceManager::sources() const @@ -65,14 +51,10 @@ namespace scopeone::core::internal } return false; } - if (const HardwareProviderPtr provider = m_providerBySource.value(sourceId)) - { - SignalAcquisitionConfig normalized = config; - normalized.sourceId = sourceId; - return dynamic_cast(provider.get())->startSignal(normalized, errorMessage); - } - SignalSource* source = sourceInstance(sourceId, errorMessage); - return source && source->start(config, errorMessage); + SignalAcquisitionConfig normalized = config; + normalized.sourceId = sourceId; + const HardwareProviderPtr provider = m_providerBySource.value(sourceId); + return dynamic_cast(provider.get())->startSignal(normalized, errorMessage); } void SignalSourceManager::stopTrace(const QString& sourceId) @@ -81,47 +63,17 @@ namespace scopeone::core::internal if (const HardwareProviderPtr provider = m_providerBySource.value(id)) { dynamic_cast(provider.get())->stopSignal(id); - return; - } - if (SignalSource* source = m_sources.value(id)) - { - source->stop(); } } SignalSourceState SignalSourceManager::state(const QString& sourceId) const { - const QString id = sourceId.trimmed(); - const QPointer source = m_sources.value(id); - return source ? source->state() : m_states.value(id, SignalSourceState::Idle); + return m_states.value(sourceId.trimmed(), SignalSourceState::Idle); } QString SignalSourceManager::stateMessage(const QString& sourceId) const { - const QString id = sourceId.trimmed(); - const QPointer source = m_sources.value(id); - return source ? source->stateMessage() - : m_messages.value(id, QStringLiteral("Signal source is idle")); - } - - void SignalSourceManager::registerBuiltInSources() - { - m_factories.push_back(std::make_unique()); - for (const std::unique_ptr& factory : m_factories) - { - for (const SignalSourceDescriptor& descriptor : factory->signalSources()) - { - const QString sourceId = descriptor.id.trimmed(); - if (sourceId.isEmpty() || m_descriptors.contains(sourceId)) - { - continue; - } - m_descriptors.insert(sourceId, descriptor); - m_factoryBySource.insert(sourceId, factory.get()); - m_states.insert(sourceId, SignalSourceState::Idle); - m_messages.insert(sourceId, QStringLiteral("Signal source is idle")); - } - } + return m_messages.value(sourceId.trimmed(), QStringLiteral("Signal source is idle")); } // Follows the drivers as they are registered and removed: the sources of each @@ -231,51 +183,4 @@ namespace scopeone::core::internal emit sourceError(sourceId, message); } } - - SignalSource* SignalSourceManager::sourceInstance(const QString& sourceId, - QString* errorMessage) - { - const QString id = sourceId.trimmed(); - if (SignalSource* existing = m_sources.value(id)) - { - return existing; - } - SignalSourceFactory* factory = m_factoryBySource.value(id); - if (!factory) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Unknown signal source: %1").arg(id); - } - return nullptr; - } - SignalSource* source = factory->createSignalSource(id, this); - if (!source) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Failed to create signal source: %1").arg(id); - } - return nullptr; - } - - connect(source, &SignalSource::timeSeriesReady, - this, &SignalSourceManager::timeSeriesReady); - connect(source, &SignalSource::timestampedEventsReady, - this, &SignalSourceManager::timestampedEventsReady); - connect(source, &SignalSource::stateChanged, - this, [this, id](SignalSourceState state, const QString& message) - { - m_states.insert(id, state); - m_messages.insert(id, message); - emit sourceStateChanged(id, state, message); - }); - connect(source, &SignalSource::sourceError, - this, [this, id](const QString& message) - { - emit sourceError(id, message); - }); - m_sources.insert(id, source); - return source; - } } diff --git a/drivers/ExampleCamera/ExampleCameraDriver.cpp b/drivers/ExampleCamera/ExampleCameraDriver.cpp index 47d7516..f83487a 100644 --- a/drivers/ExampleCamera/ExampleCameraDriver.cpp +++ b/drivers/ExampleCamera/ExampleCameraDriver.cpp @@ -308,26 +308,6 @@ namespace example_camera m_previewStateSink = std::move(sink); } - bool startPreview() override - { - for (auto& camera : m_cameras) - { - camera->start(); - } - reportPreviewState(); - return true; - } - - bool stopPreview() override - { - for (auto& camera : m_cameras) - { - camera->stop(); - } - reportPreviewState(); - return true; - } - bool startPreviewFor(const QString& cameraId) override { Camera* camera = find(cameraId); @@ -358,14 +338,9 @@ namespace example_camera return camera && camera->isRunning(); } - // An id that is not a camera, such as "All", reads the first camera - bool getExposure(const QString& cameraIdOrAll, double& exposureMs) const override + bool getExposure(const QString& cameraId, double& exposureMs) const override { - const Camera* camera = find(cameraIdOrAll); - if (!camera && !m_cameras.empty()) - { - camera = m_cameras.front().get(); - } + const Camera* camera = find(cameraId); if (!camera) { return false; @@ -374,19 +349,10 @@ namespace example_camera return true; } - // An id that is not a camera sets the exposure of every camera - bool setExposure(const QString& cameraIdOrAll, double exposureMs) override + bool setExposure(const QString& cameraId, double exposureMs) override { - if (Camera* camera = find(cameraIdOrAll)) - { - return camera->setExposureMs(exposureMs); - } - bool all = !m_cameras.empty(); - for (auto& camera : m_cameras) - { - all = camera->setExposureMs(exposureMs) && all; - } - return all; + Camera* camera = find(cameraId); + return camera && camera->setExposureMs(exposureMs); } bool setROI(const QString& cameraId, int x, int y, int width, int height) override diff --git a/drivers/ExampleCamera/driver.json b/drivers/ExampleCamera/driver.json index c249bde..94a249f 100644 --- a/drivers/ExampleCamera/driver.json +++ b/drivers/ExampleCamera/driver.json @@ -2,7 +2,7 @@ "id": "example.camera", "name": "Example Camera", "version": "1.0.0", - "scopeOneApi": 1, + "scopeOneApi": 2, "kind": "driver", "providerId": "example.camera", "autoLoad": false diff --git a/drivers/ExamplePmt/driver.json b/drivers/ExamplePmt/driver.json index 104ccbb..62a534a 100644 --- a/drivers/ExamplePmt/driver.json +++ b/drivers/ExamplePmt/driver.json @@ -2,7 +2,7 @@ "id": "example.pmt", "name": "Example PMT", "version": "1.0.0", - "scopeOneApi": 1, + "scopeOneApi": 2, "kind": "driver", "providerId": "example.pmt", "autoLoad": false diff --git a/drivers/NIDaqmx/driver.json b/drivers/NIDaqmx/driver.json index 080b59b..5b0d687 100644 --- a/drivers/NIDaqmx/driver.json +++ b/drivers/NIDaqmx/driver.json @@ -2,7 +2,7 @@ "id": "scopeone.nidaqmx", "name": "NI-DAQmx", "version": "1.0.0", - "scopeOneApi": 1, + "scopeOneApi": 2, "kind": "driver", "providerId": "scopeone.nidaqmx", "autoLoad": true diff --git a/drivers/README.md b/drivers/README.md index b430cd6..a23c21f 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -62,7 +62,7 @@ the drivers into `ScopeOneCore/install/bin/drivers`, from where the GUI build co "id": "my.camera", "name": "My Camera", "version": "1.0.0", - "scopeOneApi": 1, + "scopeOneApi": 2, "kind": "driver", "providerId": "my.camera", "autoLoad": false From 6c670a635c5c43ef3f38d05af11de5a3340753f4 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:24:37 +0100 Subject: [PATCH 28/49] Fix the review findings and share frames through POSIX memory whose names DriverHost removes once ScopeOne has attached --- ScopeOneCore/DRIVERHOST_PROTOCOL.md | 12 +++-- ScopeOneCore/internal/DriverHostProtocol.h | 40 ++++++++--------- ScopeOneCore/internal/StreamRing.h | 4 +- ScopeOneCore/src/DriverHostClient.cpp | 37 +++++++++++++--- ScopeOneCore/src/DriverHostMain.cpp | 51 ++++++++++++++++++++-- ScopeOneCore/src/StreamRing.cpp | 10 ++--- 6 files changed, 115 insertions(+), 39 deletions(-) diff --git a/ScopeOneCore/DRIVERHOST_PROTOCOL.md b/ScopeOneCore/DRIVERHOST_PROTOCOL.md index f935ba6..87c3dc8 100644 --- a/ScopeOneCore/DRIVERHOST_PROTOCOL.md +++ b/ScopeOneCore/DRIVERHOST_PROTOCOL.md @@ -5,7 +5,7 @@ than one camera, through a `ScopeOne_DriverHost` process. This file describes wh process boundary: control messages on a local socket, camera frames in shared memory, and the chunks of signal sources and DAQ devices in stream rings. Both ends are part of ScopeOne, so the protocol is internal and changes with it; drivers are written against `scopeone::DriverSDK` instead. It is version -7, `kProtocolVersion` in `ScopeOneCore/internal/DriverHostProtocol.h`, which holds the names used +8, `kProtocolVersion` in `ScopeOneCore/internal/DriverHostProtocol.h`, which holds the names used below. ## Process @@ -81,6 +81,7 @@ capability the device lacks is answered with `ok` false. |---|---|---|---| | `Camera` | `StartPreview`, `StopPreview` | | | | | `SetFrameDeliveryMode` | `mode`: `PreviewLatest`, `LatestOnly` or `AllFrames` | `mode` | +| | `ReleaseSharedMemory`, provider wide | `keys`, the rings ScopeOne has attached; the host removes their names | | | | `SetFrameDeliveryPaused` | `paused`; the host writes no frames while paused, and a Micro-Manager camera starts again the live acquisition an event capture stopped | | | | `GetExposure` | | `exposureMs` | | | `SetExposure` | `value` in ms | `exposureMs`, the value the camera took | @@ -135,9 +136,12 @@ capability the device lacks is answered with `ok` false. ## Camera frames -Each camera has one ring in shared memory, created by the host under `sharedMemoryKey`. On Windows -the key is the name of a file mapping. On Linux and macOS it is the path of a file in the user's -runtime directory, which `QSharedMemory` turns into a System V key. +Each camera has one ring in shared memory, created by the host under `sharedMemoryKey`. Both ends +turn the key into a native name with `QSharedMemory::platformSafeKey`: a file mapping on Windows, +and POSIX shared memory on Linux and macOS, as System V memory holds at most 4 MB in one block on +macOS. ScopeOne attaches the camera rings right after `Describe` and a stream ring right after the +response that names it, and then sends `ReleaseSharedMemory`. The host removes the names, so the +memory goes away with the last process that maps it, also after a crash. The ring is a 64-byte control block followed by 32 slots. A slot is a 64-byte header and room for `4 * 2048 * 2048` bytes of pixels. All fields are little-endian. diff --git a/ScopeOneCore/internal/DriverHostProtocol.h b/ScopeOneCore/internal/DriverHostProtocol.h index ca53968..532486e 100644 --- a/ScopeOneCore/internal/DriverHostProtocol.h +++ b/ScopeOneCore/internal/DriverHostProtocol.h @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include #include @@ -17,7 +17,7 @@ namespace scopeone::core::internal::driverhost { - inline constexpr quint32 kProtocolVersion = 7; + inline constexpr quint32 kProtocolVersion = 8; // The names of the device kinds as the protocol sends them inline constexpr std::pair kDeviceKindNames[]{ @@ -106,6 +106,10 @@ namespace scopeone::core::internal::driverhost inline const QString kCommandStopPreview = QStringLiteral("StopPreview"); inline const QString kCommandSetFrameDeliveryMode = QStringLiteral("SetFrameDeliveryMode"); inline const QString kCommandSetFrameDeliveryPaused = QStringLiteral("SetFrameDeliveryPaused"); + // ScopeOne has attached the rings named in "keys", so DriverHost removes their names. The + // memory stays mapped on both ends and goes away with the last of them, even after a crash. + inline const QString kCommandReleaseSharedMemory = QStringLiteral("ReleaseSharedMemory"); + inline const QString kKeysField = QStringLiteral("keys"); inline const QString kCommandGetExposure = QStringLiteral("GetExposure"); inline const QString kCommandSetExposure = QStringLiteral("SetExposure"); inline const QString kCommandListProperties = QStringLiteral("ListProperties"); @@ -187,31 +191,27 @@ namespace scopeone::core::internal::driverhost return QStringLiteral("ScopeOne.%1.ctrl").arg(QString::fromLatin1(hash)); } - // On Unix the System V key is a file that ftok reads, so keep it in the - // per-user runtime directory where other users cannot collide with it - inline QString sharedMemoryKeyPath(const QString& name) - { -#ifdef Q_OS_WIN - return name; -#else - return QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) - + QLatin1Char('/') + name; -#endif - } - inline QString sharedMemoryKey(const QString& hostKey, int cameraIndex) { - return sharedMemoryKeyPath(QStringLiteral("ScopeOne.DriverHost.%1.%2.shm") - .arg(hostKey) - .arg(cameraIndex)); + return QStringLiteral("ScopeOne.%1.%2").arg(hostKey).arg(cameraIndex); } // Each start of a stream gets a ring of its own, sized for what that acquisition delivers inline QString streamSharedMemoryKey(const QString& hostKey, quint64 streamIndex) { - return sharedMemoryKeyPath(QStringLiteral("ScopeOne.DriverHost.%1.stream%2.shm") - .arg(hostKey) - .arg(streamIndex)); + return QStringLiteral("ScopeOne.%1.stream%2").arg(hostKey).arg(streamIndex); + } + + // POSIX shared memory on Linux and macOS. The System V memory that Qt uses by default holds + // at most 4 MB in one block on macOS, too little for a frame ring. Qt makes the name fit the + // limits of the platform, 31 characters on macOS. + inline QNativeIpcKey sharedMemoryNativeKey(const QString& key) + { +#ifdef Q_OS_WIN + return QSharedMemory::platformSafeKey(key, QNativeIpcKey::Type::Windows); +#else + return QSharedMemory::platformSafeKey(key, QNativeIpcKey::Type::PosixRealtime); +#endif } inline QString encodeUInt64(quint64 value) diff --git a/ScopeOneCore/internal/StreamRing.h b/ScopeOneCore/internal/StreamRing.h index 3653e66..e2edf05 100644 --- a/ScopeOneCore/internal/StreamRing.h +++ b/ScopeOneCore/internal/StreamRing.h @@ -41,12 +41,12 @@ namespace scopeone::core::internal::streamring // Creates the shared memory of a ring of the given capacity under the native key, dropping // a ring of that key a crashed DriverHost left behind bool createSharedRing(QSharedMemory& shm, - const QString& nativeKey, + const QNativeIpcKey& nativeKey, qsizetype capacity, QString* errorMessage); // Attaches the shared memory of a ring another process created - bool attachSharedRing(QSharedMemory& shm, const QString& nativeKey, QString* errorMessage); + bool attachSharedRing(QSharedMemory& shm, const QNativeIpcKey& nativeKey, QString* errorMessage); class Writer { diff --git a/ScopeOneCore/src/DriverHostClient.cpp b/ScopeOneCore/src/DriverHostClient.cpp index 8c80edc..a91cfeb 100644 --- a/ScopeOneCore/src/DriverHostClient.cpp +++ b/ScopeOneCore/src/DriverHostClient.cpp @@ -279,8 +279,7 @@ namespace scopeone::core::internal auto reader = std::make_shared(); reader->cameraId = device.logicalId; reader->shmKey = shmKey; - reader->shm = std::make_unique(); - reader->shm->setNativeKey(shmKey); + reader->shm = std::make_unique(driverhost::sharedMemoryNativeKey(shmKey)); m_readers.insert(device.logicalId, std::move(reader)); m_cameraIds.append(device.logicalId); } @@ -293,11 +292,39 @@ namespace scopeone::core::internal { daqDevices.append(hardwarestream::daqDeviceDescriptorFromJson(value.toObject())); } + // The camera rings are attached now, so DriverHost can remove their names + QStringList keys; + for (const auto& reader : std::as_const(m_readers)) + { + if (!ensureSharedMemory(*reader)) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("Cannot attach the frame ring of '%1'").arg(reader->cameraId); + } + cleanup(); + return false; + } + keys.append(reader->shmKey); + } + releaseSharedMemory(keys); description = response; if (errorMessage) errorMessage->clear(); return true; } + void releaseSharedMemory(const QStringList& keys) + { + if (keys.isEmpty()) + { + return; + } + QJsonObject request{{driverhost::kMessageTypeField, driverhost::kCommandReleaseSharedMemory}, + {driverhost::kKeysField, QJsonArray::fromStringList(keys)}}; + QJsonObject response; + sendRequest(request, response, kControlRequestTimeoutMs, nullptr); + } + // The images wait in DriverHost and come over a part at a time, as the frame ring holds // fewer images than a sequence can have bool captureEventSequence(const QString& cameraId, @@ -362,13 +389,13 @@ namespace scopeone::core::internal : config.value(QStringLiteral("deviceId")).toString(); auto stream = std::make_shared(); stream->streamId = response.value(driverhost::kStreamIdField).toString(); - if (!streamring::attachSharedRing(stream->shm, - response.value(driverhost::kSharedMemoryKeyField).toString(), - errorMessage)) + const QString shmKey = response.value(driverhost::kSharedMemoryKeyField).toString(); + if (!streamring::attachSharedRing(stream->shm, driverhost::sharedMemoryNativeKey(shmKey), errorMessage)) { stopStream(signal ? driverhost::kCommandStopSignal : driverhost::kCommandStopDaq, id, nullptr); return false; } + releaseSharedMemory({shmKey}); stream->reader = streamring::Reader(stream->shm.data()); m_streams.insert(stream->streamId, stream); (signal ? m_activeSignals : m_activeDaqDevices).insert(id); diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index a9d0724..b1967df 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #ifdef Q_OS_MACOS @@ -269,6 +271,7 @@ namespace scopeone::core::internal std::shared_ptr cameraTransport(const QString& deviceId) const; bool createSharedMemory(CameraTransport& transport); void writeCameraFrame(const ImageFrame& frame); + void releaseSharedMemoryName(const QString& key); void sendPendingNotice(const QString& deviceId); bool writeFrame(CameraTransport& transport, const ImageFrame& frame, @@ -561,7 +564,8 @@ namespace scopeone::core::internal auto stream = std::make_shared(); stream->streamId = streamId; stream->shmKey = driverhost::streamSharedMemoryKey(m_hostKey, m_nextStreamIndex++); - if (!streamring::createSharedRing(stream->shm, stream->shmKey, capacity, errorMessage)) + if (!streamring::createSharedRing(stream->shm, driverhost::sharedMemoryNativeKey(stream->shmKey), + capacity, errorMessage)) { return {}; } @@ -837,8 +841,7 @@ namespace scopeone::core::internal bool DriverRuntime::createSharedMemory(CameraTransport& transport) { - transport.shm = std::make_unique(); - transport.shm->setNativeKey(transport.shmKey); + transport.shm = std::make_unique(driverhost::sharedMemoryNativeKey(transport.shmKey)); return sharedframe::createRing(*transport.shm, &m_lastError); } @@ -892,6 +895,39 @@ namespace scopeone::core::internal } } + // Removes the name of a ring of this host. Qt never removes the name of POSIX shared memory, + // which then outlived a crash of both processes; removed once ScopeOne has attached, it goes + // away with the last mapping. + void DriverRuntime::releaseSharedMemoryName(const QString& key) + { +#ifndef Q_OS_WIN + bool owned = false; + { + QReadLocker locker(&m_cameraTransportsLock); + for (const auto& transport : std::as_const(m_cameraTransports)) + { + owned = owned || transport->shmKey == key; + } + } + { + QMutexLocker locker(&m_streamsMutex); + for (const auto& stream : std::as_const(m_streams)) + { + owned = owned || stream->shmKey == key; + } + } + if (owned) + { + // Qt guards the memory with a POSIX semaphore of the same name, whose name stays too + const QByteArray name = driverhost::sharedMemoryNativeKey(key).nativeKey().toLocal8Bit(); + ::shm_unlink(name.constData()); + ::sem_unlink(name.constData()); + } +#else + (void)key; +#endif + } + void DriverRuntime::sendPendingNotice(const QString& deviceId) { const auto transport = cameraTransport(deviceId); @@ -1021,6 +1057,15 @@ namespace scopeone::core::internal reply(response); return; } + if (type == driverhost::kCommandReleaseSharedMemory) + { + for (const QJsonValue& value : message.value(driverhost::kKeysField).toArray()) + { + releaseSharedMemoryName(value.toString()); + } + reply(makeResponse(type, requestId, true)); + return; + } if (type == driverhost::kCommandShutdown) { stopForExit(); diff --git a/ScopeOneCore/src/StreamRing.cpp b/ScopeOneCore/src/StreamRing.cpp index bacbf2f..0ed5640 100644 --- a/ScopeOneCore/src/StreamRing.cpp +++ b/ScopeOneCore/src/StreamRing.cpp @@ -69,7 +69,7 @@ namespace scopeone::core::internal::streamring } bool createSharedRing(QSharedMemory& shm, - const QString& nativeKey, + const QNativeIpcKey& nativeKey, qsizetype capacity, QString* errorMessage) { @@ -86,7 +86,7 @@ namespace scopeone::core::internal::streamring if (errorMessage) { *errorMessage = QStringLiteral("Cannot create shared memory '%1': %2") - .arg(nativeKey, shm.errorString()); + .arg(nativeKey.nativeKey(), shm.errorString()); } return false; } @@ -95,7 +95,7 @@ namespace scopeone::core::internal::streamring return true; } - bool attachSharedRing(QSharedMemory& shm, const QString& nativeKey, QString* errorMessage) + bool attachSharedRing(QSharedMemory& shm, const QNativeIpcKey& nativeKey, QString* errorMessage) { shm.setNativeKey(nativeKey); if (!shm.attach(QSharedMemory::ReadWrite)) @@ -103,7 +103,7 @@ namespace scopeone::core::internal::streamring if (errorMessage) { *errorMessage = QStringLiteral("Cannot attach shared memory '%1': %2") - .arg(nativeKey, shm.errorString()); + .arg(nativeKey.nativeKey(), shm.errorString()); } return false; } @@ -115,7 +115,7 @@ namespace scopeone::core::internal::streamring shm.detach(); if (errorMessage) { - *errorMessage = QStringLiteral("Shared memory '%1' is not a stream ring").arg(nativeKey); + *errorMessage = QStringLiteral("Shared memory '%1' is not a stream ring").arg(nativeKey.nativeKey()); } return false; } From 9002e9d9da9e4783331d8f0aa96ef6f659dea46a Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:38:07 +0100 Subject: [PATCH 29/49] Include the system semaphore header on macOS, where semaphore.h found the Semaphore.h of MMCore --- ScopeOneCore/src/DriverHostMain.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index b1967df..a876d33 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -29,7 +29,13 @@ #include #include #include +#ifdef Q_OS_MACOS +// The file system of macOS ignores case, so found the Semaphore.h of MMCore on the +// include path, and sem_unlink was undeclared. The system header declares it here. +#include +#else #include +#endif #include #include #include @@ -40,6 +46,12 @@ #endif #endif +#ifndef Q_OS_WIN +// The macOS SDK declares shm_unlink only outside strict POSIX mode, and the build of DriverHost +// failed there on it. libc has it on every Unix, so it is declared here as the SDK does. +extern "C" int shm_unlink(const char* name); +#endif + #include #include #include From 2c781a3ffbe3fab2085f9881bc7464bdde1e946e Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:46:52 +0100 Subject: [PATCH 30/49] Keep shared memory names within the 30 characters Qt allows on macOS, which gave two cameras of one host the same name --- ScopeOneCore/internal/DriverHostProtocol.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/ScopeOneCore/internal/DriverHostProtocol.h b/ScopeOneCore/internal/DriverHostProtocol.h index 532486e..00b67f1 100644 --- a/ScopeOneCore/internal/DriverHostProtocol.h +++ b/ScopeOneCore/internal/DriverHostProtocol.h @@ -181,25 +181,30 @@ namespace scopeone::core::internal::driverhost inline const QString kExecutableFileName = QStringLiteral("ScopeOne_DriverHost"); #endif - // A unix socket path holds at most 104 characters on macOS, where the temporary folder takes - // about half of them, and a name with the 36 character host key made the server fail to - // listen. The key is hashed to a name of fixed length. + // The names of a host on macOS have to be short: a unix socket path holds at most 104 + // characters, with the temporary folder taking about half, and Qt cuts a POSIX shared memory + // name to 30, which gave two cameras of one host the same name. The host key is hashed to 16 + // characters for them. + inline QString hostHash(const QString& hostKey) + { + return QString::fromLatin1( + QCryptographicHash::hash(hostKey.toUtf8(), QCryptographicHash::Sha1).toHex().left(16)); + } + inline QString controlServerName(const QString& hostKey) { - const QByteArray hash = - QCryptographicHash::hash(hostKey.toUtf8(), QCryptographicHash::Sha1).toHex().left(16); - return QStringLiteral("ScopeOne.%1.ctrl").arg(QString::fromLatin1(hash)); + return QStringLiteral("ScopeOne.%1.ctrl").arg(hostHash(hostKey)); } inline QString sharedMemoryKey(const QString& hostKey, int cameraIndex) { - return QStringLiteral("ScopeOne.%1.%2").arg(hostKey).arg(cameraIndex); + return QStringLiteral("so.%1.c%2").arg(hostHash(hostKey)).arg(cameraIndex); } // Each start of a stream gets a ring of its own, sized for what that acquisition delivers inline QString streamSharedMemoryKey(const QString& hostKey, quint64 streamIndex) { - return QStringLiteral("ScopeOne.%1.stream%2").arg(hostKey).arg(streamIndex); + return QStringLiteral("so.%1.s%2").arg(hostHash(hostKey)).arg(streamIndex); } // POSIX shared memory on Linux and macOS. The System V memory that Qt uses by default holds From 450e45e0e8f043c9d0c94c8c7f76496c98389d49 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:54:13 +0100 Subject: [PATCH 31/49] Make a new Local API frame memory on start, as macOS cannot resize the one a crashed ScopeOne left behind --- src/ScopeOneLocalApiServer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ScopeOneLocalApiServer.cpp b/src/ScopeOneLocalApiServer.cpp index 792a99b..dd0cc60 100644 --- a/src/ScopeOneLocalApiServer.cpp +++ b/src/ScopeOneLocalApiServer.cpp @@ -1197,7 +1197,10 @@ namespace scopeone::ui #else const size_t mappingSize = static_cast( scopeone::core::kSharedFrameHeaderSize + scopeone::core::kSharedFrameMaxBytes); - m_frameShmFd = ::shm_open(kPosixFrameShmName, O_CREAT | O_RDWR, 0600); + // A ScopeOne that crashed left its frame memory behind, and macOS sizes a POSIX shared + // memory object only once, so resizing the old one failed. A new one is made instead. + ::shm_unlink(kPosixFrameShmName); + m_frameShmFd = ::shm_open(kPosixFrameShmName, O_CREAT | O_EXCL | O_RDWR, 0600); if (m_frameShmFd < 0) { qWarning().noquote() << QStringLiteral("ScopeOne API frame shm create failed"); From d068dd5b9ce02f58d64a2cb6a46c6e9cccee397c Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:10:04 +0100 Subject: [PATCH 32/49] Run autogen.sh and configure of Micro-Manager only on the first build and with --clean --- scripts/build.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/build.sh b/scripts/build.sh index 9698587..b0bb5bd 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -235,8 +235,12 @@ step "Building required Micro-Manager components" ( cd "$MM_REPO_DIR" run git submodule update --init --recursive - run ./autogen.sh - run ./configure --without-java --enable-static + # autogen.sh and configure took most of an incremental build, so they run only on the first + # build and with --clean + if [[ "$CLEAN" == true || ! -f config.status ]]; then + run ./autogen.sh + run ./configure --without-java --enable-static + fi run make -C "$MMCORE_LINK/MMDevice" -j "$JOBS" run make -C "$MMCORE_LINK/MMCore" -j "$JOBS" run make -C "$MMCORE_LINK/DeviceAdapters/DemoCamera" -j "$JOBS" From c2753ec04fbbf8f1595f5dc780180586e5bfad5e Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:10:04 +0100 Subject: [PATCH 33/49] Draw the grid view labels in the system UI font instead of Segoe UI, which macOS lacks --- src/PreviewWidget.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/PreviewWidget.cpp b/src/PreviewWidget.cpp index 1208563..a348905 100644 --- a/src/PreviewWidget.cpp +++ b/src/PreviewWidget.cpp @@ -1902,7 +1902,10 @@ namespace scopeone::ui painter.save(); painter.setRenderHint(QPainter::Antialiasing); - const QFont font(QStringLiteral("Segoe UI"), 9); + // A named "Segoe UI" is missing on macOS, where Qt scanned every font family alias before + // falling back, so the labels take the system UI font like the scale bar + QFont font = painter.font(); + font.setPointSize(9); painter.setFont(font); const QFontMetrics fm(font); const bool overlay = m_layerLayoutMode == LayerLayoutMode::Overlay From 1ddc5010a341a3c17a914fa235856366906761c8 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 17:05:11 +0100 Subject: [PATCH 34/49] Model drivers as devices created by name with pre-init properties, and name the SDK interfaces after devices --- ScopeOneCore/CMakeLists.txt | 8 +- ScopeOneCore/DRIVERHOST_PROTOCOL.md | 9 +- ScopeOneCore/README.md | 8 +- ScopeOneCore/include/scopeone/ConfigSetting.h | 14 ++ .../include/scopeone/DriverManifest.h | 2 +- .../include/scopeone/ExperimentDocument.h | 2 +- ScopeOneCore/include/scopeone/ScopeOneCore.h | 2 +- ScopeOneCore/internal/AcquisitionEngine.h | 2 +- ScopeOneCore/internal/ConfigurationProvider.h | 36 +++ ScopeOneCore/internal/DaqDeviceManager.h | 10 +- ScopeOneCore/internal/DriverHostClient.h | 19 +- ScopeOneCore/internal/DriverHostProtocol.h | 6 - ScopeOneCore/internal/HardwareRuntime.h | 36 +-- ScopeOneCore/internal/MicroManagerCamera.h | 6 +- ScopeOneCore/internal/MicroManagerProvider.h | 38 ++-- ScopeOneCore/internal/SignalSourceManager.h | 10 +- .../scopeone/{CameraProvider.h => Camera.h} | 18 +- ScopeOneCore/sdk/include/scopeone/DaqDevice.h | 23 ++ .../sdk/include/scopeone/DaqProvider.h | 33 --- ScopeOneCore/sdk/include/scopeone/Device.h | 134 ++++++++++++ ScopeOneCore/sdk/include/scopeone/Driver.h | 18 +- ScopeOneCore/sdk/include/scopeone/DriverSDK.h | 8 +- .../include/scopeone/HardwareCapabilities.h | 103 +-------- .../sdk/include/scopeone/HardwareProvider.h | 23 -- .../sdk/include/scopeone/SignalSource.h | 26 +++ .../include/scopeone/SignalSourceProvider.h | 36 --- ScopeOneCore/sdk/src/Device.cpp | 24 ++ ScopeOneCore/sdk/src/HardwareProvider.cpp | 19 -- ScopeOneCore/src/AcquisitionEngine.cpp | 20 +- ScopeOneCore/src/DaqDeviceManager.cpp | 22 +- ScopeOneCore/src/DriverHostClient.cpp | 150 +++---------- ScopeOneCore/src/DriverHostMain.cpp | 186 ++++++---------- ScopeOneCore/src/HardwareRuntime.cpp | 207 +++++++++--------- ScopeOneCore/src/MDAManager.cpp | 2 +- ScopeOneCore/src/MicroManagerProvider.cpp | 64 +++--- ScopeOneCore/src/ScopeOneCore.cpp | 7 +- ScopeOneCore/src/SignalSourceManager.cpp | 22 +- drivers/ExampleCamera/ExampleCameraDriver.cpp | 79 ++++--- drivers/ExampleCamera/driver.json | 2 +- drivers/ExamplePmt/ExamplePmtDriver.cpp | 34 +-- drivers/ExamplePmt/driver.json | 2 +- drivers/NIDaqmx/NIDaqmxDriver.cpp | 33 ++- drivers/NIDaqmx/driver.json | 2 +- drivers/README.md | 17 +- 44 files changed, 737 insertions(+), 785 deletions(-) create mode 100644 ScopeOneCore/include/scopeone/ConfigSetting.h create mode 100644 ScopeOneCore/internal/ConfigurationProvider.h rename ScopeOneCore/sdk/include/scopeone/{CameraProvider.h => Camera.h} (87%) delete mode 100644 ScopeOneCore/sdk/include/scopeone/DaqProvider.h create mode 100644 ScopeOneCore/sdk/include/scopeone/Device.h delete mode 100644 ScopeOneCore/sdk/include/scopeone/HardwareProvider.h delete mode 100644 ScopeOneCore/sdk/include/scopeone/SignalSourceProvider.h create mode 100644 ScopeOneCore/sdk/src/Device.cpp delete mode 100644 ScopeOneCore/sdk/src/HardwareProvider.cpp diff --git a/ScopeOneCore/CMakeLists.txt b/ScopeOneCore/CMakeLists.txt index ec161fa..7172c04 100644 --- a/ScopeOneCore/CMakeLists.txt +++ b/ScopeOneCore/CMakeLists.txt @@ -78,21 +78,19 @@ file(GLOB CORE_HEADERS CONFIGURE_DEPENDS internal/*.h include/scopeone/*.h) # The driver SDK: the only headers and the only library a driver sees. It is a library of its # own so a driver that reaches for anything else of Core does not compile or link. set(SDK_HEADERS - sdk/include/scopeone/CameraProvider.h + sdk/include/scopeone/Camera.h sdk/include/scopeone/DaqDevice.h - sdk/include/scopeone/DaqProvider.h sdk/include/scopeone/Driver.h sdk/include/scopeone/HardwareCapabilities.h sdk/include/scopeone/DriverSDK.h - sdk/include/scopeone/HardwareProvider.h + sdk/include/scopeone/Device.h sdk/include/scopeone/HardwareTypes.h sdk/include/scopeone/ImageFrame.h sdk/include/scopeone/scopeone_sdk_export.h sdk/include/scopeone/SignalSource.h - sdk/include/scopeone/SignalSourceProvider.h ) add_library(ScopeOneDriverSDK SHARED - sdk/src/HardwareProvider.cpp + sdk/src/Device.cpp sdk/src/SignalSource.cpp ${SDK_HEADERS} ) diff --git a/ScopeOneCore/DRIVERHOST_PROTOCOL.md b/ScopeOneCore/DRIVERHOST_PROTOCOL.md index 87c3dc8..542cd60 100644 --- a/ScopeOneCore/DRIVERHOST_PROTOCOL.md +++ b/ScopeOneCore/DRIVERHOST_PROTOCOL.md @@ -15,12 +15,12 @@ with more than one camera: ```text ScopeOne_DriverHost --provider --driver --hostKey - --launcherPid [--option ] + --launcherPid [--device ] [--properties ] ScopeOne_DriverHost --provider micro-manager --micromanagerCamera --hostKey --launcherPid ``` -`--option` carries the options from the Drivers dialog. `--micromanagerCamera` carries +`--device` names the device to create in the driver, the first of its `deviceNames()` by default, and `--properties` carries its pre-init properties. `--micromanagerCamera` carries `cameraId`, `adapter`, `device`, `exposureMs`, and `preInitProperties` and `properties`, arrays of `{"name", "value"}` objects. The host loads the camera on a core of its own, as two cameras of the same model in one process would share the state of one vendor library. The host loads the @@ -116,11 +116,6 @@ capability the device lacks is answered with `ok` false. | | `StopSignal` | `sourceId` | | | `Daq` | `StartDaq` | `config`, a `DaqSessionConfig` with its raster scans expanded | `streamId`, `sharedMemoryKey` | | | `StopDaq` | `deviceId` | | -| `Configuration` | `ListConfigGroups` | | `groups` | -| | `ListConfigs` | `group` | `configs` | -| | `GetCurrentConfig` | `group` | `config` | -| | `SetConfig` | `group`, `config` | | -| | `GetConfigSettings` | `group`, `config` | `settings`, an array of `device`, `property`, `value` | ## Events diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index c68eda4..30ff5a3 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -26,7 +26,7 @@ ScopeOneCore/ #include ``` -`sdk/include/scopeone` contains the driver contract and the types it carries, such as `Driver`, `HardwareProvider`, `CameraProvider`, `SignalSourceProvider`, `DaqProvider` and `ImageFrame`. These headers keep the `scopeone/` include prefix. They build the `ScopeOneDriverSDK` library (`scopeone::DriverSDK`), which Core links publicly. A driver links only that target, so it sees neither the other Core headers nor the Core library, and on Linux it is linked with `--no-undefined`. An SDK header includes only Qt and other SDK headers. +`sdk/include/scopeone` contains the driver contract and the types it carries, such as `Driver`, `Device`, `Hub`, `Camera`, `SignalSource`, `Daq` and `ImageFrame`. These headers keep the `scopeone/` include prefix. They build the `ScopeOneDriverSDK` library (`scopeone::DriverSDK`), which Core links publicly. A driver links only that target, so it sees neither the other Core headers nor the Core library, and on Linux it is linked with `--no-undefined`. An SDK header includes only Qt and other SDK headers. `internal` contains implementation contracts between Core managers, processing modules and DriverHost. These headers are available to the `ScopeOneCore` target through a private include path, are not installed, and may change without preserving source compatibility. Code outside `ScopeOneCore` must not include them. @@ -47,7 +47,7 @@ Use this placement rule: | Namespace | Purpose | Examples | |---|---|---| | `scopeone::core` | Stable Core-facing types and public facades | `ScopeOneCore`, `ExperimentDocument`, `ImageSceneModel` | -| `scopeone::core` driver contract | Stable types of `DriverSDK.h` | `Driver`, `HardwareProvider`, `CameraProvider`, `SignalSourceProvider`, `DaqProvider`, `ImageFrame` | +| `scopeone::core` driver contract | Stable types of `DriverSDK.h` | `Driver`, `Device`, `Hub`, `Camera`, `SignalSource`, `Daq`, `ImageFrame` | | `scopeone::core::internal` | Core-only managers, processing modules and built-in devices | `HardwareRuntime`, `MicroManagerProvider`, `RecordingManager`, `IscatModule`, `SignalSourceManager` | | `scopeone::core::internal::driverhost` | Shared DriverHost message framing | Versioned request, response and event envelopes | | `scopeone::ui` | Desktop application widgets and UI coordination outside this library | `MainWindow`, `PreviewWidget`, `DhmTool`, `ToolRegistry` | @@ -108,14 +108,14 @@ The installed headers in `include/scopeone` and `include/sdk/scopeone` are the s - `ScopeOneCore.h` provides the main hardware, acquisition, processing, recording and frame-graph facade. - `ProcessingPipeline.h`, `ExperimentDocument.h` and `ImageSceneModel.h` are Core-owned runtime models and services. -- `DriverSDK.h` includes the headers of the public driver contract: `CameraProvider.h`, `DaqDevice.h`, `DaqProvider.h`, `Driver.h`, `HardwareCapabilities.h`, `HardwareProvider.h`, `HardwareTypes.h`, `ImageFrame.h`, `SignalSource.h` and `SignalSourceProvider.h`. The `scopeone::DriverSDK` CMake target provides them. +- `DriverSDK.h` includes the headers of the public driver contract: `Camera.h`, `DaqDevice.h`, `Device.h`, `Driver.h`, `HardwareCapabilities.h`, `HardwareTypes.h`, `ImageFrame.h` and `SignalSource.h`. The `scopeone::DriverSDK` CMake target provides them. - `ProcessingTypes.h` holds the processing module base class and the descriptors of modules and their parameters. - `DriverManifest.h` is how ScopeOne discovers and enables drivers. `SharedFrame.h` and `DhmReconstruction.h` serve the application. None of them are driver contracts. - `scopeone_core_export.h` supplies DLL import and export declarations and is normally included indirectly. External code should enter through these headers and `scopeone::core::ScopeOneCore`. Internal managers are implementation details and must not become alternate access paths. -Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `CameraProvider::FrameSink`. The sink copies a frame before it returns, so a frame can point into the buffer of the camera SDK through `QByteArray::fromRawData` instead of being copied by the driver first. Core loads a driver with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider. It runs the camera of a configuration with one camera in ScopeOne, and each camera of a configuration with more in a DriverHost of its own, so two cameras of the same model do not share one vendor library. +Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `Camera::FrameSink`. The sink copies a frame before it returns, so a frame can point into the buffer of the camera SDK through `QByteArray::fromRawData` instead of being copied by the driver first. Core loads a driver with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider. It runs the camera of a configuration with one camera in ScopeOne, and each camera of a configuration with more in a DriverHost of its own, so two cameras of the same model do not share one vendor library. Inside Core, the rest of Core reaches hardware only through `HardwareRuntime`. It routes each call to the provider of the device and answers the calls that span providers itself, such as all cameras, the default stages and shutter, and the frame delivery to the live view, live processing and recording. Every provider, `MicroManagerProvider` as well as each driver, offers its devices through the same interfaces of the driver SDK, so a driver can do what Micro-Manager does here, from hardware sequences to autofocus. How frames are delivered and paused is ScopeOne's own policy and stays out of the driver SDK. diff --git a/ScopeOneCore/include/scopeone/ConfigSetting.h b/ScopeOneCore/include/scopeone/ConfigSetting.h new file mode 100644 index 0000000..ba050b7 --- /dev/null +++ b/ScopeOneCore/include/scopeone/ConfigSetting.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace scopeone::core +{ + // One device property that a configuration preset sets + struct ConfigSetting + { + QString device; + QString property; + QString value; + }; +} diff --git a/ScopeOneCore/include/scopeone/DriverManifest.h b/ScopeOneCore/include/scopeone/DriverManifest.h index 990b244..c577b25 100644 --- a/ScopeOneCore/include/scopeone/DriverManifest.h +++ b/ScopeOneCore/include/scopeone/DriverManifest.h @@ -31,7 +31,7 @@ namespace scopeone::core // Raised whenever an interface of the driver SDK changes its virtual functions. A driver built // against other ones calls the wrong functions and crashes DriverHost, so it is rejected. - inline constexpr int ScopeOneDriverApiVersion = 2; + inline constexpr int ScopeOneDriverApiVersion = 3; // The drivers directory beside the application and in the user data directory SCOPEONE_CORE_EXPORT QStringList driverDirectories(); diff --git a/ScopeOneCore/include/scopeone/ExperimentDocument.h b/ScopeOneCore/include/scopeone/ExperimentDocument.h index d2704de..365b7a1 100644 --- a/ScopeOneCore/include/scopeone/ExperimentDocument.h +++ b/ScopeOneCore/include/scopeone/ExperimentDocument.h @@ -7,7 +7,7 @@ #include #include #include -#include "scopeone/HardwareCapabilities.h" +#include "scopeone/ConfigSetting.h" #include #include #include diff --git a/ScopeOneCore/include/scopeone/ScopeOneCore.h b/ScopeOneCore/include/scopeone/ScopeOneCore.h index 96e90de..862ae2e 100644 --- a/ScopeOneCore/include/scopeone/ScopeOneCore.h +++ b/ScopeOneCore/include/scopeone/ScopeOneCore.h @@ -24,7 +24,7 @@ #include "scopeone/ExperimentDocument.h" #include "scopeone/DaqDevice.h" #include "scopeone/HardwareTypes.h" -#include "scopeone/HardwareProvider.h" +#include "scopeone/Device.h" #include "scopeone/ImageFrame.h" #include "scopeone/ProcessingTypes.h" #include "scopeone/ProcessingPipeline.h" diff --git a/ScopeOneCore/internal/AcquisitionEngine.h b/ScopeOneCore/internal/AcquisitionEngine.h index aae9b16..3bdf2aa 100644 --- a/ScopeOneCore/internal/AcquisitionEngine.h +++ b/ScopeOneCore/internal/AcquisitionEngine.h @@ -2,7 +2,7 @@ #include -#include "scopeone/CameraProvider.h" +#include "scopeone/Camera.h" namespace scopeone::core::internal { diff --git a/ScopeOneCore/internal/ConfigurationProvider.h b/ScopeOneCore/internal/ConfigurationProvider.h new file mode 100644 index 0000000..4bd6aa6 --- /dev/null +++ b/ScopeOneCore/internal/ConfigurationProvider.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#include + +#include "scopeone/ConfigSetting.h" + +namespace scopeone::core::internal +{ + // A provider that defines configuration presets, which MicroManagerProvider reads from the + // configuration file of Micro-Manager. Drivers do not implement it. + class ConfigurationProvider + { + public: + virtual ~ConfigurationProvider() = default; + + // The device properties that applying a preset sets. Empty when the provider cannot + // tell, which is not an error. + virtual std::vector configSettings(const QString& groupName, + const QString& configName) const + { + (void)groupName; + (void)configName; + return {}; + } + + virtual QStringList availableConfigGroups() const = 0; + virtual QStringList availableConfigs(const QString& groupName) const = 0; + virtual QString currentConfig(const QString& groupName) const = 0; + virtual bool setConfig(const QString& groupName, + const QString& configName, + QString* errorMessage = nullptr) = 0; + }; +} diff --git a/ScopeOneCore/internal/DaqDeviceManager.h b/ScopeOneCore/internal/DaqDeviceManager.h index 155b3f0..d310776 100644 --- a/ScopeOneCore/internal/DaqDeviceManager.h +++ b/ScopeOneCore/internal/DaqDeviceManager.h @@ -4,8 +4,8 @@ #include #include "scopeone/DaqDevice.h" -#include "scopeone/DaqProvider.h" -#include "scopeone/HardwareProvider.h" +#include "scopeone/DaqDevice.h" +#include "scopeone/Device.h" namespace scopeone::core::internal { @@ -38,12 +38,12 @@ namespace scopeone::core::internal private: void refreshProviderDevices(); - void detachProvider(const HardwareProviderPtr& provider); + void detachProvider(const DevicePtr& provider); void updateState(const QString& deviceId, DaqState state, const QString& message); DeviceRegistry* m_registry{nullptr}; - QHash m_attachedProviders; - QHash m_providerByDevice; + QHash m_attachedProviders; + QHash m_providerByDevice; QHash m_descriptors; QHash m_states; QHash m_messages; diff --git a/ScopeOneCore/internal/DriverHostClient.h b/ScopeOneCore/internal/DriverHostClient.h index 96a165f..0a62a83 100644 --- a/ScopeOneCore/internal/DriverHostClient.h +++ b/ScopeOneCore/internal/DriverHostClient.h @@ -1,28 +1,29 @@ #pragma once -#include #include - #include +#include -#include "scopeone/HardwareProvider.h" +#include "scopeone/Device.h" namespace scopeone::core::internal { // A DriverHost is a process of its own that runs hardware for ScopeOne, so a vendor library that - // crashes or holds global state stays out of ScopeOne. The provider these functions return + // crashes or holds global state stays out of ScopeOne. The device these functions return // stands for what the DriverHost runs. It also implements CameraFrameSource. // A DriverHost that runs what the arguments name, such as one camera of a Micro-Manager // configuration for MicroManagerProvider - HardwareProviderPtr startDriverHost(const QString& providerId, + DevicePtr startDriverHost(const QString& providerId, const QStringList& arguments, QString* errorMessage = nullptr); - // The devices of one ScopeOne driver. Each driver is a provider of its own, as each camera of a - // Micro-Manager configuration is a device of MicroManagerProvider. - HardwareProviderPtr createScopeOneProvider(const QString& providerId, + // One device of a ScopeOne driver, created by its name in the driver, the first one when the + // name is empty. Each driver is a provider of its own, as each camera of a Micro-Manager + // configuration is a device of MicroManagerProvider. + DevicePtr createScopeOneProvider(const QString& providerId, const QString& driverPath, - const QJsonObject& options, + const QString& deviceName, + const QVariantMap& preInitProperties, QString* errorMessage = nullptr); } diff --git a/ScopeOneCore/internal/DriverHostProtocol.h b/ScopeOneCore/internal/DriverHostProtocol.h index 00b67f1..baed965 100644 --- a/ScopeOneCore/internal/DriverHostProtocol.h +++ b/ScopeOneCore/internal/DriverHostProtocol.h @@ -144,11 +144,6 @@ namespace scopeone::core::internal::driverhost inline const QString kCommandGetState = QStringLiteral("GetState"); inline const QString kCommandSetState = QStringLiteral("SetState"); inline const QString kCommandGetStateLabel = QStringLiteral("GetStateLabel"); - inline const QString kCommandListConfigGroups = QStringLiteral("ListConfigGroups"); - inline const QString kCommandListConfigs = QStringLiteral("ListConfigs"); - inline const QString kCommandGetCurrentConfig = QStringLiteral("GetCurrentConfig"); - inline const QString kCommandSetConfig = QStringLiteral("SetConfig"); - inline const QString kCommandGetConfigSettings = QStringLiteral("GetConfigSettings"); inline const QString kCommandStartSignal = QStringLiteral("StartSignal"); inline const QString kCommandStopSignal = QStringLiteral("StopSignal"); inline const QString kCommandStartDaq = QStringLiteral("StartDaq"); @@ -159,7 +154,6 @@ namespace scopeone::core::internal::driverhost inline const QString kCapabilityStage = QStringLiteral("Stage"); inline const QString kCapabilityShutter = QStringLiteral("Shutter"); inline const QString kCapabilityState = QStringLiteral("State"); - inline const QString kCapabilityConfiguration = QStringLiteral("Configuration"); inline const QString kCapabilityAutofocus = QStringLiteral("Autofocus"); inline const QString kFrameDeliveryModePreviewLatest = QStringLiteral("PreviewLatest"); diff --git a/ScopeOneCore/internal/HardwareRuntime.h b/ScopeOneCore/internal/HardwareRuntime.h index 899442a..f2d40cd 100644 --- a/ScopeOneCore/internal/HardwareRuntime.h +++ b/ScopeOneCore/internal/HardwareRuntime.h @@ -9,8 +9,9 @@ #include -#include "scopeone/HardwareProvider.h" -#include "scopeone/CameraProvider.h" +#include "scopeone/Device.h" +#include "scopeone/HardwareCapabilities.h" +#include "scopeone/Camera.h" #include "internal/AcquisitionEngine.h" #include "internal/CameraFrameDelivery.h" #include "internal/MicroManagerProvider.h" @@ -36,15 +37,15 @@ namespace scopeone::core::internal explicit DeviceRegistry(QObject* parent = nullptr); void clear(); - bool registerProvider(const HardwareProviderPtr& provider, + bool registerProvider(const DevicePtr& provider, const HardwareProviderDescriptor& descriptor, const QList& devices); void unregisterProvider(const QString& providerId); QList providers() const; QList devices() const; HardwareDeviceDescriptor device(const QString& logicalId) const; - HardwareProviderPtr provider(const QString& providerId) const; - HardwareProviderPtr providerForDevice(const QString& logicalId) const; + DevicePtr provider(const QString& providerId) const; + DevicePtr providerForDevice(const QString& logicalId) const; signals: void changed(); @@ -52,7 +53,7 @@ namespace scopeone::core::internal private: struct ProviderEntry { - HardwareProviderPtr provider; + DevicePtr provider; HardwareProviderDescriptor descriptor; QList devices; }; @@ -186,10 +187,11 @@ namespace scopeone::core::internal // A driver in a DriverHost of its own. Safe to call from any thread, and the provider is // registered afterwards on the thread of the runtime. - static HardwareProviderPtr startScopeOneProvider(const QString& providerId, - const QString& driverPath, - const QJsonObject& options, - QString* errorMessage); + static DevicePtr startScopeOneProvider(const QString& providerId, + const QString& driverPath, + const QString& deviceName, + const QVariantMap& preInitProperties, + QString* errorMessage); // The Micro-Manager configuration. Loading and unloading block, so they run on a worker // thread, while the cameras are started and stopped on the thread of the runtime. @@ -200,7 +202,7 @@ namespace scopeone::core::internal QString& errorMessage); QStringList startMicroManagerCameras(const QList& cameras); // The cameras, to be destroyed on a worker thread, as their DriverHosts take time to exit - QList takeMicroManagerCameras(); + QList takeMicroManagerCameras(); // Registers the devices again, false when one conflicts with the devices of a driver bool setMicroManagerDevices(const QList& devices); void stopMicroManagerPreview(); @@ -210,7 +212,7 @@ namespace scopeone::core::internal bool hasProvider(const QString& providerId) const { return m_registry.provider(providerId) != nullptr; } static QString microManagerVersion(); void clear(); - bool registerProvider(const HardwareProviderPtr& provider); + bool registerProvider(const DevicePtr& provider); void unregisterProvider(const QString& providerId); bool refreshProvider(const QString& providerId); void stopPreviewForProvider(const QString& providerId); @@ -221,11 +223,11 @@ namespace scopeone::core::internal void previewStateChanged(bool running); private: - CameraProvider* cameraProviderForDevice(const QString& logicalId) const; - DevicePropertyProvider* propertyProviderForDevice(const QString& logicalId) const; - StageProvider* stageProviderForDevice(const QString& logicalId) const; - ShutterProvider* shutterProviderForDevice(const QString& logicalId) const; - StateProvider* stateProviderForDevice(const QString& logicalId) const; + Camera* cameraForDevice(const QString& logicalId) const; + Device* propertyDeviceForDevice(const QString& logicalId) const; + Stage* stageForDevice(const QString& logicalId) const; + Shutter* shutterForDevice(const QString& logicalId) const; + StateDevice* stateDeviceForDevice(const QString& logicalId) const; ConfigurationProvider* configurationProviderForGroup(const QString& groupName) const; QList> frameSourcesFor(const QStringList& cameraIds) const; bool applyFrameDeliveryModes(const QHash& modes); diff --git a/ScopeOneCore/internal/MicroManagerCamera.h b/ScopeOneCore/internal/MicroManagerCamera.h index 5b86bfa..4da0a84 100644 --- a/ScopeOneCore/internal/MicroManagerCamera.h +++ b/ScopeOneCore/internal/MicroManagerCamera.h @@ -11,8 +11,8 @@ #include #include "internal/FrameDeliveryMode.h" -#include "scopeone/CameraProvider.h" -#include "scopeone/HardwareProvider.h" +#include "scopeone/Camera.h" +#include "scopeone/Device.h" class CMMCore; @@ -39,7 +39,7 @@ namespace scopeone::core::internal // ScopeOne. A configuration with more runs each camera in a DriverHost of its own, on a core that // holds only that camera, because a vendor library loaded twice in one process shares its state // and two cameras of the same model would stop each other. - class MicroManagerCamera final : public HardwareProvider, public CameraProvider + class MicroManagerCamera final : public Device, public Camera { public: // A camera that lends its frames hands out frames that point into the circular buffer of diff --git a/ScopeOneCore/internal/MicroManagerProvider.h b/ScopeOneCore/internal/MicroManagerProvider.h index 22020fe..d134582 100644 --- a/ScopeOneCore/internal/MicroManagerProvider.h +++ b/ScopeOneCore/internal/MicroManagerProvider.h @@ -10,9 +10,11 @@ #include #include "internal/CameraFrameDelivery.h" +#include "internal/ConfigurationProvider.h" #include "internal/MicroManagerCamera.h" -#include "scopeone/HardwareProvider.h" -#include "scopeone/CameraProvider.h" +#include "scopeone/Device.h" +#include "scopeone/HardwareCapabilities.h" +#include "scopeone/Camera.h" class CMMCore; @@ -21,12 +23,12 @@ namespace scopeone::core::internal // The devices of the loaded Micro-Manager configuration. They run on the core of ScopeOne, // except the cameras of a configuration with more than one camera: each of those runs in a // DriverHost of its own, so two cameras of the same model do not share one vendor library. - class MicroManagerProvider final : public HardwareProvider, - public CameraProvider, - public StageProvider, - public ShutterProvider, - public AutofocusProvider, - public StateProvider, + class MicroManagerProvider final : public Device, + public Camera, + public Stage, + public Shutter, + public Autofocus, + public StateDevice, public ConfigurationProvider, public CameraFrameSource { @@ -65,7 +67,7 @@ namespace scopeone::core::internal // DriverHosts. QStringList startCameras(const QList& cameras); // Hands over the cameras, so they can be stopped away from the calling thread - QList takeCameras(); + QList takeCameras(); void setFrameDelivery(CameraFrameDelivery* delivery) override; bool setFrameDeliveryMode(const QStringList& cameraIds, FrameDeliveryMode mode) override; @@ -173,11 +175,11 @@ namespace scopeone::core::internal double pixelSizeUm(const QString& cameraId) const override; private: - struct Camera + struct CameraEntry { // A MicroManagerCamera on the core of ScopeOne, or the DriverHost of the camera - HardwareProviderPtr provider; - CameraProvider* camera{nullptr}; + DevicePtr provider; + Camera* camera{nullptr}; CameraFrameSource* frameSource{nullptr}; std::shared_ptr local; // The index of the last frame the local camera delivered, 0 after a change of mode @@ -188,24 +190,24 @@ namespace scopeone::core::internal // cannot free it meanwhile struct CameraRef { - HardwareProviderPtr owner; - CameraProvider* camera{nullptr}; + DevicePtr owner; + Camera* camera{nullptr}; explicit operator bool() const { return camera != nullptr; } - CameraProvider* operator->() const { return camera; } + Camera* operator->() const { return camera; } }; bool startLocalCamera(const QString& cameraId); CameraRef camera(const QString& cameraId) const; - Camera cameraEntry(const QString& cameraId) const; + CameraEntry cameraEntry(const QString& cameraId) const; void readBinning(const QString& cameraId); - QList cameras() const; + QList cameras() const; std::shared_ptr m_core; QStringList m_adapterSearchPaths; QList m_devices; // Written when a configuration loads or unloads, read by calls on any thread mutable QReadWriteLock m_camerasLock; - QMap m_cameras; + QMap m_cameras; // The binning of each camera in a DriverHost, read when it changes, as the core of // ScopeOne does not hold these cameras to apply it to the pixel size QHash m_binnings; diff --git a/ScopeOneCore/internal/SignalSourceManager.h b/ScopeOneCore/internal/SignalSourceManager.h index d6726bd..729de8c 100644 --- a/ScopeOneCore/internal/SignalSourceManager.h +++ b/ScopeOneCore/internal/SignalSourceManager.h @@ -3,9 +3,9 @@ #include #include -#include "scopeone/HardwareProvider.h" +#include "scopeone/Device.h" +#include "scopeone/SignalSource.h" #include "scopeone/SignalSource.h" -#include "scopeone/SignalSourceProvider.h" namespace scopeone::core::internal { @@ -39,12 +39,12 @@ namespace scopeone::core::internal private: void refreshProviderSources(); - void detachProvider(const HardwareProviderPtr& provider); + void detachProvider(const DevicePtr& provider); void updateState(const QString& sourceId, SignalSourceState state, const QString& message); DeviceRegistry* m_registry{nullptr}; - QHash m_providerBySource; - QHash m_attachedProviders; + QHash m_providerBySource; + QHash m_attachedProviders; QHash m_descriptors; QHash m_states; QHash m_messages; diff --git a/ScopeOneCore/sdk/include/scopeone/CameraProvider.h b/ScopeOneCore/sdk/include/scopeone/Camera.h similarity index 87% rename from ScopeOneCore/sdk/include/scopeone/CameraProvider.h rename to ScopeOneCore/sdk/include/scopeone/Camera.h index cd3e6d3..55b854b 100644 --- a/ScopeOneCore/sdk/include/scopeone/CameraProvider.h +++ b/ScopeOneCore/sdk/include/scopeone/Camera.h @@ -6,33 +6,33 @@ #include #include -#include "scopeone/HardwareCapabilities.h" +#include "scopeone/scopeone_sdk_export.h" #include "scopeone/ImageFrame.h" namespace scopeone::core { - // A provider whose devices include cameras. Every provider implements this interface, the - // drivers as well as Micro-Manager inside ScopeOne, and every call names one camera by its id. + // A device that includes cameras. Every such device implements this interface, the drivers + // as well as Micro-Manager inside ScopeOne, and every call names one camera by its id. // // Threads: DriverHost calls the methods of a driver one at a time, from one thread, so they // need no locks against each other. The frame sink may be called from any thread, such as a // callback thread of the camera SDK, while a method runs. // // Frames: Mono8 or Mono16, at most 16 MB each; a larger frame is not delivered. frameIndex - // counts the images the camera took, from 1, including those the provider did not hand on, + // counts the images the camera took, from 1, including those the device did not hand on, // so ScopeOne can tell the frame rate and see frames that were lost. timestampNs is the time // the image was taken, in nanoseconds since the Unix epoch, or 0 to let ScopeOne stamp it. - class SCOPEONE_SDK_EXPORT CameraProvider : public DevicePropertyProvider + class SCOPEONE_SDK_EXPORT Camera { public: // ScopeOne copies the frame before the sink returns, so the bytes of a frame may point // into a buffer of the camera, for example through QByteArray::fromRawData, and need to // stay valid only during the call. The copy takes about a millisecond for 8 MB. using FrameSink = std::function; - // Called with whether any camera of the provider is running, whenever that may change + // Called with whether any camera of the device is running, whenever that may change using PreviewStateSink = std::function; - ~CameraProvider() override; + virtual ~Camera(); // Once a new sink is set, the old one is no longer called from any thread virtual void setFrameSink(FrameSink sink) = 0; @@ -53,13 +53,13 @@ namespace scopeone::core int& height) = 0; // Takes one new image with the current settings, started after the call, and returns it // instead of handing it to the sink. A running acquisition may be stopped for it. - // timeoutMs is how long ScopeOne waits for the image; a provider that waits for the camera + // timeoutMs is how long ScopeOne waits for the image; a device that waits for the camera // itself should give up after it. virtual bool captureEventFrame(const QString& cameraId, ImageFrame& frame, int timeoutMs = 1500) = 0; // The size of a pixel in the sample in micrometers, with the current objective and - // binning, or 0 when the provider does not know it + // binning, or 0 when the device does not know it virtual double pixelSizeUm(const QString& cameraId) const { (void)cameraId; diff --git a/ScopeOneCore/sdk/include/scopeone/DaqDevice.h b/ScopeOneCore/sdk/include/scopeone/DaqDevice.h index 97c3611..4298a68 100644 --- a/ScopeOneCore/sdk/include/scopeone/DaqDevice.h +++ b/ScopeOneCore/sdk/include/scopeone/DaqDevice.h @@ -6,6 +6,8 @@ #include #include +#include + #include "scopeone/scopeone_sdk_export.h" namespace scopeone::core @@ -151,6 +153,27 @@ namespace scopeone::core QList digitalTasks; QList routes; }; + + // A data acquisition device. It runs timed analog, digital and counter + // tasks, puts out scan waveforms and clocks and reads its inputs continuously + class SCOPEONE_SDK_EXPORT Daq + { + public: + using InputSink = std::function; + using StateSink = std::function; + + virtual ~Daq(); + + virtual QList daqDevices() const = 0; + // The sinks may be called from any thread, and the input sink in the order of the samples + virtual void setDaqSinks(InputSink input, StateSink state) = 0; + // Raster scans of the session are already expanded into its tasks + virtual bool startDaq(const DaqSessionConfig& config, + QString* errorMessage = nullptr) = 0; + virtual void stopDaq(const QString& deviceId) = 0; + }; } Q_DECLARE_METATYPE(scopeone::core::DaqState) diff --git a/ScopeOneCore/sdk/include/scopeone/DaqProvider.h b/ScopeOneCore/sdk/include/scopeone/DaqProvider.h deleted file mode 100644 index 8b4cdb9..0000000 --- a/ScopeOneCore/sdk/include/scopeone/DaqProvider.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include -#include - -#include - -#include "scopeone/DaqDevice.h" -#include "scopeone/scopeone_sdk_export.h" - -namespace scopeone::core -{ - // A hardware provider of data acquisition devices, which run timed analog, digital and - // counter tasks: they put out scan waveforms and clocks and read their inputs continuously - class SCOPEONE_SDK_EXPORT DaqProvider - { - public: - using InputSink = std::function; - using StateSink = std::function; - - virtual ~DaqProvider(); - - virtual QList daqDevices() const = 0; - // The sinks may be called from any thread, and the input sink in the order of the samples - virtual void setDaqSinks(InputSink input, StateSink state) = 0; - // Raster scans of the session are already expanded into its tasks - virtual bool startDaq(const DaqSessionConfig& config, - QString* errorMessage = nullptr) = 0; - virtual void stopDaq(const QString& deviceId) = 0; - }; -} diff --git a/ScopeOneCore/sdk/include/scopeone/Device.h b/ScopeOneCore/sdk/include/scopeone/Device.h new file mode 100644 index 0000000..9daef82 --- /dev/null +++ b/ScopeOneCore/sdk/include/scopeone/Device.h @@ -0,0 +1,134 @@ +#pragma once + +#include +#include +#include + +#include + +#include "scopeone/HardwareTypes.h" +#include "scopeone/scopeone_sdk_export.h" + +namespace scopeone::core +{ + // What a driver creates by device name. A device implements the capability interfaces that + // fit it, such as Camera or Stage, next to this class, and ScopeOne finds them by casting. + // Every call that names a deviceId means one of the devices() of the object, which tells + // the peripherals of a hub apart. The property methods default to a device without + // properties. + class SCOPEONE_SDK_EXPORT Device + { + public: + virtual ~Device(); + + virtual HardwareProviderDescriptor descriptor() const = 0; + // The devices the object exposes. A Hub lists its peripherals, and so does the default. + virtual QList devices() const; + + virtual QStringList listProperties(const QString& deviceId) + { + (void)deviceId; + return {}; + } + virtual QString getProperty(const QString& deviceId, + const QString& name, + bool fromCache = false) + { + (void)deviceId; + (void)name; + (void)fromCache; + return {}; + } + virtual bool setProperty(const QString& deviceId, + const QString& name, + const QString& value, + QString* errorMessage = nullptr) + { + (void)deviceId; + (void)value; + if (errorMessage) *errorMessage = QStringLiteral("Unknown property %1").arg(name); + return false; + } + virtual QString getPropertyType(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return {}; + } + virtual bool isPropertyReadOnly(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return true; + } + virtual bool isPropertyPreInit(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return false; + } + virtual QStringList getAllowedPropertyValues(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return {}; + } + virtual bool hasPropertyLimits(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return false; + } + virtual double getPropertyLowerLimit(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return 0.0; + } + virtual double getPropertyUpperLimit(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return 0.0; + } + + // A property sequence steps a property through a list of values, one value on each + // trigger of the camera, without a call per value. The capacity is the longest list the + // device takes, 0 when the property cannot be sequenced. + virtual long propertySequenceCapacity(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + return 0; + } + // Loads the values and arms the property for the next triggers + virtual bool startPropertySequence(const QString& deviceId, + const QString& name, + const QStringList& values, + QString* errorMessage = nullptr) + { + (void)deviceId; + (void)name; + (void)values; + if (errorMessage) *errorMessage = QStringLiteral("The property cannot be sequenced"); + return false; + } + virtual void stopPropertySequence(const QString& deviceId, const QString& name) + { + (void)deviceId; + (void)name; + } + }; + + // A device that has peripherals, such as a DAQ board with its channels. The peripherals are + // the devices() of the hub, each with its own deviceId. + class SCOPEONE_SDK_EXPORT Hub + { + public: + virtual ~Hub(); + + virtual QList peripherals() const = 0; + }; + + using DevicePtr = std::shared_ptr; +} diff --git a/ScopeOneCore/sdk/include/scopeone/Driver.h b/ScopeOneCore/sdk/include/scopeone/Driver.h index 47a4a2b..82f8b61 100644 --- a/ScopeOneCore/sdk/include/scopeone/Driver.h +++ b/ScopeOneCore/sdk/include/scopeone/Driver.h @@ -1,24 +1,30 @@ #pragma once -#include +#include +#include #include -#include "scopeone/HardwareProvider.h" +#include "scopeone/Device.h" namespace scopeone::core { // The entry point of a driver library. ScopeOne_DriverHost loads the library, gets this - // object from Qt's plugin loader and asks it for the provider of the devices. + // object from Qt's plugin loader and asks it to create one device by name. A library may + // offer several devices, as an adapter library of Micro-Manager does. class Driver { public: virtual ~Driver() = default; virtual QString providerId() const = 0; - virtual HardwareProviderPtr createProvider(const QJsonObject& options, - QString* errorMessage = nullptr) = 0; + virtual QStringList deviceNames() const = 0; + // The pre-init properties are the values a device needs before it can start, such as a + // port, and come from the configuration of the system + virtual DevicePtr createDevice(const QString& name, + const QVariantMap& preInitProperties, + QString* errorMessage = nullptr) = 0; }; } -#define ScopeOneDriver_iid "org.scopeone.Driver/1.0" +#define ScopeOneDriver_iid "org.scopeone.Driver/2.0" Q_DECLARE_INTERFACE(scopeone::core::Driver, ScopeOneDriver_iid) diff --git a/ScopeOneCore/sdk/include/scopeone/DriverSDK.h b/ScopeOneCore/sdk/include/scopeone/DriverSDK.h index 579dff0..ff683cd 100644 --- a/ScopeOneCore/sdk/include/scopeone/DriverSDK.h +++ b/ScopeOneCore/sdk/include/scopeone/DriverSDK.h @@ -2,11 +2,11 @@ // Everything a driver is written against. These headers are the public contract of // ScopeOne for code outside this repository. -#include "scopeone/CameraProvider.h" -#include "scopeone/DaqProvider.h" +#include "scopeone/Camera.h" +#include "scopeone/DaqDevice.h" +#include "scopeone/Device.h" #include "scopeone/Driver.h" #include "scopeone/HardwareCapabilities.h" -#include "scopeone/HardwareProvider.h" #include "scopeone/HardwareTypes.h" #include "scopeone/ImageFrame.h" -#include "scopeone/SignalSourceProvider.h" +#include "scopeone/SignalSource.h" diff --git a/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h b/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h index f789d57..c6ff4ba 100644 --- a/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h +++ b/ScopeOneCore/sdk/include/scopeone/HardwareCapabilities.h @@ -9,60 +9,10 @@ namespace scopeone::core { - class SCOPEONE_SDK_EXPORT DevicePropertyProvider + class SCOPEONE_SDK_EXPORT Stage { public: - virtual ~DevicePropertyProvider(); - - virtual QStringList listProperties(const QString& deviceId) = 0; - virtual QString getProperty(const QString& deviceId, - const QString& name, - bool fromCache = false) = 0; - virtual bool setProperty(const QString& deviceId, - const QString& name, - const QString& value, - QString* errorMessage = nullptr) = 0; - virtual QString getPropertyType(const QString& deviceId, const QString& name) = 0; - virtual bool isPropertyReadOnly(const QString& deviceId, const QString& name) = 0; - virtual bool isPropertyPreInit(const QString& deviceId, const QString& name) = 0; - virtual QStringList getAllowedPropertyValues(const QString& deviceId, - const QString& name) = 0; - virtual bool hasPropertyLimits(const QString& deviceId, const QString& name) = 0; - virtual double getPropertyLowerLimit(const QString& deviceId, const QString& name) = 0; - virtual double getPropertyUpperLimit(const QString& deviceId, const QString& name) = 0; - - // A property sequence steps a property through a list of values, one value on each - // trigger of the camera, without a call per value. The capacity is the longest list the - // device takes, 0 when the property cannot be sequenced. - virtual long propertySequenceCapacity(const QString& deviceId, const QString& name) - { - (void)deviceId; - (void)name; - return 0; - } - // Loads the values and arms the property for the next triggers - virtual bool startPropertySequence(const QString& deviceId, - const QString& name, - const QStringList& values, - QString* errorMessage = nullptr) - { - (void)deviceId; - (void)name; - (void)values; - if (errorMessage) *errorMessage = QStringLiteral("The property cannot be sequenced"); - return false; - } - virtual void stopPropertySequence(const QString& deviceId, const QString& name) - { - (void)deviceId; - (void)name; - } - }; - - class SCOPEONE_SDK_EXPORT StageProvider - { - public: - virtual ~StageProvider(); + virtual ~Stage(); virtual QString defaultXYStage() const = 0; virtual QString defaultZStage() const = 0; @@ -112,10 +62,10 @@ namespace scopeone::core } }; - class SCOPEONE_SDK_EXPORT ShutterProvider + class SCOPEONE_SDK_EXPORT Shutter { public: - virtual ~ShutterProvider(); + virtual ~Shutter(); virtual bool isShutterOpen(const QString& deviceId, bool& open, @@ -124,37 +74,37 @@ namespace scopeone::core bool open, QString* errorMessage = nullptr) = 0; - // The shutter that lights the sample while a camera of the provider takes an image + // The shutter that lights the sample while a camera of the device takes an image virtual QString defaultShutter() const { return {}; } - // Whether the provider opens the default shutter around each image by itself + // Whether the device opens the default shutter around each image by itself virtual bool autoShutter() const { return false; } virtual bool setAutoShutter(bool enabled, QString* errorMessage = nullptr) { - if (errorMessage && enabled) *errorMessage = QStringLiteral("The provider has no automatic shutter"); + if (errorMessage && enabled) *errorMessage = QStringLiteral("The device has no automatic shutter"); return !enabled; } }; - class SCOPEONE_SDK_EXPORT AutofocusProvider + class SCOPEONE_SDK_EXPORT Autofocus { public: - virtual ~AutofocusProvider(); + virtual ~Autofocus(); virtual QString defaultAutofocus() const = 0; // Focuses with the autofocus device and returns once the focus has settled virtual bool runAutofocus(const QString& deviceId, QString* errorMessage = nullptr) = 0; }; - class SCOPEONE_SDK_EXPORT StateProvider + class SCOPEONE_SDK_EXPORT StateDevice { public: - virtual ~StateProvider(); + virtual ~StateDevice(); virtual bool getState(const QString& deviceId, long& state, @@ -164,35 +114,4 @@ namespace scopeone::core QString* errorMessage = nullptr) = 0; virtual QString stateLabel(const QString& deviceId, long state) const = 0; }; - - // One device property that a configuration preset sets - struct ConfigSetting - { - QString device; - QString property; - QString value; - }; - - class SCOPEONE_SDK_EXPORT ConfigurationProvider - { - public: - virtual ~ConfigurationProvider(); - - // The device properties that applying a preset sets. Empty when the provider cannot - // tell, which is not an error. - virtual std::vector configSettings(const QString& groupName, - const QString& configName) const - { - (void)groupName; - (void)configName; - return {}; - } - - virtual QStringList availableConfigGroups() const = 0; - virtual QStringList availableConfigs(const QString& groupName) const = 0; - virtual QString currentConfig(const QString& groupName) const = 0; - virtual bool setConfig(const QString& groupName, - const QString& configName, - QString* errorMessage = nullptr) = 0; - }; } diff --git a/ScopeOneCore/sdk/include/scopeone/HardwareProvider.h b/ScopeOneCore/sdk/include/scopeone/HardwareProvider.h deleted file mode 100644 index 351b38d..0000000 --- a/ScopeOneCore/sdk/include/scopeone/HardwareProvider.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include - -#include - -#include "scopeone/HardwareTypes.h" -#include "scopeone/scopeone_sdk_export.h" - -namespace scopeone::core -{ - class SCOPEONE_SDK_EXPORT HardwareProvider - { - public: - virtual ~HardwareProvider(); - - virtual HardwareProviderDescriptor descriptor() const = 0; - virtual QList devices() const = 0; - }; - - using HardwareProviderPtr = std::shared_ptr; -} diff --git a/ScopeOneCore/sdk/include/scopeone/SignalSource.h b/ScopeOneCore/sdk/include/scopeone/SignalSource.h index 92472c7..cfd11ed 100644 --- a/ScopeOneCore/sdk/include/scopeone/SignalSource.h +++ b/ScopeOneCore/sdk/include/scopeone/SignalSource.h @@ -10,6 +10,8 @@ #include #include +#include + #include "scopeone/scopeone_sdk_export.h" namespace scopeone::core @@ -178,6 +180,30 @@ namespace scopeone::core QVector m_pendingMarkers; QList m_readyChunks; }; + + // A device that puts out one value over time, such as a PMT, an APD or a + // photon counter. It hands over the stream and the scan markers; ScopeOne plots the stream + // and assembles scan images from it. + class SCOPEONE_SDK_EXPORT SignalSource + { + public: + using TimeSeriesSink = std::function; + using TimestampedEventSink = std::function; + using StateSink = std::function; + + virtual ~SignalSource(); + + virtual QList signalSources() const = 0; + // The sinks may be called from any thread, and each in the order of the stream + virtual void setSignalSinks(TimeSeriesSink timeSeries, + TimestampedEventSink timestampedEvents, + StateSink state) = 0; + virtual bool startSignal(const SignalAcquisitionConfig& config, + QString* errorMessage = nullptr) = 0; + virtual void stopSignal(const QString& sourceId) = 0; + }; } Q_DECLARE_METATYPE(scopeone::core::SignalSourceState) diff --git a/ScopeOneCore/sdk/include/scopeone/SignalSourceProvider.h b/ScopeOneCore/sdk/include/scopeone/SignalSourceProvider.h deleted file mode 100644 index e3f7d5b..0000000 --- a/ScopeOneCore/sdk/include/scopeone/SignalSourceProvider.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include -#include - -#include - -#include "scopeone/SignalSource.h" -#include "scopeone/scopeone_sdk_export.h" - -namespace scopeone::core -{ - // A hardware provider whose devices put out one value over time, such as a PMT, an APD or a - // photon counter. It hands over the stream and the scan markers; ScopeOne plots the stream - // and assembles scan images from it. - class SCOPEONE_SDK_EXPORT SignalSourceProvider - { - public: - using TimeSeriesSink = std::function; - using TimestampedEventSink = std::function; - using StateSink = std::function; - - virtual ~SignalSourceProvider(); - - virtual QList signalSources() const = 0; - // The sinks may be called from any thread, and each in the order of the stream - virtual void setSignalSinks(TimeSeriesSink timeSeries, - TimestampedEventSink timestampedEvents, - StateSink state) = 0; - virtual bool startSignal(const SignalAcquisitionConfig& config, - QString* errorMessage = nullptr) = 0; - virtual void stopSignal(const QString& sourceId) = 0; - }; -} diff --git a/ScopeOneCore/sdk/src/Device.cpp b/ScopeOneCore/sdk/src/Device.cpp new file mode 100644 index 0000000..a6d3a93 --- /dev/null +++ b/ScopeOneCore/sdk/src/Device.cpp @@ -0,0 +1,24 @@ +#include "scopeone/Camera.h" +#include "scopeone/Device.h" +#include "scopeone/DaqDevice.h" +#include "scopeone/HardwareCapabilities.h" +#include "scopeone/SignalSource.h" + +namespace scopeone::core +{ + Device::~Device() = default; + Hub::~Hub() = default; + Stage::~Stage() = default; + Shutter::~Shutter() = default; + Autofocus::~Autofocus() = default; + StateDevice::~StateDevice() = default; + Camera::~Camera() = default; + SignalSource::~SignalSource() = default; + Daq::~Daq() = default; + + QList Device::devices() const + { + const auto* const hub = dynamic_cast(this); + return hub ? hub->peripherals() : QList{}; + } +} diff --git a/ScopeOneCore/sdk/src/HardwareProvider.cpp b/ScopeOneCore/sdk/src/HardwareProvider.cpp deleted file mode 100644 index 17191f6..0000000 --- a/ScopeOneCore/sdk/src/HardwareProvider.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "scopeone/CameraProvider.h" -#include "scopeone/HardwareCapabilities.h" -#include "scopeone/DaqProvider.h" -#include "scopeone/HardwareProvider.h" -#include "scopeone/SignalSourceProvider.h" - -namespace scopeone::core -{ - HardwareProvider::~HardwareProvider() = default; - DevicePropertyProvider::~DevicePropertyProvider() = default; - StageProvider::~StageProvider() = default; - ShutterProvider::~ShutterProvider() = default; - AutofocusProvider::~AutofocusProvider() = default; - StateProvider::~StateProvider() = default; - ConfigurationProvider::~ConfigurationProvider() = default; - CameraProvider::~CameraProvider() = default; - SignalSourceProvider::~SignalSourceProvider() = default; - DaqProvider::~DaqProvider() = default; -} diff --git a/ScopeOneCore/src/AcquisitionEngine.cpp b/ScopeOneCore/src/AcquisitionEngine.cpp index 9a86d93..7773a44 100644 --- a/ScopeOneCore/src/AcquisitionEngine.cpp +++ b/ScopeOneCore/src/AcquisitionEngine.cpp @@ -1,6 +1,6 @@ #include "internal/AcquisitionEngine.h" -#include "scopeone/CameraProvider.h" +#include "scopeone/Camera.h" #include "internal/HardwareRuntime.h" namespace scopeone::core::internal @@ -21,7 +21,7 @@ namespace scopeone::core::internal { struct StartedCamera { - CameraProvider* provider; + Camera* provider; QString cameraId; }; QList startedCameras; @@ -33,8 +33,8 @@ namespace scopeone::core::internal continue; } found = true; - const HardwareProviderPtr provider = m_deviceRegistry.provider(device.providerId); - auto* cameraProvider = dynamic_cast(provider.get()); + const DevicePtr provider = m_deviceRegistry.provider(device.providerId); + auto* cameraProvider = dynamic_cast(provider.get()); if (!cameraProvider) { for (auto it = startedCameras.crbegin(); it != startedCameras.crend(); ++it) @@ -59,8 +59,8 @@ namespace scopeone::core::internal } return found; } - const HardwareProviderPtr provider = m_deviceRegistry.providerForDevice(target); - auto* cameraProvider = dynamic_cast(provider.get()); + const DevicePtr provider = m_deviceRegistry.providerForDevice(target); + auto* cameraProvider = dynamic_cast(provider.get()); return cameraProvider && cameraProvider->startPreviewFor(target); } @@ -82,8 +82,8 @@ namespace scopeone::core::internal continue; } found = true; - const HardwareProviderPtr provider = m_deviceRegistry.provider(device.providerId); - auto* cameraProvider = dynamic_cast(provider.get()); + const DevicePtr provider = m_deviceRegistry.provider(device.providerId); + auto* cameraProvider = dynamic_cast(provider.get()); if (!cameraProvider) { stopped = false; @@ -96,8 +96,8 @@ namespace scopeone::core::internal } return found && stopped; } - const HardwareProviderPtr provider = m_deviceRegistry.providerForDevice(target); - auto* cameraProvider = dynamic_cast(provider.get()); + const DevicePtr provider = m_deviceRegistry.providerForDevice(target); + auto* cameraProvider = dynamic_cast(provider.get()); return cameraProvider && cameraProvider->stopPreviewFor(target); } } diff --git a/ScopeOneCore/src/DaqDeviceManager.cpp b/ScopeOneCore/src/DaqDeviceManager.cpp index d96f1de..b1c6301 100644 --- a/ScopeOneCore/src/DaqDeviceManager.cpp +++ b/ScopeOneCore/src/DaqDeviceManager.cpp @@ -82,7 +82,7 @@ namespace scopeone::core::internal DaqDeviceManager::~DaqDeviceManager() { - for (const HardwareProviderPtr& provider : std::as_const(m_attachedProviders)) + for (const DevicePtr& provider : std::as_const(m_attachedProviders)) { detachProvider(provider); } @@ -104,7 +104,7 @@ namespace scopeone::core::internal QString* errorMessage) { const QString deviceId = config.deviceId.trimmed(); - const HardwareProviderPtr provider = m_providerByDevice.value(deviceId); + const DevicePtr provider = m_providerByDevice.value(deviceId); if (!provider) { if (errorMessage) @@ -120,15 +120,15 @@ namespace scopeone::core::internal { expandRasterScan(scan, expanded); } - return dynamic_cast(provider.get())->startDaq(expanded, errorMessage); + return dynamic_cast(provider.get())->startDaq(expanded, errorMessage); } void DaqDeviceManager::stop(const QString& deviceId) { const QString id = deviceId.trimmed(); - if (const HardwareProviderPtr provider = m_providerByDevice.value(id)) + if (const DevicePtr provider = m_providerByDevice.value(id)) { - dynamic_cast(provider.get())->stopDaq(id); + dynamic_cast(provider.get())->stopDaq(id); } } @@ -145,11 +145,11 @@ namespace scopeone::core::internal // Follows the drivers as they are registered and removed, like the signal sources void DaqDeviceManager::refreshProviderDevices() { - QHash current; + QHash current; for (const HardwareProviderDescriptor& descriptor : m_registry->providers()) { - const HardwareProviderPtr provider = m_registry->provider(descriptor.id); - if (dynamic_cast(provider.get())) + const DevicePtr provider = m_registry->provider(descriptor.id); + if (dynamic_cast(provider.get())) { current.insert(descriptor.id, provider); } @@ -172,7 +172,7 @@ namespace scopeone::core::internal { continue; } - auto* const daqProvider = dynamic_cast(it.value().get()); + auto* const daqProvider = dynamic_cast(it.value().get()); const QPointer self(this); daqProvider->setDaqSinks( [self](const DaqInputChunk& chunk) @@ -210,9 +210,9 @@ namespace scopeone::core::internal } } - void DaqDeviceManager::detachProvider(const HardwareProviderPtr& provider) + void DaqDeviceManager::detachProvider(const DevicePtr& provider) { - dynamic_cast(provider.get())->setDaqSinks({}, {}); + dynamic_cast(provider.get())->setDaqSinks({}, {}); for (auto it = m_providerByDevice.begin(); it != m_providerByDevice.end();) { if (it.value() != provider) diff --git a/ScopeOneCore/src/DriverHostClient.cpp b/ScopeOneCore/src/DriverHostClient.cpp index a91cfeb..661e607 100644 --- a/ScopeOneCore/src/DriverHostClient.cpp +++ b/ScopeOneCore/src/DriverHostClient.cpp @@ -5,9 +5,9 @@ #include "internal/HardwareStreamCodec.h" #include "internal/SharedFrameRing.h" #include "internal/StreamRing.h" -#include "scopeone/CameraProvider.h" -#include "scopeone/DaqProvider.h" -#include "scopeone/SignalSourceProvider.h" +#include "scopeone/Camera.h" +#include "scopeone/DaqDevice.h" +#include "scopeone/SignalSource.h" #include "scopeone/HardwareCapabilities.h" #include "scopeone/SharedFrame.h" @@ -85,11 +85,11 @@ namespace scopeone::core::internal // Where the chunks and states of the signal sources and DAQ devices go, on the transport thread struct StreamHandlers { - SignalSourceProvider::TimeSeriesSink timeSeries; - SignalSourceProvider::TimestampedEventSink timestampedEvents; - SignalSourceProvider::StateSink signalState; - DaqProvider::InputSink daqInput; - DaqProvider::StateSink daqState; + SignalSource::TimeSeriesSink timeSeries; + SignalSource::TimestampedEventSink timestampedEvents; + SignalSource::StateSink signalState; + Daq::InputSink daqInput; + Daq::StateSink daqState; }; DriverHostTransport(QString providerId, @@ -928,15 +928,14 @@ namespace scopeone::core::internal // Stands in ScopeOne for what one DriverHost runs. Every call goes to the DriverHost as a // request on the transport thread. class DriverHostClient final : public QObject, - public HardwareProvider, - public CameraProvider, - public StageProvider, - public ShutterProvider, - public AutofocusProvider, - public StateProvider, - public ConfigurationProvider, - public SignalSourceProvider, - public DaqProvider, + public Device, + public Camera, + public Stage, + public Shutter, + public Autofocus, + public StateDevice, + public SignalSource, + public Daq, public CameraFrameSource { public: @@ -1046,7 +1045,7 @@ namespace scopeone::core::internal void setSignalSinks(TimeSeriesSink timeSeries, TimestampedEventSink timestampedEvents, - SignalSourceProvider::StateSink state) override + SignalSource::StateSink state) override { QMutexLocker locker(&m_sinkMutex); m_timeSeriesSink = std::move(timeSeries); @@ -1066,7 +1065,7 @@ namespace scopeone::core::internal QList daqDevices() const override { return m_daqDevices; } - void setDaqSinks(InputSink input, DaqProvider::StateSink state) override + void setDaqSinks(InputSink input, Daq::StateSink state) override { QMutexLocker locker(&m_sinkMutex); m_daqInputSink = std::move(input); @@ -1445,27 +1444,6 @@ namespace scopeone::core::internal kAutofocusTimeoutMs); } - std::vector configSettings(const QString& groupName, - const QString& configName) const override - { - QJsonObject response; - std::vector settings; - if (!request(driverhost::kCommandGetConfigSettings, {}, - {{QStringLiteral("group"), groupName}, {QStringLiteral("config"), configName}}, - response, nullptr)) - { - return settings; - } - for (const QJsonValue& value : response.value(QStringLiteral("settings")).toArray()) - { - const QJsonObject setting = value.toObject(); - settings.push_back({setting.value(QStringLiteral("device")).toString(), - setting.value(QStringLiteral("property")).toString(), - setting.value(QStringLiteral("value")).toString()}); - } - return settings; - } - bool getXYPosition(const QString& deviceId, double& x, double& y, @@ -1631,65 +1609,6 @@ namespace scopeone::core::internal : QString{}; } - QStringList availableConfigGroups() const override - { - QJsonObject response; - if (!request(driverhost::kCommandListConfigGroups, - {}, - {}, - response, - nullptr)) - { - return {}; - } - return jsonStringList(response.value(QStringLiteral("groups")).toArray()); - } - - QStringList availableConfigs(const QString& groupName) const override - { - QJsonObject fields; - fields.insert(QStringLiteral("group"), groupName); - QJsonObject response; - return request(driverhost::kCommandListConfigs, - {}, - fields, - response, - nullptr) - ? jsonStringList(response.value(QStringLiteral("configs")).toArray()) - : QStringList{}; - } - - QString currentConfig(const QString& groupName) const override - { - QJsonObject fields; - fields.insert(QStringLiteral("group"), groupName); - QJsonObject response; - return request(driverhost::kCommandGetCurrentConfig, - {}, - fields, - response, - nullptr) - ? response.value(QStringLiteral("config")).toString() - : QString{}; - } - - bool setConfig(const QString& groupName, - const QString& configName, - QString* errorMessage) override - { - QJsonObject fields; - fields.insert(QStringLiteral("group"), groupName); - fields.insert(QStringLiteral("config"), configName); - QJsonObject response; - clearPropertyDetails(); - const bool ok = request(driverhost::kCommandSetConfig, {}, fields, response, errorMessage); - if (ok) - { - refreshPixelSizes(cameraIds()); - } - return ok; - } - private: bool isCamera(const QString& cameraId) const { @@ -1846,7 +1765,7 @@ namespace scopeone::core::internal }; handlers.signalState = [this](const QString& sourceId, SignalSourceState state, const QString& message) { - SignalSourceProvider::StateSink sink; + SignalSource::StateSink sink; { QMutexLocker locker(&m_sinkMutex); sink = m_signalStateSink; @@ -1864,7 +1783,7 @@ namespace scopeone::core::internal }; handlers.daqState = [this](const QString& deviceId, DaqState state, const QString& message) { - DaqProvider::StateSink sink; + Daq::StateSink sink; { QMutexLocker locker(&m_sinkMutex); sink = m_daqStateSink; @@ -1887,13 +1806,6 @@ namespace scopeone::core::internal QMetaObject::invokeMethod(m_worker, std::forward(function), Qt::BlockingQueuedConnection); } - static QStringList jsonStringList(const QJsonArray& array) - { - QStringList result; - for (const QJsonValue& value : array) result.append(value.toString()); - return result; - } - QString m_expectedProviderId; QStringList m_arguments; HardwareProviderDescriptor m_descriptor; @@ -1910,9 +1822,9 @@ namespace scopeone::core::internal PreviewStateSink m_previewStateSink; TimeSeriesSink m_timeSeriesSink; TimestampedEventSink m_timestampedEventSink; - SignalSourceProvider::StateSink m_signalStateSink; + SignalSource::StateSink m_signalStateSink; InputSink m_daqInputSink; - DaqProvider::StateSink m_daqStateSink; + Daq::StateSink m_daqStateSink; QList m_signalSources; QList m_daqDevices; mutable QMutex m_stateMutex; @@ -1921,9 +1833,10 @@ namespace scopeone::core::internal QHash m_pixelSizes; }; - HardwareProviderPtr createScopeOneProvider(const QString& providerId, + DevicePtr createScopeOneProvider(const QString& providerId, const QString& driverPath, - const QJsonObject& options, + const QString& deviceName, + const QVariantMap& preInitProperties, QString* errorMessage) { const QString normalizedProviderId = providerId.trimmed(); @@ -1939,15 +1852,20 @@ namespace scopeone::core::internal return {}; } QStringList arguments{QStringLiteral("--driver"), normalizedDriverPath}; - if (!options.isEmpty()) + if (!deviceName.isEmpty()) + { + arguments << QStringLiteral("--device") << deviceName; + } + if (!preInitProperties.isEmpty()) { - arguments << QStringLiteral("--option") - << QString::fromUtf8(QJsonDocument(options).toJson(QJsonDocument::Compact)); + arguments << QStringLiteral("--properties") + << QString::fromUtf8(QJsonDocument(QJsonObject::fromVariantMap(preInitProperties)) + .toJson(QJsonDocument::Compact)); } return startDriverHost(normalizedProviderId, arguments, errorMessage); } - HardwareProviderPtr startDriverHost(const QString& providerId, + DevicePtr startDriverHost(const QString& providerId, const QStringList& arguments, QString* errorMessage) { diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index a876d33..f9ddde4 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -68,13 +68,13 @@ extern "C" int shm_unlink(const char* name); #include "internal/HardwareStreamCodec.h" #include "internal/SharedFrameRing.h" #include "internal/StreamRing.h" -#include "scopeone/CameraProvider.h" -#include "scopeone/DaqProvider.h" +#include "scopeone/Camera.h" +#include "scopeone/DaqDevice.h" #include "scopeone/Driver.h" #include "scopeone/DriverManifest.h" #include "scopeone/HardwareCapabilities.h" #include "scopeone/SharedFrame.h" -#include "scopeone/SignalSourceProvider.h" +#include "scopeone/SignalSource.h" namespace scopeone::core::internal { @@ -197,7 +197,7 @@ namespace scopeone::core::internal QByteArray m_readBuffer; }; - // Runs one provider, a driver or one Micro-Manager camera, and answers the requests of + // Runs one device of a driver or one Micro-Manager camera, and answers the requests of // ScopeOne on a thread of its own class DriverRuntime final : public QObject { @@ -209,14 +209,16 @@ namespace scopeone::core::internal std::optional cameraSetup, QString providerId, QString hostKey, - QJsonObject options, + QString deviceName, + QVariantMap preInitProperties, QObject* parent = nullptr) : QObject(parent) , m_driverPath(std::move(driverPath)) , m_cameraSetup(std::move(cameraSetup)) , m_providerId(std::move(providerId)) , m_hostKey(std::move(hostKey)) - , m_options(std::move(options)) + , m_deviceName(std::move(deviceName)) + , m_preInitProperties(std::move(preInitProperties)) { } @@ -254,7 +256,7 @@ namespace scopeone::core::internal return response; } - bool createProvider(); + bool createDevice(); struct CameraTransport { @@ -322,7 +324,8 @@ namespace scopeone::core::internal std::optional m_cameraSetup; QString m_providerId; QString m_hostKey; - QJsonObject m_options; + QString m_deviceName; + QVariantMap m_preInitProperties; QString m_lastError; HardwareProviderDescriptor m_descriptor; QList m_devices; @@ -330,17 +333,16 @@ namespace scopeone::core::internal QHash> m_cameraTransports; mutable QReadWriteLock m_cameraTransportsLock; std::unique_ptr m_driverLoader; - HardwareProviderPtr m_provider; + DevicePtr m_device; std::shared_ptr m_microManagerCamera; - CameraProvider* m_camera{nullptr}; - DevicePropertyProvider* m_properties{nullptr}; - StageProvider* m_stage{nullptr}; - ShutterProvider* m_shutter{nullptr}; - AutofocusProvider* m_autofocus{nullptr}; - StateProvider* m_state{nullptr}; - ConfigurationProvider* m_configuration{nullptr}; - SignalSourceProvider* m_signals{nullptr}; - DaqProvider* m_daq{nullptr}; + Camera* m_camera{nullptr}; + Device* m_properties{nullptr}; + Stage* m_stage{nullptr}; + Shutter* m_shutter{nullptr}; + Autofocus* m_autofocus{nullptr}; + StateDevice* m_state{nullptr}; + SignalSource* m_signals{nullptr}; + Daq* m_daq{nullptr}; QList m_signalSources; QList m_daqDevices; QHash> m_streams; @@ -352,14 +354,14 @@ namespace scopeone::core::internal std::atomic_bool m_acceptStreams{false}; }; - // A Micro-Manager camera is built into DriverHost, a driver comes from its library - bool DriverRuntime::createProvider() + // A Micro-Manager camera is built into DriverHost, a device of a driver comes from its library + bool DriverRuntime::createDevice() { if (m_cameraSetup) { m_microManagerCamera = MicroManagerCamera::load(*m_cameraSetup, true, &m_lastError); - m_provider = m_microManagerCamera; - return m_provider != nullptr; + m_device = m_microManagerCamera; + return m_device != nullptr; } m_driverLoader = std::make_unique(m_driverPath); DriverManifest manifest; @@ -396,13 +398,21 @@ namespace scopeone::core::internal return false; } - m_options.insert(QStringLiteral("providerId"), m_providerId); - m_provider = factory->createProvider(m_options, &m_lastError); - if (!m_provider) + const QStringList deviceNames = factory->deviceNames(); + const QString deviceName = m_deviceName.isEmpty() && !deviceNames.isEmpty() + ? deviceNames.first() + : m_deviceName; + if (!deviceNames.contains(deviceName)) + { + m_lastError = QStringLiteral("The driver has no device %1").arg(deviceName); + return false; + } + m_device = factory->createDevice(deviceName, m_preInitProperties, &m_lastError); + if (!m_device) { if (m_lastError.isEmpty()) { - m_lastError = QStringLiteral("Provider module returned no provider"); + m_lastError = QStringLiteral("The driver returned no device"); } return false; } @@ -412,11 +422,11 @@ namespace scopeone::core::internal bool DriverRuntime::initializeRuntime() { m_lastError.clear(); - if (!createProvider()) + if (!createDevice()) { return false; } - m_descriptor = m_provider->descriptor(); + m_descriptor = m_device->descriptor(); if (m_descriptor.id != m_providerId || m_descriptor.id != m_descriptor.id.trimmed()) { @@ -424,20 +434,19 @@ namespace scopeone::core::internal return false; } - HardwareProvider* const provider = m_provider.get(); - m_camera = dynamic_cast(provider); - m_properties = dynamic_cast(provider); - m_stage = dynamic_cast(provider); - m_shutter = dynamic_cast(provider); - m_autofocus = dynamic_cast(provider); - m_state = dynamic_cast(provider); - m_configuration = dynamic_cast(provider); - m_signals = dynamic_cast(provider); - m_daq = dynamic_cast(provider); + Device* const provider = m_device.get(); + m_camera = dynamic_cast(provider); + m_properties = provider; + m_stage = dynamic_cast(provider); + m_shutter = dynamic_cast(provider); + m_autofocus = dynamic_cast(provider); + m_state = dynamic_cast(provider); + m_signals = dynamic_cast(provider); + m_daq = dynamic_cast(provider); QSet logicalIds; int cameraIndex = 0; - for (HardwareDeviceDescriptor device : m_provider->devices()) + for (HardwareDeviceDescriptor device : m_device->devices()) { const QString logicalId = device.logicalId.trimmed(); const bool capabilityAvailable = @@ -797,7 +806,6 @@ namespace scopeone::core::internal { result.append(driverhost::kCapabilityAutofocus); } - if (m_configuration) result.append(driverhost::kCapabilityConfiguration); return result; } @@ -1085,7 +1093,7 @@ namespace scopeone::core::internal QTimer::singleShot(50, this, [this]() { emit shutdownRequested(); }); return; } - if (!m_provider) + if (!m_device) { reply(makeErrorResponse(type, requestId, QStringLiteral("Provider is not initialized"))); return; @@ -1115,15 +1123,8 @@ namespace scopeone::core::internal return; } - const bool configurationCommand = - type == driverhost::kCommandListConfigGroups - || type == driverhost::kCommandListConfigs - || type == driverhost::kCommandGetCurrentConfig - || type == driverhost::kCommandSetConfig - || type == driverhost::kCommandGetConfigSettings; - const HardwareDeviceDescriptor* const device = - configurationCommand ? nullptr : requestedDevice(message); - if (!configurationCommand && !device) + const HardwareDeviceDescriptor* const device = requestedDevice(message); + if (!device) { reply(makeErrorResponse(type, requestId, QStringLiteral("Unknown provider device"))); return; @@ -1707,67 +1708,6 @@ namespace scopeone::core::internal return; } - // The same list decides the commands that need no device, so a configuration command is - // never accepted there and then missed here - if (configurationCommand) - { - if (!m_configuration) - { - unsupported(driverhost::kCapabilityConfiguration); - return; - } - if (type == driverhost::kCommandListConfigGroups) - { - QJsonObject response = makeResponse(type, requestId, true); - response.insert(QStringLiteral("groups"), - QJsonArray::fromStringList( - m_configuration->availableConfigGroups())); - reply(response); - return; - } - const QString group = message.value(QStringLiteral("group")).toString(); - if (type == driverhost::kCommandGetConfigSettings) - { - QJsonArray settings; - for (const ConfigSetting& setting : - m_configuration->configSettings(group, message.value(QStringLiteral("config")).toString())) - { - settings.append(QJsonObject{{QStringLiteral("device"), setting.device}, - {QStringLiteral("property"), setting.property}, - {QStringLiteral("value"), setting.value}}); - } - QJsonObject response = makeResponse(type, requestId, true); - response.insert(QStringLiteral("settings"), settings); - reply(response); - return; - } - if (type == driverhost::kCommandListConfigs) - { - QJsonObject response = makeResponse(type, requestId, true); - response.insert(QStringLiteral("configs"), - QJsonArray::fromStringList( - m_configuration->availableConfigs(group))); - reply(response); - return; - } - if (type == driverhost::kCommandGetCurrentConfig) - { - QJsonObject response = makeResponse(type, requestId, true); - response.insert(QStringLiteral("config"), - m_configuration->currentConfig(group)); - reply(response); - return; - } - QString error; - const bool ok = m_configuration->setConfig( - group, - message.value(QStringLiteral("config")).toString(), - &error); - reply(ok ? makeResponse(type, requestId, true) - : makeErrorResponse(type, requestId, error)); - return; - } - reply(makeErrorResponse(type, requestId, QStringLiteral("Unknown control command"))); } @@ -1816,8 +1756,7 @@ namespace scopeone::core::internal m_shutter = nullptr; m_autofocus = nullptr; m_state = nullptr; - m_configuration = nullptr; - m_provider.reset(); + m_device.reset(); { QWriteLocker locker(&m_cameraTransportsLock); m_cameraTransports.clear(); @@ -2108,9 +2047,12 @@ int main(int argc, char* argv[]) QCommandLineOption optHostKey(QStringLiteral("hostKey"), QStringLiteral("DriverHost instance key"), QStringLiteral("key")); - QCommandLineOption optProviderOption(QStringLiteral("option"), - QStringLiteral("JSON provider options"), - QStringLiteral("json")); + QCommandLineOption optDeviceName(QStringLiteral("device"), + QStringLiteral("Device name in the driver, the first one by default"), + QStringLiteral("name")); + QCommandLineOption optProperties(QStringLiteral("properties"), + QStringLiteral("JSON pre-init properties of the device"), + QStringLiteral("json")); QCommandLineOption optLauncherPid(QStringLiteral("launcherPid"), QStringLiteral("Process ID of the launching app"), QStringLiteral("pid")); @@ -2119,7 +2061,8 @@ int main(int argc, char* argv[]) parser.addOption(optDriver); parser.addOption(optCamera); parser.addOption(optHostKey); - parser.addOption(optProviderOption); + parser.addOption(optDeviceName); + parser.addOption(optProperties); parser.addOption(optLauncherPid); parser.process(app); @@ -2162,8 +2105,8 @@ int main(int argc, char* argv[]) } cameraSetup = scopeone::core::internal::MicroManagerCameraSetup::fromJson(object); } - QJsonObject providerOptions; - for (const QString& encodedOptions : parser.values(optProviderOption)) + QJsonObject preInitProperties; + for (const QString& encodedOptions : parser.values(optProperties)) { QJsonObject object; if (!parseObject(encodedOptions, object)) @@ -2172,12 +2115,13 @@ int main(int argc, char* argv[]) } for (auto it = object.constBegin(); it != object.constEnd(); ++it) { - providerOptions.insert(it.key(), it.value()); + preInitProperties.insert(it.key(), it.value()); } } auto* runtime = new scopeone::core::internal::DriverRuntime( - driverPath, std::move(cameraSetup), providerId, hostKey, providerOptions); + driverPath, std::move(cameraSetup), providerId, hostKey, + parser.value(optDeviceName).trimmed(), preInitProperties.toVariantMap()); scopeone::core::internal::DriverHost driverHost(hostKey, runtime); if (!driverHost.start()) { diff --git a/ScopeOneCore/src/HardwareRuntime.cpp b/ScopeOneCore/src/HardwareRuntime.cpp index 009dcc1..bdc83b7 100644 --- a/ScopeOneCore/src/HardwareRuntime.cpp +++ b/ScopeOneCore/src/HardwareRuntime.cpp @@ -3,7 +3,7 @@ #include "internal/DaqDeviceManager.h" #include "internal/DriverHostClient.h" #include "internal/SignalSourceManager.h" -#include "scopeone/CameraProvider.h" +#include "scopeone/Camera.h" #include #include @@ -61,20 +61,20 @@ namespace scopeone::core::internal return match; } - ShutterProvider* defaultShutter(const DeviceRegistry& registry, QString* shutter) + Shutter* defaultShutter(const DeviceRegistry& registry, QString* shutter) { - return uniqueDefault(registry, &ShutterProvider::defaultShutter, HardwareDeviceKind::Shutter, shutter); + return uniqueDefault(registry, &Shutter::defaultShutter, HardwareDeviceKind::Shutter, shutter); } - AutofocusProvider* defaultAutofocus(const DeviceRegistry& registry, QString* device) + Autofocus* defaultAutofocus(const DeviceRegistry& registry, QString* device) { - return uniqueDefault(registry, &AutofocusProvider::defaultAutofocus, HardwareDeviceKind::AutoFocus, device); + return uniqueDefault(registry, &Autofocus::defaultAutofocus, HardwareDeviceKind::AutoFocus, device); } void stopCameras(const DeviceRegistry& registry, const QString& providerId) { - const HardwareProviderPtr provider = registry.provider(providerId); - auto* cameraProvider = dynamic_cast(provider.get()); + const DevicePtr provider = registry.provider(providerId); + auto* cameraProvider = dynamic_cast(provider.get()); if (!cameraProvider) { return; @@ -88,11 +88,11 @@ namespace scopeone::core::internal } } - void detachCameraProvider(const DeviceRegistry& registry, const QString& providerId) + void detachCamera(const DeviceRegistry& registry, const QString& providerId) { stopCameras(registry, providerId); - const HardwareProviderPtr provider = registry.provider(providerId); - if (auto* cameraProvider = dynamic_cast(provider.get())) + const DevicePtr provider = registry.provider(providerId); + if (auto* cameraProvider = dynamic_cast(provider.get())) { cameraProvider->setPreviewStateSink({}); } @@ -122,7 +122,7 @@ namespace scopeone::core::internal } bool DeviceRegistry::registerProvider( - const HardwareProviderPtr& provider, + const DevicePtr& provider, const HardwareProviderDescriptor& descriptor, const QList& devices) { @@ -237,14 +237,14 @@ namespace scopeone::core::internal return {}; } - HardwareProviderPtr DeviceRegistry::provider(const QString& providerId) const + DevicePtr DeviceRegistry::provider(const QString& providerId) const { QReadLocker locker(&m_lock); const auto it = m_providers.constFind(providerId.trimmed()); - return it == m_providers.constEnd() ? HardwareProviderPtr{} : it->provider; + return it == m_providers.constEnd() ? DevicePtr{} : it->provider; } - HardwareProviderPtr DeviceRegistry::providerForDevice(const QString& logicalId) const + DevicePtr DeviceRegistry::providerForDevice(const QString& logicalId) const { const QString normalizedId = logicalId.trimmed(); QReadLocker locker(&m_lock); @@ -278,35 +278,35 @@ namespace scopeone::core::internal registerProvider(m_microManager); } - CameraProvider* HardwareRuntime::cameraProviderForDevice(const QString& logicalId) const + Camera* HardwareRuntime::cameraForDevice(const QString& logicalId) const { - const HardwareProviderPtr provider = m_registry.providerForDevice(logicalId); - return dynamic_cast(provider.get()); + const DevicePtr provider = m_registry.providerForDevice(logicalId); + return dynamic_cast(provider.get()); } - DevicePropertyProvider* HardwareRuntime::propertyProviderForDevice( + Device* HardwareRuntime::propertyDeviceForDevice( const QString& logicalId) const { - const HardwareProviderPtr provider = m_registry.providerForDevice(logicalId); - return dynamic_cast(provider.get()); + const DevicePtr provider = m_registry.providerForDevice(logicalId); + return dynamic_cast(provider.get()); } - StageProvider* HardwareRuntime::stageProviderForDevice(const QString& logicalId) const + Stage* HardwareRuntime::stageForDevice(const QString& logicalId) const { - const HardwareProviderPtr provider = m_registry.providerForDevice(logicalId); - return dynamic_cast(provider.get()); + const DevicePtr provider = m_registry.providerForDevice(logicalId); + return dynamic_cast(provider.get()); } - ShutterProvider* HardwareRuntime::shutterProviderForDevice(const QString& logicalId) const + Shutter* HardwareRuntime::shutterForDevice(const QString& logicalId) const { - const HardwareProviderPtr provider = m_registry.providerForDevice(logicalId); - return dynamic_cast(provider.get()); + const DevicePtr provider = m_registry.providerForDevice(logicalId); + return dynamic_cast(provider.get()); } - StateProvider* HardwareRuntime::stateProviderForDevice(const QString& logicalId) const + StateDevice* HardwareRuntime::stateDeviceForDevice(const QString& logicalId) const { - const HardwareProviderPtr provider = m_registry.providerForDevice(logicalId); - return dynamic_cast(provider.get()); + const DevicePtr provider = m_registry.providerForDevice(logicalId); + return dynamic_cast(provider.get()); } ConfigurationProvider* HardwareRuntime::configurationProviderForGroup( @@ -320,7 +320,7 @@ namespace scopeone::core::internal ConfigurationProvider* match = nullptr; for (const HardwareProviderDescriptor& descriptor : m_registry.providers()) { - const HardwareProviderPtr provider = m_registry.provider(descriptor.id); + const DevicePtr provider = m_registry.provider(descriptor.id); auto* configurationProvider = dynamic_cast(provider.get()); if (!configurationProvider || !configurationProvider->availableConfigGroups().contains(normalizedGroup)) @@ -358,7 +358,7 @@ namespace scopeone::core::internal bool HardwareRuntime::isPreviewRunning(const QString& cameraId) const { - CameraProvider* provider = cameraProviderForDevice(cameraId); + Camera* provider = cameraForDevice(cameraId); return provider && provider->isPreviewRunning(cameraId); } @@ -367,7 +367,7 @@ namespace scopeone::core::internal const QString target = cameraIdOrAll.trimmed(); if (target.compare(QStringLiteral("All"), Qt::CaseInsensitive) != 0) { - CameraProvider* provider = cameraProviderForDevice(target); + Camera* provider = cameraForDevice(target); return provider && provider->getExposure(target, exposureMs); } bool found = false; @@ -378,7 +378,7 @@ namespace scopeone::core::internal { continue; } - CameraProvider* provider = cameraProviderForDevice(device.logicalId); + Camera* provider = cameraForDevice(device.logicalId); double deviceExposureMs = 0.0; if (!provider || !provider->getExposure(device.logicalId, deviceExposureMs)) { @@ -403,7 +403,7 @@ namespace scopeone::core::internal const QString target = cameraIdOrAll.trimmed(); if (target.compare(QStringLiteral("All"), Qt::CaseInsensitive) != 0) { - CameraProvider* provider = cameraProviderForDevice(target); + Camera* provider = cameraForDevice(target); return provider && provider->setExposure(target, exposureMs); } bool ok = false; @@ -414,7 +414,7 @@ namespace scopeone::core::internal { continue; } - CameraProvider* provider = cameraProviderForDevice(device.logicalId); + Camera* provider = cameraForDevice(device.logicalId); ok = true; all = provider && provider->setExposure(device.logicalId, exposureMs) && all; } @@ -423,7 +423,7 @@ namespace scopeone::core::internal QStringList HardwareRuntime::listProperties(const QString& cameraId) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider ? provider->listProperties(cameraId) : QStringList{}; } @@ -431,7 +431,7 @@ namespace scopeone::core::internal const QString& name, bool fromCache) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider ? provider->getProperty(cameraId, name, fromCache) : QString{}; } @@ -440,50 +440,50 @@ namespace scopeone::core::internal const QString& value, QString* errorMessage) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider && provider->setProperty(cameraId, name, value, errorMessage); } QString HardwareRuntime::getPropertyType(const QString& cameraId, const QString& name) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider ? provider->getPropertyType(cameraId, name) : QStringLiteral("Unknown"); } bool HardwareRuntime::isPropertyReadOnly(const QString& cameraId, const QString& name) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return !provider || provider->isPropertyReadOnly(cameraId, name); } bool HardwareRuntime::isPropertyPreInit(const QString& cameraId, const QString& name) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider && provider->isPropertyPreInit(cameraId, name); } QStringList HardwareRuntime::getAllowedPropertyValues(const QString& cameraId, const QString& name) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider ? provider->getAllowedPropertyValues(cameraId, name) : QStringList{}; } bool HardwareRuntime::hasPropertyLimits(const QString& cameraId, const QString& name) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider && provider->hasPropertyLimits(cameraId, name); } double HardwareRuntime::getPropertyLowerLimit(const QString& cameraId, const QString& name) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider ? provider->getPropertyLowerLimit(cameraId, name) : 0.0; } double HardwareRuntime::getPropertyUpperLimit(const QString& cameraId, const QString& name) { - DevicePropertyProvider* provider = propertyProviderForDevice(cameraId); + Device* provider = propertyDeviceForDevice(cameraId); return provider ? provider->getPropertyUpperLimit(cameraId, name) : 0.0; } @@ -493,13 +493,13 @@ namespace scopeone::core::internal int width, int height) { - CameraProvider* provider = cameraProviderForDevice(cameraId); + Camera* provider = cameraForDevice(cameraId); return provider && provider->setROI(cameraId, x, y, width, height); } bool HardwareRuntime::clearROI(const QString& cameraId) { - CameraProvider* provider = cameraProviderForDevice(cameraId); + Camera* provider = cameraForDevice(cameraId); return provider && provider->clearROI(cameraId); } @@ -509,7 +509,7 @@ namespace scopeone::core::internal int& width, int& height) { - CameraProvider* provider = cameraProviderForDevice(cameraId); + Camera* provider = cameraForDevice(cameraId); return provider && provider->getROI(cameraId, x, y, width, height); } @@ -517,13 +517,13 @@ namespace scopeone::core::internal ImageFrame& frame, int timeoutMs) { - CameraProvider* provider = cameraProviderForDevice(cameraId); + Camera* provider = cameraForDevice(cameraId); return provider && provider->captureEventFrame(cameraId, frame, timeoutMs); } bool HardwareRuntime::supportsEventSequence(const QString& cameraId) const { - CameraProvider* provider = cameraProviderForDevice(cameraId); + Camera* provider = cameraForDevice(cameraId); return provider && provider->supportsEventSequence(cameraId); } @@ -532,7 +532,7 @@ namespace scopeone::core::internal std::vector& frames, int timeoutMs) { - CameraProvider* provider = cameraProviderForDevice(cameraId); + Camera* provider = cameraForDevice(cameraId); return provider && provider->captureEventSequence(cameraId, count, frames, timeoutMs); } @@ -543,7 +543,7 @@ namespace scopeone::core::internal for (const QString& cameraId : cameraIds) { const QString normalizedId = cameraId.trimmed(); - const HardwareProviderPtr provider = m_registry.providerForDevice(normalizedId); + const DevicePtr provider = m_registry.providerForDevice(normalizedId); if (auto* source = dynamic_cast(provider.get())) { QStringList& providerCameraIds = grouped[source]; @@ -658,14 +658,14 @@ namespace scopeone::core::internal QString HardwareRuntime::defaultXYStage() const { QString stage; - uniqueDefault(m_registry, &StageProvider::defaultXYStage, HardwareDeviceKind::XYStage, &stage); + uniqueDefault(m_registry, &Stage::defaultXYStage, HardwareDeviceKind::XYStage, &stage); return stage; } QString HardwareRuntime::defaultZStage() const { QString stage; - uniqueDefault(m_registry, &StageProvider::defaultZStage, HardwareDeviceKind::ZStage, &stage); + uniqueDefault(m_registry, &Stage::defaultZStage, HardwareDeviceKind::ZStage, &stage); return stage; } @@ -674,8 +674,8 @@ namespace scopeone::core::internal double& y, QString* errorMessage) const { - return forward(stageProviderForDevice(deviceId), "Stage provider not available", errorMessage, - [&](StageProvider& provider) + return forward(stageForDevice(deviceId), "Stage provider not available", errorMessage, + [&](Stage& provider) { return provider.getXYPosition(deviceId, x, y, errorMessage); }); @@ -685,8 +685,8 @@ namespace scopeone::core::internal double& z, QString* errorMessage) const { - return forward(stageProviderForDevice(deviceId), "Stage provider not available", errorMessage, - [&](StageProvider& provider) + return forward(stageForDevice(deviceId), "Stage provider not available", errorMessage, + [&](Stage& provider) { return provider.getZPosition(deviceId, z, errorMessage); }); @@ -697,8 +697,8 @@ namespace scopeone::core::internal double dy, QString* errorMessage) { - return forward(stageProviderForDevice(deviceId), "Stage provider not available", errorMessage, - [&](StageProvider& provider) + return forward(stageForDevice(deviceId), "Stage provider not available", errorMessage, + [&](Stage& provider) { return provider.setRelativeXYPosition(deviceId, dx, dy, errorMessage); }); @@ -708,8 +708,8 @@ namespace scopeone::core::internal double dz, QString* errorMessage) { - return forward(stageProviderForDevice(deviceId), "Stage provider not available", errorMessage, - [&](StageProvider& provider) + return forward(stageForDevice(deviceId), "Stage provider not available", errorMessage, + [&](Stage& provider) { return provider.setRelativeZPosition(deviceId, dz, errorMessage); }); @@ -720,8 +720,8 @@ namespace scopeone::core::internal double y, QString* errorMessage) { - return forward(stageProviderForDevice(deviceId), "Stage provider not available", errorMessage, - [&](StageProvider& provider) + return forward(stageForDevice(deviceId), "Stage provider not available", errorMessage, + [&](Stage& provider) { return provider.setXYPosition(deviceId, x, y, errorMessage); }); @@ -731,8 +731,8 @@ namespace scopeone::core::internal double z, QString* errorMessage) { - return forward(stageProviderForDevice(deviceId), "Stage provider not available", errorMessage, - [&](StageProvider& provider) + return forward(stageForDevice(deviceId), "Stage provider not available", errorMessage, + [&](Stage& provider) { return provider.setZPosition(deviceId, z, errorMessage); }); @@ -742,8 +742,8 @@ namespace scopeone::core::internal bool& open, QString* errorMessage) const { - return forward(shutterProviderForDevice(deviceId), "Shutter provider not available", errorMessage, - [&](ShutterProvider& provider) + return forward(shutterForDevice(deviceId), "Shutter provider not available", errorMessage, + [&](Shutter& provider) { return provider.isShutterOpen(deviceId, open, errorMessage); }); @@ -753,8 +753,8 @@ namespace scopeone::core::internal bool open, QString* errorMessage) { - return forward(shutterProviderForDevice(deviceId), "Shutter provider not available", errorMessage, - [&](ShutterProvider& provider) + return forward(shutterForDevice(deviceId), "Shutter provider not available", errorMessage, + [&](Shutter& provider) { return provider.setShutterOpen(deviceId, open, errorMessage); }); @@ -764,8 +764,8 @@ namespace scopeone::core::internal long& state, QString* errorMessage) const { - return forward(stateProviderForDevice(deviceId), "State provider not available", errorMessage, - [&](StateProvider& provider) + return forward(stateDeviceForDevice(deviceId), "State provider not available", errorMessage, + [&](StateDevice& provider) { return provider.getState(deviceId, state, errorMessage); }); @@ -775,8 +775,8 @@ namespace scopeone::core::internal long state, QString* errorMessage) { - return forward(stateProviderForDevice(deviceId), "State provider not available", errorMessage, - [&](StateProvider& provider) + return forward(stateDeviceForDevice(deviceId), "State provider not available", errorMessage, + [&](StateDevice& provider) { return provider.setState(deviceId, state, errorMessage); }); @@ -784,7 +784,7 @@ namespace scopeone::core::internal QString HardwareRuntime::stateLabel(const QString& deviceId, long state) const { - StateProvider* provider = stateProviderForDevice(deviceId); + StateDevice* provider = stateDeviceForDevice(deviceId); return provider ? provider->stateLabel(deviceId, state) : QString{}; } @@ -793,7 +793,7 @@ namespace scopeone::core::internal QHash groupCounts; for (const HardwareProviderDescriptor& descriptor : m_registry.providers()) { - const HardwareProviderPtr provider = m_registry.provider(descriptor.id); + const DevicePtr provider = m_registry.provider(descriptor.id); if (auto* configurationProvider = dynamic_cast(provider.get())) { QSet providerGroups; @@ -858,7 +858,7 @@ namespace scopeone::core::internal bool HardwareRuntime::autoShutter(bool& enabled, QString* errorMessage) const { QString shutter; - ShutterProvider* const provider = defaultShutter(m_registry, &shutter); + Shutter* const provider = defaultShutter(m_registry, &shutter); enabled = provider && provider->autoShutter(); if (!provider && errorMessage) { @@ -871,14 +871,14 @@ namespace scopeone::core::internal { QString shutter; return forward(defaultShutter(m_registry, &shutter), "No shutter lights the cameras", errorMessage, - [&](ShutterProvider& provider) { return provider.setAutoShutter(enabled, errorMessage); }); + [&](Shutter& provider) { return provider.setAutoShutter(enabled, errorMessage); }); } bool HardwareRuntime::setDefaultShutterOpen(bool open, QString* errorMessage) { QString shutter; return forward(defaultShutter(m_registry, &shutter), "No shutter lights the cameras", errorMessage, - [&](ShutterProvider& provider) { return provider.setShutterOpen(shutter, open, errorMessage); }); + [&](Shutter& provider) { return provider.setShutterOpen(shutter, open, errorMessage); }); } bool HardwareRuntime::hasAutofocus() const @@ -891,12 +891,12 @@ namespace scopeone::core::internal { QString device; return forward(defaultAutofocus(m_registry, &device), "Autofocus is not available", errorMessage, - [&](AutofocusProvider& provider) { return provider.runAutofocus(device, errorMessage); }); + [&](Autofocus& provider) { return provider.runAutofocus(device, errorMessage); }); } long HardwareRuntime::focusSequenceCapacity(const QString& focusDevice) const { - StageProvider* provider = stageProviderForDevice(focusDevice); + Stage* provider = stageForDevice(focusDevice); return provider ? provider->stageSequenceCapacity(focusDevice) : 0; } @@ -904,8 +904,8 @@ namespace scopeone::core::internal const std::vector& positions, QString* errorMessage) { - return forward(stageProviderForDevice(focusDevice), "Stage provider not available", errorMessage, - [&](StageProvider& provider) + return forward(stageForDevice(focusDevice), "Stage provider not available", errorMessage, + [&](Stage& provider) { return provider.startStageSequence(focusDevice, positions, errorMessage); }); @@ -913,7 +913,7 @@ namespace scopeone::core::internal void HardwareRuntime::stopFocusSequence(const QString& focusDevice) { - if (StageProvider* provider = stageProviderForDevice(focusDevice)) + if (Stage* provider = stageForDevice(focusDevice)) { provider->stopStageSequence(focusDevice); } @@ -921,7 +921,7 @@ namespace scopeone::core::internal long HardwareRuntime::propertySequenceCapacity(const QString& device, const QString& property) const { - DevicePropertyProvider* provider = propertyProviderForDevice(device); + Device* provider = propertyDeviceForDevice(device); return provider ? provider->propertySequenceCapacity(device, property) : 0; } @@ -932,7 +932,7 @@ namespace scopeone::core::internal for (std::size_t index = 0; index < sequences.size(); ++index) { const PropertySequence& sequence = sequences[index]; - DevicePropertyProvider* provider = propertyProviderForDevice(sequence.device); + Device* provider = propertyDeviceForDevice(sequence.device); const QStringList values(sequence.values.begin(), sequence.values.end()); if (!provider || !provider->startPropertySequence(sequence.device, sequence.property, values, errorMessage)) { @@ -947,7 +947,7 @@ namespace scopeone::core::internal { for (const PropertySequence& sequence : sequences) { - if (DevicePropertyProvider* provider = propertyProviderForDevice(sequence.device)) + if (Device* provider = propertyDeviceForDevice(sequence.device)) { provider->stopPropertySequence(sequence.device, sequence.property); } @@ -961,16 +961,17 @@ namespace scopeone::core::internal double HardwareRuntime::pixelSizeUm(const QString& cameraId) const { - CameraProvider* provider = cameraProviderForDevice(cameraId); + Camera* provider = cameraForDevice(cameraId); return provider ? provider->pixelSizeUm(cameraId) : 0.0; } - HardwareProviderPtr HardwareRuntime::startScopeOneProvider(const QString& providerId, - const QString& driverPath, - const QJsonObject& options, - QString* errorMessage) + DevicePtr HardwareRuntime::startScopeOneProvider(const QString& providerId, + const QString& driverPath, + const QString& deviceName, + const QVariantMap& preInitProperties, + QString* errorMessage) { - return createScopeOneProvider(providerId, driverPath, options, errorMessage); + return createScopeOneProvider(providerId, driverPath, deviceName, preInitProperties, errorMessage); } QStringList HardwareRuntime::microManagerAdapterSearchPaths() const @@ -995,7 +996,7 @@ namespace scopeone::core::internal return m_microManager->startCameras(cameras); } - QList HardwareRuntime::takeMicroManagerCameras() + QList HardwareRuntime::takeMicroManagerCameras() { return m_microManager->takeCameras(); } @@ -1036,22 +1037,22 @@ namespace scopeone::core::internal } } - bool HardwareRuntime::registerProvider(const HardwareProviderPtr& provider) + bool HardwareRuntime::registerProvider(const DevicePtr& provider) { if (!provider) { return false; } const HardwareProviderDescriptor descriptor = provider->descriptor(); - const HardwareProviderPtr previous = m_registry.provider(descriptor.id); + const DevicePtr previous = m_registry.provider(descriptor.id); if (previous && previous != provider) { return false; } - auto* cameraProvider = dynamic_cast(provider.get()); - auto* stageProvider = dynamic_cast(provider.get()); - auto* shutterProvider = dynamic_cast(provider.get()); - auto* stateProvider = dynamic_cast(provider.get()); + auto* cameraProvider = dynamic_cast(provider.get()); + auto* stageProvider = dynamic_cast(provider.get()); + auto* shutterProvider = dynamic_cast(provider.get()); + auto* stateProvider = dynamic_cast(provider.get()); const QList providerDevices = provider->devices(); for (const HardwareDeviceDescriptor& device : providerDevices) { @@ -1063,7 +1064,7 @@ namespace scopeone::core::internal && (device.kind != HardwareDeviceKind::Shutter || shutterProvider) && (device.kind != HardwareDeviceKind::State || stateProvider) && (device.kind != HardwareDeviceKind::AutoFocus - || dynamic_cast(provider.get())); + || dynamic_cast(provider.get())); if (!supported) { return false; @@ -1114,13 +1115,13 @@ namespace scopeone::core::internal void HardwareRuntime::unregisterProvider(const QString& providerId) { - detachCameraProvider(m_registry, providerId); + detachCamera(m_registry, providerId); m_registry.unregisterProvider(providerId); } bool HardwareRuntime::refreshProvider(const QString& providerId) { - const HardwareProviderPtr provider = m_registry.provider(providerId); + const DevicePtr provider = m_registry.provider(providerId); return provider && registerProvider(provider); } diff --git a/ScopeOneCore/src/MDAManager.cpp b/ScopeOneCore/src/MDAManager.cpp index e6333b3..9470601 100644 --- a/ScopeOneCore/src/MDAManager.cpp +++ b/ScopeOneCore/src/MDAManager.cpp @@ -1,7 +1,7 @@ #include "internal/MDAManager.h" #include "internal/HardwareRuntime.h" -#include "scopeone/HardwareCapabilities.h" +#include "scopeone/ConfigSetting.h" #include #include diff --git a/ScopeOneCore/src/MicroManagerProvider.cpp b/ScopeOneCore/src/MicroManagerProvider.cpp index 9d308c8..f531b23 100644 --- a/ScopeOneCore/src/MicroManagerProvider.cpp +++ b/ScopeOneCore/src/MicroManagerProvider.cpp @@ -57,7 +57,7 @@ namespace scopeone::core::internal } // Each DriverHost loads its camera on its own, which can take seconds, so they start // together - QList> hosts; + QList> hosts; for (const MicroManagerCameraSetup& setup : cameras) { hosts.append(QtConcurrent::run([setup]() @@ -66,7 +66,7 @@ namespace scopeone::core::internal const QStringList arguments{ QStringLiteral("--micromanagerCamera"), QString::fromUtf8(QJsonDocument(setup.toJson()).toJson(QJsonDocument::Compact))}; - HardwareProviderPtr host = startDriverHost(micromanager::descriptor().id, arguments, &error); + DevicePtr host = startDriverHost(micromanager::descriptor().id, arguments, &error); if (!host) { qWarning().noquote() << QStringLiteral("Failed to start the DriverHost of camera '%1': %2") @@ -78,15 +78,15 @@ namespace scopeone::core::internal QStringList failed; for (qsizetype index = 0; index < cameras.size(); ++index) { - const HardwareProviderPtr host = hosts[index].result(); + const DevicePtr host = hosts[index].result(); if (!host) { failed.append(cameras[index].cameraId); continue; } - Camera entry; + CameraEntry entry; entry.provider = host; - entry.camera = dynamic_cast(host.get()); + entry.camera = dynamic_cast(host.get()); entry.frameSource = dynamic_cast(host.get()); entry.camera->setPreviewStateSink(m_previewStateSink); entry.frameSource->setFrameDelivery(m_delivery); @@ -128,7 +128,7 @@ namespace scopeone::core::internal } }); camera->setPreviewStateSink(m_previewStateSink); - Camera entry; + CameraEntry entry; entry.provider = camera; entry.camera = camera.get(); entry.local = camera; @@ -138,16 +138,16 @@ namespace scopeone::core::internal return true; } - QList MicroManagerProvider::takeCameras() + QList MicroManagerProvider::takeCameras() { - QMap taken; + QMap taken; { QWriteLocker locker(&m_camerasLock); taken.swap(m_cameras); m_binnings.clear(); } - QList providers; - for (const Camera& entry : std::as_const(taken)) + QList providers; + for (const CameraEntry& entry : std::as_const(taken)) { entry.camera->setPreviewStateSink({}); providers.append(entry.provider); @@ -157,17 +157,17 @@ namespace scopeone::core::internal MicroManagerProvider::CameraRef MicroManagerProvider::camera(const QString& cameraId) const { - const Camera entry = cameraEntry(cameraId); + const CameraEntry entry = cameraEntry(cameraId); return {entry.provider, entry.camera}; } - MicroManagerProvider::Camera MicroManagerProvider::cameraEntry(const QString& cameraId) const + MicroManagerProvider::CameraEntry MicroManagerProvider::cameraEntry(const QString& cameraId) const { QReadLocker locker(&m_camerasLock); return m_cameras.value(cameraId.trimmed()); } - QList MicroManagerProvider::cameras() const + QList MicroManagerProvider::cameras() const { QReadLocker locker(&m_camerasLock); return m_cameras.values(); @@ -176,7 +176,7 @@ namespace scopeone::core::internal void MicroManagerProvider::setFrameDelivery(CameraFrameDelivery* delivery) { m_delivery = delivery; - for (const Camera& entry : cameras()) + for (const CameraEntry& entry : cameras()) { if (entry.frameSource) { @@ -189,7 +189,7 @@ namespace scopeone::core::internal { for (const QString& cameraId : cameraIds) { - const Camera entry = cameraEntry(cameraId); + const CameraEntry entry = cameraEntry(cameraId); if (entry.local) { entry.local->setFrameDeliveryMode(mode); @@ -207,7 +207,7 @@ namespace scopeone::core::internal { for (const QString& cameraId : cameraIds) { - const Camera entry = cameraEntry(cameraId); + const CameraEntry entry = cameraEntry(cameraId); if (entry.local) { entry.local->setFrameDeliveryPaused(paused); @@ -227,7 +227,7 @@ namespace scopeone::core::internal void MicroManagerProvider::setPreviewStateSink(PreviewStateSink sink) { m_previewStateSink = std::move(sink); - for (const Camera& entry : cameras()) + for (const CameraEntry& entry : cameras()) { entry.camera->setPreviewStateSink(m_previewStateSink); } @@ -267,7 +267,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->listProperties(cameraId.trimmed()); + return provider.owner->listProperties(cameraId.trimmed()); } return orElse(QStringList{}, [&] { @@ -281,7 +281,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->getProperty(cameraId.trimmed(), name, fromCache); + return provider.owner->getProperty(cameraId.trimmed(), name, fromCache); } return orElse(QString{}, [&] { @@ -301,7 +301,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - const bool ok = provider->setProperty(cameraId.trimmed(), name, value, errorMessage); + const bool ok = provider.owner->setProperty(cameraId.trimmed(), name, value, errorMessage); if (ok && name.trimmed() == QLatin1String(MM::g_Keyword_Binning)) { readBinning(cameraId.trimmed()); @@ -321,7 +321,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->getPropertyType(cameraId.trimmed(), name); + return provider.owner->getPropertyType(cameraId.trimmed(), name); } return micromanager::propertyType(*m_core, cameraId, name); } @@ -330,7 +330,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->isPropertyReadOnly(cameraId.trimmed(), name); + return provider.owner->isPropertyReadOnly(cameraId.trimmed(), name); } return orElse(true, [&] { @@ -342,7 +342,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->isPropertyPreInit(cameraId.trimmed(), name); + return provider.owner->isPropertyPreInit(cameraId.trimmed(), name); } return orElse(false, [&] { @@ -355,7 +355,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->getAllowedPropertyValues(cameraId.trimmed(), name); + return provider.owner->getAllowedPropertyValues(cameraId.trimmed(), name); } return orElse(QStringList{}, [&] { @@ -368,7 +368,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->hasPropertyLimits(cameraId.trimmed(), name); + return provider.owner->hasPropertyLimits(cameraId.trimmed(), name); } return orElse(false, [&] { @@ -380,7 +380,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->getPropertyLowerLimit(cameraId.trimmed(), name); + return provider.owner->getPropertyLowerLimit(cameraId.trimmed(), name); } return orElse(0.0, [&] { @@ -392,7 +392,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(cameraId)) { - return provider->getPropertyUpperLimit(cameraId.trimmed(), name); + return provider.owner->getPropertyUpperLimit(cameraId.trimmed(), name); } return orElse(0.0, [&] { @@ -784,7 +784,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(deviceId)) { - return provider->propertySequenceCapacity(deviceId.trimmed(), name); + return provider.owner->propertySequenceCapacity(deviceId.trimmed(), name); } return micromanager::propertySequenceCapacity(*m_core, deviceId, name); } @@ -796,7 +796,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(deviceId)) { - return provider->startPropertySequence(deviceId.trimmed(), name, values, errorMessage); + return provider.owner->startPropertySequence(deviceId.trimmed(), name, values, errorMessage); } return micromanager::startPropertySequence(*m_core, deviceId, name, values, errorMessage); } @@ -805,7 +805,7 @@ namespace scopeone::core::internal { if (const CameraRef provider = camera(deviceId)) { - provider->stopPropertySequence(deviceId.trimmed(), name); + provider.owner->stopPropertySequence(deviceId.trimmed(), name); return; } micromanager::stopPropertySequence(*m_core, deviceId, name); @@ -816,7 +816,7 @@ namespace scopeone::core::internal // a DriverHost is scaled by its own binning here. double MicroManagerProvider::pixelSizeUm(const QString& cameraId) const { - const Camera entry = cameraEntry(cameraId); + const CameraEntry entry = cameraEntry(cameraId); if (!entry.camera) { return 0.0; @@ -834,7 +834,7 @@ namespace scopeone::core::internal { const CameraRef provider = camera(cameraId); // Cameras name their binning "2" or "2x2" - const QString value = provider->getProperty(cameraId, QLatin1String(MM::g_Keyword_Binning), false); + const QString value = provider.owner->getProperty(cameraId, QLatin1String(MM::g_Keyword_Binning), false); const int binning = (std::max)(1, value.section(QLatin1Char('x'), 0, 0).toInt()); QWriteLocker locker(&m_camerasLock); m_binnings.insert(cameraId, binning); diff --git a/ScopeOneCore/src/ScopeOneCore.cpp b/ScopeOneCore/src/ScopeOneCore.cpp index 34f7e81..bd9809e 100644 --- a/ScopeOneCore/src/ScopeOneCore.cpp +++ b/ScopeOneCore/src/ScopeOneCore.cpp @@ -348,7 +348,7 @@ namespace constexpr qint64 kPreviewRefreshIntervalMs = 16; struct ProviderRegistrationResult { - scopeone::core::HardwareProviderPtr provider; + scopeone::core::DevicePtr provider; QString errorMessage; }; @@ -1209,14 +1209,13 @@ namespace scopeone::core } watcher->deleteLater(); }); - const QJsonObject providerOptions = QJsonObject::fromVariantMap(options); watcher->setFuture(QtConcurrent::run( m_hardwareThreadPool.get(), - [normalizedId, modulePath, providerOptions]() + [normalizedId, modulePath, options]() { ProviderRegistrationResult result; result.provider = HardwareRuntime::startScopeOneProvider( - normalizedId, modulePath, providerOptions, &result.errorMessage); + normalizedId, modulePath, QString{}, options, &result.errorMessage); return result; })); return true; diff --git a/ScopeOneCore/src/SignalSourceManager.cpp b/ScopeOneCore/src/SignalSourceManager.cpp index 364d96e..a8ab00c 100644 --- a/ScopeOneCore/src/SignalSourceManager.cpp +++ b/ScopeOneCore/src/SignalSourceManager.cpp @@ -21,7 +21,7 @@ namespace scopeone::core::internal SignalSourceManager::~SignalSourceManager() { - for (const HardwareProviderPtr& provider : std::as_const(m_attachedProviders)) + for (const DevicePtr& provider : std::as_const(m_attachedProviders)) { detachProvider(provider); } @@ -53,16 +53,16 @@ namespace scopeone::core::internal } SignalAcquisitionConfig normalized = config; normalized.sourceId = sourceId; - const HardwareProviderPtr provider = m_providerBySource.value(sourceId); - return dynamic_cast(provider.get())->startSignal(normalized, errorMessage); + const DevicePtr provider = m_providerBySource.value(sourceId); + return dynamic_cast(provider.get())->startSignal(normalized, errorMessage); } void SignalSourceManager::stopTrace(const QString& sourceId) { const QString id = sourceId.trimmed(); - if (const HardwareProviderPtr provider = m_providerBySource.value(id)) + if (const DevicePtr provider = m_providerBySource.value(id)) { - dynamic_cast(provider.get())->stopSignal(id); + dynamic_cast(provider.get())->stopSignal(id); } } @@ -80,11 +80,11 @@ namespace scopeone::core::internal // driver that provides signal sources join the list, and their chunks are passed on void SignalSourceManager::refreshProviderSources() { - QHash current; + QHash current; for (const HardwareProviderDescriptor& descriptor : m_registry->providers()) { - const HardwareProviderPtr provider = m_registry->provider(descriptor.id); - if (dynamic_cast(provider.get())) + const DevicePtr provider = m_registry->provider(descriptor.id); + if (dynamic_cast(provider.get())) { current.insert(descriptor.id, provider); } @@ -107,7 +107,7 @@ namespace scopeone::core::internal { continue; } - auto* const signalProvider = dynamic_cast(it.value().get()); + auto* const signalProvider = dynamic_cast(it.value().get()); const QPointer self(this); signalProvider->setSignalSinks( [self](const TimeSeriesChunk& chunk) @@ -150,9 +150,9 @@ namespace scopeone::core::internal } } - void SignalSourceManager::detachProvider(const HardwareProviderPtr& provider) + void SignalSourceManager::detachProvider(const DevicePtr& provider) { - dynamic_cast(provider.get())->setSignalSinks({}, {}, {}); + dynamic_cast(provider.get())->setSignalSinks({}, {}, {}); for (auto it = m_providerBySource.begin(); it != m_providerBySource.end();) { if (it.value() != provider) diff --git a/drivers/ExampleCamera/ExampleCameraDriver.cpp b/drivers/ExampleCamera/ExampleCameraDriver.cpp index f83487a..aa73900 100644 --- a/drivers/ExampleCamera/ExampleCameraDriver.cpp +++ b/drivers/ExampleCamera/ExampleCameraDriver.cpp @@ -22,11 +22,12 @@ namespace example_camera { - using scopeone::core::CameraProvider; + using scopeone::core::Camera; using scopeone::core::HardwareDeviceDescriptor; using scopeone::core::HardwareDeviceKind; using scopeone::core::HardwareDeviceState; - using scopeone::core::HardwareProvider; + using scopeone::core::Device; + using scopeone::core::Hub; using scopeone::core::HardwareProviderDescriptor; using scopeone::core::ImageFrame; using scopeone::core::ImagePixelFormat; @@ -36,10 +37,10 @@ namespace example_camera constexpr double kMaximumExposureMs = 1000.0; // One camera. It stands in for the handle a vendor SDK gives out for each camera it finds. - class Camera + class ExampleCamera { public: - Camera(QString id, std::shared_ptr patterns, int sensorWidth, int sensorHeight) + ExampleCamera(QString id, std::shared_ptr patterns, int sensorWidth, int sensorHeight) : m_id(std::move(id)) , m_patterns(std::move(patterns)) , m_sensorWidth(sensorWidth) @@ -49,14 +50,14 @@ namespace example_camera { } - ~Camera() + ~ExampleCamera() { stop(); } const QString& id() const { return m_id; } - void setFrameSink(CameraProvider::FrameSink sink) + void setFrameSink(Camera::FrameSink sink) { QMutexLocker locker(&m_mutex); m_sink = std::move(sink); @@ -73,7 +74,7 @@ namespace example_camera while (m_running.load()) { const ImageFrame frame = expose(); - CameraProvider::FrameSink sink; + Camera::FrameSink sink; { QMutexLocker locker(&m_mutex); sink = m_sink; @@ -237,7 +238,7 @@ namespace example_camera const int m_sensorHeight; mutable QMutex m_mutex; QMutex m_readoutMutex; - CameraProvider::FrameSink m_sink; + Camera::FrameSink m_sink; double m_exposureMs{10.0}; int m_gain{1}; TestPattern m_pattern{TestPattern::MovingSpot}; @@ -252,12 +253,12 @@ namespace example_camera std::thread m_thread; }; - // One provider that offers several cameras, as the driver of a camera maker does when its SDK - // finds several. ScopeOne addresses each camera by its id. - class MultiCameraProvider final : public HardwareProvider, public CameraProvider + // One device that offers several cameras, as the driver of a camera maker does when its SDK + // finds several. It is a hub, and ScopeOne addresses each camera by its id. + class ExampleCameraDevice final : public Device, public Hub, public Camera { public: - MultiCameraProvider(const QString& providerId, int cameraCount, int width, int height) + ExampleCameraDevice(const QString& providerId, int cameraCount, int width, int height) : m_providerId(providerId) { const auto patterns = std::make_shared(width, height); @@ -266,7 +267,7 @@ namespace example_camera const QString id = cameraCount == 1 ? QStringLiteral("camera.example") : QStringLiteral("camera.example.%1").arg(index + 1); - m_cameras.push_back(std::make_unique(id, patterns, width, height)); + m_cameras.push_back(std::make_unique(id, patterns, width, height)); } } @@ -275,7 +276,7 @@ namespace example_camera return {m_providerId, QStringLiteral("Example Camera"), QStringLiteral("1")}; } - QList devices() const override + QList peripherals() const override { QList result; for (size_t index = 0; index < m_cameras.size(); ++index) @@ -310,7 +311,7 @@ namespace example_camera bool startPreviewFor(const QString& cameraId) override { - Camera* camera = find(cameraId); + ExampleCamera* camera = find(cameraId); if (!camera) { return false; @@ -322,7 +323,7 @@ namespace example_camera bool stopPreviewFor(const QString& cameraId) override { - Camera* camera = find(cameraId); + ExampleCamera* camera = find(cameraId); if (!camera) { return false; @@ -334,13 +335,13 @@ namespace example_camera bool isPreviewRunning(const QString& cameraId) const override { - const Camera* camera = find(cameraId); + const ExampleCamera* camera = find(cameraId); return camera && camera->isRunning(); } bool getExposure(const QString& cameraId, double& exposureMs) const override { - const Camera* camera = find(cameraId); + const ExampleCamera* camera = find(cameraId); if (!camera) { return false; @@ -351,25 +352,25 @@ namespace example_camera bool setExposure(const QString& cameraId, double exposureMs) override { - Camera* camera = find(cameraId); + ExampleCamera* camera = find(cameraId); return camera && camera->setExposureMs(exposureMs); } bool setROI(const QString& cameraId, int x, int y, int width, int height) override { - Camera* camera = find(cameraId); + ExampleCamera* camera = find(cameraId); return camera && camera->setRoi(x, y, width, height); } bool clearROI(const QString& cameraId) override { - Camera* camera = find(cameraId); + ExampleCamera* camera = find(cameraId); return camera && camera->clearRoi(); } bool getROI(const QString& cameraId, int& x, int& y, int& width, int& height) override { - Camera* camera = find(cameraId); + ExampleCamera* camera = find(cameraId); if (!camera) { return false; @@ -381,7 +382,7 @@ namespace example_camera // A software trigger: one exposure, read out while the caller waits bool captureEventFrame(const QString& cameraId, ImageFrame& frame, int timeoutMs) override { - Camera* camera = find(cameraId); + ExampleCamera* camera = find(cameraId); if (!camera || camera->isRunning() || camera->exposureMs() > timeoutMs) { return false; @@ -400,7 +401,7 @@ namespace example_camera QString getProperty(const QString& deviceId, const QString& name, bool) override { - Camera* camera = find(deviceId); + ExampleCamera* camera = find(deviceId); if (!camera) { return {}; @@ -425,7 +426,7 @@ namespace example_camera const QString& value, QString* errorMessage) override { - Camera* camera = find(deviceId); + ExampleCamera* camera = find(deviceId); TestPattern pattern = TestPattern::MovingSpot; if (camera && name == QStringLiteral("Pattern") && testPatternFromName(value, pattern)) { @@ -490,7 +491,7 @@ namespace example_camera } private: - Camera* find(const QString& cameraId) const + ExampleCamera* find(const QString& cameraId) const { for (const auto& camera : m_cameras) { @@ -524,7 +525,7 @@ namespace example_camera } QString m_providerId; - std::vector> m_cameras; + std::vector> m_cameras; QMutex m_mutex; PreviewStateSink m_previewStateSink; bool m_lastPreviewState{false}; @@ -543,18 +544,26 @@ namespace example_camera return QStringLiteral("example.camera"); } - // Options: width and height of the sensor, and cameraCount, the number of cameras (2 by default) - scopeone::core::HardwareProviderPtr createProvider(const QJsonObject& options, - QString* errorMessage) override + QStringList deviceNames() const override + { + return {QStringLiteral("Example Camera")}; + } + + // Pre-init properties: width and height of the sensor, and cameraCount, the number of + // cameras (2 by default) + scopeone::core::DevicePtr createDevice(const QString& name, + const QVariantMap& preInitProperties, + QString* errorMessage) override { if (errorMessage) { errorMessage->clear(); } - const int width = options.value(QStringLiteral("width")).toInt(512); - const int height = options.value(QStringLiteral("height")).toInt(512); - const int cameraCount = options.value(QStringLiteral("cameraCount")).toInt(2); - if (width <= 0 || height <= 0 || cameraCount < 1 || cameraCount > 8) + const int width = preInitProperties.value(QStringLiteral("width"), 512).toInt(); + const int height = preInitProperties.value(QStringLiteral("height"), 512).toInt(); + const int cameraCount = preInitProperties.value(QStringLiteral("cameraCount"), 2).toInt(); + if (name != deviceNames().first() + || width <= 0 || height <= 0 || cameraCount < 1 || cameraCount > 8) { if (errorMessage) { @@ -562,7 +571,7 @@ namespace example_camera } return {}; } - return std::make_shared(providerId(), cameraCount, width, height); + return std::make_shared(providerId(), cameraCount, width, height); } }; } diff --git a/drivers/ExampleCamera/driver.json b/drivers/ExampleCamera/driver.json index 94a249f..a539442 100644 --- a/drivers/ExampleCamera/driver.json +++ b/drivers/ExampleCamera/driver.json @@ -2,7 +2,7 @@ "id": "example.camera", "name": "Example Camera", "version": "1.0.0", - "scopeOneApi": 2, + "scopeOneApi": 3, "kind": "driver", "providerId": "example.camera", "autoLoad": false diff --git a/drivers/ExamplePmt/ExamplePmtDriver.cpp b/drivers/ExamplePmt/ExamplePmtDriver.cpp index e824022..ab17b3a 100644 --- a/drivers/ExamplePmt/ExamplePmtDriver.cpp +++ b/drivers/ExamplePmt/ExamplePmtDriver.cpp @@ -305,9 +305,9 @@ namespace example_pmt } } - void setSinks(SignalSourceProvider::TimeSeriesSink timeSeries, - SignalSourceProvider::TimestampedEventSink timestampedEvents, - SignalSourceProvider::StateSink state) + void setSinks(SignalSource::TimeSeriesSink timeSeries, + SignalSource::TimestampedEventSink timestampedEvents, + SignalSource::StateSink state) { QMutexLocker locker(&m_sinkMutex); m_timeSeriesSink = std::move(timeSeries); @@ -400,9 +400,9 @@ namespace example_pmt } QMutex m_sinkMutex; - SignalSourceProvider::TimeSeriesSink m_timeSeriesSink; - SignalSourceProvider::TimestampedEventSink m_timestampedEventSink; - SignalSourceProvider::StateSink m_stateSink; + SignalSource::TimeSeriesSink m_timeSeriesSink; + SignalSource::TimestampedEventSink m_timestampedEventSink; + SignalSource::StateSink m_stateSink; std::atomic_bool m_running{false}; std::jthread m_worker; }; @@ -590,7 +590,7 @@ namespace example_pmt } } - class PmtProvider final : public HardwareProvider, public SignalSourceProvider + class PmtDevice final : public Device, public SignalSource { public: HardwareProviderDescriptor descriptor() const override @@ -598,12 +598,6 @@ namespace example_pmt return {QString::fromLatin1(kSourceId), QStringLiteral("Example PMT"), QStringLiteral("1.0.0")}; } - // The PMT is listed as a signal source, not as a device with properties - QList devices() const override - { - return {}; - } - QList signalSources() const override { return {sourceDescriptor()}; @@ -653,13 +647,23 @@ namespace example_pmt return QString::fromLatin1(kSourceId); } - HardwareProviderPtr createProvider(const QJsonObject&, QString* errorMessage) override + QStringList deviceNames() const override + { + return {QStringLiteral("Example PMT")}; + } + + DevicePtr createDevice(const QString& name, const QVariantMap&, QString* errorMessage) override { if (errorMessage) { errorMessage->clear(); } - return std::make_shared(); + if (name != deviceNames().first()) + { + if (errorMessage) *errorMessage = QStringLiteral("Unknown device %1").arg(name); + return {}; + } + return std::make_shared(); } }; } diff --git a/drivers/ExamplePmt/driver.json b/drivers/ExamplePmt/driver.json index 62a534a..9882e0d 100644 --- a/drivers/ExamplePmt/driver.json +++ b/drivers/ExamplePmt/driver.json @@ -2,7 +2,7 @@ "id": "example.pmt", "name": "Example PMT", "version": "1.0.0", - "scopeOneApi": 2, + "scopeOneApi": 3, "kind": "driver", "providerId": "example.pmt", "autoLoad": false diff --git a/drivers/NIDaqmx/NIDaqmxDriver.cpp b/drivers/NIDaqmx/NIDaqmxDriver.cpp index da62242..ab26b70 100644 --- a/drivers/NIDaqmx/NIDaqmxDriver.cpp +++ b/drivers/NIDaqmx/NIDaqmxDriver.cpp @@ -1305,10 +1305,10 @@ namespace ni_daqmx // its own. The sources of one device run in one analog input task. A device runs one such task // at a time, so its sources and a session that reads its analog inputs do not run together, // and NI-DAQmx says so when the later one starts. - class NIDaqmxProvider final : public HardwareProvider, public DaqProvider, public SignalSourceProvider + class NIDaqmxDevice final : public Device, public Hub, public Daq, public SignalSource { public: - explicit NIDaqmxProvider(QList devices) + explicit NIDaqmxDevice(QList devices) : m_devices(std::move(devices)) { for (const DaqDeviceDescriptor& device : m_devices) @@ -1339,8 +1339,9 @@ namespace ni_daqmx return {QStringLiteral("scopeone.nidaqmx"), QStringLiteral("NI-DAQmx"), QStringLiteral("1.0.0")}; } - // The DAQ devices are listed as DAQ devices, not as devices with properties - QList devices() const override + // The boards and their channels are listed by daqDevices() and signalSources(), not as + // devices with properties + QList peripherals() const override { return {}; } @@ -1350,7 +1351,7 @@ namespace ni_daqmx return m_devices; } - void setDaqSinks(InputSink input, DaqProvider::StateSink state) override + void setDaqSinks(InputSink input, Daq::StateSink state) override { QMutexLocker locker(&m_sinkMutex); m_inputSink = std::move(input); @@ -1386,7 +1387,7 @@ namespace ni_daqmx void setSignalSinks(TimeSeriesSink timeSeries, TimestampedEventSink, - SignalSourceProvider::StateSink state) override + SignalSource::StateSink state) override { QMutexLocker locker(&m_sinkMutex); m_timeSeriesSink = std::move(timeSeries); @@ -1480,9 +1481,9 @@ namespace ni_daqmx QHash m_sourceDevices; QMutex m_sinkMutex; InputSink m_inputSink; - DaqProvider::StateSink m_stateSink; + Daq::StateSink m_stateSink; TimeSeriesSink m_timeSeriesSink; - SignalSourceProvider::StateSink m_signalStateSink; + SignalSource::StateSink m_signalStateSink; // Declared last so they are destroyed first: their readers call the sinks above until joined QHash> m_controllers; QHash> m_aiGroups; @@ -1500,9 +1501,19 @@ namespace ni_daqmx return QStringLiteral("scopeone.nidaqmx"); } - // Fails when the NI-DAQmx runtime is not installed, so the driver can be turned off under Tools > Drivers - HardwareProviderPtr createProvider(const QJsonObject&, QString* errorMessage) override + QStringList deviceNames() const override { + return {QStringLiteral("NI-DAQmx")}; + } + + // Fails when the NI-DAQmx runtime is not installed + DevicePtr createDevice(const QString& name, const QVariantMap&, QString* errorMessage) override + { + if (name != deviceNames().first()) + { + if (errorMessage) *errorMessage = QStringLiteral("Unknown device %1").arg(name); + return {}; + } QString error; QList devices = findDevices(&error); if (!error.isEmpty()) @@ -1511,7 +1522,7 @@ namespace ni_daqmx return {}; } if (errorMessage) errorMessage->clear(); - return std::make_shared(std::move(devices)); + return std::make_shared(std::move(devices)); } }; } diff --git a/drivers/NIDaqmx/driver.json b/drivers/NIDaqmx/driver.json index 5b0d687..84148df 100644 --- a/drivers/NIDaqmx/driver.json +++ b/drivers/NIDaqmx/driver.json @@ -2,7 +2,7 @@ "id": "scopeone.nidaqmx", "name": "NI-DAQmx", "version": "1.0.0", - "scopeOneApi": 2, + "scopeOneApi": 3, "kind": "driver", "providerId": "scopeone.nidaqmx", "autoLoad": true diff --git a/drivers/README.md b/drivers/README.md index a23c21f..7ff4071 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -16,13 +16,14 @@ files. Those adapters are not ScopeOne drivers. ## Writing a driver A driver has one entry class, a `Driver`. ScopeOne_DriverHost loads the library, gets that object -from Qt's plugin loader and calls `createProvider`, which returns a `HardwareProvider` with the -capabilities its devices have (camera, stage, shutter, state, configuration, signal source, DAQ). +from Qt's plugin loader and calls `createDevice` with one of its `deviceNames()`, which returns a +`Device` with the capabilities it has (`Camera`, `Stage`, `Shutter`, `StateDevice`, `Autofocus`, +`SignalSource`, `Daq`). A device with peripherals is also a `Hub`. A signal source, such as a PMT or a photon counter, puts out one value over time: -`SignalSourceProvider` hands over its chunks of samples or photon events with the scan markers, +`SignalSource` hands over its chunks of samples or photon events with the scan markers, and ScopeOne shows them on the oscilloscope screen of the Signal Monitor, up to four sources at once on one time axis, and assembles scan images from them. A -`DaqProvider` runs the timed tasks of a DAQ session and hands over the inputs it reads. From then +`Daq` runs the timed tasks of a DAQ session and hands over the inputs it reads. From then on ScopeOne only exchanges messages and data with the host. The host does not link ScopeOneCore, so a driver process holds only the host, the SDK library, Qt Core and Network, and the driver. @@ -62,7 +63,7 @@ the drivers into `ScopeOneCore/install/bin/drivers`, from where the GUI build co "id": "my.camera", "name": "My Camera", "version": "1.0.0", - "scopeOneApi": 2, + "scopeOneApi": 3, "kind": "driver", "providerId": "my.camera", "autoLoad": false @@ -82,8 +83,10 @@ class MyCameraDriver final : public QObject, public scopeone::core::Driver public: QString providerId() const override; - scopeone::core::HardwareProviderPtr createProvider(const QJsonObject& options, - QString* errorMessage) override; + QStringList deviceNames() const override; + scopeone::core::DevicePtr createDevice(const QString& name, + const QVariantMap& preInitProperties, + QString* errorMessage) override; }; ``` From cd903bcbbd88d599b5f09db78fc6af809aab43fe Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 17:30:00 +0100 Subject: [PATCH 35/49] Load native devices from ScopeOneDevice lines of the cfg through ScopeOneProvider, with the cfg label as device id --- ScopeOneCore/README.md | 2 +- .../include/scopeone/DriverManifest.h | 3 - ScopeOneCore/include/scopeone/ScopeOneCore.h | 11 +- ScopeOneCore/internal/DriverHostClient.h | 13 +- ScopeOneCore/internal/HardwareRuntime.h | 22 +-- ScopeOneCore/internal/MicroManagerProvider.h | 3 + ScopeOneCore/internal/ScopeOneProvider.h | 65 +++++++ ScopeOneCore/sdk/include/scopeone/Driver.h | 2 +- ScopeOneCore/src/DriverHostClient.cpp | 181 ++++++++++++++++-- ScopeOneCore/src/DriverHostMain.cpp | 28 ++- ScopeOneCore/src/DriverManifest.cpp | 14 -- ScopeOneCore/src/HardwareRuntime.cpp | 43 +++-- .../src/MicroManagerConfiguration.cpp | 38 ++-- ScopeOneCore/src/ScopeOneCore.cpp | 107 +++-------- ScopeOneCore/src/ScopeOneProvider.cpp | 178 +++++++++++++++++ config/MMConfig_demo.cfg | 5 + drivers/ExampleCamera/ExampleCameraDriver.cpp | 13 +- drivers/ExampleCamera/driver.json | 4 +- drivers/ExamplePmt/ExamplePmtDriver.cpp | 2 +- drivers/ExamplePmt/driver.json | 4 +- drivers/NIDaqmx/NIDaqmxDriver.cpp | 2 +- drivers/NIDaqmx/driver.json | 4 +- drivers/README.md | 6 +- src/DriversDialog.cpp | 173 ++--------------- src/DriversDialog.h | 12 +- src/main.cpp | 5 - 26 files changed, 558 insertions(+), 382 deletions(-) create mode 100644 ScopeOneCore/internal/ScopeOneProvider.h create mode 100644 ScopeOneCore/src/ScopeOneProvider.cpp diff --git a/ScopeOneCore/README.md b/ScopeOneCore/README.md index 30ff5a3..4ac6aae 100644 --- a/ScopeOneCore/README.md +++ b/ScopeOneCore/README.md @@ -115,7 +115,7 @@ The installed headers in `include/scopeone` and `include/sdk/scopeone` are the s External code should enter through these headers and `scopeone::core::ScopeOneCore`. Internal managers are implementation details and must not become alternate access paths. -Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `Camera::FrameSink`. The sink copies a frame before it returns, so a frame can point into the buffer of the camera SDK through `QByteArray::fromRawData` instead of being copied by the driver first. Core loads a driver with `ScopeOneCore::registerDriverHostProvider(providerId, modulePath, options)`, which returns false with an error message when the DriverHost cannot start it. One DriverHost process owns the complete Provider and registers all of its cameras and control devices together. Micro-Manager remains the built-in provider. It runs the camera of a configuration with one camera in ScopeOne, and each camera of a configuration with more in a DriverHost of its own, so two cameras of the same model do not share one vendor library. +Providers use ScopeOne logical device IDs and publish `ImageFrame` objects through `Camera::FrameSink`. The sink copies a frame before it returns, so a frame can point into the buffer of the camera SDK through `QByteArray::fromRawData` instead of being copied by the driver first. Core loads the `ScopeOneDevice` lines of the configuration file: each starts one device of a driver in a DriverHost of its own, with the `ScopeOneProperty` lines of its label as pre-init properties, and registers it under that label. Micro-Manager remains the built-in provider. It runs the camera of a configuration with one camera in ScopeOne, and each camera of a configuration with more in a DriverHost of its own, so two cameras of the same model do not share one vendor library. Inside Core, the rest of Core reaches hardware only through `HardwareRuntime`. It routes each call to the provider of the device and answers the calls that span providers itself, such as all cameras, the default stages and shutter, and the frame delivery to the live view, live processing and recording. Every provider, `MicroManagerProvider` as well as each driver, offers its devices through the same interfaces of the driver SDK, so a driver can do what Micro-Manager does here, from hardware sequences to autofocus. How frames are delivered and paused is ScopeOne's own policy and stays out of the driver SDK. diff --git a/ScopeOneCore/include/scopeone/DriverManifest.h b/ScopeOneCore/include/scopeone/DriverManifest.h index c577b25..79902ef 100644 --- a/ScopeOneCore/include/scopeone/DriverManifest.h +++ b/ScopeOneCore/include/scopeone/DriverManifest.h @@ -16,7 +16,6 @@ namespace scopeone::core QString id; QString name; QString version; - bool autoLoad{false}; QJsonObject metadata; }; @@ -35,8 +34,6 @@ namespace scopeone::core // The drivers directory beside the application and in the user data directory SCOPEONE_CORE_EXPORT QStringList driverDirectories(); - SCOPEONE_CORE_EXPORT QString driverSettingsKey(const QString& driverId, const QString& name); - SCOPEONE_CORE_EXPORT bool driverEnabled(const DriverManifest& manifest); SCOPEONE_CORE_EXPORT QList discoverDrivers(const QStringList& directories = {}); SCOPEONE_CORE_EXPORT bool parseDriverManifest(const QJsonObject& metadata, DriverManifest& manifest, diff --git a/ScopeOneCore/include/scopeone/ScopeOneCore.h b/ScopeOneCore/include/scopeone/ScopeOneCore.h index 862ae2e..0b07f4f 100644 --- a/ScopeOneCore/include/scopeone/ScopeOneCore.h +++ b/ScopeOneCore/include/scopeone/ScopeOneCore.h @@ -263,10 +263,6 @@ namespace scopeone::core QStringList cameraIds() const { return m_cameraIds; } QList hardwareDevices() const; - bool registerDriverHostProvider(const QString& providerId, - const QString& modulePath, - const QVariantMap& options = {}, - QString* errorMessage = nullptr); QStringList runningPreviewCameraIds() const; double cameraPixelSizeUm(const QString& cameraId) const; bool setCameraPixelSizeUm(const QString& cameraId, double pixelSizeUm); @@ -638,9 +634,9 @@ namespace scopeone::core void finishConfigurationLoadFailure(const LoadConfigResult& result, const QString& errorMessage); void clearConfigurationRuntime(bool notify, bool stopCameras); - // Stops the Micro-Manager cameras on the hardware thread, as their DriverHosts can take a - // second to exit, and then calls next - void stopMicroManagerCameras(std::function next); + // Stops the DriverHosts of the Micro-Manager cameras and ScopeOne devices on the hardware + // thread, as they can take a second to exit, and then calls next + void stopDriverHosts(std::function next); void startConfigurationLoadTask(const QString& configPath); void startConfigurationLoadCleanupTask(const LoadConfigResult& result, const QString& errorMessage); @@ -742,7 +738,6 @@ namespace scopeone::core QHash m_layerFrameCounts; QMap m_layerFrameRates; QSet m_sessionsSaving; - QSet m_pendingProviderRegistrations; enum class ConfigurationState { Unloaded, diff --git a/ScopeOneCore/internal/DriverHostClient.h b/ScopeOneCore/internal/DriverHostClient.h index 0a62a83..609a6bb 100644 --- a/ScopeOneCore/internal/DriverHostClient.h +++ b/ScopeOneCore/internal/DriverHostClient.h @@ -19,11 +19,10 @@ namespace scopeone::core::internal QString* errorMessage = nullptr); // One device of a ScopeOne driver, created by its name in the driver, the first one when the - // name is empty. Each driver is a provider of its own, as each camera of a Micro-Manager - // configuration is a device of MicroManagerProvider. - DevicePtr createScopeOneProvider(const QString& providerId, - const QString& driverPath, - const QString& deviceName, - const QVariantMap& preInitProperties, - QString* errorMessage = nullptr); + // name is empty. The provider id of the device is the label the configuration gives it. + DevicePtr createScopeOneDevice(const QString& label, + const QString& driverPath, + const QString& deviceName, + const QVariantMap& preInitProperties, + QString* errorMessage = nullptr); } diff --git a/ScopeOneCore/internal/HardwareRuntime.h b/ScopeOneCore/internal/HardwareRuntime.h index f2d40cd..ae5166c 100644 --- a/ScopeOneCore/internal/HardwareRuntime.h +++ b/ScopeOneCore/internal/HardwareRuntime.h @@ -15,6 +15,7 @@ #include "internal/AcquisitionEngine.h" #include "internal/CameraFrameDelivery.h" #include "internal/MicroManagerProvider.h" +#include "internal/ScopeOneProvider.h" namespace scopeone::core::internal { @@ -185,14 +186,6 @@ namespace scopeone::core::internal DaqDeviceManager* daqDeviceManager() const { return m_daqDeviceManager; } double pixelSizeUm(const QString& cameraId) const; - // A driver in a DriverHost of its own. Safe to call from any thread, and the provider is - // registered afterwards on the thread of the runtime. - static DevicePtr startScopeOneProvider(const QString& providerId, - const QString& driverPath, - const QString& deviceName, - const QVariantMap& preInitProperties, - QString* errorMessage); - // The Micro-Manager configuration. Loading and unloading block, so they run on a worker // thread, while the cameras are started and stopped on the thread of the runtime. QStringList microManagerAdapterSearchPaths() const; @@ -201,20 +194,24 @@ namespace scopeone::core::internal MicroManagerProvider::LoadResult& result, QString& errorMessage); QStringList startMicroManagerCameras(const QList& cameras); - // The cameras, to be destroyed on a worker thread, as their DriverHosts take time to exit - QList takeMicroManagerCameras(); + // The ScopeOne devices of the configuration, each in a DriverHost. Loading blocks, so it + // runs on a worker thread, while the devices are registered on the thread of the runtime. + ScopeOneProvider::LoadResult loadScopeOneDevices(const QString& configPath); + // False when a device conflicts with registered hardware + bool registerScopeOneDevices(); + // The cameras of Micro-Manager and the ScopeOne devices, unregistered and to be destroyed + // on a worker thread, as their DriverHosts take time to exit + QList takeDriverHosts(); // Registers the devices again, false when one conflicts with the devices of a driver bool setMicroManagerDevices(const QList& devices); void stopMicroManagerPreview(); bool unloadMicroManagerDevices(QString* errorMessage = nullptr); void applyMicroManagerShutdownPreset(); QList devices() const { return m_registry.devices(); } - bool hasProvider(const QString& providerId) const { return m_registry.provider(providerId) != nullptr; } static QString microManagerVersion(); void clear(); bool registerProvider(const DevicePtr& provider); void unregisterProvider(const QString& providerId); - bool refreshProvider(const QString& providerId); void stopPreviewForProvider(const QString& providerId); signals: @@ -235,6 +232,7 @@ namespace scopeone::core::internal DeviceRegistry m_registry; std::shared_ptr m_microManager; + ScopeOneProvider m_scopeOne; SignalSourceManager* m_signalSourceManager{nullptr}; DaqDeviceManager* m_daqDeviceManager{nullptr}; diff --git a/ScopeOneCore/internal/MicroManagerProvider.h b/ScopeOneCore/internal/MicroManagerProvider.h index d134582..48d3119 100644 --- a/ScopeOneCore/internal/MicroManagerProvider.h +++ b/ScopeOneCore/internal/MicroManagerProvider.h @@ -20,6 +20,9 @@ class CMMCore; namespace scopeone::core::internal { + // Splits one line of a cfg file into fieldCount fields, the last of which may contain commas + QStringList splitConfigLine(const QString& line, int fieldCount); + // The devices of the loaded Micro-Manager configuration. They run on the core of ScopeOne, // except the cameras of a configuration with more than one camera: each of those runs in a // DriverHost of its own, so two cameras of the same model do not share one vendor library. diff --git a/ScopeOneCore/internal/ScopeOneProvider.h b/ScopeOneCore/internal/ScopeOneProvider.h new file mode 100644 index 0000000..71217cd --- /dev/null +++ b/ScopeOneCore/internal/ScopeOneProvider.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include +#include + +#include "scopeone/Device.h" + +namespace scopeone::core::internal +{ + // A ScopeOneDevice line of the cfg file with the ScopeOneProperty lines of its label + struct ScopeOneDeviceLine + { + QString label; + QString driverId; + QString deviceName; + QList> properties; + }; + + struct ScopeOneConfigGroupLine + { + QString group; + QString preset; + QString label; + QString property; + QString value; + }; + + // The ScopeOne lines of a cfg file. The other lines belong to Micro-Manager. + struct ScopeOneConfiguration + { + QList devices; + QList configGroups; + // Lines that name no device to start, such as a second device of a label or a property of + // a label without device, each as "label (reason)" + QStringList rejected; + }; + + ScopeOneConfiguration readScopeOneConfiguration(const QString& configPath); + + // The devices of the loaded cfg file that are not Micro-Manager's. It is the counterpart of + // MicroManagerProvider: each ScopeOneDevice line runs its device of a driver in a DriverHost of + // its own, and the runtime registers each of them under the label of the line. + class ScopeOneProvider + { + public: + struct LoadResult + { + QStringList failedDevices; + int successCount{0}; + }; + + // Starts the devices of the file together and blocks until all have started or failed + LoadResult load(const QString& configPath); + const ScopeOneConfiguration& configuration() const { return m_configuration; } + QList devices() const { return m_devices; } + // Hands over the devices, so they can be stopped away from the calling thread + QList takeDevices(); + + private: + ScopeOneConfiguration m_configuration; + QList m_devices; + }; +} diff --git a/ScopeOneCore/sdk/include/scopeone/Driver.h b/ScopeOneCore/sdk/include/scopeone/Driver.h index 82f8b61..b784708 100644 --- a/ScopeOneCore/sdk/include/scopeone/Driver.h +++ b/ScopeOneCore/sdk/include/scopeone/Driver.h @@ -16,7 +16,7 @@ namespace scopeone::core public: virtual ~Driver() = default; - virtual QString providerId() const = 0; + virtual QString driverId() const = 0; virtual QStringList deviceNames() const = 0; // The pre-init properties are the values a device needs before it can start, such as a // port, and come from the configuration of the system diff --git a/ScopeOneCore/src/DriverHostClient.cpp b/ScopeOneCore/src/DriverHostClient.cpp index 661e607..fa47167 100644 --- a/ScopeOneCore/src/DriverHostClient.cpp +++ b/ScopeOneCore/src/DriverHostClient.cpp @@ -228,13 +228,19 @@ namespace scopeone::core::internal return false; } + m_rename = m_arguments.contains(QStringLiteral("--driver")); QSet logicalIds; const QJsonArray deviceArray = response.value(driverhost::kDevicesField).toArray(); + const QJsonArray sourceArray = response.value(driverhost::kSignalSourcesField).toArray(); + const QJsonArray daqArray = response.value(driverhost::kDaqDevicesField).toArray(); + int deviceIndex = 0; for (const QJsonValue& value : deviceArray) { const QJsonObject object = value.toObject(); HardwareDeviceDescriptor device; - device.logicalId = object.value(driverhost::kDeviceIdField).toString(); + device.logicalId = m_devices.add( + object.value(driverhost::kDeviceIdField).toString(), + logicalName(deviceArray.size(), deviceIndex++)); device.providerId = descriptor.id; device.providerDeviceId = object.value(driverhost::kProviderDeviceIdField).toString(); @@ -284,13 +290,48 @@ namespace scopeone::core::internal m_cameraIds.append(device.logicalId); } } - for (const QJsonValue& value : response.value(driverhost::kSignalSourcesField).toArray()) + // A driver that exposes no device is still a device of its label + if (m_rename && deviceArray.isEmpty()) { - signalSources.append(hardwarestream::signalSourceDescriptorFromJson(value.toObject())); + HardwareDeviceDescriptor device; + device.logicalId = m_devices.add(QString{}, m_providerId); + device.providerId = descriptor.id; + device.name = m_providerId; + device.kind = HardwareDeviceKind::Generic; + device.state = HardwareDeviceState::Initialized; + device.endpoint = HardwareEndpointKind::DriverHost; + devices.append(device); } - for (const QJsonValue& value : response.value(driverhost::kDaqDevicesField).toArray()) + int sourceIndex = 0; + for (const QJsonValue& value : sourceArray) { - daqDevices.append(hardwarestream::daqDeviceDescriptorFromJson(value.toObject())); + SignalSourceDescriptor source = hardwarestream::signalSourceDescriptorFromJson(value.toObject()); + source.id = m_sources.add(source.id, logicalName(sourceArray.size(), sourceIndex++)); + signalSources.append(source); + } + int daqIndex = 0; + for (const QJsonValue& value : daqArray) + { + DaqDeviceDescriptor daq = hardwarestream::daqDeviceDescriptorFromJson(value.toObject()); + daq.id = m_daqDevices.add(daq.id, logicalName(daqArray.size(), daqIndex++)); + daqDevices.append(daq); + } + // Everything downstream of the description reads logical ids. The cameras kept the ids + // of the driver here, so the event sequence and pixel size of a renamed camera were + // looked up under the wrong id. + QJsonArray logicalDevices; + for (const QJsonValue& value : deviceArray) + { + QJsonObject object = value.toObject(); + object.insert(driverhost::kDeviceIdField, + m_devices.toLogical(object.value(driverhost::kDeviceIdField).toString())); + logicalDevices.append(object); + } + response.insert(driverhost::kDevicesField, logicalDevices); + for (const QString& field : {driverhost::kDefaultXYStageField, driverhost::kDefaultZStageField, + driverhost::kDefaultShutterField, driverhost::kDefaultAutofocusField}) + { + response.insert(field, m_devices.toLogical(response.value(field).toString())); } // The camera rings are attached now, so DriverHost can remove their names QStringList keys; @@ -388,7 +429,7 @@ namespace scopeone::core::internal const QString id = signal ? config.value(QStringLiteral("sourceId")).toString() : config.value(QStringLiteral("deviceId")).toString(); auto stream = std::make_shared(); - stream->streamId = response.value(driverhost::kStreamIdField).toString(); + stream->streamId = logicalStreamId(response.value(driverhost::kStreamIdField).toString()); const QString shmKey = response.value(driverhost::kSharedMemoryKeyField).toString(); if (!streamring::attachSharedRing(stream->shm, driverhost::sharedMemoryNativeKey(shmKey), errorMessage)) { @@ -441,6 +482,7 @@ namespace scopeone::core::internal return false; } + translateRequest(request); m_waitingRequestId = m_nextRequestId++; m_waitingResponse = {}; m_lastError.clear(); @@ -673,21 +715,30 @@ namespace scopeone::core::internal { TimeSeriesChunk chunk; if (hardwarestream::decode(record.payload, chunk) && m_streamHandlers.timeSeries) + { + chunk.sourceId = m_sources.toLogical(chunk.sourceId); m_streamHandlers.timeSeries(chunk); + } break; } case streamring::RecordType::TimestampedEvents: { TimestampedEventChunk chunk; if (hardwarestream::decode(record.payload, chunk) && m_streamHandlers.timestampedEvents) + { + chunk.sourceId = m_sources.toLogical(chunk.sourceId); m_streamHandlers.timestampedEvents(chunk); + } break; } case streamring::RecordType::DaqInput: { DaqInputChunk chunk; if (hardwarestream::decode(record.payload, chunk) && m_streamHandlers.daqInput) + { + chunk.deviceId = m_daqDevices.toLogical(chunk.deviceId); m_streamHandlers.daqInput(chunk); + } break; } case streamring::RecordType::Skip: @@ -769,7 +820,7 @@ namespace scopeone::core::internal } if (kind == driverhost::kMessageKindEvent) { - handleEvent(message); + handleEvent(translateEvent(message)); } } } @@ -899,9 +950,111 @@ namespace scopeone::core::internal } } + // The ids of a ScopeOne device as its driver names them and as ScopeOne knows them. The + // driver and DriverHost know no labels, so the ids are translated here, in the requests + // to DriverHost and in what comes back. + struct IdMap + { + QHash toDriverId; + QHash toLogicalId; + + // An empty logical id keeps the id of the driver + QString add(const QString& driverId, const QString& name) + { + const QString logicalId = name.isEmpty() ? driverId : name; + toDriverId.insert(logicalId, driverId); + if (!driverId.isEmpty()) + { + toLogicalId.insert(driverId, logicalId); + } + return logicalId; + } + QString toDriver(const QString& id) const { return toDriverId.value(id, id); } + QString toLogical(const QString& id) const { return toLogicalId.value(id, id); } + }; + + // The label for a single device, and the label with the number of the device for several + QString logicalName(qsizetype count, int index) const + { + if (!m_rename) + { + return {}; + } + return count == 1 ? m_providerId : QStringLiteral("%1.%2").arg(m_providerId).arg(index + 1); + } + + QString logicalStreamId(const QString& streamId) const + { + const qsizetype colon = streamId.indexOf(QLatin1Char(':')); + const QString kind = streamId.left(colon + 1); + const QString id = streamId.mid(colon + 1); + if (kind == QStringLiteral("signal:")) return kind + m_sources.toLogical(id); + if (kind == QStringLiteral("daq:")) return kind + m_daqDevices.toLogical(id); + return streamId; + } + + void translateRequest(QJsonObject& request) const + { + const bool daq = request.value(driverhost::kMessageTypeField).toString() + == driverhost::kCommandStopDaq; + if (request.contains(driverhost::kDeviceIdField)) + { + const QString id = request.value(driverhost::kDeviceIdField).toString(); + request.insert(driverhost::kDeviceIdField, + daq ? m_daqDevices.toDriver(id) : m_devices.toDriver(id)); + } + if (request.contains(driverhost::kSourceIdField)) + { + request.insert(driverhost::kSourceIdField, + m_sources.toDriver(request.value(driverhost::kSourceIdField).toString())); + } + QJsonObject config = request.value(driverhost::kConfigField).toObject(); + if (config.contains(QStringLiteral("sourceId"))) + { + config.insert(QStringLiteral("sourceId"), + m_sources.toDriver(config.value(QStringLiteral("sourceId")).toString())); + } + if (config.contains(QStringLiteral("deviceId"))) + { + config.insert(QStringLiteral("deviceId"), + m_daqDevices.toDriver(config.value(QStringLiteral("deviceId")).toString())); + } + if (request.contains(driverhost::kConfigField)) + { + request.insert(driverhost::kConfigField, config); + } + } + + QJsonObject translateEvent(QJsonObject event) const + { + const QString type = event.value(driverhost::kMessageTypeField).toString(); + if (event.contains(driverhost::kDeviceIdField)) + { + const QString id = event.value(driverhost::kDeviceIdField).toString(); + event.insert(driverhost::kDeviceIdField, + type == driverhost::kEventDaqState ? m_daqDevices.toLogical(id) + : m_devices.toLogical(id)); + } + if (event.contains(driverhost::kSourceIdField)) + { + event.insert(driverhost::kSourceIdField, + m_sources.toLogical(event.value(driverhost::kSourceIdField).toString())); + } + if (event.contains(driverhost::kStreamIdField)) + { + event.insert(driverhost::kStreamIdField, + logicalStreamId(event.value(driverhost::kStreamIdField).toString())); + } + return event; + } + QString m_providerId; QStringList m_arguments; QString m_hostKey; + bool m_rename{false}; + IdMap m_devices; + IdMap m_sources; + IdMap m_daqDevices; // Set and read on the transport thread only CameraFrameDelivery* m_delivery{nullptr}; PreviewHandler m_previewHandler; @@ -1833,17 +1986,17 @@ namespace scopeone::core::internal QHash m_pixelSizes; }; - DevicePtr createScopeOneProvider(const QString& providerId, - const QString& driverPath, - const QString& deviceName, - const QVariantMap& preInitProperties, - QString* errorMessage) + DevicePtr createScopeOneDevice(const QString& label, + const QString& driverPath, + const QString& deviceName, + const QVariantMap& preInitProperties, + QString* errorMessage) { - const QString normalizedProviderId = providerId.trimmed(); + const QString normalizedProviderId = label.trimmed(); const QString normalizedDriverPath = QFileInfo(driverPath).absoluteFilePath(); if (normalizedProviderId.isEmpty() || driverPath.trimmed().isEmpty()) { - if (errorMessage) *errorMessage = QStringLiteral("Provider ID and module path are required"); + if (errorMessage) *errorMessage = QStringLiteral("Label and driver path are required"); return {}; } if (!QFileInfo::exists(normalizedDriverPath)) diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index f9ddde4..06b16ad 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -330,6 +330,7 @@ namespace scopeone::core::internal HardwareProviderDescriptor m_descriptor; QList m_devices; QHash m_devicesById; + HardwareDeviceDescriptor m_rootDevice; QHash> m_cameraTransports; mutable QReadWriteLock m_cameraTransportsLock; std::unique_ptr m_driverLoader; @@ -372,13 +373,6 @@ namespace scopeone::core::internal { return false; } - if (manifest.id != m_providerId - || manifest.metadata.value(QStringLiteral("providerId")).toString().trimmed() - != m_providerId) - { - m_lastError = QStringLiteral("Provider manifest identity mismatch"); - return false; - } QObject* const instance = m_driverLoader->instance(); if (!instance) { @@ -392,9 +386,9 @@ namespace scopeone::core::internal m_lastError = QStringLiteral("Module does not implement Driver"); return false; } - if (factory->providerId() != m_providerId) + if (factory->driverId() != manifest.id) { - m_lastError = QStringLiteral("Provider module identity mismatch"); + m_lastError = QStringLiteral("Driver identity mismatch"); return false; } @@ -426,13 +420,9 @@ namespace scopeone::core::internal { return false; } + // ScopeOne names the provider, which is the cfg label of a driver device m_descriptor = m_device->descriptor(); - if (m_descriptor.id != m_providerId - || m_descriptor.id != m_descriptor.id.trimmed()) - { - m_lastError = QStringLiteral("Provider descriptor identity mismatch"); - return false; - } + m_descriptor.id = m_providerId; Device* const provider = m_device.get(); m_camera = dynamic_cast(provider); @@ -459,7 +449,6 @@ namespace scopeone::core::internal && (device.kind != HardwareDeviceKind::AutoFocus || m_autofocus); if (logicalId.isEmpty() || device.logicalId != logicalId - || device.providerId != m_providerId || logicalIds.contains(logicalId) || !capabilityAvailable) { @@ -467,6 +456,7 @@ namespace scopeone::core::internal return false; } logicalIds.insert(logicalId); + device.providerId = m_providerId; device.endpoint = HardwareEndpointKind::DriverHost; m_devices.append(device); m_devicesById.insert(logicalId, device); @@ -843,6 +833,12 @@ namespace scopeone::core::internal { const QString deviceId = message.value(driverhost::kDeviceIdField) .toString().trimmed(); + // A request without a device id addresses the Device itself, whose properties a driver + // with no peripherals has. It was answered as an unknown device before. + if (deviceId.isEmpty()) + { + return &m_rootDevice; + } const auto it = m_devicesById.constFind(deviceId); return it == m_devicesById.cend() ? nullptr : &it.value(); } diff --git a/ScopeOneCore/src/DriverManifest.cpp b/ScopeOneCore/src/DriverManifest.cpp index ffb8bea..9972209 100644 --- a/ScopeOneCore/src/DriverManifest.cpp +++ b/ScopeOneCore/src/DriverManifest.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include namespace scopeone::core @@ -21,18 +20,6 @@ namespace scopeone::core .filePath(QStringLiteral("drivers"))}; } - QString driverSettingsKey(const QString& driverId, const QString& name) - { - return QStringLiteral("Drivers/%1/%2").arg(driverId, name); - } - - bool driverEnabled(const DriverManifest& manifest) - { - QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - const QString key = driverSettingsKey(manifest.id, QStringLiteral("enabled")); - return settings.contains(key) ? settings.value(key).toBool() : manifest.autoLoad; - } - QList discoverDrivers(const QStringList& directories) { QList drivers; @@ -122,7 +109,6 @@ namespace scopeone::core manifest.id = id; manifest.name = name; manifest.version = version; - manifest.autoLoad = metadata.value(QStringLiteral("autoLoad")).toBool(); manifest.metadata = metadata; if (errorMessage) { diff --git a/ScopeOneCore/src/HardwareRuntime.cpp b/ScopeOneCore/src/HardwareRuntime.cpp index bdc83b7..3caa56c 100644 --- a/ScopeOneCore/src/HardwareRuntime.cpp +++ b/ScopeOneCore/src/HardwareRuntime.cpp @@ -1,7 +1,6 @@ #include "internal/HardwareRuntime.h" #include "internal/DaqDeviceManager.h" -#include "internal/DriverHostClient.h" #include "internal/SignalSourceManager.h" #include "scopeone/Camera.h" @@ -965,15 +964,6 @@ namespace scopeone::core::internal return provider ? provider->pixelSizeUm(cameraId) : 0.0; } - DevicePtr HardwareRuntime::startScopeOneProvider(const QString& providerId, - const QString& driverPath, - const QString& deviceName, - const QVariantMap& preInitProperties, - QString* errorMessage) - { - return createScopeOneProvider(providerId, driverPath, deviceName, preInitProperties, errorMessage); - } - QStringList HardwareRuntime::microManagerAdapterSearchPaths() const { return m_microManager->adapterSearchPaths(); @@ -996,9 +986,32 @@ namespace scopeone::core::internal return m_microManager->startCameras(cameras); } - QList HardwareRuntime::takeMicroManagerCameras() + ScopeOneProvider::LoadResult HardwareRuntime::loadScopeOneDevices(const QString& configPath) + { + return m_scopeOne.load(configPath); + } + + bool HardwareRuntime::registerScopeOneDevices() + { + for (const DevicePtr& device : m_scopeOne.devices()) + { + if (!registerProvider(device)) + { + return false; + } + } + return true; + } + + QList HardwareRuntime::takeDriverHosts() { - return m_microManager->takeCameras(); + QList hosts = m_microManager->takeCameras(); + for (const DevicePtr& device : m_scopeOne.takeDevices()) + { + unregisterProvider(device->descriptor().id); + hosts.append(device); + } + return hosts; } bool HardwareRuntime::setMicroManagerDevices(const QList& devices) @@ -1119,12 +1132,6 @@ namespace scopeone::core::internal m_registry.unregisterProvider(providerId); } - bool HardwareRuntime::refreshProvider(const QString& providerId) - { - const DevicePtr provider = m_registry.provider(providerId); - return provider && registerProvider(provider); - } - void HardwareRuntime::stopPreviewForProvider(const QString& providerId) { stopCameras(m_registry, providerId); diff --git a/ScopeOneCore/src/MicroManagerConfiguration.cpp b/ScopeOneCore/src/MicroManagerConfiguration.cpp index 3ccba0e..31ac74e 100644 --- a/ScopeOneCore/src/MicroManagerConfiguration.cpp +++ b/ScopeOneCore/src/MicroManagerConfiguration.cpp @@ -155,25 +155,6 @@ namespace scopeone::core::internal return QJsonObject{{QStringLiteral("name"), name}, {QStringLiteral("value"), value}}; } - // Splits one configuration line while preserving commas in the final field - QStringList splitConfigLine(const QString& line, int fieldCount) - { - QStringList parts; - int start = 0; - for (int i = 1; i < fieldCount; ++i) - { - const int comma = line.indexOf(QLatin1Char(','), start); - if (comma < 0) - { - return {}; - } - parts.append(line.mid(start, comma - start).trimmed()); - start = comma + 1; - } - parts.append(line.mid(start).trimmed()); - return parts; - } - struct ConfigPropertyReplay { QHash> explicitProperties; @@ -422,6 +403,25 @@ namespace scopeone::core::internal // Loads and initializes the devices, but does not start the cameras. The devices of an // earlier configuration are unloaded first. + // Splits one configuration line while preserving commas in the final field + QStringList splitConfigLine(const QString& line, int fieldCount) + { + QStringList parts; + int start = 0; + for (int i = 1; i < fieldCount; ++i) + { + const int comma = line.indexOf(QLatin1Char(','), start); + if (comma < 0) + { + return {}; + } + parts.append(line.mid(start, comma - start).trimmed()); + start = comma + 1; + } + parts.append(line.mid(start).trimmed()); + return parts; + } + bool MicroManagerProvider::loadConfiguration(const QString& configPath, LoadResult& result, QString& errorMessage) diff --git a/ScopeOneCore/src/ScopeOneCore.cpp b/ScopeOneCore/src/ScopeOneCore.cpp index bd9809e..0f9e4f0 100644 --- a/ScopeOneCore/src/ScopeOneCore.cpp +++ b/ScopeOneCore/src/ScopeOneCore.cpp @@ -10,6 +10,7 @@ #include "internal/DriverHostClient.h" #include "internal/HardwareRuntime.h" #include "internal/MicroManagerProvider.h" +#include "internal/ScopeOneProvider.h" #include "internal/CameraFrameDelivery.h" #include "internal/ParticleAnalysis.h" #include "internal/RecordingManager.h" @@ -346,12 +347,6 @@ namespace constexpr qint64 kLineProfileRefreshIntervalMs = 16; // Display delivery is bounded independently from acquisition and processing throughput constexpr qint64 kPreviewRefreshIntervalMs = 16; - struct ProviderRegistrationResult - { - scopeone::core::DevicePtr provider; - QString errorMessage; - }; - struct OfflineProcessingResult { QList frames; @@ -651,6 +646,7 @@ namespace bool success{false}; scopeone::core::internal::MicroManagerProvider::LoadResult loadResult; QStringList failedCameras; + scopeone::core::internal::ScopeOneProvider::LoadResult scopeOneResult; QString errorMessage; }; @@ -1168,59 +1164,6 @@ namespace scopeone::core return m_managers->hardwareRuntime->devices(); } - bool ScopeOneCore::registerDriverHostProvider(const QString& providerId, - const QString& modulePath, - const QVariantMap& options, - QString* errorMessage) - { - if (errorMessage) errorMessage->clear(); - const QString normalizedId = providerId.trimmed(); - if (normalizedId.isEmpty() - || m_configurationOperationRunning - || m_pendingStageCommands > 0 - || !m_managers->activeExperimentId.isEmpty() - || isRecording() - || m_pendingProviderRegistrations.contains(normalizedId) - || m_managers->hardwareRuntime->hasProvider(normalizedId)) - { - if (errorMessage) - { - *errorMessage = QStringLiteral("Provider cannot be registered in the current state"); - } - return false; - } - - m_pendingProviderRegistrations.insert(normalizedId); - auto* watcher = new QFutureWatcher(this); - connect(watcher, &QFutureWatcher::finished, - this, [this, watcher, normalizedId]() - { - ProviderRegistrationResult result = watcher->result(); - m_pendingProviderRegistrations.remove(normalizedId); - if (!result.provider) - { - qWarning().noquote() << QStringLiteral("Hardware provider %1: %2") - .arg(normalizedId, result.errorMessage); - } - else if (!m_managers->hardwareRuntime->registerProvider(result.provider)) - { - qWarning().noquote() << QStringLiteral( - "Hardware provider %1 conflicts with registered hardware").arg(normalizedId); - } - watcher->deleteLater(); - }); - watcher->setFuture(QtConcurrent::run( - m_hardwareThreadPool.get(), - [normalizedId, modulePath, options]() - { - ProviderRegistrationResult result; - result.provider = HardwareRuntime::startScopeOneProvider( - normalizedId, modulePath, QString{}, options, &result.errorMessage); - return result; - })); - return true; - } - // Return the linked MMCore version QString ScopeOneCore::getMMCoreVersion() { @@ -1827,7 +1770,7 @@ namespace scopeone::core m_managers->hardwareRuntime->stopMicroManagerPreview(); if (stopCameras) { - m_managers->hardwareRuntime->takeMicroManagerCameras(); + m_managers->hardwareRuntime->takeDriverHosts(); } for (const QString& cameraId : cameraIds) @@ -1865,11 +1808,6 @@ namespace scopeone::core m_configurationError = QStringLiteral("Another configuration operation is running"); return false; } - if (!m_pendingProviderRegistrations.isEmpty()) - { - m_configurationError = QStringLiteral("A hardware provider is still loading"); - return false; - } if (m_pendingStageCommands > 0) { m_configurationError = QStringLiteral("A stage command is running"); @@ -1908,11 +1846,11 @@ namespace scopeone::core applySystemShutdownPreset(); m_configurationOperationRunning = true; clearConfigurationRuntime(true, false); - stopMicroManagerCameras([this, path]() { startConfigurationLoadTask(path); }); + stopDriverHosts([this, path]() { startConfigurationLoadTask(path); }); return true; } - // Starts MMCore configuration loading after camera teardown + // Starts the loading of Micro-Manager and ScopeOne devices after the teardown of the DriverHosts void ScopeOneCore::startConfigurationLoadTask(const QString& path) { auto* watcher = new QFutureWatcher(this); @@ -1942,24 +1880,36 @@ namespace scopeone::core task.loadResult.failCount += static_cast(failedCameras.size()); task.loadResult.failedDevices.append(failedCameras); result = toFacadeLoadConfigResult(task.loadResult); + // The ScopeOne devices that failed count with the failed devices of Micro-Manager. + // They were left out of the result when the cameras failed to start. + result.successCount += task.scopeOneResult.successCount; + result.failCount += static_cast(task.scopeOneResult.failedDevices.size()); + result.failedDevices.append(task.scopeOneResult.failedDevices); if (!failedCameras.isEmpty()) { - const QString errorMessage = result.failedDevices.isEmpty() - ? QStringLiteral("Failed to start the cameras") - : QStringLiteral("Failed to start the cameras: %1") - .arg(result.failedDevices.join(QStringLiteral(", "))); + const QString errorMessage = QStringLiteral("Failed to start the cameras: %1") + .arg(failedCameras.join(QStringLiteral(", "))); startConfigurationLoadCleanupTask(result, errorMessage); watcher->deleteLater(); return; } - if (!applyLoadedConfiguration(path, result)) + if (!m_managers->hardwareRuntime->registerScopeOneDevices() + || !applyLoadedConfiguration(path, result)) { const QString errorMessage = - QStringLiteral("Micro-Manager device catalog conflicts with registered hardware"); + QStringLiteral("Device catalog conflicts with registered hardware"); startConfigurationLoadCleanupTask(result, errorMessage); watcher->deleteLater(); return; } + for (const HardwareDeviceDescriptor& device : m_managers->hardwareRuntime->devices()) + { + if (device.kind == HardwareDeviceKind::Camera + && !result.cameraIds.contains(device.logicalId)) + { + result.cameraIds.append(device.logicalId); + } + } m_configurationOperationRunning = false; emit hardwareConfigurationChanged(); emit configurationLoadFinished(true, result, {}); @@ -1981,6 +1931,7 @@ namespace scopeone::core if (task.success) { task.failedCameras = runtime->startMicroManagerCameras(task.loadResult.cameras); + task.scopeOneResult = runtime->loadScopeOneDevices(path); } QString cleanupError; if (!task.success && !runtime->unloadMicroManagerDevices(&cleanupError)) @@ -1992,7 +1943,7 @@ namespace scopeone::core watcher->setFuture(future); } - void ScopeOneCore::stopMicroManagerCameras(std::function next) + void ScopeOneCore::stopDriverHosts(std::function next) { auto* watcher = new QFutureWatcher(this); connect(watcher, &QFutureWatcher::finished, this, [watcher, next = std::move(next)]() @@ -2001,9 +1952,9 @@ namespace scopeone::core next(); }); watcher->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), - [cameras = m_managers->hardwareRuntime->takeMicroManagerCameras()]() mutable + [hosts = m_managers->hardwareRuntime->takeDriverHosts()]() mutable { - cameras.clear(); + hosts.clear(); })); } @@ -2011,7 +1962,7 @@ namespace scopeone::core void ScopeOneCore::startConfigurationLoadCleanupTask(const LoadConfigResult& result, const QString& errorMessage) { - stopMicroManagerCameras([this, result, errorMessage]() + stopDriverHosts([this, result, errorMessage]() { startDeviceUnloadAfterFailedLoad(result, errorMessage); }); @@ -2058,7 +2009,7 @@ namespace scopeone::core applySystemShutdownPreset(); m_configurationOperationRunning = true; clearConfigurationRuntime(true, false); - stopMicroManagerCameras([this]() { startConfigurationUnloadTask(); }); + stopDriverHosts([this]() { startConfigurationUnloadTask(); }); return true; } diff --git a/ScopeOneCore/src/ScopeOneProvider.cpp b/ScopeOneCore/src/ScopeOneProvider.cpp new file mode 100644 index 0000000..e2f29f7 --- /dev/null +++ b/ScopeOneCore/src/ScopeOneProvider.cpp @@ -0,0 +1,178 @@ +// The ScopeOne devices of a cfg file +#include "internal/ScopeOneProvider.h" + +#include +#include +#include +#include +#include +#include + +#include + +#include "internal/DriverHostClient.h" +#include "internal/MicroManagerProvider.h" +#include "scopeone/DriverManifest.h" + +namespace scopeone::core::internal +{ + namespace + { + // Creates the device of one line in a DriverHost and applies the properties that are not + // pre-init. The pre-init ones reach the driver as the arguments of createDevice. + DevicePtr startDevice(const ScopeOneDeviceLine& line, + const QList& drivers, + QString* errorMessage) + { + const DiscoveredDriver* driver = nullptr; + for (const DiscoveredDriver& candidate : drivers) + { + if (candidate.error.isEmpty() && candidate.manifest.id == line.driverId) + { + driver = &candidate; + break; + } + } + if (!driver) + { + *errorMessage = QStringLiteral("No driver %1").arg(line.driverId); + return {}; + } + + QVariantMap preInitProperties; + for (const auto& property : line.properties) + { + preInitProperties.insert(property.first, property.second); + } + DevicePtr device = createScopeOneDevice( + line.label, driver->path, line.deviceName, preInitProperties, errorMessage); + if (!device) + { + return {}; + } + for (const HardwareDeviceDescriptor& peripheral : device->devices()) + { + const QStringList names = device->listProperties(peripheral.logicalId); + for (const auto& property : line.properties) + { + if (names.contains(property.first) + && !device->isPropertyPreInit(peripheral.logicalId, property.first) + && !device->setProperty( + peripheral.logicalId, property.first, property.second, errorMessage)) + { + return {}; + } + } + } + return device; + } + } + + // Reads the ScopeOneDevice, ScopeOneProperty and ScopeOneConfigGroup lines, which follow the + // fields of Device, Property and ConfigGroup of Micro-Manager + ScopeOneConfiguration readScopeOneConfiguration(const QString& configPath) + { + ScopeOneConfiguration configuration; + QFile file(configPath); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + return configuration; + } + + // The properties are collected by label after the whole file is read, so their place + // relative to the device line does not matter + QHash>> properties; + QTextStream stream(&file); + while (!stream.atEnd()) + { + const QString line = stream.readLine().trimmed(); + if (!line.startsWith(QStringLiteral("ScopeOne"))) + { + continue; + } + + const QStringList device = splitConfigLine(line, 4); + if (device.size() == 4 && device[0] == QStringLiteral("ScopeOneDevice")) + { + const bool used = std::any_of(configuration.devices.cbegin(), configuration.devices.cend(), + [&](const ScopeOneDeviceLine& existing) + { return existing.label == device[1]; }); + if (used) + { + configuration.rejected.append( + QStringLiteral("%1 (label used twice)").arg(device[1])); + } + else + { + configuration.devices.append({device[1], device[2], device[3], {}}); + } + continue; + } + if (device.size() == 4 && device[0] == QStringLiteral("ScopeOneProperty")) + { + properties[device[1]].append({device[2], device[3]}); + continue; + } + const QStringList group = splitConfigLine(line, 6); + if (group.size() == 6 && group[0] == QStringLiteral("ScopeOneConfigGroup")) + { + configuration.configGroups.append({group[1], group[2], group[3], group[4], group[5]}); + } + } + for (ScopeOneDeviceLine& device : configuration.devices) + { + device.properties = properties.take(device.label); + } + for (auto it = properties.cbegin(); it != properties.cend(); ++it) + { + configuration.rejected.append(QStringLiteral("%1 (no ScopeOneDevice)").arg(it.key())); + } + return configuration; + } + + ScopeOneProvider::LoadResult ScopeOneProvider::load(const QString& configPath) + { + LoadResult result; + m_configuration = readScopeOneConfiguration(configPath); + result.failedDevices = m_configuration.rejected; + m_devices.clear(); + const QList drivers = discoverDrivers(); + + // Each DriverHost loads its device on its own, which can take seconds, so they start + // together + QList> hosts; + for (const ScopeOneDeviceLine& line : std::as_const(m_configuration.devices)) + { + hosts.append(QtConcurrent::run([line, &drivers]() + { + QString error; + DevicePtr device = startDevice(line, drivers, &error); + if (!device) + { + qWarning().noquote() << QStringLiteral("Failed to start ScopeOne device '%1': %2") + .arg(line.label, error); + } + return device; + })); + } + for (qsizetype index = 0; index < hosts.size(); ++index) + { + const DevicePtr device = hosts[index].result(); + if (!device) + { + result.failedDevices.append(m_configuration.devices[index].label); + continue; + } + m_devices.append(device); + ++result.successCount; + } + return result; + } + + QList ScopeOneProvider::takeDevices() + { + QList taken; + taken.swap(m_devices); + return taken; + } +} diff --git a/config/MMConfig_demo.cfg b/config/MMConfig_demo.cfg index befec8e..e150967 100644 --- a/config/MMConfig_demo.cfg +++ b/config/MMConfig_demo.cfg @@ -227,3 +227,8 @@ ConfigPixelSize,Res10x,Objective,Label,Nikon 10X S Fluor PixelSize_um,Res10x,1.0 + + +# ScopeOne devices +ScopeOneDevice,Example Camera,example.camera,Example Camera +ScopeOneDevice,Example PMT,example.pmt,Example PMT diff --git a/drivers/ExampleCamera/ExampleCameraDriver.cpp b/drivers/ExampleCamera/ExampleCameraDriver.cpp index aa73900..1f39cad 100644 --- a/drivers/ExampleCamera/ExampleCameraDriver.cpp +++ b/drivers/ExampleCamera/ExampleCameraDriver.cpp @@ -258,8 +258,8 @@ namespace example_camera class ExampleCameraDevice final : public Device, public Hub, public Camera { public: - ExampleCameraDevice(const QString& providerId, int cameraCount, int width, int height) - : m_providerId(providerId) + ExampleCameraDevice(const QString& driverId, int cameraCount, int width, int height) + : m_driverId(driverId) { const auto patterns = std::make_shared(width, height); for (int index = 0; index < cameraCount; ++index) @@ -273,7 +273,7 @@ namespace example_camera HardwareProviderDescriptor descriptor() const override { - return {m_providerId, QStringLiteral("Example Camera"), QStringLiteral("1")}; + return {m_driverId, QStringLiteral("Example Camera"), QStringLiteral("1")}; } QList peripherals() const override @@ -283,7 +283,6 @@ namespace example_camera { HardwareDeviceDescriptor device; device.logicalId = m_cameras[index]->id(); - device.providerId = m_providerId; device.providerDeviceId = m_cameras[index]->id(); device.hardwareId = QStringLiteral("EX-%1").arg(1000 + index); device.name = QStringLiteral("Example Camera %1").arg(index + 1); @@ -524,7 +523,7 @@ namespace example_camera } } - QString m_providerId; + QString m_driverId; std::vector> m_cameras; QMutex m_mutex; PreviewStateSink m_previewStateSink; @@ -539,7 +538,7 @@ namespace example_camera Q_INTERFACES(scopeone::core::Driver) public: - QString providerId() const override + QString driverId() const override { return QStringLiteral("example.camera"); } @@ -571,7 +570,7 @@ namespace example_camera } return {}; } - return std::make_shared(providerId(), cameraCount, width, height); + return std::make_shared(driverId(), cameraCount, width, height); } }; } diff --git a/drivers/ExampleCamera/driver.json b/drivers/ExampleCamera/driver.json index a539442..eb8d08a 100644 --- a/drivers/ExampleCamera/driver.json +++ b/drivers/ExampleCamera/driver.json @@ -3,7 +3,5 @@ "name": "Example Camera", "version": "1.0.0", "scopeOneApi": 3, - "kind": "driver", - "providerId": "example.camera", - "autoLoad": false + "kind": "driver" } diff --git a/drivers/ExamplePmt/ExamplePmtDriver.cpp b/drivers/ExamplePmt/ExamplePmtDriver.cpp index ab17b3a..aaadf0d 100644 --- a/drivers/ExamplePmt/ExamplePmtDriver.cpp +++ b/drivers/ExamplePmt/ExamplePmtDriver.cpp @@ -642,7 +642,7 @@ namespace example_pmt Q_INTERFACES(scopeone::core::Driver) public: - QString providerId() const override + QString driverId() const override { return QString::fromLatin1(kSourceId); } diff --git a/drivers/ExamplePmt/driver.json b/drivers/ExamplePmt/driver.json index 9882e0d..6d452ae 100644 --- a/drivers/ExamplePmt/driver.json +++ b/drivers/ExamplePmt/driver.json @@ -3,7 +3,5 @@ "name": "Example PMT", "version": "1.0.0", "scopeOneApi": 3, - "kind": "driver", - "providerId": "example.pmt", - "autoLoad": false + "kind": "driver" } diff --git a/drivers/NIDaqmx/NIDaqmxDriver.cpp b/drivers/NIDaqmx/NIDaqmxDriver.cpp index ab26b70..b43c4fd 100644 --- a/drivers/NIDaqmx/NIDaqmxDriver.cpp +++ b/drivers/NIDaqmx/NIDaqmxDriver.cpp @@ -1496,7 +1496,7 @@ namespace ni_daqmx Q_INTERFACES(scopeone::core::Driver) public: - QString providerId() const override + QString driverId() const override { return QStringLiteral("scopeone.nidaqmx"); } diff --git a/drivers/NIDaqmx/driver.json b/drivers/NIDaqmx/driver.json index 84148df..86478b8 100644 --- a/drivers/NIDaqmx/driver.json +++ b/drivers/NIDaqmx/driver.json @@ -3,7 +3,5 @@ "name": "NI-DAQmx", "version": "1.0.0", "scopeOneApi": 3, - "kind": "driver", - "providerId": "scopeone.nidaqmx", - "autoLoad": true + "kind": "driver" } diff --git a/drivers/README.md b/drivers/README.md index 7ff4071..2da570b 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -64,9 +64,7 @@ the drivers into `ScopeOneCore/install/bin/drivers`, from where the GUI build co "name": "My Camera", "version": "1.0.0", "scopeOneApi": 3, - "kind": "driver", - "providerId": "my.camera", - "autoLoad": false + "kind": "driver" } ``` @@ -82,7 +80,7 @@ class MyCameraDriver final : public QObject, public scopeone::core::Driver Q_INTERFACES(scopeone::core::Driver) public: - QString providerId() const override; + QString driverId() const override; QStringList deviceNames() const override; scopeone::core::DevicePtr createDevice(const QString& name, const QVariantMap& preInitProperties, diff --git a/src/DriversDialog.cpp b/src/DriversDialog.cpp index 2ff3d00..fcd6474 100644 --- a/src/DriversDialog.cpp +++ b/src/DriversDialog.cpp @@ -1,7 +1,6 @@ #include "DriversDialog.h" #include "scopeone/DriverManifest.h" -#include "scopeone/ScopeOneCore.h" #include #include @@ -10,12 +9,10 @@ #include #include #include -#include #include #include #include #include -#include #include #include #include @@ -25,54 +22,11 @@ namespace scopeone::ui { namespace { - constexpr int kIdRole = Qt::UserRole; - constexpr int kStatusRole = Qt::UserRole + 1; - constexpr int kMetadataRole = Qt::UserRole + 2; + constexpr int kStatusRole = Qt::UserRole; + constexpr int kMetadataRole = Qt::UserRole + 1; using scopeone::core::DiscoveredDriver; using scopeone::core::discoverDrivers; - using scopeone::core::driverSettingsKey; - } - - QStringList loadConfiguredDrivers(scopeone::core::ScopeOneCore& core) - { - QStringList errors; - QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - for (const DiscoveredDriver& driver : discoverDrivers()) - { - if (!driver.error.isEmpty()) - { - errors.append(QStringLiteral("%1: %2") - .arg(QFileInfo(driver.path).fileName(), driver.error)); - continue; - } - - const QString id = driver.manifest.id; - if (!scopeone::core::driverEnabled(driver.manifest)) - { - continue; - } - - const QString providerId = driver.manifest.metadata - .value(QStringLiteral("providerId")) - .toString().trimmed(); - if (providerId != id) - { - errors.append(QStringLiteral("%1: providerId must match the driver id") - .arg(QFileInfo(driver.path).fileName())); - continue; - } - const QVariantMap options = settings - .value(driverSettingsKey(id, QStringLiteral("options"))) - .toMap(); - QString error; - if (!core.registerDriverHostProvider(providerId, driver.path, options, &error)) - { - errors.append(QStringLiteral("%1: %2") - .arg(QFileInfo(driver.path).fileName(), error)); - } - } - return errors; } DriversDialog::DriversDialog(QWidget* parent) @@ -83,15 +37,15 @@ namespace scopeone::ui auto* layout = new QVBoxLayout(this); m_table = new QTableWidget(this); - m_table->setColumnCount(5); + m_table->setColumnCount(4); m_table->setHorizontalHeaderLabels( - {tr("Enabled"), tr("Name"), tr("Version"), tr("Status"), tr("Location")}); + {tr("Name"), tr("Version"), tr("Status"), tr("Location")}); m_table->setSelectionBehavior(QAbstractItemView::SelectRows); m_table->setSelectionMode(QAbstractItemView::SingleSelection); m_table->setEditTriggers(QAbstractItemView::NoEditTriggers); m_table->verticalHeader()->setVisible(false); m_table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); - m_table->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch); + m_table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch); layout->addWidget(m_table, 1); layout->addWidget(new QLabel(tr("Metadata and diagnostics"), this)); @@ -100,59 +54,36 @@ namespace scopeone::ui m_detailsEdit->setMaximumHeight(160); layout->addWidget(m_detailsEdit); - layout->addWidget(new QLabel(tr("Driver options (JSON)"), this)); - m_optionsEdit = new QPlainTextEdit(this); - m_optionsEdit->setMaximumHeight(100); - m_optionsEdit->setPlaceholderText(QStringLiteral("{}")); - layout->addWidget(m_optionsEdit); - - auto* buttons = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Close, - this); + auto* buttons = new QDialogButtonBox(QDialogButtonBox::Close, this); auto* installButton = buttons->addButton(tr("Install Driver..."), QDialogButtonBox::ActionRole); layout->addWidget(buttons); - connect(buttons->button(QDialogButtonBox::Save), &QPushButton::clicked, - this, &DriversDialog::saveSettings); connect(installButton, &QPushButton::clicked, this, &DriversDialog::installDriver); connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(m_table, &QTableWidget::itemSelectionChanged, - this, &DriversDialog::showSelectedOptions); + this, &DriversDialog::showSelectedDetails); refreshDrivers(); } void DriversDialog::refreshDrivers() { - QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); const QList drivers = discoverDrivers(); m_table->setRowCount(drivers.size()); for (int row = 0; row < drivers.size(); ++row) { const DiscoveredDriver& driver = drivers.at(row); - auto* enabled = new QTableWidgetItem(); - enabled->setData(kIdRole, driver.manifest.id); const QString status = driver.error.isEmpty() ? tr("Ready") : driver.error; - enabled->setData(kStatusRole, status); - enabled->setData( + auto* name = new QTableWidgetItem(driver.manifest.name.isEmpty() + ? QFileInfo(driver.path).completeBaseName() + : driver.manifest.name); + name->setData(kStatusRole, status); + name->setData( kMetadataRole, QString::fromUtf8(QJsonDocument(driver.metadata).toJson(QJsonDocument::Indented))); - if (driver.error.isEmpty()) - { - enabled->setFlags(enabled->flags() | Qt::ItemIsUserCheckable); - const QString key = driverSettingsKey( - driver.manifest.id, QStringLiteral("enabled")); - const bool checked = settings.contains(key) - ? settings.value(key).toBool() - : driver.manifest.autoLoad; - enabled->setCheckState(checked ? Qt::Checked : Qt::Unchecked); - } - m_table->setItem(row, 0, enabled); - m_table->setItem(row, 1, new QTableWidgetItem( - driver.manifest.name.isEmpty() - ? QFileInfo(driver.path).completeBaseName() - : driver.manifest.name)); - m_table->setItem(row, 2, new QTableWidgetItem(driver.manifest.version)); - m_table->setItem(row, 3, new QTableWidgetItem(status)); - m_table->setItem(row, 4, new QTableWidgetItem(driver.path)); + m_table->setItem(row, 0, name); + m_table->setItem(row, 1, new QTableWidgetItem(driver.manifest.version)); + m_table->setItem(row, 2, new QTableWidgetItem(status)); + m_table->setItem(row, 3, new QTableWidgetItem(driver.path)); } if (m_table->rowCount() > 0) { @@ -160,35 +91,19 @@ namespace scopeone::ui } } - void DriversDialog::showSelectedOptions() + void DriversDialog::showSelectedDetails() { const int row = m_table->currentRow(); if (row < 0) { - m_optionsEdit->clear(); - m_optionsEdit->setEnabled(false); + m_detailsEdit->clear(); return; } const QTableWidgetItem* item = m_table->item(row, 0); - const QString id = item->data(kIdRole).toString(); - const QString status = item->data(kStatusRole).toString(); m_detailsEdit->setPlainText( item->data(kMetadataRole).toString() + QStringLiteral("\n\n") - + tr("Status: %1").arg(status)); - const bool driver = !id.isEmpty() && status == tr("Ready"); - m_optionsEdit->setEnabled(driver); - if (!driver) - { - m_optionsEdit->clear(); - return; - } - QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - const QJsonObject options = QJsonObject::fromVariantMap( - settings.value(driverSettingsKey( - id, QStringLiteral("options"))).toMap()); - m_optionsEdit->setPlainText(QString::fromUtf8( - QJsonDocument(options).toJson(QJsonDocument::Indented))); + + tr("Status: %1").arg(item->data(kStatusRole).toString())); } void DriversDialog::installDriver() @@ -241,54 +156,6 @@ namespace scopeone::ui } refreshDrivers(); QMessageBox::information(this, tr("Drivers"), - tr("It will be available after ScopeOne restarts.")); - } - - void DriversDialog::saveSettings() - { - const int row = m_table->currentRow(); - if (row < 0) - { - return; - } - QTableWidgetItem* item = m_table->item(row, 0); - const QString id = item->data(kIdRole).toString(); - QVariantMap selectedOptions; - if (!id.isEmpty()) - { - QJsonParseError parseError; - QByteArray optionJson = m_optionsEdit->toPlainText().trimmed().toUtf8(); - if (optionJson.isEmpty()) - { - optionJson = QByteArrayLiteral("{}"); - } - const QJsonDocument options = QJsonDocument::fromJson(optionJson, &parseError); - if (parseError.error != QJsonParseError::NoError || !options.isObject()) - { - QMessageBox::warning(this, tr("Drivers"), tr("Driver options must be a JSON object.")); - return; - } - selectedOptions = options.object().toVariantMap(); - } - - QSettings settings(QStringLiteral("ScopeOne"), QStringLiteral("ScopeOne")); - for (int driverRow = 0; driverRow < m_table->rowCount(); ++driverRow) - { - QTableWidgetItem* driverItem = m_table->item(driverRow, 0); - const QString driverId = driverItem->data(kIdRole).toString(); - if (!driverId.isEmpty()) - { - settings.setValue(driverSettingsKey( - driverId, QStringLiteral("enabled")), - driverItem->checkState() == Qt::Checked); - } - } - if (!id.isEmpty()) - { - settings.setValue(driverSettingsKey( - id, QStringLiteral("options")), selectedOptions); - } - QMessageBox::information(this, tr("Drivers"), - tr("The settings will take effect after ScopeOne restarts.")); + tr("The driver is installed.")); } } diff --git a/src/DriversDialog.h b/src/DriversDialog.h index 54b8cfb..6efe23e 100644 --- a/src/DriversDialog.h +++ b/src/DriversDialog.h @@ -1,20 +1,12 @@ #pragma once #include -#include class QPlainTextEdit; class QTableWidget; -namespace scopeone::core -{ - class ScopeOneCore; -} - namespace scopeone::ui { - QStringList loadConfiguredDrivers(scopeone::core::ScopeOneCore& core); - class DriversDialog final : public QDialog { Q_OBJECT @@ -25,11 +17,9 @@ namespace scopeone::ui private: void refreshDrivers(); void installDriver(); - void showSelectedOptions(); - void saveSettings(); + void showSelectedDetails(); QTableWidget* m_table{nullptr}; QPlainTextEdit* m_detailsEdit{nullptr}; - QPlainTextEdit* m_optionsEdit{nullptr}; }; } diff --git a/src/main.cpp b/src/main.cpp index a8473cc..717493b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,6 @@ #include "ConsoleWidget.h" #include "Appearance.h" #include "MainWindow.h" -#include "DriversDialog.h" int main(int argc, char *argv[]) { @@ -28,10 +27,6 @@ int main(int argc, char *argv[]) scopeone::ui::ConsoleWidget::installQtMessageHandler(); auto scopeOneCore = std::make_unique(); - for (const QString& error : scopeone::ui::loadConfiguredDrivers(*scopeOneCore)) - { - qWarning().noquote() << QStringLiteral("Failed to load driver %1").arg(error); - } scopeone::ui::MainWindow window(scopeOneCore.get()); window.show(); return app.exec(); From 02a04c1c5709211a1bfd350c7984e6e705e5e6ef Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 17:40:46 +0100 Subject: [PATCH 36/49] Merge ScopeOneConfigGroup presets with the Micro-Manager presets of the same name --- ScopeOneCore/include/scopeone/ScopeOneCore.h | 2 +- ScopeOneCore/internal/HardwareRuntime.h | 6 +- ScopeOneCore/src/DriverHostClient.cpp | 39 +++- ScopeOneCore/src/DriverHostMain.cpp | 5 + ScopeOneCore/src/HardwareRuntime.cpp | 222 +++++++++++++------ ScopeOneCore/src/ScopeOneCore.cpp | 60 +++-- ScopeOneCore/src/ScopeOneProvider.cpp | 5 + config/MMConfig_demo.cfg | 2 + 8 files changed, 252 insertions(+), 89 deletions(-) diff --git a/ScopeOneCore/include/scopeone/ScopeOneCore.h b/ScopeOneCore/include/scopeone/ScopeOneCore.h index 0b07f4f..81018d4 100644 --- a/ScopeOneCore/include/scopeone/ScopeOneCore.h +++ b/ScopeOneCore/include/scopeone/ScopeOneCore.h @@ -636,7 +636,7 @@ namespace scopeone::core void clearConfigurationRuntime(bool notify, bool stopCameras); // Stops the DriverHosts of the Micro-Manager cameras and ScopeOne devices on the hardware // thread, as they can take a second to exit, and then calls next - void stopDriverHosts(std::function next); + void stopDriverHosts(std::function next, bool applyShutdown = false); void startConfigurationLoadTask(const QString& configPath); void startConfigurationLoadCleanupTask(const LoadConfigResult& result, const QString& errorMessage); diff --git a/ScopeOneCore/internal/HardwareRuntime.h b/ScopeOneCore/internal/HardwareRuntime.h index ae5166c..7bdb37e 100644 --- a/ScopeOneCore/internal/HardwareRuntime.h +++ b/ScopeOneCore/internal/HardwareRuntime.h @@ -160,9 +160,11 @@ namespace scopeone::core::internal QString currentConfig(const QString& groupName) const; std::vector configSettings( const QString& groupName, const QString& configName) const; + // The presets are those of Micro-Manager and of the ScopeOneConfigGroup lines, merged by name bool setConfig(const QString& groupName, const QString& configName, QString* errorMessage = nullptr); + void applyScopeOneStartupPreset(); bool autoShutter(bool& enabled, QString* errorMessage = nullptr) const; bool setAutoShutter(bool enabled, QString* errorMessage = nullptr); @@ -207,6 +209,7 @@ namespace scopeone::core::internal void stopMicroManagerPreview(); bool unloadMicroManagerDevices(QString* errorMessage = nullptr); void applyMicroManagerShutdownPreset(); + void applyScopeOneShutdownPreset(); QList devices() const { return m_registry.devices(); } static QString microManagerVersion(); void clear(); @@ -225,7 +228,8 @@ namespace scopeone::core::internal Stage* stageForDevice(const QString& logicalId) const; Shutter* shutterForDevice(const QString& logicalId) const; StateDevice* stateDeviceForDevice(const QString& logicalId) const; - ConfigurationProvider* configurationProviderForGroup(const QString& groupName) const; + std::vector scopeOneConfigLines() const; + bool applyScopeOnePreset(const QString& groupName, const QString& configName, QString* errorMessage); QList> frameSourcesFor(const QStringList& cameraIds) const; bool applyFrameDeliveryModes(const QHash& modes); diff --git a/ScopeOneCore/src/DriverHostClient.cpp b/ScopeOneCore/src/DriverHostClient.cpp index fa47167..089e4e3 100644 --- a/ScopeOneCore/src/DriverHostClient.cpp +++ b/ScopeOneCore/src/DriverHostClient.cpp @@ -1346,17 +1346,36 @@ namespace scopeone::core::internal const QString& name, bool fromCache) override { + // The values are kept here, so that reading from the cache never waits for the host. + // A property that was never read or set is asked of the host once. + const QString key = deviceId.trimmed() + QLatin1Char('\n') + name; + // Only for ScopeOne drivers. The cache of MMCore in a camera host also follows the + // property changes the device reports, which this one would hide. + const bool keepsValues = m_arguments.contains(QStringLiteral("--driver")); + if (fromCache && keepsValues) + { + QMutexLocker locker(&m_stateMutex); + const auto cached = m_propertyValues.constFind(key); + if (cached != m_propertyValues.cend()) + { + return cached.value(); + } + } QJsonObject response; QJsonObject fields; fields.insert(QStringLiteral("name"), name); fields.insert(QStringLiteral("fromCache"), fromCache); - return request(driverhost::kCommandGetProperty, - deviceId, - fields, - response, - nullptr) - ? response.value(QStringLiteral("value")).toString() - : QString{}; + if (!request(driverhost::kCommandGetProperty, deviceId, fields, response, nullptr)) + { + return {}; + } + const QString value = response.value(QStringLiteral("value")).toString(); + if (keepsValues) + { + QMutexLocker locker(&m_stateMutex); + m_propertyValues.insert(key, value); + } + return value; } bool setProperty(const QString& deviceId, @@ -1370,6 +1389,11 @@ namespace scopeone::core::internal QJsonObject response; clearPropertyDetails(); const bool ok = request(driverhost::kCommandSetProperty, deviceId, fields, response, errorMessage); + if (ok && m_arguments.contains(QStringLiteral("--driver"))) + { + QMutexLocker locker(&m_stateMutex); + m_propertyValues.insert(deviceId.trimmed() + QLatin1Char('\n') + name, value); + } if (ok && isCamera(deviceId)) { refreshPixelSizes({deviceId.trimmed()}); @@ -1983,6 +2007,7 @@ namespace scopeone::core::internal mutable QMutex m_stateMutex; QSet m_runningCameras; QHash m_propertyDetails; + QHash m_propertyValues; QHash m_pixelSizes; }; diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index 06b16ad..3825d77 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -101,6 +101,11 @@ namespace scopeone::core::internal connect(m_socket, &QLocalSocket::errorOccurred, this, [this](QLocalSocket::LocalSocketError socketError) { + // Core closing the connection at a normal unload is no error + if (socketError == QLocalSocket::PeerClosedError) + { + return; + } qWarning().noquote() << QString("DriverHost control socket error (%1) on connection %2") .arg(static_cast(socketError)) diff --git a/ScopeOneCore/src/HardwareRuntime.cpp b/ScopeOneCore/src/HardwareRuntime.cpp index 3caa56c..fdee775 100644 --- a/ScopeOneCore/src/HardwareRuntime.cpp +++ b/ScopeOneCore/src/HardwareRuntime.cpp @@ -5,6 +5,7 @@ #include "scopeone/Camera.h" #include +#include #include #include #include @@ -15,6 +16,24 @@ namespace scopeone::core::internal { namespace { + bool sameSettings(const std::vector& a, + const std::vector& b) + { + return !a.empty() && a.size() == b.size() + && std::equal(a.begin(), a.end(), b.begin(), [](const auto& x, const auto& y) + { return x.device == y.device && x.property == y.property && x.value == y.value; }); + } + + // Property values compare as numbers when both are, so 1 and 1.000 are the same setting + bool propertyValuesEqual(const QString& a, const QString& b) + { + bool aIsNumber = false; + bool bIsNumber = false; + const double x = a.toDouble(&aIsNumber); + const double y = b.toDouble(&bIsNumber); + return aIsNumber && bIsNumber ? x == y : a == b; + } + // Calls through to a provider, or reports that nothing serves the request template bool forward(Provider* provider, const char* missing, QString* errorMessage, Call&& call) @@ -308,33 +327,6 @@ namespace scopeone::core::internal return dynamic_cast(provider.get()); } - ConfigurationProvider* HardwareRuntime::configurationProviderForGroup( - const QString& groupName) const - { - const QString normalizedGroup = groupName.trimmed(); - if (normalizedGroup.isEmpty()) - { - return nullptr; - } - ConfigurationProvider* match = nullptr; - for (const HardwareProviderDescriptor& descriptor : m_registry.providers()) - { - const DevicePtr provider = m_registry.provider(descriptor.id); - auto* configurationProvider = dynamic_cast(provider.get()); - if (!configurationProvider - || !configurationProvider->availableConfigGroups().contains(normalizedGroup)) - { - continue; - } - if (match) - { - return nullptr; - } - match = configurationProvider; - } - return match; - } - bool HardwareRuntime::startPreview() { return m_acquisitionEngine.start(QStringLiteral("All")); @@ -787,71 +779,161 @@ namespace scopeone::core::internal return provider ? provider->stateLabel(deviceId, state) : QString{}; } - QStringList HardwareRuntime::availableConfigGroups() const + // The ScopeOne lines of the cfg file for the devices that are loaded. A device that failed to + // load or is unloaded takes its lines out of the presets. + std::vector HardwareRuntime::scopeOneConfigLines() const { - QHash groupCounts; - for (const HardwareProviderDescriptor& descriptor : m_registry.providers()) + std::vector lines; + for (const ScopeOneConfigGroupLine& line : m_scopeOne.configuration().configGroups) { - const DevicePtr provider = m_registry.provider(descriptor.id); - if (auto* configurationProvider = dynamic_cast(provider.get())) + if (m_registry.providerForDevice(line.label)) { - QSet providerGroups; - for (const QString& group : configurationProvider->availableConfigGroups()) - { - const QString normalizedGroup = group.trimmed(); - if (!normalizedGroup.isEmpty()) - { - providerGroups.insert(normalizedGroup); - } - } - for (const QString& group : providerGroups) - { - ++groupCounts[group]; - } + lines.push_back(line); } } - QStringList groups; - for (auto it = groupCounts.cbegin(); it != groupCounts.cend(); ++it) + return lines; + } + + // The preset names of the group in cfg order, which are the Micro-Manager ones first + QStringList HardwareRuntime::availableConfigs(const QString& groupName) const + { + const QString group = groupName.trimmed(); + QStringList presets = m_microManager->availableConfigs(group); + for (const ScopeOneConfigGroupLine& line : scopeOneConfigLines()) { - if (it.value() == 1) + if (line.group == group && !presets.contains(line.preset)) { - groups.append(it.key()); + presets.append(line.preset); } } - std::sort(groups.begin(), groups.end()); - return groups; + return presets; } - QStringList HardwareRuntime::availableConfigs(const QString& groupName) const + // The groups of Micro-Manager, then the groups that only ScopeOne devices define + QStringList HardwareRuntime::availableConfigGroups() const { - ConfigurationProvider* provider = configurationProviderForGroup(groupName); - return provider ? provider->availableConfigs(groupName) : QStringList{}; + QStringList groups = m_microManager->availableConfigGroups(); + for (const ScopeOneConfigGroupLine& line : scopeOneConfigLines()) + { + if (!groups.contains(line.group)) + { + groups.append(line.group); + } + } + return groups; } + // The Micro-Manager settings of the preset, then those of the ScopeOne devices std::vector HardwareRuntime::configSettings( const QString& groupName, const QString& configName) const { - ConfigurationProvider* provider = configurationProviderForGroup(groupName); - return provider ? provider->configSettings(groupName, configName) - : std::vector{}; + std::vector settings = + m_microManager->configSettings(groupName.trimmed(), configName); + for (const ScopeOneConfigGroupLine& line : scopeOneConfigLines()) + { + if (line.group == groupName.trimmed() && line.preset == configName) + { + settings.push_back({line.label, line.property, line.value}); + } + } + return settings; } + // The first preset of the group that both parts match. The ScopeOne values come from the cache + // of the DriverHost clients, so the refresh of the interface sends nothing to the devices. QString HardwareRuntime::currentConfig(const QString& groupName) const { - ConfigurationProvider* provider = configurationProviderForGroup(groupName); - return provider ? provider->currentConfig(groupName) : QString{}; + const QString group = groupName.trimmed(); + const QString microManagerCurrent = m_microManager->currentConfig(group); + const QStringList microManagerPresets = m_microManager->availableConfigs(group); + const std::vector lines = scopeOneConfigLines(); + for (const QString& preset : availableConfigs(group)) + { + // Presets that differ only in their ScopeOne part have the same Micro-Manager settings, + // and Micro-Manager reports just one of them, so the settings are compared. + if (microManagerPresets.contains(preset) && microManagerCurrent != preset + && !sameSettings(m_microManager->configSettings(group, preset), + m_microManager->configSettings(group, microManagerCurrent))) + { + continue; + } + bool matches = true; + for (const ScopeOneConfigGroupLine& line : lines) + { + if (line.group != group || line.preset != preset) + { + continue; + } + Device* device = propertyDeviceForDevice(line.label); + if (!device || !propertyValuesEqual(device->getProperty(line.label, line.property, true), + line.value)) + { + matches = false; + break; + } + } + if (matches) + { + return preset; + } + } + return {}; + } + + // Sets the ScopeOne part of a preset, stopping at the first property that cannot be set + bool HardwareRuntime::applyScopeOnePreset(const QString& groupName, + const QString& configName, + QString* errorMessage) + { + for (const ScopeOneConfigGroupLine& line : scopeOneConfigLines()) + { + if (line.group != groupName.trimmed() || line.preset != configName) + { + continue; + } + QString error; + if (!setProperty(line.label, line.property, line.value, &error)) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("%1 %2: %3").arg(line.label, line.property, error); + } + return false; + } + } + return true; } + // Applies the Micro-Manager part of the preset, if it has one, then the ScopeOne part bool HardwareRuntime::setConfig(const QString& groupName, const QString& configName, QString* errorMessage) { - return forward(configurationProviderForGroup(groupName), "Configuration provider not available", - errorMessage, - [&](ConfigurationProvider& provider) - { - return provider.setConfig(groupName, configName, errorMessage); - }); + // A preset that neither side defines is an error, so a misspelt name does not pass as set + if (!availableConfigs(groupName).contains(configName)) + { + if (errorMessage) + { + *errorMessage = QStringLiteral("No preset %1 in group %2").arg(configName, groupName); + } + return false; + } + if (m_microManager->availableConfigs(groupName.trimmed()).contains(configName) + && !m_microManager->setConfig(groupName.trimmed(), configName, errorMessage)) + { + return false; + } + return applyScopeOnePreset(groupName, configName, errorMessage); + } + + // System/Startup of the ScopeOne devices, after the load. MMCore has applied its own part. + void HardwareRuntime::applyScopeOneStartupPreset() + { + QString error; + if (!applyScopeOnePreset(QStringLiteral("System"), QStringLiteral("Startup"), &error)) + { + qWarning().noquote() << QStringLiteral("System/Startup failed: %1").arg(error); + } } bool HardwareRuntime::autoShutter(bool& enabled, QString* errorMessage) const @@ -1036,6 +1118,16 @@ namespace scopeone::core::internal m_microManager->applyShutdownPreset(); } + // System/Shutdown of the ScopeOne devices, which the caller runs off the GUI thread + void HardwareRuntime::applyScopeOneShutdownPreset() + { + QString error; + if (!applyScopeOnePreset(QStringLiteral("System"), QStringLiteral("Shutdown"), &error)) + { + qWarning().noquote() << QStringLiteral("System/Shutdown failed: %1").arg(error); + } + } + // Removes the drivers. Micro-Manager stays registered, as the runtime owns it for its lifetime. void HardwareRuntime::clear() { diff --git a/ScopeOneCore/src/ScopeOneCore.cpp b/ScopeOneCore/src/ScopeOneCore.cpp index 0f9e4f0..f84ba6b 100644 --- a/ScopeOneCore/src/ScopeOneCore.cpp +++ b/ScopeOneCore/src/ScopeOneCore.cpp @@ -1748,6 +1748,7 @@ namespace scopeone::core void ScopeOneCore::unloadConfigurationForShutdown() { applySystemShutdownPreset(); + m_managers->hardwareRuntime->applyScopeOneShutdownPreset(); clearConfigurationRuntime(false, true); m_managers->hardwareRuntime->unloadMicroManagerDevices(); m_managers->hardwareRuntime->clear(); @@ -1846,7 +1847,7 @@ namespace scopeone::core applySystemShutdownPreset(); m_configurationOperationRunning = true; clearConfigurationRuntime(true, false); - stopDriverHosts([this, path]() { startConfigurationLoadTask(path); }); + stopDriverHosts([this, path]() { startConfigurationLoadTask(path); }, true); return true; } @@ -1910,9 +1911,19 @@ namespace scopeone::core result.cameraIds.append(device.logicalId); } } - m_configurationOperationRunning = false; - emit hardwareConfigurationChanged(); - emit configurationLoadFinished(true, result, {}); + // System/Startup of the ScopeOne devices sets properties over the DriverHosts, which + // the hardware thread does while the load stays unfinished + auto* startup = new QFutureWatcher(this); + connect(startup, &QFutureWatcher::finished, this, [this, startup, result]() + { + startup->deleteLater(); + m_configurationOperationRunning = false; + emit hardwareConfigurationChanged(); + emit configurationLoadFinished(true, result, {}); + }); + HardwareRuntime* const runtime = m_managers->hardwareRuntime; + startup->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), + [runtime]() { runtime->applyScopeOneStartupPreset(); })); } else { @@ -1943,19 +1954,38 @@ namespace scopeone::core watcher->setFuture(future); } - void ScopeOneCore::stopDriverHosts(std::function next) + // Stops the DriverHosts away from the GUI thread. With applyShutdown, System/Shutdown of the + // ScopeOne devices is applied first, as the hosts are taken only after it. + void ScopeOneCore::stopDriverHosts(std::function next, bool applyShutdown) { - auto* watcher = new QFutureWatcher(this); - connect(watcher, &QFutureWatcher::finished, this, [watcher, next = std::move(next)]() + auto stop = [this, next = std::move(next)]() { - watcher->deleteLater(); - next(); + auto* watcher = new QFutureWatcher(this); + connect(watcher, &QFutureWatcher::finished, this, [watcher, next]() + { + watcher->deleteLater(); + next(); + }); + watcher->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), + [hosts = m_managers->hardwareRuntime->takeDriverHosts()]() mutable + { + hosts.clear(); + })); + }; + if (!applyShutdown) + { + stop(); + return; + } + auto* shutdown = new QFutureWatcher(this); + connect(shutdown, &QFutureWatcher::finished, this, [shutdown, stop]() + { + shutdown->deleteLater(); + stop(); }); - watcher->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), - [hosts = m_managers->hardwareRuntime->takeDriverHosts()]() mutable - { - hosts.clear(); - })); + HardwareRuntime* const runtime = m_managers->hardwareRuntime; + shutdown->setFuture(QtConcurrent::run(m_hardwareThreadPool.get(), + [runtime]() { runtime->applyScopeOneShutdownPreset(); })); } // Stops the cameras and removes the devices after a failed configuration load @@ -2009,7 +2039,7 @@ namespace scopeone::core applySystemShutdownPreset(); m_configurationOperationRunning = true; clearConfigurationRuntime(true, false); - stopDriverHosts([this]() { startConfigurationUnloadTask(); }); + stopDriverHosts([this]() { startConfigurationUnloadTask(); }, true); return true; } diff --git a/ScopeOneCore/src/ScopeOneProvider.cpp b/ScopeOneCore/src/ScopeOneProvider.cpp index e2f29f7..b97bb48 100644 --- a/ScopeOneCore/src/ScopeOneProvider.cpp +++ b/ScopeOneCore/src/ScopeOneProvider.cpp @@ -53,6 +53,11 @@ namespace scopeone::core::internal for (const HardwareDeviceDescriptor& peripheral : device->devices()) { const QStringList names = device->listProperties(peripheral.logicalId); + // One read of every property fills the cache that the presets are matched against + for (const QString& name : names) + { + device->getProperty(peripheral.logicalId, name, false); + } for (const auto& property : line.properties) { if (names.contains(property.first) diff --git a/config/MMConfig_demo.cfg b/config/MMConfig_demo.cfg index e150967..c00ad03 100644 --- a/config/MMConfig_demo.cfg +++ b/config/MMConfig_demo.cfg @@ -142,6 +142,7 @@ ConfigGroup,Channel,FITC,Dichroic,Label,Q505LP ConfigGroup,Channel,FITC,Emission,Label,Chroma-HQ535 ConfigGroup,Channel,FITC,Excitation,Label,Chroma-HQ480 ConfigGroup,Channel,FITC,Core,Shutter,White Light Shutter +ScopeOneConfigGroup,Channel,FITC,Example Camera.1,Pattern,Gradient # Preset: Rhodamine ConfigGroup,Channel,Rhodamine,Dichroic,Label,Q585LP @@ -154,6 +155,7 @@ ConfigGroup,Channel,DAPI,Dichroic,Label,400DCLP ConfigGroup,Channel,DAPI,Emission,Label,Chroma-HQ620 ConfigGroup,Channel,DAPI,Excitation,Label,Chroma-D360 ConfigGroup,Channel,DAPI,Core,Shutter,White Light Shutter +ScopeOneConfigGroup,Channel,DAPI,Example Camera.1,Pattern,Hologram # Preset: Cy5 ConfigGroup,Channel,Cy5,Dichroic,Label,400DCLP From 3e25e1af53618859fa57acf414671fbcf18fff65 Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 18:10:25 +0100 Subject: [PATCH 37/49] Add a serial driver that runs JSON device descriptions, starting with the Cobolt 05-01 lasers --- README.md | 6 + ScopeOneCore/cmake/ScopeOneDriver.cmake | 14 + .../include/scopeone/DriverManifest.h | 3 + ScopeOneCore/src/DriverHostClient.cpp | 32 +- ScopeOneCore/src/DriverHostMain.cpp | 11 +- ScopeOneCore/src/DriverManifest.cpp | 38 ++ ScopeOneCore/src/ScopeOneProvider.cpp | 44 +- drivers/CMakeLists.txt | 1 + drivers/Serial/CMakeLists.txt | 18 + drivers/Serial/SerialDevice.cpp | 546 ++++++++++++++++++ drivers/Serial/SerialDevice.h | 123 ++++ drivers/Serial/SerialDriver.cpp | 41 ++ drivers/Serial/driver.json | 7 + drivers/Serial/profiles/cobolt-05-01.json | 25 + vcpkg.json | 4 + 15 files changed, 886 insertions(+), 27 deletions(-) create mode 100644 drivers/Serial/CMakeLists.txt create mode 100644 drivers/Serial/SerialDevice.cpp create mode 100644 drivers/Serial/SerialDevice.h create mode 100644 drivers/Serial/SerialDriver.cpp create mode 100644 drivers/Serial/driver.json create mode 100644 drivers/Serial/profiles/cobolt-05-01.json diff --git a/README.md b/README.md index 5e97ff0..a95883c 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,8 @@ ScopeOne needs Qt 6.8 or later. There are three ways to get it. `winget` comes with Windows 10 and 11. The command installs MSVC, the Windows SDK and CMake. It asks for administrator rights, downloads a few gigabytes and takes 10 to 20 minutes. An existing Visual Studio 2022 with "Desktop development with C++" works as well. + A Qt passed with `--qt` needs the Qt Serial Port module, which the serial driver links. + 2. **Clone ScopeOne.** ```powershell @@ -173,6 +175,8 @@ macOS support is experimental. The second command installs the X11 and OpenGL development files that vcpkg's Qt needs, and `--no-upgrade` keeps the installed Mesa drivers. + A Qt passed with `--qt` needs the Qt Serial Port module, which the serial driver links. + macOS: ```bash @@ -182,6 +186,8 @@ macOS support is experimental. pkg-config ninja boost ``` + A Qt passed with `--qt` needs the Qt Serial Port module, which the serial driver links. + 2. **Clone ScopeOne.** ```bash diff --git a/ScopeOneCore/cmake/ScopeOneDriver.cmake b/ScopeOneCore/cmake/ScopeOneDriver.cmake index 8246e81..fb59402 100644 --- a/ScopeOneCore/cmake/ScopeOneDriver.cmake +++ b/ScopeOneCore/cmake/ScopeOneDriver.cmake @@ -35,6 +35,20 @@ function(scopeone_add_driver target) if (UNIX AND NOT APPLE) target_link_options(${target} PRIVATE "LINKER:--no-undefined") endif () + # DriverHost dlopens the module, so the RPATH of DriverHost does not reach the libraries the + # module needs. The module gets its own, with the link directories of its dependencies. + if (APPLE) + set(_origin "@loader_path") + elseif (UNIX) + set(_origin "$ORIGIN") + endif () + if (UNIX) + set_target_properties(${target} PROPERTIES + BUILD_RPATH "${_origin}" + INSTALL_RPATH "${_origin}" + INSTALL_RPATH_USE_LINK_PATH TRUE + ) + endif () install(TARGETS ${target} RUNTIME DESTINATION "drivers" LIBRARY DESTINATION "drivers" diff --git a/ScopeOneCore/include/scopeone/DriverManifest.h b/ScopeOneCore/include/scopeone/DriverManifest.h index 79902ef..03bfae4 100644 --- a/ScopeOneCore/include/scopeone/DriverManifest.h +++ b/ScopeOneCore/include/scopeone/DriverManifest.h @@ -19,6 +19,8 @@ namespace scopeone::core QJsonObject metadata; }; + // A driver library, or a description file of a serial device, which the Serial driver runs. + // For a description the path is the file itself. struct DiscoveredDriver { DriverManifest manifest; @@ -26,6 +28,7 @@ namespace scopeone::core QString interfaceId; QJsonObject metadata; QString error; + bool description{false}; }; // Raised whenever an interface of the driver SDK changes its virtual functions. A driver built diff --git a/ScopeOneCore/src/DriverHostClient.cpp b/ScopeOneCore/src/DriverHostClient.cpp index 089e4e3..99aab6f 100644 --- a/ScopeOneCore/src/DriverHostClient.cpp +++ b/ScopeOneCore/src/DriverHostClient.cpp @@ -147,6 +147,7 @@ namespace scopeone::core::internal m_process->readAllStandardOutput()).trimmed(); if (!output.isEmpty()) { + m_lastHostLine = output.split(QLatin1Char('\n')).last(); qInfo().noquote() << QString("[DriverHost %1] %2").arg(m_providerId, output); } @@ -183,6 +184,8 @@ namespace scopeone::core::internal } const QString serverName = driverhost::controlServerName(m_hostKey); + // This runs on a worker thread without an event loop, so the exit is polled for here + bool exited = false; QElapsedTimer connectTimer; connectTimer.start(); while (connectTimer.elapsed() < kControlReadyTimeoutMs) @@ -193,8 +196,14 @@ namespace scopeone::core::internal { break; } - if (m_process->state() == QProcess::NotRunning) + if (m_process->waitForFinished(0)) { + exited = true; + const QString rest = QString::fromUtf8(m_process->readAllStandardOutput()).trimmed(); + if (!rest.isEmpty()) + { + m_lastHostLine = rest.split(QLatin1Char('\n')).last(); + } break; } } @@ -202,7 +211,12 @@ namespace scopeone::core::internal { if (errorMessage) { - *errorMessage = QStringLiteral("DriverHost control server did not become ready"); + // A host that exits at start names its reason as its last line + const QString reasonPrefix = + QStringLiteral("DriverHost init failed for '%1': ").arg(m_hostKey); + *errorMessage = exited && m_lastHostLine.startsWith(reasonPrefix) + ? m_lastHostLine.mid(reasonPrefix.size()) + : QStringLiteral("DriverHost control server did not become ready"); } cleanup(); return false; @@ -290,18 +304,6 @@ namespace scopeone::core::internal m_cameraIds.append(device.logicalId); } } - // A driver that exposes no device is still a device of its label - if (m_rename && deviceArray.isEmpty()) - { - HardwareDeviceDescriptor device; - device.logicalId = m_devices.add(QString{}, m_providerId); - device.providerId = descriptor.id; - device.name = m_providerId; - device.kind = HardwareDeviceKind::Generic; - device.state = HardwareDeviceState::Initialized; - device.endpoint = HardwareEndpointKind::DriverHost; - devices.append(device); - } int sourceIndex = 0; for (const QJsonValue& value : sourceArray) { @@ -1073,6 +1075,8 @@ namespace scopeone::core::internal QEventLoop* m_waitingLoop{nullptr}; QJsonObject m_waitingResponse; QString m_lastError; + // The last line the DriverHost printed, which is its reason when it exits at start + QString m_lastHostLine; quint64 m_nextRequestId{1}; quint64 m_waitingRequestId{0}; bool m_cleanedUp{false}; diff --git a/ScopeOneCore/src/DriverHostMain.cpp b/ScopeOneCore/src/DriverHostMain.cpp index 3825d77..d74128b 100644 --- a/ScopeOneCore/src/DriverHostMain.cpp +++ b/ScopeOneCore/src/DriverHostMain.cpp @@ -401,7 +401,8 @@ namespace scopeone::core::internal const QString deviceName = m_deviceName.isEmpty() && !deviceNames.isEmpty() ? deviceNames.first() : m_deviceName; - if (!deviceNames.contains(deviceName)) + // A driver that lists no names checks the name in createDevice itself + if (!deviceNames.isEmpty() && !deviceNames.contains(deviceName)) { m_lastError = QStringLiteral("The driver has no device %1").arg(deviceName); return false; @@ -438,6 +439,10 @@ namespace scopeone::core::internal m_state = dynamic_cast(provider); m_signals = dynamic_cast(provider); m_daq = dynamic_cast(provider); + // A device without peripherals is one device under its label, and its kind follows its capability + m_rootDevice.name = m_providerId; + m_rootDevice.kind = m_shutter ? HardwareDeviceKind::Shutter : HardwareDeviceKind::Generic; + m_rootDevice.state = HardwareDeviceState::Initialized; QSet logicalIds; int cameraIndex = 0; @@ -1049,6 +1054,10 @@ namespace scopeone::core::internal { devices.append(deviceDescription(device)); } + if (m_devices.isEmpty() && !m_microManagerCamera) + { + devices.append(deviceDescription(m_rootDevice)); + } response.insert(driverhost::kDevicesField, devices); QJsonArray signalSources; for (const SignalSourceDescriptor& source : m_signalSources) diff --git a/ScopeOneCore/src/DriverManifest.cpp b/ScopeOneCore/src/DriverManifest.cpp index 9972209..3ccd153 100644 --- a/ScopeOneCore/src/DriverManifest.cpp +++ b/ScopeOneCore/src/DriverManifest.cpp @@ -4,7 +4,9 @@ #include #include +#include #include +#include #include #include #include @@ -20,6 +22,33 @@ namespace scopeone::core .filePath(QStringLiteral("drivers"))}; } + namespace + { + // A description file has a properties object. The manifests of the libraries do not, so they + // are skipped here. + bool readDeviceDescription(const QString& path, DiscoveredDriver& driver) + { + QFile file(path); + if (!file.open(QIODevice::ReadOnly)) + { + return false; + } + const QJsonObject object = QJsonDocument::fromJson(file.readAll()).object(); + if (!object.value(QStringLiteral("properties")).isObject()) + { + return false; + } + // The file name is the id, as the cfg names the description by it + driver.manifest.id = QFileInfo(path).completeBaseName(); + driver.manifest.name = object.value(QStringLiteral("name")).toString(); + driver.manifest.metadata = object; + driver.path = path; + driver.metadata = object; + driver.description = true; + return true; + } + } + QList discoverDrivers(const QStringList& directories) { QList drivers; @@ -29,6 +58,15 @@ namespace scopeone::core const QDir directory(directoryPath); for (const QFileInfo& file : directory.entryInfoList(QDir::Files, QDir::Name)) { + if (file.suffix() == QStringLiteral("json")) + { + DiscoveredDriver description; + if (readDeviceDescription(file.absoluteFilePath(), description)) + { + drivers.append(std::move(description)); + } + continue; + } if (!QLibrary::isLibrary(file.absoluteFilePath())) { continue; diff --git a/ScopeOneCore/src/ScopeOneProvider.cpp b/ScopeOneCore/src/ScopeOneProvider.cpp index b97bb48..3396167 100644 --- a/ScopeOneCore/src/ScopeOneProvider.cpp +++ b/ScopeOneCore/src/ScopeOneProvider.cpp @@ -9,6 +9,7 @@ #include #include +#include #include "internal/DriverHostClient.h" #include "internal/MicroManagerProvider.h" @@ -20,19 +21,24 @@ namespace scopeone::core::internal { // Creates the device of one line in a DriverHost and applies the properties that are not // pre-init. The pre-init ones reach the driver as the arguments of createDevice. - DevicePtr startDevice(const ScopeOneDeviceLine& line, - const QList& drivers, - QString* errorMessage) + // The driver with the id that loads, or nullptr + const DiscoveredDriver* findDriver(const QList& drivers, const QString& id) { - const DiscoveredDriver* driver = nullptr; for (const DiscoveredDriver& candidate : drivers) { - if (candidate.error.isEmpty() && candidate.manifest.id == line.driverId) + if (candidate.error.isEmpty() && candidate.manifest.id == id) { - driver = &candidate; - break; + return &candidate; } } + return nullptr; + } + + DevicePtr startDevice(const ScopeOneDeviceLine& line, + const QList& drivers, + QString* errorMessage) + { + const DiscoveredDriver* driver = findDriver(drivers, line.driverId); if (!driver) { *errorMessage = QStringLiteral("No driver %1").arg(line.driverId); @@ -44,8 +50,21 @@ namespace scopeone::core::internal { preInitProperties.insert(property.first, property.second); } + // A description file is a device of the Serial driver, which gets the file as a pre-init property + QString libraryPath = driver->path; + if (driver->description) + { + const DiscoveredDriver* serial = findDriver(drivers, QStringLiteral("scopeone.serial")); + if (!serial) + { + *errorMessage = QStringLiteral("No driver scopeone.serial for %1").arg(line.driverId); + return {}; + } + libraryPath = serial->path; + preInitProperties.insert(QStringLiteral("Description File"), driver->path); + } DevicePtr device = createScopeOneDevice( - line.label, driver->path, line.deviceName, preInitProperties, errorMessage); + line.label, libraryPath, line.deviceName, preInitProperties, errorMessage); if (!device) { return {}; @@ -145,7 +164,7 @@ namespace scopeone::core::internal // Each DriverHost loads its device on its own, which can take seconds, so they start // together - QList> hosts; + QList>> hosts; for (const ScopeOneDeviceLine& line : std::as_const(m_configuration.devices)) { hosts.append(QtConcurrent::run([line, &drivers]() @@ -157,15 +176,16 @@ namespace scopeone::core::internal qWarning().noquote() << QStringLiteral("Failed to start ScopeOne device '%1': %2") .arg(line.label, error); } - return device; + return std::make_pair(device, error); })); } for (qsizetype index = 0; index < hosts.size(); ++index) { - const DevicePtr device = hosts[index].result(); + const auto [device, error] = hosts[index].result(); if (!device) { - result.failedDevices.append(m_configuration.devices[index].label); + result.failedDevices.append( + QStringLiteral("%1 (%2)").arg(m_configuration.devices[index].label, error)); continue; } m_devices.append(device); diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 3347856..e1802e5 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -24,3 +24,4 @@ find_package(ScopeOneCore CONFIG REQUIRED) add_subdirectory(ExampleCamera) add_subdirectory(ExamplePmt) add_subdirectory(NIDaqmx) +add_subdirectory(Serial) diff --git a/drivers/Serial/CMakeLists.txt b/drivers/Serial/CMakeLists.txt new file mode 100644 index 0000000..6e9ddf3 --- /dev/null +++ b/drivers/Serial/CMakeLists.txt @@ -0,0 +1,18 @@ +# The serial driver runs the description files in profiles, and it is the only driver that links +# Qt SerialPort +find_package(Qt6 ${SCOPEONE_QT_MINIMUM_VERSION} REQUIRED COMPONENTS Core SerialPort) + +add_library(ScopeOneSerial MODULE + SerialDriver.cpp + SerialDevice.cpp + SerialDevice.h + driver.json +) +target_link_libraries(ScopeOneSerial PRIVATE scopeone::DriverSDK Qt6::Core Qt6::SerialPort) +scopeone_add_driver(ScopeOneSerial) + +# The description files sit beside the libraries, and DriverManifest lists them as drivers +install(FILES + profiles/cobolt-05-01.json + DESTINATION "drivers" +) diff --git a/drivers/Serial/SerialDevice.cpp b/drivers/Serial/SerialDevice.cpp new file mode 100644 index 0000000..de378e0 --- /dev/null +++ b/drivers/Serial/SerialDevice.cpp @@ -0,0 +1,546 @@ +#include "SerialDevice.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace serial_driver +{ + namespace + { + const QString kPort = QStringLiteral("Port"); + const QString kBaudRate = QStringLiteral("BaudRate"); + const QString kAnswerTimeout = QStringLiteral("AnswerTimeout"); + const QString kDescriptionFile = QStringLiteral("Description File"); + + bool isPreInitName(const QString& name) + { + return name == kPort || name == kBaudRate || name == kAnswerTimeout || name == kDescriptionFile; + } + } + + std::optional loadSerialProfile(const QString& path, + const QString& deviceName, + QString* errorMessage) + { + QFile file(path); + if (!file.open(QIODevice::ReadOnly)) + { + *errorMessage = QStringLiteral("Cannot read the description file %1").arg(path); + return std::nullopt; + } + QJsonParseError parseError; + const QJsonDocument document = QJsonDocument::fromJson(file.readAll(), &parseError); + if (parseError.error != QJsonParseError::NoError || !document.isObject()) + { + *errorMessage = QStringLiteral("The description file %1 is not a JSON object: %2") + .arg(path, parseError.errorString()); + return std::nullopt; + } + + const QJsonObject root = document.object(); + SerialProfile profile; + profile.name = root.value(QStringLiteral("name")).toString(); + for (const QJsonValue& value : root.value(QStringLiteral("devices")).toArray()) + { + profile.devices.append(value.toString()); + } + if (!profile.devices.contains(deviceName)) + { + *errorMessage = QStringLiteral("%1 has no device %2").arg(profile.name, deviceName); + return std::nullopt; + } + + const QJsonObject serial = root.value(QStringLiteral("serial")).toObject(); + profile.baudRate = serial.value(QStringLiteral("BaudRate")).toInt(profile.baudRate); + const QString parity = serial.value(QStringLiteral("parity")).toString(QStringLiteral("none")); + profile.parity = parity == QStringLiteral("even") ? QSerialPort::EvenParity + : parity == QStringLiteral("odd") ? QSerialPort::OddParity + : QSerialPort::NoParity; + profile.stopBits = serial.value(QStringLiteral("stopBits")).toInt(1) == 2 + ? QSerialPort::TwoStop + : QSerialPort::OneStop; + const QString flowControl = serial.value(QStringLiteral("flowControl")).toString(QStringLiteral("none")); + profile.flowControl = flowControl == QStringLiteral("hardware") ? QSerialPort::HardwareControl + : flowControl == QStringLiteral("software") ? QSerialPort::SoftwareControl + : QSerialPort::NoFlowControl; + + const QJsonObject terminator = root.value(QStringLiteral("terminator")).toObject(); + profile.writeTerminator = terminator.value(QStringLiteral("write")).toString(); + profile.readTerminator = terminator.value(QStringLiteral("read")).toString(); + profile.setReply = root.value(QStringLiteral("setReply")).toString(); + profile.errorPattern = root.value(QStringLiteral("error")).toString(); + profile.ack = root.value(QStringLiteral("ack")).toString(); + profile.answerTimeout = root.value(QStringLiteral("AnswerTimeout")).toInt(profile.answerTimeout); + + const QJsonObject properties = root.value(QStringLiteral("properties")).toObject(); + for (auto it = properties.constBegin(); it != properties.constEnd(); ++it) + { + const QJsonObject object = it.value().toObject(); + SerialProperty property; + property.type = object.value(QStringLiteral("type")).toString(); + property.get = object.value(QStringLiteral("get")).toString(); + property.set = object.value(QStringLiteral("set")).toString(); + property.parse = object.value(QStringLiteral("parse")).toString(); + const QJsonObject values = object.value(QStringLiteral("values")).toObject(); + for (auto value = values.constBegin(); value != values.constEnd(); ++value) + { + property.values.append({value.key(), value.value().toString()}); + } + const QJsonValue minimum = object.value(QStringLiteral("min")); + if (minimum.isDouble()) + { + property.min = minimum.toDouble(); + } + // A maximum is either a number or a query that the device answers once at creation + const QJsonValue maximum = object.value(QStringLiteral("max")); + if (maximum.isDouble()) + { + property.max = maximum.toDouble(); + } + else + { + property.maxGet = maximum.toObject().value(QStringLiteral("get")).toString(); + } + profile.properties.insert(it.key(), property); + } + + const QJsonObject shutter = root.value(QStringLiteral("shutter")).toObject(); + if (!shutter.isEmpty()) + { + profile.shutter = SerialShutter{shutter.value(QStringLiteral("property")).toString(), + shutter.value(QStringLiteral("open")).toString(), + shutter.value(QStringLiteral("closed")).toString()}; + } + return profile; + } + + DevicePtr createSerialDevice(const QString& name, + const QVariantMap& preInitProperties, + QString* errorMessage) + { + const QString descriptionPath = preInitProperties.value(kDescriptionFile).toString(); + std::optional loaded = loadSerialProfile(descriptionPath, name, errorMessage); + if (!loaded) + { + return {}; + } + SerialProfile profile = std::move(*loaded); + profile.baudRate = preInitProperties.value(kBaudRate, profile.baudRate).toInt(); + profile.answerTimeout = preInitProperties.value(kAnswerTimeout, profile.answerTimeout).toInt(); + const QString port = preInitProperties.value(kPort).toString(); + if (port.isEmpty()) + { + *errorMessage = QStringLiteral("The serial device has no Port"); + return {}; + } + + std::shared_ptr device; + if (profile.shutter) + { + device = std::make_shared(std::move(profile), port, descriptionPath); + } + else + { + device = std::make_shared(std::move(profile), port, descriptionPath); + } + if (!device->open(errorMessage)) + { + return {}; + } + return device; + } + + SerialDevice::SerialDevice(SerialProfile profile, const QString& portName, const QString& descriptionPath) + : m_profile(std::move(profile)) + { + // The pre-init properties show what the device was created with + m_cache.insert(kPort, portName); + m_cache.insert(kBaudRate, QString::number(m_profile.baudRate)); + m_cache.insert(kAnswerTimeout, QString::number(m_profile.answerTimeout)); + m_cache.insert(kDescriptionFile, descriptionPath); + } + + bool SerialDevice::open(QString* errorMessage) + { + const QString portName = m_cache.value(kPort); + m_port.setPortName(portName); + m_port.setBaudRate(m_profile.baudRate); + m_port.setDataBits(QSerialPort::Data8); + m_port.setParity(m_profile.parity); + m_port.setStopBits(m_profile.stopBits); + m_port.setFlowControl(m_profile.flowControl); + if (!m_port.open(QIODevice::ReadWrite)) + { + *errorMessage = QStringLiteral("Cannot open the serial port %1: %2") + .arg(portName, m_port.errorString()); + return false; + } + + for (auto it = m_profile.properties.begin(); it != m_profile.properties.end(); ++it) + { + if (it->maxGet.isEmpty()) + { + continue; + } + QString reply; + if (!exchange(it->maxGet, &reply, errorMessage)) + { + return false; + } + bool ok = false; + const double maximum = reply.toDouble(&ok); + if (!ok) + { + *errorMessage = QStringLiteral("Reply \"%1\" to \"%2\" from %3 is not a number") + .arg(reply, it->maxGet, portName); + return false; + } + it->max = maximum; + } + return true; + } + + HardwareProviderDescriptor SerialDevice::descriptor() const + { + return {QString{}, m_profile.name, QStringLiteral("1")}; + } + + QStringList SerialDevice::listProperties(const QString&) + { + QStringList names{kPort, kBaudRate, kAnswerTimeout, kDescriptionFile}; + names.append(m_profile.properties.keys()); + return names; + } + + QString SerialDevice::getProperty(const QString&, const QString& name, bool fromCache) + { + const auto it = m_profile.properties.constFind(name); + if (fromCache || it == m_profile.properties.cend() || it->get.isEmpty()) + { + return m_cache.value(name); + } + QString value; + QString errorMessage; + if (!readValue(name, &value, &errorMessage)) + { + // The last value the device answered stays as the value of the property + qWarning().noquote() << errorMessage; + return m_cache.value(name); + } + return value; + } + + bool SerialDevice::setProperty(const QString&, + const QString& name, + const QString& value, + QString* errorMessage) + { + QString unused; + return setValue(name, value, errorMessage ? errorMessage : &unused); + } + + QString SerialDevice::getPropertyType(const QString&, const QString& name) + { + if (isPreInitName(name)) + { + return name == kBaudRate || name == kAnswerTimeout ? QStringLiteral("Integer") : QStringLiteral("String"); + } + const auto it = m_profile.properties.constFind(name); + if (it == m_profile.properties.cend()) + { + return {}; + } + if (it->type == QStringLiteral("float")) + { + return QStringLiteral("Float"); + } + if (it->type == QStringLiteral("int")) + { + return QStringLiteral("Integer"); + } + return QStringLiteral("String"); + } + + bool SerialDevice::isPropertyReadOnly(const QString&, const QString& name) + { + const auto it = m_profile.properties.constFind(name); + return isPreInitName(name) || it == m_profile.properties.cend() || it->set.isEmpty(); + } + + bool SerialDevice::isPropertyPreInit(const QString&, const QString& name) + { + return isPreInitName(name); + } + + QStringList SerialDevice::getAllowedPropertyValues(const QString&, const QString& name) + { + const auto it = m_profile.properties.constFind(name); + if (it == m_profile.properties.cend()) + { + return {}; + } + if (it->type == QStringLiteral("action")) + { + return {QStringLiteral("Run")}; + } + QStringList names; + for (const auto& value : it->values) + { + names.append(value.first); + } + return names; + } + + bool SerialDevice::hasPropertyLimits(const QString&, const QString& name) + { + const auto it = m_profile.properties.constFind(name); + return it != m_profile.properties.cend() && it->min && it->max; + } + + double SerialDevice::getPropertyLowerLimit(const QString&, const QString& name) + { + const auto it = m_profile.properties.constFind(name); + return it == m_profile.properties.cend() ? 0.0 : it->min.value_or(0.0); + } + + double SerialDevice::getPropertyUpperLimit(const QString&, const QString& name) + { + const auto it = m_profile.properties.constFind(name); + return it == m_profile.properties.cend() ? 0.0 : it->max.value_or(0.0); + } + + bool SerialDevice::readValue(const QString& name, QString* value, QString* errorMessage) const + { + const auto it = m_profile.properties.constFind(name); + if (it == m_profile.properties.cend() || it->get.isEmpty()) + { + *errorMessage = QStringLiteral("%1 cannot be read").arg(name); + return false; + } + if (!query(*it, value, errorMessage)) + { + return false; + } + m_cache.insert(name, *value); + return true; + } + + bool SerialDevice::setValue(const QString& name, const QString& value, QString* errorMessage) + { + const auto it = m_profile.properties.constFind(name); + if (it == m_profile.properties.cend() || it->set.isEmpty()) + { + *errorMessage = QStringLiteral("%1 cannot be set").arg(name); + return false; + } + const SerialProperty& property = *it; + + // The command carries the token of the value, so an enum value goes out as its token + QString token = value; + if (property.type == QStringLiteral("enum")) + { + token.clear(); + for (const auto& allowed : property.values) + { + if (allowed.first == value) + { + token = allowed.second; + } + } + if (token.isEmpty()) + { + *errorMessage = QStringLiteral("%1 is not a value of %2").arg(value, name); + return false; + } + } + else if (property.type == QStringLiteral("float") || property.type == QStringLiteral("int")) + { + bool ok = false; + const double number = value.toDouble(&ok); + if (!ok || (property.min && number < *property.min) || (property.max && number > *property.max)) + { + *errorMessage = QStringLiteral("%1 is not a number in the range of %2").arg(value, name); + return false; + } + // QString::number wrote small values such as 5e-05, which instruments do not read, so + // floats are sent in fixed notation without trailing zeros + if (property.type == QStringLiteral("int")) + { + token = QString::number(std::llround(number)); + } + else + { + token = QString::number(number, 'f', 9); + token.remove(QRegularExpression(QStringLiteral("\\.?0+$"))); + } + } + + QString command = property.set; + command.replace(QStringLiteral("{value}"), token); + QString reply; + if (!exchange(command, &reply, errorMessage)) + { + return false; + } + if (reply != m_profile.setReply) + { + *errorMessage = QStringLiteral("Reply \"%1\" to \"%2\" from %3 is not \"%4\"") + .arg(reply, command, m_cache.value(kPort), m_profile.setReply); + return false; + } + m_cache.insert(name, value); + return true; + } + + // One command and its reply. The reply is the line after the echo of the command, and an ack + // line follows it when the description has one. + bool SerialDevice::exchange(const QString& command, QString* reply, QString* errorMessage) const + { + const QString portName = m_port.portName(); + m_port.clear(QSerialPort::Input); + m_pending.clear(); + const QByteArray bytes = (command + m_profile.writeTerminator).toLatin1(); + if (m_port.write(bytes) != bytes.size() || !m_port.waitForBytesWritten(m_profile.answerTimeout)) + { + *errorMessage = QStringLiteral("Cannot send \"%1\" to %2: %3") + .arg(command, portName, m_port.errorString()); + return false; + } + + do + { + if (!readLine(reply)) + { + *errorMessage = QStringLiteral("No reply to \"%1\" from %2 within %3 ms") + .arg(command, portName) + .arg(m_profile.answerTimeout); + return false; + } + } while (*reply == command); + if (isErrorLine(*reply)) + { + *errorMessage = QStringLiteral("Error reply \"%1\" to \"%2\" from %3") + .arg(*reply, command, portName); + return false; + } + + if (!m_profile.ack.isEmpty()) + { + QString ack; + if (!readLine(&ack)) + { + *errorMessage = QStringLiteral("No ack to \"%1\" from %2 within %3 ms") + .arg(command, portName) + .arg(m_profile.answerTimeout); + return false; + } + // The error pattern was checked only on the reply line, so an error in place of the ack + // was reported as a wrong ack instead of the device's error + if (isErrorLine(ack)) + { + *errorMessage = QStringLiteral("Error reply \"%1\" to \"%2\" from %3") + .arg(ack, command, portName); + return false; + } + if (ack != m_profile.ack) + { + *errorMessage = QStringLiteral("Reply \"%1\" to \"%2\" from %3 is not \"%4\"") + .arg(ack, command, portName, m_profile.ack); + return false; + } + } + return true; + } + + // Reads up to the read terminator, and waits no longer than the answer timeout in all + bool SerialDevice::readLine(QString* line) const + { + const QByteArray terminator = m_profile.readTerminator.toLatin1(); + QElapsedTimer timer; + timer.start(); + qsizetype end = m_pending.indexOf(terminator); + while (end < 0) + { + const qint64 remaining = m_profile.answerTimeout - timer.elapsed(); + if (remaining <= 0 || !m_port.waitForReadyRead(static_cast(remaining))) + { + return false; + } + m_pending += m_port.readAll(); + end = m_pending.indexOf(terminator); + } + *line = QString::fromLatin1(m_pending.first(end)); + m_pending.remove(0, end + terminator.size()); + return true; + } + + bool SerialDevice::isErrorLine(const QString& line) const + { + return !m_profile.errorPattern.isEmpty() && QRegularExpression(m_profile.errorPattern).match(line).hasMatch(); + } + + bool SerialDevice::query(const SerialProperty& property, QString* value, QString* errorMessage) const + { + QString reply; + if (!exchange(property.get, &reply, errorMessage)) + { + return false; + } + if (!property.parse.isEmpty()) + { + const QRegularExpressionMatch match = QRegularExpression(property.parse).match(reply); + if (!match.hasMatch()) + { + *errorMessage = QStringLiteral("Unexpected reply \"%1\" to \"%2\" from %3") + .arg(reply, property.get, m_cache.value(kPort)); + return false; + } + reply = match.captured(1); + } + if (property.type == QStringLiteral("enum")) + { + // The device answers with a token, which the description names + for (const auto& allowed : property.values) + { + if (allowed.second == reply) + { + *value = allowed.first; + return true; + } + } + *errorMessage = QStringLiteral("Unexpected reply \"%1\" to \"%2\" from %3") + .arg(reply, property.get, m_cache.value(kPort)); + return false; + } + *value = reply; + return true; + } + + bool SerialShutterDevice::isShutterOpen(const QString&, bool& open, QString* errorMessage) const + { + QString unused; + QString value; + const SerialShutter& shutter = *profile().shutter; + if (!readValue(shutter.property, &value, errorMessage ? errorMessage : &unused)) + { + return false; + } + open = value == shutter.open; + return true; + } + + bool SerialShutterDevice::setShutterOpen(const QString&, bool open, QString* errorMessage) + { + QString unused; + const SerialShutter& shutter = *profile().shutter; + return setValue(shutter.property, open ? shutter.open : shutter.closed, errorMessage ? errorMessage : &unused); + } +} diff --git a/drivers/Serial/SerialDevice.h b/drivers/Serial/SerialDevice.h new file mode 100644 index 0000000..a4e0567 --- /dev/null +++ b/drivers/Serial/SerialDevice.h @@ -0,0 +1,123 @@ +#pragma once + +#include "scopeone/DriverSDK.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace serial_driver +{ + using namespace scopeone::core; + + // A property as the description file gives it. The values pair the display name with the + // token the device uses. + struct SerialProperty + { + QString type; + QString get; + QString set; + QString parse; + QList> values; + std::optional min; + std::optional max; + QString maxGet; + }; + + struct SerialShutter + { + QString property; + QString open; + QString closed; + }; + + // What a description file says about one serial device + struct SerialProfile + { + QString name; + QStringList devices; + int baudRate{9600}; + QSerialPort::Parity parity{QSerialPort::NoParity}; + QSerialPort::StopBits stopBits{QSerialPort::OneStop}; + QSerialPort::FlowControl flowControl{QSerialPort::NoFlowControl}; + QString writeTerminator; + QString readTerminator; + QString setReply; + QString errorPattern; + QString ack; + int answerTimeout{500}; + QMap properties; + std::optional shutter; + }; + + // Reads a description file, and returns nothing with the reason in errorMessage + std::optional loadSerialProfile(const QString& path, + const QString& deviceName, + QString* errorMessage); + + // Creates the device of a description file on the port in the pre-init properties + DevicePtr createSerialDevice(const QString& name, + const QVariantMap& preInitProperties, + QString* errorMessage); + + // A device that talks over a serial port with the commands of its description file + class SerialDevice : public Device + { + public: + SerialDevice(SerialProfile profile, const QString& portName, const QString& descriptionPath); + + // Opens the port named in the cache and reads the maximums that the description takes from the device + bool open(QString* errorMessage); + + HardwareProviderDescriptor descriptor() const override; + QStringList listProperties(const QString& deviceId) override; + QString getProperty(const QString& deviceId, const QString& name, bool fromCache = false) override; + bool setProperty(const QString& deviceId, + const QString& name, + const QString& value, + QString* errorMessage = nullptr) override; + QString getPropertyType(const QString& deviceId, const QString& name) override; + bool isPropertyReadOnly(const QString& deviceId, const QString& name) override; + bool isPropertyPreInit(const QString& deviceId, const QString& name) override; + QStringList getAllowedPropertyValues(const QString& deviceId, const QString& name) override; + bool hasPropertyLimits(const QString& deviceId, const QString& name) override; + double getPropertyLowerLimit(const QString& deviceId, const QString& name) override; + double getPropertyUpperLimit(const QString& deviceId, const QString& name) override; + + protected: + const SerialProfile& profile() const { return m_profile; } + // Reads a property from the device and caches the value + bool readValue(const QString& name, QString* value, QString* errorMessage) const; + bool setValue(const QString& name, const QString& value, QString* errorMessage); + + private: + bool exchange(const QString& command, QString* reply, QString* errorMessage) const; + bool readLine(QString* line) const; + bool isErrorLine(const QString& line) const; + bool query(const SerialProperty& property, QString* value, QString* errorMessage) const; + + SerialProfile m_profile; + // The port and the cache are mutable so that the const shutter query can read the device + mutable QSerialPort m_port; + mutable QByteArray m_pending; + mutable QHash m_cache; + }; + + // The serial device of a description with a shutter, which adds the Shutter capability + class SerialShutterDevice final : public SerialDevice, public Shutter + { + public: + using SerialDevice::SerialDevice; + + bool isShutterOpen(const QString& deviceId, bool& open, QString* errorMessage) const override; + bool setShutterOpen(const QString& deviceId, bool open, QString* errorMessage) override; + }; +} diff --git a/drivers/Serial/SerialDriver.cpp b/drivers/Serial/SerialDriver.cpp new file mode 100644 index 0000000..59fd7a9 --- /dev/null +++ b/drivers/Serial/SerialDriver.cpp @@ -0,0 +1,41 @@ +// The built-in driver of serial devices. Each description file in the drivers folder is a device +// of this driver, and the file says which commands the device takes. +#include "SerialDevice.h" + +#include "scopeone/DriverSDK.h" + +#include + +namespace serial_driver +{ + class SerialDriver final : public QObject, public Driver + { + Q_OBJECT + Q_PLUGIN_METADATA(IID ScopeOneDriver_iid FILE "scopeone_driver.json") + Q_INTERFACES(scopeone::core::Driver) + + public: + QString driverId() const override + { + return QStringLiteral("scopeone.serial"); + } + + // The devices are the description files, so the name is checked when the device is created + QStringList deviceNames() const override + { + return {}; + } + + DevicePtr createDevice(const QString& name, const QVariantMap& preInitProperties, QString* errorMessage) override + { + QString unused; + if (!errorMessage) + { + errorMessage = &unused; + } + return createSerialDevice(name, preInitProperties, errorMessage); + } + }; +} + +#include "SerialDriver.moc" diff --git a/drivers/Serial/driver.json b/drivers/Serial/driver.json new file mode 100644 index 0000000..971c135 --- /dev/null +++ b/drivers/Serial/driver.json @@ -0,0 +1,7 @@ +{ + "id": "scopeone.serial", + "name": "Serial Devices", + "version": "1.0.0", + "scopeOneApi": 3, + "kind": "driver" +} diff --git a/drivers/Serial/profiles/cobolt-05-01.json b/drivers/Serial/profiles/cobolt-05-01.json new file mode 100644 index 0000000..bf1fcb3 --- /dev/null +++ b/drivers/Serial/profiles/cobolt-05-01.json @@ -0,0 +1,25 @@ +{ + "name": "Cobolt 05-01", + "devices": ["Laser"], + "serial": {"BaudRate": 115200, "parity": "none", "stopBits": 1, "flowControl": "none"}, + "terminator": {"write": "\r", "read": "\r\n"}, + "setReply": "OK", + "error": "[Ee]rror", + "AnswerTimeout": 500, + "properties": { + "Laser": {"type": "enum", "get": "l?", "set": "l{value}", "values": {"Off": "0", "On": "1"}}, + "Operating Mode": {"type": "enum", "get": "gom?", + "values": {"Off": "0", "Waiting for Temperatures": "1", "Waiting for Key": "2", + "Warming Up": "3", "Completed": "4", "Fault": "5", "Aborted": "6", + "Waiting for Remote": "7", "Standby": "8"}}, + "Power Setpoint": {"type": "float", "unit": "W", "get": "glp?", "set": "slp {value}", + "min": 0, "max": {"get": "gmlp?"}}, + "Power Reading": {"type": "float", "unit": "W", "get": "pa?"}, + "Fault": {"type": "enum", "get": "f?", + "values": {"No Fault": "0", "TEC Fault": "1", "Interlock": "3", "Current Clamp": "4"}}, + "Clear Fault": {"type": "action", "set": "cf"}, + "Model": {"type": "string", "get": "glm?"}, + "Serial Number": {"type": "string", "get": "gsn?"}, + "Operating Hours": {"type": "string", "get": "hrs?"} + } +} diff --git a/vcpkg.json b/vcpkg.json index a136dcc..4b31ca9 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -30,6 +30,10 @@ "name": "qtsvg", "default-features": false }, + { + "name": "qtserialport", + "default-features": false + }, { "name": "qtbase", "default-features": false, From d890d6cf91ace4a49731f6c5b95b3b3f354fc97c Mon Sep 17 00:00:00 2001 From: tz <185176969+tzhaoo@users.noreply.github.com> Date: Sat, 7 Nov 2026 18:17:56 +0100 Subject: [PATCH 38/49] Document native devices and serial descriptions, install a description from the Drivers dialog and keep the Micro-Manager demo beside two ScopeOne demos --- .gitignore | 2 + ScopeOneCore/DRIVERHOST_PROTOCOL.md | 5 + ScopeOneCore/README.md | 10 +- config/MMConfig_ScopeOne_demo.cfg | 236 ++++++++++++++++++++++ config/MMConfig_demo.cfg | 7 - config/ScopeOne_demo.cfg | 10 + drivers/README.md | 177 +++++++++++++--- drivers/Serial/profiles/cobolt-05-01.json | 4 +- src/DriversDialog.cpp | 83 +++++++- src/DriversDialog.h | 1 + 10 files changed, 485 insertions(+), 50 deletions(-) create mode 100644 config/MMConfig_ScopeOne_demo.cfg create mode 100644 config/ScopeOne_demo.cfg diff --git a/.gitignore b/.gitignore index 74c17b7..bcfa79e 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,8 @@ !/config/MMConfig_demo.cfg !/config/dual_hamamatsu_897D.cfg !/config/MMConfig_demo_dual.cfg +!/config/MMConfig_ScopeOne_demo.cfg +!/config/ScopeOne_demo.cfg !/CMakeLists.txt !/README.md diff --git a/ScopeOneCore/DRIVERHOST_PROTOCOL.md b/ScopeOneCore/DRIVERHOST_PROTOCOL.md index 542cd60..cd7691d 100644 --- a/ScopeOneCore/DRIVERHOST_PROTOCOL.md +++ b/ScopeOneCore/DRIVERHOST_PROTOCOL.md @@ -69,6 +69,11 @@ autofocus `defaultAutofocus`. | `eventSequence` | cameras only, whether the camera takes a series of images in one sequence acquisition | | `pixelSizeUm` | cameras only, the size of a pixel in the sample, 0 when unknown; `GetPixelSize` reads it again after a setting changed | +A driver without peripherals describes one device with an empty `deviceId`, and a request without a +`deviceId` addresses that device. With several devices, ScopeOne names them in the order of `devices`: +the label of the `ScopeOneDevice` line for one device, and `