Skip to content

Commit

Permalink
fix(android): should call native renderer destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Aug 10, 2023
1 parent d11670f commit 6cbd767
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ jint CreateNativeRenderManager(JNIEnv* j_env, jobject j_object) {

void DestroyNativeRenderManager(JNIEnv* j_env, jobject j_object, jint j_render_manager_id) {
auto render_manager_id = footstone::check::checked_numeric_cast<jint, uint32_t>(j_render_manager_id);
auto flag = hippy::global_data_holder.Erase(render_manager_id);
std::any render_manager;
auto flag = hippy::global_data_holder.Find(render_manager_id, render_manager);
if (flag) {
std::static_pointer_cast<NativeRenderManager>(std::any_cast<std::shared_ptr<RenderManager>>(render_manager))->DestroyRenderDelegate(j_env);
}
flag = hippy::global_data_holder.Erase(render_manager_id);
FOOTSTONE_DCHECK(flag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public void attachToDom(@NonNull Connector domConnector) {
@Override
public void destroy() {
destroyNativeRenderManager(mInstanceId);
mRenderer = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class HippyEngineWrapper {
hippyEngine.destroyModule(hippyRootView) { result, e ->
hippyEngine.destroyEngine()
}
hippyRootView = null
}

fun load(context: Context, callback: HippyEngineLoadCallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class PageConfiguration : AppCompatActivity(), View.OnClickListener {
}

override fun onBackPressed() {
val goBack: () -> Unit = { buildSnapshot { moveTaskToBack(true) } }
val goBack: () -> Unit = { buildSnapshot {
moveTaskToBack(true)
} }
hippyEngineWrapper?.apply {
if (hippyEngine.onBackPressed(goBack)) {
return
Expand All @@ -140,6 +142,7 @@ class PageConfiguration : AppCompatActivity(), View.OnClickListener {
hippyEngineWrapper?.snapshot = bitmap
(pageConfigurationContainer as ViewGroup).removeAllViews()
runnable.run()
hippyEngineWrapper = null
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ void OnCreateNativeRenderProvider(JNIEnv* j_env,
jint j_render_manager_id,
jfloat j_density);

void DestroyNativeRenderManager(JNIEnv* j_env,
jobject j_object,
jint j_render_manager_id);

jobject GetNativeRendererInstance(JNIEnv* j_env,
jobject j_object,
jint j_render_manager_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class NativeRenderManager : public RenderManager, public std::enable_shared_from
return j_render_manager_;
}
void CreateRenderDelegate();
void DestroyRenderDelegate(JNIEnv* j_env);
void InitDensity();
void CreateRenderNode(std::weak_ptr<RootNode> root_node, std::vector<std::shared_ptr<DomNode>>&& nodes) override;
void UpdateRenderNode(std::weak_ptr<RootNode> root_node, std::vector<std::shared_ptr<DomNode>>&& nodes) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@ static void JNI_OnUnload(__unused JavaVM* j_vm, __unused void* reserved) {
REGISTER_JNI_ONLOAD(JNI_OnLoad)
REGISTER_JNI_ONUNLOAD(JNI_OnUnload)

void DestroyNativeRenderManager(JNIEnv* j_env, jobject j_object, jint j_render_manager_id) {
auto& map = NativeRenderManager::PersistentMap();
bool ret = map.Erase(static_cast<uint32_t>(j_render_manager_id));
if (!ret) {
FOOTSTONE_DLOG(WARNING) << "DestroyNativeRenderManager delete render manager invalid";
}
}

bool CreateJavaRenderManager(uint32_t id, std::shared_ptr<JavaRef>&j_render_manager,
std::shared_ptr<JavaRef>&render_delegate) {
auto instance = JNIEnvironment::GetInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ void NativeRenderManager::CreateRenderDelegate() {
NativeRenderManager::GetStyleFilter(j_render_manager_);
}

void NativeRenderManager::DestroyRenderDelegate(JNIEnv* j_env) {
jobject j_object = j_render_manager_->GetObj();
jclass j_class = j_env->GetObjectClass(j_object);
if (!j_class) {
FOOTSTONE_LOG(ERROR) << "CallNativeMethod j_class error";
return;
}
jmethodID j_method_id = j_env->GetMethodID(j_class, "destroy", "()V");
if (!j_method_id) {
FOOTSTONE_LOG(ERROR) << "destroy" << " j_method_id error";
return;
}
j_env->CallVoidMethod(j_object, j_method_id);
JNIEnvironment::ClearJEnvException(j_env);
j_env->DeleteLocalRef(j_class);
}

void NativeRenderManager::InitDensity() {
density_ = hippy::GetDensity(j_render_manager_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@

public class ControllerManager {

@NonNull
private final Renderer mRenderer;
@NonNull
private final ControllerRegistry mControllerRegistry;
@NonNull
Expand All @@ -79,6 +77,8 @@ public class ControllerManager {
@NonNull
private final Map<Integer, Pool<String, View>> mRecycleViewPools = new HashMap<>();
@Nullable
private Renderer mRenderer;
@Nullable
private static List<Class<?>> sDefaultControllers;

public ControllerManager(@NonNull Renderer renderer) {
Expand All @@ -87,14 +87,14 @@ public ControllerManager(@NonNull Renderer renderer) {
mControllerUpdateManger = new ControllerUpdateManger<>(renderer);
}

@NonNull
@Nullable
public RenderManager getRenderManager() {
return ((NativeRender) mRenderer).getRenderManager();
return mRenderer != null ? ((NativeRender) mRenderer).getRenderManager() : null;
}

@NonNull
@Nullable
public NativeRender getNativeRender() {
return (NativeRender) mRenderer;
return mRenderer != null ? ((NativeRender) mRenderer) : null;
}

@NonNull
Expand Down Expand Up @@ -179,6 +179,8 @@ public HippyCustomPropsController getCustomPropsController() {
}

public void destroy() {
mControllerRegistry.clear();
mControllerUpdateManger.clear();
for (Pool<Integer, View> pool : mPreCreateViewPools.values()) {
pool.clear();
}
Expand All @@ -193,6 +195,7 @@ public void destroy() {
deleteRootView(mControllerRegistry.getRootIdAt(i));
}
}
mRenderer = null;
}

@Nullable
Expand Down Expand Up @@ -647,15 +650,21 @@ private String getViewOperationExceptionMessage(int pid, View parent, int id, Vi
}

private void reportRemoveViewException(int pid, View parent, int id, View child) {
NativeRenderException exception = new NativeRenderException(REMOVE_CHILD_VIEW_FAILED_ERR,
getViewOperationExceptionMessage(pid, parent, id, child, "Remove view failed:"));
mRenderer.handleRenderException(exception);
if (mRenderer != null) {
NativeRenderException exception = new NativeRenderException(
REMOVE_CHILD_VIEW_FAILED_ERR,
getViewOperationExceptionMessage(pid, parent, id, child,
"Remove view failed:"));
mRenderer.handleRenderException(exception);
}
}

private void reportAddViewException(int pid, View parent, int id, View child) {
NativeRenderException exception = new NativeRenderException(ADD_CHILD_VIEW_FAILED_ERR,
getViewOperationExceptionMessage(pid, parent, id, child,
"Add child to parent failed:"));
mRenderer.handleRenderException(exception);
if (mRenderer != null) {
NativeRenderException exception = new NativeRenderException(ADD_CHILD_VIEW_FAILED_ERR,
getViewOperationExceptionMessage(pid, parent, id, child,
"Add child to parent failed:"));
mRenderer.handleRenderException(exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ public class ControllerRegistry {
private final SparseArray<View> mRootViews = new SparseArray<>();
@NonNull
private final Map<String, ControllerHolder> mControllers = new HashMap<>();
@NonNull
private final Renderer mRenderer;
@Nullable
private Renderer mRenderer;

public ControllerRegistry(@NonNull Renderer renderer) {
mRenderer = renderer;
}

public void clear() {
mRenderer = null;
}

public void addControllerHolder(String name, ControllerHolder controllerHolder) {
mControllers.put(name, controllerHolder);
}
Expand All @@ -62,9 +66,11 @@ public ControllerHolder getControllerHolder(String className) {
public HippyViewController getViewController(@NonNull String className) {
ControllerHolder holder = mControllers.get(className);
if (holder == null) {
NativeRenderException exception = new NativeRenderException(
GET_VIEW_CONTROLLER_FAILED_ERR, "Unknown class name=" + className);
mRenderer.handleRenderException(exception);
if (mRenderer != null) {
NativeRenderException exception = new NativeRenderException(
GET_VIEW_CONTROLLER_FAILED_ERR, "Unknown class name=" + className);
mRenderer.handleRenderException(exception);
}
return null;
}
return holder.getViewController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
import com.tencent.renderer.utils.PropertyUtils.PropertyMethodHolder;
import com.tencent.renderer.node.RenderNode;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -61,8 +63,8 @@ public class ControllerUpdateManger<T, G> {
NodeProps.OPACITY,
NodeProps.OVERFLOW
};
@NonNull
private final Renderer mRenderer;
@Nullable
private Renderer mRenderer;
@Nullable
private ComponentController mComponentController;
@Nullable
Expand All @@ -78,6 +80,10 @@ public ControllerUpdateManger(@NonNull Renderer renderer) {
mRenderer = renderer;
}

public void clear() {
mRenderer = null;
}

public void setCustomPropsController(T controller) {
mCustomPropsController = controller;
}
Expand Down Expand Up @@ -124,9 +130,7 @@ private static void initComponentPropsMap() {
sRenderPropsList.add(controllerProps.name());
}
}
for (String layoutStyle : sLayoutStyleList) {
sRenderPropsList.add(layoutStyle);
}
Collections.addAll(sRenderPropsList, sLayoutStyleList);
}

void findViewPropsMethod(Class<?> cls,
Expand Down Expand Up @@ -177,9 +181,11 @@ private void invokePropMethod(@NonNull Object obj, @NonNull Object arg1,
methodHolder.method.invoke(obj, arg1, value);
}
} catch (Exception exception) {
mRenderer.handleRenderException(
PropertyUtils.makePropertyConvertException(exception, key,
methodHolder.method));
if (mRenderer != null) {
mRenderer.handleRenderException(
PropertyUtils.makePropertyConvertException(exception, key,
methodHolder.method));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public abstract class HippyViewController<T extends View & HippyViewBase> implem
private static final double[] sTransformDecompositionArray = new double[16];
private boolean bUserChangeFocus = false;

public View createView(@NonNull View rootView, int id, @NonNull Renderer renderer,
public View createView(@NonNull View rootView, int id, @Nullable Renderer renderer,
@NonNull String className, @Nullable Map<String, Object> props) {
View view = null;
Context context = rootView.getContext();
Object object = renderer.getCustomViewCreator();
Object object = renderer != null ? renderer.getCustomViewCreator() : null;
if (object instanceof HippyCustomViewCreator) {
view = ((HippyCustomViewCreator) object)
.createCustomView(className, context, props);
Expand Down
Loading

0 comments on commit 6cbd767

Please sign in to comment.