#ifndef PHSTBDFB_INTERNAL_H    /* Multi-include protection.   <FILENAME>_H */
#define PHSTBDFB_INTERNAL_H
/*****************************************************************************/
/* Copyright (c) 2008 NXP B.V.                                               */
/*                                                                           */
/* This library is free software; you can redistribute it and/or             */
/* modify it under the terms of the GNU Lesser General Public                */
/* License as published by the Free Software Foundation, using               */
/* version 2.1 of the License.                                               */
/*                                                                           */
/* This library is distributed in the hope that it will be useful,           */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU          */
/* Lesser General Public License for more details.                           */
/*                                                                           */
/* You should have received a copy of the GNU Lesser General Public          */
/* License along with this library; if not, write to the Free Software       */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   */
/*                                                                           */
/*****************************************************************************/
/*
   NXP STB22X Driver

   Code is derived from TI Davinci driver.

   TI Davinci driver

   (c) Copyright 2007  Telio AG

   Written by Denis Oliver Kropp <dok@directfb.org>

   Code is derived from VMWare driver.

   (c) Copyright 2001-2007  The DirectFB Organization (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*//**
    @file   phStbDFB_internal.h
    @brief  Local header file for DirectFB graphics driver.

@b  Component:  phStbDFB.

*//*

Rev Date        Author      Comments
--------------------------------------------------------------------------------
  1 20080103    laird       New version for DirectFB 1.1.1
  2 20080716    laird       Rework.
  3 20080716    laird       Adding support for video layers.
  4 20080902    laird       Update Copyright headers.
 4.1.1 20080904    batelaan    Don't need #include <phStbStreamingSystem_Server.h>
  5 20080904    laird       Tidy up header file inclusion.
  6 20080905    laird       Restartable DirectFB driver.
  7 20080905    neill       Fix problems with single framebuffers
  8 20080905    neill       Add VGA modes
  9 20081219    neill       Add support for multiple graphics formats
 10 20090202    steel       Add more cache flushing
 11 20090218    neill       Correct conversion macro defaults
--------------------------------------------------------------------------------
    For consistency and standardisation retain the Section Separators.
*/


/*******************
* INCLUDE FILES    *
********************/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/*Must be included before directfb.h due to bool nonsense*/
#include <tmCompId.h>
#include <ph833xCore.h>
#include <phStbCommon.h>

/* Standard Linux includes.*/
#include <linux/fb.h>
#include <linux/videodenc.h>
#include <asm/page.h>
#include <asm/types.h> /*Do I need this???*/
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <malloc.h>
#include <errno.h>
#include <math.h>

/* Useful DirectFB include files.*/
#include <directfb.h>
#include <dfb_types.h>
#include <core/core.h>
#include <core/coredefs.h>
#include <core/coretypes.h>
#include <core/gfxcard.h>
#include <core/layers.h>
#include <core/layers_internal.h>  // to obtain screen from a CoreLayer
#include <core/layer_control.h>
#include <core/palette.h>
#include <core/screens.h>
#include <core/screens_internal.h> // to obtain screen_data from a CoreScreen
#include <core/state.h>
#include <core/surface.h>
#include <core/surface_pool.h>
#include <core/surface_buffer.h>
#include <core/system.h>
#include <core/windowstack.h>
#include <direct/debug.h>
#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/util.h>
#include <gfx/convert.h>
#include <gfx/util.h>
#include <misc/conf.h>
#include <misc/util.h>

/* NXP Component Include Files. */
#include <phStbDbg.h>
#include <phStbVideoRenderer.h>
#include <phStbDraw.h>
#include <phStbVideoTypes.h>
#include <ak470x.h>
#include <phStbScartSwitch.h>

 /* Include these here to solve redefinition issues.*/
#include "phStbDFB_2d.h"
#include "phStbDFB_gfx.h"
#include "phStbDFB_gfxdriver.h"
#include "phStbDFB_gfx_pool.h"
#include "phStbDFB_encoder.h"
#include "phStbDFB_screen.h"
#include "phStbDFB_mixer.h"
#include "phStbDFB_output.h"
#include "phStbDFB_video.h"
#include "phStbDFB_video_pool.h"

