mv3d: pass raw pointer to g_async_queue instead to make code simple 14/285714/1
authorSeungbae Shin <seungbae.shin@samsung.com>
Thu, 22 Sep 2022 11:58:24 +0000 (20:58 +0900)
committerTae-Young Chung <ty83.chung@samsung.com>
Mon, 19 Dec 2022 01:35:10 +0000 (10:35 +0900)
[Issue type] refactoring

In addition, make g_async_queue to cleanup automatically for the remained items.

This commit is cherry-picked from https://review.tizen.org/gerrit/gitweb?p=platform/core/api/mediavision.git;a=commit;h=8f29b5a3a4a0498fb05e4345b020ae1156c1d3de
to avoid conflict.

Change-Id: Ibab33c79bd520d2d8acbdfdebca07628c8b2edc7

mv_3d/3d/src/Mv3d.cpp

index bbc8f41..af55399 100644 (file)
@@ -34,6 +34,20 @@ namespace mediavision
 {
 namespace mv3d
 {
+       static void __destroyNotifyCb(gpointer data)
+       {
+               if (!data)
+                       return;
+
+               LOGW("Cleanup remained DfsInputData (%p)", data);
+               auto remained = static_cast<DfsInputData *>(data);
+
+               delete[] static_cast<unsigned char *>(remained->data);
+               delete[] static_cast<unsigned char *>(remained->extraData);
+
+               delete remained;
+       }
+
        Mv3d::Mv3d() :
                mDfsParameter(),
                mDfsAdaptor(nullptr),
@@ -71,13 +85,6 @@ namespace mv3d
                }
 
                if (mDfsAsyncQueue) {
-                       gpointer base = nullptr;
-                       while ((base = g_async_queue_try_pop(mDfsAsyncQueue))) {
-                               auto pItem = static_cast<std::shared_ptr<DfsInputData> *>(base);
-                               auto item = std::move(*pItem);
-                               delete pItem;
-                               item.reset();
-                       }
                        g_async_queue_unref(mDfsAsyncQueue);
                }
 
@@ -370,7 +377,7 @@ namespace mv3d
        {
                try {
                        if (!mDfsAsyncQueue) {
-                               mDfsAsyncQueue = g_async_queue_new();
+                               mDfsAsyncQueue = g_async_queue_new_full(__destroyNotifyCb);
                                if (!mDfsAsyncQueue) {
                                        LOGE("Fail to g_async_queue_new()");
                                        return MEDIA_VISION_ERROR_INTERNAL;
@@ -389,7 +396,7 @@ namespace mv3d
                                mDfsIsLive = true;
                        }
 
-                       std::shared_ptr<DfsInputData> input(new DfsInputData);
+                       auto input = std::make_unique<DfsInputData>();
                        GetDfsDataFromSources(baseSource, extraSource, *input);
                        if (!mInternalSource) {
                                int ret = mv_create_source(&mInternalSource);
@@ -398,9 +405,7 @@ namespace mv3d
                                        return MEDIA_VISION_ERROR_INTERNAL;
                                }
                        }
-                       g_async_queue_push(mDfsAsyncQueue, static_cast<void*>(
-                                                                                       new std::shared_ptr<DfsInputData>(
-                                                                                               std::move(input))));
+                       g_async_queue_push(mDfsAsyncQueue, static_cast<gpointer>(input.release()));
                } catch (const std::exception &e) {
                        LOGE("Failed to Run with %s", e.what());
                        return MEDIA_VISION_ERROR_INVALID_OPERATION;
@@ -411,7 +416,7 @@ namespace mv3d
 
        gpointer Mv3d::DfsThreadLoop(gpointer data)
        {
-               Mv3d *handle = static_cast<Mv3d*>(data);
+               auto handle = static_cast<Mv3d *>(data);
                while(handle->mDfsIsLive) {
                        auto base = g_async_queue_timeout_pop(handle->mDfsAsyncQueue, MV_3D_TIMEOUT_SEC * G_TIME_SPAN_SECOND);
                        if (!base) {
@@ -419,24 +424,22 @@ namespace mv3d
                                break;
                        }
 
-                       auto queueRemaining = static_cast<int>(g_async_queue_length(handle->mDfsAsyncQueue));
+                       auto queueRemaining = g_async_queue_length(handle->mDfsAsyncQueue);
                        LOGI("%d remaining", queueRemaining);
 
                        while (queueRemaining > 0) {
-                               auto pItem = static_cast<std::shared_ptr<DfsInputData> *>(base);
-                               auto item = std::move(*pItem);
-                               delete pItem;
-                               item.reset();
+                               auto itemToDelete = static_cast<DfsInputData *>(base);
+                               delete[] static_cast<unsigned char *>(itemToDelete->data);
+                               delete[] static_cast<unsigned char *>(itemToDelete->extraData);
+                               delete itemToDelete;
 
                                base = g_async_queue_pop(handle->mDfsAsyncQueue);
                                queueRemaining--;
                                LOGI("%d remaining", queueRemaining);
                        }
 
-                       auto pInput = static_cast<std::shared_ptr<DfsInputData>*>(base);
-                       auto input = std::move(*pInput);
-                       delete pInput;
-                       handle->mDfsAdaptor->run(*input);
+                       std::unique_ptr<DfsInputData> pInput(static_cast<DfsInputData *>(base));
+                       handle->mDfsAdaptor->run(*pInput);
 
                        auto depthData = handle->mDfsAdaptor->getOutputData();
                        auto leftData = handle->mDfsAdaptor->getLeftData();
@@ -446,7 +449,7 @@ namespace mv3d
                                                                leftData.width,
                                                                leftData.height,
                                                                leftData.type == DFS_DATA_TYPE_UINT8C3 ? MEDIA_VISION_COLORSPACE_RGB888 :
-                                                                                               MEDIA_VISION_COLORSPACE_Y800);
+                                                                                                                                                MEDIA_VISION_COLORSPACE_Y800);
                        if (MEDIA_VISION_ERROR_NONE != ret) {
                                LOGW("Errors were occurred during source filling %i", ret);
                                continue;
@@ -462,7 +465,7 @@ namespace mv3d
                                //mPointcloudThread = g_thread_new("pointcloud_thread",
                                //              &Mv3d::PointcloudThreadLoop,
                                //              static_cast<gpointer>(this));
-                               handle->GetPointcloudFromSource(*input, depthData, p);
+                               handle->GetPointcloudFromSource(*pInput, depthData, p);
 
                                mv_3d_pointcloud_h pcd = &p;
                                handle->mPointcloudCallback(static_cast<mv_source_h>(handle->mInternalSource),
@@ -473,10 +476,9 @@ namespace mv3d
                                delete _pcd;
                        }
 #endif
-                       delete [] static_cast<unsigned char*>(input->data);
-                       delete [] static_cast<unsigned char*>(input->extraData);
+                       delete[] static_cast<unsigned char *>(pInput->data);
+                       delete[] static_cast<unsigned char *>(pInput->extraData);
                        mv_source_clear(handle->mInternalSource);
-                       input.reset();
                }
 
                return nullptr;