1 /* Copyright (c) 2016, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 
30 #include "QCameraHAL3PreviewTest.h"
31 #include "QCameraHAL3MainTestContext.h"
32 
33 namespace qcamera {
34 extern hal3_camera_lib_test *CamObj_handle;
35 int req_sent;
36 extern pthread_cond_t mRequestAppCond;
37 int test_case_end;
38 bool thread_exit;
39 extern std::list<uint32_t> PreviewQueue;
40 int preview_buffer_allocated;
41 extern pthread_mutex_t TestAppLock, mCaptureRequestLock;
42 int snapshot_buffer = -1;
43 
44 
QCameraHAL3PreviewTest(int camid)45 QCameraHAL3PreviewTest::QCameraHAL3PreviewTest(int camid) :
46     QCameraHAL3Test(0),
47     mPreviewHandle(NULL),
48     mCaptureHandle(NULL),
49     mPreviewStream(NULL),
50     nobuffer(0),
51     mCamId(camid),
52     ir_mode(0),
53     svhdr_mode(0)
54 {
55 
56 }
57 
initTest(hal3_camera_lib_test * handle,int testcase,int camid,int w,int h)58 void QCameraHAL3PreviewTest::initTest(hal3_camera_lib_test *handle,
59                                 int testcase, int camid, int w, int h)
60 {
61     int i;
62     CamObj_handle = handle; thread_exit = 0; test_case_end = 0;
63     LOGD("\n buffer thread created for testcase %d  %d and %d ",testcase, w, h);
64     configurePreviewStream(&(handle->test_obj) , camid, w, h);
65     LOGD("\n preview stream configured");
66     constructDefaultRequest(&(handle->test_obj), camid);
67     LOGD("Default stream setting read ");
68     printf("\npipeline_depth is %d", mPipelineDepthPreview);
69     mPreviewHandle = new native_handle_t *[mPipelineDepthPreview];
70     for (i = 0; i < mPipelineDepthPreview; i++)
71         mPreviewHandle[i] = new native_handle_t;
72     for (i = 0, req_sent = 1; i < mPipelineDepthPreview; i++, req_sent++) {
73         previewAllocateBuffers(width, height, i);
74         PreviewQueue.push_back(i);
75     }
76     LOGD(" Request Number is preview : %d ",mRequest.frame_number);
77     mRequest.frame_number = 0;
78     previewProcessThreadCreate(handle);
79 }
80 
snapshotCaptureRequest(hal3_camera_lib_test * handle,int testcase,int camid,int w,int h)81 void QCameraHAL3PreviewTest::snapshotCaptureRequest(hal3_camera_lib_test *handle,
82         int testcase, int camid, int w, int h)
83 {
84     LOGD("Requested Capture Sizes for testcase:%d are :%d  X %d",testcase, w, h);
85     captureRequestRepeat(handle, camid, MENU_START_CAPTURE);
86     pthread_mutex_unlock(&mCaptureRequestLock);
87 }
88 
configurePreviewStream(hal3_camera_test_obj_t * my_test_obj,int camid,int w,int h)89 void QCameraHAL3PreviewTest::configurePreviewStream(hal3_camera_test_obj_t *my_test_obj,
90                                 int camid, int w, int h)
91 {
92     camera3_device_t *device_handle = my_test_obj->device;
93     mPreviewStream =  new camera3_stream_t;
94     memset(mPreviewStream, 0, sizeof(camera3_stream_t));
95     mPreviewStream = initStream(CAMERA3_STREAM_OUTPUT, camid, w, h, 0,
96             HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, HAL3_DATASPACE_UNKNOWN);
97 
98     mPreviewConfig = configureStream(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE, 1);
99     mPreviewConfig.streams[0] = mPreviewStream;
100     device_handle->ops->configure_streams(my_test_obj->device, &(mPreviewConfig));
101     mPipelineDepthPreview = mPreviewConfig.streams[0]->max_buffers;
102     preview_buffer_allocated = mPipelineDepthPreview;
103 }
104 
constructDefaultRequest(hal3_camera_test_obj_t * my_test_obj,int camid)105 void QCameraHAL3PreviewTest::constructDefaultRequest(
106         hal3_camera_test_obj_t *my_test_obj, int camid)
107 {
108     camera3_device_t *device_handle = my_test_obj->device;
109     LOGD("Camera ID : %d",camid);
110     mMetaDataPtr[0]= device_handle->ops->construct_default_request_settings(my_test_obj->device,
111             CAMERA3_TEMPLATE_PREVIEW);
112     mMetaDataPtr[1] = device_handle->ops->construct_default_request_settings(my_test_obj->device,
113             CAMERA3_TEMPLATE_STILL_CAPTURE);
114 }
115 
captureRequestRepeat(hal3_camera_lib_test * my_hal3test_obj,int camid,int testcase)116 void QCameraHAL3PreviewTest::captureRequestRepeat(
117         hal3_camera_lib_test *my_hal3test_obj, int camid, int testcase)
118 {
119     int num1, num2;
120     int ir_mode_changed;
121     int svhdr_mode_changed;
122     int32_t set_svhdr_mode;
123     int32_t set_ir_mode;
124     hal3_camera_test_obj_t *my_test_obj = &(my_hal3test_obj->test_obj);
125     camera3_device_t *device_handle = my_test_obj->device;
126 
127     if (testcase == MENU_START_PREVIEW) {
128         if (PreviewQueue.empty()) {
129             LOGE("no preview buffer for CamID : %d", camid);
130         }
131         else {
132             if (test_case_end == 0) {
133                 LOGD(" Request Number is preview : %d ",mRequest.frame_number);
134                 pthread_mutex_lock(&mCaptureRequestLock);
135                 num2 = PreviewQueue.front();
136                 PreviewQueue.pop_front();
137                 num1 = mRequest.frame_number;
138                 ir_mode_changed = get_ir_mode(ir_mode);
139                 svhdr_mode_changed = get_svhdr_mode(svhdr_mode);
140                 ALOGE("setting IR mode :%d",ir_mode_changed);
141                 ALOGE("setting SVHDR mode :%d",svhdr_mode_changed);
142                 if (num1 < 1) {
143                     (mRequest).settings = mMetaDataPtr[0];
144                 }
145                 else if(ir_mode_changed == 1) {
146                     hal3app_preview_settings = mMetaDataPtr[0];
147                     if(ir_mode == 0) {
148                         ALOGE("setting IR mode off");
149                         set_ir_mode = (int32_t)QCAMERA3_IR_MODE_OFF;
150                     }
151                     else {
152                         ALOGE("setting IR mode On");
153                         set_ir_mode = (int32_t)QCAMERA3_IR_MODE_ON;
154                     }
155                     hal3app_preview_settings.update(QCAMERA3_IR_MODE, &set_ir_mode, 1);
156                     (mRequest).settings = hal3app_preview_settings.release();
157                 }
158                 else if(svhdr_mode_changed == 1) {
159                     hal3app_preview_settings = mMetaDataPtr[0];
160                     if(svhdr_mode == 0) {
161                         ALOGE("setting HDR mode off");
162                         set_svhdr_mode = (int32_t)QCAMERA3_VIDEO_HDR_MODE_OFF;
163                     }
164                     else {
165                         ALOGE("setting HDR mode On");
166                         set_svhdr_mode = (int32_t)QCAMERA3_VIDEO_HDR_MODE_ON;
167                     }
168                     hal3app_preview_settings.update(QCAMERA3_VIDEO_HDR_MODE, &set_svhdr_mode, 1);
169                     (mRequest).settings = hal3app_preview_settings.release();
170                 }
171                 else {
172                     (mRequest).settings = NULL;
173                 }
174                 (mRequest).input_buffer = NULL;
175                 (mRequest).num_output_buffers = 1;
176                 mPreviewStreamBuffs.stream = mPreviewStream;
177                 mPreviewStreamBuffs.status = 0;
178                 mPreviewStreamBuffs.buffer =
179                         (const native_handle_t**)&mPreviewHandle[num2];
180                 mPreviewStreamBuffs.release_fence = -1;
181                 mPreviewStreamBuffs.acquire_fence = -1;
182                 (mRequest).output_buffers = &(mPreviewStreamBuffs);
183                 LOGD("Calling HAL3APP repeat capture request %d and %d and free buffer :%d "
184                         , num1, num2, PreviewQueue.size());
185 
186                 device_handle->ops->process_capture_request(my_test_obj->device, &(mRequest));
187                 (mRequest.frame_number)++;
188                 pthread_mutex_unlock(&mCaptureRequestLock);
189             }
190         }
191     }
192     else {
193         snapshot_buffer = mRequest.frame_number;
194         (mRequest).settings = mMetaDataPtr[1];
195         mSnapshotStreamBuffs = hal3appGetStreamBuffs(mSnapshotStream);
196         mSnapshotStreamBuffs.buffer = (const native_handle_t**)&mCaptureHandle;
197         mRequest = hal3appGetRequestSettings(&mSnapshotStreamBuffs, 1);
198         LOGD("Calling snap HAL3APP repeat capture request repeat %d  ", snapshot_buffer);
199         device_handle->ops->process_capture_request(my_test_obj->device, &(mRequest));
200         (mRequest.frame_number)++;
201     }
202 }
203 
previewTestEnd(hal3_camera_lib_test * my_hal3test_obj,int camid)204 void QCameraHAL3PreviewTest::previewTestEnd(
205         hal3_camera_lib_test *my_hal3test_obj, int camid)
206 {
207     test_case_end = 1;
208     hal3_camera_test_obj_t *my_test_obj = &(my_hal3test_obj->test_obj);
209     camera3_device_t *device_handle = my_test_obj->device;
210     device_handle->ops->flush(my_test_obj->device);
211     LOGD("%s Closing Camera %d", __func__, camid);
212     ioctl(mPreviewMeminfo.ion_fd, ION_IOC_FREE, &mPreviewMeminfo.ion_handle);
213     close(mPreviewMeminfo.ion_fd);
214     mPreviewMeminfo.ion_fd = -1;
215     LOGD("%s Closing thread", __func__);
216     thread_exit = 1;
217 }
218 
previewAllocateBuffers(int width,int height,int num)219 void QCameraHAL3PreviewTest::previewAllocateBuffers(int width, int height, int num)
220 {
221     mPreviewHandle[num] = allocateBuffers(width, height, &mPreviewMeminfo);
222 }
223 
snapshotAllocateBuffers(int width,int height)224 void QCameraHAL3PreviewTest::snapshotAllocateBuffers(int width, int height)
225 {
226     mCaptureHandle = allocateBuffers(width, height, &mCaptureMemInfo);
227 }
228 
previewProcessThreadCreate(hal3_camera_lib_test * handle)229 bool QCameraHAL3PreviewTest::previewProcessThreadCreate(
230         hal3_camera_lib_test *handle)
231 {
232     if(handle == NULL) {
233         LOGD("Camera Hanle is NULL");
234     }
235     processThreadCreate(this, MENU_START_PREVIEW);
236     return 1;
237 }
238 
get_ir_mode(int ir_mode)239 int QCameraHAL3PreviewTest::get_ir_mode(int ir_mode)
240 {
241     static int prev_irmode;
242     if(ir_mode == prev_irmode)
243         return 0;
244     else {
245         prev_irmode = ir_mode;
246         return 1;
247     }
248 }
249 
get_svhdr_mode(int hdr_mode)250 int QCameraHAL3PreviewTest::get_svhdr_mode(int hdr_mode)
251 {
252     static int prev_hdrmode;
253     if(hdr_mode == prev_hdrmode)
254         return 0;
255     else {
256         prev_hdrmode = hdr_mode;
257         return 1;
258     }
259 }
260 
~QCameraHAL3PreviewTest()261 QCameraHAL3PreviewTest::~QCameraHAL3PreviewTest()
262 {
263 
264 }
265 
266 }
267