/**************************************************************************
* EXPORTED MACROS                               PH_STBDFB_<_WORD>+        *
***************************************************************************/
#define PAGE_SIZE (size_t)sysconf(_SC_PAGESIZE)
#define PAGE_MASK (size_t)(~(PAGE_SIZE - 1))
/** to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr)	(size_t)(((addr) + PAGE_SIZE - 1) & PAGE_MASK)

/** Number of bits to shift a DFB color adjustment value (range 16 bits)
    to a phStbVideoRenderer value (0-255). */
#define DFB2VIDEORENDERER_COLOR_ADJ_SHIFT 8U

/** Number of bits to shift a DFB brightness adjustment value (range 16 bits)
    to a phStbVideoRenderer brightness value (0-255). */
#define DFB2VIDEORENDERER_BRIGHTNESS_ADJ_SHIFT 8U

/**
 * Define the GFX layer IDs and video layers on each screen
 *
 * The DirectFB Layer numbers are counted from the TV screen back i.e
 *
 * TV  |       |       |       |       |
 *     |   0   |   1   |   2   |   3   |
 *     |  OSD  | Image |  PiP  | Video |
 *
 * LCD                                 |       |
 *                                     |   4   |
 *                                     | Panel |
 */
#define PH_STBDFB_SCREEN_MAIN_GRAPHICS1 0
#define PH_STBDFB_SCREEN_MAIN_GRAPHICS2 1
#define PH_STBDFB_SCREEN_MAIN_DISPLAY1  2
#define PH_STBDFB_SCREEN_MAIN_DISPLAY2  3
#define PH_STBDFB_SCREEN_MAIN_LCD       4

#define PH_STBDFB_MAX_LAYERS            (PH_STBDFB_SCREEN_MAIN_LCD + 1)
#define PH_STBDFB_MAX_SCREENS           (PH_STBVIDEORENDERER_MAX_INSTANCES)


/**
 * Define the screen IDS as an enum
 */
typedef enum
{
    PH_STBDFB_SCREEN_MAIN = DSCID_PRIMARY,
    PH_STBDFB_SCREEN_LCD = 1
} phStbDfb_ScreenID;

/** Total number of layers that we make known to DFB. */
#define PH_STBDFB_NUM_LAYERS_USED 5U

/** Supported Display Layer Options */
#define PH_STBDFB_DLOP_SUPPORTED (DLOP_ALPHACHANNEL | DLOP_OPACITY | DLOP_FLICKER_FILTERING )

/** default Display Layer Options */
#define PH_STBDFB_DEFAULT_DLOP_SUPPORTED (DLOP_ALPHACHANNEL | DLOP_OPACITY)

/**
 * @brief Max number of encoders per renderer. As there is a maximum of
 *        8 encoders in the system and 2 renderers have given 4 to each.
 */
#define PH_STBDFB_MAX_ENCODERS_RENDERER (VIDEODENC_MAX / PH_STBVIDEORENDERER_MAX_INSTANCES)

/**
 * @brief Value for Opaque (solid)
 */
#define OPAQUE      ((UInt8)0xFFU)

/**
 * @brief value of TRANSPARENT
 */
#define TRANSPARENT ((UInt8)0x00U)

/**
 * @brief Convert from TM to DFB pixel formats and ViceVersa.
 */
