Skip to content

Commit

Permalink
Enable additional plugin - cucim.kit.cumed
Browse files Browse the repository at this point in the history
  • Loading branch information
gigony committed Oct 13, 2021
1 parent 5e302bb commit 95e4483
Show file tree
Hide file tree
Showing 19 changed files with 384 additions and 50 deletions.
4 changes: 4 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ add_library(${CUCIM_PACKAGE_NAME}
include/cucim/macros/defines.h
include/cucim/memory/dlpack.h
include/cucim/memory/memory_manager.h
include/cucim/plugin/image_format.h
include/cucim/plugin/plugin_config.h
include/cucim/util/cuda.h
include/cucim/util/file.h
include/cucim/util/platform.h
Expand Down Expand Up @@ -96,6 +98,8 @@ add_library(${CUCIM_PACKAGE_NAME}
src/logger/logger.cpp
src/logger/timer.cpp
src/memory/memory_manager.cu
src/plugin/image_format.cpp
src/plugin/plugin_config.cpp
src/util/file.cpp
src/util/platform.cpp)

Expand Down
3 changes: 3 additions & 0 deletions cpp/include/cucim/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "cucim/macros/api_header.h"
#include "cucim/cache/cache_type.h"
#include "cucim/cache/image_cache_config.h"
#include "cucim/plugin/plugin_config.h"

#include <string>
#include <string_view>
Expand All @@ -38,6 +39,7 @@ class EXPORT_VISIBLE Config
Config();

cucim::cache::ImageCacheConfig& cache();
cucim::plugin::PluginConfig& plugin();

std::string shm_name() const;
pid_t pid() const;
Expand All @@ -52,6 +54,7 @@ class EXPORT_VISIBLE Config
std::string source_path_;

cucim::cache::ImageCacheConfig cache_;
cucim::plugin::PluginConfig plugin_;
};

} // namespace cucim::config
Expand Down
5 changes: 3 additions & 2 deletions cpp/include/cucim/core/framework.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,7 +63,7 @@ struct PluginLoadingDesc

struct Framework
{
void load_plugins(const PluginLoadingDesc& desc = PluginLoadingDesc::get_default());
// void load_plugins(const PluginLoadingDesc& desc = PluginLoadingDesc::get_default());
bool(CUCIM_ABI* register_plugin)(const char* client_name, const PluginRegistrationDesc& desc);
void*(CUCIM_ABI* acquire_interface_from_library_with_client)(const char* client_name,
InterfaceDesc desc,
Expand All @@ -74,6 +74,7 @@ struct Framework
T* acquire_interface_from_library(const char* library_path);

// cuCIM-specific methods
void(CUCIM_ABI* load_plugin)(const char* library_path);
const char*(CUCIM_ABI* get_plugin_root)();
void(CUCIM_ABI* set_plugin_root)(const char* path);
};
Expand Down
6 changes: 4 additions & 2 deletions cpp/include/cucim/cuimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "cucim/io/device.h"
#include "cucim/io/format/image_format.h"
#include "cucim/memory/dlpack.h"
#include "cucim/plugin/image_format.h"

#include <array>
#include <set>
Expand Down Expand Up @@ -106,7 +107,7 @@ class EXPORT_VISIBLE CuImage : public std::enable_shared_from_this<CuImage>

operator bool() const
{
return !!image_formats_ && !is_loaded_;
return !!image_format_ && !is_loaded_;
}

static Framework* get_framework();
Expand Down Expand Up @@ -181,9 +182,10 @@ class EXPORT_VISIBLE CuImage : public std::enable_shared_from_this<CuImage>
// Note: config_ should be placed before cache_manager_ (cache_manager_ depends on config_)
static std::unique_ptr<config::Config> config_;
static std::unique_ptr<cache::ImageCacheManager> cache_manager_;
static std::unique_ptr<cucim::plugin::ImageFormat> image_format_plugins_;

mutable Mutex mutex_;
cucim::io::format::IImageFormat* image_formats_ = nullptr;
cucim::io::format::ImageFormatDesc* image_format_ = nullptr;
CuCIMFileHandle file_handle_{};
io::format::ImageMetadataDesc* image_metadata_ = nullptr;
io::format::ImageDataDesc* image_data_ = nullptr;
Expand Down
3 changes: 2 additions & 1 deletion cpp/include/cucim/io/format/image_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ struct ImageCheckerDesc
* Returns true if the given file is valid for the format
* @param file_name
* @param buf
* @param size
* @return
*/
bool(CUCIM_ABI* is_valid)(const char* file_name, const char* buf);
bool(CUCIM_ABI* is_valid)(const char* file_name, const char* buf, size_t size);
};

