diff --git a/hardware/hardware.cpp b/hardware/hardware.cpp index 4448fd4..d9d8ada 100644 --- a/hardware/hardware.cpp +++ b/hardware/hardware.cpp @@ -51,11 +51,6 @@ scanner_hw::~scanner_hw() close(); } -int scanner_hw::empty_printf(const char* fmt, ...) -{ - return 0; -} - void scanner_hw::init(void) { std::string text(""); @@ -284,11 +279,8 @@ void scanner_hw::init(void) }; OPT_HANDLER(papertm) - { - if(*(bool*)value) - dbg_print_ = printf; - else - dbg_print_ = &scanner_hw::empty_printf; + { + log_debug_info_ = *(bool*)value; }; opt_handler_[SANE_OPT_NAME(IS_CHECK_STAPLE)] = staple; @@ -486,7 +478,7 @@ void scanner_hw::thread_image_capture(bool paper_ready) } scanning_ = false; - img_callback((dyn_mem_ptr)WORKER_STATUS_IDLE, false, (LPPACKIMAGE)err); + img_callback((dyn_mem_ptr)WORKER_STATUS_IDLE, CIS_CB_SCAN_OVER, (LPPACKIMAGE)err); } int scanner_hw::start_and_wait_lifter(int to_ms, int* ui_words_id) { @@ -510,10 +502,13 @@ int scanner_hw::start_and_wait_lifter(int to_ms, int* ui_words_id) } int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_cnt, int* ui_words, bool* mbstopped) { - int ret = SCANNER_ERR_OK, minh = 210 * dpi_y_ / 25.4, + int ret = SCANNER_ERR_OK, minh = 210 * dpi_y_ / MM_PER_INCH, words = 0; + bool resok = true; uint32_t pass = 0; + uint64_t bgn = chronograph::from_process_born(); chronograph watch; + std::string loginfo(""); std::pair mbev; dyn_mem_shared_ptr mem = nullptr; std::function img_callback(img_handler_); @@ -532,26 +527,6 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c img->life = chronograph::from_process_born(); while(scanning_) { - bool resok = true; - uint64_t bgn = chronograph::from_process_born(); - uint32_t oper_index = 0; -#ifdef REBUILD_IN_CIS_THREAD - if(mem) - { - img_callback(mem, true, img); - dbg_print_("\n\t%d: to rebuild spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); - mem->release(); - mem = nullptr; - retrieve_v4l2_mem(cism, cism_cnt); - - resok = res_(TASK_CAPTURER, false, 0); - dbg_print_("\t%d: to check resource spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); - } -#else - resok = res_(TASK_CAPTURER, false, 0); - dbg_print_("\t%d: to check resource spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); -#endif - int fatal = 0; ret = SCANNER_ERR_DEVICE_HD_002; words = ID_WORDS_STATUS_DEVICE_HD_002; @@ -577,7 +552,7 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c ret = SCANNER_ERR_OK; words = 0; } - dbg_print_("\t%d: to wait paper-out spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); + dbg_print_(loginfo, img->pos.paper_ind + 1, "paperout", chronograph::from_process_born() - bgn, false); pass = watch.elapse_ms(); img->pos.paper_ind++; @@ -585,13 +560,13 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c if(!count_mode_) { img->height = get_image_real_height(minh); - dbg_print_("\t%d: to get_image_real_height spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); + dbg_print_(loginfo, img->pos.paper_ind, "getheight", chronograph::from_process_born() - bgn, false); size_t size = 0; int ind = -1; void* frame = camera_->read_frame(to_stream_, size, ind); - dbg_print_("\t%d: to read_frame spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); + dbg_print_(loginfo, img->pos.paper_ind, "rdframe", chronograph::from_process_born() - bgn, false); if(!frame) { if(fatal != MOTOR_BORD_EVENT_SCAN_DONE) @@ -609,30 +584,13 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c mem->set_param(cism, 0); mem->set_param((void*)(long)ind, 1); img->pos.status = hg_err_2_image_status(ret); - dbg_print_("\t%d: to construct dyn_mem_shared spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); -#ifndef REBUILD_IN_CIS_THREAD - img_handler_(mem, true, img); // (frame, ind, LPPACKIMAGE, cism, ret, pass, bool) - mem->release(); -#endif + dbg_print_(loginfo, img->pos.paper_ind, "genimgobj", chronograph::from_process_born() - bgn, false); if(!scan_cntless_ && img->pos.paper_ind == scan_count_) { utils::to_log(LOG_LEVEL_ALL, "Scanning finished for given count %d!\n", scan_count_); break; } - -#ifndef REBUILD_IN_CIS_THREAD - // retrieve V4L2 memory ... - retrieve_v4l2_mem(cism, cism_cnt); - dbg_print_("\t%d: to retrieve_v4l2_mem spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); - if(cism_cnt[0] >= camera_->get_mem_count()) - { - ret = SCANNER_ERR_DEVICE_CIS_OUT_OF_MEM; - words = ID_WORDS_STATUS_CIS_OUT_OF_MEM; - utils::to_log(LOG_LEVEL_FATAL, "Scanning stopped for that V4L2 is out of memory!\n"); - break; - } -#endif } else { @@ -642,17 +600,34 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c if(ret || fatal == MOTOR_BORD_EVENT_SCAN_DONE) break; - // resource checking ... - if(!resok) - resok = res_(TASK_CAPTURER, true, SEC_2_MS(10)); if(resok && scanning_) { - dbg_print_("\t%d: to flow completed spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); + dbg_print_(loginfo, img->pos.paper_ind, "final", chronograph::from_process_born() - bgn, true); +#ifndef REBUILD_IN_CIS_THREAD + if(cism_cnt[0] >= camera_->get_mem_count()) + { + ret = SCANNER_ERR_DEVICE_CIS_OUT_OF_MEM; + words = ID_WORDS_STATUS_CIS_OUT_OF_MEM; + utils::to_log(LOG_LEVEL_FATAL, "Scanning stopped for that V4L2 is out of memory!\n"); + break; + } +#endif // should check paper ready ? watch.reset(); + bgn = img->life = chronograph::from_process_born(); motor_->pick_paper(); - img->life = chronograph::from_process_born(); + + img_callback(mem, CIS_CB_IMAGE, img); + dbg_print_(loginfo, img->pos.paper_ind + 1, "rebuild", chronograph::from_process_born() - bgn, false); + mem->release(); + mem = nullptr; + + retrieve_v4l2_mem(cism, cism_cnt); + dbg_print_(loginfo, img->pos.paper_ind + 1, "retrieve_v4l2_mem", chronograph::from_process_born() - bgn, false); + + resok = res_(TASK_CAPTURER, false, 0); + dbg_print_(loginfo, img->pos.paper_ind + 1, "chkrsc", chronograph::from_process_born() - bgn, false); } else { @@ -665,14 +640,12 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c } } -#ifdef REBUILD_IN_CIS_THREAD if(mem) { - img_callback(mem, true, img); + img_callback(mem, CIS_CB_IMAGE, img); mem->release(); mem = nullptr; } -#endif if(ui_words) *ui_words = words; @@ -1044,6 +1017,39 @@ int scanner_hw::open(std::function image_handler, CHK_RES_F } } + auto empty_log = [this](LOG_CIS_TIME_PROTO) -> void + { + + }; + auto real_log = [this](LOG_CIS_TIME_PROTO) -> void + { + if(log.empty()) + log = "Paper " + std::to_string(paper) + " - "; + else + log += " -> "; + log += std::to_string(ms) + "ms(" + step + ")"; + + if(last) + { + size_t len = sizeof(PACK_BASE) + log.length() + 1; + dyn_mem_ptr ptr = dyn_mem::memory(len); + LPPACK_BASE pck = (LPPACK_BASE)ptr->ptr(); + + BASE_PACKET_REPLY(*pck, PACK_CMD_DEBUG_INFO, 0, 0); + strcpy(pck->payload, log.c_str()); + pck->payload[log.length()] = 0; + pck->payload_len = log.length() + 1; + ptr->set_len(len); + img_handler_(ptr, CIS_CB_STATUS, nullptr); + ptr->release(); + log = ""; + } + }; + if(log_debug_info_) + dbg_print_ = real_log; + else + dbg_print_ = empty_log; + return SCANNER_ERR_OK; } int scanner_hw::start_scan(void) diff --git a/hardware/hardware.h b/hardware/hardware.h index 514270d..38ceb84 100644 --- a/hardware/hardware.h +++ b/hardware/hardware.h @@ -16,7 +16,7 @@ // // dyn_mem_ptr: image data buffer, or be uint32_t if bool was false // -// bool: true - image, dyn_mem_ptr is image data buffer; false - dyn_mem_ptr is an uint32_t code of hardware event +// type: see cis_cb_type // // LPPACKIMAGE: image information, or uint32_t event data when bool is false // @@ -25,7 +25,8 @@ // real channels(color mode) should judge by // // LPPACKIMAGE->width / 2 sides / cis::get_line_stream_length(LPPACKIMAGE->resolution_x, false) -#define IMAGE_HANDLER_PROTO void(dyn_mem_ptr, bool, LPPACKIMAGE) +#define IMAGE_HANDLER_PROTO void(CIS_IMAGE_PROTO) +#define LOG_CIS_TIME_PROTO std::string& log, int paper, const char* step, uint32_t ms, bool last #include "./cis/cis_param.h" class GVideoISP1; @@ -98,8 +99,8 @@ class scanner_hw : public sane_opt_provider int to_paper_out_ = 5000; // timeout in ms of paper passed int to_stream_ = 5000; // timeout in ms of frame data can be read - static int empty_printf(const char* fmt, ...); - int (*dbg_print_)(const char* fmt, ...) = &scanner_hw::empty_printf; + bool log_debug_info_ = false; + std::function dbg_print_; void init(void); void init_version(std::string& text); diff --git a/imgproc/imgprc_mgr.cpp b/imgproc/imgprc_mgr.cpp index ceb374f..39f0ed3 100644 --- a/imgproc/imgprc_mgr.cpp +++ b/imgproc/imgprc_mgr.cpp @@ -143,6 +143,7 @@ void imgproc_mgr::send_image(LPPAGEDPARAM obj, LPPACKIMAGE head, cv::Mat& mat, v h.prc_stage = head->prc_stage; h.prc_time = head->prc_time; } + h.prc_last = last; #ifdef USE_THREAD_PAGED_DATA ptr = new image_packet(&h, compd, obj->mean.scan_id, info, info_l); @@ -542,28 +543,28 @@ int imgproc_mgr::clear(void) static uint64_t rebuild_cis = 0; static uint32_t scan_count = 0; -int imgproc_mgr::process(LPPACKIMAGE info, dyn_mem_ptr data, bool img) +int imgproc_mgr::process(CIS_IMAGE_PROTO) { RAWIMG ri; int ret = SCANNER_ERR_OK; int ind = put_ind_ % working_cnt_; #ifdef REBUILD_IN_CIS_THREAD - if(img) + if(type == CIS_CB_IMAGE) { LPPAGEDPARAM paged = nullptr; #ifdef USE_THREAD_PAGED_DATA - paged = params_[0]; + paged = params_[ind]; #endif if(dump_img_) { - cv::Mat mat(info->width, info->height, CV_8UC1, data->ptr()); - send_image(paged, info, mat, nullptr, 0, false); + cv::Mat mat(lpinfo->width, lpinfo->height, CV_8UC1, data->ptr()); + send_image(paged, lpinfo, mat, nullptr, 0, false); } if(first_->is_enable()) { chronograph watch; - first_->do_rebuild(info, data->ptr(), ri.imgs); + first_->do_rebuild(lpinfo, data->ptr(), ri.imgs); rebuild_cis += ri.imgs[0].info.prc_time; scan_count++; if(dump_img_) @@ -572,15 +573,15 @@ int imgproc_mgr::process(LPPACKIMAGE info, dyn_mem_ptr data, bool img) else { PROCIMGINFO i; - i.info = *info; - i.img = cv::Mat(info->width, info->height, CV_8UC1, data->ptr()); + i.info = *lpinfo; + i.img = cv::Mat(lpinfo->width, lpinfo->height, CV_8UC1, data->ptr()); ri.imgs.push_back(i); } } - else + else if(type == CIS_CB_SCAN_OVER) { PROCIMGINFO i; - i.info.data_size = (uint32_t)(long)info; + i.info.data_size = (uint32_t)(long)lpinfo; ri.imgs.push_back(i); if(scan_count) { @@ -590,15 +591,25 @@ int imgproc_mgr::process(LPPACKIMAGE info, dyn_mem_ptr data, bool img) } #else ri.data = data; - if(img) + if(type == CIS_CB_IMAGE) + { data->add_ref(); - if(img) - ri.info = *info; - else - ri.info.data_size = (uint32_t)(long)info; + ri.info = *lpinfo; + } + else if(type == CIS_CB_SCAN_OVER) + { + ri.info.data_size = (uint32_t)(long)lpinfo; + } #endif + else if(type == CIS_CB_STATUS) + { + data->set_session_id(session_id_); + img_sender_(data, sender_param_); - ri.img = img; + return SCANNER_ERR_OK; + } + + ri.img = type == CIS_CB_IMAGE; #ifdef USE_THREAD_PAGED_DATA params_[ind]->mean.que->save(ri, true); #else diff --git a/imgproc/imgprc_mgr.h b/imgproc/imgprc_mgr.h index df31bbd..39a3d0b 100644 --- a/imgproc/imgprc_mgr.h +++ b/imgproc/imgprc_mgr.h @@ -132,7 +132,7 @@ public: public: int load_processor(const char* path); // outer-modules path int clear(void); - int process(LPPACKIMAGE info, dyn_mem_ptr data, bool img); + int process(CIS_IMAGE_PROTO); // life public: diff --git a/scanner/async_scanner.cpp b/scanner/async_scanner.cpp index 23f5b83..01b6e90 100644 --- a/scanner/async_scanner.cpp +++ b/scanner/async_scanner.cpp @@ -282,15 +282,15 @@ void async_scanner::init(void) } else if(devui::UI_CMD_COUNT_PAPER == pack->msg) { - auto receiver = [this](dyn_mem_ptr data, bool img, LPPACKIMAGE lpinfo) -> void + auto receiver = [this](CIS_IMAGE_PROTO) -> void { - img_prcr_->process(lpinfo, data, img); + img_prcr_->process(data, type, lpinfo); }; bool auto_scan = true; std::string prev(cfg_mgr_->get_option_value(SANE_OPT_NAME(WAIT_TO_SCAN), SANE_ACTION_GET_VALUE)); cis_->set_value(SANE_OPT_NAME(WAIT_TO_SCAN), &auto_scan); cis_->open(receiver, CHK_RES_FUNC(), nullptr, true); - cis_->start_scan(); + cis_->start_scan(); } else if(devui::UI_CMD_STOP_SCAN == pack->msg) { @@ -693,9 +693,9 @@ dyn_mem_ptr async_scanner::handle_scan_start(LPPACK_BASE pack, uint32_t* used, p scan_err_ = 0; reply_start_ = false; - auto receiver = [this](dyn_mem_ptr data, bool img, LPPACKIMAGE lpinfo) -> void + auto receiver = [this](CIS_IMAGE_PROTO) -> void { - img_prcr_->process(lpinfo, data, img); + img_prcr_->process(data, type, lpinfo); }; DECL_CHK_RES_FUNC(this, res) { diff --git a/sdk/base/data.h b/sdk/base/data.h index 88c9569..90302d9 100644 --- a/sdk/base/data.h +++ b/sdk/base/data.h @@ -409,3 +409,11 @@ public: #define SENDER_PROTO data_source_ptr ptr, void* param #define RES_CHK_PROTO int type, bool wait, int to_ms, void* param + +enum cis_cb_type +{ + CIS_CB_STATUS = 0, // data is status text string + CIS_CB_IMAGE, // data is image, dyn_mem_ptr is image data buffer + CIS_CB_SCAN_OVER, // dyn_mem_ptr is an uint32_t code of hardware event +}; +#define CIS_IMAGE_PROTO dyn_mem_ptr data, int type, LPPACKIMAGE lpinfo \ No newline at end of file diff --git a/sdk/base/packet.h b/sdk/base/packet.h index 3bfd0d3..9b4f930 100644 --- a/sdk/base/packet.h +++ b/sdk/base/packet.h @@ -112,6 +112,7 @@ enum packet_cmd PAIR_COMMAND(PACK_CMD_SCAN_START), // start scanning, [in]: PACK_BASE, [out]: PACK_BASE PAIR_COMMAND(PACK_CMD_SCAN_IMG), // device -> host, PACK_BASE::payload - LPPACKIMAGE PAIR_COMMAND(PACK_CMD_SCAN_PAPER), // device -> host, ONE paper has passed through the CIS. PACK_BASE::data - index of this paper + PACK_CMD_DEBUG_INFO, // device -> host, PACK_BASE::data is (char*)debug-info-text PACK_CMD_SCAN_FINISHED_ROGER, // device -> host, PACK_BASE::data is scanner_status PAIR_COMMAND(PACK_CMD_SCAN_STOP), // stop scanning, [in]: PACK_BASE, [out]: PACK_BASE //PAIR_COMMAND(PACK_CMD_SCAN_IMAGE_REQ), // get image request, [in]: PACK_BASE, [out] PACK_BASE on error, or PACK_BASE::payload - LPPACKIMAGE @@ -321,7 +322,8 @@ typedef struct _pack_img uint32_t bpp : 6; // bits per pixel. (image-collector set) uint32_t bppc : 6; // bits per pixel in this channel, equal to 'bpp' if pos.channel_ind == 0x0f. (image-collector set) uint32_t compression : 6; // image data compression, see 'img_compression'. (image-collector set) - uint32_t reserved : 2; // + uint32_t reserved : 1; // + uint32_t prc_last : 1; // whether the last image processor uint32_t prc_stage : 14; // position of image processor, 0 is raw from CIS directly uint32_t prc_time : 18; // spent time in milliseconds of image-process prc_stage uint32_t info_size; // image information size in bytes, information part is used for quality of JPEG, pallete of BMP .... (image-collector set)