#define CONVERT_TO_DFB_PIXELFORMAT(_rendFormat) \
       ((_rendFormat) == phStbVideoTypes_PixelFormatYUV420)  ?DSPF_UYVY:    \
       ((_rendFormat) == phStbVideoTypes_PixelFormatYUY2)    ?DSPF_YUY2:    \
       ((_rendFormat) == phStbVideoTypes_PixelFormatUYVY)    ?DSPF_UYVY:    \
       ((_rendFormat) == phStbVideoTypes_PixelFormatARGB8888)?DSPF_ARGB:    \
       ((_rendFormat) == phStbVideoTypes_PixelFormatXRGB8888)?DSPF_RGB32:\
       ((_rendFormat) == phStbVideoTypes_PixelFormatARGB4444)?DSPF_ARGB4444:\
       ((_rendFormat) == phStbVideoTypes_PixelFormatXRGB4444)?DSPF_RGB444:\
       ((_rendFormat) == phStbVideoTypes_PixelFormatARGB1555)?DSPF_ARGB1555:\
       ((_rendFormat) == phStbVideoTypes_PixelFormatXRGB1555)?DSPF_RGB555:\
       ((_rendFormat) == phStbVideoTypes_PixelFormatRGB565)  ?DSPF_RGB16:\
       ((_rendFormat) == phStbVideoTypes_PixelFormatClut256) ?DSPF_LUT8:\
       DSPF_UNKNOWN

#define CONVERT_TO_TM_PIXELFORMAT(_DFBFormat) \
       ((_DFBFormat) == DSPF_UYVY) ?phStbVideoTypes_PixelFormatYUV420:       \
       ((_DFBFormat) == DSPF_YUY2) ?phStbVideoTypes_PixelFormatYUY2:         \
       ((_DFBFormat) == DSPF_UYVY) ?phStbVideoTypes_PixelFormatUYVY:         \
       ((_DFBFormat) == DSPF_ARGB) ?phStbVideoTypes_PixelFormatARGB8888:     \
       ((_DFBFormat) == DSPF_RGB32) ?phStbVideoTypes_PixelFormatXRGB8888: \
       ((_DFBFormat) == DSPF_ARGB4444) ?phStbVideoTypes_PixelFormatARGB4444: \
       ((_DFBFormat) == DSPF_RGB444)   ?phStbVideoTypes_PixelFormatXRGB4444: \
       ((_DFBFormat) == DSPF_ARGB1555) ?phStbVideoTypes_PixelFormatARGB1555: \
       ((_DFBFormat) == DSPF_RGB555)   ?phStbVideoTypes_PixelFormatXRGB1555: \
       ((_DFBFormat) == DSPF_RGB16) ?phStbVideoTypes_PixelFormatRGB565: \
       ((_DFBFormat) == DSPF_LUT8) ?phStbVideoTypes_PixelFormatClut256: \
       phStbVideoTypes_PixelFormatNone

/**
 * @brief convert from DFB to Video denc tv standards and vice versa.
 */
#define CONVERT_TO_DFB_TVSTANDARD(_vdencTVStandard) \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_PAL_BG)   ? DSETV_PAL_BG: \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_PAL_I)    ? DSETV_PAL_I:  \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_PAL_M)    ? DSETV_PAL_M:  \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_PAL_N)    ? DSETV_PAL_N:  \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_PAL_NC)   ? DSETV_PAL_NC: \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_NTSC_M)   ? DSETV_NTSC:   \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_NTSC_M_JPN)  ? DSETV_NTSC_M_JPN: \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_SECAM)    ? DSETV_SECAM:  \
       ((_vdencTVStandard) == VIDEODENC_TV_STANDARD_DIGITAL)  ? DSETV_DIGITAL:\
       DSETV_UNKNOWN

#define CONVERT_FROM_DFB_TVSTANDARD(_DFBTVStandard) \
       ((_DFBTVStandard) == DSETV_PAL_BG)   ? VIDEODENC_TV_STANDARD_PAL_BG: \
       ((_DFBTVStandard) == DSETV_PAL_I)    ? VIDEODENC_TV_STANDARD_PAL_I :  \
       ((_DFBTVStandard) == DSETV_PAL_M)    ? VIDEODENC_TV_STANDARD_PAL_M :  \
       ((_DFBTVStandard) == DSETV_PAL_N)    ? VIDEODENC_TV_STANDARD_PAL_N :  \
       ((_DFBTVStandard) == DSETV_PAL_NC)   ? VIDEODENC_TV_STANDARD_PAL_NC : \
       ((_DFBTVStandard) == DSETV_NTSC)     ? VIDEODENC_TV_STANDARD_NTSC_M :   \
       ((_DFBTVStandard) == DSETV_NTSC_M_JPN)  ? VIDEODENC_TV_STANDARD_NTSC_M_JPN : \
       ((_DFBTVStandard) == DSETV_SECAM)    ? VIDEODENC_TV_STANDARD_SECAM:  \
       ((_DFBTVStandard) == DSETV_DIGITAL)  ? VIDEODENC_TV_STANDARD_DIGITAL :\
       VIDEODENC_TV_STANDARD_PAL_BG

