xref: /aosp_15_r20/external/swiftshader/src/WSI/DirectFBSurfaceEXT.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 "DirectFBSurfaceEXT.hpp"
16 
17 #include "Vulkan/VkDeviceMemory.hpp"
18 #include "Vulkan/VkImage.hpp"
19 
20 namespace vk {
21 
DirectFBSurfaceEXT(const VkDirectFBSurfaceCreateInfoEXT * pCreateInfo,void * mem)22 DirectFBSurfaceEXT::DirectFBSurfaceEXT(const VkDirectFBSurfaceCreateInfoEXT *pCreateInfo, void *mem)
23     : dfb(pCreateInfo->dfb)
24     , surface(pCreateInfo->surface)
25 {
26 }
27 
destroySurface(const VkAllocationCallbacks * pAllocator)28 void DirectFBSurfaceEXT::destroySurface(const VkAllocationCallbacks *pAllocator)
29 {
30 }
31 
ComputeRequiredAllocationSize(const VkDirectFBSurfaceCreateInfoEXT * pCreateInfo)32 size_t DirectFBSurfaceEXT::ComputeRequiredAllocationSize(const VkDirectFBSurfaceCreateInfoEXT *pCreateInfo)
33 {
34 	return 0;
35 }
36 
getSurfaceCapabilities(const void * pSurfaceInfoPNext,VkSurfaceCapabilitiesKHR * pSurfaceCapabilities,void * pSurfaceCapabilitiesPNext) const37 VkResult DirectFBSurfaceEXT::getSurfaceCapabilities(const void *pSurfaceInfoPNext, VkSurfaceCapabilitiesKHR *pSurfaceCapabilities, void *pSurfaceCapabilitiesPNext) const
38 {
39 	int width, height;
40 	surface->GetSize(surface, &width, &height);
41 	VkExtent2D extent = { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
42 
43 	pSurfaceCapabilities->currentExtent = extent;
44 	pSurfaceCapabilities->minImageExtent = extent;
45 	pSurfaceCapabilities->maxImageExtent = extent;
46 
47 	setCommonSurfaceCapabilities(pSurfaceInfoPNext, pSurfaceCapabilities, pSurfaceCapabilitiesPNext);
48 	return VK_SUCCESS;
49 }
50 
attachImage(PresentImage * image)51 void DirectFBSurfaceEXT::attachImage(PresentImage *image)
52 {
53 	DFBSurfaceDescription desc;
54 	desc.flags = static_cast<DFBSurfaceDescriptionFlags>(DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PREALLOCATED);
55 	desc.pixelformat = DSPF_RGB32;
56 	const VkExtent3D &extent = image->getImage()->getExtent();
57 	desc.width = extent.width;
58 	desc.height = extent.height;
59 	desc.preallocated[0].data = image->getImageMemory()->getOffsetPointer(0);
60 	desc.preallocated[0].pitch = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
61 	IDirectFBSurface *dfbImage;
62 	dfb->CreateSurface(dfb, &desc, &dfbImage);
63 	imageMap[image] = dfbImage;
64 }
65 
detachImage(PresentImage * image)66 void DirectFBSurfaceEXT::detachImage(PresentImage *image)
67 {
68 	auto it = imageMap.find(image);
69 	if(it != imageMap.end())
70 	{
71 		IDirectFBSurface *dfbImage = it->second;
72 		dfbImage->Release(dfbImage);
73 		imageMap.erase(it);
74 	}
75 }
76 
present(PresentImage * image)77 VkResult DirectFBSurfaceEXT::present(PresentImage *image)
78 {
79 	auto it = imageMap.find(image);
80 	if(it != imageMap.end())
81 	{
82 		IDirectFBSurface *dfbImage = it->second;
83 		surface->Blit(surface, dfbImage, NULL, 0, 0);
84 		surface->Flip(surface, NULL, DSFLIP_WAITFORSYNC);
85 	}
86 
87 	return VK_SUCCESS;
88 }
89 
90 }  // namespace vk
91