Calulating stride for Android native image 84/247784/6
authorCheng-Shiun Tsai <cheng.tsai@samsung.com>
Mon, 16 Nov 2020 14:17:52 +0000 (14:17 +0000)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Fri, 20 Nov 2020 10:45:16 +0000 (10:45 +0000)
Android AHardwareBuffer_Desc.stride doesn't seem to mean width * pixelbytes like other OS
instead it appears to be width in pixels but with padding.
Adding clause to reflect this fact

Change-Id: I3aaeeea5436bb5be0e1d5defac1c40b214e8af22

dali/internal/imaging/android/native-image-source-impl-android.cpp

index 2a64bfb..1052d0b 100755 (executable)
@@ -183,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 )