aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/extensions/cl_khr_dx9_media_sharing/test_interop_sync.cpp
blob: fbc616e2bfac51f7938a2e216cbdb8be76c2aa18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "utils.h"

int interop_user_sync(cl_device_id deviceID, cl_context context,
                      cl_command_queue queue, int num_elements,
                      unsigned int width, unsigned int height,
                      TContextFuncType functionCreate,
                      cl_dx9_media_adapter_type_khr adapterType,
                      TSurfaceFormat surfaceFormat,
                      TSharedHandleType sharedHandle, cl_bool userSync)
{
    CResult result;

    // create device
    std::auto_ptr<CDeviceWrapper> deviceWrapper;
    if (!DeviceCreate(adapterType, deviceWrapper))
    {
        result.ResultSub(CResult::TEST_ERROR);
        return result.Result();
    }

    // generate input data
    std::vector<cl_uchar> bufferIn(width * height * 3 / 2, 0);
    if (!YUVGenerate(surfaceFormat, bufferIn, width, height, 0, 255))
    {
        result.ResultSub(CResult::TEST_ERROR);
        return result.Result();
    }

    while (deviceWrapper->AdapterNext())
    {
        cl_int error;
        // check if the test can be run on the adapter
        if (CL_SUCCESS
            != (error = deviceExistForCLTest(gPlatformIDdetected, adapterType,
                                             deviceWrapper->Device(), result,
                                             sharedHandle)))
        {
            return result.Result();
        }

        if (surfaceFormat != SURFACE_FORMAT_NV12
            && !SurfaceFormatCheck(adapterType, *deviceWrapper, surfaceFormat))
        {
            std::string sharedHandleStr =
                (sharedHandle == SHARED_HANDLE_ENABLED) ? "yes" : "no";
            std::string syncStr = (userSync == CL_TRUE) ? "yes" : "no";
            std::string formatStr;
            std::string adapterStr;
            SurfaceFormatToString(surfaceFormat, formatStr);
            AdapterToString(adapterType, adapterStr);
            log_info("Skipping test case, image format is not supported by a "
                     "device (adapter type: %s, format: %s, shared handle: %s, "
                     "user sync: %s)\n",
                     adapterStr.c_str(), formatStr.c_str(),
                     sharedHandleStr.c_str(), syncStr.c_str());
            return result.Result();
        }

        void *objectSharedHandle = 0;
        std::auto_ptr<CSurfaceWrapper> surface;
        if (!MediaSurfaceCreate(
                adapterType, width, height, surfaceFormat, *deviceWrapper,
                surface, (sharedHandle == SHARED_HANDLE_ENABLED) ? true : false,
                &objectSharedHandle))
        {
            log_error("Media surface creation failed for %i adapter\n",
                      deviceWrapper->AdapterIdx());
            result.ResultSub(CResult::TEST_ERROR);
            return result.Result();
        }

        cl_context_properties contextProperties[] = {
            CL_CONTEXT_PLATFORM,
            (cl_context_properties)gPlatformIDdetected,
            AdapterTypeToContextInfo(adapterType),
            (cl_context_properties)deviceWrapper->Device(),
            CL_CONTEXT_INTEROP_USER_SYNC,
            userSync,
            0,
        };


        clContextWrapper ctx;
        switch (functionCreate)
        {
            case CONTEXT_CREATE_DEFAULT:
                ctx = clCreateContext(&contextProperties[0], 1,
                                      &gDeviceIDdetected, NULL, NULL, &error);
                break;
            case CONTEXT_CREATE_FROM_TYPE:
                ctx = clCreateContextFromType(&contextProperties[0],
                                              gDeviceTypeSelected, NULL, NULL,
                                              &error);
                break;
            default:
                log_error("Unknown context creation function enum\n");
                result.ResultSub(CResult::TEST_ERROR);
                return result.Result();
                break;
        }

        if (error != CL_SUCCESS)
        {
            std::string functionName;
            FunctionContextCreateToString(functionCreate, functionName);
            log_error("%s failed: %s\n", functionName.c_str(),
                      IGetErrorString(error));
            result.ResultSub(CResult::TEST_FAIL);
            return result.Result();
        }

        if (!YUVSurfaceSet(surfaceFormat, surface, bufferIn, width, height))
        {
            result.ResultSub(CResult::TEST_ERROR);
            return result.Result();
        }

#if defined(_WIN32)
        cl_dx9_surface_info_khr surfaceInfo;
        surfaceInfo.resource =
            *(static_cast<CD3D9SurfaceWrapper *>(surface.get()));
        surfaceInfo.shared_handle = objectSharedHandle;
#else
        void *surfaceInfo = 0;
        return TEST_NOT_IMPLEMENTED;
#endif

        std::vector<cl_mem> memObjList;
        unsigned int planesNum = PlanesNum(surfaceFormat);
        std::vector<clMemWrapper> planesList(planesNum);
        for (unsigned int planeIdx = 0; planeIdx < planesNum; ++planeIdx)
        {
            planesList[planeIdx] = clCreateFromDX9MediaSurfaceKHR(
                ctx, CL_MEM_READ_WRITE, adapterType, &surfaceInfo, planeIdx,
                &error);
            if (error != CL_SUCCESS)
            {
                log_error(
                    "clCreateFromDX9MediaSurfaceKHR failed for plane %i: %s\n",
                    planeIdx, IGetErrorString(error));
                result.ResultSub(CResult::TEST_FAIL);
                return result.Result();
            }
            memObjList.push_back(planesList[planeIdx]);
        }

        clCommandQueueWrapper cmdQueue = clCreateCommandQueueWithProperties(
            ctx, gDeviceIDdetected, 0, &error);
        if (error != CL_SUCCESS)
        {
            log_error("Unable to create command queue: %s\n",
                      IGetErrorString(error));
            result.ResultSub(CResult::TEST_FAIL);
            return result.Result();
        }

        if (!ImageInfoVerify(adapterType, memObjList, width, height, surface,
                             objectSharedHandle))
        {
            log_error("Image info verification failed\n");
            result.ResultSub(CResult::TEST_FAIL);
        }

        if (userSync == CL_TRUE)
        {
#if defined(_WIN32)
            IDirect3DQuery9 *eventQuery = NULL;
            switch (adapterType)
            {
                case CL_ADAPTER_D3D9_KHR: {
                    LPDIRECT3DDEVICE9 device =
                        (LPDIRECT3DDEVICE9)deviceWrapper->Device();
                    device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
                    eventQuery->Issue(D3DISSUE_END);

                    while (S_FALSE
                           == eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH))
                        ;
                }
                break;
                case CL_ADAPTER_D3D9EX_KHR: {
                    LPDIRECT3DDEVICE9EX device =
                        (LPDIRECT3DDEVICE9EX)deviceWrapper->Device();
                    device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
                    eventQuery->Issue(D3DISSUE_END);

                    while (S_FALSE
                           == eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH))
                        ;
                }
                break;
                case CL_ADAPTER_DXVA_KHR: {
                    CDXVAWrapper *DXVADevice =
                        dynamic_cast<CDXVAWrapper *>(&(*deviceWrapper));
                    LPDIRECT3DDEVICE9EX device =
                        (LPDIRECT3DDEVICE9EX)(DXVADevice->D3D9()).Device();
                    device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
                    eventQuery->Issue(D3DISSUE_END);

                    while (S_FALSE
                           == eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH))
                        ;
                }
                break;
                default:
                    log_error("Unknown adapter type\n");
                    return false;
                    break;
            }
            if (eventQuery)
            {
                eventQuery->Release();
            }
