Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add filter for css style to native renderer #3405

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading