Skip to content

Commit

Permalink
Synchronize HarmonyOS Next platform from cocos creator 2.4.14 (cocos#…
Browse files Browse the repository at this point in the history
…18247)

* Synchronize the HarmonyOS Next platform update for 2.4.14
  • Loading branch information
qiuguohua authored Jan 24, 2025
1 parent 2f9e8d2 commit fd7270c
Show file tree
Hide file tree
Showing 24 changed files with 528 additions and 218 deletions.
1 change: 1 addition & 0 deletions native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3408,6 +3408,7 @@ if(OPENHARMONY)
libace_ndk.z.so
libz.so
librawfile.z.so
libohsensor.so
${CC_EXTERNAL_LIBS}
)
if(USE_SE_JSVM)
Expand Down
16 changes: 5 additions & 11 deletions native/cocos/audio/openharmony/PcmAudioService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ THE SOFTWARE.
#include "audio/openharmony/PcmAudioService.h"
#include "audio/android/AudioMixerController.h"
#include "audio/android/cutils/log.h"
#include "audio/android/utils/Compat.h"
namespace cc {

static ccstd::vector<char> __silenceData; //NOLINT(bugprone-reserved-identifier, readability-identifier-naming)
namespace cc {

PcmAudioService::PcmAudioService()
: _controller(nullptr) {
Expand All @@ -55,25 +53,24 @@ int32_t PcmAudioService::AudioRendererOnWriteData(OH_AudioRenderer *renderer,
int32_t bufferLen) {
auto *thiz = reinterpret_cast<PcmAudioService *>(userData);
if (bufferLen != thiz->_bufferSizeInBytes) {
__silenceData.resize(bufferLen, 0x00);
thiz->_bufferSizeInBytes = bufferLen;
thiz->_controller->updateBufferSize(thiz->_bufferSizeInBytes);
}

if (thiz->_controller->hasPlayingTacks()) {
if (thiz->_controller->isPaused()) {
memcpy(buffer, __silenceData.data(), bufferLen);
return AUDIO_DATA_CALLBACK_RESULT_INVALID;
} else {
thiz->_controller->mixOneFrame();
auto *current = thiz->_controller->current();
ALOG_ASSERT(current != nullptr, "current buffer is nullptr ...");
memcpy(buffer, current->buf, current->size < bufferLen ? current->size : bufferLen);
}
} else {
memcpy(buffer, __silenceData.data(), bufferLen);
return AUDIO_DATA_CALLBACK_RESULT_INVALID;
}

return 0;
return AUDIO_DATA_CALLBACK_RESULT_VALID;
}

int32_t PcmAudioService::AudioRendererOnInterrupt(OH_AudioRenderer *renderer,
Expand Down Expand Up @@ -126,10 +123,6 @@ bool PcmAudioService::init(AudioMixerController *controller, int numChannels, in
_bufferSizeInBytes = buffer_size * numChannels * 2;
*bufferSizeInBytes = buffer_size;

if (__silenceData.empty()) {
__silenceData.resize(_bufferSizeInBytes, 0x00);
}

ret = OH_AudioRenderer_Start(_audioRenderer);
if (ret != AUDIOSTREAM_SUCCESS) {
return false;
Expand All @@ -146,6 +139,7 @@ void PcmAudioService::pause() {

void PcmAudioService::resume() {
if (_audioRenderer != nullptr) {
OH_AudioRenderer_Flush(_audioRenderer);
OH_AudioRenderer_Start(_audioRenderer);
}
}
Expand Down
46 changes: 21 additions & 25 deletions native/cocos/bindings/jswrapper/jsvm/CommonHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,21 @@
#define NODE_API_ASSERT_RETURN_VOID(env, assertion, message) \
NODE_API_ASSERT_BASE(env, assertion, message, NODE_API_RETVAL_NOTHING)

#define NODE_API_CALL_BASE(env, the_call, ret_val) \
do { \
if ((the_call) != JSVM_OK) { \
assert(false); \
} \
#define NODE_API_CALL_BASE(env, the_call, ret_val) \
do { \
if ((the_call) != JSVM_OK) { \
se::internal::logJsException((env), __FILE_NAME__, __LINE__); \
} \
} while (0)

// Returns nullptr if the_call doesn't return JSVM_OK.
#define NODE_API_CALL(status, env, the_call) \
status = the_call; \
if (status != JSVM_OK) { \
bool isPending = false; \
if (JSVM_OK == OH_JSVM_IsExceptionPending((env), &isPending) && isPending) { \
JSVM_Value error; \
if (JSVM_OK == OH_JSVM_GetAndClearLastException((env), &error)) { \
JSVM_Value stack; \
OH_JSVM_GetNamedProperty((env), error, "stack", &stack); \
JSVM_Value message; \
OH_JSVM_GetNamedProperty((env), error, "message", &message); \
char stackstr[256]; \
OH_JSVM_GetValueStringUtf8(env, stack, stackstr, 256, nullptr); \
SE_LOGE("JSVM error stack: %s", stackstr); \
char messagestr[256]; \
OH_JSVM_GetValueStringUtf8(env, message, messagestr, 256, nullptr); \
SE_LOGE("JSVM error message: %s", messagestr); \
} \
} \
}
#define NODE_API_CALL(status, env, the_call) \
do { \
status = the_call; \
if (status != JSVM_OK) { \
se::internal::logJsException((env), __FILE_NAME__, __LINE__); \
} \
} while (0)

// Returns empty if the_call doesn't return JSVM_OK.
#define NODE_API_CALL_RETURN_VOID(env, the_call) \
Expand All @@ -79,6 +66,15 @@
#define DECLARE_NODE_API_GETTER(name, func) \
{ (name), nullptr, nullptr, (func), nullptr, nullptr, JSVM_DEFAULT, nullptr }

#define JSVM_CALL_BASE(theCall, retVal) \
do { \
if((theCall) != JSVM_OK){ \
return retVal; \
} \
} while (0)

#define JSVM_CALL_RETURN_VOID(theCall) JSVM_CALL_BASE(theCall, NODE_API_RETVAL_NOTHING)

void add_returned_status(JSVM_Env env,
const char* key,
JSVM_Value object,
Expand Down
42 changes: 31 additions & 11 deletions native/cocos/bindings/jswrapper/jsvm/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@

namespace se {
std::unique_ptr<std::unordered_map<Object*, void*>> __objectMap; // Currently, the value `void*` is always nullptr
std::set<Object*> Object::objBaseSet = {};

bool Object::restarting = false;

Object::Object() {}
Object::~Object() {
if(restarting) {
objBaseSet.insert(this);
}
if (__objectMap) {
__objectMap->erase(this);
}
Expand Down Expand Up @@ -698,32 +704,37 @@ void Object::weakCallback(JSVM_Env env, void* nativeObject, void* finalizeHint /
}
void* rawPtr = reinterpret_cast<Object*>(finalizeHint)->_privateData;
Object* seObj = reinterpret_cast<Object*>(finalizeHint);
Object* rawPtrObj = reinterpret_cast<Object*>(rawPtr);

auto it = objBaseSet.find(seObj);
if(it != objBaseSet.end()) {
return;
}

if (seObj->_onCleaingPrivateData) { //called by cleanPrivateData, not release seObj;
return;
}
if (seObj->_clearMappingInFinalizer && rawPtr != nullptr) {
auto iter = NativePtrToObjectMap::find(rawPtr);
if (iter != NativePtrToObjectMap::end()) {
if (seObj->_finalizeCb != nullptr) {
seObj->_finalizeCb(env, finalizeHint, finalizeHint);
} else {
assert(seObj->_getClass() != nullptr);
if (seObj->_getClass()->_getFinalizeFunction() != nullptr) {
seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint);
}
}
seObj->decRef();
NativePtrToObjectMap::erase(iter);
} else {
SE_LOGE("not find ptr in NativePtrToObjectMap");
}
}

if (seObj->_finalizeCb != nullptr) {
seObj->_finalizeCb(env, finalizeHint, finalizeHint);
} else {
assert(seObj->_getClass() != nullptr);
if (seObj->_getClass()->_getFinalizeFunction() != nullptr) {
seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint);
}
}
seObj->decRef();
}
}

void Object::setup() {
restarting = false;
__objectMap = std::make_unique<std::unordered_map<Object*, void*>>();
}

Expand Down Expand Up @@ -845,6 +856,15 @@ void Object::clearPrivateData(bool clearMapping) {
}
}

JSVM_Value ObjectRef::getValue(JSVM_Env env) const {
JSVM_Value result;
JSVM_Status status;
NODE_API_CALL(status, env, OH_JSVM_GetReferenceValue(env, _ref, &result));
assert(status == JSVM_OK);
assert(result != nullptr);
return result;
}

Object* Object::createUTF8String(const std::string& str) {
JSVM_Status status;
JSVM_Value result;
Expand Down
19 changes: 11 additions & 8 deletions native/cocos/bindings/jswrapper/jsvm/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
****************************************************************************/

#pragma once
#include <set>
#include <cassert>
#include "../PrivateObject.h"
#include "../RefCounter.h"
#include "../Value.h"
#include "../config.h"
#include "CommonHeader.h"
#include "HelperMacros.h"
#include "Utils.h"

namespace se {
class Class;
Expand All @@ -49,14 +51,9 @@ class ObjectRef {
~ObjectRef() {
deleteRef();
}
JSVM_Value getValue(JSVM_Env env) const {
JSVM_Value result;
JSVM_Status status;
NODE_API_CALL(status, env, OH_JSVM_GetReferenceValue(env, _ref, &result));
assert(status == JSVM_OK);
assert(result != nullptr);
return result;
}

JSVM_Value getValue(JSVM_Env env) const;

void initWeakref(JSVM_Env env, JSVM_Value obj) {
assert(_ref == nullptr);
_obj = obj;
Expand Down Expand Up @@ -121,6 +118,12 @@ class Object : public RefCounter {
BIGUINT64
};

static std::set<Object*> objBaseSet;
static bool restarting;
static void resetBaseSet() {
objBaseSet.erase(objBaseSet.begin(), objBaseSet.end());
}

using BufferContentsFreeFunc = void (*)(void *contents, size_t byteLength, void *userData);

struct ExternalArrayBufferCallbackParams {
Expand Down
Loading

0 comments on commit fd7270c

Please sign in to comment.