大家可以通过这个链接下载头文件:
http://download.csdn.net/detail/wjskeepmaking/9811058
public class OpenclTest {
public String testOpencl(){
return testopencl();
}
public String getplatformName(){
return getPlatformName();
}
public String getdeviceName(){
return getDeviceName();
}
public native String testopencl();
public native String getPlatformName();
public native String getDeviceName();
}
进入package的同级目录,利用javah命令生成头文件:
//
// Created by wujs on 2017/4/11.
//
#include "CL/cl.h"
#include
#include
#include
#include
#include "topencl.h"
#include"com_pax_imagesobelfilter_OpenclTest.h"
#include
#define LOG_TAG "test"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#define LEN(arr) sizeof(arr) / sizeof(arr[0])
#define N 1024
#define NUM_THREAD 128
cl_uint num_device;
cl_uint num_platform;
cl_platform_id *platform;
cl_device_id *devices;
cl_int err;
cl_context context;
cl_command_queue cmdQueue;
cl_mem buffer,sum_buffer;
cl_program program ;
cl_kernel kernel;
const char* src[] = {
" __kernel void redution( \n"
" __global int *data, \n"
" __global int *output, \n"
" __local int *data_local \n"
" ) \n"
" { \n"
" int gid=get_group_id(0); \n"
" int tid=get_global_id(0); \n"
" int size=get_local_size(0); \n"
" int id=get_local_id(0); \n"
" data_local[id]=data[tid]; \n"
" barrier(CLK_LOCAL_MEM_FENCE); \n"
" for(int i=size/2;i>0;i>>=1){ \n"
" if(idNewStringUTF(result);
}
JNIEXPORT jstring JNICALL Java_com_pax_imagesobelfilter_OpenclTest_getPlatformName(JNIEnv *env , jobject thisobject)
{
char buffer[1024];
LOGD("getPlatformName start");
clGetPlatformInfo(platform[0],CL_PLATFORM_NAME,sizeof(buffer),buffer,NULL);
return env->NewStringUTF(buffer);
}
JNIEXPORT jstring JNICALL Java_com_pax_imagesobelfilter_OpenclTest_getDeviceName(JNIEnv *env , jobject thisobject)
{
char buffer[1024];
LOGD("getDeviceName start");
clGetDeviceInfo(devices[0],CL_DEVICE_NAME,sizeof(buffer),buffer,NULL);
return env->NewStringUTF(buffer);
}
void Init_OpenCL()
{
LOGD("start init OpenCL");
size_t nameLen1;
char platformName[1024];
err = clGetPlatformIDs(0, 0, &num_platform);
LOGD("err=clGetPlatformIDs");
platform=(cl_platform_id*)malloc(sizeof(cl_platform_id)*num_platform);
err = clGetPlatformIDs(num_platform, platform, NULL);
err=clGetDeviceIDs(platform[0],CL_DEVICE_TYPE_GPU,0,NULL,&num_device);
devices=(cl_device_id*)malloc(sizeof(cl_device_id)*num_device);
err=clGetDeviceIDs(platform[0],CL_DEVICE_TYPE_GPU,num_device,devices,NULL);
}
void Context_cmd()
{
context=clCreateContext(NULL,num_device,devices,NULL,NULL,&err);
cmdQueue=clCreateCommandQueue(context,devices[0],0,&err);
}
void Create_Buffer(int *data)
{
buffer=clCreateBuffer(context,CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,sizeof(int)*N,data,&err);
sum_buffer=clCreateBuffer(context,CL_MEM_WRITE_ONLY,sizeof(int)*num_block,0,&err);
}
void Create_program()
{
program=clCreateProgramWithSource(context, LEN(src), src, NULL, NULL);
err=clBuildProgram(program,num_device,devices,NULL,NULL,NULL);
kernel = clCreateKernel(program, "redution", NULL);
}
void Set_arg()
{
err=clSetKernelArg(kernel,0,sizeof(cl_mem),&buffer);
err=clSetKernelArg(kernel,1,sizeof(cl_mem),&sum_buffer);
err=clSetKernelArg(kernel,2,sizeof(int)*NUM_THREAD,NULL);
}
void Execution()
{
const size_t globalWorkSize[1]={N};
const size_t localWorkSize[1]={NUM_THREAD};
err=clEnqueueNDRangeKernel(cmdQueue,kernel,1,NULL,globalWorkSize,localWorkSize,0,NULL,NULL);
clFinish(cmdQueue);
}
void CopyOutResult(int*out)
{
err=clEnqueueReadBuffer(cmdQueue,sum_buffer,CL_TRUE,0,sizeof(int)*num_block,out,0,NULL,NULL);
}
下面是topencl.c的库加载代码:
#define LIB_OPENCL "/system/vendor/lib/libOpenCL.so"
#define LIB_GLES_MALI "/system/vendor/lib/egl/libGLES_adreno.so"
#define LIB_LLVM "/system/lib/libllvm-a3xx.so"
static void *getCLHandle(){
LOGD("get_handle");
void *res = NULL;
char* so_name="Unknown Shared library for OpenCL";
res = dlopen(LIB_OPENCL,RTLD_LAZY);
if(res != NULL){
so_name = LIB_OPENCL;
}else{
res = dlopen(LIB_GLES_MALI,RTLD_LAZY);
}
if(res != NULL){
so_name = LIB_GLES_MALI;
}else{
res = dlopen(LIB_LLVM,RTLD_LAZY);
}
/* if(res==NULL){
res = dlopen("/home/rahul/stream/sdk2.7/lib/x86_64/libOpenCL.so",RTLD_LAZY);
}*/
if(res != NULL) {
so_name=LIB_LLVM;
} else {
LOGD("Could not open library :(\n");
}
LOGD("Loaded library name:%s\n",so_name);
return res;
}
要根据自己的Opencv4Android的路径去修改OPENCV_ANDROID_SDK
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_ANDROID_SDK := F:\Android\opencv-3.2.0-android-sdk\OpenCV-android-sdk
ifdef OPENCV_ANDROID_SDK
ifneq ("","$(wildcard $(OPENCV_ANDROID_SDK)/OpenCV.mk)")
include ${OPENCV_ANDROID_SDK}/OpenCV.mk
else
include ${OPENCV_ANDROID_SDK}/sdk/native/jni/OpenCV.mk
endif
else
include ../../sdk/native/jni/OpenCV.mk
endif
LOCAL_MODULE := ocl
LOCAL_CPPFLAGS += -I$(LOCAL_PATH)
LOCAL_SRC_FILES := ocl.cpp topencl.c
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
APP_CPPFLAGS := -frtti -fexceptions
#APP_STL := stlport_static
APP_STL := gnustl_static
APP_ABI := armeabi-v7a arm64-v8a
APP_PLATFORM := android-8