Create wrapper for vulkan.hpp
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-standalone-test.cpp
1 /*
2  * This is the test code that allows to run Vulkan backend
3  * as standalone application. Supports both Xcb and Xlib window
4  * integration. MUST NOT BE COMPILED WITH DALI!
5  */
6
7 // for surface implementation
8 #ifndef VK_USE_PLATFORM_XLIB_KHR
9 #define VK_USE_PLATFORM_XLIB_KHR
10 #endif
11 #ifndef VK_USE_PLATFORM_XCB_KHR
12 #define VK_USE_PLATFORM_XCB_KHR
13 #endif
14
15 #include <dali/graphics/vulkan/vulkan-hpp-wrapper.h>
16
17 #include <dali/integration-api/graphics/vulkan/vk-surface-factory.h>
18 #include <dali/integration-api/graphics/graphics.h>
19 #include <xcb/xcb.h>
20 #include <unistd.h>
21
22 #define USE_XLIB 0
23
24 using Dali::Integration::Graphics::Graphics;
25 using Dali::Integration::Graphics::Vulkan::VkSurfaceFactory;
26
27 template< typename T, typename... Args >
28 std::unique_ptr< T > MakeUnique(Args&&... args)
29 {
30   return std::unique_ptr< T >(new T(std::forward< Args >(args)...));
31 }
32
33
34 class VkSurfaceXlib : public Dali::Integration::Graphics::Vulkan::VkSurfaceFactory
35 {
36 public:
37   /**
38    * Instantiates surface factory ( should
39    * @param display
40    * @param window
41    */
42   VkSurfaceXlib(Display* display, Window window)
43     : VkSurfaceFactory(), mDisplay(display), mWindow(window)
44   {
45   }
46
47   virtual vk::SurfaceKHR Create(vk::Instance instance, vk::AllocationCallbacks* allocCallbacks,
48                                 vk::PhysicalDevice physicalDevice) const override
49   {
50     vk::XlibSurfaceCreateInfoKHR info;
51     info.setDpy(mDisplay).setWindow(mWindow);
52     auto retval = instance.createXlibSurfaceKHR(info, allocCallbacks).value;
53     return retval;
54   }
55
56 private:
57   Display*       mDisplay;
58   Window         mWindow;
59   vk::SurfaceKHR mSurface;
60 };
61
62 class VkSurfaceXcb : public Dali::Integration::Graphics::Vulkan::VkSurfaceFactory
63 {
64 public:
65   /**
66    * Instantiates surface factory ( should
67    * @param display
68    * @param window
69    */
70   VkSurfaceXcb(xcb_connection_t* connection, xcb_window_t window)
71     : VkSurfaceFactory{}, mConnection(connection), mWindow(window)
72   {
73   }
74
75   virtual vk::SurfaceKHR Create(vk::Instance instance, vk::AllocationCallbacks* allocCallbacks,
76                                 vk::PhysicalDevice physicalDevice) const override
77   {
78     vk::XcbSurfaceCreateInfoKHR info;
79     info.setConnection(mConnection).setWindow(mWindow);
80     auto retval = instance.createXcbSurfaceKHR(info, allocCallbacks).value;
81     return retval;
82   }
83
84 private:
85   xcb_connection_t* mConnection;
86   xcb_window_t      mWindow;
87   vk::SurfaceKHR    mSurface;
88 };
89
90 namespace Test
91 {
92 struct xlib_window_t
93 {
94   uint32_t width{0u};
95   uint32_t height{0u};
96   Window   window{};
97   Display* display{nullptr};
98
99   ~xlib_window_t()
100   {
101     XDestroyWindow(display, window);
102   }
103 };
104
105 std::unique_ptr< xlib_window_t > create_xlib_window(int width, int height)
106 {
107   std::unique_ptr< xlib_window_t > wnd{new xlib_window_t};
108   // 1. Create Window ( done by DALI
109
110   wnd->width         = width;
111   wnd->height        = height;
112   wnd->display       = XOpenDisplay(nullptr);
113   auto defaultScreen = DefaultScreen(wnd->display);
114   wnd->window =
115     XCreateSimpleWindow(wnd->display, RootWindow(wnd->display, defaultScreen), 0, 0, wnd->width,
116                         wnd->height, 1, BlackPixel(wnd->display, defaultScreen),
117                         WhitePixel(wnd->display, defaultScreen));
118
119   XSelectInput(wnd->display, wnd->window, ExposureMask | KeyPressMask);
120   XMapWindow(wnd->display, wnd->window);
121   XSync(wnd->display, false);
122
123   return wnd;
124 }
125
126 struct xcb_window_t
127 {
128   uint32_t          width{0u};
129   uint32_t          height{0u};
130   ::xcb_window_t    window;
131   xcb_connection_t* connection;
132
133   ~xcb_window_t()
134   {
135     xcb_destroy_window(connection, window);
136   }
137 };
138
139 std::unique_ptr< Test::xcb_window_t > create_xcb_window(int width, int height)
140 {
141   std::unique_ptr< Test::xcb_window_t > wnd{new Test::xcb_window_t};
142   // 1. Create Window ( done by DALI
143
144   wnd->width  = width;
145   wnd->height = height;
146
147   int screenNum(0);
148
149   xcb_connection_t*     connection = xcb_connect(NULL, &screenNum);
150   const xcb_setup_t*    setup      = xcb_get_setup(connection);
151   xcb_screen_iterator_t iter       = xcb_setup_roots_iterator(setup);
152   for(int i = 0; i < screenNum; ++i)
153     xcb_screen_next(&iter);
154
155   xcb_screen_t* screen = iter.data;
156   ::xcb_window_t  window = xcb_generate_id(connection);
157
158   uint32_t mask     = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
159   uint32_t values[] = {screen->white_pixel, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS};
160
161   xcb_create_window(connection, XCB_COPY_FROM_PARENT, window, screen->root, 0, 0, wnd->width,
162                     wnd->height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, mask,
163                     values);
164
165   xcb_map_window(connection, window);
166   const uint32_t coords[] = {100, 100};
167   xcb_configure_window(connection, window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
168   xcb_flush(connection);
169
170   wnd->connection = connection;
171   wnd->window     = window;
172
173   return wnd;
174 }
175 }
176
177 namespace VulkanTest
178 {
179 int RunTestMain()
180 {
181
182 #if USE_XLIB == 1
183   auto window = Test::create_xlib_window(640, 480);
184   auto surfaceFactory =
185       std::unique_ptr< VkSurfaceXlib >{new VkSurfaceXlib{window->display, window->window}};
186 #else
187   auto window         = Test::create_xcb_window(640, 480);
188   auto surfaceFactory =
189          std::unique_ptr<VkSurfaceXcb>{new VkSurfaceXcb{window->connection, window->window}};
190 #endif
191
192   auto graphics = MakeUnique<Graphics>();
193   auto fbid = graphics->Create( std::move(surfaceFactory) );
194
195   while(1)
196   {
197     graphics->PreRender( fbid );
198     graphics->PostRender( fbid );
199   }
200   return 0;
201 }
202 }