hostsupport/hostegl/src/EGLSurface.cpp
branchbug235_bringup_0
changeset 53 c2ef9095503a
child 56 40cc73c24bf8
equal deleted inserted replaced
52:39e5f73667ba 53:c2ef9095503a
       
     1 /* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2  *
       
     3  * Permission is hereby granted, free of charge, to any person obtaining a
       
     4  * copy of this software and /or associated documentation files
       
     5  * (the "Materials "), to deal in the Materials without restriction,
       
     6  * including without limitation the rights to use, copy, modify, merge,
       
     7  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
     8  * and to permit persons to whom the Materials are furnished to do so,
       
     9  * subject to the following conditions:
       
    10  *
       
    11  * The above copyright notice and this permission notice shall be included
       
    12  * in all copies or substantial portions of the Materials.
       
    13  *
       
    14  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    20  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    21  *
       
    22  * Initial Contributors:
       
    23  * Nokia Corporation - initial contribution.
       
    24  *
       
    25  * Contributors:
       
    26  *
       
    27  * Description:
       
    28  *
       
    29  */
       
    30 
       
    31 #include "EGLSurface.h"
       
    32 #include "SurfaceDescriptor.h"
       
    33 #include "EGLConfig.h"
       
    34 #include "EGLContext.h"
       
    35 #include "EGLOs.h"
       
    36 
       
    37 #include <string.h>
       
    38 // Required for free. \todo Shouldn't this use C++ new[]/delete[] instead?
       
    39 #include <stdlib.h>
       
    40 
       
    41 CEGLSurface::CEGLSurface( CEGLSurface::SurfaceType type, EGLint colorSpace, EGLint alphaFormat, EGLint renderBuffer, CEGLConfig* config ) :
       
    42     m_type( type ),
       
    43     m_colorSpace( colorSpace ),
       
    44     m_alphaFormat( alphaFormat ),
       
    45     m_renderBuffer( renderBuffer ),
       
    46     m_config( config ),
       
    47     m_boundContext( NULL ),
       
    48     m_thread( NULL ),
       
    49     m_mipmapLevel( 0 ),
       
    50     m_multisampleResolve( EGL_MULTISAMPLE_RESOLVE_DEFAULT ),
       
    51     m_swapBehavior( /*EGL_BUFFER_DESTROYED*/EGL_BUFFER_PRESERVED ),
       
    52     m_vgDirty(false),
       
    53     m_gles1Dirty(false),
       
    54     m_gles2Dirty(false),
       
    55     m_isLocked(false)
       
    56     {
       
    57     EGLI_ASSERT( m_config != NULL );
       
    58 	memset( &m_textureBinding, 0, sizeof(struct EGLITextureBinding) );
       
    59     memset( &m_vgBuffers, 0, sizeof(struct BufferContainer) );
       
    60     memset( &m_glesBuffers, 0, sizeof(struct BufferContainer) );
       
    61     CEGLOs::InitializeLock( &m_lock );
       
    62     }
       
    63 
       
    64 CEGLSurface::~CEGLSurface(void)
       
    65     {
       
    66     if( m_glesBuffers.m_colorBuffer )
       
    67         free (m_glesBuffers.m_colorBuffer );
       
    68     CEGLOs::DestroyLock( &m_lock );
       
    69     }
       
    70 
       
    71 void CEGLSurface::BindToContext( CEGLContext* context ) 
       
    72     { 
       
    73     m_boundContext = context;
       
    74     }
       
    75 
       
    76 bool CEGLSurface::Resize( EGLint width, EGLint height )
       
    77     {
       
    78     if( width == m_width && height == m_height && m_glesBuffers.m_colorBuffer )
       
    79         {
       
    80         // Size didn't change and we have gles color buffer already
       
    81         return true;
       
    82         }
       
    83         
       
    84     if( width != m_width )
       
    85         {
       
    86         SetStride( (width * m_config->GetDescriptor()->Bpp() + 7)/8 );
       
    87         }
       
    88     m_width = width;
       
    89     m_height = height;
       
    90     if( m_glesBuffers.m_colorBuffer )
       
    91         free( m_glesBuffers.m_colorBuffer );
       
    92     m_glesBuffers.m_colorBuffer = malloc( height * m_stride );
       
    93     if( !(m_glesBuffers.m_colorBuffer) ) return false;
       
    94     return true;
       
    95     }
       
    96 
       
    97 SurfaceDescriptor* CEGLSurface::Descriptor()
       
    98     {
       
    99     CColorDescriptor* colorDesc = m_config->GetDescriptor();
       
   100     EGLI_ASSERT( colorDesc != NULL );
       
   101     m_surfaceDescriptor.m_width = Width();
       
   102     m_surfaceDescriptor.m_height = Height();
       
   103     m_surfaceDescriptor.m_stride = Stride();
       
   104     m_surfaceDescriptor.m_colorDescriptor = *colorDesc;
       
   105     m_surfaceDescriptor.m_colorDescriptor.SetSurfaceType(m_colorSpace, m_alphaFormat);
       
   106 
       
   107     EGLint alphaSize = colorDesc->AlphaSize();
       
   108     EGLint blueSize = colorDesc->BlueSize();
       
   109     EGLint greenSize = colorDesc->GreenSize();
       
   110     EGLint redSize = colorDesc->RedSize();
       
   111     EGLint luminanceSize = colorDesc->LuminanceSize();
       
   112 
       
   113     if( colorDesc->isLuminance() )
       
   114         {
       
   115         // \todo LA AL?
       
   116         m_surfaceDescriptor.m_redShift = 0;
       
   117         m_surfaceDescriptor.m_greenShift = 0;
       
   118         m_surfaceDescriptor.m_blueShift = 0;
       
   119         m_surfaceDescriptor.m_alphaShift = 0;
       
   120         m_surfaceDescriptor.m_luminanceShift = alphaSize;
       
   121         }
       
   122     else
       
   123         {
       
   124         m_surfaceDescriptor.m_luminanceShift = 0;
       
   125         /*  Found these from CPlatsimEGL::egliGetSymbianPixelFormat()
       
   126             in platsim_graphics/platsimegl/src/platsimegl.cpp. Pixel format
       
   127             should be transfered through serialization.
       
   128 
       
   129             EUidPixelFormatARGB_8888
       
   130             EUidPixelFormatARGB_8888_PRE
       
   131             EUidPixelFormatXRGB_8888
       
   132 
       
   133             EUidPixelFormatARGB_4444
       
   134             EUidPixelFormatXRGB_4444
       
   135 
       
   136             EUidPixelFormatRGB_565
       
   137             EUidPixelFormatARGB_1555
       
   138             
       
   139             -- 
       
   140             Harri
       
   141         */
       
   142         // \todo We need pixel format through serialization.
       
   143 #if defined(EGLI_USE_PLATSIM_EXTENSIONS)
       
   144         m_surfaceDescriptor.m_blueShift = 0;
       
   145         m_surfaceDescriptor.m_greenShift = blueSize;
       
   146         m_surfaceDescriptor.m_redShift = blueSize + greenSize;
       
   147         m_surfaceDescriptor.m_alphaShift = blueSize + greenSize + redSize;
       
   148 #else
       
   149         // \note TeroP, changing these to match OpenVG color formats. This should make blitting
       
   150         // to _win32_ window fast, but a solution for Symbian vs. Win32 native build should be
       
   151         // defined.
       
   152         // That is: This has to match the EGLtoVGInterface::CopyBuffers VGImageFormat somehow.
       
   153         // Currently: VG_sARGB_8888_PRE;
       
   154 
       
   155         m_surfaceDescriptor.m_blueShift = 0;
       
   156         m_surfaceDescriptor.m_greenShift = blueSize;
       
   157         m_surfaceDescriptor.m_redShift = blueSize + greenSize;
       
   158         m_surfaceDescriptor.m_alphaShift = redSize + greenSize + blueSize;
       
   159 #endif //EGLI_USE_PLATSIM_EXTENSIONS
       
   160         }
       
   161     
       
   162     m_surfaceDescriptor.m_maskSize = m_surfaceDescriptor.m_colorDescriptor.AlphaMaskSize();
       
   163     m_surfaceDescriptor.m_stencilSize = m_config->GetAttribute( EGL_STENCIL_SIZE );
       
   164     m_surfaceDescriptor.m_depthSize = m_config->GetAttribute( EGL_DEPTH_SIZE );
       
   165     return &m_surfaceDescriptor;
       
   166     }
       
   167 
       
   168 void CEGLSurface::SetAttribute( EGLint attrib, EGLint value )
       
   169     {
       
   170     switch( attrib )
       
   171         {
       
   172         case EGL_MIPMAP_LEVEL:
       
   173             {
       
   174             m_mipmapLevel = value;
       
   175             break;
       
   176             }
       
   177         case EGL_MULTISAMPLE_RESOLVE:
       
   178             {
       
   179             m_multisampleResolve = value;
       
   180             break;
       
   181             }
       
   182         case EGL_SWAP_BEHAVIOR:
       
   183             {
       
   184             m_swapBehavior = value;
       
   185             break;
       
   186             }
       
   187         }
       
   188     }
       
   189 
       
   190 EGLint CEGLSurface::Attribute( EGLint attrib ) const
       
   191     {
       
   192     EGLint ret = EGL_BAD_ATTRIBUTE;
       
   193     switch( attrib )
       
   194         {
       
   195         case EGL_MIPMAP_LEVEL:
       
   196             {
       
   197             ret = m_mipmapLevel;
       
   198             break;
       
   199             }
       
   200         case EGL_MULTISAMPLE_RESOLVE:
       
   201             {
       
   202             ret = m_multisampleResolve;
       
   203             break;
       
   204             }
       
   205         case EGL_SWAP_BEHAVIOR:
       
   206             {
       
   207             ret = m_swapBehavior;
       
   208             break;
       
   209             }
       
   210         }
       
   211     return ret;
       
   212     }
       
   213 
       
   214 void CEGLSurface::Lock()
       
   215     {
       
   216     CEGLOs::GetLock( &m_lock );
       
   217     m_isLocked = true;
       
   218     }
       
   219 void CEGLSurface::Unlock()
       
   220     {
       
   221     CEGLOs::ReleaseLock( &m_lock );
       
   222     m_isLocked = false;
       
   223     }
       
   224