idp_scen_cp_mt6252.c 61 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *    idp_scen_cp_mt6252.c
 *
 * Project:
 * --------
 *    MAUI
 *
 * Description:
 * ------------
 *    ISP -> CRZ -> ROTDMA -> MEM -> LCD
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by ClearCase. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * $Log$
 *
 * 11 14 2013 chrono.wu
 * removed!
 * .
 *
 * 07 18 2013 chrono.wu
 * removed!
 * fix change buffer condition for 1 pass preview case.
 *
 * 03 25 2013 chrono.wu
 * removed!
 * MT6260 WVGA Camera features support full screen.
 *
 * 02 20 2013 peter.wang
 * removed!
 * .
 *
 * 02 19 2013 peter.wang
 * removed!
 * .
 *
 * 02 07 2013 peter.wang
 * removed!
 * .
 *
 * 01 11 2013 peter.wang
 * removed!
 * .
 *
 * 12 13 2012 peter.wang
 * removed!
 * .
 *
 * 11 06 2012 peter.wang
 * removed!
 * .
 * 
 * 09 11 2012 peter.wang
 * removed!
 * .
 * 
 * 07 20 2012 peter.wang
 * removed!
 * .
 *
 * 07 05 2012 peter.wang
 * removed!
 * .
 *
 * 07 03 2012 peter.wang
 * removed!
 * .
 *
 * 07 02 2012 peter.wang
 * removed!
 * .
 *
 * 06 19 2012 peter.wang
 * removed!
 * .
 *
 * 06 19 2012 peter.wang
 * removed!
 * .
 *
 * 06 13 2012 peter.wang
 * removed!
 * .
 *
 * 06 12 2012 peter.wang
 * removed!
 * .
 *
 * 05 31 2012 peter.wang
 * removed!
 * .
 *
 * 05 30 2012 peter.wang
 * removed!
 * .
 *
 * 05 25 2012 peter.wang
 * removed!
 * .
 *
 * 05 25 2012 peter.wang
 * removed!
 * .
 *
 * 05 16 2012 peter.wang
 * removed!
 * .
 *
 * 05 08 2012 peter.wang
 * removed!
 * .
 *
 * 05 02 2012 peter.wang
 * removed!
 * .
 *
 * 04 23 2012 peter.wang
 * removed!
 * .
 *
 * 04 18 2012 peter.wang
 * removed!
 * .
 *
 * 03 20 2012 peter.wang
 * removed!
 * .
 *
 * 03 16 2012 peter.wang
 * removed!
 * .
 *
 * 03 15 2012 peter.wang
 * removed!
 * .
 *
 * 03 12 2012 peter.wang
 * removed!
 * .
 *
 * 02 24 2012 peter.wang
 * removed!
 * .
 *
 * 02 23 2012 peter.wang
 * removed!
 * .
 *
 * 02 23 2012 peter.wang
 * removed!
 * .
 *
 * 02 19 2012 peter.wang
 * removed!
 * .
 *
 * 02 15 2012 peter.wang
 * removed!
 * .
 *
 * 01 10 2012 peter.wang
 * removed!
 * .
 *
 * 12 28 2011 peter.wang
 * removed!
 * .
 *
 * 12 27 2011 peter.wang
 * removed!
 * .
 *
 * 12 16 2011 peter.wang
 * removed!
 * .
 *
 * 11 25 2011 peter.wang
 * removed!
 * .
 *
 * 11 17 2011 peter.wang
 * removed!
 * .
 *
 * 09 21 2011 peter.wang
 * removed!
 * .
 *
 * 09 15 2011 peter.wang
 * removed!
 * .
 *
 * 08 29 2011 peter.wang
 * removed!
 * .
 *
 * 08 19 2011 peter.wang
 * removed!
 * .
 *
 * 08 10 2011 peter.wang
 * removed!
 * .
 *
 * 07 14 2011 peter.wang
 * removed!
 * .
 *
 * 07 12 2011 peter.wang
 * removed!
 * .
 *
 * 06 29 2011 peter.wang
 * removed!
 * .
 *
 * 05 23 2011 peter.wang
 * removed!
 * .
 *
 * 05 17 2011 peter.wang
 * removed!
 * .
 *
 * 05 16 2011 peter.wang
 * removed!
 * .
 *
 * 05 11 2011 peter.wang
 * removed!
 * .
 *
 * 04 26 2011 peter.wang
 * removed!
 * .
 *
 * 04 22 2011 peter.wang
 * removed!
 * .
 *
 * 04 18 2011 peter.wang
 * removed!
 * .
 *
 * 04 15 2011 peter.wang
 * removed!
 * .
 *
 * 02 17 2011 cm.huang
 * removed!
 * .
 *
 * 01 14 2011 cm.huang
 * removed!
 * .
 *
 * 12 06 2010 cm.huang
 * removed!
 * .
 *
 * 12 06 2010 cm.huang
 * removed!
 * .
 *
 * 12 01 2010 cm.huang
 * removed!
 * .
 *
 * 11 26 2010 cm.huang
 * removed!
 * .
 *
 * 11 24 2010 cm.huang
 * removed!
 * .
 *
 * 11 23 2010 cm.huang
 * removed!
 * .
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by ClearCase. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/
/****************************************************************************
 * macros and options
 ****************************************************************************/
#define THIS_FILE_BELONGS_TO_IMAGE_DATA_PATH_MODULE_INTERNAL

/****************************************************************************
 * include directives
 ****************************************************************************/
/* system includes */
#include "app_ltlcom.h" /* Task message communiction */

#include "drv_comm.h"
#include <idp_define.h>

#if defined(DRV_IDP_6252_SERIES)

#include <idp_core.h>

#include <idp_camera_preview.h>
#include <mt6252/idp_engines.h>
#include <idp_cam_if.h>

#if defined(IDP_HISR_SUPPORT)
#include <mt6252/idp_hisr.h>
#endif
#include <mt6252/idp_scen_common_mt6252.h>

//include wfc check busy api
#include <wfc/inc/wfc_mm_api.h>

/* to access the define of __POSTPROC_SUPPORT__ */
#include "pp_feature_def.h"

//include G2D api
#include "g2d_enum.h"
#include "g2d_common_api.h"
#include "g2d_lt_api.h"
#include "g2d_bitblt_api.h"
#include "g2d_font_api.h"
#include "g2d_rectfill_api.h"
#include "g2d_mutex.h"

//include CAL api
#include "cal_task_msg_if.h"

IDP_COMMON_API_DECLARATION(camera_preview)

#if (defined(__POSTPROC_SUPPORT__) && defined(G2D_SUPPORT))
#define IDP_USE_G2D_AS_2NDPASS
#endif

#if (defined(__MULTIPLE_ANGLE_VIEW_SUPPORT__)||defined(__3D_IMAGE_SUPPORT__))
#define IDP_SELF_2NDPASS
#endif

/****************************************************************************
 * local variables
 ****************************************************************************/
static struct
{
    // Note this! this does not represent HW/SW trigger...
    kal_bool                  enable_trigger_lcd; /**< KAL_TRUE to enable trigger LCD, KAL_FALSE to disable trigger LCD */

    /**  CRZ param/config_cp START  *************************************/
    kal_uint16                image_src_width;  /* image width for CRZ input */
    kal_uint16                image_src_height; /* image height for CRZ input */
    kal_uint16                preview_width;    /* image width for LCD preview */
    kal_uint16                preview_height;   /* image height for LCD preview */

    kal_bool                  enable_crz_frame_start_intr;
    /**  CRZ param/config_cp   END  *************************************/

    /**  ROT0 param/config_cp  START  ***********************************/
    kal_uint32                frame_buffer_address; /* image buffer for LCM source buffer */
    kal_uint32                frame_buffer_address1; /* second image buffer for LCM source buffer */
    kal_uint32                frame_buffer_address2; /* Third image buffer for LCM source buffer */

    img_rot_angle_enum_t      rot_angle;
#if defined(__CAMERA_GYRO_SUPPORT__)
    MM_IMAGE_COLOR_FORMAT_ENUM preview_image_color_format;
#endif
    /**  ROT0 param/config_cp    END  ***********************************/

    /** FW trigger param ***********/
    kal_uint32                 preview_buffer_idx;
    /**  FW trigger param   END  *******/

    /** crop param ***********/
    //kal_bool                  enable_crop; /* KAL_TRUE or KAL_FALSE to enable or disable overlay function */
    kal_uint16                crop_width;
    kal_uint16                crop_height;

    kal_bool                  dbuf_tearing_free_enable;
    /** crop param END***********/

#if defined(__ATV_SUPPORT__)
    kal_bool                  trigger_display_delay_to_frame_start_enable;
#endif

#if (defined(IDP_USE_G2D_AS_2NDPASS) || defined(IDP_SELF_2NDPASS))
    kal_bool                  postproc_image_enable;
    img_rot_angle_enum_t      postproc_image_rot_angle;
    kal_uint32                postproc_image_width;
    kal_uint32                postproc_image_height;
    kal_uint32                postproc_image_buffer_address[3]; 

