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