Skip to content

Commit

Permalink
Switch TizenEventLoop to use CurrentTimeProc (flutter-tizen#137)
Browse files Browse the repository at this point in the history
* Switch TizenEventLoop to use CurrentTimeProc

* Additional clean-ups
  • Loading branch information
swift-kim authored Jul 10, 2021
1 parent 39fd66f commit 14319f6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
6 changes: 5 additions & 1 deletion shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ template("embedder_for_profile") {
}

defines = invoker.defines
defines += [ "FLUTTER_ENGINE_NO_PROTOTYPES" ]

if (use_evas_gl_renderer || enable_desktop_embeddings) {
sources += [ "tizen_renderer_evas_gl.cc" ]
Expand Down Expand Up @@ -260,7 +261,10 @@ template("embedder_executable") {
"evas",
]

defines = [ "TIZEN_RENDERER_EVAS_GL" ]
defines = [
"FLUTTER_ENGINE_NO_PROTOTYPES",
"TIZEN_RENDERER_EVAS_GL",
]

cflags_cc = [
"-Wno-newline-eof",
Expand Down
3 changes: 2 additions & 1 deletion shell/platform/tizen/flutter_tizen_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ FlutterTizenEngine::FlutterTizenEngine(const FlutterProjectBundle& project)
// thread). UI threads need to send flutter task to platform thread.
event_loop_ = std::make_unique<TizenPlatformEventLoop>(
std::this_thread::get_id(), // main thread
[this](const auto* task) {
embedder_api_.GetCurrentTime, [this](const auto* task) {
if (embedder_api_.RunTask(this->engine_, task) != kSuccess) {
FT_LOGE("Could not post an engine task.");
}
Expand Down Expand Up @@ -74,6 +74,7 @@ void FlutterTizenEngine::InitializeRenderer(int32_t x,

render_loop_ = std::make_unique<TizenRenderEventLoop>(
std::this_thread::get_id(), // main thread
embedder_api_.GetCurrentTime,
[this](const auto* task) {
if (embedder_api_.RunTask(this->engine_, task) != kSuccess) {
FT_LOGE("Could not post an engine task.");
Expand Down
15 changes: 10 additions & 5 deletions shell/platform/tizen/tizen_event_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
namespace flutter {

TizenEventLoop::TizenEventLoop(std::thread::id main_thread_id,
CurrentTimeProc get_current_time,
TaskExpiredCallback on_task_expired)
: main_thread_id_(main_thread_id),
get_current_time_(get_current_time),
on_task_expired_(std::move(on_task_expired)) {
ecore_pipe_ = ecore_pipe_add(ExcuteTaskEvents, this);
}
Expand Down Expand Up @@ -54,7 +56,7 @@ TizenEventLoop::TaskTimePoint TizenEventLoop::TimePointFromFlutterTime(
uint64_t flutter_target_time_nanos) {
const auto now = TaskTimePoint::clock::now();
const int64_t flutter_duration =
flutter_target_time_nanos - FlutterEngineGetCurrentTime();
flutter_target_time_nanos - get_current_time_();
return now + std::chrono::nanoseconds(flutter_duration);
}

Expand Down Expand Up @@ -83,7 +85,7 @@ void TizenEventLoop::ExcuteTaskEvents(void* data,

const double flutter_duration =
(static_cast<double>(p_task->fire_time.time_since_epoch().count()) -
FlutterEngineGetCurrentTime()) /
self->get_current_time_()) /
1000000000.0;
if (flutter_duration > 0) {
{
Expand All @@ -102,8 +104,9 @@ void TizenEventLoop::ExcuteTaskEvents(void* data,

TizenPlatformEventLoop::TizenPlatformEventLoop(
std::thread::id main_thread_id,
CurrentTimeProc get_current_time,
TaskExpiredCallback on_task_expired)
: TizenEventLoop(main_thread_id, on_task_expired) {}
: TizenEventLoop(main_thread_id, get_current_time, on_task_expired) {}

TizenPlatformEventLoop::~TizenPlatformEventLoop() {}

Expand All @@ -116,9 +119,11 @@ void TizenPlatformEventLoop::OnTaskExpired() {

#ifdef TIZEN_RENDERER_EVAS_GL
TizenRenderEventLoop::TizenRenderEventLoop(std::thread::id main_thread_id,
CurrentTimeProc get_current_time,
TaskExpiredCallback on_task_expired,
TizenRenderer* renderer)
: TizenEventLoop(main_thread_id, on_task_expired), renderer_(renderer) {
: TizenEventLoop(main_thread_id, get_current_time, on_task_expired),
renderer_(renderer) {
evas_object_image_pixels_get_callback_set(
static_cast<TizenRendererEvasGL*>(renderer_)->GetImageHandle(),
[](void* data, Evas_Object* o) { // Render callback
Expand Down Expand Up @@ -150,6 +155,6 @@ void TizenRenderEventLoop::OnTaskExpired() {
// Do nothing
}
}
#endif
#endif // TIZEN_RENDERER_EVAS_GL

} // namespace flutter
26 changes: 19 additions & 7 deletions shell/platform/tizen/tizen_event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@

namespace flutter {

typedef uint64_t (*CurrentTimeProc)();

class TizenRenderer;

class TizenEventLoop {
public:
using TaskExpiredCallback = std::function<void(const FlutterTask*)>;

TizenEventLoop(std::thread::id main_thread_id,
CurrentTimeProc get_current_time,
TaskExpiredCallback on_task_expired);
virtual ~TizenEventLoop();

// Prevent copying.
TizenEventLoop(const TizenEventLoop&) = delete;
TizenEventLoop& operator=(const TizenEventLoop&) = delete;

bool RunsTasksOnCurrentThread() const;

void ExcuteTaskEvents(
Expand All @@ -41,6 +50,7 @@ class TizenEventLoop {

protected:
using TaskTimePoint = std::chrono::steady_clock::time_point;

struct Task {
uint64_t order;
TaskTimePoint fire_time;
Expand All @@ -55,7 +65,9 @@ class TizenEventLoop {
}
};
};

std::thread::id main_thread_id_;
CurrentTimeProc get_current_time_;
TaskExpiredCallback on_task_expired_;
std::mutex task_queue_mutex_;
std::priority_queue<Task, std::deque<Task>, Task::Comparer> task_queue_;
Expand All @@ -67,40 +79,40 @@ class TizenEventLoop {
private:
Ecore_Pipe* ecore_pipe_;

TizenEventLoop(const TizenEventLoop&) = delete;

TizenEventLoop& operator=(const TizenEventLoop&) = delete;
// Returns a TaskTimePoint computed from the given target time from Flutter.
TaskTimePoint TimePointFromFlutterTime(uint64_t flutter_target_time_nanos);

static void ExcuteTaskEvents(void* data, void* buffer, unsigned int nbyte);

static Eina_Bool TaskTimerCallback(void* data);

static TaskTimePoint TimePointFromFlutterTime(
uint64_t flutter_target_time_nanos);
};

class TizenPlatformEventLoop : public TizenEventLoop {
public:
TizenPlatformEventLoop(std::thread::id main_thread_id,
CurrentTimeProc get_current_time,
TaskExpiredCallback on_task_expired);
virtual ~TizenPlatformEventLoop();

virtual void OnTaskExpired() override;
};

#ifdef TIZEN_RENDERER_EVAS_GL
class TizenRenderEventLoop : public TizenEventLoop {
public:
TizenRenderEventLoop(std::thread::id main_thread_id,
CurrentTimeProc get_current_time,
TaskExpiredCallback on_task_expired,
TizenRenderer* renderer);
virtual ~TizenRenderEventLoop();

virtual void OnTaskExpired() override;

private:
TizenRenderer* renderer_{nullptr};
std::atomic_bool has_pending_renderer_callback_{false};
};
#endif
#endif // TIZEN_RENDERER_EVAS_GL

} // namespace flutter

Expand Down

0 comments on commit 14319f6

Please sign in to comment.