Home | History | Annotate | Download | only in win
      1 
      2 /*
      3  * Copyright 2016 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 #include "vk/GrVkVulkan.h"
     10 
     11 #include "WindowContextFactory_win.h"
     12 
     13 #include "../VulkanWindowContext.h"
     14 #include "Window_win.h"
     15 
     16 #include "vk/GrVkInterface.h"
     17 #include "vk/GrVkUtil.h"
     18 
     19 #include "vk/VkTestUtils.h"
     20 
     21 #include <Windows.h>
     22 
     23 namespace sk_app {
     24 namespace window_context_factory {
     25 
     26 WindowContext* NewVulkanForWin(HWND hwnd, const DisplayParams& params) {
     27     PFN_vkGetInstanceProcAddr instProc;
     28     PFN_vkGetDeviceProcAddr devProc;
     29     if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
     30         return nullptr;
     31     }
     32 
     33     auto createVkSurface = [hwnd, instProc] (VkInstance instance) -> VkSurfaceKHR {
     34         static PFN_vkCreateWin32SurfaceKHR createWin32SurfaceKHR = nullptr;
     35         if (!createWin32SurfaceKHR) {
     36             createWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR)
     37                 instProc(instance, "vkCreateWin32SurfaceKHR");
     38         }
     39         HINSTANCE hinstance = GetModuleHandle(0);
     40         VkSurfaceKHR surface;
     41 
     42         VkWin32SurfaceCreateInfoKHR surfaceCreateInfo;
     43         memset(&surfaceCreateInfo, 0, sizeof(VkWin32SurfaceCreateInfoKHR));
     44         surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
     45         surfaceCreateInfo.pNext = nullptr;
     46         surfaceCreateInfo.flags = 0;
     47         surfaceCreateInfo.hinstance = hinstance;
     48         surfaceCreateInfo.hwnd = hwnd;
     49 
     50         VkResult res = createWin32SurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface);
     51         if (VK_SUCCESS != res) {
     52             return VK_NULL_HANDLE;
     53         }
     54 
     55         return surface;
     56     };
     57 
     58     auto canPresent = [instProc] (VkInstance instance, VkPhysicalDevice physDev,
     59                                   uint32_t queueFamilyIndex) {
     60         static PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR
     61                                             getPhysicalDeviceWin32PresentationSupportKHR = nullptr;
     62         if (!getPhysicalDeviceWin32PresentationSupportKHR) {
     63             getPhysicalDeviceWin32PresentationSupportKHR =
     64                 (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)
     65                     instProc(instance, "vkGetPhysicalDeviceWin32PresentationSupportKHR");
     66         }
     67 
     68         VkBool32 check = getPhysicalDeviceWin32PresentationSupportKHR(physDev, queueFamilyIndex);
     69         return (VK_FALSE != check);
     70     };
     71 
     72     WindowContext* ctx = new VulkanWindowContext(params, createVkSurface, canPresent,
     73                                                  instProc, devProc);
     74     if (!ctx->isValid()) {
     75         delete ctx;
     76         return nullptr;
     77     }
     78     return ctx;
     79 }
     80 
     81 }  // namespace window_context_factory
     82 }  // namespace sk_app
     83