framework: initialize soft module framework
Signed-off-by: Wind Yuan <feng.yuan@intel.com>
diff --git a/configure.ac b/configure.ac
index 019dd48..4555022 100644
--- a/configure.ac
+++ b/configure.ac
@@ -352,6 +352,7 @@
clx_kernel/Makefile
xcore/Makefile
modules/Makefile
+ modules/soft/Makefile
modules/isp/Makefile
modules/ocl/Makefile
wrapper/Makefile
diff --git a/modules/Makefile.am b/modules/Makefile.am
index bb9ba21..fd362f8 100644
--- a/modules/Makefile.am
+++ b/modules/Makefile.am
@@ -10,4 +10,4 @@
ISP_DIR =
endif
-SUBDIRS = $(ISP_DIR) $(OCL_DIR)
+SUBDIRS = soft $(ISP_DIR) $(OCL_DIR)
diff --git a/modules/soft/Makefile.am b/modules/soft/Makefile.am
new file mode 100644
index 0000000..14ca816
--- /dev/null
+++ b/modules/soft/Makefile.am
@@ -0,0 +1,41 @@
+lib_LTLIBRARIES = libxcam_soft.la
+
+XCAMSOFT_CXXFLAGS = \
+ $(LIBCL_CFLAGS) \
+ -I$(top_srcdir)/xcore \
+ -I$(top_srcdir)/modules \
+ $(NULL)
+
+xcam_soft_sources = \
+ soft_handler.cpp \
+ soft_image_allocator.cpp \
+ soft_worker.cpp \
+ $(NULL)
+
+libxcam_soft_la_SOURCES = \
+ $(xcam_soft_sources) \
+ $(NULL)
+
+libxcam_soft_la_CXXFLAGS = \
+ $(XCAMSOFT_CXXFLAGS) \
+ $(XCAM_CXXFLAGS) \
+ $(NULL)
+
+libxcam_soft_la_LIBADD = \
+ $(top_builddir)/xcore/libxcam_core.la \
+ $(NULL)
+
+libxcam_soft_la_LDFLAGS = \
+ $(XCAM_LT_LDFLAGS) \
+ $(PTHREAD_LDFLAGS) \
+ $(NULL)
+
+libxcam_softincludedir = $(includedir)/xcam/soft
+
+nobase_libxcam_softinclude_HEADERS = \
+ soft_handler.h \
+ soft_image_allocator.h \
+ soft_worker.h \
+ $(NULL)
+
+libxcam_soft_la_LIBTOOLFLAGS = --tag=disable-static
diff --git a/modules/soft/soft_handler.cpp b/modules/soft/soft_handler.cpp
new file mode 100644
index 0000000..73b2850
--- /dev/null
+++ b/modules/soft/soft_handler.cpp
@@ -0,0 +1,198 @@
+/*
+ * soft_handler.cpp - soft image handler implementation
+ *
+ * Copyright (c) 2017 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Author: Wind Yuan <feng.yuan@intel.com>
+ */
+
+#include "soft_handler.h"
+#include "soft_image_allocator.h"
+#include "thread_pool.h"
+
+#define DEFAULT_SOFT_BUF_COUNT 4
+
+namespace XCam {
+
+class SyncMeta
+ : public MetaBase
+{
+public:
+ SyncMeta () : _done (false) {}
+ void signal_done (XCamReturn err);
+ void wakeup ();
+ XCamReturn signal_wait_ret ();
+
+private:
+ Mutex _mutex;
+ Cond _cond;
+ bool _done;
+ XCamReturn _error;
+};
+
+void
+SyncMeta::signal_done (XCamReturn err)
+{
+ SmartLock locker (_mutex);
+ _done = true;
+ _error = err;
+ _cond.broadcast ();
+}
+
+void
+SyncMeta::wakeup ()
+{
+ SmartLock locker (_mutex);
+ _error = XCAM_RETURN_ERROR_UNKNOWN;
+ _cond.broadcast ();
+}
+
+XCamReturn
+SyncMeta::signal_wait_ret ()
+{
+ SmartLock locker (_mutex);
+ if (_done)
+ return _error;
+ _cond.wait (_mutex);
+ return _error;
+}
+
+SoftHandler::SoftHandler (const char* name)
+ : ImageHandler (name)
+ , _need_configure (true)
+ , _wip_buf_count (0)
+{
+ set_allocator (new SoftImageAllocator);
+ char thrds_name[XCAM_MAX_STR_SIZE];
+ snprintf (thrds_name, XCAM_MAX_STR_SIZE, "t-pool-%s", XCAM_STR(name));
+ _threads = new ThreadPool (thrds_name);
+}
+
+SoftHandler::~SoftHandler ()
+{
+}
+
+bool
+SoftHandler::set_threads (uint32_t num)
+{
+ return _threads->set_threads (num, num);
+}
+
+XCamReturn
+SoftHandler::configure_resource (const SmartPtr<ImageHandler::Parameters> ¶ms)
+{
+ XCamReturn ret = XCAM_RETURN_NO_ERROR;
+ if (!_need_configure)
+ return ret;
+
+ if (!params->out_buf.ptr ()) {
+ ret = reserve_buffers (params->out_buf->get_video_info (), DEFAULT_SOFT_BUF_COUNT);
+ XCAM_FAIL_RETURN (
+ ERROR, ret == XCAM_RETURN_NO_ERROR, ret,
+ "soft_hander(%s) configure resource failed in reserving buffers", XCAM_STR (get_name ()));
+ _need_configure = false;
+ }
+
+ if (_threads.ptr ()) {
+ ret = _threads->start ();
+ XCAM_FAIL_RETURN (
+ ERROR, ret == XCAM_RETURN_NO_ERROR, ret,
+ "soft_hander(%s) configure resource failed when starting threads", XCAM_STR (get_name ()));
+ }
+
+ return ret;
+}
+
+XCamReturn
+SoftHandler::execute_buffer (SmartPtr<ImageHandler::Parameters> ¶ms, bool sync)
+{
+ SmartPtr<SyncMeta> sync_meta;
+ XCamReturn ret = configure_resource (params);
+ XCAM_FAIL_RETURN (
+ WARNING, ret == XCAM_RETURN_NO_ERROR, ret,
+ "soft_hander(%s) configure resource failed", XCAM_STR (get_name ()));
+
+ if (sync) {
+ XCAM_ASSERT (!params->find_meta<SyncMeta> ().ptr ());
+ sync_meta = new SyncMeta ();
+ XCAM_ASSERT (sync_meta.ptr ());
+ params->add_meta (sync_meta);
+ }
+
+ SmartPtr<Worker> worker = get_first_worker ();
+ XCAM_FAIL_RETURN (
+ WARNING, worker.ptr (), XCAM_RETURN_ERROR_PARAM,
+ "No worder set to soft_hander(%s)", XCAM_STR (get_name ()));
+
+ SmartPtr<Worker::Arguments> args = get_first_worker_args (params);
+ XCAM_FAIL_RETURN (
+ WARNING, args.ptr (), XCAM_RETURN_ERROR_PARAM,
+ "soft_hander(%s) get first worker(%s) args failed",
+ XCAM_STR (get_name ()), XCAM_STR (worker->get_name ()));
+
+ ret = worker->work (args);
+
+ XCAM_FAIL_RETURN (
+ WARNING, ret >= XCAM_RETURN_NO_ERROR, XCAM_RETURN_ERROR_PARAM,
+ "soft_hander(%s) execute buffer failed in working", XCAM_STR (get_name ()));
+ ++_wip_buf_count;
+
+ if (sync) {
+ XCAM_ASSERT (sync_meta.ptr ());
+ _cur_sync = sync_meta;
+ ret = sync_meta->signal_wait_ret ();
+ _cur_sync.release ();
+ }
+
+ return ret;
+}
+
+XCamReturn
+SoftHandler::finish ()
+{
+ XCamReturn ret = XCAM_RETURN_NO_ERROR;
+ if (_cur_sync.ptr ()) {
+ ret = _cur_sync->signal_wait_ret ();
+ }
+ //wait for _wip_buf_count = 0
+ //if (ret == XCAM_RETURN_NO_ERROR)
+ // XCAM_ASSERT (_wip_buf_count == 0);
+
+ return ret;
+}
+
+XCamReturn
+SoftHandler::terminate ()
+{
+ if (_cur_sync.ptr ()) {
+ _cur_sync->wakeup ();
+ _cur_sync.release ();
+ }
+ return ImageHandler::terminate ();
+}
+
+XCamReturn
+SoftHandler::last_worker_done (SmartPtr<ImageHandler::Parameters> ¶ms, XCamReturn err)
+{
+ SmartPtr<SyncMeta> sync_meta = params->find_meta<SyncMeta> ();
+ if (sync_meta.ptr ())
+ sync_meta->signal_done (err);
+
+ --_wip_buf_count;
+ return execute_status_check (params, err);
+}
+
+}
+
diff --git a/modules/soft/soft_handler.h b/modules/soft/soft_handler.h
new file mode 100644
index 0000000..c8a36d4
--- /dev/null
+++ b/modules/soft/soft_handler.h
@@ -0,0 +1,64 @@
+/*
+ * soft_handler.h - soft image handler class
+ *
+ * Copyright (c) 2017 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Author: Wind Yuan <feng.yuan@intel.com>
+ */
+
+#ifndef XCAM_SOFT_HANDLER_H
+#define XCAM_SOFT_HANDLER_H
+
+#include "xcam_utils.h"
+#include "image_handler.h"
+
+namespace XCam {
+
+class SoftHandler;
+class ThreadPool;
+class SyncMeta;
+
+class SoftHandler
+ : public ImageHandler
+{
+public:
+ explicit SoftHandler (const char* name);
+ ~SoftHandler ();
+
+ bool set_threads (uint32_t num);
+
+ // derive from ImageHandler
+ virtual XCamReturn execute_buffer (SmartPtr<Parameters> ¶ms, bool sync);
+ virtual XCamReturn finish ();
+ virtual XCamReturn terminate ();
+
+protected:
+ virtual XCamReturn configure_resource (const SmartPtr<Parameters> ¶ms);
+ virtual SmartPtr<Worker::Arguments> get_first_worker_args (SmartPtr<Parameters> ¶ms) = 0;
+ virtual XCamReturn last_worker_done (SmartPtr<Parameters> ¶ms, XCamReturn err);
+
+private:
+ XCAM_DEAD_COPY (SoftHandler);
+
+private:
+ SmartPtr<ThreadPool> _threads;
+ SmartPtr<SyncMeta> _cur_sync;
+ bool _need_configure;
+ mutable std::atomic<int32_t> _wip_buf_count;
+};
+
+}
+
+#endif //XCAM_SOFT_HANDLER_H
diff --git a/modules/soft/soft_image_allocator.cpp b/modules/soft/soft_image_allocator.cpp
new file mode 100644
index 0000000..7499f2c
--- /dev/null
+++ b/modules/soft/soft_image_allocator.cpp
@@ -0,0 +1,92 @@
+/*
+ * soft_image_allocator.cpp - soft image allocator implementation
+ *
+ * Copyright (c) 2017 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Author: Wind Yuan <feng.yuan@intel.com>
+ */
+
+#include "soft_image_allocator.h"
+
+namespace XCam {
+
+class VideoMemData
+ : public BufferData
+{
+public:
+ explicit VideoMemData (uint32_t size);
+ virtual ~VideoMemData ();
+ bool is_valid () const {
+ return (_mem_ptr ? true : false);
+ }
+
+ //derive from BufferData
+ virtual uint8_t *map ();
+ virtual bool unmap ();
+
+private:
+ uint8_t *_mem_ptr;
+ uint32_t _mem_size;
+};
+
+VideoMemData::VideoMemData (uint32_t size)
+ : _mem_ptr (NULL)
+ , _mem_size (0)
+{
+ XCAM_ASSERT (size > 0);
+ _mem_ptr = xcam_malloc_type_array (uint8_t, size);
+ if (_mem_ptr)
+ _mem_size = size;
+}
+
+VideoMemData::~VideoMemData ()
+{
+ xcam_free (_mem_ptr);
+}
+
+uint8_t *
+VideoMemData::map ()
+{
+ XCAM_ASSERT (_mem_ptr);
+ return _mem_ptr;
+}
+
+bool
+VideoMemData::unmap ()
+{
+ return true;
+}
+
+SoftImageAllocator::SoftImageAllocator ()
+{
+}
+
+SoftImageAllocator::~SoftImageAllocator ()
+{
+}
+
+SmartPtr<BufferData>
+SoftImageAllocator::allocate_data (const VideoBufferInfo &buffer_info)
+{
+ SmartPtr<VideoMemData> data = new VideoMemData (buffer_info.size);
+ XCAM_FAIL_RETURN (
+ ERROR, data.ptr () && data->is_valid (),
+ NULL, "SoftImageAllocator allocate data failed. buf_size:%d", buffer_info.size);
+
+ return data;
+}
+
+}
+
diff --git a/modules/soft/soft_image_allocator.h b/modules/soft/soft_image_allocator.h
new file mode 100644
index 0000000..6c21e5d
--- /dev/null
+++ b/modules/soft/soft_image_allocator.h
@@ -0,0 +1,56 @@
+/*
+ * soft_image_allocator.h - soft image allocator class
+ *
+ * Copyright (c) 2017 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Author: Wind Yuan <feng.yuan@intel.com>
+ */
+
+#ifndef XCAM_SOFT_IMAGE_ALLOCATOR_H
+#define XCAM_SOFT_IMAGE_ALLOCATOR_H
+
+#include "xcam_utils.h"
+#include "buffer_pool.h"
+
+namespace XCam {
+
+class SoftImageAllocator
+ : public BufferPool
+{
+public:
+ explicit SoftImageAllocator ();
+ virtual ~SoftImageAllocator ();
+
+private:
+ //derive from BufferPool
+ virtual SmartPtr<BufferData> allocate_data (const VideoBufferInfo &buffer_info);
+};
+
+#if 0
+class AllocatorPool {
+public:
+ explicit AllocatorPool ();
+ virtual ~AllocatorPool ();
+
+ SmartPtr<VideoBuffer> allocate_video_buf (const VideoBufferInfo &info);
+
+private:
+ SafeList<BufferPool> _pools;
+};
+#endif
+
+}
+
+#endif //XCAM_SOFT_IMAGE_ALLOCATOR_H
diff --git a/modules/soft/soft_worker.cpp b/modules/soft/soft_worker.cpp
new file mode 100644
index 0000000..b5e5587
--- /dev/null
+++ b/modules/soft/soft_worker.cpp
@@ -0,0 +1,178 @@
+/*
+ * soft_worker.cpp - soft worker implementation
+ *
+ * Copyright (c) 2017 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Author: Wind Yuan <feng.yuan@intel.com>
+ */
+
+#include "soft_worker.h"
+#include "thread_pool.h"
+#include "xcam_mutex.h"
+
+namespace XCam {
+
+class ItemSynch {
+private:
+ mutable std::atomic<uint32_t> _remain_items;
+ Mutex _mutex;
+ XCamReturn _error;
+
+public:
+ ItemSynch (uint32_t items)
+ : _remain_items(items), _error (XCAM_RETURN_NO_ERROR)
+ {}
+ void update_error (XCamReturn err) {
+ SmartLock locker(_mutex);
+ _error = err;
+ }
+ XCamReturn get_error () {
+ SmartLock locker(_mutex);
+ return _error;
+ }
+ uint32_t dec() {
+ return --_remain_items;
+ }
+
+private:
+ XCAM_DEAD_COPY (ItemSynch);
+};
+
+class WorkItem
+ : public ThreadPool::UserData
+{
+public:
+ WorkItem (
+ const SmartPtr<SoftWorker> &worker,
+ const SmartPtr<Worker::Arguments> &args,
+ const SoftWorker::WorkSize &item,
+ SmartPtr<ItemSynch> &sync)
+ : _worker (worker)
+ , _args (args)
+ , _item (item)
+ , _sync (sync)
+ , _error (XCAM_RETURN_NO_ERROR)
+ {
+ }
+ virtual XCamReturn run ();
+ virtual void done (XCamReturn err);
+
+
+private:
+ SmartPtr<SoftWorker> _worker;
+ SmartPtr<Worker::Arguments> _args;
+ SoftWorker::WorkSize _item;
+ SmartPtr<ItemSynch> _sync;
+ XCamReturn _error;
+};
+
+XCamReturn
+WorkItem::run ()
+{
+ XCamReturn ret = _sync->get_error();
+ if (!xcam_ret_is_ok (ret))
+ return ret;
+
+ ret = _worker->work_impl (_args, _item);
+ if (!xcam_ret_is_ok (ret))
+ _sync->update_error (ret);
+
+ return ret;
+}
+
+void
+WorkItem::done (XCamReturn err)
+{
+ if (_sync->dec () == 0) {
+ XCamReturn ret = _sync->get_error ();
+ if (xcam_ret_is_ok (ret))
+ ret = err;
+ _worker->all_items_done (_args, ret);
+ }
+}
+
+SoftWorker::SoftWorker (const char *name)
+ : Worker (name)
+{
+}
+
+SoftWorker::~SoftWorker ()
+{
+}
+
+bool
+SoftWorker::set_threads (const SmartPtr<ThreadPool> &threads)
+{
+ XCAM_FAIL_RETURN (
+ ERROR, !_threads.ptr (), false,
+ "SoftWorker(%s) set threads failed, it's already set before.", XCAM_STR (get_name ()));
+ _threads = threads;
+ return true;
+}
+
+bool
+SoftWorker::set_work_size (const WorkSize &size)
+{
+ _work_size = size;
+ return true;
+}
+
+XCamReturn
+SoftWorker::work (const SmartPtr<Worker::Arguments> &args)
+{
+ XCamReturn ret = XCAM_RETURN_NO_ERROR;
+
+ if (_work_size.x == 1 && _work_size.y == 1 && _work_size.z == 1) {
+ XCAM_ASSERT (_work_size.x == 1 && _work_size.y == 1 && _work_size.z == 1);
+ ret = work_impl (args, _work_size);
+ return status_check (args, ret);
+ }
+
+ uint32_t max_items = _work_size.x * _work_size.y * _work_size.z;
+ if (!_threads.ptr ()) {
+ char thr_name [XCAM_MAX_STR_SIZE];
+ snprintf (thr_name, XCAM_MAX_STR_SIZE, "%s-thread-pool", XCAM_STR(get_name ()));
+ _threads = new ThreadPool (thr_name);
+ XCAM_ASSERT (_threads.ptr ());
+ _threads->set_threads (max_items, max_items);
+ ret = _threads->start ();
+ XCAM_FAIL_RETURN (
+ ERROR, xcam_ret_is_ok (ret), ret,
+ "SoftWorker(%s) work failed when starting threads", XCAM_STR(get_name()));
+ }
+
+ SmartPtr<ItemSynch> sync = new ItemSynch (_work_size.x * _work_size.y * _work_size.z);
+ for (int z = 0; z < _work_size.z; ++z)
+ for (int y = 0; y < _work_size.y; ++y)
+ for (int x = 0; x < _work_size.x; ++x)
+ {
+ SmartPtr<WorkItem> item = new WorkItem (this, args, WorkSize(x, y, z), sync);
+ ret = _threads->queue (item);
+ XCAM_FAIL_RETURN (
+ ERROR, xcam_ret_is_ok (ret), ret,
+ "SoftWorker(%s) queue work item(x:%d y: %d z:%d) failed",
+ _work_size.x, _work_size.y, _work_size.z);
+ }
+
+ return XCAM_RETURN_NO_ERROR;
+}
+
+void
+SoftWorker::all_items_done (const SmartPtr<Arguments> &args, XCamReturn error)
+{
+ status_check (args, error);
+}
+
+};
diff --git a/modules/soft/soft_worker.h b/modules/soft/soft_worker.h
new file mode 100644
index 0000000..c3bc359
--- /dev/null
+++ b/modules/soft/soft_worker.h
@@ -0,0 +1,67 @@
+/*
+ * soft_worker.h - soft worker class
+ *
+ * Copyright (c) 2017 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Author: Wind Yuan <feng.yuan@intel.com>
+ */
+
+#ifndef XCAM_SOFT_WORKER_H
+#define XCAM_SOFT_WORKER_H
+
+#include "xcam_utils.h"
+#include "worker.h"
+
+namespace XCam {
+
+class ThreadPool;
+
+//multi-thread worker
+class SoftWorker
+ : public Worker
+{
+ friend class WorkItem;
+
+public:
+ struct WorkSize {
+ uint32_t x, y, z;
+ WorkSize (uint32_t x0 = 1, uint32_t y0 = 1, uint32_t z0 = 1) : x(x0), y(y0), z(z0) {}
+ };
+
+public:
+ explicit SoftWorker (const char *name);
+ virtual ~SoftWorker ();
+ bool set_threads (const SmartPtr<ThreadPool> &threads);
+ bool set_work_size (const WorkSize &size);
+ const WorkSize &get_work_size () const {
+ return _work_size;
+ }
+
+ // derived from Worker
+ virtual XCamReturn work (const SmartPtr<Arguments> &args);
+
+private:
+ virtual XCamReturn work_impl (const SmartPtr<Arguments> &args, const WorkSize &item) = 0;
+ void all_items_done (const SmartPtr<Arguments> &args, XCamReturn error);
+
+ XCAM_DEAD_COPY (SoftWorker);
+
+private:
+ SmartPtr<ThreadPool> _threads;
+ WorkSize _work_size;
+};
+
+}
+#endif //XCAM_SOFT_WORKER_H