You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by GitBox <gi...@apache.org> on 2018/11/05 07:56:29 UTC

[GitHub] YorkShen closed pull request #1647: [WEEX-648][Android]native batch for dom operations generating from JavaScript

YorkShen closed pull request #1647: [WEEX-648][Android]native batch for dom operations generating from JavaScript
URL: https://github.com/apache/incubator-weex/pull/1647
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java b/android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java
index 95cabd79b5..ac02fa8278 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java
@@ -30,11 +30,13 @@
 import com.taobao.weex.dom.RenderContext;
 import com.taobao.weex.performance.WXInstanceApm;
 import com.taobao.weex.ui.action.BasicGraphicAction;
+import com.taobao.weex.ui.action.GraphicActionBatchAction;
 import com.taobao.weex.ui.component.WXComponent;
 import com.taobao.weex.utils.WXExceptionUtils;
 import com.taobao.weex.utils.WXUtils;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -47,6 +49,10 @@
 
   private volatile ConcurrentHashMap<String, RenderContextImpl> mRenderContext;
   private WXRenderHandler mWXRenderHandler;
+  private ArrayList<Map<String,Object>> mBatchActions = new ArrayList<>();
+  private final int MAX_DROP_FRAME_NATIVE_BATCH = 2000;
+  private final static  String sKeyAction = "Action";
+  private static int nativeBatchTimes = 0;
 
   public WXRenderManager() {
     mRenderContext = new ConcurrentHashMap<>();
@@ -116,11 +122,43 @@ public void removeRenderStatement(String instanceId) {
     }
   }
 
+  private void postAllStashedGraphicAction(final String instanceId,final BasicGraphicAction action) {
+      ArrayList<Map<String, Object>> tmpList = new ArrayList<>(mBatchActions);
+      this.mBatchActions.clear();
+      ArrayList<BasicGraphicAction> actions = new ArrayList<>(tmpList.size());
+      for (int i = 0 ; i < tmpList.size(); i ++) {
+          Map<String, Object> item = tmpList.get(i);
+          BasicGraphicAction tmpAction = (BasicGraphicAction)item.get(sKeyAction);
+          if (tmpAction.mActionType == BasicGraphicAction.ActionTypeBatchBegin || tmpAction.mActionType == BasicGraphicAction.ActionTypeBatchEnd) {
+              continue;
+          }
+          actions.add(tmpAction);
+      }
+      nativeBatchTimes = 0;
+      postGraphicAction(instanceId, new GraphicActionBatchAction(action.getWXSDKIntance(),action.getRef(), actions));
+  }
+
   public void postGraphicAction(final String instanceId, final BasicGraphicAction action) {
     final RenderContextImpl renderContext = mRenderContext.get(instanceId);
     if (renderContext == null) {
       return;
     }
+
+    if (action.mActionType == BasicGraphicAction.ActionTypeBatchEnd) {
+        postAllStashedGraphicAction(instanceId,action);
+        return;
+    } else if (action.mActionType == BasicGraphicAction.ActionTypeBatchBegin || this.mBatchActions.size() > 0 ) {
+        nativeBatchTimes ++ ;
+        if (nativeBatchTimes > MAX_DROP_FRAME_NATIVE_BATCH) {
+            postAllStashedGraphicAction(instanceId,action);
+        } else {
+            HashMap<String, Object> item = new HashMap<>(1);
+            item.put(sKeyAction, action);
+            mBatchActions.add(item);
+            return;
+        }
+    }
+
     mWXRenderHandler.post(instanceId, action);
   }
 
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/BasicGraphicAction.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/BasicGraphicAction.java
index 6f158c38a9..e232584b3d 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/action/BasicGraphicAction.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/BasicGraphicAction.java
@@ -28,6 +28,11 @@
 
   private WXSDKInstance mInstance;
   private final String mRef;
