update opencl-opengl interop sample (remove odd no processing branch)

This commit is contained in:
Vladimir Dudnik 2015-08-01 01:16:06 +03:00
parent ea10290153
commit 5c4c8bcea3

View File

@ -34,7 +34,6 @@
/* /*
// Press key to // Press key to
// 0 no processing
// 1 processing on CPU // 1 processing on CPU
// 2 processing on GPU // 2 processing on GPU
// 9 toggle texture/buffer // 9 toggle texture/buffer
@ -45,18 +44,21 @@
class GLWinApp : public WinApp class GLWinApp : public WinApp
{ {
public: public:
enum MODE
{
MODE_CPU = 0,
MODE_GPU
};
GLWinApp(int width, int height, std::string& window_name, cv::VideoCapture& cap) : GLWinApp(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
WinApp(width, height, window_name) WinApp(width, height, window_name)
{ {
m_shutdown = false; m_shutdown = false;
m_mode = 0; m_use_buffer = false;
m_modeStr[0] = cv::String("Texture/No processing"); m_demo_processing = true;
m_modeStr[1] = cv::String("Texture/Processing on CPU"); m_mode = MODE_CPU;
m_modeStr[2] = cv::String("Texture/Processing on GPU"); m_modeStr[0] = cv::String("Processing on CPU");
m_modeStr[3] = cv::String("Buffer/No processing"); m_modeStr[1] = cv::String("Processing on GPU");
m_modeStr[4] = cv::String("Buffer/Processing on CPU");
m_modeStr[5] = cv::String("Buffer/Processing on GPU");
m_disableProcessing = false;
m_cap = cap; m_cap = cap;
} }
@ -78,9 +80,14 @@ public:
switch (message) switch (message)
{ {
case WM_CHAR: case WM_CHAR:
if (wParam >= '0' && wParam <= '2') if (wParam == '1')
{ {
set_mode((char)wParam - '0'); set_mode(MODE_CPU);
return 0;
}
if (wParam == '2')
{
set_mode(MODE_GPU);
return 0; return 0;
} }
else if (wParam == '9') else if (wParam == '9')
@ -90,7 +97,7 @@ public:
} }
else if (wParam == VK_SPACE) else if (wParam == VK_SPACE)
{ {
m_disableProcessing = !m_disableProcessing; m_demo_processing = !m_demo_processing;
return 0; return 0;
} }
else if (wParam == VK_ESCAPE) else if (wParam == VK_ESCAPE)
@ -136,16 +143,13 @@ public:
switch(keycode_to_keysym(e.xkey.keycode)) switch(keycode_to_keysym(e.xkey.keycode))
{ {
case XK_space: case XK_space:
m_disableProcessing = !m_disableProcessing; m_demo_processing = !m_demo_processing;
break;
case XK_0:
set_mode(0);
break; break;
case XK_1: case XK_1:
set_mode(1); set_mode(MODE_CPU);
break; break;
case XK_2: case XK_2:
set_mode(2); set_mode(MODE_GPU);
break; break;
case XK_9: case XK_9:
toggle_buffer(); toggle_buffer();
@ -198,14 +202,14 @@ public:
return 0; return 0;
} // init() } // init()
int get_frame(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer) int get_frame(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
{ {
if (!m_cap.read(m_frame_bgr)) if (!m_cap.read(m_frame_bgr))
return -1; return -1;
cv::cvtColor(m_frame_bgr, m_frame_rgba, CV_RGB2RGBA); cv::cvtColor(m_frame_bgr, m_frame_rgba, CV_RGB2RGBA);
if (use_buffer()) if (do_buffer)
buffer.copyFrom(m_frame_rgba, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true); buffer.copyFrom(m_frame_rgba, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
else else
texture.copyFrom(m_frame_rgba, true); texture.copyFrom(m_frame_rgba, true);
@ -213,7 +217,7 @@ public:
return 0; return 0;
} }
void print_info(int mode, float time, cv::String oclDevName) void print_info(MODE mode, float time, cv::String& oclDevName)
{ {
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
HDC hDC = m_hDC; HDC hDC = m_hDC;
@ -231,12 +235,12 @@ public:
int y = 0; int y = 0;
buf[0] = 0; buf[0] = 0;
sprintf_s(buf, sizeof(buf)-1, "Mode: %s", m_modeStr[mode].c_str()); sprintf_s(buf, sizeof(buf)-1, "Mode: %s OpenGL %s", m_modeStr[mode].c_str(), use_buffer() ? "buffer" : "texture");
::TextOut(hDC, 0, y, buf, (int)strlen(buf)); ::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight; y += tm.tmHeight;
buf[0] = 0; buf[0] = 0;
sprintf_s(buf, sizeof(buf)-1, "Time: %2.1f", time); sprintf_s(buf, sizeof(buf)-1, "Time, msec: %2.1f", time);
::TextOut(hDC, 0, y, buf, (int)strlen(buf)); ::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight; y += tm.tmHeight;
@ -249,7 +253,7 @@ public:
#elif defined(__linux__) #elif defined(__linux__)
char buf[256+1]; char buf[256+1];
snprintf(buf, sizeof(buf)-1, "Time: %2.1f Mode: %s Device: %s", time, m_modeStr[mode].c_str(), oclDevName.c_str()); snprintf(buf, sizeof(buf)-1, "Time, msec: %2.1f, Mode: %s OpenGL %s, Device: %s", time, m_modeStr[mode].c_str(), use_buffer() ? "buffer" : "texture", oclDevName.c_str());
XStoreName(m_display, m_window, buf); XStoreName(m_display, m_window, buf);
#endif #endif
} }
@ -273,26 +277,23 @@ public:
texture.setAutoRelease(true); texture.setAutoRelease(true);
buffer.setAutoRelease(true); buffer.setAutoRelease(true);
r = get_frame(texture, buffer); MODE mode = get_mode();
bool do_buffer = use_buffer();
r = get_frame(texture, buffer, do_buffer);
if (r != 0) if (r != 0)
{ {
return -1; return -1;
} }
bool do_buffer = use_buffer(); switch (mode)
switch (get_mode())
{ {
case 0: // no processing case MODE_CPU: // process frame on CPU
m_timer.clear(); processFrameCPU(texture, buffer, do_buffer);
break; break;
case 1: // process frame on CPU case MODE_GPU: // process frame on GPU
processFrameCPU(texture, buffer); processFrameGPU(texture, buffer, do_buffer);
break;
case 2: // process frame on GPU
processFrameGPU(texture, buffer);
break; break;
} // switch } // switch
@ -328,7 +329,7 @@ public:
glXSwapBuffers(m_display, m_window); glXSwapBuffers(m_display, m_window);
#endif #endif
print_info(m_mode, m_timer.time(Timer::UNITS::MSEC), m_oclDevName); print_info(mode, m_timer.time(Timer::UNITS::MSEC), m_oclDevName);
} }
@ -343,12 +344,10 @@ public:
protected: protected:
void processFrameCPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer) void processFrameCPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
{ {
cv::Mat m(m_height, m_width, CV_8UC4); cv::Mat m(m_height, m_width, CV_8UC4);
bool do_buffer = use_buffer();
m_timer.start(); m_timer.start();
if (do_buffer) if (do_buffer)
@ -356,7 +355,7 @@ protected:
else else
texture.copyTo(m); texture.copyTo(m);
if (!m_disableProcessing) if (m_demo_processing)
{ {
// blur texture image with OpenCV on CPU // blur texture image with OpenCV on CPU
cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7));
@ -370,12 +369,10 @@ protected:
m_timer.stop(); m_timer.stop();
} }
void processFrameGPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer) void processFrameGPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
{ {
cv::UMat u; cv::UMat u;
bool do_buffer = use_buffer();
m_timer.start(); m_timer.start();
if (do_buffer) if (do_buffer)
@ -383,7 +380,7 @@ protected:
else else
cv::ogl::convertFromGLTexture2D(texture, u); cv::ogl::convertFromGLTexture2D(texture, u);
if (!m_disableProcessing) if (m_demo_processing)
{ {
// blur texture image with OpenCV on GPU with OpenCL // blur texture image with OpenCV on GPU with OpenCL
cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7));
@ -432,8 +429,10 @@ protected:
int pfmt = ChoosePixelFormat(m_hDC, &pfd); int pfmt = ChoosePixelFormat(m_hDC, &pfd);
if (pfmt == 0) if (pfmt == 0)
return -1; return -1;
if (SetPixelFormat(m_hDC, pfmt, &pfd) == 0) if (SetPixelFormat(m_hDC, pfmt, &pfd) == 0)
return -2; return -2;
return 0; return 0;
} }
#endif #endif
@ -449,36 +448,17 @@ protected:
} }
#endif #endif
// modes: 0,1,2 - use texture bool use_buffer() { return m_use_buffer; }
// 3,4,5 - use buffer void toggle_buffer() { m_use_buffer = !m_use_buffer; }
bool use_buffer() MODE get_mode() { return m_mode; }
{ void set_mode(MODE mode) { m_mode = mode; }
return bool(m_mode >= 3);
}
void toggle_buffer()
{
if (m_mode < 3)
m_mode += 3;
else
m_mode -= 3;
}
int get_mode()
{
return (m_mode % 3);
}
void set_mode(int mode)
{
bool do_buffer = bool(m_mode >= 3);
m_mode = (mode % 3);
if (do_buffer)
m_mode += 3;
}
private: private:
bool m_shutdown; bool m_shutdown;
int m_mode; bool m_use_buffer;
cv::String m_modeStr[3*2]; bool m_demo_processing;
int m_disableProcessing; MODE m_mode;
cv::String m_modeStr[2];
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
HDC m_hDC; HDC m_hDC;
HGLRC m_hRC; HGLRC m_hRC;
@ -515,10 +495,10 @@ using namespace std;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
cv::CommandLineParser parser(argc, argv, keys); \ cv::CommandLineParser parser(argc, argv, keys);
bool useCamera = parser.has("camera"); \ bool useCamera = parser.has("camera");
string file = parser.get<string>("file"); \ string file = parser.get<string>("file");
bool showHelp = parser.get<bool>("help"); \ bool showHelp = parser.get<bool>("help");
if (showHelp) if (showHelp)
{ {