/**
 * @file hv_mw_DynamicColorWin.c
 * @brief  dpu middleware source file
 * @details This file provides the following functions: \n
 *          (1) dpu operation function \n
 *          (2) init handle \n
 *          (3) Feed 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 <string.h>
#include "PanelConfig.h"
#include "hv_comm_Assert.h"
#include "hv_comm_DataBase.h"
#include "hv_mw_DynamicColorWin.h"
#include "hv_mw_Channel_Manager.h"
#include "hv_mw_PanelManager.h"
#include "hv_mw_SystemManager.h"

#include "hv_drv_DpuCommon.h"
#include "hv_drv_DynamicColorWin.h"

#include "hv_cal_DynamicColorWin.h"

static MainRxFakeVideoParam UpdateMainRxFakeVideoParam(FLOAT32 fRatio, USHORT16 usHsize, USHORT16 usVsize, USHORT16 usPosH, USHORT16 usPosV)
{
    MainRxFakeVideoParam stMainRxFakeVideoParam = {0};
    stMainRxFakeVideoParam.fMainFakeRatio = fRatio;
    stMainRxFakeVideoParam.usMainFakeHsize = usHsize;
    stMainRxFakeVideoParam.usMainFakeVsize = usVsize;
    stMainRxFakeVideoParam.usMainFakePosH = usPosH;
    stMainRxFakeVideoParam.usMainFakePosV = usPosV;
    return stMainRxFakeVideoParam;
}

static VOID SetBrightFramePos(USHORT16 usBfPosH, USHORT16 usBfPosV)
{
    const ChannelManager* pstChannelManager = Hv_Mw_Channel_GetManager();
    const PanelParam stPanelParam = Hv_Mw_Panel_GetCurrentPanelParam();

    USHORT16 usBrightFrameSwitch = (HV_COMMON_DATABASE_GET(UserData, bBrightFrameEn) != HV_FALSE) ? HV_TRUE : HV_FALSE;
    USHORT16 usBfHsize  = HV_COMMON_DATABASE_GET(UserData, usBrightFrameHsize);
    USHORT16 usBfVsize  = HV_COMMON_DATABASE_GET(UserData, usBrightFrameVsize);
    USHORT16 usBfRatio = HV_COMMON_DATABASE_GET(UserData, usBrightFrameRatio);

    MainRxFakeVideoParam stBrightFrameParam = UpdateMainRxFakeVideoParam(usBfRatio, usBfHsize, usBfVsize, usBfPosH, usBfPosV);
    if (usBrightFrameSwitch)
    {
        Hv_Cal_DpuScaler_DcwReadFreeze(1);
        DpuCapCutParam stDpuCapCutParam = {0};
        Hv_Mw_Channel_UpdateMainRxFakeVideoParam(stBrightFrameParam);
        Hv_Drv_DpuScaler_CalcDcwCapCutParam(&(pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].stVideoConfigParams), &stDpuCapCutParam);
        Hv_Drv_DpuScaler_SetBrightFramSyncFlag(HV_TRUE, &stDpuCapCutParam);
    }
    return;
}

static VOID SetBrightFrameSize(USHORT16 usBfHsize, USHORT16 usBfVsize)
{
    const ChannelManager* pstChannelManager = Hv_Mw_Channel_GetManager();
    USHORT16 usBfwitch = (HV_COMMON_DATABASE_GET(UserData, bBrightFrameEn) != HV_FALSE) ? HV_TRUE : HV_FALSE;
    USHORT16 usBfPosH   = HV_COMMON_DATABASE_GET(UserData, usBrightFramePosH);
    USHORT16 usBfPosV   = HV_COMMON_DATABASE_GET(UserData, usBrightFramePosV);
    FLOAT32 fBfRatio = HV_COMMON_DATABASE_GET(UserData, usBrightFrameRatio);
    MainRxFakeVideoParam stMagnifierDynamicParam = UpdateMainRxFakeVideoParam(fBfRatio, usBfHsize, usBfVsize, usBfPosH, usBfPosV);
    if (usBfwitch)
    {
        Hv_Cal_DpuScaler_SetDcwLdgLatch(1);
        Hv_Cal_DpuScaler_DcwSrpLatch(1);
        Hv_Cal_DpuScaler_SetDcwSeamlessApplyInEn(1);

        DpuCapCutParam stDpuCapCutParam = {0};
        Hv_Cal_DpuScaler_DwcLdgTopBlendEn(0);
        Hv_Mw_Channel_UpdateMainRxFakeVideoParam(stMagnifierDynamicParam);
        Hv_Drv_DpuScaler_DcwCapVgsfSettings(&(pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].stVideoConfigParams));
        Hv_Cal_DpuScaler_DcwCapScalerDownParam(&(pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].stVideoConfigParams));
        Hv_Drv_DpuScaler_FrameBufferConfig01(&(pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].stVideoConfigParams));

        HV_ASSERT_SUCCESS_VOID(Hv_Drv_DpuScaler_DpuVdm01Settings(&(pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].stVideoConfigParams)));
        //Hv_Drv_DpuScaler_SubRatio(&(pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].stVideoConfigParams));
        Hv_Drv_DpuScaler_DcwWindowConfig();

        if (HV_COMMON_DATABASE_GET(UserData, ucOsdBfSizeLevel) < 7)
        {
            Hv_Drv_DpuScaler_SetMainFakeBlendFlag(HV_TRUE);
        }
        else
        {
            Hv_Cal_DpuScaler_DcwRvdmRdEnable(0);
            Hv_Cal_DpuScaler_DcwWvdmWrEnable(0);
        }
        /*为了帧率不同步问题，main timing走frame sync，都走frc 1 buf*/
        Hv_Cal_DpuScaler_DcwFrcLowLatencyMode(usBfwitch, pstChannelManager->astChannelData[CHANNEL_TYPE_MAIN].stVideoConfigParams.uiDdrOrWtrVideoHight);
    }
    return;
}

