• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * fake_poll_thread.cpp - poll thread for raw image
3  *
4  *  Copyright (c) 2014-2015 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Jia Meng <jia.meng@intel.com>
19  */
20 
21 #include "fake_poll_thread.h"
22 #if HAVE_LIBDRM
23 #include "drm_bo_buffer.h"
24 #endif
25 
26 #define DEFAULT_FPT_BUF_COUNT 4
27 
28 namespace XCam {
29 
FakePollThread(const char * raw_path)30 FakePollThread::FakePollThread (const char *raw_path)
31     : _raw_path (NULL)
32     , _raw (NULL)
33 {
34     XCAM_ASSERT (raw_path);
35 
36     if (raw_path)
37         _raw_path = strndup (raw_path, XCAM_MAX_STR_SIZE);
38 }
39 
~FakePollThread()40 FakePollThread::~FakePollThread ()
41 {
42     if (_raw_path)
43         xcam_free (_raw_path);
44 
45     if (_raw)
46         fclose (_raw);
47 }
48 
49 XCamReturn
start()50 FakePollThread::start()
51 {
52     XCAM_FAIL_RETURN(
53         ERROR,
54         _raw_path,
55         XCAM_RETURN_ERROR_FILE,
56         "FakePollThread failed due to raw path NULL");
57 
58     _raw = fopen (_raw_path, "rb");
59     XCAM_FAIL_RETURN(
60         ERROR,
61         _raw,
62         XCAM_RETURN_ERROR_FILE,
63         "FakePollThread failed to open file:%s", XCAM_STR (_raw_path));
64 
65     return PollThread::start ();
66 }
67 
68 XCamReturn
stop()69 FakePollThread::stop ()
70 {
71     if (_buf_pool.ptr ())
72         _buf_pool->stop ();
73 
74     return PollThread::stop ();;
75 }
76 
77 XCamReturn
read_buf(SmartPtr<VideoBuffer> & buf)78 FakePollThread::read_buf (SmartPtr<VideoBuffer> &buf)
79 {
80     uint8_t *dst = buf->map ();
81     const VideoBufferInfo info = buf->get_video_info ();
82     VideoBufferPlanarInfo planar;
83     XCamReturn ret = XCAM_RETURN_NO_ERROR;
84 
85     for (uint32_t index = 0; index < info.components; index++) {
86         info.get_planar_info(planar, index);
87         uint32_t line_bytes = planar.width * planar.pixel_bytes;
88 
89         for (uint32_t i = 0; i < planar.height; i++) {
90             if (fread (dst + info.offsets [index] + i * info.strides [index], 1, line_bytes, _raw) < line_bytes) {
91                 if (feof (_raw)) {
92                     fseek (_raw, 0, SEEK_SET);
93                     ret = XCAM_RETURN_BYPASS;
94                 } else {
95                     XCAM_LOG_ERROR ("poll_buffer_loop failed to read file");
96                     ret = XCAM_RETURN_ERROR_FILE;
97                 }
98                 goto done;
99             }
100         }
101     }
102 
103 done:
104     buf->unmap ();
105     return ret;
106 }
107 
108 XCamReturn
poll_buffer_loop()109 FakePollThread::poll_buffer_loop ()
110 {
111     XCamReturn ret = XCAM_RETURN_NO_ERROR;
112 
113     if (!_buf_pool.ptr () && init_buffer_pool () != XCAM_RETURN_NO_ERROR)
114         return XCAM_RETURN_ERROR_MEM;
115 
116     SmartPtr<VideoBuffer> buf = _buf_pool->get_buffer (_buf_pool);
117     if (!buf.ptr ()) {
118         XCAM_LOG_WARNING ("FakePollThread get buffer failed");
119         return XCAM_RETURN_ERROR_MEM;
120     }
121 
122     ret = read_buf (buf);
123     if (ret == XCAM_RETURN_BYPASS) {
124         ret = read_buf (buf);
125     }
126 
127     SmartPtr<VideoBuffer> video_buf = buf;
128     if (ret == XCAM_RETURN_NO_ERROR && _poll_callback)
129         return _poll_callback->poll_buffer_ready (video_buf);
130 
131     return ret;
132 }
133 
134 XCamReturn
init_buffer_pool()135 FakePollThread::init_buffer_pool ()
136 {
137     struct v4l2_format format;
138     if (!_capture_dev.ptr () ||
139             _capture_dev->get_format (format) != XCAM_RETURN_NO_ERROR) {
140         XCAM_LOG_ERROR ("Can't init buffer pool without format");
141         return XCAM_RETURN_ERROR_PARAM;
142     }
143     VideoBufferInfo info;
144     info.init(format.fmt.pix.pixelformat,
145               format.fmt.pix.width,
146               format.fmt.pix.height, 0, 0, 0);
147 #if HAVE_LIBDRM
148     SmartPtr<DrmDisplay> drm_disp = DrmDisplay::instance ();
149     _buf_pool = new DrmBoBufferPool (drm_disp);
150     XCAM_ASSERT (_buf_pool.ptr ());
151 
152     if (_buf_pool->set_video_info (info) && _buf_pool->reserve (DEFAULT_FPT_BUF_COUNT))
153         return XCAM_RETURN_NO_ERROR;
154 #endif
155 
156     return XCAM_RETURN_ERROR_MEM;
157 }
158 
159 };
160