Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Commit 817f6ef

Browse files
acton393YorkShen
authored andcommitted
[WEEX-648][Android]native batch for dom operations generating from JavaScript (#1647)
* [WEEX-648][Android]native batch for dom operations generating from JavaScript introduce a native batch for dom operations generating from JavaScript wrap a series of dom operations with beginBatch and endBatch directives, when every V-sync signal comes, we try to ensure that the operations between beginBatch and endBatch can be performed in current V-sync, this policy can drop frames maybe, for our policy dropping frames, we only allow 20 frames at most. see iOS pull request #1644
1 parent 77c9219 commit 817f6ef

File tree

6 files changed

+171
-1
lines changed

6 files changed

+171
-1
lines changed

android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
import com.taobao.weex.dom.RenderContext;
3131
import com.taobao.weex.performance.WXInstanceApm;
3232
import com.taobao.weex.ui.action.BasicGraphicAction;
33+
import com.taobao.weex.ui.action.GraphicActionBatchAction;
3334
import com.taobao.weex.ui.component.WXComponent;
3435
import com.taobao.weex.utils.WXExceptionUtils;
3536
import com.taobao.weex.utils.WXUtils;
3637

3738
import java.util.ArrayList;
39+
import java.util.HashMap;
3840
import java.util.List;
3941
import java.util.Map;
4042
import java.util.concurrent.ConcurrentHashMap;
@@ -47,6 +49,10 @@ public class WXRenderManager {
4749

4850
private volatile ConcurrentHashMap<String, RenderContextImpl> mRenderContext;
4951
private WXRenderHandler mWXRenderHandler;
52+
private ArrayList<Map<String,Object>> mBatchActions = new ArrayList<>();
53+
private final int MAX_DROP_FRAME_NATIVE_BATCH = 2000;
54+
private final static String sKeyAction = "Action";
55+
private static int nativeBatchTimes = 0;
5056

5157
public WXRenderManager() {
5258
mRenderContext = new ConcurrentHashMap<>();
@@ -116,11 +122,43 @@ public void removeRenderStatement(String instanceId) {
116122
}
117123
}
118124

125+
private void postAllStashedGraphicAction(final String instanceId,final BasicGraphicAction action) {
126+
ArrayList<Map<String, Object>> tmpList = new ArrayList<>(mBatchActions);
127+
this.mBatchActions.clear();
128+
ArrayList<BasicGraphicAction> actions = new ArrayList<>(tmpList.size());
129+
for (int i = 0 ; i < tmpList.size(); i ++) {
130+
Map<String, Object> item = tmpList.get(i);
131+
BasicGraphicAction tmpAction = (BasicGraphicAction)item.get(sKeyAction);
132+
if (tmpAction.mActionType == BasicGraphicAction.ActionTypeBatchBegin || tmpAction.mActionType == BasicGraphicAction.ActionTypeBatchEnd) {
133+
continue;
134+
}
135+
actions.add(tmpAction);
136+
}
137+
nativeBatchTimes = 0;
138+
postGraphicAction(instanceId, new GraphicActionBatchAction(action.getWXSDKIntance(),action.getRef(), actions));
139+
}
140+
119141
public void postGraphicAction(final String instanceId, final BasicGraphicAction action) {
120142
final RenderContextImpl renderContext = mRenderContext.get(instanceId);
121143
if (renderContext == null) {
122144
return;
123145
}
146+
147+
if (action.mActionType == BasicGraphicAction.ActionTypeBatchEnd) {
148+
postAllStashedGraphicAction(instanceId,action);
149+
return;
150+
} else if (action.mActionType == BasicGraphicAction.ActionTypeBatchBegin || this.mBatchActions.size() > 0 ) {
151+
nativeBatchTimes ++ ;
152+
if (nativeBatchTimes > MAX_DROP_FRAME_NATIVE_BATCH) {
153+
postAllStashedGraphicAction(instanceId,action);
154+
} else {
155+
HashMap<String, Object> item = new HashMap<>(1);
156+
item.put(sKeyAction, action);
157+
mBatchActions.add(item);
158+
return;
159+
}
160+
}
161+
124162
mWXRenderHandler.post(instanceId, action);
125163
}
126164

android/sdk/src/main/java/com/taobao/weex/ui/action/BasicGraphicAction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public abstract class BasicGraphicAction implements IExecutable, Runnable {
2828

2929
private WXSDKInstance mInstance;
3030
private final String mRef;
31+
public int mActionType = ActionTypeNormal;
32+
public static final int ActionTypeBatchBegin = 1;
33+
public static final int ActionTypeBatchEnd = 2;
34+
public static final int ActionTypeNormal = 0;
35+
3136

3237
public BasicGraphicAction(WXSDKInstance instance, String ref) {
3338
this.mInstance = instance;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.taobao.weex.ui.action;
21+
22+
import com.taobao.weex.WXSDKInstance;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class GraphicActionBatchAction extends BasicGraphicAction {
28+
private List<BasicGraphicAction> mActions;
29+
30+
public GraphicActionBatchAction(WXSDKInstance instance, String ref, List<BasicGraphicAction> mActions) {
31+
super(instance, ref);
32+
this.mActions = new ArrayList<>(mActions);
33+
}
34+
35+
@Override
36+
public void executeAction() {
37+
for (int i = 0;i < mActions.size();i ++) {
38+
mActions.get(i).executeAction();
39+
}
40+
}
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.taobao.weex.ui.action;
21+
22+
import com.taobao.weex.WXSDKInstance;
23+
24+
public class GraphicActionBatchBegin extends BasicGraphicAction {
25+
public GraphicActionBatchBegin(WXSDKInstance instance, String ref) {
26+
super(instance, ref);
27+
this.mActionType = ActionTypeBatchBegin;
28+
}
29+
30+
@Override
31+
public void executeAction() {
32+
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.taobao.weex.ui.action;
21+
22+
import com.taobao.weex.WXSDKInstance;
23+
24+
public class GraphicActionBatchEnd extends BasicGraphicAction {
25+
public GraphicActionBatchEnd(WXSDKInstance instance, String ref) {
26+
super(instance, ref);
27+
this.mActionType = ActionTypeBatchEnd;
28+
}
29+
30+
@Override
31+
public void executeAction() {
32+
33+
}
34+
}

android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import com.taobao.weex.ui.action.ActionAddRule;
2828
import com.taobao.weex.ui.action.ActionGetComponentRect;
2929
import com.taobao.weex.ui.action.ActionInvokeMethod;
30+
import com.taobao.weex.ui.action.GraphicActionBatchBegin;
31+
import com.taobao.weex.ui.action.GraphicActionBatchEnd;
3032
import com.taobao.weex.ui.action.GraphicActionScrollToElement;
3133
import com.taobao.weex.ui.action.UpdateComponentDataAction;
3234
import com.taobao.weex.utils.WXLogUtils;
@@ -50,11 +52,14 @@ public final class WXDomModule extends WXModule {
5052

5153
public static final String UPDATE_COMPONENT_DATA = "updateComponentData";
5254

55+
public static final String BATCH_BEGIN = "beginBatchMark";
56+
public static final String BATCH_END = "endBatchMark";
57+
5358
/**
5459
* Methods expose to js. Every method which will be called in js should add to this array.
5560
*/
5661
public static final String[] METHODS = {SCROLL_TO_ELEMENT, ADD_RULE, GET_COMPONENT_RECT,
57-
INVOKE_METHOD};
62+
INVOKE_METHOD, BATCH_BEGIN, BATCH_END};
5863

5964
public WXDomModule(WXSDKInstance instance){
6065
mWXSDKInstance = instance;
@@ -118,6 +123,19 @@ public Object callDomMethod(String method, JSONArray args, long... parseNanos) {
118123
}
119124
new UpdateComponentDataAction(mWXSDKInstance, args.getString(0), JSONUtils.toJSON(args.get(1)), args.getString(2)).executeAction();
120125
break;
126+
case BATCH_BEGIN: {
127+
if(args == null){
128+
return null;
129+
}
130+
String ref = args.size() >= 1 ? args.getString(0) : null;
131+
new GraphicActionBatchBegin(mWXSDKInstance, ref).executeActionOnRender();
132+
break;
133+
}
134+
case BATCH_END: {
135+
String ref = args.size() >= 1 ? args.getString(0) : null;
136+
new GraphicActionBatchEnd(mWXSDKInstance, ref).executeActionOnRender();
137+
break;
138+
}
121139
default:
122140
WXLogUtils.e("Unknown dom action.");
123141
break;

0 commit comments

Comments
 (0)