adding mode switching via menu
This commit is contained in:
parent
9ab291ea1c
commit
56bde913d4
@ -1,6 +1,8 @@
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
float vertices[] = {
|
||||
@ -60,6 +62,10 @@ GLuint FBO = 0;
|
||||
GLuint texOES = 0;
|
||||
int texWidth = 0, texHeight = 0;
|
||||
|
||||
enum ProcMode {PROC_MODE_CPU=1, PROC_MODE_OCL_DIRECT=2, PROC_MODE_OCL_OCV=3};
|
||||
|
||||
ProcMode procMode = PROC_MODE_CPU;
|
||||
|
||||
static inline void deleteTex(GLuint* tex)
|
||||
{
|
||||
if(tex && *tex)
|
||||
@ -223,12 +229,9 @@ void drawFrameOrig()
|
||||
void procCPU(char* buff, int w, int h)
|
||||
{
|
||||
int64_t t = getTimeMs();
|
||||
for(int i=0; i<h; i++)
|
||||
{
|
||||
buff[i*w*4+i*4+0] = 255;
|
||||
buff[i*w*4+i*4+4] = 255;
|
||||
buff[i*w*4+i*4+8] = 255;
|
||||
}
|
||||
cv::Mat m(h, w, CV_8UC4, buff);
|
||||
cv::Laplacian(m, m, CV_8U);
|
||||
m *= 10;
|
||||
LOGD("procCPU() costs %d ms", getTimeInterval(t));
|
||||
}
|
||||
|
||||
@ -271,20 +274,35 @@ void drawFrameProcOCL()
|
||||
|
||||
// modify pixels in FBO texture using OpenCL and CL-GL interop
|
||||
procOCL_I2I(FBOtex, FBOtex2, texWidth, texHeight);
|
||||
//procOCL_OCV(FBOtex, texWidth, texHeight);
|
||||
|
||||
// render to screen
|
||||
drawTex(FBOtex2, GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void drawFrameProcOCLOCV()
|
||||
{
|
||||
drawTex(texOES, GL_TEXTURE_EXTERNAL_OES, FBO);
|
||||
|
||||
// modify pixels in FBO texture using OpenCL and CL-GL interop
|
||||
procOCL_OCV(FBOtex, texWidth, texHeight);
|
||||
|
||||
// render to screen
|
||||
drawTex(FBOtex, GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
extern "C" void drawFrame()
|
||||
{
|
||||
LOGD("*** drawFrame() ***");
|
||||
int64_t t = getTimeMs();
|
||||
//drawFrameOrig();
|
||||
//drawFrameProcCPU();
|
||||
drawFrameProcOCL();
|
||||
|
||||
switch(procMode)
|
||||
{
|
||||
case PROC_MODE_CPU: drawFrameProcCPU(); break;
|
||||
case PROC_MODE_OCL_DIRECT: drawFrameProcOCL(); break;
|
||||
case PROC_MODE_OCL_OCV: drawFrameProcOCLOCV(); break;
|
||||
default: drawFrameOrig();
|
||||
}
|
||||
|
||||
glFinish();
|
||||
LOGD("*** drawFrame() costs %d ms ***", getTimeInterval(t));
|
||||
}
|
||||
@ -342,3 +360,13 @@ extern "C" void changeSize(int width, int height)
|
||||
texHeight = height <= MAX_H ? height : MAX_H;
|
||||
initFBO(texWidth, texHeight);
|
||||
}
|
||||
|
||||
extern "C" void setProcessingMode(int mode)
|
||||
{
|
||||
switch(mode)
|
||||
{
|
||||
case PROC_MODE_CPU: procMode = PROC_MODE_CPU; break;
|
||||
case PROC_MODE_OCL_DIRECT: procMode = PROC_MODE_OCL_DIRECT; break;
|
||||
case PROC_MODE_OCL_OCV: procMode = PROC_MODE_OCL_OCV; break;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ int initGL();
|
||||
void closeGL();
|
||||
void changeSize(int width, int height);
|
||||
void drawFrame();
|
||||
void setProcessingMode(int mode);
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_opencv_samples_tutorial4_NativeGLRenderer_initGL(JNIEnv * env, jclass cls)
|
||||
{
|
||||
@ -24,3 +25,8 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativeGLRenderer_drawFr
|
||||
{
|
||||
drawFrame();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativeGLRenderer_setProcessingMode(JNIEnv * env, jclass cls, jint mode)
|
||||
{
|
||||
setProcessingMode(mode);
|
||||
}
|
||||
|
@ -7,10 +7,20 @@
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/my_gl_surface_view" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fps_text_view"
|
||||
android:text="FPS:" />
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation = "vertical" >
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fps_text_view"
|
||||
android:text="FPS:" />
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/proc_mode_text_view"
|
||||
android:text="Processing mode:" />
|
||||
</LinearLayout>
|
||||
|
||||
</FrameLayout>
|
||||
|
8
samples/android/tutorial-4-opencl/res/menu/menu.xml
Normal file
8
samples/android/tutorial-4-opencl/res/menu/menu.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||
<group android:checkableBehavior="single">
|
||||
<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_ocv" android:title="Use OpenCL via OpenCV" />
|
||||
</group>
|
||||
</menu>
|
@ -1,8 +1,10 @@
|
||||
package org.opencv.samples.tutorial4;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.widget.TextView;
|
||||
|
||||
@ -53,4 +55,11 @@ public class MyGLSurfaceView extends GLSurfaceView {
|
||||
mRenderer.onPause();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent e) {
|
||||
if(e.getAction() == MotionEvent.ACTION_DOWN)
|
||||
((Activity)getContext()).openOptionsMenu();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,14 @@ public class NativeGLRenderer {
|
||||
System.loadLibrary("opencv_java3");
|
||||
System.loadLibrary("JNIrender");
|
||||
}
|
||||
|
||||
public static final int PROCESSING_MODE_CPU = 1;
|
||||
public static final int PROCESSING_MODE_OCL_DIRECT = 2;
|
||||
public static final int PROCESSING_MODE_OCL_OCV = 3;
|
||||
|
||||
public static native int initGL();
|
||||
public static native void closeGL();
|
||||
public static native void drawFrame();
|
||||
public static native void changeSize(int width, int height);
|
||||
public static native void setProcessingMode(int mode);
|
||||
}
|
||||
|
@ -3,6 +3,9 @@ package org.opencv.samples.tutorial4;
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.TextView;
|
||||
@ -10,6 +13,7 @@ import android.widget.TextView;
|
||||
public class Tutorial4Activity extends Activity {
|
||||
|
||||
private MyGLSurfaceView mView;
|
||||
private TextView mProcMode;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@ -27,7 +31,14 @@ public class Tutorial4Activity extends Activity {
|
||||
mView = (MyGLSurfaceView) findViewById(R.id.my_gl_surface_view);
|
||||
TextView tv = (TextView)findViewById(R.id.fps_text_view);
|
||||
mView.setFpsTextView(tv);
|
||||
}
|
||||
mProcMode = (TextView)findViewById(R.id.proc_mode_text_view);
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
mProcMode.setText("Processing mode: CPU");
|
||||
}
|
||||
});
|
||||
|
||||
NativeGLRenderer.setProcessingMode(NativeGLRenderer.PROCESSING_MODE_CPU); }
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
@ -40,4 +51,43 @@ public class Tutorial4Activity extends Activity {
|
||||
super.onResume();
|
||||
mView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.menu, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.cpu:
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
mProcMode.setText("Processing mode: CPU");
|
||||
}
|
||||
});
|
||||
NativeGLRenderer.setProcessingMode(NativeGLRenderer.PROCESSING_MODE_CPU);
|
||||
return true;
|
||||
case R.id.ocl_direct:
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
mProcMode.setText("Processing mode: OpenCL direct");
|
||||
}
|
||||
});
|
||||
NativeGLRenderer.setProcessingMode(NativeGLRenderer.PROCESSING_MODE_OCL_DIRECT);
|
||||
return true;
|
||||
case R.id.ocl_ocv:
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
mProcMode.setText("Processing mode: OpenCL via OpenCV (TAPI)");
|
||||
}
|
||||
});
|
||||
NativeGLRenderer.setProcessingMode(NativeGLRenderer.PROCESSING_MODE_OCL_OCV);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user