Skip to content

Commit

Permalink
PerformanceHintManager needs a valid Java thread
Browse files Browse the repository at this point in the history
FIXES=[343965368, 343550453]
  • Loading branch information
pixelflinger committed Jun 4, 2024
1 parent 7aefa99 commit 749b063
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ class PlatformEGLAndroid : public PlatformEGL {
AcquiredImage transformAcquiredImage(AcquiredImage source) noexcept override;

private:
struct InitializeJvmForPerformanceManagerIfNeeded {
InitializeJvmForPerformanceManagerIfNeeded();
};

int mOSVersion;
ExternalStreamManagerAndroid& mExternalStreamManager;
InitializeJvmForPerformanceManagerIfNeeded const mInitializeJvmForPerformanceManagerIfNeeded;
utils::PerformanceHintManager mPerformanceHintManager;
utils::PerformanceHintManager::Session mPerformanceHintSession;

Expand Down
12 changes: 11 additions & 1 deletion filament/backend/src/VirtualMachineEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@

#include "private/backend/VirtualMachineEnv.h"

#include <utils/compiler.h>
#include <utils/debug.h>

#include <jni.h>

namespace filament {

JavaVM* VirtualMachineEnv::sVirtualMachine = nullptr;

// This is called when the library is loaded. We need this to get a reference to the global VM
/*
* This is typically called by filament_jni.so when it is loaded. If filament_jni.so is not used,
* then this must be called manually -- however, this is a problem because VirtualMachineEnv.h
* is currently private and part of backend.
* For now, we authorize this usage, but we will need to fix it; by making a proper public
* API for this.
*/
UTILS_PUBLIC
UTILS_NOINLINE
jint VirtualMachineEnv::JNI_OnLoad(JavaVM* vm) noexcept {
JNIEnv* env = nullptr;
Expand Down
18 changes: 17 additions & 1 deletion filament/backend/src/opengl/platforms/PlatformEGLAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <backend/platforms/PlatformEGL.h>
#include <backend/platforms/PlatformEGLAndroid.h>

#include <private/backend/VirtualMachineEnv.h>

#include "opengl/GLUtils.h"
#include "ExternalStreamManagerAndroid.h"

Expand Down Expand Up @@ -82,9 +84,23 @@ using EGLStream = Platform::Stream;

// ---------------------------------------------------------------------------------------------

PlatformEGLAndroid::InitializeJvmForPerformanceManagerIfNeeded::InitializeJvmForPerformanceManagerIfNeeded() {
// PerformanceHintManager() needs the calling thread to be a Java thread; so we need
// to attach this thread to the JVM before we initialize PerformanceHintManager.
// This should be done in PerformanceHintManager(), but libutils doesn't have access to
// VirtualMachineEnv.
if (PerformanceHintManager::isSupported()) {
(void)VirtualMachineEnv::get().getEnvironment();
}
}

// ---------------------------------------------------------------------------------------------

PlatformEGLAndroid::PlatformEGLAndroid() noexcept
: PlatformEGL(),
mExternalStreamManager(ExternalStreamManagerAndroid::create()) {
mExternalStreamManager(ExternalStreamManagerAndroid::create()),
mInitializeJvmForPerformanceManagerIfNeeded(),
mPerformanceHintManager() {

char scratch[PROP_VALUE_MAX + 1];
int length = __system_property_get("ro.build.version.release", scratch);
Expand Down
6 changes: 6 additions & 0 deletions libs/utils/include/utils/android/PerformanceHintManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ class UTILS_PUBLIC PerformanceHintManager :
int reportActualWorkDuration(int64_t actualDurationNanos) noexcept;
};

// caveat: This must be called on a Java thread
PerformanceHintManager() noexcept;
~PerformanceHintManager() noexcept;

// Whether PerformanceHintManager APIs are supported (which doesn't mean PerformanceHintManager
// itself is, use isValid()).
static bool isSupported() noexcept;

// Whether PerformanceHintManager can be used.
bool isValid() const;

int64_t getPreferredUpdateRateNanos() const noexcept;
Expand Down
7 changes: 7 additions & 0 deletions libs/utils/src/android/PerformanceHintManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ PerformanceHintManager::PerformanceHintManager() noexcept {

PerformanceHintManager::~PerformanceHintManager() noexcept = default;

bool PerformanceHintManager::isSupported() noexcept {
if (__builtin_available(android __ANDROID_API_T__, *)) {
return true;
}
return false;
}

bool PerformanceHintManager::isValid() const {
return mImpl->mManager != nullptr;
}
Expand Down

0 comments on commit 749b063

Please sign in to comment.