/**
 * @brief Convert from DFB scantype to Video denc scantype and vice versa.
 */
#define CONVERT_TO_DFB_SCANTYPE(_vdencScantype) \
       ((_vdencScantype) == VIDEODENC_SCANTYPE_PROGRESSIVE)   ? DSESM_PROGRESSIVE: \
       ((_vdencScantype) == VIDEODENC_SCANTYPE_INTERLACED)    ? DSESM_INTERLACED:  \
       DSESM_UNKNOWN

#define CONVERT_FROM_DFB_SCANTYPE(_DFBScantype) \
       ((_DFBScantype) == DSESM_PROGRESSIVE )   ? VIDEODENC_SCANTYPE_PROGRESSIVE: \
       ((_DFBScantype) == DSESM_INTERLACED )    ? VIDEODENC_SCANTYPE_INTERLACED: \
       VIDEODENC_SCANTYPE_INTERLACED

/**
 * @brief Convert from TM to DFB Framerates and ViceVersa.
 */
#define CONVERT_TO_DFB_FRAMERATE(_rendFrameRate) \
       ((_rendFrameRate) == phStbVideoRenderer_FrameRate_25)    ?DSEF_25HZ: \
       ((_rendFrameRate) == phStbVideoRenderer_FrameRate_29_97) ?DSEF_29_97HZ: \
       ((_rendFrameRate) == phStbVideoRenderer_FrameRate_50)    ?DSEF_50HZ: \
       ((_rendFrameRate) == phStbVideoRenderer_FrameRate_59_94) ?DSEF_59_94HZ: \
       ((_rendFrameRate) == phStbVideoRenderer_FrameRate_60)    ?DSEF_60HZ: \
       DSEF_UNKNOWN

/**
 * @brief Convert from DFB to Video denc refresh rates and ViceVersa.
 */
#define CONVERT_TO_DFB_REFRESH_RATE(_vdencRefreshRate) \
       ((_vdencRefreshRate) == VIDEODENC_REFRESH_RATE_25HZ)    ?DSEF_25HZ: \
       ((_vdencRefreshRate) == VIDEODENC_REFRESH_RATE_29_97HZ) ?DSEF_29_97HZ: \
       ((_vdencRefreshRate) == VIDEODENC_REFRESH_RATE_50HZ)    ?DSEF_50HZ: \
       ((_vdencRefreshRate) == VIDEODENC_REFRESH_RATE_59_94HZ) ?DSEF_59_94HZ: \
       ((_vdencRefreshRate) == VIDEODENC_REFRESH_RATE_60HZ)    ?DSEF_60HZ: \
       ((_vdencRefreshRate) == VIDEODENC_REFRESH_RATE_75HZ)    ?DSEF_75HZ: \
       DSEF_UNKNOWN

#define CONVERT_FROM_DFB_REFRESH_RATE(_DFBFrameRate) \
       ((_DFBFrameRate) == DSEF_25HZ)    ? VIDEODENC_REFRESH_RATE_25HZ: \
       ((_DFBFrameRate) == DSEF_29_97HZ) ? VIDEODENC_REFRESH_RATE_29_97HZ: \
       ((_DFBFrameRate) == DSEF_50HZ)    ? VIDEODENC_REFRESH_RATE_50HZ: \
       ((_DFBFrameRate) == DSEF_59_94HZ) ? VIDEODENC_REFRESH_RATE_59_94HZ: \
       ((_DFBFrameRate) == DSEF_60HZ)    ? VIDEODENC_REFRESH_RATE_60HZ: \
       ((_DFBFrameRate) == DSEF_75HZ)    ? VIDEODENC_REFRESH_RATE_75HZ: \
       VIDEODENC_REFRESH_RATE_UNKNOWN

