Skip to content

harmonyos: Port SDL3 to HarmonyOS/OpenHarmony. - #16281

Merged
icculus merged 1 commit into
libsdl-org:mainfrom
icculus:sdl3-harmonyos-squashed
Sep 15, 2026
Merged

icculus merged 1 commit into
libsdl-org:mainfrom
icculus:sdl3-harmonyos-squashed

Conversation

@icculus

@icculus icculus commented Sep 9, 2026 •

Copy link
Copy Markdown
Collaborator
  • I confirm that I am the author of this code and release it to the SDL project under the zlib license. This contribution does not contain code from other sources, including code generated by a Large Language Model ("AI").

This is my port of SDL3 to HarmonyOS/OpenHarmony. It has been tested on real hardware from Huawei, and commercial games are being prepped to ship with it already.

There is heavy documentation in docs/README-harmonyos.md, including how to set up a development environment and project, build, and debug, for the completely uninitiated.

This work was sponsored by Outfit7: https://outfit7.com/

Fixes #9837.

@icculus icculus added this to the 3.6.0 milestone Sep 9, 2026
@icculus icculus self-assigned this Sep 9, 2026
@icculus

icculus commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator Author

This has been sitting on a branch forever, so it's time to get it into a PR.

There are some pieces that are still unimplemented, but this is more than enough to ship serious games and apps. The largest missing piece is joystick support; there is a GameControllerKit API that can be used, but this has not been attempted yet. The rest of the missing pieces are minor (pen support, dialogs, etc, see the "STILL TODO" section of the readme). This is definitely quite competitive with the Android and iOS ports at this point, though.

This branch is squashed/rebased against main. All 77 commits of the work are sitting in my sdl3-harmonyos branch, but this isn't interesting except for historical purposes.

I need to verify this still works after rebasing, which I'll do tonight. We'll leave this as draft until then.

@icculus
icculus marked this pull request as draft September 9, 2026 15:45
@sezero

sezero commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

testsybols.c needs touching, like below I guess?

diff --git a/test/testsymbols.c b/test/testsymbols.c
index 3b5591e..2eb4f24 100644
--- a/test/testsymbols.c
+++ b/test/testsymbols.c
@@ -34,6 +34,12 @@ extern SDL_DECLSPEC void SDLCALL SDL_SendAndroidMessage(void);
 extern SDL_DECLSPEC void SDLCALL SDL_ShowAndroidToast(void);
 #endif
 
+#ifndef SDL_PLATFORM_OPENHARMONY
+extern SDL_DECLSPEC int SDLCALL SDL_GetOpenHarmonySDKVersion(void);
+extern SDL_DECLSPEC void SDLCALL SDL_GetOpenHarmonyInternalStoragePath(void);
+extern SDL_DECLSPEC bool SDLCALL SDL_RequestOpenHarmonyPermission(void);
+#endif
+
 #if !defined(SDL_PLATFORM_GDK)
 extern SDL_DECLSPEC void SDLCALL SDL_GDKResumeGPU(void);
 extern SDL_DECLSPEC void SDLCALL SDL_GDKSuspendGPU(void);

@MetsukiMio

MetsukiMio commented Sep 9, 2026 •

Copy link
Copy Markdown

This has been sitting on a branch forever, so it's time to get it into a PR.

There are some pieces that are still unimplemented, but this is more than enough to ship serious games and apps. The largest missing piece is joystick support; there is a GameControllerKit API that can be used, but this has not been attempted yet. The rest of the missing pieces are minor (pen support, dialogs, etc, see the "STILL TODO" section of the readme). This is definitely quite competitive with the Android and iOS ports at this point, though.

This branch is squashed/rebased against main. All 77 commits of the work are sitting in my sdl3-harmonyos branch, but this isn't interesting except for historical purposes.

I need to verify this still works after rebasing, which I'll do tonight. We'll leave this as draft until then.

So does it work on emulators?

