From 3497df47e5b1b1a14176d458fe67a1fd8200f86d Mon Sep 17 00:00:00 2001 From: duanjf <605126199@qq.com> Date: Wed, 20 Mar 2024 14:26:57 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=85=89?= =?UTF-8?q?=E6=A0=87=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 3 +- .../entry/src/main/ets/service/SdlModule.ts | 26 +++++++++++ src/core/ohos/SDL_ohos.cpp | 12 +++-- src/core/ohos/SDL_ohosthreadsafe.cpp | 44 ++++++++++++++++--- 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48b0f2747..ba08c2b8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1042,7 +1042,8 @@ elseif(OHOS) find_library(OHOS_LOG_LIBRARY NAMES libhilog_ndk.z.so hilog) find_library(OHOS_NDK_LIBRARY NAMES libace_ndk.z.so ndk) find_library(OHOS_RAWFILE_LIBRARY NAMES librawfile.z.so rawfile) - list(APPEND EXTRA_LIBS ${OHOS_ACE_LIBRARY} ${OHOS_LOG_LIBRARY} ${OHOS_NDK_LIBRARY} ${OHOS_RAWFILE_LIBRARY}) + find_library(OHOS_PIXELMAP_LIBRARY NAMES libpixelmap_ndk.z.so pixelmap) + list(APPEND EXTRA_LIBS ${OHOS_ACE_LIBRARY} ${OHOS_LOG_LIBRARY} ${OHOS_NDK_LIBRARY} ${OHOS_RAWFILE_LIBRARY} ${OHOS_PIXELMAP_LIBRARY}) add_definitions(-DOHOS_PLATFORM) file(GLOB OHOS_CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/ohos/*.c) file(GLOB OHOS_MAIN_SOURCES_CPP ${SDL2_SOURCE_DIR}/src/core/ohos/*.cpp) diff --git a/ohos-project/entry/src/main/ets/service/SdlModule.ts b/ohos-project/entry/src/main/ets/service/SdlModule.ts index f8e578fc8..e9e2537c3 100644 --- a/ohos-project/entry/src/main/ets/service/SdlModule.ts +++ b/ohos-project/entry/src/main/ets/service/SdlModule.ts @@ -27,6 +27,7 @@ import common from '@ohos.app.ability.common' import window from '@ohos.window' import { BusinessError } from '@ohos.base' import { Context } from '@ohos.abilityAccessCtrl' +import image from '@ohos.multimedia.image' export let sdlPageContext: common.UIAbilityContext @@ -46,9 +47,34 @@ export interface NapiCallback { requestPermission(permission: string): void; setPointer(cursorID: number): void; + + setCustomCursorandCreate(pixelmap_test: image.PixelMap, focusX: number, focusY: number): void; } export class ArkNapiCallback implements NapiCallback { + setCustomCursorandCreate(pixelmap_test: image.PixelMap, focusX: number, focusY: number) { + window.getLastWindow(sdlPageContext, (error: BusinessError, windowClass: window.Window) => { + if (error.code) { + console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(error)) + return + } + let windowId = windowClass.getWindowProperties().id + if (windowId < 0) { + console.log(`Invalid windowId`) + return + } + try { + pointer.setCustomCursor(windowId, pixelmap_test, focusX, focusY).then(() => { + console.log(`Successfully set custom pointer`) + }).catch(error => { + console.log('Failed to set the custom pointer, error=' + error) + }) + } catch (error) { + console.log(`Failed to set the custom pointer, error=${JSON.stringify(error)}, msg=${JSON.stringify(`message`)}`) + } + }) + } + setPointer(cursorID: number) { const SDL_SYSTEM_CURSOR_NONE = -1 const SDL_SYSTEM_CURSOR_ARROW = 0 diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index 3770aafda..35d261870 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -243,12 +243,18 @@ OHOS_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y) return -1; } cJSON_AddNumberToObject(root, OHOS_TS_CALLBACK_TYPE, NAPI_CALLBACK_CREATE_CUSTOMCURSOR); - long long surfaceAddress = (long long)surface; - cJSON_AddNumberToObject(root, "surfaceAddress", (double)surfaceAddress); cJSON_AddNumberToObject(root, "hot_x", hot_x); cJSON_AddNumberToObject(root, "hot_y", hot_y); + cJSON_AddNumberToObject(root, "BytesPerPixel", surface->format->BytesPerPixel); + cJSON_AddNumberToObject(root, "w", surface->w); + cJSON_AddNumberToObject(root, "h", surface->h); + size_t bufferSize = surface->w * surface->h * surface->format->BytesPerPixel; + void *buff = malloc(bufferSize); + std::memcpy(buff, surface->pixels, bufferSize); + long long surfacepixel = (long long)buff; + cJSON_AddNumberToObject(root, "surfacepixel", (double)surfacepixel); napi_call_threadsafe_function(napiCallback->tsfn, root, napi_tsfn_nonblocking); - return -1; + return 1; } SDL_bool diff --git a/src/core/ohos/SDL_ohosthreadsafe.cpp b/src/core/ohos/SDL_ohosthreadsafe.cpp index 34bf9505e..2cbb7f631 100644 --- a/src/core/ohos/SDL_ohosthreadsafe.cpp +++ b/src/core/ohos/SDL_ohosthreadsafe.cpp @@ -25,6 +25,9 @@ extern "C" { #include "../../SDL_internal.h" #include "SDL_timer.h" #include "SDL_log.h" +#include +#include +#include std::unique_ptr napiCallback = nullptr; static SDL_Thread *sdlMainThread; @@ -258,17 +261,48 @@ OHOS_TS_SetOrientation(const cJSON *root) static void OHOS_TS_CreateCustomCursor(const cJSON *root) { - int hot_x, hot_y; - long long surfaceAddress; - cJSON *data = cJSON_GetObjectItem(root, "surfaceAddress"); - surfaceAddress = (long)data->valuedouble; - SDL_Surface *surface = reinterpret_cast(surfaceAddress); + int hot_x, hot_y, BytesPerPixel, w, h; + long long surfacePixels; + cJSON *data = cJSON_GetObjectItem(root, "surfacepixel"); + surfacePixels = (long long)data->valuedouble; + void *surfacepixelbuffer = reinterpret_cast(surfacePixels); data = cJSON_GetObjectItem(root, "hot_x"); hot_x = data->valueint; data = cJSON_GetObjectItem(root, "hot_y"); hot_y = data->valueint; + + data = cJSON_GetObjectItem(root, "BytesPerPixel"); + BytesPerPixel = data->valueint; + + data = cJSON_GetObjectItem(root, "w"); + w = data->valueint; + + data = cJSON_GetObjectItem(root, "h"); + h = data->valueint; + // create buffer + void *headptr = surfacepixelbuffer; // void* + napi_value argv[3] = {nullptr}; + struct OhosPixelMapCreateOps createOps; + createOps.width = w; + createOps.height = h; + createOps.pixelFormat = 4; + createOps.alphaType = 0; + size_t bufferSize = createOps.width * createOps.height * BytesPerPixel; + int32_t res = OH_PixelMap_CreatePixelMap(napiCallback->env, createOps, (uint8_t *)headptr, bufferSize, &argv[0]); + if (res != IMAGE_RESULT_SUCCESS || argv[0] == nullptr) { + SDL_Log("OH_PixelMap_CreatePixelMap is failed"); + } + napi_create_int32(napiCallback->env, hot_x, &argv[1]); // coordinate x + napi_create_int32(napiCallback->env, hot_y, &argv[2]); // coordinate y + + napi_value callback = nullptr; + napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, &callback); + napi_value jsMethod; + napi_get_named_property(napiCallback->env, callback, "setCustomCursorandCreate", &jsMethod); + napi_call_function(napiCallback->env, nullptr, jsMethod, 3, argv, nullptr); + return; } -- Gitee From 00672a85c14dac0dd6ca39d4aa346c0ea7be7c82 Mon Sep 17 00:00:00 2001 From: duanjf <605126199@qq.com> Date: Thu, 21 Mar 2024 09:37:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E9=9C=B2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/ohos/SDL_ohosthreadsafe.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/core/ohos/SDL_ohosthreadsafe.cpp b/src/core/ohos/SDL_ohosthreadsafe.cpp index 2cbb7f631..85e36b883 100644 --- a/src/core/ohos/SDL_ohosthreadsafe.cpp +++ b/src/core/ohos/SDL_ohosthreadsafe.cpp @@ -281,8 +281,7 @@ OHOS_TS_CreateCustomCursor(const cJSON *root) data = cJSON_GetObjectItem(root, "h"); h = data->valueint; - // create buffer - void *headptr = surfacepixelbuffer; // void* + napi_value argv[3] = {nullptr}; struct OhosPixelMapCreateOps createOps; createOps.width = w; @@ -290,7 +289,7 @@ OHOS_TS_CreateCustomCursor(const cJSON *root) createOps.pixelFormat = 4; createOps.alphaType = 0; size_t bufferSize = createOps.width * createOps.height * BytesPerPixel; - int32_t res = OH_PixelMap_CreatePixelMap(napiCallback->env, createOps, (uint8_t *)headptr, bufferSize, &argv[0]); + int32_t res = OH_PixelMap_CreatePixelMap(napiCallback->env, createOps, (uint8_t *)surfacepixelbuffer, bufferSize, &argv[0]); if (res != IMAGE_RESULT_SUCCESS || argv[0] == nullptr) { SDL_Log("OH_PixelMap_CreatePixelMap is failed"); } @@ -302,7 +301,7 @@ OHOS_TS_CreateCustomCursor(const cJSON *root) napi_value jsMethod; napi_get_named_property(napiCallback->env, callback, "setCustomCursorandCreate", &jsMethod); napi_call_function(napiCallback->env, nullptr, jsMethod, 3, argv, nullptr); - + free(surfacepixelbuffer); return; } -- Gitee From d03a31160c34bfcf24cf63bef3176d1717f7244c Mon Sep 17 00:00:00 2001 From: duanjf <605126199@qq.com> Date: Thu, 21 Mar 2024 14:57:25 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=85=89?= =?UTF-8?q?=E6=A0=87=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ohos-project/entry/src/main/ets/service/SdlModule.ts | 6 +++--- src/core/ohos/SDL_ohos.cpp | 4 ++-- src/core/ohos/SDL_ohosthreadsafe.cpp | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ohos-project/entry/src/main/ets/service/SdlModule.ts b/ohos-project/entry/src/main/ets/service/SdlModule.ts index e9e2537c3..5c41fc3a5 100644 --- a/ohos-project/entry/src/main/ets/service/SdlModule.ts +++ b/ohos-project/entry/src/main/ets/service/SdlModule.ts @@ -48,11 +48,11 @@ export interface NapiCallback { setPointer(cursorID: number): void; - setCustomCursorandCreate(pixelmap_test: image.PixelMap, focusX: number, focusY: number): void; + setCustomCursorandCreate(pixelmapCreated: image.PixelMap, focusX: number, focusY: number): void; } export class ArkNapiCallback implements NapiCallback { - setCustomCursorandCreate(pixelmap_test: image.PixelMap, focusX: number, focusY: number) { + setCustomCursorandCreate(pixelmapCreated: image.PixelMap, focusX: number, focusY: number) { window.getLastWindow(sdlPageContext, (error: BusinessError, windowClass: window.Window) => { if (error.code) { console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(error)) @@ -64,7 +64,7 @@ export class ArkNapiCallback implements NapiCallback { return } try { - pointer.setCustomCursor(windowId, pixelmap_test, focusX, focusY).then(() => { + pointer.setCustomCursor(windowId, pixelmapCreated, focusX, focusY).then(() => { console.log(`Successfully set custom pointer`) }).catch(error => { console.log('Failed to set the custom pointer, error=' + error) diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index 35d261870..608bcfe3e 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -249,8 +249,8 @@ OHOS_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y) cJSON_AddNumberToObject(root, "w", surface->w); cJSON_AddNumberToObject(root, "h", surface->h); size_t bufferSize = surface->w * surface->h * surface->format->BytesPerPixel; - void *buff = malloc(bufferSize); - std::memcpy(buff, surface->pixels, bufferSize); + void *buff = SDL_malloc(bufferSize); + SDL_memcpy(buff, surface->pixels, bufferSize); long long surfacepixel = (long long)buff; cJSON_AddNumberToObject(root, "surfacepixel", (double)surfacepixel); napi_call_threadsafe_function(napiCallback->tsfn, root, napi_tsfn_nonblocking); diff --git a/src/core/ohos/SDL_ohosthreadsafe.cpp b/src/core/ohos/SDL_ohosthreadsafe.cpp index 85e36b883..84775f667 100644 --- a/src/core/ohos/SDL_ohosthreadsafe.cpp +++ b/src/core/ohos/SDL_ohosthreadsafe.cpp @@ -286,7 +286,7 @@ OHOS_TS_CreateCustomCursor(const cJSON *root) struct OhosPixelMapCreateOps createOps; createOps.width = w; createOps.height = h; - createOps.pixelFormat = 4; + createOps.pixelFormat = BytesPerPixel; createOps.alphaType = 0; size_t bufferSize = createOps.width * createOps.height * BytesPerPixel; int32_t res = OH_PixelMap_CreatePixelMap(napiCallback->env, createOps, (uint8_t *)surfacepixelbuffer, bufferSize, &argv[0]); @@ -301,7 +301,7 @@ OHOS_TS_CreateCustomCursor(const cJSON *root) napi_value jsMethod; napi_get_named_property(napiCallback->env, callback, "setCustomCursorandCreate", &jsMethod); napi_call_function(napiCallback->env, nullptr, jsMethod, 3, argv, nullptr); - free(surfacepixelbuffer); + SDL_free(surfacepixelbuffer); return; } @@ -352,7 +352,7 @@ void OHOS_TS_Call(napi_env env, napi_value jsCb, void *context, void *data) NapiCallBackType type = (NapiCallBackType)json->valueint; OHOS_TS_Fuction fuc = tsFuctions[type]; fuc(root); - free(data); + SDL_free(data); root = NULL; json = NULL; } -- Gitee