/**
 * @brief Convert from Scart switch blanking to Directfb blanking and vice versa.
 */
#define CONVERT_TO_DFB_SLOWBLANKING(_scartBlanking) \
       ((_scartBlanking) == BLANK_SLOW_16_9)    ?DSOSB_16x9: \
       ((_scartBlanking) == BLANK_SLOW_4_3)     ?DSOSB_4x3: \
       ((_scartBlanking) == BLANK_SLOW_FOLLOW)  ?DSOSB_FOLLOW: \
       ((_scartBlanking) == BLANK_SLOW_MONITOR) ?DSOSB_MONITOR: \
       ((_scartBlanking) == BLANK_SLOW_OFF)     ?DSOSB_OFF: \
       DSOSB_OFF

#define CONVERT_FROM_DFB_SLOWBLANKING(_DFBSlowBlanking) \
       ((_DFBSlowBlanking) == DSOSB_16x9) ? BLANK_SLOW_16_9: \
       ((_DFBSlowBlanking) == DSOSB_4x3) ? BLANK_SLOW_4_3: \
       ((_DFBSlowBlanking) == DSOSB_FOLLOW) ? BLANK_SLOW_FOLLOW: \
       ((_DFBSlowBlanking) == DSOSB_MONITOR) ? BLANK_SLOW_MONITOR: \
       ((_DFBSlowBlanking) == DSOSB_OFF) ? BLANK_SLOW_OFF: \
       BLANK_SLOW_4_3

/**
 * @brief Convert from video denc output signals to DFB signals and vice versa.
 */
#define CONVERT_TO_DFB_OUTPUT_SIGNAL(_outSignal) \
       ((_outSignal) == (VIDEODENC_OUTSIGNAL_CVBS|VIDEODENC_OUTSIGNAL_YC)) ? (DSOS_CVBS|DSOS_YC): \
       ((_outSignal) == VIDEODENC_OUTSIGNAL_CVBS)    ? DSOS_CVBS: \
       ((_outSignal) == VIDEODENC_OUTSIGNAL_YC)      ? DSOS_YC: \
       ((_outSignal) == VIDEODENC_OUTSIGNAL_YPRPB)   ? DSOS_YCBCR: \
       ((_outSignal) == VIDEODENC_OUTSIGNAL_RGB)     ? DSOS_RGB: \
       ((_outSignal) == VIDEODENC_OUTSIGNAL_DIGITAL) ? DSOS_HDMI: \
       DSOS_CVBS

#define CONVERT_FROM_DFB_OUTPUT_SIGNAL(_DFBOutputSignal) \
       ((_DFBOutputSignal) == (DSOS_CVBS|DSOS_YC))  ? (VIDEODENC_OUTSIGNAL_CVBS|VIDEODENC_OUTSIGNAL_YC): \
       ((_DFBOutputSignal) == DSOS_CVBS)  ? VIDEODENC_OUTSIGNAL_CVBS: \
       ((_DFBOutputSignal) == DSOS_YC)    ? VIDEODENC_OUTSIGNAL_YC: \
       ((_DFBOutputSignal) == DSOS_YCBCR) ? VIDEODENC_OUTSIGNAL_YPRPB: \
       ((_DFBOutputSignal) == DSOS_RGB)   ? VIDEODENC_OUTSIGNAL_RGB: \
       ((_DFBOutputSignal) == DSOS_HDMI)  ? VIDEODENC_OUTSIGNAL_DIGITAL: \
       VIDEODENC_OUTSIGNAL_CVBS

/**
 * @brief Convert from video denc output connectors to DFB connectors and vice versa.
 */
