xref: /aosp_15_r20/external/swiftshader/src/WSI/VkSurfaceKHR.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2018 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 #ifndef SWIFTSHADER_VKSURFACEKHR_HPP_
16 #define SWIFTSHADER_VKSURFACEKHR_HPP_
17 
18 #include "Vulkan/VkImage.hpp"
19 #include "Vulkan/VkObject.hpp"
20 #include "Vulkan/VulkanPlatform.hpp"
21 
22 #include <vector>
23 
24 namespace vk {
25 
26 enum PresentImageStatus
27 {
28 	NONEXISTENT,  // Image wasn't created
29 	AVAILABLE,
30 	DRAWING,
31 	PRESENTING,
32 };
33 
34 class DeviceMemory;
35 class Image;
36 class SwapchainKHR;
37 
38 class PresentImage
39 {
40 public:
41 	VkResult createImage(VkDevice device, const VkImageCreateInfo &createInfo);
42 	VkResult allocateAndBindImageMemory(VkDevice device, const VkMemoryAllocateInfo &allocateInfo);
43 	void release();
44 	VkImage asVkImage() const;
45 
getImage() const46 	const Image *getImage() const { return image; }
getImageMemory() const47 	DeviceMemory *getImageMemory() const { return imageMemory; }
isAvailable() const48 	bool isAvailable() const { return (imageStatus == AVAILABLE); }
exists() const49 	bool exists() const { return (imageStatus != NONEXISTENT); }
setStatus(PresentImageStatus status)50 	void setStatus(PresentImageStatus status) { imageStatus = status; }
51 
52 private:
53 	Image *image = nullptr;
54 	DeviceMemory *imageMemory = nullptr;
55 	PresentImageStatus imageStatus = NONEXISTENT;
56 };
57 
58 class SurfaceKHR
59 {
60 public:
61 	virtual ~SurfaceKHR() = default;
62 
operator VkSurfaceKHR()63 	operator VkSurfaceKHR()
64 	{
65 		return vk::TtoVkT<SurfaceKHR, VkSurfaceKHR>(this);
66 	}
67 
Cast(VkSurfaceKHR object)68 	static inline SurfaceKHR *Cast(VkSurfaceKHR object)
69 	{
70 		return vk::VkTtoT<SurfaceKHR, VkSurfaceKHR>(object);
71 	}
72 
destroy(const VkAllocationCallbacks * pAllocator)73 	void destroy(const VkAllocationCallbacks *pAllocator)
74 	{
75 		destroySurface(pAllocator);
76 	}
77 
78 	virtual void destroySurface(const VkAllocationCallbacks *pAllocator) = 0;
79 
80 	virtual VkResult getSurfaceCapabilities(const void *pSurfaceInfoPNext, VkSurfaceCapabilitiesKHR *pSurfaceCapabilities, void *pSurfaceCapabilitiesPNext) const = 0;
81 
82 	uint32_t getSurfaceFormatsCount(const void *pSurfaceInfoPNext) const;
83 	VkResult getSurfaceFormats(const void *pSurfaceInfoPNext, uint32_t *pSurfaceFormatCount, VkSurfaceFormat2KHR *pSurfaceFormats) const;
84 
85 	uint32_t getPresentModeCount() const;
86 	VkResult getPresentModes(uint32_t *pPresentModeCount, VkPresentModeKHR *pPresentModes) const;
87 
88 	VkResult getPresentRectangles(uint32_t *pRectCount, VkRect2D *pRects) const;
89 
allocateImageMemory(PresentImage * image,const VkMemoryAllocateInfo & allocateInfo)90 	virtual void* allocateImageMemory(PresentImage *image, const VkMemoryAllocateInfo &allocateInfo) { return nullptr; }
releaseImageMemory(PresentImage * image)91 	virtual void releaseImageMemory(PresentImage *image) {}
92 	virtual void attachImage(PresentImage *image) = 0;
93 	virtual void detachImage(PresentImage *image) = 0;
94 	virtual VkResult present(PresentImage *image) = 0;
95 
96 	void associateSwapchain(SwapchainKHR *swapchain);
97 	void disassociateSwapchain();
98 	bool hasAssociatedSwapchain();
99 
100 protected:
101 	static void setCommonSurfaceCapabilities(const void *pSurfaceInfoPNext, VkSurfaceCapabilitiesKHR *pSurfaceCapabilities, void *pSurfaceCapabilitiesPNext);
102 
103 private:
104 	SwapchainKHR *associatedSwapchain = nullptr;
105 };
106 
Cast(VkSurfaceKHR object)107 static inline SurfaceKHR *Cast(VkSurfaceKHR object)
108 {
109 	return SurfaceKHR::Cast(object);
110 }
111 
112 }  // namespace vk
113 
114 #endif  // SWIFTSHADER_VKSURFACEKHR_HPP_
115