| Flutter Linux Embedder |

Classes | Typedefs | Functions | Variables
fl_task_runner.cc File Reference
`#include "flutter/shell/platform/linux/fl_task_runner.h"`
`#include "flutter/shell/platform/linux/fl_engine_private.h"`

Go to the source code of this file.

## Classes
struct | _FlTaskRunner
struct | _FlTaskRunnerTask

## Typedefs
typedef struct _FlTaskRunnerTask | FlTaskRunnerTask

## Functions
static void | fl_task_runner_process_expired_tasks_locked (FlTaskRunner *self)
static void | fl_task_runner_tasks_did_change_locked (FlTaskRunner *self)
static gboolean | fl_task_runner_on_expired_timeout (gpointer data)
static gint64 | fl_task_runner_next_task_expiration_time_locked (FlTaskRunner *self)
void | fl_task_runner_dispose (GObject *object)
static void | fl_task_runner_class_init (FlTaskRunnerClass *klass)
static void | fl_task_runner_init (FlTaskRunner *self)
FlTaskRunner * | fl_task_runner_new (FlEngine *engine)
void | fl_task_runner_post_flutter_task (FlTaskRunner *self, FlutterTask task, uint64_t target_time_nanos)
void | fl_task_runner_wait (FlTaskRunner *self)
void | fl_task_runner_stop_wait (FlTaskRunner *self)

## Variables
static constexpr int | kMicrosecondsPerNanosecond = 1000
static constexpr int | kMillisecondsPerMicrosecond = 1000

## Typedef Documentation

## ◆ FlTaskRunnerTask

typedef struct _FlTaskRunnerTask FlTaskRunnerTask

## Function Documentation

## ◆ fl_task_runner_class_init()

static void fl_task_runner_class_init | ( | FlTaskRunnerClass * | klass | ) |
| static

Definition at line 138 of file fl_task_runner.cc.

138 {
139 G_OBJECT_CLASS(klass)->dispose = fl_task_runner_dispose;
140 }
fl_task_runner_dispose
void fl_task_runner_dispose(GObject *object)
Definition: fl_task_runner.cc:123

References fl_task_runner_dispose().

## ◆ fl_task_runner_dispose()

void fl_task_runner_dispose | ( | GObject * | object | ) |

Definition at line 123 of file fl_task_runner.cc.

123 {
124 FlTaskRunner* self = FL_TASK_RUNNER(object);
125
126 g_weak_ref_clear(&self->engine);
127 g_mutex_clear(&self->mutex);
128 g_cond_clear(&self->cond);
129
130 g_list_free_full(self->pending_tasks, g_free);
131 if (self->timeout_source_id != 0) {
132 g_source_remove(self->timeout_source_id);
133 }
134
135 G_OBJECT_CLASS(fl_task_runner_parent_class)->dispose(object);
136 }

Referenced by fl_task_runner_class_init().

## ◆ fl_task_runner_init()

static void fl_task_runner_init | ( | FlTaskRunner * | self | ) |
| static

Definition at line 142 of file fl_task_runner.cc.

142 {
143 g_mutex_init(&self->mutex);
144 g_cond_init(&self->cond);
145 }

## ◆ fl_task_runner_new()

FlTaskRunner* fl_task_runner_new | ( | FlEngine * | engine | ) |

fl_task_runner_new: @engine: the #FlEngine owning the task runner.

Creates new task runner instance.

Returns: an #FlTaskRunner.

Definition at line 147 of file fl_task_runner.cc.

147 {
148 FlTaskRunner* self =
149 FL_TASK_RUNNER(g_object_new(fl_task_runner_get_type(), nullptr));
150 g_weak_ref_init(&self->engine, G_OBJECT(engine));
151 return self;
152 }

Referenced by fl_engine_init(), and TEST().

## ◆ fl_task_runner_next_task_expiration_time_locked()

static gint64 fl_task_runner_next_task_expiration_time_locked | ( | FlTaskRunner * | self | ) |
| static

Definition at line 96 of file fl_task_runner.cc.

