xref: /aosp_15_r20/external/swiftshader/src/WSI/WaylandSurfaceKHR.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2020 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "WaylandSurfaceKHR.hpp"
16 
17 #include "libWaylandClient.hpp"
18 #include "Vulkan/VkDeviceMemory.hpp"
19 #include "Vulkan/VkImage.hpp"
20 
21 #include <string.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24 
25 namespace vk {
26 
isSupported()27 bool WaylandSurfaceKHR::isSupported()
28 {
29 	return libWaylandClient.isPresent();
30 }
31 
wl_registry_handle_global(void * data,struct wl_registry * registry,unsigned int name,const char * interface,unsigned int version)32 static void wl_registry_handle_global(void *data, struct wl_registry *registry, unsigned int name, const char *interface, unsigned int version)
33 {
34 	struct wl_shm **pshm = (struct wl_shm **)data;
35 	if(!strcmp(interface, "wl_shm"))
36 	{
37 		*pshm = static_cast<struct wl_shm *>(libWaylandClient->wl_registry_bind(registry, name, libWaylandClient->wl_shm_interface, 1));
38 	}
39 }
40 
wl_registry_handle_global_remove(void * data,struct wl_registry * registry,unsigned int name)41 static void wl_registry_handle_global_remove(void *data, struct wl_registry *registry, unsigned int name)
42 {
43 }
44 
45 static const struct wl_registry_listener wl_registry_listener = { wl_registry_handle_global, wl_registry_handle_global_remove };
46 
WaylandSurfaceKHR(const VkWaylandSurfaceCreateInfoKHR * pCreateInfo,void * mem)47 WaylandSurfaceKHR::WaylandSurfaceKHR(const VkWaylandSurfaceCreateInfoKHR *pCreateInfo, void *mem)
48     : display(pCreateInfo->display)
49     , surface(pCreateInfo->surface)
50 {
51 	struct wl_registry *registry = libWaylandClient->wl_display_get_registry(display);
52 	libWaylandClient->wl_registry_add_listener(registry, &wl_registry_listener, &shm);
53 	libWaylandClient->wl_display_dispatch(display);
54 }
55 
destroySurface(const VkAllocationCallbacks * pAllocator)56 void WaylandSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
57 {
58 }
59 
ComputeRequiredAllocationSize(const VkWaylandSurfaceCreateInfoKHR * pCreateInfo)60 size_t WaylandSurfaceKHR::ComputeRequiredAllocationSize(const VkWaylandSurfaceCreateInfoKHR *pCreateInfo)
61 {
62 	return 0;
63 }
64 
getSurfaceCapabilities(const void * pSurfaceInfoPNext,VkSurfaceCapabilitiesKHR * pSurfaceCapabilities,void * pSurfaceCapabilitiesPNext) const65 VkResult WaylandSurfaceKHR::getSurfaceCapabilities(const void *pSurfaceInfoPNext, VkSurfaceCapabilitiesKHR *pSurfaceCapabilities, void *pSurfaceCapabilitiesPNext) const
66 {
67 	pSurfaceCapabilities->currentExtent = { 0xFFFFFFFF, 0xFFFFFFFF };
68 	pSurfaceCapabilities->minImageExtent = { 1, 1 };
69 	pSurfaceCapabilities->maxImageExtent = { 0xFFFFFFFF, 0xFFFFFFFF };
70 
71 	setCommonSurfaceCapabilities(pSurfaceInfoPNext, pSurfaceCapabilities, pSurfaceCapabilitiesPNext);
72 	return VK_SUCCESS;
73 }
74 
attachImage(PresentImage * image)75 void WaylandSurfaceKHR::attachImage(PresentImage *image)
76 {
77 	WaylandImage *wlImage = new WaylandImage;
78 	char path[] = "/tmp/XXXXXX";
79 	int fd = mkstemp(path);
80 	const VkExtent3D &extent = image->getImage()->getExtent();
81 	int stride = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
82 	assert(ftruncate(fd, extent.height * stride) == 0);
83 	struct wl_shm_pool *pool = libWaylandClient->wl_shm_create_pool(shm, fd, extent.height * stride);
84 	wlImage->buffer = libWaylandClient->wl_shm_pool_create_buffer(pool, 0, extent.width, extent.height, stride, WL_SHM_FORMAT_XRGB8888);
85 	wlImage->data = static_cast<uint8_t *>(mmap(NULL, extent.height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
86 	libWaylandClient->wl_shm_pool_destroy(pool);
87 	close(fd);
88 	imageMap[image] = wlImage;
89 }
90 
detachImage(PresentImage * image)91 void WaylandSurfaceKHR::detachImage(PresentImage *image)
92 {
93 	auto it = imageMap.find(image);
94 	if(it != imageMap.end())
95 	{
96 		WaylandImage *wlImage = it->second;
97 		const VkExtent3D &extent = image->getImage()->getExtent();
98 		int stride = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
99 		munmap(wlImage->data, extent.height * stride);
100 		libWaylandClient->wl_buffer_destroy(wlImage->buffer);
101 		delete wlImage;
102 		imageMap.erase(it);
103 	}
104 }
105 
present(PresentImage * image)106 VkResult WaylandSurfaceKHR::present(PresentImage *image)
107 {
108 	auto it = imageMap.find(image);
109 	if(it != imageMap.end())
110 	{
111 		WaylandImage *wlImage = it->second;
112 		const VkExtent3D &extent = image->getImage()->getExtent();
113 		int bufferRowPitch = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
114 		image->getImage()->copyTo(reinterpret_cast<uint8_t *>(wlImage->data), bufferRowPitch);
115 		libWaylandClient->wl_surface_attach(surface, wlImage->buffer, 0, 0);
116 		libWaylandClient->wl_surface_damage(surface, 0, 0, extent.width, extent.height);
117 		libWaylandClient->wl_surface_commit(surface);
118 		libWaylandClient->wl_display_roundtrip(display);
119 		libWaylandClient->wl_display_sync(display);
120 	}
121 
122 	return VK_SUCCESS;
123 }
124 
125 }  // namespace vk
126