Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android window destruction when suspending #16363

Merged
merged 4 commits into from Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.cpp
Expand Up @@ -268,6 +268,9 @@ namespace AZ
Render::Bootstrap::DefaultWindowBus::Handler::BusConnect();
Render::Bootstrap::RequestBus::Handler::BusConnect();

// Listen for application's window creation/destruction (e.g. window is created/destroyed on Android when suspending the app)
AzFramework::ApplicationLifecycleEvents::Bus::Handler::BusConnect();

// delay one frame for Initialize which asset system is ready by then
AZ::TickBus::QueueFunction(
[this]()
Expand All @@ -292,6 +295,7 @@ namespace AZ

void BootstrapSystemComponent::Deactivate()
{
AzFramework::ApplicationLifecycleEvents::Bus::Handler::BusDisconnect();
Render::Bootstrap::RequestBus::Handler::BusDisconnect();
Render::Bootstrap::DefaultWindowBus::Handler::BusDisconnect();

Expand Down Expand Up @@ -367,6 +371,25 @@ namespace AZ
}
}

void BootstrapSystemComponent::OnApplicationWindowCreated()
{
if (!m_nativeWindow)
{
auto projectTitle = AZ::Utils::GetProjectDisplayName();
m_nativeWindow = AZStd::make_unique<AzFramework::NativeWindow>(projectTitle.c_str(), AzFramework::WindowGeometry(0, 0, r_width, r_height));
AZ_Assert(m_nativeWindow, "Failed to create the game window\n");

m_nativeWindow->Activate();

OnWindowCreated(m_nativeWindow->GetWindowHandle());
}
}

void BootstrapSystemComponent::OnApplicationWindowDestroy()
{
m_nativeWindow = nullptr;
}

void BootstrapSystemComponent::CreateViewportContext()
{
RHI::Device* device = RHI::RHISystemInterface::Get()->GetDevice();
Expand Down Expand Up @@ -681,7 +704,9 @@ namespace AZ
{
m_windowHandle = nullptr;
m_viewportContext.reset();
#if AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_EXIT_ON_WINDOW_CLOSE
akioCL marked this conversation as resolved.
Show resolved Hide resolved
AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop);
#endif
AzFramework::WindowNotificationBus::Handler::BusDisconnect();
}

Expand Down
6 changes: 6 additions & 0 deletions Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.h
Expand Up @@ -12,6 +12,7 @@
#include <AzCore/Settings/SettingsRegistry.h>
#include <AzCore/std/smart_ptr/shared_ptr.h>

#include <AzFramework/API/ApplicationAPI.h>
#include <AzFramework/Scene/Scene.h>
#include <AzFramework/Scene/SceneSystemInterface.h>
#include <AzFramework/Windowing/NativeWindow.h>
Expand All @@ -38,6 +39,7 @@ namespace AZ
class BootstrapSystemComponent
: public Component
, public TickBus::Handler
, public AzFramework::ApplicationLifecycleEvents::Bus::Handler
, public AzFramework::WindowNotificationBus::Handler
, public AzFramework::WindowSystemNotificationBus::Handler
, public AzFramework::WindowSystemRequestBus::Handler
Expand Down Expand Up @@ -84,6 +86,10 @@ namespace AZ
// AzFramework::WindowSystemNotificationBus::Handler overrides ...
void OnWindowCreated(AzFramework::NativeWindowHandle windowHandle) override;

// AzFramework::ApplicationLifecycleEvents::Bus::Handler overrides ...
void OnApplicationWindowCreated() override;
void OnApplicationWindowDestroy() override;

private:
void Initialize();

Expand Down
Expand Up @@ -7,3 +7,4 @@
*/

#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_PIPELINE_NAME "passes/LowEndRenderPipeline.azasset"
#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_EXIT_ON_WINDOW_CLOSE 0
Expand Up @@ -7,3 +7,4 @@
*/

#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_PIPELINE_NAME "passes/MainRenderPipeline.azasset"
#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_EXIT_ON_WINDOW_CLOSE 1
Expand Up @@ -7,3 +7,4 @@
*/

#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_PIPELINE_NAME "passes/MainRenderPipeline.azasset"
#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_EXIT_ON_WINDOW_CLOSE 1
Expand Up @@ -6,4 +6,5 @@
*
*/

#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_PIPELINE_NAME "passes/MainRenderPipeline.azasset"
#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_PIPELINE_NAME "passes/LowEndRenderPipeline.azasset"
#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_EXIT_ON_WINDOW_CLOSE 1
Expand Up @@ -7,3 +7,4 @@
*/

#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_PIPELINE_NAME "passes/LowEndRenderPipeline.azasset"
#define AZ_TRAIT_BOOTSTRAPSYSTEMCOMPONENT_EXIT_ON_WINDOW_CLOSE 1
5 changes: 5 additions & 0 deletions Gems/LyShine/Code/Source/UiRenderer.cpp
Expand Up @@ -303,6 +303,11 @@ AZ::Matrix4x4 UiRenderer::GetModelViewProjectionMatrix()
AZ::Vector2 UiRenderer::GetViewportSize()
{
auto viewportContext = GetViewportContext();
if (!viewportContext)
{
return AZ::Vector2::CreateZero();
}

auto windowContext = viewportContext->GetWindowContext();

const AZ::RHI::Viewport& viewport = windowContext->GetViewport();
Expand Down