Skip to content

Commit 8e669eb

Browse files
authored
refactor: remove attach/detach thread management (#1240)
1 parent 7e3791f commit 8e669eb

7 files changed

Lines changed: 0 additions & 146 deletions

File tree

include/MaaControlUnit/ControlUnitAPI.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ class AndroidNativeControlUnitAPI : public ControlUnitAPI
9090
{
9191
public:
9292
~AndroidNativeControlUnitAPI() override = default;
93-
94-
virtual void* attach_thread() const = 0;
95-
virtual int detach_thread(void* env) const = 0;
9693
};
9794

9895
class Win32ControlUnitAPI

source/MaaAndroidNativeControlUnit/General/AndroidExternalLib.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ std::optional<AndroidNativeNS::AndroidExternalFunctions>
3333

3434
bool ok = get_required_function<AndroidNativeNS::GetLockedPixelsSignature>(get_locked_pixels_func_name_, funcs.get_locked_pixels);
3535
ok = ok && get_required_function<AndroidNativeNS::UnlockPixelsSignature>(unlock_pixels_func_name_, funcs.unlock_pixels);
36-
ok = ok && get_required_function<AndroidNativeNS::AttachThreadSignature>(attach_thread_func_name_, funcs.attach_thread);
37-
ok = ok && get_required_function<AndroidNativeNS::DetachThreadSignature>(detach_thread_func_name_, funcs.detach_thread);
3836
ok = ok
3937
&& get_required_function<AndroidNativeNS::DispatchInputMessageSignature>(
4038
dispatch_input_message_func_name_,

source/MaaAndroidNativeControlUnit/General/AndroidExternalLib.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,16 @@ struct MethodParam
8585

8686
using GetLockedPixelsSignature = FrameInfo();
8787
using UnlockPixelsSignature = int(FrameInfo);
88-
using AttachThreadSignature = void*();
89-
using DetachThreadSignature = int(void*);
9088
using DispatchInputMessageSignature = int(MethodParam);
9189

9290
using GetLockedPixelsFunc = boost::function<GetLockedPixelsSignature>;
9391
using UnlockPixelsFunc = boost::function<UnlockPixelsSignature>;
94-
using AttachThreadFunc = boost::function<AttachThreadSignature>;
95-
using DetachThreadFunc = boost::function<DetachThreadSignature>;
9692
using DispatchInputMessageFunc = boost::function<DispatchInputMessageSignature>;
9793

9894
struct AndroidExternalFunctions
9995
{
10096
GetLockedPixelsFunc get_locked_pixels { };
10197
UnlockPixelsFunc unlock_pixels { };
102-
AttachThreadFunc attach_thread { };
103-
DetachThreadFunc detach_thread { };
10498
DispatchInputMessageFunc dispatch_input_message { };
10599
};
106100

@@ -116,8 +110,6 @@ class AndroidNativeExternalLibraryHolder : public LibraryHolder<AndroidNativeExt
116110
private:
117111
static constexpr auto get_locked_pixels_func_name_ = "GetLockedPixels";
118112
static constexpr auto unlock_pixels_func_name_ = "UnlockPixels";
119-
static constexpr auto attach_thread_func_name_ = "AttachThread";
120-
static constexpr auto detach_thread_func_name_ = "DetachThread";
121113
static constexpr auto dispatch_input_message_func_name_ = "DispatchInputMessage";
122114
};
123115

source/MaaAndroidNativeControlUnit/General/ScopedThreadAttach.h

Lines changed: 0 additions & 49 deletions
This file was deleted.

source/MaaAndroidNativeControlUnit/Manager/AndroidNativeControlUnitMgr.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <algorithm>
44
#include <utility>
55

6-
#include "General/ScopedThreadAttach.h"
76
#include "MaaUtils/Logger.h"
87
#include "MaaUtils/Platform.h"
98

@@ -109,10 +108,6 @@ bool AndroidNativeControlUnitMgr::screencap(cv::Mat& image)
109108
return false;
110109
}
111110

112-
if (const ScopedThreadAttach attach(&*funcs_); !attach.attached()) {
113-
return false;
114-
}
115-
116111
const FrameInfo info = funcs_->get_locked_pixels();
117112
const bool need_unlock = info.frame_ref != nullptr;
118113

