Skip to content

Commit

Permalink
feat: exec_agent 从 c api 到 impl 的调用
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 3, 2023
1 parent 4836e33 commit 4a405e3
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 25 deletions.
2 changes: 2 additions & 0 deletions include/MaaFramework/Utility/MaaBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ extern "C"
int32_t MAA_FRAMEWORK_API MaaGetRectY(MaaRectHandle handle);
int32_t MAA_FRAMEWORK_API MaaGetRectW(MaaRectHandle handle);
int32_t MAA_FRAMEWORK_API MaaGetRectH(MaaRectHandle handle);

MaaBool MAA_FRAMEWORK_API MaaSetRect(MaaRectHandle handle, int32_t x, int32_t y, int32_t w, int32_t h);
MaaBool MAA_FRAMEWORK_API MaaSetRectX(MaaRectHandle handle, int32_t value);
MaaBool MAA_FRAMEWORK_API MaaSetRectY(MaaRectHandle handle, int32_t value);
MaaBool MAA_FRAMEWORK_API MaaSetRectW(MaaRectHandle handle, int32_t value);
Expand Down
26 changes: 20 additions & 6 deletions source/MaaFramework/API/MaaBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void MaaDestroyImageBuffer(MaaImageBufferHandle handle)
delete handle;
}

void* MaaGetImageRawData(MaaImageBufferHandle handle)
MaaImageRawData MaaGetImageRawData(MaaImageBufferHandle handle)
{
if (!handle) {
LogError << "handle is null";
Expand Down Expand Up @@ -176,7 +176,7 @@ MaaBool MaaSetImageRawData(MaaImageBufferHandle handle, MaaImageRawData data, in
return true;
}

uint8_t* MaaGetImageEncoded(MaaImageBufferHandle handle)
MaaImageEncodedData MaaGetImageEncoded(MaaImageBufferHandle handle)
{
if (!handle) {
LogError << "handle is null";
Expand Down Expand Up @@ -268,11 +268,25 @@ int32_t MaaGetRectH(MaaRectHandle handle)
return handle->height;
}

MaaBool MaaSetRect(MaaRectHandle handle, int32_t x, int32_t y, int32_t w, int32_t h)
{
if (!handle) {
LogError << "handle is null";
return false;
}

handle->x = x;
handle->y = y;
handle->width = w;
handle->height = h;
return true;
}

MaaBool MaaSetRectX(MaaRectHandle handle, int32_t value)
{
if (!handle) {
LogError << "handle is null";
return 0;
return false;
}

handle->x = value;
Expand All @@ -283,7 +297,7 @@ MaaBool MaaSetRectY(MaaRectHandle handle, int32_t value)
{
if (!handle) {
LogError << "handle is null";
return 0;
return false;
}

handle->y = value;
Expand All @@ -294,7 +308,7 @@ MaaBool MaaSetRectW(MaaRectHandle handle, int32_t value)
{
if (!handle) {
LogError << "handle is null";
return 0;
return false;
}

handle->width = value;
Expand All @@ -305,7 +319,7 @@ MaaBool MaaSetRectH(MaaRectHandle handle, int32_t value)
{
if (!handle) {
LogError << "handle is null";
return 0;
return false;
}

handle->height = value;
Expand Down
37 changes: 30 additions & 7 deletions source/MaaToolKit/ExecAgent/ActionExecAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ MAA_TOOLKIT_NS_BEGIN

ActionExecAgent::ActionExecAgent()
{
custom_action_.run = &ActionExecAgent::action_run;
custom_action_.stop = &ActionExecAgent::action_stop;
custom_action_.run = &ActionExecAgent::maa_api_run;
custom_action_.stop = &ActionExecAgent::maa_api_stop;
}

bool ActionExecAgent::register_for_maa_inst(MaaInstanceHandle handle, std::string_view name)
Expand All @@ -23,13 +23,36 @@ bool ActionExecAgent::unregister_for_maa_inst(MaaInstanceHandle handle, std::str
return MaaUnregisterCustomAction(handle, name.data());
}

MaaBool ActionExecAgent::action_run(MaaSyncContextHandle sync_context, MaaStringView task_name,
MaaStringView custom_action_param, MaaRectHandle cur_box,
MaaStringView cur_rec_detail, MaaTransparentArg action_arg)
bool ActionExecAgent::run(MaaSyncContextHandle sync_context, std::string_view task_name,
std::string_view custom_action_param, MaaRectHandle cur_box, std::string_view cur_rec_detail)
{
return MaaBool();
return false;
}

void ActionExecAgent::action_stop(MaaTransparentArg action_arg) {}
void ActionExecAgent::stop() {}

MaaBool ActionExecAgent::maa_api_run(MaaSyncContextHandle sync_context, MaaStringView task_name,
MaaStringView custom_action_param, MaaRectHandle cur_box,
MaaStringView cur_rec_detail, MaaTransparentArg action_arg)
{
auto* self = static_cast<ActionExecAgent*>(action_arg);
if (!self) {
LogError << "action_arg is nullptr";
return false;
}

return self->run(sync_context, task_name, custom_action_param, cur_box, cur_rec_detail);
}

void ActionExecAgent::maa_api_stop(MaaTransparentArg action_arg)
{
auto* self = static_cast<ActionExecAgent*>(action_arg);
if (!self) {
LogError << "action_arg is nullptr";
return;
}

self->stop();
}

MAA_TOOLKIT_NS_END
16 changes: 11 additions & 5 deletions source/MaaToolKit/ExecAgent/ActionExecAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ class ActionExecAgent final : public ExecAgentBase, public SingletonHolder<Actio
virtual bool register_for_maa_inst(MaaInstanceHandle handle, std::string_view name) override;
virtual bool unregister_for_maa_inst(MaaInstanceHandle handle, std::string_view name) override;

private: // for MaaCustomActionAPI
static MaaBool action_run(MaaSyncContextHandle sync_context, MaaStringView task_name,
MaaStringView custom_action_param, MaaRectHandle cur_box, MaaStringView cur_rec_detail,
MaaTransparentArg action_arg);
static void action_stop(MaaTransparentArg action_arg);
private:
bool run(MaaSyncContextHandle sync_context, std::string_view task_name, std::string_view custom_action_param,
MaaRectHandle cur_box, std::string_view cur_rec_detail);
void stop();

private:
// for MaaCustomActionAPI
static MaaBool maa_api_run( //
MaaSyncContextHandle sync_context, MaaStringView task_name, MaaStringView custom_action_param,
MaaRectHandle cur_box, MaaStringView cur_rec_detail, MaaTransparentArg action_arg);
static void maa_api_stop(MaaTransparentArg action_arg);

private:
MaaCustomActionAPI custom_action_;
Expand Down
45 changes: 39 additions & 6 deletions source/MaaToolKit/ExecAgent/RecognizerExecAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MAA_TOOLKIT_NS_BEGIN

RecognizerExecAgent::RecognizerExecAgent()
{
custom_recognizer_.analyze = &RecognizerExecAgent::recognizer_analyze;
custom_recognizer_.analyze = &RecognizerExecAgent::maa_api_analyze;
}

bool RecognizerExecAgent::register_for_maa_inst(MaaInstanceHandle handle, std::string_view name)
Expand All @@ -22,12 +22,45 @@ bool RecognizerExecAgent::unregister_for_maa_inst(MaaInstanceHandle handle, std:
return MaaUnregisterCustomRecognizer(handle, name.data());
}

MaaBool RecognizerExecAgent::recognizer_analyze(MaaSyncContextHandle sync_context, const MaaImageBufferHandle image,
MaaStringView task_name, MaaStringView custom_recognition_param,
MaaTransparentArg recognizer_arg, MaaRectHandle out_box,
MaaStringBufferHandle out_detail)
std::optional<RecognizerExecAgent::AnalyzeResult> RecognizerExecAgent::analyze(
MaaSyncContextHandle sync_context, const cv::Mat& image, std::string_view task_name,
std::string_view custom_recognition_param)
{
return MaaBool();
return std::optional<AnalyzeResult>();
}

MaaBool RecognizerExecAgent::maa_api_analyze( //
MaaSyncContextHandle sync_context, const MaaImageBufferHandle image, MaaStringView task_name,
MaaStringView custom_recognition_param, MaaTransparentArg recognizer_arg, MaaRectHandle out_box,
MaaStringBufferHandle out_detail)
{
auto* self = static_cast<RecognizerExecAgent*>(recognizer_arg);
if (!self) {
LogError << "recognizer_arg is nullptr";
return false;
}

void* raw_data = MaaGetImageRawData(image);
int32_t width = MaaGetImageWidth(image);
int32_t height = MaaGetImageHeight(image);
int32_t type = MaaGetImageType(image);
cv::Mat image_mat(height, width, type, raw_data);

auto result_opt = self->analyze(sync_context, image_mat, task_name, custom_recognition_param);

if (!result_opt) {
MaaSetRect(out_box, 0, 0, 0, 0);
MaaClearString(out_detail);
return false;
}

auto& box = result_opt->box;
MaaSetRect(out_box, box.x, box.y, box.width, box.height);

auto& detail = result_opt->detail;
MaaSetStringEx(out_detail, detail.c_str(), detail.size());

return true;
}

MAA_TOOLKIT_NS_END
13 changes: 12 additions & 1 deletion source/MaaToolKit/ExecAgent/RecognizerExecAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ExecAgentBase.h"

#include "MaaFramework/Task/MaaCustomRecognizer.h"
#include "Utils/NoWarningCVMat.hpp"
#include "Utils/SingletonHolder.hpp"

MAA_TOOLKIT_NS_BEGIN
Expand All @@ -18,9 +19,19 @@ class RecognizerExecAgent final : public ExecAgentBase, public SingletonHolder<R
virtual bool register_for_maa_inst(MaaInstanceHandle handle, std::string_view name) override;
virtual bool unregister_for_maa_inst(MaaInstanceHandle handle, std::string_view name) override;

private:
struct AnalyzeResult
{
cv::Rect box {};
std::string detail;
};
std::optional<AnalyzeResult> analyze( //
MaaSyncContextHandle sync_context, const cv::Mat& image, std::string_view task_name,
std::string_view custom_recognition_param);

private:
// for MaaCustomRecognizerAPI
static MaaBool recognizer_analyze( //
static MaaBool maa_api_analyze( //
MaaSyncContextHandle sync_context, const MaaImageBufferHandle image, MaaStringView task_name,
MaaStringView custom_recognition_param, MaaTransparentArg recognizer_arg,
/*out*/ MaaRectHandle out_box,
Expand Down

0 comments on commit 4a405e3

Please sign in to comment.