    MM_IMAGE_COLOR_FORMAT_ENUM postproc_image_color_format;

    kal_bool                  mav_capture_enable;
#endif

    kal_bool                  end_config;

    kal_bool                  crop_en;
#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    kal_uint32                cp_tmp_buffer;
    tile_calc_para            tile_para;
#endif
} config_cp;


#if defined(IDP_HISR_SUPPORT)
static idp_hisr_handle_t idp_camera_preview_frame_start_hisr_handle;
static idp_hisr_handle_t idp_camera_preview_display_frame_done_hisr_handle;
#endif

#if defined(MT6252H) || defined(MT6252)
static kal_bool _idp_scen_crz_width_over_limit = KAL_FALSE;
static kal_uint32 _idp_scen_crz_over_limit_width;
#endif

#if defined(__ATV_SUPPORT__)
kal_bool idp_camera_preview_pxd;
#endif

#if defined(IDP_USE_G2D_AS_2NDPASS)
static kal_bool idp_g2d_busy_flag;
static G2D_HANDLE_STRUCT *G2D_CAL_handle;
#endif

#if (defined(IDP_USE_G2D_AS_2NDPASS) || defined(IDP_SELF_2NDPASS))
static kal_uint32 postproc_output_buffer_idx = 0;
#endif

#if defined(IDP_SELF_2NDPASS)
enum idp_cp_2pass_state_enum
{
    IDP_CP_2PASS_STATE_NULL,
    IDP_CP_2PASS_STATE_LCD,
    IDP_CP_2PASS_STATE_PP
};
typedef enum idp_cp_2pass_state_enum idp_cp_2pass_state_t;

static idp_cp_2pass_state_t cp_2pass_state = IDP_CP_2PASS_STATE_NULL;
#endif

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
static kal_bool idp_cp_width_over_limit = KAL_FALSE;
static kal_bool idp_cp_crop_setting_in_use = KAL_FALSE;
static kal_uint32 idp_cp_pass_num = 1;
static kal_uint32 idp_cp_crop_width = 0;
static kal_uint32 idp_cp_crop_height = 0;
#endif

/****************************************************************************
 * local function bodies -- LISR
 ****************************************************************************/
static void rotdma0_config_lcd_buffer_addr(kal_uint32 buffer)
{
#if defined(MT6252H) || defined(MT6252)
    if (_idp_scen_crz_width_over_limit == KAL_TRUE)
    {
        REG_IMGDMA_ROT_DMA_Y_DST_STR_ADDR = buffer + (_idp_scen_crz_over_limit_width-320)*crz_struct.tar_height;
    }
    else
#endif
    {
        REG_IMGDMA_ROT_DMA_Y_DST_STR_ADDR = buffer;
    }
    rotdma0_struct.y_dest_start_addr = REG_IMGDMA_ROT_DMA_Y_DST_STR_ADDR;

#if defined(__CAMERA_GYRO_SUPPORT__)
    if (rotdma0_struct.yuv_color_fmt == MM_IMAGE_COLOR_FORMAT_YUV420)
    {
        kal_uint32 size = crz_struct.tar_width * crz_struct.tar_height;

        rotdma0_struct.u_dest_start_addr = rotdma0_struct.y_dest_start_addr + size;
        rotdma0_struct.v_dest_start_addr = rotdma0_struct.u_dest_start_addr + (size>>2);

        REG_IMGDMA_ROT_DMA_U_DST_STR_ADDR = rotdma0_struct.u_dest_start_addr;
        REG_IMGDMA_ROT_DMA_V_DST_STR_ADDR = rotdma0_struct.v_dest_start_addr;
    }
#endif
}

static void
_idp_scen_cp__lisr_crz__frame_start(void *user_data)
{
#if defined(__MTK_TARGET__)

    idp_add_traced_lisr(IDP_TRACED_API_camera_preview__________CRZ_START_LISR);

#if defined(__CAMERA_MATV_ESD_RECOVERY_SUPPORT__)
    resz_esd_check_flag = KAL_TRUE;
#endif

#if defined(IDP_HISR_SUPPORT)
    idp_hisr_activate(idp_camera_preview_frame_start_hisr_handle);
#endif

#endif
}

static void 
_idp_scen_cp_lcd_change_buffer(void)
{
#if defined(MDP_SUPPORT_CP_TRIPLE_DISPLAY_BUFFER)
    if (KAL_TRUE == _idp_scen_triple_display_buffer_enable)
    {
        if (1 < _idp_scen_display_buffer_cnt)
        {
            // Currently 0 for display done. Switch to write buffer 1
            rotdma0_config_lcd_buffer_addr(_idp_scen_display_buffer_addr[1]);
        }
        else
        {
            // Currently 0 for display done, but we only have 1 buffer now
            // Still set write buffer to 0
            rotdma0_config_lcd_buffer_addr(_idp_scen_display_buffer_addr[0]);
        }
    }
    else
#endif
    {
        if (0 == config_cp.preview_buffer_idx)  // Currently writing buffer 0 done.
        {
            // Switch to write to buffer 1
            rotdma0_config_lcd_buffer_addr(config_cp.frame_buffer_address1);
        }
        else if (1 == config_cp.preview_buffer_idx)// Currently writing buffer 1 done.
        {
            // Switch to write to buffer 0
            rotdma0_config_lcd_buffer_addr(config_cp.frame_buffer_address);
        }
    }
}

#if defined(IDP_SELF_2NDPASS)
static void 
_idp_scen_cp_pp_change_buffer(void)
{
    kal_uint32 size = crz_struct.tar_width * crz_struct.tar_height;

    rotdma0_struct.y_dest_start_addr = config_cp.postproc_image_buffer_address[postproc_output_buffer_idx];
    rotdma0_struct.u_dest_start_addr = rotdma0_struct.y_dest_start_addr + size;
    rotdma0_struct.v_dest_start_addr = rotdma0_struct.u_dest_start_addr + (size>>2);
}

static void 
_idp_scen_cp_pp_change_state(void)
{
    // Pass2 change format and output size only
    if (cp_2pass_state == IDP_CP_2PASS_STATE_LCD)
    {
        // Change ROTDMA dest address, color format and rotate angle
        _idp_scen_cp_pp_change_buffer();
        rotdma0_struct.yuv_color_fmt = config_cp.postproc_image_color_format;
        rotdma0_struct.rot_angle = IMG_ROT_ANGLE_0;

        cp_2pass_state = IDP_CP_2PASS_STATE_PP;
    }
    else if (cp_2pass_state == IDP_CP_2PASS_STATE_PP)
    {
        // Change ROTDMA dest address, color format and rotate angle
        _idp_scen_cp_lcd_change_buffer();
        rotdma0_struct.yuv_color_fmt = MM_IMAGE_COLOR_FORMAT_PACKET_UYVY422;
        rotdma0_struct.rot_angle = config_cp.rot_angle;

        cp_2pass_state = IDP_CP_2PASS_STATE_LCD;
    }

    // Change rotdma src width/height
    rotdma0_struct.src_width = crz_struct.tar_width;
    rotdma0_struct.src_height = crz_struct.tar_height;

    idp_imgdma_rotdma0_config_partial(&rotdma0_struct);
}
#endif

static void
_idp_scen_cp__lisr_rotdma0__frame_done(void *user_data)
{
    idp_add_traced_lisr(IDP_TRACED_API_camera_preview__________ROTDMA0_DONE_LISR);

#if defined(__ATV_SUPPORT__)
    idp_camera_preview_pxd = KAL_FALSE;
#endif

#if defined(IDP_SELF_2NDPASS)
    if (KAL_TRUE == config_cp.mav_capture_enable)
    {
        _idp_scen_cp_pp_change_state();
    }
#endif

    // MATV no need check busy
#if defined(__ATV_SUPPORT__)
    if (KAL_TRUE != config_cp.trigger_display_delay_to_frame_start_enable)
#endif
    {
        // No switch and trigger LCD
        if ((WFC_TRUE == wfcMMCheckBusy()) && (config_cp.dbuf_tearing_free_enable == KAL_TRUE))
        {
          #if defined(MDP_SUPPORT_CP_TRIPLE_DISPLAY_BUFFER)
            if (KAL_FALSE == _idp_scen_triple_display_buffer_enable)
          #endif
          #if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
            if (KAL_FALSE == idp_cp_width_over_limit)
          #endif
            {
                rotdma0_warm_reset();
                IMGDMA_ROTDMA_START();

                return;
            }
        }
    }

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    if (KAL_FALSE == idp_cp_width_over_limit)
#endif
    {
    rotdma0_warm_reset();

#if defined(IDP_SELF_2NDPASS)
    if (KAL_FALSE == config_cp.mav_capture_enable)
#endif
    {
        _idp_scen_cp_lcd_change_buffer();
    }

    IMGDMA_ROTDMA_START();
    }
#if defined(IDP_HISR_SUPPORT)
    if (KAL_TRUE == config_cp.enable_trigger_lcd)
    {
        idp_hisr_activate(idp_camera_preview_display_frame_done_hisr_handle);
    }
#endif
}

