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_android.h"
12 
13 #include "../VulkanWindowContext.h"
14 
15 #include "vk/VkTestUtils.h"
16 
17 namespace sk_app {
18 
19 namespace window_context_factory {
20 
NewVulkanForAndroid(ANativeWindow * window,const DisplayParams & params)21 WindowContext* NewVulkanForAndroid(ANativeWindow* window, const DisplayParams& params) {
22     PFN_vkGetInstanceProcAddr instProc;
23     PFN_vkGetDeviceProcAddr devProc;
24     if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
25         return nullptr;
26     }
27 
28     auto createVkSurface = [window, instProc] (VkInstance instance) -> VkSurfaceKHR {
29         PFN_vkCreateAndroidSurfaceKHR createAndroidSurfaceKHR =
30                 (PFN_vkCreateAndroidSurfaceKHR) instProc(instance, "vkCreateAndroidSurfaceKHR");
31 
32         if (!window) {
33             return VK_NULL_HANDLE;
34         }
35         VkSurfaceKHR surface;
36 
37         VkAndroidSurfaceCreateInfoKHR surfaceCreateInfo;
38         memset(&surfaceCreateInfo, 0, sizeof(VkAndroidSurfaceCreateInfoKHR));
39         surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
40         surfaceCreateInfo.pNext = nullptr;
41         surfaceCreateInfo.flags = 0;
42         surfaceCreateInfo.window = window;
43 
44         VkResult res = createAndroidSurfaceKHR(instance, &surfaceCreateInfo,
45                                                nullptr, &surface);
46         return (VK_SUCCESS == res) ? surface : VK_NULL_HANDLE;
47     };
48 
49     auto canPresent = [](VkInstance, VkPhysicalDevice, uint32_t) { return true; };
50 
51     WindowContext* ctx = new VulkanWindowContext(params, createVkSurface, canPresent,
52                                                  instProc, devProc);
53     if (!ctx->isValid()) {
54         delete ctx;
55         return nullptr;
56     }
57     return ctx;
58 }
59 
60 }  // namespace window_context_factory
61 }  // namespace sk_app
62