Home | History | Annotate | Download | only in src
      1 /* Copyright (c) 2013-2014, 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 <pthread.h>
     31 #include <errno.h>
     32 #include <sys/ioctl.h>
     33 #include <sys/types.h>
     34 #include <sys/stat.h>
     35 #include <fcntl.h>
     36 
     37 #include "mm_jpeg_dbg.h"
     38 #include "mm_jpeg_interface.h"
     39 #include "mm_jpeg.h"
     40 
     41 static pthread_mutex_t g_dec_intf_lock = PTHREAD_MUTEX_INITIALIZER;
     42 
     43 static mm_jpeg_obj* g_jpegdec_obj = NULL;
     44 
     45 /** mm_jpeg_intf_start_job:
     46  *
     47  *  Arguments:
     48  *    @client_hdl: client handle
     49  *    @job: jpeg job object
     50  *    @jobId: job id
     51  *
     52  *  Return:
     53  *       0 success, failure otherwise
     54  *
     55  *  Description:
     56  *       start the jpeg job
     57  *
     58  **/
     59 static int32_t mm_jpegdec_intf_start_job(mm_jpeg_job_t* job, uint32_t* job_id)
     60 {
     61   int32_t rc = -1;
     62 
     63   if (NULL == job ||
     64     NULL == job_id) {
     65     CDBG_ERROR("%s:%d] invalid parameters for job or jobId", __func__, __LINE__);
     66     return rc;
     67   }
     68 
     69   pthread_mutex_lock(&g_dec_intf_lock);
     70   if (NULL == g_jpegdec_obj) {
     71     /* mm_jpeg obj not exists, return error */
     72     CDBG_ERROR("%s:%d] mm_jpeg is not opened yet", __func__, __LINE__);
     73     pthread_mutex_unlock(&g_dec_intf_lock);
     74     return rc;
     75   }
     76   rc = mm_jpegdec_start_decode_job(g_jpegdec_obj, job, job_id);
     77   pthread_mutex_unlock(&g_dec_intf_lock);
     78   return rc;
     79 }
     80 
     81 /** mm_jpeg_intf_create_session:
     82  *
     83  *  Arguments:
     84  *    @client_hdl: client handle
     85  *    @p_params: encode parameters
     86  *    @p_session_id: session id
     87  *
     88  *  Return:
     89  *       0 success, failure otherwise
     90  *
     91  *  Description:
     92  *       Create new jpeg session
     93  *
     94  **/
     95 static int32_t mm_jpegdec_intf_create_session(uint32_t client_hdl,
     96     mm_jpeg_decode_params_t *p_params,
     97     uint32_t *p_session_id)
     98 {
     99   int32_t rc = -1;
    100 
    101   if (0 == client_hdl || NULL == p_params || NULL == p_session_id) {
    102     CDBG_ERROR("%s:%d] invalid client_hdl or jobId", __func__, __LINE__);
    103     return rc;
    104   }
    105 
    106   pthread_mutex_lock(&g_dec_intf_lock);
    107   if (NULL == g_jpegdec_obj) {
    108     /* mm_jpeg obj not exists, return error */
    109     CDBG_ERROR("%s:%d] mm_jpeg is not opened yet", __func__, __LINE__);
    110     pthread_mutex_unlock(&g_dec_intf_lock);
    111     return rc;
    112   }
    113 
    114   rc = mm_jpegdec_create_session(g_jpegdec_obj, client_hdl, p_params, p_session_id);
    115   pthread_mutex_unlock(&g_dec_intf_lock);
    116   return rc;
    117 }
    118 
    119 /** mm_jpeg_intf_destroy_session:
    120  *
    121  *  Arguments:
    122  *    @session_id: session id
    123  *
    124  *  Return:
    125  *       0 success, failure otherwise
    126  *
    127  *  Description:
    128  *       Destroy jpeg session
    129  *
    130  **/
    131 static int32_t mm_jpegdec_intf_destroy_session(uint32_t session_id)
    132 {
    133   int32_t rc = -1;
    134 
    135   if (0 == session_id) {
    136     CDBG_ERROR("%s:%d] invalid client_hdl or jobId", __func__, __LINE__);
    137     return rc;
    138   }
    139 
    140   pthread_mutex_lock(&g_dec_intf_lock);
    141   if (NULL == g_jpegdec_obj) {
    142     /* mm_jpeg obj not exists, return error */
    143     CDBG_ERROR("%s:%d] mm_jpeg is not opened yet", __func__, __LINE__);
    144     pthread_mutex_unlock(&g_dec_intf_lock);
    145     return rc;
    146   }
    147 
    148   rc = mm_jpegdec_destroy_session_by_id(g_jpegdec_obj, session_id);
    149   pthread_mutex_unlock(&g_dec_intf_lock);
    150   return rc;
    151 }
    152 
    153 /** mm_jpegdec_intf_abort_job:
    154  *
    155  *  Arguments:
    156  *    @jobId: job id
    157  *
    158  *  Return:
    159  *       0 success, failure otherwise
    160  *
    161  *  Description:
    162  *       Abort the jpeg job
    163  *
    164  **/
    165 static int32_t mm_jpegdec_intf_abort_job(uint32_t job_id)
    166 {
    167   int32_t rc = -1;
    168 
    169   if (0 == job_id) {
    170     CDBG_ERROR("%s:%d] invalid jobId", __func__, __LINE__);
    171     return rc;
    172   }
    173 
    174   pthread_mutex_lock(&g_dec_intf_lock);
    175   if (NULL == g_jpegdec_obj) {
    176     /* mm_jpeg obj not exists, return error */
    177     CDBG_ERROR("%s:%d] mm_jpeg is not opened yet", __func__, __LINE__);
    178     pthread_mutex_unlock(&g_dec_intf_lock);
    179     return rc;
    180   }
    181 
    182   rc = mm_jpegdec_abort_job(g_jpegdec_obj, job_id);
    183   pthread_mutex_unlock(&g_dec_intf_lock);
    184   return rc;
    185 }
    186 
    187 /** mm_jpeg_intf_close:
    188  *
    189  *  Arguments:
    190  *    @client_hdl: client handle
    191  *
    192  *  Return:
    193  *       0 success, failure otherwise
    194  *
    195  *  Description:
    196  *       Close the jpeg job
    197  *
    198  **/
    199 static int32_t mm_jpegdec_intf_close(uint32_t client_hdl)
    200 {
    201   int32_t rc = -1;
    202 
    203   if (0 == client_hdl) {
    204     CDBG_ERROR("%s:%d] invalid client_hdl", __func__, __LINE__);
    205     return rc;
    206   }
    207 
    208   pthread_mutex_lock(&g_dec_intf_lock);
    209   if (NULL == g_jpegdec_obj) {
    210     /* mm_jpeg obj not exists, return error */
    211     CDBG_ERROR("%s:%d] mm_jpeg is not opened yet", __func__, __LINE__);
    212     pthread_mutex_unlock(&g_dec_intf_lock);
    213     return rc;
    214   }
    215 
    216   rc = mm_jpeg_close(g_jpegdec_obj, client_hdl);
    217   g_jpegdec_obj->num_clients--;
    218   if(0 == rc) {
    219     if (0 == g_jpegdec_obj->num_clients) {
    220       /* No client, close jpeg internally */
    221       rc = mm_jpegdec_deinit(g_jpegdec_obj);
    222       free(g_jpegdec_obj);
    223       g_jpegdec_obj = NULL;
    224     }
    225   }
    226 
    227   pthread_mutex_unlock(&g_dec_intf_lock);
    228   return rc;
    229 }
    230 
    231 
    232 
    233 /** jpegdec_open:
    234  *
    235  *  Arguments:
    236  *    @ops: ops table pointer
    237  *
    238  *  Return:
    239  *       0 failure, success otherwise
    240  *
    241  *  Description:
    242  *       Open a jpeg client
    243  *
    244  **/
    245 uint32_t jpegdec_open(mm_jpegdec_ops_t *ops)
    246 {
    247   int32_t rc = 0;
    248   uint32_t clnt_hdl = 0;
    249   mm_jpeg_obj* jpeg_obj = NULL;
    250 
    251   pthread_mutex_lock(&g_dec_intf_lock);
    252   /* first time open */
    253   if(NULL == g_jpegdec_obj) {
    254     jpeg_obj = (mm_jpeg_obj *)malloc(sizeof(mm_jpeg_obj));
    255     if(NULL == jpeg_obj) {
    256       CDBG_ERROR("%s:%d] no mem", __func__, __LINE__);
    257       pthread_mutex_unlock(&g_dec_intf_lock);
    258       return clnt_hdl;
    259     }
    260 
    261     /* initialize jpeg obj */
    262     memset(jpeg_obj, 0, sizeof(mm_jpeg_obj));
    263     rc = mm_jpegdec_init(jpeg_obj);
    264     if(0 != rc) {
    265       CDBG_ERROR("%s:%d] mm_jpeg_init err = %d", __func__, __LINE__, rc);
    266       free(jpeg_obj);
    267       pthread_mutex_unlock(&g_dec_intf_lock);
    268       return clnt_hdl;
    269     }
    270 
    271     /* remember in global variable */
    272     g_jpegdec_obj = jpeg_obj;
    273   }
    274 
    275   /* open new client */
    276   clnt_hdl = mm_jpeg_new_client(g_jpegdec_obj);
    277   if (clnt_hdl > 0) {
    278     /* valid client */
    279     if (NULL != ops) {
    280       /* fill in ops tbl if ptr not NULL */
    281       ops->start_job = mm_jpegdec_intf_start_job;
    282       ops->abort_job = mm_jpegdec_intf_abort_job;
    283       ops->create_session = mm_jpegdec_intf_create_session;
    284       ops->destroy_session = mm_jpegdec_intf_destroy_session;
    285       ops->close = mm_jpegdec_intf_close;
    286     }
    287   } else {
    288     /* failed new client */
    289     CDBG_ERROR("%s:%d] mm_jpeg_new_client failed", __func__, __LINE__);
    290 
    291     if (0 == g_jpegdec_obj->num_clients) {
    292       /* no client, close jpeg */
    293       mm_jpegdec_deinit(g_jpegdec_obj);
    294       free(g_jpegdec_obj);
    295       g_jpegdec_obj = NULL;
    296     }
    297   }
    298 
    299   pthread_mutex_unlock(&g_dec_intf_lock);
    300   return clnt_hdl;
    301 }
    302 
    303 
    304 
    305