From 590509b34dad1fa63f975d765397367aee9950b6 Mon Sep 17 00:00:00 2001 From: Jack253-png Date: Sun, 1 Jun 2025 07:22:00 +0800 Subject: [PATCH] Harmony port: review changes --- src/core/ohos/SDL_ohos.c | 57 +++++++++++++++++++-------------- src/core/ohos/SDL_ohos.h | 5 +-- src/video/ohos/SDL_ohosgl.c | 6 ++-- src/video/ohos/SDL_ohosvideo.h | 4 +-- src/video/ohos/SDL_ohosvulkan.c | 3 +- src/video/ohos/SDL_ohoswindow.c | 40 +++++++++-------------- 6 files changed, 57 insertions(+), 58 deletions(-) diff --git a/src/core/ohos/SDL_ohos.c b/src/core/ohos/SDL_ohos.c index e7fc71d358..32481d0f21 100644 --- a/src/core/ohos/SDL_ohos.c +++ b/src/core/ohos/SDL_ohos.c @@ -15,11 +15,11 @@ #include "SDL3/SDL_mutex.h" #include "../../video/ohos/SDL_ohoskeyboard.h" -OHNativeWindow *g_ohosNativeWindow; +static OHNativeWindow *g_ohosNativeWindow; SDL_Mutex *g_ohosPageMutex = NULL; static OH_NativeXComponent_Callback callback; static OH_NativeXComponent_MouseEvent_Callback mouseCallback; -SDL_WindowData windowData; +static int x, y, wid, hei; static struct { napi_env env; @@ -51,6 +51,29 @@ typedef struct napiCallbackArg arg8; } napiCallbackData; +void OHOS_windowDataFill(SDL_Window* w) +{ + w->internal = SDL_calloc(1, sizeof(SDL_WindowData)); + w->x = x; + w->y = y; + w->w = wid; + w->h = hei; + w->internal->native_window = g_ohosNativeWindow; + + SDL_VideoDevice *_this = SDL_GetVideoDevice(); + +#ifdef SDL_VIDEO_OPENGL_EGL + if (w->flags & SDL_WINDOW_OPENGL) { + SDL_LockMutex(g_ohosPageMutex); + if (w->internal->egl_surface == EGL_NO_SURFACE) + { + w->internal->egl_surface = SDL_EGL_CreateSurface(_this, w, (NativeWindowType)g_ohosNativeWindow); + } + SDL_UnlockMutex(g_ohosPageMutex); + } +#endif +} + static napi_value minus(napi_env env, napi_callback_info info) { size_t argc = 2; @@ -112,11 +135,10 @@ static void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window) OH_NativeXComponent_GetXComponentOffset(component, window, &offsetX, &offsetY); SDL_LockMutex(g_ohosPageMutex); - windowData.native_window = g_ohosNativeWindow; - windowData.width = width; - windowData.height = height; - windowData.x = offsetX; - windowData.y = offsetY; + wid = width; + hei = height; + x = (int)offsetX; + y = (int)offsetY; SDL_UnlockMutex(g_ohosPageMutex); } static void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window) @@ -131,27 +153,14 @@ static void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window) OH_NativeXComponent_GetXComponentOffset(component, window, &offsetX, &offsetY); SDL_LockMutex(g_ohosPageMutex); - windowData.native_window = g_ohosNativeWindow; - windowData.width = width; - windowData.height = height; - windowData.x = offsetX; - windowData.y = offsetY; - + wid = width; + hei = height; + x = (int)offsetX; + y = (int)offsetY; SDL_UnlockMutex(g_ohosPageMutex); } static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) { - SDL_VideoDevice* _this = SDL_GetVideoDevice(); -#ifdef SDL_VIDEO_OPENGL_EGL - if (windowData.egl_context) - { - SDL_EGL_DestroyContext(_this, windowData.egl_context); - } - if (windowData.egl_xcomponent) - { - SDL_EGL_DestroySurface(_this, windowData.egl_xcomponent); - } -#endif } static void onKeyEvent(OH_NativeXComponent *component, void *window) { diff --git a/src/core/ohos/SDL_ohos.h b/src/core/ohos/SDL_ohos.h index 5c7e633697..967f248491 100644 --- a/src/core/ohos/SDL_ohos.h +++ b/src/core/ohos/SDL_ohos.h @@ -2,12 +2,13 @@ #define SDL_OHOS_H #include "SDL3/SDL_mutex.h" +#include "SDL3/SDL_video.h" #include "video/SDL_sysvideo.h" #include extern SDL_Mutex *g_ohosPageMutex; -extern OHNativeWindow *g_ohosNativeWindow; -extern SDL_WindowData windowData; + +void OHOS_windowDataFill(SDL_Window* w); typedef struct SDL_VideoData { SDL_Rect textRect; diff --git a/src/video/ohos/SDL_ohosgl.c b/src/video/ohos/SDL_ohosgl.c index bc4398e5a2..7f8bd92158 100644 --- a/src/video/ohos/SDL_ohosgl.c +++ b/src/video/ohos/SDL_ohosgl.c @@ -7,7 +7,7 @@ bool OHOS_GLES_MakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLCon { if (window && context) { - return SDL_EGL_MakeCurrent(_this, window->internal->egl_xcomponent, context); + return SDL_EGL_MakeCurrent(_this, window->internal->egl_surface, context); } else { @@ -21,7 +21,7 @@ SDL_GLContext OHOS_GLES_CreateContext(SDL_VideoDevice *_this, SDL_Window *window SDL_LockMutex(g_ohosPageMutex); - result = SDL_EGL_CreateContext(_this, window->internal->egl_xcomponent); + result = SDL_EGL_CreateContext(_this, window->internal->egl_surface); SDL_UnlockMutex(g_ohosPageMutex); @@ -34,7 +34,7 @@ bool OHOS_GLES_SwapWindow(SDL_VideoDevice *_this, SDL_Window *window) SDL_LockMutex(g_ohosPageMutex); - result = SDL_EGL_SwapBuffers(_this, window->internal->egl_xcomponent); + result = SDL_EGL_SwapBuffers(_this, window->internal->egl_surface); SDL_UnlockMutex(g_ohosPageMutex); diff --git a/src/video/ohos/SDL_ohosvideo.h b/src/video/ohos/SDL_ohosvideo.h index a402429b6e..a27ee2e226 100644 --- a/src/video/ohos/SDL_ohosvideo.h +++ b/src/video/ohos/SDL_ohosvideo.h @@ -5,15 +5,13 @@ #include "../../core/ohos/SDL_ohos.h" struct SDL_WindowData { #ifdef SDL_VIDEO_OPENGL_EGL - EGLSurface egl_xcomponent; + EGLSurface egl_surface; EGLContext egl_context; #endif bool backup_done; OHNativeWindow *native_window; uint64_t width; uint64_t height; - double x; - double y; }; #endif diff --git a/src/video/ohos/SDL_ohosvulkan.c b/src/video/ohos/SDL_ohosvulkan.c index 9721181d9a..ca2407497d 100644 --- a/src/video/ohos/SDL_ohosvulkan.c +++ b/src/video/ohos/SDL_ohosvulkan.c @@ -8,6 +8,7 @@ #include "../SDL_sysvideo.h" #include "../../core/ohos/SDL_ohos.h" #include "vulkan/vulkan_ohos.h" +#include "SDL_ohosvideo.h" #include static int loadedCount = 0; @@ -111,7 +112,7 @@ bool OHOS_Vulkan_CreateSurface(SDL_VideoDevice *_this, createInfo.sType = VK_STRUCTURE_TYPE_SURFACE_CREATE_INFO_OHOS; createInfo.pNext = NULL; createInfo.flags = 0; - createInfo.window = g_ohosNativeWindow; + createInfo.window = window->internal->native_window; result = vkCreateSurfaceOHOS(instance, &createInfo, NULL, surface); if (result != VK_SUCCESS) { SDL_SetError("vkCreateSurfaceOHOS failed: %d", result); diff --git a/src/video/ohos/SDL_ohoswindow.c b/src/video/ohos/SDL_ohoswindow.c index 33a44dc309..8d0958fb08 100644 --- a/src/video/ohos/SDL_ohoswindow.c +++ b/src/video/ohos/SDL_ohoswindow.c @@ -1,4 +1,5 @@ #include "SDL_ohoswindow.h" +#include "SDL_internal.h" #include #ifdef SDL_VIDEO_DRIVER_OHOS @@ -6,19 +7,7 @@ #include "SDL_ohosvideo.h" bool OHOS_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props) { - window->internal = &windowData; -#ifdef SDL_VIDEO_OPENGL_EGL - if (window->flags & SDL_WINDOW_OPENGL) { - SDL_LockMutex(g_ohosPageMutex); - if (window->internal->egl_xcomponent == EGL_NO_SURFACE) - { - window->internal->egl_xcomponent = SDL_EGL_CreateSurface(_this, window, (NativeWindowType)window->internal->native_window); - } - SDL_UnlockMutex(g_ohosPageMutex); - } -#endif - window->x = (int)window->internal->x; - window->y = (int)window->internal->y; + OHOS_windowDataFill(window); return true; } @@ -26,19 +15,20 @@ bool OHOS_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_Propertie void OHOS_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window) { #ifdef SDL_VIDEO_OPENGL_EGL - if (window->flags & SDL_WINDOW_OPENGL) { - SDL_LockMutex(g_ohosPageMutex); - if (window->internal->egl_context) - { - SDL_EGL_DestroyContext(_this, window->internal->egl_context); + if (window->flags & SDL_WINDOW_OPENGL) { + SDL_LockMutex(g_ohosPageMutex); + if (window->internal->egl_context) + { + SDL_EGL_DestroyContext(_this, window->internal->egl_context); + } + if (window->internal->egl_surface != EGL_NO_SURFACE) + { + SDL_EGL_DestroySurface(_this, window->internal->egl_surface); + } + SDL_UnlockMutex(g_ohosPageMutex); } - if (window->internal->egl_xcomponent != EGL_NO_SURFACE) - { - SDL_EGL_DestroySurface(_this, window->internal->egl_xcomponent); - } - SDL_UnlockMutex(g_ohosPageMutex); - } -#endif + SDL_free(window->internal); + #endif } #endif