Skip to content

Commit 665302e

Browse files
committed
platform task runner
1 parent e686e2b commit 665302e

File tree

2 files changed

+85
-24
lines changed

2 files changed

+85
-24
lines changed

src/wayland_display.cc

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
// found in the LICENSE file.
55

66
#include "wayland_display.h"
7-
#include "utils.h"
87

98
#include <linux/input-event-codes.h>
9+
#include <rapidjson/document.h>
10+
#include <rapidjson/stringbuffer.h>
11+
#include <rapidjson/writer.h>
1012
#include <sys/mman.h>
1113
#include <sys/types.h>
1214
#include <unistd.h>
1315
#include <xkbcommon/xkbcommon.h>
16+
1417
#include <chrono>
1518
#include <sstream>
1619
#include <thread>
1720
#include <vector>
1821

19-
#include <rapidjson/document.h>
20-
#include <rapidjson/stringbuffer.h>
21-
#include <rapidjson/writer.h>
22+
#include "utils.h"
2223

2324
namespace flutter {
2425

@@ -68,7 +69,6 @@ void WaylandDisplay::InitializeApplication(
6869
config.open_gl.make_resource_current = [](void* context) -> bool {
6970
return reinterpret_cast<WaylandDisplay*>(context)->GLMakeResourceCurrent();
7071
};
71-
7272
config.open_gl.gl_proc_resolver = [](void* context,
7373
const char* name) -> void* {
7474
auto address = eglGetProcAddress(name);
@@ -78,6 +78,14 @@ void WaylandDisplay::InitializeApplication(
7878
FLWAY_ERROR << "Tried unsuccessfully to resolve: " << name << std::endl;
7979
return nullptr;
8080
};
81+
#if 0
82+
config.open_gl.gl_external_texture_frame_callback =
83+
[](void* context, int64_t texture_id, size_t width, size_t height,
84+
FlutterOpenGLTexture* texture) -> bool {
85+
return reinterpret_cast<WaylandDisplay*>(context)
86+
->GLExternalTextureFrameCallback(texture_id, width, height, texture);
87+
};
88+
#endif
8189

8290
auto icu_data_path = GetICUDataPath();
8391

@@ -106,12 +114,34 @@ void WaylandDisplay::InitializeApplication(
106114
message);
107115
};
108116

117+
// Configure task runner interop
118+
FlutterTaskRunnerDescription platform_task_runner = {};
119+
platform_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription);
120+
platform_task_runner.user_data = this;
121+
platform_task_runner.runs_task_on_current_thread_callback =
122+
[](void* context) -> bool { return true; };
123+
platform_task_runner.post_task_callback =
124+
[](FlutterTask task, uint64_t target_time, void* context) -> void {
125+
reinterpret_cast<WaylandDisplay*>(context)->PostTaskCallback(task,
126+
target_time);
127+
};
128+
129+
FlutterCustomTaskRunners custom_task_runners = {};
130+
custom_task_runners.struct_size = sizeof(FlutterCustomTaskRunners);
131+
custom_task_runners.platform_task_runner = &platform_task_runner;
132+
args.custom_task_runners = &custom_task_runners;
133+
109134
FlutterEngine engine = nullptr;
110-
auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args,
111-
this /* userdata */, &engine_);
135+
auto result = FlutterEngineInitialize(FLUTTER_ENGINE_VERSION, &config, &args,
136+
this, &engine_);
137+
if (result != kSuccess) {
138+
FLWAY_ERROR << "Could not Initialize the Flutter engine" << std::endl;
139+
return;
140+
}
112141

142+
result = FlutterEngineRunInitialized(engine_);
113143
if (result != kSuccess) {
114-
FLWAY_ERROR << "Could not run the Flutter engine" << std::endl;
144+
FLWAY_ERROR << "Could not run the initialized Flutter engine" << std::endl;
115145
return;
116146
}
117147