static Status SendNotifyMessageToSystemTask(SettingParamId enParamId, UINT32 uiValue)
{
    SystemEvent stSystemEvent = {0};
    HV_VOS_QUEUE_S* xSystembQueue = Hv_Mw_System_GetSystemQueue();
    HV_ASSERT_VALID_PTR(xSystembQueue);
    stSystemEvent.enEventType = SYSTEM_REV_EVENT_TYPE_OSD_SETTING;
    stSystemEvent.usEventId = enParamId;
    stSystemEvent.uiValue = uiValue;
    HV_ASSERT_TRUE(Hv_Vos_QueueSend(xSystembQueue, (void *)&stSystemEvent));
    return HV_SUCCESS;
}

Status Hv_Mw_DCW_SetBrightFrameEn(UCHAR8 ucEn)
{
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_MAGNIFY)
    if (ucEn)
    {
        HV_COMMON_DATABASE_SET(UserData, bMagnifyGlassSw, 0);
    }
#endif
    HV_COMMON_DATABASE_SET(UserData, bBrightFrameEn, ucEn);
    HV_COMMON_DATABASE_SET(UserData, ucPxPMode, ucEn != 0 ? PXP_MODE_PIP_ENABLE : PXP_MODE_CLOSE);
    HV_ASSERT_SUCCESS(SendNotifyMessageToSystemTask(SETTING_PARAM_ID_MAGNIFY_GLASS_SWITCH, ucEn == 0 ? 3 : ucEn));
    /*节省代码量*/
    return HV_SUCCESS;
}

BOOL Hv_Mw_DCW_GetBrightFrameEn(VOID)
{
    return (BOOL) HV_COMMON_DATABASE_GET(UserData, bBrightFrameEn);
}