static void
_idp_scen_cp__lisr_crz__pixel_dropped(void *user_data)
{
    idp_add_traced_lisr(IDP_TRACED_API_camera_preview________________CRZ_PIXEL_DROPPED_LISR);

#if defined(__ATV_SUPPORT__)
    idp_camera_preview_pxd = KAL_TRUE;
#endif
}

static void
_idp_scen_cp__lisr_crz__lock_drop_frame(void *user_data)
{
    idp_add_traced_lisr(IDP_TRACED_API_camera_preview________________CRZ_LOCK_DROP_FRAME_LISR);
}

/****************************************************************************
 * local function bodies -- HISR
 ****************************************************************************/
#if defined(IDP_HISR_SUPPORT)
static void idp_camera_preview_update_lcd_buffer(void)
{
    CAL_BUFFER_CB_STRUCT rCPFrmDoneCBArg;
    CAL_CALLBACK_ID_ENUM eCBID = IDPCAL_CBID_CAM_PREVIEW_DISPLAY_FRM_DONE;

#if defined(MDP_SUPPORT_CP_TRIPLE_DISPLAY_BUFFER)
    if (KAL_TRUE == _idp_scen_triple_display_buffer_enable)
    {
        // get buffer for display, buffer 0 always for display
        rCPFrmDoneCBArg.ImageBuffAddr = _idp_scen_display_buffer_addr[0];

        // enable release write buffer and disable acquire next buffer process for triple display buffer
        rCPFrmDoneCBArg.EnableAcquireWriteBuffer= KAL_FALSE;
    }
    else
#endif
    {
        // Currently writing buffer 0 done, change index to 1
        if (0 == config_cp.preview_buffer_idx)
        {
            rCPFrmDoneCBArg.ImageBuffAddr = config_cp.frame_buffer_address;
            config_cp.preview_buffer_idx = 1;
        }
        else if (1 == config_cp.preview_buffer_idx)  // Currently writing buffer 0 done, change index to 0
        {
            rCPFrmDoneCBArg.ImageBuffAddr = config_cp.frame_buffer_address1;
            config_cp.preview_buffer_idx = 0;
        }

        // enable release and acquire write buffer process for double display buffer
        rCPFrmDoneCBArg.EnableAcquireWriteBuffer= KAL_TRUE;
    }

    rCPFrmDoneCBArg.EnableReleaseWriteBuffer= KAL_TRUE;

    // Trigger delay one frame and this is first frame of frame start 
#if defined(__ATV_SUPPORT__)
    if ((KAL_TRUE == config_cp.trigger_display_delay_to_frame_start_enable) && (completed_display_buffer_count < 2))
    {
        completed_display_buffer_count++;
        return; // First 2 frame no trigger display
    }
#endif

    rCPFrmDoneCBArg.ImageBuffFormat = (MM_IMAGE_FORMAT_ENUM) MM_IMAGE_COLOR_FORMAT_PACKET_UYVY422;
    rCPFrmDoneCBArg.ImageRotateAngle = (MM_IMAGE_ROTATE_ENUM) config_cp.rot_angle;
    rCPFrmDoneCBArg.ImageBuffSize = crz_struct.tar_width * crz_struct.tar_height * 2;

#if defined(__CAMERA_GYRO_SUPPORT__)
    if (rotdma0_struct.yuv_color_fmt == MM_IMAGE_COLOR_FORMAT_YUV420)
    {
        rCPFrmDoneCBArg.ImageBuffFormat = (MM_IMAGE_FORMAT_ENUM) MM_IMAGE_COLOR_FORMAT_YUV420;
        rCPFrmDoneCBArg.ImageBuffSize = (crz_struct.tar_width * crz_struct.tar_height * 3) >> 1;
    }
#endif

#if defined(MDP_SUPPORT_CP_TRIPLE_DISPLAY_BUFFER)
    if (KAL_TRUE == _idp_scen_triple_display_buffer_enable)
    {
        // release current display buffer if MDP holds more than 1 display buffer and 
        // release function is success
        // if we only hold 1 frame, no trigger display and no return buffer
        if (1 < _idp_scen_display_buffer_cnt)
        {
            if (pfIDP_CAL_CB(eCBID, &rCPFrmDoneCBArg, sizeof(CAL_BUFFER_CB_STRUCT)) == MM_ERROR_NONE)
            {
                // return current display buffer 0
                // and set next frame done buffer 0 is next frame current display buffer
                _idp_scen_display_buffer_addr[0] = _idp_scen_display_buffer_addr[1];
                _idp_scen_display_buffer_addr[1] = _idp_scen_display_buffer_addr[2];
                _idp_scen_display_buffer_cnt--;
            }
        }
    }
    else
#endif
    {
        // release and get new buffer
        pfIDP_CAL_CB(eCBID, &rCPFrmDoneCBArg, sizeof(CAL_BUFFER_CB_STRUCT));
    }
}

#if (defined(IDP_USE_G2D_AS_2NDPASS) || defined(IDP_SELF_2NDPASS))
static void idp_camera_preview_update_pp_buffer(void)
{
    CAL_BUFFER_CB_STRUCT rCPFrmDoneCBArg;
    CAL_CALLBACK_ID_ENUM eCBID = IDPCAL_CBID_CAM_PREVIEW_POSTPROC_FRM_DONE;

    kal_uint32 size = config_cp.postproc_image_width * config_cp.postproc_image_height;

    rCPFrmDoneCBArg.ImageBuffYAddr = config_cp.postproc_image_buffer_address[postproc_output_buffer_idx];
    rCPFrmDoneCBArg.ImageBuffUAddr = rCPFrmDoneCBArg.ImageBuffYAddr + size;
    rCPFrmDoneCBArg.ImageBuffVAddr = rCPFrmDoneCBArg.ImageBuffUAddr + (size>>2);

    postproc_output_buffer_idx = (postproc_output_buffer_idx+1) % 3;

    rCPFrmDoneCBArg.ImageBuffAddr = rCPFrmDoneCBArg.ImageBuffYAddr;

    // fill in image color format and rotate angle
    rCPFrmDoneCBArg.ImageBuffFormat = (MM_IMAGE_FORMAT_ENUM) config_cp.postproc_image_color_format;
    rCPFrmDoneCBArg.ImageRotateAngle = (MM_IMAGE_ROTATE_ENUM) IMG_ROT_ANGLE_0;

    // fill in image buffer size
    // MAV supports YUV420 postproc format
    if (MM_IMAGE_COLOR_FORMAT_YUV420 == config_cp.postproc_image_color_format)
    {
        rCPFrmDoneCBArg.ImageBuffYSize = size;
        rCPFrmDoneCBArg.ImageBuffUSize = (size>>2);
        rCPFrmDoneCBArg.ImageBuffVSize = rCPFrmDoneCBArg.ImageBuffUSize;          
        rCPFrmDoneCBArg.ImageBuffSize = rCPFrmDoneCBArg.ImageBuffYSize + rCPFrmDoneCBArg.ImageBuffUSize +
                                        rCPFrmDoneCBArg.ImageBuffVSize;
    }
    else if (MM_IMAGE_COLOR_FORMAT_RGB565 == config_cp.postproc_image_color_format)
    {       
        rCPFrmDoneCBArg.ImageBuffSize = (size<<1);
    }

    pfIDP_CAL_CB(eCBID, &rCPFrmDoneCBArg, sizeof(CAL_BUFFER_CB_STRUCT));
}
#endif

#if defined(IDP_USE_G2D_AS_2NDPASS)
static void idp_cal_g2d_cb(void *user_data)
{
    g2dReleaseHandle((G2D_HANDLE_STRUCT *)G2D_CAL_handle);

    idp_g2d_busy_flag = KAL_FALSE;
    idp_camera_preview_update_pp_buffer();
}

typedef struct
{
    float m[9];
} g2d_lt_matrix_struct;


