xref: /aosp_15_r20/external/skia/tools/window/android/GraphiteVulkanWindowContext_android.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2023 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/android/WindowContextFactory_android.h"
9*c8dee2aaSAndroid Build Coastguard Worker 
10*c8dee2aaSAndroid Build Coastguard Worker #include "tools/gpu/vk/VkTestUtils.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/DisplayParams.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/GraphiteNativeVulkanWindowContext.h"
13*c8dee2aaSAndroid Build Coastguard Worker 
14*c8dee2aaSAndroid Build Coastguard Worker using skwindow::DisplayParams;
15*c8dee2aaSAndroid Build Coastguard Worker 
16*c8dee2aaSAndroid Build Coastguard Worker namespace skwindow {
17*c8dee2aaSAndroid Build Coastguard Worker 
MakeGraphiteVulkanForAndroid(ANativeWindow * window,std::unique_ptr<const DisplayParams> params)18*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<WindowContext> MakeGraphiteVulkanForAndroid(
19*c8dee2aaSAndroid Build Coastguard Worker         ANativeWindow* window, std::unique_ptr<const DisplayParams> params) {
20*c8dee2aaSAndroid Build Coastguard Worker     PFN_vkGetInstanceProcAddr instProc;
21*c8dee2aaSAndroid Build Coastguard Worker     if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) {
22*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
23*c8dee2aaSAndroid Build Coastguard Worker     }
24*c8dee2aaSAndroid Build Coastguard Worker 
25*c8dee2aaSAndroid Build Coastguard Worker     auto createVkSurface = [window, instProc] (VkInstance instance) -> VkSurfaceKHR {
26*c8dee2aaSAndroid Build Coastguard Worker         PFN_vkCreateAndroidSurfaceKHR createAndroidSurfaceKHR =
27*c8dee2aaSAndroid Build Coastguard Worker                 (PFN_vkCreateAndroidSurfaceKHR) instProc(instance, "vkCreateAndroidSurfaceKHR");
28*c8dee2aaSAndroid Build Coastguard Worker 
29*c8dee2aaSAndroid Build Coastguard Worker         if (!window) {
30*c8dee2aaSAndroid Build Coastguard Worker             return VK_NULL_HANDLE;
31*c8dee2aaSAndroid Build Coastguard Worker         }
32*c8dee2aaSAndroid Build Coastguard Worker         VkSurfaceKHR surface;
33*c8dee2aaSAndroid Build Coastguard Worker 
34*c8dee2aaSAndroid Build Coastguard Worker         VkAndroidSurfaceCreateInfoKHR surfaceCreateInfo;
35*c8dee2aaSAndroid Build Coastguard Worker         memset(&surfaceCreateInfo, 0, sizeof(VkAndroidSurfaceCreateInfoKHR));
36*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
37*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.pNext = nullptr;
38*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.flags = 0;
39*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.window = window;
40*c8dee2aaSAndroid Build Coastguard Worker 
41*c8dee2aaSAndroid Build Coastguard Worker         VkResult res = createAndroidSurfaceKHR(instance, &surfaceCreateInfo,
42*c8dee2aaSAndroid Build Coastguard Worker                                                nullptr, &surface);
43*c8dee2aaSAndroid Build Coastguard Worker         return (VK_SUCCESS == res) ? surface : VK_NULL_HANDLE;
44*c8dee2aaSAndroid Build Coastguard Worker     };
45*c8dee2aaSAndroid Build Coastguard Worker 
46*c8dee2aaSAndroid Build Coastguard Worker     auto canPresent = [](VkInstance, VkPhysicalDevice, uint32_t) { return true; };
47*c8dee2aaSAndroid Build Coastguard Worker 
48*c8dee2aaSAndroid Build Coastguard Worker     std::unique_ptr<WindowContext> ctx(new internal::GraphiteVulkanWindowContext(
49*c8dee2aaSAndroid Build Coastguard Worker             std::move(params), createVkSurface, canPresent, instProc));
50*c8dee2aaSAndroid Build Coastguard Worker     if (!ctx->isValid()) {
51*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
52*c8dee2aaSAndroid Build Coastguard Worker     }
53*c8dee2aaSAndroid Build Coastguard Worker 
54*c8dee2aaSAndroid Build Coastguard Worker     return ctx;
55*c8dee2aaSAndroid Build Coastguard Worker }
56*c8dee2aaSAndroid Build Coastguard Worker 
57*c8dee2aaSAndroid Build Coastguard Worker }  // namespace skwindow
58