@@ -131,10 +161,6 @@ bool WaylandDisplay::SetWindowSize(size_t width, size_t height) {
131161
return FlutterEngineSendWindowMetricsEvent(engine_, &event) == kSuccess;
132162
}
133163

134-
void WaylandDisplay::ProcessEvents() {
135-
__FlutterEngineFlushPendingTasksNow();
136-
}
137-
138164
WaylandDisplay::WaylandDisplay(size_t width,
139165
size_t height,
140166
std::string bundle_path,
@@ -519,6 +545,15 @@ bool WaylandDisplay::Run() {
519545
// event loop
520546
while (running && valid_) {
521547
display.dispatch();
548+
549+
if (!TaskRunner.empty()) {
550+
uint64_t current = FlutterEngineGetCurrentTime();
551+
if (current >= TaskRunner.top().first) {
552+
auto item = TaskRunner.top();
553+
TaskRunner.pop();
554+
auto result = FlutterEngineRunTask(engine_, &item.second);
555+
}
556+
}
522557
}
523558

524559
return true;
@@ -572,6 +607,18 @@ bool WaylandDisplay::GLMakeResourceCurrent() {
572607
eglresourcecontext) == EGL_TRUE);
573608
}
574609

610+
bool WaylandDisplay::GLExternalTextureFrameCallback(
611+
int64_t texture_id,
612+
size_t width,
613+
size_t height,
614+
FlutterOpenGLTexture* texture) {
615+
return true;
616+
}
617+
618+
void WaylandDisplay::PostTaskCallback(FlutterTask task, uint64_t target_time) {
619+
TaskRunner.push(std::make_pair(target_time, task));
620+
}
621+
575622
void WaylandDisplay::PlatformMessageCallback(
576623
const FlutterPlatformMessage* message) {
577624
FLWAY_LOG << "Channel: " << message->channel << std::endl;

src/wayland_display.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55

66
#pragma once
77

8+
#include <GLES3/gl3.h>
9+
#include <flutter_embedder.h>
10+
#include <linux/input.h>
11+
812
#include <array>
13+
#include <functional>
914
#include <iostream>
15+
#include <queue>
1016
#include <stdexcept>
17+
#include <vector>
1118
#include <wayland-client-protocol-extra.hpp>
1219
#include <wayland-client.hpp>
1320
#include <wayland-egl.hpp>
1421

15-
#include <GLES3/gl3.h>
16-
17-
#include <linux/input.h>
18-
19-
#include <flutter_embedder.h>
20-
21-
#include <functional>
22-
#include <vector>
23-
2422
#include "macros.h"
2523

2624
using namespace wayland;
@@ -87,17 +85,33 @@ class WaylandDisplay {
8785

8886
void init_egl();
8987

90-
void ProcessEvents();
91-
9288
bool GLMakeCurrent();
9389
bool GLClearCurrent();
9490
bool GLPresent();
9591
uint32_t GLFboCallback();
9692
bool GLMakeResourceCurrent();
93+
bool GLExternalTextureFrameCallback(int64_t texture_id,
94+
size_t width,
95+
size_t height,
96+
FlutterOpenGLTexture* texture);
97+
98+
class CompareDist {
99+
public:
100+
bool operator()(std::pair<uint64_t, FlutterTask> n1,
101+
std::pair<uint64_t, FlutterTask> n2) {
102+
return n1.first > n2.first;
103+
}
104+
};
105+
std::priority_queue<std::pair<uint64_t, FlutterTask>,
106+
std::vector<std::pair<uint64_t, FlutterTask>>,
107+
CompareDist>
108+
TaskRunner;
109+
110+
void PostTaskCallback(FlutterTask task, uint64_t target_time);
97111

98112
void PlatformMessageCallback(const FlutterPlatformMessage* message);
99113

100114
FLWAY_DISALLOW_COPY_AND_ASSIGN(WaylandDisplay);
101-
};
115+
}; // namespace flutter
102116

103117
} // namespace flutter

0 commit comments

Comments
 (0)