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