+  public int mActionType = ActionTypeNormal;
+  public static final int ActionTypeBatchBegin = 1;
+  public static final int ActionTypeBatchEnd = 2;
+  public static final int ActionTypeNormal = 0;
+
 
   public BasicGraphicAction(WXSDKInstance instance, String ref) {
     this.mInstance = instance;
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchAction.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchAction.java
new file mode 100644
index 0000000000..901db4012c
--- /dev/null
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchAction.java
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package com.taobao.weex.ui.action;
+
+import com.taobao.weex.WXSDKInstance;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class GraphicActionBatchAction extends BasicGraphicAction {
+    private List<BasicGraphicAction> mActions;
+
+    public GraphicActionBatchAction(WXSDKInstance instance, String ref, List<BasicGraphicAction> mActions) {
+        super(instance, ref);
+        this.mActions = new ArrayList<>(mActions);
+    }
+
+    @Override
+    public void executeAction() {
+        for (int i = 0;i < mActions.size();i ++) {
+            mActions.get(i).executeAction();
+        }
+    }
+}
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchBegin.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchBegin.java
new file mode 100644
index 0000000000..d61540a2ee
--- /dev/null
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchBegin.java
@@ -0,0 +1,34 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package com.taobao.weex.ui.action;
+
+import com.taobao.weex.WXSDKInstance;
+
+public class GraphicActionBatchBegin extends BasicGraphicAction {
+    public GraphicActionBatchBegin(WXSDKInstance instance, String ref) {
+        super(instance, ref);
+        this.mActionType = ActionTypeBatchBegin;
+    }
+
+    @Override
+    public void executeAction() {
+
+    }
+}
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchEnd.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchEnd.java
new file mode 100644
index 0000000000..45d9744a33
--- /dev/null
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBatchEnd.java
@@ -0,0 +1,34 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package com.taobao.weex.ui.action;
+
+import com.taobao.weex.WXSDKInstance;
+
+public class GraphicActionBatchEnd extends BasicGraphicAction {
+    public GraphicActionBatchEnd(WXSDKInstance instance, String ref) {
+        super(instance, ref);
+        this.mActionType = ActionTypeBatchEnd;
+    }
+
+    @Override
+    public void executeAction() {
+
+    }
+}
diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java b/android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java
index 3cc7b660be..787ae53522 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java
@@ -27,6 +27,8 @@
 import com.taobao.weex.ui.action.ActionAddRule;
 import com.taobao.weex.ui.action.ActionGetComponentRect;
 import com.taobao.weex.ui.action.ActionInvokeMethod;
+import com.taobao.weex.ui.action.GraphicActionBatchBegin;
+import com.taobao.weex.ui.action.GraphicActionBatchEnd;
 import com.taobao.weex.ui.action.GraphicActionScrollToElement;
 import com.taobao.weex.ui.action.UpdateComponentDataAction;
 import com.taobao.weex.utils.WXLogUtils;
@@ -50,11 +52,14 @@
 
   public static final String UPDATE_COMPONENT_DATA = "updateComponentData";
 
+  public static final String BATCH_BEGIN = "beginBatchMark";
+  public static final String BATCH_END = "endBatchMark";
+
   /**
    * Methods expose to js. Every method which will be called in js should add to this array.
    */
   public static final String[] METHODS = {SCROLL_TO_ELEMENT, ADD_RULE, GET_COMPONENT_RECT,
-      INVOKE_METHOD};
+      INVOKE_METHOD, BATCH_BEGIN, BATCH_END};
 
   public WXDomModule(WXSDKInstance instance){
     mWXSDKInstance = instance;
@@ -118,6 +123,19 @@ public Object callDomMethod(String method, JSONArray args, long... parseNanos) {
           }
           new UpdateComponentDataAction(mWXSDKInstance, args.getString(0), JSONUtils.toJSON(args.get(1)), args.getString(2)).executeAction();
           break;
+        case BATCH_BEGIN: {
+          if(args == null){
+            return null;
+          }
+          String ref = args.size() >= 1 ? args.getString(0) : null;
+          new GraphicActionBatchBegin(mWXSDKInstance, ref).executeActionOnRender();
+          break;
+        }
+        case BATCH_END: {
+          String ref = args.size() >= 1 ? args.getString(0) : null;
+          new GraphicActionBatchEnd(mWXSDKInstance, ref).executeActionOnRender();
+          break;
+        }
         default:
           WXLogUtils.e("Unknown dom action.");
           break;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services