#define CONVERT_TO_DFB_OUTPUT_CONNECTOR(_outConnector, _conIndex) \
       ((_outConnector) == VIDEODENC_CONNECTOR_SINGLE_RCA && (_conIndex) == 0)    ?DSOC_CVBS: \
       ((_outConnector) == VIDEODENC_CONNECTOR_SCART && (_conIndex) == 0)         ?DSOC_SCART: \
       ((_outConnector) == VIDEODENC_CONNECTOR_SCART && (_conIndex) == 1)         ?DSOC_SCART2: \
       ((_outConnector) == VIDEODENC_CONNECTOR_MINI_DIN && (_conIndex) == 0)      ?DSOC_YC: \
       ((_outConnector) == VIDEODENC_CONNECTOR_TRIPLE_RCA && (_conIndex) == 0)    ?DSOC_COMPONENT: \
       ((_outConnector) == VIDEODENC_CONNECTOR_VGA && (_conIndex) == 0)           ?DSOC_VGA: \
       ((_outConnector) == VIDEODENC_CONNECTOR_HDMI && (_conIndex) == 0)          ?DSOC_HDMI: \
       DSOC_UNKNOWN

#define CONVERT_FROM_DFB_OUTPUT_CONNECTOR(_DFBOutputConnector) \
       ((_DFBOutputConnector) == DSOC_CVBS)  ? VIDEODENC_CONNECTOR_SINGLE_RCA: \
       ((_DFBOutputConnector) == DSOC_SCART)    ? VIDEODENC_CONNECTOR_SCART: \
       ((_DFBOutputConnector) == DSOC_SCART2) ? VIDEODENC_CONNECTOR_SCART: \
       ((_DFBOutputConnector) == DSOC_YC)   ? VIDEODENC_CONNECTOR_MINI_DIN: \
       ((_DFBOutputConnector) == DSOC_COMPONENT)   ? VIDEODENC_CONNECTOR_TRIPLE_RCA: \
       ((_DFBOutputConnector) == DSOC_VGA)    ? VIDEODENC_CONNECTOR_VGA: \
       ((_DFBOutputConnector) == DSOC_HDMI)   ? VIDEODENC_CONNECTOR_HDMI: \
       VIDEODENC_CONNECTOR_NONE

/**
 * @brief Convert from video denc resolution to a DFB enum type.
 */
#define CONVERT_TO_DFB_OUTPUT_RESOLUTION(_xres, _yres)          \
       ((_xres) == 640 && (_yres) == 480)     ? DSOR_640_480:   \
       ((_xres) == 720 && (_yres) == 480)     ? DSOR_720_480:   \
       ((_xres) == 720 && (_yres) == 576)     ? DSOR_720_576:   \
       ((_xres) == 800 && (_yres) == 600)     ? DSOR_800_600:   \
       ((_xres) == 1024 && (_yres) == 768)    ? DSOR_1024_768:  \
       ((_xres) == 1152 && (_yres) == 864)    ? DSOR_1152_864:  \
       ((_xres) == 1280 && (_yres) == 720)    ? DSOR_1280_720:  \
       ((_xres) == 1280 && (_yres) == 768)    ? DSOR_1280_768:  \
       ((_xres) == 1280 && (_yres) == 800)    ? DSOR_1280_800:  \
       ((_xres) == 1280 && (_yres) == 960)    ? DSOR_1280_960:  \
       ((_xres) == 1280 && (_yres) == 1024)   ? DSOR_1280_1024: \
       ((_xres) == 1400 && (_yres) == 1050)   ? DSOR_1400_1050: \
       ((_xres) == 1600 && (_yres) == 1200)   ? DSOR_1600_1200: \
       ((_xres) == 1920 && (_yres) == 1080)   ? DSOR_1920_1080: \
       ((_xres) == 1360 && (_yres) == 768)    ? DSOR_1360_768: \
       DSOR_UNKNOWN

