• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_
6 #define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_
7 
8 #include <mutex>
9 #include <unordered_map>
10 
11 #include "flutter/fml/macros.h"
12 #include "flutter/fml/synchronization/thread_annotations.h"
13 #include "flutter/fml/task_runner.h"
14 
15 namespace flutter {
16 
17 //------------------------------------------------------------------------------
18 /// A task runner which delegates responsibility of task execution to an
19 /// embedder. This is done by managing a dispatch table to the embedder.
20 ///
21 class EmbedderTaskRunner final : public fml::TaskRunner {
22  public:
23   struct DispatchTable {
24     //--------------------------------------------------------------------------
25     /// Delegates responsibility of deferred task execution to the embedder.
26     /// Once the embedder gets the task, it must call
27     /// `EmbedderTaskRunner::PostTask` with the supplied `task_baton` on the
28     /// correct thread after the tasks `target_time` point expires.
29     ///
30     std::function<void(EmbedderTaskRunner* task_runner,
31                        uint64_t task_baton,
32                        fml::TimePoint target_time)>
33         post_task_callback;
34     //--------------------------------------------------------------------------
35     /// Asks the embedder if tasks posted to it on this task task runner via the
36     /// `post_task_callback` will be executed (after task expiry) on the calling
37     /// thread.
38     ///
39     std::function<bool(void)> runs_task_on_current_thread_callback;
40   };
41 
42   //----------------------------------------------------------------------------
43   /// @brief      Create a task runner with a dispatch table for delegation of
44   ///             task runner responsibility to the embedder. When embedders
45   ///             specify task runner dispatch tables that service tasks on the
46   ///             same thread, they also must ensure that their
47   ///             `embedder_idetifier`s match. This allows the engine to
48   ///             determine task runner equality without actually posting tasks
49   ///             to the task runner.
50   ///
51   /// @param[in]  table                The task runner dispatch table.
52   /// @param[in]  embedder_identifier  The embedder identifier
53   ///
54   EmbedderTaskRunner(DispatchTable table, size_t embedder_identifier);
55 
56   ~EmbedderTaskRunner() override;
57 
58   //----------------------------------------------------------------------------
59   /// @brief      The unique identifier provided by the embedder for the task
60   ///             runner. Embedders whose dispatch tables service tasks on the
61   ///             same underlying OS thread must ensure that their identifiers
62   ///             match. This allows the engine to determine task runner
63   ///             equality without posting tasks on the thread.
64   ///
65   /// @return     The embedder identifier.
66   ///
67   size_t GetEmbedderIdentifier() const;
68 
69   bool PostTask(uint64_t baton);
70 
71 private:
72   const size_t embedder_identifier_;
73   DispatchTable dispatch_table_;
74   std::mutex tasks_mutex_;
75   uint64_t last_baton_ FML_GUARDED_BY(tasks_mutex_);
76   std::unordered_map<uint64_t, fml::closure> pending_tasks_
77       FML_GUARDED_BY(tasks_mutex_);
78   fml::TaskQueueId placeholder_id_;
79 
80   // |fml::TaskRunner|
81   void PostTask(fml::closure task) override;
82 
83   // |fml::TaskRunner|
84   void PostTaskForTime(fml::closure task, fml::TimePoint target_time) override;
85 
86   // |fml::TaskRunner|
87   void PostDelayedTask(fml::closure task, fml::TimeDelta delay) override;
88 
89   // |fml::TaskRunner|
90   bool RunsTasksOnCurrentThread() override;
91 
92   // |fml::TaskRunner|
93   fml::TaskQueueId GetTaskQueueId() override;
94 
95   FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTaskRunner);
96 };
97 
98 }  // namespace flutter
99 
100 #endif  // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_
101