• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ui_effect_controller_common.h"
17 constexpr int32_t POINTER_VALID = 1;
18 
19 namespace OHOS::Rosen {
20 template <typename T>
g_writeParcelableForEffect(std::shared_ptr<T> rsObject,Parcel & parcel)21 bool g_writeParcelableForEffect(std::shared_ptr<T> rsObject, Parcel& parcel)
22 {
23     if (rsObject == nullptr) {
24         return parcel.WriteInt32(0);
25     }
26     if (!parcel.WriteInt32(POINTER_VALID)) {
27         return false;
28     }
29     return rsObject->Marshalling(parcel);
30 }
31 
32 template <typename T>
g_readParcelableForEffect(std::shared_ptr<T> & rsObject,Parcel & parcel)33 bool g_readParcelableForEffect(std::shared_ptr<T>& rsObject, Parcel& parcel)
34 {
35     int32_t hasPtr = 0;
36     if (!parcel.ReadInt32(hasPtr)) {
37         return false;
38     }
39     if (hasPtr == POINTER_VALID) {
40         if (!T::Unmarshalling(parcel, rsObject)) {
41             return false;
42         }
43         if (rsObject == nullptr) {
44             return false;
45         }
46     }
47     return true;
48 }
49 
Marshalling(Parcel & parcel) const50 bool UIEffectParams::Marshalling(Parcel& parcel) const
51 {
52     if (!g_writeParcelableForEffect<Filter>(backgroundFilter_, parcel)) {
53         TLOGE(WmsLogTag::WMS_ANIMATION, "write background filter failed");
54         return false;
55     }
56     if (!g_writeParcelableForEffect<VisualEffect>(effect_, parcel)) {
57         TLOGE(WmsLogTag::WMS_ANIMATION, "write visual effect failed");
58         return false;
59     }
60     return true;
61 }
62 
Unmarshalling(Parcel & parcel)63 UIEffectParams* UIEffectParams::Unmarshalling(Parcel& parcel)
64 {
65     UIEffectParams* params = new UIEffectParams();
66     if (!g_readParcelableForEffect(params->backgroundFilter_, parcel)) {
67         TLOGE(WmsLogTag::WMS_ANIMATION, "read background filter failed");
68         delete params;
69         return nullptr;
70     }
71     if (!g_readParcelableForEffect(params->effect_, parcel)) {
72         TLOGE(WmsLogTag::WMS_ANIMATION, "read visual effect failed");
73         delete params;
74         return nullptr;
75     }
76     return params;
77 }
78 
ConvertToJsValue(napi_env env,napi_value & final_value)79 napi_status UIEffectParams::ConvertToJsValue(napi_env env, napi_value& final_value)
80 {
81     NAPI_CHECK(napi_create_object(env, &final_value), "create obj failed");
82     if (backgroundFilter_ != nullptr) {
83         napi_value jsFilter = nullptr;
84         NAPI_CHECK(napi_create_object(env, &jsFilter), "create jsFilter failed");
85         Filter* backgroundFilterCopy = new Filter(*backgroundFilter_);
86         napi_status status = napi_wrap(env, jsFilter, backgroundFilterCopy, WrapObjectDestructor<Filter>,
87             nullptr, nullptr);
88         if (status != napi_status::napi_ok) {
89             delete backgroundFilterCopy;
90             TLOGE(WmsLogTag::WMS_ANIMATION, "create js filter failed, reason: %{public}d", status);
91             return status;
92         }
93         NAPI_CHECK(napi_set_named_property(env, final_value, "backgroundFilter", jsFilter), "backgroundFilter");
94     }
95     if (effect_ != nullptr) {
96         napi_value jsEffect = nullptr;
97         NAPI_CHECK(napi_create_object(env, &jsEffect), "create jsEffect failed");
98         VisualEffect* effectCopy = new VisualEffect(*effect_);
99         napi_status status = napi_wrap(env, jsEffect, effectCopy, WrapObjectDestructor<VisualEffect>, nullptr, nullptr);
100         if (status != napi_status::napi_ok) {
101             delete effectCopy;
102             TLOGE(WmsLogTag::WMS_ANIMATION, "create js effect failed, reason: %{public}d", status);
103             return status;
104         }
105         NAPI_CHECK(napi_set_named_property(env, final_value, "visualEffect", jsEffect), "visualEffect");
106     }
107     return napi_status::napi_ok;
108 }
109 
ConvertFromJsValue(napi_env env,napi_value val)110 napi_status UIEffectParams::ConvertFromJsValue(napi_env env, napi_value val)
111 {
112     bool hasProperty = false;
113     NAPI_CHECK(napi_has_named_property(env, val, "backgroundFilter", &hasProperty), "has backgroundFilter");
114     if (hasProperty) {
115         backgroundFilter_ = nullptr;
116         napi_value jsBackgroundFilter = nullptr;
117         NAPI_CHECK(napi_get_named_property(env, val, "backgroundFilter", &jsBackgroundFilter), "filter");
118         Filter* filterObj = nullptr;
119         NAPI_CHECK(napi_unwrap(env, jsBackgroundFilter, reinterpret_cast<void**>(&filterObj)), "filter unwrap");
120         if (filterObj != nullptr) {
121             backgroundFilter_ = std::make_shared<Filter>(*filterObj);
122         }
123     }
124     NAPI_CHECK(napi_has_named_property(env, val, "visualEffect", &hasProperty), "has effect");
125     if (hasProperty) {
126         effect_ = nullptr;
127         napi_value jsEffect = nullptr;
128         NAPI_CHECK(napi_get_named_property(env, val, "visualEffect", &jsEffect), "effect");
129         VisualEffect* effectObj = nullptr;
130         NAPI_CHECK(napi_unwrap(env, jsEffect, reinterpret_cast<void**>(&effectObj)), "effect unwrap");
131         if (effectObj != nullptr) {
132             effect_ = std::make_shared<VisualEffect>(*effectObj);
133         }
134     }
135     return napi_status::napi_ok;
136 }
137 }