/** @brief Convert from DFB enum type back to video denc X resolution. */
#define CONVERT_FROM_OUTPUT_RESOLUTION_TO_X(_DFBoutputRes) \
       ((_DFBoutputRes) == DSOR_640_480)   ? 640U :        \
       ((_DFBoutputRes) == DSOR_720_480)   ? 720U :        \
       ((_DFBoutputRes) == DSOR_720_576)   ? 720U :        \
       ((_DFBoutputRes) == DSOR_800_600)   ? 800U :        \
       ((_DFBoutputRes) == DSOR_1024_768)  ? 1024U :       \
       ((_DFBoutputRes) == DSOR_1152_864)  ? 1152U :       \
       ((_DFBoutputRes) == DSOR_1280_720)  ? 1280U :       \
       ((_DFBoutputRes) == DSOR_1280_768)  ? 1280U :       \
       ((_DFBoutputRes) == DSOR_1280_800)  ? 1280U :       \
       ((_DFBoutputRes) == DSOR_1280_960)  ? 1280U :       \
       ((_DFBoutputRes) == DSOR_1280_1024) ? 1280U :       \
       ((_DFBoutputRes) == DSOR_1400_1050) ? 1400U :       \
       ((_DFBoutputRes) == DSOR_1600_1200) ? 1600U :       \
       ((_DFBoutputRes) == DSOR_1920_1080) ? 1920U :       \
       ((_DFBoutputRes) == DSOR_1360_768)  ? 1360U :       \
       0

/** @brief Convert from DFB enum type back to video denc Y resolution. */
#define CONVERT_FROM_OUTPUT_RESOLUTION_TO_Y(_DFBoutputRes) \
       ((_DFBoutputRes) == DSOR_640_480)   ? 480U :        \
       ((_DFBoutputRes) == DSOR_720_480)   ? 480U :        \
       ((_DFBoutputRes) == DSOR_720_576)   ? 576U :        \
       ((_DFBoutputRes) == DSOR_800_600)   ? 600U :        \
       ((_DFBoutputRes) == DSOR_1024_768)  ? 768U :        \
       ((_DFBoutputRes) == DSOR_1152_864)  ? 864U :        \
       ((_DFBoutputRes) == DSOR_1280_720)  ? 720U :        \
       ((_DFBoutputRes) == DSOR_1280_768)  ? 768U :        \
       ((_DFBoutputRes) == DSOR_1280_800)  ? 800U :        \
       ((_DFBoutputRes) == DSOR_1280_960)  ? 960U :        \
       ((_DFBoutputRes) == DSOR_1280_1024) ? 1024U :       \
       ((_DFBoutputRes) == DSOR_1400_1050) ? 1050U :       \
       ((_DFBoutputRes) == DSOR_1600_1200) ? 1200U :       \
       ((_DFBoutputRes) == DSOR_1920_1080) ? 1080U :       \
       ((_DFBoutputRes) == DSOR_1360_768)  ? 768U :        \
       0

/**
 * @brief some useful macros for deciding what to do when a DFB error occurs.
 */
#define DFB_RETURN_ON_ERR(x, msg...)                               \
    do                                                             \
    {                                                              \
        DFBResult err;                                             \
        err = (x);                                                 \
                                                                   \
        if (err != DFB_OK)                                         \
        {                                                          \
            D_WARN(msg);                                           \
            return err;                                            \
        }                                                          \
    } while(0)                                                     \

#define DFB_INFO_ON_ERROR(x, msg)                                  \
    do                                                             \
    {                                                              \
        DFBResult err;                                             \
        err = (x);                                                 \
                                                                   \
        if (err != DFB_OK)                                         \
        {                                                          \
            D_INFO(msg);                                           \
        }                                                          \
    } while (0)                                                    \

#define DFB_GOTO_ON_ERROR(x, msg, label)                           \
    do                                                             \
    {                                                              \
        DFBResult err;                                             \
        err = (x);                                                 \
                                                                   \
        if (err != DFB_OK)                                         \
        {                                                          \
            D_WARN(msg);                                           \
            goto (label);                                          \
        }                                                          \
    } while (0)                                                    \

#define TM_TO_DFB_RETURN_ON_ERR(x, msg...)                         \
    do                                                             \
    {                                                              \
        tmErrorCode_t err;                                         \
        err = (x);                                                 \
                                                                   \
        if (err != TM_OK)                                          \
        {                                                          \
            D_BUG(msg);                                            \
            return DFB_BUG;                                        \
        }                                                          \
    } while (0)                                                    \

/*********************************************************************
* EXPORTED TYPEDEFS                           [p]phStbDfb_<Word>+_t  *
**********************************************************************/
/**
 * @brief Storage structure mapping renderers,encoders and file descriptors.
 */