struct ImageParserDesc
Expand Down
47 changes: 47 additions & 0 deletions cpp/include/cucim/plugin/image_format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CUCIM_PLUGIN_IMAGE_FORMAT_H
#define CUCIM_PLUGIN_IMAGE_FORMAT_H

#include "cucim/filesystem/file_path.h"
#include "cucim/io/format/image_format.h"


namespace cucim::plugin
{

class ImageFormat
{
public:
ImageFormat() = default;
~ImageFormat() = default;

bool add_interfaces(cucim::io::format::IImageFormat* image_formats);
cucim::io::format::ImageFormatDesc* detect_image_format(const filesystem::Path& path);

operator bool() const
{
return !image_formats_.empty();
}

private:
std::vector<cucim::io::format::ImageFormatDesc*> image_formats_;
};

} // namespace cucim::plugin

#endif // CUCIM_PLUGIN_IMAGE_FORMAT_H
44 changes: 44 additions & 0 deletions cpp/include/cucim/plugin/plugin_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CUCIM_PLUGIN_PLUGIN_CONFIG_H
#define CUCIM_PLUGIN_PLUGIN_CONFIG_H

#include "cucim/core/framework.h"

#include <string>
#include <vector>

namespace cucim::plugin
{

#define XSTR(x) STR(x)
#define STR(x) #x

struct EXPORT_VISIBLE PluginConfig
{
void load_config(const void* json_obj);

std::vector<std::string> plugin_names{ std::string("cucim.kit.cuslide@" XSTR(CUCIM_VERSION) ".so"),
std::string("cucim.kit.cumed@" XSTR(CUCIM_VERSION) ".so") };
};

#undef STR
#undef XSTR

} // namespace cucim::plugin

#endif // CUCIM_PLUGIN_PLUGIN_CONFIG_H
18 changes: 12 additions & 6 deletions cpp/plugins/cucim.kit.cuslide/src/cuslide/cuslide.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
#include <cassert>
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#include <memory>

using json = nlohmann::json;
Expand Down Expand Up @@ -68,12 +69,17 @@ static const char* get_format_name()
return "Generic TIFF";
}

static bool CUCIM_ABI checker_is_valid(const char* file_name, const char* buf) // TODO: need buffer size parameter
static bool CUCIM_ABI checker_is_valid(const char* file_name, const char* buf, size_t size)
{
// TODO implement this
(void)file_name;
(void)buf;
return true;
(void)size;
auto file = std::filesystem::path(file_name);
auto extension = file.extension().string();
if (extension.compare(".tif") == 0 || extension.compare(".tiff") == 0)
{
return true;
}
return false;
}