static void idp_g2d_2pass_config()
{
    CAL_G2D_REQ_STRUCT *local_para_ptr;
    CAL_G2D_REQ_STRUCT ilm_param;
    G2D_STATUS_ENUM g2d_status = G2D_STATUS_OK;
    kal_uint8 *bufferAddr[3];
    kal_uint32 bufferSize[3];
    float s1,s2;
    g2d_lt_matrix_struct lt_matrix;

    ////////////////////////////////////////////////////////////////////////////////
    ilm_param.mem_in_color_fmt1 = MM_IMAGE_COLOR_FORMAT_PACKET_UYVY422;

#if defined(MDP_SUPPORT_CP_TRIPLE_DISPLAY_BUFFER)
    if (KAL_TRUE == _idp_scen_triple_display_buffer_enable)
    {
        // get buffer for display, buffer 0 always for display
        ilm_param.src_buff_y_addr = _idp_scen_display_buffer_addr[0];
    }
    else
#endif
    {
        // Currently writing buffer 0 done, change index to 1
        if (0 == config_cp.preview_buffer_idx)
        {
            ilm_param.src_buff_y_addr = config_cp.frame_buffer_address;
        }
        else if (1 == config_cp.preview_buffer_idx)  // Currently writing buffer 0 done, change index to 0
        {
            ilm_param.src_buff_y_addr = config_cp.frame_buffer_address1;
        }
    }

    if (config_cp.rot_angle == IMG_MIRROR_ROT_ANGLE_90)
    {
        ilm_param.src_width = config_cp.preview_height;
        ilm_param.src_height = config_cp.preview_width;
    }
    else
    {
        ilm_param.src_width = config_cp.preview_width;
        ilm_param.src_height = config_cp.preview_height;
    }

    ilm_param.yuv_color_fmt = MM_IMAGE_COLOR_FORMAT_RGB565;
    ilm_param.y_dest_start_addr = config_cp.postproc_image_buffer_address[postproc_output_buffer_idx];

    ilm_param.tar_width = config_cp.postproc_image_width;
    ilm_param.tar_height = config_cp.postproc_image_height;

    ilm_param.rot_angle = config_cp.rot_angle;
    ilm_param.cb_func = (idp_g2d_cb_func_t) idp_cal_g2d_cb;



    // Ori CAL do 
    local_para_ptr = &ilm_param;

    if (KAL_FALSE == g2d_mutex_check_given_status_only())
    {
        //g2d_mutex_wait_hwg2d_release();
        //HW G2D be used by another task!!!.
        return;
    }

    g2d_status = g2dGetHandle(&G2D_CAL_handle, G2D_CODEC_TYPE_HW, G2D_GET_HANDLE_MODE_DIRECT_RETURN_HANDLE);

    if(g2d_status != G2D_STATUS_OK)
    {
        ASSERT(0);
    }
    
    g2dSetDstBufferSwitchBehavior(G2D_CAL_handle, GFX_CACHE_SWITCH_BEHAVIOR_NOT_SWITCH);
    g2dSetColorReplacement(G2D_CAL_handle, KAL_FALSE, 0, 0, 0, 0, 0, 0, 0, 0);
    g2dSetSrcKey(G2D_CAL_handle, KAL_FALSE, 0, 0, 0, 0);
    g2dSetDithering(G2D_CAL_handle,KAL_FALSE,G2D_DITHERING_MODE_FIXED_PATTERN); 
    g2dSetDstRGBBufferInfo(
          G2D_CAL_handle, 
          (kal_uint8*)local_para_ptr->y_dest_start_addr, 
          (local_para_ptr->tar_width * local_para_ptr->tar_height * 2),
          local_para_ptr->tar_width, 
          local_para_ptr->tar_height, 
          G2D_COLOR_FORMAT_RGB565);   
    g2d_status = g2dSetDstClipWindow(
                     G2D_CAL_handle,
                     KAL_FALSE, 
                     0, 
                     0,  
                     local_para_ptr->tar_width,
                     local_para_ptr->tar_height);
    g2d_status = g2dLTSetDstWindow(G2D_CAL_handle, 0, 0, local_para_ptr->tar_width, local_para_ptr->tar_height);
    if (g2d_status == G2D_STATUS_RANGE_CHECK_FAIL)
    {
        ASSERT(0);
    }
    bufferAddr[0] = (kal_uint8*)local_para_ptr->src_buff_y_addr;
    bufferSize[0] = local_para_ptr->src_width * local_para_ptr->src_height * 2;
    g2dLTSetSrcYUVBufferInfo(
        G2D_CAL_handle,
        bufferAddr,
        bufferSize,
        local_para_ptr->src_width,
        local_para_ptr->src_height,
        G2D_COLOR_FORMAT_UYVY422);

    g2d_status = g2dLTSetSrcWindow(G2D_CAL_handle, 0, 0, local_para_ptr->src_width, local_para_ptr->src_height);
    g2dLTSetSrcAlpha(G2D_CAL_handle, KAL_FALSE, 0);
    g2dLTSetDstAlpha(G2D_CAL_handle, KAL_FALSE, 0);
    g2dLTSetSampleMode(G2D_CAL_handle, G2D_LT_SAMPLE_MODE_BILINEAR_WITH_EDGE_FILTER_DULPLICATE);

    /* get inverse matrix */
    kal_mem_set((void*) &lt_matrix.m[0], 0x0, 9*sizeof(lt_matrix.m[0]));
    if (local_para_ptr->rot_angle == IMG_MIRROR_ROT_ANGLE_90)
    {
        s1 = (float)local_para_ptr->tar_height;
        s2 = (float)local_para_ptr->src_width;
        //lt_matrix.m[1] = -(float)((float)s2/(float)s1);
        //lt_matrix.m[2] = (float)local_para_ptr->src_width-1;
        lt_matrix.m[1] = (float)((float)s2/(float)s1);
        lt_matrix.m[2] = 0;
        s1 = (float)local_para_ptr->tar_width;
        s2 = (float)local_para_ptr->src_height;
        lt_matrix.m[3] = (float)((float)s2/(float)s1);
        lt_matrix.m[5] = 0;
        //lt_matrix.m[3] = -(float)((float)s2/(float)s1);
        //lt_matrix.m[5] = (float)local_para_ptr->src_height-1;
        lt_matrix.m[8] = 1;
    }
    else if (local_para_ptr->rot_angle == IMG_ROT_ANGLE_0)
    {
        s1 = (float)local_para_ptr->tar_width;
        s2 = (float)local_para_ptr->src_width;
        lt_matrix.m[0] = (float)((float)s1/(float)s2);
        s1 = (float)local_para_ptr->tar_height;
        s2 = (float)local_para_ptr->src_height;
        lt_matrix.m[4] = (float)((float)s1/(float)s2);
        lt_matrix.m[8] = 1;
    }
    g2dLTSetMatrix(G2D_CAL_handle, lt_matrix.m);
    g2dLTSetInverseMatrix(G2D_CAL_handle, lt_matrix.m);

    g2dSetCallbackFunction(G2D_CAL_handle, (G2D_CALLBACK_FUNC_PTR)local_para_ptr->cb_func);

    g2d_status = g2dLTStart(G2D_CAL_handle);
    if (((G2D_STATUS_RANGE_CHECK_FAIL == g2d_status) || (G2D_STATUS_HW_ERROR == g2d_status)))
    {
        ASSERT(0);
    }
}
#endif

