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 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include "harness/testHarness.h"
20 #include "utils.h"
21 #include "procs.h"
22
23
24 test_definition test_list[] = { ADD_TEST(context_create),
25 ADD_TEST(get_device_ids),
26 ADD_TEST(api),
27 ADD_TEST(kernel),
28 ADD_TEST(other_data_types),
29 ADD_TEST(memory_access),
30 ADD_TEST(interop_user_sync) };
31
32 const int test_num = ARRAY_SIZE(test_list);
33
34 clGetDeviceIDsFromDX9MediaAdapterKHR_fn clGetDeviceIDsFromDX9MediaAdapterKHR =
35 NULL;
36 clCreateFromDX9MediaSurfaceKHR_fn clCreateFromDX9MediaSurfaceKHR = NULL;
37 clEnqueueAcquireDX9MediaSurfacesKHR_fn clEnqueueAcquireDX9MediaSurfacesKHR =
38 NULL;
39 clEnqueueReleaseDX9MediaSurfacesKHR_fn clEnqueueReleaseDX9MediaSurfacesKHR =
40 NULL;
41
42 cl_platform_id gPlatformIDdetected;
43 cl_device_id gDeviceIDdetected;
44 cl_device_type gDeviceTypeSelected = CL_DEVICE_TYPE_DEFAULT;
45
MediaSurfaceSharingExtensionInit()46 bool MediaSurfaceSharingExtensionInit()
47 {
48 clGetDeviceIDsFromDX9MediaAdapterKHR =
49 (clGetDeviceIDsFromDX9MediaAdapterKHR_fn)
50 clGetExtensionFunctionAddressForPlatform(
51 gPlatformIDdetected, "clGetDeviceIDsFromDX9MediaAdapterKHR");
52 if (clGetDeviceIDsFromDX9MediaAdapterKHR == NULL)
53 {
54 log_error("clGetExtensionFunctionAddressForPlatform("
55 "clGetDeviceIDsFromDX9MediaAdapterKHR) returned NULL.\n");
56 return false;
57 }
58
59 clCreateFromDX9MediaSurfaceKHR = (clCreateFromDX9MediaSurfaceKHR_fn)
60 clGetExtensionFunctionAddressForPlatform(
61 gPlatformIDdetected, "clCreateFromDX9MediaSurfaceKHR");
62 if (clCreateFromDX9MediaSurfaceKHR == NULL)
63 {
64 log_error("clGetExtensionFunctionAddressForPlatform("
65 "clCreateFromDX9MediaSurfaceKHR) returned NULL.\n");
66 return false;
67 }
68
69 clEnqueueAcquireDX9MediaSurfacesKHR =
70 (clEnqueueAcquireDX9MediaSurfacesKHR_fn)
71 clGetExtensionFunctionAddressForPlatform(
72 gPlatformIDdetected, "clEnqueueAcquireDX9MediaSurfacesKHR");
73 if (clEnqueueAcquireDX9MediaSurfacesKHR == NULL)
74 {
75 log_error("clGetExtensionFunctionAddressForPlatform("
76 "clEnqueueAcquireDX9MediaSurfacesKHR) returned NULL.\n");
77 return false;
78 }
79
80 clEnqueueReleaseDX9MediaSurfacesKHR =
81 (clEnqueueReleaseDX9MediaSurfacesKHR_fn)
82 clGetExtensionFunctionAddressForPlatform(
83 gPlatformIDdetected, "clEnqueueReleaseDX9MediaSurfacesKHR");
84 if (clEnqueueReleaseDX9MediaSurfacesKHR == NULL)
85 {
86 log_error("clGetExtensionFunctionAddressForPlatform("
87 "clEnqueueReleaseDX9MediaSurfacesKHR) returned NULL.\n");
88 return false;
89 }
90
91 return true;
92 }
93
DetectPlatformAndDevice()94 bool DetectPlatformAndDevice()
95 {
96 std::vector<cl_platform_id> platforms;
97 cl_uint platformsNum = 0;
98 cl_int error = clGetPlatformIDs(0, 0, &platformsNum);
99 if (error != CL_SUCCESS)
100 {
101 print_error(error, "clGetPlatformIDs failed\n");
102 return false;
103 }
104
105 platforms.resize(platformsNum);
106 error = clGetPlatformIDs(platformsNum, &platforms[0], 0);
107 if (error != CL_SUCCESS)
108 {
109 print_error(error, "clGetPlatformIDs failed\n");
110 return false;
111 }
112
113 bool found = false;
114 for (size_t i = 0; i < platformsNum; ++i)
115 {
116 std::vector<cl_device_id> devices;
117 cl_uint devicesNum = 0;
118 error = clGetDeviceIDs(platforms[i], gDeviceTypeSelected, 0, 0,
119 &devicesNum);
120 if (error != CL_SUCCESS)
121 {
122 print_error(error, "clGetDeviceIDs failed\n");
123 return false;
124 }
125
126 devices.resize(devicesNum);
127 error = clGetDeviceIDs(platforms[i], gDeviceTypeSelected, devicesNum,
128 &devices[0], 0);
129 if (error != CL_SUCCESS)
130 {
131 print_error(error, "clGetDeviceIDs failed\n");
132 return false;
133 }
134
135 for (size_t j = 0; j < devicesNum; ++j)
136 {
137 if (is_extension_available(devices[j], "cl_khr_dx9_media_sharing"))
138 {
139 gPlatformIDdetected = platforms[i];
140 gDeviceIDdetected = devices[j];
141 found = true;
142 break;
143 }
144 }
145 }
146
147 if (!found)
148 {
149 log_info("Test was not run, because the media surface sharing "
150 "extension is not supported for any devices.\n");
151 return false;
152 }
153
154 return true;
155 }
156
CmdlineParse(int argc,const char * argv[])157 bool CmdlineParse(int argc, const char *argv[])
158 {
159 char *env_mode = getenv("CL_DEVICE_TYPE");
160 if (env_mode != NULL)
161 {
162 if (strcmp(env_mode, "gpu") == 0
163 || strcmp(env_mode, "CL_DEVICE_TYPE_GPU") == 0)
164 gDeviceTypeSelected = CL_DEVICE_TYPE_GPU;
165 else if (strcmp(env_mode, "cpu") == 0
166 || strcmp(env_mode, "CL_DEVICE_TYPE_CPU") == 0)
167 gDeviceTypeSelected = CL_DEVICE_TYPE_CPU;
168 else if (strcmp(env_mode, "accelerator") == 0
169 || strcmp(env_mode, "CL_DEVICE_TYPE_ACCELERATOR") == 0)
170 gDeviceTypeSelected = CL_DEVICE_TYPE_ACCELERATOR;
171 else if (strcmp(env_mode, "default") == 0
172 || strcmp(env_mode, "CL_DEVICE_TYPE_DEFAULT") == 0)
173 gDeviceTypeSelected = CL_DEVICE_TYPE_DEFAULT;
174 else
175 {
176 log_error("Unknown CL_DEVICE_TYPE env variable setting: "
177 "%s.\nAborting...\n",
178 env_mode);
179 return false;
180 }
181 }
182
183 for (int i = 0; i < argc; ++i)
184 {
185 if (strcmp(argv[i], "gpu") == 0
186 || strcmp(argv[i], "CL_DEVICE_TYPE_GPU") == 0)
187 {
188 gDeviceTypeSelected = CL_DEVICE_TYPE_GPU;
189 continue;
190 }
191 else if (strcmp(argv[i], "cpu") == 0
192 || strcmp(argv[i], "CL_DEVICE_TYPE_CPU") == 0)
193 {
194 gDeviceTypeSelected = CL_DEVICE_TYPE_CPU;
195 continue;
196 }
197 else if (strcmp(argv[i], "accelerator") == 0
198 || strcmp(argv[i], "CL_DEVICE_TYPE_ACCELERATOR") == 0)
199 {
200 gDeviceTypeSelected = CL_DEVICE_TYPE_ACCELERATOR;
201 continue;
202 }
203 else if (strcmp(argv[i], "CL_DEVICE_TYPE_DEFAULT") == 0)
204 {
205 gDeviceTypeSelected = CL_DEVICE_TYPE_DEFAULT;
206 continue;
207 }
208 else if (strcmp(argv[i], "sw") == 0 || strcmp(argv[i], "software") == 0)
209 {
210 CDeviceWrapper::AccelerationType(CDeviceWrapper::ACCELERATION_SW);
211 }
212 }
213
214 return true;
215 }
216
main(int argc,const char * argv[])217 int main(int argc, const char *argv[])
218 {
219 if (!CmdlineParse(argc, argv)) return TEST_FAIL;
220
221 if (!DetectPlatformAndDevice())
222 {
223 log_info("Test was not run, because the media surface sharing "
224 "extension is not supported\n");
225 return TEST_SKIP;
226 }
227
228 if (!MediaSurfaceSharingExtensionInit()) return TEST_FAIL;
229
230 return runTestHarness(argc, argv, test_num, test_list, true, 0);
231 }
232