1 //
2 // Copyright 2020 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // WindowSurfaceVkHeadless.cpp:
7 //    Implements the class methods for WindowSurfaceVkHeadless.
8 //
9 
10 #include "WindowSurfaceVkHeadless.h"
11 #include "libANGLE/renderer/vulkan/RendererVk.h"
12 
13 namespace rx
14 {
15 
WindowSurfaceVkHeadless(const egl::SurfaceState & surfaceState,EGLNativeWindowType window)16 WindowSurfaceVkHeadless::WindowSurfaceVkHeadless(const egl::SurfaceState &surfaceState,
17                                                  EGLNativeWindowType window)
18     : WindowSurfaceVk(surfaceState, window)
19 {}
20 
~WindowSurfaceVkHeadless()21 WindowSurfaceVkHeadless::~WindowSurfaceVkHeadless() {}
22 
createSurfaceVk(vk::Context * context,gl::Extents * extentsOut)23 angle::Result WindowSurfaceVkHeadless::createSurfaceVk(vk::Context *context,
24                                                        gl::Extents *extentsOut)
25 {
26     RendererVk *renderer = context->getRenderer();
27     ASSERT(renderer != nullptr);
28     VkInstance instance = renderer->getInstance();
29 
30     VkHeadlessSurfaceCreateInfoEXT createInfo = {};
31     createInfo.sType                          = VK_STRUCTURE_TYPE_HEADLESS_SURFACE_CREATE_INFO_EXT;
32 
33     ANGLE_VK_TRY(context, vkCreateHeadlessSurfaceEXT(instance, &createInfo, nullptr, &mSurface));
34 
35     return getCurrentWindowSize(context, extentsOut);
36 }
37 
getCurrentWindowSize(vk::Context * context,gl::Extents * extentsOut)38 angle::Result WindowSurfaceVkHeadless::getCurrentWindowSize(vk::Context *context,
39                                                             gl::Extents *extentsOut)
40 {
41     const VkPhysicalDevice &physicalDevice = context->getRenderer()->getPhysicalDevice();
42     ANGLE_VK_TRY(context, vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, mSurface,
43                                                                     &mSurfaceCaps));
44 
45     // Spec: "For headless surfaces, currentExtent is the reserved value (0xFFFFFFFF, 0xFFFFFFFF).
46     // Whatever the application sets a swapchain's imageExtent to will be the size of the surface,
47     // after the first image is presented."
48     // For ANGLE, in headless mode, we share the same 'SimpleDisplayWindow' structure with front
49     // EGL window info to define the vulkan backend surface/image extents.
50     angle::vk::SimpleDisplayWindow *simpleWindow =
51         reinterpret_cast<angle::vk::SimpleDisplayWindow *>(mNativeWindowType);
52 
53     // Update surface extent before output the new extent.
54     mSurfaceCaps.currentExtent.width  = simpleWindow->width;
55     mSurfaceCaps.currentExtent.height = simpleWindow->height;
56 
57     *extentsOut =
58         gl::Extents(mSurfaceCaps.currentExtent.width, mSurfaceCaps.currentExtent.height, 1);
59 
60     return angle::Result::Continue;
61 }
62 
63 }  // namespace rx
64