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")
add_executable(DitheringProject
${Project_SOURCES})
+#target_compile_features(DitheringProject PUBLIC cxx_std_11)
find_package(PNG REQUIRED)
--- /dev/null
+#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();
+ }
+ }
+}
--- /dev/null
+#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