Merge pull request #5261 from apavlenko:android_ocl_sample_improvements
This commit is contained in:
commit
c223c05b82
@ -1,4 +1,5 @@
|
|||||||
#define __CL_ENABLE_EXCEPTIONS
|
#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/cl.hpp>
|
||||||
|
|
||||||
#include <EGL/egl.h>
|
#include <EGL/egl.h>
|
||||||
@ -79,6 +80,7 @@ void dumpCLinfo()
|
|||||||
cl::Context theContext;
|
cl::Context theContext;
|
||||||
cl::CommandQueue theQueue;
|
cl::CommandQueue theQueue;
|
||||||
cl::Program theProgB2B, theProgI2B, theProgI2I;
|
cl::Program theProgB2B, theProgI2B, theProgI2I;
|
||||||
|
bool haveOpenCL = false;
|
||||||
|
|
||||||
void initCL()
|
void initCL()
|
||||||
{
|
{
|
||||||
@ -100,6 +102,7 @@ void initCL()
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
haveOpenCL = false;
|
||||||
cl::Platform p = cl::Platform::getDefault();
|
cl::Platform p = cl::Platform::getDefault();
|
||||||
std::string ext = p.getInfo<CL_PLATFORM_EXTENSIONS>();
|
std::string ext = p.getInfo<CL_PLATFORM_EXTENSIONS>();
|
||||||
if(ext.find("cl_khr_gl_sharing") == std::string::npos)
|
if(ext.find("cl_khr_gl_sharing") == std::string::npos)
|
||||||
@ -124,6 +127,7 @@ void initCL()
|
|||||||
LOGD("OpenCV+OpenCL works OK!");
|
LOGD("OpenCV+OpenCL works OK!");
|
||||||
else
|
else
|
||||||
LOGE("Can't init OpenCV with OpenCL TAPI");
|
LOGE("Can't init OpenCV with OpenCL TAPI");
|
||||||
|
haveOpenCL = true;
|
||||||
}
|
}
|
||||||
catch(cl::Error& e)
|
catch(cl::Error& e)
|
||||||
{
|
{
|
||||||
@ -147,6 +151,8 @@ void closeCL()
|
|||||||
#define GL_TEXTURE_2D 0x0DE1
|
#define GL_TEXTURE_2D 0x0DE1
|
||||||
void procOCL_I2I(int texIn, int texOut, int w, int h)
|
void procOCL_I2I(int texIn, int texOut, int w, int h)
|
||||||
{
|
{
|
||||||
|
if(!haveOpenCL) return;
|
||||||
|
|
||||||
LOGD("procOCL_I2I(%d, %d, %d, %d)", texIn, texOut, w, h);
|
LOGD("procOCL_I2I(%d, %d, %d, %d)", texIn, texOut, w, h);
|
||||||
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texIn);
|
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);
|
cl::ImageGL imgOut(theContext, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, texOut);
|
||||||
@ -177,10 +183,12 @@ void procOCL_I2I(int texIn, int texOut, int w, int h)
|
|||||||
LOGD("enqueueReleaseGLObjects() costs %d ms", getTimeInterval(t));
|
LOGD("enqueueReleaseGLObjects() costs %d ms", getTimeInterval(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
void procOCL_OCV(int tex, int w, int h)
|
void procOCL_OCV(int texIn, int texOut, int w, int h)
|
||||||
{
|
{
|
||||||
|
if(!haveOpenCL) return;
|
||||||
|
|
||||||
int64_t t = getTimeMs();
|
int64_t t = getTimeMs();
|
||||||
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, tex);
|
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texIn);
|
||||||
std::vector < cl::Memory > images(1, imgIn);
|
std::vector < cl::Memory > images(1, imgIn);
|
||||||
theQueue.enqueueAcquireGLObjects(&images);
|
theQueue.enqueueAcquireGLObjects(&images);
|
||||||
theQueue.finish();
|
theQueue.finish();
|
||||||
@ -197,7 +205,7 @@ void procOCL_OCV(int tex, int w, int h)
|
|||||||
LOGD("OpenCV processing costs %d ms", getTimeInterval(t));
|
LOGD("OpenCV processing costs %d ms", getTimeInterval(t));
|
||||||
|
|
||||||
t = getTimeMs();
|
t = getTimeMs();
|
||||||
cl::ImageGL imgOut(theContext, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, tex);
|
cl::ImageGL imgOut(theContext, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, texOut);
|
||||||
images.clear();
|
images.clear();
|
||||||
images.push_back(imgOut);
|
images.push_back(imgOut);
|
||||||
theQueue.enqueueAcquireGLObjects(&images);
|
theQueue.enqueueAcquireGLObjects(&images);
|
||||||
|
@ -31,7 +31,7 @@ const char vss[] = \
|
|||||||
"varying vec2 texCoord;\n" \
|
"varying vec2 texCoord;\n" \
|
||||||
"void main() {\n" \
|
"void main() {\n" \
|
||||||
" texCoord = vTexCoord;\n" \
|
" texCoord = vTexCoord;\n" \
|
||||||
" gl_Position = vec4 ( vPosition, 0.0f, 1.0f );\n" \
|
" gl_Position = vec4 ( vPosition, 0.0, 1.0 );\n" \
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
const char fssOES[] = \
|
const char fssOES[] = \
|
||||||
@ -63,9 +63,9 @@ GLuint FBO = 0;
|
|||||||
GLuint texOES = 0;
|
GLuint texOES = 0;
|
||||||
int texWidth = 0, texHeight = 0;
|
int texWidth = 0, texHeight = 0;
|
||||||
|
|
||||||
enum ProcMode {PROC_MODE_CPU=1, PROC_MODE_OCL_DIRECT=2, PROC_MODE_OCL_OCV=3};
|
enum ProcMode {PROC_MODE_NO_PROC=0, PROC_MODE_CPU=1, PROC_MODE_OCL_DIRECT=2, PROC_MODE_OCL_OCV=3};
|
||||||
|
|
||||||
ProcMode procMode = PROC_MODE_CPU;
|
ProcMode procMode = PROC_MODE_NO_PROC;
|
||||||
|
|
||||||
static inline void deleteTex(GLuint* tex)
|
static inline void deleteTex(GLuint* tex)
|
||||||
{
|
{
|
||||||
@ -268,7 +268,7 @@ void drawFrameProcCPU()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void procOCL_I2I(int texIn, int texOut, int w, int h);
|
void procOCL_I2I(int texIn, int texOut, int w, int h);
|
||||||
void procOCL_OCV(int tex, int w, int h);
|
void procOCL_OCV(int texIn, int texOut, int w, int h);
|
||||||
void drawFrameProcOCL()
|
void drawFrameProcOCL()
|
||||||
{
|
{
|
||||||
drawTex(texOES, GL_TEXTURE_EXTERNAL_OES, FBO);
|
drawTex(texOES, GL_TEXTURE_EXTERNAL_OES, FBO);
|
||||||
@ -285,10 +285,10 @@ void drawFrameProcOCLOCV()
|
|||||||
drawTex(texOES, GL_TEXTURE_EXTERNAL_OES, FBO);
|
drawTex(texOES, GL_TEXTURE_EXTERNAL_OES, FBO);
|
||||||
|
|
||||||
// modify pixels in FBO texture using OpenCL and CL-GL interop
|
// modify pixels in FBO texture using OpenCL and CL-GL interop
|
||||||
procOCL_OCV(FBOtex, texWidth, texHeight);
|
procOCL_OCV(FBOtex, FBOtex2, texWidth, texHeight);
|
||||||
|
|
||||||
// render to screen
|
// render to screen
|
||||||
drawTex(FBOtex, GL_TEXTURE_2D, 0);
|
drawTex(FBOtex2, GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void drawFrame()
|
extern "C" void drawFrame()
|
||||||
@ -298,6 +298,7 @@ extern "C" void drawFrame()
|
|||||||
|
|
||||||
switch(procMode)
|
switch(procMode)
|
||||||
{
|
{
|
||||||
|
case PROC_MODE_NO_PROC: drawFrameOrig(); break;
|
||||||
case PROC_MODE_CPU: drawFrameProcCPU(); break;
|
case PROC_MODE_CPU: drawFrameProcCPU(); break;
|
||||||
case PROC_MODE_OCL_DIRECT: drawFrameProcOCL(); break;
|
case PROC_MODE_OCL_DIRECT: drawFrameProcOCL(); break;
|
||||||
case PROC_MODE_OCL_OCV: drawFrameProcOCLOCV(); break;
|
case PROC_MODE_OCL_OCV: drawFrameProcOCLOCV(); break;
|
||||||
@ -366,6 +367,7 @@ extern "C" void setProcessingMode(int mode)
|
|||||||
{
|
{
|
||||||
switch(mode)
|
switch(mode)
|
||||||
{
|
{
|
||||||
|
case PROC_MODE_NO_PROC: procMode = PROC_MODE_NO_PROC; break;
|
||||||
case PROC_MODE_CPU: procMode = PROC_MODE_CPU; break;
|
case PROC_MODE_CPU: procMode = PROC_MODE_CPU; break;
|
||||||
case PROC_MODE_OCL_DIRECT: procMode = PROC_MODE_OCL_DIRECT; break;
|
case PROC_MODE_OCL_DIRECT: procMode = PROC_MODE_OCL_DIRECT; break;
|
||||||
case PROC_MODE_OCL_OCV: procMode = PROC_MODE_OCL_OCV; break;
|
case PROC_MODE_OCL_OCV: procMode = PROC_MODE_OCL_OCV; break;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||||
<group android:checkableBehavior="single">
|
<group android:checkableBehavior="single">
|
||||||
|
<item android:id="@+id/no_proc" android:title="No processing" />
|
||||||
<item android:id="@+id/cpu" android:title="Use CPU code" />
|
<item android:id="@+id/cpu" android:title="Use CPU code" />
|
||||||
<item android:id="@+id/ocl_direct" android:title="Use OpenCL direct" />
|
<item android:id="@+id/ocl_direct" android:title="Use OpenCL direct" />
|
||||||
<item android:id="@+id/ocl_ocv" android:title="Use OpenCL via OpenCV" />
|
<item android:id="@+id/ocl_ocv" android:title="Use OpenCL via OpenCV" />
|
||||||
|
@ -4,12 +4,8 @@ import java.util.Arrays;
|
|||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.microedition.khronos.egl.EGLConfig;
|
|
||||||
import javax.microedition.khronos.opengles.GL10;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Point;
|
|
||||||
import android.graphics.SurfaceTexture;
|
import android.graphics.SurfaceTexture;
|
||||||
import android.hardware.camera2.CameraAccessException;
|
import android.hardware.camera2.CameraAccessException;
|
||||||
import android.hardware.camera2.CameraCaptureSession;
|
import android.hardware.camera2.CameraCaptureSession;
|
||||||
@ -73,7 +69,6 @@ import android.view.Surface;
|
|||||||
Math.abs(aspect - (float)w/h) < 0.2 ) {
|
Math.abs(aspect - (float)w/h) < 0.2 ) {
|
||||||
bestWidth = w;
|
bestWidth = w;
|
||||||
bestHeight = h;
|
bestHeight = h;
|
||||||
//mPreviewSize = psize;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.i(LOGTAG, "best size: "+bestWidth+"x"+bestHeight);
|
Log.i(LOGTAG, "best size: "+bestWidth+"x"+bestHeight);
|
||||||
@ -156,9 +151,9 @@ import android.view.Surface;
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisconnected(CameraDevice cameraDevice) {
|
public void onDisconnected(CameraDevice cameraDevice) {
|
||||||
//mCameraOpenCloseLock.release();
|
|
||||||
cameraDevice.close();
|
cameraDevice.close();
|
||||||
mCameraDevice = null;
|
mCameraDevice = null;
|
||||||
|
mCameraOpenCloseLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -185,6 +180,7 @@ import android.view.Surface;
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(null == mSTex) {
|
if(null == mSTex) {
|
||||||
|
mCameraOpenCloseLock.release();
|
||||||
Log.e(LOGTAG, "createCameraPreviewSession: preview SurfaceTexture is null");
|
Log.e(LOGTAG, "createCameraPreviewSession: preview SurfaceTexture is null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -192,7 +188,6 @@ import android.view.Surface;
|
|||||||
mSTex.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
|
mSTex.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
|
||||||
|
|
||||||
Surface surface = new Surface(mSTex);
|
Surface surface = new Surface(mSTex);
|
||||||
Log.d(LOGTAG, "createCameraPreviewSession: surface = " + surface);
|
|
||||||
|
|
||||||
mPreviewRequestBuilder = mCameraDevice
|
mPreviewRequestBuilder = mCameraDevice
|
||||||
.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
|
.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
|
||||||
@ -215,6 +210,7 @@ import android.view.Surface;
|
|||||||
mCaptureSession.setRepeatingRequest(
|
mCaptureSession.setRepeatingRequest(
|
||||||
mPreviewRequestBuilder.build(), null,
|
mPreviewRequestBuilder.build(), null,
|
||||||
mBackgroundHandler);
|
mBackgroundHandler);
|
||||||
|
Log.i(LOGTAG, "CameraPreviewSession has been started");
|
||||||
} catch (CameraAccessException e) {
|
} catch (CameraAccessException e) {
|
||||||
Log.e(LOGTAG, "createCaptureSession failed");
|
Log.e(LOGTAG, "createCaptureSession failed");
|
||||||
}
|
}
|
||||||
@ -227,7 +223,7 @@ import android.view.Surface;
|
|||||||
Log.e(LOGTAG, "createCameraPreviewSession failed");
|
Log.e(LOGTAG, "createCameraPreviewSession failed");
|
||||||
mCameraOpenCloseLock.release();
|
mCameraOpenCloseLock.release();
|
||||||
}
|
}
|
||||||
}, null);
|
}, mBackgroundHandler);
|
||||||
} catch (CameraAccessException e) {
|
} catch (CameraAccessException e) {
|
||||||
Log.e(LOGTAG, "createCameraPreviewSession");
|
Log.e(LOGTAG, "createCameraPreviewSession");
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@ -235,7 +231,7 @@ import android.view.Surface;
|
|||||||
"Interrupted while createCameraPreviewSession", e);
|
"Interrupted while createCameraPreviewSession", e);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
mCameraOpenCloseLock.release();
|
//mCameraOpenCloseLock.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,12 +258,15 @@ import android.view.Surface;
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setCameraPreviewSize(int width, int height) {
|
protected void setCameraPreviewSize(int width, int height) {
|
||||||
//mPreviewSize = new Size(width, height);
|
Log.i(LOGTAG, "setCameraPreviewSize("+width+"x"+height+")");
|
||||||
if( !cacPreviewSize(width, height) )
|
|
||||||
return;
|
|
||||||
try {
|
try {
|
||||||
mCameraOpenCloseLock.acquire();
|
mCameraOpenCloseLock.acquire();
|
||||||
|
if( !cacPreviewSize(width, height) ) {
|
||||||
|
mCameraOpenCloseLock.release();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (null != mCaptureSession) {
|
if (null != mCaptureSession) {
|
||||||
|
Log.d(LOGTAG, "closing existing previewSession");
|
||||||
mCaptureSession.close();
|
mCaptureSession.close();
|
||||||
mCaptureSession = null;
|
mCaptureSession = null;
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import android.opengl.GLSurfaceView;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public abstract class MyGLRendererBase implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener {
|
public abstract class MyGLRendererBase implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener {
|
||||||
|
@ -7,6 +7,7 @@ public class NativeGLRenderer {
|
|||||||
System.loadLibrary("JNIrender");
|
System.loadLibrary("JNIrender");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final int PROCESSING_MODE_NO_PROCESSING = 0;
|
||||||
public static final int PROCESSING_MODE_CPU = 1;
|
public static final int PROCESSING_MODE_CPU = 1;
|
||||||
public static final int PROCESSING_MODE_OCL_DIRECT = 2;
|
public static final int PROCESSING_MODE_OCL_DIRECT = 2;
|
||||||
public static final int PROCESSING_MODE_OCL_OCV = 3;
|
public static final int PROCESSING_MODE_OCL_OCV = 3;
|
||||||
|
@ -34,11 +34,11 @@ public class Tutorial4Activity extends Activity {
|
|||||||
mProcMode = (TextView)findViewById(R.id.proc_mode_text_view);
|
mProcMode = (TextView)findViewById(R.id.proc_mode_text_view);
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
mProcMode.setText("Processing mode: CPU");
|
mProcMode.setText("Processing mode: No processing");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
NativeGLRenderer.setProcessingMode(NativeGLRenderer.PROCESSING_MODE_CPU); }
|
NativeGLRenderer.setProcessingMode(NativeGLRenderer.PROCESSING_MODE_NO_PROCESSING); }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
@ -62,6 +62,14 @@ public class Tutorial4Activity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
|
case R.id.no_proc:
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
mProcMode.setText("Processing mode: No Processing");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
NativeGLRenderer.setProcessingMode(NativeGLRenderer.PROCESSING_MODE_NO_PROCESSING);
|
||||||
|
return true;
|
||||||
case R.id.cpu:
|
case R.id.cpu:
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user