-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create StarboardBridge native class with jni_generator (#4606)
b/372559388 This PR configures jni_generator for cobalt_apk_java. It creates the corresponding native class StarboardBridge for cobalt/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java, adhering to Chromium's JNI standards. The StarboardBridge native class is implemented as a singleton and is initialized with the same JNIEnv and jobject used when calling the Starboard custom JniEnvExt::Initialize function. Additionally, it updates the JNI function calls in starboard/android/shared/application_android.cc to align with Chromium's modern JNI standards. This PR amends the reverted PR #4545 There was an issue in the reverted PR that there are old annotations @UsedByNative embeded in the inner function calls in getResourceOverlay, somehow making the app not able to launch.. Culprit -> https://github.com/youtube/cobalt/pull/4545/files#diff-22285847addbd15025f71dadd357129f86573e042655067048f94fae301fb1d3R480. We can not blindly replace @UsedByNative with @CalledByNative, when switching to @CalledByNative, we should make sure to replace all occurrence of @UserByNative inside, and implement the new JNI template functions. I don't get why compiler didn't complain about the getResourceOverlay issue tho.
- Loading branch information
1 parent
9158f9a
commit 1322f53
Showing
8 changed files
with
184 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "starboard/android/shared/starboard_bridge.h" | ||
|
||
#include "starboard/android/shared/file_internal.h" | ||
#include "starboard/common/time.h" | ||
#include "starboard/media.h" | ||
#include "starboard/shared/starboard/audio_sink/audio_sink_internal.h" | ||
|
||
// Must come after all headers that specialize FromJniType() / ToJniType(). | ||
#include "cobalt/android/jni_headers/StarboardBridge_jni.h" | ||
|
||
namespace starboard { | ||
namespace android { | ||
namespace shared { | ||
|
||
extern "C" SB_EXPORT_PLATFORM void JNI_StarboardBridge_OnStop(JNIEnv* env) { | ||
SbAudioSinkPrivate::TearDown(); | ||
SbFileAndroidTeardown(); | ||
} | ||
|
||
extern "C" SB_EXPORT_PLATFORM jlong | ||
JNI_StarboardBridge_CurrentMonotonicTime(JNIEnv* env) { | ||
return CurrentMonotonicTime(); | ||
} | ||
|
||
// static | ||
StarboardBridge* StarboardBridge::GetInstance() { | ||
return base::Singleton<StarboardBridge>::get(); | ||
} | ||
|
||
void StarboardBridge::Initialize(JNIEnv* env, jobject obj) { | ||
j_starboard_bridge_.Reset(env, obj); | ||
} | ||
|
||
long StarboardBridge::GetAppStartTimestamp() { | ||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
CHECK(env); | ||
return Java_StarboardBridge_getAppStartTimestamp(env, j_starboard_bridge_); | ||
} | ||
|
||
void StarboardBridge::ApplicationStarted() { | ||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
CHECK(env); | ||
return Java_StarboardBridge_applicationStarted(env, j_starboard_bridge_); | ||
} | ||
|
||
void StarboardBridge::ApplicationStopping() { | ||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
CHECK(env); | ||
return Java_StarboardBridge_applicationStopping(env, j_starboard_bridge_); | ||
} | ||
} // namespace shared | ||
} // namespace android | ||
} // namespace starboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef STARBOARD_ANDROID_SHARED_STARBOARD_BRIDGE_H_ | ||
#define STARBOARD_ANDROID_SHARED_STARBOARD_BRIDGE_H_ | ||
|
||
#include <jni.h> | ||
|
||
#include "base/android/scoped_java_ref.h" | ||
#include "base/memory/singleton.h" | ||
|
||
namespace starboard { | ||
namespace android { | ||
namespace shared { | ||
|
||
// This class serves as a bridge between the native code and Android | ||
// StarboardBridge Java class. | ||
class StarboardBridge { | ||
public: | ||
// Returns the singleton. | ||
static StarboardBridge* GetInstance(); | ||
|
||
void Initialize(JNIEnv* env, jobject obj); | ||
|
||
long GetAppStartTimestamp(); | ||
|
||
void ApplicationStarted(); | ||
|
||
void ApplicationStopping(); | ||
|
||
private: | ||
StarboardBridge() = default; | ||
~StarboardBridge() = default; | ||
|
||
// Prevent copy construction and assignment | ||
StarboardBridge(const StarboardBridge&) = delete; | ||
StarboardBridge& operator=(const StarboardBridge&) = delete; | ||
|
||
friend struct base::DefaultSingletonTraits<StarboardBridge>; | ||
|
||
// Java StarboardBridge instance. | ||
base::android::ScopedJavaGlobalRef<jobject> j_starboard_bridge_; | ||
}; | ||
|
||
} // namespace shared | ||
} // namespace android | ||
} // namespace starboard | ||
|
||
#endif // STARBOARD_ANDROID_SHARED_STARBOARD_BRIDGE_H_ |