[Tizen] Add IsUploaded devel api in Texture
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture.cpp
index 99fca0d..9452866 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 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.
@@ -21,7 +21,7 @@
 #include <math.h>   //floor, log2
 
 // INTERNAL INCLUDES
-#include <dali/devel-api/images/native-image-interface-extension.h>
+
 
 namespace Dali
 {
@@ -517,6 +517,28 @@ void PixelFormatToGl( Pixel::Format pixelFormat, GLenum& glFormat, GLint& glInte
       break;
     }
 
+    // GLES 3.0 depth and stencil formats
+    case Pixel::DEPTH_UNSIGNED_INT:
+    {
+      glFormat = GL_DEPTH_COMPONENT;
+      pixelDataType = GL_UNSIGNED_INT;
+      break;
+    }
+
+    case Pixel::DEPTH_FLOAT:
+    {
+      glFormat = GL_DEPTH_COMPONENT;
+      pixelDataType = GL_FLOAT;
+      break;
+    }
+
+    case Pixel::DEPTH_STENCIL:
+    {
+      glFormat = GL_DEPTH_STENCIL;
+      pixelDataType = GL_UNSIGNED_INT_24_8;
+      break;
+    }
+
     case Pixel::INVALID:
     {
       DALI_LOG_ERROR( "Invalid pixel format for bitmap\n" );
@@ -533,6 +555,16 @@ void PixelFormatToGl( Pixel::Format pixelFormat, GLenum& glFormat, GLint& glInte
       glInternalFormat = GL_R11F_G11F_B10F;
       break;
     }
+    case Pixel::DEPTH_FLOAT:
+    {
+      glInternalFormat = GL_DEPTH_COMPONENT32F;
+      break;
+    }
+    case Pixel::DEPTH_STENCIL:
+    {
+      glInternalFormat = GL_DEPTH24_STENCIL8;
+      break;
+    }
     default:
     {
       glInternalFormat = glFormat;
@@ -568,6 +600,9 @@ bool IsCompressedFormat(Pixel::Format pixelFormat)
     case Pixel::BGRA8888:
     case Pixel::RGB16F:
     case Pixel::RGB32F:
+    case Pixel::DEPTH_UNSIGNED_INT:
+    case Pixel::DEPTH_FLOAT:
+    case Pixel::DEPTH_STENCIL:
     case Pixel::INVALID:
     {
       return false;
@@ -637,7 +672,8 @@ Texture::Texture( Type type, Pixel::Format format, ImageDimensions size )
   mMaxMipMapLevel( 0 ),
   mType( type ),
   mHasAlpha( HasAlpha( format ) ),
-  mIsCompressed( IsCompressedFormat( format ) )
+  mIsCompressed( IsCompressedFormat( format ) ),
+  mIsUploaded( false )
 {
   PixelFormatToGl( format,
                    mGlFormat,
@@ -658,12 +694,12 @@ Texture::Texture( NativeImageInterfacePtr nativeImageInterface )
   mMaxMipMapLevel( 0 ),
   mType( TextureType::TEXTURE_2D ),
   mHasAlpha( nativeImageInterface->RequiresBlending() ),
-  mIsCompressed( false )
+  mIsCompressed( false ),
+  mIsUploaded( false )
 {
 }
 
-Texture::~Texture()
-{}
+Texture::~Texture() = default;
 
 void Texture::Destroy( Context& context )
 {
@@ -673,7 +709,7 @@ void Texture::Destroy( Context& context )
 
     if( mNativeImage )
     {
-      mNativeImage->GlExtensionDestroy();
+      mNativeImage->DestroyResource();
     }
   }
 }
@@ -687,13 +723,9 @@ void Texture::Initialize(Context& context)
 {
   if( mNativeImage )
   {
-    if( mNativeImage->GlExtensionCreate() )
+    if( mNativeImage->CreateResource() )
     {
-      NativeImageInterface::Extension* extension = mNativeImage->GetExtension();
-      if( extension )
-      {
-        mTarget = extension->GetEglImageTextureTarget();
-      }
+      mTarget = mNativeImage->GetTextureTarget();
 
       context.GenTextures( 1, &mId );
       context.BindTexture( mTarget, mId );
@@ -709,7 +741,7 @@ void Texture::Initialize(Context& context)
       if( mNativeImage->TargetTexture() != 0u )
       {
         context.DeleteTextures( 1, &mId );
-        mNativeImage->GlExtensionDestroy();
+        mNativeImage->DestroyResource();
         mId = 0u;
       }
     }
@@ -731,11 +763,11 @@ void Texture::Initialize(Context& context)
     {
       if( !mIsCompressed )
       {
-        context.TexImage2D(GL_TEXTURE_2D, 0, mGlInternalFormat, mWidth, mHeight, 0, mGlFormat, mPixelDataType, 0 );
+        context.TexImage2D(GL_TEXTURE_2D, 0, mGlInternalFormat, mWidth, mHeight, 0, mGlFormat, mPixelDataType, nullptr );
       }
       else
       {
-        context.CompressedTexImage2D(GL_TEXTURE_2D, 0, mGlInternalFormat, mWidth, mHeight, 0, 0, 0 );
+        context.CompressedTexImage2D(GL_TEXTURE_2D, 0, mGlInternalFormat, mWidth, mHeight, 0, 0, nullptr );
       }
     }
     else if( mType == TextureType::TEXTURE_CUBE )
@@ -744,14 +776,14 @@ void Texture::Initialize(Context& context)
       {
         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 );
+          context.TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, mWidth, mHeight, 0, mGlFormat, mPixelDataType, nullptr );
         }
       }
       else
       {
         for( uint32_t i(0); i<6; ++i )
         {
-          context.CompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, mWidth, mHeight, 0, 0, 0 );
+          context.CompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, mWidth, mHeight, 0, 0, nullptr );
         }
       }
       context.TexParameteri( mTarget, GL_TEXTURE_WRAP_R, GL_WRAP_DEFAULT );
@@ -761,13 +793,13 @@ void Texture::Initialize(Context& context)
 
 void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::Texture::UploadParams& params  )
 {
-  DALI_ASSERT_ALWAYS( mNativeImage == NULL );
+  DALI_ASSERT_ALWAYS( mNativeImage == nullptr );
 
   //Get pointer to the data of the PixelData object
   uint8_t* buffer( pixelData->GetBuffer() );
 
   //This buffer is only used if manually converting from RGB to RGBA
-  uint8_t* tempBuffer(0);
+  std::vector< uint8_t > tempBuffer;
 
   //Retrieves the pixel data element type, the gl format and gl internal format of the data contained in the PixelData object.
   GLenum glFormat;
@@ -784,16 +816,11 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
                             ( params.width  != ( mWidth  / ( 1 << params.mipmap ) ) ) ||
                             ( params.height != ( mHeight / ( 1 << params.mipmap ) ) ) );
 
-  bool convert = ( ( glFormat == GL_RGB ) && ( mGlFormat == GL_RGBA ) );
-#if DALI_GLES_VERSION >= 30
-  // Don't convert manually from RGB to RGBA if GLES >= 3.0 and a sub-image is uploaded.
-  convert = convert && !isSubImage;
-#endif
-
-  if( convert )
+  if( context.TextureRequiresConverting( glFormat, mGlFormat, isSubImage ) )
   {
     uint32_t dataSize = static_cast< uint32_t >( params.width ) * params.height;
-    tempBuffer = new uint8_t[dataSize*4u];
+    //reserve() does not allocate the memory on some systems so can crash if not populated using push_back
+    tempBuffer.resize( dataSize * 4u );
     for( uint32_t i = 0u; i < dataSize; ++i )
     {
       tempBuffer[i*4u]   = buffer[i*3u];
@@ -802,7 +829,7 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
       tempBuffer[i*4u+3] = 0xFF;
     }
 
-    buffer = tempBuffer;
+    buffer = &tempBuffer[0];
     glFormat = mGlFormat; // Set the glFormat to GL_RGBA
   }
 
@@ -845,10 +872,7 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
                                        glFormat, static_cast<GLsizei>( pixelData->GetBufferSize() ), buffer );
     }
   }
-
-
-  //Destroy temp buffer used for conversion RGB->RGBA
-  delete[] tempBuffer;
+  mIsUploaded = true;
 }
 
 bool Texture::Bind( Context& context, uint32_t textureUnit, Render::Sampler* sampler )
@@ -914,18 +938,14 @@ void Texture::ApplySampler( Context& context, Render::Sampler* sampler )
       }
     }
 
-#if DALI_GLES_VERSION >= 30
-    //In GLES 3.0 we do not need to upload all of the mipmap levels, but GL_TEXTURE_MAX_LEVEL must be set
     if(mMaxMipMapLevel)
     {
       context.TexParameteri( mTarget, GL_TEXTURE_MAX_LEVEL, mMaxMipMapLevel );
     }
-#endif
-
   }
 }
 
-bool Texture::HasAlphaChannel()
+bool Texture::HasAlphaChannel() const
 {
   return mHasAlpha;
 }