static kal_bool
_idp_scen_cp__hisr_crz__frame_start(void *user_data)
{
    kal_uint32 const savedMask = SaveAndSetIRQMask();
    kal_bool const enable = config_cp.enable_crz_frame_start_intr;
    RestoreIRQMask(savedMask);

    // Triger display in frame start
#if defined(__ATV_SUPPORT__)
    if (KAL_TRUE == config_cp.trigger_display_delay_to_frame_start_enable)
    {
        idp_camera_preview_update_lcd_buffer();
    }
#endif

#if defined(IDP_SELF_2NDPASS)
    // Pass2 change format and output size only
    if (cp_2pass_state == IDP_CP_2PASS_STATE_LCD)
    {
        crz_struct.tar_width = config_cp.postproc_image_width;
        crz_struct.tar_height = config_cp.postproc_image_height;
    }
    else if (cp_2pass_state == IDP_CP_2PASS_STATE_PP)
    {
        // Change CRZ tar width/height to preview
        crz_struct.tar_width = config_cp.preview_width;
        crz_struct.tar_height = config_cp.preview_height;
    }

    idp_resz_crz_config_real(&crz_struct, KAL_TRUE);
#endif

    if (KAL_TRUE == enable)
    {
        pfIDP_CAL_CB(IDPCAL_CBID_CAM_PREVIEW_CRZ_FRM_START, NULL, 0);
    }
    return KAL_TRUE;
}

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
static void idp_cp_config_and_start_next_pass()
{
    // stop current pass
    idp_imgdma_rotdma0_stop_real(&rotdma0_struct);
    idp_resz_crz_stop_real(&crz_struct);

    //config CRZ
    if (0 == idp_cp_pass_num)
    {
        crz_struct.input_src1 = IDP_MODULE_OUTER_ISP;
        crz_struct.fstart1_intr_en = KAL_TRUE;
        crz_struct.auto_restart = KAL_TRUE;
        crz_struct.clip_en = KAL_FALSE;
        // set CRZ to bypass mode because of limitation
        crz_struct.src_width = config_cp.image_src_width;
        crz_struct.src_height = config_cp.image_src_height;
        crz_struct.tar_width = config_cp.crop_width;
        crz_struct.tar_height = config_cp.crop_height;
        crz_struct.org_width = config_cp.crop_width;
        if (KAL_FALSE == idp_cp_crop_setting_in_use)
        {
            idp_cp_crop_width = config_cp.crop_width;
            idp_cp_crop_height = config_cp.crop_height;
            idp_cp_crop_setting_in_use = KAL_TRUE;
        }

        crz_struct.crop_en1 = config_cp.crop_en;
        idp_config_crz_crop(config_cp.image_src_width, config_cp.image_src_height, 
                            config_cp.crop_width, config_cp.crop_height);
        crz_struct.tile_mode = KAL_FALSE;
    }
    else
    {
        crz_struct.input_src1 = IDP_MODULE_IMGDMA_RDMA0;
        crz_struct.fstart1_intr_en = KAL_FALSE;
        crz_struct.auto_restart = KAL_FALSE;
        crz_struct.clip_en = KAL_TRUE;
        crz_struct.org_width = idp_cp_crop_width;

        if (1 == idp_cp_pass_num)
        {
            // tile pass 1 setting
            crz_struct.src_width = config_cp.tile_para.tile_src_size_x[0];
            crz_struct.src_height = idp_cp_crop_height;
            crz_struct.tar_width = config_cp.preview_width >> 1;
            crz_struct.tar_height = config_cp.preview_height;
            crz_struct.src_buff_y_addr = config_cp.cp_tmp_buffer;

            crz_struct.crop_en1 = KAL_TRUE;
            crz_struct.crop_left1 = config_cp.tile_para.tile_bi_sa_src_start_pos[0] - config_cp.tile_para.tile_adj_src_start_pos[0];
            crz_struct.crop_right1 = config_cp.tile_para.tile_bi_sa_src_end_pos[0] - config_cp.tile_para.tile_adj_src_start_pos[0];
            crz_struct.crop_top1 = 0;
            crz_struct.crop_bottom1 = idp_cp_crop_height - 1;

            // Tile Setting
            crz_struct.tile_mode = KAL_TRUE;
            crz_struct.tile_org_frame_width = idp_cp_crop_width;
            crz_struct.tile_org_tar_width = config_cp.preview_width;
            crz_struct.tile_start_pos_x = config_cp.tile_para.tile_start_pos_x[0];
            crz_struct.tile_start_pos_y = config_cp.tile_para.tile_start_pos_y[0];
            crz_struct.tile_trunc_err_comp_x = config_cp.tile_para.tile_trunc_err_comp_x[0];
            crz_struct.tile_trunc_err_comp_y = config_cp.tile_para.tile_trunc_err_comp_y[0];
            crz_struct.tile_resid_x = config_cp.tile_para.tile_init_resid_x[0];
            crz_struct.tile_resid_y = config_cp.tile_para.tile_init_resid_y[0];
        }
        else
        {
            // tile pass 2 setting
            crz_struct.src_width = config_cp.tile_para.tile_src_size_x[1];
            crz_struct.src_height = idp_cp_crop_height;
            crz_struct.tar_width = config_cp.preview_width >> 1;
            crz_struct.tar_height = config_cp.preview_height;
            crz_struct.src_buff_y_addr += ((idp_cp_crop_width - crz_struct.src_width) << 1);

            crz_struct.crop_en1 = KAL_TRUE;
            crz_struct.crop_left1 = config_cp.tile_para.tile_bi_sa_src_start_pos[1] - config_cp.tile_para.tile_adj_src_start_pos[1];
            crz_struct.crop_right1 = config_cp.tile_para.tile_bi_sa_src_end_pos[1] - config_cp.tile_para.tile_adj_src_start_pos[1];
            crz_struct.crop_top1 = 0;
            crz_struct.crop_bottom1 = idp_cp_crop_height - 1;

            // Tile Setting
            crz_struct.tile_mode = KAL_TRUE;
            crz_struct.tile_org_frame_width = idp_cp_crop_width;
            crz_struct.tile_org_tar_width = config_cp.preview_width;
            crz_struct.tile_start_pos_x = config_cp.tile_para.tile_start_pos_x[1];
            crz_struct.tile_start_pos_y = config_cp.tile_para.tile_start_pos_y[1];
            crz_struct.tile_trunc_err_comp_x = config_cp.tile_para.tile_trunc_err_comp_x[1];
            crz_struct.tile_trunc_err_comp_y = config_cp.tile_para.tile_trunc_err_comp_y[1];
            crz_struct.tile_resid_x = config_cp.tile_para.tile_init_resid_x[1];
            crz_struct.tile_resid_y = config_cp.tile_para.tile_init_resid_y[1];

            idp_cp_crop_setting_in_use = KAL_FALSE;
        }
    }
    idp_resz_crz_config_real(&crz_struct, KAL_TRUE);

    //config ROTDMA
    rotdma0_struct.src_width = crz_struct.tar_width;
    rotdma0_struct.src_height = crz_struct.tar_height;

    if (0 == idp_cp_pass_num)
    {
        rotdma0_struct.rot_angle = IMG_ROT_ANGLE_0;
        rotdma0_struct.y_dest_start_addr = config_cp.cp_tmp_buffer;
        rotdma0_struct.pitch_enable = KAL_FALSE;
    }
    else
    {
        rotdma0_struct.rot_angle = config_cp.rot_angle;
        rotdma0_struct.pitch_enable = KAL_TRUE;
        if (1 == idp_cp_pass_num)
        {
            if (0 == config_cp.preview_buffer_idx)  // Currently writing buffer 0 done.
            {
                rotdma0_struct.y_dest_start_addr = config_cp.frame_buffer_address;
            }
            else
            {
                rotdma0_struct.y_dest_start_addr = config_cp.frame_buffer_address1;
            }
        }
        else
        {
            if (IMG_ROT_ANGLE_0 == rotdma0_struct.rot_angle)
                rotdma0_struct.y_dest_start_addr += config_cp.preview_width;
            else
                rotdma0_struct.y_dest_start_addr += config_cp.preview_height * config_cp.preview_width;
        }
    }

    rotdma0_warm_reset();
    idp_imgdma_rotdma0_config_partial(&rotdma0_struct);

    //start pass
    idp_imgdma_rotdma0_start_real(&rotdma0_struct);
    idp_resz_crz_start_real(&crz_struct);
}
#endif

static kal_bool
_idp_scen_cp__hisr_rotdma0__frame_done(void *user_data)
{
#if defined(IDP_SELF_2NDPASS)
    // Check enable mav, else update lcd only
    if (KAL_TRUE == config_cp.mav_capture_enable)
    {
        // pp done update lcd and pp buffer together
        if (cp_2pass_state == IDP_CP_2PASS_STATE_LCD)
        {
            idp_camera_preview_update_pp_buffer();
            goto update_lcd;
        }
        else // do nothing.
        {
            return KAL_TRUE;
        }
    }
    else
    {
        goto update_lcd;
    }
update_lcd:
#endif

#if defined(IDP_USE_G2D_AS_2NDPASS)
    if ((KAL_TRUE == config_cp.postproc_image_enable) && (KAL_FALSE == config_cp.mav_capture_enable))
    {
        // Trigger g2d do 2nd pass
        idp_g2d_2pass_config();
    }
#endif

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    // do tile calculation when 1st pass is done
    if (KAL_TRUE == idp_cp_width_over_limit)
    {
        if (1 == idp_cp_pass_num)
        {
            if (idp_cp_crop_width * idp_cp_crop_height <= config_cp.preview_width * config_cp.preview_height)
            {
                idp_bi_tile_calc(idp_cp_crop_width, idp_cp_crop_height,
                                 config_cp.preview_width, config_cp.preview_height,
                                 &config_cp.tile_para);

                crz_struct.sa_en_x = KAL_FALSE;
                crz_struct.sa_en_y = KAL_FALSE;
            }
            else
            {
                idp_sa_tile_calc(idp_cp_crop_width, idp_cp_crop_height,
                                 config_cp.preview_width, config_cp.preview_height,
                                 &config_cp.tile_para);            

                crz_struct.sa_en_x = KAL_TRUE;
                crz_struct.sa_en_y = KAL_TRUE;
            }
        }
    
        idp_cp_config_and_start_next_pass();
    }
#endif

    // Triger display in frame done
#if defined(__ATV_SUPPORT__)
    if (KAL_TRUE != config_cp.trigger_display_delay_to_frame_start_enable)
#endif
#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    // change display buffer only at tile pass 2
    if ((KAL_FALSE == idp_cp_width_over_limit) ||
        (KAL_TRUE == idp_cp_width_over_limit && 
         0 == idp_cp_pass_num &&
         WFC_FALSE == wfcMMCheckBusy()))
#endif
    {
        idp_camera_preview_update_lcd_buffer();
    }

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    if (KAL_TRUE == idp_cp_width_over_limit)
    {
    if (2 == idp_cp_pass_num)
        idp_cp_pass_num = 0;
    else
        //update pass flags
        idp_cp_pass_num++;
    }
#endif

    return KAL_TRUE;
}
#endif  // #if defined(IDP_HISR_SUPPORT)

