Ask Your Question

Revision history [back]

Unknown error code -220 on cv::ocl::attachContext

Hi guy, I am new to OpenCV and I am trying to make an Android app with OpenGL OpenCL and OpenCV.

I looked around the internet and I have found many examples but none of them works with my devices. They all stuck at the init of OpenCL. This is an example of what I mean:

void initCL( JNIEnv *env, jobject /* this */){
EGLDisplay mEglDisplay = eglGetCurrentDisplay();
    if (mEglDisplay == EGL_NO_DISPLAY)
        LOGE("initCL: eglGetCurrentDisplay() returned 'EGL_NO_DISPLAY', error = %x", eglGetError());
    else
            LOGD("initCL: eglGetCurrentDisplay() returned 'EGL_DISPLAY'");

    EGLContext mEglContext = eglGetCurrentContext();
    if (mEglContext == EGL_NO_CONTEXT)
        LOGE("initCL: eglGetCurrentContext() returned 'EGL_NO_CONTEXT', error = %x", eglGetError());
    else
            LOGD("initCL: eglGetCurrentContext() returned 'EGL_CONTEXT'");

    cl_context_properties props[] =
            {   CL_GL_CONTEXT_KHR,   (cl_context_properties) mEglContext,
                CL_EGL_DISPLAY_KHR,  (cl_context_properties) mEglDisplay,
                CL_CONTEXT_PLATFORM, 0,
                0 };

    try
    {
        haveOpenCL = false;
        cl::Platform p = cl::Platform::getDefault();
        std::string ext = p.getInfo<CL_PLATFORM_EXTENSIONS>();
        if(ext.find("cl_khr_gl_sharing") == std::string::npos)
            LOGE("Warning1: CL-GL sharing isn't supported by PLATFORM");
        props[5] = (cl_context_properties) p();

        std::vector<cl::Device> devs;
        p.getDevices(CL_DEVICE_TYPE_ALL,&devs);
        theContext = cl::Context(devs[0],props);

        LOGD("Context returned %d devices, taking the 1st one", devs.size());
        ext = devs[0].getInfo<CL_DEVICE_EXTENSIONS>();
        if(ext.find("cl_khr_gl_sharing") == std::string::npos)
            LOGE("Warning2: CL-GL sharing isn't supported by DEVICE");


        cl::Program::Sources src;
        std::string kernel_code =
                "__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST; \n" \
    "\n" \
    "__kernel void Laplacian( \n" \
    "        __read_only image2d_t imgIn, \n" \
    "        __write_only image2d_t imgOut \n" \
    "    ) { \n" \
    "  \n" \
    "    const int2 pos = {get_global_id(0), get_global_id(1)}; \n" \
    "  \n" \
    "    float4 sum = (float4) 0.0f; \n" \
    "    sum += read_imagef(imgIn, sampler, pos + (int2)(-1,0)); \n" \
    "    sum += read_imagef(imgIn, sampler, pos + (int2)(+1,0)); \n" \
    "    sum += read_imagef(imgIn, sampler, pos + (int2)(0,-1)); \n" \
    "    sum += read_imagef(imgIn, sampler, pos + (int2)(0,+1)); \n" \
    "    sum -= read_imagef(imgIn, sampler, pos) * 4; \n" \
    "  \n" \
    "    write_imagef(imgOut, pos, sum*10); \n" \
    "} \n";


        src.push_back({kernel_code.c_str(),kernel_code.length()});
        cl::Program prg(theContext,src);
        if(prg.build({devs[0]})!=CL_SUCCESS){
            LOGE("COULD NOT COMPILE PROGRAM");
        }

        cv::ocl::attachContext(p.getInfo<CL_PLATFORM_NAME>(),p(),theContext(),devs[0]());//The code blocks here

}

As I said in the comment the code blocks at cv::ocl::attachContext with this log:

E/cv::error(): OpenCV(3.4.2) Error: Unknown error code -220 (no OpenCL platform available!) in void cv::ocl::attachContext(const cv::String&, void, void, void), file /build/3_4_pack-android/opencv/modules/core/src/ocl.cpp, line 2486 E/JNIpart: std::exception: OpenCV(3.4.2) /build/3_4_pack-android/opencv/modules/core/src/ocl.cpp:2486: error: (-220:Unknown error code -220) no OpenCL platform available! in function 'void cv::ocl::attachContext(const cv::String&, void, void, void)'

Does anyone know what causes this error? How can I solve It?