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