Fix Vulkan null driver
[platform/upstream/VK-GL-CTS.git] / framework / platform / lnx / tcuLnxVulkanPlatform.cpp
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program Tester Core
3  * ----------------------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Linux Vulkan Platform.
22  *//*--------------------------------------------------------------------*/
23
24 #include "tcuLnxVulkanPlatform.hpp"
25 #include "tcuLnxPlatform.hpp"
26 #include "vkWsiPlatform.hpp"
27 #include "gluPlatform.hpp"
28 #include "tcuLnx.hpp"
29 #include "tcuFunctionLibrary.hpp"
30 #include "deUniquePtr.hpp"
31 #include "deMemory.h"
32
33 #include <sys/utsname.h>
34
35 using de::MovePtr;
36 using de::UniquePtr;
37
38 #if defined (DEQP_SUPPORT_X11)
39 #       include "tcuLnxX11.hpp"
40 #       if defined (DEQP_SUPPORT_XCB)
41 #               include "tcuLnxX11Xcb.hpp"
42 #       endif // DEQP_SUPPORT_XCB
43 #endif // DEQP_SUPPORT_X11
44
45 #if defined (DEQP_SUPPORT_WAYLAND)
46 #       include "tcuLnxWayland.hpp"
47 #endif // DEQP_SUPPORT_WAYLAND
48
49 namespace tcu
50 {
51 namespace lnx
52 {
53
54 #if defined (DEQP_SUPPORT_X11)
55
56 class VulkanWindowXlib : public vk::wsi::XlibWindowInterface
57 {
58 public:
59         VulkanWindowXlib (MovePtr<x11::XlibWindow> window)
60                 : vk::wsi::XlibWindowInterface  (vk::pt::XlibWindow(window->getXID()))
61                 , m_window                                              (window)
62         {
63         }
64
65         void resize (const UVec2& newSize)
66         {
67                 m_window->setDimensions((int)newSize.x(), (int)newSize.y());
68         }
69
70 private:
71         UniquePtr<x11::XlibWindow>      m_window;
72 };
73
74 class VulkanDisplayXlib : public vk::wsi::XlibDisplayInterface
75 {
76 public:
77         VulkanDisplayXlib (MovePtr<x11::DisplayBase> display)
78                 : vk::wsi::XlibDisplayInterface (vk::pt::XlibDisplayPtr(((x11::XlibDisplay*)display.get())->getXDisplay()))
79                 , m_display     (display)
80         {
81         }
82
83         vk::wsi::Window* createWindow (const Maybe<UVec2>& initialSize) const
84         {
85                 x11::XlibDisplay*       instance        = (x11::XlibDisplay*)(m_display.get());
86                 const deUint32          height          = !initialSize ? (deUint32)DEFAULT_WINDOW_HEIGHT : initialSize->y();
87                 const deUint32          width           = !initialSize ? (deUint32)DEFAULT_WINDOW_WIDTH : initialSize->x();
88                 return new VulkanWindowXlib(MovePtr<x11::XlibWindow>(new x11::XlibWindow(*instance, (int)width, (int)height, instance->getVisual(0))));
89         }
90
91 private:
92         MovePtr<x11::DisplayBase> m_display;
93 };
94
95 #endif // DEQP_SUPPORT_X11
96
97 #if defined (DEQP_SUPPORT_XCB)
98
99 class VulkanWindowXcb : public vk::wsi::XcbWindowInterface
100 {
101 public:
102         VulkanWindowXcb (MovePtr<x11::XcbWindow> window)
103                 : vk::wsi::XcbWindowInterface   (vk::pt::XcbWindow(window->getXID()))
104                 , m_window                                              (window)
105         {
106         }
107
108         void resize (const UVec2& newSize)
109         {
110                 m_window->setDimensions((int)newSize.x(), (int)newSize.y());
111         }
112
113 private:
114         UniquePtr<x11::XcbWindow>       m_window;
115 };
116
117 class VulkanDisplayXcb : public vk::wsi::XcbDisplayInterface
118 {
119 public:
120         VulkanDisplayXcb (MovePtr<x11::DisplayBase> display)
121                 : vk::wsi::XcbDisplayInterface  (vk::pt::XcbConnectionPtr(((x11::XcbDisplay*)display.get())->getConnection()))
122                 , m_display             (display)
123         {
124         }
125
126         vk::wsi::Window* createWindow (const Maybe<UVec2>& initialSize) const
127         {
128                 x11::XcbDisplay*        instance        = (x11::XcbDisplay*)(m_display.get());
129                 const deUint32          height          = !initialSize ? (deUint32)DEFAULT_WINDOW_HEIGHT : initialSize->y();
130                 const deUint32          width           = !initialSize ? (deUint32)DEFAULT_WINDOW_WIDTH : initialSize->x();
131                 return new VulkanWindowXcb(MovePtr<x11::XcbWindow>(new x11::XcbWindow(*instance, (int)width, (int)height, DE_NULL)));
132         }
133
134 private:
135         MovePtr<x11::DisplayBase> m_display;
136 };
137 #endif // DEQP_SUPPORT_XCB
138
139 #if defined (DEQP_SUPPORT_WAYLAND)
140 class VulkanWindowWayland : public vk::wsi::WaylandWindowInterface
141 {
142 public:
143         VulkanWindowWayland (MovePtr<wayland::Window> window)
144                 : vk::wsi::WaylandWindowInterface       (vk::pt::WaylandSurfacePtr(window->getSurface()))
145                 , m_window                                                      (window)
146         {
147         }
148
149         void resize (const UVec2& newSize)
150         {
151                 m_window->setDimensions((int)newSize.x(), (int)newSize.y());
152         }
153
154 private:
155         UniquePtr<wayland::Window>      m_window;
156 };
157
158 class VulkanDisplayWayland : public vk::wsi::WaylandDisplayInterface
159 {
160 public:
161         VulkanDisplayWayland (MovePtr<wayland::Display> display)
162                 : vk::wsi::WaylandDisplayInterface      (vk::pt::WaylandDisplayPtr(display->getDisplay()))
163                 , m_display             (display)
164         {
165         }
166
167         vk::wsi::Window* createWindow (const Maybe<UVec2>& initialSize) const
168         {
169                 const deUint32  height          = !initialSize ? (deUint32)DEFAULT_WINDOW_HEIGHT : initialSize->y();
170                 const deUint32  width           = !initialSize ? (deUint32)DEFAULT_WINDOW_WIDTH : initialSize->x();
171                 return new VulkanWindowWayland(MovePtr<wayland::Window>(new wayland::Window(*m_display, (int)width, (int)height)));
172         }
173
174 private:
175         MovePtr<wayland::Display> m_display;
176 };
177 #endif // DEQP_SUPPORT_WAYLAND
178
179 class VulkanLibrary : public vk::Library
180 {
181 public:
182         VulkanLibrary (void)
183                 : m_library     ("libvulkan.so.1")
184                 , m_driver      (m_library)
185         {
186         }
187
188         const vk::PlatformInterface& getPlatformInterface (void) const
189         {
190                 return m_driver;
191         }
192
193 private:
194         const DynamicFunctionLibrary    m_library;
195         const vk::PlatformDriver                m_driver;
196 };
197
198 VulkanPlatform::VulkanPlatform (EventState& eventState)
199         : m_eventState(eventState)
200 {
201 }
202
203 vk::wsi::Display* VulkanPlatform::createWsiDisplay (vk::wsi::Type wsiType) const
204 {
205         switch(wsiType)
206         {
207 #if defined (DEQP_SUPPORT_X11)
208         case vk::wsi::TYPE_XLIB:
209                 return new VulkanDisplayXlib(MovePtr<x11::DisplayBase>(new x11::XlibDisplay(m_eventState,"")));
210                 break;
211 #endif // DEQP_SUPPORT_X11
212 #if defined (DEQP_SUPPORT_XCB)
213         case vk::wsi::TYPE_XCB:
214                 return new VulkanDisplayXcb(MovePtr<x11::DisplayBase>(new x11::XcbDisplay(m_eventState,"")));
215                 break;
216 #endif // DEQP_SUPPORT_XCB
217 #if defined (DEQP_SUPPORT_WAYLAND)
218         case vk::wsi::TYPE_WAYLAND:
219                 return new VulkanDisplayWayland(MovePtr<wayland::Display>(new wayland::Display(m_eventState, DE_NULL)));
220                 break;
221 #endif // DEQP_SUPPORT_WAYLAND
222
223         default:
224                 TCU_THROW(NotSupportedError, "WSI type not supported");
225
226         };
227 }
228
229 vk::Library* VulkanPlatform::createLibrary (void) const
230 {
231         return new VulkanLibrary();
232 }
233
234 void VulkanPlatform::describePlatform (std::ostream& dst) const
235 {
236         utsname         sysInfo;
237         deMemset(&sysInfo, 0, sizeof(sysInfo));
238
239         if (uname(&sysInfo) != 0)
240                 throw std::runtime_error("uname() failed");
241
242         dst << "OS: " << sysInfo.sysname << " " << sysInfo.release << " " << sysInfo.version << "\n";
243         dst << "CPU: " << sysInfo.machine << "\n";
244 }
245
246 void VulkanPlatform::getMemoryLimits (vk::PlatformMemoryLimits& limits) const
247 {
248         limits.totalSystemMemory                                        = 256*1024*1024;
249         limits.totalDeviceLocalMemory                           = 128*1024*1024;
250         limits.deviceMemoryAllocationGranularity        = 64*1024;
251         limits.devicePageSize                                           = 4096;
252         limits.devicePageTableEntrySize                         = 8;
253         limits.devicePageTableHierarchyLevels           = 3;
254 }
255
256 } // linux
257 } // tcu
258