Skip to content

Commit

Permalink
feat(ohos): support double layout engines for vl
Browse files Browse the repository at this point in the history
  • Loading branch information
sohotz committed Jan 21, 2025
1 parent 9c6d20b commit 5ee6c8a
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 30 deletions.
28 changes: 21 additions & 7 deletions dom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ if (NOT DEFINED LAYOUT_ENGINE)
endif ()

if ("${LAYOUT_ENGINE}" STREQUAL "Yoga")
set(USE_YOGA 1)
target_compile_definitions(${PROJECT_NAME} PRIVATE "LAYOUT_ENGINE_YOGA")
elseif ("${LAYOUT_ENGINE}" STREQUAL "Taitank")
set(USE_TAITANK 1)
target_compile_definitions(${PROJECT_NAME} PRIVATE "LAYOUT_ENGINE_TAITANK")
elseif ("${LAYOUT_ENGINE}" STREQUAL "YogaAndTaitank")
set(USE_YOGA 1)
set(USE_TAITANK 1)
target_compile_definitions(${PROJECT_NAME} PRIVATE "LAYOUT_ENGINE_YOGA_AND_TAITANK")
else ()
message(FATAL_ERROR "Layout Engine ${LAYOUT_ENGINE} is not supported")
endif ()
# endregion

if (USE_YOGA STREQUAL 1)
if (ANDROID)
InfraPackage_Add(yoga
REMOTE "dom/third_party/yoga/1.19.0/git-repo.tgz"
Expand All @@ -63,18 +78,16 @@ if ("${LAYOUT_ENGINE}" STREQUAL "Yoga")
LOCAL "third_party/yoga"
)
endif ()
target_compile_definitions(${PROJECT_NAME} PRIVATE "USE_YOGA")
target_link_libraries(${PROJECT_NAME} PRIVATE yogacore)
elseif ("${LAYOUT_ENGINE}" STREQUAL "Taitank")
endif ()

if (USE_TAITANK STREQUAL 1)
InfraPackage_Add(taitank
REMOTE "dom/third_party/taitank/1.0.6/git-repo.tar.gz"
LOCAL "third_party/taitank"
)
target_link_libraries(${PROJECT_NAME} PRIVATE taitank)
else ()
message(FATAL_ERROR "Layout Engine ${LAYOUT_ENGINE} is not supported")
endif ()
# endregion

# region source set
set(SOURCE_SET
Expand All @@ -95,9 +108,10 @@ set(SOURCE_SET
src/dom/root_node.cc
src/dom/scene.cc
src/dom/scene_builder.cc)
if (${LAYOUT_ENGINE} STREQUAL "Yoga")
if (USE_YOGA STREQUAL 1)
list(APPEND SOURCE_SET src/dom/yoga_layout_node.cc)
elseif (${LAYOUT_ENGINE} STREQUAL "Taitank")
endif ()
if (USE_TAITANK STREQUAL 1)
list(APPEND SOURCE_SET src/dom/taitank_layout_node.cc)
endif ()
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_SET})
Expand Down
4 changes: 2 additions & 2 deletions dom/include/dom/dom_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ class DomNode : public std::enable_shared_from_this<DomNode> {
DomNode(uint32_t id, uint32_t pid, int32_t index, std::string tag_name, std::string view_name,
std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<HippyValue>>> style_map,
std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<HippyValue>>> dom_ext_map,
std::weak_ptr<RootNode> weak_root_node);
std::weak_ptr<RootNode> weak_root_node, LayoutEngineType layout_engine_type);

DomNode(uint32_t id, uint32_t pid, std::weak_ptr<RootNode> weak_root_node);
DomNode(uint32_t id, uint32_t pid, std::weak_ptr<RootNode> weak_root_node, LayoutEngineType layout_engine_type);
DomNode();
virtual ~DomNode();

Expand Down
10 changes: 8 additions & 2 deletions dom/include/dom/layout_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ class LayoutNode {
virtual void ResetLayoutCache() = 0;
};

void InitLayoutConsts();
std::shared_ptr<LayoutNode> CreateLayoutNode();
enum LayoutEngineType {
LayoutEngineDefault = 0,
LayoutEngineTaitank,
LayoutEngineYoga
};