typedef struct
{
    bool inUse;
    uint32_t renderer;
    uint32_t encoder;
    char_t   devName[32];
} phStbDFB_VdencMap_t;

/** Runtime information associated with each DFB Screen.
 *  Maps to a phStbVideoRenderer_t instance.
 */
typedef struct {
    CoreScreen                        *screen;
    uint32_t                          unitId;
    phStbVideoRenderer_DisplayChain_t dispChain;
    uint32_t                          screenWidth;
    uint32_t                          screenHeight;
    uint32_t                          numberEncoders;
    phStbDFB_VdencMap_t               vdencMapping[PH_STBDFB_MAX_ENCODERS_RENDERER];
} phStbDFB_ScreenData_t;

/**
 * Runtime info associated with a DFB layer.
 * Maps to a phStbVideoRenderer_Layer_t.
 */
typedef struct
{
    /** This is the DFB layer ID (which is different to the Renderer Layer ID) */
    uint32_t                    layerId;

    /** The layer Handle for the renderer*/
    phStbVideoRenderer_Layer_t *pLayer;

    /** The video layer name */
    char_t                      name[PH_STBVIDEORENDERER_MAX_NAME_LENGTH + 1];

    /**The Coresurface associated with this layer NULL if not being used by DFB*/
    CoreSurface                *surface;

    /** The CoreLayer configuration*/
    CoreLayerRegionConfig       config;

    /** The supported layer types*/
    DFBDisplayLayerTypeFlags    layerType;

} phStbDFB_LayerData_t;

typedef struct
{
    /* --  HW Acceleration variables -- */
    /* state validation */
    int            v_flags;

    /* prepared register values */
    uint32_t       SurfAlpha;
    uint32_t       HAlphaColor;
    uint32_t       MonoPatFColor;

    /* cached values */
    DFBSurfaceDrawingFlags  drawingflags;
    DFBSurfaceBlittingFlags blittingflags;
    DFBSurfaceBlendFunction src_blend;

    bool cacheFlushNeeded;

    /** The Framebuffer Fixed screen information.*/
    struct fb_fix_screeninfo  fix[PH_STBDFB_MAX_LAYERS];

    /** The Framebuffer Variable information. */
    struct fb_var_screeninfo  var[PH_STBDFB_MAX_LAYERS];

    /* Surface Pools. */
    CoreSurfacePool          *gfx_pool;
    CoreSurfacePool          *video_pool;
} phStbDFB_DeviceData_t;

typedef struct {
     uint32_t                 num;
     int32_t                  fd;
     void                     *mem;
     uint32_t                 size;
} phStbDFB_FBinfo_t;

typedef struct {
     phStbDFB_DeviceData_t    *pdev;

     CoreDFB                  *core;

     /* Used by each instance of DirectFB.*/
     phStbDFB_FBinfo_t        fb[PH_STBDFB_MAX_LAYERS];
     CoreScreen*              screens[PH_STBDFB_MAX_SCREENS];
     CoreLayer*               layers[PH_STBDFB_MAX_LAYERS];

     uint32_t                 num_gfx_layers;
     uint32_t                 num_video_layers;

     uint32_t                 resource_id[PH_STBDFB_MAX_LAYERS];

     /* Difference File Decriptor for each instance of DirectFB. */
     phStbDFB_PNXDraw_t       pnxdraw;
     bool                     pnxdraw_present;
} phStbDFB_DriverData_t;

/**************************************************************************
* EXPORTED DATA                          g[k|p|kp|pk|kpk]phStbDfb_<Word>+ *
***************************************************************************/

extern int32_t  phStbDFB_screen_idx;
extern uint32_t phStbDFB_gfx_layer_idx;
extern uint32_t phStbDFB_video_layer_idx;

/****************************************************************************
* EXPORTED FUNCTION PROTOTYPES                         phStbDfb[_<Word>+]   *
*****************************************************************************/

#endif /* PHSTBDFB_INTERNAL_H      Do not add any thing below this line */