VOID Hv_Mw_DCW_SetBrightFrameSize(UCHAR8 ucSizeLevel) 
{
    const ChannelManager* pstChannelManager = Hv_Mw_Channel_GetManager();
    HV_COMMON_DATABASE_SET(UserData, ucOsdBfSizeLevel, ucSizeLevel);
    USHORT16 usPanelVactive = Hv_Mw_Panel_GetPanelVactive();
    USHORT16 usPanelHactive = Hv_Mw_Panel_GetPanelHactive();
    Hv_Drv_DpuScaler_CalcBrightFrameSize(usPanelHactive, usPanelVactive);
    USHORT16 usWidth = HV_COMMON_DATABASE_GET(UserData, usBrightFrameHsize);
    USHORT16 usHeight = HV_COMMON_DATABASE_GET(UserData, usBrightFrameVsize);
    if ((0 != HV_COMMON_DATABASE_GET(UserData, bBrightFrameEn))
    && (CHANNEL_STATE_ACTIVE == pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].enCurrentCHState))
    {
        SetBrightFrameSize(usWidth, usHeight);
    }
    return;
}

UCHAR8 Hv_Mw_DCW_GetBrightFrameSize(VOID)
{
    return HV_COMMON_DATABASE_GET(UserData, ucOsdBfSizeLevel);
}

VOID Hv_Mw_DCW_SetBrightFrameHPos(UCHAR8 ucHPosLevel)
{
    const ChannelManager* pstChannelManager = Hv_Mw_Channel_GetManager();
    HV_COMMON_DATABASE_SET(UserData, ucOsdBfHPosLevel, ucHPosLevel);
    USHORT16 usPanelVactive = Hv_Mw_Panel_GetPanelVactive();
    USHORT16 usPanelHactive = Hv_Mw_Panel_GetPanelHactive();
    Hv_Drv_DpuScaler_CalcBrightFramePos(usPanelHactive, usPanelVactive);
    USHORT16 usHPos = HV_COMMON_DATABASE_GET(UserData, usBrightFramePosH);
    USHORT16 usVPos = HV_COMMON_DATABASE_GET(UserData, usBrightFramePosV);
    if ((0 != HV_COMMON_DATABASE_GET(UserData, bBrightFrameEn))
    && (CHANNEL_STATE_ACTIVE == pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].enCurrentCHState))
    {
        SetBrightFramePos(usHPos, usVPos);
    }
    return;
}

UCHAR8 Hv_Mw_DCW_GetBrightFrameHPos(VOID)
{
    return HV_COMMON_DATABASE_GET(UserData, ucOsdBfHPosLevel);
}

VOID Hv_Mw_DCW_SetBrightFrameVPos(UCHAR8 ucVPosLevel)
{
    const ChannelManager* pstChannelManager = Hv_Mw_Channel_GetManager();
    HV_COMMON_DATABASE_SET(UserData, ucOsdBfVPosLevel, ucVPosLevel);
    USHORT16 usPanelVactive = Hv_Mw_Panel_GetPanelVactive();
    USHORT16 usPanelHactive = Hv_Mw_Panel_GetPanelHactive();
    Hv_Drv_DpuScaler_CalcBrightFramePos(usPanelHactive, usPanelVactive);
    USHORT16 usHPos = HV_COMMON_DATABASE_GET(UserData, usBrightFramePosH);
    USHORT16 usVPos = HV_COMMON_DATABASE_GET(UserData, usBrightFramePosV);
    if ((0 != HV_COMMON_DATABASE_GET(UserData, bBrightFrameEn))
    && (CHANNEL_STATE_ACTIVE == pstChannelManager->astChannelData[DYNAMIC_COLOR_WIN].enCurrentCHState))
    {
        SetBrightFramePos(usHPos, usVPos);
    }
    return;
}

UCHAR8 Hv_Mw_DCW_GetBrightFrameVPos(VOID)
{
    return HV_COMMON_DATABASE_GET(UserData, ucOsdBfVPosLevel);
}
#endif
