Skip to content

Cocos2d x 3.17 oh #20900

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

Open
wants to merge 13 commits into
base: cocos2d-x-3.17-oh
Choose a base branch
from
83 changes: 7 additions & 76 deletions cocos/platform/ohos/CCGLViewImpl-ohos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,82 +106,13 @@ void GLViewImpl::setIMEKeyboardState(bool bOpen) {
}

Rect GLViewImpl::getSafeAreaRect() const {
Rect safeAreaRect = GLView::getSafeAreaRect();
float deviceAspectRatio = 0;
if(safeAreaRect.size.height > safeAreaRect.size.width) {
deviceAspectRatio = safeAreaRect.size.height / safeAreaRect.size.width;
} else {
deviceAspectRatio = safeAreaRect.size.width / safeAreaRect.size.height;
}

float marginX = DEFAULT_MARGIN_OHOS / _scaleX;
float marginY = DEFAULT_MARGIN_OHOS / _scaleY;

bool isScreenRound = JSFunction::getFunction("DeviceUtils.isRoundScreen").invoke<bool>();
bool hasSoftKeys = JSFunction::getFunction("DeviceUtils.hasSoftKeys").invoke<bool>();
bool isCutoutEnabled = JSFunction::getFunction("DeviceUtils.isCutoutEnable").invoke<bool>();

if(isScreenRound) {
// edge screen
if(safeAreaRect.size.width < safeAreaRect.size.height) {
safeAreaRect.origin.y += marginY * 2.f;
safeAreaRect.size.height -= (marginY * 2.f);

safeAreaRect.origin.x += marginX;
safeAreaRect.size.width -= (marginX * 2.f);
} else {
safeAreaRect.origin.y += marginY;
safeAreaRect.size.height -= (marginY * 2.f);

// landscape: no changes with X-coords
}
} else if (deviceAspectRatio >= WIDE_SCREEN_ASPECT_RATIO_OHOS) {
// almost all devices on the market have round corners
float bottomMarginIfPortrait = 0;
if(hasSoftKeys) {
bottomMarginIfPortrait = marginY * 2.f;
}

if(safeAreaRect.size.width < safeAreaRect.size.height) {
// portrait: double margin space if device has soft menu
safeAreaRect.origin.y += bottomMarginIfPortrait;
safeAreaRect.size.height -= (bottomMarginIfPortrait + marginY);
} else {
// landscape: ignore double margin at the bottom in any cases
// prepare signle margin for round corners
safeAreaRect.origin.y += marginY;
safeAreaRect.size.height -= (marginY * 2.f);
}
} else {
if(hasSoftKeys && (safeAreaRect.size.width < safeAreaRect.size.height)) {
// portrait: preserve only for soft system menu
safeAreaRect.origin.y += marginY * 2.f;
safeAreaRect.size.height -= (marginY * 2.f);
}
}

if (isCutoutEnabled) {
// screen with enabled cutout area
int orientation = JSFunction::getFunction("DeviceUtils.getOrientation").invoke<int>();

if(static_cast<int>(GLViewImpl::Orientation::PORTRAIT) == orientation) {
double height = JSFunction::getFunction("DeviceUtils.getCutoutHeight").invoke<int>() / _scaleY;
safeAreaRect.origin.y += height;
safeAreaRect.size.height -= height;
} else if(static_cast<int>(GLViewImpl::Orientation::PORTRAIT_INVERTED) == orientation) {
double height =JSFunction::getFunction("DeviceUtils.getCutoutHeight").invoke<int>() / _scaleY;
safeAreaRect.size.height -= height;
} else if(static_cast<int>(GLViewImpl::Orientation::LANDSCAPE) == orientation) {
double width = JSFunction::getFunction("DeviceUtils.getCutoutWidth").invoke<int>() / _scaleX;
safeAreaRect.size.width -= width;
} else if(static_cast<int>(GLViewImpl::Orientation::LANDSCAPE_INVERTED) == orientation) {
double width = JSFunction::getFunction("DeviceUtils.getCutoutWidth").invoke<int>() / _scaleX;
safeAreaRect.origin.x += width;
safeAreaRect.size.width -= width;
}
}

return safeAreaRect;
Rect safeAreaRect1;
safeAreaRect1.origin.x = JSFunction::getFunction("DeviceUtils.getSafeAreaLeft").invoke<int>() / _scaleX;
safeAreaRect1.origin.y = JSFunction::getFunction("DeviceUtils.getSafeAreaTop").invoke<int>() / _scaleY;
safeAreaRect1.size.width = JSFunction::getFunction("DeviceUtils.getSafeAreaWidth").invoke<int>() / _scaleX;
safeAreaRect1.size.height = JSFunction::getFunction("DeviceUtils.getSafeAreaHeight").invoke<int>() / _scaleX;
OHOS_LOGD("GLViewImpl getsafeAreaRect1, x:%{public}f, y:%{public}f, width:%{public}f, height:%{public}f", safeAreaRect1.origin.x, safeAreaRect1.origin.y, safeAreaRect1.size.width, safeAreaRect1.size.height);
return safeAreaRect1;
}
NS_CC_END

