From 8a8cbd21f96cd2ede1f9a2dfcdb617922df6bdeb Mon Sep 17 00:00:00 2001 From: yaozhupeng Date: Fri, 26 Apr 2024 13:00:54 +0800 Subject: [PATCH] add pixelmap ndk Signed-off-by: yaozhupeng --- bundle.json | 10 ++ rosen/modules/effect/effect_ndk/BUILD.gn | 117 +++++++++++++++ .../effect/effect_ndk/include/effect_filter.h | 141 ++++++++++++++++++ .../effect/effect_ndk/include/effect_types.h | 94 ++++++++++++ .../effect/effect_ndk/src/effect_filter.cpp | 112 ++++++++++++++ .../effect/effect_ndk/src/filter/filter.cpp | 98 ++++++++++++ .../effect/effect_ndk/src/filter/filter.h | 49 ++++++ 7 files changed, 621 insertions(+) create mode 100644 rosen/modules/effect/effect_ndk/BUILD.gn create mode 100644 rosen/modules/effect/effect_ndk/include/effect_filter.h create mode 100644 rosen/modules/effect/effect_ndk/include/effect_types.h create mode 100644 rosen/modules/effect/effect_ndk/src/effect_filter.cpp create mode 100644 rosen/modules/effect/effect_ndk/src/filter/filter.cpp create mode 100644 rosen/modules/effect/effect_ndk/src/filter/filter.h diff --git a/bundle.json b/bundle.json index 84ebce504e..ba7fde93f5 100644 --- a/bundle.json +++ b/bundle.json @@ -120,6 +120,7 @@ "//foundation/graphic/graphic_2d/rosen/modules/effect/effectChain:libeffectchain", "//foundation/graphic/graphic_2d/rosen/modules/effect/color_picker:color_picker", "//foundation/graphic/graphic_2d/rosen/modules/effect/skia_effectChain:skeffectchain", + "//foundation/graphic/graphic_2d/rosen/modules/effect/effect_ndk:native_effect_ndk", "//foundation/graphic/graphic_2d/frameworks/opengl_wrapper:EGL", "//foundation/graphic/graphic_2d/frameworks/opengl_wrapper:GLESv2", "//foundation/graphic/graphic_2d/frameworks/opengl_wrapper:GLESv3", @@ -310,6 +311,15 @@ "header_base": "//foundation/graphic/graphic_2d/rosen/modules/2d_graphics/drawing_ndk/include" } }, + { + "type": "so", + "name": "//foundation/graphic/graphic_2d/rosen/modules/effect/effect_ndk:native_effect_ndk", + "header": { + "header_files": [ + ], + "header_base": "//foundation/graphic/graphic_2d/rosen/modules/effect/effect_ndk/include" + } + }, { "type": "so", "name": "//foundation/graphic/graphic_2d/rosen/modules/2d_graphics:2d_graphics_new", diff --git a/rosen/modules/effect/effect_ndk/BUILD.gn b/rosen/modules/effect/effect_ndk/BUILD.gn new file mode 100644 index 0000000000..02ce2f041c --- /dev/null +++ b/rosen/modules/effect/effect_ndk/BUILD.gn @@ -0,0 +1,117 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved. +# 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") +import("//foundation/graphic/graphic_2d/graphic_config.gni") +effect_root = "$graphic_2d_root/rosen/modules/effect" +effect_ndk_include_dir = + "$graphic_2d_root/rosen/modules/effect/effect_ndk/include" +effect_ndk_src_dir = "$graphic_2d_root/rosen/modules/effect/effect_ndk/src" + +config("export_config") { + include_dirs = [ "$effect_ndk_include_dir" ] +} + +template("effect_ndk_source_set") { + forward_variables_from(invoker, "*") + + ohos_source_set(target_name) { + defines += invoker.defines + cflags_cc += invoker.cflags_cc + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "image_framework:image", + ] + + public_deps = [ + "$effect_root//skia_effectChain:skeffectchain", + "$effect_root/color_picker:color_picker", + ] + public_external_deps = [ "jsoncpp:jsoncpp" ] + + configs = [ ":export_config" ] + sources = [ + "$effect_ndk_src_dir/effect_filter.cpp", + "$effect_ndk_src_dir/filter/filter.cpp", + ] + + if (is_arkui_x) { + defines += [ "CROSS_PLATFORM" ] + } + if (ace_enable_gpu) { + defines += [ "ACE_ENABLE_GPU" ] + } + if (is_emulator) { + defines += [ "ROSEN_EMULATOR" ] + } + + include_dirs = [ + "$graphic_2d_root/rosen/modules/render_service_base/include", + "$graphic_2d_root/modules/2d_graphics/include", + ] + + if (platform == "ohos" || platform == "ohos_ng") { + defines += [ "OHOS_PLATFORM" ] + external_deps += [ + "bounds_checking_function:libsec_static", + "image_framework:pixelmap", + "image_framework:pixelmap_ndk", + ] + deps = [ "$graphic_2d_root/rosen/modules/render_service_base:librender_service_base" ] + } else { + deps = [] + cflags = [ "-std=c++17" ] + + if (!is_arkui_x) { + deps += [ "$rosen_root/modules/platform:hilog" ] + } + } + part_name = "graphic_2d" + subsystem_name = "graphic" + } +} + +effect_ndk_source_set("effect_ndk_source_ohos") { + platform = "ohos" + defines = [] + cflags_cc = [] +} + +ohos_shared_library("native_effect_ndk") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + platform = current_os + if (platform == "mingw") { + platform = "windows" + } + public_configs = [ ":export_config" ] + + if (platform == "ohos") { + deps = [ "$effect_root/effect_ndk:effect_ndk_source_ohos" ] + symlink_target_name = [ "libnative_effect.so" ] + innerapi_tags = [ "ndk" ] + } + part_name = "graphic_2d" + subsystem_name = "graphic" +} diff --git a/rosen/modules/effect/effect_ndk/include/effect_filter.h b/rosen/modules/effect/effect_ndk/include/effect_filter.h new file mode 100644 index 0000000000..03813c897f --- /dev/null +++ b/rosen/modules/effect/effect_ndk/include/effect_filter.h @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2024 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 C_INCLUDE_EFFECT_FILTER_H +#define C_INCLUDE_EFFECT_FILTER_H + +/** + * @addtogroup image + * @{ + * + * @brief Provides APIs for obtaining effect filter and information. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @since 12 + */ + +/** + * @file effect_filter.h + * + * @brief Declares the APIs that can access a effect filter. + * + * @library libnative_effect.so + * @syscap SystemCapability.Multimedia.Image.Core + * @since 12 + */ + +#include "effect_types.h" +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Creates an OH_Filter object. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @param pixelmap The pixelmap pointer to create filter. + * @param filter The OH_Filter pointer will be operated. + * @return Returns {@link EffectErrorCode}. + * @since 12 + * @version 1.0 + */ +EffectErrorCode OH_Filter_CreateEffect(OH_PixelmapNative* pixelmap, OH_Filter** filter); + +/** + * @brief Release an OH_Filter object. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @param filter The OH_Filter pointer will be operated. + * @return Returns {@link EffectErrorCode} + * @since 12 + * @version 1.0 + */ +EffectErrorCode OH_Filter_Release(OH_Filter* filter); + +/** + * @brief Creates a blur effect and then add to the filter. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @param filter The OH_Filter pointer will be operated. + * @param radius The radius of the blur effect. + * @return Returns {@link EffectErrorCode}. + * @since 12 + * @version 1.0 + */ +EffectErrorCode OH_Filter_Blur(OH_Filter* filter, float radius); + +/** + * @brief Creates a brighten effect and then add to the filter. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @param filter The OH_Filter pointer will be operated. + * @param brightness The brightness of the brighten effect. + * @return Returns {@link EffectErrorCode}. + * @since 12 + * @version 1.0 + */ +EffectErrorCode OH_Filter_Brighten(OH_Filter* filter, float brightness); + +/** + * @brief Creates a gray scale effect and then add to the filter. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @param filter The OH_Filter pointer will be operated. + * @return Returns {@link EffectErrorCode}. + * @since 12 + * @version 1.0 + */ +EffectErrorCode OH_Filter_GrayScale(OH_Filter* filter); + +/** + * @brief Creates a invert effect and then add to the filter. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @param filter The OH_Filter pointer will be operated. + * @return Returns {@link EffectErrorCode}. + * @since 12 + * @version 1.0 + */ +EffectErrorCode OH_Filter_Invert(OH_Filter* filter); + +/** + * @brief Creates a effect with a matrix and then add to the filter. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @param filter The OH_Filter pointer will be operated. + * @param matrix The {@link OH_Filter_ColorMatrix} pointer to create a custom effect. + * @return Returns {@link EffectErrorCode}. + * @since 12 + * @version 1.0 + */ +EffectErrorCode OH_Filter_SetColorMatrix(OH_Filter* filter, OH_Filter_ColorMatrix* matrix); + +/** + * @brief Get a pixelmap with the filter effect. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @param filter The OH_Filter pointer will be operated. + * @param pixelmap The pixelmap pointer wiil be operated. + * @return Returns {@link EffectErrorCode}. + * @since 12 + * @version 1.0 + */ +EffectErrorCode OH_Filter_GetEffectPixelMap(OH_Filter* filter, OH_PixelmapNative** pixelmap); + +#ifdef __cplusplus +} +#endif +/** @} */ +#endif diff --git a/rosen/modules/effect/effect_ndk/include/effect_types.h b/rosen/modules/effect/effect_ndk/include/effect_types.h new file mode 100644 index 0000000000..e73052533d --- /dev/null +++ b/rosen/modules/effect/effect_ndk/include/effect_types.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2024 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 C_INCLUDE_EFFECT_TYPES_H +#define C_INCLUDE_EFFECT_TYPES_H + +/** + * @addtogroup image + * @{ + * + * @brief Provides APIs for obtaining effect filter and information. + * + * @syscap SystemCapability.Multimedia.Image.Core + * @since 12 + */ + +/** + * @file effect_types.h + * + * @brief Declares the data types for effect filter. + * + * @library libnative_effect.so + * @syscap SystemCapability.Multimedia.Image.Core + * @since 12 + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Defines a effect filter. + * + * @since 12 + * @version 1.0 + */ +typedef struct OH_Filter OH_Filter; + +/** + * @brief Defines a pixelmap. + * + * @since 12 + * @version 1.0 + */ +typedef struct OH_PixelmapNative OH_PixelmapNative; + +/** + * @brief Defines a matrix for create effect filter. + * + * @since 12 + * @version 1.0 + */ +struct OH_Filter_ColorMatrix { + /** val mast be 5*4 */ + float val[20]; +}; + +/** + * @brief Defines a effect filter error code. + * + * @since 12 + * @version 1.0 + */ +typedef enum { + /** success */ + EFFECT_SUCCESS = 0, + /** invalid parameter */ + EFFECT_BAD_PARAMETER = 401, + /** unsupported operations */ + EFFECT_UNSUPPORTED_OPERATION = 7600201, + /** unknown error */ + EFFECT_UNKNOWN_ERROR = 7600901, +} EffectErrorCode; + +#ifdef __cplusplus +} +#endif +/** @} */ +#endif \ No newline at end of file diff --git a/rosen/modules/effect/effect_ndk/src/effect_filter.cpp b/rosen/modules/effect/effect_ndk/src/effect_filter.cpp new file mode 100644 index 0000000000..add84db131 --- /dev/null +++ b/rosen/modules/effect/effect_ndk/src/effect_filter.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2024 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 "effect_filter.h" + +#include "filter/filter.h" +#include "sk_image_chain.h" +#include "sk_image_filter_factory.h" + +#include "utils/log.h" + +using namespace OHOS; +using namespace Rosen; + +static Filter* CastToFilter(OH_Filter* filter) +{ + return reinterpret_cast(filter); +} + +static OH_Filter* CastToOH_Filter(Filter* filter) +{ + return reinterpret_cast(filter); +} + +EffectErrorCode OH_Filter_CreateEffect(OH_PixelmapNative* pixelmap, OH_Filter** filter) +{ + if (!pixelmap || !filter) { + return EFFECT_BAD_PARAMETER; + } + *filter = CastToOH_Filter(new Filter(pixelmap->GetInnerPixelmap())); + if (*filter == nullptr) { + return EFFECT_BAD_PARAMETER; + } + return EFFECT_SUCCESS; +} + +EffectErrorCode OH_Filter_Release(OH_Filter* filter) +{ + if (filter == nullptr) { + return EFFECT_BAD_PARAMETER; + } + delete CastToFilter(filter); + return EFFECT_SUCCESS; +} + +EffectErrorCode OH_Filter_Blur(OH_Filter* filter, float radius) +{ + if (!filter || !(CastToFilter(filter)->Blur(radius))) { + return EFFECT_BAD_PARAMETER; + } + return EFFECT_SUCCESS; +} +EffectErrorCode OH_Filter_Brighten(OH_Filter* filter, float brightness) +{ + if (!filter || !(CastToFilter(filter)->Brightness(brightness))) { + return EFFECT_BAD_PARAMETER; + } + return EFFECT_SUCCESS; +} + +EffectErrorCode OH_Filter_GrayScale(OH_Filter* filter) +{ + if (!filter || !(CastToFilter(filter)->Grayscale())) { + return EFFECT_BAD_PARAMETER; + } + return EFFECT_SUCCESS; +} +EffectErrorCode OH_Filter_Invert(OH_Filter* filter) +{ + if (!filter || !(CastToFilter(filter)->Invert())) { + return EFFECT_BAD_PARAMETER; + } + return EFFECT_SUCCESS; +} +EffectErrorCode OH_Filter_SetColorMatrix(OH_Filter* filter, OH_Filter_ColorMatrix* matrix) +{ + if (!filter || !matrix) { + return EFFECT_BAD_PARAMETER; + } + PixelColorMatrix colorMatrix; + for (size_t i = 0; i < colorMatrix.MATRIX_SIZE; i++) { + colorMatrix.val[i] = matrix->val[i]; + } + if (!(CastToFilter(filter)->SetColorMatrix(colorMatrix))) { + return EFFECT_BAD_PARAMETER; + } + return EFFECT_SUCCESS; +} + +EffectErrorCode OH_Filter_GetEffectPixelMap(OH_Filter* filter, OH_PixelmapNative** pixelmap) +{ + if (!pixelmap || !filter) { + return EFFECT_BAD_PARAMETER; + } + *pixelmap = new OH_PixelmapNative(CastToFilter(filter)->GetPixelMap()); + if (pixelmap == nullptr) { + return EFFECT_BAD_PARAMETER; + } + return EFFECT_SUCCESS; +} \ No newline at end of file diff --git a/rosen/modules/effect/effect_ndk/src/filter/filter.cpp b/rosen/modules/effect/effect_ndk/src/filter/filter.cpp new file mode 100644 index 0000000000..6b1d2f2c2c --- /dev/null +++ b/rosen/modules/effect/effect_ndk/src/filter/filter.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2024 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 "filter.h" + +#include "sk_image_chain.h" +#include "utils/log.h" + +namespace OHOS { +namespace Rosen { +Filter::Filter(std::shared_ptr pixelMap) : srcPixelMap_(pixelMap) +{} + +void Filter::Render(bool forceCPU) +{ + Rosen::SKImageChain skImage(srcPixelMap_); + for (auto filter : skFilters_) { + skImage.SetFilters(filter); + } + skImage.ForceCPU(forceCPU); + skImage.Draw(); + dstPixelMap_ = skImage.GetPixelMap(); +} + +void Filter::AddNextFilter(sk_sp filter) +{ + skFilters_.emplace_back(filter); +} + +std::shared_ptr Filter::GetPixelMap() +{ + Render(true); + return dstPixelMap_; +} + +bool Filter::Blur(float radius) +{ + auto blur = Rosen::SKImageFilterFactory::Blur(radius); + if (!blur) { + return false; + } + AddNextFilter(blur); + return true; +} + +bool Filter::Brightness(float brightness) +{ + auto bright = Rosen::SKImageFilterFactory::Brightness(brightness); + if (!bright) { + return false; + } + AddNextFilter(bright); + return true; +} + +bool Filter::Grayscale() +{ + auto grayscale = Rosen::SKImageFilterFactory::Grayscale(); + if (!grayscale) { + return false; + } + AddNextFilter(grayscale); + return true; +} + +bool Filter::Invert() +{ + auto invert = Rosen::SKImageFilterFactory::Invert(); + if (!invert) { + return false; + } + AddNextFilter(invert); + return true; +} + +bool Filter::SetColorMatrix(const PixelColorMatrix& matrix) +{ + auto applyColorMatrix = Rosen::SKImageFilterFactory::ApplyColorMatrix(matrix); + if (!applyColorMatrix) { + return false; + } + AddNextFilter(applyColorMatrix); + return true; +} +} // namespace Rosen +} // namespace OHOS \ No newline at end of file diff --git a/rosen/modules/effect/effect_ndk/src/filter/filter.h b/rosen/modules/effect/effect_ndk/src/filter/filter.h new file mode 100644 index 0000000000..8aa6ee2a47 --- /dev/null +++ b/rosen/modules/effect/effect_ndk/src/filter/filter.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024 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 EFFECT_NATIVE_FILTER_H +#define EFFECT_NATIVE_FILTER_H + +#include +#include +#include + +#include "include/core/SkImageFilter.h" +#include "pixelmap_native_impl.h" +#include "sk_image_filter_factory.h" + +namespace OHOS { +namespace Rosen { +class Filter { +public: + Filter(){}; + explicit Filter(std::shared_ptr pixelMap); + + std::shared_ptr GetPixelMap(); + bool Blur(float radius); + bool Brightness(float brightness); + bool Grayscale(); + bool Invert(); + bool SetColorMatrix(const PixelColorMatrix& matrix); + private: + void AddNextFilter(sk_sp filter); + void Render(bool forceCPU); + std::vector > skFilters_; + std::shared_ptr srcPixelMap_ = nullptr; + std::shared_ptr dstPixelMap_ = nullptr; +}; +} +} +#endif // EFFECT_NATIVE_FILTER_H \ No newline at end of file