mesa/src/vulkan/wsi/wsi_common_win32.c

553 lines
17 KiB
C

/*
* Copyright © 2015 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "vk_format.h"
#include "vk_instance.h"
#include "vk_physical_device.h"
#include "vk_util.h"
#include "wsi_common_entrypoints.h"
#include "wsi_common_private.h"
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size
#endif
struct wsi_win32;
struct wsi_win32 {
struct wsi_interface base;
struct wsi_device *wsi;
const VkAllocationCallbacks *alloc;
VkPhysicalDevice physical_device;
};
struct wsi_win32_image {
struct wsi_image base;
struct wsi_win32_swapchain *chain;
HDC dc;
HBITMAP bmp;
int bmp_row_pitch;
void *ppvBits;
};
struct wsi_win32_swapchain {
struct wsi_swapchain base;
struct wsi_win32 *wsi;
VkIcdSurfaceWin32 *surface;
uint64_t flip_sequence;
VkResult status;
VkExtent2D extent;
HWND wnd;
HDC chain_dc;
struct wsi_win32_image images[0];
};
VKAPI_ATTR VkBool32 VKAPI_CALL
wsi_GetPhysicalDeviceWin32PresentationSupportKHR(VkPhysicalDevice physicalDevice,
uint32_t queueFamilyIndex)
{
return TRUE;
}
VKAPI_ATTR VkResult VKAPI_CALL
wsi_CreateWin32SurfaceKHR(VkInstance _instance,
const VkWin32SurfaceCreateInfoKHR *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
VkSurfaceKHR *pSurface)
{
VK_FROM_HANDLE(vk_instance, instance, _instance);
VkIcdSurfaceWin32 *surface;
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR);
surface = vk_zalloc2(&instance->alloc, pAllocator, sizeof(*surface), 8,
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
if (surface == NULL)
return VK_ERROR_OUT_OF_HOST_MEMORY;
surface->base.platform = VK_ICD_WSI_PLATFORM_WIN32;
surface->hinstance = pCreateInfo->hinstance;
surface->hwnd = pCreateInfo->hwnd;
*pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
return VK_SUCCESS;
}
static VkResult
wsi_win32_surface_get_support(VkIcdSurfaceBase *surface,
struct wsi_device *wsi_device,
uint32_t queueFamilyIndex,
VkBool32* pSupported)
{
*pSupported = true;
return VK_SUCCESS;
}
static VkResult
wsi_win32_surface_get_capabilities(VkIcdSurfaceBase *surf,
struct wsi_device *wsi_device,
VkSurfaceCapabilitiesKHR* caps)
{
VkIcdSurfaceWin32 *surface = (VkIcdSurfaceWin32 *)surf;
RECT win_rect;
if (!GetClientRect(surface->hwnd, &win_rect))
return VK_ERROR_SURFACE_LOST_KHR;
caps->minImageCount = 1;
/* There is no real maximum */
caps->maxImageCount = 0;
caps->currentExtent = (VkExtent2D) {
win_rect.right - win_rect.left,
win_rect.bottom - win_rect.top
};
caps->minImageExtent = (VkExtent2D) { 1, 1 };
caps->maxImageExtent = (VkExtent2D) {
wsi_device->maxImageDimension2D,
wsi_device->maxImageDimension2D,
};
caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
caps->maxImageArrayLayers = 1;
caps->supportedCompositeAlpha =
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
caps->supportedUsageFlags =
VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_STORAGE_BIT |
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
return VK_SUCCESS;
}
static VkResult
wsi_win32_surface_get_capabilities2(VkIcdSurfaceBase *surface,
struct wsi_device *wsi_device,
const void *info_next,
VkSurfaceCapabilities2KHR* caps)
{
assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
VkResult result =
wsi_win32_surface_get_capabilities(surface, wsi_device,
&caps->surfaceCapabilities);
vk_foreach_struct(ext, caps->pNext) {
switch (ext->sType) {
case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
VkSurfaceProtectedCapabilitiesKHR *protected = (void *)ext;
protected->supportsProtected = VK_FALSE;
break;
}
default:
/* Ignored */
break;
}
}
return result;
}
static const struct {
VkFormat format;
} available_surface_formats[] = {
{ .format = VK_FORMAT_B8G8R8A8_SRGB },
{ .format = VK_FORMAT_B8G8R8A8_UNORM },
};
static void
get_sorted_vk_formats(struct wsi_device *wsi_device, VkFormat *sorted_formats)
{
for (unsigned i = 0; i < ARRAY_SIZE(available_surface_formats); i++)
sorted_formats[i] = available_surface_formats[i].format;
if (wsi_device->force_bgra8_unorm_first) {
for (unsigned i = 0; i < ARRAY_SIZE(available_surface_formats); i++) {
if (sorted_formats[i] == VK_FORMAT_B8G8R8A8_UNORM) {
sorted_formats[i] = sorted_formats[0];
sorted_formats[0] = VK_FORMAT_B8G8R8A8_UNORM;
break;
}
}
}
}
static VkResult
wsi_win32_surface_get_formats(VkIcdSurfaceBase *icd_surface,
struct wsi_device *wsi_device,
uint32_t* pSurfaceFormatCount,
VkSurfaceFormatKHR* pSurfaceFormats)
{
VK_OUTARRAY_MAKE_TYPED(VkSurfaceFormatKHR, out, pSurfaceFormats, pSurfaceFormatCount);
VkFormat sorted_formats[ARRAY_SIZE(available_surface_formats)];
get_sorted_vk_formats(wsi_device, sorted_formats);
for (unsigned i = 0; i < ARRAY_SIZE(sorted_formats); i++) {
vk_outarray_append_typed(VkSurfaceFormatKHR, &out, f) {
f->format = sorted_formats[i];
f->colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
}
}
return vk_outarray_status(&out);
}
static VkResult
wsi_win32_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
struct wsi_device *wsi_device,
const void *info_next,
uint32_t* pSurfaceFormatCount,
VkSurfaceFormat2KHR* pSurfaceFormats)
{
VK_OUTARRAY_MAKE_TYPED(VkSurfaceFormat2KHR, out, pSurfaceFormats, pSurfaceFormatCount);
VkFormat sorted_formats[ARRAY_SIZE(available_surface_formats)];
get_sorted_vk_formats(wsi_device, sorted_formats);
for (unsigned i = 0; i < ARRAY_SIZE(sorted_formats); i++) {
vk_outarray_append_typed(VkSurfaceFormat2KHR, &out, f) {
assert(f->sType == VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR);
f->surfaceFormat.format = sorted_formats[i];
f->surfaceFormat.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
}
}
return vk_outarray_status(&out);
}
static const VkPresentModeKHR present_modes[] = {
//VK_PRESENT_MODE_MAILBOX_KHR,
VK_PRESENT_MODE_FIFO_KHR,
};
static VkResult
wsi_win32_surface_get_present_modes(VkIcdSurfaceBase *surface,
uint32_t* pPresentModeCount,
VkPresentModeKHR* pPresentModes)
{
if (pPresentModes == NULL) {
*pPresentModeCount = ARRAY_SIZE(present_modes);
return VK_SUCCESS;
}
*pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
if (*pPresentModeCount < ARRAY_SIZE(present_modes))
return VK_INCOMPLETE;
else
return VK_SUCCESS;
}
static VkResult
wsi_win32_surface_get_present_rectangles(VkIcdSurfaceBase *surface,
struct wsi_device *wsi_device,
uint32_t* pRectCount,
VkRect2D* pRects)
{
VK_OUTARRAY_MAKE_TYPED(VkRect2D, out, pRects, pRectCount);
vk_outarray_append_typed(VkRect2D, &out, rect) {
/* We don't know a size so just return the usual "I don't know." */
*rect = (VkRect2D) {
.offset = { 0, 0 },
.extent = { UINT32_MAX, UINT32_MAX },
};
}
return vk_outarray_status(&out);
}
static VkResult
wsi_win32_image_init(VkDevice device_h,
struct wsi_win32_swapchain *chain,
const VkSwapchainCreateInfoKHR *create_info,
const VkAllocationCallbacks *allocator,
struct wsi_win32_image *image)
{
assert(chain->base.use_buffer_blit);
VkResult result = wsi_create_image(&chain->base, &chain->base.image_info,
&image->base);
if (result != VK_SUCCESS)
return result;
VkIcdSurfaceWin32 *win32_surface = (VkIcdSurfaceWin32 *)create_info->surface;
chain->wnd = win32_surface->hwnd;
chain->chain_dc = GetDC(chain->wnd);
image->dc = CreateCompatibleDC(chain->chain_dc);
HBITMAP bmp = NULL;
BITMAPINFO info = { 0 };
info.bmiHeader.biSize = sizeof(BITMAPINFO);
info.bmiHeader.biWidth = create_info->imageExtent.width;
info.bmiHeader.biHeight = -create_info->imageExtent.height;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biCompression = BI_RGB;
bmp = CreateDIBSection(image->dc, &info, DIB_RGB_COLORS, &image->ppvBits, NULL, 0);
assert(bmp && image->ppvBits);
SelectObject(image->dc, bmp);
BITMAP header;
int status = GetObject(bmp, sizeof(BITMAP), &header);
(void)status;
image->bmp_row_pitch = header.bmWidthBytes;
image->bmp = bmp;
image->chain = chain;
return VK_SUCCESS;
}
static void
wsi_win32_image_finish(struct wsi_win32_swapchain *chain,
const VkAllocationCallbacks *allocator,
struct wsi_win32_image *image)
{
DeleteDC(image->dc);
if(image->bmp)
DeleteObject(image->bmp);
wsi_destroy_image(&chain->base, &image->base);
}
static VkResult
wsi_win32_swapchain_destroy(struct wsi_swapchain *drv_chain,
const VkAllocationCallbacks *allocator)
{
struct wsi_win32_swapchain *chain =
(struct wsi_win32_swapchain *) drv_chain;
for (uint32_t i = 0; i < chain->base.image_count; i++)
wsi_win32_image_finish(chain, allocator, &chain->images[i]);
DeleteDC(chain->chain_dc);
wsi_swapchain_finish(&chain->base);
vk_free(allocator, chain);
return VK_SUCCESS;
}
static struct wsi_image *
wsi_win32_get_wsi_image(struct wsi_swapchain *drv_chain,
uint32_t image_index)
{
struct wsi_win32_swapchain *chain =
(struct wsi_win32_swapchain *) drv_chain;
return &chain->images[image_index].base;
}
static VkResult
wsi_win32_acquire_next_image(struct wsi_swapchain *drv_chain,
const VkAcquireNextImageInfoKHR *info,
uint32_t *image_index)
{
struct wsi_win32_swapchain *chain =
(struct wsi_win32_swapchain *)drv_chain;
/* Bail early if the swapchain is broken */
if (chain->status != VK_SUCCESS)
return chain->status;
*image_index = 0;
return VK_SUCCESS;
}
static VkResult
wsi_win32_queue_present(struct wsi_swapchain *drv_chain,
uint32_t image_index,
const VkPresentRegionKHR *damage)
{
struct wsi_win32_swapchain *chain = (struct wsi_win32_swapchain *) drv_chain;
assert(image_index < chain->base.image_count);
struct wsi_win32_image *image = &chain->images[image_index];
assert(chain->base.use_buffer_blit);
char *ptr = image->base.cpu_map;
char *dptr = image->ppvBits;
for (unsigned h = 0; h < chain->extent.height; h++) {
memcpy(dptr, ptr, chain->extent.width * 4);
dptr += image->bmp_row_pitch;
ptr += image->base.row_pitches[0];
}
if (!StretchBlt(chain->chain_dc, 0, 0, chain->extent.width, chain->extent.height, image->dc, 0, 0, chain->extent.width, chain->extent.height, SRCCOPY))
chain->status = VK_ERROR_MEMORY_MAP_FAILED;
return chain->status;
}
static VkResult
wsi_win32_surface_create_swapchain(
VkIcdSurfaceBase *icd_surface,
VkDevice device,
struct wsi_device *wsi_device,
const VkSwapchainCreateInfoKHR *create_info,
const VkAllocationCallbacks *allocator,
struct wsi_swapchain **swapchain_out)
{
VkIcdSurfaceWin32 *surface = (VkIcdSurfaceWin32 *)icd_surface;
struct wsi_win32 *wsi =
(struct wsi_win32 *) wsi_device->wsi[VK_ICD_WSI_PLATFORM_WIN32];
assert(create_info->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
const unsigned num_images = create_info->minImageCount;
struct wsi_win32_swapchain *chain;
size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
chain = vk_zalloc(allocator, size,
8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
if (chain == NULL)
return VK_ERROR_OUT_OF_HOST_MEMORY;
VkResult result = wsi_swapchain_init(wsi_device, &chain->base, device,
create_info, allocator, false);
if (result != VK_SUCCESS) {
vk_free(allocator, chain);
return result;
}
chain->base.destroy = wsi_win32_swapchain_destroy;
chain->base.get_wsi_image = wsi_win32_get_wsi_image;
chain->base.acquire_next_image = wsi_win32_acquire_next_image;
chain->base.queue_present = wsi_win32_queue_present;
chain->base.present_mode = wsi_swapchain_get_present_mode(wsi_device, create_info);
chain->base.image_count = num_images;
chain->extent = create_info->imageExtent;
chain->wsi = wsi;
chain->status = VK_SUCCESS;
chain->surface = surface;
assert(wsi_device->sw);
chain->base.use_buffer_blit = true;
result = wsi_configure_cpu_image(&chain->base, create_info, NULL /*alloc_shm*/, &chain->base.image_info);
if (result != VK_SUCCESS) {
vk_free(allocator, chain);
goto fail_init_images;
}
for (uint32_t image = 0; image < chain->base.image_count; image++) {
result = wsi_win32_image_init(device, chain,
create_info, allocator,
&chain->images[image]);
if (result != VK_SUCCESS) {
while (image > 0) {
--image;
wsi_win32_image_finish(chain, allocator,
&chain->images[image]);
}
wsi_destroy_image_info(&chain->base, &chain->base.image_info);
vk_free(allocator, chain);
goto fail_init_images;
}
}
*swapchain_out = &chain->base;
return VK_SUCCESS;
fail_init_images:
return result;
}
VkResult
wsi_win32_init_wsi(struct wsi_device *wsi_device,
const VkAllocationCallbacks *alloc,
VkPhysicalDevice physical_device)
{
struct wsi_win32 *wsi;
VkResult result;
wsi = vk_alloc(alloc, sizeof(*wsi), 8,
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (!wsi) {
result = VK_ERROR_OUT_OF_HOST_MEMORY;
goto fail;
}
wsi->physical_device = physical_device;
wsi->alloc = alloc;
wsi->wsi = wsi_device;
wsi->base.get_support = wsi_win32_surface_get_support;
wsi->base.get_capabilities2 = wsi_win32_surface_get_capabilities2;
wsi->base.get_formats = wsi_win32_surface_get_formats;
wsi->base.get_formats2 = wsi_win32_surface_get_formats2;
wsi->base.get_present_modes = wsi_win32_surface_get_present_modes;
wsi->base.get_present_rectangles = wsi_win32_surface_get_present_rectangles;
wsi->base.create_swapchain = wsi_win32_surface_create_swapchain;
wsi_device->wsi[VK_ICD_WSI_PLATFORM_WIN32] = &wsi->base;
return VK_SUCCESS;
fail:
wsi_device->wsi[VK_ICD_WSI_PLATFORM_WIN32] = NULL;
return result;
}
void
wsi_win32_finish_wsi(struct wsi_device *wsi_device,
const VkAllocationCallbacks *alloc)
{
struct wsi_win32 *wsi =
(struct wsi_win32 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WIN32];
if (!wsi)
return;
vk_free(alloc, wsi);
}