#else
            return TEST_NOT_IMPLEMENTED;
#endif
        }

        error = clEnqueueAcquireDX9MediaSurfacesKHR(
            cmdQueue, static_cast<cl_uint>(memObjList.size()),
            &memObjList.at(0), 0, 0, 0);
        if (error != CL_SUCCESS)
        {
            log_error("clEnqueueAcquireDX9MediaSurfacesKHR failed: %s\n",
                      IGetErrorString(error));
            result.ResultSub(CResult::TEST_FAIL);
            return result.Result();
        }

        size_t origin[3] = { 0, 0, 0 };
        size_t offset = 0;
        size_t frameSize = width * height * 3 / 2;
        std::vector<cl_uchar> out(frameSize, 0);
        for (size_t i = 0; i < memObjList.size(); ++i)
        {
            size_t planeWidth = (i == 0) ? width : width / 2;
            size_t planeHeight = (i == 0) ? height : height / 2;
            size_t regionPlane[3] = { planeWidth, planeHeight, 1 };

            error =
                clEnqueueReadImage(cmdQueue, memObjList.at(i), CL_TRUE, origin,
                                   regionPlane, 0, 0, &out.at(offset), 0, 0, 0);
            if (error != CL_SUCCESS)
            {
                log_error("clEnqueueReadImage failed: %s\n",
                          IGetErrorString(error));
                result.ResultSub(CResult::TEST_FAIL);
            }

            offset += planeWidth * planeHeight;
        }

        if (!YUVCompare(surfaceFormat, out, bufferIn, width, height))
        {
            log_error("OCL object verification failed - clEnqueueReadImage\n");
            result.ResultSub(CResult::TEST_FAIL);
        }

        error = clEnqueueReleaseDX9MediaSurfacesKHR(
            cmdQueue, static_cast<cl_uint>(memObjList.size()),
            &memObjList.at(0), 0, 0, 0);
        if (error != CL_SUCCESS)
        {
            log_error("clEnqueueReleaseDX9MediaSurfacesKHR failed: %s\n",
                      IGetErrorString(error));
            result.ResultSub(CResult::TEST_FAIL);
        }

        if (userSync == CL_TRUE)
        {
            error = clFinish(cmdQueue);
            if (error != CL_SUCCESS)
            {
                log_error("clFinish failed: %s\n", IGetErrorString(error));
                result.ResultSub(CResult::TEST_FAIL);
            }
        }

        // shared object verification
        std::vector<cl_uchar> bufferOut(frameSize, 0);
        if (!YUVSurfaceGet(surfaceFormat, surface, bufferOut, width, height))
        {
            result.ResultSub(CResult::TEST_FAIL);
            return result.Result();
        }

        if (!YUVCompare(surfaceFormat, bufferOut, bufferIn, width, height))
        {
            log_error("Media surface is different than expected\n");
            result.ResultSub(CResult::TEST_FAIL);
        }
    }

    if (deviceWrapper->Status() != DEVICE_PASS)
    {
        std::string adapterName;
        AdapterToString(adapterType, adapterName);

        if (deviceWrapper->Status() == DEVICE_FAIL)
        {
            log_error("%s init failed\n", adapterName.c_str());
            result.ResultSub(CResult::TEST_FAIL);
        }
        else
        {
            log_error("%s init incomplete due to unsupported device\n",
                      adapterName.c_str());
            result.ResultSub(CResult::TEST_NOTSUPPORTED);
        }
    }

    return result.Result();
}

