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 #ifndef OHOS_ROSEN_WM_ANIMATION_COMMON_H 17 #define OHOS_ROSEN_WM_ANIMATION_COMMON_H 18 19 #include <iomanip> 20 #include <memory> 21 #include <sstream> 22 #include <string> 23 24 #include <parcel.h> 25 26 namespace OHOS { 27 namespace Rosen { 28 /** 29 * @brief Enumerates window transition type. 30 */ 31 enum class WindowTransitionType : uint32_t { 32 /** 33 * window destroy. 34 */ 35 DESTROY = 0, 36 37 /** 38 * end type. 39 */ 40 END, 41 }; 42 43 /** 44 * @brief Enumerates window animation curve type. 45 */ 46 enum class WindowAnimationCurve : uint32_t { 47 /** 48 * animation curve type linear. 49 */ 50 LINEAR = 0, 51 52 /** 53 * animation curve type interpolation_spring. 54 */ 55 INTERPOLATION_SPRING = 1, 56 57 /** 58 * animation curve type cubic bezier curve. 59 */ 60 CUBIC_BEZIER = 2, 61 }; 62 63 /** 64 * Describes the window animation type 65 */ 66 enum class AnimationType : uint32_t { 67 /** 68 * window animation type fade in out 69 */ 70 FADE_IN_OUT = 0, 71 /** 72 * window animation type fade in 73 */ 74 FADE_IN = 1, 75 /** 76 * end type 77 */ 78 END, 79 }; 80 81 const uint32_t ANIMATION_PARAM_SIZE = 4; 82 const uint32_t ANIMATION_MAX_DURATION = 3000; 83 const uint32_t FLOAT_VALUE_LENGTH = 2; 84 85 // LCOV_EXCL_START 86 struct WindowAnimationProperty : public Parcelable { 87 float targetScale = 0.0f; 88 MarshallingWindowAnimationProperty89 bool Marshalling(Parcel& parcel) const override 90 { 91 if (!parcel.WriteFloat(targetScale)) { 92 return false; 93 } 94 return true; 95 } 96 UnmarshallingWindowAnimationProperty97 static WindowAnimationProperty* Unmarshalling(Parcel& parcel) 98 { 99 WindowAnimationProperty* animationProperty = new WindowAnimationProperty(); 100 if (!parcel.ReadFloat(animationProperty->targetScale)) { 101 delete animationProperty; 102 return nullptr; 103 } 104 return animationProperty; 105 } 106 }; 107 // LCOV_EXCL_STOP 108 109 /** 110 * @brief Window transition animation configuration. 111 */ 112 struct WindowAnimationOption : public Parcelable { 113 WindowAnimationCurve curve = WindowAnimationCurve::LINEAR; 114 uint32_t duration = 0; 115 std::array<float, ANIMATION_PARAM_SIZE> params; 116 117 // LCOV_EXCL_START MarshallingWindowAnimationOption118 bool Marshalling(Parcel& parcel) const override 119 { 120 if (!(parcel.WriteUint32(static_cast<uint32_t>(curve)) && parcel.WriteUint32(duration))) { 121 return false; 122 } 123 if (params.size() > ANIMATION_PARAM_SIZE) { 124 return false; 125 } 126 for (const auto& param: params) { 127 if (!parcel.WriteFloat(param)) { 128 return false; 129 } 130 } 131 return true; 132 } 133 // LCOV_EXCL_STOP 134 UnmarshallingWindowAnimationOption135 static WindowAnimationOption* Unmarshalling(Parcel& parcel) 136 { 137 WindowAnimationOption* windowAnimationConfig = new WindowAnimationOption(); 138 uint32_t curve = 0; 139 if (!parcel.ReadUint32(curve)) { 140 delete windowAnimationConfig; 141 return nullptr; 142 } 143 windowAnimationConfig->curve = static_cast<WindowAnimationCurve>(curve); 144 if (!parcel.ReadUint32(windowAnimationConfig->duration)) { 145 delete windowAnimationConfig; 146 return nullptr; 147 } 148 if (windowAnimationConfig->params.size() > ANIMATION_PARAM_SIZE) { 149 delete windowAnimationConfig; 150 return nullptr; 151 } 152 for (auto& param: windowAnimationConfig->params) { 153 if (!parcel.ReadFloat(param)) { 154 delete windowAnimationConfig; 155 return nullptr; 156 } 157 } 158 return windowAnimationConfig; 159 } 160 161 // LCOV_EXCL_START ToStringWindowAnimationOption162 std::string ToString() const 163 { 164 std::ostringstream oss; 165 oss << "curve: " << std::to_string(static_cast<int32_t>(curve)) << ", duration: " << \ 166 std::to_string(static_cast<int32_t>(duration)) << ", params: [ "; 167 for (auto param: params) { 168 oss << std::fixed << std::setprecision(FLOAT_VALUE_LENGTH) << param << ", "; 169 } 170 oss << "]"; 171 return oss.str(); 172 } 173 // LCOV_EXCL_STOP 174 }; 175 176 /** 177 * The animation configuration of start scene animation 178 */ 179 struct StartAnimationOptions : public Parcelable { 180 /** 181 * The type of window animation 182 */ 183 AnimationType animationType = AnimationType::FADE_IN_OUT; 184 185 // LCOV_EXCL_START MarshallingStartAnimationOptions186 bool Marshalling(Parcel& parcel) const override 187 { 188 if (!parcel.WriteUint32(static_cast<uint32_t>(animationType))) { 189 return false; 190 } 191 return true; 192 } 193 // LCOV_EXCL_STOP 194 UnmarshallingStartAnimationOptions195 static StartAnimationOptions* Unmarshalling(Parcel& parcel) 196 { 197 StartAnimationOptions* startAnimationOptions = new StartAnimationOptions(); 198 uint32_t animationType = 0; 199 if (!parcel.ReadUint32(animationType)) { 200 delete startAnimationOptions; 201 return nullptr; 202 } 203 startAnimationOptions->animationType = static_cast<AnimationType>(animationType); 204 return startAnimationOptions; 205 } 206 }; 207 208 /** 209 * The animation configuration of SA start scene animation 210 */ 211 struct StartAnimationSystemOptions : public Parcelable { 212 /** 213 * The type of window animation 214 */ 215 AnimationType animationType = AnimationType::FADE_IN_OUT; 216 /** 217 * The config of start secne animation 218 */ 219 std::shared_ptr<WindowAnimationOption> animationConfig = nullptr; 220 221 // LCOV_EXCL_START MarshallingStartAnimationSystemOptions222 bool Marshalling(Parcel& parcel) const override 223 { 224 if (!parcel.WriteUint32(static_cast<uint32_t>(animationType))) { 225 return false; 226 } 227 if (!parcel.WriteParcelable(animationConfig.get())) { 228 return false; 229 } 230 return true; 231 } 232 // LCOV_EXCL_STOP 233 UnmarshallingStartAnimationSystemOptions234 static StartAnimationSystemOptions* Unmarshalling(Parcel& parcel) 235 { 236 StartAnimationSystemOptions* startAnimationSystemOptions = new StartAnimationSystemOptions(); 237 uint32_t animationType = 0; 238 if (!parcel.ReadUint32(animationType)) { 239 delete startAnimationSystemOptions; 240 return nullptr; 241 } 242 startAnimationSystemOptions->animationType = static_cast<AnimationType>(animationType); 243 startAnimationSystemOptions->animationConfig = 244 std::shared_ptr<WindowAnimationOption>(parcel.ReadParcelable<WindowAnimationOption>()); 245 return startAnimationSystemOptions; 246 } 247 }; 248 249 /** 250 * The animation params of window create 251 */ 252 struct WindowCreateParams : public Parcelable { 253 /** 254 * The animation configuration of start scene animation 255 */ 256 std::shared_ptr<StartAnimationOptions> animationParams = nullptr; 257 /** 258 * The animation configuration of SA start scene animation 259 */ 260 std::shared_ptr<StartAnimationSystemOptions> animationSystemParams = nullptr; 261 262 // LCOV_EXCL_START MarshallingWindowCreateParams263 bool Marshalling(Parcel& parcel) const override 264 { 265 if (!parcel.WriteParcelable(animationParams.get())) { 266 return false; 267 } 268 if (!parcel.WriteParcelable(animationSystemParams.get())) { 269 return false; 270 } 271 return true; 272 } 273 // LCOV_EXCL_STOP 274 UnmarshallingWindowCreateParams275 static WindowCreateParams* Unmarshalling(Parcel& parcel) 276 { 277 WindowCreateParams* windowCreateParams = new WindowCreateParams(); 278 windowCreateParams->animationParams = 279 std::shared_ptr<StartAnimationOptions>(parcel.ReadParcelable<StartAnimationOptions>()); 280 windowCreateParams->animationSystemParams = 281 std::shared_ptr<StartAnimationSystemOptions>(parcel.ReadParcelable<StartAnimationSystemOptions>()); 282 return windowCreateParams; 283 } 284 }; 285 286 /** 287 * @brief Transition animation configuration. 288 */ 289 struct TransitionAnimation : public Parcelable { 290 WindowAnimationOption config; 291 float opacity = 1.0f; 292 293 // LCOV_EXCL_START MarshallingTransitionAnimation294 bool Marshalling(Parcel& parcel) const override 295 { 296 if (!(parcel.WriteFloat(opacity) && parcel.WriteParcelable(&config))) { 297 return false; 298 } 299 return true; 300 } 301 // LCOV_EXCL_STOP 302 UnmarshallingTransitionAnimation303 static TransitionAnimation* Unmarshalling(Parcel& parcel) 304 { 305 TransitionAnimation* transitionAnimation = new TransitionAnimation(); 306 if (!parcel.ReadFloat(transitionAnimation->opacity)) { 307 delete transitionAnimation; 308 return nullptr; 309 } 310 std::shared_ptr<WindowAnimationOption> animationConfig = 311 std::shared_ptr<WindowAnimationOption>(parcel.ReadParcelable<WindowAnimationOption>()); 312 if (animationConfig == nullptr) { 313 delete transitionAnimation; 314 return nullptr; 315 } 316 transitionAnimation->config = *animationConfig; 317 return transitionAnimation; 318 } 319 }; 320 } 321 } 322 #endif // OHOS_ROSEN_WM_ANIMATION_COMMON_H 323