task_manager: drop arguments from callback for CallbackNode 67/311267/2
authorInki Dae <inki.dae@samsung.com>
Fri, 17 May 2024 04:00:12 +0000 (13:00 +0900)
committerInki Dae <inki.dae@samsung.com>
Fri, 17 May 2024 06:07:08 +0000 (15:07 +0900)
Drop unnecessary arguments from callback for CallbackNode.
Now CallbackNode can act like normal node such as InferenceNode
so drop the unnecessary arguments and get them(input and result)
from CallbackNode itself.

This patch is a step for separating CallbackNode into two nodes
- BridgeNode and EndpointNode - as I shared already, which will drop
the code smell that CallbackNode has two kinds of behaviors.

Change-Id: I25bbc87a1f1d9a82e5c7340b39b8d789599db933
Signed-off-by: Inki Dae <inki.dae@samsung.com>
services/task_manager/include/CallbackNode.h
services/task_manager/include/INode.h
services/task_manager/src/CallbackNode.cpp
services/task_manager/src/TaskManager.cpp
test/services/test_task_manager.cpp

index 6fff52a8f8df6109163234bdd0b694fe622672d6..f36fe1125b84956750a76bb398c8f3f9a24cff2a 100644 (file)
@@ -56,10 +56,7 @@ public:
        {
                return _name;
        }
-       void addInput(std::shared_ptr<BaseDataType> input)
-       {
-               throw exception::InvalidOperation("Not supported.");
-       }
+       void addInput(std::shared_ptr<BaseDataType> input) override;
        std::vector<std::shared_ptr<BaseDataType> > &getInputs() override;
        void addDependency(std::shared_ptr<INode> node) override;
        std::vector<std::shared_ptr<INode> > &getDependencies() override;
@@ -72,7 +69,7 @@ public:
        NodeCb getCb();
        void setUserData(void *user_data);
        void *getUserData();
-       std::vector<std::shared_ptr<BaseResultType> > &getResult();
+       std::vector<std::shared_ptr<BaseResultType> > &getResults();
        void addResult(std::shared_ptr<BaseResultType> result);
 };
 
index 366a0cd2821d27c5d00dcfcddaa19bfb98ac91c7..4b371a5bd85e4900c6b01d041f05ce9c508fecb0 100644 (file)
@@ -30,8 +30,7 @@ namespace services
 {
 enum class NodeType { NONE, INFERENCE, CB };
 
-using NodeCb = std::function<void(std::vector<BaseResultType *> &results, std::shared_ptr<BaseDataType> input,
-                                                                 void *user_data)>;
+using NodeCb = std::function<void(void *user_data)>;
 
 class INode
 {
index 95d1312b071a51fc0cc7f0d368d2d0798bd008fe..58a69fadf5202ba8559094746f6c0a4af9f02e79 100644 (file)
@@ -28,6 +28,11 @@ NodeType CallbackNode::getType()
        return _type;
 }
 
+void CallbackNode::addInput(shared_ptr<BaseDataType> input)
+{
+       _inputs.push_back(input);
+}
+
 vector<shared_ptr<BaseDataType> > &CallbackNode::getInputs()
 {
        return _inputs;
@@ -79,7 +84,7 @@ void CallbackNode::addResult(std::shared_ptr<BaseResultType> result)
        _results.push_back(result);
 }
 
-vector<shared_ptr<BaseResultType> > &CallbackNode::getResult()
+vector<shared_ptr<BaseResultType> > &CallbackNode::getResults()
 {
        return _results;
 }
index 1ff4a1a18296453539fcf362ac9d25cfd703ca8e..7400c957683a8c74065d9cb50fdd0be1aace1775 100644 (file)
@@ -78,20 +78,31 @@ void TaskManager::threadCb(shared_ptr<INode> &node)
                        throw InvalidOperation("Callback function or user data is null.");
                }
 