97 {
98 gint64 min_time = G_MAXINT64;
99 GList* l = self->pending_tasks;
100 while (l != nullptr) {
101 FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
102 min_time = MIN(min_time, task->task_time_micros);
103 l = l->next;
104 }
105 return min_time;
106 }
_FlTaskRunnerTask
Definition: fl_task_runner.cc:23
_FlTaskRunnerTask::task_time_micros
gint64 task_time_micros
Definition: fl_task_runner.cc:25

References _FlTaskRunnerTask::task_time_micros.

Referenced by fl_task_runner_tasks_did_change_locked(), and fl_task_runner_wait().

## ◆ fl_task_runner_on_expired_timeout()

static gboolean fl_task_runner_on_expired_timeout | ( | gpointer | data | ) |
| static

Definition at line 75 of file fl_task_runner.cc.

75 {
76 FlTaskRunner* self = FL_TASK_RUNNER(data);
77
78 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
79 (void)locker; // unused variable
80
81 g_object_ref(self);
82
83 self->timeout_source_id = 0;
84 fl_task_runner_process_expired_tasks_locked(self);
85
86 // reschedule timeout
87 fl_task_runner_tasks_did_change_locked(self);
88
89 g_object_unref(self);
90
91 return FALSE;
92 }
g_autoptr
g_autoptr(GMutexLocker) locker
fl_task_runner_process_expired_tasks_locked
static void fl_task_runner_process_expired_tasks_locked(FlTaskRunner *self)
Definition: fl_task_runner.cc:36
fl_task_runner_tasks_did_change_locked
static void fl_task_runner_tasks_did_change_locked(FlTaskRunner *self)
Definition: fl_task_runner.cc:108

References fl_task_runner_process_expired_tasks_locked(), fl_task_runner_tasks_did_change_locked(), and g_autoptr().

Referenced by fl_task_runner_tasks_did_change_locked().

## ◆ fl_task_runner_post_flutter_task()

void fl_task_runner_post_flutter_task | ( | FlTaskRunner * | task_runner,
|  | FlutterTask | task,
|  | uint64_t | target_time_nanos
| ) |  |

fl_task_runner_post_flutter_task: @task_runner: an #FlTaskRunner. @task: Flutter task being scheduled @target_time_nanos: absolute time in nanoseconds

Posts a Flutter task to be executed on main thread. This function is thread safe and may be called from any thread.

Definition at line 154 of file fl_task_runner.cc.

156 {
157 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
158 (void)locker; // unused variable
159
160 FlTaskRunnerTask* runner_task = g_new0(FlTaskRunnerTask, 1);
161 runner_task->task = task;
162 runner_task->task_time_micros =
163 target_time_nanos / kMicrosecondsPerNanosecond;
164
165 self->pending_tasks = g_list_append(self->pending_tasks, runner_task);
166 fl_task_runner_tasks_did_change_locked(self);
167
168 // Tasks changed, so wake up anything blocking in fl_task_runner_wait.
169 g_cond_signal(&self->cond);
170 }
kMicrosecondsPerNanosecond
static constexpr int kMicrosecondsPerNanosecond
Definition: fl_task_runner.cc:8
_FlTaskRunnerTask::task
FlutterTask task
Definition: fl_task_runner.cc:29

References fl_task_runner_tasks_did_change_locked(), g_autoptr(), kMicrosecondsPerNanosecond, _FlTaskRunnerTask::task, and _FlTaskRunnerTask::task_time_micros.

Referenced by fl_engine_post_task().

## ◆ fl_task_runner_process_expired_tasks_locked()

static void fl_task_runner_process_expired_tasks_locked | ( | FlTaskRunner * | self | ) |
| static

Definition at line 36 of file fl_task_runner.cc.

36 {
37 GList* expired_tasks = nullptr;
38
39 gint64 current_time = g_get_monotonic_time();
40
41 GList* l = self->pending_tasks;
42 while (l != nullptr) {
43 FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
44 if (task->task_time_micros <= current_time) {
45 GList* link = l;
46 l = l->next;
47 self->pending_tasks = g_list_remove_link(self->pending_tasks, link);
48 expired_tasks = g_list_concat(expired_tasks, link);
49 } else {
50 l = l->next;
51 }
52 }
53
54 g_mutex_unlock(&self->mutex);
55
56 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
57 if (engine != nullptr) {
58 l = expired_tasks;
59 while (l != nullptr) {
60 FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
61 fl_engine_execute_task(engine, &task->task);
62 l = l->next;
63 }
64 }
65
66 g_list_free_full(expired_tasks, g_free);
67
68 g_mutex_lock(&self->mutex);
69 }
fl_engine_execute_task
void fl_engine_execute_task(FlEngine *self, FlutterTask *task)
Definition: fl_engine.cc:1458