Expand Down
5 changes: 2 additions & 3 deletions cocos/platform/ohos/CCGLViewImpl-ohos.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

NS_CC_BEGIN

class CC_DLL GLViewImpl : public GLView
{
class CC_DLL GLViewImpl : public GLView {
public:
enum class Orientation {
PORTRAIT = 0,
Expand Down Expand Up @@ -48,4 +47,4 @@ class CC_DLL GLViewImpl : public GLView

NS_CC_END
#endif // end of CC_TARGET_PLATFORM == CC_PLATFORM_OHOS
#endif // end of __CC_EGLVIEW_ANDROID_H__
#endif // end of __CC_EGLVIEWIMPL_OHOS_H__
6 changes: 1 addition & 5 deletions cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include "platform/ohos/CCLogOhos.h"

napi_env Js_Cocos2dxHelper::_env = nullptr;
napi_value Js_Cocos2dxHelper::initJsCocos2dxHelper(napi_env env, napi_callback_info info)
{
napi_value Js_Cocos2dxHelper::initJsCocos2dxHelper(napi_env env, napi_callback_info info) {
_env = env;
return 0;
}
Expand All @@ -15,7 +14,6 @@ napi_value Js_Cocos2dxHelper::initJsCocos2dxHelper(napi_env env, napi_callback_i
* If you have more information that can be obtained asynchronously, add it here.
*/
napi_value Js_Cocos2dxHelper::initAsyncInfo(napi_env env, napi_callback_info info) {
JSFunction::getFunction("DeviceUtils.initScreenInfo").invoke<void>();
return nullptr;
}

Expand All @@ -24,5 +22,3 @@ std::string Js_Cocos2dxHelper::_asyncInfoMap[AsyncInfo::LAST_INDEX];
void Js_Cocos2dxHelper::terminateProcess() {
JSFunction::getFunction("ApplicationManager.exit").invoke<void>();
}


5 changes: 2 additions & 3 deletions cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

enum AsyncInfo {
// VERSION_NAME = 0,
LAST_INDEX // 仅为表明用于保存数据的数组大小,无意义,后续如新增枚举,请保持 LAST_INDEX 在最后一个
LAST_INDEX // Only indicates the size of the array used for storing data. It is meaningless. If an enumeration is added later, keep LAST_INDEX at the end.
};

class Js_Cocos2dxHelper
{
class Js_Cocos2dxHelper {
public:
static napi_value initJsCocos2dxHelper(napi_env env, napi_callback_info info);
static napi_value initAsyncInfo(napi_env env, napi_callback_info info);
Expand Down
27 changes: 22 additions & 5 deletions cocos/platform/ohos/napi/helper/NapiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
const static int BUFFER_SIZE = 1024 * 10;

// Defines locks and semaphores.
typedef struct ThreadLockInfo
{
typedef struct ThreadLockInfo {
std::mutex mutex;
std::condition_variable condition;
bool ready = false;
} ThreadLockInfo;

typedef struct WorkParam
{
typedef struct WorkParam {
napi_env env;
napi_ref funcRef;
std::string inParam;
Expand Down Expand Up @@ -116,7 +114,11 @@ class JSFunction {
napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...};
napi_value return_val;
status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val);

if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
ReturnType value;
if (!NapiValueConverter::ToCppValue(env, return_val, value)) {
// Handle error here
Expand All @@ -138,6 +140,11 @@ class JSFunction {
napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...};
napi_value return_val;
status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}

static void callFunctionWithParams(WorkParam *param) {
Expand Down Expand Up @@ -169,6 +176,11 @@ class JSFunction {
}
if (status != napi_ok) {
LOGI("XXXXXX:napi_call_function getClassObject != napi_ok %{public}d", status);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}

napi_value thenFunc = nullptr;
Expand All @@ -189,6 +201,11 @@ class JSFunction {
status = napi_call_function(env, promise, thenFunc, 1, &successFunc, &ret);
if (status != napi_ok) {
LOGI("XXXXXX:napi_call_function thenFunc failed, ret: %{public}d", status);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}
}
// Callback Function Type
Expand Down
3 changes: 1 addition & 2 deletions cocos/platform/ohos/napi/modules/RawFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
#include <rawfile/raw_file.h>
#include <rawfile/raw_file_manager.h>

class RawFileUtils
{
class RawFileUtils {
public:
static bool InitResourceManager(napi_env env, napi_value info);

Expand Down
7 changes: 6 additions & 1 deletion cocos/platform/ohos/napi/plugin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) {

int64_t value;
NAPI_CALL(env, napi_get_value_int64(env, args[0], &value));

napi_handle_scope scope = nullptr;
NAPI_CALL(env, napi_open_handle_scope(env, &scope));
if(scope == nullptr){
return nullptr;
}
NAPI_CALL(env, napi_create_object(env, &exports));

switch (value) {
Expand Down Expand Up @@ -164,6 +168,7 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) {
default:
OHOS_LOGE("unknown type");
}
NAPI_CALL(env, napi_close_handle_scope(env, scope));
return exports;
}

Expand Down
14 changes: 5 additions & 9 deletions cocos/platform/ohos/napi/render/egl_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ EGLConfig getConfig(int version, EGLDisplay eglDisplay) {
return configs;
}

void EGLCore::GLContextInit(void* window, int w, int h)
{
void EGLCore::GLContextInit(void* window, int w, int h) {
OHOS_LOGD("EGLCore::GLContextInit window = %{public}p, w = %{public}d, h = %{public}d.", window, w, h);
width_ = w;
height_ = h;
Expand All @@ -54,9 +53,8 @@ void EGLCore::GLContextInit(void* window, int w, int h)
}

// 2. Create EGL Surface from Native Window
EGLint winAttribs[] = {EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_LINEAR_KHR, EGL_NONE};
if (mEglWindow) {
mEGLSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mEglWindow, winAttribs);
mEGLSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mEglWindow, nullptr);
if (mEGLSurface == nullptr) {
OHOS_LOGE("EGLCore::eglCreateContext eglSurface is null");
return;
Expand All @@ -76,13 +74,11 @@ void EGLCore::GLContextInit(void* window, int w, int h)
}
}

void EGLCore::Update()
{
void EGLCore::Update() {
eglSwapBuffers(mEGLDisplay, mEGLSurface);
}

bool EGLCore::checkGlError(const char* op)
{
bool EGLCore::checkGlError(const char* op) {
OHOS_LOGE("EGL ERROR CODE = %{public}x", eglGetError());
GLint error;
for (error = glGetError(); error; error = glGetError()) {
Expand Down Expand Up @@ -113,4 +109,4 @@ void EGLCore::createSurface(void* window) {
OHOS_LOGE("eglMakeCurrent error = %{public}d", eglGetError());
}
return;
}
}
26 changes: 22 additions & 4 deletions cocos/platform/ohos/napi/render/plugin_render.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <ace/xcomponent/native_xcomponent_key_event.h>
#include <stdint.h>
#include <unistd.h>

Expand Down Expand Up @@ -31,7 +32,7 @@ float mousePositionY = -1;
bool isMouseLeftActive = false;
double scrollDistance = 0;

std::unordered_map<int, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
const std::unordered_map<OH_NativeXComponent_KeyCode, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
{KEY_ESCAPE, cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE},
{KEY_GRAVE, cocos2d::EventKeyboard::KeyCode::KEY_GRAVE},
{KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS},
Expand Down Expand Up @@ -61,7 +62,23 @@ std::unordered_map<int, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
{KEY_DPAD_UP, cocos2d::EventKeyboard::KeyCode::KEY_DPAD_UP},
{KEY_SYSRQ, cocos2d::EventKeyboard::KeyCode::KEY_PRINT},
{KEY_INSERT, cocos2d::EventKeyboard::KeyCode::KEY_INSERT},
{KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE}
{KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE},
{KEY_SCROLL_LOCK, cocos2d::EventKeyboard::KeyCode::KEY_SCROLL_LOCK},
{KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS},
{KEY_AT, cocos2d::EventKeyboard::KeyCode::KEY_AT},
{KEY_PLUS, cocos2d::EventKeyboard::KeyCode::KEY_PLUS},
{KEY_MENU, cocos2d::EventKeyboard::KeyCode::KEY_MENU},
{KEY_BREAK, cocos2d::EventKeyboard::KeyCode::KEY_PAUSE},
{KEY_MOVE_HOME, cocos2d::EventKeyboard::KeyCode::KEY_HOME},
{KEY_MOVE_END, cocos2d::EventKeyboard::KeyCode::KEY_END},
{KEY_PAGE_UP, cocos2d::EventKeyboard::KeyCode::KEY_PG_UP},
{KEY_PAGE_DOWN, cocos2d::EventKeyboard::KeyCode::KEY_PG_DOWN},
{KEY_NUMPAD_ADD, cocos2d::EventKeyboard::KeyCode::KEY_KP_PLUS},
{KEY_NUMPAD_SUBTRACT, cocos2d::EventKeyboard::KeyCode::KEY_KP_MINUS},
{KEY_NUMPAD_MULTIPLY, cocos2d::EventKeyboard::KeyCode::KEY_KP_MULTIPLY},
{KEY_NUMPAD_DIVIDE, cocos2d::EventKeyboard::KeyCode::KEY_KP_DIVIDE},
{KEY_NUMPAD_ENTER, cocos2d::EventKeyboard::KeyCode::KEY_KP_ENTER}

};

cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyCode ohKeyCode)
Expand All @@ -81,8 +98,9 @@ cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyC
if (ohKeyCode >= KEY_A && ohKeyCode <= KEY_Z) {
// A - Z
return cocos2d::EventKeyboard::KeyCode(int(cocos2d::EventKeyboard::KeyCode::KEY_A) + (ohKeyCode - KEY_A));
}
return cocos2d::EventKeyboard::KeyCode(ohKeyCode);
}
OHOS_LOGW("Unmapped OH key code: %d", ohKeyCode);
return cocos2d::EventKeyboard::KeyCode::KEY_NONE;
}

void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)
Expand Down