From 5647216a56f78531bc6485e120a7925f79a64359 Mon Sep 17 00:00:00 2001 From: zhanghang Date: Fri, 15 Aug 2025 18:46:04 +0800 Subject: [PATCH] add drawable static2danamic Signed-off-by: zhanghang --- frameworks/core/BUILD.gn | 2 + .../drawable/animated_drawable_descriptor.cpp | 19 + .../core/drawable/drawable_descriptor.cpp | 41 ++ .../drawable/layered_drawable_descriptor.cpp | 36 ++ .../pixel_map_drawable_descriptor.cpp | 12 + .../ets/@ohos.arkui.drawableDescriptor.ets | 16 + .../src/drawable_descriptor_inner.cpp | 2 +- .../new_js_drawable_descriptor.cpp | 358 ++++++++++++++++-- .../new_js_drawable_descriptor.h | 19 + 9 files changed, 479 insertions(+), 26 deletions(-) create mode 100644 frameworks/core/drawable/drawable_descriptor.cpp diff --git a/frameworks/core/BUILD.gn b/frameworks/core/BUILD.gn index 49dc63eb16f..da5b71472b5 100644 --- a/frameworks/core/BUILD.gn +++ b/frameworks/core/BUILD.gn @@ -305,6 +305,7 @@ template("ace_core_source_set") { # drawable "drawable/animated_drawable_descriptor.cpp", + "drawable/drawable_descriptor.cpp", "drawable/layered_drawable_descriptor.cpp", "drawable/pixel_map_drawable_descriptor.cpp", ] @@ -955,6 +956,7 @@ template("ace_core_ng_source_set") { # drawable "drawable/animated_drawable_descriptor.cpp", + "drawable/drawable_descriptor.cpp", "drawable/layered_drawable_descriptor.cpp", "drawable/pixel_map_drawable_descriptor.cpp", ] diff --git a/frameworks/core/drawable/animated_drawable_descriptor.cpp b/frameworks/core/drawable/animated_drawable_descriptor.cpp index e0daa94e57d..5e038e4989b 100644 --- a/frameworks/core/drawable/animated_drawable_descriptor.cpp +++ b/frameworks/core/drawable/animated_drawable_descriptor.cpp @@ -46,3 +46,22 @@ std::vector AnimatedDrawableDescriptor::GetDurations() return durations_; } } // namespace OHOS::Ace + +extern "C" ACE_FORCE_EXPORT void OHOS_ACE_AnimatedDrawableDescriptor_GetParams( + void* object, void* pixelMapVec, size_t* duration, size_t* iterations) +{ + if (object == nullptr || pixelMapVec == nullptr || duration == nullptr || iterations == nullptr) { + return; + } + auto* pixelMapVecPtr = reinterpret_cast>*>(pixelMapVec); + (*pixelMapVecPtr).clear(); + auto* drawable = reinterpret_cast(object); + auto pixelMapList = drawable->GetPixelMapList(); + for (const auto& refPtrPixelMap : pixelMapList) { + if (refPtrPixelMap) { + (*pixelMapVecPtr).push_back(refPtrPixelMap->GetPixelMapSharedPtr()); + } + } + *duration = static_cast(drawable->GetTotalDuration()); + *iterations = static_cast(drawable->GetIterations()); +} diff --git a/frameworks/core/drawable/drawable_descriptor.cpp b/frameworks/core/drawable/drawable_descriptor.cpp new file mode 100644 index 00000000000..1b7872fd29e --- /dev/null +++ b/frameworks/core/drawable/drawable_descriptor.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/drawable/drawable_descriptor.h" + +extern "C" ACE_FORCE_EXPORT size_t OHOS_ACE_DrawableDescriptor_GetDrawableType(void* object) +{ + size_t type = 0; + if (object == nullptr) { + return type; + } + auto* drawable = reinterpret_cast(object); + if (drawable) { + type = static_cast(drawable->GetDrawableType()); + } + return type; +} + +extern "C" ACE_FORCE_EXPORT void OHOS_ACE_DrawableDescriptor_GetPixelMap(void* object, void* pixelMap) +{ + if (object == nullptr || pixelMap == nullptr) { + return; + } + auto* pixelMapPtr = reinterpret_cast*>(pixelMap); + auto* drawable = reinterpret_cast(object); + if (drawable && drawable->GetPixelMap()) { + *pixelMapPtr = drawable->GetPixelMap()->GetPixelMapSharedPtr(); + } +} diff --git a/frameworks/core/drawable/layered_drawable_descriptor.cpp b/frameworks/core/drawable/layered_drawable_descriptor.cpp index 4f98c995cef..e9a05a8aed3 100644 --- a/frameworks/core/drawable/layered_drawable_descriptor.cpp +++ b/frameworks/core/drawable/layered_drawable_descriptor.cpp @@ -413,3 +413,39 @@ extern "C" ACE_FORCE_EXPORT void OHOS_ACE_LayeredDrawableDescriptor_SetMaskPath( drawable->SetMaskPath(std::string(path)); } } + +extern "C" ACE_FORCE_EXPORT void OHOS_ACE_LayeredDrawableDescriptor_GetForeground(void* object, void* pixelMap) +{ + if (object == nullptr || pixelMap == nullptr) { + return; + } + auto* pixelMapPtr = reinterpret_cast*>(pixelMap); + auto* drawable = reinterpret_cast(object); + if (drawable && drawable->GetForeground()) { + *pixelMapPtr = drawable->GetForeground()->GetPixelMapSharedPtr(); + } +} + +extern "C" ACE_FORCE_EXPORT void OHOS_ACE_LayeredDrawableDescriptor_GetBackground(void* object, void* pixelMap) +{ + if (object == nullptr || pixelMap == nullptr) { + return; + } + auto* pixelMapPtr = reinterpret_cast*>(pixelMap); + auto* drawable = reinterpret_cast(object); + if (drawable && drawable->GetBackground()) { + *pixelMapPtr = drawable->GetBackground()->GetPixelMapSharedPtr(); + } +} + +extern "C" ACE_FORCE_EXPORT void OHOS_ACE_LayeredDrawableDescriptor_GetMask(void* object, void* pixelMap) +{ + if (object == nullptr || pixelMap == nullptr) { + return; + } + auto* pixelMapPtr = reinterpret_cast*>(pixelMap); + auto* drawable = reinterpret_cast(object); + if (drawable && drawable->GetMask()) { + *pixelMapPtr = drawable->GetMask()->GetPixelMapSharedPtr(); + } +} \ No newline at end of file diff --git a/frameworks/core/drawable/pixel_map_drawable_descriptor.cpp b/frameworks/core/drawable/pixel_map_drawable_descriptor.cpp index a2df1ba43b1..4a753c9b424 100644 --- a/frameworks/core/drawable/pixel_map_drawable_descriptor.cpp +++ b/frameworks/core/drawable/pixel_map_drawable_descriptor.cpp @@ -61,3 +61,15 @@ extern "C" ACE_FORCE_EXPORT void OHOS_ACE_PixelMapDrawableDescriptor_SetRawData( drawable->SetRawData(data, len); } } + +extern "C" ACE_FORCE_EXPORT void OHOS_ACE_PixelMapDrawableDescriptor_GetPixelMap(void* object, void* pixelMap) +{ + if (object == nullptr || pixelMap == nullptr) { + return; + } + auto* pixelMapPtr = reinterpret_cast*>(pixelMap); + auto* drawable = reinterpret_cast(object); + if (drawable && drawable->GetPixelMap()) { + *pixelMapPtr = drawable->GetPixelMap()->GetPixelMapSharedPtr(); + } +} \ No newline at end of file diff --git a/interfaces/ets/ani/drawableDescriptor/ets/@ohos.arkui.drawableDescriptor.ets b/interfaces/ets/ani/drawableDescriptor/ets/@ohos.arkui.drawableDescriptor.ets index e27499a68ba..9e395a486e0 100644 --- a/interfaces/ets/ani/drawableDescriptor/ets/@ohos.arkui.drawableDescriptor.ets +++ b/interfaces/ets/ani/drawableDescriptor/ets/@ohos.arkui.drawableDescriptor.ets @@ -33,6 +33,22 @@ export class DrawableDescriptor { let inputVal = ESValue.wrap(input) as ESValue; return DrawableDescriptorInner.nativeTransferStatic(inputVal); } + + static transferDynamic(input: Object): Any { + if (input == null) { + return undefined; + } + let drawableDescriptor = input as DrawableDescriptor; + if (drawableDescriptor) { + let module = ESValue.load("@ohos.arkui.drawableDescriptor"); + let initFunc = module.getProperty("__createTransfer__"); + if (initFunc) { + let drawableRet = initFunc.invoke(ESObject.wrap(drawableDescriptor.nativeObj)); + return drawableRet; + } + } + return undefined; + } } export class PixelMapDrawableDescriptor extends DrawableDescriptor { diff --git a/interfaces/ets/ani/drawableDescriptor/src/drawable_descriptor_inner.cpp b/interfaces/ets/ani/drawableDescriptor/src/drawable_descriptor_inner.cpp index 94e7889126c..8d616cf96bb 100644 --- a/interfaces/ets/ani/drawableDescriptor/src/drawable_descriptor_inner.cpp +++ b/interfaces/ets/ani/drawableDescriptor/src/drawable_descriptor_inner.cpp @@ -69,7 +69,7 @@ ani_ref CreateDouble(ani_env* env, ani_int value) ani_method ctor; env->Class_FindMethod(cls, "", "d:", &ctor); ani_object rs; - env->Object_New(cls, ctor, &rs, value); + env->Object_New(cls, ctor, &rs, static_cast(value)); return rs; } diff --git a/interfaces/inner_api/drawable_descriptor/new_js_drawable_descriptor.cpp b/interfaces/inner_api/drawable_descriptor/new_js_drawable_descriptor.cpp index 039aee2eeeb..0e6b6493d9c 100644 --- a/interfaces/inner_api/drawable_descriptor/new_js_drawable_descriptor.cpp +++ b/interfaces/inner_api/drawable_descriptor/new_js_drawable_descriptor.cpp @@ -15,6 +15,9 @@ #include "new_js_drawable_descriptor.h" +#ifdef OHOS_PLATFORM +#include +#endif #include #ifndef PREVIEW @@ -39,6 +42,24 @@ constexpr char DRAWABLE_DESCRIPTOR_NAME[] = "DrawableDescriptor"; constexpr char LAYERED_DRAWABLE_DESCRIPTOR_NAME[] = "LayeredDrawableDescriptor"; constexpr char ANIMATED_DRAWABLE_DESCRIPTOR_NAME[] = "AnimatedDrawableDescriptor"; constexpr char PIXELMAP_DRAWABLE_DESCRIPTOR_NAME[] = "PixelMapDrawableDescriptor"; +#ifdef OHOS_PLATFORM +constexpr char DRAWBLE_GET_DRAWBLE_TYPE[] = "OHOS_ACE_DrawableDescriptor_GetDrawableType"; +constexpr char DRAWBLE_GET_PIXEL_MAP[] = "OHOS_ACE_DrawableDescriptor_GetPixelMap"; +constexpr char LAYER_GET_FOREGROUND[] = "OHOS_ACE_LayeredDrawableDescriptor_GetForeground"; +constexpr char LAYER_GET_BACKGROUND[] = "OHOS_ACE_LayeredDrawableDescriptor_GetBackground"; +constexpr char LAYER_GET_MASK[] = "OHOS_ACE_LayeredDrawableDescriptor_GetMask"; +constexpr char ANIMATED_GET_PARAMS[] = "OHOS_ACE_AnimatedDrawableDescriptor_GetParams"; +constexpr char PIXEL_MAP_GET_PIXEL_MAP[] = "OHOS_ACE_PixelMapDrawableDescriptor_GetPixelMap"; +constexpr char LIBACE_MODULE[] = "libace_compatible.z.so"; + +using DrawableGetDrawableTypeFunc = size_t (*)(void*); +using DrawableGetPixelMapFunc = void (*)(void*, void*); +using LayeredGetForegroundFunc = void (*)(void*, void*); +using LayeredGetBackgroundFunc = void (*)(void*, void*); +using LayeredGetMaskFunc = void (*)(void*, void*); +using PixelMapGetPixelMapFunc = void (*)(void*, void*); +using AnimatedGetParamsFunc = void (*)(void* object, void*, size_t*, size_t*); +#endif constexpr size_t ESTIMATE_DRAWABLE_SIZE = 10 * 1024 * 1024; constexpr int32_t PARAMS_NUM_TWO = 2; constexpr int32_t PARAMS_NUM_THREE = 3; @@ -186,6 +207,268 @@ napi_value JsDrawableDescriptor::ToNapi( return result; } +size_t JsDrawableDescriptor::DrawableGetDrawableTypeC(void* drawable) +{ + size_t drawableType = 0; +#ifdef OHOS_PLATFORM + void* handle = dlopen(LIBACE_MODULE, RTLD_LAZY | RTLD_LOCAL); + if (handle == nullptr) { + return 0; + } + auto entry = reinterpret_cast(dlsym(handle, DRAWBLE_GET_DRAWBLE_TYPE)); + if (entry == nullptr) { + dlclose(handle); + return 0; + } + drawableType = entry(drawable); + dlclose(handle); +#endif + return drawableType; +} + +std::shared_ptr JsDrawableDescriptor::DrawableGetPixelMapC(void* drawable) +{ + std::shared_ptr pixelMap; +#ifdef OHOS_PLATFORM + void* handle = dlopen(LIBACE_MODULE, RTLD_LAZY | RTLD_LOCAL); + if (handle == nullptr) { + return nullptr; + } + auto entry = reinterpret_cast(dlsym(handle, DRAWBLE_GET_PIXEL_MAP)); + if (entry == nullptr) { + dlclose(handle); + return nullptr; + } + entry(drawable, &pixelMap); + dlclose(handle); +#endif + return pixelMap; +} + +std::shared_ptr JsDrawableDescriptor::LayeredGetForegroundC(void* drawable) +{ + std::shared_ptr pixelMap; +#ifdef OHOS_PLATFORM + void* handle = dlopen(LIBACE_MODULE, RTLD_LAZY | RTLD_LOCAL); + CHECK_NULL_RETURN(handle, nullptr); + auto entry = reinterpret_cast(dlsym(handle, LAYER_GET_FOREGROUND)); + if (entry == nullptr) { + dlclose(handle); + return nullptr; + } + entry(drawable, &pixelMap); + dlclose(handle); +#endif + return pixelMap; +} + +std::shared_ptr JsDrawableDescriptor::LayeredGetBackgroundC(void* drawable) +{ + std::shared_ptr pixelMap; +#ifdef OHOS_PLATFORM + void* handle = dlopen(LIBACE_MODULE, RTLD_LAZY | RTLD_LOCAL); + CHECK_NULL_RETURN(handle, nullptr); + auto entry = reinterpret_cast(dlsym(handle, LAYER_GET_BACKGROUND)); + if (entry == nullptr) { + dlclose(handle); + return nullptr; + } + entry(drawable, &pixelMap); + dlclose(handle); +#endif + return pixelMap; +} + +std::shared_ptr JsDrawableDescriptor::LayeredGetMaskC(void* drawable) +{ + std::shared_ptr pixelMap; +#ifdef OHOS_PLATFORM + void* handle = dlopen(LIBACE_MODULE, RTLD_LAZY | RTLD_LOCAL); + CHECK_NULL_RETURN(handle, nullptr); + auto entry = reinterpret_cast(dlsym(handle, LAYER_GET_MASK)); + if (entry == nullptr) { + dlclose(handle); + return nullptr; + } + entry(drawable, &pixelMap); + dlclose(handle); +#endif + return pixelMap; +} + +void JsDrawableDescriptor::AnimatedGetParamsC(void* drawable, void* pixelMapVec, size_t* duration, size_t* iterations) +{ +#ifdef OHOS_PLATFORM + void* handle = dlopen(LIBACE_MODULE, RTLD_LAZY | RTLD_LOCAL); + CHECK_NULL_VOID(handle); + auto entry = reinterpret_cast(dlsym(handle, ANIMATED_GET_PARAMS)); + if (entry == nullptr) { + dlclose(handle); + return; + } + entry(drawable, pixelMapVec, duration, iterations); + dlclose(handle); +#endif +} + +std::shared_ptr JsDrawableDescriptor::PixelMapGetPixelMapC(void* drawable) +{ + std::shared_ptr pixelMap; +#ifdef OHOS_PLATFORM + void* handle = dlopen(LIBACE_MODULE, RTLD_LAZY | RTLD_LOCAL); + if (handle == nullptr) { + return nullptr; + } + auto entry = reinterpret_cast(dlsym(handle, PIXEL_MAP_GET_PIXEL_MAP)); + if (entry == nullptr) { + dlclose(handle); + return nullptr; + } + entry(drawable, &pixelMap); + dlclose(handle); +#endif + return pixelMap; +} + +napi_value JsDrawableDescriptor::CreateDrawable(napi_env env, void* native) +{ + if (native == nullptr) { + return nullptr; + } + napi_value cons = nullptr; + napi_create_object(env, &cons); + auto* drawable = new Drawable::DrawableDescriptor; + CHECK_NULL_RETURN(drawable, nullptr); + auto napi_status = napi_wrap(env, cons, drawable, Destructor, nullptr, nullptr); + if (napi_status != napi_ok) { + delete drawable; + return nullptr; + } + auto pixelMap = DrawableGetPixelMapC(native); + drawable->SetPixelMap(pixelMap); + auto baseDes = GetBaseDrawableDescriptor(env); + NAPI_CALL(env, napi_define_properties(env, cons, baseDes.size(), baseDes.data())); + return cons; +} + +napi_value JsDrawableDescriptor::CreateLayeredDrawable(napi_env env, void* native) +{ + if (native == nullptr) { + return nullptr; + } + napi_value cons = nullptr; + napi_create_object(env, &cons); + auto* layerDrawable = new LayeredDrawableDescriptor; + CHECK_NULL_RETURN(layerDrawable, nullptr); + auto napi_status = napi_wrap(env, cons, layerDrawable, OldDestructor, nullptr, nullptr); + if (napi_status != napi_ok) { + delete layerDrawable; + return nullptr; + } + + auto pixelForeground = LayeredGetForegroundC(native); + auto pixelBackground = LayeredGetBackgroundC(native); + auto pixelMask = LayeredGetMaskC(native); + layerDrawable->SetForeground(pixelForeground); + layerDrawable->SetBackground(pixelBackground); + layerDrawable->SetMask(pixelMask); + auto layeredDes = GetLayeredDrawableDescriptor(env); + NAPI_CALL(env, napi_define_properties(env, cons, layeredDes.size(), layeredDes.data())); + return cons; +} + +napi_value JsDrawableDescriptor::CreateAnimatedDrawable(napi_env env, void* native) +{ + if (native == nullptr) { + return nullptr; + } + napi_value cons = nullptr; + napi_create_object(env, &cons); + + std::vector> pixelMapVec; + size_t duration = 0; + size_t iterations = 0; + + AnimatedGetParamsC(native, &pixelMapVec, &duration, &iterations); + auto* animatedDrawable = new AnimatedDrawableDescriptor(pixelMapVec, duration, iterations); + CHECK_NULL_RETURN(animatedDrawable, nullptr); + auto napi_status = napi_wrap(env, cons, animatedDrawable, OldDestructor, nullptr, nullptr); + if (napi_status != napi_ok) { + delete animatedDrawable; + return nullptr; + } + + auto animatedDes = GetAnimatedDrawableDescriptor(env); + NAPI_CALL(env, napi_define_properties(env, cons, animatedDes.size(), animatedDes.data())); + return cons; +} + +napi_value JsDrawableDescriptor::CreatePixelMapDrawable(napi_env env, void* native) +{ + if (native == nullptr) { + return nullptr; + } + napi_value cons = nullptr; + napi_create_object(env, &cons); + auto* drawable = new Drawable::PixelmapDrawableDescriptor; + CHECK_NULL_RETURN(drawable, nullptr); + auto napi_status = napi_wrap(env, cons, drawable, Destructor, nullptr, nullptr); + if (napi_status != napi_ok) { + delete drawable; + return nullptr; + } + auto pixelMap = PixelMapGetPixelMapC(native); + drawable->SetPixelMap(pixelMap); + Drawable::SourceInfo sourceInfo; + sourceInfo.SetSrcType(Drawable::SrcType::PIXMAP); + drawable->SetSourceInfo(sourceInfo); + auto pixelDes = GetPixelMapDrawableDescriptor(env); + NAPI_CALL(env, napi_define_properties(env, cons, pixelDes.size(), pixelDes.data())); + return cons; +} + +napi_value JsDrawableDescriptor::CreateDrawableDescriptorTransfer(napi_env env, napi_callback_info info) +{ + size_t argc = 0; + napi_value argv; + napi_get_cb_info(env, info, &argc, &argv, NULL, NULL); + if (argc != 1) { + return nullptr; + } + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv, &valueType); + if (valueType != napi_number) { + return nullptr; + } + int64_t addr = 0; + napi_get_value_int64(env, argv, &addr); + auto* drawable = reinterpret_cast(addr); + auto drawableType = DrawableGetDrawableTypeC(drawable); + auto type = DrawableDescriptor::DrawableType(drawableType); + napi_value cons = nullptr; + switch (type) { + case DrawableDescriptor::DrawableType::BASE: { + cons = CreateDrawable(env, drawable); + break; + } + case DrawableDescriptor::DrawableType::LAYERED: { + cons = CreateLayeredDrawable(env, drawable); + break; + } + case DrawableDescriptor::DrawableType::ANIMATED: { + cons = CreateAnimatedDrawable(env, drawable); + break; + } + case DrawableDescriptor::DrawableType::PIXELMAP: { + cons = CreatePixelMapDrawable(env, drawable); + break; + } + default: + break; + } + return cons; +} + napi_value JsDrawableDescriptor::Export(napi_env env, napi_value exports) { // export base class @@ -199,6 +482,11 @@ napi_value JsDrawableDescriptor::Export(napi_env env, napi_value exports) // export child class cons = InitAnimatedDrawable(env); NAPI_CALL(env, napi_set_named_property(env, exports, ANIMATED_DRAWABLE_DESCRIPTOR_NAME, cons)); + + napi_property_descriptor createTransferDesc[] = { DECLARE_NAPI_FUNCTION( + "__createTransfer__", CreateDrawableDescriptorTransfer) }; + NAPI_CALL(env, napi_define_properties( + env, exports, sizeof(createTransferDesc) / sizeof(createTransferDesc[0]), createTransferDesc)); return exports; } @@ -541,11 +829,10 @@ napi_value JsDrawableDescriptor::PixelMapConstructor(napi_env env, napi_callback return thisVar; } -napi_value JsDrawableDescriptor::InitDrawable(napi_env env) +std::vector JsDrawableDescriptor::GetBaseDrawableDescriptor(napi_env env) { - napi_value cons = nullptr; napi_value typeName = Drawable::NapiUtils::CreateString(env, "DrawableDescriptor"); - napi_property_descriptor baseDes[] = { + return { DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap), DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth), DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight), @@ -553,17 +840,12 @@ napi_value JsDrawableDescriptor::InitDrawable(napi_env env) DECLARE_NAPI_FUNCTION("fetchSync", FetchSync), DECLARE_NAPI_PROPERTY("typeName", typeName) }; - NAPI_CALL(env, napi_define_class(env, DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, DrawableConstructor, nullptr, - sizeof(baseDes) / sizeof(napi_property_descriptor), baseDes, &cons)); - NAPI_CALL(env, napi_create_reference(env, cons, 1, &baseConstructor_)); - return cons; } -napi_value JsDrawableDescriptor::InitPixelMapDrawable(napi_env env) +std::vector JsDrawableDescriptor::GetPixelMapDrawableDescriptor(napi_env env) { - napi_value cons = nullptr; napi_value typeName = Drawable::NapiUtils::CreateString(env, "PixelMapDrawableDescriptor"); - napi_property_descriptor pixelDes[] = { + return { DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap), DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth), DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight), @@ -571,17 +853,12 @@ napi_value JsDrawableDescriptor::InitPixelMapDrawable(napi_env env) DECLARE_NAPI_FUNCTION("fetchSync", FetchSync), DECLARE_NAPI_PROPERTY("typeName", typeName) }; - NAPI_CALL(env, napi_define_class(env, PIXELMAP_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, PixelMapConstructor, - nullptr, sizeof(pixelDes) / sizeof(napi_property_descriptor), pixelDes, &cons)); - NAPI_CALL(env, napi_create_reference(env, cons, 1, &pixelMapConstructor_)); - return cons; } -napi_value JsDrawableDescriptor::InitLayeredDrawable(napi_env env) +std::vector JsDrawableDescriptor::GetLayeredDrawableDescriptor(napi_env env) { - napi_value cons = nullptr; napi_value typeName = Drawable::NapiUtils::CreateString(env, "LayeredDrawableDescriptor"); - napi_property_descriptor layeredDes[] = { + return { DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap), DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth), DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight), @@ -593,17 +870,12 @@ napi_value JsDrawableDescriptor::InitLayeredDrawable(napi_env env) DECLARE_NAPI_STATIC_FUNCTION("getMaskClipPath", GetMaskClipPath), DECLARE_NAPI_PROPERTY("typeName", typeName) }; - NAPI_CALL(env, napi_define_class(env, LAYERED_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, LayeredConstructor, - nullptr, sizeof(layeredDes) / sizeof(napi_property_descriptor), layeredDes, &cons)); - NAPI_CALL(env, napi_create_reference(env, cons, 1, &layeredConstructor_)); - return cons; } -napi_value JsDrawableDescriptor::InitAnimatedDrawable(napi_env env) +std::vector JsDrawableDescriptor::GetAnimatedDrawableDescriptor(napi_env env) { - napi_value cons = nullptr; napi_value typeName = Drawable::NapiUtils::CreateString(env, "AnimatedDrawableDescriptor"); - napi_property_descriptor animatedDes[] = { + return { DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap), DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth), DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight), @@ -614,8 +886,44 @@ napi_value JsDrawableDescriptor::InitAnimatedDrawable(napi_env env) DECLARE_NAPI_FUNCTION("stop", Stop), DECLARE_NAPI_PROPERTY("typeName", typeName) }; +} + +napi_value JsDrawableDescriptor::InitDrawable(napi_env env) +{ + napi_value cons = nullptr; + auto baseDes = GetBaseDrawableDescriptor(env); + NAPI_CALL(env, napi_define_class(env, DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, DrawableConstructor, nullptr, + baseDes.size(), baseDes.data(), &cons)); + NAPI_CALL(env, napi_create_reference(env, cons, 1, &baseConstructor_)); + return cons; +} + +napi_value JsDrawableDescriptor::InitPixelMapDrawable(napi_env env) +{ + napi_value cons = nullptr; + auto pixelDes = GetPixelMapDrawableDescriptor(env); + NAPI_CALL(env, napi_define_class(env, PIXELMAP_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, PixelMapConstructor, + nullptr, pixelDes.size(), pixelDes.data(), &cons)); + NAPI_CALL(env, napi_create_reference(env, cons, 1, &pixelMapConstructor_)); + return cons; +} + +napi_value JsDrawableDescriptor::InitLayeredDrawable(napi_env env) +{ + napi_value cons = nullptr; + auto layeredDes = GetLayeredDrawableDescriptor(env); + NAPI_CALL(env, napi_define_class(env, LAYERED_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, LayeredConstructor, + nullptr, layeredDes.size(), layeredDes.data(), &cons)); + NAPI_CALL(env, napi_create_reference(env, cons, 1, &layeredConstructor_)); + return cons; +} + +napi_value JsDrawableDescriptor::InitAnimatedDrawable(napi_env env) +{ + napi_value cons = nullptr; + auto animatedDes = GetAnimatedDrawableDescriptor(env); NAPI_CALL(env, napi_define_class(env, ANIMATED_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, AnimatedConstructor, - nullptr, sizeof(animatedDes) / sizeof(napi_property_descriptor), animatedDes, &cons)); + nullptr, animatedDes.size(), animatedDes.data(), &cons)); NAPI_CALL(env, napi_create_reference(env, cons, 1, &animatedConstructor_)); return cons; } diff --git a/interfaces/inner_api/drawable_descriptor/new_js_drawable_descriptor.h b/interfaces/inner_api/drawable_descriptor/new_js_drawable_descriptor.h index d36c21eccb8..8b253206a33 100644 --- a/interfaces/inner_api/drawable_descriptor/new_js_drawable_descriptor.h +++ b/interfaces/inner_api/drawable_descriptor/new_js_drawable_descriptor.h @@ -70,6 +70,25 @@ private: static thread_local napi_ref layeredConstructor_; static thread_local napi_ref animatedConstructor_; static thread_local napi_ref pixelMapConstructor_; + + static std::vector GetBaseDrawableDescriptor(napi_env env); + static std::vector GetLayeredDrawableDescriptor(napi_env env); + static std::vector GetAnimatedDrawableDescriptor(napi_env env); + static std::vector GetPixelMapDrawableDescriptor(napi_env env); + + static napi_value CreateDrawableDescriptorTransfer(napi_env env, napi_callback_info info); + static napi_value CreateDrawableDescriptor(napi_env env, DrawableDescriptor* drawableDescriptor); + static size_t DrawableGetDrawableTypeC(void* drawable); + static std::shared_ptr DrawableGetPixelMapC(void* drawable); + static std::shared_ptr LayeredGetForegroundC(void* drawable); + static std::shared_ptr LayeredGetBackgroundC(void* drawable); + static std::shared_ptr LayeredGetMaskC(void* drawable); + static void AnimatedGetParamsC(void* drawable, void* pixelMapVec, size_t* duration, size_t* iterations); + static std::shared_ptr PixelMapGetPixelMapC(void* drawable); + static napi_value CreateDrawable(napi_env env, void* native); + static napi_value CreateLayeredDrawable(napi_env env, void* native); + static napi_value CreateAnimatedDrawable(napi_env env, void* native); + static napi_value CreatePixelMapDrawable(napi_env env, void* native); }; } // namespace Napi } // namespace Ace -- Gitee