References fl_engine_execute_task(), g_autoptr(), _FlTaskRunnerTask::task, and _FlTaskRunnerTask::task_time_micros.

Referenced by fl_task_runner_on_expired_timeout(), and fl_task_runner_wait().

## ◆ fl_task_runner_stop_wait()

void fl_task_runner_stop_wait | ( | FlTaskRunner * | self | ) |

fl_task_runner_stop_wait: @task_runner: an #FlTaskRunner.

Cause fl_task_runner_wait() to complete. May be called even if fl_task_runner_wait() is not being used.

May be called by any thread.

Definition at line 182 of file fl_task_runner.cc.

182 {
183 g_cond_signal(&self->cond);
184 }

## ◆ fl_task_runner_tasks_did_change_locked()

static void fl_task_runner_tasks_did_change_locked | ( | FlTaskRunner * | self | ) |
| static

Definition at line 108 of file fl_task_runner.cc.

108 {
109 // Reschedule timeout
110 if (self->timeout_source_id != 0) {
111 g_source_remove(self->timeout_source_id);
112 self->timeout_source_id = 0;
113 }
114 gint64 min_time = fl_task_runner_next_task_expiration_time_locked(self);
115 if (min_time != G_MAXINT64) {
116 gint64 remaining = MAX(min_time - g_get_monotonic_time(), 0);
117 self->timeout_source_id =
118 g_timeout_add(remaining / kMillisecondsPerMicrosecond + 1,
119 fl_task_runner_on_expired_timeout, self);
120 }
121 }
fl_task_runner_next_task_expiration_time_locked
static gint64 fl_task_runner_next_task_expiration_time_locked(FlTaskRunner *self)
Definition: fl_task_runner.cc:96
kMillisecondsPerMicrosecond
static constexpr int kMillisecondsPerMicrosecond
Definition: fl_task_runner.cc:9
fl_task_runner_on_expired_timeout
static gboolean fl_task_runner_on_expired_timeout(gpointer data)
Definition: fl_task_runner.cc:75

References fl_task_runner_next_task_expiration_time_locked(), fl_task_runner_on_expired_timeout(), and kMillisecondsPerMicrosecond.

Referenced by fl_task_runner_on_expired_timeout(), fl_task_runner_post_flutter_task(), and fl_task_runner_wait().

## ◆ fl_task_runner_wait()

void fl_task_runner_wait | ( | FlTaskRunner * | task_runner | ) |

fl_task_runner_wait: @task_runner: an #FlTaskRunner.

Block until the next task is ready and then perform it. May be interrupted by fl_task_runner_stop_wait(), in which case no task is run but execution will be returned to the caller.

Must be called only by the GTK thread.

Definition at line 172 of file fl_task_runner.cc.

172 {
173 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
174 (void)locker; // unused variable
175
176 g_cond_wait_until(&self->cond, &self->mutex,
177 fl_task_runner_next_task_expiration_time_locked(self));
178 fl_task_runner_process_expired_tasks_locked(self);
179 fl_task_runner_tasks_did_change_locked(self);
180 }

References fl_task_runner_next_task_expiration_time_locked(), fl_task_runner_process_expired_tasks_locked(), fl_task_runner_tasks_did_change_locked(), and g_autoptr().

Referenced by fl_compositor_opengl_render(), and fl_compositor_software_render().

## Variable Documentation

## ◆ kMicrosecondsPerNanosecond

constexpr int kMicrosecondsPerNanosecond = 1000
| static constexpr

Definition at line 8 of file fl_task_runner.cc.

Referenced by fl_task_runner_post_flutter_task().

## ◆ kMillisecondsPerMicrosecond

constexpr int kMillisecondsPerMicrosecond = 1000
| static constexpr

Definition at line 9 of file fl_task_runner.cc.

Referenced by fl_task_runner_tasks_did_change_locked().