1 /*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 
23 //!
24 //! \file      mock_device.cpp
25 //! \brief     MockDevice member functions.
26 //!
27 
28 #include "mock_device.h"
29 
30 namespace CMRT_UMD
31 {
32 vaDestroySurfacesFunc MockDevice::vaDestroySurfaces = nullptr;
33 
ReleaseVaSurface(void * va_display,void * surface)34 int32_t ReleaseVaSurface(void *va_display, void *surface)
35 {
36     VADisplayContext *display = reinterpret_cast<VADisplayContext*>(va_display);
37     VASurfaceID *surface_id = reinterpret_cast<VASurfaceID*>(surface);
38     return MockDevice::vaDestroySurfaces(display->pDriverContext, surface_id,
39                                          1);
40 }//=========================================
41 
42 template<class InputData>
SendRequestMessage(InputData * input,uint32_t function_id)43 int32_t MockDevice::SendRequestMessage(InputData *input, uint32_t function_id)
44 {
45     uint32_t va_module_id = 2;  // VAExtModuleCMRT.
46     uint32_t input_size = sizeof(InputData);
47     int32_t output = 0;
48     uint32_t output_size = sizeof(output_size);
49     return this->vaCmExtSendReqMsg(&m_vaDisplay, &va_module_id,
50                                    &function_id, input,
51                                    &input_size, nullptr, &output,
52                                    &output_size);
53 }//==============================================
54 
Create(DriverDllLoader * driver_loader,uint32_t additinal_options)55 bool MockDevice::Create(DriverDllLoader *driver_loader,
56                         uint32_t additinal_options)
57 {
58     this->vaCmExtSendReqMsg = driver_loader->GetDriverSymbols().vaCmExtSendReqMsg;
59     if (nullptr == vaDestroySurfaces)
60     {
61         vaDestroySurfaces = driver_loader->m_vtable.vaDestroySurfaces;
62     }
63     m_vaDisplay.pDriverContext = &driver_loader->m_ctx;
64     m_cmDevice = CreateNewDevice(additinal_options);
65     return nullptr != m_cmDevice;
66 }//==============================
67 
Release()68 bool MockDevice::Release()
69 {
70     if (nullptr == m_cmDevice)
71     {
72         return true;
73     }
74     int32_t result = ReleaseNewDevice(m_cmDevice);
75     if (CM_SUCCESS == result)
76     {
77         vaDestroySurfaces = nullptr;
78         m_cmDevice = nullptr;
79         vaCmExtSendReqMsg = nullptr;
80         m_vaDisplay.pDriverContext = nullptr;
81         return true;
82     }
83     return false;
84 }//==============
85 
CreateNewDevice(uint32_t additional_options)86 CmDevice* MockDevice::CreateNewDevice(uint32_t additional_options)
87 {
88     CreateDeviceParam create_param;
89     create_param.release_surface_func = ReleaseVaSurface;
90     create_param.create_option |= additional_options;
91     uint32_t function_id = 0x1000;  // CM_FN_CREATECMDEVICE;
92     SendRequestMessage(&create_param, function_id);
93     CmDevice *new_device = reinterpret_cast<CmDevice*>(
94         create_param.device_in_umd);
95     if (CM_SUCCESS != create_param.return_value || nullptr == new_device)
96     {
97         assert(0);
98         printf("Failed to create a CM device!\n");
99     }
100     return new_device;
101 }//===================
102 
ReleaseNewDevice(CmDevice * device)103 int32_t MockDevice::ReleaseNewDevice(CmDevice *device)
104 {
105     DestroyDeviceParam destroy_param;
106     destroy_param.device_in_umd = device;
107     uint32_t function_id = 0x1001;  // CM_FN_DESTROYCMDEVICE;
108     SendRequestMessage(&destroy_param, function_id);
109     return destroy_param.return_value;
110 }//===================================
111 }  // namespace
112