void InitLayoutConsts(LayoutEngineType type);
std::shared_ptr<LayoutNode> CreateLayoutNode(LayoutEngineType type);

} // namespace dom
} // namespace hippy
6 changes: 5 additions & 1 deletion dom/include/dom/root_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RootNode : public DomNode {
using EventCallback = std::function<void(const std::shared_ptr<DomEvent>&)>;
using EventCallBackRunner = std::function<void(const std::shared_ptr<DomEvent>&)>;

RootNode(uint32_t id);
RootNode(uint32_t id, LayoutEngineType layout_engine_type = LayoutEngineDefault);
RootNode();

inline std::weak_ptr<DomManager> GetDomManager() { return dom_manager_; }
Expand Down Expand Up @@ -117,6 +117,8 @@ class RootNode : public DomNode {
}

std::vector<std::weak_ptr<DomNode>> GetAllTextNodes();

LayoutEngineType GetLayoutEngineType() { return layout_engine_type_; }

private:
static void MarkLayoutNodeDirty(const std::vector<std::shared_ptr<DomNode>>& nodes);
Expand Down Expand Up @@ -149,6 +151,8 @@ class RootNode : public DomNode {
std::unique_ptr<DomNodeStyleDiffer> style_differ_;

bool disable_set_root_size_ { false };

LayoutEngineType layout_engine_type_ = LayoutEngineDefault;

static footstone::utils::PersistentObjectMap<uint32_t, std::shared_ptr<RootNode>> persistent_map_;
};
Expand Down
4 changes: 4 additions & 0 deletions dom/include/dom/taitank_layout_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,9 @@ class TaitankLayoutNode : public LayoutNode, public std::enable_shared_from_this
MeasureFunction measure_function_ = nullptr;
};


void InitLayoutConstsTaitank();
std::shared_ptr<LayoutNode> CreateLayoutNodeTaitank();

} // namespace dom
} // namespace hippy
3 changes: 3 additions & 0 deletions dom/include/dom/yoga_layout_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,8 @@ class YogaLayoutNode : public LayoutNode, public std::enable_shared_from_this<Yo
int64_t key_;
};

