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

fixes edge cases in Android Native App rendering #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mgood7123
Copy link

@mgood7123 mgood7123 commented Jun 27, 2021

adapted from https://github.com/151706061/OpenGLSamples/blob/1e871c0c7fe0d8d30825f467bfdc8de50961b6f9/extensions/src/NvAppBase/NvAndroidNativeAppGlue.c#L128

this fixes possible rendering edge cases mentioned in https://developer.nvidia.com/fixing-common-android-lifecycle-issues-games

  • this additionally fixes an uncommon crash when the assistant is brought up over the application
  • this additionally fixes an uncommon crash when the application is switched from the top split screen to the bottom split screen, in Samsung One UI devices

@@ -81,15 +84,39 @@ class AndroidAppBase : public AppBase
android_app* app_ = nullptr;
std::string native_activity_class_name_;

pthread_mutex_t mutex;
pthread_cond_t cond;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for? Nothing waits on this conditional

@@ -81,15 +84,39 @@ class AndroidAppBase : public AppBase
android_app* app_ = nullptr;
std::string native_activity_class_name_;

pthread_mutex_t mutex;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use std::mutex

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, though i think std::mutex is slightly slower than pthread_mutex in terms of overhead, but i will switch to std::mutex

{
// Android lifecycle status flags. Not app-specific
// Set between onCreate and onDestroy
NV_APP_STATUS_RUNNING = 0x00000001,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for NV_ prefix

NV_APP_STATUS_INTERACTABLE = 0x0000000f
};

unsigned int lifecycleFlags = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lifecycle_flags_ to be consistent with the rest

};

unsigned int lifecycleFlags = 0;
int32_t window_width = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window_width_