static CuCIMFileHandle CUCIM_ABI parser_open(const char* file_path)
Expand Down Expand Up @@ -277,7 +283,7 @@ static bool CUCIM_ABI writer_write(const CuCIMFileHandle* handle,

void fill_interface(cucim::io::format::IImageFormat& iface)
{
static cucim::io::format::ImageCheckerDesc image_checker = { 0, 80, checker_is_valid };
static cucim::io::format::ImageCheckerDesc image_checker = { 0, 0, checker_is_valid };
static cucim::io::format::ImageParserDesc image_parser = { parser_open, parser_parse, parser_close };

static cucim::io::format::ImageReaderDesc image_reader = { reader_read };
Expand Down
5 changes: 5 additions & 0 deletions cpp/plugins/cucim.kit.cuslide/src/cuslide/tiff/tiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ TIFF::TIFF(const cucim::filesystem::Path& file_path, int mode) : file_path_(file
throw std::invalid_argument(fmt::format("Cannot open {}!", file_path));
}
tiff_client_ = ::TIFFFdOpen(fd, file_path_cstr, "rm"); // Add 'm' to disable memory-mapped file
if (tiff_client_ == nullptr)
{
cucim_free(file_path_cstr);
throw std::invalid_argument(fmt::format("Cannot load {}!", file_path));
}
// TODO: make file_handle_ object to pointer
file_handle_ = CuCIMFileHandle{ fd, nullptr, FileHandleType::kPosix, file_path_cstr, this };

Expand Down
12 changes: 12 additions & 0 deletions cpp/src/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ cucim::cache::ImageCacheConfig& Config::cache()
return cache_;
}

cucim::plugin::PluginConfig& Config::plugin()
{
return plugin_;
}

std::string Config::shm_name() const
{
return fmt::format("cucim-shm.{}", pgid());
Expand Down Expand Up @@ -113,11 +118,18 @@ bool Config::parse_config(std::string& path)
{
std::ifstream ifs(path);
json obj = json::parse(ifs, nullptr /*cb*/, true /*allow_exceptions*/, true /*ignore_comments*/);

json cache = obj["cache"];
if (cache.is_object())
{
cache_.load_config(&cache);
}

json plugin = obj["plugin"];
if (plugin.is_object())
{
plugin_.load_config(&plugin);
}
}
catch (const json::parse_error& e)
{
Expand Down
38 changes: 37 additions & 1 deletion cpp/src/core/cucim_framework.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -131,6 +131,25 @@ bool CuCIMFramework::register_plugin(const std::shared_ptr<Plugin>& plugin)
return true;
}

size_t CuCIMFramework::get_plugin_count() const
{
ScopedLock g(mutex_);
return plugin_manager_.get_plugin_indices().size();
}

void CuCIMFramework::get_plugins(PluginDesc* out_plugins) const
{
ScopedLock g(mutex_);
const std::unordered_set<size_t>& plugins = plugin_manager_.get_plugin_indices();
size_t i = 0;
for (const auto& plugin_index : plugins)
{
if (out_plugins)
{
out_plugins[i++] = plugin_manager_.get_plugin(plugin_index)->get_plugin_desc();
}
}
}

size_t CuCIMFramework::get_plugin_index(const char* name) const
{
Expand Down Expand Up @@ -603,6 +622,23 @@ bool CuCIMFramework::register_plugin(const std::string& file_path, bool reloadab
}

// cuCIM-specific methods

void CuCIMFramework::load_plugin(const char* library_path)
{

ScopedLock g(mutex_);

const std::string canonical_library_path(library_path);

Plugin* plugin = get_plugin_by_library_path(canonical_library_path);
// Check if plugin with this library path was already loaded
if (!plugin)
{
// It was not loaded, try to register such plugin and get it again
register_plugin(canonical_library_path);
}
}

std::string& CuCIMFramework::get_plugin_root()
{
return plugin_root_path_;
Expand Down
5 changes: 4 additions & 1 deletion cpp/src/core/cucim_framework.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,12 +58,15 @@ class CuCIMFramework
const InterfaceDesc& desc,
const char* library_path,
bool optional = false);
size_t get_plugin_count() const;
void get_plugins(PluginDesc* out_plugins) const;
size_t get_plugin_index(const char* name) const;
Plugin* get_plugin(size_t index) const;
Plugin* get_plugin(const char* name) const;
Plugin* get_plugin_by_library_path(const std::string& library_path);

// cuCIM-specific methods;
void load_plugin(const char* library_path);
std::string& get_plugin_root();
void set_plugin_root(const char* path);

Expand Down
Loading

0 comments on commit 95e4483

Please sign in to comment.