@icculus
icculus force-pushed the sdl3-harmonyos-squashed branch from f4274cc to 8be8613 Compare September 10, 2026 00:52
@icculus

icculus commented Sep 10, 2026 •

Copy link
Copy Markdown
Collaborator Author

So does it work on emulators?

I don't know; DevEco Studio doesn't offer emulators outside of mainland China, except for smartwatches, apparently.

If it still doesn't work for you on an emulator, let's try to figure out why.

@icculus

icculus commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator Author

testsybols.c needs touching, like below I guess?

Fixed this up and a handful of other mistakes.

@MetsukiMio

Copy link
Copy Markdown

So does it work on emulators?

I don't know; DevEco Studio doesn't offer emulators outside of mainland China, except for smartwatches, apparently.

If it still doesn't work for you on an emulator, let's try to figure out why.

Seems like 2in1 device supports are missing

@MetsukiMio

Copy link
Copy Markdown

So does it work on emulators?

I don't know; DevEco Studio doesn't offer emulators outside of mainland China, except for smartwatches, apparently.

If it still doesn't work for you on an emulator, let's try to figure out why.

Simple gl test works, but it crashes with js error(input method client detached, need to be attached first), seems like initialization issues

@MetsukiMio

Copy link
Copy Markdown

So does it work on emulators?

I don't know; DevEco Studio doesn't offer emulators outside of mainland China, except for smartwatches, apparently.

If it still doesn't work for you on an emulator, let's try to figure out why.

And I hope the single app entrypoint's name is "main", not "SDL_main" (or try to load either funcs)

@icculus

icculus commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator Author

Simple gl test works, but it crashes with js error(input method client detached, need to be attached first), seems like initialization issues

We attach it when showing the keyboard...if there's a project somewhere I can look at, I'll try to reproduce the issue here.

Seems like 2in1 device supports are missing

What needs to be done for this?

And I hope the single app entrypoint's name is "main", not "SDL_main" (or try to load either funcs)

It loads "SDL_main", and expects the source file with a "main" function to include SDL_main.h, like all other platforms.

@MetsukiMio

Copy link
Copy Markdown

Simple gl test works, but it crashes with js error(input method client detached, need to be attached first), seems like initialization issues

We attach it when showing the keyboard...if there's a project somewhere I can look at, I'll try to reproduce the issue here.

Seems like 2in1 device supports are missing

What needs to be done for this?

And I hope the single app entrypoint's name is "main", not "SDL_main" (or try to load either funcs)

It loads "SDL_main", and expects the source file with a "main" function to include SDL_main.h, like all other platforms.

for the first question, the sample app in my port can reproduce it, for the second question, you can add "2in1" to the list containing "phone" (you can search it, it is inside a json file)

@icculus

icculus commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator Author

you can add "2in1" to the list containing "phone" (you can search it, it is inside a json file)

This is fixed in 16cc084, thanks!

I'll try your sample app later today and see what problems I run into.

@icculus

icculus commented Sep 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

I'll try your sample app later today and see what problems I run into.

Okay, I needed to make some changes, but I got it working:

trim.2996FBB3-5B4C-4A6C-8EBA-098E6988F6F4.MOV
  • #include SDL3/SDL_main.h or SDL won't find the entry point function.
  • Had to add argc and argv to main, or C++ thinks this is a normal C++ function and name-mangles it, so SDL can't find it in the shared library.
  • The SDL_SetHint calls aren't necessary, and caused my physical phone to find the wrong library...libGLESv2.so exists, but the phone needs libGLESv3.so, even if you use OpenGL ES 2.0. I don't know why. This might be different on the emulator, or different hardware, so we'll need to keep an eye on this for problems later.
  • The fragment shader needs precision lowp float;, or the GL refuses the shader. This is a GLES thing, and it's likely that when running an emulator using the desktop's OpenGL, it worked without it, against the GLES specification.

The specific changes I made:

