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

Fix the incorrect orientation of the gravity sensor and JSVM crash issue in HarmonyOS Next #18041

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion native/cocos/audio/openharmony/PcmAudioService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@ void PcmAudioService::resume() {
OH_AudioRenderer_Start(_audioRenderer);
}
}
} // namespace cocos2d
} // namespace cc
25 changes: 6 additions & 19 deletions native/cocos/bindings/jswrapper/jsvm/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void Object::setPrivateObject(PrivateObjectBase* data) {
auto tmpThis = _objRef.getValue(_env);
JSVM_Ref result = nullptr;
NODE_API_CALL(status, _env,
OH_JSVM_Wrap(_env, tmpThis, this, sendWeakCallback,
OH_JSVM_Wrap(_env, tmpThis, this, weakCallback,
(void*)this /* finalize_hint */, &result));
//_objRef.setWeakref(_env, result);
setProperty("__native_ptr__", se::Value(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(data))));
Expand Down Expand Up @@ -423,19 +423,8 @@ Object* Object::createTypedArrayWithBuffer(TypedArrayType type, const Object *ob
Object* Object::createExternalArrayBufferObject(void* contents, size_t byteLength, BufferContentsFreeFunc freeFunc, void* freeUserData) {
JSVM_Status status;
JSVM_Value result;
if (freeFunc) {
struct ExternalArrayBufferCallbackParams* param = new (struct ExternalArrayBufferCallbackParams);
param->func = freeFunc;
param->contents = contents;
param->byteLength = byteLength;
param->userData = freeUserData;
NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &contents, &result));
param->func(param->contents, param->byteLength, param->userData);
delete param;
} else {
NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &contents, &result));
}

// JSVM does not support the napi_create_external_arraybuffer interface like in NAPI.
NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArrayBufferFromBackingStoreData(ScriptEngine::getEnv(), contents, byteLength, 0, byteLength, &result));
Object* obj = Object::_createJSObject(ScriptEngine::getEnv(), result, nullptr);
return obj;
}
Expand Down Expand Up @@ -703,16 +692,14 @@ void Object::sendWeakCallback(JSVM_Env env, void* nativeObject, void* finalizeHi
}

