refactoring Tutorial-4 using new OpenCV CameraGLSurfaceView

This commit is contained in:
Andrey Pavlenko
2015-10-07 18:59:43 +03:00
parent 15db8243ef
commit a041105491
12 changed files with 188 additions and 934 deletions

View File

@@ -2,6 +2,7 @@
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS /*let's give a chance for OpenCL 1.1 devices*/
#include <CL/cl.hpp>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
#include <opencv2/core.hpp>
@@ -82,7 +83,7 @@ cl::CommandQueue theQueue;
cl::Program theProgB2B, theProgI2B, theProgI2I;
bool haveOpenCL = false;
void initCL()
extern "C" void initCL()
{
dumpCLinfo();
@@ -144,14 +145,19 @@ void initCL()
LOGD("initCL completed");
}
void closeCL()
extern "C" void closeCL()
{
}
#define GL_TEXTURE_2D 0x0DE1
void procOCL_I2I(int texIn, int texOut, int w, int h)
{
if(!haveOpenCL) return;
LOGD("Processing OpenCL Direct (image2d)");
if(!haveOpenCL)
{
LOGE("OpenCL isn't initialized");
return;
}
LOGD("procOCL_I2I(%d, %d, %d, %d)", texIn, texOut, w, h);
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texIn);
@@ -185,7 +191,12 @@ void procOCL_I2I(int texIn, int texOut, int w, int h)
void procOCL_OCV(int texIn, int texOut, int w, int h)
{
if(!haveOpenCL) return;
LOGD("Processing OpenCL via OpenCV");
if(!haveOpenCL)
{
LOGE("OpenCL isn't initialized");
return;
}
int64_t t = getTimeMs();
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texIn);
@@ -219,3 +230,60 @@ void procOCL_OCV(int texIn, int texOut, int w, int h)
cv::ocl::finish();
LOGD("uploading results to texture costs %d ms", getTimeInterval(t));
}
void drawFrameProcCPU(int w, int h, int texOut)
{
LOGD("Processing on CPU");
int64_t t;
// let's modify pixels in FBO texture in C++ code (on CPU)
const int BUFF_SIZE = 1<<24;//2k*2k*4;
static char tmpBuff[BUFF_SIZE];
if(w*h > BUFF_SIZE)
{
LOGE("Internal temp buffer is too small, can't make CPU frame processing");
return;
}
// read
t = getTimeMs();
// expecting FBO to be bound
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, tmpBuff);
LOGD("glReadPixels() costs %d ms", getTimeInterval(t));
// modify
t = getTimeMs();
cv::Mat m(h, w, CV_8UC4, tmpBuff);
cv::Laplacian(m, m, CV_8U);
m *= 10;
LOGD("Laplacian() costs %d ms", getTimeInterval(t));
// write back
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texOut);
t = getTimeMs();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, tmpBuff);
LOGD("glTexSubImage2D() costs %d ms", getTimeInterval(t));
}
enum ProcMode {PROC_MODE_NO_PROC=0, PROC_MODE_CPU=1, PROC_MODE_OCL_DIRECT=2, PROC_MODE_OCL_OCV=3};
extern "C" void processFrame(int tex1, int tex2, int w, int h, int mode)
{
switch(mode)
{
//case PROC_MODE_NO_PROC:
case PROC_MODE_CPU:
drawFrameProcCPU(w, h, tex2);
break;
case PROC_MODE_OCL_DIRECT:
procOCL_I2I(tex1, tex2, w, h);
break;
case PROC_MODE_OCL_OCV:
procOCL_OCV(tex1, tex2, w, h);
break;
default:
LOGE("Unexpected processing mode: %d", mode);
}
}