void InitLayoutConstsYoga();
std::shared_ptr<LayoutNode> CreateLayoutNodeYoga();

} // namespace dom
} // namespace hippy
10 changes: 5 additions & 5 deletions dom/src/dom/dom_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ using HippyValueObjectType = footstone::value::HippyValue::HippyValueObjectType;
DomNode::DomNode(uint32_t id, uint32_t pid, int32_t index, std::string tag_name, std::string view_name,
std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<HippyValue>>> style_map,
std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<HippyValue>>> dom_ext_map,
std::weak_ptr<RootNode> weak_root_node)
std::weak_ptr<RootNode> weak_root_node, LayoutEngineType layout_engine_type)
: id_(id),
pid_(pid),
index_(index),
Expand All @@ -76,13 +76,13 @@ DomNode::DomNode(uint32_t id, uint32_t pid, int32_t index, std::string tag_name,
current_callback_id_(0),
func_cb_map_(nullptr),
event_listener_map_(nullptr) {
layout_node_ = hippy::dom::CreateLayoutNode();
layout_node_ = hippy::dom::CreateLayoutNode(layout_engine_type);
}

DomNode::DomNode(uint32_t id, uint32_t pid, std::weak_ptr<RootNode> weak_root_node)
: DomNode(id, pid, 0, "", "", nullptr, nullptr, std::move(weak_root_node)) {}
DomNode::DomNode(uint32_t id, uint32_t pid, std::weak_ptr<RootNode> weak_root_node, LayoutEngineType layout_engine_type)
: DomNode(id, pid, 0, "", "", nullptr, nullptr, std::move(weak_root_node), layout_engine_type) {}

DomNode::DomNode() : DomNode(0, 0, {}) {}
DomNode::DomNode() : DomNode(0, 0, {}, LayoutEngineDefault) {}

DomNode::~DomNode() = default;

Expand Down
36 changes: 36 additions & 0 deletions dom/src/dom/layout_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
*/

#include "dom/layout_node.h"
#if defined(LAYOUT_ENGINE_YOGA)
#include "dom/yoga_layout_node.h"
#elif defined(LAYOUT_ENGINE_TAITANK)
#include "dom/taitank_layout_node.h"
#elif defined(LAYOUT_ENGINE_YOGA_AND_TAITANK)
#include "dom/yoga_layout_node.h"
#include "dom/taitank_layout_node.h"
#endif

namespace hippy {
inline namespace dom {
Expand All @@ -27,5 +35,33 @@ LayoutNode::LayoutNode() = default;

LayoutNode::~LayoutNode() = default;

void InitLayoutConsts(LayoutEngineType type) {
#if defined(LAYOUT_ENGINE_YOGA)
InitLayoutConstsYoga();
#elif defined(LAYOUT_ENGINE_TAITANK)
InitLayoutConstsTaitank();
#elif defined(LAYOUT_ENGINE_YOGA_AND_TAITANK)
if (type == LayoutEngineYoga) {
InitLayoutConstsYoga();
} else {
InitLayoutConstsTaitank();
}
#endif
}

std::shared_ptr<LayoutNode> CreateLayoutNode(LayoutEngineType type) {
#if defined(LAYOUT_ENGINE_YOGA)
return CreateLayoutNodeYoga();
#elif defined(LAYOUT_ENGINE_TAITANK)
return CreateLayoutNodeTaitank();
#elif defined(LAYOUT_ENGINE_YOGA_AND_TAITANK)
if (type == LayoutEngineYoga) {
return CreateLayoutNodeYoga();
} else {
return CreateLayoutNodeTaitank();
}
#endif
}

} // namespace dom
} // namespace hippy
5 changes: 3 additions & 2 deletions dom/src/dom/root_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ bool DomNodeStyleDiffer::Calculate(const std::shared_ptr<hippy::dom::RootNode>&
return true;
}

RootNode::RootNode(uint32_t id) : DomNode(id, 0, 0, "", "", nullptr, nullptr, {}) {
InitLayoutConsts();
RootNode::RootNode(uint32_t id, LayoutEngineType layout_engine_type) : DomNode(id, 0, 0, "", "", nullptr, nullptr, {}, layout_engine_type) {
layout_engine_type_ = layout_engine_type;
InitLayoutConsts(layout_engine_type);
SetRenderInfo({id, 0, 0});
animation_manager_ = std::make_shared<AnimationManager>();
interceptors_.push_back(animation_manager_);
Expand Down
4 changes: 2 additions & 2 deletions dom/src/dom/taitank_layout_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,13 @@ void TaitankLayoutNode::Deallocate() {
engine_node_ = nullptr;
}

void InitLayoutConsts() {
void InitLayoutConstsTaitank() {
if (global_layout_consts == nullptr) {
global_layout_consts = new TaitankLayoutConsts();
}
}

std::shared_ptr<LayoutNode> CreateLayoutNode() { return std::make_shared<TaitankLayoutNode>(); }
std::shared_ptr<LayoutNode> CreateLayoutNodeTaitank() { return std::make_shared<TaitankLayoutNode>(); }

} // namespace dom
} // namespace hippy
4 changes: 2 additions & 2 deletions dom/src/dom/yoga_layout_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,8 @@ void YogaLayoutNode::Deallocate() {
YGConfigFree(yoga_config_);
}

void InitLayoutConsts() {}
std::shared_ptr<LayoutNode> CreateLayoutNode() { return std::make_shared<YogaLayoutNode>(); }
void InitLayoutConstsYoga() {}
std::shared_ptr<LayoutNode> CreateLayoutNodeYoga() { return std::make_shared<YogaLayoutNode>(); }

} // namespace dom
} // namespace hippy
15 changes: 12 additions & 3 deletions driver/js/src/modules/scene_builder_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,17 @@ CreateNode(const std::shared_ptr<Ctx> &context,
auto ext = std::make_shared<std::unordered_map<std::string, std::shared_ptr<HippyValue>>>(
std::move(std::get<3>(props_tuple)));
FOOTSTONE_CHECK(!scope->GetDomManager().expired());
auto root_node = scope->GetRootNode().lock();
LayoutEngineType layout_type = root_node ? root_node->GetLayoutEngineType() : LayoutEngineDefault;
dom_node = std::make_shared<DomNode>(std::get<2>(id_tuple),
std::get<2>(pid_tuple),
0,
std::move(u8_tag_name),
std::move(u8_view_name),
style,
ext,
scope->GetRootNode());
scope->GetRootNode(),
layout_type);
return std::make_tuple(true, "", dom_node);
}

