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

[Android] solve components exceed gpu limit problem #2603

Merged
merged 15 commits into from
Jul 1, 2019
10 changes: 9 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 @@ -152,6 +152,7 @@ public class WXSDKInstance implements IWXActivityStateListener,View.OnLayoutChan
private Map<String,String> mContainerInfo;

public boolean isNewFsEnd = false;
private List<Map<String,String>> componentsInfoExceedGPULimit = new ArrayList<>();

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

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

public List<String> getLayerOverFlowListeners() {
return mLayerOverFlowListeners;
Expand Down Expand Up @@ -827,7 +835,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 @@ -2948,7 +2951,67 @@ 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){
e.printStackTrace();
YorkShen marked this conversation as resolved.
Show resolved Hide resolved
}
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<>();
YorkShen marked this conversation as resolved.
Show resolved Hide resolved
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());
}
WXSDKManager.getInstance().getSDKInstance(instanceId).setComponentsInfoExceedGPULimit(ext);
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 +3045,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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
*/
package com.taobao.weex.ui;

import android.opengl.EGL14;
import android.opengl.EGLConfig;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.opengl.EGLSurface;
import android.opengl.GLES10;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import android.support.annotation.RestrictTo.Scope;
Expand All @@ -33,6 +40,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 +49,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
* Manager class for render operation, mainly for managing {@link RenderContextImpl}.
* This is <strong>not</strong> a thread-safe class
Expand All @@ -54,6 +63,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 +91,58 @@ public WXSDKInstance getWXSDKInstance(String instanceId) {
return statement.getWXSDKInstance();
}

public static int getOpenGLRenderLimitValue() {
if(mOpenGLRenderLimitValue == 0){
int maxsize = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
EGLDisplay dpy = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
int[] vers = new int[2];
EGL14.eglInitialize(dpy, vers, 0, vers, 1);
int[] configAttr = {
EGL14.EGL_BUFFER_SIZE, 32,
EGL14.EGL_ALPHA_SIZE, 8,
EGL14.EGL_BLUE_SIZE, 8,
EGL14.EGL_GREEN_SIZE, 8,
EGL14.EGL_RED_SIZE, 8,
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT,
EGL14.EGL_NONE};
EGLConfig[] configs = new EGLConfig[1];
int[] numConfig = new int[1];
EGL14.eglChooseConfig(dpy, configAttr, 0, configs, 0, configs.length, numConfig, 0);
YorkShen marked this conversation as resolved.
Show resolved Hide resolved
EGLConfig config = configs[0];
int[] surfAttr = {
EGL14.EGL_WIDTH, 64,
EGL14.EGL_HEIGHT, 64,
EGL14.EGL_NONE};
EGLSurface surf = EGL14.eglCreatePbufferSurface(dpy, config, surfAttr, 0);
int[] ctxAttrib = {
EGL14.EGL_CONTEXT_CLIENT_VERSION, 1,
EGL14.EGL_NONE};
EGLContext ctx = EGL14.eglCreateContext(dpy, config, EGL14.EGL_NO_CONTEXT, ctxAttrib, 0);
EGL14.eglMakeCurrent(dpy, surf, surf, ctx);
int[] maxSize = new int[1];
GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
YorkShen marked this conversation as resolved.
Show resolved Hide resolved
EGL14.eglMakeCurrent(dpy, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE,
EGL14.EGL_NO_CONTEXT);
EGL14.eglDestroySurface(dpy, surf);
EGL14.eglDestroyContext(dpy, ctx);
EGL14.eglTerminate(dpy);
maxsize = maxSize[0];
} catch(Exception e){
e.printStackTrace();
}
} else {
int[] maxSize = new int[1];
GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
maxsize = maxSize[0];
}
mOpenGLRenderLimitValue = maxsize;
YorkShen marked this conversation as resolved.
Show resolved Hide resolved
}
return mOpenGLRenderLimitValue;
}

@RestrictTo(Scope.LIBRARY)
public void postOnUiThread(Runnable runnable, long delayMillis) {
mWXRenderHandler.postDelayed(WXThread.secure(runnable), delayMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.WXSDKManager;
import com.taobao.weex.adapter.IWXAccessibilityRoleAdapter;
import com.taobao.weex.adapter.IWXConfigAdapter;
import com.taobao.weex.bridge.EventResult;
import com.taobao.weex.bridge.Invoker;
import com.taobao.weex.bridge.WXBridgeManager;
Expand All @@ -78,6 +79,7 @@
import com.taobao.weex.tracing.Stopwatch;
import com.taobao.weex.tracing.WXTracing;
import com.taobao.weex.ui.IFComponentHolder;
import com.taobao.weex.ui.WXRenderManager;
import com.taobao.weex.ui.action.BasicComponentData;
import com.taobao.weex.ui.action.GraphicActionAnimation;
import com.taobao.weex.ui.action.GraphicActionUpdateStyle;
Expand Down Expand Up @@ -1682,12 +1684,33 @@ public void setBackgroundImage(@NonNull String bgImage) {
getOrCreateBorder().setImage(shader);
}
}
private boolean shouldCancelHardwareAccelerate() {
IWXConfigAdapter adapter = WXSDKManager.getInstance().getWxConfigAdapter();
boolean cancel_hardware_accelerate = false;
if (adapter != null) {
try {
cancel_hardware_accelerate = Boolean.parseBoolean(adapter
.getConfig("android_weex_test_gpu",
"cancel_hardware_accelerate",
"false"));
}catch (Exception e){
e.printStackTrace();
YorkShen marked this conversation as resolved.
Show resolved Hide resolved
}
WXLogUtils.i("cancel_hardware_accelerate : " + cancel_hardware_accelerate);
}
return cancel_hardware_accelerate;
}

public void setOpacity(float opacity) {
if (opacity >= 0 && opacity <= 1 && mHost.getAlpha() != opacity) {
int limit = WXRenderManager.getOpenGLRenderLimitValue();
if (isLayerTypeEnabled()) {
mHost.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
if(shouldCancelHardwareAccelerate() && limit > 0 && (getLayoutHeight() > limit ||
getLayoutWidth() > limit)){
mHost.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
YorkShen marked this conversation as resolved.
Show resolved Hide resolved
}
mHost.setAlpha(opacity);
}
}
Expand Down