int test_interop_user_sync(cl_device_id deviceID, cl_context context,
                           cl_command_queue queue, int num_elements)
{
    const unsigned int WIDTH = 256;
    const unsigned int HEIGHT = 256;

    std::vector<cl_dx9_media_adapter_type_khr> adapters;
#if defined(_WIN32)
    adapters.push_back(CL_ADAPTER_D3D9_KHR);
    adapters.push_back(CL_ADAPTER_D3D9EX_KHR);
    adapters.push_back(CL_ADAPTER_DXVA_KHR);
#else
    return TEST_NOT_IMPLEMENTED;
#endif

    std::vector<TContextFuncType> contextFuncs;
    contextFuncs.push_back(CONTEXT_CREATE_DEFAULT);
    contextFuncs.push_back(CONTEXT_CREATE_FROM_TYPE);

    std::vector<TSurfaceFormat> formats;
    formats.push_back(SURFACE_FORMAT_NV12);
    formats.push_back(SURFACE_FORMAT_YV12);

    std::vector<TSharedHandleType> sharedHandleTypes;
    sharedHandleTypes.push_back(SHARED_HANDLE_DISABLED);
    sharedHandleTypes.push_back(SHARED_HANDLE_ENABLED);

    std::vector<cl_bool> sync;
    sync.push_back(CL_FALSE);
    sync.push_back(CL_TRUE);

    CResult result;
    for (size_t adapterIdx = 0; adapterIdx < adapters.size(); ++adapterIdx)
    {
        // iteration through all create context functions
        for (size_t contextFuncIdx = 0; contextFuncIdx < contextFuncs.size();
             ++contextFuncIdx)
        {
            // iteration through YUV formats
            for (size_t formatIdx = 0; formatIdx < formats.size(); ++formatIdx)
            {
                // shared handle enabled or disabled
                for (size_t sharedHandleIdx = 0;
                     sharedHandleIdx < sharedHandleTypes.size();
                     ++sharedHandleIdx)
                {
                    // user sync interop disabled or enabled
                    for (size_t syncIdx = 0; syncIdx < sync.size(); ++syncIdx)
                    {
                        if (adapters[adapterIdx] == CL_ADAPTER_D3D9_KHR
                            && sharedHandleTypes[sharedHandleIdx]
                                == SHARED_HANDLE_ENABLED)
                            continue;

                        if (interop_user_sync(
                                deviceID, context, queue, num_elements, WIDTH,
                                HEIGHT, contextFuncs[contextFuncIdx],
                                adapters[adapterIdx], formats[formatIdx],
                                sharedHandleTypes[sharedHandleIdx],
                                sync[syncIdx])
                            != 0)
                        {
                            std::string syncStr = (sync[syncIdx] == CL_TRUE)
                                ? "user sync enabled"
                                : "user sync disabled";
                            std::string sharedHandle =
                                (sharedHandleTypes[sharedHandleIdx]
                                 == SHARED_HANDLE_ENABLED)
                                ? "shared handle"
                                : "no shared handle";
                            std::string adapterStr;
                            std::string formatStr;
                            SurfaceFormatToString(formats[formatIdx],
                                                  formatStr);
                            AdapterToString(adapters[adapterIdx], adapterStr);

                            log_error("\nTest case - clCreateContext (%s, %s, "
                                      "%s, %s) failed\n\n",
                                      adapterStr.c_str(), formatStr.c_str(),
                                      sharedHandle.c_str(), syncStr.c_str());
                            result.ResultSub(CResult::TEST_FAIL);
                        }
                    }
                }
            }
        }
    }

    return result.Result();
}