Add skeleton code for OpenCL handling

This commit is contained in:
Stephen Seo 2021-11-15 14:40:11 +09:00
parent 19e4dfcb1c
commit e07691f71d
3 changed files with 101 additions and 1 deletions

View 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)

43
src/opencl_handle.cc Normal file
View file

@ -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();
}
}
}

55
src/opencl_handle.h Normal file
View file

@ -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