1 /*
2 * main.cpp - test
3 *
4 * Copyright (c) 2014 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: Wind Yuan <feng.yuan@intel.com>
19 * Author: John Ye <john.ye@intel.com>
20 */
21
22 #include "device_manager.h"
23 #include "isp/atomisp_device.h"
24 #include "isp/x3a_analyzer_aiq.h"
25 #include "isp/isp_controller.h"
26 #include "isp/isp_image_processor.h"
27 #include <unistd.h>
28 #include <signal.h>
29 #include "test_common.h"
30
31 #if HAVE_LIBDRM
32 #include "drm_display.h"
33 #endif
34
35 using namespace XCam;
36
37 class PollCB: public PollCallback {
38 public:
39
40 #if HAVE_LIBDRM
PollCB(SmartPtr<DrmDisplay> & drm_dev,struct v4l2_format & format)41 PollCB(SmartPtr<DrmDisplay> &drm_dev, struct v4l2_format &format)
42 : _file (NULL)
43 , _format(format)
44 , _drm_dev(drm_dev)
45
46 {
47 open_file();
48 };
49 #else
50 PollCB(struct v4l2_format &format)
51 : _file (NULL),
52 _format(format)
53 {
54 open_file();
55 };
56 #endif
57
~PollCB()58 ~PollCB() {
59 close_file ();
60 };
61 XCamReturn poll_buffer_ready (SmartPtr<VideoBuffer> &buf);
poll_buffer_failed(int64_t timestamp,const char * msg)62 XCamReturn poll_buffer_failed (int64_t timestamp, const char *msg)
63 {
64 XCAM_UNUSED(timestamp);
65 XCAM_UNUSED(msg);
66 XCAM_LOG_DEBUG("%s", __FUNCTION__);
67 return XCAM_RETURN_NO_ERROR;
68 }
x3a_stats_ready(const SmartPtr<X3aStats> & stats)69 XCamReturn x3a_stats_ready (const SmartPtr<X3aStats> &stats) {
70 XCAM_UNUSED(stats);
71 XCAM_LOG_DEBUG("%s", __FUNCTION__);
72 return XCAM_RETURN_NO_ERROR;
73 }
dvs_stats_ready()74 XCamReturn dvs_stats_ready() {
75 XCAM_LOG_DEBUG("%s", __FUNCTION__);
76 return XCAM_RETURN_NO_ERROR;
77 }
78
79 private:
80 void open_file ();
81 void close_file ();
82 size_t dump_to_file(const void *buf, size_t nbyte);
83
84 FILE *_file;
85 struct v4l2_format _format;
86 #if HAVE_LIBDRM
87 SmartPtr<DrmDisplay> _drm_dev;
88 #endif
89 };
90
91 XCamReturn
poll_buffer_ready(SmartPtr<VideoBuffer> & buf)92 PollCB::poll_buffer_ready (SmartPtr<VideoBuffer> &buf) {
93
94 SmartPtr<VideoBuffer> base = buf;
95 XCAM_LOG_DEBUG("%s", __FUNCTION__);
96 FPS_CALCULATION (fps_buf, XCAM_OBJ_DUR_FRAME_NUM);
97
98 // dump_to_file( (void*) buf->get_v4l2_userptr(),
99 // buf->get_v4l2_buf_length()
100 // );
101
102 #if HAVE_LIBDRM
103 //if (!_drm_dev->has_frame_buffer (base))
104 _drm_dev->render_setup_frame_buffer (base);
105
106 _drm_dev->render_buffer (base);
107 #endif
108
109 return XCAM_RETURN_NO_ERROR;
110 }
111
112
113 void
open_file()114 PollCB::open_file ()
115 {
116 if (_file)
117 return;
118 _file = fopen ("capture_buffer.nv12", "wb");
119 }
120
121 void
close_file()122 PollCB::close_file ()
123 {
124 if (_file)
125 fclose (_file);
126 _file = NULL;
127 }
128
129 size_t
dump_to_file(const void * buf,size_t nbyte)130 PollCB::dump_to_file (const void *buf, size_t nbyte)
131 {
132 if (!_file)
133 return 0;
134 return fwrite(buf, nbyte, 1, _file);
135 }
136
137
138 #define V4L2_CAPTURE_MODE_STILL 0x2000
139 #define V4L2_CAPTURE_MODE_VIDEO 0x4000
140 #define V4L2_CAPTURE_MODE_PREVIEW 0x8000
141
142 static Mutex g_mutex;
143 static Cond g_cond;
144 static bool g_stop = false;
145
dev_stop_handler(int sig)146 void dev_stop_handler(int sig)
147 {
148 XCAM_UNUSED (sig);
149
150 SmartLock locker (g_mutex);
151 g_stop = true;
152 g_cond.broadcast ();
153
154 // exit(0);
155 }
156
main(int argc,const char * argv[])157 int main (int argc, const char *argv[])
158 {
159 (void)argv;
160 (void)argc; // suppress unused variable warning
161
162 XCamReturn ret = XCAM_RETURN_NO_ERROR;
163 SmartPtr<V4l2Device> device = new AtomispDevice (DEFAULT_CAPTURE_DEVICE);
164 SmartPtr<V4l2SubDevice> event_device = new V4l2SubDevice (DEFAULT_EVENT_DEVICE);
165 SmartPtr<IspController> isp_controller = new IspController (device);
166 SmartPtr<ImageProcessor> processor = new IspImageProcessor (isp_controller);
167
168 device->set_sensor_id (0);
169 device->set_capture_mode (V4L2_CAPTURE_MODE_VIDEO);
170 //device->set_mem_type (V4L2_MEMORY_MMAP);
171 device->set_mem_type (V4L2_MEMORY_DMABUF);
172 device->set_buffer_count (8);
173 device->set_framerate (25, 1);
174 ret = device->open ();
175 CHECK (ret, "device(%s) open failed", device->get_device_name());
176 //ret = device->set_format (1920, 1080, V4L2_PIX_FMT_NV12, V4L2_FIELD_NONE, 1920 * 2);
177 ret = device->set_format (1920, 1080, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE, 1920 * 2);
178 CHECK (ret, "device(%s) set format failed", device->get_device_name());
179
180
181 ret = event_device->open ();
182 CHECK (ret, "event device(%s) open failed", event_device->get_device_name());
183 int event = V4L2_EVENT_ATOMISP_3A_STATS_READY;
184 ret = event_device->subscribe_event (event);
185 CHECK_CONTINUE (
186 ret,
187 "device(%s) subscribe event(%d) failed",
188 event_device->get_device_name(), event);
189 event = V4L2_EVENT_FRAME_SYNC;
190 ret = event_device->subscribe_event (event);
191 CHECK_CONTINUE (
192 ret,
193 "device(%s) subscribe event(%d) failed",
194 event_device->get_device_name(), event);
195 ret = event_device->start();
196 CHECK (ret, "event device start failed");
197
198 struct v4l2_format format;
199 device->get_format(format);
200
201 #if HAVE_LIBDRM
202 AtomispDevice* atom_isp_dev = (AtomispDevice*)device.ptr();
203 SmartPtr<DrmDisplay> drmdisp = DrmDisplay::instance();
204 struct v4l2_rect rect = { 0, 0, format.fmt.pix.width, format.fmt.pix.height };
205 drmdisp->render_init(
206 0,
207 0,
208 1920,
209 1080,
210 format.fmt.pix.pixelformat,
211 &rect);
212 atom_isp_dev->set_drm_display(drmdisp);
213
214 ret = device->start();
215 CHECK (ret, "capture device start failed");
216 SmartPtr<PollThread> poll_thread = new PollThread();
217 PollCB* poll_cb = new PollCB(drmdisp, format);
218 #else
219 ret = device->start();
220 CHECK(ret, "capture device start failed");
221 SmartPtr<PollThread> poll_thread = new PollThread();
222 PollCB* poll_cb = new PollCB(format);
223 #endif
224
225 poll_thread->set_capture_device(device);
226 poll_thread->set_event_device(event_device);
227 poll_thread->set_poll_callback(poll_cb);
228
229 signal(SIGINT, dev_stop_handler);
230
231 poll_thread->start();
232 CHECK (ret, "poll thread start failed");
233
234 // wait for interruption
235 {
236 SmartLock locker (g_mutex);
237 while (!g_stop)
238 g_cond.wait (g_mutex);
239 }
240
241 ret = poll_thread->stop();
242 CHECK_CONTINUE (ret, "poll thread stop failed");
243 device->close ();
244 event_device->close ();
245
246 return 0;
247 }
248