blob: 8508455f43608a50c7ba074813f0d0564cc2de38 [file] [log] [blame]
Hernan Liatisc6eb41b2019-02-22 11:12:59 -08001// 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
Nicolas Capensd3545372019-08-09 13:59:18 -040018#include "Vulkan/VkImage.hpp"
Ben Clayton45c697a2019-12-17 20:38:03 +000019#include "Vulkan/VkObject.hpp"
Antonio Maiorano42fd1592020-04-27 11:30:40 -040020#include "Vulkan/VulkanPlatform.hpp"
Nicolas Capensd3545372019-08-09 13:59:18 -040021
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080022#include <vector>
23
Nicolas Capens157ba262019-12-10 17:49:14 -050024namespace vk {
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080025
Hernan Liatisf945a5e2019-03-06 15:31:04 -080026enum PresentImageStatus
27{
Nicolas Capensd3545372019-08-09 13:59:18 -040028 NONEXISTENT, // Image wasn't created
Hernan Liatisf945a5e2019-03-06 15:31:04 -080029 AVAILABLE,
30 DRAWING,
31 PRESENTING,
32};
33
Alexis Hetu63ae9242019-06-06 13:52:15 -040034class DeviceMemory;
35class Image;
36class SwapchainKHR;
37
38class PresentImage
Hernan Liatisf945a5e2019-03-06 15:31:04 -080039{
Alexis Hetu63ae9242019-06-06 13:52:15 -040040public:
Ben Clayton45c697a2019-12-17 20:38:03 +000041 VkResult allocateImage(VkDevice device, const VkImageCreateInfo &createInfo);
42 VkResult allocateAndBindImageMemory(VkDevice device, const VkMemoryAllocateInfo &allocateInfo);
Alexis Hetu63ae9242019-06-06 13:52:15 -040043 void clear();
44 VkImage asVkImage() const;
45
Ben Clayton45c697a2019-12-17 20:38:03 +000046 const Image *getImage() const { return image; }
47 DeviceMemory *getImageMemory() const { return imageMemory; }
Alexis Hetu63ae9242019-06-06 13:52:15 -040048 bool isAvailable() const { return (imageStatus == AVAILABLE); }
49 bool exists() const { return (imageStatus != NONEXISTENT); }
50 void setStatus(PresentImageStatus status) { imageStatus = status; }
51
52private:
Ben Clayton45c697a2019-12-17 20:38:03 +000053 Image *image = nullptr;
54 DeviceMemory *imageMemory = nullptr;
Alexis Hetu63ae9242019-06-06 13:52:15 -040055 PresentImageStatus imageStatus = NONEXISTENT;
Hernan Liatisf945a5e2019-03-06 15:31:04 -080056};
57
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080058class SurfaceKHR
59{
60public:
Ben Claytoncaf60312019-05-21 15:31:12 +010061 virtual ~SurfaceKHR() = default;
62
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080063 operator VkSurfaceKHR()
64 {
Alexis Hetubd4cf812019-06-14 15:14:07 -040065 return vk::TtoVkT<SurfaceKHR, VkSurfaceKHR>(this);
66 }
67
Ben Clayton45c697a2019-12-17 20:38:03 +000068 static inline SurfaceKHR *Cast(VkSurfaceKHR object)
Alexis Hetubd4cf812019-06-14 15:14:07 -040069 {
70 return vk::VkTtoT<SurfaceKHR, VkSurfaceKHR>(object);
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080071 }
72
Ben Clayton45c697a2019-12-17 20:38:03 +000073 void destroy(const VkAllocationCallbacks *pAllocator)
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080074 {
75 destroySurface(pAllocator);
76 }
77
Ben Clayton45c697a2019-12-17 20:38:03 +000078 virtual void destroySurface(const VkAllocationCallbacks *pAllocator) = 0;
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080079
Antonio Maiorano51286432021-02-19 09:56:47 -050080 virtual VkResult getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const = 0;
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080081
82 uint32_t getSurfaceFormatsCount() const;
Ben Clayton45c697a2019-12-17 20:38:03 +000083 VkResult getSurfaceFormats(uint32_t *pSurfaceFormatCount, VkSurfaceFormatKHR *pSurfaceFormats) const;
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080084
85 uint32_t getPresentModeCount() const;
Ben Clayton45c697a2019-12-17 20:38:03 +000086 VkResult getPresentModes(uint32_t *pPresentModeCount, VkPresentModeKHR *pPresentModes) const;
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080087
Chris Forbesb52384b2019-08-28 12:01:29 -070088 VkResult getPresentRectangles(uint32_t *pRectCount, VkRect2D *pRects) const;
89
Ben Clayton45c697a2019-12-17 20:38:03 +000090 virtual void attachImage(PresentImage *image) = 0;
91 virtual void detachImage(PresentImage *image) = 0;
92 virtual VkResult present(PresentImage *image) = 0;
Hernan Liatis6b12a502019-03-01 15:06:13 -080093
Ben Clayton45c697a2019-12-17 20:38:03 +000094 void associateSwapchain(SwapchainKHR *swapchain);
Hernan Liatis43be7162019-03-08 17:57:41 -080095 void disassociateSwapchain();
Alexis Hetu63ae9242019-06-06 13:52:15 -040096 bool hasAssociatedSwapchain();
Hernan Liatis43be7162019-03-08 17:57:41 -080097
Antonio Maiorano51286432021-02-19 09:56:47 -050098protected:
99 static void setCommonSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities);
100
Hernan Liatisc6eb41b2019-02-22 11:12:59 -0800101private:
Ben Clayton45c697a2019-12-17 20:38:03 +0000102 SwapchainKHR *associatedSwapchain = nullptr;
Hernan Liatisc6eb41b2019-02-22 11:12:59 -0800103};
104
Ben Clayton45c697a2019-12-17 20:38:03 +0000105static inline SurfaceKHR *Cast(VkSurfaceKHR object)
Hernan Liatisc6eb41b2019-02-22 11:12:59 -0800106{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400107 return SurfaceKHR::Cast(object);
Hernan Liatisc6eb41b2019-02-22 11:12:59 -0800108}
109
Nicolas Capens157ba262019-12-10 17:49:14 -0500110} // namespace vk
Hernan Liatisc6eb41b2019-02-22 11:12:59 -0800111
Sean Risser08762e32021-05-05 14:28:24 -0400112#endif // SWIFTSHADER_VKSURFACEKHR_HPP_