Skip to content

Commit 6c699b2

Browse files
authored
Refactor TizenVsyncWaiter (#317)
1 parent 3d21ac7 commit 6c699b2

File tree

6 files changed

+111
-115
lines changed

6 files changed

+111
-115
lines changed

shell/platform/tizen/external_texture.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct ExternalTextureGLState {
2020
ExternalTextureExtensionType gl_extension;
2121
};
2222

23-
static std::atomic_long next_texture_id = {1};
23+
static std::atomic<int64_t> next_texture_id = {1};
2424

2525
class ExternalTexture {
2626
public:
@@ -30,18 +30,19 @@ class ExternalTexture {
3030
texture_id_(next_texture_id++) {
3131
state_->gl_extension = gl_extension;
3232
}
33+
3334
virtual ~ExternalTexture() = default;
3435

3536
// Returns the unique id for the ExternalTextureGL instance.
36-
int64_t TextureId() { return (int64_t)texture_id_; }
37+
int64_t TextureId() { return texture_id_; }
3738

3839
virtual bool PopulateTexture(size_t width,
3940
size_t height,
4041
FlutterOpenGLTexture* opengl_texture) = 0;
4142

4243
protected:
4344
std::unique_ptr<ExternalTextureGLState> state_;
44-
const long texture_id_{0};
45+
const int64_t texture_id_ = 0;
4546
};
4647

4748
} // namespace flutter

shell/platform/tizen/tizen_view_elementary.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
#include "flutter/shell/platform/tizen/logger.h"
1111
#include "flutter/shell/platform/tizen/tizen_view_event_handler_delegate.h"
1212

13+
namespace flutter {
14+
1315
namespace {
1416

15-
static const int kScrollDirectionVertical = 0;
16-
static const int kScrollDirectionHorizontal = 1;
17-
static const int kScrollOffsetMultiplier = 20;
17+
constexpr int kScrollDirectionVertical = 0;
18+
constexpr int kScrollDirectionHorizontal = 1;
19+
constexpr int kScrollOffsetMultiplier = 20;
1820

1921
uint32_t EvasModifierToEcoreEventModifiers(const Evas_Modifier* evas_modifier) {
2022
uint32_t modifiers = 0;
@@ -40,8 +42,6 @@ void EvasObjectResizeWithMinMaxHint(Evas_Object* object,
4042

4143
} // namespace
4244

43-
namespace flutter {
44-
4545
TizenViewElementary::TizenViewElementary(int32_t width,
4646
int32_t height,
4747
Evas_Object* parent)

shell/platform/tizen/tizen_vsync_waiter.cc

Lines changed: 71 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,44 @@
99
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
1010
#include "flutter/shell/platform/tizen/logger.h"
1111

12-
static const int kMessageQuit = -1;
13-
static const int kMessageRequestVblank = 0;
14-
1512
namespace flutter {
1613

17-
typedef struct {
14+
namespace {
15+
16+
constexpr int kMessageQuit = -1;
17+
constexpr int kMessageRequestVblank = 0;
18+
19+
struct Message {
1820
Eina_Thread_Queue_Msg head;
1921
int event;
2022
intptr_t baton;
21-
} Msg;
23+
};
2224

23-
TizenVsyncWaiter::TizenVsyncWaiter(FlutterTizenEngine* engine)
24-
: engine_(engine) {
25-
vblank_thread_ = ecore_thread_feedback_run(RequestVblankLoop, nullptr,
26-
nullptr, nullptr, this, EINA_TRUE);
25+
} // namespace
26+
27+
TizenVsyncWaiter::TizenVsyncWaiter(FlutterTizenEngine* engine) {
28+
tdm_client_ = std::make_shared<TdmClient>(engine);
29+
30+
vblank_thread_ = ecore_thread_feedback_run(RunVblankLoop, nullptr, nullptr,
31+
nullptr, this, EINA_TRUE);
2732
}
2833

2934
TizenVsyncWaiter::~TizenVsyncWaiter() {
30-
if (tdm_client_) {
31-
tdm_client_->OnEngineStop();
32-
}
33-
Send(kMessageQuit, 0);
35+
tdm_client_.reset();
36+
37+
SendMessage(kMessageQuit, 0);
38+
3439
if (vblank_thread_) {
3540
ecore_thread_cancel(vblank_thread_);
3641
vblank_thread_ = nullptr;
3742
}
3843
}
3944

40-
void TizenVsyncWaiter::SetTdmClient(TdmClient* tdm_client) {
41-
tdm_client_ = tdm_client;
42-
}
43-
4445
void TizenVsyncWaiter::AsyncWaitForVsync(intptr_t baton) {
45-
Send(kMessageRequestVblank, baton);
46+
SendMessage(kMessageRequestVblank, baton);
4647
}
4748

48-
void TizenVsyncWaiter::Send(int event, intptr_t baton) {
49+
void TizenVsyncWaiter::SendMessage(int event, intptr_t baton) {
4950
if (!vblank_thread_ || ecore_thread_check(vblank_thread_)) {
5051
FT_LOG(Error) << "Invalid vblank thread.";
5152
return;
@@ -56,104 +57,83 @@ void TizenVsyncWaiter::Send(int event, intptr_t baton) {
5657
return;
5758
}
5859

59-
Msg* msg;
6060
void* ref;
61-
msg = static_cast<Msg*>(
62-
eina_thread_queue_send(vblank_thread_queue_, sizeof(Msg), &ref));
63-
msg->event = event;
64-
msg->baton = baton;
61+
Message* message = static_cast<Message*>(
62+
eina_thread_queue_send(vblank_thread_queue_, sizeof(Message), &ref));
63+
message->event = event;
64+
message->baton = baton;
6565
eina_thread_queue_send_done(vblank_thread_queue_, ref);
6666
}
6767

68-
void TizenVsyncWaiter::RequestVblankLoop(void* data, Ecore_Thread* thread) {
69-
TizenVsyncWaiter* tizen_vsync_waiter =
70-
reinterpret_cast<TizenVsyncWaiter*>(data);
71-
TdmClient tdm_client(tizen_vsync_waiter->engine_);
72-
tizen_vsync_waiter->SetTdmClient(&tdm_client);
73-
if (!tdm_client.IsValid()) {
68+
void TizenVsyncWaiter::RunVblankLoop(void* data, Ecore_Thread* thread) {
69+
auto* self = reinterpret_cast<TizenVsyncWaiter*>(data);
70+
71+
std::weak_ptr<TdmClient> tdm_client = self->tdm_client_;
72+
if (!tdm_client.lock()->IsValid()) {
7473
FT_LOG(Error) << "Invalid tdm_client.";
7574
ecore_thread_cancel(thread);
7675
return;
7776
}
77+
7878
Eina_Thread_Queue* vblank_thread_queue = eina_thread_queue_new();
7979
if (!vblank_thread_queue) {
8080
FT_LOG(Error) << "Invalid vblank thread queue.";
8181
ecore_thread_cancel(thread);
8282
return;
8383
}
84+
self->vblank_thread_queue_ = vblank_thread_queue;
8485

85-
tizen_vsync_waiter->vblank_thread_queue_ = vblank_thread_queue;
8686
while (!ecore_thread_check(thread)) {
8787
void* ref;
88-
Msg* msg;
89-
msg = static_cast<Msg*>(eina_thread_queue_wait(vblank_thread_queue, &ref));
90-
if (msg) {
88+
Message* message = static_cast<Message*>(
89+
eina_thread_queue_wait(vblank_thread_queue, &ref));
90+
if (message->event == kMessageQuit) {
9191
eina_thread_queue_wait_done(vblank_thread_queue, ref);
92-
} else {
93-
FT_LOG(Error) << "Received a null message.";
94-
continue;
92+
break;
9593
}
96-
if (msg->event == kMessageQuit) {
94+
intptr_t baton = message->baton;
95+
eina_thread_queue_wait_done(vblank_thread_queue, ref);
96+
97+
if (tdm_client.expired()) {
9798
break;
9899
}
99-
tdm_client.WaitVblank(msg->baton);
100+
tdm_client.lock()->AwaitVblank(baton);
100101
}
102+
101103
if (vblank_thread_queue) {
102104
eina_thread_queue_free(vblank_thread_queue);
103105
}
104106
}
105107

106108
TdmClient::TdmClient(FlutterTizenEngine* engine) {
107-
if (!CreateTdm()) {
108-
FT_LOG(Error) << "CreateTdm() failed.";
109-
}
110-
engine_ = engine;
111-
}
112-
113-
TdmClient::~TdmClient() {
114-
DestroyTdm();
115-
}
116-
117-
void TdmClient::OnEngineStop() {
118-
std::lock_guard<std::mutex> lock(engine_mutex_);
119-
engine_ = nullptr;
120-
}
121-
122-
void TdmClient::WaitVblank(intptr_t baton) {
123-
baton_ = baton;
124-
tdm_error error = tdm_client_vblank_wait(vblank_, 1, VblankCallback, this);
125-
if (error != TDM_ERROR_NONE) {
126-
FT_LOG(Error) << "tdm_client_vblank_wait() failed.";
127-
return;
128-
}
129-
tdm_client_handle_events(client_);
130-
}
131-
132-
bool TdmClient::CreateTdm() {
133109
tdm_error ret;
134110
client_ = tdm_client_create(&ret);
135-
if (ret != TDM_ERROR_NONE && client_ != NULL) {
111+
if (ret != TDM_ERROR_NONE) {
136112
FT_LOG(Error) << "Failed to create a TDM client.";
137-
return false;
113+
return;
138114
}
139115

140116
output_ = tdm_client_get_output(client_, const_cast<char*>("default"), &ret);
141-
if (ret != TDM_ERROR_NONE && output_ != NULL) {
117+
if (ret != TDM_ERROR_NONE) {
142118
FT_LOG(Error) << "Could not obtain the default client output.";
143-
return false;
119+
return;
144120
}
145121

146122
vblank_ = tdm_client_output_create_vblank(output_, &ret);
147-
if (ret != TDM_ERROR_NONE && vblank_ != NULL) {
123+
if (ret != TDM_ERROR_NONE) {
148124
FT_LOG(Error) << "Failed to create a vblank object.";
149-
return false;
125+
return;
150126
}
151-
152127
tdm_client_vblank_set_enable_fake(vblank_, 1);
153-
return true;
128+
129+
engine_ = engine;
154130
}
155131

156-
void TdmClient::DestroyTdm() {
132+
TdmClient::~TdmClient() {
133+
{
134+
std::lock_guard<std::mutex> lock(engine_mutex_);
135+
engine_ = nullptr;
136+
}
157137
if (vblank_) {
158138
tdm_client_vblank_destroy(vblank_);
159139
vblank_ = nullptr;
@@ -165,6 +145,16 @@ void TdmClient::DestroyTdm() {
165145
}
166146
}
167147

148+
void TdmClient::AwaitVblank(intptr_t baton) {
149+
baton_ = baton;
150+
tdm_error ret = tdm_client_vblank_wait(vblank_, 1, VblankCallback, this);
151+
if (ret != TDM_ERROR_NONE) {
152+
FT_LOG(Error) << "tdm_client_vblank_wait failed with error: " << ret;
153+
return;
154+
}
155+
tdm_client_handle_events(client_);
156+
}
157+
168158
bool TdmClient::IsValid() {
169159
return vblank_ && client_;
170160
}
@@ -175,14 +165,15 @@ void TdmClient::VblankCallback(tdm_client_vblank* vblank,
175165
unsigned int tv_sec,
176166
unsigned int tv_usec,
177167
void* user_data) {
178-
TdmClient* client = reinterpret_cast<TdmClient*>(user_data);
179-
FT_ASSERT(client != nullptr);
180-
std::lock_guard<std::mutex> lock(client->engine_mutex_);
181-
if (client->engine_) {
168+
auto* self = reinterpret_cast<TdmClient*>(user_data);
169+
FT_ASSERT(self != nullptr);
170+
171+
std::lock_guard<std::mutex> lock(self->engine_mutex_);
172+
if (self->engine_) {
182173
uint64_t frame_start_time_nanos = tv_sec * 1e9 + tv_usec * 1e3;
183174
uint64_t frame_target_time_nanos = 16.6 * 1e6 + frame_start_time_nanos;
184-
client->engine_->OnVsync(client->baton_, frame_start_time_nanos,
185-
frame_target_time_nanos);
175+
self->engine_->OnVsync(self->baton_, frame_start_time_nanos,
176+
frame_target_time_nanos);
186177
}
187178
}
188179

shell/platform/tizen/tizen_vsync_waiter.h

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <Ecore.h>
99
#include <tdm_client.h>
10+
11+
#include <memory>
1012
#include <mutex>
1113

1214
#include "flutter/shell/platform/embedder/embedder.h"
@@ -19,41 +21,43 @@ class TdmClient {
1921
public:
2022
TdmClient(FlutterTizenEngine* engine);
2123
virtual ~TdmClient();
22-
bool CreateTdm();
23-
void DestroyTdm();
24+
2425
bool IsValid();
25-
void WaitVblank(intptr_t baton);
26-
void OnEngineStop();
26+
void AwaitVblank(intptr_t baton);
27+
28+
private:
2729
static void VblankCallback(tdm_client_vblank* vblank,
2830
tdm_error error,
2931
unsigned int sequence,
3032
unsigned int tv_sec,
3133
unsigned int tv_usec,
3234
void* user_data);
3335

34-
private:
36+
FlutterTizenEngine* engine_ = nullptr;
3537
std::mutex engine_mutex_;
36-
tdm_client* client_{nullptr};
37-
tdm_client_output* output_{nullptr};
38-
tdm_client_vblank* vblank_{nullptr};
39-
FlutterTizenEngine* engine_{nullptr};
40-
intptr_t baton_{0};
38+
39+
tdm_client* client_ = nullptr;
40+
tdm_client_output* output_ = nullptr;
41+
tdm_client_vblank* vblank_ = nullptr;
42+
43+
intptr_t baton_ = 0;
4144
};
4245

4346
class TizenVsyncWaiter {
4447
public:
4548
TizenVsyncWaiter(FlutterTizenEngine* engine);
4649
virtual ~TizenVsyncWaiter();
50+
4751
void AsyncWaitForVsync(intptr_t baton);
48-
void SetTdmClient(TdmClient* tdm_client);
4952

5053
private:
51-
void Send(int event, intptr_t baton);
52-
static void RequestVblankLoop(void* data, Ecore_Thread* thread);
53-
Ecore_Thread* vblank_thread_{nullptr};
54-
Eina_Thread_Queue* vblank_thread_queue_{nullptr};
55-
FlutterTizenEngine* engine_{nullptr};
56-
TdmClient* tdm_client_{nullptr};
54+
void SendMessage(int event, intptr_t baton);
55+
56+
static void RunVblankLoop(void* data, Ecore_Thread* thread);
57+
58+
std::shared_ptr<TdmClient> tdm_client_;
59+
Ecore_Thread* vblank_thread_ = nullptr;
60+
Eina_Thread_Queue* vblank_thread_queue_ = nullptr;
5761
};
5862

5963
} // namespace flutter

shell/platform/tizen/tizen_window_ecore_wl2.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
#include "flutter/shell/platform/tizen/logger.h"
1212
#include "flutter/shell/platform/tizen/tizen_view_event_handler_delegate.h"
1313

14+
namespace flutter {
15+
1416
namespace {
1517

16-
static const int kScrollDirectionVertical = 0;
17-
static const int kScrollDirectionHorizontal = 1;
18-
static const int kScrollOffsetMultiplier = 20;
18+
constexpr int kScrollDirectionVertical = 0;
19+
constexpr int kScrollDirectionHorizontal = 1;
20+
constexpr int kScrollOffsetMultiplier = 20;
1921

2022
} // namespace
2123

22-
namespace flutter {
23-
2424
TizenWindowEcoreWl2::TizenWindowEcoreWl2(TizenGeometry geometry,
2525
bool transparent,
2626
bool focusable,

0 commit comments

Comments
 (0)