• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "wrappers.h"
17 #include "harness/errorHelpers.h"
18 
19 LPCTSTR CDeviceWrapper::WINDOW_TITLE = _T( "cl_khr_dx9_media_sharing" );
20 const int CDeviceWrapper::WINDOW_WIDTH = 256;
21 const int CDeviceWrapper::WINDOW_HEIGHT = 256;
22 CDeviceWrapper::TAccelerationType CDeviceWrapper::accelerationType = CDeviceWrapper::ACCELERATION_HW;
23 
24 #if defined(_WIN32)
25 const D3DFORMAT CDXVAWrapper::RENDER_TARGET_FORMAT = D3DFMT_X8R8G8B8;
26 const D3DFORMAT CDXVAWrapper::VIDEO_FORMAT = D3DFMT_X8R8G8B8;
27 const unsigned int CDXVAWrapper::VIDEO_FPS = 60;
28 #endif
29 
30 #if defined(_WIN32)
WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)31 static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
32 {
33   switch(msg)
34   {
35   case WM_DESTROY:
36     PostQuitMessage(0);
37     return 0;
38   case WM_PAINT:
39     ValidateRect(hWnd, 0);
40     return 0;
41   default:
42     break;
43   }
44 
45   return DefWindowProc(hWnd, msg, wParam, lParam);
46 }
47 #endif
48 
CDeviceWrapper()49 CDeviceWrapper::CDeviceWrapper()
50 #if defined(_WIN32)
51 :_hInstance(NULL),_hWnd(NULL)
52 #endif
53 {
54 
55 }
56 
WindowInit()57 void CDeviceWrapper::WindowInit()
58 {
59 #if defined(_WIN32)
60   _hInstance = GetModuleHandle(NULL);
61   static WNDCLASSEX wc =
62   {
63     sizeof(WNDCLASSEX),
64     CS_CLASSDC,
65     WndProc,
66     0L,
67     0L,
68     _hInstance,
69     NULL,
70     NULL,
71     NULL,
72     NULL,
73     WINDOW_TITLE,
74     NULL
75   };
76 
77   RegisterClassEx(&wc);
78 
79   _hWnd = CreateWindow(
80     WINDOW_TITLE,
81     WINDOW_TITLE,
82     WS_OVERLAPPEDWINDOW,
83     0, 0,
84     WINDOW_WIDTH, WINDOW_HEIGHT,
85     NULL,
86     NULL,
87     wc.hInstance,
88     NULL);
89 
90   if (!_hWnd)
91   {
92     log_error("Failed to create window");
93     return;
94   }
95 
96   ShowWindow(_hWnd,SW_SHOWDEFAULT);
97   UpdateWindow(_hWnd);
98 #endif
99 }
100 
WindowDestroy()101 void CDeviceWrapper::WindowDestroy()
102 {
103 #if defined(_WIN32)
104   if (_hWnd)
105     DestroyWindow(_hWnd);
106   _hWnd = NULL;
107 #endif
108 }
109 
110 #if defined(_WIN32)
WindowHandle() const111 HWND CDeviceWrapper::WindowHandle() const
112 {
113   return _hWnd;
114 }
115 #endif
116 
WindowWidth() const117 int CDeviceWrapper::WindowWidth() const
118 {
119   return WINDOW_WIDTH;
120 }
121 
WindowHeight() const122 int CDeviceWrapper::WindowHeight() const
123 {
124   return WINDOW_HEIGHT;
125 }
126 
AccelerationType()127 CDeviceWrapper::TAccelerationType CDeviceWrapper::AccelerationType()
128 {
129   return accelerationType;
130 }
131 
AccelerationType(TAccelerationType accelerationTypeNew)132 void CDeviceWrapper::AccelerationType( TAccelerationType accelerationTypeNew )
133 {
134   accelerationType = accelerationTypeNew;
135 }
136 
~CDeviceWrapper()137 CDeviceWrapper::~CDeviceWrapper()
138 {
139   WindowDestroy();
140 }
141 
142 #if defined(_WIN32)
CD3D9Wrapper()143 CD3D9Wrapper::CD3D9Wrapper():
144 _d3d9(NULL), _d3dDevice(NULL), _status(DEVICE_PASS), _adapterIdx(0), _adapterFound(false)
145 {
146   WindowInit();
147 
148   _d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
149   if (!_d3d9)
150   {
151     log_error("Direct3DCreate9 failed\n");
152     _status = DEVICE_FAIL;
153   }
154 }
155 
~CD3D9Wrapper()156 CD3D9Wrapper::~CD3D9Wrapper()
157 {
158   Destroy();
159 
160   if(_d3d9)
161     _d3d9->Release();
162   _d3d9 = 0;
163 }
164 
Destroy()165 void CD3D9Wrapper::Destroy()
166 {
167   if (_d3dDevice)
168     _d3dDevice->Release();
169   _d3dDevice = 0;
170 }
171 
Init()172 cl_int CD3D9Wrapper::Init()
173 {
174   if (!WindowHandle())
175   {
176     log_error("D3D9: Window is not created\n");
177     _status = DEVICE_FAIL;
178     return DEVICE_FAIL;
179   }
180 
181   if(!_d3d9 || DEVICE_PASS  != _status || !_adapterFound)
182     return false;
183 
184   _d3d9->GetAdapterDisplayMode(_adapterIdx - 1, &_d3ddm);
185 
186   D3DPRESENT_PARAMETERS d3dParams;
187   ZeroMemory(&d3dParams, sizeof(d3dParams));
188 
189   d3dParams.Windowed = TRUE;
190   d3dParams.BackBufferCount = 1;
191   d3dParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
192   d3dParams.hDeviceWindow = WindowHandle();
193   d3dParams.BackBufferWidth = WindowWidth();
194   d3dParams.BackBufferHeight = WindowHeight();
195   d3dParams.BackBufferFormat = _d3ddm.Format;
196 
197   DWORD processingType = (AccelerationType() == ACCELERATION_HW)? D3DCREATE_HARDWARE_VERTEXPROCESSING:
198     D3DCREATE_SOFTWARE_VERTEXPROCESSING;
199 
200   if ( FAILED( _d3d9->CreateDevice( _adapterIdx - 1, D3DDEVTYPE_HAL, WindowHandle(),
201     processingType, &d3dParams, &_d3dDevice) ) )
202   {
203     log_error("CreateDevice failed\n");
204     _status = DEVICE_FAIL;
205     return DEVICE_FAIL;
206   }
207 
208   _d3dDevice->BeginScene();
209   _d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0);
210   _d3dDevice->EndScene();
211 
212   return true;
213 }
214 
D3D() const215 void * CD3D9Wrapper::D3D() const
216 {
217   return _d3d9;
218 }
219 
Device() const220 void *CD3D9Wrapper::Device() const
221 {
222   return _d3dDevice;
223 }
224 
Format()225 D3DFORMAT CD3D9Wrapper::Format()
226 {
227   return _d3ddm.Format;
228 }
229 
Adapter()230 D3DADAPTER_IDENTIFIER9 CD3D9Wrapper::Adapter()
231 {
232   return _adapter;
233 }
234 
Status() const235 TDeviceStatus CD3D9Wrapper::Status() const
236 {
237   return _status;
238 }
239 
AdapterNext()240 bool CD3D9Wrapper::AdapterNext()
241 {
242   if (DEVICE_PASS != _status)
243     return false;
244 
245   _adapterFound = false;
246   for(; _adapterIdx < _d3d9->GetAdapterCount();)
247   {
248     ++_adapterIdx;
249     D3DCAPS9 caps;
250     if (FAILED(_d3d9->GetDeviceCaps(_adapterIdx - 1, D3DDEVTYPE_HAL, &caps)))
251       continue;
252 
253     if(FAILED(_d3d9->GetAdapterIdentifier(_adapterIdx - 1, 0, &_adapter)))
254     {
255       log_error("D3D9: GetAdapterIdentifier failed\n");
256       _status = DEVICE_FAIL;
257       return false;
258     }
259 
260     _adapterFound = true;
261 
262     Destroy();
263     if(!Init())
264     {
265       _status = DEVICE_FAIL;
266       _adapterFound = false;
267     }
268     break;
269   }
270 
271   return _adapterFound;
272 }
273 
AdapterIdx() const274 unsigned int CD3D9Wrapper::AdapterIdx() const
275 {
276   return _adapterIdx - 1;
277 }
278 
279 
CD3D9ExWrapper()280 CD3D9ExWrapper::CD3D9ExWrapper():
281 _d3d9Ex(NULL), _d3dDeviceEx(NULL), _status(DEVICE_PASS), _adapterIdx(0), _adapterFound(false)
282 {
283   WindowInit();
284 
285   HRESULT result = Direct3DCreate9Ex(D3D_SDK_VERSION, &_d3d9Ex);
286   if (FAILED(result) || !_d3d9Ex)
287   {
288     log_error("Direct3DCreate9Ex failed\n");
289     _status = DEVICE_FAIL;
290   }
291 }
292 
~CD3D9ExWrapper()293 CD3D9ExWrapper::~CD3D9ExWrapper()
294 {
295   Destroy();
296 
297   if(_d3d9Ex)
298     _d3d9Ex->Release();
299   _d3d9Ex = 0;
300 }
301 
D3D() const302 void * CD3D9ExWrapper::D3D() const
303 {
304   return _d3d9Ex;
305 }
306 
Device() const307 void *CD3D9ExWrapper::Device() const
308 {
309   return _d3dDeviceEx;
310 }
311 
Format()312 D3DFORMAT CD3D9ExWrapper::Format()
313 {
314   return _d3ddmEx.Format;
315 }
316 
Adapter()317 D3DADAPTER_IDENTIFIER9 CD3D9ExWrapper::Adapter()
318 {
319   return _adapter;
320 }
321 
Init()322 cl_int CD3D9ExWrapper::Init()
323 {
324   if (!WindowHandle())
325   {
326     log_error("D3D9EX: Window is not created\n");
327     _status = DEVICE_FAIL;
328     return DEVICE_FAIL;
329   }
330 
331   if(!_d3d9Ex || DEVICE_FAIL == _status || !_adapterFound)
332     return DEVICE_FAIL;
333 
334   RECT rect;
335   GetClientRect(WindowHandle(),&rect);
336 
337   D3DPRESENT_PARAMETERS d3dParams;
338   ZeroMemory(&d3dParams, sizeof(d3dParams));
339 
340   d3dParams.Windowed = TRUE;
341   d3dParams.SwapEffect = D3DSWAPEFFECT_FLIP;
342   d3dParams.BackBufferFormat = D3DFMT_X8R8G8B8;
343   d3dParams.BackBufferWidth = WindowWidth();
344   d3dParams.BackBufferHeight = WindowHeight();
345 
346   d3dParams.BackBufferCount = 1;
347   d3dParams.hDeviceWindow = WindowHandle();
348 
349   DWORD processingType = (AccelerationType() == ACCELERATION_HW)? D3DCREATE_HARDWARE_VERTEXPROCESSING:
350     D3DCREATE_SOFTWARE_VERTEXPROCESSING;
351 
352   if ( FAILED( _d3d9Ex->CreateDeviceEx( _adapterIdx - 1, D3DDEVTYPE_HAL, WindowHandle(),
353     processingType, &d3dParams, NULL, &_d3dDeviceEx) ) )
354   {
355     log_error("CreateDeviceEx failed\n");
356     _status = DEVICE_FAIL;
357     return DEVICE_FAIL;
358   }
359 
360   _d3dDeviceEx->BeginScene();
361   _d3dDeviceEx->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0);
362   _d3dDeviceEx->EndScene();
363 
364   return DEVICE_PASS;
365 }
366 
Destroy()367 void CD3D9ExWrapper::Destroy()
368 {
369   if (_d3dDeviceEx)
370     _d3dDeviceEx->Release();
371   _d3dDeviceEx = 0;
372 }
373 
Status() const374 TDeviceStatus CD3D9ExWrapper::Status() const
375 {
376   return _status;
377 }
378 
AdapterNext()379 bool CD3D9ExWrapper::AdapterNext()
380 {
381   if (DEVICE_FAIL == _status)
382     return false;
383 
384   _adapterFound = false;
385   for(; _adapterIdx < _d3d9Ex->GetAdapterCount();)
386   {
387     ++_adapterIdx;
388     D3DCAPS9 caps;
389     if (FAILED(_d3d9Ex->GetDeviceCaps(_adapterIdx - 1, D3DDEVTYPE_HAL, &caps)))
390       continue;
391 
392     if(FAILED(_d3d9Ex->GetAdapterIdentifier(_adapterIdx - 1, 0, &_adapter)))
393     {
394       log_error("D3D9EX: GetAdapterIdentifier failed\n");
395       _status = DEVICE_FAIL;
396       return false;
397     }
398 
399     _adapterFound = true;
400     Destroy();
401     if(!Init())
402     {
403       _status = DEVICE_FAIL;
404       _adapterFound = _status;
405     }
406 
407     break;
408   }
409 
410   return _adapterFound;
411 }
412 
AdapterIdx() const413 unsigned int CD3D9ExWrapper::AdapterIdx() const
414 {
415   return _adapterIdx - 1;
416 }
417 
CDXVAWrapper()418 CDXVAWrapper::CDXVAWrapper():
419 _dxvaDevice(NULL), _status(DEVICE_PASS), _adapterFound(false)
420 {
421   _status = _d3d9.Status();
422 }
423 
~CDXVAWrapper()424 CDXVAWrapper::~CDXVAWrapper()
425 {
426   DXVAHDDestroy();
427 }
428 
Device() const429 void * CDXVAWrapper::Device() const
430 {
431   return _dxvaDevice;
432 }
433 
Status() const434 TDeviceStatus CDXVAWrapper::Status() const
435 {
436     if(_status == DEVICE_FAIL || _d3d9.Status() == DEVICE_FAIL)
437         return DEVICE_FAIL;
438     else if(_status == DEVICE_NOTSUPPORTED || _d3d9.Status() == DEVICE_NOTSUPPORTED)
439         return DEVICE_NOTSUPPORTED;
440     else
441         return DEVICE_PASS;
442 }
443 
AdapterNext()444 bool CDXVAWrapper::AdapterNext()
445 {
446   if (DEVICE_PASS != _status)
447     return false;
448 
449   _adapterFound = _d3d9.AdapterNext();
450   _status = _d3d9.Status();
451   if (DEVICE_PASS != _status)
452   {
453     _adapterFound = false;
454     return false;
455   }
456 
457   if (!_adapterFound)
458     return false;
459 
460   DXVAHDDestroy();
461   _status = DXVAHDInit();
462   if (DEVICE_PASS != _status)
463   {
464     _adapterFound = false;
465     return false;
466   }
467 
468   return true;
469 }
470 
DXVAHDInit()471 TDeviceStatus CDXVAWrapper::DXVAHDInit()
472 {
473   if ((_status == DEVICE_FAIL) || (_d3d9.Status() == DEVICE_FAIL) || !_adapterFound)
474     return DEVICE_FAIL;
475 
476   DXVAHD_RATIONAL fps = { VIDEO_FPS, 1 };
477 
478   DXVAHD_CONTENT_DESC desc;
479   desc.InputFrameFormat= DXVAHD_FRAME_FORMAT_PROGRESSIVE;
480   desc.InputFrameRate = fps;
481   desc.InputWidth = WindowWidth();
482   desc.InputHeight = WindowHeight();
483   desc.OutputFrameRate = fps;
484   desc.OutputWidth = WindowWidth();
485   desc.OutputHeight = WindowHeight();
486 
487 #ifdef USE_SOFTWARE_PLUGIN
488   _status = DEVICE_FAIL;
489   return DEVICE_FAIL;
490 #endif
491 
492   HRESULT hr = DXVAHD_CreateDevice(static_cast<IDirect3DDevice9Ex *>(_d3d9.Device()),
493     &desc, DXVAHD_DEVICE_USAGE_PLAYBACK_NORMAL, NULL, &_dxvaDevice);
494   if(FAILED(hr))
495   {
496     if (hr == E_NOINTERFACE)
497     {
498       log_error("DXVAHD_CreateDevice skipped due to no supported devices!\n");
499       _status = DEVICE_NOTSUPPORTED;
500     }
501     else
502     {
503     log_error("DXVAHD_CreateDevice failed\n");
504     _status = DEVICE_FAIL;
505     }
506   }
507 
508   return _status;
509 }
510 
DXVAHDDestroy()511 void CDXVAWrapper::DXVAHDDestroy()
512 {
513   if (_dxvaDevice)
514     _dxvaDevice->Release();
515   _dxvaDevice = 0;
516 }
517 
D3D() const518 void * CDXVAWrapper::D3D() const
519 {
520   return _d3d9.D3D();
521 }
522 
AdapterIdx() const523 unsigned int CDXVAWrapper::AdapterIdx() const
524 {
525   return _d3d9.AdapterIdx();
526 }
527 
D3D9() const528 const CD3D9ExWrapper & CDXVAWrapper::D3D9() const
529 {
530   return _d3d9;
531 }
532 
CD3D9SurfaceWrapper()533 CD3D9SurfaceWrapper::CD3D9SurfaceWrapper():
534 mMem(NULL)
535 {
536 
537 }
538 
CD3D9SurfaceWrapper(IDirect3DSurface9 * mem)539 CD3D9SurfaceWrapper::CD3D9SurfaceWrapper( IDirect3DSurface9* mem ):
540 mMem(mem)
541 {
542 
543 }
544 
~CD3D9SurfaceWrapper()545 CD3D9SurfaceWrapper::~CD3D9SurfaceWrapper()
546 {
547   if(mMem != NULL)
548     mMem->Release();
549   mMem = NULL;
550 }
551 
552 #endif
553 
CSurfaceWrapper()554 CSurfaceWrapper::CSurfaceWrapper()
555 {
556 
557 }
558 
~CSurfaceWrapper()559 CSurfaceWrapper::~CSurfaceWrapper()
560 {
561 
562 }
563