Skip to content

Commit

Permalink
feat(android): add filter for css style to native renderer (#3405)
Browse files Browse the repository at this point in the history
* feat(android): add filter for css style to native renderer

* feat(renderer): android native renderer support style filter

---------

Co-authored-by: maxli <maxli@tencent.com>
Co-authored-by: ilikethese <shenli0124@qq.com>
  • Loading branch information
3 people authored and zealotchen0 committed Jul 25, 2023
1 parent e3351e6 commit c459934
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#pragma once

#include <jni.h>

#include <unordered_set>
#include <memory>

#include "jni/scoped_java_ref.h"
Expand Down Expand Up @@ -65,6 +65,9 @@ bool CreateJavaRenderManager(uint32_t id, std::shared_ptr<JavaRef>&j_render_mana

float GetDensity(std::shared_ptr<JavaRef>&j_render_manager);

void GetPropsRegisterForRender(const std::shared_ptr<JavaRef>& j_render_manager,
std::unordered_set<std::string>& style_set);

} // namespace native
} // namespace render
} // namespace hippy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <atomic>
#include <memory>
#include <unordered_set>

#include "dom/dom_node.h"
#include "dom/render_manager.h"
Expand All @@ -36,6 +37,23 @@ namespace hippy {
inline namespace render {
inline namespace native {

class StyleFilter {
public:
StyleFilter(const std::shared_ptr<JavaRef>& j_render_manager);
~StyleFilter() = default;
StyleFilter(const StyleFilter&) = delete;
StyleFilter(StyleFilter&&) = delete;
StyleFilter& operator=(const StyleFilter&) = delete;
StyleFilter& operator=(StyleFilter&&) = delete;

bool Enable(const std::string& style) {
return styles_.find(style) != styles_.end();
}

private:
std::unordered_set<std::string> styles_;
};

class NativeRenderManager : public RenderManager, public std::enable_shared_from_this<NativeRenderManager> {
public:
NativeRenderManager();
Expand Down Expand Up @@ -83,6 +101,11 @@ class NativeRenderManager : public RenderManager, public std::enable_shared_from
return persistent_map_;
}

static std::shared_ptr<StyleFilter> GetStyleFilter(const std::shared_ptr<JavaRef>& j_render_manager) {
static std::shared_ptr<StyleFilter> style_filter = std::make_shared<StyleFilter>(j_render_manager);
return style_filter;
}

private:
inline void MarkTextDirty(std::weak_ptr<RootNode> weak_root_node, uint32_t node_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static jmethodID j_render_manager_init_method_id;
static jmethodID j_render_manager_set_id_method_id;
static jmethodID j_render_manager_get_density_method_id;
static jmethodID j_render_manager_get_provider_method_id;
static jmethodID j_render_manager_get_style_for_render_id;

REGISTER_JNI("com/tencent/renderer/NativeRenderProvider",
"updateNodeSize",
Expand Down Expand Up @@ -84,6 +85,9 @@ static jint JNI_OnLoad(__unused JavaVM* j_vm, __unused void* reserved) {
j_render_manager_get_provider_method_id = j_env->GetMethodID(j_render_manager_clazz,
"getRenderProvider",
"()Lcom/tencent/renderer/NativeRenderProvider;");
j_render_manager_get_style_for_render_id = j_env->GetMethodID(j_render_manager_clazz,
"getPropsRegisterForRender",
"()[Ljava/lang/Object;");
return JNI_VERSION_1_4;;
}

Expand Down Expand Up @@ -128,6 +132,24 @@ float GetDensity(std::shared_ptr<JavaRef>&j_render_manager) {
return static_cast<float>(j_float);
}

void GetPropsRegisterForRender(const std::shared_ptr<JavaRef>& j_render_manager,
std::unordered_set<std::string>& style_set) {
auto instance = JNIEnvironment::GetInstance();
auto j_env = instance->AttachCurrentThread();
jobjectArray j_object_array =
(jobjectArray)j_env->CallObjectMethod(j_render_manager->GetObj(), j_render_manager_get_style_for_render_id);
jsize j_size = j_env->GetArrayLength(j_object_array);
for (int i = 0; i < j_size; i++) {
jstring j_style = reinterpret_cast<jstring>(j_env->GetObjectArrayElement(j_object_array, i));
const char* utf_c = j_env->GetStringUTFChars(j_style, nullptr);
if (utf_c != nullptr) {
std::string style_name(utf_c);
style_set.insert(style_name);
j_env->ReleaseStringUTFChars(j_style, utf_c);
}
}
}

jobject GetNativeRendererInstance(JNIEnv* j_env, jobject j_object, jint j_render_manager_id) {
return nullptr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ static bool IsMeasureNode(const std::string &name) {
std::atomic<uint32_t> NativeRenderManager::unique_native_render_manager_id_{1};
footstone::utils::PersistentObjectMap<uint32_t, std::shared_ptr<hippy::NativeRenderManager>> NativeRenderManager::persistent_map_;


StyleFilter::StyleFilter(const std::shared_ptr<JavaRef>& j_render_manager) {
hippy::GetPropsRegisterForRender(j_render_manager, styles_);
}

NativeRenderManager::NativeRenderManager() : RenderManager("NativeRenderManager"),
serializer_(std::make_shared<footstone::value::Serializer>()) {
id_ = unique_native_render_manager_id_.fetch_add(1);
Expand All @@ -87,6 +92,7 @@ NativeRenderManager::NativeRenderManager() : RenderManager("NativeRenderManager"
void NativeRenderManager::CreateRenderDelegate() {
persistent_map_.Insert(id_, shared_from_this());
FOOTSTONE_CHECK(hippy::CreateJavaRenderManager(id_, j_render_manager_, j_render_delegate_));
NativeRenderManager::GetStyleFilter(j_render_manager_);
}

void NativeRenderManager::InitDensity() {
Expand Down Expand Up @@ -139,11 +145,13 @@ void NativeRenderManager::CreateRenderNode(std::weak_ptr<RootNode> root_node,
// 样式属性
auto style = nodes[i]->GetStyleMap();
auto iter = style->begin();
auto style_filter = NativeRenderManager::GetStyleFilter(j_render_manager_);
while (iter != style->end()) {
props[iter->first] = *(iter->second);
if (style_filter->Enable(iter->first)) {
props[iter->first] = *(iter->second);
}
iter++;
}

// 用户自定义属性
auto dom_ext = *nodes[i]->GetExtStyle();
iter = dom_ext.begin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public NativeRender getNativeRender() {
return (NativeRender) mRenderer;
}

@NonNull
public ControllerUpdateManger getControllerUpdateManger() {
return mControllerUpdateManger;
}

private synchronized static void checkDefaultControllers() {
if (sDefaultControllers != null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.tencent.renderer.node.RenderNode;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -48,7 +49,8 @@ public class ControllerUpdateManger<T, G> {

private static final Map<Class<?>, Map<String, PropertyMethodHolder>> sViewPropsMethodMap = new HashMap<>();
private static final Map<String, PropertyMethodHolder> sComponentPropsMethodMap = new HashMap<>();
private static final Set<String> sTextPropsMap = new HashSet<>();
private static final Set<String> sTextPropsSet = new HashSet<>();
private static final ArrayList<String> sRenderPropsList = new ArrayList<>();
@NonNull
private final Renderer mRenderer;
@Nullable
Expand All @@ -70,6 +72,11 @@ public void setCustomPropsController(T controller) {
mCustomPropsController = controller;
}

@NonNull
public ArrayList<String> getPropsRegisterForRender() {
return sRenderPropsList;
}

private static void collectMethodHolder(@NonNull Class<?> cls,
@NonNull Map<String, PropertyMethodHolder> methodHolderMap) {
Method[] methods = cls.getMethods();
Expand All @@ -78,6 +85,7 @@ private static void collectMethodHolder(@NonNull Class<?> cls,
.getAnnotation(HippyControllerProps.class);
if (controllerProps != null) {
String style = controllerProps.name();
sRenderPropsList.add(style);
PropertyMethodHolder propsMethodHolder = new PropertyMethodHolder();
propsMethodHolder.defaultNumber = controllerProps.defaultNumber();
propsMethodHolder.defaultType = controllerProps.defaultType();
Expand All @@ -102,7 +110,8 @@ private static void initComponentPropsMap() {
HippyControllerProps controllerProps = method
.getAnnotation(HippyControllerProps.class);
if (controllerProps != null) {
sTextPropsMap.add(controllerProps.name());
sTextPropsSet.add(controllerProps.name());
sRenderPropsList.add(controllerProps.name());
}
}
}
Expand Down Expand Up @@ -201,7 +210,7 @@ protected void updateProps(@NonNull RenderNode node, @NonNull T controller, @Nul
}
Set<String> keySet = props.keySet();
for (String key : keySet) {
if (node instanceof TextRenderNode && sTextPropsMap.contains(key)) {
if (node instanceof TextRenderNode && sTextPropsSet.contains(key)) {
// The text related attributes have been processed in the build layout,
// so the following process no longer needs to be executed.
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public NativeRenderProvider getRenderProvider() {
return mRenderProvider;
}

public Object[] getPropsRegisterForRender() {
ArrayList<String> props = mRenderManager.getControllerManager().getControllerUpdateManger()
.getPropsRegisterForRender();
return props.toArray();
}

public void setId(int instanceId) {
mRenderProvider.setInstanceId(instanceId);
}
Expand Down

0 comments on commit c459934

Please sign in to comment.