1/*--------------------------------------------------------------------------
2Copyright (c) 2012-2013, 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
6met:
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
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28--------------------------------------------------------------------------*/
29#include <utils/Log.h>
30#include <gralloc_priv.h>
31#include "vidc_color_converter.h"
32#undef DEBUG_PRINT_LOW
33#undef DEBUG_PRINT_HIGH
34#undef DEBUG_PRINT_ERROR
35
36#define DEBUG_PRINT_LOW ALOGV
37#define DEBUG_PRINT_HIGH ALOGE
38#define DEBUG_PRINT_ERROR ALOGE
39
40omx_c2d_conv::omx_c2d_conv()
41{
42    c2dcc = NULL;
43    mLibHandle = NULL;
44    mConvertOpen = NULL;
45    mConvertClose = NULL;
46    src_format = NV12_2K;
47}
48
49bool omx_c2d_conv::init()
50{
51    bool status = true;
52
53    if (mLibHandle || mConvertOpen || mConvertClose) {
54        DEBUG_PRINT_ERROR("\n omx_c2d_conv::init called twice");
55        status = false;
56    }
57
58    if (status) {
59        mLibHandle = dlopen("libc2dcolorconvert.so", RTLD_LAZY);
60
61        if (mLibHandle) {
62            mConvertOpen = (createC2DColorConverter_t *)
63                dlsym(mLibHandle,"createC2DColorConverter");
64            mConvertClose = (destroyC2DColorConverter_t *)
65                dlsym(mLibHandle,"destroyC2DColorConverter");
66
67            if (!mConvertOpen || !mConvertClose)
68                status = false;
69        } else
70            status = false;
71    }
72
73    if (!status && mLibHandle) {
74        dlclose(mLibHandle);
75        mLibHandle = NULL;
76        mConvertOpen = NULL;
77        mConvertClose = NULL;
78    }
79
80    return status;
81}
82
83bool omx_c2d_conv::convert(int src_fd, void *src_base, void *src_viraddr,
84        int dest_fd, void *dest_base, void *dest_viraddr)
85{
86    int result;
87
88    if (!src_viraddr || !dest_viraddr || !c2dcc || !dest_base || !src_base) {
89        DEBUG_PRINT_ERROR("\n Invalid arguments omx_c2d_conv::convert");
90        return false;
91    }
92
93    result =  c2dcc->convertC2D(src_fd, src_base, src_viraddr,
94            dest_fd, dest_base, dest_viraddr);
95    DEBUG_PRINT_LOW("\n Color convert status %d",result);
96    return ((result < 0)?false:true);
97}
98
99bool omx_c2d_conv::open(unsigned int height,unsigned int width,
100        ColorConvertFormat src, ColorConvertFormat dest)
101{
102    bool status = false;
103
104    if (!c2dcc) {
105        c2dcc = mConvertOpen(width, height, width, height,
106                src,dest,0,0);
107
108        if (c2dcc) {
109            src_format = src;
110            status = true;
111        } else
112            DEBUG_PRINT_ERROR("\n mConvertOpen failed");
113    }
114
115    return status;
116}
117void omx_c2d_conv::close()
118{
119    if (mLibHandle) {
120        if (mConvertClose && c2dcc)
121            mConvertClose(c2dcc);
122
123        c2dcc = NULL;
124    }
125}
126
127void omx_c2d_conv::destroy()
128{
129    DEBUG_PRINT_ERROR("\n Destroy C2D instance");
130
131    if (mLibHandle) {
132        if (mConvertClose && c2dcc)
133            mConvertClose(c2dcc);
134
135        dlclose(mLibHandle);
136    }
137
138    c2dcc = NULL;
139    mLibHandle = NULL;
140    mConvertOpen = NULL;
141    mConvertClose = NULL;
142}
143omx_c2d_conv::~omx_c2d_conv()
144{
145    destroy();
146}
147int omx_c2d_conv::get_src_format()
148{
149    int format = -1;
150
151    if (src_format == NV12_2K) {
152        format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE;
153    } else if (src_format == RGBA8888) {
154        format = HAL_PIXEL_FORMAT_RGBA_8888;
155    }
156
157    return format;
158}
159bool omx_c2d_conv::get_buffer_size(int port,unsigned int &buf_size)
160{
161    int cret = 0;
162    bool ret = false;
163    C2DBuffReq bufferreq;
164
165    if (c2dcc) {
166        bufferreq.size = 0;
167        cret = c2dcc->getBuffReq(port,&bufferreq);
168        DEBUG_PRINT_LOW("\n Status of getbuffer is %d", cret);
169        ret = (cret)?false:true;
170        buf_size = bufferreq.size;
171    }
172
173    return ret;
174}
175