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 #ifndef __UTILS_KHR_MEDIA_H
17 #define __UTILS_KHR_MEDIA_H
18 
19 #include <string>
20 #include <iostream>
21 #include <memory>
22 #include <vector>
23 #include "wrappers.h"
24 #include "CL/cl_dx9_media_sharing.h"
25 
26 #include "harness/typeWrappers.h"
27 
28 
29 extern clGetDeviceIDsFromDX9MediaAdapterKHR_fn
30     clGetDeviceIDsFromDX9MediaAdapterKHR;
31 extern clCreateFromDX9MediaSurfaceKHR_fn clCreateFromDX9MediaSurfaceKHR;
32 extern clEnqueueAcquireDX9MediaSurfacesKHR_fn
33     clEnqueueAcquireDX9MediaSurfacesKHR;
34 extern clEnqueueReleaseDX9MediaSurfacesKHR_fn
35     clEnqueueReleaseDX9MediaSurfacesKHR;
36 
37 extern cl_platform_id gPlatformIDdetected;
38 extern cl_device_id gDeviceIDdetected;
39 extern cl_device_type gDeviceTypeSelected;
40 
41 #define NL "\n"
42 #define TEST_NOT_IMPLEMENTED -1
43 #define TEST_NOT_SUPPORTED -2
44 
45 enum TSurfaceFormat
46 {
47     SURFACE_FORMAT_NV12,
48     SURFACE_FORMAT_YV12,
49     SURFACE_FORMAT_R32F,
50     SURFACE_FORMAT_R16F,
51     SURFACE_FORMAT_L16,
52     SURFACE_FORMAT_A8,
53     SURFACE_FORMAT_L8,
54     SURFACE_FORMAT_G32R32F,
55     SURFACE_FORMAT_G16R16F,
56     SURFACE_FORMAT_G16R16,
57     SURFACE_FORMAT_A8L8,
58     SURFACE_FORMAT_A32B32G32R32F,
59     SURFACE_FORMAT_A16B16G16R16F,
60     SURFACE_FORMAT_A16B16G16R16,
61     SURFACE_FORMAT_A8B8G8R8,
62     SURFACE_FORMAT_X8B8G8R8,
63     SURFACE_FORMAT_A8R8G8B8,
64     SURFACE_FORMAT_X8R8G8B8,
65 };
66 
67 enum TContextFuncType
68 {
69     CONTEXT_CREATE_DEFAULT,
70     CONTEXT_CREATE_FROM_TYPE,
71 };
72 
73 enum TSharedHandleType
74 {
75     SHARED_HANDLE_ENABLED,
76     SHARED_HANDLE_DISABLED,
77 };
78 
79 class CResult {
80 public:
81     enum TTestResult
82     {
83         TEST_NORESULT,
84         TEST_NOTSUPPORTED,
85         TEST_PASS,
86         TEST_FAIL,
87         TEST_ERROR,
88     };
89 
90     CResult();
91     ~CResult();
92 
93     void ResultSub(TTestResult result);
94     TTestResult ResultLast() const;
95     int Result() const;
96 
97 private:
98     TTestResult _result;
99     TTestResult _resultLast;
100 };
101 
102 void FunctionContextCreateToString(TContextFuncType contextCreateFunction,
103                                    std::string &contextFunction);
104 void AdapterToString(cl_dx9_media_adapter_type_khr adapterType,
105                      std::string &adapter);
106 cl_context_info
107 AdapterTypeToContextInfo(cl_dx9_media_adapter_type_khr adapterType);
108 
109 // YUV utils
110 void YUVGenerateNV12(std::vector<cl_uchar> &yuv, unsigned int width,
111                      unsigned int height, cl_uchar valueMin, cl_uchar valueMax,
112                      double valueAdd = 0.0);
113 void YUVGenerateYV12(std::vector<cl_uchar> &yuv, unsigned int width,
114                      unsigned int height, cl_uchar valueMin, cl_uchar valueMax,
115                      double valueAdd = 0.0);
116 bool YUVGenerate(TSurfaceFormat surfaceFormat, std::vector<cl_uchar> &yuv,
117                  unsigned int width, unsigned int height, cl_uchar valueMin,
118                  cl_uchar valueMax, double valueAdd = 0.0);
119 bool YUVSurfaceSetNV12(std::auto_ptr<CSurfaceWrapper> &surface,
120                        const std::vector<cl_uchar> &yuv, unsigned int width,
121                        unsigned int height);
122 bool YUVSurfaceSetYV12(std::auto_ptr<CSurfaceWrapper> &surface,
123                        const std::vector<cl_uchar> &yuv, unsigned int width,
124                        unsigned int height);
125 bool YUVSurfaceSet(TSurfaceFormat surfaceFormat,
126                    std::auto_ptr<CSurfaceWrapper> &surface,
127                    const std::vector<cl_uchar> &yuv, unsigned int width,
128                    unsigned int height);
129 bool YUVSurfaceGetNV12(std::auto_ptr<CSurfaceWrapper> &surface,
130                        std::vector<cl_uchar> &yuv, unsigned int width,
131                        unsigned int height);
132 bool YUVSurfaceGetYV12(std::auto_ptr<CSurfaceWrapper> &surface,
133                        std::vector<cl_uchar> &yuv, unsigned int width,
134                        unsigned int height);
135 bool YUVSurfaceGet(TSurfaceFormat surfaceFormat,
136                    std::auto_ptr<CSurfaceWrapper> &surface,
137                    std::vector<cl_uchar> &yuv, unsigned int width,
138                    unsigned int height);
139 bool YUVCompareNV12(const std::vector<cl_uchar> &yuvTest,
140                     const std::vector<cl_uchar> &yuvRef, unsigned int width,
141                     unsigned int height);
142 bool YUVCompareYV12(const std::vector<cl_uchar> &yuvTest,
143                     const std::vector<cl_uchar> &yuvRef, unsigned int width,
144                     unsigned int height);
145 bool YUVCompare(TSurfaceFormat surfaceFormat,
146                 const std::vector<cl_uchar> &yuvTest,
147                 const std::vector<cl_uchar> &yuvRef, unsigned int width,
148                 unsigned int height);
149 
150 // other types utils
151 void DataGenerate(TSurfaceFormat surfaceFormat, cl_channel_type type,
152                   std::vector<float> &data, unsigned int width,
153                   unsigned int height, unsigned int channelNum,
154                   float cmin = 0.0f, float cmax = 1.0f, float add = 0.0f);
155 void DataGenerate(TSurfaceFormat surfaceFormat, cl_channel_type type,
156                   std::vector<cl_half> &data, unsigned int width,
157                   unsigned int height, unsigned int channelNum,
158                   float cmin = 0.0f, float cmax = 1.0f, float add = 0.0f);
159 void DataGenerate(TSurfaceFormat surfaceFormat, cl_channel_type type,
160                   std::vector<cl_uchar> &data, unsigned int width,
161                   unsigned int height, unsigned int channelNum,
162                   float cmin = 0.0f, float cmax = 1.0f, float add = 0.0f);
163 bool DataCompare(TSurfaceFormat surfaceFormat, cl_channel_type type,
164                  const std::vector<cl_float> &dataTest,
165                  const std::vector<cl_float> &dataExp, unsigned int width,
166                  unsigned int height, unsigned int channelNum);
167 bool DataCompare(TSurfaceFormat surfaceFormat, cl_channel_type type,
168                  const std::vector<cl_half> &dataTest,
169                  const std::vector<cl_half> &dataExp, unsigned int width,
170                  unsigned int height, unsigned int channelNum);
171 bool DataCompare(TSurfaceFormat surfaceFormat, cl_channel_type type,
172                  const std::vector<cl_uchar> &dataTest,
173                  const std::vector<cl_uchar> &dataExp, unsigned int width,
174                  unsigned int height, unsigned int channelNum);
175 
176 bool GetImageInfo(cl_mem object, cl_image_format formatExp,
177                   size_t elementSizeExp, size_t rowPitchExp,
178                   size_t slicePitchExp, size_t widthExp, size_t heightExp,
179                   size_t depthExp, unsigned int planeExp);
180 bool GetMemObjInfo(cl_mem object, cl_dx9_media_adapter_type_khr adapterType,
181                    std::auto_ptr<CSurfaceWrapper> &surface,
182                    void *shareHandleExp);
183 bool ImageInfoVerify(cl_dx9_media_adapter_type_khr adapterType,
184                      const std::vector<cl_mem> &memObjList, unsigned int width,
185                      unsigned int height,
186                      std::auto_ptr<CSurfaceWrapper> &surface,
187                      void *sharedHandle);
188 bool ImageFormatCheck(cl_context context, cl_mem_object_type imageType,
189                       const cl_image_format imageFormatCheck);
190 unsigned int ChannelNum(TSurfaceFormat surfaceFormat);
191 unsigned int PlanesNum(TSurfaceFormat surfaceFormat);
192 
193 #if defined(_WIN32)
194 D3DFORMAT SurfaceFormatToD3D(TSurfaceFormat surfaceFormat);
195 #endif
196 
197 bool DeviceCreate(cl_dx9_media_adapter_type_khr adapterType,
198                   std::auto_ptr<CDeviceWrapper> &device);
199 bool SurfaceFormatCheck(cl_dx9_media_adapter_type_khr adapterType,
200                         const CDeviceWrapper &device,
201                         TSurfaceFormat surfaceFormat);
202 bool SurfaceFormatToOCL(TSurfaceFormat surfaceFormat, cl_image_format &format);
203 void SurfaceFormatToString(TSurfaceFormat surfaceFormat, std::string &str);
204 bool MediaSurfaceCreate(cl_dx9_media_adapter_type_khr adapterType,
205                         unsigned int width, unsigned int height,
206                         TSurfaceFormat surfaceFormat, CDeviceWrapper &device,
207                         std::auto_ptr<CSurfaceWrapper> &surface,
208                         bool sharedHandle, void **objectSharedHandle);
209 
210 cl_int
211 deviceExistForCLTest(cl_platform_id platform,
212                      cl_dx9_media_adapter_type_khr media_adapters_type,
213                      void *media_adapters, CResult &result,
214                      TSharedHandleType sharedHandle = SHARED_HANDLE_DISABLED);
215 #endif // __UTILS_KHR_MEDIA_H
216