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