1 /* 2 * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_JSI_ANIMATOR_BRIDGE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_JSI_ANIMATOR_BRIDGE_H 18 19 #include <string> 20 #include <unordered_map> 21 22 #include "frameworks/bridge/js_frontend/engine/common/base_animation_bridge.h" 23 #include "frameworks/bridge/js_frontend/engine/jsi/jsi_engine.h" 24 25 namespace OHOS::Ace::Framework { 26 using std::shared_ptr; 27 28 class JsiAnimatorBridgeUtils { 29 public: 30 static shared_ptr<JsValue> CreateAnimatorContext(shared_ptr<JsRuntime> runtime, int32_t pageId, int32_t bridgeId); 31 static shared_ptr<JsValue> JsAnimatorPlay(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj, 32 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 33 static shared_ptr<JsValue> JsAnimatorFinish(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj, 34 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 35 static shared_ptr<JsValue> JsAnimatorPause(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj, 36 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 37 static shared_ptr<JsValue> JsAnimatorCancel(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj, 38 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 39 static shared_ptr<JsValue> JsAnimatorReverse(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj, 40 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 41 static shared_ptr<JsValue> JsAnimatorUpdate(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj, 42 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 43 static shared_ptr<JsValue> JsAnimatorReset(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj, 44 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 45 static int32_t JsCreateBridgeId(); 46 }; 47 48 class JsiAnimatorBridge : public BaseAnimationBridge { 49 DECLARE_ACE_TYPE(JsiAnimatorBridge, BaseAnimationBridge) 50 public: 51 JsiAnimatorBridge(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& animatorContext); 52 ~JsiAnimatorBridge() override; 53 54 void JsCreateAnimation(const std::string& param); JsGetAnimator()55 RefPtr<Animator> JsGetAnimator() override 56 { 57 return animator_; 58 } 59 OnJsEngineDestroy()60 void OnJsEngineDestroy() override 61 { 62 animatorObject_.reset(); 63 } 64 GetJsObject()65 shared_ptr<JsValue> GetJsObject() 66 { 67 return animatorObject_; 68 } 69 GetRuntime()70 shared_ptr<JsRuntime> GetRuntime() const 71 { 72 return runtime_; 73 } 74 75 private: 76 RefPtr<KeyframeAnimation<double>> CreateDoubleAnimation( 77 std::unordered_map<std::string, double>& animationParams, const RefPtr<Curve>& curve); 78 79 shared_ptr<JsRuntime> runtime_; 80 shared_ptr<JsValue> animatorObject_; 81 RefPtr<Animator> animator_; 82 }; 83 84 class JsiAnimatorTaskCreate : public AnimatorBridgeTask { 85 DECLARE_ACE_TYPE(JsiAnimatorTaskCreate, AnimatorBridgeTask) 86 public: 87 JsiAnimatorTaskCreate(shared_ptr<JsRuntime> runtime, 88 const RefPtr<JsiAnimatorBridge>& bridge, const std::string& param); 89 ~JsiAnimatorTaskCreate() override = default; 90 void AnimatorBridgeTaskFunc(const RefPtr<JsAcePage>& page, int32_t bridgeId) override; 91 92 private: 93 RefPtr<JsiAnimatorBridge> bridge_; 94 shared_ptr<JsRuntime> runtime_; 95 std::string param_; 96 }; 97 98 class JsiAnimatorTaskOperation : public AnimatorBridgeTask { DECLARE_ACE_TYPE(JsiAnimatorTaskOperation,AnimatorBridgeTask)99 DECLARE_ACE_TYPE(JsiAnimatorTaskOperation, AnimatorBridgeTask) 100 public: 101 explicit JsiAnimatorTaskOperation(AnimatorOperation operation) : operation_(operation) {}; 102 ~JsiAnimatorTaskOperation() override = default; 103 void AnimatorBridgeTaskFunc(const RefPtr<JsAcePage>& page, int32_t bridgeId) override; 104 105 private: 106 AnimatorOperation operation_ = AnimatorOperation::NONE; 107 }; 108 109 class JsiAnimatorTaskUpdate : public AnimatorBridgeTask { DECLARE_ACE_TYPE(JsiAnimatorTaskUpdate,AnimatorBridgeTask)110 DECLARE_ACE_TYPE(JsiAnimatorTaskUpdate, AnimatorBridgeTask) 111 public: 112 JsiAnimatorTaskUpdate(shared_ptr<JsRuntime> runtime, const std::unordered_map<std::string, std::string>& params) 113 : runtime_(runtime), params_(params) {}; 114 ~JsiAnimatorTaskUpdate() override = default; 115 void AnimatorBridgeTaskFunc(const RefPtr<JsAcePage>& page, int32_t bridgeId) override; 116 117 private: 118 void UpdateAnimator(const RefPtr<Animator>& animator, const RefPtr<JsiAnimatorBridge>& bridge, 119 shared_ptr<JsRuntime> runtime, const std::unordered_map<std::string, std::string>& params); 120 RefPtr<KeyframeAnimation<double>> CreateDoubleAnimation(double begin, double end, const RefPtr<Curve>& curve); 121 122 shared_ptr<JsRuntime> runtime_; 123 std::unordered_map<std::string, std::string> params_; 124 }; 125 126 } // namespace OHOS::Ace::Framework 127 128 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_JSI_ANIMATOR_BRIDGE_H 129