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

Weex prerender #1978

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
150 changes: 134 additions & 16 deletions android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@
import com.taobao.weex.instance.InstanceOnFireEventInterceptor;
import com.taobao.weex.layout.ContentBoxMeasurement;
import com.taobao.weex.performance.WXInstanceApm;
import com.taobao.weex.performance.WXAnalyzerDataTransfer;
import com.taobao.weex.prerender.PreRenderContext;
import com.taobao.weex.tracing.WXTracing;
import com.taobao.weex.ui.action.GraphicActionAddElement;
import com.taobao.weex.ui.action.GraphicActionBaseAddElement;
import com.taobao.weex.ui.component.NestedContainer;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.ui.component.WXEmbed;
import com.taobao.weex.ui.component.node.WXComponentNode;
import com.taobao.weex.ui.flat.FlatGUIContext;
import com.taobao.weex.ui.view.WXScrollView;
import com.taobao.weex.utils.Trace;
Expand All @@ -90,6 +94,7 @@
import java.util.Map;
import java.util.PriorityQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;


/**
Expand Down Expand Up @@ -139,8 +144,14 @@ public class WXSDKInstance implements IWXActivityStateListener,View.OnLayoutChan
private @NonNull
FlatGUIContext mFlatGUIContext =new FlatGUIContext();

private float mRealWidth = 0;
private float mRealHeight = 0;

private Map<String,String> mContainerInfo;

@NonNull
private PreRenderContext mPrerenderContext = new PreRenderContext();

public boolean isNewFsEnd = false;

/**
Expand Down Expand Up @@ -206,7 +217,7 @@ public void removeLayerOverFlowListener(String ref) {

private boolean mCurrentGround = false;
private ComponentObserver mComponentObserver;
private Map<String, GraphicActionAddElement> inactiveAddElementAction = new ArrayMap<>();
private Map<String, GraphicActionBaseAddElement> inactiveAddElementAction = new ArrayMap<>();

private Map<Long, ContentBoxMeasurement> mContentBoxMeasurements = new ArrayMap<>();

Expand Down Expand Up @@ -266,6 +277,85 @@ public void setUseSingleProcess(boolean flag) {
WXBridgeManager.getInstance().setUseSingleProcess(flag);
}

public void preRenderByUrl(String pageName,
final String url,
Map<String, Object> options,
final String jsonInitData,
final int width,
final int height,
final WXRenderStrategy flag,
final int layoutMode, final float offset) {
if (mPrerenderContext.interceptRenderState.compareAndSet(PreRenderContext.INTERCEPT_RENDER_CLOSE, PreRenderContext.INTERCEPT_RENDER_OPEN)) {
mPrerenderContext.width = width;
mPrerenderContext.height = height;
mPrerenderContext.enableOffset = offset;
mPrerenderContext.setLayoutMode(layoutMode);
mPrerenderContext.onPreRender();
renderByUrl(pageName, url, options, jsonInitData, flag);
}
}

public void preRenderByTemplateWithLayoutMode(String pageName,
final String template,
Map<String, Object> options,
final String jsonInitData,
final int width,
final int height,
final WXRenderStrategy flag,
final int layoutMode,
final float offset) {
if (mPrerenderContext.interceptRenderState.compareAndSet(PreRenderContext.INTERCEPT_RENDER_CLOSE, PreRenderContext.INTERCEPT_RENDER_OPEN)) {
mPrerenderContext.width = width;
mPrerenderContext.height = height;
mPrerenderContext.enableOffset = offset;
mPrerenderContext.setLayoutMode(layoutMode);
mPrerenderContext.onPreRender();
render(pageName, template, options, jsonInitData, flag);
}
}

public boolean realRender(@NonNull Context context) {
if (mPrerenderContext.interceptRenderState.compareAndSet(PreRenderContext.INTERCEPT_RENDER_OPEN, PreRenderContext.INTERCEPT_RENDER_CLOSE)) {
mContext = context;
mPrerenderContext.onRealRender();
WXComponentNode rootNode = mPrerenderContext.rootNode;
if (rootNode != null) {
mPrerenderContext.rootNode.startTransform();
return true;
} else {
WXLogUtils.w("real render failed, rootNode is null");
}
} else {
WXLogUtils.w("real render failed, state wrong");
}
return false;
}

public void specifiedRootNode(WXComponentNode rootNode) {
mPrerenderContext.rootNode = rootNode;
}

public void updateRootNode() {
updateDefaultRootSize(mPrerenderContext.getRenderWidth(), mPrerenderContext.getRenderHeight());
}

public AtomicBoolean isRenderSuccess() {
return mPrerenderContext.isRenderSuccess;
}

@NonNull
public PreRenderContext getPrerenderContext() {
return mPrerenderContext;
}

public Map<String, WXComponentNode> getNodeMap() {
return mPrerenderContext.nodeMap;
}

public boolean getNeedInterceptRender() {
return mPrerenderContext.interceptRenderState.get() == PreRenderContext.INTERCEPT_RENDER_OPEN;
}

/**
* set open SandBox
* @param flag
Expand All @@ -288,7 +378,7 @@ public void setMaxHiddenEmbedsNum(int maxHiddenEmbedsNum) {

@WorkerThread
@RestrictTo(Scope.LIBRARY)
public void addInActiveAddElementAction(String ref, GraphicActionAddElement action){
public void addInActiveAddElementAction(String ref, GraphicActionBaseAddElement action){
inactiveAddElementAction.put(ref, action);
}

Expand All @@ -300,7 +390,7 @@ public void removeInActiveAddElmentAction(String ref){

@WorkerThread
@RestrictTo(Scope.LIBRARY)
public GraphicActionAddElement getInActiveAddElementAction(String ref){
public GraphicActionBaseAddElement getInActiveAddElementAction(String ref){
return inactiveAddElementAction.get(ref);
}

Expand Down Expand Up @@ -1712,27 +1802,51 @@ public void setSize(int width, int height) {
}
ViewGroup.LayoutParams layoutParams = mRenderContainer.getLayoutParams();
if (layoutParams != null) {
final float realWidth = width;
final float realHeight = height;


if (mRenderContainer.getWidth() != width || mRenderContainer.getHeight() != height) {
layoutParams.width = width;
layoutParams.height = height;
mRenderContainer.setLayoutParams(layoutParams);
}

if (mRootComp != null && layoutParams != null) {
final boolean isWidthWrapContent = layoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT;
final boolean isHeightWrapContent = layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT;
updateDefaultRootSize(width, height);
}
}
}

private void updateDefaultRootSize(final int width, final int height) {
// 预渲染中定下的宽高需要有个误差值,防止浮点的计算误差
float offset = mPrerenderContext.enableOffset;
if (Math.abs(mRealWidth - width) < offset && Math.abs(mRealHeight - height) < offset) {
WXLogUtils.w("update default root size failed, size not changed");
return;
}
int paramWidth = width;
int paramHeight = height;

WXBridgeManager.getInstance().post(new Runnable() {
@Override
public void run() {
WXBridgeManager.getInstance().setDefaultRootSize(getInstanceId(), realWidth, realHeight, isWidthWrapContent,
isHeightWrapContent);
}
});
if (mRenderContainer != null && mRenderContainer.getLayoutParams() != null) {
paramWidth = mRenderContainer.getLayoutParams().width;
paramHeight = mRenderContainer.getLayoutParams().height;
}

WXLogUtils.i("update default size, oldSize:(" + mRealWidth + ", " + mRealHeight + "), newSize:(" + width + ", " + height + ")");
if (mRootComp != null || mPrerenderContext.rootNode != null) {
final boolean isWidthWrapContent = (paramWidth == ViewGroup.LayoutParams.WRAP_CONTENT);
final boolean isHeightWrapContent = (paramHeight == ViewGroup.LayoutParams.WRAP_CONTENT);

final float realWidth = width;
final float realHeight = height;
mRealWidth = realWidth;
mRealHeight = realHeight;

WXBridgeManager.getInstance().post(new Runnable() {
@Override
public void run() {
WXBridgeManager.getInstance().setDefaultRootSize(getInstanceId(), realWidth, realHeight, isWidthWrapContent,
isHeightWrapContent);
}
}
});
}
}

Expand Down Expand Up @@ -2035,4 +2149,8 @@ public void addInstanceOnFireEventInterceptor(InstanceOnFireEventInterceptor ins
getInstanceOnFireEventInterceptorList().add(instanceOnFireEventInterceptor);
}
}

public boolean hasPrerender() {
return mPrerenderContext.isPrerenderMode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,13 @@
import com.taobao.weex.dom.CSSShorthand;
import com.taobao.weex.layout.ContentBoxMeasurement;
import com.taobao.weex.performance.WXInstanceApm;
import com.taobao.weex.ui.IActionCreator;
import com.taobao.weex.ui.WXComponentRegistry;
import com.taobao.weex.ui.action.ActionReloadPage;
import com.taobao.weex.ui.action.BasicGraphicAction;
import com.taobao.weex.ui.action.GraphicActionAddElement;
import com.taobao.weex.ui.action.GraphicActionAddEvent;
import com.taobao.weex.ui.action.GraphicActionAppendTreeCreateFinish;
import com.taobao.weex.ui.action.GraphicActionCreateBody;
import com.taobao.weex.ui.action.GraphicActionCreateFinish;
import com.taobao.weex.ui.action.GraphicActionBaseAddElement;
import com.taobao.weex.ui.action.GraphicActionCreator;
import com.taobao.weex.ui.action.GraphicActionLayout;
import com.taobao.weex.ui.action.GraphicActionMoveElement;
import com.taobao.weex.ui.action.GraphicActionRefreshFinish;
Expand All @@ -78,6 +77,7 @@
import com.taobao.weex.ui.action.GraphicSize;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.ui.module.WXDomModule;
import com.taobao.weex.ui.prerenderaction.PrerenderActionCreator;
import com.taobao.weex.utils.WXExceptionUtils;
import com.taobao.weex.utils.WXFileUtils;
import com.taobao.weex.utils.WXJsonUtils;
Expand Down Expand Up @@ -206,6 +206,9 @@ public enum BundType {

private static Map<String, String> mWeexCoreEnvOptions = new HashMap<>();

private IActionCreator mPrerenderCreator = new PrerenderActionCreator();
private IActionCreator mNormalCreator = new GraphicActionCreator();

/**
* package
**/
Expand Down Expand Up @@ -2456,7 +2459,7 @@ public int callCreateBody(String pageId, String componentType, String ref,
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(pageId);
if (instance != null) {
final BasicGraphicAction action = new GraphicActionCreateBody(instance, ref, componentType,
final BasicGraphicAction action = getActionCreator(instance).createGraphicActionCreateBody(instance, ref, componentType,
styles, attributes, events, margins, paddings, borders);
WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(action.getPageId(), action);
}
Expand Down Expand Up @@ -2501,8 +2504,10 @@ public int callAddElement(String pageId, String componentType, String ref, int i
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(pageId);
if (instance != null) {
final GraphicActionAddElement action = new GraphicActionAddElement(instance, ref, componentType, parentRef, index,
styles, attributes, events, margins, paddings, borders);
final GraphicActionBaseAddElement action = getActionCreator(instance)
.createGraphicActionAddElement(instance,
ref, componentType, parentRef, index,
styles, attributes, events, margins, paddings, borders);
if(willLayout) {
instance.addInActiveAddElementAction(ref, action);
}else{
Expand Down Expand Up @@ -2545,7 +2550,7 @@ public int callRemoveElement(String instanceId, String ref) {
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance != null) {
final BasicGraphicAction action = new GraphicActionRemoveElement(instance, ref);
final BasicGraphicAction action = getActionCreator(instance).createGraphicActionRemoveElement(instance, ref);
if(instance.getInActiveAddElementAction(ref)!=null){
instance.removeInActiveAddElmentAction(ref);
}
Expand Down Expand Up @@ -2592,7 +2597,7 @@ public int callMoveElement(String instanceId, String ref, String parentref, int
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance != null) {
final BasicGraphicAction action = new GraphicActionMoveElement(instance, ref, parentref, index);
final BasicGraphicAction action = getActionCreator(instance).createGraphicActionMoveElement(instance, ref, parentref, index);
WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(action.getPageId(), action);
}
} catch (Exception e) {
Expand Down Expand Up @@ -2632,7 +2637,7 @@ public int callAddEvent(String instanceId, String ref, String event) {
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance != null) {
new GraphicActionAddEvent(instance, ref, event).executeActionOnRender();
getActionCreator(instance).createGraphicActionAddEvent(instance, ref, event).executeActionOnRender();
}
} catch (Exception e) {
WXLogUtils.e("[WXBridgeManager] callAddEvent exception: ", e);
Expand Down Expand Up @@ -2673,7 +2678,7 @@ public int callRemoveEvent(String instanceId, String ref, String event) {
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance != null) {
new GraphicActionRemoveEvent(instance, ref, event).executeActionOnRender();
getActionCreator(instance).createGraphicActionRemoveEvent(instance, ref, event).executeActionOnRender();
}
} catch (Exception e) {
WXLogUtils.e("[WXBridgeManager] callRemoveEvent exception: ", e);
Expand Down Expand Up @@ -2720,7 +2725,7 @@ public int callUpdateStyle(String instanceId, String ref, HashMap<String, Object
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance != null) {
final BasicGraphicAction action = new GraphicActionUpdateStyle(instance, ref, styles, paddings, margins, borders);
final BasicGraphicAction action = getActionCreator(instance).createGraphicActionUpdateStyle(instance, ref, styles, paddings, margins, borders);
WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(action.getPageId(), action);
}
} catch (Exception e) {
Expand Down Expand Up @@ -2760,7 +2765,7 @@ public int callUpdateAttrs(String instanceId, String ref, HashMap<String, String
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance != null) {
final BasicGraphicAction action = new GraphicActionUpdateAttr(instance, ref, attrs);
final BasicGraphicAction action = getActionCreator(instance).createGraphicActionUpdateAttr(instance, ref, attrs);
WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(action.getPageId(), action);
}
} catch (Exception e) {
Expand Down Expand Up @@ -2806,7 +2811,7 @@ public int callLayout(String pageId, String ref, int top, int bottom, int left,
if (instance != null) {
GraphicSize size = new GraphicSize(width, height);
GraphicPosition position = new GraphicPosition(left, top, right, bottom);
GraphicActionAddElement addAction = instance.getInActiveAddElementAction(ref);
GraphicActionBaseAddElement addAction = instance.getInActiveAddElementAction(ref);
if(addAction!=null) {
addAction.setRTL(isRTL);
addAction.setSize(size);
Expand All @@ -2818,7 +2823,7 @@ public int callLayout(String pageId, String ref, int top, int bottom, int left,
instance.removeInActiveAddElmentAction(ref);
}
else {
final BasicGraphicAction action = new GraphicActionLayout(instance, ref, position, size, isRTL);
final BasicGraphicAction action = getActionCreator(instance).createGraphicActionLayout(instance, ref, position, size, isRTL);
WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(action.getPageId(), action);
}
}
Expand Down Expand Up @@ -2892,7 +2897,7 @@ public int callCreateFinish(String instanceId) {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance != null) {
instance.firstScreenCreateInstanceTime(start);
GraphicActionCreateFinish action = new GraphicActionCreateFinish(instance);
BasicGraphicAction action = getActionCreator(instance).createGraphicActionCreateFinish(instance);
WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(instanceId, action);
}
} catch (Exception e) {
Expand Down Expand Up @@ -2928,7 +2933,7 @@ public int callRenderSuccess(String instanceId) {
try {
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance != null) {
GraphicActionRenderSuccess action = new GraphicActionRenderSuccess(instance);
BasicGraphicAction action = getActionCreator(instance).createGraphicActionRenderSuccess(instance);
WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(instanceId, action);
}
} catch (Exception e) {
Expand Down Expand Up @@ -3077,6 +3082,14 @@ public void registerCoreEnv(String key, String value) {
mWeexCoreEnvOptions.put(key, value);
}

public IActionCreator getActionCreator(WXSDKInstance instance) {
if (instance.hasPrerender()) {
return mPrerenderCreator;
} else {
return mNormalCreator;
}
}

private void onJsFrameWorkInitSuccees() {
for (Map.Entry<String, String> entry : mWeexCoreEnvOptions.entrySet()) {
mWXBridge.registerCoreEnv(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum WXRenderStrategy {
DATA_RENDER("DATA_RENDER"),
DATA_RENDER_BINARY("DATA_RENDER_BINARY");


private String flag;

WXRenderStrategy(String flag) {
Expand Down
Loading