18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#ifndef OHOS_RENDER_3D_ANIMATION_JS_H
178bf80f4bSopenharmony_ci#define OHOS_RENDER_3D_ANIMATION_JS_H
188bf80f4bSopenharmony_ci
198bf80f4bSopenharmony_ci#include "BaseObjectJS.h"
208bf80f4bSopenharmony_ci#include "SceneResourceImpl.h"
218bf80f4bSopenharmony_ci
228bf80f4bSopenharmony_ciclass ThreadSafeCallback {
238bf80f4bSopenharmony_cipublic:
248bf80f4bSopenharmony_ci    // can be called from any thread.
258bf80f4bSopenharmony_ci    void Release()
268bf80f4bSopenharmony_ci    {
278bf80f4bSopenharmony_ci        napi_status status;
288bf80f4bSopenharmony_ci        if (termfun) {
298bf80f4bSopenharmony_ci            status =
308bf80f4bSopenharmony_ci                napi_release_threadsafe_function(termfun, napi_threadsafe_function_release_mode::napi_tsfn_release);
318bf80f4bSopenharmony_ci            termfun = nullptr;
328bf80f4bSopenharmony_ci        }
338bf80f4bSopenharmony_ci    }
348bf80f4bSopenharmony_ci    void Trigger()
358bf80f4bSopenharmony_ci    {
368bf80f4bSopenharmony_ci        napi_status status;
378bf80f4bSopenharmony_ci        if (termfun) {
388bf80f4bSopenharmony_ci            status =
398bf80f4bSopenharmony_ci                napi_call_threadsafe_function(termfun, nullptr, napi_threadsafe_function_call_mode::napi_tsfn_blocking);
408bf80f4bSopenharmony_ci        }
418bf80f4bSopenharmony_ci    }
428bf80f4bSopenharmony_ci
438bf80f4bSopenharmony_ciprotected:
448bf80f4bSopenharmony_ci    virtual void Finalize(napi_env) = 0;
458bf80f4bSopenharmony_ci    virtual void Invoked(napi_env) = 0;
468bf80f4bSopenharmony_ci    ThreadSafeCallback(napi_env env, const char* n)
478bf80f4bSopenharmony_ci    {
488bf80f4bSopenharmony_ci        napi_status status;
498bf80f4bSopenharmony_ci        napi_value name;
508bf80f4bSopenharmony_ci        napi_create_string_latin1(env, n, NAPI_AUTO_LENGTH, &name);
518bf80f4bSopenharmony_ci        status = napi_create_threadsafe_function(env, nullptr, nullptr, name, 1, 1, this, &ThreadSafeCallback::Final,
528bf80f4bSopenharmony_ci            this, &ThreadSafeCallback::Call, &termfun);
538bf80f4bSopenharmony_ci    }
548bf80f4bSopenharmony_ci    static void Call(napi_env env, napi_value js_callback, void* context, void* inData)
558bf80f4bSopenharmony_ci    {
568bf80f4bSopenharmony_ci        ThreadSafeCallback* data = (ThreadSafeCallback*)context;
578bf80f4bSopenharmony_ci        data->Invoked(env);
588bf80f4bSopenharmony_ci    }
598bf80f4bSopenharmony_ci    static void Final(napi_env env, void* finalize_data, void* finalize_hint)
608bf80f4bSopenharmony_ci    {
618bf80f4bSopenharmony_ci        ThreadSafeCallback* data = (ThreadSafeCallback*)finalize_data;
628bf80f4bSopenharmony_ci        data->Finalize(env);
638bf80f4bSopenharmony_ci        delete data;
648bf80f4bSopenharmony_ci    };
658bf80f4bSopenharmony_ci    virtual ~ThreadSafeCallback()
668bf80f4bSopenharmony_ci    {
678bf80f4bSopenharmony_ci        // Called by final on JS thread.
688bf80f4bSopenharmony_ci        Release();
698bf80f4bSopenharmony_ci    }
708bf80f4bSopenharmony_ci    napi_threadsafe_function termfun { nullptr };
718bf80f4bSopenharmony_ci};
728bf80f4bSopenharmony_ci
738bf80f4bSopenharmony_ciclass AnimationJS : public BaseObject<AnimationJS>, public SceneResourceImpl {
748bf80f4bSopenharmony_cipublic:
758bf80f4bSopenharmony_ci    static constexpr uint32_t ID = 70;
768bf80f4bSopenharmony_ci    static void Init(napi_env env, napi_value exports);
778bf80f4bSopenharmony_ci    AnimationJS(napi_env, napi_callback_info);
788bf80f4bSopenharmony_ci    ~AnimationJS() override;
798bf80f4bSopenharmony_ci    virtual void* GetInstanceImpl(uint32_t id) override;
808bf80f4bSopenharmony_ci
818bf80f4bSopenharmony_ciprivate:
828bf80f4bSopenharmony_ci    napi_value GetEnabled(NapiApi::FunctionContext<>& ctx);
838bf80f4bSopenharmony_ci    void SetEnabled(NapiApi::FunctionContext<bool>& ctx);
848bf80f4bSopenharmony_ci    napi_value GetDuration(NapiApi::FunctionContext<>& ctx);
858bf80f4bSopenharmony_ci    napi_value GetRunning(NapiApi::FunctionContext<>& ctx);
868bf80f4bSopenharmony_ci    napi_value GetProgress(NapiApi::FunctionContext<>& ctx);
878bf80f4bSopenharmony_ci
888bf80f4bSopenharmony_ci    napi_value OnFinished(NapiApi::FunctionContext<NapiApi::Function>& ctx);
898bf80f4bSopenharmony_ci    napi_value OnStarted(NapiApi::FunctionContext<NapiApi::Function>& ctx);
908bf80f4bSopenharmony_ci
918bf80f4bSopenharmony_ci    napi_value Pause(NapiApi::FunctionContext<>& ctx);
928bf80f4bSopenharmony_ci    napi_value Restart(NapiApi::FunctionContext<>& ctx);
938bf80f4bSopenharmony_ci    napi_value Seek(NapiApi::FunctionContext<float>& ctx);
948bf80f4bSopenharmony_ci
958bf80f4bSopenharmony_ci    napi_value Start(NapiApi::FunctionContext<>& ctx);
968bf80f4bSopenharmony_ci    napi_value Stop(NapiApi::FunctionContext<>& ctx);
978bf80f4bSopenharmony_ci    napi_value Finish(NapiApi::FunctionContext<>& ctx);
988bf80f4bSopenharmony_ci
998bf80f4bSopenharmony_ci    void DisposeNative() override;
1008bf80f4bSopenharmony_ci    void Finalize(napi_env env) override;
1018bf80f4bSopenharmony_ci
1028bf80f4bSopenharmony_ci    META_NS::IEvent::Token OnFinishedToken_ { 0 };
1038bf80f4bSopenharmony_ci    META_NS::IEvent::Token OnStartedToken_ { 0 };
1048bf80f4bSopenharmony_ci
1058bf80f4bSopenharmony_ci    // we don't actually own these two, as lifetime is controlled by napi_threadsafe_function
1068bf80f4bSopenharmony_ci    ThreadSafeCallback* OnStartedCB_ { nullptr };
1078bf80f4bSopenharmony_ci    ThreadSafeCallback* OnFinishedCB_ { nullptr };
1088bf80f4bSopenharmony_ci    NapiApi::WeakRef scene_;
1098bf80f4bSopenharmony_ci};
1108bf80f4bSopenharmony_ci#endif // OHOS_RENDER_3D_ANIMATION_JS_H
111