Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4613919277 | ||
|
|
bde990ac82 | ||
|
|
a14b10d62f | ||
|
|
06f4a95020 | ||
|
|
6c99ae75f6 | ||
|
|
8b6e24fa61 |
No files matched your search
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"app": {
|
||||
"bundleName": "com.xiaobai.mrpsimulator",
|
||||
"vendor": "xiaobai",
|
||||
"versionCode": 1000000,
|
||||
"versionName": "1.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"app": {
|
||||
"products": [
|
||||
{
|
||||
"name": "default",
|
||||
"signingConfig": "default",
|
||||
"compatibleSdkVersion": "5.0.0(12)",
|
||||
"runtimeOS": "HarmonyOS"
|
||||
}
|
||||
],
|
||||
"buildModeSet": [
|
||||
{
|
||||
"name": "debug"
|
||||
},
|
||||
{
|
||||
"name": "release"
|
||||
}
|
||||
],
|
||||
"signingConfigs": [
|
||||
{
|
||||
"name": "default",
|
||||
"type": "HarmonyOS",
|
||||
"material": {
|
||||
"storeFile": "./AppScope/debug.p12",
|
||||
"storePassword": "123456",
|
||||
"keyAlias": "debugKey",
|
||||
"keyPassword": "123456",
|
||||
"signAlg": "SHA256withECDSA",
|
||||
"profile": "./AppScope/debug.mobileprovision"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"modules": [
|
||||
{
|
||||
"name": "entry",
|
||||
"srcPath": "./entry",
|
||||
"targets": [
|
||||
{
|
||||
"name": "default",
|
||||
"applyToProducts": [
|
||||
"default"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
# ArkTS 壳层最小可演示闭环说明 v1
|
||||
|
||||
项目:mrp-simulator-harmonyos
|
||||
|
||||
## 1. 运行入口在哪里
|
||||
|
||||
运行入口在:
|
||||
- `entry/src/main/ets/pages/ShellRootPage.ets`
|
||||
- `entry/src/main/ets/pages/AppListPage.ets`
|
||||
|
||||
当前可见入口:
|
||||
1. App 启动进入 `ShellRootPage`
|
||||
2. 默认路由进入 `AppListPage`
|
||||
3. 用户点击 **“可运行”应用卡片**
|
||||
4. 通过 `onNavigateRun(appId)` 切到 `RunPage`
|
||||
|
||||
## 2. 页面流如何走
|
||||
|
||||
当前最小页面流:
|
||||
- `AppListPage -> ImportPage -> AppListPage`
|
||||
- `AppListPage -> RunPage -> AppListPage`
|
||||
|
||||
具体链路:
|
||||
1. **列表页**
|
||||
- 展示应用列表
|
||||
- 展示运行入口提示
|
||||
- 点击“导入应用”进入 `ImportPage`
|
||||
- 点击“可运行”卡片进入 `RunPage`
|
||||
2. **导入页**
|
||||
- 选择文件
|
||||
- 展示预览
|
||||
- 点击“开始导入”
|
||||
- 导入成功后返回列表页
|
||||
- 返回列表页时触发列表刷新 token
|
||||
3. **运行页**
|
||||
- 进入时根据 `appId` 发起 `startSession(appId)`
|
||||
- 状态从 `starting -> running`
|
||||
- 点击退出后回到列表页
|
||||
|
||||
## 3. 哪些接口已经为 NAPI 预留
|
||||
|
||||
### 3.1 App 列表相关
|
||||
- `entry/src/main/ets/adapters/NativeAppAdapter.ts`
|
||||
- `listInstalledApps()`
|
||||
|
||||
### 3.2 导入相关
|
||||
- `entry/src/main/ets/adapters/NativeImportAdapter.ts`
|
||||
- `pickFile()`
|
||||
- `inspectPackage(fileUri)`
|
||||
- `importPackage(fileUri)`
|
||||
|
||||
### 3.3 运行时相关
|
||||
- `entry/src/main/ets/adapters/NativeRuntimeAdapter.ts`
|
||||
- `startSession(appId)`
|
||||
- `restartSession(appId)`
|
||||
- `stopSession()`
|
||||
- `notifyPageShow()`
|
||||
- `notifyPageHide()`
|
||||
|
||||
### 3.4 Service 收口层
|
||||
- `entry/src/main/ets/services/AppService.ts`
|
||||
- `entry/src/main/ets/services/ImportService.ts`
|
||||
- `entry/src/main/ets/services/RuntimeService.ts`
|
||||
|
||||
说明:
|
||||
- 后续 NAPI 接入时,优先替换 Adapter 内部实现
|
||||
- 页面层和 Store 层不直接碰 native 细节
|
||||
|
||||
## 4. 当前可见结果
|
||||
|
||||
当前已经具备:
|
||||
- 列表页可见运行入口与导入入口
|
||||
- 导入成功后返回列表并刷新
|
||||
- 运行页可见 `starting / running / stopped` 状态展示
|
||||
- 运行页工具栏根据状态控制按钮可用性
|
||||
|
||||
## 5. 本轮不做的事
|
||||
|
||||
本轮不继续扩大量新页面/组件,只收口:
|
||||
- 现有页面可见闭环
|
||||
- 现有 Adapter/NAPI 接缝
|
||||
- 现有运行入口与状态提示
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"apiType": "stageMode",
|
||||
"buildOption": {
|
||||
"externalNativeOptions": {
|
||||
"path": "src/main/cpp/CMakeLists.txt",
|
||||
"arguments": "",
|
||||
"cppFlags": "-std=c++17"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"modelVersion": "5.0.0",
|
||||
"name": "entry",
|
||||
"version": "1.0.0",
|
||||
"description": "Entry module for MRP simulator HarmonyOS app",
|
||||
"dependencies": {}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(mrp_napi)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_library(mrp_napi SHARED
|
||||
napi/module_init.cpp
|
||||
napi/facade_binding.cpp
|
||||
platform/MrpFacade.cpp
|
||||
runtime/MrpRuntime.cpp
|
||||
render/FrameBuffer.cpp
|
||||
input/InputDispatcher.cpp
|
||||
)
|
||||
|
||||
target_include_directories(mrp_napi PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/platform
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/runtime
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/render
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/input
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/napi
|
||||
)
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "InputEvent.h"
|
||||
|
||||
bool DispatchInput(const MrpInputEvent &event)
|
||||
{
|
||||
(void)event;
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef MRP_INPUT_INPUT_EVENT_H
|
||||
#define MRP_INPUT_INPUT_EVENT_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
struct MrpInputEvent {
|
||||
std::string type = "key";
|
||||
std::string action = "down";
|
||||
int32_t keyCode = 0;
|
||||
int32_t x = 0;
|
||||
int32_t y = 0;
|
||||
int64_t timestamp = 0;
|
||||
};
|
||||
|
||||
bool DispatchInput(const MrpInputEvent &event);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,224 @@
|
||||
#include <node_api.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../input/InputEvent.h"
|
||||
#include "../platform/MrpFacade.h"
|
||||
|
||||
namespace {
|
||||
MrpFacade g_facade;
|
||||
|
||||
napi_value MakeString(napi_env env, const std::string &value)
|
||||
{
|
||||
napi_value result;
|
||||
napi_create_string_utf8(env, value.c_str(), value.size(), &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value MakeCommonResult(napi_env env, const CommonResult &result)
|
||||
{
|
||||
napi_value obj;
|
||||
napi_create_object(env, &obj);
|
||||
|
||||
napi_value ok;
|
||||
napi_get_boolean(env, result.ok, &ok);
|
||||
napi_set_named_property(env, obj, "ok", ok);
|
||||
|
||||
napi_value errorCode;
|
||||
napi_create_int32(env, result.errorCode, &errorCode);
|
||||
napi_set_named_property(env, obj, "errorCode", errorCode);
|
||||
|
||||
napi_set_named_property(env, obj, "errorMessage", MakeString(env, result.errorMessage));
|
||||
return obj;
|
||||
}
|
||||
|
||||
std::string GetStringProperty(napi_env env, napi_value obj, const char *name, const std::string &fallback)
|
||||
{
|
||||
bool hasProperty = false;
|
||||
napi_has_named_property(env, obj, name, &hasProperty);
|
||||
if (!hasProperty) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
napi_value value;
|
||||
napi_get_named_property(env, obj, name, &value);
|
||||
size_t length = 0;
|
||||
napi_get_value_string_utf8(env, value, nullptr, 0, &length);
|
||||
std::string result(length, '\0');
|
||||
napi_get_value_string_utf8(env, value, result.data(), length + 1, &length);
|
||||
return result;
|
||||
}
|
||||
|
||||
int32_t GetIntProperty(napi_env env, napi_value obj, const char *name, int32_t fallback)
|
||||
{
|
||||
bool hasProperty = false;
|
||||
napi_has_named_property(env, obj, name, &hasProperty);
|
||||
if (!hasProperty) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
napi_value value;
|
||||
napi_get_named_property(env, obj, name, &value);
|
||||
int32_t result = fallback;
|
||||
napi_get_value_int32(env, value, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool GetBoolProperty(napi_env env, napi_value obj, const char *name, bool fallback)
|
||||
{
|
||||
bool hasProperty = false;
|
||||
napi_has_named_property(env, obj, name, &hasProperty);
|
||||
if (!hasProperty) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
napi_value value;
|
||||
napi_get_named_property(env, obj, name, &value);
|
||||
bool result = fallback;
|
||||
napi_get_value_bool(env, value, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value Init(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = 1;
|
||||
napi_value argv[1] = {nullptr};
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
|
||||
InitOptions options;
|
||||
if (argc == 1 && argv[0] != nullptr) {
|
||||
options.workDir = GetStringProperty(env, argv[0], "workDir", "");
|
||||
options.width = GetIntProperty(env, argv[0], "width", 240);
|
||||
options.height = GetIntProperty(env, argv[0], "height", 320);
|
||||
options.debug = GetBoolProperty(env, argv[0], "debug", false);
|
||||
}
|
||||
|
||||
return MakeCommonResult(env, g_facade.Init(options));
|
||||
}
|
||||
|
||||
napi_value LoadPackage(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = 1;
|
||||
napi_value argv[1] = {nullptr};
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
|
||||
std::string packagePath;
|
||||
if (argc == 1 && argv[0] != nullptr) {
|
||||
size_t length = 0;
|
||||
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &length);
|
||||
packagePath.resize(length);
|
||||
napi_get_value_string_utf8(env, argv[0], packagePath.data(), length + 1, &length);
|
||||
}
|
||||
|
||||
return MakeCommonResult(env, g_facade.LoadPackage(packagePath));
|
||||
}
|
||||
|
||||
napi_value Start(napi_env env, napi_callback_info info)
|
||||
{
|
||||
(void)info;
|
||||
return MakeCommonResult(env, g_facade.Start());
|
||||
}
|
||||
|
||||
napi_value Pause(napi_env env, napi_callback_info info)
|
||||
{
|
||||
(void)info;
|
||||
return MakeCommonResult(env, g_facade.Pause());
|
||||
}
|
||||
|
||||
napi_value Resume(napi_env env, napi_callback_info info)
|
||||
{
|
||||
(void)info;
|
||||
return MakeCommonResult(env, g_facade.Resume());
|
||||
}
|
||||
|
||||
napi_value SendInput(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = 1;
|
||||
napi_value argv[1] = {nullptr};
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
|
||||
MrpInputEvent event;
|
||||
if (argc == 1 && argv[0] != nullptr) {
|
||||
event.type = GetStringProperty(env, argv[0], "type", "key");
|
||||
event.action = GetStringProperty(env, argv[0], "action", "down");
|
||||
event.keyCode = GetIntProperty(env, argv[0], "keyCode", 0);
|
||||
event.x = GetIntProperty(env, argv[0], "x", 0);
|
||||
event.y = GetIntProperty(env, argv[0], "y", 0);
|
||||
event.timestamp = static_cast<int64_t>(GetIntProperty(env, argv[0], "timestamp", 0));
|
||||
}
|
||||
|
||||
return MakeCommonResult(env, g_facade.SendInput(event));
|
||||
}
|
||||
|
||||
napi_value PullFrame(napi_env env, napi_callback_info info)
|
||||
{
|
||||
(void)info;
|
||||
FrameResult frame = g_facade.PullFrame();
|
||||
|
||||
napi_value obj;
|
||||
napi_create_object(env, &obj);
|
||||
|
||||
napi_value ok;
|
||||
napi_get_boolean(env, frame.ok, &ok);
|
||||
napi_set_named_property(env, obj, "ok", ok);
|
||||
|
||||
napi_value hasNewFrame;
|
||||
napi_get_boolean(env, frame.hasNewFrame, &hasNewFrame);
|
||||
napi_set_named_property(env, obj, "hasNewFrame", hasNewFrame);
|
||||
|
||||
napi_value width;
|
||||
napi_create_int32(env, frame.width, &width);
|
||||
napi_set_named_property(env, obj, "width", width);
|
||||
|
||||
napi_value height;
|
||||
napi_create_int32(env, frame.height, &height);
|
||||
napi_set_named_property(env, obj, "height", height);
|
||||
|
||||
napi_set_named_property(env, obj, "pixelFormat", MakeString(env, frame.pixelFormat));
|
||||
|
||||
void *data = nullptr;
|
||||
napi_value buffer;
|
||||
napi_create_arraybuffer(env, frame.buffer.size(), &data, &buffer);
|
||||
if (data != nullptr && !frame.buffer.empty()) {
|
||||
std::memcpy(data, frame.buffer.data(), frame.buffer.size());
|
||||
}
|
||||
napi_set_named_property(env, obj, "buffer", buffer);
|
||||
|
||||
napi_value frameId;
|
||||
napi_create_int64(env, frame.frameId, &frameId);
|
||||
napi_set_named_property(env, obj, "frameId", frameId);
|
||||
|
||||
napi_value errorCode;
|
||||
napi_create_int32(env, frame.errorCode, &errorCode);
|
||||
napi_set_named_property(env, obj, "errorCode", errorCode);
|
||||
|
||||
napi_set_named_property(env, obj, "errorMessage", MakeString(env, frame.errorMessage));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
napi_value Release(napi_env env, napi_callback_info info)
|
||||
{
|
||||
(void)info;
|
||||
return MakeCommonResult(env, g_facade.Release());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
napi_value ExportMrpFacade(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor descriptors[] = {
|
||||
{"init", nullptr, Init, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
{"loadPackage", nullptr, LoadPackage, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
{"start", nullptr, Start, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
{"pause", nullptr, Pause, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
{"resume", nullptr, Resume, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
{"sendInput", nullptr, SendInput, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
{"pullFrame", nullptr, PullFrame, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
{"release", nullptr, Release, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
};
|
||||
|
||||
napi_define_properties(env, exports, sizeof(descriptors) / sizeof(descriptors[0]), descriptors);
|
||||
return exports;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <node_api.h>
|
||||
|
||||
napi_value ExportMrpFacade(napi_env env, napi_value exports);
|
||||
|
||||
static napi_value InitModule(napi_env env, napi_value exports)
|
||||
{
|
||||
return ExportMrpFacade(env, exports);
|
||||
}
|
||||
|
||||
static napi_module g_mrpModule = {
|
||||
1,
|
||||
0,
|
||||
nullptr,
|
||||
InitModule,
|
||||
"mrp_napi",
|
||||
nullptr,
|
||||
{0},
|
||||
};
|
||||
|
||||
extern "C" __attribute__((constructor)) void RegisterMrpModule(void)
|
||||
{
|
||||
napi_module_register(&g_mrpModule);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "MrpFacade.h"
|
||||
|
||||
#include "../render/FrameBuffer.h"
|
||||
#include "../runtime/MrpRuntime.h"
|
||||
|
||||
namespace {
|
||||
MrpRuntime g_runtime;
|
||||
}
|
||||
|
||||
CommonResult MrpFacade::Init(const InitOptions &options)
|
||||
{
|
||||
options_ = options;
|
||||
return g_runtime.Init(options.width, options.height, options.debug);
|
||||
}
|
||||
|
||||
CommonResult MrpFacade::LoadPackage(const std::string &packagePath)
|
||||
{
|
||||
packagePath_ = packagePath;
|
||||
return g_runtime.LoadPackage(packagePath);
|
||||
}
|
||||
|
||||
CommonResult MrpFacade::Start()
|
||||
{
|
||||
return g_runtime.Start();
|
||||
}
|
||||
|
||||
CommonResult MrpFacade::Pause()
|
||||
{
|
||||
return g_runtime.Pause();
|
||||
}
|
||||
|
||||
CommonResult MrpFacade::Resume()
|
||||
{
|
||||
return g_runtime.Resume();
|
||||
}
|
||||
|
||||
CommonResult MrpFacade::SendInput(const MrpInputEvent &event)
|
||||
{
|
||||
DispatchInput(event);
|
||||
return CommonResult{true, 0, ""};
|
||||
}
|
||||
|
||||
FrameResult MrpFacade::PullFrame()
|
||||
{
|
||||
FrameBuffer frameBuffer(g_runtime.Width(), g_runtime.Height());
|
||||
frameBuffer.FillTestPattern(g_runtime.NextFrameId());
|
||||
|
||||
FrameResult result;
|
||||
result.ok = true;
|
||||
result.hasNewFrame = true;
|
||||
result.width = g_runtime.Width();
|
||||
result.height = g_runtime.Height();
|
||||
result.pixelFormat = "RGBA_8888";
|
||||
result.buffer = frameBuffer.Data();
|
||||
result.frameId = g_runtime.CurrentFrameId();
|
||||
return result;
|
||||
}
|
||||
|
||||
CommonResult MrpFacade::Release()
|
||||
{
|
||||
packagePath_.clear();
|
||||
return g_runtime.Release();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef MRP_PLATFORM_MRP_FACADE_H
|
||||
#define MRP_PLATFORM_MRP_FACADE_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../input/InputEvent.h"
|
||||
|
||||
struct InitOptions {
|
||||
std::string workDir;
|
||||
int32_t width = 240;
|
||||
int32_t height = 320;
|
||||
bool debug = false;
|
||||
};
|
||||
|
||||
struct CommonResult {
|
||||
bool ok = false;
|
||||
int32_t errorCode = 0;
|
||||
std::string errorMessage;
|
||||
};
|
||||
|
||||
struct FrameResult {
|
||||
bool ok = false;
|
||||
bool hasNewFrame = false;
|
||||
int32_t width = 0;
|
||||
int32_t height = 0;
|
||||
std::string pixelFormat = "RGBA_8888";
|
||||
std::vector<uint8_t> buffer;
|
||||
int64_t frameId = 0;
|
||||
int32_t errorCode = 0;
|
||||
std::string errorMessage;
|
||||
};
|
||||
|
||||
class MrpFacade {
|
||||
public:
|
||||
CommonResult Init(const InitOptions &options);
|
||||
CommonResult LoadPackage(const std::string &packagePath);
|
||||
CommonResult Start();
|
||||
CommonResult Pause();
|
||||
CommonResult Resume();
|
||||
CommonResult SendInput(const MrpInputEvent &event);
|
||||
FrameResult PullFrame();
|
||||
CommonResult Release();
|
||||
|
||||
private:
|
||||
InitOptions options_;
|
||||
std::string packagePath_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "FrameBuffer.h"
|
||||
|
||||
FrameBuffer::FrameBuffer(int32_t width, int32_t height)
|
||||
: width_(width), height_(height), rgba_(static_cast<size_t>(width) * static_cast<size_t>(height) * 4U, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void FrameBuffer::FillTestPattern(int64_t frameId)
|
||||
{
|
||||
for (int32_t y = 0; y < height_; ++y) {
|
||||
for (int32_t x = 0; x < width_; ++x) {
|
||||
const size_t index = static_cast<size_t>(y * width_ + x) * 4U;
|
||||
rgba_[index] = static_cast<uint8_t>((x + frameId) % 255);
|
||||
rgba_[index + 1] = static_cast<uint8_t>((y + frameId) % 255);
|
||||
rgba_[index + 2] = static_cast<uint8_t>((x + y) % 255);
|
||||
rgba_[index + 3] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<uint8_t> &FrameBuffer::Data() const
|
||||
{
|
||||
return rgba_;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef MRP_RENDER_FRAME_BUFFER_H
|
||||
#define MRP_RENDER_FRAME_BUFFER_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
class FrameBuffer {
|
||||
public:
|
||||
FrameBuffer(int32_t width, int32_t height);
|
||||
|
||||
void FillTestPattern(int64_t frameId);
|
||||
const std::vector<uint8_t> &Data() const;
|
||||
|
||||
private:
|
||||
int32_t width_;
|
||||
int32_t height_;
|
||||
std::vector<uint8_t> rgba_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "MrpRuntime.h"
|
||||
|
||||
CommonResult MrpRuntime::Init(int32_t width, int32_t height, bool debug)
|
||||
{
|
||||
width_ = width > 0 ? width : 240;
|
||||
height_ = height > 0 ? height : 320;
|
||||
debug_ = debug;
|
||||
frameId_ = 0;
|
||||
state_ = State::Initialized;
|
||||
return CommonResult{true, 0, ""};
|
||||
}
|
||||
|
||||
CommonResult MrpRuntime::LoadPackage(const std::string &packagePath)
|
||||
{
|
||||
if (state_ != State::Initialized && state_ != State::Released) {
|
||||
return CommonResult{false, 1001, "runtime not initialized"};
|
||||
}
|
||||
(void)packagePath;
|
||||
state_ = State::Loaded;
|
||||
return CommonResult{true, 0, ""};
|
||||
}
|
||||
|
||||
CommonResult MrpRuntime::Start()
|
||||
{
|
||||
if (state_ != State::Loaded && state_ != State::Paused) {
|
||||
return CommonResult{false, 1002, "runtime not ready to start"};
|
||||
}
|
||||
state_ = State::Running;
|
||||
return CommonResult{true, 0, ""};
|
||||
}
|
||||
|
||||
CommonResult MrpRuntime::Pause()
|
||||
{
|
||||
if (state_ != State::Running) {
|
||||
return CommonResult{false, 1003, "runtime not running"};
|
||||
}
|
||||
state_ = State::Paused;
|
||||
return CommonResult{true, 0, ""};
|
||||
}
|
||||
|
||||
CommonResult MrpRuntime::Resume()
|
||||
{
|
||||
if (state_ != State::Paused) {
|
||||
return CommonResult{false, 1004, "runtime not paused"};
|
||||
}
|
||||
state_ = State::Running;
|
||||
return CommonResult{true, 0, ""};
|
||||
}
|
||||
|
||||
CommonResult MrpRuntime::Release()
|
||||
{
|
||||
state_ = State::Released;
|
||||
frameId_ = 0;
|
||||
return CommonResult{true, 0, ""};
|
||||
}
|
||||
|
||||
int32_t MrpRuntime::Width() const
|
||||
{
|
||||
return width_;
|
||||
}
|
||||
|
||||
int32_t MrpRuntime::Height() const
|
||||
{
|
||||
return height_;
|
||||
}
|
||||
|
||||
int64_t MrpRuntime::NextFrameId()
|
||||
{
|
||||
return ++frameId_;
|
||||
}
|
||||
|
||||
int64_t MrpRuntime::CurrentFrameId() const
|
||||
{
|
||||
return frameId_;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef MRP_RUNTIME_MRP_RUNTIME_H
|
||||
#define MRP_RUNTIME_MRP_RUNTIME_H
|
||||
|
||||
#include "../platform/MrpFacade.h"
|
||||
|
||||
class MrpRuntime {
|
||||
public:
|
||||
CommonResult Init(int32_t width, int32_t height, bool debug);
|
||||
CommonResult LoadPackage(const std::string &packagePath);
|
||||
CommonResult Start();
|
||||
CommonResult Pause();
|
||||
CommonResult Resume();
|
||||
CommonResult Release();
|
||||
|
||||
int32_t Width() const;
|
||||
int32_t Height() const;
|
||||
int64_t NextFrameId();
|
||||
int64_t CurrentFrameId() const;
|
||||
|
||||
private:
|
||||
enum class State {
|
||||
Idle,
|
||||
Initialized,
|
||||
Loaded,
|
||||
Running,
|
||||
Paused,
|
||||
Released,
|
||||
};
|
||||
|
||||
State state_ = State::Idle;
|
||||
int32_t width_ = 240;
|
||||
int32_t height_ = 320;
|
||||
bool debug_ = false;
|
||||
int64_t frameId_ = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,23 +1,59 @@
|
||||
const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))
|
||||
import nativeMrp from 'libmrp_napi.so'
|
||||
|
||||
export interface NativeFrameDTO {
|
||||
ok: boolean
|
||||
hasNewFrame: boolean
|
||||
width: number
|
||||
height: number
|
||||
pixelFormat: 'RGBA_8888'
|
||||
buffer?: ArrayBuffer
|
||||
frameId?: number
|
||||
errorCode?: number
|
||||
errorMessage?: string
|
||||
}
|
||||
|
||||
function assertOk(result: { ok: boolean; errorCode?: number; errorMessage?: string }, action: string): void {
|
||||
if (!result.ok) {
|
||||
throw new Error(`${action} failed: ${result.errorCode ?? -1} ${result.errorMessage ?? ''}`.trim())
|
||||
}
|
||||
}
|
||||
|
||||
export class NativeRuntimeAdapter {
|
||||
async startSession(_appId: string): Promise<void> {
|
||||
await sleep(600)
|
||||
async startSession(appId: string): Promise<void> {
|
||||
assertOk(nativeMrp.init({
|
||||
workDir: '/data/storage/el2/base/files/mrp',
|
||||
width: 240,
|
||||
height: 320,
|
||||
debug: true,
|
||||
}), 'init')
|
||||
assertOk(nativeMrp.loadPackage(appId), 'loadPackage')
|
||||
assertOk(nativeMrp.start(), 'start')
|
||||
}
|
||||
|
||||
async restartSession(_appId: string): Promise<void> {
|
||||
await sleep(400)
|
||||
async restartSession(appId: string): Promise<void> {
|
||||
await this.stopSession()
|
||||
await this.startSession(appId)
|
||||
}
|
||||
|
||||
async stopSession(): Promise<void> {
|
||||
return
|
||||
assertOk(nativeMrp.release(), 'release')
|
||||
}
|
||||
|
||||
async pullFrame(): Promise<NativeFrameDTO> {
|
||||
return nativeMrp.pullFrame() as NativeFrameDTO
|
||||
}
|
||||
|
||||
async notifyPageShow(): Promise<void> {
|
||||
return
|
||||
const result = nativeMrp.resume()
|
||||
if (!result.ok) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
async notifyPageHide(): Promise<void> {
|
||||
return
|
||||
const result = nativeMrp.pause()
|
||||
if (!result.ok) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import nativeMrp from 'libmrp_napi.so';
|
||||
|
||||
export function smokeTestNativeInit(): boolean {
|
||||
const result = nativeMrp.init({
|
||||
workDir: '/data/storage/el2/base/files/mrp',
|
||||
width: 240,
|
||||
height: 320,
|
||||
debug: true,
|
||||
});
|
||||
return !!result?.ok;
|
||||
}
|
||||
@@ -1,10 +1,43 @@
|
||||
@Component
|
||||
export struct RuntimeContainer {
|
||||
@Prop hint: string = 'Runtime Surface Placeholder'
|
||||
@Prop hasFrame: boolean = false
|
||||
@Prop frameId: number = 0
|
||||
@Prop frameWidth: number = 0
|
||||
@Prop frameHeight: number = 0
|
||||
|
||||
private frameColor(): string {
|
||||
const palette: string[] = ['#DCEBFF', '#C6F6D5', '#FEEBC8', '#FED7E2', '#E9D8FD']
|
||||
return this.hasFrame ? palette[this.frameId % palette.length] : '#F3F4F6'
|
||||
}
|
||||
|
||||
build() {
|
||||
Column() {
|
||||
Text('Runtime Surface Placeholder')
|
||||
Column({ space: 12 }) {
|
||||
Text('MRP Runtime Surface Placeholder')
|
||||
.fontSize(18)
|
||||
.fontWeight(FontWeight.Medium)
|
||||
Stack() {
|
||||
Column() {
|
||||
Text(this.hasFrame ? `测试帧 #${this.frameId}` : '尚未拉到测试帧')
|
||||
.fontSize(18)
|
||||
.fontWeight(FontWeight.Bold)
|
||||
Text(this.hasFrame ? `${this.frameWidth} x ${this.frameHeight}` : '等待 pullFrame()')
|
||||
.fontSize(14)
|
||||
.fontColor('#666666')
|
||||
}
|
||||
}
|
||||
.width('88%')
|
||||
.aspectRatio(3 / 4)
|
||||
.backgroundColor(this.frameColor())
|
||||
.borderRadius(16)
|
||||
.padding(16)
|
||||
Text(this.hint)
|
||||
.fontSize(14)
|
||||
.fontColor('#666666')
|
||||
}
|
||||
.justifyContent(FlexAlign.Center)
|
||||
.alignItems(HorizontalAlign.Center)
|
||||
.width('100%')
|
||||
.height('100%')
|
||||
.layoutWeight(1)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,11 @@
|
||||
export struct RuntimeToolbar {
|
||||
onBack?: () => void
|
||||
onRestart?: () => void
|
||||
onPullFrame?: () => void
|
||||
onExit?: () => void
|
||||
@Prop canRestart: boolean = true
|
||||
@Prop canPullFrame: boolean = true
|
||||
@Prop canExit: boolean = true
|
||||
|
||||
build() {
|
||||
Row({ space: 12 }) {
|
||||
@@ -11,16 +15,27 @@ export struct RuntimeToolbar {
|
||||
this.onBack()
|
||||
}
|
||||
})
|
||||
Button('重启').onClick(() => {
|
||||
if (this.onRestart) {
|
||||
this.onRestart()
|
||||
}
|
||||
})
|
||||
Button('退出').onClick(() => {
|
||||
if (this.onExit) {
|
||||
this.onExit()
|
||||
}
|
||||
})
|
||||
Button('重启')
|
||||
.enabled(this.canRestart)
|
||||
.onClick(() => {
|
||||
if (this.onRestart) {
|
||||
this.onRestart()
|
||||
}
|
||||
})
|
||||
Button('拉帧')
|
||||
.enabled(this.canPullFrame)
|
||||
.onClick(() => {
|
||||
if (this.onPullFrame) {
|
||||
this.onPullFrame()
|
||||
}
|
||||
})
|
||||
Button('退出')
|
||||
.enabled(this.canExit)
|
||||
.onClick(() => {
|
||||
if (this.onExit) {
|
||||
this.onExit()
|
||||
}
|
||||
})
|
||||
}
|
||||
.padding(16)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import UIAbility from '@ohos.app.ability.UIAbility';
|
||||
import window from '@ohos.window';
|
||||
import hilog from '@ohos.hilog';
|
||||
|
||||
const DOMAIN = 0xFF00;
|
||||
const TAG = 'EntryAbility';
|
||||
|
||||
export default class EntryAbility extends UIAbility {
|
||||
onWindowStageCreate(windowStage: window.WindowStage): void {
|
||||
windowStage.loadContent('pages/ShellRootPage', (err) => {
|
||||
if (err.code) {
|
||||
hilog.error(DOMAIN, TAG, 'Failed to load ShellRootPage: %{public}s', JSON.stringify(err));
|
||||
return;
|
||||
}
|
||||
hilog.info(DOMAIN, TAG, 'ShellRootPage loaded');
|
||||
});
|
||||
}
|
||||
}
|
||||
Vendored
+44
@@ -0,0 +1,44 @@
|
||||
declare module 'libmrp_napi.so' {
|
||||
export function init(options: {
|
||||
workDir: string;
|
||||
width: number;
|
||||
height: number;
|
||||
debug?: boolean;
|
||||
}): {
|
||||
ok: boolean;
|
||||
errorCode?: number;
|
||||
errorMessage?: string;
|
||||
};
|
||||
|
||||
export function loadPackage(packagePath: string): {
|
||||
ok: boolean;
|
||||
errorCode?: number;
|
||||
errorMessage?: string;
|
||||
};
|
||||
|
||||
export function start(): { ok: boolean; errorCode?: number; errorMessage?: string };
|
||||
export function pause(): { ok: boolean; errorCode?: number; errorMessage?: string };
|
||||
export function resume(): { ok: boolean; errorCode?: number; errorMessage?: string };
|
||||
export function sendInput(event: {
|
||||
type: 'key' | 'touch';
|
||||
action: 'down' | 'up' | 'move';
|
||||
keyCode?: number;
|
||||
x?: number;
|
||||
y?: number;
|
||||
timestamp?: number;
|
||||
}): { ok: boolean; errorCode?: number; errorMessage?: string };
|
||||
|
||||
export function pullFrame(): {
|
||||
ok: boolean;
|
||||
hasNewFrame: boolean;
|
||||
width: number;
|
||||
height: number;
|
||||
pixelFormat: 'RGBA_8888';
|
||||
buffer?: ArrayBuffer;
|
||||
frameId?: number;
|
||||
errorCode?: number;
|
||||
errorMessage?: string;
|
||||
};
|
||||
|
||||
export function release(): { ok: boolean; errorCode?: number; errorMessage?: string };
|
||||
}
|
||||
@@ -65,6 +65,7 @@ export struct AppListPage {
|
||||
Column() {
|
||||
Text(`${this.refreshTick}-${this.refreshToken}`).fontSize(1).opacity(0)
|
||||
PageHeader({ title: '应用列表' })
|
||||
StatusBanner({ text: '运行入口:点击“可运行”应用卡片进入运行页;导入入口:点击下方“导入应用”。' })
|
||||
if (this.selectedAppId) {
|
||||
StatusBanner({ text: `已选择应用:${this.selectedAppId}` })
|
||||
}
|
||||
@@ -81,6 +82,7 @@ export struct AppListPage {
|
||||
items: this.store.items,
|
||||
onItemTap: (appId: string) => this.openRunPage(appId)
|
||||
})
|
||||
ImportEntryButton({ onTap: () => this.openImportPage() })
|
||||
}
|
||||
}
|
||||
.width('100%')
|
||||
|
||||
@@ -45,6 +45,10 @@ export struct RunPage {
|
||||
await this.store.restartSession()
|
||||
}
|
||||
|
||||
private async onPullFrame(): Promise<void> {
|
||||
await this.store.refreshFrame()
|
||||
}
|
||||
|
||||
private async onExit(): Promise<void> {
|
||||
await this.store.stopSession()
|
||||
if (this.onBack) {
|
||||
@@ -59,15 +63,26 @@ export struct RunPage {
|
||||
if (this.appId) {
|
||||
StatusBanner({ text: `当前应用:${this.appId}` })
|
||||
}
|
||||
StatusBanner({ text: this.store.runtimeHint })
|
||||
if (this.store.error) {
|
||||
StatusBanner({ text: this.store.error.message })
|
||||
}
|
||||
RuntimeToolbar({
|
||||
canRestart: this.store.canRestart,
|
||||
canPullFrame: this.store.phase === 'running',
|
||||
canExit: this.store.canExit,
|
||||
onBack: () => this.onBackClick(),
|
||||
onRestart: () => this.onRestart(),
|
||||
onPullFrame: () => this.onPullFrame(),
|
||||
onExit: () => this.onExit()
|
||||
})
|
||||
RuntimeContainer()
|
||||
RuntimeContainer({
|
||||
hint: this.store.runtimeHint,
|
||||
hasFrame: this.store.frame.hasFrame,
|
||||
frameId: this.store.frame.frameId,
|
||||
frameWidth: this.store.frame.width,
|
||||
frameHeight: this.store.frame.height,
|
||||
})
|
||||
if (this.store.phase !== 'running') {
|
||||
RuntimeStatusOverlay({ text: `当前状态:${this.store.phase}` })
|
||||
}
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
import { NativeRuntimeAdapter } from '../adapters/NativeRuntimeAdapter'
|
||||
|
||||
export interface RuntimeFrameVM {
|
||||
hasFrame: boolean
|
||||
width: number
|
||||
height: number
|
||||
frameId: number
|
||||
pixelFormat: string
|
||||
}
|
||||
|
||||
export class RuntimeService {
|
||||
constructor(private readonly adapter: NativeRuntimeAdapter = new NativeRuntimeAdapter()) {}
|
||||
|
||||
@@ -15,6 +23,20 @@ export class RuntimeService {
|
||||
await this.adapter.stopSession()
|
||||
}
|
||||
|
||||
async pullFrame(): Promise<RuntimeFrameVM> {
|
||||
const frame = await this.adapter.pullFrame()
|
||||
if (!frame.ok) {
|
||||
throw new Error(frame.errorMessage || 'pullFrame failed')
|
||||
}
|
||||
return {
|
||||
hasFrame: frame.hasNewFrame,
|
||||
width: frame.width,
|
||||
height: frame.height,
|
||||
frameId: frame.frameId ?? 0,
|
||||
pixelFormat: frame.pixelFormat,
|
||||
}
|
||||
}
|
||||
|
||||
async onPageShow(): Promise<void> {
|
||||
await this.adapter.notifyPageShow()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { RuntimeService } from '../services/RuntimeService'
|
||||
import type { RuntimeFrameVM } from '../services/RuntimeService'
|
||||
import type { UiError } from '../models/UiError'
|
||||
import type { RuntimePhase } from '../models/RuntimeSessionState'
|
||||
import { StoreObserver } from './StoreObserver'
|
||||
@@ -9,6 +10,14 @@ export class RuntimeSessionStore extends StoreObserver {
|
||||
error: UiError | null = null
|
||||
canRestart: boolean = false
|
||||
canExit: boolean = false
|
||||
runtimeHint: string = '等待启动'
|
||||
frame: RuntimeFrameVM = {
|
||||
hasFrame: false,
|
||||
width: 0,
|
||||
height: 0,
|
||||
frameId: 0,
|
||||
pixelFormat: 'RGBA_8888',
|
||||
}
|
||||
|
||||
constructor(private readonly service: RuntimeService = new RuntimeService()) {
|
||||
super()
|
||||
@@ -20,15 +29,19 @@ export class RuntimeSessionStore extends StoreObserver {
|
||||
this.error = null
|
||||
this.canRestart = false
|
||||
this.canExit = false
|
||||
this.runtimeHint = '正在初始化 Native/NAPI...'
|
||||
this.notify()
|
||||
try {
|
||||
await this.service.start(appId)
|
||||
this.phase = 'running'
|
||||
this.canRestart = true
|
||||
this.canExit = true
|
||||
} catch (_error) {
|
||||
this.runtimeHint = 'Native/NAPI 已连通,准备拉取测试帧'
|
||||
await this.refreshFrame()
|
||||
} catch (error) {
|
||||
this.phase = 'error'
|
||||
this.error = { message: '启动失败', retryable: true }
|
||||
this.runtimeHint = error instanceof Error ? error.message : 'Native/NAPI 启动失败'
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
@@ -40,15 +53,19 @@ export class RuntimeSessionStore extends StoreObserver {
|
||||
this.phase = 'starting'
|
||||
this.error = null
|
||||
this.canRestart = false
|
||||
this.runtimeHint = '正在重启 Native/NAPI...'
|
||||
this.notify()
|
||||
try {
|
||||
await this.service.restart(this.currentAppId)
|
||||
this.phase = 'running'
|
||||
this.canRestart = true
|
||||
this.canExit = true
|
||||
} catch (_error) {
|
||||
this.runtimeHint = '重启完成,准备重新拉取测试帧'
|
||||
await this.refreshFrame()
|
||||
} catch (error) {
|
||||
this.phase = 'error'
|
||||
this.error = { message: '重启失败', retryable: true }
|
||||
this.runtimeHint = error instanceof Error ? error.message : '重启失败'
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
@@ -58,6 +75,28 @@ export class RuntimeSessionStore extends StoreObserver {
|
||||
this.phase = 'stopped'
|
||||
this.canExit = false
|
||||
this.canRestart = false
|
||||
this.runtimeHint = '运行会话已释放'
|
||||
this.frame = {
|
||||
hasFrame: false,
|
||||
width: 0,
|
||||
height: 0,
|
||||
frameId: 0,
|
||||
pixelFormat: 'RGBA_8888',
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
|
||||
async refreshFrame(): Promise<void> {
|
||||
try {
|
||||
const frame = await this.service.pullFrame()
|
||||
this.frame = frame
|
||||
this.runtimeHint = frame.hasFrame
|
||||
? `已拉到测试帧 #${frame.frameId}(${frame.width}x${frame.height} ${frame.pixelFormat})`
|
||||
: '当前未返回新帧'
|
||||
} catch (error) {
|
||||
this.runtimeHint = error instanceof Error ? error.message : 'pullFrame 失败'
|
||||
this.error = { message: '拉帧失败', retryable: true }
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"module": {
|
||||
"name": "entry",
|
||||
"type": "entry",
|
||||
"description": "$string:module_desc",
|
||||
"mainElement": "EntryAbility",
|
||||
"deviceTypes": [
|
||||
"phone",
|
||||
"tablet"
|
||||
],
|
||||
"deliveryWithInstall": true,
|
||||
"installationFree": false,
|
||||
"pages": "$profile:main_pages",
|
||||
"abilities": [
|
||||
{
|
||||
"name": "EntryAbility",
|
||||
"srcEntry": "./ets/entryability/EntryAbility.ets",
|
||||
"description": "$string:entry_ability_desc",
|
||||
"icon": "$media:app_icon",
|
||||
"label": "$string:app_name",
|
||||
"startWindowIcon": "$media:app_icon",
|
||||
"startWindowBackground": "$color:start_window_background",
|
||||
"exported": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "start_window_background",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "app_name",
|
||||
"value": "MRP Simulator"
|
||||
},
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "MRP simulator HarmonyOS entry module"
|
||||
},
|
||||
{
|
||||
"name": "entry_ability_desc",
|
||||
"value": "MRP simulator entry ability"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 256 256">
|
||||
<rect width="256" height="256" rx="48" fill="#0A59F7"/>
|
||||
<text x="128" y="148" text-anchor="middle" font-size="84" fill="#FFFFFF" font-family="sans-serif">M</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 262 B |
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"src": [
|
||||
"pages/ShellRootPage"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { appTasks } from '@ohos/hvigor-ohos-plugin';
|
||||
|
||||
export default {
|
||||
system: appTasks,
|
||||
plugins: []
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"modelVersion": "5.0.0",
|
||||
"name": "mrp-simulator-harmonyos",
|
||||
"version": "1.0.0",
|
||||
"description": "HarmonyOS shell for MRP simulator",
|
||||
"dependencies": {}
|
||||
}
|
||||
Reference in new issue
Block a user