no message

This commit is contained in:
gem
2025-02-18 15:21:31 +08:00
commit 2d133e56d7
1980 changed files with 465595 additions and 0 deletions

View File

@@ -0,0 +1,298 @@
/****************************************************************************
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "platform/java/jni/glue/JniNativeGlue.h"
#include <functional>
#include <future>
#include "application/ApplicationManager.h"
#include "engine/EngineEvents.h"
#include "platform/BasePlatform.h"
#include "platform/java/jni/JniImp.h"
#include "platform/java/jni/glue/MessagePipe.h"
#include "platform/java/jni/log.h"
#include "platform/java/modules/SystemWindow.h"
#include "platform/java/modules/SystemWindowManager.h"
namespace cc {
JniNativeGlue::~JniNativeGlue() = default;
JniNativeGlue* JniNativeGlue::getInstance() {
static JniNativeGlue jniNativeGlue;
return &jniNativeGlue;
}
void JniNativeGlue::start(int argc, const char** argv) {
_messagePipe = std::make_unique<MessagePipe>();
BasePlatform* platform = cc::BasePlatform::getPlatform();
if (platform->init()) {
LOGV("Platform initialization failed");
}
platform->run(argc, argv);
}
void JniNativeGlue::setWindowHandle(NativeWindowType* window) {
if (_pendingWindow) {
writeCommandSync(JniCommand::JNI_CMD_TERM_WINDOW);
}
_pendingWindow = window;
if (window) {
writeCommandSync(JniCommand::JNI_CMD_INIT_WINDOW);
}
}
void JniNativeGlue::setActivityGetter(std::function<NativeActivity(void)> getter) {
_activityGetter = std::move(getter);
}
void* JniNativeGlue::getActivity() {
return _activityGetter ? _activityGetter() : nullptr;
}
void JniNativeGlue::setEnvGetter(std::function<NativeEnv(void)> getter) {
_envGetter = std::move(getter);
}
void* JniNativeGlue::getEnv() {
return _envGetter ? _envGetter() : nullptr;
}
void JniNativeGlue::setResourceManager(ResourceManagerType* resourceManager) {
_resourceManager = resourceManager;
}
ResourceManagerType* JniNativeGlue::getResourceManager() {
return _resourceManager;
}
NativeWindowType* JniNativeGlue::getWindowHandle() {
return _window;
}
void JniNativeGlue::setSdkVersion(int sdkVersion) {
_sdkVersion = sdkVersion;
}
int JniNativeGlue::getSdkVersion() const {
return _sdkVersion;
}
void JniNativeGlue::setObbPath(const std::string& path) {
_obbPath = path;
}
std::string JniNativeGlue::getObbPath() const {
return _obbPath;
}
void JniNativeGlue::setRunning(bool isRunning) {
_threadPromise.set_value();
_running = isRunning;
}
void JniNativeGlue::waitRunning() {
_threadPromise.get_future().get();
}
bool JniNativeGlue::isRunning() const {
return _running;
}
void JniNativeGlue::flushTasksOnGameThread() const {
// Handle java events send by UI thread. Input events are handled here too.
flushTasksOnGameThreadJNI();
if (_animating) {
flushTasksOnGameThreadAtForegroundJNI();
}
}
void JniNativeGlue::writeCommandAsync(JniCommand cmd) {
CommandMsg msg{.cmd = cmd, .callback = nullptr};
_messagePipe->writeCommand(&msg, sizeof(msg));
}
void JniNativeGlue::writeCommandSync(JniCommand cmd) {
std::promise<void> fu;
CommandMsg msg{.cmd = cmd, .callback = [&fu]() {
fu.set_value();
}};
_messagePipe->writeCommand(&msg, sizeof(msg));
fu.get_future().get();
}
int JniNativeGlue::readCommand(CommandMsg* msg) {
return _messagePipe->readCommand(msg, sizeof(CommandMsg));
}
int JniNativeGlue::readCommandWithTimeout(CommandMsg* cmd, int delayMS) {
return _messagePipe->readCommandWithTimeout(cmd, sizeof(CommandMsg), delayMS);
}
bool JniNativeGlue::isPause() const {
if (!_animating) {
return true;
}
if (_appState == JniCommand::JNI_CMD_PAUSE) {
return true;
}
return false;
}
void JniNativeGlue::onPause() {
writeCommandAsync(JniCommand::JNI_CMD_PAUSE);
}
void JniNativeGlue::onResume() {
writeCommandAsync(JniCommand::JNI_CMD_RESUME);
}
void JniNativeGlue::onLowMemory() {
writeCommandAsync(JniCommand::JNI_CMD_LOW_MEMORY);
}
void JniNativeGlue::execCommand() {
static CommandMsg msg;
static bool runInLowRate{false};
runInLowRate = !_animating || JniCommand::JNI_CMD_PAUSE == _appState;
if (readCommandWithTimeout(&msg, runInLowRate ? 50 : 0) > 0) {
preExecCmd(msg.cmd);
engineHandleCmd(msg.cmd);
postExecCmd(msg.cmd);
if (msg.callback) {
msg.callback();
}
}
}
void JniNativeGlue::preExecCmd(JniCommand cmd) {
switch (cmd) {
case JniCommand::JNI_CMD_INIT_WINDOW: {
LOGV("JNI_CMD_INIT_WINDOW");
_animating = true;
_window = _pendingWindow;
} break;
case JniCommand::JNI_CMD_TERM_WINDOW:
LOGV("JNI_CMD_TERM_WINDOW");
_animating = false;
break;
case JniCommand::JNI_CMD_RESUME:
LOGV("JNI_CMD_RESUME");
_appState = cmd;
break;
case JniCommand::JNI_CMD_PAUSE:
LOGV("JNI_CMD_PAUSE");
_appState = cmd;
break;
default:
break;
}
}
void JniNativeGlue::engineHandleCmd(JniCommand cmd) {
static bool isWindowInitialized = false;
// Handle CMD here if needed.
switch (cmd) {
case JniCommand::JNI_CMD_INIT_WINDOW: {
if (isWindowInitialized) {
return;
}
isWindowInitialized = true;
// cc::CustomEvent event;
// event.name = EVENT_RECREATE_WINDOW;
// event.args->ptrVal = reinterpret_cast<void*>(getWindowHandle());
ISystemWindowInfo info;
info.width = getWidth();
info.height = getHeight();
info.externalHandle = getWindowHandle();
BasePlatform* platform = cc::BasePlatform::getPlatform();
auto* windowMgr = platform->getInterface<SystemWindowManager>();
CC_ASSERT(windowMgr != nullptr);
windowMgr->createWindow(info);
events::WindowRecreated::broadcast(ISystemWindow::mainWindowId);
} break;
case JniCommand::JNI_CMD_TERM_WINDOW: {
events::WindowDestroy::broadcast(ISystemWindow::mainWindowId);
} break;
case JniCommand::JNI_CMD_RESUME: {
events::WindowChanged::broadcast(WindowEvent::Type::SHOW);
} break;
case JniCommand::JNI_CMD_PAUSE: {
events::WindowChanged::broadcast(WindowEvent::Type::HIDDEN);
} break;
case JniCommand::JNI_CMD_DESTROY: {
LOGV("APP_CMD_DESTROY");
events::WindowChanged::broadcast(WindowEvent::Type::CLOSE);
setRunning(false);
} break;
case JniCommand::JNI_CMD_LOW_MEMORY: {
events::LowMemory::broadcast();
break;
}
default:
break;
}
}
void JniNativeGlue::postExecCmd(JniCommand cmd) {
switch (cmd) {
case JniCommand::JNI_CMD_TERM_WINDOW: {
#if CC_PLATFORM == CC_PLATFORM_ANDROID
if (_window) {
ANativeWindow_release(_window);
}
#endif
_window = nullptr;
} break;
default:
break;
}
}
int32_t JniNativeGlue::getWidth() const {
int32_t width = 0;
if (_window) {
#if (CC_PLATFORM == CC_PLATFORM_ANDROID)
width = ANativeWindow_getWidth(_window);
#elif (CC_PLATFORM == CC_PLATFORM_OHOS)
width = NativeLayerHandle(_window, NativeLayerOps::GET_WIDTH);
#endif
}
return width;
}
int32_t JniNativeGlue::getHeight() const {
int32_t height = 0;
if (_window) {
#if (CC_PLATFORM == CC_PLATFORM_ANDROID)
height = ANativeWindow_getHeight(_window);
#elif (CC_PLATFORM == CC_PLATFORM_OHOS)
height = NativeLayerHandle(_window, NativeLayerOps::GET_HEIGHT);
#endif
}
return height;
}
} // namespace cc

View File

@@ -0,0 +1,142 @@
/****************************************************************************
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
#include <functional>
#include <future>
#include <memory>
#include "base/Macros.h"
#include "platform/java/jni/glue/MessagePipe.h"
#if (CC_PLATFORM == CC_PLATFORM_ANDROID)
#include <android/asset_manager.h>
#include <android/native_window.h>
using ResourceManagerType = AAssetManager;
using NativeWindowType = ANativeWindow;
#elif (CC_PLATFORM == CC_PLATFORM_OHOS)
#include <native_layer.h>
#include <rawfile/resource_manager.h>
using ResourceManagerType = ResourceManager;
using NativeWindowType = NativeLayer;
#endif
using NativeActivity = void*; //jobject
using NativeEnv = void*; //jnienv
namespace cc {
class IEventDispatch;
class TouchEvent;
class CC_DLL JniNativeGlue {
public:
enum class JniCommand {
JNI_CMD_TERM_WINDOW = 0,
JNI_CMD_INIT_WINDOW,
JNI_CMD_RESUME,
JNI_CMD_PAUSE,
JNI_CMD_DESTROY,
JNI_CMD_LOW_MEMORY,
JNI_CMD_UNKNOW,
};
virtual ~JniNativeGlue();
static JniNativeGlue* getInstance();
virtual void start(int argc, const char** argv);
void setWindowHandle(NativeWindowType* window);
NativeWindowType* getWindowHandle();
void setActivityGetter(std::function<NativeActivity(void)>);
void* getActivity();
void setEnvGetter(std::function<NativeEnv(void)>);
void* getEnv();
void setResourceManager(ResourceManagerType* resourceManager);
ResourceManagerType* getResourceManager();
void setSdkVersion(int sdkVersion);
int getSdkVersion() const;
void setObbPath(const std::string& path);
std::string getObbPath() const;
bool isRunning() const;
void setRunning(bool isRunning);
void waitRunning();
void flushTasksOnGameThread() const;
struct CommandMsg {
JniCommand cmd;
std::function<void()> callback;
};
void writeCommandAsync(JniCommand cmd);
void writeCommandSync(JniCommand cmd);
int readCommand(CommandMsg* msg);
int readCommandWithTimeout(CommandMsg* cmd, int delayMS);
void onPause();
void onResume();
void onLowMemory();
bool isPause() const;
void execCommand();
int32_t getWidth() const;
int32_t getHeight() const;
bool isAnimating() const { return _animating; }
private:
void preExecCmd(JniCommand cmd);
void engineHandleCmd(JniCommand cmd);
void postExecCmd(JniCommand cmd);
bool _running{false};
int _sdkVersion{0};
bool _animating{false};
std::promise<void> _threadPromise;
std::string _obbPath;
ResourceManagerType* _resourceManager{nullptr};
NativeWindowType* _window{nullptr};
NativeWindowType* _pendingWindow{nullptr};
JniCommand _appState{JniCommand::JNI_CMD_UNKNOW};
IEventDispatch* _eventDispatcher{nullptr};
std::unique_ptr<MessagePipe> _messagePipe{nullptr};
std::function<NativeEnv(void)> _envGetter;
std::function<NativeActivity(void)> _activityGetter;
};
} // namespace cc
#define JNI_NATIVE_GLUE() cc::JniNativeGlue::getInstance()

View File

@@ -0,0 +1,89 @@
/****************************************************************************
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "platform/java/jni/glue/MessagePipe.h"
#include <fcntl.h>
#include <unistd.h>
#include "platform/java/jni/log.h"
//#include <android/log.h>
//#include "platform/android/jni/JniCocosActivity.h"
namespace cc {
MessagePipe::MessagePipe() {
int messagePipe[2] = {0};
if (pipe(messagePipe)) {
LOGV("Can not create pipe: %s", strerror(errno));
}
_pipeRead = messagePipe[0];
_pipeWrite = messagePipe[1];
if (fcntl(_pipeRead, F_SETFL, O_NONBLOCK) < 0) {
LOGV("Can not make pipe read to non blocking mode.");
}
}
MessagePipe::~MessagePipe() {
close(_pipeRead);
close(_pipeWrite);
}
void MessagePipe::writeCommand(int8_t cmd) const {
write(_pipeWrite, &cmd, sizeof(cmd));
}
int MessagePipe::readCommand(int8_t &cmd) const {
return read(_pipeRead, &cmd, sizeof(cmd));
}
int MessagePipe::readCommandWithTimeout(void *msg, int32_t size, int delayMS) {
if (delayMS > 0) {
static fd_set fdSet;
static timeval timeout;
timeout = {delayMS / 1000, (delayMS % 1000) * 1000};
FD_ZERO(&fdSet);
FD_SET(_pipeRead, &fdSet);
auto ret = select(_pipeRead + 1, &fdSet, nullptr, nullptr, &timeout);
if (ret < 0) {
LOGV("failed to run select(..): %s\n", strerror(errno));
return ret;
}
if (ret == 0) {
return 0;
}
}
return readCommand(msg, size);
}
void MessagePipe::writeCommand(void *msg, int32_t size) const {
write(_pipeWrite, msg, size);
}
int MessagePipe::readCommand(void *msg, int32_t size) const {
return read(_pipeRead, msg, size);
}
} // namespace cc

View File

@@ -0,0 +1,48 @@
/****************************************************************************
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
#include <iostream>
#include "base/Macros.h"
namespace cc {
class CC_DLL MessagePipe {
public:
MessagePipe();
~MessagePipe();
void writeCommand(int8_t cmd) const;
int readCommand(int8_t &cmd) const;
void writeCommand(void *msg, int32_t size) const;
int readCommand(void *msg, int32_t size) const;
int readCommandWithTimeout(void *msg, int32_t size, int delayMS);
private:
int _pipeRead = 0;
int _pipeWrite = 0;
};
} // namespace cc