@@ -336,10 +331,6 @@ bool AndroidNativeControlUnitMgr::validate_contact(int contact)
336331

337332
bool AndroidNativeControlUnitMgr::dispatch_input_message(MethodParam param) const
338333
{
339-
if (const ScopedThreadAttach attach(&*funcs_); !attach.attached()) {
340-
return false;
341-
}
342-
343334
int ret = funcs_->dispatch_input_message(param);
344335
if (ret == 0) {
345336
return true;
@@ -374,22 +365,4 @@ cv::Point AndroidNativeControlUnitMgr::get_touch_up_point(int contact) const
374365
};
375366
}
376367

377-
void* AndroidNativeControlUnitMgr::attach_thread() const
378-
{
379-
if (!funcs_) {
380-
LogError << "library is not loaded";
381-
return nullptr;
382-
}
383-
return funcs_->attach_thread();
384-
}
385-
386-
int AndroidNativeControlUnitMgr::detach_thread(void* env) const
387-
{
388-
if (!funcs_) {
389-
LogError << "library is not loaded";
390-
return -1;
391-
}
392-
return funcs_->detach_thread(env);
393-
}
394-
395368
MAA_CTRL_UNIT_NS_END

source/MaaAndroidNativeControlUnit/Manager/AndroidNativeControlUnitMgr.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ class AndroidNativeControlUnitMgr : public AndroidNativeControlUnitAPI
5555

5656
json::object get_info() const override;
5757

58-
void* attach_thread() const override;
59-
int detach_thread(void* env) const override;
60-
6158
private:
6259
static bool validate_contact(int contact);
6360
bool dispatch_input_message(AndroidNativeNS::MethodParam param) const;

source/MaaFramework/Controller/ControllerAgent.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,6 @@
99

1010
MAA_CTRL_NS_BEGIN
1111

12-
namespace
13-
{
14-
15-
#ifdef __ANDROID__
16-
class ScopedAndroidNativeThreadAttach
17-
{
18-
public:
19-
explicit ScopedAndroidNativeThreadAttach(const std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>& control_unit)
20-
: unit_(std::dynamic_pointer_cast<MAA_CTRL_UNIT_NS::AndroidNativeControlUnitAPI>(control_unit))
21-
{
22-
if (!unit_ || !unit_->connected()) {
23-
return;
24-
}
25-
26-
env_ = unit_->attach_thread();
27-
attach_failed_ = env_ == nullptr;
28-
if (attach_failed_) {
29-
LogError << "Android native control unit attach_thread returned nullptr";
30-
}
31-
}
32-
33-
ScopedAndroidNativeThreadAttach(const ScopedAndroidNativeThreadAttach&) = delete;
34-
ScopedAndroidNativeThreadAttach& operator=(const ScopedAndroidNativeThreadAttach&) = delete;
35-
36-
~ScopedAndroidNativeThreadAttach()
37-
{
38-
if (!unit_ || !env_) {
39-
return;
40-
}
41-
42-
int ret = unit_->detach_thread(env_);
43-
if (ret != 0) {
44-
LogWarn << "Android native control unit detach_thread failed" << VAR(ret);
45-
}
46-
}
47-
48-
bool ready() const { return !attach_failed_; }
49-
50-
private:
51-
std::shared_ptr<MAA_CTRL_UNIT_NS::AndroidNativeControlUnitAPI> unit_;
52-
void* env_ = nullptr;
53-
bool attach_failed_ = false;
54-
};
55-
#endif
56-
57-
} // namespace
58-
5912
ControllerAgent::ControllerAgent(std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> control_unit)
6013
: control_unit_(std::move(control_unit))
6114
{
@@ -968,13 +921,6 @@ bool ControllerAgent::run_action(typename AsyncRunner<Action>::Id id, Action act
968921
{
969922
bool ret = false;
970923

971-
#ifdef __ANDROID__
972-
const ScopedAndroidNativeThreadAttach attach(control_unit_);
973-
if (!attach.ready()) {
974-
return false;
975-
}
976-
#endif
977-
978924
bool notify = false;
979925
{
980926
std::unique_lock lock { focus_ids_mutex_ };

0 commit comments

Comments
 (0)