1

I am trying to develop an Android app to process (in OpenCL) every frame of the camera preview. The preview is implemented with Android's GLSurfaceView and SurfaceTexture, which use OpenGL. My problem is that, from the JNI side, I don't know of a way to get hold of the OpenGL context.

I am trying to follow this excellent article, OpenCL and OpenGL Interoperability Tutorial, by Maxim Shevtsov of Intel. Maxim's article assumes that the reader has the OpenGL context already created, and that both the OpenGL context and HDC are available. That's a very good starting point for most people. However, that's where my challenges are.

The Java code that I use to create the OpenGL context is like this:

// SurfaceView
public class CamGLView extends GLSurfaceView
{
    CamGLViewRenderer mRenderer;
    CamGLView(Context context) {
        super(context);
        mRenderer = new CamGLViewRenderer(this);
        setEGLContextClientVersion(3);  //OpenGL context is created, but how to get hold of it from JNI side ?
        setRenderer((Renderer)mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
    ......
}

// Renderer
public class CamGLViewRenderer implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener
{
    @Override
    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        mSTexture = new SurfaceTexture(mTexID[0]);
        if (null == mSTexture) {
            Log.e(mTag, "Creating SurfaceTexture failed !");
            return;
        }
        mSTexture.setOnFrameAvailableListener(this);

        // Setup the camera preview
        try {
            mCamera.setPreviewTexture(mSTexture);
        } catch(IOException ioe) 
        {
            Log.e(mTag, "Camera.setPreviewTexture Error! " + ioe.getMessage());
            ioe.printStackTrace();
            return;
        }
    }
    ......
}

The Android app is up and running. The camera preview is displayed. Now on the JNI side, I want to grab each preview frame, send it to the OpenCL kernel to be processed, and then send it back to OpenGL to be displayed. To begin with, I need the OpenGL context on the JNI side.

Any ideas and thoughts to solve my problem?

Thank you very much

hubeir
  • 1,279
  • 2
  • 13
  • 24
  • Resources for investigation: http://stackoverflow.com/questions/10670953/how-do-i-get-the-raw-android-camera-buffer-in-c-using-jni http://source.android.com/devices/camera/camera.html – Morrison Chang Aug 20 '14 at 20:31
  • If you look at the SDK source code of GLSurfaceView, you'll see that the code is very bad. Improve on it and write your own GLSurfaceView. On one device I owned I had to do this, as all I got was a black screen. – user1095108 Aug 20 '14 at 22:11

2 Answers2

1

I believe that you can find what you are looking for in a similar way as the NativeActivity source demonstrates.

The NativeActivity comes with the SDK in the android-ndk/samples/NativeActivity and calls upon eglCreateContext(), but there are similar calls such as eglGetCurrentContext() associated with the EGL references available in the NDK as well. You should be able to retrieve the context in this manner through these APIs.

Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
1

In our code the context is created in Java and then control is simply passed to C via JNI.

We use eglGetCurrentContext() for CL_GL_CONTEXT_KHR property as mentioned by Jay and in addition as the CL_EGL_DISPLAY_KHR property we use eglGetCurrentDisplay(). These have worked on all Android devices we have tested.

sharpneli
  • 1,601
  • 10
  • 9
  • That's what I was speculating. It's good to have someone confirming it. clGetGLContextInfoKHR() still returns error code -64 (yes it's -64), but I think I am on the right path. Thanks! – hubeir Aug 21 '14 at 19:15
  • Have you tried passing them as is to clCreateContext in the properties list? It might work fine even though clGetGLContextInfoKHR fails. And remember that IMG GPU's only support sharing of textures via EGL image sharing, you cannot access VBO's via it. All other GPU's I'm aware of do support the GL sharing. – sharpneli Aug 22 '14 at 00:12