/****************************************************************************
 * local function bodies -- HW config functions
 ****************************************************************************/
#if defined(__MTK_TARGET__)
static kal_bool
_idp_scen_cp__config_crz(
    idp_owner_t * const owner,
    kal_bool const config_to_hardware)
{
    kal_bool result;

    crz_struct.input_src1 = IDP_MODULE_OUTER_ISP;

    crz_struct.fstart1_intr_en = KAL_TRUE;
    crz_struct.fstart1_intr_cb = _idp_scen_cp__lisr_crz__frame_start;

    crz_struct.pixel_dropped_intr_en = KAL_TRUE;
    crz_struct.pixel_dropped_intr_cb = _idp_scen_cp__lisr_crz__pixel_dropped;

    crz_struct.lock_drop_frame_intr_en = KAL_TRUE;
    crz_struct.lock_drop_frame_intr_cb = _idp_scen_cp__lisr_crz__lock_drop_frame;

    crz_struct.src_width = config_cp.image_src_width;
    crz_struct.src_height = config_cp.image_src_height;

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    if (KAL_TRUE == idp_cp_width_over_limit)
    {
        // set CRZ to bypass mode because of limitation
        crz_struct.tar_width = config_cp.crop_width;
        crz_struct.tar_height = config_cp.crop_height;
        crz_struct.mem_in_color_fmt1 = MM_IMAGE_COLOR_FORMAT_PACKET_UYVY422;
        crz_struct.src_buff_y_addr = config_cp.cp_tmp_buffer;
        crz_struct.org_width = config_cp.crop_width;
        if (KAL_FALSE == idp_cp_crop_setting_in_use)
        {
            idp_cp_crop_width = config_cp.crop_width;
            idp_cp_crop_height = config_cp.crop_height;            
            idp_cp_crop_setting_in_use = KAL_TRUE;
        }
    }
    else
#endif
    {
    crz_struct.tar_width = config_cp.preview_width;
    crz_struct.tar_height = config_cp.preview_height;
    }

    crz_struct.crop_en1 = config_cp.crop_en;
    crz_struct.auto_restart = KAL_TRUE;
    crz_struct.hw_frame_sync = KAL_TRUE;

#if defined(IDP_SELF_2NDPASS)
    if (KAL_TRUE == config_cp.mav_capture_enable)
    {
        if (cp_2pass_state == IDP_CP_2PASS_STATE_NULL) // config end set state to LCD for pass1
        {
            cp_2pass_state = IDP_CP_2PASS_STATE_LCD;
        }
        else if (cp_2pass_state == IDP_CP_2PASS_STATE_LCD) // config has end and running, next is pp path
        {
            crz_struct.tar_width = config_cp.postproc_image_width;
            crz_struct.tar_height = config_cp.postproc_image_height;
        }
    }
#endif

    //if (KAL_TRUE == crz_struct.crop_en1) // Always TRUE, code shrink
    {
        idp_config_crz_crop(config_cp.image_src_width, config_cp.image_src_height, config_cp.crop_width, config_cp.crop_height);
    }

#if defined(MT6252H) || defined(MT6252)
    if (crz_struct.tar_width >= 320)
    {
        _idp_scen_crz_over_limit_width = crz_struct.tar_width;
        crz_struct.tar_width = 320;
        _idp_scen_crz_width_over_limit = KAL_TRUE;

        // change crop for same width/hieght rate.
        crz_struct.crop_left1 = (config_cp.image_src_width - config_cp.crop_width*320/_idp_scen_crz_over_limit_width) >> 1;
        crz_struct.crop_right1 = crz_struct.crop_left1 + config_cp.crop_width*320/_idp_scen_crz_over_limit_width - 1;
    }
#endif

    result = idp_resz_crz_config(owner->crz_key, &crz_struct, config_to_hardware);
    if (KAL_FALSE == result)
    {
        return KAL_FALSE;
    }

    return KAL_TRUE;
}


static kal_bool
_idp_scen_cp__config_rotdma0(
    idp_owner_t * const owner,
    kal_bool const config_to_hardware)
{
    kal_bool result;

    if (KAL_TRUE == owner->have_config_imgdma_irt0)
    {
        return KAL_TRUE;
    }

#if defined(__CAMERA_GYRO_SUPPORT__)
    rotdma0_struct.yuv_color_fmt = config_cp.preview_image_color_format;
#else
    rotdma0_struct.yuv_color_fmt = MM_IMAGE_COLOR_FORMAT_PACKET_UYVY422;
#endif

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    if (KAL_TRUE == idp_cp_width_over_limit)
    {
        if (NULL == config_cp.cp_tmp_buffer)
        {
            return KAL_FALSE;
        }
        else
        {
            rotdma0_struct.y_dest_start_addr = config_cp.cp_tmp_buffer;
            if (IMG_ROT_ANGLE_0 == config_cp.rot_angle)
                rotdma0_struct.pitch_bytes = (config_cp.preview_width << 1); //Final width
            else
                rotdma0_struct.pitch_bytes = (config_cp.preview_height << 1); //Final width
        }
    }
    else
#endif
    rotdma0_struct.y_dest_start_addr = config_cp.frame_buffer_address;

#if defined(__CAMERA_GYRO_SUPPORT__)
    if (rotdma0_struct.yuv_color_fmt == MM_IMAGE_COLOR_FORMAT_YUV420)
    {
        kal_uint32 size = crz_struct.tar_width * crz_struct.tar_height;
        rotdma0_struct.u_dest_start_addr = rotdma0_struct.y_dest_start_addr + size;
        rotdma0_struct.v_dest_start_addr = rotdma0_struct.u_dest_start_addr + (size>>2);
    }
#endif

    rotdma0_struct.src_width = crz_struct.tar_width;
    rotdma0_struct.src_height = crz_struct.tar_height;

    rotdma0_struct.fend_intr_en = KAL_TRUE;
    rotdma0_struct.fend_intr_cb = _idp_scen_cp__lisr_rotdma0__frame_done;

    rotdma0_struct.hw_frame_sync = KAL_TRUE;

    rotdma0_struct.protect_en = KAL_TRUE;   //According to spec. From Camera, enable protect
    rotdma0_struct.bus_control_threshold = ROTDMA_BUS_CONTROL_THRESHOLD;

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    if (KAL_TRUE == idp_cp_width_over_limit)
        rotdma0_struct.rot_angle = IMG_ROT_ANGLE_0;
    else
#endif
    rotdma0_struct.rot_angle = config_cp.rot_angle;

#if defined(MT6252H) || defined(MT6252)
    // reset start address, clear write buffer to black
    if (_idp_scen_crz_width_over_limit == KAL_TRUE)
    {
        kal_uint32 i, size;
        kal_uint32* pu4Addr1= (kal_uint32*) config_cp.frame_buffer_address;
        kal_uint32* pu4Addr2= (kal_uint32*) config_cp.frame_buffer_address1;

        rotdma0_struct.y_dest_start_addr += (_idp_scen_crz_over_limit_width-320)*crz_struct.tar_height;

        size = (_idp_scen_crz_over_limit_width*crz_struct.tar_height) >> 1;
        for (i = 0; i <size; i++)
        {
            *(pu4Addr1 + i) = 0x00800080;
            *(pu4Addr2 + i) = 0x00800080;
        }
    }
#endif

    result = idp_imgdma_rotdma0_config(owner->irt0_key, &rotdma0_struct, config_to_hardware);
    if (KAL_FALSE == result)
    {
        return KAL_FALSE;
    }

    owner->have_config_imgdma_irt0 = KAL_TRUE;

#if defined(MDP_SUPPORT_CP_TRIPLE_DISPLAY_BUFFER)
    if (KAL_TRUE == _idp_scen_triple_display_buffer_enable)
    {
        //record the 3 display buffers
        _idp_scen_display_buffer_addr[0] = config_cp.frame_buffer_address;
        _idp_scen_display_buffer_addr[1] = config_cp.frame_buffer_address1;
        _idp_scen_display_buffer_addr[2] = config_cp.frame_buffer_address2;
        _idp_scen_display_buffer_cnt = 3;
    }
#endif

    return KAL_TRUE;
}

#endif  // #if defined(__MTK_TARGET__)

/****************************************************************************
 * local function bodies -- IDP scenario hook function bodies
 ****************************************************************************/