--- ../SDL-OpenMinecraft/ohos-project/entry/src/main/cpp/entrypoint.cpp	2026-09-10 17:50:00.098754462 -0400
+++ openharmony-project/entry/src/main/cpp/entrypoint.cpp	2026-09-10 22:34:52.877388785 -0400
@@ -13,6 +13,7 @@
 #include "SDL3/SDL_power.h"
 #include "SDL3/SDL_dialog.h"
 #include "SDL3/SDL_sensor.h"
+#include "SDL3/SDL_main.h"
 #include <GLES2/gl2.h>
 #include <stdlib.h>
 #include <thread>
@@ -34,7 +35,7 @@
     1.f, 0.f, 0.f
 };
 
-int main()
+int main(int argc, char **argv)
 {
     /*OH_AudioStreamBuilder* builder;
     OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RENDERER);
@@ -70,9 +71,9 @@
     OH_AudioRenderer_Start(audioRenderer);
     OH_AudioStreamBuilder_Destroy(builder);*/
     
-    SDL_SetHint(SDL_HINT_EGL_LIBRARY, "libEGL.so");
-    SDL_SetHint(SDL_HINT_OPENGL_LIBRARY, "libGLESv2.so");
-    SDL_SetHint(SDL_HINT_OPENGL_ES_DRIVER, "libGLESv2.so");
+    //SDL_SetHint(SDL_HINT_EGL_LIBRARY, "libEGL.so");
+    //SDL_SetHint(SDL_HINT_OPENGL_LIBRARY, "libGLESv2.so");
+    //SDL_SetHint(SDL_HINT_OPENGL_ES_DRIVER, "libGLESv2.so");
     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
@@ -92,7 +93,7 @@
     int frgshader = ((PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader"))(GL_FRAGMENT_SHADER);
     int vexshader = ((PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader"))(GL_VERTEX_SHADER);
     
-    auto frag = "varying vec4 v_color;void main() {gl_FragColor = v_color;}";
+    auto frag = "precision lowp float; varying vec4 v_color;void main() {gl_FragColor = v_color;}";
     ((PFNGLSHADERSOURCEPROC)SDL_GL_GetProcAddress("glShaderSource"))(frgshader, 1, &frag, nullptr);
     ((PFNGLCOMPILESHADERPROC)SDL_GL_GetProcAddress("glCompileShader"))(frgshader);
     int status = 1;

@MetsukiMio

Copy link
Copy Markdown

you can add "2in1" to the list containing "phone" (you can search it, it is inside a json file)

This is fixed in 16cc084, thanks!

I'll try your sample app later today and see what problems I run into.

ime crash still happens sometimes

@icculus

icculus commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator Author

If there's a crash you can reproduce, it would be helpful if you could either catch it in the debugger, or find the stacktrace or any reasonable error messages in hilog and post it here.

@icculus
icculus force-pushed the sdl3-harmonyos-squashed branch from 16cc084 to ee7470d Compare September 14, 2026 14:28
@icculus
icculus marked this pull request as ready for review September 14, 2026 14:28
This is my port of SDL3 to HarmonyOS/OpenHarmony. It has been tested on real
hardware from Huawei, and commercial games are being prepped to ship with it
already.

There is _heavy_ documentation in docs/README-harmonyos.md, including how to
set up a development environment and project, build, and debug, for the
completely uninitiated.

This work was sponsored by Outfit7: https://outfit7.com/

Fixes libsdl-org#9837.
@icculus
icculus force-pushed the sdl3-harmonyos-squashed branch from ee7470d to 158d10f Compare September 14, 2026 14:29
@icculus

icculus commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator Author

Going to merge this tonight; other bugs that might show up can be fixed after.

@icculus
icculus merged commit a24429c into libsdl-org:main Sep 15, 2026
10 checks passed
@icculus
icculus deleted the sdl3-harmonyos-squashed branch September 15, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is there any plan to support HarmonyOS?

3 participants