Skip to content

Commit

Permalink
Fix vsync issue (#204)
Browse files Browse the repository at this point in the history
* Fix vsync issue

After the engine is shutdown, OnVsync no longer be called.

* Add lock when call OnVsync

1.Remove lock when create TdmClient.
2.Add lock when call OnVsync.
3.Change tdmClient to tdm_client.
  • Loading branch information
xiaowei-guan authored Nov 11, 2021
1 parent 1aba96b commit 919eb7e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions shell/platform/tizen/flutter_tizen_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ bool FlutterTizenEngine::StopEngine() {
if (plugin_registrar_destruction_callback_) {
plugin_registrar_destruction_callback_(plugin_registrar_.get());
}
#ifndef TIZEN_RENDERER_EVAS_GL
tizen_vsync_waiter_.reset();
#endif
FlutterEngineResult result = embedder_api_.Shutdown(engine_);
engine_ = nullptr;
return (result == kSuccess);
Expand Down
26 changes: 20 additions & 6 deletions shell/platform/tizen/tizen_vsync_waiter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ TizenVsyncWaiter::TizenVsyncWaiter(FlutterTizenEngine* engine)
}

TizenVsyncWaiter::~TizenVsyncWaiter() {
if (tdm_client_) {
tdm_client_->OnEngineStop();
}
Send(kMessageQuit, 0);
if (vblank_thread_) {
ecore_thread_cancel(vblank_thread_);
vblank_thread_ = nullptr;
}
}

void TizenVsyncWaiter::SetTdmClient(TdmClient* tdm_client) {
tdm_client_ = tdm_client;
}

void TizenVsyncWaiter::AsyncWaitForVsync(intptr_t baton) {
Send(kMessageRequestVblank, baton);
}
Expand Down Expand Up @@ -62,6 +69,7 @@ void TizenVsyncWaiter::RequestVblankLoop(void* data, Ecore_Thread* thread) {
TizenVsyncWaiter* tizen_vsync_waiter =
reinterpret_cast<TizenVsyncWaiter*>(data);
TdmClient tdm_client(tizen_vsync_waiter->engine_);
tizen_vsync_waiter->SetTdmClient(&tdm_client);
if (!tdm_client.IsValid()) {
FT_LOG(Error) << "Invalid tdm_client.";
ecore_thread_cancel(thread);
Expand Down Expand Up @@ -106,6 +114,11 @@ TdmClient::~TdmClient() {
DestroyTdm();
}

void TdmClient::OnEngineStop() {
std::lock_guard<std::mutex> lock(engine_mutex_);
engine_ = nullptr;
}

void TdmClient::WaitVblank(intptr_t baton) {
baton_ = baton;
tdm_error error = tdm_client_vblank_wait(vblank_, 1, VblankCallback, this);
Expand Down Expand Up @@ -164,12 +177,13 @@ void TdmClient::VblankCallback(tdm_client_vblank* vblank,
void* user_data) {
TdmClient* client = reinterpret_cast<TdmClient*>(user_data);
FT_ASSERT(client != nullptr);
FT_ASSERT(client->engine_ != nullptr);

uint64_t frame_start_time_nanos = tv_sec * 1e9 + tv_usec * 1e3;
uint64_t frame_target_time_nanos = 16.6 * 1e6 + frame_start_time_nanos;
client->engine_->OnVsync(client->baton_, frame_start_time_nanos,
frame_target_time_nanos);
std::lock_guard<std::mutex> lock(client->engine_mutex_);
if (client->engine_) {
uint64_t frame_start_time_nanos = tv_sec * 1e9 + tv_usec * 1e3;
uint64_t frame_target_time_nanos = 16.6 * 1e6 + frame_start_time_nanos;
client->engine_->OnVsync(client->baton_, frame_start_time_nanos,
frame_target_time_nanos);
}
}

} // namespace flutter
5 changes: 5 additions & 0 deletions shell/platform/tizen/tizen_vsync_waiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <Ecore.h>
#include <tdm_client.h>
#include <mutex>

#include "flutter/shell/platform/embedder/embedder.h"

Expand All @@ -22,6 +23,7 @@ class TdmClient {
void DestroyTdm();
bool IsValid();
void WaitVblank(intptr_t baton);
void OnEngineStop();
static void VblankCallback(tdm_client_vblank* vblank,
tdm_error error,
unsigned int sequence,
Expand All @@ -30,6 +32,7 @@ class TdmClient {
void* user_data);

private:
std::mutex engine_mutex_;
tdm_client* client_{nullptr};
tdm_client_output* output_{nullptr};
tdm_client_vblank* vblank_{nullptr};
Expand All @@ -42,13 +45,15 @@ class TizenVsyncWaiter {
TizenVsyncWaiter(FlutterTizenEngine* engine);
virtual ~TizenVsyncWaiter();
void AsyncWaitForVsync(intptr_t baton);
void SetTdmClient(TdmClient* tdm_client);

private:
void Send(int event, intptr_t baton);
static void RequestVblankLoop(void* data, Ecore_Thread* thread);
Ecore_Thread* vblank_thread_{nullptr};
Eina_Thread_Queue* vblank_thread_queue_{nullptr};
FlutterTizenEngine* engine_{nullptr};
TdmClient* tdm_client_{nullptr};
};

} // namespace flutter
Expand Down

0 comments on commit 919eb7e

Please sign in to comment.