mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-07 19:58:32 +09:00
[Fix] (MG_Backend/DirectVulkan): Avoid directly using ANativeWindow in abstracted layers.
This commit is contained in:
@@ -14,7 +14,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
void VulkanContext::Initialize(ANativeWindow* window, const std::string& appName) {
|
||||
void VulkanContext::Initialize(NativeWindowType window, const std::string& appName) {
|
||||
if (Initialized) return;
|
||||
CreateInstance(appName);
|
||||
CreateSurface(window);
|
||||
@@ -58,11 +58,20 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
ThrowIfFailed(vkCreateInstance(&ci, nullptr, &Instance), "vkCreateInstance failed");
|
||||
}
|
||||
|
||||
void VulkanContext::CreateSurface(ANativeWindow* window) {
|
||||
void VulkanContext::CreateSurface(NativeWindowType window) {
|
||||
#if __ANDROID__
|
||||
if (!Instance) throw MobileGL::RuntimeError("Instance not created");
|
||||
|
||||
auto* nativeWindow = static_cast<ANativeWindow*>(window);
|
||||
if (!nativeWindow) throw MobileGL::RuntimeError("ANativeWindowType is null");
|
||||
|
||||
VkAndroidSurfaceCreateInfoKHR sci{VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR};
|
||||
sci.window = window;
|
||||
sci.window = nativeWindow;
|
||||
ThrowIfFailed(vkCreateAndroidSurfaceKHR(Instance, &sci, nullptr, &Surface), "vkCreateAndroidSurfaceKHR failed");
|
||||
#else
|
||||
MGLOG_W("VulkanRenderer::Initialize called on a platform which is not supported yet"); // TODO: support more
|
||||
// platforms
|
||||
#endif
|
||||
}
|
||||
|
||||
void VulkanContext::PickPhysicalDevice() {
|
||||
@@ -110,4 +119,4 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
vkGetDeviceQueue(Device, GraphicsQueueFamily, 0, &GraphicsQueue);
|
||||
MGLOG_D("Logical device created");
|
||||
}
|
||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
VulkanContext() = default;
|
||||
~VulkanContext();
|
||||
|
||||
void Initialize(ANativeWindow* window, const std::string& appName = "VulkanEngineApp");
|
||||
void Initialize(NativeWindowType window, const std::string& appName = "MobileGL-VulkanRenderer");
|
||||
void Shutdown();
|
||||
|
||||
VkInstance GetInstance() const { return Instance; }
|
||||
@@ -34,7 +34,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
|
||||
private:
|
||||
void CreateInstance(const std::string& appName);
|
||||
void CreateSurface(ANativeWindow* window);
|
||||
void CreateSurface(NativeWindowType window);
|
||||
void PickPhysicalDevice();
|
||||
void CreateLogicalDevice();
|
||||
|
||||
@@ -46,4 +46,4 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
VkSurfaceKHR Surface = VK_NULL_HANDLE;
|
||||
bool Initialized = false;
|
||||
};
|
||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "FrameContext.h"
|
||||
|
||||
namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
VulkanRenderer::VulkanRenderer(ANativeWindow* window, const RendererConfig& cfg) : Window(window), Config(cfg) {
|
||||
VulkanRenderer::VulkanRenderer(NativeWindowType window, const RendererConfig& cfg) : Window(window), Config(cfg) {
|
||||
Ctx = std::make_unique<VulkanContext>();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
}
|
||||
|
||||
void VulkanRenderer::Initialize() {
|
||||
if (!Window) throw MobileGL::RuntimeError("ANativeWindow is null");
|
||||
Ctx->Initialize(Window, Config.AppName);
|
||||
|
||||
Swapchain = std::make_unique<SwapchainManager>(*Ctx);
|
||||
@@ -271,4 +270,4 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
void VulkanRenderer::WaitIdle() {
|
||||
if (Ctx && Ctx->GetDevice() != VK_NULL_HANDLE) vkDeviceWaitIdle(Ctx->GetDevice());
|
||||
}
|
||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
|
||||
class VulkanRenderer {
|
||||
public:
|
||||
VulkanRenderer(ANativeWindow* window, const RendererConfig& cfg = {});
|
||||
VulkanRenderer(NativeWindowType window, const RendererConfig& cfg = {});
|
||||
~VulkanRenderer();
|
||||
|
||||
void Initialize();
|
||||
@@ -43,7 +43,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
void WaitIdle();
|
||||
|
||||
private:
|
||||
ANativeWindow* Window = nullptr;
|
||||
NativeWindowType Window = 0;
|
||||
RendererConfig Config;
|
||||
|
||||
std::unique_ptr<VulkanContext> Ctx;
|
||||
|
||||
@@ -86,25 +86,17 @@ namespace MobileGL {
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
void CreateWindowSurfaceForVulkan(ANativeWindow* window) {
|
||||
void CreateWindowSurfaceForVulkan(NativeWindowType window) {
|
||||
MG_Backend::DirectVulkan::pVulkanRenderer = MakeUnique<MG_Backend::DirectVulkan::VulkanRenderer>(window);
|
||||
MG_Backend::DirectVulkan::pVulkanRenderer->Initialize();
|
||||
|
||||
PrepareDemoRes(); // for demo use
|
||||
}
|
||||
#endif
|
||||
|
||||
EGLSurface CreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window,
|
||||
const EGLint* attrib_list) {
|
||||
MGLOG_D("EGLForVulkan::CreateWindowSurface called with window=%p", window);
|
||||
#ifdef __ANDROID__
|
||||
auto* nativeWindow = static_cast<ANativeWindow*>(window);
|
||||
CreateWindowSurfaceForVulkan(nativeWindow);
|
||||
#else
|
||||
MGLOG_W("EGLForVulkan::CreateWindowSurface is not implemented for this platform"); // TODO: support more
|
||||
// platforms
|
||||
#endif
|
||||
CreateWindowSurfaceForVulkan(window);
|
||||
return (EGLSurface)1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user