-               vector<BaseResultType *> results;
-
                for (auto &n : node->getDependencies()) {
                        // Add the result if dependency node is inference service not callback.
                        if (n->getType() == NodeType::INFERENCE) {
                                auto inferenceNode = dynamic_pointer_cast<InferenceNode>(n);
-                               results.push_back(&inferenceNode->getInferenceTask()->result());
+                               auto &result = inferenceNode->getInferenceTask()->result();
+
+                               if (result._type == ResultType::FACE_DETECTION)
+                                       callbackNode->addResult(make_shared<FdResultType>(
+                                                       dynamic_cast<FdResultType &>(inferenceNode->getInferenceTask()->result())));
+                               else if (result._type == ResultType::FACE_LANDMARK)
+                                       callbackNode->addResult(make_shared<FldResultType>(
+                                                       dynamic_cast<FldResultType &>(inferenceNode->getInferenceTask()->result())));
+                               else if (result._type == ResultType::OBJECT_DETECTION)
+                                       callbackNode->addResult(make_shared<OdResultType>(
+                                                       dynamic_cast<OdResultType &>(inferenceNode->getInferenceTask()->result())));
                        }
                }
 
+               // TODO. consider for mulitple inputs later.
+               node->addInput(_inputs[0]);
+
                // Call the callback function registered in this callback node.
                // In this callback, new data should be set by calling node->setOutput()
                // if new data is made. TODO. consider for multiple inputs later.
-               cb(results, _inputs[0], user_data);
+               cb(user_data);
        }
 
        // Wake up.
@@ -189,7 +200,7 @@ vector<shared_ptr<BaseResultType> > &TaskManager::output()
        } else {
                auto callbackNode = dynamic_pointer_cast<CallbackNode>(lastNode);
 
-               _results = callbackNode->getResult();
+               _results = callbackNode->getResults();
        }
 
        return _results;
index e4f221e888c743fe5ba65006e489fe4de1cee343..939eabc04957339a12cc396ba7f7fcdd1855e3b7 100644 (file)
@@ -34,11 +34,11 @@ using namespace singleo;
 using namespace singleo::inference;
 using namespace singleo::services;
 
-void BridgeNodeCallback(vector<BaseResultType *> &results, shared_ptr<BaseDataType> inputData, void *user_data)
+void BridgeNodeCallback(void *user_data)
 {
-       shared_ptr<ImageDataType> imageData = dynamic_pointer_cast<ImageDataType>(inputData);
-       size_t buffer_size = imageData->width * imageData->height * imageData->byte_per_pixel;
        CallbackNode *node = static_cast<CallbackNode *>(user_data);
+       shared_ptr<ImageDataType> imageData = dynamic_pointer_cast<ImageDataType>(node->getInputs()[0]);
+       size_t buffer_size = imageData->width * imageData->height * imageData->byte_per_pixel;
        ImageDataType newImage;
 
        newImage = *imageData;
@@ -49,13 +49,15 @@ void BridgeNodeCallback(vector<BaseResultType *> &results, shared_ptr<BaseDataTy
        cv::Mat cv_image(cv::Size(newImage.width, newImage.height), CV_MAKETYPE(CV_8U, 3), newImage.ptr);
 
        cout << "Face detection result" << endl;
+
+       auto &results = node->getResults();
        for (auto r : results) {
                if (r->_type != ResultType::FACE_DETECTION) {
                        cout << "invalid result type" << endl;
                        continue;
                }
 
-               FdResultType *f_r = dynamic_cast<FdResultType *>(r);
+               auto f_r = dynamic_pointer_cast<FdResultType>(r);
 
                for (auto rect : f_r->_rects)
                        cout << rect.left << " x " << rect.top << " ~ " << rect.right << " x " << rect.bottom << endl;
@@ -188,21 +190,16 @@ TEST(SingloTaskManager, MultipleNodesBasedGraphBShouldWork)
        }
 }
 
-void LastNodeCallback(vector<BaseResultType *> &results, shared_ptr<BaseDataType> inputData, void *user_data)
+void LastNodeCallback(void *user_data)
 {
        CallbackNode *node = static_cast<CallbackNode *>(user_data);
 
-       for (auto &r : results) {
-               ASSERT_EQ((r->_type == ResultType::FACE_DETECTION || r->_type == ResultType::FACE_LANDMARK), true);
+       auto &results = node->getResults();
 
-               if (r->_type == ResultType::FACE_DETECTION) {
-                       auto fdResult = dynamic_cast<FdResultType *>(r);
-                       node->addResult(make_shared<FdResultType>(*fdResult));
-               } else {
-                       auto fldResult = dynamic_cast<FldResultType *>(r);
-                       node->addResult(make_shared<FldResultType>(*fldResult));
-               }
-       }
+       cout << "result count = " << results.size() << endl;
+
+       // Nothing to do in case of last node.
+       // So do anything you want here if you want to do something in last node.
 }
 
 // GraphC: