Skip to content

feat(share): integrate native Rust rendering runtime - #679

Open
YinCheng0106 wants to merge 1 commit into
mainfrom
feat/native-share-rendering
Open

YinCheng0106 wants to merge 1 commit into
mainfrom
feat/native-share-rendering

Conversation

@YinCheng0106

@YinCheng0106 YinCheng0106 commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

這個 PR 做了什麼

  • 加入 ReportPicture 服務。在建立 renderer 前檢查 ABI 1.2 與 v2/16:9 能力。將原始 UTF-8 交給 Rust 驗證和渲染,回傳 Dart 持有的 PNG 或結構化錯誤。
  • 初始化與渲染在背景 isolate 執行。同一個 renderer 依序處理請求,dispose() 等待已接受的請求完成後釋放 handle。
  • 新增可重現的原生建置與封裝。iOS 靜態 XCFramework 包含裝置 arm64 和 Simulator arm64/x86_64,Runner 保留九個匯出供 DynamicLibrary.process() 查找。Android APK 封裝 arm64-v8a 與 x86_64 的 libeq_report_pic.so,沿用 DPIP 的 ABI 篩選。
  • 渲染服務目前由開發測試入口呼叫。地震報告詳情與系統分享介面留到下一階段。

相關 issue

  • Rust 與 Dart 綁定的來源是已合併的 eq-report-pic #12,固定為 b135d02bd1ba5103cd3d116a951b1b63a710a312。
  • 沿用 #677 的載入設計,從 DPIP main 獨立建置。#674 是直式分享卡的相關工作。

怎麼驗

檢查 結果
原生重建 Rust 1.99.1、Xcode 27.2、Android NDK 28.2.13676358。Cargo 使用固定提交的離線 vendor 封存檔。兩次重建的四份函式庫 SHA-256 一致。腳本輸出來源、工具鏈與每件產物的 SHA-256。
iOS iOS 27.2 ARM64 模擬器經 tool/run.sh 通過端到端、缺庫回傳 unavailable 和一般 DPIP 啟動。未簽署的裝置 Release 及模擬器 Debug 封裝各保留九個匯出。RunnerTests 421 項通過。
Android Android 17/API 37 ARM64 模擬器在 16 KiB 頁面且無對外路由時通過端到端。ABI 1.1 回傳 unsupportedAbi。原生探針確認 3 個 PNG 緩衝區與 2 個 renderer 都已釋放。Debug/Release APK 的兩種 ABI 均通過九個符號、ELF 對齊及 zipalign -P 16 檢查。一般 DPIP Release 已啟動。
PNG 兩平台均回報 ABI 1.2、能力 0x11F,成功解碼為 1920×1080。相同輸入的 PNG 共 195,390 bytes,SHA-256 為 d1a2080c35193928026c05e8aef2eb6fe4ce28c24a9a8bf0fd4b2b0f3c4cae5d。驗證連續請求、無效 JSON/版本、失敗後重試、釋放後解碼與重新開啟。
回歸 tool/check.sh 通過。Flutter 分析無問題,3,837 項測試通過、11 項跳過。 Android lint 通過。Android JVM 測試以 2 GiB 的本機測試 heap 重跑,620 項通過。

已知限制

  • 標準 CI 建置可省略選用的原生產物。啟用時從固定來源重建,保留字型與 Rust 相依授權。公開散布原生產物前仍須確認內嵌底圖及資產權利。
  • iOS/Android 實機、iOS Intel Simulator 及 Android x86_64 的執行驗收尚待進行。DPIP 的 Android 發布架構為 arm64-v8a 與 x86_64。

檢查清單

  • tool/check/commits.sh origin/main..HEAD 通過。
  • 格式、分析、codegen 與 Flutter 全套回歸通過。
  • iOS/Android ARM64 模擬環境的離線 fixture 通過。
  • 建置與測試用產物保存在本機,提交包含來源、測試與授權資訊。

New(zh-Hant): 加入地震報告橫式分享圖的原生渲染基礎
New(en-US): Add native rendering support for landscape earthquake report images

Platform: all
Copilot AI balanced review requested due to automatic review settings November 21, 2026 18:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🔍 OpenCodeReview found 6 issue(s) in this PR.

  • ✅ Successfully posted inline: 5 comment(s)
  • 📝 In summary (no line info): 1 comment(s)

maintainability · low

📄 tool/dev/build_report_picture.sh

⚠️ GitHub could not post this as an inline comment: No line information provided

建置腳本中硬編碼了特定的 Android NDK 版本與 Git revision。雖然在開發工具中常見,但若未來環境升級或需要測試不同版本,建議考慮改為可配置的環境變數或參數。

💡 Suggested Change

Before:

ndk_version=28.2.13676358
revision=b135d02bd1ba5103cd3d116a951b1b63a710a312

After:

ndk_version="${EQ_REPORT_PIC_NDK_VERSION:-28.2.13676358}"
revision="${EQ_REPORT_PIC_REVISION:-b135d02bd1ba5103cd3d116a951b1b63a710a312}"

Comment on lines +126 to +131
final class ReportPictureFailure extends Failure {
const ReportPictureFailure(this.reason, super.message, {this.nativeStatus});

final ReportPictureFailureReason reason;
final int? nativeStatus;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · medium
ReportPictureFailure 的設計與 NativeLibrary.open 回傳的錯誤類型存在斷層。NativeLibrary.open 目前僅回傳通用的 UnexpectedFailure,這使得業務層在捕獲錯誤時,難以精確地將其轉換為 ReportPictureFailureReason(例如辨別是 unavailable 還是其他原因),建議在錯誤處理流程中加入更明確的錯誤分類機制。

Comment on lines +17 to +25
String? nativeLibraryFileName(String name, NativeLibraryHost host) =>
switch (host) {
NativeLibraryHost.ios => null,
NativeLibraryHost.android ||
NativeLibraryHost.linux ||
NativeLibraryHost.fuchsia => 'lib$name.so',
NativeLibraryHost.macos => 'lib$name.dylib',
NativeLibraryHost.windows => '$name.dll',
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other · low
nativeLibraryFileName 的映射邏輯目前依賴於手動維護各平台的檔名規範,建議未來若增加更多平台時,能更嚴謹地檢查各平台的動態庫命名規範,以確保擴展性。

Comment on lines +59 to +61
final library = fileName == null
? (openProcess ?? DynamicLibrary.process)()
: (openFile ?? DynamicLibrary.open)(fileName);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · high
NativeLibrary.open 中的 openProcess 處理邏輯存在潛在的 Runtime Bug。當 openProcess 為 null 時,程式碼會嘗試執行 DynamicLibrary.process(),但 DynamicLibrary.process 是一個 getter 而非函數,這會導致 NoSuchMethodError。建議將 fallback 改為 () => DynamicLibrary.process 以確保類型一致性。

Suggestion:

Suggested change
final library = fileName == null
? (openProcess ?? DynamicLibrary.process)()
: (openFile ?? DynamicLibrary.open)(fileName);
final library = fileName == null
? (openProcess ?? () => DynamicLibrary.process)()
: (openFile ?? DynamicLibrary.open)(fileName);

Comment on lines +87 to +90
final render = _pending.then(
(_) => _renderRequest(_address, input, mode, theme),
);
_pending = render.then((_) {});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · high
當 _renderInIsolate 失敗時,_pending 會變成一個失敗的 Future。由於後續的 _pending.then(...) 僅處理成功情況,這會導致所有後續的渲染請求在尚未呼叫 _renderInIsolate 之前就直接因為 _pending 的錯誤而失敗,這與註解中「失敗請求應讓渲染器保持可用」的設計不符。

Suggestion:

Suggested change
final render = _pending.then(
(_) => _renderRequest(_address, input, mode, theme),
);
_pending = render.then((_) {});
final render = _pending.then(
(_) => _renderRequest(_address, input, mode, theme),
);
_pending = render.then((_) {}, onError: (_) {});

Comment on lines +157 to +208
Future<Uint8List> _renderInIsolate(
int address,
Uint8List input,
EqMapMode mode,
EqTheme theme,
) => Isolate.run(
() => EqReportPic.fromAddress(
_workerBindings(),
address,
).renderSharePngV2(input, mode: mode, theme: theme),
debugName: 'report-picture-render',
);

Future<Result<Uint8List>> _renderRequest(
int address,
Uint8List input,
EqMapMode mode,
EqTheme theme,
) async {
try {
return Ok(await _renderInIsolate(address, input, mode, theme));
} on ReportPictureFailure catch (failure) {
return Err(failure);
} on EqShareRenderException catch (error) {
final reason = switch (error.reason) {
EqShareRenderError.invalidArgument =>
ReportPictureFailureReason.invalidInput,
EqShareRenderError.unsupportedVersion =>
ReportPictureFailureReason.unsupportedVersion,
EqShareRenderError.unsupportedFeature =>
ReportPictureFailureReason.unsupportedFeature,
EqShareRenderError.limitExceeded =>
ReportPictureFailureReason.limitExceeded,
EqShareRenderError.internal ||
EqShareRenderError.unknown => ReportPictureFailureReason.renderFailed,
};
return Err(
ReportPictureFailure(
reason,
error.message,
nativeStatus: error.nativeStatus,
),
);
} catch (error) {
return Err(
ReportPictureFailure(
ReportPictureFailureReason.renderFailed,
'The native report renderer failed: $error',
),
);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other · medium
_renderInIsolate 的回傳類型宣告與其實際回傳值(Result<Uint8List>)不符,且在 _renderRequest 中使用 Ok(await ...) 會導致回傳值變成 Result<Result<Uint8List>> 的巢狀結構。

Suggestion:

Suggested change
Future<Uint8List> _renderInIsolate(
int address,
Uint8List input,
EqMapMode mode,
EqTheme theme,
) => Isolate.run(
() => EqReportPic.fromAddress(
_workerBindings(),
address,
).renderSharePngV2(input, mode: mode, theme: theme),
debugName: 'report-picture-render',
);
Future<Result<Uint8List>> _renderRequest(
int address,
Uint8List input,
EqMapMode mode,
EqTheme theme,
) async {
try {
return Ok(await _renderInIsolate(address, input, mode, theme));
} on ReportPictureFailure catch (failure) {
return Err(failure);
} on EqShareRenderException catch (error) {
final reason = switch (error.reason) {
EqShareRenderError.invalidArgument =>
ReportPictureFailureReason.invalidInput,
EqShareRenderError.unsupportedVersion =>
ReportPictureFailureReason.unsupportedVersion,
EqShareRenderError.unsupportedFeature =>
ReportPictureFailureReason.unsupportedFeature,
EqShareRenderError.limitExceeded =>
ReportPictureFailureReason.limitExceeded,
EqShareRenderError.internal ||
EqShareRenderError.unknown => ReportPictureFailureReason.renderFailed,
};
return Err(
ReportPictureFailure(
reason,
error.message,
nativeStatus: error.nativeStatus,
),
);
} catch (error) {
return Err(
ReportPictureFailure(
ReportPictureFailureReason.renderFailed,
'The native report renderer failed: $error',
),
);
}
}
Future<Result<Uint8List>> _renderInIsolate(
int address,
Uint8List input,
EqMapMode mode,
EqTheme theme,
) => Isolate.run(
() => EqReportPic.fromAddress(
_workerBindings(),
address,
).renderSharePngV2(input, mode: mode, theme: theme),
debugName: 'report-picture-render',
);
Future<Result<Uint8List>> _renderRequest(
int address,
Uint8List input,
EqMapMode mode,
EqTheme theme,
) async {
try {
return await _renderInIsolate(address, input, mode, theme);
} on ReportPictureFailure catch (failure) {
return Err(failure);
} on EqShareRenderException catch (error) {
final reason = switch (error.reason) {
EqShareRenderError.invalidArgument =>
ReportPictureFailureReason.invalidInput,
EqShareRenderError.unsupportedVersion =>
ReportPictureFailureReason.unsupportedVersion,
EqShareRenderError.unsupportedFeature =>
ReportPictureFailureReason.unsupportedFeature,
EqShareRenderError.limitExceeded =>
ReportPictureFailureReason.limitExceeded,
EqShareRenderError.internal ||
EqShareRenderError.unknown => ReportPictureFailureReason.renderFailed,
};
return Err(
ReportPictureFailure(
reason,
error.message,
nativeStatus: error.nativeStatus,
),
);
} catch (error) {
return Err(
ReportPictureFailure(
ReportPictureFailureReason.renderFailed,
'The native report renderer failed: $error',
),
);
}
}

@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 8.60215% with 170 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.27%. Comparing base (d88c21b) to head (b493a1f).

Files with missing lines Patch % Lines
...seismic/report_picture/eq_report_pic_bindings.dart 0.00% 86 Missing ⚠️
.../shared/seismic/report_picture/report_picture.dart 0.00% 73 Missing ⚠️
lib/core/platform/native_library.dart 61.53% 10 Missing ⚠️
lib/core/error/failure.dart 0.00% 1 Missing ⚠️

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants