Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / android / native-image-source-impl-android.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 4d4ee1b..1052d0b
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
 #include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
-#include <dali/devel-api/adaptor-framework/bitmap-saver.h>
-#include <dali/integration-api/render-surface.h>
+#include <dali/integration-api/adaptor-framework/render-surface-interface.h>
 #include <dali/internal/graphics/common/egl-image-extensions.h>
 #include <dali/internal/graphics/gles/egl-graphics.h>
 #include <dali/internal/adaptor/common/adaptor-impl.h>
 
+
+namespace
+{
+const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
+const char* SAMPLER_TYPE = "samplerExternalOES";
+}
+
+
 namespace Dali
 {
 
@@ -176,9 +183,30 @@ bool NativeImageSourceAndroid::GetPixels(std::vector<unsigned char>& pixbuf, uns
     return success;
   }
 
-  uint32_t size = bufferDescription.stride * bufferDescription.height;
-  pixbuf.resize( size );
-  memcpy( pixbuf.data(), buffer, size );
+  uint32_t pixelBytes = GetBytesPerPixel(pixelFormat);
+  if ( bufferDescription.stride < (pixelBytes * bufferDescription.width) )
+  {
+    //On Android device, bufferDescription.stride doesn't seem to mean (width * pixelbytes)
+    //in an actual case, (AHardwareBuffer_Desc) bufferDescription = (width = 1080, height = 1060, layers = 1, format = 1, usage = 306, stride = 1088, rfu0 = 0, rfu1 = 0)
+    //deal with situation
+    uint32_t dstStride = pixelBytes * bufferDescription.width;
+    uint32_t srcStride = pixelBytes * bufferDescription.stride;
+    uint32_t size = dstStride * bufferDescription.height;
+    pixbuf.resize( size );
+    //copy each row over
+    const unsigned char* ptrSrc = reinterpret_cast<const unsigned char*>(buffer);
+    unsigned char* ptrDst = pixbuf.data();
+    for (int y=0; y < bufferDescription.height; y++, ptrSrc += srcStride, ptrDst += dstStride )
+    {
+      memcpy( ptrDst, ptrSrc, dstStride );
+    }
+  }
+  else
+  {
+    uint32_t size = bufferDescription.stride * bufferDescription.height;
+    pixbuf.resize( size );
+    memcpy( pixbuf.data(), buffer, size );
+  }
 
   ret = AHardwareBuffer_unlock( mPixmap, NULL );
   if( ret != 0 )
@@ -191,19 +219,6 @@ bool NativeImageSourceAndroid::GetPixels(std::vector<unsigned char>& pixbuf, uns
   return success;
 }
 
-bool NativeImageSourceAndroid::EncodeToFile(const std::string& filename) const
-{
-  std::vector< unsigned char > pixbuf;
-  unsigned int width(0), height(0);
-  Pixel::Format pixelFormat;
-
-  if( GetPixels( pixbuf, width, height, pixelFormat ) )
-  {
-    return Dali::EncodeToFile( &pixbuf[0], filename, pixelFormat, width, height );
-  }
-  return false;
-}
-
 void NativeImageSourceAndroid::SetSource( Any source )
 {
   if( mPixmap )
@@ -231,12 +246,12 @@ bool NativeImageSourceAndroid::IsColorDepthSupported( Dali::NativeImageSource::C
   return true;
 }
 
-bool NativeImageSourceAndroid::GlExtensionCreate()
+bool NativeImageSourceAndroid::CreateResource()
 {
   // if the image existed previously delete it.
   if( mEglImageKHR != NULL )
   {
-    GlExtensionDestroy();
+    DestroyResource();
   }
 
   DALI_ASSERT_ALWAYS( mPixmap );
@@ -275,7 +290,7 @@ bool NativeImageSourceAndroid::GlExtensionCreate()
   return mEglImageKHR != NULL;
 }
 
-void NativeImageSourceAndroid::GlExtensionDestroy()
+void NativeImageSourceAndroid::DestroyResource()
 {
   mEglImageExtensions->DestroyImageKHR( mEglImageKHR );
 
@@ -293,6 +308,22 @@ void NativeImageSourceAndroid::PrepareTexture()
 {
 }
 
+int NativeImageSourceAndroid::GetTextureTarget() const
+{
+  return GL_TEXTURE_2D;
+}
+
+const char* NativeImageSourceAndroid::GetCustomFragmentPrefix() const
+{
+  return nullptr;
+}
+
+const char* NativeImageSourceAndroid::GetCustomSamplerTypename() const
+{
+  return nullptr;
+}
+
+
 void* NativeImageSourceAndroid::GetPixmapFromAny(Any pixmap) const
 {
   if( pixmap.Empty() )
@@ -308,12 +339,13 @@ void NativeImageSourceAndroid::GetPixmapDetails()
   // get the width, height and depth
   mBlendingRequired = false;
 
-  AHardwareBuffer_Desc* bufferDescription = nullptr;
-  AHardwareBuffer_describe( mPixmap, bufferDescription );
+  AHardwareBuffer_Desc bufferDescription;
+  memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
+  AHardwareBuffer_describe( mPixmap, &bufferDescription );
 
-  mWidth = bufferDescription->width;
-  mHeight = bufferDescription->height;
-  switch (bufferDescription->format)
+  mWidth = bufferDescription.width;
+  mHeight = bufferDescription.height;
+  switch (bufferDescription.format)
   {
   case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
     mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_32;
@@ -330,6 +362,46 @@ void NativeImageSourceAndroid::GetPixmapDetails()
   }
 }
 
+uint8_t* NativeImageSourceAndroid::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
+{
+  if( mPixmap )
+  {
+    AHardwareBuffer_Desc bufferDescription;
+    memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
+    AHardwareBuffer_describe( mPixmap, &bufferDescription );
+
+    void* buffer = NULL;
+    if( AHardwareBuffer_lock( mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer ) != 0 )
+    {
+      DALI_LOG_ERROR( "Failed to AHardwareBuffer_lock\n" );
+      return NULL;
+    }
+
+    stride = bufferDescription.stride;
+    width  = bufferDescription.width;
+    height = bufferDescription.height;
+
+    return static_cast< uint8_t* >( buffer );
+  }
+
+  return NULL;
+}
+
+
+bool NativeImageSourceAndroid::ReleaseBuffer()
+{
+  if( mPixmap )
+  {
+    if( AHardwareBuffer_unlock( mPixmap, NULL ) != 0 )
+    {
+      DALI_LOG_ERROR( "failed to AHardwareBuffer_unlock\n" );
+      return false;
+    }
+    return true;
+  }
+  return false;
+}
+
 } // namespace Adaptor
 
 } // namespace internal