hostsupport/hostegl/src/ColorDescriptor.cpp
branchbug235_bringup_0
changeset 53 c2ef9095503a
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 "ColorDescriptor.h"
       
    32 #include "eglInternal.h"
       
    33 
       
    34 CColorDescriptor::CColorDescriptor(EGLint red, EGLint green, EGLint blue, 
       
    35                              EGLint luminance, bool nonlinear, EGLint alpha, bool premultiplied,
       
    36                              EGLint alphaMask, EGLint bpp) :
       
    37     m_bpp( bpp ),
       
    38     m_redSize( red ),
       
    39     m_greenSize( green ),
       
    40     m_blueSize( blue ),
       
    41     m_luminanceSize( luminance ),
       
    42     m_alphaSize( alpha ),
       
    43     m_alphaMaskSize( alphaMask )
       
    44     {
       
    45     unsigned int f  = 0;
       
    46 
       
    47     if (nonlinear)
       
    48         f |= NONLINEAR;
       
    49    
       
    50     if (premultiplied)
       
    51         f |= PREMULTIPLIED;
       
    52    
       
    53     if ( luminance )
       
    54         f |= LUMINANCE;
       
    55 
       
    56     m_format = (ColorFormat)f;
       
    57 
       
    58     }
       
    59 
       
    60 void CColorDescriptor::Set( int red, int green, int blue, int luminance,
       
    61                            bool nonlinear, int alpha, bool premultiplied, int alphaMask, int bpp)
       
    62     {
       
    63     m_bpp = bpp;
       
    64     m_redSize = red;
       
    65     m_greenSize = green;
       
    66     m_blueSize = blue;
       
    67     m_luminanceSize = luminance;
       
    68     m_alphaSize = alpha;
       
    69     m_alphaMaskSize = alphaMask;
       
    70 
       
    71     unsigned int f  = 0;
       
    72     if (nonlinear)
       
    73         f |= NONLINEAR;
       
    74    
       
    75     if (premultiplied)
       
    76         f |= PREMULTIPLIED;
       
    77    
       
    78     if ( luminance )
       
    79         f |= LUMINANCE;
       
    80 
       
    81     m_format = (ColorFormat)f;
       
    82     }
       
    83 
       
    84 void CColorDescriptor::SetAttribute( EGLint attribute, EGLint value )
       
    85     {
       
    86     switch( attribute )
       
    87         {
       
    88         case EGL_BUFFER_SIZE:
       
    89             {
       
    90             m_bpp = value;
       
    91             break;
       
    92             }
       
    93         case EGL_RED_SIZE:
       
    94             {
       
    95             m_redSize = value;
       
    96             break;
       
    97             }
       
    98         case EGL_GREEN_SIZE:
       
    99             {
       
   100             m_greenSize = value;
       
   101             break;
       
   102             }
       
   103         case EGL_BLUE_SIZE:
       
   104             {
       
   105             m_blueSize = value;
       
   106             break;
       
   107             }
       
   108         case EGL_LUMINANCE_SIZE:
       
   109             {
       
   110             m_luminanceSize = value;
       
   111             break;
       
   112             }
       
   113         case EGL_ALPHA_SIZE:
       
   114             {
       
   115             m_alphaSize = value;
       
   116             break;
       
   117             }
       
   118         case EGL_ALPHA_MASK_SIZE:
       
   119             {
       
   120             m_alphaMaskSize = value;
       
   121             break;
       
   122             }
       
   123         case EGL_COLOR_BUFFER_TYPE:
       
   124             {
       
   125             SetColorBufferType(value);
       
   126             break;
       
   127             }
       
   128         case EGL_SURFACE_TYPE:
       
   129             SetSurfaceType(value);
       
   130             break;
       
   131         default:
       
   132             EGLI_ASSERT( false );
       
   133         }
       
   134     }
       
   135 
       
   136 EGLint CColorDescriptor::GetAttribute( EGLint attribute ) const
       
   137     {
       
   138     switch( attribute )
       
   139         {
       
   140         case EGL_RED_SIZE:
       
   141             {
       
   142             return m_redSize;
       
   143             }
       
   144         case EGL_GREEN_SIZE:
       
   145             {
       
   146             return m_greenSize;
       
   147             }
       
   148         case EGL_BLUE_SIZE:
       
   149             {
       
   150             return m_blueSize;
       
   151             }
       
   152         case EGL_LUMINANCE_SIZE:
       
   153             {
       
   154             return m_luminanceSize;
       
   155             }
       
   156         case EGL_ALPHA_SIZE:
       
   157             {
       
   158             return m_alphaSize;
       
   159             }
       
   160         case EGL_ALPHA_MASK_SIZE:
       
   161             {
       
   162             return m_alphaMaskSize;
       
   163             }
       
   164         case EGL_COLOR_BUFFER_TYPE:
       
   165             {
       
   166             if( isLuminance() )
       
   167                 return EGL_LUMINANCE_BUFFER;
       
   168             else
       
   169                 return EGL_RGB_BUFFER;
       
   170             }
       
   171         case EGL_SURFACE_TYPE:
       
   172             return GetSurfaceType();
       
   173             break;
       
   174         default:
       
   175             EGLI_ASSERT( false );
       
   176             return 0;
       
   177         }
       
   178     }
       
   179 
       
   180 /**
       
   181  * \brief Set alpha type and color space using bits
       
   182  * \note  Be careful not to mix with enumerations. Use the two-parameter version
       
   183  *        to set from enums.
       
   184  */
       
   185 void CColorDescriptor::SetSurfaceType(EGLint surfaceType)
       
   186 {
       
   187     unsigned int f = m_format;
       
   188 
       
   189     f &= ~PREMULTIPLIED;
       
   190 
       
   191     if (surfaceType & EGL_VG_ALPHA_FORMAT_PRE_BIT)
       
   192         f |= PREMULTIPLIED;     
       
   193 
       
   194     f &= ~NONLINEAR;
       
   195     if (!(surfaceType & EGL_VG_COLORSPACE_LINEAR_BIT))
       
   196         f |= NONLINEAR;
       
   197 
       
   198     m_format = (ColorFormat)f;    
       
   199 }
       
   200 
       
   201 /**
       
   202  * \brief Set alpha type and color space using enumerations.
       
   203  */
       
   204 void CColorDescriptor::SetSurfaceType(EGLint colorSpace, EGLint alphaType)
       
   205 {
       
   206     unsigned int f = m_format;
       
   207 
       
   208     f &= ~PREMULTIPLIED;
       
   209 
       
   210     if (alphaType == EGL_VG_ALPHA_FORMAT_PRE)
       
   211         f |= PREMULTIPLIED;     
       
   212 
       
   213     f &= ~NONLINEAR;
       
   214     if (colorSpace == EGL_VG_COLORSPACE_sRGB)
       
   215         f |= NONLINEAR;
       
   216 
       
   217     m_format = (ColorFormat)f;    
       
   218 }
       
   219 
       
   220 
       
   221 EGLint CColorDescriptor::GetSurfaceType() const
       
   222 {
       
   223     EGLint ret = 0;
       
   224 
       
   225     if (!(m_format & NONLINEAR))
       
   226         ret |= EGL_VG_COLORSPACE_LINEAR_BIT;
       
   227 
       
   228     if (m_format & PREMULTIPLIED)
       
   229         ret |= EGL_VG_ALPHA_FORMAT_PRE_BIT;
       
   230 
       
   231     return ret;
       
   232 }
       
   233 
       
   234 void CColorDescriptor::SetColorBufferType( EGLint colorBufferType )
       
   235 {
       
   236     unsigned int f = m_format & (~LUMINANCE);
       
   237 
       
   238     switch(colorBufferType)
       
   239     {
       
   240     case EGL_LUMINANCE_BUFFER:
       
   241         f |= LUMINANCE;
       
   242         break;
       
   243     default:
       
   244         EGLI_ASSERT(colorBufferType == EGL_RGB_BUFFER);
       
   245         break;
       
   246     }
       
   247 
       
   248     m_format = (ColorFormat)f;
       
   249 }
       
   250 
       
   251 CColorDescriptor& CColorDescriptor::operator=( const CColorDescriptor& rhs )
       
   252     {
       
   253     if( this == &rhs ) return *this;
       
   254     m_bpp = rhs.m_bpp;
       
   255     m_redSize = rhs.m_redSize;
       
   256     m_greenSize = rhs.m_greenSize;
       
   257     m_blueSize = rhs.m_blueSize;
       
   258     m_luminanceSize = rhs.m_luminanceSize;
       
   259     m_alphaSize = rhs.m_alphaSize;
       
   260     m_alphaMaskSize = rhs.m_alphaMaskSize;
       
   261     m_format = rhs.m_format;
       
   262     return *this;
       
   263     }
       
   264 
       
   265 bool CColorDescriptor::operator==( const CColorDescriptor& rhs ) const
       
   266     {
       
   267     if( this == &rhs ) return true;
       
   268     
       
   269     if( isNonlinear() != rhs.isNonlinear() ) return false;
       
   270     if( isPremultiplied() != rhs.isPremultiplied() ) return false;
       
   271 
       
   272     if( m_alphaSize != rhs.m_alphaSize ) return false;
       
   273     if( m_alphaMaskSize != rhs.m_alphaMaskSize ) return false;
       
   274     
       
   275     if( isLuminance() && rhs.isLuminance()  )
       
   276         {
       
   277         if( m_luminanceSize != rhs.m_luminanceSize ) return false;        
       
   278         }
       
   279     else
       
   280         {
       
   281         if( m_bpp != rhs.m_bpp ) return false;
       
   282         if( m_redSize != rhs.m_redSize ) return false;
       
   283         if( m_greenSize != rhs.m_greenSize ) return false;
       
   284         if( m_blueSize != rhs.m_blueSize ) return false;
       
   285         }
       
   286     return true;
       
   287     }
       
   288 
       
   289 bool CColorDescriptor::operator!=( const CColorDescriptor& rhs ) const
       
   290     {
       
   291     return !(operator==(rhs));
       
   292     }
       
   293 
       
   294 bool CColorDescriptor::MatchBitDepth( const CColorDescriptor& desc ) const
       
   295     {
       
   296     if( m_alphaSize != desc.m_alphaSize ) return false;
       
   297     if( isPremultiplied() != desc.isPremultiplied() ) return false;
       
   298     if( isLuminance() )
       
   299         {
       
   300         if( !desc.isLuminance() || (m_luminanceSize != desc.m_luminanceSize ) ) return false;
       
   301         }
       
   302     else
       
   303         {
       
   304         if( m_redSize != desc.m_redSize ) return false;
       
   305         if( m_greenSize != desc.m_greenSize ) return false;
       
   306         if( m_blueSize != desc.m_blueSize ) return false;
       
   307         if( isNonlinear() != desc.isNonlinear() ) return false;
       
   308         }
       
   309     return true;
       
   310     }
       
   311 
       
   312