浏览代码

fix format

/siyaoH-1.17-PlatformMessage
siyao 4 年前
当前提交
d894a9ee
共有 5 个文件被更改,包括 478 次插入543 次删除
  1. 2
      engine/Build.bee.cs
  2. 887
      engine/src/shell/platform/unity/android/uiwidgets_panel.cc
  3. 16
      engine/src/shell/platform/unity/android/uiwidgets_panel.h
  4. 98
      engine/src/shell/platform/unity/android/unity_surface_manager.cc
  5. 18
      engine/src/shell/platform/unity/android/unity_surface_manager.h

2
engine/Build.bee.cs


var builtNP = np.SetupSpecificConfiguration(config, androidToolchain.DynamicLibraryFormat).DeployTo("build");
builtNP.DeployTo("../Samples/UIWidgetsSamples_2019_4/Assets/Plugins/Android");
builtNP.DeployTo("/Users/siyao/Documents/GitHub/untitled folder 2/unityLibrary/src/main/jniLibs/armeabi-v7a");
builtNP.DeployTo("/Users/siyao/Documents/GitHub/untitled folder 3/unityLibrary/src/main/jniLibs/armeabi-v7a");
}
np.ValidConfigurations = validConfigurations;
}

887
engine/src/shell/platform/unity/android/uiwidgets_panel.cc


#include "uiwidgets_panel.h"
// #include <Windows.h>
// #include <include\utils\SkBase64.h>
#include <fstream>
#include <iostream>

