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