Home | History | Annotate | Download | only in src
      1 /*--------------------------------------------------------------------------
      2 Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
      3 
      4 Redistribution and use in source and binary forms, with or without
      5 modification, are permitted provided that the following conditions are
      6 met:
      7     * Redistributions of source code must retain the above copyright
      8       notice, this list of conditions and the following disclaimer.
      9     * Redistributions in binary form must reproduce the above
     10       copyright notice, this list of conditions and the following
     11       disclaimer in the documentation and/or other materials provided
     12       with the distribution.
     13     * Neither the name of The Linux Foundation nor the names of its
     14       contributors may be used to endorse or promote products derived
     15       from this software without specific prior written permission.
     16 
     17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 --------------------------------------------------------------------------*/
     29 #define LOG_TAG "OMX_C2D"
     30 
     31 #include <utils/Log.h>
     32 #include <gralloc_priv.h>
     33 #include "vidc_color_converter.h"
     34 #include "vidc_debug.h"
     35 
     36 omx_c2d_conv::omx_c2d_conv()
     37 {
     38     c2dcc = NULL;
     39     mLibHandle = NULL;
     40     mConvertOpen = NULL;
     41     mConvertClose = NULL;
     42     src_format = NV12_2K;
     43 }
     44 
     45 bool omx_c2d_conv::init()
     46 {
     47     bool status = true;
     48 
     49     if (mLibHandle || mConvertOpen || mConvertClose) {
     50         DEBUG_PRINT_ERROR("omx_c2d_conv::init called twice");
     51         status = false;
     52     }
     53 
     54     if (status) {
     55         mLibHandle = dlopen("libc2dcolorconvert.so", RTLD_LAZY);
     56 
     57         if (mLibHandle) {
     58             mConvertOpen = (createC2DColorConverter_t *)
     59                 dlsym(mLibHandle,"createC2DColorConverter");
     60             mConvertClose = (destroyC2DColorConverter_t *)
     61                 dlsym(mLibHandle,"destroyC2DColorConverter");
     62 
     63             if (!mConvertOpen || !mConvertClose)
     64                 status = false;
     65         } else
     66             status = false;
     67     }
     68 
     69     if (!status && mLibHandle) {
     70         dlclose(mLibHandle);
     71         mLibHandle = NULL;
     72         mConvertOpen = NULL;
     73         mConvertClose = NULL;
     74     }
     75 
     76     return status;
     77 }
     78 
     79 bool omx_c2d_conv::convert(int src_fd, void *src_base, void *src_viraddr,
     80         int dest_fd, void *dest_base, void *dest_viraddr)
     81 {
     82     int result;
     83 
     84     if (!src_viraddr || !dest_viraddr || !c2dcc || !dest_base || !src_base) {
     85         DEBUG_PRINT_ERROR("Invalid arguments omx_c2d_conv::convert");
     86         return false;
     87     }
     88 
     89     result =  c2dcc->convertC2D(src_fd, src_base, src_viraddr,
     90             dest_fd, dest_base, dest_viraddr);
     91     DEBUG_PRINT_LOW("Color convert status %d",result);
     92     return ((result < 0)?false:true);
     93 }
     94 
     95 bool omx_c2d_conv::open(unsigned int height,unsigned int width,
     96         ColorConvertFormat src, ColorConvertFormat dest)
     97 {
     98     bool status = false;
     99 
    100     if (!c2dcc) {
    101         c2dcc = mConvertOpen(width, height, width, height,
    102                 src,dest,0,0);
    103 
    104         if (c2dcc) {
    105             src_format = src;
    106             status = true;
    107         } else
    108             DEBUG_PRINT_ERROR("mConvertOpen failed");
    109     }
    110 
    111     return status;
    112 }
    113 void omx_c2d_conv::close()
    114 {
    115     if (mLibHandle) {
    116         if (mConvertClose && c2dcc)
    117             mConvertClose(c2dcc);
    118 
    119         c2dcc = NULL;
    120     }
    121 }
    122 
    123 void omx_c2d_conv::destroy()
    124 {
    125     DEBUG_PRINT_HIGH("Destroy C2D instance");
    126 
    127     if (mLibHandle) {
    128         if (mConvertClose && c2dcc)
    129             mConvertClose(c2dcc);
    130 
    131         dlclose(mLibHandle);
    132     }
    133 
    134     c2dcc = NULL;
    135     mLibHandle = NULL;
    136     mConvertOpen = NULL;
    137     mConvertClose = NULL;
    138 }
    139 omx_c2d_conv::~omx_c2d_conv()
    140 {
    141     destroy();
    142 }
    143 int omx_c2d_conv::get_src_format()
    144 {
    145     int format = -1;
    146 
    147     if (src_format == NV12_2K) {
    148         format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE;
    149     } else if (src_format == RGBA8888) {
    150         format = HAL_PIXEL_FORMAT_RGBA_8888;
    151     }
    152 
    153     return format;
    154 }
    155 bool omx_c2d_conv::get_buffer_size(int port,unsigned int &buf_size)
    156 {
    157     int cret = 0;
    158     bool ret = false;
    159     C2DBuffReq bufferreq;
    160 
    161     if (c2dcc) {
    162         bufferreq.size = 0;
    163         cret = c2dcc->getBuffReq(port,&bufferreq);
    164         DEBUG_PRINT_LOW("Status of getbuffer is %d", cret);
    165         ret = (cret)?false:true;
    166         buf_size = bufferreq.size;
    167     }
    168 
    169     return ret;
    170 }
    171 
    172 bool omx_c2d_conv::get_output_filled_length(unsigned int &filled_length)
    173 {
    174     bool ret = false;
    175     C2DBuffReq req;
    176     filled_length = 0;
    177 
    178     if (c2dcc) {
    179         int cret = c2dcc->getBuffReq(C2D_OUTPUT, &req);
    180         DEBUG_PRINT_LOW("Status of getBuffReq is %d", cret);
    181         if (!cret && (req.bpp.denominator > 0)) {
    182             filled_length = (req.stride * req.sliceHeight * req.bpp.numerator);
    183             filled_length /= req.bpp.denominator;
    184             ret = true;
    185         }
    186     }
    187 
    188     return ret;
    189 }
    190