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(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const = 0;
81
82 uint32_t getSurfaceFormatsCount() const;
83 VkResult getSurfaceFormats(uint32_t *pSurfaceFormatCount, VkSurfaceFormatKHR *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
90 virtual void attachImage(PresentImage *image) = 0;
91 virtual void detachImage(PresentImage *image) = 0;
92 virtual VkResult present(PresentImage *image) = 0;
93
94 void associateSwapchain(SwapchainKHR *swapchain);
95 void disassociateSwapchain();
96 bool hasAssociatedSwapchain();
97
98 protected:
99 static void setCommonSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities);
100
101 private:
102 SwapchainKHR *associatedSwapchain = nullptr;
103 };
104
Cast(VkSurfaceKHR object)105 static inline SurfaceKHR *Cast(VkSurfaceKHR object)
106 {
107 return SurfaceKHR::Cast(object);
108 }
109
110 } // namespace vk
111
112 #endif // SWIFTSHADER_VKSURFACEKHR_HPP_
113