diff --git a/BUILD.gn b/BUILD.gn index e7f33ac501bfe0b852b596d63cfa374ad4072d98..23c0de0e980fd8f9ae0578c8dcddd6ac0ed5d149 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -16,6 +16,7 @@ import("//build/ohos.gni") group("image_framework") { deps = [ "frameworks/innerkitsimpl/utils:image_utils", + "frameworks/kits/js/common/pixelmap_ndk:pixelmap_ndk", "interfaces/innerkits:image", "interfaces/innerkits:image_native", ] diff --git a/frameworks/kits/js/common/pixel_map_napi.cpp b/frameworks/kits/js/common/pixel_map_napi.cpp index 2250c7c56bb25b738c2205402e1fec5b2623475e..ed62ac9799c6479ac05204128e1d27c4b2973970 100644 --- a/frameworks/kits/js/common/pixel_map_napi.cpp +++ b/frameworks/kits/js/common/pixel_map_napi.cpp @@ -17,6 +17,7 @@ #include "media_errors.h" #include "hilog/log.h" #include "image_napi_utils.h" +#include "image_pixel_map_napi.h" using OHOS::HiviewDFX::HiLog; namespace { @@ -271,8 +272,8 @@ napi_value PixelMapNapi::Init(napi_env env, napi_value exports) IMG_NAPI_CHECK_RET_D(IMG_IS_OK( napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, - Constructor, nullptr, IMG_ARRAY_SIZE(props), - props, &constructor)), + Constructor, nullptr, IMG_ARRAY_SIZE(props), + props, &constructor)), nullptr, HiLog::Error(LABEL, "define class fail") ); @@ -322,6 +323,24 @@ std::shared_ptr* PixelMapNapi::GetPixelMap() return &nativePixelMap_; } +bool PixelMapNapi::IsLockPixelMap() +{ + return (lockCount > 0); +} + +bool PixelMapNapi::LockPixelMap() +{ + lockCount++; + return true; +} + +void PixelMapNapi::UnlockPixelMap() +{ + if (lockCount > 0) { + lockCount--; + } +} + extern "C" __attribute__((visibility("default"))) void* OHOS_MEDIA_GetPixelMap(napi_env env, napi_value value) { PixelMapNapi *pixmapNapi = nullptr; @@ -333,6 +352,100 @@ extern "C" __attribute__((visibility("default"))) void* OHOS_MEDIA_GetPixelMap(n return reinterpret_cast(pixmapNapi->GetPixelMap()); } +extern "C" __attribute__((visibility("default"))) int32_t OHOS_MEDIA_GetImageInfo(napi_env env, napi_value value, + OhosPixelMapInfo *info) +{ + HiLog::Debug(LABEL, "GetImageInfo IN"); + + if (info == nullptr) { + HiLog::Error(LABEL, "info is nullptr"); + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + PixelMapNapi *pixmapNapi = nullptr; + napi_unwrap(env, value, reinterpret_cast(&pixmapNapi)); + if (pixmapNapi == nullptr) { + HiLog::Error(LABEL, "pixmapNapi unwrapped is nullptr"); + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + std::shared_ptr *pixelMap = pixmapNapi->GetPixelMap(); + if ((pixelMap == nullptr) || ((*pixelMap) == nullptr)) { + HiLog::Error(LABEL, "pixelMap is nullptr"); + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + ImageInfo imageInfo; + (*pixelMap)->GetImageInfo(imageInfo); + info->width = imageInfo.size.width; + info->height = imageInfo.size.height; + info->rowSize = (*pixelMap)->GetRowBytes(); + info->pixelFormat = static_cast(imageInfo.pixelFormat); + + HiLog::Debug(LABEL, "GetImageInfo, w=%{public}u, h=%{public}u, r=%{public}u, f=%{public}d", + info->width, info->height, info->rowSize, info->pixelFormat); + + HiLog::Debug(LABEL, "GetImageInfo OUT"); + return OHOS_IMAGE_RESULT_SUCCESS; +} + +extern "C" __attribute__((visibility("default"))) int32_t OHOS_MEDIA_AccessPixels(napi_env env, napi_value value, + uint8_t** addrPtr) +{ + HiLog::Debug(LABEL, "AccessPixels IN"); + + PixelMapNapi *pixmapNapi = nullptr; + napi_unwrap(env, value, reinterpret_cast(&pixmapNapi)); + if (pixmapNapi == nullptr) { + HiLog::Error(LABEL, "pixmapNapi unwrapped is nullptr"); + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + std::shared_ptr *pixelMap = pixmapNapi->GetPixelMap(); + if ((pixelMap == nullptr) || ((*pixelMap) == nullptr)) { + HiLog::Error(LABEL, "pixelMap is nullptr"); + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + const uint8_t *constPixels = (*pixelMap)->GetPixels(); + if (constPixels == nullptr) { + HiLog::Error(LABEL, "const pixels is nullptr"); + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + uint8_t *pixels = const_cast(constPixels); + if (pixels == nullptr) { + HiLog::Error(LABEL, "pixels is nullptr"); + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + pixmapNapi->LockPixelMap(); + + if (addrPtr != nullptr) { + *addrPtr = pixels; + } + + HiLog::Debug(LABEL, "AccessPixels OUT"); + return OHOS_IMAGE_RESULT_SUCCESS; +} + +extern "C" __attribute__((visibility("default"))) int32_t OHOS_MEDIA_UnAccessPixels(napi_env env, napi_value value) +{ + HiLog::Debug(LABEL, "UnAccessPixels IN"); + + PixelMapNapi *pixmapNapi = nullptr; + napi_unwrap(env, value, reinterpret_cast(&pixmapNapi)); + if (pixmapNapi == nullptr) { + HiLog::Error(LABEL, "pixmapNapi unwrapped is nullptr"); + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + pixmapNapi->UnlockPixelMap(); + + HiLog::Debug(LABEL, "UnAccessPixels OUT"); + return OHOS_IMAGE_RESULT_SUCCESS; +} + napi_value PixelMapNapi::Constructor(napi_env env, napi_callback_info info) { napi_value undefineVar = nullptr; @@ -890,8 +1003,13 @@ napi_value PixelMapNapi::Release(napi_env env, napi_callback_info info) [](napi_env env, void *data) { auto context = static_cast(data); - context->nConstructor->nativePixelMap_ = nullptr; - context->status = SUCCESS; + if (context->nConstructor->IsLockPixelMap()) { + context->status = ERROR; + return; + } else { + context->nConstructor->nativePixelMap_= nullptr; + context->status = SUCCESS; + } }, EmptyResultComplete, asyncContext, asyncContext->work); IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), diff --git a/frameworks/kits/js/common/pixelmap_ndk/BUILD.gn b/frameworks/kits/js/common/pixelmap_ndk/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..391527de8240d60362e8c2e0c6b3911cde166ee7 --- /dev/null +++ b/frameworks/kits/js/common/pixelmap_ndk/BUILD.gn @@ -0,0 +1,33 @@ +# Copyright (C) 2022 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. + +import("//build/ohos.gni") + +SUBSYSTEM_DIR = "//foundation/multimedia/image_standard" + +ohos_shared_library("pixelmap_ndk") { + include_dirs = [ "include" ] + + sources = [ "image_pixel_map_napi.cpp" ] + + public_configs = + [ "$SUBSYSTEM_DIR/interfaces/innerkits:image_external_config" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/innerkits:image", + "//foundation/ace/napi:ace_napi", + ] + + subsystem_name = "multimedia" + part_name = "multimedia_image_standard" +} diff --git a/frameworks/kits/js/common/pixelmap_ndk/image_pixel_map_napi.cpp b/frameworks/kits/js/common/pixelmap_ndk/image_pixel_map_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4022b265c35ea40a656eab8af554e0be1056a46b --- /dev/null +++ b/frameworks/kits/js/common/pixelmap_ndk/image_pixel_map_napi.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2022 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 "image_pixel_map_napi.h" + +extern "C" int32_t OHOS_MEDIA_GetImageInfo(napi_env env, napi_value value, OhosPixelMapInfo *info); +extern "C" int32_t OHOS_MEDIA_AccessPixels(napi_env env, napi_value value, uint8_t** addrPtr); +extern "C" int32_t OHOS_MEDIA_UnAccessPixels(napi_env env, napi_value value); + +extern "C" __attribute__((visibility("default"))) int32_t OH_GetImageInfo(napi_env env, napi_value value, + OhosPixelMapInfo *info) +{ + int32_t ret = OHOS_MEDIA_GetImageInfo(env, value, info); + if (ret != OHOS_IMAGE_RESULT_SUCCESS) { + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + return OHOS_IMAGE_RESULT_SUCCESS; +} + +extern "C" __attribute__((visibility("default"))) int32_t OH_AccessPixels(napi_env env, napi_value value, + void** addrPtr) +{ + int32_t ret = OHOS_MEDIA_AccessPixels(env, value, reinterpret_cast(addrPtr)); + if (ret != OHOS_IMAGE_RESULT_SUCCESS) { + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + return OHOS_IMAGE_RESULT_SUCCESS; +} + +extern "C" __attribute__((visibility("default"))) int32_t OH_UnAccessPixels(napi_env env, napi_value value) +{ + int32_t ret = OHOS_MEDIA_UnAccessPixels(env, value); + if (ret != OHOS_IMAGE_RESULT_SUCCESS) { + return OHOS_IMAGE_RESULT_BAD_PARAMETER; + } + + return OHOS_IMAGE_RESULT_SUCCESS; +} + diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 76a585f0fccf73401c328625e7800e8804d48808..50d8696763787eb212c34b2f5253a07ef5de3de7 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -31,6 +31,7 @@ config("image_external_config") { "//foundation/multimedia/utils/include", "//foundation/multimedia/image_standard/plugins/manager/include", "//foundation/multimedia/image_standard/interfaces/innerkits/include", + "//foundation/multimedia/image_standard/interfaces/kits/native/include", "//utils/jni/jnikit/include", "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", "//foundation/graphic/standard/interfaces/innerkits/surface", diff --git a/interfaces/kits/js/common/include/pixel_map_napi.h b/interfaces/kits/js/common/include/pixel_map_napi.h index 32a1d3ac98cda5cab83e314d38595e4ea6051121..2eaed7118d3a4e24c1fb855d5722b78099c7d216 100644 --- a/interfaces/kits/js/common/include/pixel_map_napi.h +++ b/interfaces/kits/js/common/include/pixel_map_napi.h @@ -34,6 +34,9 @@ public: static napi_value CreatePixelMap(napi_env env, std::shared_ptr pixelmap); static std::shared_ptr GetPixelMap(napi_env env, napi_value pixelmap); std::shared_ptr* GetPixelMap(); + bool IsLockPixelMap(); + bool LockPixelMap(); + void UnlockPixelMap(); private: static napi_value Constructor(napi_env env, napi_callback_info info); @@ -63,6 +66,7 @@ private: napi_env env_ = nullptr; napi_ref wrapper_ = nullptr; std::shared_ptr nativePixelMap_; + int32_t lockCount = 0; bool isRelease = false; }; } // namespace Media diff --git a/interfaces/kits/native/BUILD.gn b/interfaces/kits/native/BUILD.gn index ba315977fee8a48fe90e6c51d41375d91d937db9..4c50d7bcc33b7dc28c1d57c60bd0d0b6b3a7f0d5 100644 --- a/interfaces/kits/native/BUILD.gn +++ b/interfaces/kits/native/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2021 Huawei Device Co., Ltd. +# Copyright (C) 2022 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 @@ -12,14 +12,14 @@ # limitations under the License. import("//build/ohos.gni") -import("//build/ohos/ndk/ndk.gni") ohos_ndk_library("libpixelmap_ndk") { - output_name = "image_pixelmap" - ndk_description_file = "./libpixelmap.ndk.json" + ndk_description_file = "./libimage_pixelmap_napi.json" + min_compact_version = "1" + output_name = "pixelmap_ndk" } -ohos_ndk_headers("pixelmap_header") { - dest_dir = "$ndk_headers_out_dir/multimedia/image" - sources = [ "./include/pixel_map_ndk.h" ] +ohos_ndk_headers("image_header") { + dest_dir = "$ndk_headers_out_dir/multimedia/image_standard" + sources = [ "./include/image_pixel_map_napi.h" ] } diff --git a/interfaces/kits/native/include/image_pixel_map_napi.h b/interfaces/kits/native/include/image_pixel_map_napi.h new file mode 100644 index 0000000000000000000000000000000000000000..4fa78afa663944f5d0e9952f10953f4ffd86e76d --- /dev/null +++ b/interfaces/kits/native/include/image_pixel_map_napi.h @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2022 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. + */ + +/** + * @addtogroup image + * @{ + * + * @brief Provides access to pixel data and pixel map information. + * + * @Syscap SystemCapability.Multimedia.Image + * @since 8 + * @version 1.0 + */ + +/** + * @file image_pixel_map_napi.h + * + * @brief Declares functions for you to lock and access or unlock pixel data, and obtain the width and height of a pixel + * map. + * + * @since 8 + * @version 1.0 + */ + +#ifndef IMAGE_PIXEL_MAP_NAPI_H +#define IMAGE_PIXEL_MAP_NAPI_H +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enumerates the result codes that may be returned by a function. + * + * @since 8 + * @version 1.0 + */ +enum { + /** Success result */ + OHOS_IMAGE_RESULT_SUCCESS = 0, + /** Invalid parameters */ + OHOS_IMAGE_RESULT_BAD_PARAMETER = -1, +}; + +/** + * @brief Enumerates pixel formats. + * + * @since 8 + * @version 1.0 + */ +enum { + /** + * Unknown format + */ + OHOS_PIXEL_MAP_FORMAT_NONE = 0, + /** + * 32-bit RGBA. Components R, G, B, and A each occupies 8 bits + * and are stored from the higher-order to the lower-order bits. + */ + OHOS_PIXEL_MAP_FORMAT_RGBA_8888 = 3, + /** + * 16-bit RGB. Only the R, G, and B components are encoded + * from the higher-order to the lower-order bits: red is stored with 5 bits of precision, + * green is stored with 6 bits of precision, and blue is stored with 5 bits of precision. + */ + OHOS_PIXEL_MAP_FORMAT_RGB_565 = 2, +}; + +/** + * @brief Defines pixel map information. + * + * @since 8 + * @version 1.0 + */ +struct OhosPixelMapInfo { + /** Image width, in pixels. */ + uint32_t width; + /** Image height, in pixels. */ + uint32_t height; + /** Number of bytes in each row of a pixel map */ + uint32_t rowSize; + /** Pixel format */ + int32_t pixelFormat; +}; + +/** + * @brief Obtains information about a given PixelMap and stores the information in a {@link OhosPixelMapInfo} + * structure. + * + * @param env Indicates the pointer to the JNI environment. + * @param pixelMapObject Indicates the Java PixelMap object. + * @param info Indicates the pointer to the pixel map information to obtain. For details, see {@link + * OhosPixelMapInfo}. + * @return Returns 0 if the information is obtained and stored in the structure; returns result codes if the + * operation fails. + * @see OhosPixelMapInfo + * @since 8 + * @version 1.0 + */ +int32_t OH_GetImageInfo(napi_env env, napi_value value, OhosPixelMapInfo *info); + +/** + * @brief Obtains the memory address of a given PixelMap object and locks the memory. + * + * If this function call is successful, *addrPtr is set to the memory address. After accessing the pixel data, + * you must use {@link UnAccessPixels} to unlock the memory. Otherwise, resources cannot be released. + * After the memory is unlocked, it can be invalid and should not be accessed. + * + * @param env Indicates the pointer to the JNI environment. + * @param pixelMapObject Indicates the Java PixelMap object. + * @param addrPtr Indicates the double pointer to the memory address. + * @see UnAccessPixels + * @return Returns {@link OHOS_IMAGE_RESULT_SUCCESS} if the operation is successful; returns other result codes if + * the operation fails. + * @since 8 + * @version 1.0 + */ +int32_t OH_AccessPixels(napi_env env, napi_value value, void** addrPtr); + +/** + * @brief Unlocks the memory storing the pixel data of a given PixelMap to balance a successful call to {@link + * AccessPixels}. + * + * @param env Indicates the pointer to the JNI environment. + * @param pixelMapObject Indicates the Java PixelMap object. + * @return Returns {@link OHOS_IMAGE_RESULT_SUCCESS} if the operation is successful; returns other result codes if + * the operation fails. + * @see AccessPixels + * @since 8 + * @version 1.0 + */ +int32_t OH_UnAccessPixels(napi_env env, napi_value value); + +#ifdef __cplusplus +}; +#endif + +/** @} */ +#endif // IMAGE_PIXEL_MAP_NAPI_H + diff --git a/interfaces/kits/native/libimage_pixelmap_napi.json b/interfaces/kits/native/libimage_pixelmap_napi.json new file mode 100644 index 0000000000000000000000000000000000000000..07722e8239134c2a00fb6ba329010a2880da356d --- /dev/null +++ b/interfaces/kits/native/libimage_pixelmap_napi.json @@ -0,0 +1,12 @@ +[ + { + "first_introduced": "1", + "name": "OH_GetImageInfo" + }, + { + "name": "OH_AccessPixels" + }, + { + "name": "OH_UnAccessPixels" + } +] \ No newline at end of file diff --git a/interfaces/kits/native/libpixelmap.ndk.json b/interfaces/kits/native/libpixelmap.ndk.json deleted file mode 100644 index a654a260d4eefd83c053345baaa236996620b05c..0000000000000000000000000000000000000000 --- a/interfaces/kits/native/libpixelmap.ndk.json +++ /dev/null @@ -1,11 +0,0 @@ -[ - { - "name": "OH_GetImageInfo" - }, - { - "name": "OH_AccessPixels" - }, - { - "name": "OH_UnAccessPixels" - } -] \ No newline at end of file diff --git a/interfaces/kits/native/ndk_test_example/@ohos.xtstest.mypixelmap.d.ts b/interfaces/kits/native/ndk_test_example/@ohos.xtstest.mypixelmap.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f465aeec7b8685bd319c89c5f160a6fdebf078f --- /dev/null +++ b/interfaces/kits/native/ndk_test_example/@ohos.xtstest.mypixelmap.d.ts @@ -0,0 +1,23 @@ +/* +* Copyright (C) 2022 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. +*/ +import image from '@ohos.multimedia.image' + +declare namespace mypixelmap { + function testGetImageInfo(PixelMap pixelMap): void; + function testAccessPixels(PixelMap pixelMap): void; + function testUnAccessPixels(PixelMap pixelMap): void; +} + +export default mypixelmap; \ No newline at end of file diff --git a/interfaces/kits/native/ndk_test_example/BUILD.gn b/interfaces/kits/native/ndk_test_example/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..66e87144dd039a9c819ae73bf6b1d04649585914 --- /dev/null +++ b/interfaces/kits/native/ndk_test_example/BUILD.gn @@ -0,0 +1,53 @@ +# Copyright (C) 2022 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. + +import("//build/ohos.gni") + +############################################################################ +# Debug Used : mypixelmap +############################################################################ + +js_declaration("mypixelmap_js") { + part_name = "multimedia_image" + sources = [ "//foundation/multimedia/image_standard/interfaces/kits/native/ndk_test_example/@ohos.xtstest.mypixelmap.d.ts" ] +} + +ohos_copy("mypixelmap_declaration") { + sources = [ "//foundation/multimedia/image_standard/interfaces/kits/native/ndk_test_example/@ohos.xtstest.mypixelmap.d.ts" ] + outputs = [ target_out_dir + "/$target_name/" ] + module_source_dir = target_out_dir + "/$target_name" + module_install_name = "" +} + +ohos_shared_library("mypixelmap") { + install_enable = true + + sources = [ "my_pixel_map.cpp" ] + + include_dirs = [ + "include", + "//foundation/multimedia/image_standard/interfaces/kits/native/include", + ] + + deps = [ + "//foundation/ace/napi:ace_napi", + "//foundation/multimedia/image_standard/frameworks/kits/js/common/pixelmap_ndk:pixelmap_ndk", + ] + + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + + relative_install_dir = "module" + + subsystem_name = "multimedia" + part_name = "multimedia_image" +} diff --git a/interfaces/kits/native/ndk_test_example/my_pixel_map.cpp b/interfaces/kits/native/ndk_test_example/my_pixel_map.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0eb4f93a175d3bbb3e6a6a71bbbc42812c426bba --- /dev/null +++ b/interfaces/kits/native/ndk_test_example/my_pixel_map.cpp @@ -0,0 +1,207 @@ +/* + * Copyright (C) 2022 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 "my_pixel_map.h" +#include "media_errors.h" +#include "hilog/log.h" +#include "image_napi_utils.h" +#include "image_pixel_map_napi.h" + +using OHOS::HiviewDFX::HiLog; +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "MyPixelMapNapiTest"}; +constexpr uint32_t TEST_ARG_SUM = 1; +} +namespace OHOS { +namespace Media { +static const std::string CLASS_NAME = "MyPixelMap"; +napi_ref MyPixelMap::sConstructor_ = nullptr; +MyPixelMap::MyPixelMap() + :env_(nullptr), wrapper_(nullptr) +{ +} + +MyPixelMap::~MyPixelMap() +{ + if (wrapper_ != nullptr) { + napi_delete_reference(env_, wrapper_); + } +} + +napi_value MyPixelMap::Init(napi_env env, napi_value exports) +{ + napi_property_descriptor props[] = { + }; + + napi_property_descriptor static_prop[] = { + DECLARE_NAPI_STATIC_FUNCTION("testGetImageInfo", TestGetImageInfo), + DECLARE_NAPI_STATIC_FUNCTION("testAccessPixels", TestAccessPixels), + DECLARE_NAPI_STATIC_FUNCTION("testUnAccessPixels", TestUnAccessPixels), + }; + + napi_value constructor = nullptr; + + if (napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr, IMG_ARRAY_SIZE(props), + props, &constructor) != napi_ok) { + HiLog::Error(LABEL, "define class fail"); + return nullptr; + } + + if (napi_create_reference(env, constructor, 1, &sConstructor_) != napi_ok) { + HiLog::Error(LABEL, "create reference fail"); + return nullptr; + } + + if (napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor) != napi_ok) { + HiLog::Error(LABEL, "set named property fail"); + return nullptr; + } + + if (napi_define_properties(env, exports, IMG_ARRAY_SIZE(static_prop), static_prop) != napi_ok) { + HiLog::Error(LABEL, "define properties fail"); + return nullptr; + } + + HiLog::Debug(LABEL, "Init success"); + return exports; +} + +napi_value MyPixelMap::Constructor(napi_env env, napi_callback_info info) +{ + HiLog::Debug(LABEL, "Constructor IN"); + napi_value undefineVar = nullptr; + napi_get_undefined(env, &undefineVar); + + napi_status status; + napi_value thisVar = nullptr; + napi_get_undefined(env, &thisVar); + + status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + + HiLog::Debug(LABEL, "Constructor OUT"); + return thisVar; +} + +napi_value MyPixelMap::TestGetImageInfo(napi_env env, napi_callback_info info) +{ + HiLog::Debug(LABEL, "TestGetImageInfo IN"); + + napi_value result = nullptr; + napi_get_undefined(env, &result); + + napi_status status; + napi_value thisVar = nullptr; + napi_value argValue[TEST_ARG_SUM] = {0}; + size_t argCount = TEST_ARG_SUM; + + status = napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr); + if (status != napi_ok) { + HiLog::Error(LABEL, "napi_get_cb_info fail"); + } + + HiLog::Debug(LABEL, "OH_GetImageInfo Test|Begin"); + OhosPixelMapInfo pixelMapInfo; + int32_t res = OH_GetImageInfo(env, argValue[0], &pixelMapInfo); + HiLog::Debug(LABEL, "OH_GetImageInfo Test|End, res=%{public}d", res); + HiLog::Debug(LABEL, "OH_GetImageInfo, w=%{public}u, h=%{public}u, r=%{public}u, f=%{public}d", + pixelMapInfo.width, pixelMapInfo.height, pixelMapInfo.rowSize, pixelMapInfo.pixelFormat); + + HiLog::Debug(LABEL, "TestGetImageInfo OUT"); + return result; +} + +napi_value MyPixelMap::TestAccessPixels(napi_env env, napi_callback_info info) +{ + HiLog::Debug(LABEL, "TestAccessPixels IN"); + + napi_value result = nullptr; + napi_get_undefined(env, &result); + + napi_status status; + napi_value thisVar = nullptr; + napi_value argValue[TEST_ARG_SUM] = {0}; + size_t argCount = TEST_ARG_SUM; + + status = napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr); + if (status != napi_ok) { + HiLog::Error(LABEL, "napi_get_cb_info fail"); + } + + HiLog::Debug(LABEL, "OH_AccessPixels Test|Begin"); + void* addrPtr = nullptr; + int32_t res = OH_AccessPixels(env, argValue[0], &addrPtr); + HiLog::Debug(LABEL, "OH_AccessPixels Test|End, res=%{public}d, addrPtr=%{public}p", res, addrPtr); + + HiLog::Debug(LABEL, "TestAccessPixels OUT"); + return result; +} + +napi_value MyPixelMap::TestUnAccessPixels(napi_env env, napi_callback_info info) +{ + HiLog::Debug(LABEL, "TestUnAccessPixels IN"); + + napi_value result = nullptr; + napi_get_undefined(env, &result); + + napi_status status; + napi_value thisVar = nullptr; + napi_value argValue[TEST_ARG_SUM] = {0}; + size_t argCount = TEST_ARG_SUM; + + status = napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr); + if (status != napi_ok) { + HiLog::Error(LABEL, "napi_get_cb_info fail"); + } + + HiLog::Debug(LABEL, "OH_UnAccessPixels Test|Begin"); + int32_t res = OH_UnAccessPixels(env, argValue[0]); + HiLog::Debug(LABEL, "OH_UnAccessPixels Test|End, res=%{public}d", res); + + HiLog::Debug(LABEL, "TestUnAccessPixels OUT"); + return result; +} + +/* + * Function registering all props and functions of ohos.medialibrary module + */ +static napi_value Export(napi_env env, napi_value exports) +{ + HiLog::Error(LABEL, "MyPixelMap CALL"); + MyPixelMap::Init(env, exports); + return exports; +} + +/* + * module define + */ +static napi_module g_module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Export, + .nm_modname = "xtstest.mypixelmap", + .nm_priv = ((void*)0), + .reserved = {0} +}; + +/* + * module register + */ +extern "C" __attribute__((constructor)) void MyPixelMapRegisterModule(void) +{ + napi_module_register(&g_module); +} +} // namespace Media +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/native/include/pixel_map_ndk.h b/interfaces/kits/native/ndk_test_example/my_pixel_map.h similarity index 39% rename from interfaces/kits/native/include/pixel_map_ndk.h rename to interfaces/kits/native/ndk_test_example/my_pixel_map.h index 2b9ddbd9776ed15f8dc08d45bdeea44d742d485d..0762048c8dfcdf967521f1fb5d349afdc7e841cf 100644 --- a/interfaces/kits/native/include/pixel_map_ndk.h +++ b/interfaces/kits/native/ndk_test_example/my_pixel_map.h @@ -1,59 +1,48 @@ -/* - * Copyright (C) 2021 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. - */ - -/** - * @file pixel_map_ndk.h - * - * @brief Declares functions for you to lock and access or unlock pixel data, - * and obtain the width and height of a pixel map. - * - * @since 8 - * @version 1.0 - */ - -#ifndef PIXEL_MAP_NDK_H -#define PIXEL_MAP_NDK_H - -#include "napi/native_api.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct OhosPixelMapInfo { - /** Image width, in pixels. */ - uint32_t width; - - /** Image height, in pixels. */ - uint32_t height; - - /** Number of bytes in each row of a pixel map */ - uint32_t rowSize; - - /** Pixel format */ - int32_t pixelFormat; -}; - -int32_t OH_GetImageInfo(napi_env env, napi_value value, OhosPixelMapInfo *info); - -int32_t OH_AccessPixels(napi_env env, napi_value value, void** addrPtr); - -int32_t OH_UnAccessPixels(napi_env env, napi_value value); - -#ifdef __cplusplus -} -#endif - -#endif // PIXEL_MAP_NDK_H +/* + * Copyright (C) 2022 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. + */ + +#ifndef PIXEL_MAP_NAPI_TEST_H_ +#define PIXEL_MAP_NAPI_TEST_H_ + +#include "pixel_map.h" +#include "image_type.h" +#include "image_source.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace Media { +class MyPixelMap { +public: + MyPixelMap(); + ~MyPixelMap(); + + static napi_value Init(napi_env env, napi_value exports); + + static napi_value TestGetImageInfo(napi_env env, napi_callback_info info); + static napi_value TestAccessPixels(napi_env env, napi_callback_info info); + static napi_value TestUnAccessPixels(napi_env env, napi_callback_info info); + +private: + static napi_value Constructor(napi_env env, napi_callback_info info); + + static napi_ref sConstructor_; + + napi_env env_ = nullptr; + napi_ref wrapper_ = nullptr; +}; +} // namespace Media +} // namespace OHOS +#endif /* PIXEL_MAP_NAPI_TEST_H_ */