Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into frame_refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Oct 18, 2023
2 parents fa691cf + e383127 commit 9a7136b
Show file tree
Hide file tree
Showing 21 changed files with 258 additions and 99 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Hippy Group](https://img.shields.io/badge/group-Hippy-blue.svg) [![license](https://img.shields.io/badge/license-Apache%202-blue)](https://github.com/Tencent/Hippy/blob/master/LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/Hippy/pulls) ![node](https://img.shields.io/badge/node-%3E%3D10.0.0-green.svg) [![Actions Status](https://github.com/Tencent/Hippy/workflows/build/badge.svg?branch=master)](https://github.com/Tencent/Hippy/actions) [![Codecov](https://img.shields.io/codecov/c/github/Tencent/Hippy)](https://codecov.io/gh/Tencent/Hippy) [![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/Tencent/Hippy)](https://github.com/Tencent/Hippy/releases)

[Homepage](//tencent.github.io/Hippy/)
[Homepage](https://openhippy.com)

## 💡 Introduction

Expand Down
4 changes: 4 additions & 0 deletions dom/src/dom/dom_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,13 @@ LayoutResult DomNode::GetLayoutInfoFromRoot() {
void DomNode::TransferLayoutOutputsRecursive(std::vector<std::shared_ptr<DomNode>>& changed_nodes) {
auto not_equal = std::not_equal_to<>();
bool changed = layout_node_->IsDirty() || layout_node_->HasNewLayout();
#ifdef __ANDROID__
bool trigger_layout_event = true;
#else
bool trigger_layout_event =
not_equal(layout_.left, layout_node_->GetLeft()) || not_equal(layout_.top, layout_node_->GetTop()) ||
not_equal(layout_.width, layout_node_->GetWidth()) || not_equal(layout_.height, layout_node_->GetHeight());
#endif

layout_.left = layout_node_->GetLeft();
layout_.top = layout_node_->GetTop();
Expand Down
13 changes: 13 additions & 0 deletions dom/src/dom/root_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ void RootNode::RemoveEventListener(const std::string& name, uint64_t listener_id
void RootNode::ReleaseResources() {}

void RootNode::CreateDomNodes(std::vector<std::shared_ptr<DomInfo>>&& nodes) {
HP_PERF_LOG("CreateDomNodes Begin");

for (const auto& interceptor : interceptors_) {
interceptor->OnDomNodeCreate(nodes);
}
HP_PERF_LOG("CreateDomNodes Interceptor callback done");
std::vector<std::shared_ptr<DomNode>> nodes_to_create;
for (const auto& node_info : nodes) {
auto& node = node_info->dom_node;
Expand All @@ -85,6 +88,7 @@ void RootNode::CreateDomNodes(std::vector<std::shared_ptr<DomInfo>>&& nodes) {
node->HandleEvent(event);
OnDomNodeCreated(node);
}
HP_PERF_LOG("CreateDomNodes Nodes created and finished callback");
for (const auto& node : nodes_to_create) {
node->SetRenderInfo({node->GetId(), node->GetPid(), node->GetSelfIndex()});
}
Expand All @@ -94,6 +98,7 @@ void RootNode::CreateDomNodes(std::vector<std::shared_ptr<DomInfo>>&& nodes) {
if (!nodes_to_create.empty()) {
dom_operations_.push_back({DomOperation::Op::kOpCreate, nodes_to_create});
}
HP_PERF_LOG("CreateDomNodes End");
}

void RootNode::UpdateDomNodes(std::vector<std::shared_ptr<DomInfo>>&& nodes) {
Expand Down Expand Up @@ -272,14 +277,21 @@ void RootNode::CallFunction(uint32_t id, const std::string& name, const DomArgum
}

void RootNode::SyncWithRenderManager(const std::shared_ptr<RenderManager>& render_manager) {
HP_PERF_LOG("RootNode::SyncWithRenderManager");
unsigned long cnt = dom_operations_.size();
FlushDomOperations(render_manager);
HP_PERF_LOG("RootNode::FlushDomOperations Done, dom op count:%lld", cnt);
cnt = event_operations_.size();
FlushEventOperations(render_manager);
HP_PERF_LOG("RootNode::FlushEventOperations Done, event op count:%d",cnt);
DoAndFlushLayout(render_manager);
HP_PERF_LOG("RootNode::DoAndFlushLayout Done");
auto dom_manager = dom_manager_.lock();
if (dom_manager) {
dom_manager->RecordDomEndTimePoint();
}
render_manager->EndBatch(GetWeakSelf());
HP_PERF_LOG("RootNode::SyncWithRenderManager End");
}

void RootNode::AddEvent(uint32_t id, const std::string& event_name) {
Expand Down Expand Up @@ -422,6 +434,7 @@ void RootNode::DoAndFlushLayout(const std::shared_ptr<RenderManager>& render_man

void RootNode::FlushDomOperations(const std::shared_ptr<RenderManager>& render_manager) {
for (auto& dom_operation : dom_operations_) {
HP_PERF_LOG("RootNode::FlushDomOperations dom_operation.op cnt:%lld", dom_operation.nodes.size());
MarkLayoutNodeDirty(dom_operation.nodes);
switch (dom_operation.op) {
case DomOperation::Op::kOpCreate:
Expand Down
4 changes: 2 additions & 2 deletions driver/js/include/driver/js_driver_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class JsDriverUtils {
const string_view& global_config,
std::function<void(std::shared_ptr<Scope>)>&& scope_initialized_callback,
const JsCallback& call_host_callback);
static void DestroyInstance(const std::shared_ptr<Engine>& engine,
const std::shared_ptr<Scope>& scope,
static void DestroyInstance(std::shared_ptr<Engine>&& engine,
std::shared_ptr<Scope>&& scope,
const std::function<void(bool)>& callback,
bool is_reload);
static bool RunScript(const std::shared_ptr<Scope>& scope,
Expand Down
49 changes: 29 additions & 20 deletions driver/js/src/js_driver_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -379,28 +379,10 @@ bool JsDriverUtils::RunScript(const std::shared_ptr<Scope>& scope,
return flag;
}

void JsDriverUtils::DestroyInstance(const std::shared_ptr<Engine>& engine,
const std::shared_ptr<Scope>& scope,
void JsDriverUtils::DestroyInstance(std::shared_ptr<Engine>&& engine,
std::shared_ptr<Scope>&& scope,
const std::function<void(bool)>& callback,
bool is_reload) {
auto scope_destroy_callback = [engine, scope, is_reload, callback] {
#if defined(JS_V8) && defined(ENABLE_INSPECTOR) && !defined(V8_WITHOUT_INSPECTOR)
auto v8_vm = std::static_pointer_cast<V8VM>(engine->GetVM());
if (v8_vm->IsDebug()) {
auto inspector_client = v8_vm->GetInspectorClient();
if (inspector_client) {
auto inspector_context = scope->GetInspectorContext();
inspector_client->DestroyInspectorContext(is_reload, inspector_context);
}
} else {
scope->WillExit();
}
#else
scope->WillExit();
#endif
FOOTSTONE_LOG(INFO) << "js destroy end";
callback(true);
};
auto vm = engine->GetVM();
auto group = vm->GetGroupId();
if (vm->IsDebug()) {
Expand All @@ -423,6 +405,33 @@ void JsDriverUtils::DestroyInstance(const std::shared_ptr<Engine>& engine,
}
}
auto runner = engine->GetJsTaskRunner();

// DestroyInstance is called in the bridge thread, while the
// scope_destroy_callback is pushed into the task_runner to run in the DOM
// thread. When DestroyInstance is called, it holds the scope and engine.
// However, executing scope_destroy_callback may add tasks (e.g., setTimeout)
// holding the scope, even when the engine is destroyed. To prevent this, move
// the scope and engine into the scope_destroy_callback, which will be released
// when the callback is executed, ensuring no other tasks can be added to the
// task runner.
auto scope_destroy_callback = [engine = std::move(engine), scope = std::move(scope), is_reload, callback] {
#if defined(JS_V8) && defined(ENABLE_INSPECTOR) && !defined(V8_WITHOUT_INSPECTOR)
auto v8_vm = std::static_pointer_cast<V8VM>(engine->GetVM());
if (v8_vm->IsDebug()) {
auto inspector_client = v8_vm->GetInspectorClient();
if (inspector_client) {
auto inspector_context = scope->GetInspectorContext();
inspector_client->DestroyInspectorContext(is_reload, inspector_context);
}
} else {
scope->WillExit();
}
#else
scope->WillExit();
#endif
FOOTSTONE_LOG(INFO) << "js destroy end";
callback(true);
};
runner->PostTask(std::move(scope_destroy_callback));
FOOTSTONE_DLOG(INFO) << "destroy, group = " << group;
}
Expand Down
33 changes: 32 additions & 1 deletion driver/js/src/modules/scene_builder_module.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
Expand Down Expand Up @@ -27,6 +27,7 @@
#include "driver/modules/scene_builder_module.h"
#include "driver/modules/ui_manager_module.h"
#include "driver/scope.h"
#include "footstone/logging.h"
#include "footstone/string_view.h"
#include "footstone/string_view_utils.h"

Expand Down Expand Up @@ -419,12 +420,15 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[],
std::shared_ptr<CtxValue>&) -> std::shared_ptr<CtxValue> {
HP_PERF_LOG("SceneBuilder.create()");
auto scope = weak_scope.lock();
if (!scope) {
HP_PERF_LOG("SceneBuilder.create() exit with error");
return nullptr;
}
auto ret = HandleJsValue(scope->GetContext(), arguments[0], scope);
SceneBuilder::Create(scope->GetDomManager(), scope->GetRootNode(), std::move(std::get<2>(ret)));
HP_PERF_LOG("SceneBuilder.create() End");
return nullptr;
};
class_template.functions.emplace_back(std::move(create_func_def));
Expand All @@ -436,12 +440,16 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[],
std::shared_ptr<CtxValue>&) -> std::shared_ptr<CtxValue> {
HP_PERF_LOG("SceneBuilder.update()");
auto scope = weak_scope.lock();
if (!scope) {
HP_PERF_LOG("SceneBuilder.update() exit with error");
return nullptr;
}
auto ret = HandleJsValue(scope->GetContext(), arguments[0], scope);
SceneBuilder::Update(scope->GetDomManager(), scope->GetRootNode(), std::move(std::get<2>(ret)));
HP_PERF_LOG("SceneBuilder.update() End");

return nullptr;
};
class_template.functions.emplace_back(std::move(update_func_def));
Expand All @@ -452,8 +460,11 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
const std::shared_ptr<CtxValue> arguments[],
std::shared_ptr<CtxValue>&)
-> std::shared_ptr<CtxValue> {
HP_PERF_LOG("SceneBuilder.move()");

auto scope = weak_scope.lock();
if (!scope) {
HP_PERF_LOG("SceneBuilder.move() exit with error");
return nullptr;
}
auto weak_dom_manager = scope->GetDomManager();
Expand All @@ -469,11 +480,13 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
auto node = context->CopyArrayElement(info, 0);
auto id_tuple = GetNodeId(context, node);
if (!std::get<0>(id_tuple)) {
HP_PERF_LOG("SceneBuilder.move() exit with error");
return nullptr;
}

auto pid_tuple = GetNodePid(context, node);
if (!std::get<0>(pid_tuple)) {
HP_PERF_LOG("SceneBuilder.move() exit with error");
return nullptr;
}
if (length >= 2) {
Expand All @@ -489,6 +502,7 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
}
}
SceneBuilder::Move(weak_dom_manager, scope->GetRootNode(), std::move(dom_infos));
HP_PERF_LOG("SceneBuilder.move() End");
return nullptr;
};
class_template.functions.emplace_back(std::move(move_func_def));
Expand All @@ -500,8 +514,11 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[],
std::shared_ptr<CtxValue>&) -> std::shared_ptr<CtxValue> {
HP_PERF_LOG("SceneBuilder.delete()");

auto scope = weak_scope.lock();
if (!scope) {
HP_PERF_LOG("SceneBuilder.delete() exit with error");
return nullptr;
}
auto nodes = arguments[0];
Expand All @@ -521,6 +538,8 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea

auto pid_tuple = GetNodePid(context, node);
if (!std::get<0>(pid_tuple)) {
HP_PERF_LOG("SceneBuilder.delete() exit with error");

return nullptr;
}
dom_infos.push_back(std::make_shared<DomInfo>(
Expand All @@ -532,6 +551,8 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
}
}
SceneBuilder::Delete(scope->GetDomManager(), scope->GetRootNode(), std::move(dom_infos));
HP_PERF_LOG("SceneBuilder.delete() End");

return nullptr;
};
class_template.functions.emplace_back(std::move(delete_func_def));
Expand All @@ -543,14 +564,17 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[],
std::shared_ptr<CtxValue>&) -> std::shared_ptr<CtxValue> {
HP_PERF_LOG("SceneBuilder.addEventListener()");
auto scope = weak_scope.lock();
if (!scope) {
HP_PERF_LOG("SceneBuilder.addEventListener() exit with error");
return nullptr;
}
Scope::EventListenerInfo listener_info;
HandleEventListenerInfo(scope->GetContext(), argument_count, arguments, listener_info);
auto dom_listener_info = scope->AddListener(listener_info);
SceneBuilder::AddEventListener(scope->GetDomManager(), scope->GetRootNode(), dom_listener_info);
HP_PERF_LOG("SceneBuilder.addEventListener() End");
return nullptr;
};
class_template.functions.emplace_back(std::move(add_event_listener_def));
Expand All @@ -562,14 +586,18 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[],
std::shared_ptr<CtxValue>&) -> std::shared_ptr<CtxValue> {
HP_PERF_LOG("SceneBuilder.removeEventListener()");

auto scope = weak_scope.lock();
if (!scope) {
HP_PERF_LOG("SceneBuilder.removeEventListener() exit with error");
return nullptr;
}
Scope::EventListenerInfo listener_info;
HandleEventListenerInfo(scope->GetContext(), argument_count, arguments, listener_info);
auto dom_listener_info = scope->RemoveListener(listener_info);
SceneBuilder::RemoveEventListener(scope->GetDomManager(), scope->GetRootNode(), dom_listener_info);
HP_PERF_LOG("SceneBuilder.removeEventListener() End");
return nullptr;
};
class_template.functions.emplace_back(std::move(remove_event_listener_def));
Expand All @@ -582,11 +610,14 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[],
std::shared_ptr<CtxValue>&) -> std::shared_ptr<CtxValue> {
HP_PERF_LOG("SceneBuilder.build()");
auto scope = weak_scope.lock();
if (!scope) {
HP_PERF_LOG("SceneBuilder.build() exit with error");
return nullptr;
}
SceneBuilder::Build(scope->GetDomManager(), scope->GetRootNode());
HP_PERF_LOG("SceneBuilder.build() End");
return nullptr;
};
class_template.functions.emplace_back(std::move(build_func_def));
Expand Down
Loading

0 comments on commit 9a7136b

Please sign in to comment.