<commit_before>#include <algorithm>
#include "swap_chain.hpp"
#include "texture.hpp"
#include "texture_format.hpp"
#include "framebuffer.hpp"

#ifdef __unix__
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#endif

namespace AgpuVulkan
{
using namespace AgpuCommon;

AVkSwapChain::AVkSwapChain(const agpu::device_ref &device)
    : device(device)
{
    surface = VK_NULL_HANDLE;
    handle = VK_NULL_HANDLE;
    currentBackBufferIndex = 0;
}

AVkSwapChain::~AVkSwapChain()
{
    for(auto semaphore : semaphores)
    {
        if(semaphore)
            vkDestroySemaphore(deviceForVk->device, semaphore, nullptr);
    }

    if (handle)
        vkDestroySwapchainKHR(deviceForVk->device, handle, nullptr);
    if(surface)
        vkDestroySurfaceKHR(deviceForVk->vulkanInstance, surface, nullptr);
}

static VkResult createXlibSurface(const agpu::device_ref &device, const agpu::command_queue_ref &graphicsCommandQueue, agpu_swap_chain_create_info *createInfo, const OverlaySwapChainWindowPtr& overlayWindow, VkSurfaceKHR *surface)
{
#ifdef __unix__
    if(!deviceForVk->displayHandle)
        deviceForVk->displayHandle = XOpenDisplay(nullptr);

    if (!deviceForVk->displayHandle || !createInfo->window)
        return VK_INCOMPLETE;

    VkXcbSurfaceCreateInfoKHR surfaceCreateInfo;
    memset(&surfaceCreateInfo, 0, sizeof(surfaceCreateInfo));
    surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
    surfaceCreateInfo.connection = XGetXCBConnection((Display*)deviceForVk->displayHandle);
    surfaceCreateInfo.window = (xcb_window_t)(uintptr_t)createInfo->window;

    return vkCreateXcbSurfaceKHR(deviceForVk->vulkanInstance, &surfaceCreateInfo, nullptr, surface);
#else
    return VK_INCOMPLETE;
#endif
}

static VkResult createXcbSurface(const agpu::device_ref &device, const agpu::command_queue_ref &graphicsCommandQueue, agpu_swap_chain_create_info *createInfo, const OverlaySwapChainWindowPtr& overlayWindow, VkSurfaceKHR *surface)
{
#ifdef __unix__
    if(!deviceForVk->displayHandle)
        deviceForVk->displayHandle = xcb_connect(nullptr, nullptr);

    if (!deviceForVk->displayHandle || !createInfo->window)
        return VK_INCOMPLETE;

    VkXcbSurfaceCreateInfoKHR surfaceCreateInfo;
    memset(&surfaceCreateInfo, 0, sizeof(surfaceCreateInfo));
    surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
    surfaceCreateInfo.connection = XGetXCBConnection((Display*)deviceForVk->displayHandle);
    surfaceCreateInfo.window = (xcb_window_t)(uintptr_t)createInfo->window;

    return vkCreateXcbSurfaceKHR(deviceForVk->vulkanInstance, &surfaceCreateInfo, nullptr, surface);
#else
    return VK_INCOMPLETE;
#endif
}

static VkResult createWin32Surface(const agpu::device_ref &device, const agpu::command_queue_ref &graphicsCommandQueue, agpu_swap_chain_create_info *createInfo, const OverlaySwapChainWindowPtr &overlayWindow, VkSurfaceKHR *surface)
{
#ifdef _WIN32
    if (!createInfo->window)
        return VK_INCOMPLETE;
    VkWin32SurfaceCreateInfoKHR surfaceCreateInfo;
    memset(&surfaceCreateInfo, 0, sizeof(surfaceCreateInfo));
    surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
    surfaceCreateInfo.hinstance = GetModuleHandle(nullptr);
    surfaceCreateInfo.hwnd = (HWND)createInfo->window;
    if(overlayWindow)
        surfaceCreateInfo.hwnd = (HWND)overlayWindow->getWindowHandle();<commit_msg>Use the display that is passed by the swap chain creation info in the vulkan backend.<commit_after>#include <algorithm>
#include "swap_chain.hpp"
#include "texture.hpp"
#include "texture_format.hpp"
#include "framebuffer.hpp"

#ifdef __unix__
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#endif

namespace AgpuVulkan
{
using namespace AgpuCommon;

AVkSwapChain::AVkSwapChain(const agpu::device_ref &device)
    : device(device)
{
    surface = VK_NULL_HANDLE;
    handle = VK_NULL_HANDLE;
    currentBackBufferIndex = 0;
}

AVkSwapChain::~AVkSwapChain()
{
    for(auto semaphore : semaphores)
    {
        if(semaphore)
            vkDestroySemaphore(deviceForVk->device, semaphore, nullptr);
    }

    if (handle)
        vkDestroySwapchainKHR(deviceForVk->device, handle, nullptr);
    if(surface)
        vkDestroySurfaceKHR(deviceForVk->vulkanInstance, surface, nullptr);
}

static VkResult createXlibSurface(const agpu::device_ref &device, const agpu::command_queue_ref &graphicsCommandQueue, agpu_swap_chain_create_info *createInfo, const OverlaySwapChainWindowPtr& overlayWindow, VkSurfaceKHR *surface)
{
#ifdef __unix__
    auto displayHandle = createInfo->display;
    if(!displayHandle)
    {
        if(!deviceForVk->displayHandle)
            deviceForVk->displayHandle = XOpenDisplay(nullptr);
        displayHandle = deviceForVk->displayHandle;
    }

    if (!displayHandle || !createInfo->window)
        return VK_INCOMPLETE;

    VkXcbSurfaceCreateInfoKHR surfaceCreateInfo;
    memset(&surfaceCreateInfo, 0, sizeof(surfaceCreateInfo));
    surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
    surfaceCreateInfo.connection = XGetXCBConnection((Display*)displayHandle);
    surfaceCreateInfo.window = (xcb_window_t)(uintptr_t)createInfo->window;

    return vkCreateXcbSurfaceKHR(deviceForVk->vulkanInstance, &surfaceCreateInfo, nullptr, surface);
#else
    return VK_INCOMPLETE;
#endif
}

static VkResult createXcbSurface(const agpu::device_ref &device, const agpu::command_queue_ref &graphicsCommandQueue, agpu_swap_chain_create_info *createInfo, const OverlaySwapChainWindowPtr& overlayWindow, VkSurfaceKHR *surface)
{
#ifdef __unix__
    auto displayHandle = createInfo->display;
    if(!displayHandle)
    {
        if(!deviceForVk->displayHandle)
            deviceForVk->displayHandle = xcb_connect(nullptr, nullptr);
        displayHandle = deviceForVk->displayHandle;
    }

    if (!displayHandle || !createInfo->window)
        return VK_INCOMPLETE;

    VkXcbSurfaceCreateInfoKHR surfaceCreateInfo;
    memset(&surfaceCreateInfo, 0, sizeof(surfaceCreateInfo));
    surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
    surfaceCreateInfo.connection = (xcb_connection_t*)displayHandle;
    surfaceCreateInfo.window = (xcb_window_t)(uintptr_t)createInfo->window;

    return vkCreateXcbSurfaceKHR(deviceForVk->vulkanInstance, &surfaceCreateInfo, nullptr, surface);
#else
    return VK_INCOMPLETE;
#endif
}

static VkResult createWin32Surface(const agpu::device_ref &device, const agpu::command_queue_ref &graphicsCommandQueue, agpu_swap_chain_create_info *createInfo, const OverlaySwapChainWindowPtr &overlayWindow, VkSurfaceKHR *surface)
{
#ifdef _WIN32
    if (!createInfo->window)
        return VK_INCOMPLETE;
    VkWin32SurfaceCreateInfoKHR surfaceCreateInfo;
    memset(&surfaceCreateInfo, 0, sizeof(surfaceCreateInfo));
    surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
    surfaceCreateInfo.hinstance = GetModuleHandle(nullptr);
    surfaceCreateInfo.hwnd = (HWND)createInfo->window;
    if(overlayWindow)
        surfaceCreateInfo.hwnd = (HWND)overlayWindow->getWindowHandle();
{'fraction_non_alphanumeric': 0.09233154778686213, 'fraction_numerical': 0.004510854243022272, 'mean_word_length': 5.712393566698203, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6944052', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2312, 'n_words': 376}