1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

Refreshed OpenCL sample for Android.

This commit is contained in:
Alexander Smorkalov
2023-12-01 17:35:06 +03:00
parent 81656597e9
commit 7f3b6c177f
11 changed files with 131 additions and 89 deletions
@@ -1,6 +1,8 @@
#ifdef OPENCL_FOUND
#define __CL_ENABLE_EXCEPTIONS
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS /*let's give a chance for OpenCL 1.1 devices*/
#include <CL/cl.hpp>
#endif
#include <GLES2/gl2.h>
#include <EGL/egl.h>
@@ -10,7 +12,9 @@
#include <opencv2/core/ocl.hpp>
#include "common.hpp"
#include "CLprocessor.hpp"
#ifdef OPENCL_FOUND
const char oclProgB2B[] = "// clBuffer to clBuffer";
const char oclProgI2B[] = "// clImage to clBuffer";
const char oclProgI2I[] = \
@@ -33,7 +37,7 @@ const char oclProgI2I[] = \
" write_imagef(imgOut, pos, sum*10); \n" \
"} \n";
void dumpCLinfo()
static void dumpCLinfo()
{
LOGD("*** OpenCL info ***");
try
@@ -83,7 +87,7 @@ cl::CommandQueue theQueue;
cl::Program theProgB2B, theProgI2B, theProgI2I;
bool haveOpenCL = false;
extern "C" void initCL()
int initCL()
{
dumpCLinfo();
@@ -133,20 +137,24 @@ extern "C" void initCL()
catch(const cl::Error& e)
{
LOGE("cl::Error: %s (%d)", e.what(), e.err());
return 1;
}
catch(const std::exception& e)
{
LOGE("std::exception: %s", e.what());
return 2;
}
catch(...)
{
LOGE( "OpenCL info: unknown error while initializing OpenCL stuff" );
return 3;
}
LOGD("initCL completed");
}
extern "C" void closeCL()
{
if (haveOpenCL)
return 0;
else
return 4;
}
#define GL_TEXTURE_2D 0x0DE1
@@ -230,6 +238,16 @@ void procOCL_OCV(int texIn, int texOut, int w, int h)
cv::ocl::finish();
LOGD("uploading results to texture costs %d ms", getTimeInterval(t));
}
#else
int initCL()
{
return 5;
}
#endif
void closeCL()
{
}
void drawFrameProcCPU(int w, int h, int texOut)
{
@@ -263,7 +281,7 @@ void drawFrameProcCPU(int w, int h, int texOut)
enum ProcMode {PROC_MODE_NO_PROC=0, PROC_MODE_CPU=1, PROC_MODE_OCL_DIRECT=2, PROC_MODE_OCL_OCV=3};
extern "C" void processFrame(int tex1, int tex2, int w, int h, int mode)
void processFrame(int tex1, int tex2, int w, int h, int mode)
{
switch(mode)
{
@@ -271,12 +289,14 @@ extern "C" void processFrame(int tex1, int tex2, int w, int h, int mode)
case PROC_MODE_CPU:
drawFrameProcCPU(w, h, tex2);
break;
#ifdef OPENCL_FOUND
case PROC_MODE_OCL_DIRECT:
procOCL_I2I(tex1, tex2, w, h);
break;
case PROC_MODE_OCL_OCV:
procOCL_OCV(tex1, tex2, w, h);
break;
#endif
default:
LOGE("Unexpected processing mode: %d", mode);
}
@@ -0,0 +1,8 @@
#ifndef __CL_PROCESSOR_HPP__
#define __CL_PROCESSOR_HPP__
int initCL();
void closeCL();
void processFrame(int tex1, int tex2, int w, int h, int mode);
#endif
@@ -4,17 +4,32 @@ set(target JNIpart)
project(${target} CXX)
if (OPENCV_FROM_SDK)
message(STATUS "Using OpenCV from local SDK")
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
else()
message(STATUS "Using OpenCV from AAR (Maven repo)")
set(ANDROID_OPENCV_COMPONENTS "OpenCV::opencv_java${OPENCV_VERSION_MAJOR}" CACHE STRING "")
endif()
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
find_package(OpenCL QUIET)
file(GLOB srcs *.cpp *.c)
file(GLOB hdrs *.hpp *.h)
include_directories("${CMAKE_CURRENT_LIST_DIR}")
add_library(${target} SHARED ${srcs} ${hdrs})
target_link_libraries(${target} ${ANDROID_OPENCV_COMPONENTS} -lGLESv2 -lEGL -lOpenCL)
target_link_libraries(${target} ${ANDROID_OPENCV_COMPONENTS} -lGLESv2 -lEGL -llog)
if(OpenCL_FOUND)
include_directories(${OpenCL_INCLUDE_DIRS})
target_link_libraries(${OpenCL_LIBRARIES})
add_definitions("-DOPENCL_FOUND")
elseif(DEFINED ANDROID_OPENCL_SDK)
include_directories(${ANDROID_OPENCL_SDK}/include)
link_directories(${ANDROID_OPENCL_SDK}/lib/${ANDROID_NDK_ABI_NAME})
target_link_libraries(-lOpenCL)
add_definitions("-DOPENCL_FOUND")
endif()
@@ -1,20 +0,0 @@
#include <jni.h>
int initCL();
void closeCL();
void processFrame(int tex1, int tex2, int w, int h, int mode);
JNIEXPORT jint JNICALL Java_org_opencv_samples_tutorial4_NativePart_initCL(JNIEnv * env, jclass cls)
{
return initCL();
}
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_closeCL(JNIEnv * env, jclass cls)
{
closeCL();
}
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_processFrame(JNIEnv * env, jclass cls, jint tex1, jint tex2, jint w, jint h, jint mode)
{
processFrame(tex1, tex2, w, h, mode);
}
@@ -0,0 +1,36 @@
#include <jni.h>
#include "CLprocessor.hpp"
extern "C" {
JNIEXPORT jboolean JNICALL Java_org_opencv_samples_tutorial4_NativePart_builtWithOpenCL(JNIEnv * env, jclass cls);
JNIEXPORT jint JNICALL Java_org_opencv_samples_tutorial4_NativePart_initCL(JNIEnv * env, jclass cls);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_closeCL(JNIEnv * env, jclass cls);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_processFrame(JNIEnv * env, jclass cls, jint tex1, jint tex2, jint w, jint h, jint mode);
JNIEXPORT jboolean JNICALL Java_org_opencv_samples_tutorial4_NativePart_builtWithOpenCL(JNIEnv * env, jclass cls)
{
#ifdef OPENCL_FOUND
return JNI_TRUE;
#else
return JNI_FALSE;
#endif
}
JNIEXPORT jint JNICALL Java_org_opencv_samples_tutorial4_NativePart_initCL(JNIEnv * env, jclass cls)
{
return initCL();
}
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_closeCL(JNIEnv * env, jclass cls)
{
closeCL();
}
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_processFrame(JNIEnv * env, jclass cls, jint tex1, jint tex2, jint w, jint h, jint mode)
{
processFrame(tex1, tex2, w, h, mode);
}
} // extern "C"