namespace Diligent
{

AndroidAppBase::AndroidAppBase() {
pthread_mutex_init(&mutex, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to initialize if you use std::mutex

case APP_CMD_SAVE_STATE:
case APP_CMD_INPUT_CHANGED:
#if(LOG_NATIVE_APPLICATION)
LOG_ERROR_MESSAGE("APP_CMD_INPUT_CHANGED");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it an error?

Copy link
Author

@mgood7123 mgood7123 Jun 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make it stand out in the log as it is important for seeing what is going on with the state of the native app internally when debugging the state, since errors have red text in logcat

//pthread_mutex_lock(&android_app->mutex);
//if (android_app->inputQueue != NULL) {
// AInputQueue_detachLooper(android_app->inputQueue);
//}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this?

{
pthread_mutex_lock(&android_app->mutex);
#if(LOG_NATIVE_APPLICATION)
LOG_ERROR_MESSAGE("APP_CMD_INIT_WINDOW");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make it stand out in the log as it is important for seeing what is going on with the state of the native app internally when debugging the state, since errors have red text in logcat

// The window is being shown, get it ready.
if (app->window != NULL)
{
pthread_mutex_lock(&android_app->mutex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::lock_guard lock{mutex}; here and in similar places

Copy link
Contributor

@TheMostDiligent TheMostDiligent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 11 commits in this branch already. Please squash them into one.

NativeApp/include/Android/AndroidAppBase.hpp Outdated Show resolved Hide resolved
APP_STATUS_HAS_REAL_SURFACE = 0x00000008,
};

unsigned int lifecycleFlags_ = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APP_STATUS_FLAGS app_status_ = APP_STATUS_FLAG_NONE

@@ -30,8 +30,12 @@
//#include <android/log.h>
//#include <android_native_app_glue.h>
#include <android/native_window_jni.h>
#include <Errors.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Errors.hpp", this is not a system header

#if (LOG_NATIVE_APPLICATION)
LOG_INFO_MESSAGE("HAS SURFACE");
#endif
std::lock_guard<std::mutex> give_me_a_name(eng->mutex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lock{eng->mutex}

@@ -89,7 +91,24 @@ class AndroidAppBase : public AppBase
void UpdateFPS(float fFPS);

bool initialized_resources_ = false;
bool has_focus_ = false;

enum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest give it a name (e.g. APP_STATUS_FLAGS) and use DEFINE_FLAG_ENUM_OPERATORS(APP_STATUS_FLAGS)

case APP_CMD_LOST_FOCUS:
#if (LOG_NATIVE_APPLICATION)
LOG_INFO_MESSAGE("APP_CMD_LOST_FOCUS");
LOG_INFO_MESSAGE("LOST FOCUS");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why extra message?

case APP_CMD_RESUME:
#if (LOG_NATIVE_APPLICATION)
LOG_INFO_MESSAGE("APP_CMD_RESUME");
LOG_INFO_MESSAGE("IS ACTIVE");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why extra message?

eng->has_focus_ = false;
#if (LOG_NATIVE_APPLICATION)
LOG_INFO_MESSAGE("APP_CMD_PAUSE");
LOG_INFO_MESSAGE("IS NOT ACTIVE");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why extra message?

case APP_CMD_DESTROY:
#if (LOG_NATIVE_APPLICATION)
LOG_INFO_MESSAGE("APP_CMD_DESTROY");
LOG_INFO_MESSAGE("IS NOT RUNNING");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why extra message?


return false;
std::lock_guard<std::mutex> give_me_a_name(mutex);
return lifecycleFlags_ & APP_STATUS_ACTIVE &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look pretty.
(app_status_ & APP_STATUS_FLAG_ACTIVE) != APP_STATUS_FLAG_NONE
Would be clearer

@mgood7123
Copy link
Author

There are 11 commits in this branch already. Please squash them into one.

is it possible to do this via github.com or would i manually need to do some git command line kung fu? (eg git reset, git add, git force push)

@TheMostDiligent
Copy link
Contributor

You can force-push to the PR branch

@mgood7123
Copy link
Author

You can force-push to the PR branch

ok

@mgood7123
Copy link
Author

apparently force pushing a branch with no changes closes the pull request...

@mgood7123 mgood7123 reopened this Jun 28, 2021
@mgood7123 mgood7123 force-pushed the patch-1 branch 5 times, most recently from 58a9e7e to 2d43095 Compare June 28, 2021 03:45
case APP_CMD_INIT_WINDOW:
// The window is being shown, get it ready.
if (app->window != NULL)
#if (DILIGENT_DEVLOPMENT)
Copy link
Contributor

@TheMostDiligent TheMostDiligent Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think it makes sense to always log this - there is really no reason not to...
Same for other places.

Copy link
Author

@mgood7123 mgood7123 Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this?

        case APP_CMD_INIT_WINDOW:
            LOG_INFO_MESSAGE("APP_CMD_INIT_WINDOW");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logging of all events

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm ok

Copy link
Author

@mgood7123 mgood7123 Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this? 60f08d8

@mgood7123 mgood7123 force-pushed the patch-1 branch 2 times, most recently from 60f08d8 to 3d76fb8 Compare June 28, 2021 04:51
@mgood7123
Copy link
Author

is this better?

@mgood7123 mgood7123 force-pushed the patch-1 branch 7 times, most recently from 67c0830 to e09d30c Compare July 2, 2021 23:31

std::atomic<APP_STATUS_FLAGS> app_status_{APP_STATUS_FLAG_NONE};

int32_t window_width_ = 0;
Copy link
Author

@mgood7123 mgood7123 Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable is unused, i need to remove it when i have time

std::atomic<APP_STATUS_FLAGS> app_status_{APP_STATUS_FLAG_NONE};

int32_t window_width_ = 0;
int32_t window_height_ = 0;
Copy link
Author

@mgood7123 mgood7123 Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable is unused, i need to remove it when i have time

Squashed commit of the following:

commit ba7271f
Author: mgood7123 <[email protected]>
Date:   Sat Jul 3 03:12:45 2021 +1000

    use std::atomic instead of std::mutex

commit 54e3973
Author: mgood7123 <[email protected]>
Date:   Tue Jun 29 07:52:35 2021 +1000

    fixes edge cases in Android Native App rendering

Revert "fix possible rendering edge cases"

This reverts commit 3d76fb8.

fix possible rendering edge cases

Update AndroidMain.cpp

add functions for app status

apply validator suggestions

Update AndroidAppBase.hpp

Update AndroidAppBase.cpp

remove unused variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants