Skip to content

Commit

Permalink
feat(android): add layout module for reset layout cache (#4089)
Browse files Browse the repository at this point in the history
* feat(android): add layout module for reset layout cache

* fix(android): correct to YogaLayoutNode

---------

Co-authored-by: siguangli <[email protected]>
  • Loading branch information
siguangli and siguangli2018 authored Oct 23, 2024
1 parent e73c63c commit 78aff6f
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ if ("${LAYOUT_ENGINE}" STREQUAL "Yoga")
target_link_libraries(${PROJECT_NAME} PRIVATE yogacore)
elseif ("${LAYOUT_ENGINE}" STREQUAL "Taitank")
InfraPackage_Add(taitank
REMOTE "dom/third_party/taitank/1.0.5/git-repo.tgz"
REMOTE "dom/third_party/taitank/1.0.6/git-repo.tar.gz"
LOCAL "third_party/taitank"
)
target_link_libraries(${PROJECT_NAME} PRIVATE taitank)
Expand Down
2 changes: 1 addition & 1 deletion dom/include/dom/dom_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class DomNode : public std::enable_shared_from_this<DomNode> {
void UpdateLayoutStyleInfo(
const std::unordered_map<std::string, std::shared_ptr<footstone::value::HippyValue>>& style_update,
const std::vector<std::string>& style_delete);

void ResetLayoutCache();
/**
* this method should run in dom taskrunner
* */
Expand Down
5 changes: 5 additions & 0 deletions dom/include/dom/layout_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class LayoutNode {
virtual void SetLayoutStyles(
const std::unordered_map<std::string, std::shared_ptr<footstone::value::HippyValue>>& style_update,
const std::vector<std::string>& style_delete) = 0;

/**
* @brief 清除节点layout缓存
*/
virtual void ResetLayoutCache() = 0;
};

void InitLayoutConsts();
Expand Down
5 changes: 5 additions & 0 deletions dom/include/dom/taitank_layout_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class TaitankLayoutNode : public LayoutNode, public std::enable_shared_from_this
*/
void SetHasNewLayout(bool has_new_layout) override;

/**
* @brief 清除节点layout缓存
*/
void ResetLayoutCache() override;

/**
* @brief 节点标脏
*/
Expand Down
2 changes: 2 additions & 0 deletions dom/include/dom/yoga_layout_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class YogaLayoutNode : public LayoutNode, public std::enable_shared_from_this<Yo

void SetHasNewLayout(bool has_new_layout) override;

void ResetLayoutCache() override;

void MarkDirty() override;

bool HasParentEngineNode() override;
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 @@ -326,6 +326,10 @@ LayoutResult DomNode::GetLayoutInfoFromRoot() {
return result;
}

void DomNode::ResetLayoutCache() {
layout_node_->ResetLayoutCache();
}

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();
Expand Down
5 changes: 5 additions & 0 deletions dom/src/dom/taitank_layout_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ void TaitankLayoutNode::SetLayoutStyles(
Parser(style_update, style_delete);
}

void TaitankLayoutNode::ResetLayoutCache() {
FOOTSTONE_DLOG(INFO) << "do reset layout cache";
engine_node_->ResetLayoutRecursive();
}

void TaitankLayoutNode::Parser(
const std::unordered_map<std::string, std::shared_ptr<footstone::value::HippyValue>>& style_update,
const std::vector<std::string>& style_delete) {
Expand Down
4 changes: 4 additions & 0 deletions dom/src/dom/yoga_layout_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ void YogaLayoutNode::SetLayoutStyles(
Parser(style_update, style_delete);
}

void YogaLayoutNode::ResetLayoutCache() {
FOOTSTONE_DLOG(INFO) << "ResetLayoutCache not impl in yoga";
}

void YogaLayoutNode::SetWidth(float width) { YGNodeStyleSetWidth(yoga_node_, width); }

void YogaLayoutNode::SetHeight(float height) { YGNodeStyleSetHeight(yoga_node_, height); }
Expand Down
3 changes: 2 additions & 1 deletion driver/js/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ set(SOURCE_SET_STANDALONE
src/modules/performance/performance_paint_timing_module.cc
src/modules/performance/performance_resource_timing_module.cc
src/modules/timer_module.cc
src/modules/ui_manager_module.cc)
src/modules/ui_manager_module.cc
src/modules/ui_layout_module.cc)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_SET})
# The linker treats `.a` and `.o` files are different:
# * `.o` files from `.a` file only referenced `.o` object files are linked.
Expand Down
56 changes: 56 additions & 0 deletions driver/js/include/driver/modules/ui_layout_module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* 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.
*
*/