void Object::weakCallback(JSVM_Env env, void* nativeObject, void* finalizeHint /*finalize_hint*/) {

if (finalizeHint) {
if (nativeObject == nullptr) {
return;
}
void *rawPtr = reinterpret_cast<Object*>(finalizeHint)->_privateData;
Object* seObj = reinterpret_cast<Object*>(finalizeHint);
Object* rawPtrObj = reinterpret_cast<Object*>(rawPtr);
if(rawPtrObj->getRefCount() == 0) {
return;
}
if (seObj->_onCleaingPrivateData) { //called by cleanPrivateData, not release seObj;
return;
}
Expand All @@ -726,11 +713,11 @@ void Object::weakCallback(JSVM_Env env, void* nativeObject, void* finalizeHint /
}

if (seObj->_finalizeCb != nullptr) {
seObj->_finalizeCb(env, rawPtr, rawPtr);
seObj->_finalizeCb(env, finalizeHint, finalizeHint);
} else {
assert(seObj->_getClass() != nullptr);
if (seObj->_getClass()->_getFinalizeFunction() != nullptr) {
seObj->_getClass()->_getFinalizeFunction()(env, rawPtr, rawPtr);
seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint);
}
}
seObj->decRef();
Expand Down
8 changes: 4 additions & 4 deletions native/cocos/bindings/jswrapper/jsvm/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ class Object : public RefCounter {


/**
* @brief Delete a property of an object.
* @param[in] name A utf-8 string containing the property's name.
* @return true if the property is deleted successfully, otherwise false.
*/
* @brief Delete a property of an object.
* @param[in] name A utf-8 string containing the property's name.
* @return true if the property is deleted successfully, otherwise false.
*/
bool deleteProperty(const char *name);

/**
Expand Down
4 changes: 2 additions & 2 deletions native/cocos/bindings/jswrapper/napi/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,11 @@ void Object::weakCallback(napi_env env, void* nativeObject, void* finalizeHint /
}

if (seObj->_finalizeCb != nullptr) {
seObj->_finalizeCb(env, rawPtr, rawPtr);
seObj->_finalizeCb(env, finalizeHint, finalizeHint);
} else {
assert(seObj->_getClass() != nullptr);
if (seObj->_getClass()->_getFinalizeFunction() != nullptr) {
seObj->_getClass()->_getFinalizeFunction()(env, rawPtr, rawPtr);
seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint);
}
}
seObj->decRef();
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/bindings/manual/jsb_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ static bool jsb_createExternalArrayBuffer(se::State &s) { // NOLINT
SE_PRECONDITION2(ok, false, "Error processing arguments");
if (byteLength > 0) {
// NOTE: Currently V8 use shared_ptr which has different abi on win64-debug and win64-release
#if CC_PLATFORM == CC_PLATFORM_WINDOWS && SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8
#if (CC_PLATFORM == CC_PLATFORM_WINDOWS && SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8) || (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSVM)
se::HandleObject arrayBuffer{se::Object::createArrayBufferObject(nullptr, byteLength)};
#else
void *buffer = malloc(byteLength);
Expand Down
4 changes: 2 additions & 2 deletions native/cocos/platform/openharmony/modules/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ Screen::Orientation Screen::getDeviceOrientation() const {
if(value == 0) {
return Orientation::PORTRAIT;
} else if(value == 1) {
return Orientation::LANDSCAPE_RIGHT;
return Orientation::LANDSCAPE_LEFT;
} else if(value == 2) {
return Orientation::PORTRAIT_UPSIDE_DOWN;
} else if(value == 3) {
return Orientation::LANDSCAPE_LEFT;
return Orientation::LANDSCAPE_RIGHT;
}
CC_ASSERT(false);
return Orientation::PORTRAIT;
Expand Down
5 changes: 2 additions & 3 deletions native/cocos/platform/openharmony/napi/NapiHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ enum ContextType {
ENGINE_UTILS,
EDITBOX_UTILS,
WEBVIEW_UTILS,
SYSTEM_UTILS,
DISPLAY_UTILS,
UV_ASYNC_SEND,
VIDEO_UTILS
Expand Down Expand Up @@ -226,13 +225,13 @@ static void napiOnDisplayChange(const Napi::CallbackInfo &info) {
orientation = static_cast<int>(cc::IScreen::Orientation::PORTRAIT);
break;
case 1: // ROTATION_90
orientation = static_cast<int>(cc::IScreen::Orientation::LANDSCAPE_RIGHT);
orientation = static_cast<int>(cc::IScreen::Orientation::LANDSCAPE_LEFT);
break;
case 2: // ROTATION_180
orientation = static_cast<int>(cc::IScreen::Orientation::PORTRAIT_UPSIDE_DOWN);
break;
case 3: // ROTATION_270
orientation = static_cast<int>(cc::IScreen::Orientation::LANDSCAPE_LEFT);
orientation = static_cast<int>(cc::IScreen::Orientation::LANDSCAPE_RIGHT);
break;
}
cc::events::Orientation::broadcast(orientation);
Expand Down
6 changes: 0 additions & 6 deletions platforms/native/builtin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,6 @@ for (const key in jsbWindow) {
}
}

// In the openharmony platform, XMLHttpRequest is not undefined, but there are problems to using it.
// So the native implementation is forced to be used.
if (window.oh && typeof globalThis.XMLHttpRequest !== 'undefined') {
globalThis.XMLHttpRequest = jsbWindow.XMLHttpRequest;
}

if (typeof globalThis.window === 'undefined') {
globalThis.window = globalThis;
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/native-pack-tool/source/platforms/harmonyos-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export class HarmonyOSNextPackTool extends NativePackTool {
moduleJSON.module.abilities[0].orientation = 'auto_rotation';
}
else if (cfg.landscapeRight && !cfg.landscapeLeft) {
moduleJSON.module.abilities[0].orientation = 'landscape';
moduleJSON.module.abilities[0].orientation = 'landscape_inverted';
}
else if (!cfg.landscapeRight && cfg.landscapeLeft) {
moduleJSON.module.abilities[0].orientation = 'landscape_inverted';
moduleJSON.module.abilities[0].orientation = 'landscape';
}
else if (cfg.landscapeRight && cfg.landscapeLeft) {
moduleJSON.module.abilities[0].orientation = 'auto_rotation_landscape';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libcocos.so",
"types": "./index.d.ts",
"version": "",
"version": "1.0.0",
"description": "Please describe the basic information."
}
Loading
Loading