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 <Windows.h>
10 #include "WindowContextFactory_win.h"
11
12 #include "../VulkanWindowContext.h"
13 #include "Window_win.h"
14
15 #include "vk/GrVkInterface.h"
16 #include "vk/GrVkUtil.h"
17
18 namespace sk_app {
19 namespace window_context_factory {
20
NewVulkanForWin(HWND hwnd,const DisplayParams & params)21 WindowContext* NewVulkanForWin(HWND hwnd, const DisplayParams& params) {
22 auto createVkSurface = [hwnd] (VkInstance instance) -> VkSurfaceKHR {
23 static PFN_vkCreateWin32SurfaceKHR createWin32SurfaceKHR = nullptr;
24 if (!createWin32SurfaceKHR) {
25 createWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR)
26 vkGetInstanceProcAddr(instance, "vkCreateWin32SurfaceKHR");
27 }
28 HINSTANCE hinstance = GetModuleHandle(0);
29 VkSurfaceKHR surface;
30
31 VkWin32SurfaceCreateInfoKHR surfaceCreateInfo;
32 memset(&surfaceCreateInfo, 0, sizeof(VkWin32SurfaceCreateInfoKHR));
33 surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
34 surfaceCreateInfo.pNext = nullptr;
35 surfaceCreateInfo.flags = 0;
36 surfaceCreateInfo.hinstance = hinstance;
37 surfaceCreateInfo.hwnd = hwnd;
38
39 VkResult res = createWin32SurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface);
40 if (VK_SUCCESS != res) {
41 return VK_NULL_HANDLE;
42 }
43
44 return surface;
45 };
46
47 auto canPresent = [hwnd] (VkInstance instance, VkPhysicalDevice physDev,
48 uint32_t queueFamilyIndex) {
49 static PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR
50 getPhysicalDeviceWin32PresentationSupportKHR = nullptr;
51 if (!getPhysicalDeviceWin32PresentationSupportKHR) {
52 getPhysicalDeviceWin32PresentationSupportKHR =
53 (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)
54 vkGetInstanceProcAddr(instance,
55 "vkGetPhysicalDeviceWin32PresentationSupportKHR");
56 }
57
58 VkBool32 check = getPhysicalDeviceWin32PresentationSupportKHR(physDev, queueFamilyIndex);
59 return (VK_FALSE != check);
60 };
61
62 WindowContext* ctx = new VulkanWindowContext(params, createVkSurface, canPresent);
63 if (!ctx->isValid()) {
64 delete ctx;
65 return nullptr;
66 }
67 return ctx;
68 }
69
70 } // namespace window_context_factory
71 } // namespace sk_app
72