1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge pull request #24715 from AleksandrPanov:update_android_opencl_sample

Update Android OpenCL sample #24715

Update Android OpenCL sample and tutorial text.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Alexander Panov
2023-12-20 16:23:00 +03:00
committed by GitHub
parent 3d9cb5329c
commit 9434c89ba0
7 changed files with 95 additions and 191 deletions
@@ -14,11 +14,13 @@ android {
cmake {
if (gradle.opencv_source == "sdk_path") {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@",
"-DOPENCV_FROM_SDK=TRUE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
"-DOPENCV_FROM_SDK=TRUE",
"-DANDROID_OPENCL_SDK=@ANDROID_OPENCL_SDK@" @OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
} else {
arguments "-DOPENCV_VERSION_MAJOR=@OPENCV_VERSION_MAJOR@",
"-DOPENCV_FROM_SDK=FALSE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
"-DOPENCV_FROM_SDK=FALSE",
"-DANDROID_OPENCL_SDK=@ANDROID_OPENCL_SDK@" @OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
}
targets "JNIpart"
}
@@ -1,7 +1,7 @@
#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>
#include <CL/opencl.hpp>
#endif
#include <GLES2/gl2.h>
@@ -87,10 +87,11 @@ cl::CommandQueue theQueue;
cl::Program theProgB2B, theProgI2B, theProgI2I;
bool haveOpenCL = false;
//![init_opencl]
int initCL()
{
dumpCLinfo();
LOGE("initCL: start initCL");
EGLDisplay mEglDisplay = eglGetCurrentDisplay();
if (mEglDisplay == EGL_NO_DISPLAY)
LOGE("initCL: eglGetCurrentDisplay() returned 'EGL_NO_DISPLAY', error = %x", eglGetError());
@@ -156,6 +157,7 @@ int initCL()
else
return 4;
}
//![init_opencl]
#define GL_TEXTURE_2D 0x0DE1
void procOCL_I2I(int texIn, int texOut, int w, int h)
@@ -168,6 +170,7 @@ void procOCL_I2I(int texIn, int texOut, int w, int h)
}
LOGD("procOCL_I2I(%d, %d, %d, %d)", texIn, texOut, w, h);
//![process_pure_opencl]
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texIn);
cl::ImageGL imgOut(theContext, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, texOut);
std::vector < cl::Memory > images;
@@ -195,6 +198,7 @@ void procOCL_I2I(int texIn, int texOut, int w, int h)
theQueue.enqueueReleaseGLObjects(&images);
theQueue.finish();
LOGD("enqueueReleaseGLObjects() costs %d ms", getTimeInterval(t));
//![process_pure_opencl]
}
void procOCL_OCV(int texIn, int texOut, int w, int h)
@@ -206,6 +210,7 @@ void procOCL_OCV(int texIn, int texOut, int w, int h)
return;
}
//![process_tapi]
int64_t t = getTimeMs();
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texIn);
std::vector < cl::Memory > images(1, imgIn);
@@ -232,11 +237,12 @@ void procOCL_OCV(int texIn, int texOut, int w, int h)
cl_command_queue q = (cl_command_queue)cv::ocl::Queue::getDefault().ptr();
size_t offset = 0;
size_t origin[3] = { 0, 0, 0 };
size_t region[3] = { w, h, 1 };
size_t region[3] = { (size_t)w, (size_t)h, 1 };
CV_Assert(clEnqueueCopyBufferToImage (q, clBuffer, imgOut(), offset, origin, region, 0, NULL, NULL) == CL_SUCCESS);
theQueue.enqueueReleaseGLObjects(&images);
cv::ocl::finish();
LOGD("uploading results to texture costs %d ms", getTimeInterval(t));
//![process_tapi]
}
#else
int initCL()
@@ -25,11 +25,18 @@ target_link_libraries(${target} ${ANDROID_OPENCV_COMPONENTS} -lGLESv2 -lEGL -llo
if(OpenCL_FOUND)
include_directories(${OpenCL_INCLUDE_DIRS})
target_link_libraries(${OpenCL_LIBRARIES})
target_link_libraries(${target} ${OpenCL_LIBRARIES})
add_definitions("-DOPENCL_FOUND")
elseif(DEFINED ANDROID_OPENCL_SDK)
elseif(NOT ("${ANDROID_OPENCL_SDK}" STREQUAL ""))
include_directories(${ANDROID_OPENCL_SDK}/include)
link_directories(${ANDROID_OPENCL_SDK}/lib/${ANDROID_NDK_ABI_NAME})
target_link_libraries(-lOpenCL)
link_directories(${ANDROID_OPENCL_SDK}/lib)
target_link_directories(${target} PRIVATE ${ANDROID_OPENCL_SDK}/lib)
set_target_properties(${target} PROPERTIES LINK_FLAGS "-Wl,--allow-shlib-undefined")
target_link_libraries(${target} -lOpenCL)
add_definitions("-DOPENCL_FOUND")
add_definitions("-DCL_HPP_MINIMUM_OPENCL_VERSION=120")
add_definitions("-DCL_HPP_TARGET_OPENCL_VERSION=120")
add_definitions("-DCL_HPP_ENABLE_PROGRAM_CONSTRUCTION_FROM_ARRAY_COMPATIBILITY")
endif()
@@ -13,6 +13,7 @@ import android.view.SurfaceHolder;
import android.widget.TextView;
import android.widget.Toast;
//![minimal_surface_view]
public class MyGLSurfaceView extends CameraGLSurfaceView implements CameraGLSurfaceView.CameraTextureListener {
static final String LOGTAG = "MyGLSurfaceView";
@@ -111,3 +112,4 @@ public class MyGLSurfaceView extends CameraGLSurfaceView implements CameraGLSurf
return true;
}
}
//![minimal_surface_view]
@@ -1,5 +1,6 @@
package org.opencv.samples.tutorial4;
//![native_part]
public class NativePart {
static
{
@@ -17,3 +18,4 @@ public class NativePart {
public static native void closeCL();
public static native void processFrame(int tex1, int tex2, int w, int h, int mode);
}
//![native_part]