/**
 * @file hv_drv_DynamicColorWin.c
 * @brief dpu driver source code.
 * @details This file provides the following functions: \n
 *          (1) Initialization and reset functions \n
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2024-09-10
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                        <th>Date         <th>Change Description
 * <tr><td>HiView SoC Software Team      <td>2024-09-10   <td>init
 * </table>
*/
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_DPU_BRIGHT_FRAME)

#include "hv_comm_MemoryConfig.h"
#include "hv_vos_Comm.h"

#include "hv_comm_Assert.h"
#include "hv_comm_DataBase.h"
#include "hv_drv_DpuCommon.h"
#include "hv_drv_DynamicColorWin.h"
#include "hv_cal_DynamicColorWin.h"

#define BRIGHT_FRAME_ALIGNMENT      (20)
#define BRIGHT_FRAME_SIZE_MAX       (7)

static USHORT16 RoundUpToMultiple(USHORT16 usOrgValue, USHORT16 usAlignment)
{
    /* 计算需要补齐的数值 */
    USHORT16 usRemainder = usOrgValue % usAlignment;
    if (0 == usRemainder)
    {
        /* 如果已经是usAlignment的整数倍，直接返回 */
        return usOrgValue;
    }
    {
        /* 否则补齐到下一个usAlignment的整数倍 */
        return usOrgValue + (usAlignment - usRemainder);
    }
}

VOID Hv_Drv_DpuScaler_CalcBrightFrameSize(USHORT16 usPanelHactive, USHORT16 usPanelVactive)
{
    UCHAR8 ucSizeLevel = HV_COMMON_DATABASE_GET(UserData, ucOsdBfSizeLevel);
    HV_ASSERT_TRUE_VOID(ucSizeLevel <= BRIGHT_FRAME_SIZE_MAX);
    UCHAR8 ucWidthDiv = 1;
    UCHAR8 ucHeightDiv = 1;
    USHORT16 usWidth = 0;
    USHORT16 usHeight = 0;

    if (BRIGHT_FRAME_SIZE_MAX == ucSizeLevel)
    {
        ucWidthDiv = 1;
        ucHeightDiv = 1;
    }
    else if ((BRIGHT_FRAME_SIZE_MAX - 1) == ucSizeLevel)
    {
        ucWidthDiv = 2;
        ucHeightDiv = 1;
    }
    else
    {
        ucWidthDiv = BRIGHT_FRAME_SIZE_MAX - ucSizeLevel;
        ucHeightDiv = BRIGHT_FRAME_SIZE_MAX - ucSizeLevel;
    }

    usWidth = usPanelHactive / ucWidthDiv;
    usHeight = usPanelVactive / ucHeightDiv;
    usWidth = RoundUpToMultiple(usWidth, BRIGHT_FRAME_ALIGNMENT);
    usHeight = RoundUpToMultiple(usHeight, BRIGHT_FRAME_ALIGNMENT);
    HV_COMMON_DATABASE_SET(UserData, usBrightFrameHsize, usWidth);
    HV_COMMON_DATABASE_SET(UserData, usBrightFrameVsize, usHeight);

    Hv_Drv_DpuScaler_CalcBrightFramePos(usPanelHactive, usPanelVactive);
}

VOID Hv_Drv_DpuScaler_CalcBrightFramePos(USHORT16 usPanelHactive, USHORT16 usPanelVactive)
{
    USHORT16 usWidth = HV_COMMON_DATABASE_GET(UserData, usBrightFrameHsize);
    UCHAR8 ucHPosLevel = HV_COMMON_DATABASE_GET(UserData, ucOsdBfHPosLevel);
    USHORT16 usHPos = 0;
    if (usPanelHactive >= usWidth)
    {
        usHPos = ucHPosLevel * (usPanelHactive - usWidth) / BRIGHT_FRAME_ALIGNMENT + usWidth / 2;
    }

    USHORT16 usHeight = HV_COMMON_DATABASE_GET(UserData, usBrightFrameVsize);
    UCHAR8 ucVPosLevel = HV_COMMON_DATABASE_GET(UserData, ucOsdBfVPosLevel);
    USHORT16 usVPos = 0;
    if (usPanelVactive >= usHeight)
    {
        usVPos = ucVPosLevel * (usPanelVactive - usHeight) / BRIGHT_FRAME_ALIGNMENT + usHeight / 2;
    }

    HV_COMMON_DATABASE_SET(UserData, usBrightFramePosH, usHPos);
    HV_COMMON_DATABASE_SET(UserData, usBrightFramePosV, usVPos);
}

extern VOID Hv_Cal_DpuScaler_DcwCapVgsfOutMaskOn(UINT32 uiValue);
VOID Hv_Drv_DpuScaler_DcwCapVgsfSettings(const VideoConfigParams* pstVideoConfigParams)
{
    HV_ASSERT_VALID_PTR_VOID(pstVideoConfigParams);
    Hv_Cal_DpuScaler_DcwCapVgsfInOutSize(pstVideoConfigParams);
    DpuCapCutParam stDpuCapCutParam = {0};
    if (HV_TRUE == pstVideoConfigParams->bMainFakeSwitch)
    {
        Hv_Drv_DpuScaler_CalcDcwCapCutParam(pstVideoConfigParams, &stDpuCapCutParam);
        Hv_Cal_DpuScaler_DcwCapVgsfOutCutParam(&stDpuCapCutParam);
        Hv_Cal_DpuScaler_DcwCapVgsfOutMaskOn(1);
    }
    return;
}


VOID Hv_Drv_DpuScaler_DcwWindowConfig(VOID)
{
    DpuWindowParam stDcwWindowParam = {0};
    USHORT16 usDcwHsize = HV_COMMON_DATABASE_GET(UserData, usBrightFrameHsize);
    USHORT16 usDcwVsize = HV_COMMON_DATABASE_GET(UserData, usBrightFrameVsize);
    USHORT16 usDcwHpos = HV_COMMON_DATABASE_GET(UserData, usBrightFramePosH);
    USHORT16 usDcwVpos = HV_COMMON_DATABASE_GET(UserData, usBrightFramePosV);
    stDcwWindowParam.usDtcChHsta = usDcwHpos - (usDcwHsize / 2);
    stDcwWindowParam.usDtcChHend = usDcwHpos + (usDcwHsize / 2) - 1;
    stDcwWindowParam.usDtcChVsta = usDcwVpos - (usDcwVsize / 2);
    stDcwWindowParam.usDtcChVend = usDcwVpos + (usDcwVsize / 2) - 1;
    stDcwWindowParam.usBorderHstart = usDcwHpos - (usDcwHsize / 2);
    stDcwWindowParam.usBorderVstart = usDcwVpos - (usDcwVsize / 2);
    stDcwWindowParam.usBorderLength = usDcwHsize;
    stDcwWindowParam.usBorderHeight = usDcwVsize;
    stDcwWindowParam.bBorderEn = 0;
    Hv_Cal_DpuScaler_SetDcwWindowConifg(&stDcwWindowParam);
    return;
}



#endif

