This repository was archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathuiwidgets_panel.cc
More file actions
511 lines (424 loc) · 16.5 KB
/
Copy pathuiwidgets_panel.cc
File metadata and controls
511 lines (424 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include "uiwidgets_panel.h"
#include <flutter/fml/synchronization/waitable_event.h>
#include <fstream>
#include <iostream>
#include "lib/ui/window/viewport_metrics.h"
#include "runtime/mono_api.h"
#include "shell/common/switches.h"
#include "shell/platform/embedder/embedder_engine.h"
#include "uiwidgets_system.h"
#include "third_party/Unity/IUnityGraphics.h"
namespace uiwidgets
{
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,
UIWidgetsWindowType window_type,
EntrypointCallback entrypoint_callback)
: handle_(handle), window_type_(window_type), entrypoint_callback_(entrypoint_callback) {}
UIWidgetsPanel::~UIWidgetsPanel() = default;
bool UIWidgetsPanel::NeedUpdateByPlayerLoop()
{
return window_type_ == GameObjectPanel;
}
bool UIWidgetsPanel::NeedUpdateByEditorLoop()
{
return window_type_ == EditorWindowPanel;
}
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());
FML_DCHECK(fbo_ == 0);
surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
fbo_ = surface_manager_->CreateRenderSurface(native_texture_ptr);
surface_manager_->ClearCurrent();
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();
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;
}
});
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);
};
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;
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 ui_task_runner = {};
ui_task_runner.struct_size = sizeof(UIWidgetsTaskRunnerDescription);
ui_task_runner.identifier = 2;
ui_task_runner.user_data = task_runner_.get();
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);
};
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;
UIWidgetsProjectArgs args = {};
args.struct_size = sizeof(UIWidgetsProjectArgs);
args.assets_path = streaming_assets_path;
args.font_asset = settings;
args.icu_mapper = GetICUStaticMapping;
// // 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.platform_message_callback =
[](const UIWidgetsPlatformMessage *engine_message,
void *user_data) -> void {};
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.custom_mono_entrypoint = [](void *user_data) -> void {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
panel->MonoEntrypoint();
};
args.vsync_callback = [](void *user_data, intptr_t baton) -> void {
auto *panel = static_cast<UIWidgetsPanel *>(user_data);
panel->VSyncCallback(baton);
};
args.initial_window_metrics.width = width;
args.initial_window_metrics.height = height;
args.initial_window_metrics.pixel_ratio = device_pixel_ratio;
UIWidgetsEngine engine = nullptr;
auto result = UIWidgetsEngineInitialize(&config, &args, this, &engine);
if (result != kSuccess || engine == nullptr)
{
std::cerr << "Failed to start UIWidgets engine: error " << result
<< std::endl;
return;
}
engine_ = engine;
UIWidgetsEngineRunInitialized(engine);
UIWidgetsSystem::GetInstancePtr()->RegisterPanel(this);
process_events_ = true;
}
void UIWidgetsPanel::MonoEntrypoint() { entrypoint_callback_(handle_); }
void UIWidgetsPanel::OnDisable()
{
// drain pending messages
ProcessMessages();
// drain pending vsync batons
ProcessVSync(0);
process_events_ = false;
UIWidgetsSystem::GetInstancePtr()->UnregisterPanel(this);
if (engine_)
{
UIWidgetsEngineShutdown(engine_);
engine_ = nullptr;
}
gfx_worker_task_runner_ = nullptr;
task_runner_ = nullptr;
if (fbo_)
{
surface_manager_->MakeCurrent(EGL_NO_DISPLAY);
surface_manager_->DestroyRenderSurface();
fbo_ = 0;
surface_manager_->ClearCurrent();
}
surface_manager_ = nullptr;
}
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);
if (fbo_)
{
surface_manager_->DestroyRenderSurface();
fbo_ = 0;
}
fbo_ = surface_manager_->CreateRenderSurface(native_texture_ptr);
surface_manager_->ClearCurrent();
});
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);
}
int UIWidgetsPanel::RegisterTexture(void *native_texture_ptr)
{
std::cerr << "registering external texture is not implemented for android" << std::endl;
int texture_identifier = 0;
// texture_identifier++;
// auto* engine = reinterpret_cast<EmbedderEngine*>(engine_);
// engine->GetShell().GetPlatformView()->RegisterTexture(
// std::make_unique<UnityExternalTextureGL>(
// texture_identifier, native_texture_ptr, surface_manager_.get()));
return texture_identifier;
}
void UIWidgetsPanel::UnregisterTexture(int texture_id)
{
std::cerr << "registering external texture is not implemented for android" << std::endl;
auto *engine = reinterpret_cast<EmbedderEngine *>(engine_);
engine->GetShell().GetPlatformView()->UnregisterTexture(texture_id);
}
std::chrono::nanoseconds UIWidgetsPanel::ProcessMessages()
{
return std::chrono::nanoseconds(task_runner_->ProcessTasks().count());
}
void UIWidgetsPanel::ProcessVSync(double frame_duration)
{
std::vector<intptr_t> batons;
vsync_batons_.swap(batons);
for (intptr_t baton : batons)
{
reinterpret_cast<EmbedderEngine *>(engine_)->OnVsyncEvent(
baton, fml::TimePoint::Now(),
fml::TimePoint::Now() +
fml::TimeDelta::FromNanoseconds(1000000000 * frame_duration));
}
}
void UIWidgetsPanel::VSyncCallback(intptr_t baton)
{
vsync_batons_.push_back(baton);
}
void UIWidgetsPanel::dispatchTouches(float x, float y, int button, UIWidgetsTouchPhase phase)
{
PointerData pointer_data;
pointer_data.Clear();
auto packet = std::make_unique<PointerDataPacket>(1);
pointer_data.time_stamp = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count();
pointer_data.change = UIWidgetsPanel::PointerDataChangeFromUITouchPhase(phase);
pointer_data.kind = UIWidgetsPanel::DeviceKindFromTouchType();
pointer_data.device = -button;
pointer_data.pointer_identifier = 0;
pointer_data.physical_x = x;
pointer_data.physical_y = y;
pointer_data.physical_delta_x = 0.0;
pointer_data.physical_delta_y = 0.0;
pointer_data.pressure = 1.0;
pointer_data.pressure_max = 1.0;
pointer_data.signal_kind = PointerData::SignalKind::kNone;
packet->SetPointerData(0, pointer_data);
reinterpret_cast<EmbedderEngine*>(engine_)->DispatchPointerDataPacket(
std::move(packet));
}
PointerData::Change UIWidgetsPanel::PointerDataChangeFromUITouchPhase(UIWidgetsTouchPhase phase)
{
switch(phase) {
case TouchBegan:
return PointerData::Change::kDown;
case TouchMoved:
return PointerData::Change::kMove;
case TouchEnded:
return PointerData::Change::kUp;
case TouchCancelled:
return PointerData::Change::kCancel;
default:
std::cerr << "Unhandled touch phase: " << phase << std::endl;
break;
}
return PointerData::Change::kCancel;
}
PointerData::DeviceKind UIWidgetsPanel::DeviceKindFromTouchType()
{
return PointerData::DeviceKind::kTouch;
}
void UIWidgetsPanel::OnKeyDown(int keyCode, bool isKeyDown, int64_t modifier)
{
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();
event.modifier = modifier;
UIWidgetsEngineSendPointerEvent(engine_, &event, 1);
}
}
void UIWidgetsPanel::OnMouseMove(float x, float y, int button)
{
if (process_events_)
{
dispatchTouches(x, y, button, TouchMoved);
}
}
void UIWidgetsPanel::OnMouseDown(float x, float y, int button)
{
if (process_events_)
{
dispatchTouches(x, y, button, TouchBegan);
}
}
void UIWidgetsPanel::OnMouseUp(float x, float y, int button)
{
if (process_events_)
{
dispatchTouches(x, y, button, TouchEnded);
}
}
void UIWidgetsPanel::OnMouseLeave()
{
//there should not be mouse leave events on iPhone!
std::cerr << "Invalid input on iPhone: mouse leave" << std::endl;
}
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();
}
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_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(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_onKey(UIWidgetsPanel* panel, int keyCode, bool isKeyDown, int64_t modifier) {
panel->OnKeyDown(keyCode, isKeyDown, modifier);
}
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, int button)
{
panel->OnMouseMove(x, y, button);
}
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseLeave(UIWidgetsPanel *panel) { panel->OnMouseLeave(); }
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.
UIWIDGETS_API(UnityRenderingEvent)
GetUnityContextEventFunc()
{
return OnGetUnityContextEvent;
}
} // namespace uiwidgets