From e07691f71d434f9355bcda223b45582c948f8cc2 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 15 Nov 2021 14:40:11 +0900 Subject: [PATCH] Add skeleton code for OpenCL handling --- CMakeLists.txt | 4 +++- src/opencl_handle.cc | 43 ++++++++++++++++++++++++++++++++++ src/opencl_handle.h | 55 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/opencl_handle.cc create mode 100644 src/opencl_handle.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b850316..e70fcd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,10 @@ project(EN605.617.81.FA21_StephenSeo_DitheringProject) set(Project_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cc ${CMAKE_CURRENT_SOURCE_DIR}/src/image.cc + ${CMAKE_CURRENT_SOURCE_DIR}/src/opencl_handle.cc ) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wpedantic") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG") @@ -17,6 +18,7 @@ endif() add_executable(DitheringProject ${Project_SOURCES}) +#target_compile_features(DitheringProject PUBLIC cxx_std_11) find_package(PNG REQUIRED) diff --git a/src/opencl_handle.cc b/src/opencl_handle.cc new file mode 100644 index 0000000..d8303c2 --- /dev/null +++ b/src/opencl_handle.cc @@ -0,0 +1,43 @@ +#include "opencl_handle.h" + +std::unique_ptr OpenCLContext::instance_ = {}; + +OpenCLHandle::OpenCLHandle() { + // TODO +} + +OpenCLHandle::~OpenCLHandle() { OpenCLContext::CheckRefCount(); } + +OpenCLContext::OpenCLContext() { + // TODO +} + +OpenCLContext::~OpenCLContext() { + // TODO +} + +OpenCLHandle::Ptr OpenCLContext::GetHandle() { + if (!instance_) { + // cannot use make_unique due to private constructor + instance_ = std::unique_ptr(new OpenCLContext()); + } + + auto strong_handle = instance_->weak_handle_.lock(); + if (strong_handle) { + return strong_handle; + } + // cannot use make_shared due to private constructor + strong_handle = std::shared_ptr(new OpenCLHandle()); + instance_->weak_handle_ = strong_handle; + + return strong_handle; +} + +void OpenCLContext::CheckRefCount() { + if (instance_) { + if (instance_->weak_handle_.use_count() <= 1) { + // Last shared_ptr is destructing, cleanup context by calling destructor + instance_.reset(); + } + } +} diff --git a/src/opencl_handle.h b/src/opencl_handle.h new file mode 100644 index 0000000..c0584c6 --- /dev/null +++ b/src/opencl_handle.h @@ -0,0 +1,55 @@ +#ifndef IGPUP_DITHERING_PROJECT_OPENCL_H_ +#define IGPUP_DITHERING_PROJECT_OPENCL_H_ + +#include + +class OpenCLHandle { + public: + typedef std::shared_ptr Ptr; + typedef std::weak_ptr WeakPtr; + + ~OpenCLHandle(); + + // no copy + OpenCLHandle(const OpenCLHandle &other) = delete; + OpenCLHandle &operator=(const OpenCLHandle &other) = delete; + + // allow move + OpenCLHandle(OpenCLHandle &&other) = default; + OpenCLHandle &operator=(OpenCLHandle &&other) = default; + + // TODO add functions here that allow creating/deleting/using kernel function + // programs + + private: + friend class OpenCLContext; + + OpenCLHandle(); +}; + +class OpenCLContext { + public: + ~OpenCLContext(); + + // no copy + OpenCLContext(const OpenCLContext &other) = delete; + OpenCLContext &operator=(const OpenCLContext &other) = delete; + + // no move + OpenCLContext(OpenCLContext &&other) = delete; + OpenCLContext &operator=(OpenCLContext &&other) = delete; + + OpenCLHandle::Ptr GetHandle(); + + private: + friend class OpenCLHandle; + + OpenCLContext(); + + static std::unique_ptr instance_; + OpenCLHandle::WeakPtr weak_handle_; + + static void CheckRefCount(); +}; + +#endif