mv3d: pass raw pointer to g_async_queue instead to make code simple
authorSeungbae Shin <seungbae.shin@samsung.com>
Thu, 22 Sep 2022 11:58:24 +0000 (20:58 +0900)
committerKwanghoon Son <k.son@samsung.com>
Wed, 14 Dec 2022 06:33:18 +0000 (15:33 +0900)
[Issue type] refactoring
[Version] 0.26.2

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

Change-Id: Ibab33c79bd520d2d8acbdfdebca07628c8b2edc7

mv_3d/3d/src/Mv3d.cpp
packaging/capi-media-vision.spec

index 39f5cf5..56cf271 100644 (file)
@@ -33,6 +33,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)
@@ -70,13 +84,6 @@ Mv3d::~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);
        }
 
@@ -342,7 +349,7 @@ int Mv3d::RunAsync(mv_source_h baseSource, mv_source_h extraSource)
 {
        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;
@@ -361,7 +368,7 @@ int Mv3d::RunAsync(mv_source_h baseSource, mv_source_h extraSource)
                        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);
@@ -370,7 +377,7 @@ int Mv3d::RunAsync(mv_source_h baseSource, mv_source_h extraSource)
                                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;
@@ -381,7 +388,7 @@ int Mv3d::RunAsync(mv_source_h baseSource, mv_source_h extraSource)
 
 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) {
@@ -389,31 +396,29 @@ gpointer Mv3d::DfsThreadLoop(gpointer data)
                        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;
 
-                       gpointer base = g_async_queue_pop(handle->mDfsAsyncQueue);
+                       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();
                int ret = mv_source_fill_by_buffer(handle->mInternalSource, static_cast<unsigned char *>(leftData.data),
                                                                                   leftData.stride * leftData.height, 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;
@@ -427,7 +432,7 @@ gpointer Mv3d::DfsThreadLoop(gpointer data)
                        //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), pcd,
@@ -437,10 +442,9 @@ gpointer Mv3d::DfsThreadLoop(gpointer data)
                        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;
index 1d6cfa2..e555415 100644 (file)
@@ -1,6 +1,6 @@
 Name:        capi-media-vision
 Summary:     Media Vision library for Tizen Native API
-Version:     0.26.1
+Version:     0.26.2
 Release:     0
 Group:       Multimedia/Framework
 License:     Apache-2.0 and BSD-3-Clause