Skip to content

Commit

Permalink
feat(voltron): add voltron_screen_info to make flutter_module faster (#…
Browse files Browse the repository at this point in the history
…3541)

* feat(voltron): add voltron_screen_info to make flutter_module faster

* feat(voltron): adapt xcode 14 for voltron ios native demo

* feat(voltron): add voltron changelog

* feat(voltron): voltron_renderer adapt unit test

* feat(voltron): add code owner for voltron-demo

* fix(voltron): remove unnecessary import
  • Loading branch information
henryjin0511 authored Nov 14, 2023
1 parent 9d006bc commit 5abd5ad
Show file tree
Hide file tree
Showing 23 changed files with 206 additions and 79 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ CMakeLists.txt @ilikethese @etkmao
/framework/examples/android-demo/res/ @zealotchen0
/framework/examples/ios-demo/ @wwwcg @ruifanyuan
/framework/examples/ios-demo/res/ @zealotchen0
/framework/examples/voltron-demo/ @henryjin0511

# doc: pages
/*.md @zealotchen0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 56;
objectVersion = 55;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -229,7 +229,7 @@
};
};
buildConfigurationList = C2DC03F32A3EE271004B7147 /* Build configuration list for PBXProject "IOSProj" */;
compatibilityVersion = "Xcode 14.0";
compatibilityVersion = "Xcode 13.0";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,35 @@
import android.view.View;
import android.widget.Button;

import java.util.concurrent.atomic.AtomicLong;

import io.flutter.Log;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;

public class MainActivity extends AppCompatActivity {
private final AtomicLong idCounter = new AtomicLong(0);
private long currentEngineId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNewEngine();

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openWithNewEngine();
}
});
}

void createNewEngine() {
currentEngineId = idCounter.incrementAndGet();

// Instantiate a FlutterEngine.
FlutterEngine flutterEngine = new FlutterEngine(this);
Expand All @@ -45,16 +63,16 @@ protected void onCreate(Bundle savedInstanceState) {
// Cache the FlutterEngine to be used by FlutterActivity.
FlutterEngineCache
.getInstance()
.put("my_engine_id", flutterEngine);
.put("my_engine_id_" + currentEngineId, flutterEngine);
}

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(
FlutterActivity.withCachedEngine("my_engine_id").destroyEngineWithActivity(true).build(MainActivity.this)
);
}
});
void openWithNewEngine() {
startActivity(
FlutterActivity
.withCachedEngine("my_engine_id_" + currentEngineId)
.destroyEngineWithActivity(true)
.build(MainActivity.this)
);
createNewEngine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class _BaseVoltronPageState extends State<BaseVoltronPage> {
initParams.coreJSAssetsPath = _coreBundle;
initParams.codeCacheTag = "common";
}
initParams.integratedMode = IntegratedMode.flutterModule;
initParams.providers = [
MyAPIProvider(),
];
Expand Down
3 changes: 1 addition & 2 deletions framework/examples/voltron-demo/flutter_module/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ environment:
dependencies:
flutter:
sdk: flutter
voltron:
path: ../../../voltron
voltron: 0.0.37
qr_flutter: 4.0.0

# The following adds the Cupertino Icons font to your application.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class _BaseVoltronPageState extends State<BaseVoltronPage> {
initParams.coreJSAssetsPath = _coreBundle;
initParams.codeCacheTag = "common";
}
initParams.integratedMode = IntegratedMode.flutterApp;
initParams.providers = [
MyAPIProvider(),
];
Expand Down
3 changes: 1 addition & 2 deletions framework/examples/voltron-demo/flutter_proj/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ dev_dependencies:
flutter_test:
sdk: flutter

voltron: ^0.0.29

voltron: 0.0.37
qr_flutter: 4.0.0

# The "flutter_lints" package below contains a set of recommended lints to
Expand Down
12 changes: 12 additions & 0 deletions framework/voltron/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 0.0.37