static void
idp_camera_preview_init_config_hook(
    idp_owner_t * const owner)
{
#if defined(__MTK_TARGET__)

#if defined(IDP_HISR_SUPPORT)
    if (idp_camera_preview_frame_start_hisr_handle == NULL)
    {
        idp_camera_preview_frame_start_hisr_handle =
            idp_hisr_register(owner->key,
                              IDP_HISR_CB_TYPE_RESZ,
                              _idp_scen_cp__hisr_crz__frame_start,
                              NULL);
    }

    if (idp_camera_preview_display_frame_done_hisr_handle == NULL)
    {
        idp_camera_preview_display_frame_done_hisr_handle =
            idp_hisr_register(owner->key,
                              IDP_HISR_CB_TYPE_IMGDMA,
                              _idp_scen_cp__hisr_rotdma0__frame_done,
                              NULL);
    }
#endif

#if defined(MDP_SUPPORT_CP_TRIPLE_DISPLAY_BUFFER)
    _idp_scen_triple_display_buffer_enable = KAL_FALSE;
#endif

#if defined(__ATV_SUPPORT__)
    completed_display_buffer_count = 0;
#endif

    kal_mem_set(&config_cp, 0, sizeof(config_cp));

#if defined(IDP_SELF_2NDPASS)
    cp_2pass_state = IDP_CP_2PASS_STATE_NULL;
#endif
#if (defined(IDP_USE_G2D_AS_2NDPASS) || defined(IDP_SELF_2NDPASS))
    postproc_output_buffer_idx = 0;
#endif

#if defined(IDP_MM_COLOR_SUPPORT)
    colorWrapperOpen(&mm_color_handle, COLOR_SCENARIO_CAMERA_PREVIEW);
    colorWrapperEnable(mm_color_handle, 0);
#endif

#if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
    idp_cp_crop_setting_in_use = KAL_FALSE;
    idp_cp_pass_num = 1;
#endif

#endif  // #if defined(__MTK_TARGET__)
}

kal_bool
idp_camera_preview_close_hook(
    idp_owner_t * const owner)
{
#if defined(__MTK_TARGET__)

    idp_resz_crz_close(owner->crz_key, &crz_struct);
    idp_imgdma_rotdma0_close(owner->irt0_key, &rotdma0_struct);

#if defined(IDP_HISR_SUPPORT)
    idp_hisr_clear_all(owner->key, IDP_HISR_CB_TYPE_RESZ);
    idp_hisr_clear_all(owner->key, IDP_HISR_CB_TYPE_IMGDMA);

    idp_camera_preview_frame_start_hisr_handle = NULL;
    idp_camera_preview_display_frame_done_hisr_handle = NULL;
#endif

#if defined(IDP_MM_COLOR_SUPPORT)
    colorWrapperDisable(mm_color_handle);
    colorWrapperClose(mm_color_handle);
#endif

#endif  // #if defined(__MTK_TARGET__)

    return KAL_TRUE;
}

kal_bool
idp_camera_preview_read_user_config_fast_hook(
    idp_owner_t * const owner,
    kal_uint32 const para_type,
    kal_uint32 const para_value)
{
#if defined(__MTK_TARGET__)

    switch (para_type)
    {
      #if defined(IDP_HISR_SUPPORT)
        case IDP_CAMERA_PREVIEW_FRAME_START_HISR_CB_ENABLE:
            {
                // If this is enabled, CRZ LISR will activate a registered HISR to invoke camera DZ...
                kal_uint32 const savedMask = SaveAndSetIRQMask();
                config_cp.enable_crz_frame_start_intr = (kal_bool) para_value;
                RestoreIRQMask(savedMask);
            }
            break;
      #endif
        default:
            return KAL_FALSE;
            //break;
    }
#endif

    return KAL_TRUE;
}

kal_bool
idp_camera_preview_read_user_config_hook(
    idp_owner_t * const owner,
    va_list ap)
{
    kal_uint32 para_type;
    kal_bool finish = KAL_FALSE;

    while (KAL_FALSE == finish)
    {
        para_type = va_arg(ap, kal_uint32);

        switch (para_type)
        {
            case 0:
                finish = KAL_TRUE;
                break;

            case IDP_CAMERA_PREVIEW_IMAGE_SRC_WIDTH:
                config_cp.image_src_width = va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_IMAGE_SRC_HEIGHT:
                config_cp.image_src_height = va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_PREVIEW_WIDTH:
                config_cp.preview_width = va_arg(ap, kal_uint32);
            #if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
                if (480 < config_cp.preview_width)
                {
                    idp_cp_width_over_limit = KAL_TRUE;
                }
                else
                {
                    idp_cp_width_over_limit = KAL_FALSE;
                }
            #endif
                break;

            case IDP_CAMERA_PREVIEW_PREVIEW_HEIGHT:
                config_cp.preview_height = va_arg(ap, kal_uint32);
                break;

        #if defined(__CAMERA_GYRO_SUPPORT__)
            case IDP_CAMERA_PREVIEW_PREVIEW_IMAGE_COLOR_FORMAT:
                config_cp.preview_image_color_format = va_arg(ap, kal_uint32);
                break;
        #endif

            case IDP_CAMERA_PREVIEW_FRAME_BUFFER_ADDRESS:
                config_cp.frame_buffer_address = va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_FRAME_BUFFER_ADDRESS1:
                config_cp.frame_buffer_address1 = va_arg(ap, kal_uint32);
                break;

        #if defined(MDP_SUPPORT_CP_TRIPLE_DISPLAY_BUFFER)
            case IDP_CAMERA_PREVIEW_FRAME_BUFFER_ADDRESS2:
                config_cp.frame_buffer_address2 = va_arg(ap, kal_uint32);

                if (NULL != config_cp.frame_buffer_address2)
                {
                    _idp_scen_triple_display_buffer_enable = KAL_TRUE;
                }
                else
                {
                    _idp_scen_triple_display_buffer_enable = KAL_FALSE;
                }
                break;
        #endif

            case IDP_CAMERA_PREVIEW_DISPLAY_ROT_ANGLE:
                config_cp.rot_angle = (img_rot_angle_enum_t) va_arg(ap, kal_uint32);

                if (!((IMG_ROT_ANGLE_0 == config_cp.rot_angle) ||
                    (IMG_MIRROR_ROT_ANGLE_90 == config_cp.rot_angle)))
                {
                    return KAL_FALSE;
                }
                break;

            case IDP_CAMERA_PREVIEW_ENABLE_TRIGGER_LCD:
                config_cp.enable_trigger_lcd = (kal_bool) va_arg(ap, kal_uint32);
                break;

        #if defined(IDP_HISR_SUPPORT)
            case IDP_CAMERA_PREVIEW_FRAME_START_HISR_CB_ENABLE:
                {
                    kal_uint32 const savedMask = SaveAndSetIRQMask();
                    config_cp.enable_crz_frame_start_intr = (kal_bool) va_arg(ap, kal_uint32);
                    RestoreIRQMask(savedMask);
                }
                break;
        #endif

            case IDP_CAMERA_PREVIEW_PREVIEW_CROP_ENABLE:
                config_cp.crop_en = (kal_bool) va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_PREVIEW_CROP_WIDTH:
                config_cp.crop_width = va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_PREVIEW_CROP_HEIGHT:
                config_cp.crop_height = va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_ENABLE_DOUBLE_BUFFER_TEARING_FREE:
                config_cp.dbuf_tearing_free_enable = (kal_bool)va_arg(ap, kal_uint32);  
                break;

        #if defined(__ATV_SUPPORT__)
            case IDP_CAMERA_PREVIEW_ENABLE_TRIGGER_DISPLAY_DELAY_TO_FRAME_START:
                config_cp.trigger_display_delay_to_frame_start_enable = (kal_bool) va_arg(ap, kal_uint32);
              #if defined(MT6252H) || defined(MT6252)
                // MT6252 with 5193 MATV will drop frame when LCD and MDP work at the same time.
                // So, default we not delay, let frame done trigger LCD display
                config_cp.trigger_display_delay_to_frame_start_enable = KAL_FALSE;
              #endif
                break;
        #endif

        #if (defined(IDP_USE_G2D_AS_2NDPASS) || defined(IDP_SELF_2NDPASS))
            case IDP_CAMERA_PREVIEW_POSTPROC_PATH_ENABLE:
                config_cp.postproc_image_enable = (kal_bool)va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_POSTPROC_IMAGE_WIDTH:
                config_cp.postproc_image_width = va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_POSTPROC_IMAGE_HEIGHT:
                config_cp.postproc_image_height = va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_POSTPROC_ROT_ANGLE: // set 0, if preview rotate, need rotate back
                config_cp.postproc_image_rot_angle = (img_rot_angle_enum_t) va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_POSTPROC_BUFFER_ADDRESS_1:
            case IDP_CAMERA_PREVIEW_POSTPROC_BUFFER_ADDRESS_2:
            case IDP_CAMERA_PREVIEW_POSTPROC_BUFFER_ADDRESS_3:
                config_cp.postproc_image_buffer_address[para_type - IDP_CAMERA_PREVIEW_POSTPROC_BUFFER_ADDRESS_1] = va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_POSTPROC_IMAGE_COLOR_FORMAT:
                config_cp.postproc_image_color_format = (MM_IMAGE_COLOR_FORMAT_ENUM) va_arg(ap, kal_uint32);
                break;

            case IDP_CAMERA_PREVIEW_ENABLE_MAV_CAPTURE:
                config_cp.mav_capture_enable = (kal_bool) va_arg(ap, kal_uint32);
                break;
        #endif

            case IDP_CAMERA_PREVIEW_ENG_CONFIG:
                config_cp.end_config = (kal_bool) va_arg(ap, kal_uint32);
                break;

        #if defined(IDP_CAM_PREVIEW_TILEMODE_2PASS_SUPPORT)
            case IDP_CAMERA_PREVIEW_TEMP_BUFFER:
                config_cp.cp_tmp_buffer = (kal_uint32) va_arg(ap, kal_uint32);
                break;
        #endif
        
            default:
                va_arg(ap, kal_uint32);
                break;
        }
    }

    return KAL_TRUE;
}


