@@ -1641,11 +1641,11 @@ Context& initializeContextFromGL()
1641
1641
#elif !defined(HAVE_OPENCL_OPENGL_SHARING)
1642
1642
NO_OPENCL_SHARING_ERROR;
1643
1643
#else
1644
- cl_uint platformsCnt;
1645
- cl_uint devCnt;
1646
- cl_device_id* devices;
1647
- cl_uint devUsed;
1648
- cl_context context;
1644
+ cl_uint platformsCnt = 0 ;
1645
+ cl_uint devCnt = 0 ;
1646
+ cl_device_id* devices = nullptr ;
1647
+ cl_uint devUsed = 0 ;
1648
+ cl_context context = nullptr ;
1649
1649
1650
1650
cl_int status = clGetPlatformIDs (0 , NULL , &platformsCnt);
1651
1651
if (status != CL_SUCCESS)
@@ -1667,102 +1667,109 @@ Context& initializeContextFromGL()
1667
1667
if (status != CL_SUCCESS)
1668
1668
CV_Error_ (cv::Error::OpenCLInitError, (" OpenCL: No devices available: %d" , status));
1669
1669
1670
- devices = new cl_device_id[devCnt];
1670
+ try {
1671
+ devices = new cl_device_id[devCnt];
1671
1672
1672
- status = clGetDeviceIDs (platforms[i], CL_DEVICE_TYPE_GPU, devCnt, devices, NULL );
1673
- if (status != CL_SUCCESS)
1674
- CV_Error_ (cv::Error::OpenCLInitError, (" OpenCL: Can't get platform devices: %d" , status));
1675
-
1676
- for (unsigned int j = 0 ; (!sharingSupported && (j < devCnt)); ++j) {
1677
- size_t extensionSize;
1678
- status = clGetDeviceInfo (devices[j], CL_DEVICE_EXTENSIONS, 0 , NULL , &extensionSize );
1673
+ status = clGetDeviceIDs (platforms[i], CL_DEVICE_TYPE_GPU, devCnt, devices, NULL );
1679
1674
if (status != CL_SUCCESS)
1680
- CV_Error_ (cv::Error::OpenCLInitError, (" OpenCL: No devices available: %d" , status));
1681
-
1682
- if (extensionSize > 0 )
1683
- {
1684
- char * extensions = nullptr ;
1685
-
1686
- try {
1687
- extensions = new char [extensionSize];
1688
-
1689
- status = clGetDeviceInfo (devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize);
1690
- if (status != CL_SUCCESS)
1691
- continue ;
1692
- } catch (std::exception& ex) {
1693
- CV_Error (cv::Error::OpenCLInitError, " OpenCL: Exception thrown during device extensions gathering" );
1694
- }
1675
+ CV_Error_ (cv::Error::OpenCLInitError, (" OpenCL: Can't get platform devices: %d" , status));
1676
+
1677
+ for (unsigned int j = 0 ; (!sharingSupported && (j < devCnt)); ++j) {
1678
+ size_t extensionSize;
1679
+ status = clGetDeviceInfo (devices[j], CL_DEVICE_EXTENSIONS, 0 , NULL , &extensionSize );
1680
+ if (status != CL_SUCCESS)
1681
+ CV_Error_ (cv::Error::OpenCLInitError, (" OpenCL: No devices available: %d" , status));
1682
+
1683
+ if (extensionSize > 0 )
1684
+ {
1685
+ char * extensions = nullptr ;
1686
+
1687
+ try {
1688
+ extensions = new char [extensionSize];
1689
+
1690
+ status = clGetDeviceInfo (devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize);
1691
+ if (status != CL_SUCCESS)
1692
+ continue ;
1693
+ } catch (...) {
1694
+ CV_Error (cv::Error::OpenCLInitError, " OpenCL: Exception thrown during device extensions gathering" );
1695
+ }
1695
1696
1696
- std::string devString;
1697
+ std::string devString;
1697
1698
1698
- if (extensions != nullptr ) {
1699
- devString = extensions;
1700
- delete[] extensions;
1701
- }
1702
- else {
1703
- CV_Error (cv::Error::OpenCLInitError, " OpenCL: Unexpected error during device extensions gathering" );
1704
- }
1699
+ if (extensions != nullptr ) {
1700
+ devString = extensions;
1701
+ delete[] extensions;
1702
+ }
1703
+ else {
1704
+ CV_Error (cv::Error::OpenCLInitError, " OpenCL: Unexpected error during device extensions gathering" );
1705
+ }
1705
1706
1706
- size_t oldPos = 0 ;
1707
- size_t spacePos = devString.find (' ' , oldPos); // extensions string is space delimited
1708
- while (spacePos != devString.npos ) {
1709
- if (strcmp (GL_SHARING_EXTENSION,
1710
- devString.substr (oldPos, spacePos - oldPos).c_str ())
1711
- == 0 ) {
1712
- // Device supports context sharing with OpenGL
1713
- devUsed = i;
1714
- sharingSupported = true ;
1715
- break ;
1707
+ size_t oldPos = 0 ;
1708
+ size_t spacePos = devString.find (' ' , oldPos); // extensions string is space delimited
1709
+ while (spacePos != devString.npos ) {
1710
+ if (strcmp (GL_SHARING_EXTENSION,
1711
+ devString.substr (oldPos, spacePos - oldPos).c_str ())
1712
+ == 0 ) {
1713
+ // Device supports context sharing with OpenGL
1714
+ devUsed = i;
1715
+ sharingSupported = true ;
1716
+ break ;
1717
+ }
1718
+ do {
1719
+ oldPos = spacePos + 1 ;
1720
+ spacePos = devString.find (' ' , oldPos);
1721
+ } while (spacePos == oldPos);
1716
1722
}
1717
- do {
1718
- oldPos = spacePos + 1 ;
1719
- spacePos = devString.find (' ' , oldPos);
1720
- } while (spacePos == oldPos);
1721
1723
}
1722
1724
}
1723
- }
1724
-
1725
- if (!sharingSupported)
1725
+ } catch (...) {
1726
+ CV_Error (cv::Error::OpenCLInitError, " OpenCL: Exception thrown during device information gathering" );
1727
+ if (devices != nullptr ) {
1728
+ delete[] devices;
1729
+ }
1726
1730
continue ;
1731
+ }
1727
1732
1728
- // Define OS-specific context properties and create the OpenCL context
1729
- #if defined (__APPLE__)
1730
- CGLContextObj cglContext = CGLGetCurrentContext ();
1731
- CGLShareGroupObj cglShareGroup = CGLGetShareGroup (cglContext);
1732
- cl_context_properties props[] =
1733
- {
1734
- CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)cglShareGroup,
1735
- 0
1736
- };
1737
- context = clCreateContext (props, 0 ,0 , NULL , NULL , &ciErrNum);
1738
- #elif defined(__ANDROID__)
1739
- cl_context_properties props[] =
1740
- {
1741
- CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext (),
1742
- CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay (),
1743
- CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i],
1744
- 0
1745
- };
1746
- context = clCreateContext (props, 1 , &devices[devUsed], NULL , NULL , &status);
1747
- #elif defined(_WIN32)
1748
- cl_context_properties props[] =
1749
- {
1750
- CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext (),
1751
- CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC (),
1752
- CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i],
1753
- 0
1754
- };
1755
- context = clCreateContext (props, 1 , &devices[devUsed], NULL , NULL , &status);
1756
- #elif defined(__linux__)
1757
- cl_context_properties props[] =
1758
- {
1759
- CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext (),
1760
- CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay (),
1761
- CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i],
1762
- 0
1763
- };
1764
- context = clCreateContext (props, 1 , &devices[devUsed], NULL , NULL , &status);
1765
- #endif
1733
+ if (sharingSupported) {
1734
+ // Define OS-specific context properties and create the OpenCL context
1735
+ #if defined (__APPLE__)
1736
+ CGLContextObj cglContext = CGLGetCurrentContext ();
1737
+ CGLShareGroupObj cglShareGroup = CGLGetShareGroup (cglContext);
1738
+ cl_context_properties props[] =
1739
+ {
1740
+ CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)cglShareGroup,
1741
+ 0
1742
+ };
1743
+ context = clCreateContext (props, 0 ,0 , NULL , NULL , &ciErrNum);
1744
+ #elif defined(__ANDROID__)
1745
+ cl_context_properties props[] =
1746
+ {
1747
+ CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext (),
1748
+ CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay (),
1749
+ CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i],
1750
+ 0
1751
+ };
1752
+ context = clCreateContext (props, 1 , &devices[devUsed], NULL , NULL , &status);
1753
+ #elif defined(_WIN32)
1754
+ cl_context_properties props[] =
1755
+ {
1756
+ CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext (),
1757
+ CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC (),
1758
+ CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i],
1759
+ 0
1760
+ };
1761
+ context = clCreateContext (props, 1 , &devices[devUsed], NULL , NULL , &status);
1762
+ #elif defined(__linux__)
1763
+ cl_context_properties props[] =
1764
+ {
1765
+ CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext (),
1766
+ CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay (),
1767
+ CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i],
1768
+ 0
1769
+ };
1770
+ context = clCreateContext (props, 1 , &devices[devUsed], NULL , NULL , &status);
1771
+ #endif
1772
+ }
1766
1773
1767
1774
if (status != CL_SUCCESS)
1768
1775
CV_Error_ (cv::Error::OpenCLInitError, (" OpenCL: Can't create context for OpenGL interop: %d" , status));
0 commit comments