- resolve merge conflicts

## 0.0.36

- update ffi_manager

## 0.0.35

- update voltron_render to 0.0.25 to make flutter module faster

## 0.0.34

- support voltron dynamic library load
Expand Down
18 changes: 12 additions & 6 deletions framework/voltron/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ PODS:
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
- sqflite (0.0.2):
- sqflite (0.0.3):
- Flutter
- FMDB (>= 2.7.5)
- voltron (0.0.1):
- Flutter
- voltron_screen_info (0.0.1):
- Flutter
- webview_cookie_manager (0.0.1):
- Flutter
- webview_flutter_wkwebview (0.0.1):
Expand All @@ -42,6 +44,7 @@ DEPENDENCIES:
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/ios`)
- sqflite (from `.symlinks/plugins/sqflite/ios`)
- voltron (from `.symlinks/plugins/voltron/ios`)
- voltron_screen_info (from `.symlinks/plugins/voltron_screen_info/ios`)
- webview_cookie_manager (from `.symlinks/plugins/webview_cookie_manager/ios`)
- webview_flutter_wkwebview (from `.symlinks/plugins/webview_flutter_wkwebview/ios`)

Expand Down Expand Up @@ -71,6 +74,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/sqflite/ios"
voltron:
:path: ".symlinks/plugins/voltron/ios"
voltron_screen_info:
:path: ".symlinks/plugins/voltron_screen_info/ios"
webview_cookie_manager:
:path: ".symlinks/plugins/webview_cookie_manager/ios"
webview_flutter_wkwebview:
Expand All @@ -84,14 +89,15 @@ SPEC CHECKSUMS:
gradient_like_css: 5bf90b38d326b3883145898d762735ca07d17991
keyboard_utils: ab24bc711be9e91a5937c20489056b8dd650fecc
package_info_plus: fd030dabf36271f146f1f3beacd48f564b0f17f7
path_provider_foundation: c68054786f1b4f3343858c1e1d0caaded73f0be9
path_provider_foundation: eaf5b3e458fc0e5fbb9940fb09980e853fe058b8
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
shared_preferences_foundation: e2dae3258e06f44cc55f49d42024fd8dd03c590c
sqflite: 6d358c025f5b867b29ed92fc697fd34924e11904
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126
sqflite: 31f7eba61e3074736dff8807a9b41581e4f7f15a
voltron: 573e6755e966a450ea768ab1074d237ad0f41d82
voltron_screen_info: 3f0614af8cdf6aab0b174854a2c8a29fb58316d5
webview_cookie_manager: eaf920722b493bd0f7611b5484771ca53fed03f7
webview_flutter_wkwebview: 2e2d318f21a5e036e2c3f26171342e95908bd60a

PODFILE CHECKSUM: b4c93064582ae64fa0d6a91b4d9b0c383edd9a76
PODFILE CHECKSUM: 9c6f6e76df8eb7d6e4cc1dbb5dae28135d6b4305

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
2 changes: 2 additions & 0 deletions framework/voltron/example/lib/base_voltron_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class _BaseVoltronPageState extends State<BaseVoltronPage> {
initParams.coreJSAssetsPath = _coreBundle;
initParams.codeCacheTag = "common";
}
// 这里可以不传,默认就是flutterApp,如果是使用flutter module,建议这里使用flutterModule以获取更快的启动速度
initParams.integratedMode = IntegratedMode.flutterApp;
initParams.providers = [
MyAPIProvider(),
];
Expand Down
8 changes: 8 additions & 0 deletions framework/voltron/lib/engine/engine_define.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ enum EngineMode {
singleThread,
}

// IntegratedMode 集成模式
// flutterApp 如果开发的是flutter app,推荐这种方式
// flutterModule 如果开发的是native app + flutter module,推荐这种方式,这种模式下会通过method channel获取屏幕参数,更快
enum IntegratedMode {
flutterApp,
flutterModule,
}

/// 引擎初始化过程中的错误码,对于Voltron sdk开发者调查Voltron sdk的使用者在使用过程中遇到的问题,很必须。
enum EngineInitStatus {
ok, // 初始化过程,一切正常
Expand Down
4 changes: 3 additions & 1 deletion framework/voltron/lib/engine/js_engine_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class EngineContext with RenderContextProxy {
VoltronBundleLoader? coreLoader,
int bridgeType,
bool isDevModule,
IntegratedMode integratedMode,
String serverHost,
int groupId,
VoltronThirdPartyAdapter? thirdPartyAdapter,
Expand Down Expand Up @@ -155,7 +156,8 @@ class EngineContext with RenderContextProxy {

void _initVfsManager() {
_vfsManager = VfsManager();
DefaultProcessor processor = DefaultProcessor(VoltronResourceLoader(_globalConfigs.httpAdapter));
DefaultProcessor processor =
DefaultProcessor(VoltronResourceLoader(_globalConfigs.httpAdapter));
_vfsManager.addProcessor(processor);
}

Expand Down
3 changes: 3 additions & 0 deletions framework/voltron/lib/engine/js_init_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class EngineInitParams {
// 可选参数 引擎模式 默认为NORMAL
EngineMode engineMode = EngineMode.normal;

// 可选参数
IntegratedMode integratedMode = IntegratedMode.flutterApp;

// 可选参数 自定义的,用来提供Native modules、JavaScript modules、View controllers的管理器。1个或多个
List<APIProvider>? providers;

Expand Down
34 changes: 28 additions & 6 deletions framework/voltron/lib/engine/voltron_js_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class VoltronJSEngine implements OnResumeAndPauseListener, DevServerCallback {
// 从网络上加载jsbundle
late bool _debugMode;

late IntegratedMode _integratedMode;

// Server的jsbundle名字,调试模式下有效
late String _serverBundleName;

Expand Down Expand Up @@ -125,17 +127,20 @@ class VoltronJSEngine implements OnResumeAndPauseListener, DevServerCallback {
}

try {
LogUtils.d(_kTag, "init ffi function binding start");
_initBridge();
LogUtils.d(_kTag, "init ffi function binding done");
} catch (e) {
_currentState = EngineState.initError;
if (e is Error) {
LogUtils.e(_kTag, "${e.stackTrace}");
LogUtils.e(_kTag, "init ffi function binding fail, error: ${e.stackTrace}");
}
}
LogUtils.d(_kTag, "initEngine initBridge done");

_id = VoltronApi.getVoltronEngineIndex();

LogUtils.d(_kTag, "get voltron engine index: ${_id}");

CookieManager.getInstance().setCookieDelegate(
params.cookieDelegateType,
originDelegate: params.originDelegate,
Expand All @@ -147,6 +152,7 @@ class VoltronJSEngine implements OnResumeAndPauseListener, DevServerCallback {
_preloadBundleLoader = preloadBundleLoader;
_apiProviders = params.providers;
_debugMode = params.debugMode;
_integratedMode = params.integratedMode;
_serverBundleName = params.debugMode ? params.debugBundleName : "";
_startTimeMonitor = TimeMonitor(true);
_engineMonitor = params.engineMonitor!;
Expand All @@ -158,17 +164,32 @@ class VoltronJSEngine implements OnResumeAndPauseListener, DevServerCallback {

Future<dynamic> initEngine(EngineListener listener) async {
_startTimeMonitor.startEvent(EngineMonitorEventKey.engineLoadEventInitEngine);
try {
// 初始化UI宽高信息, 必须放到第一位,否则可能run app之后没有UI界面宽高信息
LogUtils.d(_kTag, "init screen info start");
await ScreenUtil.getInstance().initScreen(
screenInfoSource: _integratedMode == IntegratedMode.flutterApp
? ScreenInfoSource.flutter
: ScreenInfoSource.native);
LogUtils.d(_kTag, "init screen info done");
} catch (e) {
_currentState = EngineState.initError;
if (e is Error) {
LogUtils.e(_kTag, "init screen info fail, error:${e.stackTrace}");
}
}

try {
// 初始化平台相关信息和UI宽高信息, 必须放到第一位,否则可能run app之后平台信息还未初始化完成,或者没有UI界面宽高信息
await ScreenUtil.getInstance().ensurePhysicalSizeReady();
LogUtils.d(_kTag, "init platform info start");
await PlatformManager.getInstance().initPlatform();
LogUtils.d(_kTag, "init platform info done");
} catch (e) {
_currentState = EngineState.initError;
if (e is Error) {
LogUtils.e(_kTag, "${e.stackTrace}");
LogUtils.e(_kTag, "init platform info fail, error:${e.stackTrace}");
}
}
LogUtils.d(_kTag, "initEngine getPlatform done");

if (_currentState != EngineState.unInit) {
_listen(listener);
Expand Down Expand Up @@ -294,6 +315,7 @@ class VoltronJSEngine implements OnResumeAndPauseListener, DevServerCallback {
_coreBundleLoader,
bridgeType,
_debugMode,
_integratedMode,
_serverHost,
_groupId,
_thirdPartyAdapter,
Expand Down Expand Up @@ -326,6 +348,7 @@ class VoltronJSEngine implements OnResumeAndPauseListener, DevServerCallback {
var state = _currentState;
_currentState = param ? EngineState.inited : EngineState.initError;
if (state != EngineState.onRestart) {
LogUtils.d(_kTag, "restartEngineInBackground ok");
_notifyEngineInitialized(param ? EngineInitStatus.ok : EngineInitStatus.errBridge, e);
} else {
LogUtils.e(_kTag, "initBridge callback error STATUS_WRONG_STATE, state=$_currentState");
Expand Down Expand Up @@ -511,7 +534,6 @@ class VoltronJSEngine implements OnResumeAndPauseListener, DevServerCallback {

static void _initBridge() {
if (!_hasInit) {
LogUtils.d(_kTag, "_initBridge");
VoltronApi.initBridge();
_hasInit = true;
}
Expand Down
3 changes: 0 additions & 3 deletions framework/voltron/lib/voltron/loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
//

import 'package:voltron/voltron.dart';
import 'package:voltron_renderer/voltron_renderer.dart';

import '../engine.dart';

typedef ModuleExecutor = Function();

Expand Down
4 changes: 2 additions & 2 deletions framework/voltron/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

name: voltron
description: A flutter plugin project to render hippy page.
version: 0.0.36
version: 0.0.37
homepage: https://hippyjs.org
repository: https://github.com/Tencent/Hippy

Expand Down Expand Up @@ -60,7 +60,7 @@ dependencies:
connectivity_plus: ^3.0.6
uuid: ^3.0.6
ffi: ^2.0.0
voltron_renderer: 0.0.26
voltron_renderer: 0.0.27
voltron_vfs: 0.0.7
voltron_ffi: 0.0.6

Expand Down
8 changes: 8 additions & 0 deletions renderer/voltron/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
## 0.0.27

- resolve merge conflicts

## 0.0.26

- update ffi_manager to 0.0.6

## 0.0.25

- add voltron_screen_info plugin

## 0.0.24

- fix width calculate error and add modal navigator param
Expand Down
2 changes: 1 addition & 1 deletion renderer/voltron/lib/bridge/render_bridge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class VoltronRenderBridgeManager implements Destroyable {
}

int createNativeRenderManager() {
return VoltronRenderApi.createNativeRender(ScreenUtil.getInstance().screenDensity);
return VoltronRenderApi.createNativeRender(ScreenUtil.getInstance().scale);
}

Future destroyNativeRenderManager() async {
Expand Down
Loading

0 comments on commit 5abd5ad

Please sign in to comment.