Expand Down Expand Up @@ -496,11 +499,14 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
if (length >= 2) {
auto ref_info_tuple = CreateRefInfo(
context, context->CopyArrayElement(info, 1), scope);
auto root_node = scope->GetRootNode().lock();
LayoutEngineType layout_type = root_node ? root_node->GetLayoutEngineType() : LayoutEngineDefault;
dom_infos.push_back(std::make_shared<DomInfo>(
std::make_shared<DomNode>(
std::get<2>(id_tuple),
std::get<2>(pid_tuple),
scope->GetRootNode()),
scope->GetRootNode(),
layout_type),
std::get<2>(ref_info_tuple),
nullptr));
}
Expand Down Expand Up @@ -543,11 +549,14 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea

return nullptr;
}
auto root_node = scope->GetRootNode().lock();
LayoutEngineType layout_type = root_node ? root_node->GetLayoutEngineType() : LayoutEngineDefault;
dom_infos.push_back(std::make_shared<DomInfo>(
std::make_shared<DomNode>(
std::get<2>(id_tuple),
std::get<2>(pid_tuple),
scope->GetRootNode()),
scope->GetRootNode(),
layout_type),
nullptr, nullptr));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void BizViewManager::BuildBizViews() {

// 参数说明:
// first_dom_manager_id - 实际由业务从ts层传到c层,ts层获取:hippyEngine.getHippyEngineContext()?.getDomManagerId()
uint32_t rootId = HippyViewProvider_CreateRoot(DEMO_FIRST_DOM_MANAGER_ID);
uint32_t rootId = HippyViewProvider_CreateRoot(DEMO_FIRST_DOM_MANAGER_ID, HippyLayoutEngineYoga);

// 参数说明:
// parent_node_handle - Hippy根节点的父节点
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
#define HIPPY_EXTERN extern __attribute__((visibility("default")))
#endif

HIPPY_EXTERN uint32_t HippyViewProvider_CreateRoot(uint32_t first_dom_manager_id);
typedef enum HippyLayoutEngineType {
HippyLayoutEngineDefault = 0,
HippyLayoutEngineTaitank,
HippyLayoutEngineYoga
} HippyLayoutEngineType;

HIPPY_EXTERN uint32_t HippyViewProvider_CreateRoot(uint32_t first_dom_manager_id, HippyLayoutEngineType layout_engine_type = HippyLayoutEngineDefault);
HIPPY_EXTERN void HippyViewProvider_DestroyRoot(uint32_t render_manager_id, uint32_t root_id);
HIPPY_EXTERN void HippyViewProvider_BindNativeRoot(void *parent_node_handle, uint32_t render_manager_id, uint32_t root_id);
HIPPY_EXTERN void HippyViewProvider_UnbindNativeRoot(uint32_t render_manager_id, uint32_t root_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,28 @@ static uint32_t sHippyRootIdCounter = 0;

using namespace hippy;

uint32_t HippyViewProvider_CreateRoot(uint32_t first_dom_manager_id) {
static hippy::LayoutEngineType HippyLayoutEngineTypeToInnerType(HippyLayoutEngineType layout_engine_type) {
switch (layout_engine_type) {
case HippyLayoutEngineTaitank:
return hippy::LayoutEngineTaitank;
case HippyLayoutEngineYoga:
return hippy::LayoutEngineYoga;
case HippyLayoutEngineDefault:
return hippy::LayoutEngineDefault;
}
return hippy::LayoutEngineDefault;
}

uint32_t HippyViewProvider_CreateRoot(uint32_t first_dom_manager_id, HippyLayoutEngineType layout_engine_type) {
sHippyRootIdCounter += ROOT_VIEW_ID_INCREMENT;
uint32_t root_id = sHippyRootIdCounter;
double density = HRPixelUtils::GetDensity();
hippy::LayoutEngineType layout_type = HippyLayoutEngineTypeToInnerType(layout_engine_type);

std::shared_ptr<hippy::RootNode> saved_root_node;
auto& persistent_map = RootNode::PersistentMap();
if (!persistent_map.Find(root_id, saved_root_node)) {
auto root_node = std::make_shared<hippy::RootNode>(root_id);
auto root_node = std::make_shared<hippy::RootNode>(root_id, layout_type);
auto layout = root_node->GetLayoutNode();
layout->SetScaleFactor(static_cast<float>(density));
auto flag = persistent_map.Insert(root_id, root_node);
Expand Down

0 comments on commit 5ee6c8a

Please sign in to comment.