Making DALi core internals typesafe using guaranteed types; uint8_t, uint32_t
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture.cpp
index b4646d5..99fca0d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 // CLASS HEADER
 #include <dali/internal/render/renderers/render-texture.h>
 
-// INTERNAL INCLUDES
-#include <dali/devel-api/images/pixel-devel.h>
-#include <dali/devel-api/images/native-image-interface-extension.h>
-
 // EXTERNAL INCLUDES
 #include <math.h>   //floor, log2
 
+// INTERNAL INCLUDES
+#include <dali/devel-api/images/native-image-interface-extension.h>
+
 namespace Dali
 {
 namespace Internal
@@ -130,7 +129,7 @@ GLint WrapModeToGL( WrapMode::Type wrapMode, GLint defaultWrapMode )
  * @param[out] glInternalFormat The gl internal format.
  * @param[out] pixelDataType The data type of the pixel data.
  */
-void PixelFormatToGl( DevelPixel::Format pixelFormat, GLenum& glFormat, GLint& glInternalFormat, GLenum& pixelDataType )
+void PixelFormatToGl( Pixel::Format pixelFormat, GLenum& glFormat, GLint& glInternalFormat, GLenum& pixelDataType )
 {
   // Compressed textures have no pixelDataType, so init to an invalid value:
   pixelDataType  = -1;
@@ -505,13 +504,13 @@ void PixelFormatToGl( DevelPixel::Format pixelFormat, GLenum& glFormat, GLint& g
     }
 
     // GLES 3.0 floating point formats.
-    case DevelPixel::RGB16F:
+    case Pixel::RGB16F:
     {
       glFormat = GL_RGB;
       pixelDataType = GL_HALF_FLOAT;
       break;
     }
-    case DevelPixel::RGB32F:
+    case Pixel::RGB32F:
     {
       glFormat = GL_RGB;
       pixelDataType = GL_FLOAT;
@@ -528,8 +527,8 @@ void PixelFormatToGl( DevelPixel::Format pixelFormat, GLenum& glFormat, GLint& g
 
   switch( pixelFormat )
   {
-    case DevelPixel::RGB16F:
-    case DevelPixel::RGB32F: // FALL THROUGH
+    case Pixel::RGB16F:
+    case Pixel::RGB32F: // FALL THROUGH
     {
       glInternalFormat = GL_R11F_G11F_B10F;
       break;
@@ -567,6 +566,8 @@ bool IsCompressedFormat(Pixel::Format pixelFormat)
     case Pixel::BGR8888:
     case Pixel::RGBA8888:
     case Pixel::BGRA8888:
+    case Pixel::RGB16F:
+    case Pixel::RGB32F:
     case Pixel::INVALID:
     {
       return false;
@@ -638,7 +639,7 @@ Texture::Texture( Type type, Pixel::Format format, ImageDimensions size )
   mHasAlpha( HasAlpha( format ) ),
   mIsCompressed( IsCompressedFormat( format ) )
 {
-  PixelFormatToGl( static_cast<DevelPixel::Format>( format ),
+  PixelFormatToGl( format,
                    mGlFormat,
                    mGlInternalFormat,
                    mPixelDataType );
@@ -652,8 +653,8 @@ Texture::Texture( NativeImageInterfacePtr nativeImageInterface )
   mGlInternalFormat( GL_RGB ),
   mGlFormat( GL_RGB ),
   mPixelDataType( GL_UNSIGNED_BYTE ),
-  mWidth( nativeImageInterface->GetWidth() ),
-  mHeight( nativeImageInterface->GetHeight() ),
+  mWidth( static_cast<uint16_t >( nativeImageInterface->GetWidth() ) ), // ignoring overflow, not happening in practice
+  mHeight( static_cast<uint16_t >( nativeImageInterface->GetHeight() ) ), // ignoring overflow, not happening in practice
   mMaxMipMapLevel( 0 ),
   mType( TextureType::TEXTURE_2D ),
   mHasAlpha( nativeImageInterface->RequiresBlending() ),
@@ -741,14 +742,14 @@ void Texture::Initialize(Context& context)
     {
       if( !mIsCompressed )
       {
-        for( unsigned int i(0); i<6; ++i )
+        for( uint32_t i(0); i<6; ++i )
         {
           context.TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, mWidth, mHeight, 0, mGlFormat, mPixelDataType, 0 );
         }
       }
       else
       {
-        for( unsigned int i(0); i<6; ++i )
+        for( uint32_t i(0); i<6; ++i )
         {
           context.CompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, mWidth, mHeight, 0, 0, 0 );
         }
@@ -763,16 +764,16 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
   DALI_ASSERT_ALWAYS( mNativeImage == NULL );
 
   //Get pointer to the data of the PixelData object
-  unsigned char* buffer( pixelData->GetBuffer() );
+  uint8_t* buffer( pixelData->GetBuffer() );
 
   //This buffer is only used if manually converting from RGB to RGBA
-  unsigned char* tempBuffer(0);
+  uint8_t* tempBuffer(0);
 
   //Retrieves the pixel data element type, the gl format and gl internal format of the data contained in the PixelData object.
   GLenum glFormat;
   GLint glInternalFormat;
   GLenum pixelDataElementType;
-  PixelFormatToGl( static_cast<DevelPixel::Format>( pixelData->GetPixelFormat() ), glFormat, glInternalFormat, pixelDataElementType );
+  PixelFormatToGl( pixelData->GetPixelFormat(), glFormat, glInternalFormat, pixelDataElementType );
 
   //Get the maximum mipmap level to set GL_TEXTURE_MAX_LEVEL parameter in GLES3x because is not
   //necessary to upload all the mipmap levels
@@ -791,9 +792,9 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
 
   if( convert )
   {
-    size_t dataSize = static_cast< size_t >( params.width ) * params.height;
-    tempBuffer = new unsigned char[dataSize*4u];
-    for( size_t i(0u); i<dataSize; i++ )
+    uint32_t dataSize = static_cast< uint32_t >( params.width ) * params.height;
+    tempBuffer = new uint8_t[dataSize*4u];
+    for( uint32_t i = 0u; i < dataSize; ++i )
     {
       tempBuffer[i*4u]   = buffer[i*3u];
       tempBuffer[i*4u+1] = buffer[i*3u+1];
@@ -825,7 +826,7 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
     }
     else
     {
-      context.CompressedTexImage2D( target, params.mipmap, mGlInternalFormat, params.width, params.height, 0, pixelData->GetBufferSize(), buffer );
+      context.CompressedTexImage2D( target, params.mipmap, mGlInternalFormat, params.width, params.height, 0, static_cast<GLsizei>( pixelData->GetBufferSize() ), buffer );
     }
   }
   else
@@ -841,7 +842,7 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
     {
       context.CompressedTexSubImage2D( target, params.mipmap,
                                        params.xOffset, params.yOffset, params.width, params.height,
-                                       glFormat, pixelData->GetBufferSize(), buffer );
+                                       glFormat, static_cast<GLsizei>( pixelData->GetBufferSize() ), buffer );
     }
   }
 
@@ -850,7 +851,7 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
   delete[] tempBuffer;
 }
 
-bool Texture::Bind( Context& context, unsigned int textureUnit, Render::Sampler* sampler )
+bool Texture::Bind( Context& context, uint32_t textureUnit, Render::Sampler* sampler )
 {
   if( mNativeImage && mId == 0 )
   {
@@ -859,8 +860,7 @@ bool Texture::Bind( Context& context, unsigned int textureUnit, Render::Sampler*
 
   if( mId != 0 )
   {
-    context.ActiveTexture( static_cast<TextureUnit>(textureUnit) );
-    context.BindTexture( mTarget, mId );
+    context.BindTextureForUnit( static_cast<TextureUnit>( textureUnit ), mTarget, mId );
     ApplySampler( context, sampler );
 
     if( mNativeImage )