diff --git a/entry/src/main/cpp/CMakeLists.txt b/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000..e579975 --- /dev/null +++ b/entry/src/main/cpp/CMakeLists.txt @@ -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 +) diff --git a/entry/src/main/cpp/input/InputDispatcher.cpp b/entry/src/main/cpp/input/InputDispatcher.cpp new file mode 100644 index 0000000..f5517b0 --- /dev/null +++ b/entry/src/main/cpp/input/InputDispatcher.cpp @@ -0,0 +1,7 @@ +#include "InputEvent.h" + +bool DispatchInput(const MrpInputEvent &event) +{ + (void)event; + return true; +} diff --git a/entry/src/main/cpp/input/InputEvent.h b/entry/src/main/cpp/input/InputEvent.h new file mode 100644 index 0000000..6f58edb --- /dev/null +++ b/entry/src/main/cpp/input/InputEvent.h @@ -0,0 +1,18 @@ +#ifndef MRP_INPUT_INPUT_EVENT_H +#define MRP_INPUT_INPUT_EVENT_H + +#include +#include + +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 diff --git a/entry/src/main/cpp/napi/facade_binding.cpp b/entry/src/main/cpp/napi/facade_binding.cpp new file mode 100644 index 0000000..ea9ad6c --- /dev/null +++ b/entry/src/main/cpp/napi/facade_binding.cpp @@ -0,0 +1,224 @@ +#include + +#include +#include +#include + +#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(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; +} diff --git a/entry/src/main/cpp/napi/module_init.cpp b/entry/src/main/cpp/napi/module_init.cpp new file mode 100644 index 0000000..e4fe58f --- /dev/null +++ b/entry/src/main/cpp/napi/module_init.cpp @@ -0,0 +1,23 @@ +#include + +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); +} diff --git a/entry/src/main/cpp/platform/MrpFacade.cpp b/entry/src/main/cpp/platform/MrpFacade.cpp new file mode 100644 index 0000000..849e40a --- /dev/null +++ b/entry/src/main/cpp/platform/MrpFacade.cpp @@ -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(); +} diff --git a/entry/src/main/cpp/platform/MrpFacade.h b/entry/src/main/cpp/platform/MrpFacade.h new file mode 100644 index 0000000..f55eadc --- /dev/null +++ b/entry/src/main/cpp/platform/MrpFacade.h @@ -0,0 +1,51 @@ +#ifndef MRP_PLATFORM_MRP_FACADE_H +#define MRP_PLATFORM_MRP_FACADE_H + +#include +#include +#include + +#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 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 diff --git a/entry/src/main/cpp/render/FrameBuffer.cpp b/entry/src/main/cpp/render/FrameBuffer.cpp new file mode 100644 index 0000000..e30d329 --- /dev/null +++ b/entry/src/main/cpp/render/FrameBuffer.cpp @@ -0,0 +1,24 @@ +#include "FrameBuffer.h" + +FrameBuffer::FrameBuffer(int32_t width, int32_t height) + : width_(width), height_(height), rgba_(static_cast(width) * static_cast(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(y * width_ + x) * 4U; + rgba_[index] = static_cast((x + frameId) % 255); + rgba_[index + 1] = static_cast((y + frameId) % 255); + rgba_[index + 2] = static_cast((x + y) % 255); + rgba_[index + 3] = 255; + } + } +} + +const std::vector &FrameBuffer::Data() const +{ + return rgba_; +} diff --git a/entry/src/main/cpp/render/FrameBuffer.h b/entry/src/main/cpp/render/FrameBuffer.h new file mode 100644 index 0000000..eb00d7a --- /dev/null +++ b/entry/src/main/cpp/render/FrameBuffer.h @@ -0,0 +1,20 @@ +#ifndef MRP_RENDER_FRAME_BUFFER_H +#define MRP_RENDER_FRAME_BUFFER_H + +#include +#include + +class FrameBuffer { +public: + FrameBuffer(int32_t width, int32_t height); + + void FillTestPattern(int64_t frameId); + const std::vector &Data() const; + +private: + int32_t width_; + int32_t height_; + std::vector rgba_; +}; + +#endif diff --git a/entry/src/main/cpp/runtime/MrpRuntime.cpp b/entry/src/main/cpp/runtime/MrpRuntime.cpp new file mode 100644 index 0000000..5e9ec63 --- /dev/null +++ b/entry/src/main/cpp/runtime/MrpRuntime.cpp @@ -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_; +} diff --git a/entry/src/main/cpp/runtime/MrpRuntime.h b/entry/src/main/cpp/runtime/MrpRuntime.h new file mode 100644 index 0000000..95081cf --- /dev/null +++ b/entry/src/main/cpp/runtime/MrpRuntime.h @@ -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