xref: /aosp_15_r20/external/OpenCL-CTS/test_common/gl/setup_x11.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "setup.h"
18 #include "testBase.h"
19 #include "harness/errorHelpers.h"
20 
21 #include <GL/glx.h>
22 #include <CL/cl_ext.h>
23 
24 class X11GLEnvironment : public GLEnvironment
25 {
26 private:
27     cl_device_id m_devices[64];
28     cl_uint m_device_count;
29 
30 public:
X11GLEnvironment()31     X11GLEnvironment()
32     {
33         m_device_count = 0;
34     }
Init(int * argc,char ** argv,int use_opencl_32)35     virtual int Init( int *argc, char **argv, int use_opencl_32 )
36     {
37          // Create a GLUT window to render into
38         glutInit( argc, argv );
39         glutInitWindowSize( 512, 512 );
40         glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
41         glutCreateWindow( "OpenCL <-> OpenGL Test" );
42         glewInit();
43         return 0;
44     }
45 
CreateCLContext(void)46     virtual cl_context CreateCLContext( void )
47     {
48         GLXContext context = glXGetCurrentContext();
49         Display *dpy = glXGetCurrentDisplay();
50 
51         cl_context_properties properties[] = {
52             CL_GL_CONTEXT_KHR,  (cl_context_properties) context,
53             CL_GLX_DISPLAY_KHR, (cl_context_properties) dpy,
54             0
55         };
56         cl_int status;
57 
58         if (!context || !dpy) {
59             print_error(CL_INVALID_CONTEXT, "No GL context bound");
60             return 0;
61         }
62 
63         return clCreateContext(properties, 1, m_devices, NULL, NULL, &status);
64     }
65 
SupportsCLGLInterop(cl_device_type device_type)66     virtual int SupportsCLGLInterop( cl_device_type device_type )
67     {
68         int found_valid_device = 0;
69         cl_platform_id platform;
70         cl_device_id devices[64];
71         cl_uint num_of_devices;
72         int error;
73         error = clGetPlatformIDs(1, &platform, NULL);
74         if (error) {
75             print_error(error, "clGetPlatformIDs failed");
76             return -1;
77         }
78         error = clGetDeviceIDs(platform, device_type, 64, devices, &num_of_devices);
79         // If this platform doesn't have any of the requested device_type (namely GPUs) then return 0
80         if (error == CL_DEVICE_NOT_FOUND)
81           return 0;
82         if (error) {
83             print_error(error, "clGetDeviceIDs failed");
84             return -1;
85         }
86 
87         for (int i=0; i<(int)num_of_devices; i++) {
88             if (!is_extension_available(devices[i], "cl_khr_gl_sharing"))
89             {
90                 log_info("Device %d of %d does not support required extension "
91                          "cl_khr_gl_sharing.\n",
92                          i + 1, num_of_devices);
93             }
94             else
95             {
96                 log_info("Device %d of %d supports required extension "
97                          "cl_khr_gl_sharing.\n",
98                          i + 1, num_of_devices);
99                 found_valid_device = 1;
100                 m_devices[m_device_count++] = devices[i];
101             }
102         }
103         return found_valid_device;
104     }
105 
~X11GLEnvironment()106     virtual ~X11GLEnvironment()
107     {
108     }
109 };
110 
Instance(void)111 GLEnvironment * GLEnvironment::Instance( void )
112 {
113     static X11GLEnvironment * env = NULL;
114     if( env == NULL )
115         env = new X11GLEnvironment();
116     return env;
117 }
118