Skip to content

HarmonyOS Port (Stage 2) #13064

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

Closed
wants to merge 19 commits into from
Closed
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
33 changes: 33 additions & 0 deletions .github/actions/setup-harmony-toolchain/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: 'Setup Harmony toolchain'
inputs:
version:
description: 'Harmony version'
default: '5.0.0-Release'
runs:
using: 'composite'
steps:
- uses: actions/cache/restore@v4
id: restore-cache
with:
path: /opt/native
key: harmony-${{ inputs.version }}

- name: Download Harmony toolchain
if: ${{ !steps.restore-cache.outputs.cache-hit }}
shell: bash
run: |
wget https://repo.huaweicloud.com/openharmony/os/${{ inputs.version }}/ohos-sdk-windows_linux-public.tar.gz
tar -zxvf ohos-sdk-windows_linux-public.tar.gz
mkdir -p /opt

unzip linux/native*.zip -d /opt
- uses: actions/cache/save@v4
if: ${{ !steps.restore-cache.outputs.cache-hit }}
with:
path: /opt/native
key: harmony-${{ inputs.version }}
- name: 'Set output vars'
id: final
shell: bash
run: |
echo "HARMONY_NATIVE_SDK=/opt/native" >> $GITHUB_OUTPUT
90 changes: 51 additions & 39 deletions .github/workflows/create-test-plan.py

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .github/workflows/generic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ jobs:
uses: ./.github/actions/setup-msvc-libusb
with:
arch: ${{ matrix.platform.setup-libusb-arch }}
- name: 'Set up Harmony toolchain'
if: ${{ matrix.platform.platform == 'harmony' }}
uses: ./.github/actions/setup-harmony-toolchain
- uses: mymindstorm/setup-emsdk@v14
if: ${{ matrix.platform.platform == 'emscripten' }}
with:
Expand Down Expand Up @@ -107,6 +110,7 @@ jobs:
uses: ./.github/actions/setup-loongarch64-toolchain
id: setup-loongarch64-toolchain
if: ${{ matrix.platform.platform == 'loongarch64' }}

- name: 'Setup Intel oneAPI toolchain'
id: intel
if: ${{ matrix.platform.intel }}
Expand Down
41 changes: 40 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ if(UNIX OR APPLE)
endif()
endif()

if(UNIX OR APPLE OR HAIKU OR RISCOS)
if(UNIX OR APPLE OR HAIKU OR RISCOS OR OHOS)
CheckO_CLOEXEC()
endif()

Expand Down Expand Up @@ -1502,7 +1502,46 @@ if(ANDROID)
endif()
endif()
endif()
elseif(OHOS)
# disable warnings from the toolchain
sdl_compile_options(PRIVATE "-Wno-unused-command-line-argument")

set(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN "")
set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN "")

sdl_link_dependency(OHOS_LIBS LIBS ace_napi.z hilog_ndk.z ace_ndk.z rawfile.z pixelmap_ndk.z)

sdl_glob_sources("${SDL3_SOURCE_DIR}/src/core/ohos/*.c")

