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