]> git.seodisparate.com - EN605.617.81.FA21_StephenSeo_DitheringProject/commitdiff
Add skeleton code for OpenCL handling
authorStephen Seo <seo.disparate@gmail.com>
Mon, 15 Nov 2021 05:40:11 +0000 (14:40 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 15 Nov 2021 05:40:11 +0000 (14:40 +0900)
CMakeLists.txt
src/opencl_handle.cc [new file with mode: 0644]
src/opencl_handle.h [new file with mode: 0644]

index b850316f58c429cf772eef686d1abda6db44372c..e70fcd7b09f4fd33d217c7458405b85aef8c70d0 100644 (file)
@@ -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 (file)
index 0000000..d8303c2
--- /dev/null
@@ -0,0 +1,43 @@
+#include "opencl_handle.h"
+
+std::unique_ptr<OpenCLContext> 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<OpenCLContext>(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<OpenCLHandle>(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 (file)
index 0000000..c0584c6
--- /dev/null
@@ -0,0 +1,55 @@
+#ifndef IGPUP_DITHERING_PROJECT_OPENCL_H_
+#define IGPUP_DITHERING_PROJECT_OPENCL_H_
+
+#include <memory>
+
+class OpenCLHandle {
+ public:
+  typedef std::shared_ptr<OpenCLHandle> Ptr;
+  typedef std::weak_ptr<OpenCLHandle> 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<OpenCLContext> instance_;
+  OpenCLHandle::WeakPtr weak_handle_;
+
+  static void CheckRefCount();
+};
+
+#endif