1 /* Copyright (c) 2012 - 2013, 2015, 2018 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 #ifndef C2D_ColorConverter_H_
31 #define C2D_ColorConverter_H_
32 
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <pthread.h>
36 #include <linux/msm_kgsl.h>
37 #include <sys/ioctl.h>
38 #include <utils/Log.h>
39 #include <dlfcn.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <media/msm_media_info.h>
43 #include <gralloc_priv.h>
44 #include <unordered_map>
45 
46 #include <c2d2.h>
47 #include <sys/types.h>
48 
49 #undef LOG_TAG
50 #define LOG_TAG "C2DColorConvert"
51 #define ALIGN( num, to ) (((num) + (to-1)) & (~(to-1)))
52 #define ALIGN8K 8192
53 #define ALIGN4K 4096
54 #define ALIGN2K 2048
55 #define ALIGN512 512
56 #define ALIGN256 256
57 #define ALIGN128 128
58 #define ALIGN64 64
59 #define ALIGN32 32
60 #define ALIGN16 16
61 
62 #define ADRENO_PIXELFORMAT_R8G8B8A8 28
63 #define ADRENO_PIXELFORMAT_B5G6R5   85
64 
65 typedef C2D_STATUS (*LINK_c2dCreateSurface)( uint32 *surface_id,
66         uint32 surface_bits,
67         C2D_SURFACE_TYPE surface_type,
68         void *surface_definition );
69 
70 typedef C2D_STATUS (*LINK_c2dUpdateSurface)( uint32 surface_id,
71         uint32 surface_bits,
72         C2D_SURFACE_TYPE surface_type,
73         void *surface_definition );
74 
75 typedef C2D_STATUS (*LINK_c2dReadSurface)( uint32 surface_id,
76         C2D_SURFACE_TYPE surface_type,
77         void *surface_definition,
78         int32 x, int32 y );
79 
80 typedef C2D_STATUS (*LINK_c2dDraw)( uint32 target_id,
81         uint32 target_config, C2D_RECT *target_scissor,
82         uint32 target_mask_id, uint32 target_color_key,
83         C2D_OBJECT *objects_list, uint32 num_objects );
84 
85 typedef C2D_STATUS (*LINK_c2dFlush)( uint32 target_id, c2d_ts_handle *timestamp);
86 
87 typedef C2D_STATUS (*LINK_c2dFinish)( uint32 target_id);
88 
89 typedef C2D_STATUS (*LINK_c2dWaitTimestamp)( c2d_ts_handle timestamp );
90 
91 typedef C2D_STATUS (*LINK_c2dDestroySurface)( uint32 surface_id );
92 
93 typedef C2D_STATUS (*LINK_c2dMapAddr)( int mem_fd, void * hostptr, uint32 len, uint32 offset, uint32 flags, void ** gpuaddr);
94 
95 typedef C2D_STATUS (*LINK_c2dUnMapAddr)(void * gpuaddr);
96 
97 typedef void (*LINK_adreno_compute_fmt_aligned_width_and_height)(int width, int height, int plane_id,
98                                                            int format, int num_samples,
99                                                            int tile_mode, int raster_mode,
100                                                            int padding_threshold, int *aligned_w,
101                                                            int *aligned_h);
102 /*TODO: THIS NEEDS TO ENABLED FOR JB PLUS*/
103 enum ColorConvertFormat {
104     RGB565 = 1,
105     YCbCr420Tile,
106     YCbCr420SP,
107     YCbCr420P,
108     YCrCb420P,
109     RGBA8888,
110     RGBA8888_UBWC,
111     NV12_2K,
112     NV12_512,
113     NV12_128m,
114     NV12_UBWC,
115     TP10_UBWC,
116     YCbCr420_VENUS_P010,
117     CbYCrY,
118     NO_COLOR_FORMAT
119 };
120 
121 typedef struct {
122     int32_t numerator;
123     int32_t denominator;
124 } C2DBytesPerPixel;
125 
126 typedef struct {
127   int32_t width;
128   int32_t height;
129   int32_t stride;
130   int32_t sliceHeight;
131   int32_t lumaAlign;
132   int32_t sizeAlign;
133   int32_t size;
134   C2DBytesPerPixel bpp;
135 } C2DBuffReq;
136 
137 typedef enum {
138   C2D_INPUT = 0,
139   C2D_OUTPUT,
140 } C2D_PORT;
141 
142 typedef std::unordered_map <int, int> ColorMapping;
143 
144 class C2DColorConverter{
145 
146   void *mC2DLibHandle;
147   LINK_c2dCreateSurface mC2DCreateSurface;
148   LINK_c2dUpdateSurface mC2DUpdateSurface;
149   LINK_c2dReadSurface mC2DReadSurface;
150   LINK_c2dDraw mC2DDraw;
151   LINK_c2dFlush mC2DFlush;
152   LINK_c2dFinish mC2DFinish;
153   LINK_c2dWaitTimestamp mC2DWaitTimestamp;
154   LINK_c2dDestroySurface mC2DDestroySurface;
155   LINK_c2dMapAddr mC2DMapAddr;
156   LINK_c2dUnMapAddr mC2DUnMapAddr;
157 
158   void *mAdrenoUtilsHandle;
159   LINK_adreno_compute_fmt_aligned_width_and_height mAdrenoComputeFmtAlignedWidthAndHeight;
160 
161   uint32_t mSrcSurface, mDstSurface;
162   void * mSrcSurfaceDef;
163   void * mDstSurfaceDef;
164 
165   C2D_OBJECT mBlit;
166   size_t mSrcWidth;
167   size_t mSrcHeight;
168   size_t mSrcStride;
169   size_t mDstWidth;
170   size_t mDstHeight;
171   size_t mSrcSize;
172   size_t mDstSize;
173   size_t mSrcYSize;
174   size_t mDstYSize;
175   ColorConvertFormat mSrcFormat;
176   ColorConvertFormat mDstFormat;
177   int32_t mFlags;
178 
179   bool enabled;
180   bool mConversionNeeded;
181 
182   pthread_mutex_t mLock;
183 
184  public:
185   C2DColorConverter();
186   ~C2DColorConverter();
187 
getConversionNeeded()188   bool getConversionNeeded() { return mConversionNeeded; }
setConversionNeeded(bool needed)189   void setConversionNeeded(bool needed) { mConversionNeeded = needed; }
190   bool isPropChanged(size_t srcWidth, size_t srcHeight, size_t dstWidth,
191                            size_t dstHeight, ColorConvertFormat srcFormat,
192                            ColorConvertFormat dstFormat, int32_t flags,
193                            size_t srcStride);
194   bool setResolution(size_t srcWidth, size_t srcHeight, size_t dstWidth,
195                      size_t dstHeight, ColorConvertFormat srcFormat,
196                      ColorConvertFormat dstFormat, int32_t flags,
197                      size_t srcStride);
198   int32_t getBuffSize(int32_t port);
199   bool getBuffFilledLen(int32_t port, unsigned int &filled_length);
200   bool getBuffReq(int32_t port, C2DBuffReq *req);
201   int32_t dumpOutput(char * filename, char mode);
202   bool convertC2D(int srcFd, void *srcBase, void * srcData,
203                   int dstFd, void *dstBase, void * dstData);
204   bool isYUVSurface(ColorConvertFormat format);
205   int32_t getDummySurfaceDef(ColorConvertFormat format, size_t width,
206                              size_t height, bool isSource);
207   C2D_STATUS updateYUVSurfaceDef(uint8_t *addr, void *base, void * data, bool isSource);
208   C2D_STATUS updateRGBSurfaceDef(uint8_t *addr, void * data, bool isSource);
209   uint32_t getC2DFormat(ColorConvertFormat format, bool isSource);
210   size_t calcStride(ColorConvertFormat format, size_t width);
211   size_t calcYSize(ColorConvertFormat format, size_t width, size_t height);
212   size_t calcSize(ColorConvertFormat format, size_t width, size_t height);
213   void *getMappedGPUAddr(int bufFD, void *bufPtr, size_t bufLen);
214   bool unmapGPUAddr(unsigned long gAddr);
215   size_t calcLumaAlign(ColorConvertFormat format);
216   size_t calcSizeAlign(ColorConvertFormat format);
217   C2DBytesPerPixel calcBytesPerPixel(ColorConvertFormat format);
218 };
219 
220 #endif  // C2D_ColorConverter_H_
221