#pragma once

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

#include "driver/modules/module_base.h"
#include "driver/napi/callback_info.h"
#include "driver/napi/js_ctx.h"
#include "footstone/task.h"
#include "dom/dom_manager.h"
#include "dom/dom_node.h"

namespace hippy {
inline namespace driver {
inline namespace module {

class LayoutModule : public ModuleBase {
public:
using CallbackInfo = hippy::napi::CallbackInfo;
using CtxValue = hippy::napi::CtxValue;
using DomNode = hippy::dom::DomNode;

LayoutModule() = default;

void ResetLayoutCache(CallbackInfo& info, void* data);

virtual std::shared_ptr<CtxValue> BindFunction(std::shared_ptr<Scope> scope, std::shared_ptr<CtxValue> rest_args[]) override;
};

}
}
}
4 changes: 4 additions & 0 deletions driver/js/lib/global/UIManagerModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/* eslint-disable no-underscore-dangle */

const UIManagerModule = internalBinding('UIManagerModule');
const LayoutModule = internalBinding('LayoutModule');

Hippy.document = {
createNode() {
Expand All @@ -42,6 +43,9 @@ Hippy.document = {
callUIFunction(id, name, param, cb) {
UIManagerModule.CallUIFunction(id, name, param, cb);
},
ResetLayoutCache() {
LayoutModule.ResetLayoutCache();
},
sendRenderError(error) {
if (error) {
throw error;
Expand Down
81 changes: 81 additions & 0 deletions driver/js/src/modules/ui_layout_module.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* 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.
*
*/

#include "driver/modules/ui_layout_module.h"

#include <set>
#include <tuple>

#include "driver/modules/module_register.h"
#include "driver/base/js_convert_utils.h"
#include "dom/dom_argument.h"
#include "dom/dom_event.h"
#include "dom/dom_node.h"
#include "dom/node_props.h"
#include "footstone/task.h"

using HippyValue = footstone::value::HippyValue;
using DomArgument = hippy::dom::DomArgument;
using string_view = footstone::stringview::string_view;
using TaskRunner = footstone::runner::TaskRunner;

using Ctx = hippy::napi::Ctx;
using CtxValue = hippy::napi::CtxValue;
using CallbackInfo = hippy::napi::CallbackInfo;

namespace hippy {
inline namespace driver {
inline namespace module {

GEN_INVOKE_CB(LayoutModule, ResetLayoutCache)

void LayoutModule::ResetLayoutCache(CallbackInfo& info, void* data) {
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
if (!scope) {
return;
}
auto root_node = scope->GetRootNode().lock();
if (root_node != nullptr) {
root_node->ResetLayoutCache();
}
}

std::shared_ptr<CtxValue> LayoutModule::BindFunction(std::shared_ptr<Scope> scope,
std::shared_ptr<CtxValue> rest_args[]) {
auto context = scope->GetContext();
auto object = context->CreateObject();

auto key = context->CreateString("ResetLayoutCache");
auto wrapper = std::make_unique<hippy::napi::FunctionWrapper>(InvokeLayoutModuleResetLayoutCache, nullptr);
auto value = context->CreateFunction(wrapper);
scope->SaveFunctionWrapper(std::move(wrapper));
context->SetProperty(object, key, value);

return object;
}

}
}
}

2 changes: 2 additions & 0 deletions driver/js/src/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "driver/modules/scene_builder_module.h"
#include "driver/modules/timer_module.h"
#include "driver/modules/ui_manager_module.h"
#include "driver/modules/ui_layout_module.h"
#include "driver/vm/native_source_code.h"
#include "footstone/logging.h"
#include "footstone/string_view_utils.h"
Expand Down Expand Up @@ -204,6 +205,7 @@ void Scope::BindModule() {
#ifdef JS_V8
module_object_map_["MemoryModule"] = std::make_shared<MemoryModule>();
#endif
module_object_map_["LayoutModule"] = std::make_shared<LayoutModule>();
}

void Scope::Bootstrap() {
Expand Down
4 changes: 2 additions & 2 deletions driver/js/src/vm/jsc/native_source_code_ios.cc

Large diffs are not rendered by default.

Loading

0 comments on commit 78aff6f

Please sign in to comment.