/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_RENDER_3D_BASE_OBJECT_JS_H
#define OHOS_RENDER_3D_BASE_OBJECT_JS_H
#include
#include
#include
#include
// tasks execute in the engine/render thread.
static constexpr BASE_NS::Uid ENGINE_THREAD { "2070e705-d061-40e4-bfb7-90fad2c280af" };
// tasks execute in the javascript mainthread.
static constexpr BASE_NS::Uid JS_THREAD { "b2e8cef3-453a-4651-b564-5190f8b5190d" };
class TrueRootObject {
public:
// Store a reference to a native IObject to the "JSBridge" object
// Optionally make the reference strong so that the lifetime is controlled by JS.
// for example. scene is kept strongly, and objects owned by scene are weak.
void SetNativeObject(META_NS::IObject::Ptr real, bool Strong);
META_NS::IObject::Ptr GetNativeObject();
virtual void* GetInstanceImpl(uint32_t id) = 0;
virtual void Finalize(napi_env env);
virtual void DisposeNative() = 0;
protected:
TrueRootObject();
virtual ~TrueRootObject() = default;
static void destroy(TrueRootObject* object)
{
delete object;
}
private:
META_NS::IObject::Ptr obj_;
META_NS::IObject::WeakPtr objW_;
};
template
class BaseObject : public TrueRootObject {
protected:
bool disposed_ { false };
virtual ~BaseObject() {};
BaseObject(napi_env env, napi_callback_info info) : TrueRootObject()
{
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
auto DTOR = [](napi_env env, void* nativeObject, void* finalize) {
TrueRootObject* ptr = static_cast(nativeObject);
ptr->Finalize(env);
TrueRootObject::destroy(ptr);
};
napi_wrap(env, thisVar, reinterpret_cast((TrueRootObject*)this), DTOR, nullptr, nullptr);
}
template
static inline napi_callback ctor()
{
napi_callback ctor = [](napi_env env, napi_callback_info info) -> napi_value {
napi_value thisVar = nullptr;
// fetch the "this" from javascript.
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
// The BaseObject constructor actually handles wrapping..
auto r = BASE_NS::make_unique