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 "include/gpu/vk/GrVkVulkan.h"
10
11 #include "tools/sk_app/win/WindowContextFactory_win.h"
12
13 #include "tools/sk_app/VulkanWindowContext.h"
14 #include "tools/sk_app/win/Window_win.h"
15
16 #include "src/gpu/vk/GrVkInterface.h"
17 #include "src/gpu/vk/GrVkUtil.h"
18
19 #include "tools/gpu/vk/VkTestUtils.h"
20
21 #include <Windows.h>
22
23 namespace sk_app {
24 namespace window_context_factory {
25
MakeVulkanForWin(HWND hwnd,const DisplayParams & params)26 std::unique_ptr<WindowContext> MakeVulkanForWin(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 std::unique_ptr<WindowContext> ctx(
73 new VulkanWindowContext(params, createVkSurface, canPresent, instProc, devProc));
74 if (!ctx->isValid()) {
75 return nullptr;
76 }
77 return ctx;
78 }
79
80 } // namespace window_context_factory
81 } // namespace sk_app
82