Skip to content

Commit

Permalink
feat(core): add snapshot for hippy
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang authored and zoomchan-cxj committed Mar 16, 2023
1 parent cb8a77c commit 9292a73
Show file tree
Hide file tree
Showing 32 changed files with 1,057 additions and 207 deletions.
15 changes: 14 additions & 1 deletion android/sdk/src/main/java/com/tencent/mtt/hippy/HippyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.tencent.mtt.hippy.utils.UIThreadUtils;
import com.tencent.mtt.hippy.adapter.thirdparty.HippyThirdPartyAdapter;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -177,6 +178,9 @@ public boolean isDevMode() {

public abstract HippyRootView loadModule(ModuleLoadParams loadParams, ModuleListener listener);

public abstract HippyRootView loadInstance(ModuleLoadParams loadParams, ModuleListener listener,
HippyRootView.OnLoadCompleteListener onLoadCompleteListener);

@SuppressWarnings("unused")
public abstract HippyRootView loadModule(ModuleLoadParams loadParams, ModuleListener listener,
HippyRootView.OnLoadCompleteListener onLoadCompleteListener);
Expand Down Expand Up @@ -238,9 +242,16 @@ public enum DebugMode {
UserLocal, // open Hippy remote debug in production. just like debugMode=None but use local bundle, not from remote server
}

public enum V8SnapshotType {
NoSnapshot, CreateSnapshot, UseSnapshot
}

public static class V8InitParams {
public long initialHeapSize;
public long maximumHeapSize;
public int type;
public String uri; // blob_uri, Currently only supports the file protocol
public ByteBuffer blob;
}

// Hippy 引擎初始化时的参数设置
Expand Down Expand Up @@ -353,7 +364,7 @@ protected void check() {
providers = new ArrayList<>();
}
providers.add(0, new HippyCoreAPI());
if (debugMode != DebugMode.Dev) {
if (debugMode != DebugMode.Dev && v8InitParams.type == V8SnapshotType.NoSnapshot.ordinal()) {
if (TextUtils.isEmpty(coreJSAssetsPath) && TextUtils.isEmpty(coreJSFilePath)) {
throw new RuntimeException(
"Hippy: debugMode=true, initParams.coreJSAssetsPath and coreJSFilePath both null!");
Expand Down Expand Up @@ -481,6 +492,8 @@ public interface EngineListener {
@SuppressWarnings("unused")
public interface ModuleListener {

void onLoadCompletedInCurrentThread(ModuleLoadStatus statusCode, String msg, HippyRootView hippyRootView);

void onLoadCompleted(ModuleLoadStatus statusCode, String msg, HippyRootView hippyRootView);

@SuppressWarnings("SameReturnValue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,83 @@ public HippyRootView loadModule(ModuleLoadParams loadParams, ModuleListener list
return loadModule(loadParams, listener, null);
}

@Override
public HippyRootView loadInstance(ModuleLoadParams loadParams, ModuleListener listener,
HippyRootView.OnLoadCompleteListener onLoadCompleteListener) {
if (loadParams == null) {
throw new RuntimeException("Hippy: loadModule loadParams must no be null");
}
if (loadParams.context == null) {
throw new RuntimeException("Hippy: loadModule loadParams.context must no be null");
}
if (!isDebugMode() && TextUtils.isEmpty(loadParams.jsAssetsPath) && TextUtils
.isEmpty(loadParams.jsFilePath) && v8InitParams.type == V8SnapshotType.NoSnapshot.ordinal()) {
throw new RuntimeException(
"Hippy: loadModule debugMode=true, loadParams.jsAssetsPath and jsFilePath both null!");
}
if (mEngineContext != null) {
mEngineContext.setComponentName(loadParams.componentName);
mEngineContext.setNativeParams(loadParams.nativeParams);
}
if (loadParams.jsParams == null) {
loadParams.jsParams = new HippyMap();
}
if (loadParams.hippyContext != null) {
loadParams.hippyContext.setModuleParams(loadParams);
}
if (!TextUtils.isEmpty(loadParams.jsAssetsPath)) {
loadParams.jsParams.pushString("sourcePath", loadParams.jsAssetsPath);
} else {
loadParams.jsParams.pushString("sourcePath", loadParams.jsFilePath);
}
mModuleListener = listener;

HippyRootView view = new HippyRootView(loadParams);

if (mCurrentState == EngineState.DESTROYED) {
notifyModuleLoaded(ModuleLoadStatus.STATUS_ENGINE_UNINIT,
"load module error wrong state, Engine destroyed", view);
return view;
}
if (onLoadCompleteListener != null) {
view.setOnLoadCompleteListener(onLoadCompleteListener);
}
view.setTimeMonitor(new TimeMonitor(!isDebugMode()));
view.getTimeMonitor().begine();
view.getTimeMonitor().startEvent(HippyEngineMonitorEvent.MODULE_LOAD_EVENT_WAIT_ENGINE);
view.setOnResumeAndPauseListener(this);
view.setOnSizeChangedListener(this);
view.attachEngineManager(this);
mInstances.add(view);
mDevSupportManager.attachToHost(view);
if (!mDevManagerInited && isDebugMode()) {
mDevManagerInited = true;
}

LogUtils.d(TAG, "internalLoadInstance start...");
if (mCurrentState == EngineState.INITED) {
LogUtils.d(TAG, "in internalLoadInstance");
if (mEngineContext.mInstanceLifecycleEventListeners != null) {
for (HippyInstanceLifecycleEventListener lifecycleEventListener : mEngineContext.mInstanceLifecycleEventListeners) {
lifecycleEventListener.onInstanceLoad(view.getId());
}
}
view.attachToEngine(mEngineContext);
HippyMap launchParams = view.getLaunchParams();
HippyBundleLoader loader = ((HippyInstanceContext) view.getContext()).getBundleLoader();
LogUtils.d(TAG, "in internalLoadInstance before loadInstance");
mEngineContext.getBridgeManager().loadInstance(view.getName(), view.getId(), launchParams);
if (isDebugMode()) {
notifyModuleLoaded(ModuleLoadStatus.STATUS_OK, null, view);
}
} else {
notifyModuleLoaded(ModuleLoadStatus.STATUS_ENGINE_UNINIT,
"error wrong state, Engine state not INITED, state:" + mCurrentState, view);
}

return view;
}

@Override
public HippyRootView loadModule(ModuleLoadParams loadParams, ModuleListener listener,
HippyRootView.OnLoadCompleteListener onLoadCompleteListener) {
Expand All @@ -257,8 +334,6 @@ public HippyRootView loadModule(ModuleLoadParams loadParams, ModuleListener list
}
if (!isDevMode() && TextUtils.isEmpty(loadParams.jsAssetsPath) && TextUtils
.isEmpty(loadParams.jsFilePath)) {
throw new RuntimeException(
"Hippy: loadModule debugMode=true, loadParams.jsAssetsPath and jsFilePath both null!");
}
if (mEngineContext != null) {
mEngineContext.setComponentName(loadParams.componentName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.common.Callback;
import com.tencent.mtt.hippy.common.HippyArray;
import com.tencent.mtt.hippy.common.HippyMap;
import com.tencent.mtt.hippy.devsupport.DebugWebSocketClient;
import com.tencent.mtt.hippy.devsupport.DevRemoteDebugProxy;
import com.tencent.mtt.hippy.devsupport.DevServerCallBack;
Expand All @@ -41,6 +42,7 @@
import com.tencent.mtt.hippy.utils.ArgumentUtils;
import com.tencent.mtt.hippy.utils.FileUtils;
import com.tencent.mtt.hippy.utils.LogUtils;
import com.tencent.mtt.hippy.utils.DimensionsUtil;
import com.tencent.mtt.hippy.utils.UIThreadUtils;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -113,6 +115,17 @@ public HippyBridgeImpl(HippyEngineContext engineContext, BridgeCallback callback
}
}

public static int createSnapshotFromScript(String[] script, String uri, Context context) {
HippyMap globalParams = new HippyMap();
assert (context != null);
HippyMap dimensionMap = DimensionsUtil.getDimensions(-1, -1, context, false);
globalParams.pushMap("Dimensions", dimensionMap);
HippyMap platformParams = new HippyMap();
platformParams.pushString("OS", "android");
globalParams.pushMap("Platform", platformParams);
return createSnapshot(script, uri, ArgumentUtils.objectToJson(globalParams));
};

@Override
public void initJSBridge(String globalConfig, final NativeCallback callback, final int groupId) {
mDebugGlobalConfig = globalConfig;
Expand Down Expand Up @@ -314,6 +327,8 @@ public void runInJsThread(Callback<Void> callback) {
runInJsThread(mV8RuntimeId, callback);
}

public static native int createSnapshot(String[] script, String uri, String config);

public native long initJSFramework(byte[] gobalConfig, boolean useLowMemoryMode,
boolean enableV8Serialization, boolean isDevModule, NativeCallback callback,
long groupId, V8InitParams v8InitParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.tencent.mtt.hippy.common.HippyJsException;
import com.tencent.mtt.hippy.common.HippyMap;

import java.nio.ByteBuffer;

@SuppressWarnings({"deprecation", "unused"})
public interface HippyBridgeManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ public void run() {

private void notifyModuleLoaded(final ModuleLoadStatus statusCode, final String msg,
final HippyRootView hippyRootView) {
mLoadModuleListener.onLoadCompletedInCurrentThread(statusCode, msg, hippyRootView);
Runnable action = new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,17 @@ public static int getNavigationBarHeight(Context context) {
}

public static int getStatusBarHeight() {
Context context = ContextHolder.getAppContext();
return getStatusBarHeight(ContextHolder.getAppContext());
}

public static int getStatusBarHeight(Context context) {
assert context != null;
if (STATUS_BAR_HEIGHT > 0) {
return STATUS_BAR_HEIGHT;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowManager wm = (WindowManager) ContextHolder.getAppContext()
.getSystemService(Context.WINDOW_SERVICE);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowMetrics windowMetrics = wm.getCurrentWindowMetrics();
WindowInsets windowInsets = windowMetrics.getWindowInsets();
Insets insets = windowInsets
Expand Down Expand Up @@ -201,7 +203,7 @@ public static HippyMap getDimensions(int windowWidth, int windowHeight, Context

// construct param
HippyMap dimensionMap = new HippyMap();
getStatusBarHeight();
getStatusBarHeight(context);
int navigationBarHeight = getNavigationBarHeight(context);
HippyMap windowDisplayMetricsMap = new HippyMap();

Expand Down
7 changes: 7 additions & 0 deletions android/sdk/src/main/jni/include/bridge/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ namespace bridge {

void setNativeLogHandler(JNIEnv* j_env, __unused jobject j_object, jobject j_logger);

jint CreateSnapshot(JNIEnv* j_env,
__unused jobject j_obj,
jobjectArray j_script_array,
jstring j_snapshot_uri,
jstring j_config);

jlong InitInstance(JNIEnv* j_env,
jobject j_object,
jbyteArray j_global_config,
Expand Down Expand Up @@ -62,5 +68,6 @@ void RunInJsThread(JNIEnv *j_env,
jlong j_runtime_id,
jobject j_callback);


} // namespace bridge
} // namespace hippy

0 comments on commit 9292a73

Please sign in to comment.