Skip to content

Commit 42463e6

Browse files
committed
egl resource context
1 parent a126688 commit 42463e6

File tree

2 files changed

+70
-135
lines changed

2 files changed

+70
-135
lines changed

src/wayland_display.cc

Lines changed: 59 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,28 @@ void WaylandDisplay::InitializeApplication(
5050
FlutterRendererConfig config = {};
5151
config.type = kOpenGL;
5252
config.open_gl.struct_size = sizeof(config.open_gl);
53-
config.open_gl.make_current = [](void* userdata) -> bool {
54-
return reinterpret_cast<WaylandDisplay*>(userdata)
55-
->OnApplicationContextMakeCurrent();
53+
config.open_gl.make_current = [](void* context) -> bool {
54+
return reinterpret_cast<WaylandDisplay*>(context)
55+
->GLMakeCurrent();
5656
};
57-
config.open_gl.clear_current = [](void* userdata) -> bool {
58-
return reinterpret_cast<WaylandDisplay*>(userdata)
59-
->OnApplicationContextClearCurrent();
57+
config.open_gl.clear_current = [](void* context) -> bool {
58+
return reinterpret_cast<WaylandDisplay*>(context)
59+
->GLClearCurrent();
6060
};
61-
config.open_gl.present = [](void* userdata) -> bool {
62-
return reinterpret_cast<WaylandDisplay*>(userdata)
63-
->OnApplicationPresent();
61+
config.open_gl.present = [](void* context) -> bool {
62+
return reinterpret_cast<WaylandDisplay*>(context)
63+
->GLPresent();
6464
};
65-
config.open_gl.fbo_callback = [](void* userdata) -> uint32_t {
66-
return reinterpret_cast<WaylandDisplay*>(userdata)
67-
->OnApplicationGetOnscreenFBO();
65+
config.open_gl.fbo_callback = [](void* context) -> uint32_t {
66+
return reinterpret_cast<WaylandDisplay*>(context)
67+
->GLFboCallback();
6868
};
69-
#if 0
70-
config.open_gl.make_resource_current = [](void* userdata) -> bool {
71-
return reinterpret_cast<WaylandDisplay*>(userdata)
72-
->OnApplicationMakeResouceCurrent();
69+
config.open_gl.make_resource_current = [](void* context) -> bool {
70+
return reinterpret_cast<WaylandDisplay*>(context)
71+
->GLMakeResourceCurrent();
7372
};
74-
#endif
75-
config.open_gl.gl_proc_resolver = [](void* userdata,
73+
74+
config.open_gl.gl_proc_resolver = [](void* context,
7675
const char* name) -> void* {
7776
auto address = eglGetProcAddress(name);
7877
if (address != nullptr) {
@@ -103,10 +102,12 @@ void WaylandDisplay::InitializeApplication(
103102
args.icu_data_path = icu_data_path.c_str();
104103
args.command_line_argc = static_cast<int>(command_line_args_c.size());
105104
args.command_line_argv = command_line_args_c.data();
106-
args.platform_message_callback = [](const FlutterPlatformMessage* msg,
107-
void* userdata) -> void {
108-
return reinterpret_cast<WaylandDisplay*>(userdata)->OnPlatformMessage(msg);
109-
};
105+
args.platform_message_callback =
106+
[](const FlutterPlatformMessage* message, void* context) {
107+
reinterpret_cast<WaylandDisplay*>(context)
108+
->PlatformMessageCallback(message);
109+
};
110+
110111

111112
FlutterEngine engine = nullptr;
112113
auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args,
@@ -176,7 +177,6 @@ WaylandDisplay::WaylandDisplay(size_t width, size_t height,
176177
// create a shell surface
177178
if(xdg_wm_base)
178179
{
179-
std::cout << "Using xdg_wm_base" << std::endl;
180180
xdg_wm_base.on_ping() = [&] (uint32_t serial) { xdg_wm_base.pong(serial); };
181181
xdg_surface = xdg_wm_base.get_xdg_surface(surface);
182182
xdg_surface.on_configure() = [&] (uint32_t serial) { xdg_surface.ack_configure(serial); };
@@ -186,7 +186,6 @@ WaylandDisplay::WaylandDisplay(size_t width, size_t height,
186186
}
187187
else
188188
{
189-
std::cout << "Not using xdg_wm_base" << std::endl;
190189
shell_surface = shell.get_shell_surface(surface);
191190
shell_surface.on_ping() = [&] (uint32_t serial) { shell_surface.pong(serial); };
192191
shell_surface.set_title("Flutter");
@@ -196,17 +195,6 @@ WaylandDisplay::WaylandDisplay(size_t width, size_t height,
196195

197196
display.roundtrip();
198197

199-
// load cursor theme
200-
#if 0
201-
cursor_theme_t cursor_theme = cursor_theme_t("default", 16, shm);
202-
cursor_t cursor = cursor_theme.get_cursor("cross");
203-
cursor_image = cursor.image(0);
204-
cursor_buffer = cursor_image.get_buffer();
205-
206-
// create cursor surface
207-
cursor_surface = compositor.create_surface();
208-
#endif
209-
210198
if(has_touch)
211199
{
212200
std::cout << "Touch Present" << std::endl;
@@ -379,6 +367,8 @@ WaylandDisplay::~WaylandDisplay() noexcept(false) {
379367
// finialize EGL
380368
if(eglDestroyContext(egldisplay, eglcontext) == EGL_FALSE)
381369
throw std::runtime_error("eglDestroyContext");
370+
if(eglDestroyContext(egldisplay, eglresourcecontext) == EGL_FALSE)
371+
throw std::runtime_error("eglDestroyContext Resource");
382372
if(eglTerminate(egldisplay) == EGL_FALSE)
383373
throw std::runtime_error("eglTerminate");
384374

@@ -430,6 +420,11 @@ void WaylandDisplay::init_egl()
430420
eglcontext = eglCreateContext(egldisplay, config, EGL_NO_CONTEXT, context_attribs.data());
431421
if(eglcontext == EGL_NO_CONTEXT)
432422
throw std::runtime_error("eglCreateContext");
423+
424+
eglresourcecontext = eglCreateContext(
425+
egldisplay, config, eglcontext, context_attribs.data());
426+
if(eglresourcecontext == EGL_NO_CONTEXT)
427+
throw std::runtime_error("eglCreateContext");
433428
}
434429

435430
bool WaylandDisplay::Run() {
@@ -448,126 +443,69 @@ bool WaylandDisplay::Run() {
448443
return true;
449444
}
450445

451-
static void LogLastEGLError() {
452-
struct EGLNameErrorPair {
453-
const char* name;
454-
EGLint code;
455-
};
456-
457-
#define _EGL_ERROR_DESC(a) \
458-
{ #a, a }
459-
460-
const EGLNameErrorPair pairs[] = {
461-
_EGL_ERROR_DESC(EGL_SUCCESS),
462-
_EGL_ERROR_DESC(EGL_NOT_INITIALIZED),
463-
_EGL_ERROR_DESC(EGL_BAD_ACCESS),
464-
_EGL_ERROR_DESC(EGL_BAD_ALLOC),
465-
_EGL_ERROR_DESC(EGL_BAD_ATTRIBUTE),
466-
_EGL_ERROR_DESC(EGL_BAD_CONTEXT),
467-
_EGL_ERROR_DESC(EGL_BAD_CONFIG),
468-
_EGL_ERROR_DESC(EGL_BAD_CURRENT_SURFACE),
469-
_EGL_ERROR_DESC(EGL_BAD_DISPLAY),
470-
_EGL_ERROR_DESC(EGL_BAD_SURFACE),
471-
_EGL_ERROR_DESC(EGL_BAD_MATCH),
472-
_EGL_ERROR_DESC(EGL_BAD_PARAMETER),
473-
_EGL_ERROR_DESC(EGL_BAD_NATIVE_PIXMAP),
474-
_EGL_ERROR_DESC(EGL_BAD_NATIVE_WINDOW),
475-
_EGL_ERROR_DESC(EGL_CONTEXT_LOST),
476-
};
477-
478-
#undef _EGL_ERROR_DESC
479-
480-
const auto count = sizeof(pairs) / sizeof(EGLNameErrorPair);
481-
482-
EGLint last_error = eglGetError();
483-
484-
for (size_t i = 0; i < count; i++) {
485-
if (last_error == pairs[i].code) {
486-
FLWAY_ERROR << "EGL Error: " << pairs[i].name << " (" << pairs[i].code
487-
<< ")" << std::endl;
488-
return;
489-
}
446+
bool WaylandDisplay::GLMakeCurrent() {
447+
if (!valid_) {
448+
FLWAY_ERROR << "Invalid display." << std::endl;
449+
return false;
490450
}
491451

492-
FLWAY_ERROR << "Unknown EGL Error" << std::endl;
452+
return (eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext) == EGL_TRUE);
493453
}
494454

495-
bool WaylandDisplay::OnApplicationContextMakeCurrent() {
455+
bool WaylandDisplay::GLClearCurrent() {
496456
if (!valid_) {
497457
FLWAY_ERROR << "Invalid display." << std::endl;
498458
return false;
499459
}
500460

501-
if (eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext) !=
502-
EGL_TRUE) {
503-
FLWAY_ERROR << "Could not make the onscreen context current" << std::endl;
504-
LogLastEGLError();
505-
return false;
506-
}
507-
508-
return true;
461+
return (eglMakeCurrent(egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
462+
EGL_NO_CONTEXT) == EGL_TRUE);
509463
}
510464

511-
bool WaylandDisplay::OnApplicationContextClearCurrent() {
465+
bool WaylandDisplay::GLPresent() {
512466
if (!valid_) {
513467
FLWAY_ERROR << "Invalid display." << std::endl;
514468
return false;
515469
}
516470

517-
if (eglMakeCurrent(egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
518-
EGL_NO_CONTEXT) != EGL_TRUE) {
519-
FLWAY_ERROR << "Could not clear the context." << std::endl;
520-
LogLastEGLError();
521-
return false;
522-
}
523-
524-
return true;
471+
return (eglSwapBuffers(egldisplay, eglsurface) == EGL_TRUE);
525472
}
526473

527-
bool WaylandDisplay::OnApplicationPresent() {
474+
uint32_t WaylandDisplay::GLFboCallback() {
528475
if (!valid_) {
529476
FLWAY_ERROR << "Invalid display." << std::endl;
530-
return false;
531-
}
532-
533-
if (eglSwapBuffers(egldisplay, eglsurface) != EGL_TRUE) {
534-
FLWAY_ERROR << "Could not swap the EGL buffer." << std::endl;
535-
LogLastEGLError();
536-
return false;
477+
return 999;
537478
}
538479

539-
return true;
480+
return 0; // FBO0
540481
}
541482

542-
uint32_t WaylandDisplay::OnApplicationGetOnscreenFBO() {
483+
bool WaylandDisplay::GLMakeResourceCurrent() {
543484
if (!valid_) {
544485
FLWAY_ERROR << "Invalid display." << std::endl;
545-
return 999;
486+
return false;
546487
}
547488

548-
return 0; // FBO0
489+
return (eglMakeCurrent(egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
490+
eglresourcecontext) == EGL_TRUE);
549491
}
550492

551-
/// This is an optional callback. Flutter will ask the emebdder to create a GL
552-
/// context current on a background thread. If the embedder is able to do so,
553-
/// Flutter will assume that this context is in the same sharegroup as the
554-
/// main rendering context and use this context for asynchronous texture
555-
/// uploads.
556-
bool WaylandDisplay::OnApplicationMakeResouceCurrent() {
557-
return true;
493+
void WaylandDisplay::SetPlatformMessageCallback(
494+
const std::function<void(const FlutterPlatformMessage*)>& callback) {
495+
platform_message_callback_ = callback;
558496
}
559497

560-
void WaylandDisplay::OnPlatformMessage(const FlutterPlatformMessage* msg)
561-
{
562-
FLWAY_LOG << "Channel: " << msg->channel << std::endl;
563-
FLWAY_LOG << "Message Size: " << msg->message_size << std::endl;
564-
FLWAY_LOG << "Message: " << msg->message << std::endl;
498+
void WaylandDisplay::PlatformMessageCallback(
499+
const FlutterPlatformMessage* message) {
565500

566-
auto result = FlutterEngineSendPlatformMessageResponse(engine_, msg->response_handle, NULL, 0);
567-
if (result != kSuccess) {
568-
FLWAY_ERROR << "Could not send platform message response" << std::endl;
569-
return;
501+
FLWAY_LOG << "Channel: " << message->channel << std::endl;
502+
FLWAY_LOG << "Message Size: " << message->message_size << std::endl;
503+
FLWAY_LOG << "Message: " << message->message << std::endl;
504+
505+
if (platform_message_callback_) {
506+
platform_message_callback_(message);
570507
}
571508
}
572509

510+
573511
} // namespace flutter

src/wayland_display.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
//#include <EGL/egl.h>
1717

1818
#include <linux/input.h>
19-
#include <wayland-cursor.hpp>
2019

2120
#include <flutter_embedder.h>
2221

@@ -75,16 +74,13 @@ class WaylandDisplay {
7574
pointer_t pointer;
7675
keyboard_t keyboard;
7776
touch_t touch;
78-
callback_t frame_cb;
79-
cursor_image_t cursor_image;
80-
buffer_t cursor_buffer;
81-
surface_t cursor_surface;
8277

8378
// EGL
8479
egl_window_t egl_window;
8580
EGLDisplay egldisplay;
8681
EGLSurface eglsurface;
8782
EGLContext eglcontext;
83+
EGLContext eglresourcecontext;
8884

8985
bool running;
9086
bool has_pointer;
@@ -104,17 +100,18 @@ class WaylandDisplay {
104100

105101
void ProcessEvents();
106102

107-
bool OnApplicationContextMakeCurrent();
103+
bool GLMakeCurrent();
104+
bool GLClearCurrent();
105+
bool GLPresent();
106+
uint32_t GLFboCallback();
107+
bool GLMakeResourceCurrent();
108108

109-
bool OnApplicationContextClearCurrent();
109+
std::function<void(const FlutterPlatformMessage*)> platform_message_callback_;
110+
111+
void PlatformMessageCallback(const FlutterPlatformMessage* message);
110112

111-
bool OnApplicationPresent();
112-
113-
uint32_t OnApplicationGetOnscreenFBO();
114-
115-
bool OnApplicationMakeResouceCurrent();
116-
117-
void OnPlatformMessage(const FlutterPlatformMessage* msg);
113+
void SetPlatformMessageCallback(
114+
const std::function<void(const FlutterPlatformMessage*)>& callback);
118115

119116
FLWAY_DISALLOW_COPY_AND_ASSIGN(WaylandDisplay);
120117
};

0 commit comments

Comments
 (0)