minisurf.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:15k
源码类别:

Symbian

开发平台:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK ***** 
  2.  * Version: RCSL 1.0/RPSL 1.0 
  3.  *  
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
  5.  *      
  6.  * The contents of this file, and the files included with this file, are 
  7.  * subject to the current version of the RealNetworks Public Source License 
  8.  * Version 1.0 (the "RPSL") available at 
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
  10.  * the file under the RealNetworks Community Source License Version 1.0 
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
  12.  * in which case the RCSL will apply. You may also obtain the license terms 
  13.  * directly from RealNetworks.  You may not use this file except in 
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
  16.  * RCSL for the rights, obligations and limitations governing use of the 
  17.  * contents of the file.  
  18.  *  
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the 
  20.  * developer of the Original Code and owns the copyrights in the portions 
  21.  * it created. 
  22.  *  
  23.  * This file, and the files included with this file, is distributed and made 
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  28.  * 
  29.  * Technology Compatibility Kit Test Suite(s) Location: 
  30.  *    http://www.helixcommunity.org/content/tck 
  31.  * 
  32.  * Contributor(s): 
  33.  *  
  34.  * ***** END LICENSE BLOCK ***** */ 
  35. #include "minisite.h"
  36. #include "minisurf.h"
  37. #include "ciddefs.h"
  38. #if defined(HELIX_CONFIG_NOSTATICS)
  39. //On Symbian we don't use a colorconvert DLL. We compile in 
  40. //one and use slightly different color converter calls that
  41. //don't use static data.
  42. # include "nostatic/yuv.h" //for the color_data_t struct.
  43. # include "nostatic/colorlib.h" //for the new converters
  44. #else
  45. #include "colorlib.h"
  46. #endif
  47. #if defined(_UNIX) || defined(_MACINTOSH) || defined(_SYMBIAN) || defined(_OPENWAVE)
  48. #  ifndef MAKEFOURCC
  49. #define MAKEFOURCC(ch0, ch1, ch2, ch3) 
  50.                   ((ULONG32)(BYTE)(ch0) | ((ULONG32)(BYTE)(ch1) << 8) | 
  51.                   ((ULONG32)(BYTE)(ch2) << 16) | ((ULONG32)(BYTE)(ch3) << 24 ))
  52. #  endif //defined(MAKEFOURCC)
  53. #endif //_UNIX || _MACINTOSH
  54. CMiniBaseSurface::CMiniBaseSurface(IUnknown* pContext, CMiniBaseSite* pSite)
  55.     :  m_lRefCount(0)
  56.        ,  m_pContext(pContext)
  57.        ,  m_pSite(pSite)
  58.        ,  m_pOptimizedFormat(NULL)
  59.        ,  m_nSrcCID(CID_UNKNOWN)
  60.        ,  m_nDstBuffers(0)
  61.        ,  m_fpColorConverter(NULL)
  62.        ,  m_pImageHelper(NULL)
  63. {
  64.     memset(&m_dstBufSize, 0, sizeof(m_dstBufSize));
  65.     m_pContext->AddRef();
  66.     m_pImageHelper = new CFmtObj;
  67. }
  68. CMiniBaseSurface::~CMiniBaseSurface()
  69. {
  70.     HX_RELEASE(m_pContext);
  71.     HX_DELETE(m_pImageHelper);
  72.     HX_DELETE(m_pOptimizedFormat);
  73. }
  74. /************************************************************************
  75.  *  Method:
  76.  *    IUnknown::QueryInterface
  77.  */
  78. STDMETHODIMP CMiniBaseSurface::QueryInterface(REFIID riid, void** ppvObj)
  79. {
  80.     if (IsEqualIID(riid, IID_IHXVideoSurface))
  81.     {
  82.         AddRef();
  83.         *ppvObj = (IUnknown*)(IHXVideoSurface*)this;
  84.         return HXR_OK;
  85.     }
  86.     else if (IsEqualIID(riid, IID_IUnknown))
  87.     {
  88.         AddRef();
  89.         *ppvObj = (IUnknown*)(IHXSite*)this;
  90.         return HXR_OK;
  91.     }
  92.     *ppvObj = NULL;
  93.     return HXR_NOINTERFACE;
  94. }
  95. /************************************************************************
  96.  *  Method:
  97.  *    IUnknown::AddRef
  98.  */
  99. STDMETHODIMP_(ULONG32) CMiniBaseSurface::AddRef()
  100. {
  101.     return InterlockedIncrement(&m_lRefCount);
  102. }
  103. /************************************************************************
  104.  *  Method:
  105.  *    IUnknown::Release
  106.  */
  107. STDMETHODIMP_(ULONG32) CMiniBaseSurface::Release()
  108. {
  109.     if (InterlockedDecrement(&m_lRefCount) > 0)
  110.     {
  111.         return m_lRefCount;
  112.     }
  113.     delete this;
  114.     return 0;
  115. }
  116. inline
  117. IsCompatible(const HXBitmapInfoHeader* pLeft, const HXBitmapInfoHeader* pRight)
  118. {
  119.     //return (0 == memcmp(m_pOptimizedFormat, pBitmapInfo, sizeof(HXBitmapInfoHeader));
  120.     return (pLeft->biWidth == pRight->biWidth &&
  121.             pLeft->biHeight == pRight->biHeight &&
  122.             pLeft->biPlanes == pRight->biPlanes &&
  123.             pLeft->biBitCount == pRight->biBitCount &&
  124.             pLeft->biCompression == pRight->biCompression);
  125. }
  126. /************************************************************************
  127.  *  Method:
  128.  *    IHXVideoSurface::BeginOptimizedBlt
  129.  */
  130. STDMETHODIMP CMiniBaseSurface::BeginOptimizedBlt(HXBitmapInfoHeader* pBitmapInfo)
  131. {
  132.     HX_ASSERT(pBitmapInfo);    
  133.     if(m_pOptimizedFormat && IsCompatible(m_pOptimizedFormat, pBitmapInfo))
  134.     {
  135.         // current optimized format bitmap info (as well as info we derive from it) is valid
  136.         return HXR_OK;
  137.     }
  138.     // clean up
  139.     EndOptimizedBlt();
  140.     // save copy of bitmap info
  141.     m_pOptimizedFormat = new HXBitmapInfoHeader;
  142.     if(!m_pOptimizedFormat)
  143.     {
  144.         return HXR_OUTOFMEMORY;
  145.     }
  146.     *m_pOptimizedFormat = *pBitmapInfo;
  147.     // save current color format id
  148.     m_nSrcCID = m_pImageHelper->GetBitmapColor((HXBitmapInfo*) pBitmapInfo);
  149.     HX_ASSERT(m_nSrcCID != -1);
  150.     // determine color converter
  151.     int nDstCID = GetDstCID();
  152.     switch (m_nSrcCID)
  153.     {
  154. #if defined(HELIX_FEATURE_SMIL_SITE) && defined(HELIX_FEATURE_CC_RGB444out)
  155.        case CID_RGB32:
  156.        case CID_ARGB32:
  157.            if (nDstCID == CID_RGB444)
  158.                m_fpColorConverter = RGB32toRGB444;
  159.            m_nSrcPitch = m_pImageHelper->GetBitmapPitch((HXBitmapInfo*)pBitmapInfo);
  160.            break;
  161.        case CID_RGB444:
  162.            if (nDstCID == CID_RGB444)
  163.                m_fpColorConverter = RGB444toRGB444;
  164.            m_nSrcPitch = m_pImageHelper->GetBitmapPitch((HXBitmapInfo*)pBitmapInfo);
  165.            break;
  166. #endif //HELIX_FEATURE_SMIL_SITE
  167.            
  168.        case CID_I420:
  169.         
  170. #if defined (HELIX_FEATURE_CC_YV12out)
  171.            if (nDstCID == CID_YV12)
  172.                m_fpColorConverter = I420toYV12;
  173. #endif //HELIX_FEATURE_CC_YV12out
  174. #if defined (HELIX_FEATURE_CC_YUY2out)
  175.            if (nDstCID == CID_YUY2)
  176.                m_fpColorConverter = I420toYUY2;
  177. #endif //HELIX_FEATURE_CC_YUY2out
  178. #if defined (HELIX_FEATURE_CC_RGB32out)                     
  179.            if (nDstCID == CID_RGB32)
  180.                m_fpColorConverter = I420toRGB32;
  181. #endif //HELIX_FEATURE_CC_RGB32out
  182. #if defined (HELIX_FEATURE_CC_RGB24out)
  183.            if (nDstCID == CID_RGB24)
  184.                m_fpColorConverter = I420toRGB24;
  185. #endif //HELIX_FEATURE_CC_RGB24out
  186. #if defined (HELIX_FEATURE_CC_RGB565out)
  187.            if (nDstCID == CID_RGB565)
  188.                m_fpColorConverter = I420toRGB565;
  189. #endif //HELIX_FEATURE_CC_RGB565out
  190. #if defined (HELIX_FEATURE_CC_RGB555out)
  191.            if (nDstCID == CID_RGB555)
  192.                m_fpColorConverter = I420toRGB555;
  193. #endif //HELIX_FEATURE_CC_RGB555out
  194. #if defined (HELIX_FEATURE_CC_RGB444out)
  195.            if (nDstCID == CID_RGB444)
  196.                m_fpColorConverter = I420toRGB444;
  197. #endif //HELIX_FEATURE_CC_RGB444out
  198. #if defined (HELIX_FEATURE_CC_RGB8out)
  199.            if (nDstCID == CID_RGB8)
  200.                m_fpColorConverter = I420toRGB8;
  201. #endif //HELIX_FEATURE_CC_RGB444out
  202.            m_nSrcPitch = pBitmapInfo->biWidth;
  203.            break;
  204.        case CID_XING:
  205. #if defined (HELIX_FEATURE_CC_YV12out)
  206.            if (nDstCID == CID_YV12)
  207.                m_fpColorConverter = XINGtoYV12;
  208. #endif //HELIX_FEATURE_CC_YV12out
  209. #if defined (HELIX_FEATURE_CC_YUY2out)
  210.            if (nDstCID == CID_YUY2)
  211.                m_fpColorConverter = XINGtoYUY2;
  212. #endif //HELIX_FEATURE_CC_YUY2out
  213. #if defined (HELIX_FEATURE_CC_RGB32out)
  214.            if (nDstCID == CID_RGB32)
  215.                m_fpColorConverter = XINGtoRGB32;
  216. #endif //HELIX_FEATURE_CC_YUY2out
  217.         
  218. #if defined (HELIX_FEATURE_CC_RGB565out)
  219.            if (nDstCID == CID_RGB565)
  220.                m_fpColorConverter = XINGtoRGB565;
  221. #endif //HELIX_FEATURE_CC_RGB565out
  222.            m_nSrcPitch = 768;
  223.            break;
  224.     }
  225.     HX_ASSERT(m_fpColorConverter);
  226.     
  227. #if defined(HELIX_CONFIG_NOSTATICS)
  228.     SetSrcI420Colors (0, 0, 0, 0, GetColorData());
  229. #else    
  230.     SetSrcI420Colors (0, 0, 0, 0); 
  231.     SetDestI420Colors (0, 0, 0, 0);
  232.     SetI420ChromaResamplingMode (0);
  233. #endif    
  234.     
  235.     return m_fpColorConverter ? HXR_OK : HXR_FAIL;
  236. }
  237. /************************************************************************
  238.  *  Method:
  239.  *    IHXVideoSurface::Blt
  240.  */
  241. STDMETHODIMP CMiniBaseSurface::Blt(UCHAR*              pSrcBuffer,
  242.                                    HXBitmapInfoHeader* pBitmapInfo,
  243.                                    REF(HXxRect)        rDestRect,
  244.                                    REF(HXxRect)        rSrcRect)
  245. {
  246.     
  247.     HX_RESULT hr = BeginOptimizedBlt(pBitmapInfo);
  248.     if (FAILED(hr))
  249.     {
  250.         return hr;
  251.     }
  252.         
  253.     int nDstWidth   = rDestRect.right - rDestRect.left;
  254.     int nDstHeight  = rDestRect.bottom - rDestRect.top;
  255.     // ensure destination buffers are allocated, ensuring that we properly handle image stretching.
  256.     if(ScaleDuringRender(GetDstCID()))
  257.     {
  258.         hr = AllocateDestBuffers(pBitmapInfo->biWidth, pBitmapInfo->biHeight);
  259.     }
  260.     else
  261.     {
  262.         // we stretch during the transfer from source to dest 
  263.         // buffer; dest buffer must be same size as dest rect.
  264.         hr = AllocateDestBuffers(nDstWidth, nDstHeight);
  265.     }
  266.     if(FAILED(hr))
  267.     {
  268.         return hr;
  269.     }
  270.     UCHAR*  pDestBuffer = NULL;
  271.     LONG32  nDestPitch  = 0;
  272.     HXxSize dstSize     = m_dstBufSize;
  273.     hr = _LockDestBuffer(&pDestBuffer,
  274.                          &nDestPitch,
  275.                          m_nSrcCID,
  276.                          dstSize);
  277.     if (FAILED(hr))
  278.         return hr;
  279.     HXxRect rcDst = rDestRect;
  280.     HXxRect rcSrc = rSrcRect;
  281.     // If the dest buffer dimensions do not match the dest rect,
  282.     // map the dest rect to the dest buffer for video transfer.
  283.     if ((m_dstBufSize.cx != nDstWidth) | 
  284.         (m_dstBufSize.cy != nDstHeight))
  285.     {
  286.  
  287.         rcDst.left = (rcDst.left*m_dstBufSize.cx) / nDstWidth;
  288.         rcDst.right = (rcDst.right*m_dstBufSize.cx) / nDstWidth;
  289.         rcDst.top = (rcDst.top*m_dstBufSize.cy) / nDstHeight;
  290.         rcDst.bottom = (rcDst.bottom*m_dstBufSize.cy) / nDstHeight;
  291.     }
  292.     hr = _TransferToDestBuffer(pSrcBuffer,
  293.                                pBitmapInfo,
  294.                                &rcSrc,
  295.                                &rcDst,
  296.                                pDestBuffer,
  297.                                nDestPitch);
  298.     
  299.     _UnlockDestBuffer(pDestBuffer);
  300.     if (SUCCEEDED(hr))
  301.         hr = _RenderDestBuffer(&rcDst, &rDestRect);
  302.     
  303.     //Give the device a chance to keep the screen alive.
  304.     _KeepScreenAlive();
  305.     
  306.     return hr;
  307. }
  308. /************************************************************************
  309.  *  Method:
  310.  *    IHXVideoSurface::OptimizedBlt
  311.  */
  312. STDMETHODIMP CMiniBaseSurface::OptimizedBlt(UCHAR* pSrcBuffer,
  313.                                             REF(HXxRect) rDestRect,
  314.                                             REF(HXxRect) rSrcRect)
  315. {
  316.     return Blt(pSrcBuffer, 
  317.                m_pOptimizedFormat,
  318.                rDestRect,
  319.                rSrcRect); 
  320. }
  321. /************************************************************************
  322.  *  Method:
  323.  *    IHXVideoSurface::EndOptimizedBlt
  324.  */
  325. STDMETHODIMP CMiniBaseSurface::EndOptimizedBlt(void)
  326. {
  327.     HX_RESULT hr =  DestroyDestBuffers(m_nDstBuffers);
  328.     HX_DELETE(m_pOptimizedFormat);
  329.     m_nSrcCID = CID_UNKNOWN;
  330.     return hr;
  331. }
  332. /************************************************************************
  333.  *  Method:
  334.  *    IHXVideoSurface::GetOptimizedFormat
  335.  */
  336. STDMETHODIMP CMiniBaseSurface::GetOptimizedFormat(REF(HX_COMPRESSION_TYPE) ulType)
  337. {
  338.     ulType = 0;
  339.     return HXR_OK;
  340. }
  341. /************************************************************************
  342.  *  Method:
  343.  *    IHXVideoSurface::GetPreferredFormat
  344.  */
  345. STDMETHODIMP CMiniBaseSurface::GetPreferredFormat(REF(HX_COMPRESSION_TYPE) ulType)
  346. {                             
  347.     ulType = 0;
  348.     return HXR_OK;
  349. }                                                        
  350.                                       
  351. HX_RESULT 
  352. CMiniBaseSurface::_TransferToDestBuffer(UCHAR* pSrcBuffer,
  353.                                         HXBitmapInfoHeader* pBitmapInfo,
  354.                                         HXxRect* prSrcRect,
  355.                                         HXxRect* prDstRect,
  356.                                         UCHAR* pDstBuffer,
  357.                                         LONG32 nDstPitch)
  358. {
  359.     if (!m_fpColorConverter)
  360.         return HXR_FAIL;
  361.     int nRet = m_fpColorConverter( pDstBuffer,
  362.                                    m_dstBufSize.cx,
  363.                                    m_dstBufSize.cy,
  364.                                    nDstPitch,
  365.                                    prDstRect->left, prDstRect->top,
  366.                                    prDstRect->right-prDstRect->left, 
  367.                                    prDstRect->bottom-prDstRect->top,
  368.                                    pSrcBuffer,
  369.                                    pBitmapInfo->biWidth,
  370.                                    pBitmapInfo->biHeight,
  371.                                    m_nSrcPitch,
  372.                                    prSrcRect->left, prSrcRect->top,
  373.                                    prSrcRect->right-prSrcRect->left, 
  374.                                    prSrcRect->bottom-prSrcRect->top
  375. #if defined(HELIX_CONFIG_NOSTATICS)    
  376.                                    ,GetColorData()
  377. #endif    
  378.                                    );
  379.     if (nRet < 0)
  380.         return HXR_FAIL;
  381.     else
  382.         return HXR_OK;
  383. }
  384. BOOL CMiniBaseSurface::ScaleDuringRender(int nCID)
  385. {
  386.     // Our YUVtoYUV color converters do not scale so scaling
  387.     // must be done during rendering.
  388.     //
  389.     // Our XXXtoRGB do scaling
  390.     return (nCID == CID_DVPF) | 
  391.            (nCID >= CID_I420 && nCID <= CID_UYVY);
  392. }
  393. HX_RESULT CMiniBaseSurface::AllocateDestBuffers(int nWidth,
  394.                                                 int nHeight,
  395.                                                 int nCount)
  396. {
  397.     if (nWidth == m_dstBufSize.cx &&
  398.         nHeight == m_dstBufSize.cy &&
  399.         nCount == m_nDstBuffers)
  400.     {
  401.         return HXR_OK;
  402.     }
  403.     else
  404.     {
  405.         DestroyDestBuffers(m_nDstBuffers);
  406.     }
  407.     HX_RESULT hr = _CreateDestBuffer(m_nSrcCID, nWidth, nHeight, nCount);
  408.     if (HXR_OK == hr)
  409.     {
  410.         m_dstBufSize.cx = nWidth;
  411.         m_dstBufSize.cy = nHeight;
  412.         m_nDstBuffers = nCount;
  413.     }
  414.     return hr;
  415. }
  416. HX_RESULT CMiniBaseSurface::DestroyDestBuffers(int nCount)
  417. {
  418.     HX_RESULT hr = HXR_OK;
  419.     if (m_dstBufSize.cx || m_dstBufSize.cy)
  420.     {
  421.         hr = _DestroyDestBuffer(m_nSrcCID, nCount); 
  422.         memset(&m_dstBufSize, 0, sizeof(m_dstBufSize));
  423.         m_nDstBuffers = 0;
  424.     }
  425.     return hr;
  426. }