[go: nahoru, domu]

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

Commit

Permalink
[Android] solve components exceed gpu limit problem (#2603)
Browse files Browse the repository at this point in the history
  • Loading branch information
katherine95s authored and YorkShen committed Jul 1, 2019
1 parent d23e29c commit 48a7cb7
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 14 deletions.
11 changes: 10 additions & 1 deletion android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
Expand Down Expand Up @@ -152,6 +153,7 @@ public class WXSDKInstance implements IWXActivityStateListener,View.OnLayoutChan
private Map<String,String> mContainerInfo;

public boolean isNewFsEnd = false;
private List<JSONObject> componentsInfoExceedGPULimit = new LinkedList<>();

/**
* bundle type
Expand Down Expand Up @@ -206,6 +208,13 @@ public class WXSDKInstance implements IWXActivityStateListener,View.OnLayoutChan
* */
private boolean mAutoAdjustDeviceWidth = WXEnvironment.AUTO_ADJUST_ENV_DEVICE_WIDTH;

public List<JSONObject> getComponentsExceedGPULimit(){return componentsInfoExceedGPULimit;}
@RestrictTo(Scope.LIBRARY)
public void setComponentsInfoExceedGPULimit(JSONObject component){
if(component!= null && !component.isEmpty()){
componentsInfoExceedGPULimit.add(component);
}
}

public List<String> getLayerOverFlowListeners() {
return mLayerOverFlowListeners;
Expand Down Expand Up @@ -827,7 +836,7 @@ private void renderInternal(String pageName,
mWXPerformance.JSTemplateSize = template.length() / 1024f;
mApmForInstance.addStats(WXInstanceApm.KEY_PAGE_STATS_BUNDLE_SIZE,mWXPerformance.JSTemplateSize);
mRenderStartTime = System.currentTimeMillis();
WXSDKManager.getInstance().setCrashInfo(WXEnvironment.WEEX_CURRENT_KEY,pageName);;
WXSDKManager.getInstance().setCrashInfo(WXEnvironment.WEEX_CURRENT_KEY,pageName);
if(mAutoAdjustDeviceWidth && WXDeviceUtils.isAutoResize(mContext)){
if(WXEnvironment.AUTO_UPDATE_APPLICATION_SCREEN_SIZE) {
WXViewUtils.updateApplicationScreen(mContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.taobao.weex.performance.WXInstanceApm;
import com.taobao.weex.performance.WXStateRecord;
import com.taobao.weex.ui.WXComponentRegistry;
import com.taobao.weex.ui.WXRenderManager;
import com.taobao.weex.ui.action.ActionReloadPage;
import com.taobao.weex.ui.action.BasicGraphicAction;
import com.taobao.weex.ui.action.GraphicActionAddElement;
Expand Down Expand Up @@ -102,11 +103,13 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -851,7 +854,7 @@ public int callReportCrashReloadPage(String instanceId, String crashFile) {
Log.d("jsengine", "callReportCrashReloadPage crashFile:" + crashFile);
}
} catch (Throwable e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
WXStateRecord.getInstance().onJSCCrash();
callReportCrash(crashFile, instanceId, url);
Expand Down Expand Up @@ -955,7 +958,7 @@ public void run() {
commitJscCrashAlarmMonitor(IWXUserTrackAdapter.JS_BRIDGE, WXErrorCode.WX_ERR_JSC_CRASH, result.toString(), instanceId, url);
br.close();
} catch (Exception e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
} else {
WXLogUtils.e("[WXBridgeManager] callReportCrash crash file is empty");
Expand Down Expand Up @@ -1536,7 +1539,7 @@ private void invokeCreateInstance(@NonNull WXSDKInstance instance, Script templa
WXLogUtils.e("end getBundleType type:" + type.toString() + " time:" + (end - start));
}
} catch (Throwable e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}

try {
Expand Down Expand Up @@ -1566,7 +1569,7 @@ private void invokeCreateInstance(@NonNull WXSDKInstance instance, Script templa
options.put("env", mInitParams.toMap());
}
} catch (Throwable e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
instance.bundleType = type;
if (WXEnvironment.isApkDebugable() && BRIDGE_LOG_SWITCH) {
Expand Down Expand Up @@ -1694,7 +1697,7 @@ public WXJSObject optionObjConvert(boolean useSandBox, BundType type, WXJSObject
}
return new WXJSObject(WXJSObject.JSON, obj.toString());
} catch (Throwable e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
return opt;

Expand Down Expand Up @@ -1748,7 +1751,7 @@ else if(RAX.equalsIgnoreCase(type)){
}
return BundType.Others;
} catch (Throwable e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
return BundType.Others;
}
}
Expand Down Expand Up @@ -2030,7 +2033,7 @@ private void initFramework(String framework) {
adapter.commit(WXEnvironment.getApplication(),"sJSFMStartListener",IWXUserTrackAdapter.COUNTER,null,params);
}
}catch (Exception e){
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
}

Expand All @@ -2039,15 +2042,15 @@ private void initFramework(String framework) {
try {
crashFile = WXEnvironment.getApplication().getApplicationContext().getCacheDir().getPath();
} catch (Exception e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
boolean pieSupport = true;
try {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
pieSupport = false;
}
} catch (Exception e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
sInitFrameWorkMsg.append(" | pieSupport:").append(pieSupport);
WXLogUtils.d("[WXBridgeManager] initFrameworkEnv crashFile:" + crashFile + " pieSupport:" + pieSupport);
Expand Down Expand Up @@ -2443,7 +2446,7 @@ public void reportJSException(String instanceId, String function,
return;
}
} catch (Exception e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
}
if (METHOD_CREATE_INSTANCE.equals(function) && !instance.getApmForInstance().hasAddView){
Expand Down Expand Up @@ -2489,17 +2492,17 @@ private void doReportJSException(String instanceId, String function,WXErrorCode
exceptionExt = result.toString();
br.close();
} catch (Exception e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
}
file.delete();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(throwable));
}
}
} catch (Throwable e) {
e.printStackTrace();
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
exception += "\n" + exceptionExt;
WXLogUtils.e("reportJSException:" + exception);
Expand Down Expand Up @@ -2948,7 +2951,69 @@ public int callUpdateAttrs(String instanceId, String ref, HashMap<String, String

return IWXBridge.INSTANCE_RENDERING;
}
private boolean shouldReportGPULimit() {
IWXConfigAdapter adapter = WXSDKManager.getInstance().getWxConfigAdapter();
boolean report_gpu_limited_layout = false;
float sample_rate_of_report = 0;
if (adapter != null) {
try {
sample_rate_of_report = Float.parseFloat(adapter
.getConfig("android_weex_test_gpu",
"sample_rate_of_report",
"0"));
}catch(Exception e){
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
WXLogUtils.i("sample_rate_of_report : " + sample_rate_of_report);
if(Math.random() < sample_rate_of_report){
report_gpu_limited_layout = true;
}
}
return report_gpu_limited_layout;
}

private void reportIfReachGPULimit(String instanceId,String ref,GraphicSize layoutSize){
float limit = WXRenderManager.getOpenGLRenderLimitValue();
if(limit > 0 && (layoutSize.getHeight() > limit || layoutSize.getWidth() > limit)){
Map<String, String> ext = new ArrayMap<>();
WXComponent component = WXSDKManager.getInstance().getWXRenderManager().getWXComponent(instanceId,ref);
ext.put("GPU limit",String.valueOf(limit));
ext.put("component.width",String.valueOf(layoutSize.getWidth()));
ext.put("component.height",String.valueOf(layoutSize.getHeight()));
if (component.getComponentType() != null && !component.getComponentType().isEmpty()) {
ext.put("component.type", component.getComponentType());
}
if (component.getStyles() != null && !component.getStyles().isEmpty()) {
ext.put("component.style", component.getStyles().toString());
}
if (component.getAttrs() != null && !component.getAttrs().isEmpty()) {
ext.put("component.attr", component.getAttrs().toString());
}
if (component.getEvents() != null && !component.getEvents().isEmpty()) {
ext.put("component.event", component.getEvents().toString());
}
if (component.getMargin() != null) {
ext.put("component.margin", component.getMargin().toString());
}
if (component.getPadding() != null) {
ext.put("component.padding", component.getPadding().toString());
}
if (component.getBorder() != null) {
ext.put("component.border", component.getBorder().toString());
}
JSONObject map = new JSONObject();
map.putAll(ext);
WXSDKManager.getInstance().getSDKInstance(instanceId).setComponentsInfoExceedGPULimit(map);
if(shouldReportGPULimit()) {
WXExceptionUtils.commitCriticalExceptionRT(instanceId
, WXErrorCode.WX_RENDER_WAR_GPU_LIMIT_LAYOUT,
"WXBridgeManager",
String.format(Locale.ENGLISH, "You are creating a component(%s x %2$s) which exceeds the limit of gpu(%3$s x %3$s),it may cause crash",
String.valueOf(layoutSize.getWidth()), String.valueOf(layoutSize.getHeight()), String.valueOf(limit)),
ext);
}
}
}
public int callLayout(String pageId, String ref, int top, int bottom, int left, int right, int height, int width, boolean isRTL, int index) {

if (TextUtils.isEmpty(pageId) || TextUtils.isEmpty(ref)) {
Expand Down Expand Up @@ -2982,6 +3047,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);
reportIfReachGPULimit(pageId,ref,size);
GraphicActionAddElement addAction = instance.getInActiveAddElementAction(ref);
if(addAction!=null) {
addAction.setRTL(isRTL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public enum WXErrorCode {
WX_RENDER_ERR_INSTANCE_ID_NULL("-9618", "WX_RENDER_ERR_INSTANCE_ID_NULL", ErrorType.NATIVE_ERROR, ErrorGroup.NATIVE),
WX_RENDER_ERR_LIST_INVALID_COLUMN_COUNT("-9619", "WX_RENDER_ERR_LIST_INVALID_COLUMNJ_CONUNT", ErrorType.JS_ERROR, ErrorGroup.JS),
WX_RENDER_ERR_TEXTURE_SETBACKGROUND("-9620", "WX_RENDER_ERR_TEXTURE_SETBACKGROUND", ErrorType.NATIVE_ERROR, ErrorGroup.NATIVE),
WX_RENDER_WAR_GPU_LIMIT_LAYOUT("-9621", "WX_RENDER_WAR_GPU_LIMIT_LAYOUT", ErrorType.JS_ERROR,ErrorGroup.JS),

WX_KEY_EXCEPTION_NO_BUNDLE_TYPE("-9801", "Fatal Error : No bundle type in js bundle head, cause white screen or memory leak!!", ErrorType.JS_ERROR, ErrorGroup.JS),
/**
Expand Down
61 changes: 61 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.taobao.weex.ui;

import android.opengl.GLES10;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import android.support.annotation.RestrictTo.Scope;
Expand All @@ -33,6 +34,7 @@
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.WXLogUtils;
import com.taobao.weex.utils.WXUtils;

import java.util.ArrayList;
Expand All @@ -41,6 +43,14 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;



/**
* Manager class for render operation, mainly for managing {@link RenderContextImpl}.
* This is <strong>not</strong> a thread-safe class
Expand All @@ -54,6 +64,7 @@ public class WXRenderManager {
private final int MAX_DROP_FRAME_NATIVE_BATCH = 2000;
private final static String sKeyAction = "Action";
private static int nativeBatchTimes = 0;
private static int mOpenGLRenderLimitValue = 0;

public WXRenderManager() {
mRenderContext = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -81,6 +92,56 @@ public WXSDKInstance getWXSDKInstance(String instanceId) {
return statement.getWXSDKInstance();
}

public static int getOpenGLRenderLimitValue() {
if(mOpenGLRenderLimitValue == 0){
int maxsize = 0;
try {
EGL10 egl = (EGL10) EGLContext.getEGL();
EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int[] vers = new int[2];
egl.eglInitialize(dpy, vers);
int[] configAttr = {
EGL10.EGL_COLOR_BUFFER_TYPE, EGL10.EGL_RGB_BUFFER,
EGL10.EGL_LEVEL, 0,
EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
EGL10.EGL_NONE};
EGLConfig[] configs = new EGLConfig[1];
int[] numConfig = new int[1];
egl.eglChooseConfig(dpy, configAttr, configs, 1, numConfig);
if(numConfig[0] == 0){
//There is something wrong with opengl environment.
maxsize = -1;
egl.eglTerminate(dpy);
}else {
EGLConfig config = configs[0];
int[] surfAttr = {
EGL10.EGL_WIDTH, 64,
EGL10.EGL_HEIGHT, 64,
EGL10.EGL_NONE};
EGLSurface surf = egl.eglCreatePbufferSurface(dpy, config, surfAttr);
final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;// missing in EGL10
int[] ctxAttrib = {
EGL_CONTEXT_CLIENT_VERSION, 1,
EGL10.EGL_NONE};
EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, ctxAttrib);
egl.eglMakeCurrent(dpy, surf, surf, ctx);
int[] maxSize = new int[1];
GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
egl.eglMakeCurrent(dpy, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT);
egl.eglDestroySurface(dpy, surf);
egl.eglDestroyContext(dpy, ctx);
egl.eglTerminate(dpy);
maxsize = maxSize[0];
}
} catch(Exception e){
WXLogUtils.e(WXLogUtils.getStackTrace(e));
}
mOpenGLRenderLimitValue = maxsize;
}
return mOpenGLRenderLimitValue;
}

@RestrictTo(Scope.LIBRARY)
public void postOnUiThread(Runnable runnable, long delayMillis) {
mWXRenderHandler.postDelayed(WXThread.secure(runnable), delayMillis);
Expand Down
Loading

0 comments on commit 48a7cb7

Please sign in to comment.