14d949f91Sopenharmony_ci/*
24d949f91Sopenharmony_ci* Copyright (C) 2024 Huawei Device Co., Ltd.
34d949f91Sopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
44d949f91Sopenharmony_ci* you may not use this file except in compliance with the License.
54d949f91Sopenharmony_ci* You may obtain a copy of the License at
64d949f91Sopenharmony_ci*
74d949f91Sopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
84d949f91Sopenharmony_ci*
94d949f91Sopenharmony_ci* Unless required by applicable law or agreed to in writing, software
104d949f91Sopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
114d949f91Sopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124d949f91Sopenharmony_ci* See the License for the specific language governing permissions and
134d949f91Sopenharmony_ci* limitations under the License.
144d949f91Sopenharmony_ci*/
154d949f91Sopenharmony_ci
164d949f91Sopenharmony_ci#include "image_effect.h"
174d949f91Sopenharmony_ci
184d949f91Sopenharmony_ci#include "effect_log.h"
194d949f91Sopenharmony_ci#include "efilter_factory.h"
204d949f91Sopenharmony_ci#include "external_loader.h"
214d949f91Sopenharmony_ci#include "image_effect_inner.h"
224d949f91Sopenharmony_ci#include "effect_json_helper.h"
234d949f91Sopenharmony_ci#include "native_effect_base.h"
244d949f91Sopenharmony_ci#include "native_common_utils.h"
254d949f91Sopenharmony_ci#include "native_window.h"
264d949f91Sopenharmony_ci#include "event_report.h"
274d949f91Sopenharmony_ci
284d949f91Sopenharmony_ci#define MAX_EFILTER_NUMS 100
294d949f91Sopenharmony_ci
304d949f91Sopenharmony_ciusing namespace OHOS::Media;
314d949f91Sopenharmony_ciusing namespace OHOS::Media::Effect;
324d949f91Sopenharmony_ci
334d949f91Sopenharmony_cinamespace {
344d949f91Sopenharmony_ci    std::mutex effectMutex_;
354d949f91Sopenharmony_ci
364d949f91Sopenharmony_ci    constexpr int const MAX_CHAR_LEN = 1024;
374d949f91Sopenharmony_ci    constexpr int const MAX_INFO_LEN = 5 * 1024 * 1024;
384d949f91Sopenharmony_ci    constexpr const char *EMPTY_NAME = "";
394d949f91Sopenharmony_ci}
404d949f91Sopenharmony_ci
414d949f91Sopenharmony_ci#ifdef __cplusplus
424d949f91Sopenharmony_ciextern "C" {
434d949f91Sopenharmony_ci#endif
444d949f91Sopenharmony_ci
454d949f91Sopenharmony_ciEFFECT_EXPORT
464d949f91Sopenharmony_ciOH_ImageEffect *OH_ImageEffect_Create(const char *name)
474d949f91Sopenharmony_ci{
484d949f91Sopenharmony_ci    if (name != nullptr && strlen(name) > MAX_CHAR_LEN) {
494d949f91Sopenharmony_ci        name = EMPTY_NAME;
504d949f91Sopenharmony_ci    }
514d949f91Sopenharmony_ci    if (!ExternLoader::Instance()->IsExtLoad()) {
524d949f91Sopenharmony_ci        ExternLoader::Instance()->LoadExtSo();
534d949f91Sopenharmony_ci    }
544d949f91Sopenharmony_ci    auto func = ExternLoader::Instance()->GetCreateImageEffectExtFunc();
554d949f91Sopenharmony_ci    if (func) {
564d949f91Sopenharmony_ci        void* image = func(name);
574d949f91Sopenharmony_ci        if (image != nullptr) {
584d949f91Sopenharmony_ci            return static_cast<OH_ImageEffect *>(image);
594d949f91Sopenharmony_ci        }
604d949f91Sopenharmony_ci    } else {
614d949f91Sopenharmony_ci        EFFECT_LOGE("OH_ImageEffect_Create: shared lib so not find function!");
624d949f91Sopenharmony_ci    }
634d949f91Sopenharmony_ci
644d949f91Sopenharmony_ci    EFFECT_LOGI("Creat image effect");
654d949f91Sopenharmony_ci    std::shared_ptr<ImageEffect> imageEffect = std::make_unique<ImageEffect>(name);
664d949f91Sopenharmony_ci    std::unique_ptr<OH_ImageEffect> nativeImageEffect = std::make_unique<OH_ImageEffect>();
674d949f91Sopenharmony_ci    nativeImageEffect->imageEffect_ = imageEffect;
684d949f91Sopenharmony_ci    return nativeImageEffect.release();
694d949f91Sopenharmony_ci}
704d949f91Sopenharmony_ci
714d949f91Sopenharmony_ciEFFECT_EXPORT
724d949f91Sopenharmony_ciOH_EffectFilter *OH_ImageEffect_AddFilter(OH_ImageEffect *imageEffect, const char *filterName)
734d949f91Sopenharmony_ci{
744d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, nullptr, "AddFilter: input parameter imageEffect is null!");
754d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect->filters_.size() < MAX_EFILTER_NUMS, nullptr,
764d949f91Sopenharmony_ci        "AddFilter: filter nums is out of range!");
774d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filterName != nullptr, nullptr, "AddFilter: input parameter filterName is null!");
784d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(strlen(filterName) < MAX_CHAR_LEN, nullptr,
794d949f91Sopenharmony_ci        "AddFilter: the length of input parameter filterName is too long! len = %{public}zu", strlen(filterName));
804d949f91Sopenharmony_ci
814d949f91Sopenharmony_ci    OH_EffectFilter *filter = OH_EffectFilter_Create(filterName);
824d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filter != nullptr, nullptr, "AddFilter: create filter fail! name=%{public}s", filterName);
834d949f91Sopenharmony_ci
844d949f91Sopenharmony_ci    filter->isCreatedBySystem_ = true;
854d949f91Sopenharmony_ci    ImageEffect_ErrorCode errorCode = OH_ImageEffect_AddFilterByFilter(imageEffect, filter);
864d949f91Sopenharmony_ci    if (errorCode != ImageEffect_ErrorCode::EFFECT_SUCCESS) {
874d949f91Sopenharmony_ci        OH_EffectFilter_Release(filter);
884d949f91Sopenharmony_ci        filter = nullptr;
894d949f91Sopenharmony_ci    }
904d949f91Sopenharmony_ci    return filter;
914d949f91Sopenharmony_ci}
924d949f91Sopenharmony_ci
934d949f91Sopenharmony_ciEFFECT_EXPORT
944d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_AddFilterByFilter(OH_ImageEffect *imageEffect, OH_EffectFilter *filter)
954d949f91Sopenharmony_ci{
964d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
974d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
984d949f91Sopenharmony_ci        "AddFilter: input parameter imageEffect is null!");
994d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filter != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
1004d949f91Sopenharmony_ci        "AddFilter: input parameter filter is null!");
1014d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect->filters_.size() < MAX_EFILTER_NUMS,
1024d949f91Sopenharmony_ci        ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID, "AddFilterByFilter: filter nums is out of range!");
1034d949f91Sopenharmony_ci
1044d949f91Sopenharmony_ci    EFFECT_LOGI("Add filter. name=%{public}s", filter->filter_->GetName().c_str());
1054d949f91Sopenharmony_ci
1064d949f91Sopenharmony_ci    imageEffect->imageEffect_->AddEFilter(filter->filter_);
1074d949f91Sopenharmony_ci    imageEffect->filters_.emplace_back(filter, filter->filter_->GetName());
1084d949f91Sopenharmony_ci
1094d949f91Sopenharmony_ci    EventInfo eventInfo = {
1104d949f91Sopenharmony_ci        .filterName = filter->filter_->GetName(),
1114d949f91Sopenharmony_ci    };
1124d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(ADD_FILTER_STATISTIC, eventInfo);
1134d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
1144d949f91Sopenharmony_ci}
1154d949f91Sopenharmony_ci
1164d949f91Sopenharmony_ciEFFECT_EXPORT
1174d949f91Sopenharmony_ciOH_EffectFilter *OH_ImageEffect_InsertFilter(OH_ImageEffect *imageEffect, uint32_t index, const char *filterName)
1184d949f91Sopenharmony_ci{
1194d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, nullptr, "InsertFilter: input parameter imageEffect is null!");
1204d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect->filters_.size() < MAX_EFILTER_NUMS, nullptr,
1214d949f91Sopenharmony_ci        "InsertFilter: filter nums is out of range!");
1224d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filterName != nullptr, nullptr, "InsertFilter: input parameter filterName is null!");
1234d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(strlen(filterName) < MAX_CHAR_LEN, nullptr,
1244d949f91Sopenharmony_ci        "InsertFilter: the length of input parameter filterName is too long! len = %{public}zu", strlen(filterName));
1254d949f91Sopenharmony_ci
1264d949f91Sopenharmony_ci    OH_EffectFilter *filter = OH_EffectFilter_Create(filterName);
1274d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filter != nullptr, nullptr, "InsertFilter: create filter fail! filterName=%{public}s",
1284d949f91Sopenharmony_ci        filterName);
1294d949f91Sopenharmony_ci
1304d949f91Sopenharmony_ci    filter->isCreatedBySystem_ = true;
1314d949f91Sopenharmony_ci    ImageEffect_ErrorCode errorCode = OH_ImageEffect_InsertFilterByFilter(imageEffect, index, filter);
1324d949f91Sopenharmony_ci    if (errorCode != ImageEffect_ErrorCode::EFFECT_SUCCESS) {
1334d949f91Sopenharmony_ci        OH_EffectFilter_Release(filter);
1344d949f91Sopenharmony_ci        filter = nullptr;
1354d949f91Sopenharmony_ci    }
1364d949f91Sopenharmony_ci    return filter;
1374d949f91Sopenharmony_ci}
1384d949f91Sopenharmony_ci
1394d949f91Sopenharmony_ciEFFECT_EXPORT
1404d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_InsertFilterByFilter(OH_ImageEffect *imageEffect, uint32_t index,
1414d949f91Sopenharmony_ci    OH_EffectFilter *filter)
1424d949f91Sopenharmony_ci{
1434d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
1444d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
1454d949f91Sopenharmony_ci        "InsertFilter: input parameter imageEffect is null!");
1464d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect->filters_.size() < MAX_EFILTER_NUMS,
1474d949f91Sopenharmony_ci        ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID, "InsertFilter: filter nums is out of range!");
1484d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filter != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
1494d949f91Sopenharmony_ci        "InsertFilter: input parameter filterName is null!");
1504d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(index <= static_cast<uint32_t>(imageEffect->filters_.size()),
1514d949f91Sopenharmony_ci        ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
1524d949f91Sopenharmony_ci        "InsertFilter: input parameter index is invalid! index=%{public}d, efilterSize=%{public}zu",
1534d949f91Sopenharmony_ci        index, imageEffect->filters_.size());
1544d949f91Sopenharmony_ci
1554d949f91Sopenharmony_ci    std::string filterName = filter->filter_->GetName();
1564d949f91Sopenharmony_ci    EFFECT_LOGI("Insert filter. name=%{public}s", filterName.c_str());
1574d949f91Sopenharmony_ci
1584d949f91Sopenharmony_ci    ErrorCode result = imageEffect->imageEffect_->InsertEFilter(filter->filter_, index);
1594d949f91Sopenharmony_ci    if (result != ErrorCode::SUCCESS) {
1604d949f91Sopenharmony_ci        EFFECT_LOGE("InsertFilter: insert filter fail! result=%{public}d, name=%{public}s", result, filterName.c_str());
1614d949f91Sopenharmony_ci        return ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID;
1624d949f91Sopenharmony_ci    }
1634d949f91Sopenharmony_ci
1644d949f91Sopenharmony_ci    imageEffect->filters_.emplace(imageEffect->filters_.begin() + index, filter, filterName);
1654d949f91Sopenharmony_ci    EventInfo eventInfo = {
1664d949f91Sopenharmony_ci        .filterName = filterName,
1674d949f91Sopenharmony_ci    };
1684d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(ADD_FILTER_STATISTIC, eventInfo);
1694d949f91Sopenharmony_ci
1704d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
1714d949f91Sopenharmony_ci}
1724d949f91Sopenharmony_ci
1734d949f91Sopenharmony_ciEFFECT_EXPORT
1744d949f91Sopenharmony_ciint32_t OH_ImageEffect_RemoveFilter(OH_ImageEffect *imageEffect, const char *filterName)
1754d949f91Sopenharmony_ci{
1764d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
1774d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, 0, "RemoveFilter: input parameter imageEffect is null!");
1784d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filterName != nullptr, 0, "RemoveFilter: input parameter nativeEffect is null!");
1794d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(strlen(filterName) < MAX_CHAR_LEN, 0,
1804d949f91Sopenharmony_ci        "RemoveFilter: the length of input parameter filterName is too long! len = %{public}zu", strlen(filterName));
1814d949f91Sopenharmony_ci
1824d949f91Sopenharmony_ci    int32_t count = 0;
1834d949f91Sopenharmony_ci    for (auto it = imageEffect->filters_.begin(); it != imageEffect->filters_.end();) {
1844d949f91Sopenharmony_ci        if (it->second.compare(filterName) == 0) {
1854d949f91Sopenharmony_ci            imageEffect->imageEffect_->RemoveEFilter(it->first->filter_);
1864d949f91Sopenharmony_ci            auto filter = it->first;
1874d949f91Sopenharmony_ci            if (filter != nullptr && filter->isCreatedBySystem_) {
1884d949f91Sopenharmony_ci                OH_EffectFilter_Release(filter);
1894d949f91Sopenharmony_ci            }
1904d949f91Sopenharmony_ci            it = imageEffect->filters_.erase(it);
1914d949f91Sopenharmony_ci            count++;
1924d949f91Sopenharmony_ci        } else {
1934d949f91Sopenharmony_ci            ++it;
1944d949f91Sopenharmony_ci        }
1954d949f91Sopenharmony_ci    }
1964d949f91Sopenharmony_ci    EFFECT_LOGI("Remove filter. name=%{public}s, count=%{public}d", filterName, count);
1974d949f91Sopenharmony_ci
1984d949f91Sopenharmony_ci    EventInfo eventInfo = {
1994d949f91Sopenharmony_ci        .filterName = filterName,
2004d949f91Sopenharmony_ci        .filterNum = count,
2014d949f91Sopenharmony_ci    };
2024d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(REMOVE_FILTER_STATISTIC, eventInfo);
2034d949f91Sopenharmony_ci    return count;
2044d949f91Sopenharmony_ci}
2054d949f91Sopenharmony_ci
2064d949f91Sopenharmony_ciEFFECT_EXPORT
2074d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_RemoveFilterByIndex(OH_ImageEffect *imageEffect, uint32_t index)
2084d949f91Sopenharmony_ci{
2094d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
2104d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
2114d949f91Sopenharmony_ci        "RemoveFilterByIndex: input parameter imageEffect is null!");
2124d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(index < static_cast<uint32_t>(imageEffect->filters_.size()),
2134d949f91Sopenharmony_ci        ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
2144d949f91Sopenharmony_ci        "RemoveFilterByIndex: input parameter index is invalid! index=%{public}d, efilterSize=%{public}zu",
2154d949f91Sopenharmony_ci        index, imageEffect->filters_.size());
2164d949f91Sopenharmony_ci
2174d949f91Sopenharmony_ci    ErrorCode result = imageEffect->imageEffect_->RemoveEFilter(index);
2184d949f91Sopenharmony_ci    if (result != ErrorCode::SUCCESS) {
2194d949f91Sopenharmony_ci        EFFECT_LOGE("RemoveFilterByIndex: remove filter fail! result=%{public}d", result);
2204d949f91Sopenharmony_ci        return ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID;
2214d949f91Sopenharmony_ci    }
2224d949f91Sopenharmony_ci
2234d949f91Sopenharmony_ci    const auto &filter = imageEffect->filters_.at(index);
2244d949f91Sopenharmony_ci    auto ohEFilter = filter.first;
2254d949f91Sopenharmony_ci
2264d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ohEFilter != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
2274d949f91Sopenharmony_ci        "RemoveFilterByIndex: ohEFilter is null!");
2284d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ohEFilter->filter_ != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
2294d949f91Sopenharmony_ci        "RemoveFilterByIndex: filter of ohEFilter is null!");
2304d949f91Sopenharmony_ci
2314d949f91Sopenharmony_ci    std::string filterName = ohEFilter->filter_->GetName();
2324d949f91Sopenharmony_ci    if (ohEFilter->isCreatedBySystem_) {
2334d949f91Sopenharmony_ci        OH_EffectFilter_Release(ohEFilter);
2344d949f91Sopenharmony_ci    }
2354d949f91Sopenharmony_ci    imageEffect->filters_.erase(imageEffect->filters_.begin() + index);
2364d949f91Sopenharmony_ci    EFFECT_LOGI("Remove filter by index. name=%{public}s, index=%{public}d", filterName.c_str(), index);
2374d949f91Sopenharmony_ci
2384d949f91Sopenharmony_ci    EventInfo eventInfo = {
2394d949f91Sopenharmony_ci        .filterName = filterName,
2404d949f91Sopenharmony_ci        .filterNum = 1,
2414d949f91Sopenharmony_ci    };
2424d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(REMOVE_FILTER_STATISTIC, eventInfo);
2434d949f91Sopenharmony_ci
2444d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
2454d949f91Sopenharmony_ci}
2464d949f91Sopenharmony_ci
2474d949f91Sopenharmony_ciEFFECT_EXPORT
2484d949f91Sopenharmony_ciOH_EffectFilter *OH_ImageEffect_ReplaceFilter(OH_ImageEffect *imageEffect, uint32_t index, const char *filterName)
2494d949f91Sopenharmony_ci{
2504d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, nullptr, "ReplaceFilter: input parameter imageEffect is null!");
2514d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(index < static_cast<uint32_t>(imageEffect->filters_.size()), nullptr,
2524d949f91Sopenharmony_ci        "ReplaceFilter: input parameter index is invalid! index=%{public}d, efilterSize=%{public}zu",
2534d949f91Sopenharmony_ci        index, imageEffect->filters_.size());
2544d949f91Sopenharmony_ci
2554d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filterName != nullptr, nullptr, "ReplaceFilter: input parameter filterName is null!");
2564d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(strlen(filterName) < MAX_CHAR_LEN, nullptr,
2574d949f91Sopenharmony_ci        "ReplaceFilter: the length of input parameter filterName is too long! len = %{public}zu", strlen(filterName));
2584d949f91Sopenharmony_ci
2594d949f91Sopenharmony_ci    OH_EffectFilter *filter = OH_EffectFilter_Create(filterName);
2604d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filter != nullptr, nullptr,
2614d949f91Sopenharmony_ci        "ReplaceFilter: create filter fail! name=%{public}s", filterName);
2624d949f91Sopenharmony_ci
2634d949f91Sopenharmony_ci    filter->isCreatedBySystem_ = true;
2644d949f91Sopenharmony_ci    ImageEffect_ErrorCode errorCode = OH_ImageEffect_ReplaceFilterByFilter(imageEffect, index, filter);
2654d949f91Sopenharmony_ci    if (errorCode != ImageEffect_ErrorCode::EFFECT_SUCCESS) {
2664d949f91Sopenharmony_ci        OH_EffectFilter_Release(filter);
2674d949f91Sopenharmony_ci        filter = nullptr;
2684d949f91Sopenharmony_ci    }
2694d949f91Sopenharmony_ci    return filter;
2704d949f91Sopenharmony_ci}
2714d949f91Sopenharmony_ci
2724d949f91Sopenharmony_ciEFFECT_EXPORT
2734d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_ReplaceFilterByFilter(OH_ImageEffect *imageEffect, uint32_t index,
2744d949f91Sopenharmony_ci    OH_EffectFilter *filter)
2754d949f91Sopenharmony_ci{
2764d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
2774d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
2784d949f91Sopenharmony_ci        "ReplaceFilter: input parameter imageEffect is null!");
2794d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(index < static_cast<uint32_t>(imageEffect->filters_.size()),
2804d949f91Sopenharmony_ci        ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
2814d949f91Sopenharmony_ci        "ReplaceFilter: input parameter index is invalid! index=%{public}d, efilterSize=%{public}zu",
2824d949f91Sopenharmony_ci        index, imageEffect->filters_.size());
2834d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filter != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
2844d949f91Sopenharmony_ci        "ReplaceFilter: input parameter filterName is null!");
2854d949f91Sopenharmony_ci
2864d949f91Sopenharmony_ci    EFFECT_LOGI("Replace filter. index=%{public}d, name=%{public}s", index, filter->filter_->GetName().c_str());
2874d949f91Sopenharmony_ci
2884d949f91Sopenharmony_ci    ErrorCode result = imageEffect->imageEffect_->ReplaceEFilter(filter->filter_, index);
2894d949f91Sopenharmony_ci    if (result != ErrorCode::SUCCESS) {
2904d949f91Sopenharmony_ci        EFFECT_LOGE("ReplaceFilter fail! result=%{public}d, index=%{public}d", result, index);
2914d949f91Sopenharmony_ci        return ImageEffect_ErrorCode ::EFFECT_ERROR_PARAM_INVALID;
2924d949f91Sopenharmony_ci    }
2934d949f91Sopenharmony_ci
2944d949f91Sopenharmony_ci    auto &originFilter = imageEffect->filters_.at(index);
2954d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(originFilter.first != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
2964d949f91Sopenharmony_ci        "ReplaceFilter:originFilter is null!");
2974d949f91Sopenharmony_ci    if (originFilter.first->isCreatedBySystem_) {
2984d949f91Sopenharmony_ci        OH_EffectFilter_Release(originFilter.first);
2994d949f91Sopenharmony_ci    }
3004d949f91Sopenharmony_ci    imageEffect->filters_[index] = std::pair<OH_EffectFilter *, std::string>(filter, filter->filter_->GetName());
3014d949f91Sopenharmony_ci
3024d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
3034d949f91Sopenharmony_ci}
3044d949f91Sopenharmony_ci
3054d949f91Sopenharmony_ciEFFECT_EXPORT
3064d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_Configure(OH_ImageEffect *imageEffect, const char *key,
3074d949f91Sopenharmony_ci    const ImageEffect_Any *value)
3084d949f91Sopenharmony_ci{
3094d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3104d949f91Sopenharmony_ci        "Configure: input parameter imageEffect is null!");
3114d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(key != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3124d949f91Sopenharmony_ci        "Configure: input parameter key is null!");
3134d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(strlen(key) < MAX_CHAR_LEN, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3144d949f91Sopenharmony_ci        "Configure: the length of input parameter key is too long! len = %{public}zu", strlen(key));
3154d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(value != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3164d949f91Sopenharmony_ci        "Configure: input parameter value is null!");
3174d949f91Sopenharmony_ci
3184d949f91Sopenharmony_ci    Plugin::Any any;
3194d949f91Sopenharmony_ci    ErrorCode result = NativeCommonUtils::ParseOHAny(value, any);
3204d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(result == ErrorCode::SUCCESS, ImageEffect_ErrorCode::EFFECT_PARAM_ERROR,
3214d949f91Sopenharmony_ci        "Configure: parse oh any fail! result=%{public}d, key=%{public}s, dataType=%{public}d",
3224d949f91Sopenharmony_ci        result, key, value->dataType);
3234d949f91Sopenharmony_ci
3244d949f91Sopenharmony_ci    result = imageEffect->imageEffect_->Configure(key, any);
3254d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(result == ErrorCode::SUCCESS, ImageEffect_ErrorCode::EFFECT_PARAM_ERROR,
3264d949f91Sopenharmony_ci        "Configure: config fail! result=%{public}d, key=%{public}s, dataType=%{public}d", result, key, value->dataType);
3274d949f91Sopenharmony_ci
3284d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
3294d949f91Sopenharmony_ci}
3304d949f91Sopenharmony_ci
3314d949f91Sopenharmony_ciEFFECT_EXPORT
3324d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetOutputSurface(OH_ImageEffect *imageEffect, NativeWindow *nativeWindow)
3334d949f91Sopenharmony_ci{
3344d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
3354d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3364d949f91Sopenharmony_ci        "SetOutputSurface: input parameter imageEffect is null!");
3374d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(nativeWindow != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3384d949f91Sopenharmony_ci        "SetOutputSurface: input parameter nativeWindow is null!");
3394d949f91Sopenharmony_ci
3404d949f91Sopenharmony_ci    ErrorCode errorCode = imageEffect->imageEffect_->SetOutNativeWindow(nativeWindow);
3414d949f91Sopenharmony_ci    if (errorCode != ErrorCode::SUCCESS) {
3424d949f91Sopenharmony_ci        EFFECT_LOGE("SetOutputSurface: set output surface fail! errorCode=%{public}d", errorCode);
3434d949f91Sopenharmony_ci        return ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID;
3444d949f91Sopenharmony_ci    }
3454d949f91Sopenharmony_ci
3464d949f91Sopenharmony_ci    EventInfo eventInfo = {
3474d949f91Sopenharmony_ci        .dataType = EventDataType::SURFACE,
3484d949f91Sopenharmony_ci    };
3494d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(INPUT_DATA_TYPE_STATISTIC, eventInfo);
3504d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
3514d949f91Sopenharmony_ci}
3524d949f91Sopenharmony_ci
3534d949f91Sopenharmony_ciEFFECT_EXPORT
3544d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_GetInputSurface(OH_ImageEffect *imageEffect, NativeWindow **nativeWindow)
3554d949f91Sopenharmony_ci{
3564d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3574d949f91Sopenharmony_ci        "GetInputSurface: input parameter imageEffect is null!");
3584d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(nativeWindow != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3594d949f91Sopenharmony_ci        "GetInputSurface: input parameter surfaceId is null!");
3604d949f91Sopenharmony_ci
3614d949f91Sopenharmony_ci    OHOS::sptr<OHOS::Surface> surface = imageEffect->imageEffect_->GetInputSurface();
3624d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(surface != nullptr, ImageEffect_ErrorCode::EFFECT_UNKNOWN,
3634d949f91Sopenharmony_ci        "GetInputSurface: get input surface fail! surface is null!");
3644d949f91Sopenharmony_ci
3654d949f91Sopenharmony_ci    *nativeWindow = OH_NativeWindow_CreateNativeWindow(&surface);
3664d949f91Sopenharmony_ci
3674d949f91Sopenharmony_ci    EventInfo eventInfo = {
3684d949f91Sopenharmony_ci        .dataType = EventDataType::SURFACE,
3694d949f91Sopenharmony_ci    };
3704d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(OUTPUT_DATA_TYPE_STATISTIC, eventInfo);
3714d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
3724d949f91Sopenharmony_ci}
3734d949f91Sopenharmony_ci
3744d949f91Sopenharmony_ciEFFECT_EXPORT
3754d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetInputPixelmap(OH_ImageEffect *imageEffect, OH_PixelmapNative *pixelmap)
3764d949f91Sopenharmony_ci{
3774d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
3784d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3794d949f91Sopenharmony_ci        "SetInputPixelmap: input parameter imageEffect is null!");
3804d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(pixelmap != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
3814d949f91Sopenharmony_ci        "SetInputPixelmap: input parameter pixelmap is null!");
3824d949f91Sopenharmony_ci
3834d949f91Sopenharmony_ci    ErrorCode errorCode =
3844d949f91Sopenharmony_ci        imageEffect->imageEffect_->SetInputPixelMap(NativeCommonUtils::GetPixelMapFromOHPixelmap(pixelmap));
3854d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(errorCode == ErrorCode::SUCCESS, ImageEffect_ErrorCode::EFFECT_PARAM_ERROR,
3864d949f91Sopenharmony_ci        "SetInputPixelMap: set input pixelmap fail! errorCode=%{public}d", errorCode);
3874d949f91Sopenharmony_ci
3884d949f91Sopenharmony_ci    EventInfo eventInfo = {
3894d949f91Sopenharmony_ci        .dataType = EventDataType::PIXEL_MAP,
3904d949f91Sopenharmony_ci    };
3914d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(INPUT_DATA_TYPE_STATISTIC, eventInfo);
3924d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
3934d949f91Sopenharmony_ci}
3944d949f91Sopenharmony_ci
3954d949f91Sopenharmony_ciEFFECT_EXPORT
3964d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetOutputPixelmap(OH_ImageEffect *imageEffect, OH_PixelmapNative *pixelmap)
3974d949f91Sopenharmony_ci{
3984d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
3994d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4004d949f91Sopenharmony_ci        "SetOutputPixelmap: input parameter imageEffect is null!");
4014d949f91Sopenharmony_ci
4024d949f91Sopenharmony_ci    ErrorCode errorCode =
4034d949f91Sopenharmony_ci        imageEffect->imageEffect_->SetOutputPixelMap(NativeCommonUtils::GetPixelMapFromOHPixelmap(pixelmap));
4044d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(errorCode == ErrorCode::SUCCESS, ImageEffect_ErrorCode::EFFECT_PARAM_ERROR,
4054d949f91Sopenharmony_ci        "SetOutputPixelmap: set output pixelmap fail! errorCode=%{public}d", errorCode);
4064d949f91Sopenharmony_ci
4074d949f91Sopenharmony_ci    EventInfo eventInfo = {
4084d949f91Sopenharmony_ci        .dataType = EventDataType::PIXEL_MAP,
4094d949f91Sopenharmony_ci    };
4104d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(OUTPUT_DATA_TYPE_STATISTIC, eventInfo);
4114d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
4124d949f91Sopenharmony_ci}
4134d949f91Sopenharmony_ci
4144d949f91Sopenharmony_ciEFFECT_EXPORT
4154d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetInputNativeBuffer(OH_ImageEffect *imageEffect, OH_NativeBuffer *nativeBuffer)
4164d949f91Sopenharmony_ci{
4174d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
4184d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4194d949f91Sopenharmony_ci        "SetInputNativeBuffer: input parameter imageEffect is null!");
4204d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(nativeBuffer != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4214d949f91Sopenharmony_ci        "SetInputNativeBuffer: input parameter input nativeBuffer is null!");
4224d949f91Sopenharmony_ci
4234d949f91Sopenharmony_ci    OHOS::SurfaceBuffer *surfaceBuffer = OHOS::SurfaceBuffer::NativeBufferToSurfaceBuffer(nativeBuffer);
4244d949f91Sopenharmony_ci
4254d949f91Sopenharmony_ci    ErrorCode errorCode = imageEffect->imageEffect_->SetInputSurfaceBuffer(surfaceBuffer);
4264d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(errorCode == ErrorCode::SUCCESS,
4274d949f91Sopenharmony_ci        ImageEffect_ErrorCode::EFFECT_PARAM_ERROR,
4284d949f91Sopenharmony_ci        "SetInputNativeBuffer: set input native buffer fail! errorCode=%{public}d", errorCode);
4294d949f91Sopenharmony_ci
4304d949f91Sopenharmony_ci    EventInfo eventInfo = {
4314d949f91Sopenharmony_ci        .dataType = EventDataType::SURFACE_BUFFER,
4324d949f91Sopenharmony_ci    };
4334d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(INPUT_DATA_TYPE_STATISTIC, eventInfo);
4344d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
4354d949f91Sopenharmony_ci}
4364d949f91Sopenharmony_ci
4374d949f91Sopenharmony_ciEFFECT_EXPORT
4384d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetOutputNativeBuffer(OH_ImageEffect *imageEffect, OH_NativeBuffer *nativeBuffer)
4394d949f91Sopenharmony_ci{
4404d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
4414d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4424d949f91Sopenharmony_ci        "SetOutputNativeBuffer: input parameter imageEffect is null!");
4434d949f91Sopenharmony_ci
4444d949f91Sopenharmony_ci    OHOS::SurfaceBuffer *surfaceBuffer = OHOS::SurfaceBuffer::NativeBufferToSurfaceBuffer(nativeBuffer);
4454d949f91Sopenharmony_ci
4464d949f91Sopenharmony_ci    ErrorCode errorCode = imageEffect->imageEffect_->SetOutputSurfaceBuffer(surfaceBuffer);
4474d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(errorCode == ErrorCode::SUCCESS, ImageEffect_ErrorCode::EFFECT_PARAM_ERROR,
4484d949f91Sopenharmony_ci        "SetOutputNativeBuffer: set output native buffer fail! errorCode=%{public}d", errorCode);
4494d949f91Sopenharmony_ci
4504d949f91Sopenharmony_ci    EventInfo eventInfo = {
4514d949f91Sopenharmony_ci        .dataType = EventDataType::SURFACE_BUFFER,
4524d949f91Sopenharmony_ci    };
4534d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(OUTPUT_DATA_TYPE_STATISTIC, eventInfo);
4544d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
4554d949f91Sopenharmony_ci}
4564d949f91Sopenharmony_ci
4574d949f91Sopenharmony_ciEFFECT_EXPORT
4584d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetInputUri(OH_ImageEffect *imageEffect, const char *uri)
4594d949f91Sopenharmony_ci{
4604d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
4614d949f91Sopenharmony_ci    EFFECT_LOGD("Set input uri");
4624d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4634d949f91Sopenharmony_ci        "SetInputUri: input parameter imageEffect is null!");
4644d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(uri != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4654d949f91Sopenharmony_ci        "SetInputUri: input parameter input uri is null!");
4664d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(strlen(uri) < MAX_CHAR_LEN, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4674d949f91Sopenharmony_ci        "SetInputUri: the length of input parameter uri is too long! len = %{public}zu", strlen(uri));
4684d949f91Sopenharmony_ci
4694d949f91Sopenharmony_ci    ErrorCode errorCode = imageEffect->imageEffect_->SetInputUri(uri);
4704d949f91Sopenharmony_ci    if (errorCode != ErrorCode::SUCCESS) {
4714d949f91Sopenharmony_ci        EFFECT_LOGE("SetInputUri: set input uri fail! errorCode=%{public}d", errorCode);
4724d949f91Sopenharmony_ci        return ImageEffect_ErrorCode::EFFECT_PARAM_ERROR;
4734d949f91Sopenharmony_ci    }
4744d949f91Sopenharmony_ci
4754d949f91Sopenharmony_ci    EventInfo eventInfo = {
4764d949f91Sopenharmony_ci        .dataType = EventDataType::URI,
4774d949f91Sopenharmony_ci    };
4784d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(INPUT_DATA_TYPE_STATISTIC, eventInfo);
4794d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
4804d949f91Sopenharmony_ci}
4814d949f91Sopenharmony_ci
4824d949f91Sopenharmony_ciEFFECT_EXPORT
4834d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetOutputUri(OH_ImageEffect *imageEffect, const char *uri)
4844d949f91Sopenharmony_ci{
4854d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
4864d949f91Sopenharmony_ci    EFFECT_LOGD("Set output uri.");
4874d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4884d949f91Sopenharmony_ci        "SetOutputUri: input parameter imageEffect is null!");
4894d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(uri != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4904d949f91Sopenharmony_ci        "SetOutputUri: input parameter uri is null!");
4914d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(strlen(uri) < MAX_CHAR_LEN, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
4924d949f91Sopenharmony_ci        "SetOutputUri: the length of input parameter uri is too long! len = %{public}zu", strlen(uri));
4934d949f91Sopenharmony_ci
4944d949f91Sopenharmony_ci    std::string strUri;
4954d949f91Sopenharmony_ci    if (uri != nullptr) {
4964d949f91Sopenharmony_ci        strUri = uri;
4974d949f91Sopenharmony_ci    }
4984d949f91Sopenharmony_ci    ErrorCode errorCode = imageEffect->imageEffect_->SetOutputUri(strUri);
4994d949f91Sopenharmony_ci    if (errorCode != ErrorCode::SUCCESS) {
5004d949f91Sopenharmony_ci        EFFECT_LOGE("SetOutputUri: set output uri fail! errorCode=%{public}d", errorCode);
5014d949f91Sopenharmony_ci        return ImageEffect_ErrorCode::EFFECT_PARAM_ERROR;
5024d949f91Sopenharmony_ci    }
5034d949f91Sopenharmony_ci
5044d949f91Sopenharmony_ci    EventInfo eventInfo = {
5054d949f91Sopenharmony_ci        .dataType = EventDataType::URI,
5064d949f91Sopenharmony_ci    };
5074d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(OUTPUT_DATA_TYPE_STATISTIC, eventInfo);
5084d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
5094d949f91Sopenharmony_ci}
5104d949f91Sopenharmony_ci
5114d949f91Sopenharmony_ciEFFECT_EXPORT
5124d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetInputPicture(OH_ImageEffect *imageEffect, OH_PictureNative *picture)
5134d949f91Sopenharmony_ci{
5144d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
5154d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
5164d949f91Sopenharmony_ci        "SetInputPicture: input parameter imageEffect is null!");
5174d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(picture != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
5184d949f91Sopenharmony_ci        "SetInputPicture: input parameter picture is null!");
5194d949f91Sopenharmony_ci
5204d949f91Sopenharmony_ci    ErrorCode errorCode =
5214d949f91Sopenharmony_ci        imageEffect->imageEffect_->SetInputPicture(NativeCommonUtils::GetPictureFromNativePicture(picture));
5224d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(errorCode == ErrorCode::SUCCESS, ImageEffect_ErrorCode::EFFECT_PARAM_ERROR,
5234d949f91Sopenharmony_ci        "SetInputPicture: set input picture fail! errorCode=%{public}d", errorCode);
5244d949f91Sopenharmony_ci
5254d949f91Sopenharmony_ci    EventInfo eventInfo = {
5264d949f91Sopenharmony_ci        .dataType = EventDataType::PICTURE,
5274d949f91Sopenharmony_ci    };
5284d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(INPUT_DATA_TYPE_STATISTIC, eventInfo);
5294d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
5304d949f91Sopenharmony_ci}
5314d949f91Sopenharmony_ci
5324d949f91Sopenharmony_ciEFFECT_EXPORT
5334d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_SetOutputPicture(OH_ImageEffect *imageEffect, OH_PictureNative *picture)
5344d949f91Sopenharmony_ci{
5354d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
5364d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
5374d949f91Sopenharmony_ci        "SetOutputPicture: input parameter imageEffect is null!");
5384d949f91Sopenharmony_ci
5394d949f91Sopenharmony_ci    ErrorCode errorCode =
5404d949f91Sopenharmony_ci        imageEffect->imageEffect_->SetOutputPicture(NativeCommonUtils::GetPictureFromNativePicture(picture));
5414d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(errorCode == ErrorCode::SUCCESS, ImageEffect_ErrorCode::EFFECT_PARAM_ERROR,
5424d949f91Sopenharmony_ci        "SetOutputPicture: set output picture fail! errorCode=%{public}d", errorCode);
5434d949f91Sopenharmony_ci
5444d949f91Sopenharmony_ci    EventInfo eventInfo = {
5454d949f91Sopenharmony_ci        .dataType = EventDataType::PICTURE,
5464d949f91Sopenharmony_ci    };
5474d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(OUTPUT_DATA_TYPE_STATISTIC, eventInfo);
5484d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
5494d949f91Sopenharmony_ci}
5504d949f91Sopenharmony_ci
5514d949f91Sopenharmony_ciEFFECT_EXPORT
5524d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_Start(OH_ImageEffect *imageEffect)
5534d949f91Sopenharmony_ci{
5544d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
5554d949f91Sopenharmony_ci    if (imageEffect == nullptr) {
5564d949f91Sopenharmony_ci        ImageEffect_ErrorCode errorCode = ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID;
5574d949f91Sopenharmony_ci        NativeCommonUtils::ReportEventStartFailed(errorCode, "OH_ImageEffect_Start: imageEffect is null");
5584d949f91Sopenharmony_ci        EFFECT_LOGE("Start: input parameter imageEffect is null!");
5594d949f91Sopenharmony_ci        return errorCode;
5604d949f91Sopenharmony_ci    }
5614d949f91Sopenharmony_ci
5624d949f91Sopenharmony_ci    ErrorCode errorCode = imageEffect->imageEffect_->Start();
5634d949f91Sopenharmony_ci    if (errorCode != ErrorCode::SUCCESS) {
5644d949f91Sopenharmony_ci        ImageEffect_ErrorCode res = NativeCommonUtils::ConvertStartResult(errorCode);
5654d949f91Sopenharmony_ci        NativeCommonUtils::ReportEventStartFailed(res, "OH_ImageEffect_Start fail!");
5664d949f91Sopenharmony_ci        EFFECT_LOGE("Start: start fail! errorCode=%{public}d", errorCode);
5674d949f91Sopenharmony_ci        return res;
5684d949f91Sopenharmony_ci    }
5694d949f91Sopenharmony_ci
5704d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
5714d949f91Sopenharmony_ci}
5724d949f91Sopenharmony_ci
5734d949f91Sopenharmony_ciEFFECT_EXPORT
5744d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_Stop(OH_ImageEffect *imageEffect)
5754d949f91Sopenharmony_ci{
5764d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
5774d949f91Sopenharmony_ci        "Stop: input parameter imageEffect is null!");
5784d949f91Sopenharmony_ci    imageEffect->imageEffect_->Stop();
5794d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
5804d949f91Sopenharmony_ci}
5814d949f91Sopenharmony_ci
5824d949f91Sopenharmony_ciEFFECT_EXPORT
5834d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_Release(OH_ImageEffect *imageEffect)
5844d949f91Sopenharmony_ci{
5854d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
5864d949f91Sopenharmony_ci        "Release: input parameter imageEffect is null!");
5874d949f91Sopenharmony_ci    delete imageEffect;
5884d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
5894d949f91Sopenharmony_ci}
5904d949f91Sopenharmony_ci
5914d949f91Sopenharmony_ciEFFECT_EXPORT
5924d949f91Sopenharmony_ciImageEffect_ErrorCode OH_ImageEffect_Save(OH_ImageEffect *imageEffect, char **info)
5934d949f91Sopenharmony_ci{
5944d949f91Sopenharmony_ci    EFFECT_LOGD("Save effect.");
5954d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
5964d949f91Sopenharmony_ci        "Save: input parameter imageEffect is null!");
5974d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(info != nullptr, ImageEffect_ErrorCode::EFFECT_ERROR_PARAM_INVALID,
5984d949f91Sopenharmony_ci        "Save: input parameter info is null!");
5994d949f91Sopenharmony_ci
6004d949f91Sopenharmony_ci    EffectJsonPtr effectInfo = EFFECTJsonHelper::CreateObject();
6014d949f91Sopenharmony_ci    imageEffect->imageEffect_->Save(effectInfo);
6024d949f91Sopenharmony_ci    std::string infoStr = effectInfo->ToString();
6034d949f91Sopenharmony_ci
6044d949f91Sopenharmony_ci    char *infoChar = new char[infoStr.length() + 1];
6054d949f91Sopenharmony_ci    imageEffect->saveJson = infoChar;
6064d949f91Sopenharmony_ci    auto ret = strcpy_s(infoChar, infoStr.length() + 1, infoStr.c_str());
6074d949f91Sopenharmony_ci    if (ret != 0) {
6084d949f91Sopenharmony_ci        EFFECT_LOGE("Save: strcpy for infoChar failed, ret is %{public}d", ret);
6094d949f91Sopenharmony_ci        return ImageEffect_ErrorCode::EFFECT_UNKNOWN;
6104d949f91Sopenharmony_ci    }
6114d949f91Sopenharmony_ci    *info = infoChar;
6124d949f91Sopenharmony_ci
6134d949f91Sopenharmony_ci    EventInfo eventInfo;
6144d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(SAVE_IMAGE_EFFECT_BEHAVIOR, eventInfo);
6154d949f91Sopenharmony_ci    return ImageEffect_ErrorCode::EFFECT_SUCCESS;
6164d949f91Sopenharmony_ci}
6174d949f91Sopenharmony_ci
6184d949f91Sopenharmony_ciEFFECT_EXPORT
6194d949f91Sopenharmony_ciOH_ImageEffect *OH_ImageEffect_Restore(const char *info)
6204d949f91Sopenharmony_ci{
6214d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(info != nullptr, nullptr, "Restore: input parameter info is null!");
6224d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(strlen(info) < MAX_INFO_LEN, nullptr,
6234d949f91Sopenharmony_ci        "Restore: the length of input parameter info is too long! len = %{public}zu", strlen(info));
6244d949f91Sopenharmony_ci    std::string infoStr = info;
6254d949f91Sopenharmony_ci    const EffectJsonPtr root = EFFECTJsonHelper::ParseJsonData(infoStr);
6264d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(root->HasElement("imageEffect"), nullptr, "OH_ImageEffect_Restore no imageEffect");
6274d949f91Sopenharmony_ci    EffectJsonPtr imageInfo = root->GetElement("imageEffect");
6284d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageInfo != nullptr, nullptr, "OH_ImageEffect_Restore imageInfo is nullptr");
6294d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageInfo->HasElement("name"), nullptr, "OH_ImageEffect_Restore no name");
6304d949f91Sopenharmony_ci
6314d949f91Sopenharmony_ci    std::string effectName = imageInfo->GetString("name");
6324d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(!effectName.empty(), nullptr, "OH_ImageEffect_Restore imageEffect get name failed");
6334d949f91Sopenharmony_ci    OH_ImageEffect* ohImageEffect = OH_ImageEffect_Create(effectName.c_str());
6344d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ohImageEffect != nullptr, nullptr, "ohImageEffect create failed");
6354d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageInfo->HasElement("filters"), nullptr, "OH_ImageEffect_Restore no filters");
6364d949f91Sopenharmony_ci    EffectJsonPtr filters = imageInfo->GetElement("filters");
6374d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filters != nullptr, nullptr, "OH_ImageEffect_Restore filters is null!");
6384d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(filters->IsArray(), nullptr, "OH_ImageEffect_Restore filters not array");
6394d949f91Sopenharmony_ci    std::vector<EffectJsonPtr> effects = filters->GetArray();
6404d949f91Sopenharmony_ci
6414d949f91Sopenharmony_ci    for (auto &effect : effects) {
6424d949f91Sopenharmony_ci        std::string name = effect->GetString("name");
6434d949f91Sopenharmony_ci        CHECK_AND_CONTINUE_LOG(!name.empty(), "Restore: [name] not exist");
6444d949f91Sopenharmony_ci        std::shared_ptr<IFilterDelegate> filterDelegate = EFilterFactory::Instance()->GetDelegate(name);
6454d949f91Sopenharmony_ci        if (filterDelegate != nullptr) {
6464d949f91Sopenharmony_ci            auto *filter = static_cast<OH_EffectFilter *>(filterDelegate->Restore(effect));
6474d949f91Sopenharmony_ci            CHECK_AND_CONTINUE_LOG(filter != nullptr, "Restore: filter restore fail! name=%{public}s", name.c_str());
6484d949f91Sopenharmony_ci            filter->isCreatedBySystem_ = true;
6494d949f91Sopenharmony_ci            ohImageEffect->imageEffect_->AddEFilter(filter->filter_);
6504d949f91Sopenharmony_ci            ohImageEffect->filters_.emplace_back(filter, filter->filter_->GetName());
6514d949f91Sopenharmony_ci            continue;
6524d949f91Sopenharmony_ci        }
6534d949f91Sopenharmony_ci
6544d949f91Sopenharmony_ci        std::unique_ptr<OH_EffectFilter> nativeEFilter = std::make_unique<OH_EffectFilter>();
6554d949f91Sopenharmony_ci        std::shared_ptr<EFilter> efilter = EFilterFactory::Instance()->Restore(name, effect, nativeEFilter.get());
6564d949f91Sopenharmony_ci        CHECK_AND_CONTINUE_LOG(efilter != nullptr, "Restore: efilter restore fail! name=%{public}s", name.c_str());
6574d949f91Sopenharmony_ci        nativeEFilter->filter_ = efilter;
6584d949f91Sopenharmony_ci        nativeEFilter->isCreatedBySystem_ = true;
6594d949f91Sopenharmony_ci        ohImageEffect->filters_.emplace_back(nativeEFilter.release(), efilter->GetName());
6604d949f91Sopenharmony_ci        ohImageEffect->imageEffect_->AddEFilter(efilter);
6614d949f91Sopenharmony_ci    }
6624d949f91Sopenharmony_ci    ohImageEffect->imageEffect_->SetExtraInfo(root);
6634d949f91Sopenharmony_ci
6644d949f91Sopenharmony_ci    EventInfo eventInfo;
6654d949f91Sopenharmony_ci    EventReport::ReportHiSysEvent(RESTORE_IMAGE_EFFECT_BEHAVIOR, eventInfo);
6664d949f91Sopenharmony_ci    return ohImageEffect;
6674d949f91Sopenharmony_ci}
6684d949f91Sopenharmony_ci
6694d949f91Sopenharmony_ciEFFECT_EXPORT
6704d949f91Sopenharmony_ciint32_t OH_ImageEffect_GetFilterCount(OH_ImageEffect *imageEffect)
6714d949f91Sopenharmony_ci{
6724d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
6734d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, 0, "GetFilterCount: input parameter imageEffect is null!");
6744d949f91Sopenharmony_ci    return static_cast<int32_t>(imageEffect->filters_.size());
6754d949f91Sopenharmony_ci}
6764d949f91Sopenharmony_ci
6774d949f91Sopenharmony_ciEFFECT_EXPORT
6784d949f91Sopenharmony_ciOH_EffectFilter *OH_ImageEffect_GetFilter(OH_ImageEffect *imageEffect, uint32_t index)
6794d949f91Sopenharmony_ci{
6804d949f91Sopenharmony_ci    std::unique_lock<std::mutex> lock(effectMutex_);
6814d949f91Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(imageEffect != nullptr, nullptr, "GetFilter: input parameter imageEffect is null!");
6824d949f91Sopenharmony_ci    if (index >= static_cast<uint32_t>(imageEffect->filters_.size())) {
6834d949f91Sopenharmony_ci        EFFECT_LOGE("GetFilter: input parameter index is invalid!");
6844d949f91Sopenharmony_ci        return nullptr;
6854d949f91Sopenharmony_ci    }
6864d949f91Sopenharmony_ci    return imageEffect->filters_.at(index).first;
6874d949f91Sopenharmony_ci}
6884d949f91Sopenharmony_ci
6894d949f91Sopenharmony_ci#ifdef __cplusplus
6904d949f91Sopenharmony_ci}
6914d949f91Sopenharmony_ci#endif