Home | History | Annotate | Download | only in test
      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 
     31 #include "QCameraHAL3Test.h"
     32 #include "QCameraHAL3Base.h"
     33 
     34 namespace qcamera {
     35 hal3_camera_lib_test *CamObj_handle;
     36 extern bool thread_exit;
     37 extern int test_case_end;
     38 buffer_thread_t thread;
     39 extern pthread_cond_t mRequestAppCond;
     40 extern pthread_mutex_t TestAppLock, mCaptureRequestLock;
     41 
     42 camera3_stream_t *QCameraHAL3Test::initStream(int streamtype,
     43         int camid, int w, int h, int usage, int format, int dataspace)
     44 {
     45     LOGD("Stream init for Camera : %d", camid);
     46     requested_stream =  new camera3_stream_t;
     47     memset(requested_stream, 0, sizeof(camera3_stream_t));
     48 
     49     requested_stream->stream_type = streamtype;
     50     requested_stream->width = w;
     51     requested_stream->height = h;
     52     requested_stream->format = format;
     53     requested_stream->usage = usage;
     54     requested_stream->data_space = (android_dataspace_t)dataspace;
     55     requested_stream->rotation = CAMERA3_STREAM_ROTATION_0;
     56     return requested_stream;
     57 }
     58 
     59 QCameraHAL3Test::QCameraHAL3Test(int id)
     60 {
     61     mCamId = id;
     62 }
     63 
     64 camera3_stream_configuration QCameraHAL3Test::configureStream(
     65         int opmode, int num_streams)
     66 {
     67     camera3_stream_configuration requested_config;
     68     requested_config.operation_mode  = opmode;
     69     requested_config.num_streams = num_streams;
     70     requested_config.streams = new camera3_stream_t *[num_streams];
     71     return requested_config;
     72 }
     73 
     74 
     75 camera3_stream_buffer_t QCameraHAL3Test::hal3appGetStreamBuffs(camera3_stream_t *req_stream)
     76 {
     77     camera3_stream_buffer_t stream_buffs;
     78     stream_buffs.stream = req_stream;
     79     stream_buffs.release_fence = -1;
     80     stream_buffs.acquire_fence = -1;
     81     return stream_buffs;
     82 }
     83 
     84 camera3_capture_request QCameraHAL3Test::hal3appGetRequestSettings(
     85         camera3_stream_buffer_t *stream_buffs, int num_buffer)
     86 {
     87     camera3_capture_request request_settings;
     88     request_settings.input_buffer = NULL;
     89     LOGD("Number of buffer sent : %d", num_buffer);
     90     request_settings.num_output_buffers = 1;
     91     request_settings.output_buffers = stream_buffs;
     92     return request_settings;
     93 }
     94 
     95 native_handle_t *QCameraHAL3Test::allocateBuffers(int width, int height,
     96         hal3_camtest_meminfo_t *req_meminfo)
     97 {
     98     struct ion_allocation_data alloc;
     99     struct ion_fd_data ion_info_fd;
    100     int main_ion_fd = -1, rc;
    101     size_t buf_size;
    102     native_handle_t *nh_test;
    103     main_ion_fd = open("/dev/ion", O_RDONLY);
    104     if (main_ion_fd <= 0) {
    105         LOGE("Ion dev open failed %s\n", strerror(errno));
    106         return NULL;
    107     }
    108     memset(&alloc, 0, sizeof(alloc));
    109     buf_size = (size_t)(width * height *2);
    110     alloc.len = (size_t)(buf_size);
    111     alloc.len = (alloc.len + 4095U) & (~4095U);
    112     alloc.align = 4096;
    113     alloc.flags = ION_FLAG_CACHED;
    114     alloc.heap_id_mask = ION_HEAP(ION_SYSTEM_HEAP_ID);
    115     rc = ioctl(main_ion_fd, ION_IOC_ALLOC, &alloc);
    116     if (rc < 0) {
    117         LOGE("ION allocation failed %s with rc = %d \n", strerror(errno), rc);
    118         return NULL;
    119     }
    120     memset(&ion_info_fd, 0, sizeof(ion_info_fd));
    121     ion_info_fd.handle = alloc.handle;
    122     rc = ioctl(main_ion_fd, ION_IOC_SHARE, &ion_info_fd);
    123     if (rc < 0) {
    124         LOGE("ION map failed %s\n", strerror(errno));
    125         return NULL;
    126     }
    127     req_meminfo->ion_fd = main_ion_fd;
    128     req_meminfo->ion_handle = ion_info_fd.handle;
    129     LOGD("%s ION FD %d len %d\n", __func__, ion_info_fd.fd, alloc.len);
    130     nh_test = native_handle_create(2, 4);
    131     nh_test->data[0] = ion_info_fd.fd;
    132     nh_test->data[1] = 0;
    133     nh_test->data[2] = 0;
    134     nh_test->data[3] = 0;
    135     nh_test->data[4] = alloc.len;
    136     nh_test->data[5] = 0;
    137     return nh_test;
    138 }
    139 
    140 void QCameraHAL3Test::captureRequestRepeat(
    141         hal3_camera_lib_test *my_hal3test_obj, int camid, int testcase)
    142 {
    143     if(my_hal3test_obj == NULL) {
    144         LOGE("TestCase : %d Camera:%d Handle is NULL", testcase, camid);
    145     }
    146 }
    147 
    148 bool QCameraHAL3Test::processThreadCreate(
    149         void *obj, int testcase)
    150 {
    151     int32_t ret = 0;
    152     pthread_attr_t attr;
    153     if (pipe(pfd) < 0) {
    154         LOGE("%s: Error in creating the pipe", __func__);
    155     }
    156     pthread_attr_init(&attr);
    157     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    158     pthread_mutex_init(&thread.mutex, NULL);
    159     pthread_cond_init(&thread.cond, NULL);
    160     pthread_cond_init(&mRequestAppCond, NULL);
    161     thread.is_thread_started = 0;
    162     thread.readfd = pfd[0];
    163     thread.writefd = pfd[1];
    164     thread.data_obj = obj;
    165     thread.testcase = testcase;
    166     pthread_mutex_lock(&thread.mutex);
    167     ret = pthread_create(&thread.td, &attr, processBuffers, &thread );
    168     pthread_setname_np(thread.td, "TestApp_Thread");
    169     if (ret < 0) {
    170         LOGE("Failed to create status thread");
    171         return 0;
    172     }
    173     pthread_mutex_unlock(&thread.mutex);
    174     return 1;
    175 }
    176 
    177 void * processBuffers(void *data) {
    178     buffer_thread_t *thread = (buffer_thread_t*)data;
    179     int32_t readfd, writefd;
    180     int testcase;
    181     hal3_camera_lib_test *hal3_test_handle;
    182     struct timespec ts1;
    183     thread->is_thread_started = 1;
    184     readfd = thread->readfd;
    185     writefd = thread->writefd;
    186     testcase = thread->testcase;
    187     QCameraHAL3Test *obj;
    188     obj = (QCameraHAL3Test *)thread->data_obj;
    189     while(!thread_exit) {
    190         pthread_mutex_lock(&thread->mutex);
    191         clock_gettime(CLOCK_REALTIME, &ts1);
    192         ts1.tv_nsec += 10000000L;
    193         pthread_cond_timedwait(&mRequestAppCond, &thread->mutex, &ts1);
    194         pthread_mutex_unlock(&thread->mutex);
    195         hal3_test_handle = CamObj_handle;
    196         if (test_case_end == 0) {
    197             obj->captureRequestRepeat(hal3_test_handle, 0, testcase);
    198         }
    199     }
    200     LOGD("Sensor thread is exiting");
    201     close(readfd);
    202     close(writefd);
    203     pthread_cond_destroy(&mRequestAppCond);
    204     pthread_mutex_unlock(&TestAppLock);
    205     pthread_exit(0);
    206     return NULL;
    207 }
    208 
    209 QCameraHAL3Test::~QCameraHAL3Test()
    210 {
    211 
    212 }
    213 
    214 }
    215