#include "shell/common/switches.h"
#include "shell/platform/embedder/embedder_engine.h"
#include "uiwidgets_system.h"
// #include "unity_external_texture_gl.h"
namespace uiwidgets {
namespace uiwidgets
{
fml::RefPtr<UIWidgetsPanel> UIWidgetsPanel::Create(
Mono_Handle handle, EntrypointCallback entrypoint_callback) {
return fml::MakeRefCounted<UIWidgetsPanel>(handle, entrypoint_callback);
}
fml::RefPtr<UIWidgetsPanel> UIWidgetsPanel::Create(
Mono_Handle handle, UIWidgetsWindowType window_type, EntrypointCallback entrypoint_callback)
{
return fml::MakeRefCounted<UIWidgetsPanel>(handle, window_type, entrypoint_callback);
}
UIWidgetsPanel::UIWidgetsPanel(Mono_Handle handle,
EntrypointCallback entrypoint_callback)
: handle_(handle), entrypoint_callback_(entrypoint_callback) {}
UIWidgetsPanel::UIWidgetsPanel(Mono_Handle handle,
UIWidgetsWindowType window_type,
EntrypointCallback entrypoint_callback)
: handle_(handle), window_type_(window_type), entrypoint_callback_(entrypoint_callback) {}
UIWidgetsPanel::~UIWidgetsPanel() = default;
UIWidgetsPanel::~UIWidgetsPanel() = default;
void UIWidgetsPanel::OnEnable(void* native_texture_ptr, size_t width,
size_t height, float device_pixel_ratio,
const char* streaming_assets_path,
const char* settings) {
UnityConsole::WriteLine("test ggg ++");
surface_manager_ = std::make_unique<UnitySurfaceManager>(
UIWidgetsSystem::GetInstancePtr()->GetUnityInterfaces());
bool UIWidgetsPanel::NeedUpdateByPlayerLoop()
{
return window_type_ == GameObjectPanel;
}
FML_DCHECK(fbo_ == 0);
surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
fbo_ = surface_manager_->CreateRenderSurface(native_texture_ptr);
surface_manager_->ClearCurrent();
bool UIWidgetsPanel::NeedUpdateByEditorLoop()
{
return window_type_ == EditorWindowPanel;
}
fml::AutoResetWaitableEvent latch;
std::thread::id gfx_worker_thread_id;
UIWidgetsSystem::GetInstancePtr()->PostTaskToGfxWorker(
[&latch, &gfx_worker_thread_id]() -> void {
gfx_worker_thread_id = std::this_thread::get_id();
latch.Signal();
});
latch.Wait();
void UIWidgetsPanel::OnEnable(void *native_texture_ptr, size_t width,
size_t height, float device_pixel_ratio,
const char *streaming_assets_path,
const char *settings)
{
surface_manager_ = std::make_unique<UnitySurfaceManager>(
UIWidgetsSystem::GetInstancePtr()->GetUnityInterfaces());
gfx_worker_task_runner_ = std::make_unique<GfxWorkerTaskRunner>(
gfx_worker_thread_id, [this](const auto* task) {
if (UIWidgetsEngineRunTask(engine_, task) != kSuccess) {
std::cerr << "Could not post an gfx worker task." << std::endl;
}
});
FML_DCHECK(fbo_ == 0);
surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
fbo_ = surface_manager_->CreateRenderSurface(native_texture_ptr);
surface_manager_->ClearCurrent();
UIWidgetsTaskRunnerDescription render_task_runner = {};
render_task_runner.struct_size = sizeof(UIWidgetsTaskRunnerDescription);
render_task_runner.identifier = 1;
render_task_runner.user_data = gfx_worker_task_runner_.get();
render_task_runner.runs_task_on_current_thread_callback =
[](void* user_data) -> bool {
return static_cast<GfxWorkerTaskRunner*>(user_data)
->RunsTasksOnCurrentThread();
};
render_task_runner.post_task_callback = [](UIWidgetsTask task,
uint64_t target_time_nanos,
void* user_data) -> void {
static_cast<GfxWorkerTaskRunner*>(user_data)->PostTask(task,
target_time_nanos);
};
fml::AutoResetWaitableEvent latch;
std::thread::id gfx_worker_thread_id;
UIWidgetsSystem::GetInstancePtr()->PostTaskToGfxWorker(
[&latch, &gfx_worker_thread_id]() -> void {
gfx_worker_thread_id = std::this_thread::get_id();
latch.Signal();
});
latch.Wait();
UIWidgetsRendererConfig config = {};
config.type = kOpenGL;
config.open_gl.struct_size = sizeof(config.open_gl);
config.open_gl.clear_current = [](void* user_data) -> bool {
auto* panel = static_cast<UIWidgetsPanel*>(user_data);
return panel->surface_manager_->ClearCurrent();
};
config.open_gl.make_current = [](void* user_data) -> bool {
auto* panel = static_cast<UIWidgetsPanel*>(user_data);
return panel->surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
};
config.open_gl.make_resource_current = [](void* user_data) -> bool {
auto* panel = static_cast<UIWidgetsPanel*>(user_data);
return panel->surface_manager_->MakeResourceCurrent();
};
config.open_gl.fbo_callback = [](void* user_data) -> uint32_t {
auto* panel = static_cast<UIWidgetsPanel*>(user_data);
return panel->fbo_;
};
config.open_gl.present = [](void* user_data) -> bool { return true; };
config.open_gl.gl_proc_resolver = [](void* user_data,
const char* what) -> void* {
return reinterpret_cast<void*>(eglGetProcAddress(what));
};
config.open_gl.fbo_reset_after_present = true;
gfx_worker_task_runner_ = std::make_unique<GfxWorkerTaskRunner>(
gfx_worker_thread_id, [this](const auto *task) {
if (UIWidgetsEngineRunTask(engine_, task) != kSuccess)
{
std::cerr << "Could not post an gfx worker task." << std::endl;
}
});
task_runner_ = std::make_unique<CocoaTaskRunner>(
gettid(), [this](const auto* task) {
if (UIWidgetsEngineRunTask(engine_, task) != kSuccess) {
std::cerr << "Could not post an engine task." << std::endl;
}
});
UIWidgetsTaskRunnerDescription render_task_runner = {};
render_task_runner.struct_size = sizeof(UIWidgetsTaskRunnerDescription);
render_task_runner.identifier = 1;
render_task_runner.user_data = gfx_worker_task_runner_.get();
render_task_runner.runs_task_on_current_thread_callback =
[](void *user_data) -> bool {
return static_cast<GfxWorkerTaskRunner *>(user_data)
->RunsTasksOnCurrentThread();
};
render_task_runner.post_task_callback = [](UIWidgetsTask task,
uint64_t target_time_nanos,
void *user_data) -> void {
static_cast<GfxWorkerTaskRunner *>(user_data)->PostTask(task,
target_time_nanos);
};
UIWidgetsTaskRunnerDescription ui_task_runner = {};
ui_task_runner.struct_size = sizeof(UIWidgetsTaskRunnerDescription);
ui_task_runner.identifier = 2;
ui_task_runner.user_data = task_runner_.get();
UIWidgetsRendererConfig config = {};
config.type = kOpenGL;
config.open_gl.struct_size = sizeof(config.open_gl);
config.open_gl.clear_current = [](void *user_data) -> bool {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
return panel->surface_manager_->ClearCurrent();
};
config.open_gl.make_current = [](void *user_data) -> bool {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
return panel->surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
};
config.open_gl.make_resource_current = [](void *user_data) -> bool {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
return panel->surface_manager_->MakeResourceCurrent();
};
config.open_gl.fbo_callback = [](void *user_data) -> uint32_t {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
return panel->fbo_;
};
config.open_gl.present = [](void *user_data) -> bool { return true; };
config.open_gl.gl_proc_resolver = [](void *user_data,
const char *what) -> void * {
return reinterpret_cast<void *>(eglGetProcAddress(what));
};
config.open_gl.fbo_reset_after_present = true;
// TODO: waht is this
ui_task_runner.runs_task_on_current_thread_callback =
[](void* user_data) -> bool {
return static_cast<CocoaTaskRunner*>(user_data)->RunsTasksOnCurrentThread();
};
ui_task_runner.post_task_callback = [](UIWidgetsTask task,
uint64_t target_time_nanos,
void* user_data) -> void {
static_cast<CocoaTaskRunner*>(user_data)->PostTask(task, target_time_nanos);
};
task_runner_ = std::make_unique<CocoaTaskRunner>(
gettid(), [this](const auto *task) {
if (UIWidgetsEngineRunTask(engine_, task) != kSuccess)
{
std::cerr << "Could not post an engine task." << std::endl;
}
});
UIWidgetsCustomTaskRunners custom_task_runners = {};
custom_task_runners.struct_size = sizeof(UIWidgetsCustomTaskRunners);
custom_task_runners.platform_task_runner = &ui_task_runner;
custom_task_runners.ui_task_runner = &ui_task_runner;
custom_task_runners.render_task_runner = &render_task_runner;
UIWidgetsTaskRunnerDescription ui_task_runner = {};
ui_task_runner.struct_size = sizeof(UIWidgetsTaskRunnerDescription);
ui_task_runner.identifier = 2;
ui_task_runner.user_data = task_runner_.get();
UIWidgetsProjectArgs args = {};
args.struct_size = sizeof(UIWidgetsProjectArgs);
// TODO: waht is this
ui_task_runner.runs_task_on_current_thread_callback =
[](void *user_data) -> bool {
return static_cast<CocoaTaskRunner *>(user_data)->RunsTasksOnCurrentThread();
};
ui_task_runner.post_task_callback = [](UIWidgetsTask task,
uint64_t target_time_nanos,
void *user_data) -> void {
static_cast<CocoaTaskRunner *>(user_data)->PostTask(task, target_time_nanos);
};
args.assets_path = streaming_assets_path;
args.font_asset = settings;
UIWidgetsCustomTaskRunners custom_task_runners = {};
custom_task_runners.struct_size = sizeof(UIWidgetsCustomTaskRunners);
custom_task_runners.platform_task_runner = &ui_task_runner;
custom_task_runners.ui_task_runner = &ui_task_runner;
custom_task_runners.render_task_runner = &render_task_runner;
// // std::string icu_path = std::string(streaming_assets_path) + "/icudtl.dat";
// // args.icu_data_path = icu_path.c_str();
UIWidgetsProjectArgs args = {};
args.struct_size = sizeof(UIWidgetsProjectArgs);
args.icu_mapper = GetICUStaticMapping;
args.assets_path = streaming_assets_path;
args.font_asset = settings;
// // Used for IOS build
// // std::string icu_symbol_prefix = "_binary_icudtl_dat_start";
// // std::string native_lib_path =
// // "pathtodll/Plugins/x86_64/libUIWidgets_d.dll"; args.icu_mapper =
// // [icu_symbol_prefix, native_lib_path] {
// // return GetSymbolMapping(icu_symbol_prefix, native_lib_path);
// // };
args.command_line_argc = 0;
args.command_line_argv = nullptr;
// // std::string icu_path = std::string(streaming_assets_path) + "/icudtl.dat";
// // args.icu_data_path = icu_path.c_str();
args.platform_message_callback =
[](const UIWidgetsPlatformMessage* engine_message,
void* user_data) -> void {};
args.icu_mapper = GetICUStaticMapping;
args.custom_task_runners = &custom_task_runners;
args.task_observer_add = [](intptr_t key, void* callback,
void* user_data) -> void {
auto* panel = static_cast<UIWidgetsPanel*>(user_data);
panel->task_runner_->AddTaskObserver(key,
*static_cast<fml::closure*>(callback));
};
args.task_observer_remove = [](intptr_t key, void* user_data) -> void {
auto* panel = static_cast<UIWidgetsPanel*>(user_data);
panel->task_runner_->RemoveTaskObserver(key);
};
// // Used for IOS build
// // std::string icu_symbol_prefix = "_binary_icudtl_dat_start";
// // std::string native_lib_path =
// // "pathtodll/Plugins/x86_64/libUIWidgets_d.dll"; args.icu_mapper =
// // [icu_symbol_prefix, native_lib_path] {
// // return GetSymbolMapping(icu_symbol_prefix, native_lib_path);
// // };
args.command_line_argc = 0;
args.command_line_argv = nullptr;
args.custom_mono_entrypoint = [](void* user_data) -> void {
auto* panel = static_cast<UIWidgetsPanel*>(user_data);
panel->MonoEntrypoint();
};
args.platform_message_callback =
[](const UIWidgetsPlatformMessage *engine_message,
void *user_data) -> void {};
args.vsync_callback = [](void* user_data, intptr_t baton) -> void {
auto* panel = static_cast<UIWidgetsPanel*>(user_data);
panel->VSyncCallback(baton);
};
args.custom_task_runners = &custom_task_runners;
args.task_observer_add = [](intptr_t key, void *callback,
void *user_data) -> void {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
panel->task_runner_->AddTaskObserver(key,
*static_cast<fml::closure *>(callback));
};
args.task_observer_remove = [](intptr_t key, void *user_data) -> void {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
panel->task_runner_->RemoveTaskObserver(key);
};
args.initial_window_metrics.width = width;
args.initial_window_metrics.height = height;
args.initial_window_metrics.pixel_ratio = device_pixel_ratio;
args.custom_mono_entrypoint = [](void *user_data) -> void {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
panel->MonoEntrypoint();
};
UIWidgetsEngine engine = nullptr;
auto result = UIWidgetsEngineInitialize(&config, &args, this, &engine);
args.vsync_callback = [](void *user_data, intptr_t baton) -> void {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
panel->VSyncCallback(baton);
};
if (result != kSuccess || engine == nullptr) {
std::cerr << "Failed to start UIWidgets engine: error " << result
<< std::endl;
return;
}
args.initial_window_metrics.width = width;
args.initial_window_metrics.height = height;
args.initial_window_metrics.pixel_ratio = device_pixel_ratio;
engine_ = engine;
UIWidgetsEngineRunInitialized(engine);
UIWidgetsEngine engine = nullptr;
auto result = UIWidgetsEngineInitialize(&config, &args, this, &engine);
UIWidgetsSystem::GetInstancePtr()->RegisterPanel(this);
if (result != kSuccess || engine == nullptr)
{
std::cerr << "Failed to start UIWidgets engine: error " << result
<< std::endl;
return;
}
process_events_ = true;
}
engine_ = engine;
UIWidgetsEngineRunInitialized(engine);
void UIWidgetsPanel::MonoEntrypoint() { entrypoint_callback_(handle_); }
UIWidgetsSystem::GetInstancePtr()->RegisterPanel(this);
void UIWidgetsPanel::OnDisable() {
// drain pending messages
ProcessMessages();
process_events_ = true;
}
// drain pending vsync batons
ProcessVSync();
void UIWidgetsPanel::MonoEntrypoint() { entrypoint_callback_(handle_); }
process_events_ = false;
void UIWidgetsPanel::OnDisable()
{
// drain pending messages
ProcessMessages();
UIWidgetsSystem::GetInstancePtr()->UnregisterPanel(this);
// drain pending vsync batons
ProcessVSync();
if (engine_) {
UIWidgetsEngineShutdown(engine_);
engine_ = nullptr;
}
process_events_ = false;
gfx_worker_task_runner_ = nullptr;
task_runner_ = nullptr;
UIWidgetsSystem::GetInstancePtr()->UnregisterPanel(this);
if (fbo_) {
surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
if (engine_)
{
UIWidgetsEngineShutdown(engine_);
engine_ = nullptr;
}
surface_manager_->DestroyRenderSurface();
fbo_ = 0;
gfx_worker_task_runner_ = nullptr;
task_runner_ = nullptr;
surface_manager_->ClearCurrent();
}
if (fbo_)
{
surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
surface_manager_ = nullptr;
}
surface_manager_->DestroyRenderSurface();
fbo_ = 0;
void UIWidgetsPanel::OnRenderTexture(void* native_texture_ptr, size_t width,
size_t height, float device_pixel_ratio) {
reinterpret_cast<EmbedderEngine*>(engine_)->PostRenderThreadTask(
[this, native_texture_ptr]() -> void {
surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
surface_manager_->ClearCurrent();
}
if (fbo_) {
surface_manager_->DestroyRenderSurface();
fbo_ = 0;
}
fbo_ = surface_manager_->CreateRenderSurface(native_texture_ptr);
surface_manager_ = nullptr;
}
surface_manager_->ClearCurrent();
});
void UIWidgetsPanel::OnRenderTexture(void *native_texture_ptr, size_t width,
size_t height, float device_pixel_ratio)
{
reinterpret_cast<EmbedderEngine *>(engine_)->PostRenderThreadTask(
[this, native_texture_ptr]() -> void {
surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
ViewportMetrics metrics;
metrics.physical_width = static_cast<float>(width);
metrics.physical_height = static_cast<float>(height);
metrics.device_pixel_ratio = device_pixel_ratio;
reinterpret_cast<EmbedderEngine*>(engine_)->SetViewportMetrics(metrics);
}
if (fbo_)
{
surface_manager_->DestroyRenderSurface();
fbo_ = 0;
}
fbo_ = surface_manager_->CreateRenderSurface(native_texture_ptr);
int UIWidgetsPanel::RegisterTexture(void* native_texture_ptr) {
int texture_identifier = 0;
// texture_identifier++;
surface_manager_->ClearCurrent();
});
// auto* engine = reinterpret_cast<EmbedderEngine*>(engine_);
ViewportMetrics metrics;
metrics.physical_width = static_cast<float>(width);
metrics.physical_height = static_cast<float>(height);
metrics.device_pixel_ratio = device_pixel_ratio;
reinterpret_cast<EmbedderEngine *>(engine_)->SetViewportMetrics(metrics);
}
// engine->GetShell().GetPlatformView()->RegisterTexture(
// std::make_unique<UnityExternalTextureGL>(
// texture_identifier, native_texture_ptr, surface_manager_.get()));
return texture_identifier;
}
int UIWidgetsPanel::RegisterTexture(void *native_texture_ptr)
{
int texture_identifier = 0;
// texture_identifier++;
void UIWidgetsPanel::UnregisterTexture(int texture_id) {
auto* engine = reinterpret_cast<EmbedderEngine*>(engine_);
engine->GetShell().GetPlatformView()->UnregisterTexture(texture_id);
}
// auto* engine = reinterpret_cast<EmbedderEngine*>(engine_);
std::chrono::nanoseconds UIWidgetsPanel::ProcessMessages() {
return std::chrono::nanoseconds(task_runner_->ProcessTasks().count());
}
// engine->GetShell().GetPlatformView()->RegisterTexture(
// std::make_unique<UnityExternalTextureGL>(
// texture_identifier, native_texture_ptr, surface_manager_.get()));
return texture_identifier;
}
void UIWidgetsPanel::ProcessVSync() {
std::vector<intptr_t> batons;
vsync_batons_.swap(batons);
void UIWidgetsPanel::UnregisterTexture(int texture_id)
{
auto *engine = reinterpret_cast<EmbedderEngine *>(engine_);
engine->GetShell().GetPlatformView()->UnregisterTexture(texture_id);
}
for (intptr_t baton : batons) {
reinterpret_cast<EmbedderEngine*>(engine_)->OnVsyncEvent(
baton, fml::TimePoint::Now(),
fml::TimePoint::Now() +
fml::TimeDelta::FromNanoseconds(1000000000 / 60));
std::chrono::nanoseconds UIWidgetsPanel::ProcessMessages()
{
return std::chrono::nanoseconds(task_runner_->ProcessTasks().count());
}
void UIWidgetsPanel::VSyncCallback(intptr_t baton) {
vsync_batons_.push_back(baton);
}
void UIWidgetsPanel::ProcessVSync()
{
std::vector<intptr_t> batons;
vsync_batons_.swap(batons);
void UIWidgetsPanel::SetEventPhaseFromCursorButtonState(
UIWidgetsPointerEvent* event_data) {
MouseState state = GetMouseState();
event_data->phase = state.buttons == 0
? state.state_is_down ? UIWidgetsPointerPhase::kUp
: UIWidgetsPointerPhase::kHover
: state.state_is_down ? UIWidgetsPointerPhase::kMove
: UIWidgetsPointerPhase::kDown;
}
for (intptr_t baton : batons)
{
reinterpret_cast<EmbedderEngine *>(engine_)->OnVsyncEvent(
baton, fml::TimePoint::Now(),
fml::TimePoint::Now() +
fml::TimeDelta::FromNanoseconds(1000000000 / 60));
}
}
void UIWidgetsPanel::SendMouseMove(float x, float y) {
UIWidgetsPointerEvent event = {};
event.x = x;
event.y = y;
SetEventPhaseFromCursorButtonState(&event);
SendPointerEventWithData(event);
}
void UIWidgetsPanel::VSyncCallback(intptr_t baton)
{
vsync_batons_.push_back(baton);
}
void UIWidgetsPanel::SendMouseDown(float x, float y) {
UIWidgetsPointerEvent event = {};
SetEventPhaseFromCursorButtonState(&event);
event.x = x;
event.y = y;
SendPointerEventWithData(event);
SetMouseStateDown(true);
}
void UIWidgetsPanel::SetEventPhaseFromCursorButtonState(
UIWidgetsPointerEvent *event_data)
{
MouseState state = GetMouseState();
event_data->phase = state.buttons == 0
? state.state_is_down ? UIWidgetsPointerPhase::kUp
: UIWidgetsPointerPhase::kHover
: state.state_is_down ? UIWidgetsPointerPhase::kMove
: UIWidgetsPointerPhase::kDown;
}
void UIWidgetsPanel::SendMouseUp(float x, float y) {
UIWidgetsPointerEvent event = {};
SetEventPhaseFromCursorButtonState(&event);
event.x = x;
event.y = y;
SendPointerEventWithData(event);
if (event.phase == UIWidgetsPointerPhase::kUp) {
SetMouseStateDown(false);
void UIWidgetsPanel::SendMouseMove(float x, float y)
{
UIWidgetsPointerEvent event = {};
event.x = x;
event.y = y;
SetEventPhaseFromCursorButtonState(&event);
SendPointerEventWithData(event);
}
void UIWidgetsPanel::SendMouseLeave() {
UIWidgetsPointerEvent event = {};
event.phase = UIWidgetsPointerPhase::kRemove;
SendPointerEventWithData(event);
}
void UIWidgetsPanel::SendPointerEventWithData(
const UIWidgetsPointerEvent& event_data) {
MouseState mouse_state = GetMouseState();
// If sending anything other than an add, and the pointer isn't already added,
// synthesize an add to satisfy Flutter's expectations about events.
if (!mouse_state.state_is_added &&
event_data.phase != UIWidgetsPointerPhase::kAdd) {
void UIWidgetsPanel::SendMouseDown(float x, float y)
{
event.phase = UIWidgetsPointerPhase::kAdd;
event.x = event_data.x;
event.y = event_data.y;
event.buttons = 0;
SetEventPhaseFromCursorButtonState(&event);
event.x = x;
event.y = y;
}
// Don't double-add (e.g., if events are delivered out of order, so an add has
// already been synthesized).
if (mouse_state.state_is_added &&
event_data.phase == UIWidgetsPointerPhase::kAdd) {
return;
SetMouseStateDown(true);
UIWidgetsPointerEvent event = event_data;
event.device_kind = kUIWidgetsPointerDeviceKindMouse;
event.buttons = mouse_state.buttons;
void UIWidgetsPanel::SendMouseUp(float x, float y)
{
UIWidgetsPointerEvent event = {};
SetEventPhaseFromCursorButtonState(&event);
event.x = x;
event.y = y;
SendPointerEventWithData(event);
if (event.phase == UIWidgetsPointerPhase::kUp)
{
SetMouseStateDown(false);
}
}
// Set metadata that's always the same regardless of the event.
event.struct_size = sizeof(event);
event.timestamp =
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count();
void UIWidgetsPanel::SendMouseLeave()
{
UIWidgetsPointerEvent event = {};
event.phase = UIWidgetsPointerPhase::kRemove;
SendPointerEventWithData(event);
}
UIWidgetsEngineSendPointerEvent(engine_, &event, 1);
void UIWidgetsPanel::SendPointerEventWithData(
const UIWidgetsPointerEvent &event_data)
{
MouseState mouse_state = GetMouseState();
// If sending anything other than an add, and the pointer isn't already added,
// synthesize an add to satisfy Flutter's expectations about events.
if (!mouse_state.state_is_added &&
event_data.phase != UIWidgetsPointerPhase::kAdd)
{
UIWidgetsPointerEvent event = {};
event.phase = UIWidgetsPointerPhase::kAdd;
event.x = event_data.x;
event.y = event_data.y;
event.buttons = 0;
SendPointerEventWithData(event);
}
// Don't double-add (e.g., if events are delivered out of order, so an add has
// already been synthesized).
if (mouse_state.state_is_added &&
event_data.phase == UIWidgetsPointerPhase::kAdd)
{
return;
}
if (event_data.phase == UIWidgetsPointerPhase::kAdd) {
SetMouseStateAdded(true);
} else if (event_data.phase == UIWidgetsPointerPhase::kRemove) {
SetMouseStateAdded(false);
ResetMouseState();
}
}
UIWidgetsPointerEvent event = event_data;
event.device_kind = kUIWidgetsPointerDeviceKindMouse;
event.buttons = mouse_state.buttons;
void UIWidgetsPanel::OnKeyDown(int keyCode, bool isKeyDown) {
if (process_events_) {
UIWidgetsPointerEvent event = {};
event.phase = isKeyDown ? UIWidgetsPointerPhase::kMouseDown : UIWidgetsPointerPhase::kMouseUp;
event.device_kind =
UIWidgetsPointerDeviceKind::kUIWidgetsPointerDeviceKindKeyboard;
event.buttons = keyCode;
// Set metadata that's always the same regardless of the event.
event.struct_size = sizeof(event);
event.timestamp =
std::chrono::duration_cast<std::chrono::microseconds>(

UIWidgetsEngineSendPointerEvent(engine_, &event, 1);
if (event_data.phase == UIWidgetsPointerPhase::kAdd)
{
SetMouseStateAdded(true);
}
else if (event_data.phase == UIWidgetsPointerPhase::kRemove)
{
SetMouseStateAdded(false);
ResetMouseState();
}
}
void UIWidgetsPanel::OnKeyDown(int keyCode, bool isKeyDown)
{
if (process_events_)
{
UIWidgetsPointerEvent event = {};
event.phase = isKeyDown ? UIWidgetsPointerPhase::kMouseDown : UIWidgetsPointerPhase::kMouseUp;
event.device_kind =
UIWidgetsPointerDeviceKind::kUIWidgetsPointerDeviceKindKeyboard;
event.buttons = keyCode;
event.struct_size = sizeof(event);
event.timestamp =
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count();
void UIWidgetsPanel::OnMouseMove(float x, float y) {
if (process_events_) {
SendMouseMove(x, y);
UIWidgetsEngineSendPointerEvent(engine_, &event, 1);
}
}
void UIWidgetsPanel::OnMouseMove(float x, float y)
{
if (process_events_)
{
SendMouseMove(x, y);
}
}
static uint64_t ConvertToUIWidgetsButton(int button) {
switch (button) {
static uint64_t ConvertToUIWidgetsButton(int button)
{
switch (button)
{
case -1:
return kUIWidgetsPointerButtonMousePrimary;
case -2:

}
std::cerr << "Mouse button not recognized: " << button << std::endl;
return 0;
std::cerr << "Mouse button not recognized: " << button << std::endl;
return 0;
}
void UIWidgetsPanel::OnMouseDown(float x, float y, int button) {
if (process_events_) {
uint64_t uiwidgets_button = ConvertToUIWidgetsButton(button);
if (uiwidgets_button != 0) {
uint64_t mouse_buttons = GetMouseState().buttons | uiwidgets_button;
SetMouseButtons(mouse_buttons);
SendMouseDown(x, y);
void UIWidgetsPanel::OnMouseDown(float x, float y, int button)
{
if (process_events_)
{
uint64_t uiwidgets_button = ConvertToUIWidgetsButton(button);
if (uiwidgets_button != 0)
{
uint64_t mouse_buttons = GetMouseState().buttons | uiwidgets_button;
SetMouseButtons(mouse_buttons);
SendMouseDown(x, y);
}
}
void UIWidgetsPanel::OnMouseUp(float x, float y, int button) {
if (process_events_) {
uint64_t uiwidgets_button = ConvertToUIWidgetsButton(button);
if (uiwidgets_button != 0) {
uint64_t mouse_buttons = GetMouseState().buttons & ~uiwidgets_button;
SetMouseButtons(mouse_buttons);
SendMouseUp(x, y);
void UIWidgetsPanel::OnMouseUp(float x, float y, int button)
{
if (process_events_)
{
uint64_t uiwidgets_button = ConvertToUIWidgetsButton(button);
if (uiwidgets_button != 0)
{
uint64_t mouse_buttons = GetMouseState().buttons & ~uiwidgets_button;
SetMouseButtons(mouse_buttons);
SendMouseUp(x, y);
}
}
void UIWidgetsPanel::OnMouseLeave() {
if (process_events_) {
SendMouseLeave();
void UIWidgetsPanel::OnMouseLeave()
{
if (process_events_)
{
SendMouseLeave();
}
}
UIWIDGETS_API(UIWidgetsPanel*)
UIWidgetsPanel_constructor(
Mono_Handle handle,
UIWidgetsPanel::EntrypointCallback entrypoint_callback) {
UnityConsole::WriteLine("??? constructor ggg ++");
UIWIDGETS_API(UIWidgetsPanel *)
UIWidgetsPanel_constructor(
Mono_Handle handle, int windowType,
UIWidgetsPanel::EntrypointCallback entrypoint_callback)
{
UIWidgetsWindowType window_type = static_cast<UIWidgetsWindowType>(windowType);
const auto panel = UIWidgetsPanel::Create(handle, window_type, entrypoint_callback);
panel->AddRef();
// entrypoint_callback(handle);
return panel.get();
}
const auto panel = UIWidgetsPanel::Create(handle, entrypoint_callback);
panel->AddRef();
return panel.get();
}
UIWIDGETS_API(void)
UIWidgetsPanel_dispose(UIWidgetsPanel *panel)
{
panel->Release();
}
UIWIDGETS_API(void) UIWidgetsPanel_dispose(UIWidgetsPanel* panel) {
panel->Release();
}
UIWIDGETS_API(void)
UIWidgetsPanel_onEnable(UIWidgetsPanel *panel, void *native_texture_ptr,
size_t width, size_t height, float device_pixel_ratio,
const char *streaming_assets_path,
const char *settings)
{
panel->OnEnable(native_texture_ptr, width, height, device_pixel_ratio,
streaming_assets_path, settings);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onEnable(UIWidgetsPanel* panel, void* native_texture_ptr,
size_t width, size_t height, float device_pixel_ratio,
const char* streaming_assets_path,
const char* settings) {
panel->OnEnable(native_texture_ptr, width, height, device_pixel_ratio,
streaming_assets_path, settings);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onDisable(UIWidgetsPanel *panel)
{
panel->OnDisable();
}
UIWIDGETS_API(void) UIWidgetsPanel_onDisable(UIWidgetsPanel* panel) {
panel->OnDisable();
}
UIWIDGETS_API(void)
UIWidgetsPanel_onRenderTexture(UIWidgetsPanel *panel, void *native_texture_ptr,
int width, int height, float dpi)
{
panel->OnRenderTexture(native_texture_ptr, width, height, dpi);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onRenderTexture(UIWidgetsPanel* panel, void* native_texture_ptr,
int width, int height, float dpi) {
panel->OnRenderTexture(native_texture_ptr, width, height, dpi);
}
UIWIDGETS_API(int)
UIWidgetsPanel_registerTexture(UIWidgetsPanel *panel,
void *native_texture_ptr)
{
return panel->RegisterTexture(native_texture_ptr);
}
UIWIDGETS_API(int)
UIWidgetsPanel_registerTexture(UIWidgetsPanel* panel,
void* native_texture_ptr) {
return panel->RegisterTexture(native_texture_ptr);
}
UIWIDGETS_API(void)
UIWidgetsPanel_unregisterTexture(UIWidgetsPanel *panel, int texture_id)
{
panel->UnregisterTexture(texture_id);
}
UIWIDGETS_API(void)
UIWidgetsPanel_unregisterTexture(UIWidgetsPanel* panel, int texture_id) {
panel->UnregisterTexture(texture_id);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onKey(UIWidgetsPanel* panel, int keyCode, bool isKeyDown) {
panel->OnKeyDown(keyCode, isKeyDown);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseDown(UIWidgetsPanel* panel, float x, float y,
int button) {
panel->OnMouseDown(x, y, button);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseUp(UIWidgetsPanel* panel, float x, float y, int button) {
panel->OnMouseUp(x, y, button);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseMove(UIWidgetsPanel* panel, float x, float y) {
panel->OnMouseMove(x, y);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseLeave(UIWidgetsPanel* panel) { panel->OnMouseLeave(); }
UIWIDGETS_API(void)
UIWidgetsPanel_onKey(UIWidgetsPanel *panel, int keyCode, bool isKeyDown)
{
panel->OnKeyDown(keyCode, isKeyDown);
}
UIWIDGETS_API(void*)
SetTextureFromUnity2(void* tex, int w, int h) { return UnitySurfaceManager::SetTextureFromUnity(tex, w, h); }
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseDown(UIWidgetsPanel *panel, float x, float y,
int button)
{
panel->OnMouseDown(x, y, button);
}
static void UNITY_INTERFACE_API OnGetUnityContextEvent(int eventID)
{
UnitySurfaceManager::GetUnityContext();
}
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseUp(UIWidgetsPanel *panel, float x, float y, int button)
{
panel->OnMouseUp(x, y, button);
}
// --------------------------------------------------------------------------
// GetRenderEventFunc, an example function we export which is used to get a rendering event callback function.
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseMove(UIWidgetsPanel *panel, float x, float y)
{
panel->OnMouseMove(x, y);
}
UIWIDGETS_API(UnityRenderingEvent) GetUnityContextEventFunc()
{
return OnGetUnityContextEvent;
}
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseLeave(UIWidgetsPanel *panel) { panel->OnMouseLeave(); }
static void UNITY_INTERFACE_API OnRenderEvent(int eventID)
{
UnitySurfaceManager::SetUp();
}
static void UNITY_INTERFACE_API OnGetUnityContextEvent(int eventID)
{
UnitySurfaceManager::GetUnityContext();
}
// --------------------------------------------------------------------------
// GetRenderEventFunc, an example function we export which is used to get a rendering event callback function.
// --------------------------------------------------------------------------
// GetRenderEventFunc, an example function we export which is used to get a rendering event callback function.
UIWIDGETS_API(UnityRenderingEvent) GetRenderEventFunc()
{
return OnRenderEvent;
}
UIWIDGETS_API(UnityRenderingEvent)
GetUnityContextEventFunc()
{
return OnGetUnityContextEvent;
}
UIWIDGETS_API(void)
draw_xxx() { UnitySurfaceManager::drawxxx(); }
} // namespace uiwidgets
} // namespace uiwidgets

16
engine/src/shell/platform/unity/android/uiwidgets_panel.h


#include "unity_surface_manager.h"
#include "cocoa_task_runner.h"
#include "src/shell/platform/unity/unity_console.h"
enum UIWidgetsWindowType {
InvalidPanel = 0,
GameObjectPanel = 1,
EditorWindowPanel = 2
};
struct MouseState {
bool state_is_down = false;
bool state_is_added = false;

typedef void (*EntrypointCallback)(Mono_Handle handle);
static fml::RefPtr<UIWidgetsPanel> Create(
Mono_Handle handle, EntrypointCallback entrypoint_callback);
Mono_Handle handle, UIWidgetsWindowType window_type, EntrypointCallback entrypoint_callback);
~UIWidgetsPanel();

void OnMouseLeave();
bool NeedUpdateByPlayerLoop();
bool NeedUpdateByEditorLoop();
UIWidgetsPanel(Mono_Handle handle, EntrypointCallback entrypoint_callback);
UIWidgetsPanel(Mono_Handle handle, UIWidgetsWindowType window_type, EntrypointCallback entrypoint_callback);
MouseState GetMouseState() { return mouse_state_; }

void SendPointerEventWithData(const UIWidgetsPointerEvent& event_data);
Mono_Handle handle_;
UIWidgetsWindowType window_type_;
EntrypointCallback entrypoint_callback_;
std::unique_ptr<UnitySurfaceManager> surface_manager_;

98
engine/src/shell/platform/unity/android/unity_surface_manager.cc


#include "unity_surface_manager.h"
// #include <d3d11.h>
// #include <dxgi.h>
// # include <OpenGL/gl3.h>
#include "include/gpu/vk/GrVkBackendContext.h"
// #include "src/gpu/gl/GrGLDefines.h"
#include "src/shell/common/shell_io_manager.h"
#include "src/shell/gpu/gpu_surface_delegate.h"
#include "src/shell/gpu/gpu_surface_gl_delegate.h"

bool UnitySurfaceManager::MakeCurrent(const EGLSurface surface)
{
UnityConsole::WriteLine("test c++: clear current");
return eglMakeCurrent(egl_display_, surface, surface, egl_context_) ==
EGL_TRUE;
}

void UnitySurfaceManager::CleanUp()
{
}
void UnitySurfaceManager::draw()
{
}
static void *g_TextureHandle = NULL;
static int wi = 0;
static int he = 0;
static EGLDisplay egl_display_s;
static EGLConfig egl_config_s;
static EGLContext egl_context_s;
static GLenum state;
static GLuint fbo_s = 0;
void *UnitySurfaceManager::SetTextureFromUnity(void *tex, int w, int h)
{
g_TextureHandle = tex;
wi = w;
he = h;
return nullptr;
}
void UnitySurfaceManager::SetUp()
{
auto read_s = eglGetCurrentSurface(EGL_READ);
auto draw_s = eglGetCurrentSurface(EGL_DRAW);
egl_display_s = eglGetCurrentDisplay();
auto egl_context_s_old = eglGetCurrentContext();
GLuint gltex = (GLuint)(size_t)(g_TextureHandle);
bool success = false;
std::tie(success, egl_config_s) = ChooseEGLConfiguration(egl_display_s);
EGLint attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
egl_context_s = eglCreateContext(egl_display_s, egl_config_s, egl_context_s_old, attributes);
auto state = eglMakeCurrent(egl_display_s, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context_s) == GL_TRUE;
GLint old_framebuffer_binding;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_framebuffer_binding);
glGenFramebuffers(1, &fbo_s);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_s);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gltex, 0);
FML_CHECK(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
glBindFramebuffer(GL_FRAMEBUFFER, old_framebuffer_binding);
eglMakeCurrent(egl_display_s, draw_s, read_s, egl_context_s_old);
}
static int siyao = 0;
void UnitySurfaceManager::drawxxx()
{
int width = wi;
int height = he;
int rowPitch = width * 4;
void *data = new unsigned char[rowPitch * height];
unsigned char *dst = (unsigned char *)data;
siyao++;
for (int y = 0; y < height; ++y)
{
unsigned char *ptr = dst;
for (int x = 0; x < width; ++x)
{
int vv = (244 + siyao) % 244;
// Write the texture pixel
ptr[0] = vv;
ptr[1] = (x + y + siyao) % 244;
ptr[2] = (x * y * siyao) % 244;
ptr[3] = vv;
ptr += 4;
}
dst += rowPitch;
}
GLuint gltex = (GLuint)(size_t)(g_TextureHandle);
auto state = eglMakeCurrent(egl_display_s, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context_s) == GL_TRUE;
glBindTexture(GL_TEXTURE_2D, gltex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
// glClearColor(1.0, 0, 1.0, 1.0);
// glClear(GL_COLOR_BUFFER_BIT);
eglMakeCurrent(egl_display_s, EGL_NO_DISPLAY, EGL_NO_DISPLAY, EGL_NO_CONTEXT);
delete[](unsigned char *) data;
}
} // namespace uiwidgets

18
engine/src/shell/platform/unity/android/unity_surface_manager.h


#include <EGL/eglplatform.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
// #include <d3d11.h>
// #include <windows.h>
#include <cstdint>

#include "include/core/SkTextBlob.h"
#include "include/effects/SkPerlinNoiseShader.h"
#include "include/gpu/GrBackendSurface.h"
// #include "include/gpu/GrContext.h"
// #include "modules/skottie/include/Skottie.h"
// #include "platform_base.h"
// #include "render_api.h"
// #include "src/gpu/gl/GrGLDefines.h"
// #include "windows.h"
// #include <wingdi.h>
#include "Unity/IUnityGraphicsVulkan.h"
#include "src/shell/platform/unity/unity_console.h"
// #define VulkanX
namespace uiwidgets
{

public:
static UnitySurfaceManager instance;
static void GetUnityContext();
static void *SetTextureFromUnity(void *tex, int w, int h);
static void SetUp();
static void drawxxx();
UnitySurfaceManager(IUnityInterfaces *unity_interfaces);
~UnitySurfaceManager();

FML_DISALLOW_COPY_AND_ASSIGN(UnitySurfaceManager);
private:
void draw();
bool Initialize(IUnityInterfaces *unity_interfaces);
void CleanUp();

正在加载...
取消
保存