OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gpu/vulkan/vulkan_surface.h" | 5 #include "gpu/vulkan/vulkan_surface.h" |
6 | 6 |
| 7 #include <vulkan/vulkan.h> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "gpu/vulkan/vulkan_command_buffer.h" |
7 #include "gpu/vulkan/vulkan_implementation.h" | 11 #include "gpu/vulkan/vulkan_implementation.h" |
| 12 #include "gpu/vulkan/vulkan_platform.h" |
| 13 #include "gpu/vulkan/vulkan_swap_chain.h" |
| 14 |
| 15 #if defined(USE_X11) |
| 16 #include "ui/gfx/x/x11_types.h" |
| 17 #endif // defined(USE_X11) |
8 | 18 |
9 namespace gpu { | 19 namespace gpu { |
10 | 20 |
11 VulkanSurface::VulkanSurface() {} | 21 namespace { |
| 22 const VkFormat kNativeVkFormat[] = { |
| 23 VK_FORMAT_B8G8R8A8_UNORM, // FORMAT_BGRA8888, |
| 24 VK_FORMAT_R5G6B5_UNORM_PACK16, // FORMAT_RGB565, |
| 25 }; |
| 26 static_assert(arraysize(kNativeVkFormat) == VulkanSurface::NUM_SURFACE_FORMATS, |
| 27 "Array size for kNativeVkFormat must match surface formats."); |
| 28 |
| 29 } // namespace |
| 30 |
| 31 class VulkanWSISurface : public VulkanSurface { |
| 32 public: |
| 33 explicit VulkanWSISurface(gfx::AcceleratedWidget window) : window_(window) {} |
| 34 |
| 35 ~VulkanWSISurface() override { |
| 36 DCHECK_EQ(static_cast<VkSurfaceKHR>(VK_NULL_HANDLE), surface_); |
| 37 } |
| 38 |
| 39 bool Initialize(VulkanSurface::Format format) override { |
| 40 DCHECK(format >= 0 && format < NUM_SURFACE_FORMATS); |
| 41 #if defined(VK_USE_PLATFORM_XLIB_KHR) |
| 42 VkXlibSurfaceCreateInfoKHR surface_create_info = {}; |
| 43 surface_create_info.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; |
| 44 surface_create_info.dpy = gfx::GetXDisplay(); |
| 45 surface_create_info.window = window_; |
| 46 vkCreateXlibSurfaceKHR(GetVulkanInstance(), &surface_create_info, nullptr, |
| 47 &surface_); |
| 48 #else |
| 49 #error Unsupported Vulkan Platform. |
| 50 #endif |
| 51 |
| 52 // Get list of supported formats. |
| 53 uint32_t format_count = 0; |
| 54 VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR( |
| 55 GetVulkanPhysicalDevice(), surface_, &format_count, nullptr); |
| 56 if (VK_SUCCESS != result) { |
| 57 DLOG(ERROR) << "vkGetPhysicalDeviceSurfaceFormatsKHR() failed: " |
| 58 << result; |
| 59 return false; |
| 60 } |
| 61 |
| 62 std::vector<VkSurfaceFormatKHR> formats(format_count); |
| 63 result = vkGetPhysicalDeviceSurfaceFormatsKHR( |
| 64 GetVulkanPhysicalDevice(), surface_, &format_count, formats.data()); |
| 65 if (VK_SUCCESS != result) { |
| 66 DLOG(ERROR) << "vkGetPhysicalDeviceSurfaceFormatsKHR() failed: " |
| 67 << result; |
| 68 return false; |
| 69 } |
| 70 |
| 71 const VkFormat preferred_format = kNativeVkFormat[format]; |
| 72 if (formats.size() == 1 && VK_FORMAT_UNDEFINED == formats[0].format) { |
| 73 surface_format_.format = preferred_format; |
| 74 surface_format_.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
| 75 } else { |
| 76 bool format_set = false; |
| 77 for (VkSurfaceFormatKHR supported_format : formats) { |
| 78 if (supported_format.format == preferred_format) { |
| 79 surface_format_ = supported_format; |
| 80 surface_format_.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
| 81 format_set = true; |
| 82 break; |
| 83 } |
| 84 } |
| 85 if (!format_set) { |
| 86 DLOG(ERROR) << "Format not supported."; |
| 87 return false; |
| 88 } |
| 89 } |
| 90 |
| 91 // Get Surface Information. |
| 92 VkSurfaceCapabilitiesKHR surface_caps; |
| 93 result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR( |
| 94 GetVulkanPhysicalDevice(), surface_, &surface_caps); |
| 95 if (VK_SUCCESS != result) { |
| 96 DLOG(ERROR) << "vkGetPhysicalDeviceSurfaceCapabilitiesKHR() failed: " |
| 97 << result; |
| 98 return false; |
| 99 } |
| 100 |
| 101 // These are actual surfaces so the current extent should be defined. |
| 102 DCHECK_NE(UINT_MAX, surface_caps.currentExtent.width); |
| 103 DCHECK_NE(UINT_MAX, surface_caps.currentExtent.height); |
| 104 size_ = gfx::Size(surface_caps.currentExtent.width, |
| 105 surface_caps.currentExtent.height); |
| 106 |
| 107 // Create Swapchain. |
| 108 if (!swap_chain_.Initialize(surface_, surface_caps, surface_format_)) |
| 109 return false; |
| 110 |
| 111 return true; |
| 112 } |
| 113 |
| 114 void Destroy() override { |
| 115 swap_chain_.Destroy(); |
| 116 vkDestroySurfaceKHR(GetVulkanInstance(), surface_, nullptr); |
| 117 surface_ = VK_NULL_HANDLE; |
| 118 } |
| 119 |
| 120 gfx::SwapResult SwapBuffers() override { return swap_chain_.SwapBuffers(); } |
| 121 VulkanSwapChain* GetSwapChain() override { return &swap_chain_; } |
| 122 void Finish() override { vkQueueWaitIdle(GetVulkanQueue()); } |
| 123 |
| 124 protected: |
| 125 gfx::AcceleratedWidget window_; |
| 126 gfx::Size size_; |
| 127 VkSurfaceKHR surface_ = VK_NULL_HANDLE; |
| 128 VkSurfaceFormatKHR surface_format_ = {}; |
| 129 VulkanSwapChain swap_chain_; |
| 130 }; |
12 | 131 |
13 // static | 132 // static |
14 bool VulkanSurface::InitializeOneOff() { | 133 bool VulkanSurface::InitializeOneOff() { |
15 if (!InitializeVulkan()) | 134 if (!InitializeVulkan()) |
16 return false; | 135 return false; |
17 | 136 |
18 return true; | 137 return true; |
19 } | 138 } |
20 | 139 |
21 VulkanSurface::~VulkanSurface() {} | 140 VulkanSurface::~VulkanSurface() {} |
22 | 141 |
| 142 // static |
| 143 scoped_ptr<VulkanSurface> VulkanSurface::CreateViewSurface( |
| 144 gfx::AcceleratedWidget window) { |
| 145 return scoped_ptr<VulkanSurface>(new VulkanWSISurface(window)); |
| 146 } |
| 147 |
| 148 VulkanSurface::VulkanSurface() {} |
| 149 |
23 } // namespace gpu | 150 } // namespace gpu |
OLD | NEW |