if(SDL_VIDEO)
set(SDL_VIDEO_DRIVER_OHOS 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/video/ohos/*.c")
set(HAVE_SDL_VIDEO TRUE)

if(SDL_OPENGLES)
set(SDL_VIDEO_OPENGL_EGL 1)
set(HAVE_OPENGLES TRUE)
# set(SDL_VIDEO_OPENGL_ES 1)
set(SDL_VIDEO_OPENGL_ES2 1)
set(SDL_VIDEO_RENDER_OGL_ES2 1)

sdl_link_dependency(opengles LIBS EGL GLESv2)
endif()
endif()

set(SDL_LOADSO_DLOPEN 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/loadso/dlopen/*.c")
set(HAVE_SDL_LOADSO TRUE)

set(SDL_TIME_UNIX 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/time/unix/*.c")
set(HAVE_SDL_TIME TRUE)

set(SDL_TIMER_UNIX 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/timer/unix/*.c")
set(HAVE_SDL_TIMERS TRUE)

CheckPTHREAD()
elseif(EMSCRIPTEN)
# Hide noisy warnings that intend to aid mostly during initial stages of porting a new
# project. Uncomment at will for verbose cross-compiling -I/../ path info.
Expand Down
2 changes: 1 addition & 1 deletion cmake/macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ function(SDL_PrintSummary)
message(STATUS "")
endif()

if(UNIX AND NOT (ANDROID OR APPLE OR EMSCRIPTEN OR HAIKU OR RISCOS))
if(UNIX AND NOT (ANDROID OR APPLE OR EMSCRIPTEN OR HAIKU OR RISCOS OR OHOS))
if(NOT (HAVE_X11 OR HAVE_WAYLAND))
if(NOT SDL_UNIX_CONSOLE_BUILD)
message(FATAL_ERROR
Expand Down
5 changes: 5 additions & 0 deletions include/SDL3/SDL_platform_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
#undef SDL_PLATFORM_LINUX
#endif

#if defined(OHOS) || defined(__OHOS__)
#define SDL_PLATFORM_OHOS 1
#undef SDL_PLATFORM_LINUX
#endif

#if defined(__unix__) || defined(__unix) || defined(unix)

/**
Expand Down
37 changes: 37 additions & 0 deletions src/SDL_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#include <android/log.h>
#endif

#ifdef SDL_PLATFORM_OHOS
#include <hilog/log.h>
#endif

#include "stdlib/SDL_vacopy.h"

// The size of the stack buffer to use for rendering log messages.
Expand Down Expand Up @@ -120,6 +124,18 @@ static int SDL_android_priority[] = {
SDL_COMPILE_TIME_ASSERT(android_priority, SDL_arraysize(SDL_android_priority) == SDL_LOG_PRIORITY_COUNT);
#endif // SDL_PLATFORM_ANDROID

#ifdef SDL_PLATFORM_OHOS
static int SDL_ohos_priority[] = {
LOG_DEBUG,
LOG_DEBUG,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL
};
#endif

static void SDLCALL SDL_LoggingChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_ResetLogPriorities();
Expand Down Expand Up @@ -569,6 +585,20 @@ static const char *GetCategoryPrefix(int category)
}
#endif // SDL_PLATFORM_ANDROID

#ifdef SDL_PLATFORM_OHOS
static const char *
GetCategoryPrefix(int category)
Comment on lines +589 to +590
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just re-use the Android category prefix helper function -> #if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OHOS)

{
if (category < SDL_LOG_CATEGORY_RESERVED2) {
return SDL_category_names[category];
}
if (category < SDL_LOG_CATEGORY_CUSTOM) {
return "RESERVED";
}
return "CUSTOM";
}
#endif

void SDL_LogMessageV(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
{
char *message = NULL;
Expand Down Expand Up @@ -751,6 +781,13 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
__android_log_write(SDL_android_priority[priority], tag, message);
}
#elif defined(SDL_PLATFORM_OHOS)
{
char tag[32];

SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
OH_LOG_Print(LOG_APP, SDL_ohos_priority[priority], 0, tag, "%{public}s", message);
}
#elif defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT))
/* Technically we don't need Cocoa/UIKit, but that's where this function is defined for now.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/audio/SDL_audiotypecvt.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,19 @@ static void SDL_TARGETING("ssse3") SDL_Convert_Swap32_SSSE3(Uint32* dst, const U
// behavior. However, the compiler support for this pragma is bad.
#if defined(__clang__)
#if __clang_major__ >= 12
#ifndef SDL_PLATFORM_OHOS
#pragma STDC FENV_ACCESS ON
#endif
#endif
#elif defined(_MSC_VER)
#pragma fenv_access (on)
#elif defined(__GNUC__)
// GCC does not support the pragma at all
#else
#ifndef SDL_PLATFORM_OHOS
#pragma STDC FENV_ACCESS ON
#endif
#endif

static void SDL_Convert_S8_to_F32_NEON(float *dst, const Sint8 *src, int num_samples)
{
Expand Down Expand Up @@ -814,15 +818,19 @@ static void SDL_Convert_Swap32_NEON(Uint32* dst, const Uint32* src, int num_samp

#if defined(__clang__)
#if __clang_major__ >= 12
#ifndef SDL_PLATFORM_OHOS
#pragma STDC FENV_ACCESS DEFAULT
#endif
#endif
#elif defined(_MSC_VER)
#pragma fenv_access (off)
#elif defined(__GNUC__)
//
#else
#ifndef SDL_PLATFORM_OHOS
#pragma STDC FENV_ACCESS DEFAULT
#endif
#endif

#endif

Expand Down
126 changes: 126 additions & 0 deletions src/core/ohos/SDL_ohos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include "SDL_ohos.h"
#include "SDL_internal.h"

#ifdef SDL_PLATFORM_OHOS

#define OHOS_DELAY_FIFTY 50
#define OHOS_DELAY_TEN 10
#define OHOS_START_ARGS_INDEX 2
#define OHOS_INDEX_ARG0 0
#define OHOS_INDEX_ARG1 1
#define OHOS_INDEX_ARG2 2
#define OHOS_INDEX_ARG3 3
#define OHOS_INDEX_ARG4 4
#define OHOS_INDEX_ARG5 5
#define OHOS_INDEX_ARG6 6

#include "napi/native_api.h"
#include <ace/xcomponent/native_interface_xcomponent.h>

OHNativeWindow *nativeWindow;
SDL_WindowData *windowData;
SDL_DisplayOrientation displayOrientation;
static OH_NativeXComponent_Callback callback;
static OH_NativeXComponent_MouseEvent_Callback mouseCallback;

void SDL_OHOS_SetDisplayOrientation(int orientation)
{
displayOrientation = (SDL_DisplayOrientation)orientation;
}

SDL_DisplayOrientation SDL_OHOS_GetDisplayOrientation()
{
return displayOrientation;
}

static napi_value add(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2] = { NULL };

napi_get_cb_info(env, info, &argc, args, NULL, NULL);

napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);

napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);

double value0;
napi_get_value_double(env, args[0], &value0);

double value1;
napi_get_value_double(env, args[1], &value1);

napi_value sum;
napi_create_double(env, value0 + value1, &sum);

return sum;
}

static void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)
{
nativeWindow = (OHNativeWindow *)window;
}
static void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window) {}
static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) {}
static void onKeyEvent(OH_NativeXComponent *component, void *window) {}
static void onNativeTouch(OH_NativeXComponent *component, void *window) {}
static void onNativeMouse(OH_NativeXComponent *component, void *window) {}
static void OnDispatchTouchEventCB(OH_NativeXComponent *component, void *window) {}
void OnHoverEvent(OH_NativeXComponent *component, bool isHover) {}
void OnFocusEvent(OH_NativeXComponent *component, void *window) {}
void OnBlurEvent(OH_NativeXComponent *component, void *window) {}

static napi_value SDL_OHOS_NAPI_Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "add", NULL, add, NULL, NULL, NULL, napi_default, NULL }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);

napi_value exportInstance = NULL;
if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
return exports;
}
OH_NativeXComponent *nativeXComponent;
if (napi_unwrap(env, exportInstance, (void **)(&nativeXComponent)) != napi_ok) {
return exports;
}

callback.OnSurfaceCreated = OnSurfaceCreatedCB;
callback.OnSurfaceChanged = OnSurfaceChangedCB;
callback.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
callback.DispatchTouchEvent = onNativeTouch;
OH_NativeXComponent_RegisterCallback(nativeXComponent, &callback);

mouseCallback.DispatchMouseEvent = OnDispatchTouchEventCB;
mouseCallback.DispatchMouseEvent = onNativeMouse;
mouseCallback.DispatchHoverEvent = OnHoverEvent;
OH_NativeXComponent_RegisterMouseEventCallback(nativeXComponent, &mouseCallback);

OH_NativeXComponent_RegisterKeyEventCallback(nativeXComponent, onKeyEvent);
OH_NativeXComponent_RegisterFocusEventCallback(nativeXComponent, OnFocusEvent);
OH_NativeXComponent_RegisterBlurEventCallback(nativeXComponent, OnBlurEvent);

return exports;
}

napi_module OHOS_NAPI_Module = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = NULL,
.nm_register_func = SDL_OHOS_NAPI_Init,
.nm_modname = "SDL3",
.nm_priv = ((void *)0),
.reserved = { 0 },
};

__attribute__((constructor)) void RegisterEntryModule(void)
{
napi_module_register(&OHOS_NAPI_Module);
}

#endif /* SDL_PLATFORM_OHOS */

/* vi: set ts=4 sw=4 expandtab: */
12 changes: 12 additions & 0 deletions src/core/ohos/SDL_ohos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef SDL_OHOS_H
#define SDL_OHOS_H

#include "SDL3/SDL_mutex.h"
#include "video/SDL_sysvideo.h"
#include <native_window/external_window.h>

void SDL_OHOS_SetDisplayOrientation(int orientation);
SDL_DisplayOrientation SDL_OHOS_GetDisplayOrientation();
extern OHNativeWindow *nativeWindow;

#endif
Loading