kal_bool
idp_camera_preview_config_hook(
    idp_owner_t * const owner,
    kal_bool const config_to_hardware)
{
    kal_bool result = KAL_TRUE;

#if defined(__MTK_TARGET__)
    if (config_cp.end_config == KAL_FALSE)
    {
        return KAL_TRUE;
    }

    result = _idp_scen_cp__config_crz(owner, config_to_hardware);
    if (KAL_FALSE == result)
    {
        return KAL_FALSE;
    }

    result = _idp_scen_cp__config_rotdma0(owner, config_to_hardware);
    if (KAL_FALSE == result)
    {
        return KAL_FALSE;
    }

#endif
    return result;
}

static hook_collect_t hook_collect =
{
    idp_camera_preview_init_config_hook,
    idp_common_open_hook,//idp_camera_preview_open_hook,
    idp_camera_preview_close_hook,
    idp_common_stop_hook,//idp_camera_preview_stop_hook,
    idp_camera_preview_read_user_config_hook,
    idp_camera_preview_read_user_config_fast_hook,
    idp_dummy_hook3,//idp_camera_preview_finish_read_user_config_hook,
    idp_dummy_hook,//idp_camera_preview_hardware_setting_have_been_changed_hook,
    idp_camera_preview_config_hook,
    idp_common_query_hook,//idp_camera_preview_query_hook,
    idp_dummy_hook,//idp_camera_preview_start_intermedia_pipe_hook,
    idp_common_start_input_hook,//idp_camera_preview_start_input_hook,
    idp_common_start_output_hook,//idp_camera_preview_start_output_hook,
    idp_dummy_hook2,//idp_camera_preview_compute_mem_for_each_hw_hook,
    idp_common_is_busy_hook,//idp_camera_preview_is_busy_hook,
    idp_common_is_in_use_hook,//idp_camera_preview_is_in_use_hook,
    idp_dummy_hook2,//idp_camera_preview_clear_setting_diff_hook,
    idp_dummy_hook//idp_camera_preview_is_setting_diff_is_ok_when_busy_hook
};





static IdpCheckReentrant idp_camera_preview_check_reentrant = {KAL_NILTASK_ID, IDP_CALL_BY_HISR, IDP_CHECK_REENTRANT_PREV_OPEN, KAL_FALSE};

kal_bool
idp_camera_preview_open_real(
    kal_uint32 * const key,
    char const * const filename,
    kal_uint32 const lineno)
{
    return idp_scenario_open(key,
                             &idp_camera_preview_check_reentrant,
                             IDP_TRACED_API_camera_preview_OPEN,
                             IDP_SCENARIO_camera_preview,
                             &hook_collect);
}

kal_bool
idp_camera_preview_is_in_use(void)
{
    return idp_scenario_is_in_use(&idp_camera_preview_check_reentrant,
                                  IDP_TRACED_API_camera_preview_IS_IN_USE,
                                  &hook_collect);
}

kal_bool
idp_camera_preview_close(
    kal_uint32 const key)
{
    return idp_scenario_close(key,
                              &idp_camera_preview_check_reentrant,
                              IDP_TRACED_API_camera_preview_CLOSE,
                              &hook_collect);
}

kal_bool
idp_camera_preview_stop(
    kal_uint32 const key)
{
#if defined(IDP_USE_G2D_AS_2NDPASS)
    volatile kal_uint32 i=0;
    while (idp_g2d_busy_flag == KAL_TRUE)
    {
		i++;
		kal_sleep_task(5);
		if(i>200) break;
    }
#endif

    return idp_scenario_stop(key,
                             &idp_camera_preview_check_reentrant,
                             IDP_TRACED_API_camera_preview_STOP,
                             &hook_collect);
}

kal_bool
idp_camera_preview_start_intermedia_pipe(
    kal_uint32 const key)
{
    return idp_scenario_start_intermedia_pipe(key,
                                              &idp_camera_preview_check_reentrant,
                                              IDP_TRACED_API_camera_preview_START_INTERMEDIA_PIPE,
                                              &hook_collect);
}

kal_bool
idp_camera_preview_start_input(
    kal_uint32 const key)
{
    return idp_scenario_start_input(key,
                                    &idp_camera_preview_check_reentrant,
                                    IDP_TRACED_API_camera_preview_START_INPUT,
                                    &hook_collect);
}

kal_bool
idp_camera_preview_start_output(
    kal_uint32 const key)
{
    return idp_scenario_start_output(key,
                                     &idp_camera_preview_check_reentrant,
                                     IDP_TRACED_API_camera_preview_START_OUTPUT,
                                     &hook_collect);
}

kal_bool
idp_camera_preview_start_all(
    kal_uint32 const key)
{
    return idp_scenario_start_all(key,
                                  &idp_camera_preview_check_reentrant,
                                  IDP_TRACED_API_camera_preview_START_ALL,
                                  &hook_collect);
}

kal_bool
idp_camera_preview_is_busy(
    kal_uint32 const key,
    kal_bool * const busy)
{
    return idp_scenario_is_busy(key,
                                busy,
                                IDP_TRACED_API_camera_preview_IS_BUSY,
                                &hook_collect);
}

kal_bool
idp_camera_preview_config_fast(
    kal_uint32 const key,
    kal_uint32 const para_type,
    kal_uint32 const para_value)
{
    return idp_scenario_config_fast(key,
                                    para_type,
                                    para_value,
                                    IDP_TRACED_API_camera_preview_CONFIG_FAST,
                                    &hook_collect);
}

kal_bool
idp_camera_preview_config(
    kal_uint32 const key,
    ...)
{
    va_list ap;
    kal_bool result;
    if (0 == key)
    {
        return KAL_FALSE;
    }

    va_start(ap, key);

    result = idp_scenario_config(key,
                                 &idp_camera_preview_check_reentrant,
                                 IDP_TRACED_API_camera_preview_CONFIG,
                                 &hook_collect,
                                 ap);
    va_end(ap);
    return result;

}

kal_bool
idp_camera_preview_configurable(
    kal_uint32 const key,
    ...)
{
    va_list ap;
    kal_bool result;
    if (0 == key)
    {
        return KAL_FALSE;
    }

    va_start(ap, key);

    result = idp_scenario_configurable(key,
                                       &idp_camera_preview_check_reentrant,
                                       IDP_TRACED_API_camera_preview_CONFIGURABLE,
                                       &hook_collect,
                                       ap);
    va_end(ap);
    return result;
}


kal_bool
idp_camera_preview_query(
    kal_uint32 const key,
    ...)
{
    va_list ap;
    kal_bool result;
    if (0 == key)
    {
        return KAL_FALSE;
    }

    va_start(ap, key);

    result = idp_scenario_query(key,
                                &idp_camera_preview_check_reentrant,
                                IDP_TRACED_API_camera_preview_QUERY,
                                &hook_collect,
                                ap);
    va_end(ap);
    return result;
}


kal_bool idp_camera_preview_config_and_start(
    kal_uint32 const key,
    ...)
{
    va_list ap;
    kal_bool result;
    if (0 == key)
    {
        return KAL_FALSE;
    }

    va_start(ap, key);

    result = idp_scenario_config_and_start(key,
                                           &idp_camera_preview_check_reentrant,
                                           IDP_TRACED_API_camera_preview_CONFIG_AND_START,
                                           &hook_collect,
                                           ap);
    va_end(ap);
    return result;
}


#endif  // #if defined(DRV_IDP_6252_SERIES)